From 06f7598b0b0fe2e36cd942076d94da45dc73ce0c Mon Sep 17 00:00:00 2001 From: Joseph D Hughes Date: Sat, 30 Oct 2021 12:17:12 -0500 Subject: [PATCH 1/4] ci: convert addition autotests to use flopyTest testing framework --- autotest/t017_test.py | 28 ++++++++++++--------- autotest/t018_test.py | 31 ++++++++++++----------- autotest/t019_test.py | 23 ++++++----------- autotest/t020_test.py | 27 ++++++++++---------- autotest/t021_test.py | 30 ++++++++++++----------- autotest/t023_test.py | 33 +++++++++++++++++++------ autotest/t024_test.py | 24 ++++++++++++------ autotest/t025_test.py | 57 +++++++++++++++++++++++-------------------- 8 files changed, 142 insertions(+), 111 deletions(-) diff --git a/autotest/t017_test.py b/autotest/t017_test.py index 14ce24f724..c1c01c9ca2 100644 --- a/autotest/t017_test.py +++ b/autotest/t017_test.py @@ -3,14 +3,14 @@ import os import numpy as np import flopy +from ci_framework import baseTestDir, flopyTest -cpth = os.path.join("temp", "t017") -# make the directory if it does not exists -if not os.path.isdir(cpth): - os.makedirs(cpth, exist_ok=True) +baseDir = baseTestDir(__file__, relPath="temp", verbose=True) def test_formattedfile_read(): + model_ws = f"{baseDir}_test_formattedfile_read" + testFramework = flopyTest(verbose=True, testDirs=model_ws, create=True) h = flopy.utils.FormattedHeadFile( os.path.join("..", "examples", "data", "mf2005_test", "test1tr.githds") @@ -41,7 +41,7 @@ def test_formattedfile_read(): h.close() # Check error when reading empty file - fname = os.path.join(cpth, "empty.githds") + fname = os.path.join(model_ws, "empty.githds") with open(fname, "w"): pass with pytest.raises(ValueError): @@ -51,6 +51,8 @@ def test_formattedfile_read(): def test_binaryfile_read(): + model_ws = f"{baseDir}_test_binaryfile_read" + testFramework = flopyTest(verbose=True, testDirs=model_ws, create=True) h = flopy.utils.HeadFile( os.path.join("..", "examples", "data", "freyberg", "freyberg.githds") @@ -81,7 +83,7 @@ def test_binaryfile_read(): h.close() # Check error when reading empty file - fname = os.path.join(cpth, "empty.githds") + fname = os.path.join(model_ws, "empty.githds") with open(fname, "w"): pass with pytest.raises(ValueError): @@ -148,6 +150,8 @@ def test_cellbudgetfile_read(): def test_cellbudgetfile_position(): + model_ws = f"{baseDir}_test_cellbudgetfile_position" + testFramework = flopyTest(verbose=True, testDirs=model_ws, create=True) fpth = os.path.join( "..", "examples", "data", "zonbud_examples", "freyberg.gitcbc" @@ -176,7 +180,7 @@ def test_cellbudgetfile_position(): length = os.path.getsize(fpth) - ipos buffsize = 32 - opth = os.path.join(cpth, "end.cbc") + opth = os.path.join(model_ws, "end.cbc") with open(opth, "wb") as fout: while length: chunk = min(buffsize, length) @@ -204,7 +208,7 @@ def test_cellbudgetfile_position(): assert np.array_equal(d1, d2), msg # Check error when reading empty file - fname = os.path.join(cpth, "empty.gitcbc") + fname = os.path.join(model_ws, "empty.gitcbc") with open(fname, "w"): pass with pytest.raises(ValueError): @@ -324,12 +328,14 @@ def test_cellbudgetfile_readrecord_waux(): def test_binaryfile_writeread(): + model_ws = f"{baseDir}_test_binaryfile_writeread" + testFramework = flopyTest(verbose=True, testDirs=model_ws, create=True) pth = os.path.join("..", "examples", "data", "nwt_test") model = "Pr3_MFNWT_lower.nam" ml = flopy.modflow.Modflow.load(model, version="mfnwt", model_ws=pth) # change the model work space - ml.change_model_ws(os.path.join("temp", "t017")) + ml.change_model_ws(model_ws) # ncol = ml.dis.ncol nrow = ml.dis.nrow @@ -350,7 +356,7 @@ def test_binaryfile_writeread(): kper=1, ) b = ml.dis.botm.array[0, :, :].astype(np.float64) - pth = os.path.join("temp", "t017", "bottom.hds") + pth = os.path.join(model_ws, "bottom.hds") flopy.utils.Util2d.write_bin(b.shape, pth, b, header_data=header) bo = flopy.utils.HeadFile(pth, precision=precision) @@ -380,7 +386,7 @@ def test_binaryfile_writeread(): kper=1, ) b = ml.dis.botm.array[0, :, :].astype(np.float32) - pth = os.path.join("temp", "t017", "bottom_single.hds") + pth = os.path.join(model_ws, "bottom_single.hds") flopy.utils.Util2d.write_bin(b.shape, pth, b, header_data=header) bo = flopy.utils.HeadFile(pth, precision=precision) diff --git a/autotest/t018_test.py b/autotest/t018_test.py index 2764f12f62..a0478a2578 100644 --- a/autotest/t018_test.py +++ b/autotest/t018_test.py @@ -1,25 +1,22 @@ import os import numpy as np import flopy +from ci_framework import baseTestDir, flopyTest -# import flopy.pest.templatewriter -# import flopy.pest.templatewriter as flopy.pest.templatewriter -# import flopy.pest.flopy.pest.params as flopy.pest.params - -mpth = os.path.join("temp", "t018") -# make the directory if it does not exist -if not os.path.isdir(mpth): - os.makedirs(mpth, exist_ok=True) +baseDir = baseTestDir(__file__, relPath="temp", verbose=True) def test_tpl_constant(): + model_ws = f"{baseDir}_test_tpl_constant" + testFramework = flopyTest(verbose=True, testDirs=model_ws) + # Define the model dimensions nlay = 3 nrow = 20 ncol = 20 # Create the flopy model object and add the dis and lpf packages - m = flopy.modflow.Modflow(modelname="tpl1", model_ws=mpth) + m = flopy.modflow.Modflow(modelname="tpl1", model_ws=model_ws) dis = flopy.modflow.ModflowDis(m, nlay, nrow, ncol) lpf = flopy.modflow.ModflowLpf(m, hk=10.0) @@ -46,19 +43,22 @@ def test_tpl_constant(): tw = flopy.pest.templatewriter.TemplateWriter(m, [p]) tw.write_template() - tplfile = os.path.join(mpth, "tpl1.lpf.tpl") + tplfile = os.path.join(model_ws, "tpl1.lpf.tpl") assert os.path.isfile(tplfile) return def test_tpl_layered(): + model_ws = f"{baseDir}_test_tpl_layered" + testFramework = flopyTest(verbose=True, testDirs=model_ws) + nlay = 3 nrow = 20 ncol = 20 # Create the flopy model object and add the dis and lpf packages - m = flopy.modflow.Modflow(modelname="tpl2", model_ws=mpth) + m = flopy.modflow.Modflow(modelname="tpl2", model_ws=model_ws) dis = flopy.modflow.ModflowDis(m, nlay, nrow, ncol) lpf = flopy.modflow.ModflowLpf(m, hk=10.0) @@ -81,19 +81,22 @@ def test_tpl_layered(): tw = flopy.pest.templatewriter.TemplateWriter(m, [p]) tw.write_template() - tplfile = os.path.join(mpth, "tpl2.lpf.tpl") + tplfile = os.path.join(model_ws, "tpl2.lpf.tpl") assert os.path.isfile(tplfile) return def test_tpl_zoned(): + model_ws = f"{baseDir}_test_tpl_zoned" + testFramework = flopyTest(verbose=True, testDirs=model_ws) + nlay = 3 nrow = 20 ncol = 20 # Create the flopy model object and add the dis and lpf packages - m = flopy.modflow.Modflow(modelname="tpl3", model_ws=mpth) + m = flopy.modflow.Modflow(modelname="tpl3", model_ws=model_ws) dis = flopy.modflow.ModflowDis(m, nlay, nrow, ncol) lpf = flopy.modflow.ModflowLpf(m, hk=10.0) @@ -144,7 +147,7 @@ def test_tpl_zoned(): tw = flopy.pest.templatewriter.TemplateWriter(m, plist) tw.write_template() - tplfile = os.path.join(mpth, "tpl3.lpf.tpl") + tplfile = os.path.join(model_ws, "tpl3.lpf.tpl") assert os.path.isfile(tplfile) return diff --git a/autotest/t019_test.py b/autotest/t019_test.py index 9c8593f12c..4659bd5dbc 100644 --- a/autotest/t019_test.py +++ b/autotest/t019_test.py @@ -1,18 +1,16 @@ import os import numpy as np import flopy +from ci_framework import baseTestDir, flopyTest -mpth = os.path.join("temp", "t019") -# make the directory if it does not exist -if not os.path.isdir(mpth): - os.makedirs(mpth, exist_ok=True) +baseDir = baseTestDir(__file__, relPath="temp", verbose=True) # Test hydmod data readers def test_hydmodfile_create(): - model_ws = os.path.join(mpth) - if not os.path.isdir(model_ws): - os.makedirs(model_ws, exist_ok=True) + model_ws = f"{baseDir}_test_hydmodfile_create" + testFramework = flopyTest(verbose=True, testDirs=model_ws) + m = flopy.modflow.Modflow("test", model_ws=model_ws) hyd = flopy.modflow.ModflowHyd(m) m.hyd.write_file() @@ -48,6 +46,9 @@ def test_hydmodfile_create(): def test_hydmodfile_load(): + model_ws = f"{baseDir}_test_hydmodfile_load" + testFramework = flopyTest(verbose=True, testDirs=model_ws) + model = "test1tr.nam" pth = os.path.join("..", "examples", "data", "hydmod_test") m = flopy.modflow.Modflow.load( @@ -58,10 +59,6 @@ def test_hydmodfile_load(): hydref, flopy.modflow.ModflowHyd ), "Did not load hydmod package...test1tr.hyd" - model_ws = os.path.join(mpth) - if not os.path.isdir(model_ws): - os.makedirs(model_ws, exist_ok=True) - m.change_model_ws(model_ws) m.hyd.write_file() @@ -75,8 +72,6 @@ def test_hydmodfile_load(): def test_hydmodfile_read(): - import os - import flopy pth = os.path.join( "..", "examples", "data", "hydmod_test", "test1tr.hyd.gitbin" @@ -149,8 +144,6 @@ def test_hydmodfile_read(): def test_mf6obsfile_read(): - import os - import flopy try: import pandas as pd diff --git a/autotest/t020_test.py b/autotest/t020_test.py index db425811a4..b4f4a8e891 100644 --- a/autotest/t020_test.py +++ b/autotest/t020_test.py @@ -1,13 +1,9 @@ -# Test modflow write adn run +# Test modflow write and run import numpy as np +import matplotlib.pyplot as plt +from ci_framework import baseTestDir, flopyTest -try: - import matplotlib.pyplot as plt - - # if os.getenv('TRAVIS'): # are we running https://travis-ci.org/ automated tests ? - # matplotlib.use('Agg') # Force matplotlib not to use any Xwindows backend -except: - plt = None +baseDir = baseTestDir(__file__, relPath="temp", verbose=True) def analyticalWaterTableSolution(h1, h2, z, R, K, L, x): @@ -34,10 +30,10 @@ def test_mfnwt_run(): print(f"Specified executable {exe_name} does not exist in path") return + model_ws = f"{baseDir}_test_mfnwt_run" + testFramework = flopyTest(verbose=True, testDirs=model_ws) + modelname = "watertable" - model_ws = os.path.join("temp", "t020") - if not os.path.isdir(model_ws): - os.makedirs(model_ws, exist_ok=True) # model dimensions nlay, nrow, ncol = 1, 1, 100 @@ -172,6 +168,9 @@ def test_irch(): import os import flopy + model_ws = f"{baseDir}_test_tpl_constant" + testFramework = flopyTest(verbose=True, testDirs=model_ws) + org_model_ws = os.path.join( "..", "examples", "data", "freyberg_multilayer_transient" ) @@ -197,11 +196,11 @@ def test_irch(): d = arr - aarr assert np.abs(d).sum() == 0 - new_model_ws = "temp" - m.change_model_ws(new_model_ws) + m.change_model_ws(model_ws) m.write_input() + mm = flopy.modflow.Modflow.load( - nam_file, model_ws="temp", forgive=False, verbose=True, check=False + nam_file, model_ws=model_ws, forgive=False, verbose=True, check=False ) for kper in range(m.nper): arr = irch[kper] diff --git a/autotest/t021_test.py b/autotest/t021_test.py index f59ce11dca..c397521834 100644 --- a/autotest/t021_test.py +++ b/autotest/t021_test.py @@ -1,17 +1,19 @@ -# Test modflow write adn run import os import numpy as np import flopy +from ci_framework import baseTestDir, flopyTest -mpth = os.path.join("temp", "t021") -# make the directory if it does not exist -if not os.path.isdir(mpth): - os.makedirs(mpth, exist_ok=True) +baseDir = baseTestDir(__file__, relPath="temp", verbose=True) def test_mflist_external(): + model_ws = f"{baseDir}_test_mflist_external" + testFramework = flopyTest(verbose=True, testDirs=model_ws) + ml = flopy.modflow.Modflow( - "mflist_test", model_ws=mpth, external_path=os.path.join(mpth, "ref") + "mflist_test", + model_ws=model_ws, + external_path=os.path.join(model_ws, "ref"), ) dis = flopy.modflow.ModflowDis(ml, 1, 10, 10, nper=3, perlen=1.0) wel_data = { @@ -36,24 +38,24 @@ def test_mflist_external(): def test_single_mflist_entry_load(): + model_ws = f"{baseDir}_test_single_mflist_entry_load" + testFramework = flopyTest(verbose=True, testDirs=model_ws) - import os - import numpy as np - import flopy - - model_ws = os.path.join("..", "examples", "data", "freyberg") + pth = os.path.join("..", "examples", "data", "freyberg") m = flopy.modflow.Modflow.load( - "freyberg.nam", model_ws=model_ws, load_only=["WEL"], check=False + "freyberg.nam", model_ws=pth, load_only=["WEL"], check=False ) w = m.wel spd = w.stress_period_data flopy.modflow.ModflowWel(m, stress_period_data={0: [0, 0, 0, 0.0]}) m.external_path = "." - m.change_model_ws("temp", reset_external=True) + m.change_model_ws(model_ws, reset_external=True) m.write_input() mm = flopy.modflow.Modflow.load( - "freyberg.nam", model_ws="temp", forgive=False + "freyberg.nam", + model_ws=model_ws, + forgive=False, ) assert mm.wel.stress_period_data mm.write_input() diff --git a/autotest/t023_test.py b/autotest/t023_test.py index c300f1e430..0b7f325b73 100644 --- a/autotest/t023_test.py +++ b/autotest/t023_test.py @@ -2,21 +2,28 @@ import os import numpy as np import flopy +from ci_framework import baseTestDir, flopyTest -testpth = os.path.join(".", "temp", "t023") -# make the directory if it does not exist -if not os.path.isdir(testpth): - os.makedirs(testpth, exist_ok=True) +baseDir = baseTestDir(__file__, relPath="temp", verbose=True) def test_mt3d_multispecies(): + model_ws = f"{baseDir}_test_mt3d_multispecies" + testFramework = flopyTest( + verbose=True, + testDirs=model_ws, + ) + # modflow model modelname = "multispecies" nlay = 1 nrow = 20 ncol = 20 nper = 10 - mf = flopy.modflow.Modflow(modelname=modelname, model_ws=testpth) + mf = flopy.modflow.Modflow( + modelname=modelname, + model_ws=model_ws, + ) dis = flopy.modflow.ModflowDis( mf, nlay=nlay, nrow=nrow, ncol=ncol, nper=nper ) @@ -28,7 +35,10 @@ def test_mt3d_multispecies(): # Create a 5-component mt3d model and write the files ncomp = 5 mt = flopy.mt3d.Mt3dms( - modelname=modelname, modflowmodel=mf, model_ws=testpth, verbose=True + modelname=modelname, + modflowmodel=mf, + model_ws=model_ws, + verbose=True, ) sconc3 = np.random.random((nrow, ncol)) btn = flopy.mt3d.Mt3dBtn( @@ -57,14 +67,21 @@ def test_mt3d_multispecies(): # Create a second MODFLOW model modelname2 = "multispecies2" - mf2 = flopy.modflow.Modflow(modelname=modelname2, model_ws=testpth) + mf2 = flopy.modflow.Modflow( + modelname=modelname2, + model_ws=model_ws, + ) dis2 = flopy.modflow.ModflowDis( mf2, nlay=nlay, nrow=nrow, ncol=ncol, nper=nper ) # Load the MT3D model into mt2 and then write it out fname = f"{modelname}.nam" - mt2 = flopy.mt3d.Mt3dms.load(fname, model_ws=testpth, verbose=True) + mt2 = flopy.mt3d.Mt3dms.load( + fname, + model_ws=model_ws, + verbose=True, + ) # check obs I/O assert np.all(mt.btn.obs == mt2.btn.obs) mt2.name = modelname2 diff --git a/autotest/t024_test.py b/autotest/t024_test.py index b617590014..d1070fd2fe 100644 --- a/autotest/t024_test.py +++ b/autotest/t024_test.py @@ -2,16 +2,15 @@ import os import numpy as np import flopy +from ci_framework import baseTestDir, flopyTest -model_ws = os.path.join("..", "examples", "data", "mf2005_test") +baseDir = baseTestDir(__file__, relPath="temp", verbose=True) + +ex_pth = os.path.join("..", "examples", "data", "mf2005_test") testmodels = [ - os.path.join(model_ws, f) - for f in os.listdir(model_ws) - if f.endswith(".nam") + os.path.join(ex_pth, f) for f in os.listdir(ex_pth) if f.endswith(".nam") ] -mpth = os.path.join("temp", "t024") - @pytest.mark.parametrize( "namfile", @@ -33,7 +32,10 @@ def checker_on_load(mfnam): def test_bcs_check(): - mf = flopy.modflow.Modflow(version="mf2005", model_ws=mpth) + model_ws = f"{baseDir}_test_bcs_check" + testFramework = flopyTest(verbose=True, testDirs=model_ws) + + mf = flopy.modflow.Modflow(version="mf2005", model_ws=model_ws) # test check for isolated cells dis = flopy.modflow.ModflowDis( @@ -77,7 +79,13 @@ def test_bcs_check(): def test_properties_check(): # test that storage values ignored for steady state - mf = flopy.modflow.Modflow(version="mf2005", model_ws=mpth) + model_ws = f"{baseDir}_test_properties_check" + testFramework = flopyTest(verbose=True, testDirs=model_ws) + + mf = flopy.modflow.Modflow( + version="mf2005", + model_ws=model_ws, + ) dis = flopy.modflow.ModflowDis( mf, nrow=2, diff --git a/autotest/t025_test.py b/autotest/t025_test.py index 09070fb836..427159519f 100644 --- a/autotest/t025_test.py +++ b/autotest/t025_test.py @@ -5,10 +5,12 @@ import pytest import os import flopy +import pymake +from ci_framework import baseTestDir, flopyTest -path = os.path.join("..", "examples", "data", "mf2005_test") -cpth = os.path.join("temp", "t025") +baseDir = baseTestDir(__file__, relPath="temp", verbose=True) +load_path = os.path.join("..", "examples", "data", "mf2005_test") mf_items = [ "l1b2k_bath.nam", "l2a_2k.nam", @@ -16,9 +18,6 @@ "l1b2k.nam", "l1a2k.nam", ] -pths = [] -for mi in mf_items: - pths.append(path) exe_name = "mf2005" v = flopy.which(exe_name) @@ -28,22 +27,17 @@ run = False -def load_lak(mfnam, pth, run): - try: - import pymake +def load_lak(mfnam, model_ws, run): - lpth = os.path.join(cpth, os.path.splitext(mfnam)[0]) - apth = os.path.join(lpth, "flopy") - compth = lpth - pymake.setup(os.path.join(pth, mfnam), lpth) - except: - run = False - lpth = pth - apth = cpth - compth = cpth + compth = model_ws + pymake.setup(os.path.join(load_path, mfnam), model_ws) m = flopy.modflow.Modflow.load( - mfnam, model_ws=lpth, verbose=True, forgive=False, exe_name=exe_name + mfnam, + model_ws=model_ws, + verbose=True, + forgive=False, + exe_name=exe_name, ) assert m.load_fail is False @@ -63,16 +57,18 @@ def load_lak(mfnam, pth, run): "run terminated successfully" ) print(msg) - fn0 = os.path.join(lpth, mfnam) + fn0 = os.path.join(model_ws, mfnam) # write free format files - wont run without resetting to free format - evt external file issue m.free_format_input = True # rewrite files + model_ws2 = os.path.join(model_ws, "external") m.change_model_ws( - apth, reset_external=True + model_ws2, reset_external=True ) # l1b2k_bath wont run without this m.write_input() + if run: try: success, buff = m.run_model(silent=False) @@ -89,9 +85,10 @@ def load_lak(mfnam, pth, run): "run terminated successfully" ) print(msg) - fn1 = os.path.join(apth, mfnam) + fn1 = os.path.join(model_ws2, mfnam) fsum = os.path.join(compth, f"{os.path.splitext(mfnam)[0]}.budget.out") + if run: try: success = pymake.compare_budget( @@ -106,14 +103,20 @@ def load_lak(mfnam, pth, run): @pytest.mark.parametrize( - "namfile, pth", - zip(mf_items, pths), + "namfile", + mf_items, ) -def test_mf2005load(namfile, pth): - load_lak(namfile, pth, run) +def test_mf2005load(namfile): + dirPath = namfile.replace(".nam", "") + model_ws = f"{baseDir}_{dirPath}" + testFramework = flopyTest(verbose=True, testDirs=model_ws) + testFramework.addTestDir(os.path.join(model_ws, "external")) + + load_lak(namfile, model_ws, run) + return if __name__ == "__main__": - for namfile, pth in zip(mf_items, pths): - load_lak(namfile, pth, run) + for namfile in mf_items: + test_mf2005load(namfile) From 0ddd4e701fb2ba51b10b5f01a356d7cddc1f5456 Mon Sep 17 00:00:00 2001 From: Joseph D Hughes Date: Sat, 30 Oct 2021 13:27:48 -0500 Subject: [PATCH 2/4] ci: convert addition autotests to use flopyTest testing framework --- autotest/t016_test.py | 1 - autotest/t026_test.py | 28 ++++++++++---- autotest/t027_test.py | 69 +++++++++++++++++++++------------ autotest/t028_test.py | 32 +++++++++------- autotest/t029_test.py | 31 +++++++++++---- autotest/t030_test.py | 11 +++--- autotest/t031_test.py | 88 ++++++++++++++++++++++++++++--------------- 7 files changed, 170 insertions(+), 90 deletions(-) diff --git a/autotest/t016_test.py b/autotest/t016_test.py index 60db26b246..4ec610a5a0 100644 --- a/autotest/t016_test.py +++ b/autotest/t016_test.py @@ -1,7 +1,6 @@ import os import flopy import numpy as np - from ci_framework import baseTestDir, flopyTest baseDir = baseTestDir(__file__, relPath="temp", verbose=True) diff --git a/autotest/t026_test.py b/autotest/t026_test.py index ffc7f1836d..0d6e2dbd3c 100644 --- a/autotest/t026_test.py +++ b/autotest/t026_test.py @@ -6,11 +6,9 @@ import os import numpy as np import flopy +from ci_framework import baseTestDir, flopyTest -workspace = os.path.join("temp", "t026") -# make the directory if it does not exist -if not os.path.isdir(workspace): - os.makedirs(workspace, exist_ok=True) +baseDir = baseTestDir(__file__, relPath="temp", verbose=True) seawat_exe = "swtv4" isseawat = flopy.which(seawat_exe) @@ -47,8 +45,11 @@ def test_seawat_henry(): # SEAWAT model from a modflow model and an mt3d model + model_ws = f"{baseDir}_test_seawat_henry" + testFramework = flopyTest(verbose=True, testDirs=model_ws) + modelname = "henry" - mf = flopy.modflow.Modflow(modelname, exe_name="swtv4", model_ws=workspace) + mf = flopy.modflow.Modflow(modelname, exe_name="swtv4", model_ws=model_ws) # shortened perlen to 0.1 to make this run faster -- should be about 0.5 dis = flopy.modflow.ModflowDis( mf, @@ -75,7 +76,7 @@ def test_seawat_henry(): ) # Create the basic MT3DMS model structure - mt = flopy.mt3d.Mt3dms(modelname, "nam_mt3dms", mf, model_ws=workspace) + mt = flopy.mt3d.Mt3dms(modelname, "nam_mt3dms", mf, model_ws=model_ws) btn = flopy.mt3d.Mt3dBtn( mt, nprs=-5, @@ -94,7 +95,12 @@ def test_seawat_henry(): # Create the SEAWAT model structure mswt = flopy.seawat.Seawat( - modelname, "nam_swt", mf, mt, model_ws=workspace, exe_name="swtv4" + modelname, + "nam_swt", + mf, + mt, + model_ws=model_ws, + exe_name="swtv4", ) vdf = flopy.seawat.SeawatVdf( mswt, @@ -120,9 +126,15 @@ def test_seawat_henry(): def test_seawat2_henry(): # SEAWAT model directly by adding packages + model_ws = f"{baseDir}_test_seawat2_henry" + testFramework = flopyTest(verbose=True, testDirs=model_ws) + modelname = "henry2" m = flopy.seawat.swt.Seawat( - modelname, "nam", model_ws=workspace, exe_name="swtv4" + modelname, + "nam", + model_ws=model_ws, + exe_name="swtv4", ) dis = flopy.modflow.ModflowDis( m, diff --git a/autotest/t027_test.py b/autotest/t027_test.py index e6a26eed60..564d4ebeef 100644 --- a/autotest/t027_test.py +++ b/autotest/t027_test.py @@ -9,11 +9,10 @@ import flopy import numpy as np from flopy.utils.flopy_io import line_parse +from ci_framework import baseTestDir, flopyTest + +baseDir = baseTestDir(__file__, relPath="temp", verbose=True) -cpth = os.path.join("temp", "t027") -# make the directory if it does not exist -if not os.path.isdir(cpth): - os.makedirs(cpth, exist_ok=True) mf2005pth = os.path.join("..", "examples", "data", "mnw2_examples") mnw1_path = os.path.join("..", "examples", "data", "mf2005_test") @@ -28,22 +27,25 @@ def test_line_parse(): def test_load(): """t027 test load of MNW2 Package""" + model_ws = f"{baseDir}_test_load" + testFramework = flopyTest(verbose=True, testDirs=model_ws) + # load in the test problem (1 well, 3 stress periods) m = flopy.modflow.Modflow.load( "MNW2-Fig28.nam", model_ws=mf2005pth, verbose=True, forgive=False ) - m.change_model_ws(cpth) + m.change_model_ws(model_ws) assert m.has_package("MNW2") assert m.has_package("MNWI") # load a real mnw2 package from a steady state model (multiple wells) - m2 = flopy.modflow.Modflow("br", model_ws=cpth) + m2 = flopy.modflow.Modflow("br", model_ws=model_ws) path = os.path.join("..", "examples", "data", "mnw2_examples") mnw2_2 = flopy.modflow.ModflowMnw2.load(f"{path}/BadRiver_cal.mnw2", m2) - mnw2_2.write_file(os.path.join(cpth, "brtest.mnw2")) + mnw2_2.write_file(os.path.join(model_ws, "brtest.mnw2")) - m3 = flopy.modflow.Modflow("br", model_ws=cpth) - mnw2_3 = flopy.modflow.ModflowMnw2.load(f"{cpth}/brtest.mnw2", m3) + m3 = flopy.modflow.Modflow("br", model_ws=model_ws) + mnw2_3 = flopy.modflow.ModflowMnw2.load(f"{model_ws}/brtest.mnw2", m3) mnw2_2.node_data.sort(order="wellid") mnw2_3.node_data.sort(order="wellid") assert np.array_equal(mnw2_2.node_data, mnw2_3.node_data) @@ -60,6 +62,9 @@ def test_load(): def test_mnw1_load_write(): + model_ws = f"{baseDir}_test_mnw1_load_write" + testFramework = flopyTest(verbose=True, testDirs=model_ws, create=True) + m = flopy.modflow.Modflow.load( "mnw1.nam", model_ws=mnw1_path, @@ -73,14 +78,17 @@ def test_mnw1_load_write(): assert len(m.mnw1.stress_period_data[i]) == 17 assert len(np.unique(m.mnw1.stress_period_data[i]["mnw_no"])) == 15 assert len(set(m.mnw1.stress_period_data[i]["label"])) == 4 - shutil.copy(f"{mnw1_path}/mnw1.nam", cpth) - shutil.copy(f"{mnw1_path}/mnw1.dis", cpth) - shutil.copy(f"{mnw1_path}/mnw1.bas", cpth) - m.mnw1.fn_path = f"{cpth}/mnw1.mnw" + + shutil.copy(f"{mnw1_path}/mnw1.nam", model_ws) + shutil.copy(f"{mnw1_path}/mnw1.dis", model_ws) + shutil.copy(f"{mnw1_path}/mnw1.bas", model_ws) + + m.mnw1.fn_path = f"{model_ws}/mnw1.mnw" m.mnw1.write_file() + m2 = flopy.modflow.Modflow.load( "mnw1.nam", - model_ws=cpth, + model_ws=model_ws, load_only=["mnw1"], verbose=True, forgive=False, @@ -91,7 +99,10 @@ def test_mnw1_load_write(): def test_make_package(): """t027 test make MNW2 Package""" - m4 = flopy.modflow.Modflow("mnw2example", model_ws=cpth) + model_ws = f"{baseDir}_test_make_package" + testFramework = flopyTest(verbose=True, testDirs=model_ws) + + m4 = flopy.modflow.Modflow("mnw2example", model_ws=model_ws) dis = flopy.modflow.ModflowDis( nrow=5, ncol=5, nlay=3, nper=3, top=10, botm=0, model=m4 ) @@ -282,7 +293,7 @@ def test_make_package(): mnw2_4.stress_period_data[1], mnw2fromobj.stress_period_data[1] ) # verify that loaded package is the same - m5 = flopy.modflow.Modflow("mnw2example", model_ws=cpth) + m5 = flopy.modflow.Modflow("mnw2example", model_ws=model_ws) dis = flopy.modflow.ModflowDis( nrow=5, ncol=5, nlay=3, nper=3, top=10, botm=0, model=m5 ) @@ -299,6 +310,9 @@ def test_mnw2_create_file(): """ import pandas as pd + model_ws = f"{baseDir}_test_mnw2_create_file" + testFramework = flopyTest(verbose=True, testDirs=model_ws, create=True) + mf = flopy.modflow.Modflow("test_mfmnw2", exe_name="mf2005") wellids = [1, 2] nlayers = [2, 4] @@ -359,7 +373,7 @@ def test_mnw2_create_file(): if len(mnw2.node_data) != 6: raise AssertionError("Node data not properly set") - mnw2.write_file("./temp/t027/ndata.mnw2") + mnw2.write_file(os.path.join(model_ws, "ndata.mnw2")) def test_export(): @@ -368,6 +382,10 @@ def test_export(): import netCDF4 except: netCDF4 = None + + model_ws = f"{baseDir}_test_export" + testFramework = flopyTest(verbose=True, testDirs=model_ws, create=True) + m = flopy.modflow.Modflow.load( "MNW2-Fig28.nam", model_ws=mf2005pth, @@ -378,13 +396,13 @@ def test_export(): # netDF4 tests if netCDF4 is not None: - fcw = m.wel.export(os.path.join(cpth, "MNW2-Fig28_well.nc")) + fcw = m.wel.export(os.path.join(model_ws, "MNW2-Fig28_well.nc")) fcw.write() - fpth = os.path.join(cpth, "MNW2-Fig28.nc") + fpth = os.path.join(model_ws, "MNW2-Fig28.nc") # test context statement with m.mnw2.export(fpth): pass - fpth = os.path.join(cpth, "MNW2-Fig28.nc") + fpth = os.path.join(model_ws, "MNW2-Fig28.nc") nc = netCDF4.Dataset(fpth) assert np.array_equal( nc.variables["mnw2_qdes"][:, 0, 29, 40], @@ -396,6 +414,9 @@ def test_export(): def test_blank_lines(): + model_ws = f"{baseDir}_test_blank_lines" + testFramework = flopyTest(verbose=True, testDirs=model_ws, create=True) + mnw2str = """3 50 0 EB-33 -3 SKIN -1 0 0 0 @@ -425,7 +446,7 @@ def test_blank_lines(): eb-36 -534.72 """ - fpth = os.path.join(cpth, "mymnw2.mnw2") + fpth = os.path.join(model_ws, "mymnw2.mnw2") f = open(fpth, "w") f.write(mnw2str) f.close() @@ -441,7 +462,7 @@ def test_blank_lines(): 1. 1 1. Tr PERLEN NSTP TSMULT Ss/tr """ - fpth = os.path.join(cpth, "mymnw2.dis") + fpth = os.path.join(model_ws, "mymnw2.dis") f = open(fpth, "w") f.write(disstr) f.close() @@ -450,13 +471,13 @@ def test_blank_lines(): dis 102 mymnw2.dis mnw2 103 mymnw2.mnw2""" - fpth = os.path.join(cpth, "mymnw2.nam") + fpth = os.path.join(model_ws, "mymnw2.nam") f = open(fpth, "w") f.write(namstr) f.close() m = flopy.modflow.Modflow.load( - "mymnw2.nam", model_ws=cpth, verbose=True, check=False + "mymnw2.nam", model_ws=model_ws, verbose=True, check=False ) mnw2 = m.mnw2 wellids = ["eb-33", "eb-35", "eb-36"] diff --git a/autotest/t028_test.py b/autotest/t028_test.py index edfddf608c..331456e046 100644 --- a/autotest/t028_test.py +++ b/autotest/t028_test.py @@ -2,12 +2,12 @@ import os import shutil import flopy +from ci_framework import baseTestDir, flopyTest + +baseDir = baseTestDir(__file__, relPath="temp", verbose=True) pthtest = os.path.join("..", "examples", "data", "swtv4_test") -newpth = os.path.join(".", "temp", "t028") -# make the directory if it does not exist -if not os.path.isdir(newpth): - os.makedirs(newpth, exist_ok=True) + swtv4_exe = "swtv4" isswtv4 = flopy.which(swtv4_exe) runmodel = False @@ -61,19 +61,23 @@ def test_seawat_array_format(): + testFramework = flopyTest(verbose=True) + d = "2_henry" subds = ["1_classic_case1"] for subd in subds: pth = os.path.join(pthtest, d, subd) - testpth = os.path.join(newpth, f"{d}-{subd}") - if os.path.isdir(testpth): - shutil.rmtree(testpth) - os.makedirs(testpth) + + model_ws = os.path.join( + f"{baseDir}_test_seawat_array_format_{d}-{subd}" + ) + testFramework.addTestDir(model_ws) + namfile = "seawat.nam" if subd == "6_age_simulation": namfile = "henry_mod.nam" m = flopy.seawat.Seawat.load(namfile, model_ws=pth, verbose=verbose) - m.change_model_ws(testpth, reset_external=True) + m.change_model_ws(model_ws, reset_external=True) m.bcf6.hy[0].fmtin = "(BINARY)" m.btn.prsity[0].fmtin = "(BINARY)" @@ -94,12 +98,12 @@ def test_swtv4(d, subd): def run_swtv4(d, subd): + testFramework = flopyTest(verbose=True) + model_ws = os.path.join(f"{baseDir}_test_swtv4_{d}-{subd}") + testFramework.addTestDir(model_ws) + # set up paths pth = os.path.join(pthtest, d, subd) - testpth = os.path.join(newpth, f"{d}-{subd}") - if os.path.isdir(testpth): - shutil.rmtree(testpth) - os.makedirs(testpth) namfile = "seawat.nam" if subd == "6_age_simulation": @@ -109,7 +113,7 @@ def run_swtv4(d, subd): m = flopy.seawat.swt.Seawat.load(namfile, model_ws=pth, verbose=verbose) # change working directory - m.change_model_ws(testpth) + m.change_model_ws(model_ws) # write input files m.write_input() diff --git a/autotest/t029_test.py b/autotest/t029_test.py index 2328212c19..724f45ed5e 100644 --- a/autotest/t029_test.py +++ b/autotest/t029_test.py @@ -7,13 +7,13 @@ import flopy +from ci_framework import baseTestDir, flopyTest + +baseDir = baseTestDir(__file__, relPath="temp", verbose=True) + pthtest = os.path.join("..", "examples", "data", "mfgrd_test") flowpth = os.path.join("..", "examples", "data", "mf6-freyberg") -tpth = os.path.join("temp", "t029") -if not os.path.isdir(tpth): - os.makedirs(tpth, exist_ok=True) - def test_mfgrddis_MfGrdFile(): fn = os.path.join(pthtest, "nwtp3.dis.grb") @@ -201,6 +201,9 @@ def test_mfgrddisu_modelgrid(): def test_faceflows(): + model_ws = f"{baseDir}_test_faceflows" + testFramework = flopyTest(verbose=True, testDirs=model_ws) + sim = flopy.mf6.MFSimulation.load( sim_name="freyberg", exe_name="mf6", @@ -208,7 +211,7 @@ def test_faceflows(): ) # change the simulation workspace - sim.set_sim_path(tpth) + sim.set_sim_path(model_ws) # write the model simulation files sim.write_simulation() @@ -226,7 +229,7 @@ def test_faceflows(): frf, fff, flf = flopy.mf6.utils.get_structured_faceflows( flowja, - grb_file=os.path.join(tpth, "freyberg.dis.grb"), + grb_file=os.path.join(model_ws, "freyberg.dis.grb"), ) Qx, Qy, Qz = flopy.utils.postprocessing.get_specific_discharge( (frf, fff, flf), @@ -274,15 +277,27 @@ def test_faceflows(): def test_flowja_residuals(): + model_ws = f"{baseDir}_test_flowja_residuals" + testFramework = flopyTest(verbose=True, testDirs=model_ws) + sim = flopy.mf6.MFSimulation.load( sim_name="freyberg", exe_name="mf6", - sim_ws=tpth, + sim_ws=flowpth, ) + # change the simulation workspace + sim.set_sim_path(model_ws) + + # write the model simulation files + sim.write_simulation() + + # run the simulation + sim.run_simulation() + # get output gwf = sim.get_model("freyberg") - grb_file = os.path.join(tpth, "freyberg.dis.grb") + grb_file = os.path.join(model_ws, "freyberg.dis.grb") cbc = gwf.output.budget() spdis = cbc.get_data(text="DATA-SPDIS")[0] diff --git a/autotest/t030_test.py b/autotest/t030_test.py index 31b3f6073b..b688c4b256 100644 --- a/autotest/t030_test.py +++ b/autotest/t030_test.py @@ -1,18 +1,19 @@ import os import flopy +from ci_framework import baseTestDir, flopyTest -newpth = os.path.join(".", "temp", "t030") -# make the directory if it does not exist -if not os.path.isdir(newpth): - os.makedirs(newpth, exist_ok=True) +baseDir = baseTestDir(__file__, relPath="temp", verbose=True) def test_vdf_vsc(): + model_ws = f"{baseDir}_test_vdf_vsc" + testFramework = flopyTest(verbose=True, testDirs=model_ws) + nlay = 3 nrow = 4 ncol = 5 nper = 3 - m = flopy.seawat.Seawat(modelname="vdftest", model_ws=newpth) + m = flopy.seawat.Seawat(modelname="vdftest", model_ws=model_ws) dis = flopy.modflow.ModflowDis( m, nlay=nlay, nrow=nrow, ncol=ncol, nper=nper ) diff --git a/autotest/t031_test.py b/autotest/t031_test.py index 24523f0623..baa9d16b56 100644 --- a/autotest/t031_test.py +++ b/autotest/t031_test.py @@ -13,6 +13,7 @@ from flopy.utils.modpathfile import EndpointFile, PathlineFile from flopy.utils.recarray_utils import ra_slice from flopy.modpath.mp6sim import StartingLocationsFile +from ci_framework import baseTestDir, flopyTest try: import shapefile @@ -22,17 +23,31 @@ except ImportError: shapefile = None -mffiles = glob.glob("../examples/data/mp6/EXAMPLE*") -path = os.path.join("temp", "t031") +ex_pth = "../examples/data/mp6/" -if not os.path.isdir(path): - os.makedirs(path, exist_ok=True) -for f in mffiles: - shutil.copy(f, os.path.join(path, os.path.split(f)[1])) +baseDir = baseTestDir(__file__, relPath="temp", verbose=True) + + +def copy_modpath_files(model_ws, baseName): + files = [ + file + for file in os.listdir(ex_pth) + if file.startswith(baseName) + and os.path.isfile(os.path.join(ex_pth, file)) + ] + for file in files: + src = os.path.join(ex_pth, file) + dst = os.path.join(model_ws, file) + print(f"copying {src} -> {dst}") + shutil.copy(src, dst) def test_mpsim(): - model_ws = path + model_ws = f"{baseDir}_test_mpsim" + testFramework = flopyTest(verbose=True, testDirs=model_ws, create=True) + + copy_modpath_files(model_ws, "EXAMPLE.") + m = flopy.modflow.Modflow.load("EXAMPLE.nam", model_ws=model_ws) m.get_package_list() @@ -40,7 +55,7 @@ def test_mpsim(): modelname="ex6", exe_name="mp6", modflowmodel=m, - model_ws=path, + model_ws=model_ws, dis_file=f"{m.name}.dis", head_file=f"{m.name}.hed", budget_file=f"{m.name}.bud", @@ -110,14 +125,20 @@ def test_mpsim(): stldata[1]["yloc0"] = 0.2 stl.data = stldata mp.write_input() - stllines = open(os.path.join(path, "ex6.loc")).readlines() + stllines = open(os.path.join(model_ws, "ex6.loc")).readlines() assert stllines[3].strip() == "group1" assert int(stllines[4].strip()) == 2 assert stllines[6].strip().split()[-1] == "p2" def test_get_destination_data(): - m = flopy.modflow.Modflow.load("EXAMPLE.nam", model_ws=path) + model_ws = f"{baseDir}_test_get_destination_data" + testFramework = flopyTest(verbose=True, testDirs=model_ws, create=True) + + copy_modpath_files(model_ws, "EXAMPLE.") + copy_modpath_files(model_ws, "EXAMPLE-3.") + + m = flopy.modflow.Modflow.load("EXAMPLE.nam", model_ws=model_ws) mg1 = m.modelgrid mg1.set_coord_info( @@ -135,10 +156,10 @@ def test_get_destination_data(): # test deprecation if shapefile: - m.dis.export(f"{path}/dis.shp") + m.dis.export(f"{model_ws}/dis.shp") - pthld = PathlineFile(os.path.join(path, "EXAMPLE-3.pathline")) - epd = EndpointFile(os.path.join(path, "EXAMPLE-3.endpoint")) + pthld = PathlineFile(os.path.join(model_ws, "EXAMPLE-3.pathline")) + epd = EndpointFile(os.path.join(model_ws, "EXAMPLE-3.endpoint")) well_epd = epd.get_destination_endpoint_data(dest_cells=[(4, 12, 12)]) well_pthld = pthld.get_destination_pathline_data( @@ -166,12 +187,12 @@ def test_get_destination_data(): epd.write_shapefile( well_epd, direction="starting", - shpname=os.path.join(path, "starting_locs.shp"), + shpname=os.path.join(model_ws, "starting_locs.shp"), mg=m.modelgrid, ) # test writing shapefile of pathlines - fpth = os.path.join(path, "pathlines_1per.shp") + fpth = os.path.join(model_ws, "pathlines_1per.shp") pthld.write_shapefile( well_pthld, one_per_particle=True, @@ -179,7 +200,7 @@ def test_get_destination_data(): mg=m.modelgrid, shpname=fpth, ) - fpth = os.path.join(path, "pathlines_1per_end.shp") + fpth = os.path.join(model_ws, "pathlines_1per_end.shp") pthld.write_shapefile( well_pthld, one_per_particle=True, @@ -188,7 +209,7 @@ def test_get_destination_data(): shpname=fpth, ) # test writing shapefile of pathlines - fpth = os.path.join(path, "pathlines_1per2.shp") + fpth = os.path.join(model_ws, "pathlines_1per2.shp") pthld.write_shapefile( well_pthld, one_per_particle=True, @@ -197,7 +218,7 @@ def test_get_destination_data(): shpname=fpth, ) # test writing shapefile of pathlines - fpth = os.path.join(path, "pathlines_1per2_ll.shp") + fpth = os.path.join(model_ws, "pathlines_1per2_ll.shp") pthld.write_shapefile( well_pthld, one_per_particle=True, @@ -205,7 +226,7 @@ def test_get_destination_data(): mg=mg, shpname=fpth, ) - fpth = os.path.join(path, "pathlines.shp") + fpth = os.path.join(model_ws, "pathlines.shp") pthld.write_shapefile( well_pthld, one_per_particle=False, mg=m.modelgrid, shpname=fpth ) @@ -213,7 +234,7 @@ def test_get_destination_data(): # test that endpoints were rotated and written correctly from flopy.export.shapefile_utils import shp2recarray - ra = shp2recarray(os.path.join(path, "starting_locs.shp")) + ra = shp2recarray(os.path.join(model_ws, "starting_locs.shp")) p3 = ra.geometry[ra.particleid == 4][0] xorig, yorig = m.modelgrid.get_coords(well_epd.x0[0], well_epd.y0[0]) assert p3.x - xorig + p3.y - yorig < 1e-4 @@ -223,16 +244,16 @@ def test_get_destination_data(): ) # this also checks for 1-based # test that particle attribute information is consistent with pathline file - ra = shp2recarray(os.path.join(path, "pathlines.shp")) + ra = shp2recarray(os.path.join(model_ws, "pathlines.shp")) inds = (ra.particleid == 8) & (ra.i == 12) & (ra.j == 12) assert ra.time[inds][0] - 20181.7 < 0.1 assert ra.xloc[inds][0] - 0.933 < 0.01 # test that k, i, j are correct for single geometry pathlines, forwards # and backwards - ra = shp2recarray(os.path.join(path, "pathlines_1per.shp")) + ra = shp2recarray(os.path.join(model_ws, "pathlines_1per.shp")) assert ra.i[0] == 4, ra.j[0] == 5 - ra = shp2recarray(os.path.join(path, "pathlines_1per_end.shp")) + ra = shp2recarray(os.path.join(model_ws, "pathlines_1per_end.shp")) assert ra.i[0] == 13, ra.j[0] == 13 # test use of arbitrary spatial reference and offset @@ -243,7 +264,7 @@ def test_get_destination_data(): epsg=mg.epsg, proj4=mg.proj4, ) - ra = shp2recarray(os.path.join(path, "pathlines_1per2.shp")) + ra = shp2recarray(os.path.join(model_ws, "pathlines_1per2.shp")) p3_2 = ra.geometry[ra.particleid == 4][0] test1 = mg1.xcellcenters[3, 4] test2 = mg1.ycellcenters[3, 4] @@ -258,7 +279,7 @@ def test_get_destination_data(): ) # arbitrary spatial reference with ll specified instead of ul - ra = shp2recarray(os.path.join(path, "pathlines_1per2_ll.shp")) + ra = shp2recarray(os.path.join(model_ws, "pathlines_1per2_ll.shp")) p3_2 = ra.geometry[ra.particleid == 4][0] mg.set_coord_info(xoff=mg.xoffset, yoff=mg.yoffset, angrot=30.0) assert ( @@ -274,7 +295,7 @@ def test_get_destination_data(): xul = 3628793 yul = 21940389 - m = flopy.modflow.Modflow.load("EXAMPLE.nam", model_ws=path) + m = flopy.modflow.Modflow.load("EXAMPLE.nam", model_ws=model_ws) mg4 = m.modelgrid mg4.set_coord_info( @@ -285,17 +306,24 @@ def test_get_destination_data(): proj4=mg4.proj4, ) - fpth = os.path.join(path, "dis2.shp") + fpth = os.path.join(model_ws, "dis2.shp") m.dis.export(fpth) - pthobj = flopy.utils.PathlineFile(os.path.join(path, "EXAMPLE-3.pathline")) - fpth = os.path.join(path, "pathlines_1per3.shp") + pthobj = flopy.utils.PathlineFile( + os.path.join(model_ws, "EXAMPLE-3.pathline") + ) + fpth = os.path.join(model_ws, "pathlines_1per3.shp") pthobj.write_shapefile(shpname=fpth, direction="ending", mg=mg4) def test_loadtxt(): from flopy.utils.flopy_io import loadtxt - pthfile = os.path.join(path, "EXAMPLE-3.pathline") + model_ws = f"{baseDir}_test_loadtxt" + testFramework = flopyTest(verbose=True, testDirs=model_ws, create=True) + + copy_modpath_files(model_ws, "EXAMPLE-3.") + + pthfile = os.path.join(model_ws, "EXAMPLE-3.pathline") pthld = PathlineFile(pthfile) ra = loadtxt(pthfile, delimiter=" ", skiprows=3, dtype=pthld.dtype) ra2 = loadtxt( From f004df5e192bc59a7dca48f8a5a993d1dc01eff3 Mon Sep 17 00:00:00 2001 From: Joseph D Hughes Date: Sat, 30 Oct 2021 20:53:37 -0500 Subject: [PATCH 3/4] ci: convert addition autotests to use flopyTest testing framework --- autotest/t032_test.py | 29 +++++---- autotest/t033_test.py | 22 +++---- autotest/t034_test.py | 131 ++++++++++++++++++++++---------------- autotest/t035_test.py | 84 ++++++++++++------------ autotest/t036_test.py | 99 ++++++++++++----------------- autotest/t037_test.py | 74 ++++++++++------------ autotest/t038_test.py | 28 ++++---- autotest/t040_test.py | 144 +++++++++++++++++++++--------------------- 8 files changed, 307 insertions(+), 304 deletions(-) diff --git a/autotest/t032_test.py b/autotest/t032_test.py index 95d6297847..7a3b3a4f8a 100644 --- a/autotest/t032_test.py +++ b/autotest/t032_test.py @@ -4,7 +4,6 @@ import os import shutil import numpy as np -import sys import flopy from flopy.utils.geometry import Polygon from flopy.export.shapefile_utils import ( @@ -14,16 +13,18 @@ CRS, ) from flopy.export.netcdf import NetCdf +from ci_framework import baseTestDir, flopyTest -mpth = os.path.join("temp", "t032") -# make the directory if it does not exist -if not os.path.isdir(mpth): - os.makedirs(mpth, exist_ok=True) +baseDir = baseTestDir(__file__, relPath="temp", verbose=True) def test_polygon_from_ij(): """test creation of a polygon from an i, j location using get_vertices().""" - m = flopy.modflow.Modflow("toy_model", model_ws=mpth) + model_ws = f"{baseDir}_test_polygon_from_ij" + testFramework = flopyTest(verbose=True, testDirs=model_ws) + + m = flopy.modflow.Modflow("toy_model", model_ws=model_ws) + botm = np.zeros((2, 10, 10)) botm[0, :, :] = 1.5 botm[1, 5, 5] = 4 # negative layer thickness! @@ -32,14 +33,14 @@ def test_polygon_from_ij(): nrow=10, ncol=10, nlay=2, delr=100, delc=100, top=3, botm=botm, model=m ) - fname = os.path.join(mpth, "toy.model.nc") + fname = os.path.join(model_ws, "toy.model.nc") ncdf = NetCdf(fname, m) ncdf.write() - fname = os.path.join(mpth, "toy_model_two.nc") + fname = os.path.join(model_ws, "toy_model_two.nc") m.export(fname) - fname = os.path.join(mpth, "toy_model_dis.nc") + fname = os.path.join(model_ws, "toy_model_dis.nc") dis.export(fname) mg = m.modelgrid @@ -74,19 +75,19 @@ def test_polygon_from_ij(): assert geoms[0].type == "Polygon" assert np.abs(geoms[0].bounds[-1] - 5169292.893203464) < 1e-4 - fpth = os.path.join(mpth, "test.shp") + fpth = os.path.join(model_ws, "test.shp") recarray2shp(recarray, geoms, fpth, epsg=26715) ep = EpsgReference() prj = ep.to_dict() assert 26715 in prj - fpth = os.path.join(mpth, "test.prj") - fpth2 = os.path.join(mpth, "26715.prj") + fpth = os.path.join(model_ws, "test.prj") + fpth2 = os.path.join(model_ws, "26715.prj") shutil.copy(fpth, fpth2) - fpth = os.path.join(mpth, "test.shp") + fpth = os.path.join(model_ws, "test.shp") recarray2shp(recarray, geoms, fpth, prj=fpth2) # test_dtypes - fpth = os.path.join(mpth, "test.shp") + fpth = os.path.join(model_ws, "test.shp") ra = shp2recarray(fpth) assert "int" in ra.dtype["k"].name assert "float" in ra.dtype["stuff"].name diff --git a/autotest/t033_test.py b/autotest/t033_test.py index 7f93bb82c9..73a552d238 100644 --- a/autotest/t033_test.py +++ b/autotest/t033_test.py @@ -2,22 +2,22 @@ import numpy as np import flopy from flopy.utils.util_array import Util2d +from ci_framework import baseTestDir, flopyTest -newpth = os.path.join(".", "temp", "t033") -# make the directory if it does not exist -if not os.path.isdir(newpth): - os.makedirs(newpth, exist_ok=True) -startpth = os.getcwd() +baseDir = baseTestDir(__file__, relPath="temp", verbose=True) def test_rchload(): + model_ws = f"{baseDir}_test_rchload" + testFramework = flopyTest(verbose=True, testDirs=model_ws) + nlay = 2 nrow = 3 ncol = 4 nper = 2 # create model 1 - m1 = flopy.modflow.Modflow("rchload1", model_ws=newpth) + m1 = flopy.modflow.Modflow("rchload1", model_ws=model_ws) dis1 = flopy.modflow.ModflowDis( m1, nlay=nlay, nrow=nrow, ncol=ncol, nper=nper ) @@ -29,16 +29,14 @@ def test_rchload(): m1.write_input() # load model 1 - os.chdir(newpth) - m1l = flopy.modflow.Modflow.load("rchload1.nam") + m1l = flopy.modflow.Modflow.load("rchload1.nam", model_ws=model_ws) a1 = rech1.array a2 = m1l.rch.rech[0].array assert np.allclose(a1, a2) a2 = m1l.rch.rech[1].array assert np.allclose(a1, a2) - os.chdir(startpth) - m2 = flopy.modflow.Modflow("rchload2", model_ws=newpth) + m2 = flopy.modflow.Modflow("rchload2", model_ws=model_ws) dis2 = flopy.modflow.ModflowDis( m2, nlay=nlay, nrow=nrow, ncol=ncol, nper=nper ) @@ -50,14 +48,12 @@ def test_rchload(): m2.write_input() # load model 2 - os.chdir(newpth) - m2l = flopy.modflow.Modflow.load("rchload2.nam") + m2l = flopy.modflow.Modflow.load("rchload2.nam", model_ws=model_ws) a1 = rech2.array a2 = m2l.rch.rech[0].array assert np.allclose(a1, a2) a2 = m2l.rch.rech[1].array assert np.allclose(a1, a2) - os.chdir(startpth) if __name__ == "__main__": diff --git a/autotest/t034_test.py b/autotest/t034_test.py index c9806dee02..040d900f2c 100644 --- a/autotest/t034_test.py +++ b/autotest/t034_test.py @@ -1,31 +1,32 @@ """ test UZF package """ -import sys - -sys.path.insert(0, "..") import os import shutil import glob import flopy from flopy.utils.util_array import Util2d import numpy as np -import sys +from ci_framework import baseTestDir, flopyTest + +baseDir = baseTestDir(__file__, relPath="temp", verbose=True) -cpth = os.path.join("temp", "t034") -if not os.path.isdir(cpth): - os.makedirs(cpth, exist_ok=True) +def test_create_uzf(): + model_ws = f"{baseDir}_test_create_uzf" + testFramework = flopyTest(verbose=True, testDirs=model_ws, create=True) -def test_create(): + # copy the test files gpth = os.path.join("..", "examples", "data", "mf2005_test", "UZFtest2.*") for f in glob.glob(gpth): - shutil.copy(f, cpth) + shutil.copy(f, model_ws) + + # load the model m = flopy.modflow.Modflow.load( "UZFtest2.nam", version="mf2005", exe_name="mf2005", - model_ws=cpth, + model_ws=model_ws, load_only=["ghb", "dis", "bas6", "oc", "sip", "lpf", "sfr"], verbose=True, ) @@ -139,7 +140,7 @@ def test_create(): assert uzf.uzgag == uzgag m.write_input() uzf2 = flopy.modflow.ModflowUzf1.load( - os.path.join(cpth, uzf.file_name[0]), m + os.path.join(model_ws, uzf.file_name[0]), m ) assert uzf2.uzgag == uzgag m2 = flopy.modflow.Modflow.load( @@ -169,11 +170,14 @@ def test_create(): # the created finf arrays all have a mult of 1 assert np.array_equal(a.array, l2[i].array) + # load the model files + load_and_write(model_ws) + -def test_load_and_write(): +def load_and_write(model_ws): # load in the test problem - m = flopy.modflow.Modflow("UZFtest2", model_ws=cpth, verbose=True) - m.model_ws = cpth + m = flopy.modflow.Modflow("UZFtest2", model_ws=model_ws, verbose=True) + path = os.path.join("..", "examples", "data", "mf2005_test") dis = flopy.modflow.ModflowDis.load(os.path.join(path, "UZFtest2.dis"), m) uzf = flopy.modflow.ModflowUzf1.load(os.path.join(path, "UZFtest2.uzf"), m) @@ -193,12 +197,11 @@ def test_load_and_write(): < 1e4 ) assert True - m.model_ws = cpth uzf.write_file() - m2 = flopy.modflow.Modflow("UZFtest2_2", model_ws=cpth) + m2 = flopy.modflow.Modflow("UZFtest2_2", model_ws=model_ws) dis = flopy.modflow.ModflowDis(nrow=m.nrow, ncol=m.ncol, nper=12, model=m2) uzf2 = flopy.modflow.ModflowUzf1.load( - os.path.join(cpth, "UZFtest2.uzf"), m2 + os.path.join(model_ws, "UZFtest2.uzf"), m2 ) attrs = dir(uzf) for attr in attrs: @@ -218,11 +221,11 @@ def test_load_and_write(): "..", "examples", "data", "uzf_examples", "load_uzf_for_nwt" ) [ - shutil.copy(os.path.join(tpth, f), os.path.join(cpth, f)) + shutil.copy(os.path.join(tpth, f), os.path.join(model_ws, f)) for f in os.listdir(tpth) ] m3 = flopy.modflow.Modflow("UZFtest3", version="mfnwt", verbose=True) - m3.model_ws = cpth + m3.model_ws = model_ws dis = flopy.modflow.ModflowDis.load(os.path.join(tpth, "UZFtest3.dis"), m3) uzf = flopy.modflow.ModflowUzf1.load( os.path.join(tpth, "UZFtest3.uzf"), m3 @@ -234,6 +237,9 @@ def test_load_and_write(): def test_uzf_surfk(): + model_ws = f"{baseDir}_test_uzf_surfk" + testFramework = flopyTest(verbose=True, testDirs=model_ws) + ws = os.path.join("..", "examples", "data", "uzf_examples") uzf_name = "UZFtest4.uzf" dis_name = "UZFtest2.dis" @@ -248,17 +254,16 @@ def test_uzf_surfk(): assert uzf.options.seepsurfk assert abs(np.unique(uzf.surfk.array)[0] - 0.099) < 1e-06 - ws2 = os.path.join("temp", "t034") - ml.change_model_ws(ws2) + ml.change_model_ws(model_ws) dis.write_file() uzf.write_file() ml2 = flopy.modflow.Modflow(version="mfnwt") dis2 = flopy.modflow.ModflowDis.load( - os.path.join(ws2, "UZFtest4.dis"), ml2, ext_unit_dict={} + os.path.join(model_ws, "UZFtest4.dis"), ml2, ext_unit_dict={} ) uzf2 = flopy.modflow.ModflowUzf1.load( - os.path.join(ws2, uzf_name), ml2, ext_unit_dict={} + os.path.join(model_ws, uzf_name), ml2, ext_unit_dict={} ) assert uzf2.options.seepsurfk @@ -271,6 +276,9 @@ def test_read_write_nwt_options(): from flopy.modflow import ModflowWel, ModflowUzf1, ModflowSfr2 from flopy.utils.optionblock import OptionBlock + model_ws = f"{baseDir}_test_read_write_nwt_options" + testFramework = flopyTest(verbose=True, testDirs=model_ws, create=True) + welstr = "OPTIONS\nSPECIFY 0.5 10\nTABFILES 2 28\nEND\n" welstr2 = "OPTIONS\nSPECIFY 0.3\nTABFILES 2 28\nEND\n" uzfstr = ( @@ -293,22 +301,22 @@ def test_read_write_nwt_options(): assert repr(uzfopt) == uzfstr assert repr(sfropt) == sfrstr - welopt.write_options(os.path.join(cpth, "welopt.txt")) - welopt2.write_options(os.path.join(cpth, "welopt2.txt")) - uzfopt.write_options(os.path.join(cpth, "uzfopt.txt")) - sfropt.write_options(os.path.join(cpth, "sfropt.txt")) + welopt.write_options(os.path.join(model_ws, "welopt.txt")) + welopt2.write_options(os.path.join(model_ws, "welopt2.txt")) + uzfopt.write_options(os.path.join(model_ws, "uzfopt.txt")) + sfropt.write_options(os.path.join(model_ws, "sfropt.txt")) welopt = OptionBlock.load_options( - os.path.join(cpth, "welopt.txt"), ModflowWel + os.path.join(model_ws, "welopt.txt"), ModflowWel ) welopt2 = OptionBlock.load_options( - os.path.join(cpth, "welopt2.txt"), ModflowWel + os.path.join(model_ws, "welopt2.txt"), ModflowWel ) uzfopt = OptionBlock.load_options( - os.path.join(cpth, "uzfopt.txt"), ModflowUzf1 + os.path.join(model_ws, "uzfopt.txt"), ModflowUzf1 ) sfropt = OptionBlock.load_options( - os.path.join(cpth, "sfropt.txt"), ModflowSfr2 + os.path.join(model_ws, "sfropt.txt"), ModflowSfr2 ) assert repr(welopt) == welstr @@ -318,6 +326,9 @@ def test_read_write_nwt_options(): def test_load_write_sfr_option_block(): + model_ws = f"{baseDir}_test_write_sfr_option_block" + testFramework = flopyTest(verbose=True, testDirs=model_ws, create=True) + ws = os.path.join("..", "examples", "data", "options") sfr_name = "sagehen_ob.sfr" @@ -337,11 +348,11 @@ def test_load_write_sfr_option_block(): ) sfr_name2 = "sagehen_ob2.sfr" - sfr.write_file(filename=os.path.join(cpth, sfr_name2)) + sfr.write_file(filename=os.path.join(model_ws, sfr_name2)) ml.remove_package("SFR") sfr2 = flopy.modflow.ModflowSfr2.load( - os.path.join(cpth, sfr_name2), ml, nper=2, ext_unit_dict={} + os.path.join(model_ws, sfr_name2), ml, nper=2, ext_unit_dict={} ) assert sfr.options.reachinput == sfr2.options.reachinput @@ -355,11 +366,11 @@ def test_load_write_sfr_option_block(): sfr2.options.strhc1kh = False sfr2.options.strhc1kv = False - sfr2.write_file(os.path.join(cpth, sfr_name2)) + sfr2.write_file(os.path.join(model_ws, sfr_name2)) ml.remove_package("SFR") sfr3 = flopy.modflow.ModflowSfr2.load( - os.path.join(cpth, sfr_name2), ml, nper=2, ext_unit_dict={} + os.path.join(model_ws, sfr_name2), ml, nper=2, ext_unit_dict={} ) assert sfr3.options.strhc1kh == False @@ -367,6 +378,9 @@ def test_load_write_sfr_option_block(): def test_load_write_sfr_option_line(): + model_ws = f"{baseDir}_test_load_write_sfr_option_line" + testFramework = flopyTest(verbose=True, testDirs=model_ws, create=True) + ws = os.path.join("..", "examples", "data", "options") sfr_name = "sagehen.sfr" @@ -387,11 +401,11 @@ def test_load_write_sfr_option_line(): ) sfr_name2 = "sagehen2.sfr" - sfr.write_file(os.path.join(cpth, sfr_name2)) + sfr.write_file(os.path.join(model_ws, sfr_name2)) ml.remove_package("SFR") sfr2 = flopy.modflow.ModflowSfr2.load( - os.path.join(cpth, sfr_name2), ml, nper=2, ext_unit_dict={} + os.path.join(model_ws, sfr_name2), ml, nper=2, ext_unit_dict={} ) assert sfr2.reachinput @@ -414,17 +428,20 @@ def test_load_write_sfr_option_line(): ) sfr_name2 = "sagehen2.sfr" - sfr.write_file(os.path.join(cpth, sfr_name2)) + sfr.write_file(os.path.join(model_ws, sfr_name2)) ml.remove_package("SFR") sfr2 = flopy.modflow.ModflowSfr2.load( - os.path.join(cpth, sfr_name2), ml, nper=2, ext_unit_dict={} + os.path.join(model_ws, sfr_name2), ml, nper=2, ext_unit_dict={} ) assert sfr2.reachinput def test_load_write_uzf_option_block(): + model_ws = f"{baseDir}_test_load_write_uzf_option_block" + testFramework = flopyTest(verbose=True, testDirs=model_ws, create=True) + ws = os.path.join("..", "examples", "data", "options") uzf_name = "sagehen_ob.uzf" @@ -444,11 +461,11 @@ def test_load_write_uzf_option_block(): ) uzf_name2 = "sagehen_ob2.uzf" - uzf.write_file(os.path.join(cpth, uzf_name2)) + uzf.write_file(os.path.join(model_ws, uzf_name2)) ml.remove_package("UZF") uzf2 = flopy.modflow.ModflowUzf1.load( - os.path.join(cpth, uzf_name2), ml, ext_unit_dict=None, check=False + os.path.join(model_ws, uzf_name2), ml, ext_unit_dict=None, check=False ) assert uzf.options.nosurfleak == uzf2.options.nosurfleak @@ -458,11 +475,11 @@ def test_load_write_uzf_option_block(): uzf2.smoothfact = 0.4 - uzf2.write_file(os.path.join(cpth, uzf_name2)) + uzf2.write_file(os.path.join(model_ws, uzf_name2)) ml.remove_package("UZF") uzf3 = flopy.modflow.ModflowUzf1.load( - os.path.join(cpth, uzf_name2), ml, check=False + os.path.join(model_ws, uzf_name2), ml, check=False ) assert uzf3.options.smoothfact == 0.4 @@ -471,6 +488,9 @@ def test_load_write_uzf_option_block(): def test_load_write_uzf_option_line(): + model_ws = f"{baseDir}_test_load_write_uzf_option_line" + testFramework = flopyTest(verbose=True, testDirs=model_ws, create=True) + ws = os.path.join("..", "examples", "data", "options") uzf_name = "sagehen.uzf" @@ -496,11 +516,11 @@ def test_load_write_uzf_option_line(): assert uzf.options.savefinf uzf_name2 = "sagehen2.uzf" - uzf.write_file(os.path.join(cpth, uzf_name2)) + uzf.write_file(os.path.join(model_ws, uzf_name2)) ml.remove_package("UZF") uzf2 = flopy.modflow.ModflowUzf1.load( - os.path.join(cpth, uzf_name2), ml, check=False + os.path.join(model_ws, uzf_name2), ml, check=False ) assert uzf2.nosurfleak @@ -511,6 +531,9 @@ def test_load_write_uzf_option_line(): def test_load_write_wel_option_block(): + model_ws = f"{baseDir}_test_load_write_wel_option_block" + testFramework = flopyTest(verbose=True, testDirs=model_ws, create=True) + ws = os.path.join("..", "examples", "data", "options") wel_name = "sagehen_ob.wel" @@ -523,11 +546,11 @@ def test_load_write_wel_option_block(): ) wel_name2 = "sagehen_ob2.wel" - wel.write_file(os.path.join(cpth, wel_name2)) + wel.write_file(os.path.join(model_ws, wel_name2)) ml.remove_package("WEL") wel2 = flopy.modflow.ModflowWel.load( - os.path.join(cpth, wel_name2), + os.path.join(model_ws, wel_name2), ml, nper=2, ext_unit_dict={}, @@ -541,11 +564,11 @@ def test_load_write_wel_option_block(): wel2.options.tabfiles = False wel2.phiramp = 0.4 - wel2.write_file(os.path.join(cpth, wel_name2)) + wel2.write_file(os.path.join(model_ws, wel_name2)) ml.remove_package("WEL") wel3 = flopy.modflow.ModflowWel.load( - os.path.join(cpth, wel_name2), + os.path.join(model_ws, wel_name2), ml, nper=2, ext_unit_dict={}, @@ -558,6 +581,9 @@ def test_load_write_wel_option_block(): def test_load_write_wel_option_line(): + model_ws = f"{baseDir}_test_load_write_wel_option_line" + testFramework = flopyTest(verbose=True, testDirs=model_ws, create=True) + ws = os.path.join("..", "examples", "data", "options") wel_name = "sagehen.wel" @@ -577,11 +603,11 @@ def test_load_write_wel_option_line(): wel.iunitramp = 20 wel_name2 = "sagehen2.wel" - wel.write_file(os.path.join(cpth, wel_name2)) + wel.write_file(os.path.join(model_ws, wel_name2)) ml.remove_package("WEL") wel2 = flopy.modflow.ModflowWel.load( - os.path.join(cpth, wel_name2), + os.path.join(model_ws, wel_name2), ml, nper=2, ext_unit_dict={}, @@ -595,8 +621,7 @@ def test_load_write_wel_option_line(): if __name__ == "__main__": - test_create() - test_load_and_write() + test_create_uzf() test_read_write_nwt_options() test_load_write_sfr_option_block() test_load_write_sfr_option_line() diff --git a/autotest/t035_test.py b/autotest/t035_test.py index 318d58f989..2b8466e46e 100644 --- a/autotest/t035_test.py +++ b/autotest/t035_test.py @@ -6,16 +6,10 @@ import shutil import numpy as np import flopy +import pymake +from ci_framework import baseTestDir, flopyTest -try: - import pymake -except: - print("could not import pymake") - -cpth = os.path.join("temp", "t035") -# delete the directory if it exists -if not os.path.isdir(cpth): - os.makedirs(cpth, exist_ok=True) +baseDir = baseTestDir(__file__, relPath="temp", verbose=True) exe_name = "mflgr" v = flopy.which(exe_name) @@ -31,31 +25,34 @@ def test_simplelgr_load_and_write(silent=True): # Test load and write of distributed MODFLOW-LGR example problem + model_ws = f"{baseDir}_test_simplelgr_load_and_write" + testFramework = flopyTest( + verbose=True, + testDirs=model_ws, + create=True, + ) + pth = os.path.join("..", "examples", "data", "mflgr_v2", "ex3") - opth = os.path.join(cpth, "ex3", "orig") - # delete the directory if it exists - if os.path.isdir(opth): - shutil.rmtree(opth) - os.makedirs(opth) + # copy the original files files = os.listdir(pth) for file in files: src = os.path.join(pth, file) - dst = os.path.join(opth, file) + dst = os.path.join(model_ws, file) shutil.copyfile(src, dst) # load the lgr model lgr = flopy.modflowlgr.ModflowLgr.load( - "ex3.lgr", verbose=True, model_ws=opth, exe_name=exe_name + "ex3.lgr", verbose=True, model_ws=model_ws, exe_name=exe_name ) # get the namefiles of the parent and child namefiles = lgr.get_namefiles() msg = f"get_namefiles returned {len(namefiles)} items instead of 2" assert len(namefiles) == 2, msg + tpth = os.path.dirname(namefiles[0]) - msg = f"dir path is {tpth} not {opth}" - assert tpth == opth, msg + assert tpth == model_ws, f"dir path is {tpth} not {model_ws}" # run the lgr model if run: @@ -66,16 +63,17 @@ def test_simplelgr_load_and_write(silent=True): msg = "modflow-lgr ex3 does not have 2 grids" assert lgr.ngrids == 2, msg - npth = os.path.join(cpth, "ex3", "new") - lgr.change_model_ws(new_pth=npth, reset_external=True) + model_ws2 = os.path.join(model_ws, "new") + lgr.change_model_ws(new_pth=model_ws2, reset_external=True) # get the namefiles of the parent and child namefiles = lgr.get_namefiles() - msg = f"get_namefiles returned {len(namefiles)} items instead of 2" - assert len(namefiles) == 2, msg + assert ( + len(namefiles) == 2 + ), f"get_namefiles returned {len(namefiles)} items instead of 2" + tpth = os.path.dirname(namefiles[0]) - msg = f"dir path is {tpth} not {npth}" - assert tpth == npth, msg + assert tpth == model_ws2, f"dir path is {tpth} not {model_ws2}" # write the lgr model in to the new path lgr.write_input() @@ -87,24 +85,17 @@ def test_simplelgr_load_and_write(silent=True): # compare parent results print("compare parent results") - pth0 = os.path.join(opth, "ex3_parent.nam") - pth1 = os.path.join(npth, "ex3_parent.nam") - - msg = "parent heads do not match" + pth0 = os.path.join(model_ws, "ex3_parent.nam") + pth1 = os.path.join(model_ws2, "ex3_parent.nam") success = pymake.compare_heads(pth0, pth1) - assert success, msg + assert success, "parent heads do not match" # compare child results print("compare child results") - pth0 = os.path.join(opth, "ex3_child.nam") - pth1 = os.path.join(npth, "ex3_child.nam") - - msg = "child heads do not match" + pth0 = os.path.join(model_ws, "ex3_child.nam") + pth1 = os.path.join(model_ws2, "ex3_child.nam") success = pymake.compare_heads(pth0, pth1) - assert success, msg - - # clean up - shutil.rmtree(cpth) + assert success, "child heads do not match" def singleModel( @@ -136,6 +127,7 @@ def singleModel( startingHead=0.0, lRunSingle=False, ): + if iChild > 0: print(f"child model {modelname}") iLUoffset = 100 * int(iChild) @@ -259,6 +251,13 @@ def singleModel( def test_simple_lgrmodel_from_scratch(silent=True): + model_ws = f"{baseDir}_test_simple_lgrmodel_from_scratch" + testFramework = flopyTest( + verbose=True, + testDirs=model_ws, + create=True, + ) + # coordinates and extend Mother Lx_m = 1500.0 Ly_m = 2500.0 @@ -294,7 +293,7 @@ def test_simple_lgrmodel_from_scratch(silent=True): nstp = [ats] tsmult = 1.07 steady = True - rundir = f"{cpth}b" + rundir = model_ws lgrExe = exe_name # wel data @@ -332,7 +331,7 @@ def test_simple_lgrmodel_from_scratch(silent=True): yul_c, proj4_str, exe_name, - rundir=rundir, + rundir=model_ws, welInfo=welInfo, startingHead=-2.0, ) @@ -362,7 +361,7 @@ def test_simple_lgrmodel_from_scratch(silent=True): yul_m, proj4_str, exe_name, - rundir=rundir, + rundir=model_ws, welInfo=welInfo, startingHead=-2.0, ) @@ -404,7 +403,7 @@ def test_simple_lgrmodel_from_scratch(silent=True): parent=mother, children=[child], children_data=childData, - model_ws=rundir, + model_ws=model_ws, external_path=None, verbose=False, ) @@ -417,9 +416,6 @@ def test_simple_lgrmodel_from_scratch(silent=True): success, buff = lgrModel.run_model(silent=silent) assert success - # clean up - shutil.rmtree(rundir) - return diff --git a/autotest/t036_test.py b/autotest/t036_test.py index f6911302c5..4bc091e722 100644 --- a/autotest/t036_test.py +++ b/autotest/t036_test.py @@ -2,45 +2,39 @@ Test loading and preserving existing unit numbers """ import os -import shutil - import flopy +import pymake +from ci_framework import baseTestDir, flopyTest + +baseDir = baseTestDir(__file__, relPath="temp", verbose=True) pth = os.path.join("..", "examples", "data", "mf2005_test") cpth = os.path.join("temp", "t036") -# make the directory if it does not exist -if not os.path.isdir(cpth): - os.makedirs(cpth, exist_ok=True) +exe_name = "mf2005" +v = flopy.which(exe_name) +run = True +if v is None: + run = False -def test_uzf_unit_numbers(): - exe_name = "mf2005" - v = flopy.which(exe_name) - run = True - if v is None: - run = False +def test_uzf_unit_numbers(): + model_ws = f"{baseDir}_test_uzf_unit_numbers" + testFramework = flopyTest(verbose=True, testDirs=model_ws) mfnam = "UZFtest2.nam" - pt = os.path.join("..", "examples", "data", "uzf_examples") + orig_pth = os.path.join("..", "examples", "data", "uzf_examples") # copy files - try: - import pymake - - lpth = os.path.join(cpth, os.path.splitext(mfnam)[0]) - apth = os.path.join(lpth, "flopy") - compth = lpth - pymake.setup(os.path.join(pt, mfnam), lpth) - except: - run = False - lpth = pth - apth = cpth - compth = cpth + pymake.setup(os.path.join(orig_pth, mfnam), model_ws) m = flopy.modflow.Modflow.load( - mfnam, verbose=True, model_ws=lpth, forgive=False, exe_name=exe_name + mfnam, + verbose=True, + model_ws=model_ws, + forgive=False, + exe_name=exe_name, ) assert m.load_fail is False, "failed to load all packages" @@ -64,14 +58,15 @@ def test_uzf_unit_numbers(): except: success = False assert success, "base model run did not terminate successfully" - fn0 = os.path.join(lpth, mfnam) + fn0 = os.path.join(model_ws, mfnam) # change uzf iuzfcb2 and add binary uzf output file m.uzf.iuzfcb2 = 61 m.add_output_file(m.uzf.iuzfcb2, extension="uzfcb2.bin", package="UZF") # change the model work space - m.change_model_ws(apth, reset_external=True) + model_ws2 = os.path.join(model_ws, "flopy") + m.change_model_ws(model_ws2, reset_external=True) # rewrite files m.write_input() @@ -83,11 +78,13 @@ def test_uzf_unit_numbers(): except: success = False assert success, "new model run did not terminate successfully" - fn1 = os.path.join(apth, mfnam) + fn1 = os.path.join(model_ws2, mfnam) # compare budget terms if run: - fsum = os.path.join(compth, f"{os.path.splitext(mfnam)[0]}.budget.out") + fsum = os.path.join( + model_ws, f"{os.path.splitext(mfnam)[0]}.budget.out" + ) try: success = pymake.compare_budget( fn0, fn1, max_incpd=0.1, max_cumpd=0.1, outfile=fsum @@ -98,38 +95,22 @@ def test_uzf_unit_numbers(): assert success, "budget comparison failure" - # clean up - shutil.rmtree(lpth) - return def test_unitnums_load_and_write(): - exe_name = "mf2005" - v = flopy.which(exe_name) - - run = True - if v is None: - run = False + model_ws = f"{baseDir}_test_unitnums_load_and_write" + testFramework = flopyTest(verbose=True, testDirs=model_ws) mfnam = "testsfr2_tab.nam" # copy files - try: - import pymake - - lpth = os.path.join(cpth, os.path.splitext(mfnam)[0]) - apth = os.path.join(lpth, "flopy") - compth = lpth - pymake.setup(os.path.join(pth, mfnam), lpth) - except: - run = False - lpth = pth - apth = cpth - compth = cpth + import pymake + + pymake.setup(os.path.join(pth, mfnam), model_ws) m = flopy.modflow.Modflow.load( - mfnam, verbose=True, model_ws=lpth, exe_name=exe_name + mfnam, verbose=True, model_ws=model_ws, exe_name=exe_name ) assert m.load_fail is False, "failed to load all packages" @@ -146,21 +127,26 @@ def test_unitnums_load_and_write(): except: success = False assert success, "base model run did not terminate successfully" - fn0 = os.path.join(lpth, mfnam) + fn0 = os.path.join(model_ws, mfnam) # rewrite files - m.change_model_ws(apth, reset_external=True) + model_ws2 = os.path.join(model_ws, "flopy") + m.change_model_ws(model_ws2, reset_external=True) + m.write_input() + if run: try: success, buff = m.run_model(silent=False) except: success = False assert success, "base model run did not terminate successfully" - fn1 = os.path.join(apth, mfnam) + fn1 = os.path.join(model_ws2, mfnam) if run: - fsum = os.path.join(compth, f"{os.path.splitext(mfnam)[0]}.budget.out") + fsum = os.path.join( + model_ws, f"{os.path.splitext(mfnam)[0]}.budget.out" + ) try: success = pymake.compare_budget( fn0, fn1, max_incpd=0.1, max_cumpd=0.1, outfile=fsum @@ -171,9 +157,6 @@ def test_unitnums_load_and_write(): assert success, "budget comparison failure" - # clean up - shutil.rmtree(lpth) - return diff --git a/autotest/t037_test.py b/autotest/t037_test.py index 22eabcc59b..10554876a2 100644 --- a/autotest/t037_test.py +++ b/autotest/t037_test.py @@ -5,41 +5,34 @@ import pytest import os import flopy +import pymake +from ci_framework import baseTestDir, flopyTest -path = os.path.join("..", "examples", "data", "mf2005_test") +baseDir = baseTestDir(__file__, relPath="temp", verbose=True) + +swi_path = os.path.join("..", "examples", "data", "mf2005_test") cpth = os.path.join("temp", "t037") -# make the directory if it does not exist -if not os.path.isdir(cpth): - os.makedirs(cpth, exist_ok=True) + mf_items = ["swiex1.nam", "swiex2_strat.nam", "swiex3.nam"] -pths = [] -for val in mf_items: - pths.append(path) - - -def load_swi(mfnam, pth): - exe_name = "mf2005" - v = flopy.which(exe_name) - - run = True - if v is None: - run = False - try: - import pymake - - lpth = os.path.join(cpth, os.path.splitext(mfnam)[0]) - apth = os.path.join(lpth, "flopy") - compth = lpth - pymake.setup(os.path.join(pth, mfnam), lpth) - except: - run = False - lpth = pth - apth = cpth - compth = cpth + +exe_name = "mf2005" +v = flopy.which(exe_name) + +run = True +if v is None: + run = False + + +def load_swi(mfnam): + name = mfnam.replace(".nam", "") + model_ws = f"{baseDir}_{name}" + testFramework = flopyTest(verbose=True, testDirs=model_ws) + + pymake.setup(os.path.join(swi_path, mfnam), model_ws) m = flopy.modflow.Modflow.load( - mfnam, model_ws=lpth, verbose=True, exe_name=exe_name + mfnam, model_ws=model_ws, verbose=True, exe_name=exe_name ) assert m.load_fail is False @@ -49,15 +42,16 @@ def load_swi(mfnam, pth): except: success = False assert success, "base model run did not terminate successfully" - fn0 = os.path.join(lpth, mfnam) + fn0 = os.path.join(model_ws, mfnam) # write free format files - # won't run without resetting to free format - evt external file issue m.free_format_input = True # rewrite files + model_ws2 = os.path.join(model_ws, "flopy") m.change_model_ws( - apth, reset_external=True + model_ws2, reset_external=True ) # l1b2k_bath wont run without this m.write_input() if run: @@ -66,10 +60,12 @@ def load_swi(mfnam, pth): except: success = False assert success, "base model run did not terminate successfully" - fn1 = os.path.join(apth, mfnam) + fn1 = os.path.join(model_ws2, mfnam) if run: - fsum = os.path.join(compth, f"{os.path.splitext(mfnam)[0]}.budget.out") + fsum = os.path.join( + model_ws, f"{os.path.splitext(mfnam)[0]}.budget.out" + ) try: success = pymake.compare_budget( fn0, fn1, max_incpd=0.1, max_cumpd=0.1, outfile=fsum @@ -84,14 +80,14 @@ def load_swi(mfnam, pth): @pytest.mark.parametrize( - "namfile, pth", - zip(mf_items, pths), + "namfile", + mf_items, ) -def test_mf2005swi2load(namfile, pth): - load_swi(namfile, pth) +def test_mf2005swi2load(namfile): + load_swi(namfile) return if __name__ == "__main__": - for namfile, pth in zip(mf_items, pths): - load_swi(namfile, pth) + for namfile in mf_items: + load_swi(namfile) diff --git a/autotest/t038_test.py b/autotest/t038_test.py index 000aa1af22..3b26a0535b 100644 --- a/autotest/t038_test.py +++ b/autotest/t038_test.py @@ -6,11 +6,9 @@ import pytest import os import flopy +from ci_framework import baseTestDir, flopyTest -# make the working directory -tpth = os.path.join("temp", "t038") -if not os.path.isdir(tpth): - os.makedirs(tpth, exist_ok=True) +baseDir = baseTestDir(__file__, relPath="temp", verbose=True) # build list of name files to try and load usgpth = os.path.join("..", "examples", "data", "mfusg_test") @@ -20,29 +18,37 @@ if name.endswith(".nam"): usg_files.append(os.path.join(path, name)) -# + @pytest.mark.parametrize( "fpth", usg_files, ) def test_load_usg(fpth): exdir, namfile = os.path.split(fpth) - load_model(namfile, exdir) + name = namfile.replace(".nam", "") + model_ws = f"{baseDir}_test_load_usg_{name}" + testFramework = flopyTest(verbose=True, testDirs=model_ws) + + load_model(namfile, exdir, model_ws) # function to load a MODFLOW-USG model and then write it back out -def load_model(namfile, model_ws): +def load_model(namfile, load_ws, model_ws): m = flopy.mfusg.MfUsg.load( - namfile, model_ws=model_ws, verbose=True, check=False + namfile, + model_ws=load_ws, + verbose=True, + check=False, ) assert m, f"Could not load namefile {namfile}" assert m.load_fail is False - m.change_model_ws(tpth) + + m.change_model_ws(model_ws) m.write_input() + return if __name__ == "__main__": for fusg in usg_files: - d, f = os.path.split(fusg) - load_model(f, d) + test_load_usg(fusg) diff --git a/autotest/t040_test.py b/autotest/t040_test.py index 85c43e51cb..a6d4116a89 100644 --- a/autotest/t040_test.py +++ b/autotest/t040_test.py @@ -1,10 +1,11 @@ import os +import copy +import numpy as np +import matplotlib.pyplot as plt import flopy +from ci_framework import baseTestDir, flopyTest -model_ws = os.path.join("temp", "t040") -# delete the directory if it exists -if not os.path.isdir(model_ws): - os.makedirs(model_ws, exist_ok=True) +baseDir = baseTestDir(__file__, relPath="temp", verbose=True) ibound_path = os.path.join( "..", "examples", "data", "subwt_example", "ibound.ref" @@ -18,76 +19,10 @@ run = False -def build_model(): - import matplotlib.pyplot as plt - - ml = test_subwt() - ml.write_input() - if run: - ml.run_model() - - hds_geo = flopy.utils.HeadFile( - os.path.join(model_ws, f"{ml.name}.swt_geostatic_stress.hds"), - text="stress", - ).get_alldata() - hds_eff = flopy.utils.HeadFile( - os.path.join(model_ws, f"{ml.name}.swt_eff_stress.hds"), - text="effective stress", - ).get_alldata() - - hds_sub = flopy.utils.HeadFile( - os.path.join(model_ws, f"{ml.name}.swt_subsidence.hds"), - text="subsidence", - ).get_alldata() - - hds_comp = flopy.utils.HeadFile( - os.path.join(model_ws, f"{ml.name}.swt_total_comp.hds"), - text="layer compaction", - ).get_alldata() - - hds_precon = flopy.utils.HeadFile( - os.path.join(model_ws, f"{ml.name}.swt_precon_stress.hds"), - text="preconsol stress", - ).get_alldata() - - # make 6 from subwt manual - i, j = 8, 9 - fig1 = plt.figure(figsize=(10, 10)) - ax1 = plt.subplot(4, 1, 1) - ax1.plot(hds_precon[:, 0, i, j], color="0.5", dashes=(1, 1)) - ax1.plot(hds_eff[:, 0, i, j], color="r") - - ax2 = plt.subplot(4, 1, 2) - ax2.plot(hds_geo[:, 0, i, j], color="b") - - ax3 = plt.subplot(4, 1, 3) - ax3.plot(hds_precon[:, 1, i, j], color="0.5", dashes=(1, 1)) - ax3.plot(hds_eff[:, 1, i, j], color="r") - - ax4 = plt.subplot(4, 1, 4) - ax4.plot(hds_geo[:, 1, i, j], color="b") - plt.savefig(os.path.join(model_ws, "fig6.pdf")) - - fig2 = plt.figure(figsize=(10, 10)) - ax1 = plt.subplot(2, 1, 1) - ax2 = plt.subplot(2, 1, 2) - i1, j1 = 8, 9 - i2, j2 = 11, 6 - for k in range(hds_comp.shape[1]): - hds_comp_sum = hds_comp[:, k:, :, :].sum(axis=1) - print(hds_comp_sum.shape) - ax1.plot(hds_comp_sum[:, i1, j1]) - ax2.plot(hds_comp_sum[:, i2, j2]) - # plt.show() - plt.savefig(os.path.join(model_ws, "subwt.pdf")) - - -def test_subwt(): +def build_subwt(model_ws): """ test040 subwt example problem test """ - import copy - import numpy as np ml = flopy.modflow.Modflow( "subwt_mf2005", model_ws=model_ws, exe_name=exe_name @@ -177,5 +112,70 @@ def test_subwt(): return ml +def test_subwt(): + model_ws = f"{baseDir}_test_subwt" + testFramework = flopyTest(verbose=True, testDirs=model_ws) + + ml = build_subwt(model_ws) + ml.write_input() + if run: + ml.run_model() + + hds_geo = flopy.utils.HeadFile( + os.path.join(model_ws, f"{ml.name}.swt_geostatic_stress.hds"), + text="stress", + ).get_alldata() + hds_eff = flopy.utils.HeadFile( + os.path.join(model_ws, f"{ml.name}.swt_eff_stress.hds"), + text="effective stress", + ).get_alldata() + + hds_sub = flopy.utils.HeadFile( + os.path.join(model_ws, f"{ml.name}.swt_subsidence.hds"), + text="subsidence", + ).get_alldata() + + hds_comp = flopy.utils.HeadFile( + os.path.join(model_ws, f"{ml.name}.swt_total_comp.hds"), + text="layer compaction", + ).get_alldata() + + hds_precon = flopy.utils.HeadFile( + os.path.join(model_ws, f"{ml.name}.swt_precon_stress.hds"), + text="preconsol stress", + ).get_alldata() + + # make 6 from subwt manual + i, j = 8, 9 + fig1 = plt.figure(figsize=(10, 10)) + ax1 = plt.subplot(4, 1, 1) + ax1.plot(hds_precon[:, 0, i, j], color="0.5", dashes=(1, 1)) + ax1.plot(hds_eff[:, 0, i, j], color="r") + + ax2 = plt.subplot(4, 1, 2) + ax2.plot(hds_geo[:, 0, i, j], color="b") + + ax3 = plt.subplot(4, 1, 3) + ax3.plot(hds_precon[:, 1, i, j], color="0.5", dashes=(1, 1)) + ax3.plot(hds_eff[:, 1, i, j], color="r") + + ax4 = plt.subplot(4, 1, 4) + ax4.plot(hds_geo[:, 1, i, j], color="b") + plt.savefig(os.path.join(model_ws, "fig6.pdf")) + + fig2 = plt.figure(figsize=(10, 10)) + ax1 = plt.subplot(2, 1, 1) + ax2 = plt.subplot(2, 1, 2) + i1, j1 = 8, 9 + i2, j2 = 11, 6 + for k in range(hds_comp.shape[1]): + hds_comp_sum = hds_comp[:, k:, :, :].sum(axis=1) + print(hds_comp_sum.shape) + ax1.plot(hds_comp_sum[:, i1, j1]) + ax2.plot(hds_comp_sum[:, i2, j2]) + # plt.show() + plt.savefig(os.path.join(model_ws, "subwt.pdf")) + + if __name__ == "__main__": - build_model() + test_subwt() From 86cb82d78bf48518d7a74d3eddfb8011399e28f0 Mon Sep 17 00:00:00 2001 From: Joseph D Hughes Date: Sun, 31 Oct 2021 17:15:47 -0500 Subject: [PATCH 4/4] ci: convert addition autotests to use flopyTest testing framework --- autotest/Autotesting.md | 7 ++++ autotest/t041_test.py | 89 ++++++++++++++++++++--------------------- autotest/t042_test.py | 3 -- autotest/t043_test.py | 36 ++++++----------- 4 files changed, 64 insertions(+), 71 deletions(-) diff --git a/autotest/Autotesting.md b/autotest/Autotesting.md index 1332ff69f1..bbacb60f80 100644 --- a/autotest/Autotesting.md +++ b/autotest/Autotesting.md @@ -45,6 +45,13 @@ The space between `-n` and the number of processors can be replaced with a pytest -v -n=auto t001_test.py +### Debugging failed tests + +To debug a failed autotest rerun the failed test by running the following command from the autotest directory + + python mffailedtest.py --keep + +The `--keep` will retain the test directories created by the test, which will allow the input or output files to be evaluated for errors. ## Creating an autotest diff --git a/autotest/t041_test.py b/autotest/t041_test.py index dabdb480ae..a93e6d53c4 100644 --- a/autotest/t041_test.py +++ b/autotest/t041_test.py @@ -6,16 +6,9 @@ import shutil import numpy as np import flopy +from ci_framework import baseTestDir, flopyTest -try: - import pymake -except: - print("could not import pymake") - -cpth = os.path.join("temp", "t041") -# make the directory if it does not exist -if not os.path.isdir(cpth): - os.makedirs(cpth, exist_ok=True) +baseDir = baseTestDir(__file__, relPath="temp", verbose=True) exe_name = "mf2005" v = flopy.which(exe_name) @@ -29,7 +22,9 @@ def test_hob_simple(): """ test041 create and run a simple MODFLOW-2005 OBS example """ - pth = os.path.join(cpth, "simple") + model_ws = f"{baseDir}_test_hob_simple" + testFramework = flopyTest(verbose=True, testDirs=model_ws) + modelname = "hob_simple" nlay, nrow, ncol = 1, 11, 11 shape3d = (nlay, nrow, ncol) @@ -38,7 +33,7 @@ def test_hob_simple(): ib[0, 0, 0] = -1 m = flopy.modflow.Modflow( modelname=modelname, - model_ws=pth, + model_ws=model_ws, verbose=False, exe_name=exe_name, ) @@ -68,7 +63,7 @@ def test_hob_simple(): success, buff = m.run_model(silent=False) assert success, "could not run simple MODFLOW-2005 model" - evaluate_filenames() + evaluate_filenames(model_ws) return @@ -77,22 +72,25 @@ def test_obs_load_and_write(): """ test041 load and write of MODFLOW-2005 OBS example problem """ + model_ws = f"{baseDir}_test_obs_load_and_write" + testFramework = flopyTest( + verbose=True, + testDirs=model_ws, + create=True, + ) + pth = os.path.join("..", "examples", "data", "mf2005_obs") - opth = os.path.join(cpth, "tc1-true", "orig") - # delete the directory if it exists - if os.path.isdir(opth): - shutil.rmtree(opth) - os.makedirs(opth) + # copy the original files files = os.listdir(pth) for file in files: src = os.path.join(pth, file) - dst = os.path.join(opth, file) + dst = os.path.join(model_ws, file) shutil.copyfile(src, dst) # load the modflow model mf = flopy.modflow.Modflow.load( - "tc1-true.nam", verbose=True, model_ws=opth, exe_name=exe_name + "tc1-true.nam", verbose=True, model_ws=model_ws, exe_name=exe_name ) # run the modflow-2005 model @@ -103,13 +101,13 @@ def test_obs_load_and_write(): try: iu = mf.hob.iuhobsv fpth = mf.get_output(unit=iu) - pth0 = os.path.join(opth, fpth) + pth0 = os.path.join(model_ws, fpth) obs0 = np.genfromtxt(pth0, skip_header=1) except: raise ValueError("could not load original HOB output file") - npth = os.path.join(cpth, "tc1-true", "new") - mf.change_model_ws(new_pth=npth, reset_external=True) + model_ws2 = os.path.join(model_ws, "flopy") + mf.change_model_ws(new_pth=model_ws2, reset_external=True) # write the lgr model in to the new path mf.write_input() @@ -121,7 +119,7 @@ def test_obs_load_and_write(): # compare parent results try: - pth1 = os.path.join(npth, fpth) + pth1 = os.path.join(model_ws2, fpth) obs1 = np.genfromtxt(pth1, skip_header=1) msg = "new simulated heads are not approximately equal" @@ -132,31 +130,34 @@ def test_obs_load_and_write(): except: raise ValueError("could not load new HOB output file") - eval_flwob_load() + eval_flwob_load(model_ws) def test_obs_create_and_write(): """ test041 create and write of MODFLOW-2005 OBS example problem """ + model_ws = f"{baseDir}_test_obs_create_and_write" + testFramework = flopyTest( + verbose=True, + testDirs=model_ws, + create=True, + ) + pth = os.path.join("..", "examples", "data", "mf2005_obs") - opth = os.path.join(cpth, "create", "orig") - # delete the directory if it exists - if os.path.isdir(opth): - shutil.rmtree(opth) - os.makedirs(opth) + # copy the original files files = os.listdir(pth) for file in files: src = os.path.join(pth, file) - dst = os.path.join(opth, file) + dst = os.path.join(model_ws, file) shutil.copyfile(src, dst) # load the modflow model mf = flopy.modflow.Modflow.load( "tc1-true.nam", verbose=True, - model_ws=opth, + model_ws=model_ws, exe_name=exe_name, forgive=False, ) @@ -222,13 +223,13 @@ def test_obs_create_and_write(): try: iu = mf.hob.iuhobsv fpth = mf.get_output(unit=iu) - pth0 = os.path.join(opth, fpth) + pth0 = os.path.join(model_ws, fpth) obs0 = np.genfromtxt(pth0, skip_header=1) except: raise ValueError("could not load original HOB output file") - npth = os.path.join(cpth, "create", "new") - mf.change_model_ws(new_pth=npth, reset_external=True) + model_ws2 = os.path.join(model_ws, "flopy") + mf.change_model_ws(new_pth=model_ws2, reset_external=True) # write the model at the new path mf.write_input() @@ -240,7 +241,7 @@ def test_obs_create_and_write(): # compare parent results try: - pth1 = os.path.join(npth, fpth) + pth1 = os.path.join(model_ws2, fpth) obs1 = np.genfromtxt(pth1, skip_header=1) msg = "new simulated heads are not approximately equal" @@ -326,18 +327,17 @@ def test_multilayerhob_pr_multiline(): raise AssertionError("pr, mlay... load error") -def eval_flwob_load(): +def eval_flwob_load(model_ws): """ test041 create, write, and load ModflowFlwob package. """ # load the modflow model - opth = os.path.join(cpth, "tc1-true", "orig") m = flopy.modflow.Modflow.load( - "tc1-true.nam", verbose=True, model_ws=opth, exe_name=exe_name + "tc1-true.nam", verbose=True, model_ws=model_ws, exe_name=exe_name ) - npth = os.path.join(cpth, "tc1-true", "flwob") - m.change_model_ws(new_pth=npth, reset_external=True) + model_ws2 = os.path.join(model_ws, "flwob") + m.change_model_ws(new_pth=model_ws2, reset_external=True) # write the lgr model in to the new path m.write_input() @@ -389,7 +389,7 @@ def eval_flwob_load(): pkglst = ["drob"] m = flopy.modflow.Modflow.load( "tc1-true.nam", - model_ws=npth, + model_ws=model_ws2, check=False, load_only=pkglst, verbose=False, @@ -420,7 +420,7 @@ def eval_flwob_load(): return -def evaluate_filenames(): +def evaluate_filenames(model_ws): """ test041 load and run a simple MODFLOW-2005 OBS example with specified filenames @@ -429,12 +429,11 @@ def evaluate_filenames(): "test041 load and run a simple MODFLOW-2005 OBS example with" " specified filenames" ) - pth = os.path.join(cpth, "simple") modelname = "hob_simple" pkglst = ["dis", "bas6", "pcg", "lpf"] m = flopy.modflow.Modflow.load( f"{modelname}.nam", - model_ws=pth, + model_ws=model_ws, check=False, load_only=pkglst, verbose=False, @@ -467,7 +466,7 @@ def evaluate_filenames(): s = f"output filename ({m.get_output(unit=51)}) does not match specified name" assert m.get_output(unit=51) == f_out, s s = "specified HOB input file not found" - assert os.path.isfile(os.path.join(pth, f_in)), s + assert os.path.isfile(os.path.join(model_ws, f_in)), s # run the modflow-2005 model if run: diff --git a/autotest/t042_test.py b/autotest/t042_test.py index 3d15a13255..cb54dd2e1d 100644 --- a/autotest/t042_test.py +++ b/autotest/t042_test.py @@ -2,9 +2,6 @@ Test postprocessing utilities """ -import sys - -sys.path.append("/Users/aleaf/Documents/GitHub/flopy3") import numpy as np import flopy from flopy.utils.postprocessing import ( diff --git a/autotest/t043_test.py b/autotest/t043_test.py index 09b8096e09..75fa1b2ff5 100644 --- a/autotest/t043_test.py +++ b/autotest/t043_test.py @@ -5,16 +5,10 @@ import shutil import filecmp import flopy +import pymake +from ci_framework import baseTestDir, flopyTest -try: - import pymake -except: - print("could not import pymake") - -cpth = os.path.join("temp", "t043") -# make the directory if it does not exist -if not os.path.isdir(cpth): - os.makedirs(cpth, exist_ok=True) +baseDir = baseTestDir(__file__, relPath="temp", verbose=True) exe_name = "mf2005" v = flopy.which(exe_name) @@ -28,22 +22,18 @@ def test_gage_load_and_write(): """ test043 load and write of MODFLOW-2005 GAGE example problem """ + model_ws = f"{baseDir}_test_gage_load_and_write" + testFramework = flopyTest(verbose=True, testDirs=model_ws, create=True) + pth = os.path.join("..", "examples", "data", "mf2005_test") - opth = os.path.join(cpth, "testsfr2_tab", "orig") - # delete the directory if it exists - if os.path.isdir(opth): - shutil.rmtree(opth) - os.makedirs(opth) + # copy the original files fpth = os.path.join(pth, "testsfr2_tab.nam") - try: - pymake.setup(fpth, opth) - except: - opth = pth + pymake.setup(fpth, model_ws) # load the modflow model mf = flopy.modflow.Modflow.load( - "testsfr2_tab.nam", verbose=True, model_ws=opth, exe_name=exe_name + "testsfr2_tab.nam", verbose=True, model_ws=model_ws, exe_name=exe_name ) # run the modflow-2005 model @@ -56,8 +46,8 @@ def test_gage_load_and_write(): except: raise ValueError("could not load original GAGE output files") - npth = os.path.join(cpth, "testsfr2_tab", "new") - mf.change_model_ws(new_pth=npth, reset_external=True) + model_ws2 = os.path.join(model_ws, "flopy") + mf.change_model_ws(new_pth=model_ws2, reset_external=True) # write the modflow model in to the new path mf.write_input() @@ -70,8 +60,8 @@ def test_gage_load_and_write(): # compare the two results try: for f in files: - pth0 = os.path.join(opth, f) - pth1 = os.path.join(npth, f) + pth0 = os.path.join(model_ws, f) + pth1 = os.path.join(model_ws2, f) msg = f'new and original gage file "{f}" are not binary equal.' assert filecmp.cmp(pth0, pth1), msg except: