Ejemplo n.º 1
0
def flopy_mfsimulation_load(sim, model, strict=True):
    """Execute the code in flopy.mf6.MFSimulation.load on
    existing instances of flopy.mf6.MFSimulation and flopy.mf6.MF6model"""

    instance = sim
    if not isinstance(model, list):
        model_instances = [model]
    else:
        model_instances = model
    version = sim.version
    exe_name = sim.exe_name
    verbosity_level = instance.simulation_data.verbosity_level

    if verbosity_level.value >= VerbosityLevel.normal.value:
        print('loading simulation...')

    # load simulation name file
    if verbosity_level.value >= VerbosityLevel.normal.value:
        print('  loading simulation name file...')
    instance.name_file.load(strict)

    # load TDIS file
    tdis_pkg = 'tdis{}'.format(mfstructure.MFStructure().get_version_string())
    tdis_attr = getattr(instance.name_file, tdis_pkg)
    instance._tdis_file = mftdis.ModflowTdis(instance,
                                             filename=tdis_attr.get_data())

    instance._tdis_file._filename = instance.simulation_data.mfdata[(
        'nam', 'timing', tdis_pkg)].get_data()
    if verbosity_level.value >= VerbosityLevel.normal.value:
        print('  loading tdis package...')
    instance._tdis_file.load(strict)

    # load models
    try:
        model_recarray = instance.simulation_data.mfdata[('nam', 'models',
                                                          'models')]
        models = model_recarray.get_data()
    except MFDataException as mfde:
        message = 'Error occurred while loading model names from the ' \
                  'simulation name file.'
        raise MFDataException(mfdata_except=mfde,
                              model=instance.name,
                              package='nam',
                              message=message)
    for item in models:
        # resolve model working folder and name file
        path, name_file = os.path.split(item[1])
        model_obj = [m for m in model_instances if m.namefile == name_file]
        if len(model_obj) == 0:
            print('model {} attached to {} not found in {}'.format(
                item, instance, model_instances))
            return
        model_obj = model_obj[0]
        #model_obj = PackageContainer.model_factory(item[0][:-1].lower())
        # load model
        if verbosity_level.value >= VerbosityLevel.normal.value:
            print('  loading model {}...'.format(item[0].lower()))
        instance._models[item[2]] = flopy_mf6model_load(instance,
                                                        model_obj,
                                                        strict=strict,
                                                        model_rel_path=path)
        #instance._models[item[2]] = model_obj.load(
        #    instance,
        #    instance.structure.model_struct_objs[item[0].lower()], item[2],
        #    name_file, version, exe_name, strict, path)


#
# load exchange packages and dependent packages
    try:
        exchange_recarray = instance.name_file.exchanges
        has_exch_data = exchange_recarray.has_data()
    except MFDataException as mfde:
        message = 'Error occurred while loading exchange names from the ' \
                  'simulation name file.'
        raise MFDataException(mfdata_except=mfde,
                              model=instance.name,
                              package='nam',
                              message=message)
    if has_exch_data:
        try:
            exch_data = exchange_recarray.get_data()
        except MFDataException as mfde:
            message = 'Error occurred while loading exchange names from the ' \
                      'simulation name file.'
            raise MFDataException(mfdata_except=mfde,
                                  model=instance.name,
                                  package='nam',
                                  message=message)
        for exgfile in exch_data:
            # get exchange type by removing numbers from exgtype
            exchange_type = ''.join(
                [char for char in exgfile[0] if not char.isdigit()]).upper()
            # get exchange number for this type
            if not exchange_type in instance._exg_file_num:
                exchange_file_num = 0
                instance._exg_file_num[exchange_type] = 1
            else:
                exchange_file_num = instance._exg_file_num[exchange_type]
                instance._exg_file_num[exchange_type] += 1

            exchange_name = '{}_EXG_{}'.format(exchange_type,
                                               exchange_file_num)
            # find package class the corresponds to this exchange type
            package_obj = instance.package_factory(
                exchange_type.replace('-', '').lower(), '')
            if not package_obj:
                message = 'An error occurred while loading the ' \
                          'simulation name file.  Invalid exchange type ' \
                          '"{}" specified.'.format(exchange_type)
                type_, value_, traceback_ = sys.exc_info()
                raise MFDataException(instance.name, 'nam', 'nam',
                                      'loading simulation name file',
                                      exchange_recarray.structure.name,
                                      inspect.stack()[0][3], type_, value_,
                                      traceback_, message,
                                      instance._simulation_data.debug)

            # build and load exchange package object
            exchange_file = package_obj(instance,
                                        exgtype=exgfile[0],
                                        exgmnamea=exgfile[2],
                                        exgmnameb=exgfile[3],
                                        filename=exgfile[1],
                                        pname=exchange_name,
                                        loading_package=True)
            if verbosity_level.value >= VerbosityLevel.normal.value:
                print('  loading exchange package {}..'
                      '.'.format(exchange_file._get_pname()))
            exchange_file.load(strict)
            instance._exchange_files[exgfile[1]] = exchange_file

    # load simulation packages
    solution_recarray = instance.simulation_data.mfdata[('nam',
                                                         'solutiongroup',
                                                         'solutiongroup')]

    try:
        solution_group_dict = solution_recarray.get_data()
    except MFDataException as mfde:
        message = 'Error occurred while loading solution groups from ' \
                  'the simulation name file.'
        raise MFDataException(mfdata_except=mfde,
                              model=instance.name,
                              package='nam',
                              message=message)
    for solution_group in solution_group_dict.values():
        for solution_info in solution_group:
            ims_file = mfims.ModflowIms(instance,
                                        filename=solution_info[1],
                                        pname=solution_info[2])
            if verbosity_level.value >= VerbosityLevel.normal.value:
                print('  loading ims package {}..'
                      '.'.format(ims_file._get_pname()))
            ims_file.load(strict)

    instance.simulation_data.mfpath.set_last_accessed_path()
    return instance
Ejemplo n.º 2
0
def test_create_and_run_model():

    # names
    sim_name = "testsim"
    model_name = "testmodel"
    exe_name = "mf6"

    # set up simulation
    tdis_name = "{}.tdis".format(sim_name)
    sim = MFSimulation(
        sim_name=sim_name, version="mf6", exe_name=exe_name, sim_ws=out_dir
    )
    tdis_rc = [(6.0, 2, 1.0), (6.0, 3, 1.0)]
    tdis = mftdis.ModflowTdis(
        sim, time_units="DAYS", nper=2, perioddata=tdis_rc
    )

    # create model instance
    model = mfgwf.ModflowGwf(
        sim, modelname=model_name, model_nam_file="{}.nam".format(model_name)
    )

    # create solution and add the model
    ims_package = mfims.ModflowIms(
        sim,
        print_option="ALL",
        complexity="SIMPLE",
        outer_hclose=0.00001,
        outer_maximum=50,
        under_relaxation="NONE",
        inner_maximum=30,
        inner_hclose=0.00001,
        linear_acceleration="CG",
        preconditioner_levels=7,
        preconditioner_drop_tolerance=0.01,
        number_orthogonalizations=2,
    )
    sim.register_ims_package(ims_package, [model_name])

    # add packages to model
    dis_package = mfgwfdis.ModflowGwfdis(
        model,
        length_units="FEET",
        nlay=1,
        nrow=1,
        ncol=10,
        delr=500.0,
        delc=500.0,
        top=100.0,
        botm=50.0,
        filename="{}.dis".format(model_name),
    )
    ic_package = mfgwfic.ModflowGwfic(
        model,
        strt=[
            100.0,
            100.0,
            100.0,
            100.0,
            100.0,
            100.0,
            100.0,
            100.0,
            100.0,
            100.0,
        ],
        filename="{}.ic".format(model_name),
    )
    npf_package = mfgwfnpf.ModflowGwfnpf(
        model, save_flows=True, icelltype=1, k=100.0
    )

    sto_package = mfgwfsto.ModflowGwfsto(
        model, save_flows=True, iconvert=1, ss=0.000001, sy=0.15
    )

    wel_package = mfgwfwel.ModflowGwfwel(
        model,
        print_input=True,
        print_flows=True,
        save_flows=True,
        maxbound=2,
        stress_period_data=[((0, 0, 4), -2000.0), ((0, 0, 7), -2.0)],
    )
    wel_package.stress_period_data.add_transient_key(1)
    wel_package.stress_period_data.set_data([((0, 0, 4), -200.0)], 1)

    drn_package = mfgwfdrn.ModflowGwfdrn(
        model,
        print_input=True,
        print_flows=True,
        save_flows=True,
        maxbound=1,
        stress_period_data=[((0, 0, 0), 80, 60.0)],
    )

    riv_package = mfgwfriv.ModflowGwfriv(
        model,
        print_input=True,
        print_flows=True,
        save_flows=True,
        maxbound=1,
        stress_period_data=[((0, 0, 9), 110, 90.0, 100.0)],
    )
    oc_package = mfgwfoc.ModflowGwfoc(
        model,
        budget_filerecord=["{}.cbc".format(model_name)],
        head_filerecord=["{}.hds".format(model_name)],
        saverecord=[("HEAD", "ALL"), ("BUDGET", "ALL")],
        printrecord=[("HEAD", "ALL"), ("BUDGET", "ALL")],
    )
    oc_package.saverecord.add_transient_key(1)
    oc_package.saverecord.set_data([("HEAD", "ALL"), ("BUDGET", "ALL")], 1)
    oc_package.printrecord.add_transient_key(1)
    oc_package.printrecord.set_data([("HEAD", "ALL"), ("BUDGET", "ALL")], 1)

    # write the simulation input files
    sim.write_simulation()

    # determine whether or not to run
    v = flopy.which(exe_name)
    run = True
    if v is None:
        run = False

    # run the simulation and look for output
    if run:
        sim.run_simulation()
        # head = sim.simulation_data.mfdata[(model_name, 'HDS', 'HEAD')]
        # print('HEAD: ', head)

    return
Ejemplo n.º 3
0
def test_create_and_run_model():

    # names
    sim_name = 'testsim'
    model_name = 'testmodel'
    exe_name = 'mf6'
    if platform.system() == 'Windows':
        exe_name += '.exe'

    # set up simulation
    tdis_name = '{}.tdis'.format(sim_name)
    sim = MFSimulation(sim_name=sim_name,
                       version='mf6', exe_name=exe_name,
                       sim_ws=out_dir,
                       sim_tdis_file=tdis_name)
    tdis_rc = [(6.0, 2, 1.0), (6.0, 3, 1.0)]
    tdis = mftdis.ModflowTdis(sim, time_units='DAYS', nper=2,
                              perioddata=tdis_rc)

    # create model instance
    model = MFModel(sim, model_type='gwf6',
                    modelname=model_name,
                    model_nam_file='{}.nam'.format(model_name))

    # create solution and add the model
    ims_package = mfims.ModflowIms(sim, print_option='ALL',
                                   complexity='SIMPLE', outer_hclose=0.00001,
                                   outer_maximum=50, under_relaxation='NONE',
                                   inner_maximum=30,
                                   inner_hclose=0.00001,
                                   linear_acceleration='CG',
                                   preconditioner_levels=7,
                                   preconditioner_drop_tolerance=0.01,
                                   number_orthogonalizations=2)
    sim.register_ims_package(ims_package, [model_name])

    # add packages to model
    dis_package = mfgwfdis.ModflowGwfdis(model, length_units='FEET', nlay=1,
                                         nrow=1, ncol=10, delr=500.0,
                                         delc=500.0,
                                         top=100.0, botm=50.0,
                                         fname='{}.dis'.format(model_name))
    ic_package = mfgwfic.ModflowGwfic(model,
                                      strt=[100.0, 100.0, 100.0, 100.0, 100.0,
                                            100.0, 100.0, 100.0, 100.0, 100.0],
                                      fname='{}.ic'.format(model_name))
    npf_package = mfgwfnpf.ModflowGwfnpf(model, save_flows=True, icelltype=1,
                                         k=100.0)

    sto_package = mfgwfsto.ModflowGwfsto(model, save_flows=True, iconvert=1,
                                         ss=0.000001, sy=0.15)

    wel_package = mfgwfwel.ModflowGwfwel(model, print_input=True,
                                         print_flows=True, save_flows=True,
                                         maxbound=2,
                                         stress_period_data=[((0, 0, 4),
                                                              -2000.0),
                                                             ((0, 0, 7),
                                                              -2.0)])
    wel_package.stress_period_data.add_transient_key(1)
    wel_package.stress_period_data.set_data([((0, 0, 4), -200.0)], 1)

    drn_package = mfgwfdrn.ModflowGwfdrn(model, print_input=True,
                                         print_flows=True, save_flows=True,
                                         maxbound=1, stress_period_data=[
                                         ((0, 0, 0), 80, 60.0)])

    riv_package = mfgwfriv.ModflowGwfriv(model, print_input=True,
                                         print_flows=True, save_flows=True,
                                         maxbound=1, stress_period_data=[
                                         ((0, 0, 9), 110, 90.0, 100.0)])
    oc_package = mfgwfoc.ModflowGwfoc(model, budget_filerecord=[
        '{}.cbc'.format(model_name)],
                                      head_filerecord=[
                                          '{}.hds'.format(model_name)],
                                      saverecord=[('HEAD', 'ALL'),
                                                  ('BUDGET', 'ALL')],
                                      printrecord=[('HEAD', 'ALL'),
                                                   ('BUDGET', 'ALL')])
    oc_package.saverecord.add_transient_key(1)
    oc_package.saverecord.set_data([('HEAD', 'ALL'), ('BUDGET', 'ALL')], 1)
    oc_package.printrecord.add_transient_key(1)
    oc_package.printrecord.set_data([('HEAD', 'ALL'), ('BUDGET', 'ALL')], 1)

    # write the simulation input files
    sim.write_simulation()

    # determine whether or not to run
    v = flopy.which(exe_name)
    run = True
    if v is None:
        run = False

    # run the simulation and look for output
    if run:
        sim.run_simulation()
        #head = sim.simulation_data.mfdata[(model_name, 'HDS', 'HEAD')]
        #print('HEAD: ', head)


    return