<jupyter_start><jupyter_text>Coupling modelsIn this tutorial we will learn how to:* Couple two models* Use the PyMT grid mapper<jupyter_code>import matplotlib.pyplot as plt
import numpy as np
from tqdm import tqdm<jupyter_output><empty_output><jupyter_text>Set up and initialize two modelsFor this example we will couple and landscape evolution model, *Child*, with a seascape evolution model, *Sedflux*. Although these are two very different models, notice that within *PyMT* they look and operate in a familiar way.<jupyter_code>from pymt import MODELS<jupyter_output><empty_output><jupyter_text>First, set up and initialize *Child*...<jupyter_code>child = MODELS.Child()

child_in, child_dir = child.setup(
    "_child",
    grid_node_spacing=500.0,
    grid_x_size=40000.0,
    grid_y_size=20000.0,
    run_duration=1e6,
)

child.initialize(child_in, dir=child_dir)<jupyter_output><empty_output><jupyter_text>...then set up and initialize *Sedflux*<jupyter_code>sedflux = MODELS.Sedflux3D()

sedflux_in, sedflux_dir = sedflux.setup(
    "_sedflux",
    river_bed_load_flux=0.0,
    river_suspended_load_concentration_0=0.1,
    river_suspended_load_concentration_1=0.1,
    run_duration=1e6 * 365.0,
)

sedflux.initialize(sedflux_in, dir=sedflux_dir)<jupyter_output><empty_output><jupyter_text>Before continuing, we need to sync up values of surface elevation for each of the models. For *Child*, the variable name for this is *land_surface__elevation*.In a similar way as we did in the previous tutorial, we'll set a shoreline in *Child*. In this case, though, we'll put the shoreline at *x=20km*.<jupyter_code>gid = child.var["land_surface__elevation"].grid

x, y = child.grid_x(gid), child.grid_y(gid)
z = child.get_value("land_surface__elevation")
x_shore = 20000.0
z[np.where(x > x_shore)] += 100.0
z[np.where(x <= x_shore)] -= 100.0

child.set_value("land_surface__elevation", z)<jupyter_output><empty_output><jupyter_text>Map values between gridsAs we've seen, the *Child* grid is unstructured. The *Sedflux* grid, however, is uniform rectilinear. We can see this with *quick_plot*. For *Sedflux*, the relevent parameter is *bedrock_surface__elevation*.<jupyter_code>sedflux.quick_plot(
    "bedrock_surface__elevation", edgecolors="k", vmin=-200, vmax=200, cmap="BrBG_r"
)<jupyter_output><empty_output><jupyter_text>We've set the values for the initial elevation for *Child*, we need to map these to the values of *Sedflux*. We set these values in *Sedflux* using the usual **set_value** method, only this time we use the `mapfrom` keyword.The below code, *bedrock_surface__elevation* values of *Sedflux* by mapping values of *land_surface__elevation* of child.<jupyter_code>sedflux.set_value(
    "bedrock_surface__elevation", mapfrom=("land_surface__elevation", child)
)<jupyter_output><empty_output><jupyter_text>To double-check how the mapping turned out, plot the elevations for each model.<jupyter_code>child.quick_plot(
    "land_surface__elevation", edgecolors="k", vmin=-200, vmax=200, cmap="BrBG_r"
)
sedflux.quick_plot("bedrock_surface__elevation", vmin=-200, vmax=200, cmap="BrBG_r")<jupyter_output><empty_output><jupyter_text>Although *Child* will provide sediment to *Sedflux*, *Sedflux* still requires values for the river characteristics as it enters the ocean.<jupyter_code>sedflux.set_value("channel_exit_water_flow__speed", 1.2)
sedflux.set_value("channel_exit_x-section__mean_of_width", 400.)
sedflux.set_value("channel_exit_x-section__mean_of_depth", 4.)<jupyter_output><empty_output><jupyter_text>Run a coupled simulation!We now run the time loop. For each time step, we:* update *Child** set *Child*'s sediment flux into *Sedflux** update *Sedflux** set *Sedflux*'s seafloor elevations into *Child*<jupyter_code>now = child.time
times = np.arange(now, now + 1000, 1.0)

sedflux.update()
child.update()

for t in tqdm(times):
    child.update_until(t, units="years")

    sedflux.set_value("channel_water_sediment~bedload__mass_flow_rate", mapfrom=child)
    sedflux.update_until(t, units="years")

    z = child.get_value("land_surface__elevation")
    child.set_value(
        "land_surface__elevation",
        mapfrom=("land-or-seabed_sediment_surface__elevation", sedflux),
        nomap=np.where(z > 0.0),
    )
child.quick_plot(
    "land_surface__elevation", edgecolors="k", vmin=-200, vmax=200, cmap="BrBG_r"
)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1106548026465891, 'fraction_numerical': 0.019849418206707735, 'mean_word_length': 6.494017094017094, 'pattern_counts': {'":': 0, '<': 42, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '140412', 'license': 'CC-BY-4.0', 'n_tokens_mistral': 1645, 'n_tokens_neox': 1516, 'n_words': 369, 'path': 'lessons/pymt/05_coupling.ipynb', 'repo_name': 'csdms/ivy', 'size': '4383'}