\documentclass[../main/TOP_manual]{subfiles}

\begin{document}

\chapter{Miscellaneous}

\section{TOP synthetic Workflow}

A synthetic description of the TOP interface workflow is given below to summarize the steps involved in the computation of biogeochemical and physical trends and their time integration and outputs, by reporting also the principal Fortran subroutine herein involved.

%\begin{figure}[!h]
%	\centering
%	\includegraphics[width=0.80\textwidth]{Top_FlowChart}
%	\caption{Schematic view of NEMO-TOP flowchart}
%	\label{img_cfcatm}
%\end{figure} 

\begin{minted}{bash}
nemogcm
    !                      
    nemo_init           !   NEMO General Initialisations
         !                   
         trc_init                              ! TOP  Initialisations 
    !
    stp()                   !   NEMO Time-stepping
        !
        trc_stp()                            ! TOP time-stepping
            !
            trc_wri()           ! I/O manager : Output of passive tracers 
            trc_sms()           ! Sinks and sources program manager
            trc_trp()            ! Transport of passive tracers
            trc_rst_wri()      ! Write tracer restart file
            trd_mxl_trc()     ! trends: Mixed-layer
\end{minted}

\subsection{Model initialization (./src/TOP/trcini.F90)}

This module consists on inital set up of passive tracers variables and parameters  : read the namelist, set initial tracer fields (either read restart or read data or analytical formulation and  specific initailisation in each SMS module  ( analytical initialisation of tracers or constant values )

\begin{minted}{bash}
trc_init                              ! TOP  Initialisations 
    !    
    IF( PISCES )    trc_ini_pisces()     !  PISCES bio model
    IF( MY_TRC)    trc_ini_my_trc()    !  MY_TRC model
    IF( CFCs     )    trc_ini_cfc   ()       !  CFCs
    IF( C14       )    trc_ini_c14   ()       !  C14 model
    IF( AGE      )    trc_ini_age   ()       !  AGE tracer
    !
    IF( REST   )    trc_rst_read()         ! Restart from a file  
    ELSE            trc_dta()                   ! Initialisation from data
\end{minted}

\subsection{BGC trends computation (./src/TOP/trcsms.F90)}

This is the main module where the passive tracers source minus sinks of each TOP sub-module is managed.    

\begin{minted}{bash}
trc_sms()                               ! Sinks and sources prooram manager
    ! 
    IF( PISCES  )    trc_sms_pisces()         ! main program of PISCES 
    IF( CFCs     )    trc_sms_cfc()               ! surface fluxes of CFC
    IF( C14       )    trc_sms_c14()               ! surface fluxes of C14
    IF( AGE       )    trc_sms_age()              ! Age tracer
    IF( MY_TRC)    trc_sms_my_trc()         ! MY_TRC  tracers
\end{minted}

\subsection{Physical trends computation (./src/TOP/TRP/trctrp.F90)}

This is the main module where the passive tracers transport is managed. All the physical trends is calculated ( advective \& diffusive trends, surface BC from freshwater or external inputs ) 

\begin{minted}{bash}
trc_trp()       ! Transport of passive tracers
    !
    trc_sbc()         ! Surface boundary condition of freshwater flux
    trc_bc()           ! Surface and lateral Boundary Conditions 
    trc_ais()          ! Tracers from Antarctic Ice Sheet (icb, isf)               
    trc_bbl()          ! Advective (and/or diffusive) bottom boundary layer scheme
    trc_dmp()        ! Internal damping trends
    trc_bdy()         ! BDY damping trends
    trc_adv()         ! Horizontal & Vertical advection 
    trc_ldf()           ! Lateral mixing
    trc_zdf()          ! Vert. mixing & after tracer
    trc_atf()           ! Time filtering of "now" tracer fields    
    trc_rad()         ! Correct artificial negative concentrations
\end{minted}

\subsection{Outputs  (./src/TOP/TRP/trcwri.F90)}

This is the main module where the passive tracer outputs of each TOP sub-module is managed using the I/O library XIOS.

\begin{minted}{bash}
trc_wri()                               ! I/O manager : Output of passive tracers 
!
IF( PISCES   )    trc_wri_pisces()      ! Output of PISCES diagnostics 
IF( CFCs      )    trc_wri_cfc()            ! Output of Cfcs diagnostics
IF( C14         )    trc_wri_c14()           ! surface fluxes of C14
IF( AGE        )    trc_wri_age()           ! Age tracer
IF( MY_TRC )    trc_wri_my_trc()      ! MY_TRC  tracers
\end{minted}

\section{Coupling an external BGC model using NEMO framework}

The coupling with an external BGC model through the NEMO compilation framework can be achieved in different ways according to the degree of coding complexity of the Biogeochemical model, like e.g., the whole code is made only by one file or it has multiple modules and interfaces spread across several subfolders.\\ \\
Beside the 6 core files of MY\_TRC module, see (see \label{Mytrc}, let's assume an external BGC model named \textit{"MYBGC"} and constituted by a rather essential coding structure, likely few Fortran files. The new coupled configuration name is NEMO\_MYBGC. \\ \\
The best solution is to have all files (the modified MY\_TRC routines and the BGC model ones) placed in a unique folder with root \path{<MYBGCPATH>} and to use the \textit{makenemo} external readdressing of MY\_SRC folder. \\ \\
The coupled configuration listed in \textbf{cfg.txt}  will look like

\begin{minted}{bash}
   NEMO_MYBGC OPA_SRC TOP_SRC
\end{minted}
and the related cpp\_MYBGC.fcm content will be
%
\begin{minted}{bash}
    bld::tool::fppkeys   key_xios key_top
\end{minted}

The compilation with \textit{makenemo} will be executed through the following syntax

\begin{minted}{bash}
   makenemo -n NEMO_MYBGC -m <arch_my_machine> -j 8 -e <MYBGCPATH>
\end{minted}

The makenemo feature \textit{-e} was introduced to readdress at compilation time the standard MY\_SRC folder (usually found in NEMO configurations) with a user defined external one. \\ \\

The compilation of more articulated BGC model code \& infrastructure, like in the case of BFM (BFM-NEMO coupling manual), requires some additional features. \\ \\

As before, let's assume a coupled configuration name NEMO\_MYBGC, but in this case MYBGC model root becomes <MYBGCPATH> that contains 4 different subfolders for biogeochemistry, named initialization, pelagic, and benthic, and a separate one named nemo\_coupling including the modified MY\_SRC routines. The latter folder containing the modified NEMO coupling interface will be still linked using the makenemo \textit{-e} option. \\ \\

In order to include the BGC model subfolders in the compilation of NEMO code, it will be necessary to extend the configuration \textit{cpp\_NEMO\_MYBGC.fcm} file to include the specific paths of MYBGC folders, as in the following example

\begin{minted}{bash}
   bld::tool::fppkeys   key_xios key_top

   src::MYBGC::initialization         <MYBGCPATH>/initialization
   src::MYBGC::pelagic                <MYBGCPATH>/pelagic
   src::MYBGC::benthic                <MYBGCPATH>/benthic

   bld::pp::MYBGC      1
   bld::tool::fppflags::MYBGC   \%FPPFLAGS
   bld::tool::fppkeys                  \%bld::tool::fppkeys MYBGC_MACROS
\end{minted}

where MYBGC\_MACROS is the space delimited list of macros used in MYBGC model for selecting/excluding specific parts of the code. The BGC model code will be preprocessed in the configuration BLD folder as for NEMO, but with an independent path, like NEMO\_MYBGC/BLD/MYBGC/<subfolders>.\\

The compilation will be performed similarly to in the previous case with the following

\begin{minted}{bash}
makenemo -n NEMO_MYBGC -m <arch_my_machine> -j 8 -e <MYBGCPATH>/nemo_coupling
\end{minted}

Note that, the additional lines specific for the BGC model source and build paths, can be written into a separate file, e.g. named MYBGC.fcm, and then simply included in the cpp\_NEMO\_MYBGC.fcm as follow:

\begin{minted}{bash}
bld::tool::fppkeys  key_zdftke key_dynspg_ts key_xios key_top
inc <MYBGCPATH>/MYBGC.fcm
\end{minted}

This will enable a more portable compilation structure for all MYBGC related configurations.  \\ \\

Important: the coupling interface contained in nemo\_coupling cannot be added using the FCM syntax, as the same files already exists in NEMO and they are overridden only with the readdressing of MY\_SRC contents to avoid compilation conflicts due to duplicate routines.  \\ \\

All modifications illustrated above, can be easily implemented using shell or python scripting to edit the NEMO configuration cpp.fcm file and to create the BGC model specific FCM compilation file with code paths.

\end{document}
