def test_remove_redundant_input_type_data_option( remove: bool, input_type: MultiInputType) -> None: mass_source_equipment_description = case_description.MassSourceEquipmentDescription( position=Scalar(0, "m"), temperature_input_type=input_type) yaml = convert_description_to_alfacase( mass_source_equipment_description, remove_redundant_input_type_data=remove) def has_key_in_yaml(key: str) -> bool: pattern = rf"^\s*{re.escape(key)}\s*:" return bool(re.search(pattern, yaml, re.MULTILINE)) if remove: assert not has_key_in_yaml("temperature_input_type") if input_type == MultiInputType.Constant: assert has_key_in_yaml("temperature") assert not has_key_in_yaml("temperature_curve") elif input_type == MultiInputType.Curve: assert not has_key_in_yaml("temperature") assert has_key_in_yaml("temperature_curve") else: assert has_key_in_yaml("temperature_input_type") assert has_key_in_yaml("temperature") assert has_key_in_yaml("temperature_curve")
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 generate_description(self, alfacase_config: AlfacaseTestConfig): """ Helper method to generate a "Description" from the given alfacase_config """ alfacase_string = convert_description_to_alfacase( alfacase_config.description_expected ) alfacase_content = strictyaml.dirty_load( yaml_string=alfacase_string, schema=alfacase_config.schema, allow_flow_style=True, ) # 'LoadPvtModelsDescription' is special case and the DescriptionDocument doesn't need a FakeKey skip_dict = ( alfacase_config.load_function_name == "load_pvt_models_description" ) if alfacase_config.is_sequence: alfacase_content = [alfacase_content] elif alfacase_config.is_dict and not skip_dict: alfacase_content = YAML( CommentedMap({YAML("FakeKey"): alfacase_content}) ) description_document = DescriptionDocument( content=alfacase_content, file_path=self.tmp_path / "test_case.alfacase" ) return getattr(alfacase_to_case, alfacase_config.load_function_name)( description_document )
def test_convert_description_to_alfacase_with_nan_float(): """ Ensure that NaN is generated as `.nan` instead of `nan`, and '.inf' instead of `inf` because of YAML specification 1.2 that only accepts `.nan` and `.inf`, `+.inf`, `-.inf`. """ simple_case = case_description.CaseDescription( numerical_options=NumericalOptionsDescription( tolerance=float("inf"), relaxed_tolerance=float("nan"))) simple_case_alfacase_content = convert_description_to_alfacase(simple_case) assert "relaxed_tolerance: .nan" in simple_case_alfacase_content assert "tolerance: .inf" in simple_case_alfacase_content simple_case = case_description.CaseDescription( numerical_options=NumericalOptionsDescription( tolerance=float("+inf"), relaxed_tolerance=float("-inf"))) simple_case_alfacase_content = convert_description_to_alfacase(simple_case) assert "tolerance: .inf" in simple_case_alfacase_content assert "relaxed_tolerance: -.inf" in simple_case_alfacase_content
def generate_alfatable_file(alfacase_file, alfatable_filename, description): """ Create `.alfatable` file for the given description. """ from boltons.strutils import slugify from alfasim_sdk import convert_description_to_alfacase alfatable_content = convert_description_to_alfacase( description, enable_flow_style_on_numpy=True) alfatable_file = ( alfacase_file.parent / f"{alfacase_file.stem}.{slugify(alfatable_filename)}.alfatable") alfatable_file.write_text(alfatable_content, encoding="utf-8") return alfatable_file
def test_convert_alfacase_to_description_restart_file_path(tmp_path): """ Round-trip test with a description that has a Path as type. - YAML representation should be a Str() - CaseDescription should be a pathlib.Path """ alfacase_file = tmp_path / "test_case.yaml" some_folder = tmp_path / "some_folder" some_folder.mkdir() restart_file = some_folder / "restart.state" restart_file.touch() physics_with_restart_file = attr.evolve( filled_case_descriptions.PHYSICS_DESCRIPTION, restart_filepath=restart_file ) alfacase_string = convert_description_to_alfacase(physics_with_restart_file) restart_file_relative_path = restart_file.relative_to(alfacase_file.parent) assert f"restart_filepath: {restart_file.absolute()}" in alfacase_string alfacase_string = alfacase_string.replace( f"restart_filepath: {restart_file.absolute()}", f"restart_filepath: {restart_file_relative_path}", ) alfacase_content = strictyaml.dirty_load( yaml_string=alfacase_string, schema=schema.physics_description_schema, allow_flow_style=True, ) assert isinstance(alfacase_content["restart_filepath"].value, str) assert alfacase_content["restart_filepath"].value == str( Path("some_folder/restart.state") ) description_document = DescriptionDocument( content=alfacase_content, file_path=alfacase_file ) physics_description = load_physics_description(description_document) assert physics_description.restart_filepath == restart_file
def generate_description( self, alfacase_config: AlfacaseTestConfig, remove_redundant_input_type_data: bool = False, ): """ Helper method to generate a "Description" from the given alfacase_config """ alfacase_string = convert_description_to_alfacase( alfacase_config.description_expected, remove_redundant_input_type_data= remove_redundant_input_type_data, ) alfacase_content = strictyaml.dirty_load( yaml_string=alfacase_string, schema=alfacase_config.schema, allow_flow_style=True, ) # 'LoadPvtModelsDescription' is special case and the DescriptionDocument doesn't need a FakeKey skip_dict = (alfacase_config.load_function_name == "load_pvt_models_description") if alfacase_config.is_sequence: alfacase_content = [alfacase_content] elif alfacase_config.is_dict and not skip_dict: alfacase_content = YAML( CommentedMap({YAML("FakeKey"): alfacase_content})) description_document = DescriptionDocument( content=alfacase_content, file_path=self.tmp_path / "test_case.alfacase") if hasattr(alfacase_to_case, alfacase_config.load_function_name): loader = getattr(alfacase_to_case, alfacase_config.load_function_name) else: loader = alfacase_to_case.get_instance_loader( class_=alfacase_config.description_expected.__class__) return loader(description_document)