Example #1
0
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

    m = flopy.modflow.Modflow.load(mfnam, model_ws=lpth, verbose=True,
                                   exe_name=exe_name)
    assert m.load_fail is False

    if run:
        try:
            success, buff = m.run_model(silent=False)
        except:
            pass
        assert success, 'base model run did not terminate successfully'
        fn0 = os.path.join(lpth, mfnam)

    # write free format files -
    # won't run without resetting to free format - evt external file issue
    m.free_format_input = True

    # rewrite files
    m.change_model_ws(apth,
                      reset_external=True)  # l1b2k_bath wont run without this
    m.write_input()
    if run:
        try:
            success, buff = m.run_model(silent=False)
        except:
            pass
        assert success, 'base model run did not terminate successfully'
        fn1 = os.path.join(apth, mfnam)

    if run:
        fsum = os.path.join(compth,
                            '{}.budget.out'.format(os.path.splitext(mfnam)[0]))
        try:
            success = pymake.compare_budget(fn0, fn1,
                                            max_incpd=0.1, max_cumpd=0.1,
                                            outfile=fsum)
        except:
            print('could not perform budget comparison')

        assert success, 'budget comparison failure'

    return
Example #2
0
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
Example #3
0
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
Example #4
0
def load_lak(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

    m = flopy.modflow.Modflow.load(mfnam, model_ws=lpth, verbose=True,
                                   exe_name=exe_name)
    assert m.load_fail is False

    if run:
        try:
            success, buff = m.run_model(silent=True)
        except:
            pass
        assert success, 'base model run did not terminate successfully'
        fn0 = os.path.join(lpth, mfnam)


    # write free format files - wont run without resetting to free format - evt externa file issue
    m.free_format_input = True

    # rewrite files
    m.change_model_ws(apth, reset_external=True)  # l1b2k_bath wont run without this
    m.write_input()
    if run:
        try:
            success, buff = m.run_model(silent=True)
        except:
            pass
        assert success, 'base model run did not terminate successfully'
        fn1 = os.path.join(apth, mfnam)

        fsum = os.path.join(compth,
                            '{}.budget.out'.format(os.path.splitext(mfnam)[0]))
        success = pymake.compare_budget(fn0, fn1,
                                        max_incpd=0.1, max_cumpd=0.1,
                                        outfile=fsum)
        assert success, 'budget comparison failure'


    return
def test_gage_load_and_write():
    """
    test043 load and write of MODFLOW-2005 GAGE example problem
    """
    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

    # load the modflow model
    mf = flopy.modflow.Modflow.load('testsfr2_tab.nam',
                                    verbose=True,
                                    model_ws=opth,
                                    exe_name=exe_name)

    # run the modflow-2005 model
    if run:
        success, buff = mf.run_model(silent=False)
        assert success, 'could not run original MODFLOW-2005 model'

        try:
            files = mf.gage.files
        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)

    # write the modflow model in to the new path
    mf.write_input()

    # run the modflow-2005 model
    if run:
        success, buff = mf.run_model(silent=False)
        assert success, 'could not run new MODFLOW-2005 model'

        # compare the two results
        try:
            for f in files:
                pth0 = os.path.join(opth, f)
                pth1 = os.path.join(npth, f)
                msg = 'new and original gage file "{}" '.format(f) + \
                      'are not binary equal.'
                assert filecmp.cmp(pth0, pth1), msg
        except:
            raise ValueError('could not load new GAGE output files')
Example #6
0
def test_gage_load_and_write():
    """
    test043 load and write of MODFLOW-2005 GAGE example problem
    """
    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

    # load the modflow model
    mf = flopy.modflow.Modflow.load('testsfr2_tab.nam', verbose=True,
                                    model_ws=opth, exe_name=exe_name)

    # run the modflow-2005 model
    if run:
        success, buff = mf.run_model(silent=False)
        assert success, 'could not run original MODFLOW-2005 model'

        try:
            files = mf.gage.files
        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)

    # write the modflow model in to the new path
    mf.write_input()

    # run the modflow-2005 model
    if run:
        success, buff = mf.run_model(silent=False)
        assert success, 'could not run new MODFLOW-2005 model'

        # compare the two results
        try:
            for f in files:
                pth0 = os.path.join(opth, f)
                pth1 = os.path.join(npth, f)
                msg = 'new and original gage file "{}" '.format(f) + \
                      'are not binary equal.'
                assert filecmp.cmp(pth0, pth1), msg
        except:
            raise ValueError('could not load new GAGE output files')
Example #7
0
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
Example #8
0
def load_and_write(mfnam, pth):
    """
    test045 load and write of MODFLOW-2005 GMG example problem
    """
    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

    m = flopy.modflow.Modflow.load(mfnam, model_ws=lpth, verbose=True,
                                   exe_name=exe_name)
    assert m.load_fail is False

    if run:
        try:
            success, buff = m.run_model(silent=False)
        except:
            success = False
        assert success, 'base model run did not terminate successfully'
        fn0 = os.path.join(lpth, mfnam)

    # change model workspace
    m.change_model_ws(apth)

    # recreate oc file
    oc = m.oc
    unitnumber = [oc.unit_number[0], oc.iuhead, oc.iuddn, 0, 0]
    spd = {(0,0): ['save head', 'save drawdown']}
    chedfm = '(10(1X1PE13.5))'
    cddnfm = '(10(1X1PE13.5))'
    oc = flopy.modflow.ModflowOc(m, stress_period_data=spd,
                                 chedfm=chedfm, cddnfm=cddnfm,
                                 unitnumber=unitnumber)

    # rewrite files
    m.write_input()

    if run:
        try:
            success, buff = m.run_model(silent=False)
        except:
            success = False
        assert success, 'new model run did not terminate successfully'
        fn1 = os.path.join(apth, mfnam)

    if run:
        # compare heads
        fsum = os.path.join(compth,
                            '{}.head.out'.format(os.path.splitext(mfnam)[0]))
        success = False
        try:
            success = pymake.compare_heads(fn0, fn1, outfile=fsum)
        except:
            success = False
            print('could not perform head comparison')

        assert success, 'head comparison failure'

        # compare heads
        fsum = os.path.join(compth,
                            '{}.ddn.out'.format(os.path.splitext(mfnam)[0]))
        success = False
        try:
            success = pymake.compare_heads(fn0, fn1, outfile=fsum,
                                           text='drawdown')
        except:
            success = False
            print('could not perform drawdown comparison')

        assert success, 'head comparison failure'

        # compare budgets
        fsum = os.path.join(compth,
                            '{}.budget.out'.format(os.path.splitext(mfnam)[0]))
        success = False
        try:
            success = pymake.compare_budget(fn0, fn1,
                                            max_incpd=0.1, max_cumpd=0.1,
                                            outfile=fsum)
        except:
            success = False
            print('could not perform budget comparison')

        assert success, 'budget comparison failure'

    return
Example #9
0
def load_lak(mfnam, pth, run):
    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

    m = flopy.modflow.Modflow.load(mfnam,
                                   model_ws=lpth,
                                   verbose=True,
                                   exe_name=exe_name)
    assert m.load_fail is False

    if run:
        try:
            success, buff = m.run_model(silent=True)
        except:
            msg = 'could not run base model ' + \
                  '{}'.format(os.path.splitext(mfnam)[0])
            print(msg)
            pass
        msg = 'base model {} '.format(os.path.splitext(mfnam)[0]) + \
              'run did not terminate successfully'
        assert success, msg
        msg = 'base model {} '.format(os.path.splitext(mfnam)[0]) + \
              'run terminated successfully'
        print(msg)
        fn0 = os.path.join(lpth, mfnam)

    # write free format files - wont run without resetting to free format - evt externa file issue
    m.free_format_input = True

    # rewrite files
    m.change_model_ws(apth,
                      reset_external=True)  # l1b2k_bath wont run without this
    m.write_input()
    if run:
        try:
            success, buff = m.run_model(silent=False)
        except:
            msg = 'could not run new model ' + \
                  '{}'.format(os.path.splitext(mfnam)[0])
            print(msg)
            pass
        msg = 'new model {} '.format(os.path.splitext(mfnam)[0]) + \
              'run did not terminate successfully'
        assert success, msg
        msg = 'new model {} '.format(os.path.splitext(mfnam)[0]) + \
              'run terminated successfully'
        print(msg)
        fn1 = os.path.join(apth, mfnam)

        fsum = os.path.join(compth,
                            '{}.budget.out'.format(os.path.splitext(mfnam)[0]))
    if run:
        try:
            success = pymake.compare_budget(fn0,
                                            fn1,
                                            max_incpd=0.1,
                                            max_cumpd=0.1,
                                            outfile=fsum)
        except:
            print('could not perform budget comparison')

        assert success, 'budget comparison failure'

    return
Example #10
0
def test_uzf_unit_numbers():
    exe_name = 'mf2005'
    v = flopy.which(exe_name)

    run = True
    if v is None:
        run = False

    mfnam = 'UZFtest2.nam'
    pt = 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

    m = flopy.modflow.Modflow.load(mfnam,
                                   verbose=True,
                                   model_ws=lpth,
                                   forgive=False,
                                   exe_name=exe_name)
    assert m.load_fail is False, 'failed to load all packages'

    if run:
        try:
            success, buff = m.run_model(silent=False)
        except:
            pass
        assert success, 'base model run did not terminate successfully'
        fn0 = os.path.join(lpth, 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)

    # rewrite files
    m.write_input()

    # run and compare the output files
    if run:
        try:
            success, buff = m.run_model(silent=False)
        except:
            pass
        assert success, 'base model run did not terminate successfully'
        fn1 = os.path.join(apth, mfnam)

    if run:
        fsum = os.path.join(compth,
                            '{}.budget.out'.format(os.path.splitext(mfnam)[0]))
        try:
            success = pymake.compare_budget(fn0,
                                            fn1,
                                            max_incpd=0.1,
                                            max_cumpd=0.1,
                                            outfile=fsum)
        except:
            print('could not perform ls' 'budget comparison')

        assert success, 'budget comparison failure'

    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

    # 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()
Example #12
0
def load_lak(mfnam, pth, run):
    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

    m = flopy.modflow.Modflow.load(mfnam, model_ws=lpth, verbose=True, forgive=False,
                                   exe_name=exe_name)
    assert m.load_fail is False

    if run:
        try:
            success, buff = m.run_model(silent=True)
        except:
            msg = 'could not run base model ' + \
                  '{}'.format(os.path.splitext(mfnam)[0])
            print(msg)
            pass
        msg = 'base model {} '.format(os.path.splitext(mfnam)[0]) + \
              'run did not terminate successfully'
        assert success, msg
        msg = 'base model {} '.format(os.path.splitext(mfnam)[0]) + \
              'run terminated successfully'
        print(msg)
        fn0 = os.path.join(lpth, mfnam)


    # write free format files - wont run without resetting to free format - evt externa file issue
    m.free_format_input = True

    # rewrite files
    m.change_model_ws(apth, reset_external=True)  # l1b2k_bath wont run without this
    m.write_input()
    if run:
        try:
            success, buff = m.run_model(silent=False)
        except:
            msg = 'could not run new model ' + \
                  '{}'.format(os.path.splitext(mfnam)[0])
            print(msg)
            pass
        msg = 'new model {} '.format(os.path.splitext(mfnam)[0]) + \
              'run did not terminate successfully'
        assert success, msg
        msg = 'new model {} '.format(os.path.splitext(mfnam)[0]) + \
              'run terminated successfully'
        print(msg)
        fn1 = os.path.join(apth, mfnam)

        fsum = os.path.join(compth,
                            '{}.budget.out'.format(os.path.splitext(mfnam)[0]))
    if run:
        try:
            success = pymake.compare_budget(fn0, fn1,
                                            max_incpd=0.1, max_cumpd=0.1,
                                            outfile=fsum)
        except:
            print('could not perform budget comparison')

        assert success, 'budget comparison failure'


    return
Example #13
0
def test_uzf_unit_numbers():
    exe_name = 'mf2005'
    v = flopy.which(exe_name)

    run = True
    if v is None:
        run = False

    mfnam = 'UZFtest2.nam'
    pt = 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

    m = flopy.modflow.Modflow.load(mfnam, verbose=True, model_ws=lpth,
                                   exe_name=exe_name)
    assert m.load_fail is False, 'failed to load all packages'

    if run:
        try:
            success, buff = m.run_model(silent=False)
        except:
            pass
        assert success, 'base model run did not terminate successfully'
        fn0 = os.path.join(lpth, 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)

    # rewrite files
    m.write_input()

    # run and compare the output files
    if run:
        try:
            success, buff = m.run_model(silent=False)
        except:
            pass
        assert success, 'base model run did not terminate successfully'
        fn1 = os.path.join(apth, mfnam)

    if run:
        fsum = os.path.join(compth,
                            '{}.budget.out'.format(os.path.splitext(mfnam)[0]))
        try:
            success = pymake.compare_budget(fn0, fn1,
                                            max_incpd=0.1, max_cumpd=0.1,
                                            outfile=fsum)
        except:
            print('could not perform ls'
                  'budget comparison')

        assert success, 'budget comparison failure'

    return
Example #14
0
def test_uzf_unit_numbers():
    exe_name = 'mf2005'
    v = flopy.which(exe_name)

    run = True
    if v is None:
        run = False

    mfnam = 'UZFtest2.nam'
    pt = 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

    m = flopy.modflow.Modflow.load(mfnam, verbose=True, model_ws=lpth,
                                   forgive=False, exe_name=exe_name)
    assert m.load_fail is False, 'failed to load all packages'

    # reset the oc file
    m.remove_package('OC')
    output = ['save head', 'print budget']
    spd = {}
    for iper in range(1, m.dis.nper):
        for istp in [0, 4, 9, 14]:
            spd[(iper, istp)] = output
    spd[(0, 0)] = output
    spd[(1, 1)] = output
    spd[(1, 2)] = output
    spd[(1, 3)] = output
    oc = flopy.modflow.ModflowOc(m, stress_period_data=spd)
    oc.write_file()

    if run:
        try:
            success, buff = m.run_model(silent=False)
        except:
            success = False
        assert success, 'base model run did not terminate successfully'
        fn0 = os.path.join(lpth, 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)

    # rewrite files
    m.write_input()

    # run and compare the output files
    if run:
        try:
            success, buff = m.run_model(silent=False)
        except:
            success = False
        assert success, 'new model run did not terminate successfully'
        fn1 = os.path.join(apth, mfnam)

    # compare budget terms
    if run:
        fsum = os.path.join(compth,
                            '{}.budget.out'.format(os.path.splitext(mfnam)[0]))
        try:
            success = pymake.compare_budget(fn0, fn1,
                                            max_incpd=0.1, max_cumpd=0.1,
                                            outfile=fsum)
        except:
            success = False
            print('could not perform ls'
                  'budget comparison')

        assert success, 'budget comparison failure'

    # clean up
    shutil.rmtree(lpth)

    return
Example #15
0
def load_and_write_gmg(mfnam, pth):
    """
    test045 load and write of MODFLOW-2005 GMG example problem
    """
    exe_name = "mf2005"
    v = flopy.which(exe_name)

    if pymake:
        run = v is not None
        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)
    else:
        run = False
        lpth = pth
        apth = cpth
        compth = cpth

    m = flopy.modflow.Modflow.load(
        mfnam, model_ws=lpth, verbose=True, exe_name=exe_name
    )
    assert m.load_fail is False

    if run:
        try:
            success, buff = m.run_model(silent=False)
        except:
            success = False
        assert success, "base model run did not terminate successfully"
        fn0 = os.path.join(lpth, mfnam)

    # rewrite files
    m.change_model_ws(apth)
    m.write_input()
    if run:
        try:
            success, buff = m.run_model(silent=False)
        except:
            success = False
        assert success, "new model run did not terminate successfully"
        fn1 = os.path.join(apth, mfnam)

    if run:
        fsum = os.path.join(
            compth, "{}.head.out".format(os.path.splitext(mfnam)[0])
        )
        success = False
        try:
            success = pymake.compare_heads(fn0, fn1, outfile=fsum)
        except:
            success = False
            print("could not perform head comparison")

        assert success, "head comparison failure"

        fsum = os.path.join(
            compth, "{}.budget.out".format(os.path.splitext(mfnam)[0])
        )
        success = False
        try:
            success = pymake.compare_budget(
                fn0, fn1, max_incpd=0.1, max_cumpd=0.1, outfile=fsum
            )
        except:
            success = False
            print("could not perform budget comparison")

        assert success, "budget comparison failure"

    return
Example #16
0
def test_modpath():
    pth = os.path.join("..", "examples", "data", "freyberg")
    mfnam = "freyberg.nam"

    if pymake:
        run = rung
        lpth = os.path.join(cpth, os.path.splitext(mfnam)[0])
        pymake.setup(os.path.join(pth, mfnam), lpth)
    else:
        run = False
        lpth = pth

    m = flopy.modflow.Modflow.load(
        mfnam, model_ws=lpth, verbose=True, exe_name=mf2005_exe
    )
    assert m.load_fail is False

    if run:
        try:
            success, buff = m.run_model(silent=False)
        except:
            success = False
        assert success, "modflow model run did not terminate successfully"

    # create the forward modpath file
    mpnam = "freybergmp"
    mp = flopy.modpath.Modpath6(
        mpnam, exe_name=mpth_exe, modflowmodel=m, model_ws=lpth
    )
    mpbas = flopy.modpath.Modpath6Bas(
        mp,
        hnoflo=m.bas6.hnoflo,
        hdry=m.lpf.hdry,
        ibound=m.bas6.ibound.array,
        prsity=0.2,
        prsityCB=0.2,
    )
    sim = mp.create_mpsim(
        trackdir="forward", simtype="endpoint", packages="RCH"
    )

    # write forward particle track files
    mp.write_input()

    if run:
        try:
            success, buff = mp.run_model(silent=False)
        except:
            success = False
        assert (
            success
        ), "forward modpath model run did not terminate successfully"

    mpnam = "freybergmpp"
    mpp = flopy.modpath.Modpath6(
        mpnam, exe_name=mpth_exe, modflowmodel=m, model_ws=lpth
    )
    mpbas = flopy.modpath.Modpath6Bas(
        mpp,
        hnoflo=m.bas6.hnoflo,
        hdry=m.lpf.hdry,
        ibound=m.bas6.ibound.array,
        prsity=0.2,
        prsityCB=0.2,
    )
    sim = mpp.create_mpsim(
        trackdir="backward", simtype="pathline", packages="WEL"
    )

    # write backward particle track files
    mpp.write_input()

    if run:
        try:
            success, buff = mpp.run_model(silent=False)
        except:
            success = False
        assert (
            success
        ), "backward modpath model run did not terminate successfully"

    # load modpath output files
    if run:
        endfile = os.path.join(lpth, mp.sim.endpoint_file)
        pthfile = os.path.join(lpth, mpp.sim.pathline_file)
    else:
        endfile = os.path.join(
            "..", "examples", "data", "mp6_examples", "freybergmp.gitmpend"
        )
        pthfile = os.path.join(
            "..", "examples", "data", "mp6_examples", "freybergmpp.gitmppth"
        )

    # load the endpoint data
    try:
        endobj = flopy.utils.EndpointFile(endfile)
    except:
        assert False, "could not load endpoint file"
    ept = endobj.get_alldata()
    assert ept.shape == (695,), "shape of endpoint file is not (695,)"

    # load the pathline data
    try:
        pthobj = flopy.utils.PathlineFile(pthfile)
    except:
        assert False, "could not load pathline file"
    plines = pthobj.get_alldata()
    assert len(plines) == 576, "there are not 576 particle pathlines in file"

    return
Example #17
0
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
Example #18
0
def test_modpath():

    pth = os.path.join('..', 'examples', 'data', 'freyberg')
    mfnam = 'freyberg.nam'

    run = rung
    try:
        import pymake
        lpth = os.path.join(cpth, os.path.splitext(mfnam)[0])
        pymake.setup(os.path.join(pth, mfnam), lpth)
    except:
        run = False
        lpth = pth

    m = flopy.modflow.Modflow.load(mfnam, model_ws=lpth, verbose=True,
                                   exe_name=mf2005_exe)
    assert m.load_fail is False

    if run:
        try:
            success, buff = m.run_model(silent=False)
        except:
            pass
        assert success, 'modflow model run did not terminate successfully'

    # create the forward modpath file
    mpnam = 'freybergmp'
    mp = flopy.modpath.Modpath(mpnam, exe_name=mpth_exe, modflowmodel=m,
                               model_ws=lpth)
    mpbas = flopy.modpath.ModpathBas(mp, hnoflo=m.bas6.hnoflo,
                                     hdry=m.lpf.hdry,
                                     ibound=m.bas6.ibound.array, prsity=0.2,
                                     prsityCB=0.2)
    sim = mp.create_mpsim(trackdir='forward', simtype='endpoint',
                          packages='RCH')

    # write forward particle track files
    mp.write_input()

    if run:
        try:
            success, buff = mp.run_model(silent=False)
        except:
            pass
        assert success, 'forward modpath model run ' + \
                        'did not terminate successfully'

    mpnam = 'freybergmpp'
    mpp = flopy.modpath.Modpath(mpnam, exe_name=mpth_exe,
                                modflowmodel=m, model_ws=lpth)
    mpbas = flopy.modpath.ModpathBas(mpp, hnoflo=m.bas6.hnoflo,
                                     hdry=m.lpf.hdry,
                                     ibound=m.bas6.ibound.array, prsity=0.2,
                                     prsityCB=0.2)
    sim = mpp.create_mpsim(trackdir='backward', simtype='pathline',
                           packages='WEL')

    # write backward particle track files
    mpp.write_input()

    if run:
        try:
            success, buff = mpp.run_model(silent=False)
        except:
            pass
        assert success, 'backward modpath model run ' + \
                        'did not terminate successfully'

    # load modpath output files
    if run:
        endfile = os.path.join(lpth, mp.sim.endpoint_file)
        pthfile = os.path.join(lpth, mpp.sim.pathline_file)
    else:
        endfile = os.path.join('..', 'examples', 'data', 'mp6_examples',
                               'freybergmp.gitmpend')
        pthfile = os.path.join('..', 'examples', 'data', 'mp6_examples',
                               'freybergmpp.gitmppth')

    # load the endpoint data
    try:
        endobj = flopy.utils.EndpointFile(endfile)
    except:
        assert False, 'could not load endpoint file'
    ept = endobj.get_alldata()
    assert ept.shape == (695,), 'shape of endpoint file is not (695,)'

    # load the pathline data
    try:
        pthobj = flopy.utils.PathlineFile(pthfile)
    except:
        assert False, 'could not load pathline file'
    plines = pthobj.get_alldata()
    assert len(plines) == 576, 'there are not 576 particle pathlines in file'

    return
Example #19
0
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=False,
                                    report=True)

    if success:
        # For this particular test, a lengthy period of "stop flow"
        # needs to be inserted into the linker file for simulating
        # a period of equilibration.
        print('running insert_stopflow_period.py...{}'.format(testname))
        insert_stopflow_period.InsStpFlw()

        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_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()
Example #21
0
def load_and_write_pcgn(mfnam, pth):
    """
    test044 load and write of MODFLOW-2005 PCGN example problem
    """
    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

    m = flopy.modflow.Modflow.load(mfnam, model_ws=lpth, verbose=True,
                                   exe_name=exe_name)
    assert m.load_fail is False
    if mfnam in ['twri.nam']:  # update this list for fixed models
        assert m.free_format_input is False
    else:
        assert m.free_format_input is True

    if run:
        try:
            success, buff = m.run_model(silent=False)
        except:
            success = False
        assert success, 'base model run did not terminate successfully'
        fn0 = os.path.join(lpth, mfnam)

    # rewrite files
    m.change_model_ws(apth)
    m.write_input()
    if run:
        try:
            success, buff = m.run_model(silent=False)
        except:
            success = False
        assert success, 'new model run did not terminate successfully'
        fn1 = os.path.join(apth, mfnam)

    if run:
        fsum = os.path.join(compth,
                            '{}.head.out'.format(os.path.splitext(mfnam)[0]))
        success = False
        try:
            success = pymake.compare_heads(fn0, fn1, outfile=fsum, htol=0.005)
        except:
            success = False
            print('could not perform head comparison')

        assert success, 'head comparison failure'

        fsum = os.path.join(compth,
                            '{}.budget.out'.format(os.path.splitext(mfnam)[0]))
        success = False
        try:
            success = pymake.compare_budget(fn0, fn1,
                                            max_incpd=0.1, max_cumpd=0.1,
                                            outfile=fsum)
        except:
            success = False
            print('could not perform budget comparison')

        assert success, 'budget comparison failure'

    return
Example #22
0
def test_unitnums_load_and_write():
    exe_name = 'mf2005'
    v = flopy.which(exe_name)

    run = True
    if v is None:
        run = False

    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

    m = flopy.modflow.Modflow.load(mfnam, verbose=True, model_ws=lpth,
                                   exe_name=exe_name)
    assert m.load_fail is False, 'failed to load all packages'

    msg = 'modflow-2005 testsfr2_tab does not have ' + \
          '1 layer, 7 rows, and 100 colummns'
    v = (m.nlay, m.nrow, m.ncol, m.nper)
    assert v == (1, 7, 100, 50), msg

    if run:
        try:
            success, buff = m.run_model(silent=False)
        except:
            pass
        assert success, 'base model run did not terminate successfully'
        fn0 = os.path.join(lpth, mfnam)

    # rewrite files
    m.change_model_ws(apth, reset_external=True)
    m.write_input()
    if run:
        try:
            success, buff = m.run_model(silent=False)
        except:
            pass
        assert success, 'base model run did not terminate successfully'
        fn1 = os.path.join(apth, mfnam)

    if run:
        fsum = os.path.join(compth,
                            '{}.budget.out'.format(os.path.splitext(mfnam)[0]))
        try:
            success = pymake.compare_budget(fn0, fn1,
                                            max_incpd=0.1, max_cumpd=0.1,
                                            outfile=fsum)
        except:
            print('could not perform ls'
                  'budget comparison')

        assert success, 'budget comparison failure'

    return
Example #23
0
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
Example #24
0
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
Example #25
0
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()
Example #26
0
def load_and_write(mfnam, pth):
    """
    test045 load and write of MODFLOW-2005 GMG example problem
    """
    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

    m = flopy.modflow.Modflow.load(mfnam,
                                   model_ws=lpth,
                                   verbose=True,
                                   exe_name=exe_name)
    assert m.load_fail is False

    if run:
        try:
            success, buff = m.run_model(silent=False)
        except:
            success = False
        assert success, 'base model run did not terminate successfully'
        fn0 = os.path.join(lpth, mfnam)

    # change model workspace
    m.change_model_ws(apth)

    # recreate oc file
    oc = m.oc
    unitnumber = [oc.unit_number[0], oc.iuhead, oc.iuddn, 0, 0]
    spd = {(0, 0): ['save head', 'save drawdown']}
    chedfm = '(10(1X1PE13.5))'
    cddnfm = '(10(1X1PE13.5))'
    oc = flopy.modflow.ModflowOc(m,
                                 stress_period_data=spd,
                                 chedfm=chedfm,
                                 cddnfm=cddnfm,
                                 unitnumber=unitnumber)

    # rewrite files
    m.write_input()

    if run:
        try:
            success, buff = m.run_model(silent=False)
        except:
            success = False
        assert success, 'new model run did not terminate successfully'
        fn1 = os.path.join(apth, mfnam)

    if run:
        # compare heads
        fsum = os.path.join(compth,
                            '{}.head.out'.format(os.path.splitext(mfnam)[0]))
        success = False
        try:
            success = pymake.compare_heads(fn0, fn1, outfile=fsum)
        except:
            success = False
            print('could not perform head comparison')

        assert success, 'head comparison failure'

        # compare heads
        fsum = os.path.join(compth,
                            '{}.ddn.out'.format(os.path.splitext(mfnam)[0]))
        success = False
        try:
            success = pymake.compare_heads(fn0,
                                           fn1,
                                           outfile=fsum,
                                           text='drawdown')
        except:
            success = False
            print('could not perform drawdown comparison')

        assert success, 'head comparison failure'

        # compare budgets
        fsum = os.path.join(compth,
                            '{}.budget.out'.format(os.path.splitext(mfnam)[0]))
        success = False
        try:
            success = pymake.compare_budget(fn0,
                                            fn1,
                                            max_incpd=0.1,
                                            max_cumpd=0.1,
                                            outfile=fsum)
        except:
            success = False
            print('could not perform budget comparison')

        assert success, 'budget comparison failure'

    return
Example #27
0
def test_unitnums_load_and_write():
    exe_name = 'mf2005'
    v = flopy.which(exe_name)

    run = True
    if v is None:
        run = False

    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

    m = flopy.modflow.Modflow.load(mfnam,
                                   verbose=True,
                                   model_ws=lpth,
                                   exe_name=exe_name)
    assert m.load_fail is False, 'failed to load all packages'

    msg = 'modflow-2005 testsfr2_tab does not have ' + \
          '1 layer, 7 rows, and 100 colummns'
    v = (m.nlay, m.nrow, m.ncol, m.nper)
    assert v == (1, 7, 100, 50), msg

    if run:
        try:
            success, buff = m.run_model(silent=False)
        except:
            pass
        assert success, 'base model run did not terminate successfully'
        fn0 = os.path.join(lpth, mfnam)

    # rewrite files
    m.change_model_ws(apth, reset_external=True)
    m.write_input()
    if run:
        try:
            success, buff = m.run_model(silent=False)
        except:
            pass
        assert success, 'base model run did not terminate successfully'
        fn1 = os.path.join(apth, mfnam)

    if run:
        fsum = os.path.join(compth,
                            '{}.budget.out'.format(os.path.splitext(mfnam)[0]))
        try:
            success = pymake.compare_budget(fn0,
                                            fn1,
                                            max_incpd=0.1,
                                            max_cumpd=0.1,
                                            outfile=fsum)
        except:
            print('could not perform ls' 'budget comparison')

        assert success, 'budget comparison failure'

    return
Example #28
0
def test_unitnums_load_and_write():
    exe_name = "mf2005"
    v = flopy.which(exe_name)

    run = True
    if v is None:
        run = False

    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

    m = flopy.modflow.Modflow.load(mfnam,
                                   verbose=True,
                                   model_ws=lpth,
                                   exe_name=exe_name)
    assert m.load_fail is False, "failed to load all packages"

    msg = ("modflow-2005 testsfr2_tab does not have " +
           "1 layer, 7 rows, and 100 columns")
    v = (m.nlay, m.nrow, m.ncol, m.nper)
    assert v == (1, 7, 100, 50), msg

    if run:
        try:
            success, buff = m.run_model(silent=False)
        except:
            success = False
        assert success, "base model run did not terminate successfully"
        fn0 = os.path.join(lpth, mfnam)

    # rewrite files
    m.change_model_ws(apth, 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)

    if run:
        fsum = os.path.join(compth,
                            "{}.budget.out".format(os.path.splitext(mfnam)[0]))
        try:
            success = pymake.compare_budget(fn0,
                                            fn1,
                                            max_incpd=0.1,
                                            max_cumpd=0.1,
                                            outfile=fsum)
        except:
            success = False
            print("could not perform ls" "budget comparison")

        assert success, "budget comparison failure"

    # clean up
    shutil.rmtree(lpth)

    return
Example #29
0
def load_and_write_fhb(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

    m = flopy.modflow.Modflow.load(mfnam,
                                   model_ws=lpth,
                                   verbose=True,
                                   exe_name=exe_name)
    assert m.load_fail is False

    if run:
        try:
            success, buff = m.run_model(silent=False)
        except:
            success = False
        assert success, 'base model run did not terminate successfully'
        fn0 = os.path.join(lpth, mfnam)

    # rewrite files
    m.change_model_ws(apth, reset_external=True)
    m.write_input()
    if run:
        try:
            success, buff = m.run_model(silent=False)
        except:
            success = False
        assert success, 'new model run did not terminate successfully'
        fn1 = os.path.join(apth, mfnam)

    if run:
        fsum = os.path.join(compth,
                            '{}.head.out'.format(os.path.splitext(mfnam)[0]))
        success = False
        try:
            success = pymake.compare_heads(fn0, fn1, outfile=fsum)
        except:
            success = False
            print('could not perform head comparison')

        assert success, 'head comparison failure'

        fsum = os.path.join(compth,
                            '{}.budget.out'.format(os.path.splitext(mfnam)[0]))
        success = False
        try:
            success = pymake.compare_budget(fn0,
                                            fn1,
                                            max_incpd=0.1,
                                            max_cumpd=0.1,
                                            outfile=fsum)
        except:
            success = False
            print('could not perform budget comparison')

        assert success, 'budget comparison failure'

    return
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
Example #31
0
def load_and_write_fhb(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

    m = flopy.modflow.Modflow.load(mfnam, model_ws=lpth, verbose=True,
                                   exe_name=exe_name)
    assert m.load_fail is False

    if run:
        try:
            success, buff = m.run_model(silent=False)
        except:
            pass
        assert success, 'base model run did not terminate successfully'
        fn0 = os.path.join(lpth, mfnam)

    # rewrite files
    m.change_model_ws(apth, reset_external=True)
    m.write_input()
    if run:
        try:
            success, buff = m.run_model(silent=False)
        except:
            pass
        assert success, 'new model run did not terminate successfully'
        fn1 = os.path.join(apth, mfnam)

    if run:
        fsum = os.path.join(compth,
                            '{}.head.out'.format(os.path.splitext(mfnam)[0]))
        success = False
        try:
            success = pymake.compare_heads(fn0, fn1, outfile=fsum)
        except:
            print('could not perform head comparison')

        assert success, 'head comparison failure'

        fsum = os.path.join(compth,
                            '{}.budget.out'.format(os.path.splitext(mfnam)[0]))
        success = False
        try:
            success = pymake.compare_budget(fn0, fn1,
                                            max_incpd=0.1, max_cumpd=0.1,
                                            outfile=fsum)
        except:
            print('could not perform budget comparison')

        assert success, 'budget comparison failure'

    return
Example #32
0
def run_mfusg(namefile, regression=True):
    """
    Run the simulation.

    """

    # Set root as the directory name where namefile is located
    testname = os.path.dirname(namefile).split(os.sep)[-1]

    # 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,
                                    report=True)
    f = open(os.path.join(testpth, 'output.dat'), 'w')
    for line in buff:
        f.write(line + '\n')
    f.close()
    assert success, 'model did not run successfully: {}'.format(testname)

    # If it is a regression run, then setup and run the model with the
    # release 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, buff = flopy.run_model(exe_name,
                                        nam,
                                        model_ws=testpth_reg,
                                        silent=True,
                                        report=True)
        f = open(os.path.join(testpth_reg, 'output.dat'), 'w')
        for line in buff:
            f.write(line + '\n')
        f.close()
        assert success, 'model did not run successfully: {}'.format(
            testname_reg)

        # Make comparison
        success = compare(os.path.join(testpth, nam),
                          os.path.join(testpth_reg, nam))
        assert success, 'Models do not compare: {} and {}'.format(
            testname, testname_reg)

    # Clean things up
    if success and not config.retain:
        pymake.teardown(testpth)
    assert success and success_reg

    return
Example #33
0
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
Example #34
0
def run_mt3d(mfnamefile, mtnamefile, regression=True):
    """
    Run the simulations.

    """

    # Set root as the directory name where namefile is located
    flowexe = config.target_dict['mfnwt']
    if '_mf2k' in mfnamefile:
        crep = '_mf2k'
        flowexe = config.target_dict['mf2k']
    elif '_mf2005' in mfnamefile:
        crep = '_mf2005'
        flowexe = config.target_dict['mf2005']
    else:
        crep = '_mf'
        flowexe = config.target_dict['mfnwt']
    testname = pymake.get_sim_name(mfnamefile.replace(crep, ''),
                                   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 regression:
        testname_reg = os.path.basename(config.target_release)
        testpth_reg = os.path.join(testpth, testname_reg)
        pymake.setup(mfnamefile, testpth_reg)
        pymake.setup(mtnamefile, testpth_reg, remove_existing=False)
        print('running regression {} model...{}'.format(
            os.path.basename(flowexe), testpth_reg))
        nam = os.path.basename(mfnamefile)
        exe_name = flowexe  #config.target_dict['mfnwt']
        success_reg, buff = flopy.run_model(exe_name,
                                            nam,
                                            model_ws=testpth_reg,
                                            silent=True)
        if success_reg:
            print('running regression mt3dms model...{}'.format(testpth_reg))
            nam = os.path.basename(mtnamefile)
            exe_name = os.path.abspath(config.target_release)
            success_reg, buff = flopy.run_model(exe_name,
                                                nam,
                                                model_ws=testpth_reg,
                                                silent=True,
                                                normal_msg='program completed')
            if success_reg:
                nam = os.path.basename(mtnamefile)
                namefile1 = os.path.join(testpth, nam)
                namefile2 = os.path.join(testpth_reg, 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.001,
                                                   outfile=outfileucn)
                if success_reg and 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
Example #35
0
def test_uzf_unit_numbers():
    exe_name = "mf2005"
    v = flopy.which(exe_name)

    run = True
    if v is None:
        run = False

    mfnam = "UZFtest2.nam"
    pt = 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

    m = flopy.modflow.Modflow.load(mfnam,
                                   verbose=True,
                                   model_ws=lpth,
                                   forgive=False,
                                   exe_name=exe_name)
    assert m.load_fail is False, "failed to load all packages"

    # reset the oc file
    m.remove_package("OC")
    output = ["save head", "print budget"]
    spd = {}
    for iper in range(1, m.dis.nper):
        for istp in [0, 4, 9, 14]:
            spd[(iper, istp)] = output
    spd[(0, 0)] = output
    spd[(1, 1)] = output
    spd[(1, 2)] = output
    spd[(1, 3)] = output
    oc = flopy.modflow.ModflowOc(m, stress_period_data=spd)
    oc.write_file()

    if run:
        try:
            success, buff = m.run_model(silent=False)
        except:
            success = False
        assert success, "base model run did not terminate successfully"
        fn0 = os.path.join(lpth, 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)

    # rewrite files
    m.write_input()

    # run and compare the output files
    if run:
        try:
            success, buff = m.run_model(silent=False)
        except:
            success = False
        assert success, "new model run did not terminate successfully"
        fn1 = os.path.join(apth, mfnam)

    # compare budget terms
    if run:
        fsum = os.path.join(compth,
                            "{}.budget.out".format(os.path.splitext(mfnam)[0]))
        try:
            success = pymake.compare_budget(fn0,
                                            fn1,
                                            max_incpd=0.1,
                                            max_cumpd=0.1,
                                            outfile=fsum)
        except:
            success = False
            print("could not perform ls" "budget comparison")

        assert success, "budget comparison failure"

    # clean up
    shutil.rmtree(lpth)

    return
Example #36
0
def test_modpath():
    pth = os.path.join('..', 'examples', 'data', 'freyberg')
    mfnam = 'freyberg.nam'

    run = rung
    try:
        import pymake
        lpth = os.path.join(cpth, os.path.splitext(mfnam)[0])
        pymake.setup(os.path.join(pth, mfnam), lpth)
    except:
        run = False
        lpth = pth

    m = flopy.modflow.Modflow.load(mfnam,
                                   model_ws=lpth,
                                   verbose=True,
                                   exe_name=mf2005_exe)
    assert m.load_fail is False

    if run:
        try:
            success, buff = m.run_model(silent=False)
        except:
            success = False
        assert success, 'modflow model run did not terminate successfully'

    # create the forward modpath file
    mpnam = 'freybergmp'
    mp = flopy.modpath.Modpath(mpnam,
                               exe_name=mpth_exe,
                               modflowmodel=m,
                               model_ws=lpth)
    mpbas = flopy.modpath.ModpathBas(mp,
                                     hnoflo=m.bas6.hnoflo,
                                     hdry=m.lpf.hdry,
                                     ibound=m.bas6.ibound.array,
                                     prsity=0.2,
                                     prsityCB=0.2)
    sim = mp.create_mpsim(trackdir='forward',
                          simtype='endpoint',
                          packages='RCH')

    # write forward particle track files
    mp.write_input()

    if run:
        try:
            success, buff = mp.run_model(silent=False)
        except:
            success = False
        assert success, 'forward modpath model run ' + \
                        'did not terminate successfully'

    mpnam = 'freybergmpp'
    mpp = flopy.modpath.Modpath(mpnam,
                                exe_name=mpth_exe,
                                modflowmodel=m,
                                model_ws=lpth)
    mpbas = flopy.modpath.ModpathBas(mpp,
                                     hnoflo=m.bas6.hnoflo,
                                     hdry=m.lpf.hdry,
                                     ibound=m.bas6.ibound.array,
                                     prsity=0.2,
                                     prsityCB=0.2)
    sim = mpp.create_mpsim(trackdir='backward',
                           simtype='pathline',
                           packages='WEL')

    # write backward particle track files
    mpp.write_input()

    if run:
        try:
            success, buff = mpp.run_model(silent=False)
        except:
            success = False
        assert success, 'backward modpath model run ' + \
                        'did not terminate successfully'

    # load modpath output files
    if run:
        endfile = os.path.join(lpth, mp.sim.endpoint_file)
        pthfile = os.path.join(lpth, mpp.sim.pathline_file)
    else:
        endfile = os.path.join('..', 'examples', 'data', 'mp6_examples',
                               'freybergmp.gitmpend')
        pthfile = os.path.join('..', 'examples', 'data', 'mp6_examples',
                               'freybergmpp.gitmppth')

    # load the endpoint data
    try:
        endobj = flopy.utils.EndpointFile(endfile)
    except:
        assert False, 'could not load endpoint file'
    ept = endobj.get_alldata()
    assert ept.shape == (695, ), 'shape of endpoint file is not (695,)'

    # load the pathline data
    try:
        pthobj = flopy.utils.PathlineFile(pthfile)
    except:
        assert False, 'could not load pathline file'
    plines = pthobj.get_alldata()
    assert len(plines) == 576, 'there are not 576 particle pathlines in file'

    return
Example #37
0
def load_and_write_pcgn(mfnam, pth):
    """
    test044 load and write of MODFLOW-2005 PCGN example problem
    """
    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

    m = flopy.modflow.Modflow.load(mfnam,
                                   model_ws=lpth,
                                   verbose=True,
                                   exe_name=exe_name)
    assert m.load_fail is False
    if mfnam in ['twri.nam']:  # update this list for fixed models
        assert m.free_format_input is False
    else:
        assert m.free_format_input is True

    if run:
        try:
            success, buff = m.run_model(silent=False)
        except:
            pass
        assert success, 'base model run did not terminate successfully'
        fn0 = os.path.join(lpth, mfnam)

    # rewrite files
    m.change_model_ws(apth)
    m.write_input()
    if run:
        try:
            success, buff = m.run_model(silent=False)
        except:
            pass
        assert success, 'new model run did not terminate successfully'
        fn1 = os.path.join(apth, mfnam)

    if run:
        fsum = os.path.join(compth,
                            '{}.head.out'.format(os.path.splitext(mfnam)[0]))
        success = False
        try:
            success = pymake.compare_heads(fn0, fn1, outfile=fsum)
        except:
            print('could not perform head comparison')

        assert success, 'head comparison failure'

        fsum = os.path.join(compth,
                            '{}.budget.out'.format(os.path.splitext(mfnam)[0]))
        success = False
        try:
            success = pymake.compare_budget(fn0,
                                            fn1,
                                            max_incpd=0.1,
                                            max_cumpd=0.1,
                                            outfile=fsum)
        except:
            print('could not perform budget comparison')

        assert success, 'budget comparison failure'

    return
Example #38
0
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