Back to home page

MITgcm

 
 

    


File indexing completed on 2024-03-02 06:10:49 UTC

view on githubraw file Latest commit 5cf43646 on 2024-03-01 18:50:49 UTC
5dddee4ea2 Jean*0001 CBOI
                0002 C
                0003 C !TITLE: MITGCM KERNEL CODE SYNOPSIS
                0004 C !AUTHORS: mitgcm developers ( support@mitgcm.org )
                0005 C !AFFILIATION: Massachussetts Institute of Technology
                0006 C !DATE:
                0007 C !INTRODUCTION: Kernel dynamical routines
                0008 C This document summarises MITgcm code under the model/ subdirectory.
                0009 C The code under model/ ( src/ and inc/ ) contains most of
                0010 C the driver routines for the baseline forms of the kernel equations in the
                0011 C MITgcm algorithm. Numerical code for much of the baseline forms of
                0012 C these equations is also under the model/ directory. Other numerical code
                0013 C used for the kernel equations is contained in packages in the pkg/
                0014 C directory tree.
63ef2a2a83 Jean*0015 C Code for auxiliary equations and alternate discretizations of the kernel
5dddee4ea2 Jean*0016 C equations and algorithm can also be found in the pkg/ directory tree.
                0017 C
                0018 C \subsection{Getting Help and Reporting Errors and Problems}
63ef2a2a83 Jean*0019 C If you have questions please subscribe and e-mail support@mitgcm.org.
                0020 C We also welcome reports of errors and inconsistencies in the code or
                0021 C in the accompanying documentation. Please feel free to send these
5dddee4ea2 Jean*0022 C to support@mitgcm.org. For further information and to review
                0023 C problems reported to support@mitgcm.org please visit http://mitgcm.org.
                0024 C
                0025 C \subsection{MITgcm Kernel Code Calling Sequence}
                0026 C \bv
                0027 C
                0028 C Invocation from WRAPPER level...
63ef2a2a83 Jean*0029 C
5dddee4ea2 Jean*0030 C  |
                0031 C  |-THE_MODEL_MAIN :: Primary driver for the MITgcm algorithm
                0032 C    |              :: Called from WRAPPER level numerical
                0033 C    |              :: code invocation routine. On entry
                0034 C    |              :: to THE_MODEL_MAIN separate thread and
                0035 C    |              :: separate processes will have been established.
                0036 C    |              :: Each thread and process will have a unique ID
                0037 C    |              :: but as yet it will not be associated with a
                0038 C    |              :: specific region in decomposed discrete space.
                0039 C    |
63ef2a2a83 Jean*0040 C    |-INITIALISE_FIXED :: Set fixed model arrays such as topography,
5dddee4ea2 Jean*0041 C    | |                :: grid, solver matrices etc..
                0042 C    | |
                0043 C    | |-INI_PARMS :: Routine to set kernel model parameters.
63ef2a2a83 Jean*0044 C    | |           :: Kernel parameters are read from file "data"
                0045 C    | |           :: in directory in which code executes.
5dddee4ea2 Jean*0046 C    | |
37e373688b Mart*0047 C    | |-PACKAGES_BOOT       :: Start up the optional package environment.
                0048 C    | |                     :: Runtime selection of active packages.
                0049 C    | |-CPL_IMPORT_CPLPARMS :: Import coupling parameters from/to
                0050 C    | |                     :: the coupler layer
                0051 C    | |-PACKAGES_READPARMS  :: Read each package input parameter file
63ef2a2a83 Jean*0052 C    | | |- ${PKG}_READPARMS
5dddee4ea2 Jean*0053 C    | |
37e373688b Mart*0054 C    | |-SET_PARMS :: Finalise model parameter setting (if fct of pkg usage)
5dddee4ea2 Jean*0055 C    | |
37e373688b Mart*0056 C    | |-INI_MODEL_IO   :: Initialise Input/Output setting
                0057 C    | |  |-MNC_INIT    :: Initialise MITgcm NetCDF interface (MNC)(see pkg/mnc)
                0058 C    | |  |-MNC_CW_INIT :: Initialise MNC grid and variable types  (see pkg/mnc)
                0059 C    | |  |-MON_INIT    :: Initialises monitor package ( see pkg/monitor )
                0060 C    | |
                0061 C    | |-INI_GRID       :: Control grid array (vert. and horiz.) initialisation.
                0062 C    | | |              :: Grid arrays are held and described in GRID.h.
                0063 C    | | |-LOAD_GRID_SPACING    :: Load grid spacing (vector) from files
                0064 C    | | |-INI_VERTICAL_GRID    :: Set up vertical grid and coordinate
                0065 C    | | |-INI_CARTESIAN_GRID   :: Cartesian horiz. grid initialisation
                0066 C    | | |                      :: (calculate grid from kernel parameters).
                0067 C    | | |-INI_SPHERICAL_POLAR_GRID :: Spherical polar horiz. grid setting
                0068 C    | | |                          :: (calculate grid from kernel parameters).
                0069 C    | | |-INI_CURVILINEAR_GRID :: General orthogonal, structured horiz. grid
                0070 C    | | |                      :: initialisation; input from raw grid files
                0071 C    | | |                      :: (LONC.bin, LATC.bin, DXF.bin, ... ) or per
                0072 C    | | |                      :: face file: horizGridFile(.faceXXX.bin)
                0073 C    | | |-INI_CYLINDER_GRID    :: Cylindrical horiz. grid setting
                0074 C    | |
                0075 C    | |-LOAD_REF_FILES   :: Read-in reference vertical profiles (T,S,Rho)
                0076 C    | |-INI_EOS          :: Initialise Equation Of State (EOS) coefficients
                0077 C    | |-SET_REF_STATE    :: Set reference pressure/geopotential, reference
                0078 C    | |                  :: stratification (for implicit IGW), vertical
                0079 C    | |                  :: velocity scaling factor and anelastic ref. density
                0080 C    | |-SET_GRID_FACTORS :: Set grid factors (fct of k) for deep-atmosphere
5dddee4ea2 Jean*0081 C    | |
63ef2a2a83 Jean*0082 C    | |-INI_DEPTHS    :: Read (from "bathyFile") or set bathymetry/orography.
                0083 C    | |-INI_MASKS_ETC :: Derive horizontal and vertical cell fractions and
                0084 C    | |               :: land masking for solid-fluid boundaries.
5dddee4ea2 Jean*0085 C    | |
63ef2a2a83 Jean*0086 C    | |-PACKAGES_INIT_FIXED  :: do all packages fixed-initialisation setting
                0087 C    | | |- ${PKG}_INIT_FIXED
5dddee4ea2 Jean*0088 C    | |
37e373688b Mart*0089 C    | |-INI_GLOBAL_DOMAIN :: Initialise domain related (global) quantities.
                0090 C    | |-INI_LINEAR_PHISURF :: Set ref. surface Bo_surf
                0091 C    | |
                0092 C    | |-INI_CORI          :: Set coriolis term. zero, f-plane, beta-plane,
                0093 C    | |                   :: sphere options are coded.
                0094 C    | |-INI_CG2D          :: 2D conjugate grad solver initialisation.
                0095 C    | |-INI_CG3D          :: 3D conjugate grad solver initialisation.
5dddee4ea2 Jean*0096 C    | |
37e373688b Mart*0097 C    | |-CONFIG_SUMMARY    :: Provide synopsis of kernel setup. Includes
                0098 C    | |                   :: annotated table of kernel parameter settings.
                0099 C    | |
                0100 C    | |-PACKAGES_CHECK    :: call each package configuration checking S/R
63ef2a2a83 Jean*0101 C    | | |- ${PKG}_CHECK
5dddee4ea2 Jean*0102 C    | |
37e373688b Mart*0103 C    | |-CONFIG_CHECK      :: Check config and parameter consistency.
                0104 C    | |
                0105 C    | |-WRITE_GRID        :: write grid fields to output files
                0106 C    | |-CPL_EXCH_CONFIGS  :: exchange config with coupler-interface
5dddee4ea2 Jean*0107 C    |
37e373688b Mart*0108 C    |-CTRL_UNPACK         :: Control vector support package. see pkg/ctrl
                0109 C    |-COST_DEPENDENT_INIT :: ( see pkg/cost )
5dddee4ea2 Jean*0110 C    |
                0111 C    |-ADTHE_MAIN_LOOP :: Derivative evaluating form of main time stepping loop
63ef2a2a83 Jean*0112 C    !                 :: Automatically generated by TAMC/TAF.
5dddee4ea2 Jean*0113 C    |
63ef2a2a83 Jean*0114 C    |-THE_MAIN_LOOP   :: Main timestepping loop routine.
5dddee4ea2 Jean*0115 C    | |
37e373688b Mart*0116 C    | |-INITIALISE_VARIA :: Set the initial conditions for time evolving fields
                0117 C    | | |
                0118 C #ifdef ALLOW_AUTODIFF
                0119 C    | | |-INI_DEPTHS         \
                0120 C    | | |-CTRL_DEPTH_INI      \
                0121 C    | | |-UPDATE_MASKS_ETC     } ALLOW_DEPTH_CONTROL case
                0122 C    | | |-UPDATE_CG2D         /
                0123 C #endif
                0124 C    | | |-INI_NLFS_VARS :: Initialise all Non-Lin Free-Surf arrays (SURFACE.h)
                0125 C    | | |-INI_DYNVARS   :: Initialise to zero all DYNVARS.h arrays
                0126 C    | | |-INI_NH_VARS   :: Initialise to zero all NH_VARS.h arrays
                0127 C    | | |-INI_FFIELDS   :: Initialise forcing fields in FFIELDS.h to zero
                0128 C    | | |
                0129 C    | | |-INI_FIELDS    :: Control initialising model fields to non-zero
                0130 C    | | | |-INI_VEL     :: Initialize 3D flow field.
                0131 C    | | | |-INI_THETA   :: Set model initial temperature field.
                0132 C    | | | |-INI_SALT    :: Set model initial salinity field.
                0133 C    | | | |-INI_PSURF   :: Set model initial free-surface height/pressure.
                0134 C    | | | |-READ_PICKUP :: Read in main model pickup files to restart a run.
                0135 C    | | |
                0136 C    | | |-INI_MIXING   :: Initialise diapycnal diffusivity.
                0137 C    | | |
                0138 C    | | |-TAUEDDY_INIT_VARIA :: Initialise eddy (bolus) streamfunction
5dddee4ea2 Jean*0139 C    | | |
37e373688b Mart*0140 C    | | |-INI_FORCING  :: Set model initial forcing fields, either
                0141 C    | | |   |          :: set in-line or from file as shown here:
                0142 C    | | |   |-READ_FLD_XY_RS(zonalWindFile)
                0143 C    | | |   |-READ_FLD_XY_RS(meridWindFile)
                0144 C    | | |   |-READ_FLD_XY_RS(surfQnetFile)
                0145 C    | | |   |-READ_FLD_XY_RS(EmPmRfile)
                0146 C    | | |   |-READ_FLD_XY_RS(thetaClimFile)
                0147 C    | | |   |-READ_FLD_XY_RS(saltClimFile)
                0148 C    | | |   |-READ_FLD_XY_RS(surfQswFile)
5dddee4ea2 Jean*0149 C    | | |
37e373688b Mart*0150 C    | | |-AUTODIFF_INIT_VARIA :: (see pkg/autodiff )
5dddee4ea2 Jean*0151 C    | | |
63ef2a2a83 Jean*0152 C    | | |-PACKAGES_INIT_VARIABLES :: Does initialisation of time evolving
                0153 C    | | | | ${PKG}_INIT_VARIA     :: package data.
5dddee4ea2 Jean*0154 C    | | |
37e373688b Mart*0155 C    | | |-COST_INIT_VARIA     :: ( see pkg/cost )
                0156 C    | | |-CONVECTIVE_ADJUSTMENT_INI :: Apply conv. adjustment to initial state
                0157 C    | | |
                0158 C    | | |-CALC_R_STAR    :: Calculate the new level thickness factor (r* coord)
                0159 C    | | |-UPDATE_R_STAR  :: Update the level thickness fraction (r* coord).
                0160 C    | | |-UPDATE_SIGMA   :: Update the level thickness fraction (sigma-coord).
                0161 C    | | |-CALC_SURF_DR   :: Calculate the new surface level thickness.
                0162 C    | | |-UPDATE_SURF_DR :: Update the surface-level thickness fraction.
                0163 C    | | |
                0164 C    | | |-UPDATE_CG2D    :: Update 2D conjugate grad. for Free-Surf.
                0165 C    | | |
                0166 C    | | |-INTEGR_CONTINUITY :: Integrate the continuity Equation
                0167 C    | | | |-INTEGRATE_FOR_W :: Integrate for vertical velocity
                0168 C    | | | |-OBCS_APPLY_W    :: Open boundary package (see pkg/obcs).
                0169 C    | | | |-DUMMY_FOR_ETAN  :: For printing adEtaN (see pkg/autodiff).
                0170 C    | | | |-UPDATE_ETAH     :: Update Surface height/pressure
                0171 C    | | |
                0172 C    | | |-CALC_R_STAR    :: Calculate the new level thickness factor (r* coord)
                0173 C    | | |-CALC_SURF_DR   :: Calculate the new surface level thickness.
                0174 C    | | |
63ef2a2a83 Jean*0175 C    | | |-STATE_SUMMARY    :: Summarise model prognostic variables.
37e373688b Mart*0176 C    | | |
                0177 C    | | |-MONITOR          :: Monitor state (see pkg/monitor)
                0178 C    | | |
                0179 C    | | |-DO_STATEVARS_TAVE :: Time averaging package ( see pkg/timeave ).
                0180 C    | | |  |-TIMEAVE_STATVARS :: Accumulate main model state variables
                0181 C    | | |  |-PTRACERS_TIMEAVE :: Accumulate passive tracers variables
                0182 C    | | |
                0183 C    | | |-DO_THE_MODEL_IO  :: Controlling routine for IO
                0184 C    | | | |-WRITE_STATE    ::  Write model state variables.
                0185 C    | | | |-TIMEAVE_STATV_WRITE :: Write Time averaged output (see pkg/timeave)
                0186 C    | | | |-FIZHI_WRITE_STATE :: Write Fizhi pkg output (see pkg/fizhi)
                0187 C    | | | |-AIM_WRITE_TAVE    :: Write AIM  pkg output (see pkg/aim_v23)
                0188 C    | | | |-LAND_OUTPUT       :: Write Land pkg output (see pkg/land)
                0189 C    | | | |-OBCS_OUTPUT       :: Write OBCS pkg output (see pkg/obcs)
                0190 C    | | | |-GMREDI_OUTPUT     :: Write GM-Redi pkg output (see pkg/gmredi)
                0191 C    | | | |-KPP_OUTPUT        :: Write KPP  pkg output (see pkg/kpp)
                0192 C    | | | |-PP81_OUTPUT       :: Write PP81 pkg output (see pkg/pp81)
                0193 C    | | | |-KL10_OUTPUT       :: Write KL10 pkg output (see pkg/kl10)
                0194 C    | | | |-MY82_OUTPUT       :: Write MY82 pkg output (see pkg/my82)
                0195 C    | | | |-OPPS_OUTPUT       :: Write OPPS pkg output (see pkg/opps)
                0196 C    | | | |-GGL90_OUTPUT      :: Write GGL90 pkg output (see pkg/ggl90)
                0197 C    | | | |-SBO_CALC          :: Compute SBO diagnostics (see pkg/sbo)
                0198 C    | | | |-SBO_OUTPUT        :: Write SBO  pkg output   (see pkg/sbo)
                0199 C    | | | |-SEAICE_OUTPUT     :: Write SeaIce pkg output (see pkg/seaice)
                0200 C    | | | |-SHELFICE_OUTPUT   :: Write ShelfIce pkg output (see pkg/shelfice)
                0201 C    | | | |-BULKF_OUTPUT      :: Write Bulk-Force output (see pkg/bulK_force)
                0202 C    | | | |-THSICE_OUTPUT     :: Write ThSIce pkg output (see pkg/thsice)
                0203 C    | | | |-PTRACERS_OUTPUT   :: Write pTracers pkg output (see pkg/ptracers)
                0204 C    | | | |-MATRIX_OUTPUT     :: Write Matrix pkg output (see pkg/matrix)
                0205 C    | | | |-GCHEM_OUTPUT      :: Write Geochemistry pkg output (see pkg/gchem)
                0206 C    | | | |-CPL_OUTPUT        :: Write Coupler-Interface output (see
                0207 C    | | | |                   :: pkg/atm_compon_interf, pkg/ocn_compon_interf)
                0208 C    | | | |-LAYERS_CALC       :: Calculate layers diagnostics (see pkg/layers)
                0209 C    | | | |-LAYERS_OUTPUT     :: Write Layers pkg output (see pkg/layers)
                0210 C    | | | |-DIAGNOSTICS_WRITE :: Write pkg/diagnostics output
                0211 C    | | |
5dddee4ea2 Jean*0212 C====|>| ****************************
                0213 C====|>| BEGIN MAIN TIMESTEPPING LOOP
                0214 C====|>| ****************************
63ef2a2a83 Jean*0215 C    | |-COST_AVERAGESFIELDS :: time-averaged Cost function terms (see pkg/cost)
37e373688b Mart*0216 C    | |-PROFILES_INLOOP     :: ( see pkg/profiles )
                0217 C    | /
                0218 C    | |-MAIN_DO_LOOP    :: Open-AD case: Main timestepping loop routine
                0219 C    | \                    otherwise: just call FORWARD_STEP
63ef2a2a83 Jean*0220 C    | |
                0221 C/\  | |-FORWARD_STEP        :: Step forward a time-step ( AT LAST !!! )
5dddee4ea2 Jean*0222 C/\  | | |
37e373688b Mart*0223 C/\  | | |-AUTODIFF_INADMODE_UNSET :: Set/reset some adjoint flags
                0224 C/\  | | |-RESET_NLFS_VARS   :: Reset some Non-Lin Free-Surf vars (Adjoint)
                0225 C/\  | | |-UPDATE_R_STAR     :: Reset r-star factor variables     (Adjoint)
                0226 C/\  | | |-UPDATE_SURF_DR    :: Reset NLFS surface thickness vars (Adjoint)
                0227 C/\  | | |
                0228 C/\  | | |-PTRACERS_SWITCH_ONOFF    :: Set/reset pTracers time-stepping switch
                0229 C/\  | | |-DIAGNOSTICS_SWITCH_ONOFF :: Activate/de-activate diagnostics
                0230 C/\  | | |-DO_STATEVARS_DIAGS ( 0 ) :: fill-up state variable diagnostics
                0231 C/\  | | |
                0232 C/\  | | |-NEST_CHILD_SETMEMO :: Nesting interface
                0233 C/\  | | |-NEST_PARENT_IO_1   :: Nesting interface
                0234 C/\  | | |
                0235 C/\  | | |-LOAD_FIELDS_DRIVER :: Control loading of input fields from files
                0236 C/\  | | |
                0237 C/\  | | |-BULKF_FORCING      :: Calculate surface forcing (see pkg/bulk_force)
                0238 C/\  | | |-CHEAPAML           :: Cheap AML driver ( see pkg/cheapaml )
                0239 C/\  | | |-CTRL_MAP_FORCING   :: Control vector support package. (see pkg/ctrl)
                0240 C/\  | | |-DUMMY_IN_STEPPING  :: Autodiff package ( pkg/autodiff ).
5dddee4ea2 Jean*0241 C/\  | | |
63ef2a2a83 Jean*0242 C/\  | | |-CPL_EXPORT_MY_DATA :: Send coupling fields to coupler
                0243 C/\  | | |-CPL_IMPORT_EXTERNAL_DATA :: Receive coupling fields from coupler
                0244 C/\  | | |
37e373688b Mart*0245 C/\  | | |-OASIS_PUT     :: Oasis coupler interface
                0246 C/\  | | |-OASIS_GET     :: Oasis coupler interface
                0247 C/\  | | |
                0248 C/\  | | |-EBM_DRIVER    :: Calculate EBM type atmospheric forcing (see pkg/ebm)
                0249 C/\  | | |
63ef2a2a83 Jean*0250 C/\  | | |-DO_ATMOSPHERIC_PHYS :: Atmospheric physics computation
37e373688b Mart*0251 C/\  | | | |
                0252 C/\  | | | |-UPDATE_OCEAN_EXPORTS     :: ( see pkg/fizhi )
                0253 C/\  | | | |-UPDATE_EARTH_EXPORTS     :: ( see pkg/fizhi )
                0254 C/\  | | | |-UPDATE_CHEMISTRY_EXPORTS :: ( see pkg/fizhi )
                0255 C/\  | | | |-FIZHI_WRAPPER            :: ( see pkg/fizhi )
                0256 C/\  | | | |-STEP_FIZHI_FG            :: ( see pkg/fizhi )
                0257 C/\  | | | |-FIZHI_UPDATE_TIME        :: ( see pkg/fizhi )
                0258 C/\  | | | |
                0259 C/\  | | | |-ATM_PHYS_DRIVER          :: ( see pkg/atm_phys )
                0260 C/\  | | | |
                0261 C/\  | | | |-AIM_DO_PHYSICS           :: ( see pkg/aim_v23 )
63ef2a2a83 Jean*0262 C/\  | | |
                0263 C/\  | | |-DO_OCEANIC_PHYS     :: Oceanic (& seaice) physics computation
37e373688b Mart*0264 C/\  | | | |
63ef2a2a83 Jean*0265 C/\  | | | |-OBCS_CALC         :: Open boundary. package (see pkg/obcs).
37e373688b Mart*0266 C/\  | | | |
                0267 C/\  | | | |-FRAZIL_CALC_RHS   :: Compute FRAZIL tendencies ( see pkg/frazil )
                0268 C/\  | | | |-THSICE_MAIN       :: Thermodynamic sea-ice driver (see pkg/thsice)
                0269 C/\  | | | |-SEAICE_MODEL      :: Sea-ice model driver (see pkg/seaice )
                0270 C/\  | | | |-SEAICE_COST_SENSI   :: Sea-ice cost-function (see pkg/seaice )
                0271 C/\  | | | |-SHELFICE_THERMODYNAMICS :: Compute ShelfIce thermo (pkg/shelfice)
                0272 C/\  | | | |-ICEFRONT_THERMODYNAMICS :: Compute IceFront thermo (pkg/icefront)
                0273 C/\  | | | |
                0274 C/\  | | | |-SALT_PLUME_DO_EXCH   :: (see pkg/salt_plume )
                0275 C/\  | | | |-FREEZE_SURFACE       :: Prevent SST to fall below TFreeze
                0276 C/\  | | | |-OCN_APPLY_IMPORT     :: Apply imported fields from coupler
                0277 C/\  | | | |-EXTERNAL_FORCING_SURF:: Compute appropriately dimensioned
                0278 C/\  | | | |                      :: surface forcing terms.
                0279 C/\  | | | |-FIND_RHO_2D @ p(k)   :: Calculate [rho(T,S,p)-Rho_0] of a slice
                0280 C/\  | | | |-FIND_RHO_2D @ p(k-1) :: Calculate [rho(T,S,p)-Rho_0] of a slice
                0281 C/\  | | | |-GRAD_SIGMA           :: Calculate isoneutral gradients
                0282 C/\  | | | |-CALC_IVDC       :: Set Implicit Vertical Diffusivity for Convection
                0283 C/\  | | | |-CALC_OCE_MXLAYER        :: Diagnose Oceanic Mixed Layer depth
                0284 C/\  | | | |
                0285 C/\  | | | |-SALT_PLUME_CALC_DEPTH   :: (see pkg/salt_plume )
                0286 C/\  | | | |-SALT_PLUME_VOLFRAC      :: (see pkg/salt_plume )
                0287 C/\  | | | |-SALT_PLUME_APPLY (Temp) :: (see pkg/salt_plume )
                0288 C/\  | | | |-SALT_PLUME_APPLY (Salt) :: (see pkg/salt_plume )
                0289 C/\  | | | |-SALT_PLUME_FORCING_SURF :: (see pkg/salt_plume )
                0290 C/\  | | | |-KPP_CALC           :: Compute KPP  vertical mixing ( see pkg/kpp )
                0291 C/\  | | | |-PP81_CALC          :: Compute PP81 vertical mixing ( see pkg/pp81 )
                0292 C/\  | | | |-KL10_CALC          :: Compute KL10 vertical mixing ( see pkg/kl10 )
                0293 C/\  | | | |-MY82_CALC          :: Compute MY82 vertical mixing ( see pkg/kl10 )
                0294 C/\  | | | |-GGL90_CALC         :: Compute GGL90 vertical mixing (see pkg/ggl10)
                0295 C/\  | | | |-GMREDI_CALC_TENSOR :: Compute GM-Redi tensor ( see pkg/gmredi )
                0296 C/\  | | | |-DWNSLP_CALC_FLOW   :: Compute Down-Slope flow  (see pkg/down_slope)
                0297 C/\  | | | |-BBL_CALC_RHS       :: Compute BBL tendencies ( see pkg/bbl )
                0298 C/\  | | | |-MYPACKAGE_CALC_RHS :: Compute mypackage tendencies (pkg/mypackage)
                0299 C/\  | | | |
                0300 C/\  | | | |-GMREDI_DO_EXCH     :: ( see pkg/gmredi )
                0301 C/\  | | | |-KPP_DO_EXCH        :: ( see pkg/kpp )
                0302 C/\  | | | |-DIAGS_RHO_G        :: Compute some density related diagnostics
                0303 C/\  | | | |-DIAGS_OCEANIC_SURF_FLUX :: Diagnose oceanic surface fluxes
                0304 C/\  | | | |-SALT_PLUME_DIAGNOSTICS_FILL :: (see pkg/salt_plume )
                0305 C/\  | | | |-ECCO_PHYS          :: ( see pkg/ecco )
                0306 C/\  | | |
                0307 C/\  | | |-STREAMICE_TIMESTEP   :: ( see pkg/streamice )
                0308 C/\  | | |
                0309 C/\  | | |-GCHEM_CALC_TENDENCY  :: geochemistry driver routine (see pkg/gchem)
63ef2a2a83 Jean*0310 C/\  | | |
37e373688b Mart*0311 C/\  | | |-LONGSTEP_AVERAGE        :: Averaging state vars ( see pkg/longstep )
                0312 C/\  | | |-LONGSTEP_THERMODYNAMICS :: Step forward tracers ( see pkg/longstep )
                0313 C/\  | | |
                0314 C/\  | | |-THERMODYNAMICS       :: theta, salt + tracer equations driver.
                0315 C/\  | | | |                         (synchronous time-stepping case)
                0316 C/\  | | | |-CALC_WSURF_TR          :: Compute T & S Linear-Free-Surf correction
                0317 C/\  | | | |-PTRACERS_CALC_WSURF_TR :: Compute Tracers Linear-Free-Surf correct.
                0318 C/\  | | | |
                0319 C/\  | | | |-GMREDI_RESIDUAL_FLOW :: Get the flow field used to advect tracers
                0320 C/\  | | | |
                0321 C/\  | | | |-TEMP_INTEGRATE       :: Step forward Prognostic Eq for Temperature.
                0322 C/\  | | | | |
                0323 C/\  | | | | |-ADAMS_BASHFORTH3   :: Extrapolate tracer forward in time (AB-3)
                0324 C/\  | | | | |-ADAMS_BASHFORTH2   :: Extrapolate tracer forward in time (AB-2)
                0325 C/\  | | | | |-CALC_3D_DIFFUSIVITY :: set vertical diffusivity
                0326 C/\  | | | | |
                0327 C/\  | | | | |-GAD_SOM_ADVECT     :: Second Order Moment (SOM) advection
                0328 C/\  | | | | |-GAD_ADVECTION      :: Generalised advection driver (multi-dim
                0329 C/\  | | | | |                         advection case) (see pkg/gad).
                0330 C/\  | | | | |-CALC_ADV_FLOW      :: set 3-D flow field to advect tracer
                0331 C/\  | | | | |-APPLY_FORCING_T    :: Problem specific forcing for temperature.
                0332 C/\  | | | | |-GAD_CALC_RHS       :: Calculate Advection-Diffusion tendency terms
                0333 C/\  | | | | |
                0334 C/\  | | | | |-ADAMS_BASHFORTH3   :: Extrapolate tendency forward in time (AB-3)
                0335 C/\  | | | | |-ADAMS_BASHFORTH2   :: Extrapolate tendency forward in time (AB-2)
                0336 C/\  | | | | |-FREESURF_RESCALE_G :: Re-scale Gt for free-surface height.
                0337 C/\  | | | | |-DWNSLP_APPLY       :: Add pkg/down_slope tendency
                0338 C/\  | | | | |
                0339 C/\  | | | | |-TIMESTEP_TRACER    :: Step tracer field forward in time
                0340 C/\  | | | | |
                0341 C/\  | | | | |-GAD_IMPLICIT_R     :: Solve vertical implicit Advect-Diffus. eqn.
                0342 C/\  | | | | |-IMPLDIFF           :: Solve vertical implicit diffusion equation.
                0343 C/\  | | | | |-CYCLE_AB_TRACER    :: Cycle time-stepping arrays for tracer field
                0344 C/\  | | | | |-CYCLE_TRACER       :: Cycle time-stepping arrays for tracer field
                0345 C/\  | | | |
                0346 C/\  | | | |-SALT_INTEGRATE       :: Step forward Prognostic Eq for Salinity.
                0347 C/\  | | | | |                       same sequence of calls as in TEMP_INTEGRATE
                0348 C/\  | | | |
5dddee4ea2 Jean*0349 C/\  | | | |-PTRACERS_INTEGRATE   :: Integrate other tracer(s) (see pkg/ptracers).
37e373688b Mart*0350 C/\  | | | | |                       same sequence of calls as in TEMP_INTEGRATE
                0351 C/\  | | | | |-OBCS_APPLY_PTRACER :: Open boundary package for pTracers
                0352 C/\  | | | |
63ef2a2a83 Jean*0353 C/\  | | | |-OBCS_APPLY_TS        :: Open boundary package (see pkg/obcs ).
5dddee4ea2 Jean*0354 C/\  | | |
37e373688b Mart*0355 C/\  | | |-LONGSTEP_AVERAGE        :: Averaging state vars ( see pkg/longstep )
                0356 C/\  | | |-LONGSTEP_THERMODYNAMICS :: Step forward tracers ( see pkg/longstep )
                0357 C/\  | | |
                0358 C/\  | | |-DO_STAGGER_FIELDS_EXCHANGES :: Update overlap regions of arrays
                0359 C/\  | | |                                 Theta & Salt (implicit IGW case)
                0360 C/\  | | |
5dddee4ea2 Jean*0361 C/\  | | |-DYNAMICS       :: Momentum equations driver.
                0362 C/\  | | | |
63ef2a2a83 Jean*0363 C/\  | | | |-CALC_GRAD_PHI_SURF :: Calculate the gradient of the surface
5dddee4ea2 Jean*0364 C/\  | | | |                       Potential anomaly.
63ef2a2a83 Jean*0365 C/\  | | | |-CALC_VISCOSITY   :: Calculate net vertical viscosity
37e373688b Mart*0366 C/\  | | | |-MOM_CALC_3D_STRAIN :: Calculates the strain tensor of 3D flow field
                0367 C/\  | | | |-OBCS_COPY_UV_N   :: for Stevens bndary Conditions (see pkg/obcs)
                0368 C/\  | | | |
63ef2a2a83 Jean*0369 C/\  | | | |-CALC_PHI_HYD     :: Integrate the hydrostatic relation.
37e373688b Mart*0370 C/\  | | | |-MOM_FLUXFORM     :: Flux Form momentum eqn. (pkg/mom_fluxform)
                0371 C/\  | | | |-MOM_VECINV       :: Vector Invariant momentum eqn (pkg/mom_vecinv)
                0372 C/\  | | | |-MOM_CALC_SMAG_3D :: Calculate Smagorinsky 3D (harmonic) viscosities
                0373 C/\  | | | |-MOM_UV_SMAG_3D   :: Calculate U,V mom. tendency due to Smag 3D Visc
                0374 C/\  | | | |-TIMESTEP         :: Step horizontal momentum fields forward in time
                0375 C/\  | | | |
                0376 C/\  | | | |-MOM_U_IMPLICIT_R :: Solve implicitly vertical Adv-Diffus equation.
63ef2a2a83 Jean*0377 C/\  | | | |-IMPLDIFF         :: Solve vertical implicit diffusion equation.
37e373688b Mart*0378 C/\  | | | |-OBCS_SAVE_UV_N   :: for Stevens bndary Conditions (see pkg/obcs)
                0379 C/\  | | | |-OBCS_APPLY_UV    :: Apply Open bndary Conditions to provisional U,V
                0380 C/\  | | | |-IMPLDIFF         :: (CD-Scheme) Solve vertical impl. diffus. eqn
                0381 C/\  | | | |
                0382 C/\  | | | |-CALC_GW          :: Vert. momentum tendency terms (Non-Hydrostatic)
                0383 C/\  | | | | |-MOM_W_SMAG_3D  :: Calculate W mom. tendency due to Smag 3D Visc
                0384 C/\  | | | |-TIMESTEP_WVEL    :: Step vert mom forward in time (Non-Hydrostatic)
5dddee4ea2 Jean*0385 C/\  | | |
37e373688b Mart*0386 C/\  | | |-MNC_UPDATE_TIME    :: Update MNC time record (see pkg/mnc)
                0387 C/\  | | |
                0388 C/\  | | |-UPDATE_R_STAR  :: Update the level thickness fraction (r* coord).
                0389 C/\  | | |-UPDATE_SIGMA   :: Update the level thickness fraction (sigma-coord).
63ef2a2a83 Jean*0390 C/\  | | |-UPDATE_R_STAR  :: Update the level thickness fraction.
37e373688b Mart*0391 C/\  | | |-UPDATE_SURF_DR :: Update the surface-level thickness fraction.
                0392 C/\  | | |-UPDATE_CG2D    :: Update 2D conjugate grad. for Free-Surf.
                0393 C/\  | | |
                0394 C/\  | | |-SHAP_FILT_APPLY_UV  :: Apply Shapiro Filter to provisional velocity
                0395 C/\  | | |-ZONAL_FILT_APPLY_UV :: Apply  Zonal  Filter to provisional velocity
5dddee4ea2 Jean*0396 C/\  | | |
63ef2a2a83 Jean*0397 C/\  | | |-SOLVE_FOR_PRESSURE  :: Find surface pressure.
37e373688b Mart*0398 C/\  | | | |-CALC_DIV_GHAT     :: Form the RHS of the surface pressure eqn.
5dddee4ea2 Jean*0399 C/\  | | | |-CG2D              :: Two-dim pre-con. conjugate-gradient.
37e373688b Mart*0400 C/\  | | | |-PRE_CG3D          :: Finish to set the RHS of the 3-D pressure eqn.
5dddee4ea2 Jean*0401 C/\  | | | |-CG3D              :: Three-dim pre-con. conjugate-gradient solver.
37e373688b Mart*0402 C/\  | | | |-POST_CG3D         :: finalise solution of NH and Free-Surf pressure
5dddee4ea2 Jean*0403 C/\  | | |
63ef2a2a83 Jean*0404 C/\  | | |-MOMENTUM_CORRECTION_STEP :: Finalise momentum stepping
37e373688b Mart*0405 C/\  | | | |-CALC_GRAD_PHI_SURF  :: Return DDx and DDy of surface pressure
                0406 C/\  | | | |-CORRECTION_STEP     :: Pressure correction to momentum
                0407 C/\  | | | |-OBCS_APPLY_UV       :: Open boundary package (see pkg/obcs).
                0408 C/\  | | | |-SHAP_FILT_APPLY_UV  :: Apply Shapiro Filter to latest velocity
                0409 C/\  | | | |-ZONAL_FILT_APPLY_UV :: Apply  Zonal  Filter to latest velocity
                0410 C/\  | | |
                0411 C/\  | | |-INTEGR_CONTINUITY   :: Integrate continuity equation (see above)
                0412 C/\  | | |
                0413 C/\  | | |-CALC_R_STAR    :: Calculate the new level thickness factor (r* coord)
                0414 C/\  | | |-CALC_SURF_DR   :: Calculate the new surface level thickness.
                0415 C/\  | | |
                0416 C/\  | | |-DO_STAGGER_FIELDS_EXCHANGES :: Update overlap regions of arrays
                0417 C/\  | | |                             uVel,vVel & wVel (stagger-time-step case)
5dddee4ea2 Jean*0418 C/\  | | |
37e373688b Mart*0419 C/\  | | |-DO_STATEVARS_DIAGS ( 1 ) :: fill-up state variable diagnostics
5dddee4ea2 Jean*0420 C/\  | | |
37e373688b Mart*0421 C/\  | | |-THERMODYNAMICS       :: theta, salt + tracer Eq. driver (see above).
                0422 C/\  | | |                         (staggered time-stepping case)
5dddee4ea2 Jean*0423 C/\  | | |
37e373688b Mart*0424 C/\  | | |-TRACERS_CORRECTION_STEP :: Finalise tracer stepping:
                0425 C/\  | | | |                       ::  apply filter, conv.adjustment
                0426 C/\  | | | |-TRACERS_IIGW_CORRECTION :: apply Implicit IGW adjustment to T & S
                0427 C/\  | | | |-SHAP_FILT_APPLY_TS        :: Apply Shapiro Filter to latest T & S
                0428 C/\  | | | |-ZONAL_FILT_APPLY_TS       :: Apply  Zonal  Filter to latest T & S
                0429 C/\  | | | |-PTRACERS_ZONAL_FILT_APPLY :: Apply  Zonal Filter to pTracers
                0430 C/\  | | | |-SALT_FILL                 :: Fill up negative Salt
                0431 C/\  | | | |-OPPS_INTERFACE            :: ( see pkg/opps )
                0432 C/\  | | | |-CONVECTIVE_ADJUSTMENT     :: Apply convective adjustment
                0433 C/\  | | | |-MATRIX_STORE_TENDENCY_IMP :: ( see pkg/matrix )
5dddee4ea2 Jean*0434 C/\  | | |
37e373688b Mart*0435 C/\  | | |-LONGSTEP_AVERAGE        :: Averaging state vars ( see pkg/longstep )
                0436 C/\  | | |-LONGSTEP_THERMODYNAMICS :: Step forward tracers ( see pkg/longstep )
                0437 C/\  | | |
                0438 C/\  | | |-GCHEM_FORCING_SEP :: Tracer forcing for gchem pkg (if tracer
                0439 C/\  | | |                   :: dependent tendencies calculated separately)
5dddee4ea2 Jean*0440 C/\  | | |
63ef2a2a83 Jean*0441 C/\  | | |-DO_FIELDS_BLOCKING_EXCHANGES :: Sync up overlap regions.
                0442 C/\  | | |
37e373688b Mart*0443 C/\  | | |-DO_STATEVARS_DIAGS ( 2 ) :: fill-up state variable diagnostics
                0444 C/\  | | |
                0445 C/\  | | |-GRIDALT_UPDATE    :: ( see pkg/gridalt )
                0446 C/\  | | |-STEP_FIZHI_CORR   :: ( see pkg/fizhi )
                0447 C/\  | | |
                0448 C/\  | | |-FLT_MAIN          :: Step forward Floats (see pkg/flt)
                0449 C/\  | | |
                0450 C/\  | | |-DO_STATEVARS_TAVE :: Time averaging package (see above)
                0451 C/\  | | |
                0452 C/\  | | |-NEST_PARENT_IO_2  :: Nesting interface
                0453 C/\  | | |-NEST_CHILD_TRANSP :: Nesting interface
                0454 C/\  | | |
63ef2a2a83 Jean*0455 C/\  | | |-MONITOR          :: Monitor package (pkg/monitor).
5dddee4ea2 Jean*0456 C/\  | | |
37e373688b Mart*0457 C/\  | | |-COST_TILE        :: ( see pkg/cost )
                0458 C/\  | | |
                0459 C/\  | | |-DO_THE_MODEL_IO  :: Controlling routine for IO (see above)
                0460 C/\  | | |
                0461 C/\  | | |-PTRACERS_RESET   :: Re-initialize PTRACERS ( see pkg/ptracers )
                0462 C/\  | | |
                0463 C/\  | | |-DO_WRITE_PICKUP  :: Controlling routine for writing files to restart
                0464 C/\  | | | |-PACKAGES_WRITE_PICKUP :: Write pickup files for each package
                0465 C/\  | | | | |                     ::  which needs it to restart
                0466 C/\  | | | | |-GAD_WRITE_PICKUP       :: Write Generic AdvDiff pickups for SOM
                0467 C/\  | | | | |                        :: advection scheme (pkg/generic_advdiff)
                0468 C/\  | | | | |-CD_CODE_WRITE_PICKUP   :: Write CD-code pickups (see pkg/cd_code)
                0469 C/\  | | | | |-OBCS_WRITE_PICKUP      :: Write OBCS pickups    (see pkg/obcs)
                0470 C/\  | | | | |-GGL90_WRITE_PICKUP     :: Write GGL90 pickups   (see pkg/ggl90)
                0471 C/\  | | | | |-BBL_WRITE_PICKUP       :: Write BBL pickups     (see pkg/bbl)
                0472 C/\  | | | | |-CHEAPAML_WRITE_PICKUP  :: Write CheapAML pickups (pkg/cheapaml)
                0473 C/\  | | | | |-FLT_WRITE_PICKUP       :: Write Floats pickups  (see pkg/flt)
                0474 C/\  | | | | |-PTRACERS_WRITE_PICKUP  :: Write pTracers pickups (pkg/ptracers)
                0475 C/\  | | | | |-GCHEM_WRITE_PICKUP     :: Write Geo-Chem pickups (see pkg/gchem)
                0476 C/\  | | | | |-SEAICE_WRITE_PICKUP    :: Write SeaIce pickups  (see pkg/seaice)
                0477 C/\  | | | | |-STREAMICE_WRITE_PICKUP :: Write StreamIce pickups (pkg/streamice)
                0478 C/\  | | | | |-SHELFICE_WRITE_PICKUP :: Write ShelfIce pickups (pkg/shelfice)
                0479 C/\  | | | | |-THSICE_WRITE_PICKUP    :: Write ThSIce pickups  (see pkg/thsice)
                0480 C/\  | | | | |-LAND_WRITE_PICKUP      :: Write Land   pickups  (see pkg/land)
                0481 C/\  | | | | |-ATM_PHYS_WRITE_PICKUP  :: Write Atm-Phys pickups (pkg/atm_phys)
                0482 C/\  | | | | |-FIZHI_WRITE_PICKUP     :: Write Fizhi pickups   (see pkg/fizhi)
                0483 C/\  | | | | |-FIZHI_WRITE_VEGTILES   :: Write Fizhi VegTiles  (see pkg/fizhi)
                0484 C/\  | | | | |-FIZHI_WRITE_DATETIME   :: Write Fizhi DateTime  (see pkg/fizhi)
                0485 C/\  | | | | |-CPL_WRITE_PICKUP       :: Write Coupling-Interface pickups
                0486 C/\  | | | | |-MYPACKAGE_WRITE_PICKUP :: Write pkg/mypackage pickups
                0487 C/\  | | | |
                0488 C/\  | | | |-WRITE_PICKUP          :: Write main model pickup files.
                0489 C/\  | | |
                0490 C/\  | | |-AUTODIFF_INADMODE_SET   :: Set/reset some adjoint flags
63ef2a2a83 Jean*0491 C    | |
5dddee4ea2 Jean*0492 C<===|=| **************************
                0493 C<===|=| END MAIN TIMESTEPPING LOOP
                0494 C<===|=| **************************
63ef2a2a83 Jean*0495 C    | |
37e373688b Mart*0496 C    | |-COST_AVERAGESFIELDS :: Time-averaged Cost function terms (see pkg/cost)
                0497 C    | |-PROFILES_INLOOP     :: ( see pkg/profiles )
                0498 C    | |-COST_FINAL          :: Cost function package (see pkg/cost)
5dddee4ea2 Jean*0499 C    |
37e373688b Mart*0500 C    |-CTRL_PACK       :: Control vector support package (see pkg/ctrl)
5dddee4ea2 Jean*0501 C    |
37e373688b Mart*0502 C    |-GRDCHK_MAIN     :: Gradient check package (see pkg/grdchk)
5dddee4ea2 Jean*0503 C    |
63ef2a2a83 Jean*0504 C    |-TIMER_PRINTALL  :: Computational timing summary
                0505 C    |
                0506 C    |-COMM_STATS      :: Summarise inter-proc and inter-thread communication
37e373688b Mart*0507 C    |                 :: events.
5dddee4ea2 Jean*0508 C \ev
                0509 C
                0510 CEOI
                0511 
                0512 #include "PACKAGES_CONFIG.h"
                0513 #include "CPP_OPTIONS.h"
63ef2a2a83 Jean*0514 #include "AD_CONFIG.h"
5a3db3393c Patr*0515 #ifdef ALLOW_OPENAD
                0516 # include "OPENAD_OPTIONS.h"
                0517 #endif
9c28eed2ec Jean*0518 #ifdef ALLOW_AUTODIFF
                0519 # include "AUTODIFF_OPTIONS.h"
                0520 #endif
                0521 #ifdef ALLOW_CTRL
                0522 # include "CTRL_OPTIONS.h"
                0523 #endif
63ef2a2a83 Jean*0524 #ifdef ALLOW_STREAMICE
                0525 # include "STREAMICE_OPTIONS.h"
                0526 #endif
5dddee4ea2 Jean*0527 
                0528 CBOP
                0529 C     !ROUTINE: THE_MODEL_MAIN
                0530 
                0531 C     !INTERFACE:
                0532       SUBROUTINE THE_MODEL_MAIN(myThid)
                0533 
                0534 C     !DESCRIPTION: \bv
                0535 C     *==========================================================*
                0536 C     | SUBROUTINE THE_MODEL_MAIN
                0537 C     | o Master controlling routine for model using the MITgcm
                0538 C     |   UV parallel wrapper.
                0539 C     *==========================================================*
                0540 C     | THE_MODEL_MAIN is invoked by the MITgcm UV parallel
                0541 C     | wrapper with a single integer argument "myThid". This
                0542 C     | variable identifies the thread number of an instance of
                0543 C     | THE_MODEL_MAIN. Each instance of THE_MODEL_MAIN works
                0544 C     | on a particular region of the models domain and
                0545 C     | synchronises with other instances as necessary. The
                0546 C     | routine has to "understand" the MITgcm parallel
                0547 C     | environment and the numerical algorithm. Editing this
                0548 C     | routine is best done with some knowledge of both aspects.
                0549 C     | Notes
                0550 C     | =====
                0551 C     | C*P* comments indicating place holders for which code is
                0552 C     |      presently being developed.
                0553 C     *==========================================================*
                0554 C     \ev
                0555 
                0556 C     !CALLING SEQUENCE:
                0557 C     THE_MODEL_MAIN()
                0558 C       |
                0559 C       |
63ef2a2a83 Jean*0560 C       |--INITIALISE_FIXED
5dddee4ea2 Jean*0561 C       |   o Set model configuration (fixed arrays)
                0562 C       |     Topography, hydrography, timestep, grid, etc..
                0563 C       |
                0564 C       |--CTRL_UNPACK      o Derivative mode. Unpack control vector.
                0565 C       |
                0566 C       |--ADTHE_MAIN_LOOP  o Main timestepping loop for combined
                0567 C       |                     prognostic and reverse mode integration.
                0568 C       |
                0569 C       |--THE_MAIN_LOOP    o Main timestepping loop for pure prognostic
                0570 C       |                     integration.
                0571 C       |
63ef2a2a83 Jean*0572 C       |--CTRL_PACK        o Derivative mode. Unpack control vector.
                0573 C       |
                0574 C       |--GRDCHK_MAIN      o Gradient check control routine.
5dddee4ea2 Jean*0575 C       |
                0576 C       |--TIMER_PRINTALL   o Print out timing statistics.
                0577 C       |
                0578 C       |--COMM_STATS       o Print out communication statistics.
                0579 
                0580 C     !USES:
63ef2a2a83 Jean*0581       IMPLICIT NONE
5dddee4ea2 Jean*0582 
63ef2a2a83 Jean*0583 C     == Global variables ===
                0584 C -->> OpenAD
5dddee4ea2 Jean*0585       use OAD_active
                0586       use OAD_rev
                0587       use OAD_tape
                0588       use OAD_cp
                0589 #include "cost.h"
63ef2a2a83 Jean*0590 C <<-- OpenAD
5dddee4ea2 Jean*0591 #include "SIZE.h"
                0592 #include "EEPARAMS.h"
                0593 #include "PARAMS.h"
                0594 #include "DYNVARS.h"
5e4cf2f493 Patr*0595 #include "FFIELDS.h"
5dddee4ea2 Jean*0596 
                0597 #ifdef ALLOW_CTRL
5cf4364659 Mart*0598 # include "CTRL_SIZE.h"
4d72283393 Mart*0599 # include "CTRL.h"
65754df434 Mart*0600 # include "OPTIMCYCLE.h"
81a1cb3057 Patr*0601 # include "CTRL_GENARR.h"
5dddee4ea2 Jean*0602 #endif
                0603 
                0604 C     !INPUT/OUTPUT PARAMETERS:
                0605 C     == Routine arguments ==
63ef2a2a83 Jean*0606 C     myThid :: Thread number for this instance of the routine.
5dddee4ea2 Jean*0607       INTEGER myThid
                0608 
4e4ad91a39 Jean*0609 C     !FUNCTIONS:
                0610       INTEGER  ILNBLNK
                0611       EXTERNAL ILNBLNK
                0612 
5dddee4ea2 Jean*0613 C     !LOCAL VARIABLES:
                0614 C     == Local variables ==
63ef2a2a83 Jean*0615 C     Note: Under the multi-threaded model myIter and myTime are local
                0616 C           variables passed around as routine arguments.
                0617 C           Although this is fiddly it saves the need to impose
                0618 C           additional synchronisation points when they are updated.
                0619 C     myTime :: Time counter for this thread
                0620 C     myIter :: Iteration counter for this thread
                0621       INTEGER myIter
                0622       _RL     myTime
                0623       LOGICAL exst
                0624       LOGICAL lastdiva
                0625 C -->> OpenAD
cda1c18f72 Jean*0626       _RL foo(1-OLx:sNx+OLx,1-OLy:sNy+OLy,Nr,nSx,nSy)
4e4ad91a39 Jean*0627       _RL foo2D(1-OLx:sNx+OLx,1-OLy:sNy+OLy,nSx,nSy)
5dddee4ea2 Jean*0628       CHARACTER*(10) suff
02068b0da7 Oliv*0629       CHARACTER*(MAX_LEN_FNAM) fname
5dddee4ea2 Jean*0630 C     Temprarily change precision to agree with ctrlprec
                0631       INTEGER tmpprec
5e4cf2f493 Patr*0632       INTEGER ik, il
5083edeb33 Jean*0633 #ifdef OAD_DEBUG
                0634       INTEGER i1, i2, i3, i4, i5
                0635 #endif
63ef2a2a83 Jean*0636 C <<-- OpenAD
5dddee4ea2 Jean*0637 CEOP
                0638 
63ef2a2a83 Jean*0639 C--   set default:
                0640       exst     = .TRUE.
                0641       lastdiva = .TRUE.
                0642 C -->> OpenAD
                0643 C-    Set the execution mode
5dddee4ea2 Jean*0644       our_rev_mode%arg_store=.FALSE.
                0645       our_rev_mode%arg_restore=.FALSE.
                0646       our_rev_mode%res_store=.FALSE.
                0647       our_rev_mode%res_restore=.FALSE.
                0648       our_rev_mode%plain=.TRUE.
                0649       our_rev_mode%tape=.FALSE.
                0650       our_rev_mode%adjoint=.FALSE.
                0651       our_rev_mode%switchedToCheckpoint=.FALSE.
63ef2a2a83 Jean*0652 C-    Initialize the tape
4e4ad91a39 Jean*0653       CALL OAD_TAPE_INIT()
63ef2a2a83 Jean*0654 C-    Initialize the checkpoint areas
4e4ad91a39 Jean*0655       CALL CP_INIT()
63ef2a2a83 Jean*0656 C <<-- OpenAD
                0657 
                0658 #ifdef ALLOW_PETSC
4e4ad91a39 Jean*0659       CALL STREAMICE_INITIALIZE_PETSC
63ef2a2a83 Jean*0660 #endif
5dddee4ea2 Jean*0661 
                0662 #ifdef ALLOW_DEBUG
                0663       IF (debugMode) CALL DEBUG_ENTER('THE_MODEL_MAIN',myThid)
                0664 #endif
                0665 
                0666 #if defined(USE_PAPI) || defined(USE_PCL_FLOPS_SFP) || defined(USE_PCL_FLOPS) || defined(USE_PCL)
                0667       CALL TIMER_CONTROL('','INIT','THE_MODEL_MAIN',myThid)
                0668 #endif
                0669 C--   This timer encompasses the whole code
                0670       CALL TIMER_START('ALL                    [THE_MODEL_MAIN]',myThid)
                0671 
                0672 #ifdef ALLOW_DEBUG
                0673       IF (debugMode) CALL DEBUG_CALL('INITIALISE_FIXED',myThid)
                0674 #endif
                0675 C--   Set model configuration (fixed arrays)
                0676       CALL TIMER_START('INITIALISE_FIXED       [THE_MODEL_MAIN]',myThid)
63ef2a2a83 Jean*0677 C -->> OpenAD
                0678 c     CALL INITIALISE_FIXED( myThid )
5dddee4ea2 Jean*0679       CALL OpenAD_INITIALISE_FIXED( myThid )
63ef2a2a83 Jean*0680 C <<-- OpenAD
5dddee4ea2 Jean*0681       CALL TIMER_STOP ('INITIALISE_FIXED       [THE_MODEL_MAIN]',myThid)
                0682 
63ef2a2a83 Jean*0683       myTime = startTime
                0684       myIter = nIter0
5dddee4ea2 Jean*0685 
                0686 #if ( defined (ALLOW_ADMTLM) )
63ef2a2a83 Jean*0687 
5dddee4ea2 Jean*0688       STOP 'should never get here; ADMTLM_DSVD calls ADMTLM_DRIVER'
63ef2a2a83 Jean*0689 
5dddee4ea2 Jean*0690 #elif ( defined (ALLOW_AUTODIFF))
                0691 
37e373688b Mart*0692 # ifdef ALLOW_CTRL
5dddee4ea2 Jean*0693 # ifndef EXCLUDE_CTRL_PACK
48e4487714 Gael*0694       IF (useCTRL) THEN
4e4ad91a39 Jean*0695          INQUIRE( file='costfinal', exist=exst )
5dddee4ea2 Jean*0696          IF ( .NOT. exst ) THEN
63ef2a2a83 Jean*0697             IF ( (optimcycle.NE.0 .OR. .NOT.doinitxx)
                0698      &           .AND. doMainUnpack ) THEN
                0699                CALL TIMER_START('CTRL_UNPACK   [THE_MODEL_MAIN]',myThid)
                0700                CALL CTRL_UNPACK( .TRUE. , myThid )
                0701                CALL TIMER_STOP ('CTRL_UNPACK   [THE_MODEL_MAIN]',myThid)
5dddee4ea2 Jean*0702             ENDIF
                0703          ENDIF
48e4487714 Gael*0704       ENDIF
5dddee4ea2 Jean*0705 # endif /* EXCLUDE_CTRL_PACK */
48e4487714 Gael*0706 # endif /* ALLOW_CTRL */
5dddee4ea2 Jean*0707 
                0708 # ifdef ALLOW_COST
63ef2a2a83 Jean*0709       CALL COST_DEPENDENT_INIT ( myThid )
5dddee4ea2 Jean*0710 # endif
                0711 
37e373688b Mart*0712 # if defined( ALLOW_TANGENTLINEAR_RUN )
63ef2a2a83 Jean*0713 
                0714 #  ifdef ALLOW_DEBUG
                0715       IF (debugMode) CALL DEBUG_CALL('G_THE_MAIN_LOOP',myThid)
                0716 #  endif
                0717       CALL TIMER_START('G_THE_MAIN_LOOP           [TANGENT RUN]',myThid)
                0718       CALL G_THE_MAIN_LOOP ( myTime, myIter, myThid )
                0719       CALL TIMER_STOP ('G_THE_MAIN_LOOP           [TANGENT RUN]',myThid)
                0720 
37e373688b Mart*0721 # elif defined( ALLOW_ADJOINT_RUN )
63ef2a2a83 Jean*0722 
5dddee4ea2 Jean*0723 #  ifdef ALLOW_DIVIDED_ADJOINT
63ef2a2a83 Jean*0724 C-- The following assumes the TAF option '-pure'
4e4ad91a39 Jean*0725       INQUIRE( file='costfinal', exist=exst )
5dddee4ea2 Jean*0726       IF ( .NOT. exst) THEN
63ef2a2a83 Jean*0727 #   ifdef ALLOW_DEBUG
                0728          IF (debugMode) CALL DEBUG_CALL('MDTHE_MAIN_LOOP',myThid)
                0729 #   endif
                0730          CALL TIMER_START('MDTHE_MAIN_LOOP            [MD RUN]', myThid)
                0731          CALL MDTHE_MAIN_LOOP ( myTime, myIter, myThid )
                0732          CALL TIMER_STOP ('MDTHE_MAIN_LOOP            [MD RUN]', myThid)
                0733          CALL COST_FINAL_STORE ( myThid, lastdiva )
5dddee4ea2 Jean*0734       ELSE
63ef2a2a83 Jean*0735 #   ifdef ALLOW_DEBUG
                0736          IF (debugMode) CALL DEBUG_CALL('ADTHE_MAIN_LOOP',myThid)
                0737 #   endif
                0738          CALL TIMER_START('ADTHE_MAIN_LOOP       [ADJOINT RUN]', myThid)
                0739          CALL ADTHE_MAIN_LOOP (  myThid )
                0740          CALL TIMER_STOP ('ADTHE_MAIN_LOOP       [ADJOINT RUN]', myThid)
                0741          CALL COST_FINAL_RESTORE ( myThid, lastdiva )
5dddee4ea2 Jean*0742       ENDIF
63ef2a2a83 Jean*0743 
5dddee4ea2 Jean*0744 #  else /* ALLOW_DIVIDED_ADJOINT undef */
9c28eed2ec Jean*0745 #   ifndef ALLOW_OPENAD
                0746 #    ifdef ALLOW_DEBUG
63ef2a2a83 Jean*0747       IF (debugMode) CALL DEBUG_CALL('ADTHE_MAIN_LOOP',myThid)
9c28eed2ec Jean*0748 #    endif
63ef2a2a83 Jean*0749       CALL TIMER_START('ADTHE_MAIN_LOOP          [ADJOINT RUN]', myThid)
                0750       CALL ADTHE_MAIN_LOOP ( myThid )
                0751       CALL TIMER_STOP ('ADTHE_MAIN_LOOP          [ADJOINT RUN]', myThid)
9c28eed2ec Jean*0752 #   else /* ALLOW_OPENAD defined */
63ef2a2a83 Jean*0753 C -->> OpenAD
9c28eed2ec Jean*0754 #   ifdef ALLOW_DEBUG
                0755       IF (debugMode) CALL DEBUG_CALL('THE_MAIN_LOOP',myThid)
                0756 #   endif
5dddee4ea2 Jean*0757       CALL TIMER_START('THE_MAIN_LOOP (F)      [THE_MODEL_MAIN]',myThid)
                0758       our_rev_mode%plain=.FALSE.
                0759       our_rev_mode%tape=.TRUE.
4e4ad91a39 Jean*0760       CALL TIMERATIO()
63ef2a2a83 Jean*0761       CALL OpenAD_THE_MAIN_LOOP( myTime, myIter, myThid )
5dddee4ea2 Jean*0762       CALL TIMER_STOP ('THE_MAIN_LOOP (F)      [THE_MODEL_MAIN]',myThid)
                0763       CALL TIMER_START('THE_MAIN_LOOP (A)      [THE_MODEL_MAIN]',myThid)
                0764       our_rev_mode%arg_store=.FALSE.
                0765       our_rev_mode%arg_restore=.FALSE.
                0766       our_rev_mode%plain=.FALSE.
                0767       our_rev_mode%tape=.FALSE.
                0768       our_rev_mode%adjoint=.TRUE.
f586d0a2c2 Oliv*0769       IF (myProcID .EQ. 0) THEN
                0770         fc%d=1.0
                0771       ENDIF
4e4ad91a39 Jean*0772       CALL TIMERATIO()
63ef2a2a83 Jean*0773       CALL OpenAD_THE_MAIN_LOOP( myTime, myIter, myThid )
4e4ad91a39 Jean*0774       CALL TIMERATIO()
5dddee4ea2 Jean*0775       our_rev_mode%arg_store=.FALSE.
                0776       our_rev_mode%arg_restore=.FALSE.
                0777       our_rev_mode%plain=.TRUE.
                0778       our_rev_mode%tape=.FALSE.
                0779       our_rev_mode%adjoint=.FALSE.
                0780 C     Temporarily change setting of writeBinaryPrec
                0781       tmpprec = writeBinaryPrec
                0782       writeBinaryPrec = ctrlprec
5e4cf2f493 Patr*0783       WRITE(suff,'(I10.10)') optimcycle
6f5d7c9095 Patr*0784 #   ifndef ALLOW_OPENAD_ACTIVE_READ_XYZ
9c28eed2ec Jean*0785 #   ifdef ALLOW_DIFFKR_CONTROL
5e4cf2f493 Patr*0786       foo=diffkr%d
4e4ad91a39 Jean*0787       il=ILNBLNK( xx_diffkr_file )
5e4cf2f493 Patr*0788       write(fname(1:MAX_LEN_FNAM),'(3a)')
                0789      &     'ad',xx_diffkr_file(1:il),'.'
4e4ad91a39 Jean*0790       CALL WRITE_FLD_XYZ_RL(fname,suff,foo,myIter,1)
9c28eed2ec Jean*0791 #   endif
6f5d7c9095 Patr*0792 #   endif /* ALLOW_OPENAD_ACTIVE_READ_XYZ */
                0793 
57b21c88e4 Patr*0794 cc#   ifdef ALLOW_GENARR2D_CONTROL
                0795 cc      do ik = 1, maxCtrlArr2D
                0796 cc        foo2d=xx_genarr2d(:,:,:,:,ik)%d
                0797 cc        write(fname,'(A,I2.2,A)') 'adxx_genarr2d_',ik,'.'
                0798 cc        call write_fld_xy_rl(fname,suff,foo2D,myIter,1)
                0799 cc      enddo
                0800 cc#   endif
                0801 cc#   ifdef ALLOW_GENTIM2D_CONTROL
                0802 cc      do ik = 1, maxCtrlTim2D
                0803 cc        foo2d=xx_gentim2d(:,:,:,:,ik)%d
                0804 cc        write(fname,'(A,I2.2,A)') 'adxx_gentim2d_',ik,'.'
                0805 cc        call write_fld_xy_rl(fname,suff,foo2D,myIter,1)
                0806 cc      enddo
                0807 cc#   endif
                0808 cc#   ifdef ALLOW_GENARR3D_CONTROL
                0809 cc      do ik = 1, maxCtrlArr3D
                0810 cc        foo=xx_genarr3d(:,:,:,:,:,ik)%d
                0811 cc        write(fname,'(A,I2.2,A)') 'adxx_genarr3d_',ik,'.'
                0812 cc        call write_fld_xyz_rl(fname,suff,foo,myIter,1)
                0813 cc      enddo
                0814 cc#   endif
5dddee4ea2 Jean*0815 C     Change back to original writeBinaryPrec
                0816       writeBinaryPrec = tmpprec
                0817       our_rev_mode%plain=.TRUE.
                0818       our_rev_mode%tape=.FALSE.
                0819       our_rev_mode%adjoint=.FALSE.
                0820       CALL TIMER_STOP ('THE_MAIN_LOOP (A)      [THE_MODEL_MAIN]',myThid)
9c28eed2ec Jean*0821 
63ef2a2a83 Jean*0822 C <<-- OpenAD
9c28eed2ec Jean*0823 #   endif /* ALLOW_OPENAD */
                0824 #  endif /* ALLOW_DIVIDED_ADJOINT */
                0825 
                0826 # else /* forward run only within AD setting */
                0827 
                0828 #  ifdef ALLOW_DEBUG
                0829       IF (debugMode) CALL DEBUG_CALL('THE_MAIN_LOOP',myThid)
                0830 #  endif
                0831 C--   Call time stepping loop of full model
                0832       CALL TIMER_START('THE_MAIN_LOOP          [THE_MODEL_MAIN]',myThid)
                0833       CALL THE_MAIN_LOOP( myTime, myIter, myThid )
                0834       CALL TIMER_STOP ('THE_MAIN_LOOP          [THE_MODEL_MAIN]',myThid)
5dddee4ea2 Jean*0835 
63ef2a2a83 Jean*0836 # endif /* forward run only within AD setting */
5dddee4ea2 Jean*0837 
37e373688b Mart*0838 # ifdef ALLOW_CTRL
5dddee4ea2 Jean*0839 # ifndef EXCLUDE_CTRL_PACK
ed672a977d Patr*0840 #  ifdef ALLOW_OPENAD
                0841 cph-- ad hoc fix for OpenAD time stepping counter lagging one step
                0842 cph-- after final adjoint step
                0843       myIter=nIter0
                0844 #  endif
48e4487714 Gael*0845       IF (useCTRL) THEN
63ef2a2a83 Jean*0846       IF ( lastdiva .AND. doMainPack ) THEN
                0847          CALL TIMER_START('CTRL_PACK           [THE_MODEL_MAIN]',myThid)
                0848          CALL CTRL_PACK( .FALSE. , myThid )
                0849          CALL TIMER_STOP ('CTRL_PACK           [THE_MODEL_MAIN]',myThid)
5dddee4ea2 Jean*0850          IF ( ( optimcycle.EQ.0 .OR. (.NOT. doMainUnpack) )
63ef2a2a83 Jean*0851      &        .AND. myIter.EQ.nIter0 ) THEN
                0852             CALL TIMER_START('CTRL_PACK     [THE_MODEL_MAIN]',myThid)
                0853             CALL CTRL_PACK( .TRUE. , myThid )
                0854             CALL TIMER_STOP ('CTRL_PACK     [THE_MODEL_MAIN]',myThid)
5dddee4ea2 Jean*0855          ENDIF
                0856       ENDIF
48e4487714 Gael*0857       ENDIF
5dddee4ea2 Jean*0858 # endif /* EXCLUDE_CTRL_PACK */
48e4487714 Gael*0859 # endif /* ALLOW_CTRL */
5dddee4ea2 Jean*0860 
                0861 # ifdef ALLOW_GRDCHK
                0862       IF ( useGrdchk .AND. lastdiva ) THEN
63ef2a2a83 Jean*0863          CALL TIMER_START('GRDCHK_MAIN         [THE_MODEL_MAIN]',myThid)
                0864          CALL GRDCHK_MAIN( myThid )
                0865          CALL TIMER_STOP ('GRDCHK_MAIN         [THE_MODEL_MAIN]',myThid)
5dddee4ea2 Jean*0866       ENDIF
                0867 # endif
                0868 
                0869 #else /* ALL AD-related undef */
                0870 
63ef2a2a83 Jean*0871 # ifdef ALLOW_DEBUG
5dddee4ea2 Jean*0872       IF (debugMode) CALL DEBUG_CALL('THE_MAIN_LOOP',myThid)
63ef2a2a83 Jean*0873 # endif
5dddee4ea2 Jean*0874 C--   Call time stepping loop of full model
                0875       CALL TIMER_START('THE_MAIN_LOOP          [THE_MODEL_MAIN]',myThid)
63ef2a2a83 Jean*0876       CALL THE_MAIN_LOOP( myTime, myIter, myThid )
5dddee4ea2 Jean*0877       CALL TIMER_STOP ('THE_MAIN_LOOP          [THE_MODEL_MAIN]',myThid)
                0878 
                0879 #endif /* ALLOW_TANGENTLINEAR_RUN ALLOW_ADJOINT_RUN ALLOW_ADMTLM */
                0880 
63ef2a2a83 Jean*0881 #ifdef ALLOW_PETSC
4e4ad91a39 Jean*0882       CALL STREAMICE_FINALIZE_PETSC
5dddee4ea2 Jean*0883 #endif
                0884 
                0885 #ifdef ALLOW_MNC
                0886       IF (useMNC) THEN
                0887 C       Close all open NetCDF files
63ef2a2a83 Jean*0888         _BEGIN_MASTER( myThid )
5dddee4ea2 Jean*0889         CALL MNC_FILE_CLOSE_ALL( myThid )
63ef2a2a83 Jean*0890         _END_MASTER( myThid )
5dddee4ea2 Jean*0891       ENDIF
                0892 #endif
                0893 
                0894 C--   This timer encompasses the whole code
                0895       CALL TIMER_STOP ('ALL                    [THE_MODEL_MAIN]',myThid)
                0896 
                0897 C--   Write timer statistics
                0898       IF ( myThid .EQ. 1 ) THEN
                0899        CALL TIMER_PRINTALL( myThid )
                0900        CALL COMM_STATS
                0901       ENDIF
                0902 
                0903 C--   Check threads synchronization :
                0904       CALL BAR_CHECK( 9, myThid )
                0905 
                0906 #ifdef ALLOW_DEBUG
                0907       IF (debugMode) CALL DEBUG_LEAVE('THE_MODEL_MAIN',myThid)
                0908 #endif
                0909 
                0910       RETURN
                0911       END