def test_dimr_model(): test_file = ( test_data_dir / "input" / "e02" / "c11_korte-woerden-1d" / "dimr_model" / "dimr_config.xml" ) # Confirm parsing results in correct # components for each type of submodel d = DIMR(filepath=test_file) assert len(d.component) == 2 assert isinstance(d.component[0], RRComponent) assert isinstance(d.component[1], FMComponent) # Confirm saving creates new files and # files for child model save_path = test_output_dir / test_dimr_model.__name__ / DIMR._generate_name() d.save(filepath=save_path, recurse=True) assert d.save_location == save_path assert d.save_location.is_file() assert d.component[1].model is not None assert d.component[1].model.save_location.is_file()
def test_loading_a_file_twice_returns_different_model_instances(self) -> None: # If the same source file is read multiple times, we expect that # multiple (deep) copies are returned, and not references to the # same object. test_file = ( test_input_dir / "e02" / "c11_korte-woerden-1d" / "dimr_model" / "dimr_config.xml" ) model_a = DIMR(test_file) model_b = DIMR(test_file) assert model_a is not model_b
def test_cache_with_state_returns_correct_result(self): cache = FileModelCache() path = Path.cwd() / "some-dimr.xml" model = DIMR() # empty subclass of the FileModel cache.register_model(path, model) assert cache.retrieve_model(path) is model
def test_retrieve_model_is_always_determined_from_the_absolute_path( self, register_path: Path, retrieval_path: Path, ): context = FileLoadContext() model = DIMR() context.register_model(register_path, model) assert context.retrieve_model(retrieval_path) is model
def test_existing_model_saved_docker(): test_file = (test_input_dir / "e02" / "f101_1D-boundaries" / "c01_steady-state-flow" / "dimr_config.xml") dimr = DIMR(filepath=test_file) output_dir = test_output_dir / "docker" save_path = output_dir / "dimr_config.xml" dimr.save(filepath=save_path, recurse=True) assert (output_dir / "Boundary_net.nc").is_file() assert (output_dir / "Boundary.mdu").is_file() # assert (output_dir / "observcrs.ini").is_file() # Only possible once issue #159 is done assert (output_dir / "Boundary.ext").is_file() assert (output_dir / "BoundaryConditions.bc").is_file() assert (output_dir / "CrossSectionLocations.ini").is_file() assert (output_dir / "CrossSectionDefinitions.ini").is_file() assert (output_dir / "roughness-Main.ini").is_file() assert save_path.is_file()
def test_retrieve_model_with_relative_path_returns_correct_result( self, path: Path, ): context = FileLoadContext() model = DIMR() context.register_model(path, model) retrieved_model = context.retrieve_model(path) assert retrieved_model is model
def test_dimr_validate(): d = DIMR( coupler={ "name": "test", "sourcecomponent": "test", "targetcomponent": "test", "item": [], } ) assert isinstance(d.coupler, list) assert len(d.coupler) == 1
def test_read_dimr_missing_coupler_field_raises_correct_error(): file = "missing_dimr_coupler_field.xml" identifier = "rr_to_flow" field = "targetcomponent" filepath = invalid_test_data_dir / file with pytest.raises(ValidationError) as error: DIMR(filepath) expected_message = f"{file} -> coupler -> 0 -> {identifier} -> {field}" assert expected_message in str(error.value)
def test_read_dimr_missing_component_field_raises_correct_error(): file = "missing_dimr_component_field.xml" identifier = "FlowFM" field = "workingdir" filepath = invalid_test_data_dir / file with pytest.raises(ValidationError) as error: DIMR(filepath) expected_message = f"{file} -> component -> 1 -> {identifier} -> {field}" assert expected_message in str(error.value)
def test_from_scratch_docker(): # TODO Make a valid model from scratch dimr = DIMR() fm = FMModel() fm.filepath = Path("test.mdu") # debug(fm.geometry.netfile) # manipulate the model # Start adding geometry # fm.geometry.netfile.network.mesh1d_add_branch() # Add some structures, note this is invalid because it doesn't # have a branch or coordinates yet, but it will work for demo purposes # Note: either branchid + chainage or coordinates are mandatory for the structures. struc = Weir( allowedflowdir=FlowDirection.none, crestlevel=0.0, branchid="aBranchId", chainage=4.2, ) struc.comments.crestlevel = "This is a comment" fm.geometry.structurefile = [StructureModel(structure=[struc])] # add FM as DIMR component and save to *folder* dimr.component.append( FMComponent(name="test", workingDir=".", inputfile=fm.filepath, model=fm)) dimr.control.append(Start(name="test")) output_dir = test_output_dir / "docker" save_path = test_output_dir / "docker" / "dimr_config.xml" dimr.save(filepath=save_path, recurse=True) assert (output_dir / "network.nc").is_file() assert (output_dir / "test.mdu").is_file() assert (output_dir / "structures.ini").is_file() assert save_path.is_file()
def test_parse_rr_model_returns_correct_model(): test_file = test_input_dir / "rr_sample_trimmed" / "dimr_config.xml" result = DIMR(filepath=test_file) assert len(result.component) == 1 model = result.component[0].model assert isinstance(model, RainfallRunoffModel) # verify some non-default names altered in the source file. assert model.control_file == Path("not-delft_3b.ini") expected_bui_model = BuiTestData.bui_model() # we expect the path to not be absolute, as such we need to adjust that. expected_bui_model.filepath = Path(expected_bui_model.filepath.name) assert model.bui_file == expected_bui_model assert model.rr_ascii_restart_openda == Path("ASCIIRestartOpenDA.txt")
def test_initialize_default_dimr_does_not_raise_exception(): DIMR()
def test_dimr_model_save(): file = Path(test_output_dir / "model" / "test_dimr_model_save.xml") reference_file = Path(test_reference_dir / "model" / "test_dimr_model_save.xml") dimr = DIMR() dimr.documentation.creationDate = datetime(2021, 7, 29, 12, 45) dimr.control.append( Parallel( startGroup=StartGroup( time="0 60 7200", start=ComponentOrCouplerRef(name="Rainfall Runoff"), coupler=ComponentOrCouplerRef(name="rr_to_flow"), ), start=ComponentOrCouplerRef(name="FlowFM"), ) ) dimr.component.append( RRComponent( name="Rainfall Runoff", library="rr_dll", workingDir="rr", inputFile="Sobek_3b.fnm", ) ) dimr.component.append( FMComponent( name="FlowFM", library="dflowfm", workingDir="dflowfm", inputFile="FlowFM.mdu", ) ) dimr.coupler.append( Coupler( name="rr_to_flow", sourceComponent="Rainfall Runoff", targetComponent="FlowFM", logger=Logger(workingDir=".", outputFile="rr_to_flow.nc"), ) ) dimr.coupler[0].item.append( CoupledItem( sourceName="catchments/10634/water_discharge", targetName="laterals/10634/water_discharge", ) ) dimr.coupler[0].item.append( CoupledItem( sourceName="catchments/10635/water_discharge", targetName="laterals/10635/water_discharge", ) ) dimr.filepath = file dimr.save() assert file.is_file() == True assert_files_equal(file, reference_file)