% Created 2023-10-30 Mon 14:11
% Intended LaTeX compiler: pdflatex
\documentclass[11pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{lmodern}
\usepackage[T1]{fontenc}
\usepackage{xcolor}
\usepackage{graphicx}
\usepackage{longtable}
\usepackage{float}
\usepackage{wrapfig}
\usepackage{rotating}
\usepackage[normalem]{ulem}
\usepackage{amsmath}
\usepackage{textcomp}
\usepackage{grffile}
\usepackage{marvosym}
\usepackage{wasysym}
\usepackage{amssymb}
\usepackage{amsmath}
\usepackage{svg}
\usepackage[theorems, skins]{tcolorbox}
\usepackage[version=3]{mhchem}
\usepackage{url}
\usepackage{minted}
\usepackage[strings]{underscore}
\usepackage{hyperref}
\usepackage{attachfile}
\usepackage{breakurl}
\usepackage{newuli}
\usepackage{uli-german-paragraphs}
\author{Ulrich G Wortmann}
\date{\today}
\title{ESBMTK Tutorial}
\begin{document}

\maketitle

\tableofcontents
\section{A simple example}
\label{sec:org2a1169a}

A simple model of the marine P-cycle would consider the delivery of P from weathering, the burial of P in the sediments, the thermohaline transport of dissolved PO\textsubscript{4} as well as the export of P in form of sinking organic matter (POP). The concentration in the respective surface an deep water boxes is then sum of the respective fluxes (see Fig.~\ref{pcycle}). The model parameters are taken from Glover 2011, Modeling Methods in the Marine Sciences.
\begin{figure}[htbp]
\centering
\includegraphics[width=0.5\textwidth]{/home/uliw/user/python-scripts/esbmtk/Documentation/mpc.png}
\caption{\label{pcycle}A two-box model of the marine P-cycle. F\textsubscript{w} = weathering F\textsubscript{u} = upwelling, F\textsubscript{d} = downwelling, F\textsubscript{POP} = particulate organic phosphor, F\textsubscript{b} = burial.}
\end{figure}

If we define equations that control the export of particulate P (F\textsubscript{POP}) as a fraction of the upwelling P (F\textsubscript{u}), and the burial of P (F\textsubscript{b}) as fraction of (F\textsubscript{POP}), we express this model as coupled ordinary differential equations (ODE, or initial value problem):

\begin{equation}\label{}
\frac{d[PO_{4}]_{S}}{dt} = \frac{F_w + F_u - F_d - F_{POP}}{V_S}
\end{equation}

and for the deep ocean, 

\begin{equation}\label{}
\frac{d[PO_{4}]_{D}}{dt}= \frac{F_{POP} + F_d - F_u - F_b}{V_D}
\end{equation}


which is easily encoded as a python function
\begin{minted}[fontsize=\small,frame=lines,linenos]{python}
def dCdt(t, C_0, V, F_w, thx):
    """Calculate the change in concentration as
    a function of time. After Glover 2011, Modeling
    Methods for Marine Science.

    :param C: list of initial concentrations mol/m*3
    :param time: array of time points
    :params V: lits of surface and deep ocean volume [m^3]
    :param F_w: River (weathering) flux of PO4 mol/s
    :param thx: thermohaline circulation in m*3/s
    :returns dCdt: list of concentration changes mol/s
    """

    C_S = C_0[0]  # surface
    C_D = C_0[1]  # deep
    F_d = C_S * thx  # downwelling
    F_u = C_D * thx  # upwelling
    tau = 100 # residence time of P in surface waters [yrs]
    F_POP = C_S * V[0] / tau  # export production
    F_b = F_POP / 100  # burial

    dCdt[0] = (F_w + F_u - F_d - F_POP) / V[0]
    dCdt[1] = (F_d + F_POP - F_u - F_b) / V[1]

    return dCdt
\end{minted}
\subsection{Implementing the P-cycle with ESBMTK}
\label{sec:orgeba45a5}
While ESBMTK provides abstractions to efficiently define complex models, the following section will use the basic ESBMTK classes to define the above model. While quite verbose, it demonstrates the design philosophy behind ESBMTK. More complex approaches are described further down. 

Currently ESBMTK is only available via pip install as
\begin{minted}[fontsize=\small,frame=lines,linenos]{python}
import sys
!{sys.executable} -m pip install esbmtk
\end{minted}
\subsubsection{Defining the model geometry and initial conditions}
\label{sec:org8672279}
In a first step one needs to define a model object that describes fundamental model parameters. The following code first loads the various esbmtk classes that will help with model construction, and then defines the model object. Note that units are automatically translated into model units. While convenient, there are some import caveats: 
Internally, the model uses 'year' as the time unit, mol as the mass unit, and liter as the volume unit. You can change this by setting these values to e.g., 'mol' and 'kg', however, some functions assume that their input values are in 'mol/l' rather than mol/m**3 or 'kg/s'. Ideally this would be caught by ESBMTK, but at present, this not guaranteed. So your mileage may vary, if you fiddle with these settings.  Note: Using mol/kg e.g., for seawater, will be discussed below.
\begin{minted}[fontsize=\small,frame=lines,linenos]{python}
# import classes from the esbmtk library
from esbmtk import (
    Model,  # the model class
    Reservoir,  # the reservoir class
    Connection,  # the connection class
    Source,  # the source class
    Sink,  # sink class
    Q_,  # Quantity operator
)

# define the basic model parameters
M = Model(
    name="M",  # model name
    stop="3 Myr",  # end time of model
    timestep="1 kyr",  # upper limit of time step
    element=["Phosphor"],  # list of element definitions
)
\end{minted}

Next, we need to declare some boundary conditions. Most ESBMTK classes will be able to accept input in the form of strings that also contain units (e.g., \texttt{"30 Gmol/a"} ). Internally these strings are parsed and converted into the model base units. This works most of the time, but not always. In the below example, we the residence time \(\tau\).  This variable is then used as input to calculate the scale for the primary production as \texttt{M.sb.volume / tau} which must fail since \texttt{M.sb.volume} is a numeric value and \texttt{tau} is a string. 
\begin{minted}[fontsize=\small,frame=lines,linenos]{python}
# try the following
tau = "100 years"
tau * 12
\end{minted}

To avoid this we have to manually parse the string into a quantity. This is done with the quantity operator \texttt{Q\_} Note that \texttt{Q\_} is not part of ESBMTk but imported from the \texttt{pint} library. 
\begin{minted}[fontsize=\small,frame=lines,linenos]{python}
# now try this
from esbmtk import Q_
tau = Q_("100 years")
tau * 12
\end{minted}

Most ESBMTK classes accept quantities, strings that represent quantities as well as numerical values. Weathering and burial fluxes are often defined in \texttt{mol/year}, whereas ocean models use \texttt{kg/year}. ESBMTK provides a method (\texttt{set\_flux()} )  that will automatically convert the input into the correct units. In this example it is not necessary since the flux and the model both use \texttt{mol} . It is however good practice to to relay on the automatic conversion. Note that it makes a difference for the mole to kilogram conversion whether ones uses \texttt{M.P} or \texttt{M.PO4} as the reference species!
\begin{minted}[fontsize=\small,frame=lines,linenos]{python}
# boundary conditions
F_w =  M.set_flux("45 Gmol", "year", M.P) # P @280 ppm (Filipelli 2002)
tau = Q_("100 year")  # PO4 residence time in surface box
F_b = 0.01  # About 1% of the exported P is buried in the deep ocean
thc = "20*Sv"  # Thermohaline circulation in Sverdrup
\end{minted}

To set up the model geometry, we first  use the \texttt{Source} and  \texttt{Reservoir} classes  to create a source for the weathering flux, a sink for the burial flux, and instances of the surface and deep oceans boxes. Since we loaded the element definitions for phosphor in the model definition above, we can directly refer to the "PO4" species in the reservoir definition. 
\begin{minted}[fontsize=\small,frame=lines,linenos]{python}
# Source definitions
Source(
    name="weathering",
    species=M.PO4,
    register=M,  # i.e., the instance will be available as M.weathering
)
Sink(
    name="burial",
    species=M.PO4,
    register=M,  #
)

# reservoir definitions
Reservoir(
    name="sb",  # box name
    species=M.PO4,  # species in box
    register=M,  # this box will be available as M.sb
    volume="3E16 m**3",  # surface box volume
    concentration="0 umol/l",  # initial concentration
)
Reservoir(
    name="db",  # box name
    species=M.PO4,  # species in box
    register=M,  # this box will be available M.db
    volume="100E16 m**3",  # deeb box volume
    concentration="0 umol/l",  # initial concentration
)
\end{minted}
\subsubsection{Model processes}
\label{sec:org9abb575}
For many models, processes can mapped as the transfer of mass from one box to the next. Within the ESBMTK framework this is accomplished through the \texttt{Connection} class. To connect the a weathering flux from the source object (M.w) to the surface ocean (M.sb) we declare a connection instance describing this relationship as follows:
\begin{minted}[fontsize=\small,frame=lines,linenos]{python}
Connection(
    source=M.weathering,  # source of flux
    sink=M.sb,  # target of flux
    rate=F_w,  # rate of flux
    id="river",  # connection id
)
\end{minted}
Unless the=register= keyword is given, connections will be automatically registered withe the parent of the source, i.e., the model \texttt{M}. Unless explicitly given through the \texttt{name} keyword, connection names will be automatically constructed from the names of the source and sink instances. However, it is a good habit to provide the \texttt{id} keyword to keep connections separate in cases where two reservoir instances share more than one connection. The list of all connection instances can be obtained from the model object (see below).

To map the process of thermohaline circulation, we connect the surface and deep ocean boxes  using a connection type that scales the mass transfer as a function of the concentration in a given reservoir (\texttt{ctype ="scale\_with\_concentration"} ) . The concentration data is taken from the reference reservoir which defaults to the source reservoir. As such, in most cases the \texttt{ref\_reservoirs} keyword can be omitted. The \texttt{scale} keyword can be a string, or a numerical value. If its provided as a string ESBMTK will map the value into model units. Note that the connection class does not require the \texttt{name} keyword. Rather the name is derived from the source and sink reservoir instances. Since reservoir instances can have more than one connection (i.e., surface to deep via downwelling, and surface to deep via primary production), it is required to set the \texttt{id} keyword.
\begin{minted}[fontsize=\small,frame=lines,linenos]{python}
Connection(  # thermohaline downwelling
    source=M.sb,  # source of flux
    sink=M.db,  # target of flux
    ctype="scale_with_concentration",
    scale=thc,
    id="downwelling_PO4",
    # ref_reservoirs=M.sb, defaults to the source instance
)
Connection(  # thermohaline upwelling
    source=M.db,  # source of flux
    sink=M.sb,  # target of flux
    ctype="scale_with_concentration",
    scale=thc,
    id="upwelling_PO4",
)
\end{minted}

There are several ways to define the biological export production, e.g., as  function of the upwelling PO\textsubscript{4}, or as function of the residence time of PO\textsubscript{4} in surface ocean. Here we follow Glover (2011), and use the residence time \(\tau\) = 100 years.
\begin{minted}[fontsize=\small,frame=lines,linenos]{python}
Connection(  #
    source=M.sb,  # source of flux
    sink=M.db,  # target of flux
    ctype="scale_with_concentration",
    scale=M.sb.volume / tau,
    id="primary_production",
)
\end{minted}

We require one more connection to describe the burial of P in the sediment. We describe this flux as a fraction of the primary export productivity. To create the connection we can either recalculate the export productivity, or use the previously calculated flux. We can query the export productivity using the \texttt{id\_string} of the above connection with the \texttt{flux\_summary()} method of the model instance:
\begin{minted}[fontsize=\small,frame=lines,linenos]{python}
M.flux_summary(filter_by="primary_production", return_list=True)[0]
\end{minted}
The \texttt{flux\_summary()} method will return a list of matching fluxes but since there is only one match, we can simply use  the first result, and use it to define the phosphor burial as a consequence of export production in the following way:
\begin{minted}[fontsize=\small,frame=lines,linenos]{python}
Connection(  #
    source=M.db,  # source of flux
    sink=M.burial,  # target of flux
    ctype="scale_with_flux",
    ref_flux=M.flux_summary(filter_by="primary_production", return_list=True)[0],
    scale=F_b,
    id="burial",
)
\end{minted}
\subsection{Running the model, visualizing and saving the results}
\label{sec:org977d800}

\begin{minted}[fontsize=\small,frame=lines,linenos]{python}
M.run()
M.plot([M.sb, M.db])
\end{minted}
\end{document}