Exemplo n.º 1
0
def test_convert_description_to_alfacase_with_empty_dict(
        datadir: Path) -> None:
    """
    Ensure that the conversion from a description into a Alfacase it's not generating empty dict.
    Since it's not a valid syntax for strictyaml resulting in an InconsistentIndentationDisallowed error
    """

    simple_case = case_description.CaseDescription(
        name="Simple Case",
        pipes=[
            case_description.PipeDescription(
                name="pipe",
                source="mass_source_inlet",
                target="pressure_outlet",
                segments=build_simple_segment(),
                profile=case_description.ProfileDescription(
                    x_and_y=case_description.XAndYDescription(
                        x=Array([0], "m"), y=Array([0], "m"))),
            )
        ],
    )
    simple_case_alfacase_content = convert_description_to_alfacase(simple_case)
    assert "wall_description: {}" not in simple_case_alfacase_content
    assert "tables: {}" not in simple_case_alfacase_content
    # Smoke check, ensures that the alfacase is loaded correctly without errors
    simple_case_alfacase_file = datadir / "simple_case.alfacase"
    simple_case_alfacase_file.write_text(data=simple_case_alfacase_content,
                                         encoding="UTF-8")
    loaded_alfacase = DescriptionDocument.from_file(simple_case_alfacase_file)

    assert loaded_alfacase.content["name"].data == simple_case.name
def test_check_profile_description(default_case):
    """
    Ensures that the ProfileDescription only accepts either XAndYDescription or a LengthAndElevationDescription

    Check 1: length_and_elevation and x_and_y are mutually exclusive
    """
    msg = (
        "length_and_elevation and x_and_y are mutually exclusive and you must configure only one of them, got "
        "length_and_elevation=LengthAndElevationDescription(length=Array(length, [0.0, 5.0, 7.0], m), elevation=Array(length, [1.0, 1.0, 1.0], m)) "
        "and x_and_y=XAndYDescription(x=Array(length, [1.0, 10.0, 100.0], m), y=Array(length, [42.0, 42.0, 42.0], m))"
    )
    with pytest.raises(ValueError, match=re.escape(msg)):
        case_description.ProfileDescription(
            length_and_elevation=case_description.
            LengthAndElevationDescription(length=Array([0.0, 5.0, 7.0], "m"),
                                          elevation=Array([1.0] * 3, "m")),
            x_and_y=case_description.XAndYDescription(
                x=Array([1.0, 10.0, 100.0], "m"), y=Array([42.0] * 3, "m")),
        )

    # Empty Profile is allowed.
    profile = case_description.ProfileDescription(length_and_elevation=None,
                                                  x_and_y=None)
    assert profile.length_and_elevation is None
    assert profile.x_and_y is None

    # Empty Array is allowed
    profile_2 = case_description.ProfileDescription(
        length_and_elevation=case_description.LengthAndElevationDescription(
            length=Array([], "m"), elevation=Array([], "m")),
        x_and_y=None,
    )
    assert profile_2.x_and_y is None
    assert profile_2.length_and_elevation.length.GetValues("m") == []
    assert profile_2.length_and_elevation.elevation.GetValues("m") == []
    def test_pvt_model_default_behavior(self, default_case, default_well):
        """
        1) CaseDescription should accept components that uses PvtModel as None while default_model is configured.
        2) default_model must be a valid pvt_model defined on pvt_models
        3) If default_model is None, all components that uses pvt_model must be assigined.
        """
        from alfasim_sdk._internal.constants import NodeCellType

        case = attr.evolve(
            default_case,
            pvt_models=attr.evolve(default_case.pvt_models,
                                   default_model="PVT2"),
            nodes=[
                case_description.NodeDescription(
                    name="Node 1",
                    node_type=NodeCellType.Pressure,
                    pvt_model=None)
            ],
            pipes=[
                case_description.PipeDescription(
                    name="Pipe 1",
                    pvt_model=None,
                    source="in",
                    target="out",
                    segments=build_simple_segment(),
                    profile=case_description.ProfileDescription(
                        x_and_y=case_description.XAndYDescription(
                            x=Array([0], "m"), y=Array([0], "m"))),
                )
            ],
            wells=[
                attr.evolve(
                    default_well,
                    pvt_model=None,
                    annulus=attr.evolve(default_well.annulus, pvt_model=None),
                )
            ],
        )
        # Check 1) If default_model assigned, elements that uses pvt_model can be None
        case.ensure_valid_references()
        assert case.pvt_models.default_model == "PVT2"
        assert case.nodes[0].pvt_model is None
        assert case.pipes[0].pvt_model is None
        assert case.wells[0].pvt_model is None
        assert case.wells[0].annulus.pvt_model is None

        # Check 2) default_model must be filled with a valid pvt_model
        case_with_invalid_default_pvt_model = attr.evolve(
            case,
            pvt_models=attr.evolve(case.pvt_models, default_model="Acme"))
def test_get_value_and_unit_from_length_and_elevation_description_and_xand_ydescription(
):
    """
    Ensure that GetValueAndUnit returns a pair of values along with their units.
    """
    length_and_elevation = case_description.LengthAndElevationDescription(
        length=Array([0.0, 5.0, 7.0], "m"), elevation=Array([1.0] * 3, "m"))
    assert list(length_and_elevation.iter_values_and_unit()) == [
        ((0.0, "m"), (1.0, "m")),
        ((5.0, "m"), (1.0, "m")),
        ((7.0, "m"), (1.0, "m")),
    ]
    x_and_y = case_description.XAndYDescription(x=Array([1.0, 10.0, 100.0],
                                                        "m"),
                                                y=Array([42.0] * 3, "m"))
    assert list(x_and_y.iter_values_and_unit()) == [
        ((1.0, "m"), (42.0, "m")),
        ((10.0, "m"), (42.0, "m")),
        ((100.0, "m"), (42.0, "m")),
    ]
    temperature=Scalar(1, "degC"),
    type=constants.PipeEnvironmentHeatTransferCoefficientModelType.WallsAndEnvironment,
    heat_transfer_coefficient=Scalar(1.0e50, "W/m2.K"),
    overall_heat_transfer_coefficient=Scalar(2, "W/m2.K"),
    fluid_velocity=Scalar(1, "m/s"),
)
ENVIRONMENT_DESCRIPTION = case_description.EnvironmentDescription(
    thermal_model=constants.PipeThermalModelType.SteadyState,
    reference_y_coordinate=Scalar(5.0, "m"),
    position_input_mode=constants.PipeThermalPositionInput.Tvd,
    md_properties_table=[ENVIRONMENT_PROPERTY_DESCRIPTION],
    tvd_properties_table=[ENVIRONMENT_PROPERTY_DESCRIPTION],
)

X_AND_Y_DESCRIPTION = case_description.XAndYDescription(
    x=Array([1, 2, 3], "m"), y=Array([4, 5, 6], "m")
)


PROFILE_DESCRIPTION_WITH_XY = case_description.ProfileDescription(
    x_and_y=X_AND_Y_DESCRIPTION
)
PIPE_DESCRIPTION = case_description.PipeDescription(
    environment=ENVIRONMENT_DESCRIPTION,
    equipment=EQUIPMENT_DESCRIPTION,
    initial_conditions=INITIAL_CONDITIONS_DESCRIPTION,
    name="pipe 1",
    profile=PROFILE_DESCRIPTION_WITH_XY,
    pvt_model="gavea",
    segments=PIPE_WALL_DESCRIPTION,
    source="mass_source_node",
def _empty_profile():
    """ Helper function to get a empty profile for tests. """
    return case_description.ProfileDescription(
        x_and_y=case_description.XAndYDescription(x=Array([0], "m"),
                                                  y=Array([0], "m")))