def test_gnu_make(): os.makedirs(cpth, exist_ok=True) target = "triangle" pm = pymake.Pymake(verbose=True) pm.makefile = True pm.makeclean = True pm.cc = "gcc" if sys.platform.lower() == "win32": target += ".exe" # get current directory cwd = os.getcwd() # change to working directory so triangle download directory is # a subdirectory in the working directory os.chdir(cpth) # build triangle and makefile assert (pymake.build_apps( target, clean=False, pymake_object=pm) == 0), "could not build {}".format(target) if os.path.isfile(os.path.join(cpth, "makefile")): print("cleaning with GNU make") # clean prior to make print("clean {} with makefile".format(target)) success, buff = flopy.run_model( "make", None, cargs="clean", model_ws=cpth, report=True, normal_msg="rm -rf ./triangle", silent=False, ) # build triangle with makefile if success: print("build {} with makefile".format(target)) success, buff = flopy.run_model( "make", None, model_ws=cpth, report=True, normal_msg="cc -O2 -o triangle ./obj_temp/triangle.o", silent=False, ) # finalize Pymake object pm.finalize() # return to starting directory os.chdir(cwd) assert os.path.isfile(os.path.join( cpth, target)), "could not build {} with makefile".format(target) return
def run_mf2005(namefile, regression=True): """ Run the simulation. """ # Set root as the directory name where namefile is located testname = pymake.get_sim_name(namefile, rootpth=config.testpaths[0])[0] # Set nam as namefile name without path nam = os.path.basename(namefile) # Setup testpth = os.path.join(config.testdir, testname) pymake.setup(namefile, testpth) # run test models print('running model...{}'.format(testname)) exe_name = os.path.abspath(config.target) success, buff = flopy.run_model(exe_name, nam, model_ws=testpth, silent=True) # If it is a regression run, then setup and run the model with the # release target and the reference target success_reg = True if regression: testname_reg = os.path.basename(config.target_release) testpth_reg = os.path.join(testpth, testname_reg) pymake.setup(namefile, testpth_reg) print('running regression model...{}'.format(testname_reg)) exe_name = os.path.abspath(config.target_release) success_reg, buff = flopy.run_model(exe_name, nam, model_ws=testpth_reg, silent=True) if success_reg: outfile1 = os.path.join( os.path.split(os.path.join(testpth, nam))[0], 'bud.cmp') outfile2 = os.path.join( os.path.split(os.path.join(testpth, nam))[0], 'hds.cmp') success_reg = pymake.compare(os.path.join(testpth, nam), os.path.join(testpth_reg, nam), precision='single', max_cumpd=0.01, max_incpd=0.01, htol=0.001, outfile1=outfile1, outfile2=outfile2) # Clean things up if success and success_reg and not config.retain: pymake.teardown(testpth) assert success, 'model did not run' assert success_reg, 'regression model did not meet comparison criteria' return
def run_modpath7(fn): model_ws = os.path.dirname(fn) # run the flow model run = True if 'modflow-2005' in fn.lower(): exe = emf2005 v = flopy.which(exe) if v is None: run = False nam = [name for name in os.listdir(model_ws) if '.nam' in name.lower()] if len(nam) > 0: fpth = nam[0] # read and rewrite the name file set_lowercase(os.path.join(model_ws, fpth)) else: fpth = None run = False elif 'modflow-usg' in fn.lower(): exe = emfusg v = flopy.which(exe) if v is None: run = False nam = [name for name in os.listdir(model_ws) if '.nam' in name.lower()] if len(nam) > 0: fpth = nam[0] else: fpth = None run = False elif 'modflow-6' in fn.lower(): exe = emf6 v = flopy.which(exe) if v is None: run = False fpth = None else: run = False if run: # fix any known problems replace_data(model_ws) # run the model msg = '{}'.format(exe) if fpth is not None: msg += ' {}'.format(os.path.basename(fpth)) success, buff = flopy.run_model(exe, fpth, model_ws=model_ws, silent=False) assert success, 'could not run...{}'.format(msg) # run the modpath model print('running model...{}'.format(fn)) exe = emp7 fpth = os.path.basename(fn) success, buff = flopy.run_model(exe, fpth, model_ws=model_ws, silent=False) assert success, 'could not run...{}'.format(os.path.basename(fn)) return
def run_mf2005(namefile, regression=True): """ Run the simulation. """ # Set root as the directory name where namefile is located testname = pymake.get_sim_name(namefile, rootpth=testpaths[0])[0] # Set nam as namefile name without path nam = os.path.basename(namefile) # Setup testpth = os.path.join(testdir, testname) pymake.setup(namefile, testpth) # run test models print('running model...{}'.format(testname)) exe_name = os.path.abspath(target_release) success, buff = flopy.run_model(exe_name, nam, model_ws=testpth, silent=True) assert success, 'base model {} '.format(nam) + 'did not run.' # If it is a regression run, then setup and run the model with the # release target and the reference target success_reg = True if regression: testname_reg = os.path.basename(target_previous) testpth_reg = os.path.join(testpth, testname_reg) pymake.setup(namefile, testpth_reg) print('running regression model...{}'.format(testname_reg)) exe_name = os.path.abspath(target_previous) success_reg, buff = flopy.run_model(exe_name, nam, model_ws=testpth_reg, silent=False) assert success_reg, 'regression model {} '.format(nam) + 'did not run.' # compare results outfile1 = os.path.join(os.path.split(os.path.join(testpth, nam))[0], 'bud.cmp') outfile2 = os.path.join(os.path.split(os.path.join(testpth, nam))[0], 'hds.cmp') success_reg = pymake.compare(os.path.join(testpth, nam), os.path.join(testpth_reg, nam), precision='single', max_cumpd=0.01, max_incpd=0.01, htol=0.001, outfile1=outfile1, outfile2=outfile2) # Clean things up if not retain: pymake.teardown(testpth) return
def run_mt3dusgs(temp_dir): success = False if os.path.exists(emtusgs): model_ws = os.path.join(expth, temp_dir) files = [ f for f in os.listdir(model_ws) if os.path.isfile(os.path.join(model_ws, f)) ] mf_nam = None mt_nam = None flow_model = None for f in files: if "_mf.nam" in f.lower(): mf_nam = f flow_model = "mfnwt" if "_mt.nam" in f.lower(): mt_nam = f if f == "mfsim.nam": mf_nam = f flow_model = "mf6" msg = "A MODFLOW name file not present in {}".format(model_ws) assert mf_nam is not None, msg msg = "A MT3D-USGS name file not present in {}".format(model_ws) assert mt_nam is not None, msg # run the flow model msg = "{}".format(emfnwt) if mf_nam is not None: msg += " {}".format(os.path.basename(mf_nam)) if flow_model == "mfnwt": nam = mf_nam eapp = emfnwt elif flow_model == "mf6": nam = None eapp = emf6 success, buff = flopy.run_model(eapp, nam, model_ws=model_ws, silent=False) # run the MT3D-USGS model if success: print("running model...{}".format(mt_nam)) success, buff = flopy.run_model( emtusgs, mt_nam, model_ws=model_ws, silent=False, normal_msg="Program completed.", ) return success
def run_mt3dusgs(temp_dir): model_ws = os.path.join(expth, temp_dir) files = [ f for f in os.listdir(model_ws) if os.path.isfile(os.path.join(model_ws, f)) ] mf_nam = None mt_nam = None flow_model = None for f in files: if '_mf.nam' in f.lower(): mf_nam = f flow_model = 'mfnwt' if '_mt.nam' in f.lower(): mt_nam = f if f == 'mfsim.nam': mf_nam = f flow_model = 'mf6' msg = 'A MODFLOW name file not present in {}'.format(model_ws) assert mf_nam is not None, msg msg = 'A MT3D-USGS name file not present in {}'.format(model_ws) assert mt_nam is not None, msg # run the flow model msg = '{}'.format(emfnwt) if mf_nam is not None: msg += ' {}'.format(os.path.basename(mf_nam)) if flow_model == 'mfnwt': nam = mf_nam eapp = emfnwt elif flow_model == 'mf6': nam = None eapp = emf6 success, buff = flopy.run_model(eapp, nam, model_ws=model_ws, silent=False) assert success, 'could not run...{}'.format(msg) # run the MT3D-USGS model print('running model...{}'.format(mt_nam)) exe = mt_nam success, buff = flopy.run_model(emtusgs, mt_nam, model_ws=model_ws, silent=False, normal_msg='Program completed.') assert success, 'could not run...{}'.format(os.path.basename(mt_nam)) return
def run_modpath6(fn): success = False if os.path.exists(epth): update_files(fn) # run the model print("running model...{}".format(fn)) success, buff = flopy.run_model(epth, fn, model_ws=expth, silent=False) return success
def run_function(app, example): return flopy.run_model( app, None, model_ws=example, silent=True, report=True, )
def run_modpath7(fn): # run the model print('running model...{}'.format(fn)) exe = os.path.abspath(target) fpth = os.path.basename(fn) model_ws = os.path.dirname(fn) success, buff = flopy.run_model(exe, fpth, model_ws=model_ws, silent=False) assert success, 'could not run...{}'.format(os.path.basename(fn)) return
def run_mf6(ws): success = False exe_name = os.path.abspath(epth) if os.path.exists(exe_name): # run test models print("running model...{}".format(os.path.basename(ws))) success, buff = flopy.run_model( exe_name, None, model_ws=ws, silent=False ) return success
def run_seawat(fn): # edit the name files edit_namefile(fn) # run the models success, buff = flopy.run_model( epth, os.path.basename(fn), model_ws=os.path.dirname(fn), silent=False ) errmsg = "could not run...{}".format(os.path.basename(fn)) assert success, errmsg return
def run_mfusg(fn): # edit namefile edit_namefile(fn) # run test models print("running model...{}".format(os.path.basename(fn))) success, buff = flopy.run_model(epth, os.path.basename(fn), model_ws=os.path.dirname(fn), silent=False) errmsg = "could not run {}".format(fn) assert success, errmsg return
def run_gsflow(example, control_file): # copy files copy_example_dir(example) model_ws = os.path.join(dstpth, example, 'linux') # run the flow model msg = '{}'.format(egsflow) success, buff = flopy.run_model(egsflow, control_file, model_ws=model_ws, silent=False) assert success, 'could not run...{}'.format(msg) return
def run_mt3dusgs(temp_dir): model_ws = os.path.join(expth, temp_dir) files = [f for f in os.listdir(model_ws) if os.path.isfile(os.path.join(model_ws, f))] mf_nam = None mt_nam = None for f in files: if '_mf.nam' in f.lower(): mf_nam = f if '_mt.nam' in f.lower(): mt_nam = f msg = 'A MODFLOW name file not present in {}'.format(model_ws) assert mf_nam is not None, msg msg = 'A MT3D-USGS name file not present in {}'.format(model_ws) assert mt_nam is not None, msg # run the flow model msg = '{}'.format(emfnwt) if mf_nam is not None: msg += ' {}'.format(os.path.basename(mf_nam)) success, buff = flopy.run_model(emfnwt, mf_nam, model_ws=model_ws, silent=False) assert success, 'could not run...{}'.format(msg) # run the MT3D-USGS model print('running model...{}'.format(mt_nam)) exe = mt_nam success, buff = flopy.run_model(emtusgs, mt_nam, model_ws=model_ws, silent=False, normal_msg='Program completed.') assert success, 'could not run...{}'.format(os.path.basename(mt_nam)) return
def run_mfusg(namepth, dst): print('running...{}'.format(dst)) # setup testpth = os.path.join(dstpth, dst) pymake.setup(namepth, testpth) # run test models print('running model...{}'.format(os.path.basename(namepth))) success, buff = flopy.run_model(epth, os.path.basename(namepth), model_ws=testpth, silent=True) if success: pymake.teardown(testpth) assert success is True return
def run_gsflow(example, control_file): success = False if os.path.exists(egsflow): # copy files copy_example_dir(example) model_ws = os.path.join(dstpth, example, "linux") # run the flow model success, buff = flopy.run_model( egsflow, control_file, model_ws=model_ws, silent=False ) if not success: errmsg = "could not run {}".format(control_file) return success
def run_mf6(d): print('running...{}'.format(d)) # setup epth = os.path.join(expth, d) testpth = os.path.join(dstpth, d) pymake.setup_mf6(epth, testpth) # run test models print('running model...{}'.format(os.path.basename(d))) epth = os.path.abspath(target) success, buff = flopy.run_model(epth, None, model_ws=testpth, silent=False) if success: pymake.teardown(testpth) assert success is True return
def run_modpath6(fn): # rename a few files for linux replace_files = ['example-6', 'example-7', 'example-8'] for rf in replace_files: if rf in fn.lower(): fname1 = os.path.join(expth, '{}.locations'.format(rf)) fname2 = os.path.join(expth, '{}_mod.locations'.format(rf)) print('copy {} to {}'.format(os.path.basename(fname1), os.path.basename(fname2))) shutil.copy(fname1, fname2) print('deleting {}'.format(os.path.basename(fname1))) os.remove(fname1) fname1 = os.path.join(expth, '{}.locations'.format(rf.upper())) print('renmae {} to {}'.format(os.path.basename(fname2), os.path.basename(fname1))) os.rename(fname2, fname1) # run the model print('running model...{}'.format(fn)) success, buff = flopy.run_model(epth, fn, model_ws=expth, silent=False) assert success, 'could not run...{}'.format(os.path.basename(fn)) return
def run_mfusg(regression=True): """ Download run and compare biscayne quadtree problem """ # Set the testname testname = '02_quadtree' testpth = os.path.join(config.testdir, testname) # Delete folder if exists, then download the MODFLOW-USG distribution if os.path.isdir(testpth): shutil.rmtree(testpth) url = "http://water.usgs.gov/ogw/mfusg/02_quadtree.zip" pymake.download_and_unzip(url, pth=config.testdir) # Go through name file and replace \ with / oldnam = os.path.join(testpth, 'biscayne.nam') newnam = os.path.join(testpth, 'biscayne.new.nam') fold = open(oldnam, 'r') fnew = open(newnam, 'w') for i, line in enumerate(fold): line = line.replace('\\', '/') line = line.replace('biscayne.disu', 'biscayne.new.disu') line = line.replace('biscayne.oc', 'biscayne.new.oc') fnew.write(line) fold.close() fnew.close() # Change the number of stress periods from 1000 to 10 olddis = os.path.join(testpth, 'input', 'biscayne.disu') newdis = os.path.join(testpth, 'input', 'biscayne.new.disu') fold = open(olddis, 'r') fnew = open(newdis, 'w') for i, line in enumerate(fold): if i == 1: line = line.replace('1000', '10') fnew.write(line) fold.close() fnew.close() # Write oc so that head and budget are always saved. newoc = os.path.join(testpth, 'input', 'biscayne.new.oc') fnew = open(newoc, 'w') fnew.write('HEAD SAVE UNIT 51' + '\n') fnew.write('COMPACT BUDGET' + '\n') for kper in range(10): fnew.write('PERIOD {} STEP 1'.format(kper + 1) + '\n') fnew.write(' PRINT BUDGET' + '\n') fnew.write(' SAVE BUDGET' + '\n') fnew.write(' SAVE HEAD' + '\n') fnew.close() # run test models print('running model...{}'.format(testname)) nam = 'biscayne.new.nam' exe_name = os.path.abspath(config.target) success, buff = flopy.run_model(exe_name, nam, model_ws=testpth, silent=True) assert success, 'biscayne model did not run successfully.' # If it is a regression run, then setup and run the model with the # release target if regression: testname_reg = testname + '_reg' testpth_reg = os.path.join(config.testdir, testname_reg) pymake.setup(os.path.join(testpth, nam), testpth_reg) outdir = os.path.join(testpth_reg, 'output') if not os.path.isdir(outdir): os.mkdir(outdir) print('running regression model...{}'.format(testname_reg)) exe_name = os.path.abspath(config.target_release) success, buff = flopy.run_model(exe_name, nam, model_ws=testpth_reg, silent=True) # Make comparison success = compare(os.path.join(testpth, nam), os.path.join(testpth_reg, nam)) assert success, 'Biscayne regression test did not pass' # Clean things up if success and not config.retain: pymake.teardown(testpth) if regression: pymake.teardown(testpth_reg) return
def run_mf5to6(sim): """ Run the MODFLOW 6 simulation and compare to existing head file or appropriate MODFLOW-2005, MODFLOW-NWT, MODFLOW-USG, or MODFLOW-LGR run. """ src = os.path.join(exdir, sim.name) dst = os.path.join('temp', 'working') # set default version version = 'mf2005' lgrpth = None compare = False cpth = None # determine if compare directory exists in directory or if mflgr control # file is in directory listdir = os.listdir(src) for value in listdir: fpth = os.path.join(src, value) if os.path.isfile(fpth): ext = os.path.splitext(fpth)[1] if '.lgr' in ext.lower(): version = 'mflgr' lgrpth = fpth elif os.path.isdir(fpth): if 'compare' in value.lower() or 'cmp' in value.lower(): compare = True cpth = value msg = 'Copying {} files to working directory'.format(version) # copy lgr files to working directory if lgrpth is not None: print(msg) npth = lgrpth pymake.setup(lgrpth, dst) # copy modflow 2005, NWT, or USG files to working directory else: print(msg) npths = pymake.get_namefiles(src) if len(npths) < 1: msg = 'No name files in {}'.format(src) print(msg) assert False npth = npths[0] pymake.setup(npth, dst) # read ftype from name file to set modflow version if version != 'mflgr': lines = [line.rstrip('\n') for line in open(npth)] for line in lines: if len(line) < 1: continue t = line.split() ftype = t[0].upper() if ftype == 'NWT' or ftype == 'UPW': version = 'mfnwt' break elif ftype == 'SMS' or ftype == 'DISU': version = 'mfusg' break # run converter exe = os.path.abspath(target_dict['mf5to6']) msg = sfmt.format('using executable', exe) print(msg) nmsg = 'Program terminated normally' try: nam = os.path.basename(npth) success, buff = flopy.run_model(exe, nam, model_ws=dst, silent=False, report=True, normal_msg=nmsg, cargs='mf6') msg = sfmt.format('MODFLOW 5 to 6 run', nam) if success: print(msg) else: print('ERROR: ' + msg) except: msg = sfmt.format('MODFLOW 5 to 6 run', nam) print('ERROR: ' + msg) success = False assert success, msg # path to copy # copy files in the compare directory if compare: dst2 = os.path.join(dst, cpth) tpth = os.path.join(exdir, sim.name, cpth) shutil.copytree(tpth, dst2) # copy original modflow files to the appopriate directory # (mf2005, mfnwt, or mfusg) in temp/working else: dst2 = os.path.join(dst, version) pymake.setup(npth, dst2) # standard setup src = dst dst = os.path.join('temp', sim.name) sim.setup(src, dst) # clean up temp/working directory (src) if os.path.exists(src): msg = 'Removing {} directory'.format(src) print(msg) shutil.rmtree(src) time.sleep(0.5) # standard comparison run sim.run() sim.compare() sim.teardown()
def run_mf5to6(sim): """ Run the MODFLOW 6 simulation and compare to existing head file or appropriate MODFLOW-2005, MODFLOW-NWT, MODFLOW-USG, or MODFLOW-LGR run. """ src = os.path.join(exdir, sim.name) dst = os.path.join("temp", "working") # set default version version = "mf2005" lgrpth = None # determine if compare directory exists in directory or if mflgr control # file is in directory listdir = os.listdir(src) for value in listdir: fpth = os.path.join(src, value) if os.path.isfile(fpth): ext = os.path.splitext(fpth)[1] if ".lgr" in ext.lower(): version = "mflgr" lgrpth = fpth elif os.path.isdir(fpth): if "compare" in value.lower() or "cmp" in value.lower(): compare = True cpth = value msg = "Copying {} files to working directory".format(version) # copy lgr files to working directory if lgrpth is not None: print(msg) npth = lgrpth pymake.setup(lgrpth, dst) # copy modflow 2005, NWT, or USG files to working directory else: print(msg) npths = pymake.get_namefiles(src) if len(npths) < 1: msg = "No name files in {}".format(src) print(msg) assert False npth = npths[0] pymake.setup(npth, dst) # read ftype from name file to set modflow version if version != "mflgr": lines = [line.rstrip("\n") for line in open(npth)] for line in lines: if len(line) < 1: continue t = line.split() ftype = t[0].upper() if ftype == "NWT" or ftype == "UPW": version = "mfnwt" break elif ftype == "SMS" or ftype == "DISU": version = "mfusg" break # run converter exe = os.path.abspath(target_dict["mf5to6"]) msg = sfmt.format("using executable", exe) print(msg) nmsg = "Program terminated normally" try: nam = os.path.basename(npth) success, buff = flopy.run_model( exe, nam, model_ws=dst, silent=False, report=True, normal_msg=nmsg, cargs="mf6", ) msg = sfmt.format("MODFLOW 5 to 6 run", nam) if success: print(msg) else: print("ERROR: " + msg) except: msg = sfmt.format("MODFLOW 5 to 6 run", nam) print("ERROR: " + msg) success = False assert success, msg # standard setup src = dst dst = os.path.join("temp", sim.name) sim.setup(src, dst) # clean up temp/working directory (src) if os.path.exists(src): msg = "Removing {} directory".format(src) print(msg) shutil.rmtree(src) time.sleep(0.5) # standard comparison run sim.run() sim.compare() sim.teardown()
def run_mt3d(mf5namefile, mf6namefile, mtnamefile, regression=True): """ Run the simulations. """ # Set root as the directory name where namefile is located flowexe = config.target_dict['mfnwt'] if '_mf2k' in mf5namefile: crep = '_mf2k' flowexe = config.target_dict['mf2000'] elif '_mf2005' in mf5namefile: crep = '_mf2005' flowexe = config.target_dict['mf2005'] else: crep = '_mf' flowexe = config.target_dict['mfnwt'] testname = pymake.get_sim_name(mf5namefile.replace(crep, ''), rootpth=os.path.dirname(mf5namefile))[0] # Setup modflow testpth = os.path.join(config.testdir, testname) pymake.setup(mf5namefile, testpth) # run modflow5-variant (whether NWT or 2k5) to generate flow field for mt3d-usgs print('running modflow5 model...{}'.format(testname)) nam = os.path.basename(mf5namefile) exe_name = flowexe success, buff = flopy.run_model(exe_name, nam, model_ws=testpth, silent=False, report=True) # Setup and run MF6 files for comparing with MF5-linked results flowexe = config.target_dict['mf6'] exe_name = flowexe nam = os.path.basename(mf6namefile) pymake.setup_mf6(os.path.dirname(mf6namefile), testpth, remove_existing=False) cwd = os.getcwd() os.chdir(os.path.join(testpth)) os.system(os.path.join('..', 'bin', 'mf6.exe')) os.chdir(cwd) # Setup mt3d pymake.setup(mtnamefile[0], testpth, remove_existing=False, extrafiles=mtnamefile[1]) # if modflow ran successfully, then run mt3d-usgs success_mf5 = success_mf6 = False if success: for i, nmfile in enumerate(mtnamefile): print('running mt3d-usgs model...{}'.format(nmfile)) nam = os.path.basename(nmfile) exe_name = os.path.abspath(config.target) success, buff = flopy.run_model(exe_name, nam, model_ws=testpth, silent=False, report=True, normal_msg='program completed') if i == 0: success_mf5 = success elif i > 0: success_mf6 = success # run ucn comparison if success_mf5 and success_mf6: success_cmp = True else: success_cmp = False success_reg = False if regression: if success_cmp: # if mt3d-usgs ran, then time to do a comparison #ucnfile1 = os.path.join(testpth, os.path.split(testpth)[-1] + '_Comp1_4mf5.ucn') #ucnfile2 = os.path.join(testpth, os.path.split(testpth)[-1] + '_Comp1_4mf6.ucn') namefile1 = os.path.join(testpth, os.path.split(mtnamefile[0])[-1]) namefile2 = os.path.join(testpth, os.path.split(mtnamefile[1])[-1]) outfileucn = os.path.join( testpth, os.path.split(testpth)[-1] + '_UCNcompareResults.txt') success_ucn = pymake.compare_concs(namefile1, namefile2, ctol=0.001, outfile=outfileucn) if success_ucn: success_reg = True else: success_reg = False # Clean things up if success and success_reg and not config.retain: pymake.teardown(testpth) assert success, 'model did not run' assert success_cmp, 'comparison model did not meet comparison criteria' return
def run(self): """ Run the model and assert if the model terminated successfully """ msg = sfmt.format('Run test', self.name) print(msg) # Set nam as namefile name without path nam = None # run mf6 models exe = os.path.abspath(targets.target_dict[targets.program]) msg = sfmt.format('using executable', exe) print(msg) try: success, buff = flopy.run_model(exe, nam, model_ws=self.simpath, silent=False, report=True) msg = sfmt.format('MODFLOW 6 run', self.name) if success: print(msg) else: print(msg) except: msg = sfmt.format('MODFLOW 6 run', self.name) print(msg) success = False assert success self.nam_cmp = None if success: if self.action is not None: if self.action.lower() == 'compare': msg = sfmt.format('Comparison files', self.name) print(msg) else: cpth = os.path.join(self.simpath, self.action) key = self.action.lower().replace('.cmp', '') exe = os.path.abspath(targets.target_dict[key]) npth = pymake.get_namefiles(cpth)[0] nam = os.path.basename(npth) self.nam_cmp = nam try: success_cmp, buff = flopy.run_model(exe, nam, model_ws=cpth, silent=False, report=True) msg = sfmt.format('Comparison run', self.name + '/' + key) if success: print(msg) else: print(msg) except: success_cmp = False msg = sfmt.format('Comparison run', self.name + '/' + key) print(msg) assert success_cmp return
def run_mf5to6(sim): """ Run the MODFLOW 6 simulation and compare to existing head file or appropriate MODFLOW-2005, MODFLOW-NWT, MODFLOW-USG, or MODFLOW-LGR run. """ src = os.path.join(example_basedir, sim.name) dst = os.path.join("temp", "working") # set lgrpth to None lgrpth = None # determine if compare directory exists in directory or if mflgr control # file is in directory listdir = os.listdir(src) for value in listdir: fpth = os.path.join(src, value) if os.path.isfile(fpth): ext = os.path.splitext(fpth)[1] if ".lgr" in ext.lower(): lgrpth = fpth print("Copying files to working directory") # copy lgr files to working directory if lgrpth is not None: npth = lgrpth pymake.setup(lgrpth, dst) # copy MODFLOW-2005, MODFLOW-NWT, or MODFLOW-USG files to working directory else: npths = pymake.get_namefiles(src) if len(npths) < 1: msg = "No name files in {}".format(src) print(msg) assert False npth = npths[0] pymake.setup(npth, dst) # run the mf5to6 converter exe = os.path.abspath(target_dict["mf5to6"]) print(sfmt.format("using executable", exe)) nmsg = "Program terminated normally" try: nam = os.path.basename(npth) success, buff = flopy.run_model( exe, nam, model_ws=dst, silent=False, report=True, normal_msg=nmsg, cargs="mf6", ) msg = sfmt.format("MODFLOW 5 to 6 run", nam) if success: print(msg) else: print("ERROR: " + msg) except: msg = sfmt.format("MODFLOW 5 to 6 run", nam) print("ERROR: " + msg) success = False assert success, msg # standard setup src = dst dst = os.path.join("temp", sim.name) sim.setup(src, dst) # clean up temp/working directory (src) if os.path.exists(src): msg = "Removing {} directory".format(src) print(msg) shutil.rmtree(src) time.sleep(0.5) # standard comparison run sim.run() sim.compare() sim.teardown()
def eval_zb6(sim): print('evaluating zonebudget...') # build zonebudget files zones = [-1000000, 1000000, 9999999] nzones = len(zones) fpth = os.path.join(sim.simpath, 'zonebudget.nam') f = open(fpth, 'w') f.write('BEGIN ZONEBUDGET\n') f.write(' BUD {}.cbc\n'.format(os.path.basename(sim.name))) f.write(' ZON {}.zon\n'.format(os.path.basename(sim.name))) f.write(' GRB {}.dis.grb\n'.format(os.path.basename(sim.name))) f.write('END ZONEBUDGET\n') f.close() fpth = os.path.join(sim.simpath, '{}.zon'.format(os.path.basename(sim.name))) f = open(fpth, 'w') f.write('BEGIN DIMENSIONS\n') f.write(' NCELLS {}\n'.format(size3d)) f.write('END DIMENSIONS\n\n') f.write('BEGIN GRIDDATA\n') f.write(' IZONE LAYERED\n') for k in range(nlay): f.write(' CONSTANT {:>10d}\n'.format(zones[k])) f.write('END GRIDDATA\n') f.close() # run zonebudget zbexe = os.path.abspath(targets.target_dict['zbud6']) success, buff = flopy.run_model(zbexe, 'zonebudget.nam', model_ws=sim.simpath, silent=False, report=True) if success: print('successfully ran...{}'.format(os.path.basename(zbexe))) sim.success = True else: sim.success = False msg = 'could not run...{}'.format(zbexe) assert success, msg # read data from csv file fpth = os.path.join(sim.simpath, 'zonebudget.csv') zbd = np.genfromtxt(fpth, names=True, delimiter=',', deletechars='') # sum the data for all zones nentries = int(zbd.shape[0] / 3) zbsum = np.zeros(nentries, dtype=zbd.dtype) static = ['totim', 'kstp', 'kper'] ipos = 0 ion = 0 for t in zbd: for name in zbd.dtype.names: if name in static: zbsum[name][ipos] = t[name] elif name == 'zone': zbsum[name][ipos] = 0 else: zbsum[name][ipos] += t[name] ion += 1 if ion == 3: ipos += 1 ion = 0 # get results from listing file fpth = os.path.join(sim.simpath, '{}.lst'.format(os.path.basename(sim.name))) budl = flopy.utils.Mf6ListBudget(fpth) names = list(bud_lst) d0 = budl.get_budget(names=names)[0] dtype = d0.dtype nbud = d0.shape[0] # get results from cbc file cbc_bud = ['STO-SS', 'STO-SY', 'RCH', 'CHD', 'WEL'] d = np.recarray(nbud, dtype=dtype) for key in bud_lst: d[key] = 0. fpth = os.path.join(sim.simpath, '{}.cbc'.format(os.path.basename(sim.name))) cobj = flopy.utils.CellBudgetFile(fpth, precision='double') kk = cobj.get_kstpkper() times = cobj.get_times() for idx, (k, t) in enumerate(zip(kk, times)): for text in cbc_bud: qin = 0. qout = 0. v = cobj.get_data(kstpkper=k, text=text)[0] if isinstance(v, np.recarray): vt = np.zeros(size3d, dtype=np.float) for jdx, node in enumerate(v['node']): vt[node-1] += v['q'][jdx] v = vt.reshape(shape3d) for kk in range(v.shape[0]): for ii in range(v.shape[1]): for jj in range(v.shape[2]): vv = v[kk, ii, jj] if vv < 0.: qout -= vv else: qin += vv d['totim'][idx] = t d['time_step'][idx] = k[0] d['stress_period'] = k[1] key = '{}_IN'.format(text) d[key][idx] = qin key = '{}_OUT'.format(text) d[key][idx] = qout diff = np.zeros((nbud, len(bud_lst)), dtype=np.float) for idx, key in enumerate(bud_lst): diff[:, idx] = d0[key] - d[key] diffmax = np.abs(diff).max() msg = 'maximum absolute total-budget difference ({}) '.format(diffmax) # write summary fpth = os.path.join(sim.simpath, '{}.bud.cmp.out'.format(os.path.basename(sim.name))) f = open(fpth, 'w') for i in range(diff.shape[0]): if i == 0: line = '{:>10s}'.format('TIME') for idx, key in enumerate(bud_lst): line += '{:>25s}'.format(key+'_LST') line += '{:>25s}'.format(key+'_CBC') line += '{:>25s}'.format(key + '_DIF') f.write(line + '\n') line = '{:10g}'.format(d['totim'][i]) for idx, key in enumerate(bud_lst): line += '{:25g}'.format(d0[key][i]) line += '{:25g}'.format(d[key][i]) line += '{:25g}'.format(diff[i, idx]) f.write(line + '\n') f.close() # compare zone budget to cbc output diffzb = np.zeros((nbud, len(bud_lst)), dtype=np.float) for idx, (key0, key) in enumerate(zip(zone_lst, bud_lst)): diffzb[:, idx] = zbsum[key0] - d[key] diffzbmax = np.abs(diffzb).max() msg += '\nmaximum absolute zonebudget-cell by cell difference ({}) '.format(diffzbmax) # write summary fpth = os.path.join(sim.simpath, '{}.zbud.cmp.out'.format(os.path.basename(sim.name))) f = open(fpth, 'w') for i in range(diff.shape[0]): if i == 0: line = '{:>10s}'.format('TIME') for idx, key in enumerate(bud_lst): line += '{:>25s}'.format(key+'_ZBUD') line += '{:>25s}'.format(key+'_CBC') line += '{:>25s}'.format(key + '_DIF') f.write(line + '\n') line = '{:10g}'.format(d['totim'][i]) for idx, (key0, key) in enumerate(zip(zone_lst, bud_lst)): line += '{:25g}'.format(zbsum[key0][i]) line += '{:25g}'.format(d[key][i]) line += '{:25g}'.format(diffzb[i, idx]) f.write(line + '\n') f.close() if diffmax > budtol or diffzbmax > budtol: sim.success = False msg += '\n...exceeds {}'.format(budtol) assert diffmax < budtol and diffzbmax < budtol, msg else: sim.success = True print(' ' + msg) return
def run_mt3d(spth, comparison=True): """ Run the simulations. """ # Path to folder containing tests pth = config.testpaths[0] # -- get modflow name files tpth = os.path.join(pth, spth) namefilesmf = [] namefilesmf += get_namefiles(tpth, exclude='mt') # -- get mt3d name files tpth = os.path.join(pth, spth) namefilesmt = [] namefilesmt += get_namefiles(tpth, exclude='mf') mfnamefile = namefilesmf[0] mtnamefile = namefilesmt[0] print(mfnamefile, mtnamefile) # Set root as the directory name where namefile is located testname = pymake.get_sim_name(mfnamefile.replace('_mf', ''), rootpth=os.path.dirname(mfnamefile))[0] # Setup modflow testpth = os.path.join(config.testdir, testname) pymake.setup(mfnamefile, testpth) # Setup mt3d pymake.setup(mtnamefile, testpth, remove_existing=False) # run test models print('running modflow-nwt model...{}'.format(testname)) nam = os.path.basename(mfnamefile) exe_name = config.target_dict['mfnwt'] success, buff = flopy.run_model(exe_name, nam, model_ws=testpth, silent=True) if success: print('running mt3d-usgs model...{}'.format(testname)) nam = os.path.basename(mtnamefile) exe_name = os.path.abspath(config.target) success, buff = flopy.run_model(exe_name, nam, model_ws=testpth, silent=True, normal_msg='program completed') success_cmp = True if success and comparison: action = pymake.setup_comparison(mfnamefile, testpth) action = pymake.setup_comparison(mtnamefile, testpth, remove_existing=False) testpth_cmp = os.path.join(testpth, action) if action is not None: files_cmp = None if action.lower() == '.cmp': files_cmp = [] files = os.listdir(testpth_cmp) for file in files: files1 = os.path.join(testpth, file[:-4]) files2 = os.path.join(testpth_cmp, file) outfileucn = os.path.join(testpth, file + '.txt') success_ucn = pymake.compare_concs(None, None, ctol=0.002, outfile=outfileucn, files1=files1, files2=files2) if not success_ucn: success_cmp = False else: print('running comparison modflow-nwt model...{}'.format(testpth_cmp)) key = action.lower().replace('.cmp', '') nam = os.path.basename(mfnamefile) exe_name = os.path.abspath(config.target_dict['mfnwt']) success_cmp, buff = flopy.run_model(exe_name, nam, model_ws=testpth_cmp, silent=True) if success_cmp: print('running comparison mt3dms model...{}'.format(testpth_cmp)) key = action.lower().replace('.cmp', '') nam = os.path.basename(mtnamefile) exe_name = os.path.abspath(config.target_release) success_cmp, buff = flopy.run_model(exe_name, nam, model_ws=testpth_cmp, silent=True, normal_msg='program completed') if success_cmp: nam = os.path.basename(mtnamefile) namefile1 = os.path.join(testpth, nam) namefile2 = os.path.join(testpth_cmp, nam) outfileucn = os.path.join( os.path.split(os.path.join(testpth, nam))[0], 'ucn.cmp') success_ucn = pymake.compare_concs(namefile1, namefile2, ctol=0.002, outfile=outfileucn, files2=files_cmp) if success_cmp and success_ucn: success_cmp = True else: success_cmp = False # Clean things up if success and success_cmp and not config.retain: pymake.teardown(testpth) assert success, 'model did not run' assert success_cmp, 'comparison model did not meet comparison criteria' return
def eval_zb6(sim): print("evaluating zonebudget...") # build zonebudget files zones = [-1000000, 1000000, 9999999] nzones = len(zones) fpth = os.path.join(sim.simpath, "zonebudget.nam") f = open(fpth, "w") f.write("BEGIN ZONEBUDGET\n") f.write(" BUD {}.cbc\n".format(os.path.basename(sim.name))) f.write(" ZON {}.zon\n".format(os.path.basename(sim.name))) f.write(" GRB {}.dis.grb\n".format(os.path.basename(sim.name))) f.write("END ZONEBUDGET\n") f.close() fpth = os.path.join(sim.simpath, "{}.zon".format(os.path.basename(sim.name))) f = open(fpth, "w") f.write("BEGIN DIMENSIONS\n") f.write(" NCELLS {}\n".format(size3d)) f.write("END DIMENSIONS\n\n") f.write("BEGIN GRIDDATA\n") f.write(" IZONE LAYERED\n") for k in range(nlay): f.write(" CONSTANT {:>10d}\n".format(zones[k])) f.write("END GRIDDATA\n") f.close() # run zonebudget zbexe = os.path.abspath(targets.target_dict["zbud6"]) success, buff = flopy.run_model( zbexe, "zonebudget.nam", model_ws=sim.simpath, silent=False, report=True, ) if success: print("successfully ran...{}".format(os.path.basename(zbexe))) sim.success = True else: sim.success = False msg = "could not run...{}".format(zbexe) assert success, msg # read data from csv file fpth = os.path.join(sim.simpath, "zonebudget.csv") zbd = np.genfromtxt(fpth, names=True, delimiter=",", deletechars="") # sum the data for all zones nentries = int(zbd.shape[0] / 3) zbsum = np.zeros(nentries, dtype=zbd.dtype) static = ["totim", "kstp", "kper"] ipos = 0 ion = 0 for t in zbd: for name in zbd.dtype.names: if name in static: zbsum[name][ipos] = t[name] elif name == "zone": zbsum[name][ipos] = 0 else: zbsum[name][ipos] += t[name] ion += 1 if ion == 3: ipos += 1 ion = 0 # get results from listing file fpth = os.path.join(sim.simpath, "{}.lst".format(os.path.basename(sim.name))) budl = flopy.utils.Mf6ListBudget(fpth) names = list(bud_lst) d0 = budl.get_budget(names=names)[0] dtype = d0.dtype nbud = d0.shape[0] # get results from cbc file cbc_bud = ["STO-SS", "STO-SY", "RCH", "CHD", "WEL"] d = np.recarray(nbud, dtype=dtype) for key in bud_lst: d[key] = 0.0 fpth = os.path.join(sim.simpath, "{}.cbc".format(os.path.basename(sim.name))) cobj = flopy.utils.CellBudgetFile(fpth, precision="double") kk = cobj.get_kstpkper() times = cobj.get_times() for idx, (k, t) in enumerate(zip(kk, times)): for text in cbc_bud: qin = 0.0 qout = 0.0 v = cobj.get_data(kstpkper=k, text=text)[0] if isinstance(v, np.recarray): vt = np.zeros(size3d, dtype=float) for jdx, node in enumerate(v["node"]): vt[node - 1] += v["q"][jdx] v = vt.reshape(shape3d) for kk in range(v.shape[0]): for ii in range(v.shape[1]): for jj in range(v.shape[2]): vv = v[kk, ii, jj] if vv < 0.0: qout -= vv else: qin += vv d["totim"][idx] = t d["time_step"][idx] = k[0] d["stress_period"] = k[1] key = "{}_IN".format(text) d[key][idx] = qin key = "{}_OUT".format(text) d[key][idx] = qout diff = np.zeros((nbud, len(bud_lst)), dtype=float) for idx, key in enumerate(bud_lst): diff[:, idx] = d0[key] - d[key] diffmax = np.abs(diff).max() msg = "maximum absolute total-budget difference ({}) ".format(diffmax) # write summary fpth = os.path.join(sim.simpath, "{}.bud.cmp.out".format(os.path.basename(sim.name))) f = open(fpth, "w") for i in range(diff.shape[0]): if i == 0: line = "{:>10s}".format("TIME") for idx, key in enumerate(bud_lst): line += "{:>25s}".format(key + "_LST") line += "{:>25s}".format(key + "_CBC") line += "{:>25s}".format(key + "_DIF") f.write(line + "\n") line = "{:10g}".format(d["totim"][i]) for idx, key in enumerate(bud_lst): line += "{:25g}".format(d0[key][i]) line += "{:25g}".format(d[key][i]) line += "{:25g}".format(diff[i, idx]) f.write(line + "\n") f.close() # compare zone budget to cbc output diffzb = np.zeros((nbud, len(bud_lst)), dtype=float) for idx, (key0, key) in enumerate(zip(zone_lst, bud_lst)): diffzb[:, idx] = zbsum[key0] - d[key] diffzbmax = np.abs(diffzb).max() msg += ("\nmaximum absolute zonebudget-cell by cell difference ({}) ". format(diffzbmax)) # write summary fpth = os.path.join(sim.simpath, "{}.zbud.cmp.out".format(os.path.basename(sim.name))) f = open(fpth, "w") for i in range(diff.shape[0]): if i == 0: line = "{:>10s}".format("TIME") for idx, key in enumerate(bud_lst): line += "{:>25s}".format(key + "_ZBUD") line += "{:>25s}".format(key + "_CBC") line += "{:>25s}".format(key + "_DIF") f.write(line + "\n") line = "{:10g}".format(d["totim"][i]) for idx, (key0, key) in enumerate(zip(zone_lst, bud_lst)): line += "{:25g}".format(zbsum[key0][i]) line += "{:25g}".format(d[key][i]) line += "{:25g}".format(diffzb[i, idx]) f.write(line + "\n") f.close() if diffmax > budtol or diffzbmax > budtol: sim.success = False msg += "\n...exceeds {}".format(budtol) assert diffmax < budtol and diffzbmax < budtol, msg else: sim.success = True print(" " + msg) return
def run_mt3d(spth, comparison=True): """ Run the simulations. """ # Path to folder containing tests pth = config.testpaths[0] # -- get modflow name files tpth = os.path.join(pth, spth) namefilesmf = [] namefilesmf += get_namefiles(tpth, exclude='mt') # -- get mt3d name files tpth = os.path.join(pth, spth) namefilesmt = [] namefilesmt += get_namefiles(tpth, exclude='mf') mfnamefile = namefilesmf[0] mtnamefile = namefilesmt[0] print(mfnamefile, mtnamefile) # Set root as the directory name where namefile is located testname = pymake.get_sim_name(mfnamefile.replace('_mf', ''), rootpth=os.path.dirname(mfnamefile))[0] # Setup modflow testpth = os.path.join(config.testdir, testname) pymake.setup(mfnamefile, testpth) # Setup mt3d pymake.setup(mtnamefile, testpth, remove_existing=False) # Remove HSS file reference line from MT3D-USGS name file f = open(mtnamefile, 'r') lines = f.read() f.close() lines = lines.splitlines() f = open(os.path.join(testpth, testname + '_mt.nam'), 'w') for line in lines: if ('hss_source' in line): f.write('\n') else: f.write(line + '\n') f.close() # run modflow to generate flow field for mt3d-usgs print('running modflow-nwt model...{}'.format(testname)) nam = os.path.basename(mfnamefile) exe_name = config.target_dict['mfnwt'] success, buff = flopy.run_model(exe_name, nam, model_ws=testpth, silent=False, report=True) # if modflow ran successfully, then run mt3d-usgs if success: print('running mt3d-usgs model...{}'.format(testname)) nam = os.path.basename(mtnamefile) exe_name = os.path.abspath(config.target) success, buff = flopy.run_model(exe_name, nam, model_ws=testpth, silent=False, report=True, normal_msg='program completed') success_cmp = True if success and comparison: action = pymake.setup_comparison(mfnamefile, testpth) action = pymake.setup_comparison(mtnamefile, testpth, remove_existing=False) testpth_cmp = os.path.join(testpth, action) if action is not None: files_cmp = None if action.lower() == '.cmp': files_cmp = [] files = os.listdir(testpth_cmp) # Go through all files in the .cmp folder and do a separate # comparison for each one. This will ensure that the # individual ucn files for sorbed and multi-species will be # compared. for file in files: files1 = os.path.join(testpth, file[:-4]) files2 = os.path.join(testpth_cmp, file) outfileucn = os.path.join(testpth, file + '.txt') success_ucn = pymake.compare_concs(None, None, ctol=0.002, outfile=outfileucn, files1=files1, files2=files2) if not success_ucn: success_cmp = False else: print('running comparison modflow-nwt model...{}'.format( testpth_cmp)) key = action.lower().replace('.cmp', '') nam = os.path.basename(mfnamefile) exe_name = os.path.abspath(config.target_dict['mfnwt']) success_cmp, buff = flopy.run_model(exe_name, nam, model_ws=testpth_cmp, silent=False, report=True) if success_cmp: print('running comparison mt3dms model...{}'.format( testpth_cmp)) key = action.lower().replace('.cmp', '') nam = os.path.basename(mtnamefile) exe_name = os.path.abspath(config.target_release) success_cmp, buff = flopy.run_model( exe_name, nam, model_ws=testpth_cmp, silent=False, report=True, normal_msg='program completed') if success_cmp: nam = os.path.basename(mtnamefile) namefile1 = os.path.join(testpth, nam) namefile2 = os.path.join(testpth_cmp, nam) outfileucn = os.path.join( os.path.split(os.path.join(testpth, nam))[0], 'ucn.cmp') success_ucn = pymake.compare_concs(namefile1, namefile2, ctol=0.002, outfile=outfileucn, files2=files_cmp) if success_cmp and success_ucn: success_cmp = True else: success_cmp = False # Clean things up if success and success_cmp and not config.retain: pymake.teardown(testpth) assert success, 'model did not run' assert success_cmp, 'comparison model did not meet comparison criteria' return
def run_modpath7(fn): success = False if os.path.exists(emp7): model_ws = os.path.dirname(fn) # run the flow model run = True if "modflow-2005" in fn.lower(): exe = emf2005 v = flopy.which(exe) if v is None: run = False nam = [ name for name in os.listdir(model_ws) if ".nam" in name.lower() ] if len(nam) > 0: fpth = nam[0] # read and rewrite the name file set_lowercase(os.path.join(model_ws, fpth)) else: fpth = None run = False elif "modflow-usg" in fn.lower(): exe = emfusg v = flopy.which(exe) if v is None: run = False nam = [ name for name in os.listdir(model_ws) if ".nam" in name.lower() ] if len(nam) > 0: fpth = nam[0] else: fpth = None run = False elif "modflow-6" in fn.lower(): exe = emf6 v = flopy.which(exe) if v is None: run = False fpth = None else: run = False if run: # fix any known problems replace_data(model_ws) # run the model msg = "{}".format(exe) if fpth is not None: msg += " {}".format(os.path.basename(fpth)) success, buff = flopy.run_model(exe, fpth, model_ws=model_ws, silent=False) if success: # run the modpath model print("running model...{}".format(fn)) exe = emp7 fpth = os.path.basename(fn) success, buff = flopy.run_model(exe, fpth, model_ws=model_ws, silent=False) return success
for home in homes: if os.path.exists(home): for dirpath, dirnames, filenames in os.walk(home): for f in filenames: if os.path.splitext(f)[-1] == '.nam': mod = os.path.splitext(f)[0] mod_list.append(mod) dir_list.append(dirpath) i += 1 print(' {} models read'.format(i)) # In[ ]: for model_ws in dir_list: model = os.path.normpath(model_ws).split(os.sep)[2] nam_file = '{}.nam'.format(model) print("working model is {}".format(model_ws)) fp.run_model(mfpth, nam_file, model_ws=model_ws, silent=False, pause=False, report=False, normal_msg='normal termination', async=False, cargs=None) # In[ ]:
def run_mf2005(namefile, regression=True): """ Run the simulation. """ if namefile is not None: # Set root as the directory name where namefile is located testname = pymake.get_sim_name(namefile, rootpth=expth)[0] # Set nam as namefile name without path nam = os.path.basename(namefile) # Setup testpth = os.path.join(dstpth, testname) pymake.setup(namefile, testpth) # run test models exe_name = os.path.abspath(epth) msg = "running model...{}".format(testname) + " using {}".format( exe_name) print(msg) if os.path.exists(exe_name): success, buff = flopy.run_model(exe_name, nam, model_ws=testpth, silent=True) else: success = False assert success, "base model {} ".format(nam) + "did not run." # If it is a regression run, then setup and run the model with the # release target and the reference target success_reg = True if regression: testname_reg = os.path.basename(mfpth) testpth_reg = os.path.join(testpth, testname_reg) pymake.setup(namefile, testpth_reg) # exe_name = os.path.abspath(target_previous) msg = "running regression model...{}".format( testname_reg) + " using {}".format(exe_name) print(msg) if os.path.exists(exe_name): success_reg, buff = flopy.run_model(exe_name, nam, model_ws=testpth_reg, silent=False) else: success_reg = False assert success_reg, ("regression model {} ".format(nam) + "did not run.") # compare results if success and success_reg: fpth = os.path.split(os.path.join(testpth, nam))[0] outfile1 = os.path.join(fpth, "bud.cmp") fpth = os.path.split(os.path.join(testpth, nam))[0] outfile2 = os.path.join(fpth, "hds.cmp") success_reg = pymake.compare( os.path.join(testpth, nam), os.path.join(testpth_reg, nam), precision="single", max_cumpd=0.01, max_incpd=0.01, htol=0.001, outfile1=outfile1, outfile2=outfile2, ) # Clean things up if success_reg: pymake.teardown(testpth) else: success = False errmsg = "could not run...{}".format(os.path.basename(nam)) else: success = False errmsg = "{} does not exist".format(target) assert success, errmsg return
def run_mfusg(namefile, comparison=True): """ Run the simulation. """ # Set root as the directory name where namefile is located testname = pymake.get_sim_name(namefile, rootpth=config.testpaths[2])[0] # Set nam as namefile name without path nam = os.path.basename(namefile) # Setup testpth = os.path.join(config.testdir, testname) pymake.setup(namefile, testpth) # run test models print('running model...{}'.format(testname)) exe_name = os.path.abspath(config.target) success, buff = flopy.run_model(exe_name, nam, model_ws=testpth, silent=True) success_cmp = True if comparison: action = pymake.setup_comparison(namefile, testpth) testpth_cmp = os.path.join(testpth, action) if action is not None: files_cmp = None if action.lower() == '.cmp': files_cmp = [] files = os.listdir(testpth_cmp) for file in files: files_cmp.append( os.path.abspath(os.path.join(testpth_cmp, file))) success_cmp = True print(files_cmp) else: print('running comparison model...{}'.format(testpth_cmp)) key = action.lower().replace('.cmp', '') exe_name = config.target_dict[key] success_cmp, buff = flopy.run_model(exe_name, nam, model_ws=testpth_cmp, silent=True) if success_cmp: outfile1 = os.path.join( os.path.split(os.path.join(testpth, nam))[0], 'bud.cmp') outfile2 = os.path.join( os.path.split(os.path.join(testpth, nam))[0], 'hds.cmp') success_cmp = pymake.compare(os.path.join(testpth, nam), os.path.join(testpth_cmp, nam), precision='single', max_cumpd=0.01, max_incpd=0.01, htol=0.001, outfile1=outfile1, outfile2=outfile2, files2=files_cmp) # Clean things up if success and success_cmp and not config.retain: pymake.teardown(testpth) assert success, 'model did not run' assert success_cmp, 'comparison model did not meet comparison criteria' return