MODFLOW API Quickstart

This notebook presents a quickstart guide to working with the modflowapi package through the extension modules. This quickstart guide serves as a roadmap for user development of custom callback functions. For a detailed explanation of the modflowapi.extensions objects that are accessible through a callback function, see the notebook MODFLOW-API extensions objects.ipynb.

[1]:
from pathlib import Path

import modflowapi
from modflowapi import Callbacks

Define paths to the modflow6 api shared library

[2]:
sim_ws = Path("../data/dis_model")

dll = Path("./libmf6")

First we create a callback function for adjusting model data.

The callback function allows users to wrap function that updates the modflow model at different steps. The modflowapi.Callbacks object allows users to find the particular solution step that they are currently in. modflowapi.Callbacks includes:

  • Callbacks.initialize: the initialize callback sends loaded simulation data back to the user to make adjustments before the model begins solving. This callback only occurs once at the beginning of the MODFLOW6 simulation.

  • Callbacks.stress_period_start: the stress_period_start callback sends simulation data for each solution group to the user to make adjustments to stress packages at the beginning of each stress period.

  • Callbacks.stress_period_end: the stress_period_end callback sends simulation data for each solution group to the user at the end of each stress period. This can be useful for writing custom output and coupling models.

  • Callbacks.timestep_start: the timestep_start callback sends simulation data for each solution group to the user to make adjustments to stress packages at the beginning of each timestep.

  • Callbacks.timestep_end: the timestep_end callback sends simulation data for each solution group to the user at the end of each timestep. This can be useful for writing custom output and coupling models.

  • Callbacks.iteration_start: the iteration_start callback sends simulation data for each solution group to the user to make adjustments to stress packages at the beginning of each outer solution iteration.

  • Callbacks.iteration_end: the iteration_end callback sends simulation data for each solution group to the user to make adjustments to stress packages and check values of stress packages at the end of each outer solution iteration.

The user can use any of these callbacks within their callback function.

[3]:
def callback_function(sim, callback_step):
    """
    A demonstration function that dynamically adjusts recharge
    and pumping in a modflow-6 model through the MODFLOW-API

    Parameters
    ----------
    sim : modflowapi.ApiSimulation
        A simulation object for the solution group that is
        currently being solved
    step : enumeration
        modflowapi.Callbacks enumeration object that indicates
        the part of the solution modflow is currently in.
    """
    ml = sim.test_model
    if callback_step == Callbacks.initialize:
        print(sim.models)

    if callback_step == Callbacks.stress_period_start:
        # adjust recharge for stress periods 7 through 12
        if sim.kper <= 6:
            rcha = ml.rcha_0
            spd = rcha.stress_period_data
            print(f"updating recharge: stress_period={ml.kper}")
            spd["recharge"] += 0.40 * sim.kper

    if callback_step == Callbacks.timestep_start:
        print(f"updating wel flux: stress_period={ml.kper}, timestep={ml.kstp}")
        ml.wel.stress_period_data["q"] -= ml.kstp * 1.5

    if callback_step == Callbacks.iteration_start:
        # we can implement complex solutions to boundary conditions here!
        pass

The callback function is then passed to modflowapi.run_simulation

[4]:
modflowapi.run_simulation(dll, sim_ws, callback_function, verbose=False)
[TEST_MODEL, 1 Layer, 10 Row, 10 Column model
Packages accessible include:
  ArrayPackage objects:
    dis: <class 'modflowapi.extensions.pakbase.ApiDisPackage'>
    ic: <class 'modflowapi.extensions.pakbase.ApiIcPackage'>
    sto: <class 'modflowapi.extensions.pakbase.ApiStoPackage'>
    npf: <class 'modflowapi.extensions.pakbase.ApiNpfPackage'>
  ListPackage objects:
    evt_0: <class 'modflowapi.extensions.pakbase.ApiEvtPackage'>
    rch_0: <class 'modflowapi.extensions.pakbase.ApiRchPackage'>
    drn_0: <class 'modflowapi.extensions.pakbase.ApiDrnPackage'>
    rcha_0: <class 'modflowapi.extensions.pakbase.ApiRchPackage'>
    wel_0: <class 'modflowapi.extensions.pakbase.ApiWelPackage'>
    chd_0: <class 'modflowapi.extensions.pakbase.ApiChdPackage'>
  AdvancedPackage objects:
    mvr: <class 'modflowapi.extensions.pakbase.ApiMvrPackage'>
    csub: <class 'modflowapi.extensions.pakbase.ApiCsubPackage'>
    vsc: <class 'modflowapi.extensions.pakbase.ApiVscPackage'>
    buy: <class 'modflowapi.extensions.pakbase.ApiBuyPackage'>
    gnc: <class 'modflowapi.extensions.pakbase.ApiGncPackage'>
    hfb: <class 'modflowapi.extensions.pakbase.ApiHfbPackage'>
]
updating recharge: stress_period=0
updating wel flux: stress_period=0, timestep=0
updating wel flux: stress_period=0, timestep=1
updating wel flux: stress_period=0, timestep=2
updating wel flux: stress_period=0, timestep=3
updating wel flux: stress_period=0, timestep=4
updating wel flux: stress_period=0, timestep=5
updating wel flux: stress_period=0, timestep=6
updating wel flux: stress_period=0, timestep=7
updating wel flux: stress_period=0, timestep=8
updating wel flux: stress_period=0, timestep=9
updating wel flux: stress_period=0, timestep=10
updating wel flux: stress_period=0, timestep=11
updating wel flux: stress_period=0, timestep=12
updating wel flux: stress_period=0, timestep=13
updating wel flux: stress_period=0, timestep=14
updating wel flux: stress_period=0, timestep=15
updating wel flux: stress_period=0, timestep=16
updating wel flux: stress_period=0, timestep=17
updating wel flux: stress_period=0, timestep=18
updating wel flux: stress_period=0, timestep=19
updating wel flux: stress_period=0, timestep=20
updating wel flux: stress_period=0, timestep=21
updating wel flux: stress_period=0, timestep=22
updating wel flux: stress_period=0, timestep=23
updating wel flux: stress_period=0, timestep=24
updating wel flux: stress_period=0, timestep=25
updating wel flux: stress_period=0, timestep=26
updating wel flux: stress_period=0, timestep=27
updating wel flux: stress_period=0, timestep=28
updating wel flux: stress_period=0, timestep=29
updating wel flux: stress_period=0, timestep=30
updating recharge: stress_period=1
updating wel flux: stress_period=1, timestep=0
updating wel flux: stress_period=1, timestep=1
updating wel flux: stress_period=1, timestep=2
updating wel flux: stress_period=1, timestep=3
updating wel flux: stress_period=1, timestep=4
updating wel flux: stress_period=1, timestep=5
updating wel flux: stress_period=1, timestep=6
updating wel flux: stress_period=1, timestep=7
updating wel flux: stress_period=1, timestep=8
updating wel flux: stress_period=1, timestep=9
updating wel flux: stress_period=1, timestep=10
updating wel flux: stress_period=1, timestep=11
updating wel flux: stress_period=1, timestep=12
updating wel flux: stress_period=1, timestep=13
updating wel flux: stress_period=1, timestep=14
updating wel flux: stress_period=1, timestep=15
updating wel flux: stress_period=1, timestep=16
updating wel flux: stress_period=1, timestep=17
updating wel flux: stress_period=1, timestep=18
updating wel flux: stress_period=1, timestep=19
updating wel flux: stress_period=1, timestep=20
updating wel flux: stress_period=1, timestep=21
updating wel flux: stress_period=1, timestep=22
updating wel flux: stress_period=1, timestep=23
updating wel flux: stress_period=1, timestep=24
updating wel flux: stress_period=1, timestep=25
updating wel flux: stress_period=1, timestep=26
updating wel flux: stress_period=1, timestep=27
updating recharge: stress_period=2
updating wel flux: stress_period=2, timestep=0
updating wel flux: stress_period=2, timestep=1
updating wel flux: stress_period=2, timestep=2
updating wel flux: stress_period=2, timestep=3
updating wel flux: stress_period=2, timestep=4
updating wel flux: stress_period=2, timestep=5
updating wel flux: stress_period=2, timestep=6
updating wel flux: stress_period=2, timestep=7
updating wel flux: stress_period=2, timestep=8
updating wel flux: stress_period=2, timestep=9
updating wel flux: stress_period=2, timestep=10
updating wel flux: stress_period=2, timestep=11
updating wel flux: stress_period=2, timestep=12
updating wel flux: stress_period=2, timestep=13
updating wel flux: stress_period=2, timestep=14
updating wel flux: stress_period=2, timestep=15
updating wel flux: stress_period=2, timestep=16
updating wel flux: stress_period=2, timestep=17
updating wel flux: stress_period=2, timestep=18
updating wel flux: stress_period=2, timestep=19
updating wel flux: stress_period=2, timestep=20
updating wel flux: stress_period=2, timestep=21
updating wel flux: stress_period=2, timestep=22
updating wel flux: stress_period=2, timestep=23
updating wel flux: stress_period=2, timestep=24
updating wel flux: stress_period=2, timestep=25
updating wel flux: stress_period=2, timestep=26
updating wel flux: stress_period=2, timestep=27
updating wel flux: stress_period=2, timestep=28
updating wel flux: stress_period=2, timestep=29
updating wel flux: stress_period=2, timestep=30
updating recharge: stress_period=3
updating wel flux: stress_period=3, timestep=0
updating wel flux: stress_period=3, timestep=1
updating wel flux: stress_period=3, timestep=2
updating wel flux: stress_period=3, timestep=3
updating wel flux: stress_period=3, timestep=4
updating wel flux: stress_period=3, timestep=5
updating wel flux: stress_period=3, timestep=6
updating wel flux: stress_period=3, timestep=7
updating wel flux: stress_period=3, timestep=8
updating wel flux: stress_period=3, timestep=9
updating wel flux: stress_period=3, timestep=10
updating wel flux: stress_period=3, timestep=11
updating wel flux: stress_period=3, timestep=12
updating wel flux: stress_period=3, timestep=13
updating wel flux: stress_period=3, timestep=14
updating wel flux: stress_period=3, timestep=15
updating wel flux: stress_period=3, timestep=16
updating wel flux: stress_period=3, timestep=17
updating wel flux: stress_period=3, timestep=18
updating wel flux: stress_period=3, timestep=19
updating wel flux: stress_period=3, timestep=20
updating wel flux: stress_period=3, timestep=21
updating wel flux: stress_period=3, timestep=22
updating wel flux: stress_period=3, timestep=23
updating wel flux: stress_period=3, timestep=24
updating wel flux: stress_period=3, timestep=25
updating wel flux: stress_period=3, timestep=26
updating wel flux: stress_period=3, timestep=27
updating wel flux: stress_period=3, timestep=28
updating wel flux: stress_period=3, timestep=29
updating recharge: stress_period=4
updating wel flux: stress_period=4, timestep=0
updating wel flux: stress_period=4, timestep=1
updating wel flux: stress_period=4, timestep=2
updating wel flux: stress_period=4, timestep=3
updating wel flux: stress_period=4, timestep=4
updating wel flux: stress_period=4, timestep=5
updating wel flux: stress_period=4, timestep=6
updating wel flux: stress_period=4, timestep=7
updating wel flux: stress_period=4, timestep=8
updating wel flux: stress_period=4, timestep=9
updating wel flux: stress_period=4, timestep=10
updating wel flux: stress_period=4, timestep=11
updating wel flux: stress_period=4, timestep=12
updating wel flux: stress_period=4, timestep=13
updating wel flux: stress_period=4, timestep=14
updating wel flux: stress_period=4, timestep=15
updating wel flux: stress_period=4, timestep=16
updating wel flux: stress_period=4, timestep=17
updating wel flux: stress_period=4, timestep=18
updating wel flux: stress_period=4, timestep=19
updating wel flux: stress_period=4, timestep=20
updating wel flux: stress_period=4, timestep=21
updating wel flux: stress_period=4, timestep=22
updating wel flux: stress_period=4, timestep=23
updating wel flux: stress_period=4, timestep=24
updating wel flux: stress_period=4, timestep=25
updating wel flux: stress_period=4, timestep=26
updating wel flux: stress_period=4, timestep=27
updating wel flux: stress_period=4, timestep=28
updating wel flux: stress_period=4, timestep=29
updating wel flux: stress_period=4, timestep=30
updating recharge: stress_period=5
updating wel flux: stress_period=5, timestep=0
updating wel flux: stress_period=5, timestep=1
updating wel flux: stress_period=5, timestep=2
updating wel flux: stress_period=5, timestep=3
updating wel flux: stress_period=5, timestep=4
updating wel flux: stress_period=5, timestep=5
updating wel flux: stress_period=5, timestep=6
updating wel flux: stress_period=5, timestep=7
updating wel flux: stress_period=5, timestep=8
updating wel flux: stress_period=5, timestep=9
updating wel flux: stress_period=5, timestep=10
updating wel flux: stress_period=5, timestep=11
updating wel flux: stress_period=5, timestep=12
updating wel flux: stress_period=5, timestep=13
updating wel flux: stress_period=5, timestep=14
updating wel flux: stress_period=5, timestep=15
updating wel flux: stress_period=5, timestep=16
updating wel flux: stress_period=5, timestep=17
updating wel flux: stress_period=5, timestep=18
updating wel flux: stress_period=5, timestep=19
updating wel flux: stress_period=5, timestep=20
updating wel flux: stress_period=5, timestep=21
updating wel flux: stress_period=5, timestep=22
updating wel flux: stress_period=5, timestep=23
updating wel flux: stress_period=5, timestep=24
updating wel flux: stress_period=5, timestep=25
updating wel flux: stress_period=5, timestep=26
updating wel flux: stress_period=5, timestep=27
updating wel flux: stress_period=5, timestep=28
updating wel flux: stress_period=5, timestep=29
updating recharge: stress_period=6
updating wel flux: stress_period=6, timestep=0
updating wel flux: stress_period=6, timestep=1
updating wel flux: stress_period=6, timestep=2
updating wel flux: stress_period=6, timestep=3
updating wel flux: stress_period=6, timestep=4
updating wel flux: stress_period=6, timestep=5
updating wel flux: stress_period=6, timestep=6
updating wel flux: stress_period=6, timestep=7
updating wel flux: stress_period=6, timestep=8
updating wel flux: stress_period=6, timestep=9
updating wel flux: stress_period=6, timestep=10
updating wel flux: stress_period=6, timestep=11
updating wel flux: stress_period=6, timestep=12
updating wel flux: stress_period=6, timestep=13
updating wel flux: stress_period=6, timestep=14
updating wel flux: stress_period=6, timestep=15
updating wel flux: stress_period=6, timestep=16
updating wel flux: stress_period=6, timestep=17
updating wel flux: stress_period=6, timestep=18
updating wel flux: stress_period=6, timestep=19
updating wel flux: stress_period=6, timestep=20
updating wel flux: stress_period=6, timestep=21
updating wel flux: stress_period=6, timestep=22
updating wel flux: stress_period=6, timestep=23
updating wel flux: stress_period=6, timestep=24
updating wel flux: stress_period=6, timestep=25
updating wel flux: stress_period=6, timestep=26
updating wel flux: stress_period=6, timestep=27
updating wel flux: stress_period=6, timestep=28
updating wel flux: stress_period=6, timestep=29
updating wel flux: stress_period=6, timestep=30
updating wel flux: stress_period=7, timestep=0
updating wel flux: stress_period=7, timestep=1
updating wel flux: stress_period=7, timestep=2
updating wel flux: stress_period=7, timestep=3
updating wel flux: stress_period=7, timestep=4
updating wel flux: stress_period=7, timestep=5
updating wel flux: stress_period=7, timestep=6
updating wel flux: stress_period=7, timestep=7
updating wel flux: stress_period=7, timestep=8
updating wel flux: stress_period=7, timestep=9
updating wel flux: stress_period=7, timestep=10
updating wel flux: stress_period=7, timestep=11
updating wel flux: stress_period=7, timestep=12
updating wel flux: stress_period=7, timestep=13
updating wel flux: stress_period=7, timestep=14
updating wel flux: stress_period=7, timestep=15
updating wel flux: stress_period=7, timestep=16
updating wel flux: stress_period=7, timestep=17
updating wel flux: stress_period=7, timestep=18
updating wel flux: stress_period=7, timestep=19
updating wel flux: stress_period=7, timestep=20
updating wel flux: stress_period=7, timestep=21
updating wel flux: stress_period=7, timestep=22
updating wel flux: stress_period=7, timestep=23
updating wel flux: stress_period=7, timestep=24
updating wel flux: stress_period=7, timestep=25
updating wel flux: stress_period=7, timestep=26
updating wel flux: stress_period=7, timestep=27
updating wel flux: stress_period=7, timestep=28
updating wel flux: stress_period=7, timestep=29
updating wel flux: stress_period=7, timestep=30
updating wel flux: stress_period=8, timestep=0
updating wel flux: stress_period=8, timestep=1
updating wel flux: stress_period=8, timestep=2
updating wel flux: stress_period=8, timestep=3
updating wel flux: stress_period=8, timestep=4
updating wel flux: stress_period=8, timestep=5
updating wel flux: stress_period=8, timestep=6
updating wel flux: stress_period=8, timestep=7
updating wel flux: stress_period=8, timestep=8
updating wel flux: stress_period=8, timestep=9
updating wel flux: stress_period=8, timestep=10
updating wel flux: stress_period=8, timestep=11
updating wel flux: stress_period=8, timestep=12
updating wel flux: stress_period=8, timestep=13
updating wel flux: stress_period=8, timestep=14
updating wel flux: stress_period=8, timestep=15
updating wel flux: stress_period=8, timestep=16
updating wel flux: stress_period=8, timestep=17
updating wel flux: stress_period=8, timestep=18
updating wel flux: stress_period=8, timestep=19
updating wel flux: stress_period=8, timestep=20
updating wel flux: stress_period=8, timestep=21
updating wel flux: stress_period=8, timestep=22
updating wel flux: stress_period=8, timestep=23
updating wel flux: stress_period=8, timestep=24
updating wel flux: stress_period=8, timestep=25
updating wel flux: stress_period=8, timestep=26
updating wel flux: stress_period=8, timestep=27
updating wel flux: stress_period=8, timestep=28
updating wel flux: stress_period=8, timestep=29
updating wel flux: stress_period=9, timestep=0
updating wel flux: stress_period=9, timestep=1
updating wel flux: stress_period=9, timestep=2
updating wel flux: stress_period=9, timestep=3
updating wel flux: stress_period=9, timestep=4
updating wel flux: stress_period=9, timestep=5
updating wel flux: stress_period=9, timestep=6
updating wel flux: stress_period=9, timestep=7
updating wel flux: stress_period=9, timestep=8
updating wel flux: stress_period=9, timestep=9
updating wel flux: stress_period=9, timestep=10
updating wel flux: stress_period=9, timestep=11
updating wel flux: stress_period=9, timestep=12
updating wel flux: stress_period=9, timestep=13
updating wel flux: stress_period=9, timestep=14
updating wel flux: stress_period=9, timestep=15
updating wel flux: stress_period=9, timestep=16
updating wel flux: stress_period=9, timestep=17
updating wel flux: stress_period=9, timestep=18
updating wel flux: stress_period=9, timestep=19
updating wel flux: stress_period=9, timestep=20
updating wel flux: stress_period=9, timestep=21
updating wel flux: stress_period=9, timestep=22
updating wel flux: stress_period=9, timestep=23
updating wel flux: stress_period=9, timestep=24
updating wel flux: stress_period=9, timestep=25
updating wel flux: stress_period=9, timestep=26
updating wel flux: stress_period=9, timestep=27
updating wel flux: stress_period=9, timestep=28
updating wel flux: stress_period=9, timestep=29
updating wel flux: stress_period=9, timestep=30
updating wel flux: stress_period=10, timestep=0
updating wel flux: stress_period=10, timestep=1
updating wel flux: stress_period=10, timestep=2
updating wel flux: stress_period=10, timestep=3
updating wel flux: stress_period=10, timestep=4
updating wel flux: stress_period=10, timestep=5
updating wel flux: stress_period=10, timestep=6
updating wel flux: stress_period=10, timestep=7
updating wel flux: stress_period=10, timestep=8
updating wel flux: stress_period=10, timestep=9
updating wel flux: stress_period=10, timestep=10
updating wel flux: stress_period=10, timestep=11
updating wel flux: stress_period=10, timestep=12
updating wel flux: stress_period=10, timestep=13
updating wel flux: stress_period=10, timestep=14
updating wel flux: stress_period=10, timestep=15
updating wel flux: stress_period=10, timestep=16
updating wel flux: stress_period=10, timestep=17
updating wel flux: stress_period=10, timestep=18
updating wel flux: stress_period=10, timestep=19
updating wel flux: stress_period=10, timestep=20
updating wel flux: stress_period=10, timestep=21
updating wel flux: stress_period=10, timestep=22
updating wel flux: stress_period=10, timestep=23
updating wel flux: stress_period=10, timestep=24
updating wel flux: stress_period=10, timestep=25
updating wel flux: stress_period=10, timestep=26
updating wel flux: stress_period=10, timestep=27
updating wel flux: stress_period=10, timestep=28
updating wel flux: stress_period=10, timestep=29
updating wel flux: stress_period=11, timestep=0
updating wel flux: stress_period=11, timestep=1
updating wel flux: stress_period=11, timestep=2
updating wel flux: stress_period=11, timestep=3
updating wel flux: stress_period=11, timestep=4
updating wel flux: stress_period=11, timestep=5
updating wel flux: stress_period=11, timestep=6
updating wel flux: stress_period=11, timestep=7
updating wel flux: stress_period=11, timestep=8
updating wel flux: stress_period=11, timestep=9
updating wel flux: stress_period=11, timestep=10
updating wel flux: stress_period=11, timestep=11
updating wel flux: stress_period=11, timestep=12
updating wel flux: stress_period=11, timestep=13
updating wel flux: stress_period=11, timestep=14
updating wel flux: stress_period=11, timestep=15
updating wel flux: stress_period=11, timestep=16
updating wel flux: stress_period=11, timestep=17
updating wel flux: stress_period=11, timestep=18
updating wel flux: stress_period=11, timestep=19
updating wel flux: stress_period=11, timestep=20
updating wel flux: stress_period=11, timestep=21
updating wel flux: stress_period=11, timestep=22
updating wel flux: stress_period=11, timestep=23
updating wel flux: stress_period=11, timestep=24
updating wel flux: stress_period=11, timestep=25
updating wel flux: stress_period=11, timestep=26
updating wel flux: stress_period=11, timestep=27
updating wel flux: stress_period=11, timestep=28
updating wel flux: stress_period=11, timestep=29
updating wel flux: stress_period=11, timestep=30

Normal termination of simulation.