def test_timing_nothing(flopy_dis, modflow_lib_path): mf6 = XmiWrapper(lib_path=modflow_lib_path, working_directory=flopy_dis.sim_path, timing=True) # Write output to screen: mf6.set_int("ISTDOUTTOFILE", 0) totals = mf6.report_timing_totals() assert math.isclose(totals, 0.0)
def test_update(flopy_dis, modflow_lib_path): mf6 = XmiWrapper(lib_path=modflow_lib_path, working_directory=flopy_dis.sim_path) # Write output to screen: mf6.set_int("ISTDOUTTOFILE", 0) try: # Initialize mf6.initialize() # Advance model by single time step mf6.update() finally: mf6.finalize()
def test_prepare_solve(flopy_dis, modflow_lib_path): mf6 = XmiWrapper(lib_path=modflow_lib_path, working_directory=flopy_dis.sim_path) # Write output to screen: mf6.set_int("ISTDOUTTOFILE", 0) try: # Initialize mf6.initialize() # Prepare solve sol_id = 1 mf6.prepare_solve(sol_id) finally: mf6.finalize()
def test_double_initialize(flopy_dis, modflow_lib_path): mf6 = XmiWrapper(lib_path=modflow_lib_path, working_directory=flopy_dis.sim_path) # Write output to screen: mf6.set_int("ISTDOUTTOFILE", 0) try: # Run initialize mf6.initialize() # Test if initialize fails, if initialize was called a second time with pytest.raises(InputError): mf6.initialize() finally: mf6.finalize()
def test_deactivated_timing(flopy_dis, modflow_lib_path): mf6 = XmiWrapper(lib_path=modflow_lib_path, working_directory=flopy_dis.sim_path, timing=False) # Write output to screen: mf6.set_int("ISTDOUTTOFILE", 0) try: # Run initialize mf6.initialize() with pytest.raises(TimerError): mf6.report_timing_totals() finally: mf6.finalize()
def test_get_var_type_int(flopy_dis, modflow_lib_path): mf6 = XmiWrapper(lib_path=modflow_lib_path, working_directory=flopy_dis.sim_path) # Write output to screen: mf6.set_int("ISTDOUTTOFILE", 0) try: # Initialize mf6.initialize() iactive_tag = mf6.get_var_address("IACTIVE", "SLN_1") var_type = mf6.get_var_type(iactive_tag) assert var_type == "INTEGER (90)" finally: mf6.finalize()
def test_get_var_type_double(flopy_dis, modflow_lib_path): mf6 = XmiWrapper(lib_path=modflow_lib_path, working_directory=flopy_dis.sim_path) # Write output to screen: mf6.set_int("ISTDOUTTOFILE", 0) try: # Initialize mf6.initialize() head_tag = mf6.get_var_address("X", "SLN_1") var_type = mf6.get_var_type(head_tag) assert var_type == "DOUBLE (90)" finally: mf6.finalize()
def test_get_var_address(flopy_dis, modflow_lib_path): mf6 = XmiWrapper(lib_path=modflow_lib_path, working_directory=flopy_dis.sim_path) # Write output to screen: mf6.set_int("ISTDOUTTOFILE", 0) try: # Initialize mf6.initialize() head_tag = mf6.get_var_address("X", "SLN_1") assert head_tag == "SLN_1/X" # with lowercase should work too k11_tag = mf6.get_var_address("k11", flopy_dis.model_name.lower(), "NPF") assert k11_tag == flopy_dis.model_name.upper() + "/NPF/K11" finally: mf6.finalize()
def init_bmi(self): self.simpath = Path(self.dialog.simpath) self.bmi_dll = XmiWrapper( lib_path=str(self.dialog.dllpath), working_directory=str(self.simpath), ) self.bmi_dll.initialize() self.get_model_names() self.bmi_states = [] for model_name in self.model_names: self.bmi_states.append(Bmi.get_bmi(self.bmi_dll, model_name)) self.box_modelname.addItem(model_name) self.box_modelname.setEnabled(True) self.widget_input_var_name.setEnabled(True) self.widget_input_component_name.setEnabled(True)
def test_initialize(flopy_dis, modflow_lib_path): mf6 = XmiWrapper(lib_path=modflow_lib_path, working_directory=flopy_dis.sim_path) # Write output to screen: mf6.set_int("ISTDOUTTOFILE", 0) try: # Run initialize mf6.initialize() finally: mf6.finalize()
def test_get_subcomponent_count(flopy_dis, modflow_lib_path): mf6 = XmiWrapper(lib_path=modflow_lib_path, working_directory=flopy_dis.sim_path) # Write output to screen: mf6.set_int("ISTDOUTTOFILE", 0) try: # Initialize mf6.initialize() n_solutions = mf6.get_subcomponent_count() # Modflow 6 BMI does only support the use of a single solution groups assert n_solutions == 1 finally: mf6.finalize()
def test_get_value_int_scalar(flopy_dis_idomain, modflow_lib_path): mf6 = XmiWrapper(lib_path=modflow_lib_path, working_directory=flopy_dis_idomain.sim_path) try: mf6.initialize() # get scalar variable: id_tag = next(var for var in mf6.get_output_var_names() if var.endswith("/ID")) assert mf6.get_var_rank(id_tag) == 0 tgt = mf6.get_value(id_tag) # compare with value in MODFLOW memory: orig = mf6.get_value_ptr(id_tag) assert np.array_equal(tgt, orig) finally: mf6.finalize()
def test_get_value_int(flopy_dis_idomain, modflow_lib_path): mf6 = XmiWrapper(lib_path=modflow_lib_path, working_directory=flopy_dis_idomain.sim_path) try: mf6.initialize() nodes_reduced_tag = next(var for var in mf6.get_output_var_names() if var.endswith("/NODEREDUCED")) tgt_arr = mf6.get_value(nodes_reduced_tag) # compare to array in MODFLOW memory: orig_arr = mf6.get_value_ptr(nodes_reduced_tag) assert np.array_equal(tgt_arr, orig_arr) finally: mf6.finalize()
def test_get_value_double(flopy_dis, modflow_lib_path): mf6 = XmiWrapper(lib_path=modflow_lib_path, working_directory=flopy_dis.sim_path) try: mf6.initialize() some_output_var = next(var for var in mf6.get_output_var_names() if var.endswith("/X")) copy_arr = mf6.get_value(some_output_var) # compare to array in MODFLOW memory: orig_arr = mf6.get_value_ptr(some_output_var) assert np.array_equal(copy_arr, orig_arr) finally: mf6.finalize()
def test_timing_initialize(flopy_dis, modflow_lib_path): mf6 = XmiWrapper(lib_path=modflow_lib_path, working_directory=flopy_dis.sim_path, timing=True) # Write output to screen: mf6.set_int("ISTDOUTTOFILE", 0) try: # Run initialize mf6.initialize() total = mf6.report_timing_totals() assert total > 0.0 finally: mf6.finalize()
def test_get_start_time(flopy_dis, modflow_lib_path): mf6 = XmiWrapper(lib_path=modflow_lib_path, working_directory=flopy_dis.sim_path) # Write output to screen: mf6.set_int("ISTDOUTTOFILE", 0) try: # Initialize mf6.initialize() # prescribed_start_time for modflow models is always 0 prescribed_start_time = 0.0 actual_start_time = mf6.get_start_time() assert math.isclose(prescribed_start_time, actual_start_time) finally: mf6.finalize()
def test_get_output_item_count(flopy_dis, modflow_lib_path): mf6 = XmiWrapper(lib_path=modflow_lib_path, working_directory=flopy_dis.sim_path) try: # Initialize mf6.initialize() assert mf6.get_output_item_count() > 0 finally: mf6.finalize()
def test_get_end_time(flopy_dis, modflow_lib_path): mf6 = XmiWrapper(lib_path=modflow_lib_path, working_directory=flopy_dis.sim_path) # Write output to screen: mf6.set_int("ISTDOUTTOFILE", 0) try: # Initialize mf6.initialize() prescribed_end_time = 0.0 for perlen, _, _ in flopy_dis.tdis_rc: prescribed_end_time += perlen actual_end_time = mf6.get_end_time() assert math.isclose(prescribed_end_time, actual_end_time) finally: mf6.finalize()
def test_get_input_var_names(flopy_dis, modflow_lib_path): mf6 = XmiWrapper(lib_path=modflow_lib_path, working_directory=flopy_dis.sim_path) try: # Initialize mf6.initialize() var_names = mf6.get_input_var_names() assert "TEST_MODEL_DIS/X" in var_names finally: mf6.finalize()
def test_get_grid_face_edges(flopy_dis, modflow_lib_path): """Expects to be implemented as soon as `get_grid_face_edges` is implemented""" mf6 = XmiWrapper(lib_path=modflow_lib_path, working_directory=flopy_dis.sim_path) # Write output to screen: mf6.set_int("ISTDOUTTOFILE", 0) with pytest.raises(NotImplementedError): mf6.get_grid_face_edges(1, np.zeros((1, 1)))
def test_finalize_without_initialize(flopy_dis, modflow_lib_path): mf6 = XmiWrapper(lib_path=modflow_lib_path, working_directory=flopy_dis.sim_path) # Write output to screen: mf6.set_int("ISTDOUTTOFILE", 0) # Test if finalize fails, if initialize was not called yet with pytest.raises(InputError): mf6.finalize()
def test_get_var_location(flopy_dis, modflow_lib_path): """Expects to be implemented as soon as `get_var_location` is implemented""" mf6 = XmiWrapper(lib_path=modflow_lib_path, working_directory=flopy_dis.sim_path) # Write output to screen: mf6.set_int("ISTDOUTTOFILE", 0) with pytest.raises(NotImplementedError): mf6.get_var_location("")
def test_get_output_var_names(flopy_dis, modflow_lib_path): mf6 = XmiWrapper(lib_path=modflow_lib_path, working_directory=flopy_dis.sim_path) try: # Initialize mf6.initialize() var_names = mf6.get_output_var_names() assert "TEST_MODEL_DIS/X" in var_names # this is readwrite assert "SLN_1/IA" in var_names # and this is readonly finally: mf6.finalize()
def bmifunc(exe, idx, model_ws=None): success = False name = ex[idx].upper() init_wd = os.path.abspath(os.getcwd()) if model_ws is not None: os.chdir(model_ws) mf6_config_file = os.path.join(model_ws, "mfsim.nam") try: mf6 = XmiWrapper(exe) except Exception as e: print("Failed to load " + exe) print("with message: " + str(e)) return bmi_return(success, model_ws) # initialize the model try: mf6.initialize(mf6_config_file) except: return bmi_return(success, model_ws) # time loop current_time = mf6.get_current_time() end_time = mf6.get_end_time() # maximum outer iterations mxit_tag = mf6.get_var_address("MXITER", "SLN_1") max_iter = mf6.get_value(mxit_tag) # get copy of recharge array rch_tag = mf6.get_var_address("BOUND", name, "RCHA") new_recharge = mf6.get_value(rch_tag) # model time loop idx = 0 while current_time < end_time: # get dt and prepare for non-linear iterations dt = mf6.get_time_step() mf6.prepare_time_step(dt) # convergence loop kiter = 0 mf6.prepare_solve(1) # update recharge new_recharge[:, 0] = rch_spd[idx] * area mf6.set_value(rch_tag, new_recharge) while kiter < max_iter: has_converged = mf6.solve(1) kiter += 1 if has_converged: msg = ("Component {}".format(1) + " converged in {}".format(kiter) + " outer iterations") print(msg) break if not has_converged: return bmi_return(success, model_ws) # finalize time step mf6.finalize_solve(1) # finalize time step and update time mf6.finalize_time_step() current_time = mf6.get_current_time() # increment counter idx += 1 # cleanup try: mf6.finalize() success = True except: return bmi_return(success, model_ws) if model_ws is not None: os.chdir(init_wd) # cleanup and return return bmi_return(success, model_ws)
def test_dependencies(flopy_dis, modflow_lib_path): XmiWrapper( lib_path=modflow_lib_path, lib_dependency=modflow_lib_path, working_directory=flopy_dis.sim_path, )
def test_get_component_name(flopy_dis, modflow_lib_path): mf6 = XmiWrapper(lib_path=modflow_lib_path, working_directory=flopy_dis.sim_path) assert mf6.get_component_name() == "MODFLOW 6"
def test_get_grid_face_nodes(flopy_disu, modflow_lib_path): assert True return # todo: fix this test """Tests if the grid_face_nodes can be extracted""" mf6 = XmiWrapper(lib_path=modflow_lib_path, working_directory=flopy_disu.sim_path) # Write output to screen: mf6.set_int("ISTDOUTTOFILE", 0) try: # Initialize mf6.initialize() # First 5 prescribed elements prescribed_grid_face_nodes = np.array([1, 2, 6, 5, 1]) # Getting the grid id from the model, requires specifying one variable k11_tag = mf6.get_var_address("K11", flopy_disu.model_name, "NPF") grid_id = mf6.get_var_grid(k11_tag) grid_face_count = mf6.get_grid_face_count(grid_id) grid_nodes_per_face = np.empty( shape=(grid_face_count,), dtype=np.int32, order="F" ) mf6.get_grid_nodes_per_face(grid_id, grid_nodes_per_face) face_nodes_count = np.sum(grid_nodes_per_face + 1) actual_grid_face_nodes = np.empty( shape=(face_nodes_count,), dtype=np.int32, order="F" ) mf6.get_grid_face_nodes(grid_id, actual_grid_face_nodes) assert np.array_equal(prescribed_grid_face_nodes, actual_grid_face_nodes[:5]) finally: mf6.finalize()
def test_get_grid_face_count(flopy_disu, modflow_lib_path): """Tests if the grid_face_count can be extracted""" mf6 = XmiWrapper(lib_path=modflow_lib_path, working_directory=flopy_disu.sim_path) # Write output to screen: mf6.set_int("ISTDOUTTOFILE", 0) try: # Initialize mf6.initialize() prescribed_grid_face_count = flopy_disu.nrow * flopy_disu.ncol # Getting the grid id from the model, requires specifying one variable k11_tag = mf6.get_var_address("K11", flopy_disu.model_name, "NPF") grid_id = mf6.get_var_grid(k11_tag) actual_grid_face_count = mf6.get_grid_face_count(grid_id) assert prescribed_grid_face_count == actual_grid_face_count finally: mf6.finalize()
def test_get_grid_nodes_per_face(flopy_disu, modflow_lib_path): """Tests if the grid_nodes_per_face can be extracted""" mf6 = XmiWrapper(lib_path=modflow_lib_path, working_directory=flopy_disu.sim_path) # Write output to screen: mf6.set_int("ISTDOUTTOFILE", 0) try: # Initialize mf6.initialize() # Rectangular grid -> nrow*ncol faces with 4 nodes each prescribed_nodes_per_face = np.full(flopy_disu.nrow * flopy_disu.ncol, 4) # Getting the grid id from the model, requires specifying one variable k11_tag = mf6.get_var_address("K11", flopy_disu.model_name, "NPF") grid_id = mf6.get_var_grid(k11_tag) face_count = mf6.get_grid_face_count(grid_id) actual_nodes_per_face = np.empty(shape=(face_count,), dtype=np.int32, order="F") mf6.get_grid_nodes_per_face(grid_id, actual_nodes_per_face) assert np.array_equal(prescribed_nodes_per_face, actual_nodes_per_face) finally: mf6.finalize()
def bmifunc(exe, idx, model_ws=None): success = False name = ex[idx].upper() init_wd = os.path.abspath(os.getcwd()) if model_ws is not None: os.chdir(model_ws) mf6_config_file = os.path.join(model_ws, 'mfsim.nam') try: mf6 = XmiWrapper(exe) except Exception as e: print("Failed to load " + exe) print("with message: " + str(e)) return bmi_return(success, model_ws) # initialize the model try: mf6.initialize(mf6_config_file) except: return bmi_return(success, model_ws) # time loop current_time = mf6.get_current_time() end_time = mf6.get_end_time() # get pointer to simulated heads head_tag = mf6.get_var_address("X", "LIBGWF_EVT01") head = mf6.get_value_ptr(head_tag) # maximum outer iterations mxit_tag = mf6.get_var_address("MXITER", "SLN_1") max_iter = mf6.get_value(mxit_tag) # get copy of well data well_tag = mf6.get_var_address("BOUND", name, "WEL_0") well = mf6.get_value(well_tag) twell = np.zeros(ncol, dtype=np.float64) # model time loop idx = 0 while current_time < end_time: # get dt and prepare for non-linear iterations dt = mf6.get_time_step() mf6.prepare_time_step(dt) # convergence loop kiter = 0 mf6.prepare_solve(1) while kiter < max_iter: # update well rate twell[:] = head2et_wellrate(head[0]) well[:, 0] = twell[:] mf6.set_value(well_tag, well) # solve with updated well rate has_converged = mf6.solve(1) kiter += 1 if has_converged: msg = "Component {}".format(1) + \ " converged in {}".format(kiter) + " outer iterations" print(msg) break if not has_converged: return bmi_return(success, model_ws) # finalize time step mf6.finalize_solve(1) # finalize time step and update time mf6.finalize_time_step() current_time = mf6.get_current_time() # increment counter idx += 1 # cleanup try: mf6.finalize() success = True except: return bmi_return(success, model_ws) if model_ws is not None: os.chdir(init_wd) # cleanup and return return bmi_return(success, model_ws)