Changes between Initial Version and Version 1 of ImplementAnalysis_3DEnVar_classical


Ignore:
Timestamp:
Dec 9, 2021, 12:31:08 PM (3 years ago)
Author:
lnerger
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • ImplementAnalysis_3DEnVar_classical

    v1 v1  
     1= Implementation of the Analysis Step for 3D Ensemble Var without using OMI =
     2
     3{{{
     4#!html
     5<div class="wiki-toc">
     6<h4>Implementation Guide</h4>
     7<ol><li><a href="ImplementationGuide">Main page</a></li>
     8<li><a href="AdaptParallelization">Adaptation of the parallelization</a></li>
     9<li><a href="InitPdaf">Initialization of PDAF</a></li>
     10<li><a href="ModifyModelforEnsembleIntegration">Modifications for ensemble integration</a></li>
     11<li><a href="ImplementationofAnalysisStep">Implementation of the analysis step</a></li>
     12<ol>
     13<li><a href="ImplementAnalysisestkf">Implementation for ESTKF</a></li>
     14<li><a href="ImplementAnalysislestkf">Implementation for LESTKF</a></li>
     15<li><a href="ImplementAnalysisetkf">Implementation for ETKF</a></li>
     16<li>Implementation for LETKF</li>
     17<li><a href="ImplementAnalysisseik">Implementation for SEIK</a></li>
     18<li><a href="ImplementAnalysislseik">Implementation for LSEIK</a></li>
     19<li><a href="ImplementAnalysisseek">Implementation for SEEK</a></li>
     20<li><a href="ImplementAnalysisenkf">Implementation for EnKF</a></li>
     21<li><a href="ImplementAnalysislenkf">Implementation for LEnKF</a></li>
     22<li><a href="ImplementAnalysisnetf">Implementation for NETF</a></li>
     23<li><a href="ImplementAnalysislnetf">Implementation for LNETF</a></li>
     24<li><a href="ImplementAnalysispf">Implementation for PF</a></li>
     25<li><a href="ImplementAnalysis_3dVar_classical">Implementation for 3D-Var</a></li>
     26<li>Implementation for 3D Ensemble Var</li>
     27<li><a href="ImplementAnalysis_Hyb3DVar_classical">Implementation for Hybrid 3D-Var</a></li>
     28</ol>
     29<li><a href="AddingMemoryandTimingInformation">Memory and timing information</a></li>
     30<li><a href="EnsembleGeneration">Ensemble Generation</a></li>
     31<li><a href="DataAssimilationDiagnostics">Diagnostics</a></li>
     32</ol>
     33</div>
     34}}}
     35[[PageOutline(2-3,Contents of this page)]]
     36
     37== Overview ==
     38
     39With Version 2.0 with introduced 3D variational assimilation methods to PDAF. There are genenerally three different variants: parameterized 3D-Var, 3D Ensemble Var, and hybrid (parameterized + ensemble) 3D-Var.
     40
     41This page describes the implementation of the analysis step for the 3D Ensemble Var in the classical way (without using PDAF-OMI).
     42
     43For the analysis step of 3D-Var we need different operations related to the observations. These operations are requested by PDAF by calling user-supplied routines. Intentionally, the operations are split into separate routines in order to keep the operations rather elementary as this procedure should simplify the implementation. The names of the required routines are specified in the call to the routine `PDAF_assimilate_3dvar` in the fully-parallel implementation (or `PDAF_put_state_3dvar` for the 'flexible' implementation) described below. With regard to the parallelization, all these routines (except `U_collect_state`) are executed by the filter processes (`filterpe=.true.`) only.
     44
     45For 3D Ensemble Var the ensemble is used to represent the background covariance matrix '''B'''. This ensemble perturbations need to be transformed by means of an ensemble Kalman filter. PDAF uses for this the error-subspace transform filter ESTKF. There are two variants: The first uses the localized filter LESTKF, while the second uses the global filter ESTKF.
     46
     47For completeness we discuss here all user-supplied routines that are specified in the interface to `PDAF_assimilate_en3dvar_*` and `PDAF_put_state_en3dvar_*`. Thus, some of the user-supplied routines that are explained on the page describing the modification of the model code for the ensemble integration are repeated here.
     48
     49
     50== Analysis Routines ==
     51
     52The general aspects of the filter (or solver) specific routines `PDAF_assimilate_*` have been described on the page [ModifyModelforEnsembleIntegration Modification of the model code for the ensemble integration] and its sub-page on [InsertAnalysisStep inserting the analysis step]. The routine is used in the fully-parallel implementation variant of the data assimilation system. When the 'flexible' implementation variant is used, the routines `PDAF_put_state_*` is used as described further below. Here, we list the full interface of the routine. Subsequently, the user-supplied routines specified in the call is explained.
     53
     54There are two variants that either compute the transformataion of the ensemble transformation using the local LESTKF method, or the global ESTKF.
     55
     56=== `PDAF_assimilate_en3dvar_lestkf` ===
     57
     58This routine is called for the case of transforming the ensemble perturbations using the local LESTKF.
     59
     60The interface is:
     61{{{
     62SUBROUTINE PDAF_assimilate_en3dvar_lestkf(U_collect_state, U_distribute_state, &
     63                                 U_init_dim_obs, U_obs_op, U_init_obs, U_prodRinvA, &
     64                                 U_cvt_ens, U_cvt_adj_ens, U_obs_op_lin, U_obs_op_adj, &
     65                                 U_init_dim_obs_f, U_obs_op_f, U_init_obs_f, U_init_obs_l, U_prodRinvA_l, &
     66                                 U_init_n_domains_p, U_init_dim_l, U_init_dim_obs_l, U_g2l_state, U_l2g_state, &
     67                                 U_g2l_obs, U_init_obsvar, U_init_obsvar_l, &
     68                                 U_prepoststep, U_next_observation, outflag)
     69}}}
     70with the following arguments:
     71 * [#U_collect_statecollect_state_pdaf.F90 U_collect_state]: The name of the user-supplied routine that initializes a state vector from the array holding the ensemble of model states from the model fields. This is basically the inverse operation to `U_distribute_state` used in `PDAF_get_state` as well as here.
     72 * [#U_distribute_statedistribute_state_pdaf.F90 U_distribute_state]:  The name of a user supplied routine that initializes the model fields from the array holding the ensemble of model state vectors.
     73 * [#U_init_dim_obsinit_dim_obs_pdaf.F90 U_init_dim_obs]: The name of the user-supplied routine that provides the size of observation vector
     74 * [#U_obs_opobs_op_pdaf.F90 U_obs_op]: The name of the user-supplied routine that acts as the observation operator on some state vector
     75 * [#U_init_obsinit_obs_pdaf.F90 U_init_obs]: The name of the user-supplied routine that initializes the vector of observations
     76 * [#U_prodRinvAprodrinva_pdaf.F90 U_prodRinvA]: The name of the user-supplied routine that computes the product of the inverse of the observation error covariance matrix with some matrix provided to the routine by PDAF. This operation occurs during the analysis step of the ETKF.
     77 * [#U_cvt_enscvt_ens_pdaf.F90 U_cvt_ens]: The name of the user-supplied routine that applies the ensemble control-vector transformation (square-root of the B-matrix) on some control vector to obtain a state vector.
     78 * [#U_cvt_adj_enscvt_adj_ens_pdaf.F90 U_cvt_adj_ens]: The name of the user-supplied routine that applies the adjoint ensemble control-vector transformation (with square-root of the B-matrix) on some state vector to obtain the control vector.
     79 * [#U_obs_op_linobs_op_lin_pdaf.F90 U_obs_op_lin]: The name of the user-supplied routine that acts as the linearized observation operator on some state vector
     80 * [#U_obs_op_adjobs_op_adj_pdaf.F90 U_obs_op_adj]: The name of the user-supplied routine that acts as the adjoint observation operator on some state vector
     81 * [#U_init_dim_obs_finit_dim_obs_f_pdaf.F90 U_init_dim_obs_f]: The name of the user-supplied routine that provides the size of the full observation vector
     82 * [#U_obs_op_fobs_op_f_pdaf.F90 U_obs_op_f]: The name of the user-supplied routine that acts as the full observation operator on some state vector
     83 * [#U_init_obs_finit_obs_f_pdaf.F90 U_init_obs_f]: The name of the user-supplied routine that initializes the full vector of observations
     84 * [#U_init_obs_linit_obs_l_pdaf.F90 U_init_obs_l]: The name of the user-supplied routine that initializes the vector of observations for a local analysis domain
     85 * [#U_prepoststepprepoststep_ens_pdaf.F90 U_prepoststep]: The name of the pre/poststep routine as in `PDAF_get_state`
     86 * [#U_prodRinvA_lprodrinva_l_pdaf.F90 U_prodRinvA_l]: The name of the user-supplied routine that computes the product of the inverse of the observation error covariance matrix with some matrix provided to the routine by PDAF.
     87 * [#U_init_n_domainsinit_n_domains_pdaf.F90 U_init_n_domains]: The name of the routine that provides the number of local analysis domains
     88 * [#U_init_dim_linit_dim_l_pdaf.F90 U_init_dim_l]: The name of the routine that provides the state dimension for a local analysis domain
     89 * [#U_init_dim_obs_linit_dim_obs_l_pdaf.F90 U_init_dim_obs_l]: The name of the routine that initializes the size of the observation vector for a local analysis domain
     90 * [#U_g2l_stateg2l_state_pdaf.F90 U_g2l_state]: The name of the routine that initializes a local state vector from the global state vector
     91 * [#U_l2g_statel2g_state_pdaf.F90 U_l2g_state]: The name of the routine that initializes the corresponding part of the global state vector from the the provided local state vector
     92 * [#U_g2l_obsg2l_obs_pdaf.F90 U_g2l_obs]: The name of the routine that initializes a local observation vector from a full observation vector
     93 * [#U_init_obsvarinit_obsvar_pdaf.F90 U_init_obsvar]: The name of the user-supplied routine that provides a global mean observation error variance (This routine will only be executed, if an adaptive forgetting factor is used)
     94 * [#U_init_obsvar_linit_obsvar_l_pdaf.F90 U_init_obsvar_l]: The name of the user-supplied routine that provides a mean observation error variance for the local analysis domain (This routine will only be executed, if a local adaptive forgetting factor is used)
     95 * [#U_prepoststepprepoststep_ens_pdaf.F90 U_prepoststep]: The name of the pre/poststep routine as in `PDAF_get_state`
     96 * [#U_next_observationnext_observation.F90 U_next_observation]: The name of a user supplied routine that initializes the variables `nsteps`, `timenow`, and `doexit`. The same routine is also used in `PDAF_get_state`.
     97 * `status`: The integer status flag. It is zero, if `PDAFomi_assimilate_global` is exited without errors.
     98
     99
     100
     101=== `PDAFomi_assimilate_en3dvar_estkf` ===
     102
     103This routine is called for the case of transforming the ensemble perturbations using the global ESTKF. 
     104
     105The interface is:
     106{{{
     107SUBROUTINE PDAF_assimilate_en3dvar_estkf(U_collect_state, U_distribute_state, &
     108                                 U_init_dim_obs, U_obs_op, U_init_obs, U_prodRinvA, &
     109                                 U_cvt_ens, U_cvt_adj_ens, U_obs_op_lin, U_obs_op_adj, &
     110                                 U_init_obsvar, U_prepoststep, U_next_observation, outflag)
     111}}}
     112with the following arguments:
     113 * [#U_collect_statecollect_state_pdaf.F90 U_collect_state]: The name of the user-supplied routine that initializes a state vector from the array holding the ensemble of model states from the model fields. This is basically the inverse operation to `U_distribute_state` used in `PDAF_get_state` as well as here.
     114 * [#U_distribute_statedistribute_state_pdaf.F90 U_distribute_state]:  The name of a user supplied routine that initializes the model fields from the array holding the ensemble of model state vectors.
     115 * [#U_init_dim_obsinit_dim_obs_pdaf.F90 U_init_dim_obs]: The name of the user-supplied routine that provides the size of observation vector
     116 * [#U_obs_opobs_op_pdaf.F90 U_obs_op]: The name of the user-supplied routine that acts as the observation operator on some state vector
     117 * [#U_init_obsinit_obs_pdaf.F90 U_init_obs]: The name of the user-supplied routine that initializes the vector of observations
     118 * [#U_prodRinvAprodrinva_pdaf.F90 U_prodRinvA]: The name of the user-supplied routine that computes the product of the inverse of the observation error covariance matrix with some matrix provided to the routine by PDAF. This operation occurs during the analysis step of the ETKF.
     119 * [#U_cvt_enscvt_ens_pdaf.F90 U_cvt_ens]: The name of the user-supplied routine that applies the ensemble control-vector transformation (square-root of the B-matrix) on some control vector to obtain a state vector.
     120 * [#U_cvt_adj_enscvt_adj_ens_pdaf.F90 U_cvt_adj_ens]: The name of the user-supplied routine that applies the adjoint ensemble control-vector transformation (with square-root of the B-matrix) on some state vector to obtain the control vector.
     121 * [#U_obs_op_linobs_op_lin_pdaf.F90 U_obs_op_lin]: The name of the user-supplied routine that acts as the linearized observation operator on some state vector
     122 * [#U_obs_op_adjobs_op_adj_pdaf.F90 U_obs_op_adj]: The name of the user-supplied routine that acts as the adjoint observation operator on some state vector
     123 * [#U_init_obsvarinit_obsvar_pdaf.F90 U_init_obsvar]: The name of the user-supplied routine that provides a mean observation error variance to PDAF (This routine will only be executed, if an adaptive forgetting factor is used)
     124 * [#U_prepoststepprepoststep_ens_pdaf.F90 U_prepoststep]: The name of the pre/poststep routine as in `PDAF_get_state`
     125 * [#U_next_observationnext_observation.F90 U_next_observation]: The name of a user supplied routine that initializes the variables `nsteps`, `timenow`, and `doexit`. The same routine is also used in `PDAF_get_state`.
     126 * `status`: The integer status flag. It is zero, if `PDAFomi_assimilate_global` is exited without errors.
     127
     128Note that the interface of `PDAFomi_assimilate_en3dvar_estkf` is identical to that of `PDAFomi_assimilate_3dvar` apart from using the routines `U_cvt_ens` and `U_cvt_adj_ens` in case of the ensemble variational method.
     129
     130
     131=== `PDAFomi_put_state_en3dvar_lestkf` ===
     132
     133When the 'flexible' implementation variant is chosen for the assimilation system, the routine `PDAFomi_put_state_*` has to be used instead of `PDAFomi_assimilate_*`. The general aspects of the filter specific routines `PDAF_put_state_*` have been described on the page [ModifyModelforEnsembleIntegration Modification of the model code for the ensemble integration]. The interface of the routine is identical with that of `PDAF_assimilate_*` with the exception the specification of the user-supplied routines `U_distribute_state` and `U_next_observation` are missing.
     134
     135The interface when using one of the global filters is the following:
     136{{{
     137  SUBROUTINE PDAFomi_put_state_en3dvar_lestkf(U_collect_state, &
     138                                 U_init_dim_obs_pdafomi, U_obs_op_pdafomi, &
     139                                 U_cvt_ens, U_cvt_adj_ens, U_obs_op_lin_pdafomi, U_obs_op_adj_pdafomi, &
     140                                 U_init_n_domains_p, U_init_dim_l, U_init_dim_obs_l_pdafomi, &
     141                                 U_g2l_state, U_l2g_state, U_prepoststep, outflag)
     142}}}
     143
     144=== `PDAFomi_put_state_en3dvar_estkf` ===
     145
     146The interface of this routine is analogous to that of `PDAFomi_assimilate_en3dvar_estkf'. Thus it is identical to this routine with the exception the specification of the user-supplied routines `U_distribute_state` and `U_next_observation` are missing.
     147
     148The interface when using one of the global filters is the following:
     149{{{
     150  SUBROUTINE PDAFomi_put_state_en3dvar_estkf(U_collect_state, &
     151                                 U_init_dim_obs_pdafomi, U_obs_op_pdafomi, &
     152                                 U_cvt_ens, U_cvt_adj_ens, U_obs_op_lin_pdafomi, U_obs_op_adj_pdafomi, &
     153                                 U_prepoststep, outflag)
     154}}}
     155
     156== User-supplied routines ==
     157
     158Here all user-supplied routines are described that are required in the call to `PDAFomi_assimilate_3dvar`. For some of the generic routines, we link to the page on [ModifyModelforEnsembleIntegration modifying the model code for the ensemble integration].
     159
     160To indicate user-supplied routines we use the prefix `U_`. In the template directory `templates/` as well as in the tutorial implementations in `tutorial/` these routines exist without the prefix, but with the extension `_pdaf.F90`. The user-routines relating to OMI are collected in the file `callback_obs_pdafomi.F90`. In the section titles below we provide the name of the template file in parentheses.
     161
     162In the subroutine interfaces some variables appear with the suffix `_p`. This suffix indicates that the variable is particular to a model sub-domain, if a domain decomposed model is used. Thus, the value(s) in the variable will be different for different model sub-domains.
     163
     164
     165=== `U_collect_state` (collect_state_pdaf.F90) ===
     166
     167This routine is independent of the filter algorithm used.
     168
     169See the page on [InsertAnalysisStep#U_collect_statecollect_state_pdaf.F90 inserting the analysis step] for the description of this routine.
     170
     171
     172=== `U_distribute_state` (distribute_state_pdaf.F90) ===
     173
     174This routine is independent of the filter algorithm used.
     175
     176See the page on [InsertAnalysisStep#U_distribute_statedistribute_state_pdaf.F90 inserting the analysis step] for the description of this routine.
     177
     178
     179
     180=== `U_init_dim_obs` (init_dim_obs_pdaf.F90) ===
     181
     182This routine is used by all global filter algorithms (SEEK, SEIK, EnKF, ETKF).
     183
     184The interface for this routine is:
     185{{{
     186SUBROUTINE init_dim_obs(step, dim_obs_p)
     187
     188  INTEGER, INTENT(in)  :: step       ! Current time step
     189  INTEGER, INTENT(out) :: dim_obs_p  ! Dimension of observation vector
     190}}}
     191
     192The routine is called at the beginning of each analysis step.  It has to initialize the size `dim_obs_p` of the observation vector according to the current time step. Without parallelization `dim_obs_p` will be the size for the full model domain. When a domain-decomposed model is used, `dim_obs_p` will be the size of the observation vector for the sub-domain of the calling process.
     193
     194Some hints:
     195 * It can be useful to not only determine the size of the observation vector at this point. One can also already gather information about the locations of the observations, which will be used later, e.g. to implement the observation operator. An array for the locations can be defined in a module like `mod_assimilation` of the example implementation.
     196
     197
     198=== `U_obs_op` (obs_op_pdaf.F90) ===
     199
     200This routine is used by all global filter algorithms (SEEK, SEIK, EnKF, ETKF).
     201
     202The interface for this routine is:
     203{{{
     204SUBROUTINE obs_op(step, dim_p, dim_obs_p, state_p, m_state_p)
     205
     206  INTEGER, INTENT(in) :: step               ! Current time step
     207  INTEGER, INTENT(in) :: dim_p              ! PE-local dimension of state
     208  INTEGER, INTENT(in) :: dim_obs_p          ! Dimension of observed state
     209  REAL, INTENT(in)    :: state_p(dim_p)     ! PE-local model state
     210  REAL, INTENT(out) :: m_state_p(dim_obs_p) ! PE-local observed state
     211}}}
     212
     213The routine is called during the analysis step. It has to perform the operation of the observation operator acting on a state vector that is provided as `state_p`. The observed state has to be returned in `m_state_p`.
     214
     215For a model using domain decomposition, the operation is on the PE-local sub-domain of the model and has to provide the observed sub-state for the PE-local domain.
     216
     217Hint:
     218 * If the observation operator involves a global operation, e.g. some global integration, while using domain-decomposition one has to gather the information from the other model domains using MPI communication.
     219
     220
     221=== `U_init_obs` (init_obs_pdaf.F90) ===
     222
     223This routine is used by all global filter algorithms (SEEK, SEIK, EnKF, ETKF).
     224
     225The interface for this routine is:
     226{{{
     227SUBROUTINE init_obs(step, dim_obs_p, observation_p)
     228
     229  INTEGER, INTENT(in) :: step             ! Current time step
     230  INTEGER, INTENT(in) :: dim_obs_p        ! PE-local dimension of obs. vector
     231  REAL, INTENT(out)   :: observation_p(dim_obs_p) ! PE-local observation vector
     232}}}
     233
     234The routine is called during the analysis step.
     235It has to provide the vector of observations in `observation_p` for the current time step.
     236
     237For a model using domain decomposition, the vector of observations that exist on the model sub-domain for the calling process has to be initialized.
     238
     239
     240
     241=== `U_prodRinvA` (prodrinva_pdaf.F90) ===
     242
     243This routine is used by all filter algorithms that use the inverse of the observation error covariance matrix (SEEK, SEIK, and ETKF).
     244
     245The interface for this routine is:
     246{{{
     247SUBROUTINE prodRinvA(step, dim_obs_p, dim_ens, obs_p, A_p, C_p)
     248
     249  INTEGER, INTENT(in) :: step                ! Current time step
     250  INTEGER, INTENT(in) :: dim_obs_p           ! PE-local dimension of obs. vector
     251  INTEGER, INTENT(in) :: dim_ens             ! Ensemble size
     252  REAL, INTENT(in)    :: obs_p(dim_obs_p)    ! PE-local vector of observations
     253  REAL, INTENT(in)    :: A_p(dim_obs_p, dim_ens) ! Input matrix from analysis routine
     254  REAL, INTENT(out)   :: C_p(dim_obs_p, dim_ens) ! Output matrix
     255}}}
     256
     257The routine is called during the analysis step. In the algorithms the product of the inverse of the observation error covariance matrix with some matrix has to be computed. For the ETKF, this matrix holds the observed part of the ensemble perturbations. The matrix is provided as `A_p`. The product has to be given as `C_p`.
     258
     259For a model with domain decomposition, `A_p` contains the part of the matrix that resides on the model sub-domain of the calling process. The product has to be computed for this sub-domain, too.
     260
     261Hints:
     262 * The routine does not require that the product is implemented as a real matrix-matrix product. Rather, the product can be implemented in its most efficient form. For example, if the observation error covariance matrix is diagonal, only the multiplication of the diagonal with matrix `A_p` has to be implemented.
     263 * The observation vector `obs_p` is provided through the interface for cases where the observation error variance is relative to the actual value of the observations.
     264 * The interface has a difference for SEIK and ETKF: For ETKF the third argument is the ensemble size (`dim_ens`), while for SEIK it is the rank of the covariance matrix (usually ensemble size minus one). In addition, the second dimension of `A_p` and `C_p` has size `dim_ens` for ETKF, while it is `rank` for the SEIK filter.  (Practically, one can usually ignore this difference as the fourth argument of the interface can be named arbitrarily in the routine.)
     265
     266
     267
     268=== `U_cvt_ens` (cvt_ens_pdaf.F90) ===
     269
     270The interface for this routine is:
     271{{{
     272SUBROUTINE cvt_ens_pdaf(iter, dim_p, dim_ens, dim_cv_ens_p, ens_p, cv_p, Vcv_p)
     273
     274  INTEGER, INTENT(in) :: iter               ! Iteration of optimization
     275  INTEGER, INTENT(in) :: dim_p              ! PE-local observation dimension
     276  INTEGER, INTENT(in) :: dim_ens            ! Ensemble size
     277  INTEGER, INTENT(in) :: dim_cv_ens_p       ! Dimension of control vector
     278  REAL, INTENT(in) :: ens_p(dim_p, dim_ens) ! PE-local ensemble
     279  REAL, INTENT(in) :: cv_p(dim_cv_ens_p)    ! PE-local control vector
     280  REAL, INTENT(inout) :: Vcv_p(dim_p)       ! PE-local state increment
     281}}}
     282
     283The routine is called during the analysis step during the iterative minimization of the cost function.
     284It has to apply the control vector transformation to the control vector and return the transformed result vector. Usually this transformation is the multiplication with the square-root of the background error covariance matrix '''B'''. For the 3D Ensemble Var, this square root is usually expressed through the ensemble.
     285
     286If the control vector is decomposed in case of parallelization it first needs to the gathered on each processor and afterwards the transformation is computed on the potentially domain-decomposed state vector.
     287
     288
     289=== `U_cvt_adj` (cvt_adj_pdaf.F90) ===
     290
     291The interface for this routine is:
     292{{{
     293SUBROUTINE cvt_adj_ens_pdaf(iter, dim_p, dim_ens, dim_cv_ens_p, ens_p, Vcv_p, cv_p)
     294
     295  INTEGER, INTENT(in) :: iter                ! Iteration of optimization
     296  INTEGER, INTENT(in) :: dim_p               ! PE-local observation dimension
     297  INTEGER, INTENT(in) :: dim_ens             ! Ensemble size
     298  INTEGER, INTENT(in) :: dim_cv_ens_p        ! PE-local dimension of control vector
     299  REAL, INTENT(in) :: ens_p(dim_p, dim_ens)  ! PE-local ensemble
     300  REAL, INTENT(in)    :: Vcv_p(dim_p)        ! PE-local input vector
     301  REAL, INTENT(inout) :: cv_p(dim_cv_ens_p)  ! PE-local result vector
     302}}}
     303
     304The routine is called during the analysis step during the iterative minimization of the cost function.
     305It has to apply the adjoint control vector transformation to a state vector and return the control vector. Usually this transformation is the multiplication with transpose of the square-root of the background error covariance matrix '''B'''. or the 3D Ensemble Var, this square root is usually expressed through the ensemble.
     306
     307If the state vector is decomposed in case of parallelization one needs to take care that the application of the trasformation is complete. This usually requries a comminucation with MPI_Allreduce to obtain a global sun.
     308
     309
     310
     311=== `U_obs_op_lin_pdafomi` (callback_obs_pdafomi.F90) ===
     312
     313This is a call-back routine for PDAF-OMI applying the linearized observation operator to the state vector. The routine calls a routine from the observation module for each observation type. If the full observation operator is lineaer the same operator can be used here.
     314
     315See the [wiki:OMI_Callback_obs_pdafomi documentation on callback_obs_pdafomi.F90] for more information.
     316
     317
     318=== `U_obs_op_adj_pdafomi` (callback_obs_pdafomi.F90) ===
     319
     320This is a call-back routine for PDAF-OMI applying the adjoint observation operator to some vector inthe observation space. The routine calls a routine from the observation module for each observation type.
     321
     322See the [wiki:OMI_Callback_obs_pdafomi documentation on callback_obs_pdafomi.F90] for more information.
     323
     324
     325
     326=== `U_init_dim_obs_f` (init_dim_obs_f_pdaf.F90) ===
     327
     328This routine is used by all filter algorithms with domain-localization (LSEIK, LETKF) and is independent of the particular algorithm.
     329
     330The interface for this routine is:
     331{{{
     332SUBROUTINE init_dim_obs_f(step, dim_obs_f)
     333
     334  INTEGER, INTENT(in)  :: step       ! Current time step
     335  INTEGER, INTENT(out) :: dim_obs_f  ! Dimension of full observation vector
     336}}}
     337
     338The routine is called at the beginning of each analysis step, before the loop over all local analysis domains is entered.  It has to initialize the size `dim_obs_f` of the full observation vector according to the current time step. For simplicity, `dim_obs_f` can be the size for the global model domain.
     339
     340Some hints:
     341 * It can be useful to not only determine the size of the observation vector at this point. One can also already gather information about the location of the observations, which can be used later, e.g. to implement the observation operator. In addition, one can already prepare an array that holds the full observation vector. This can be used later by `U_init_obs_l` to initialize a local vector of observations by selecting the relevant parts of the full observation vector. The required arrays can be defined in a module like `mod_assimilation`.
     342 * The routine is similar to `init_dim_obs` used in the global filters. However, if the global filter is used with a domain-decomposed model, it only initializes the size of the observation vector for the local model sub-domain. This is different for the local filters, as the local analysis also requires observational data from neighboring model sub-domains. Nonetheless, one can base on an implemented routine `init_dim_obs` to implement `init_dim_obs_f`.
     343
     344=== `U_obs_op_f` (obs_op_f_pdaf.F90) ===
     345
     346This routine is used by all filter algorithms with domain-localization (LSEIK, LETKF) and is independent of the particular algorithm.
     347
     348The interface for this routine is:
     349{{{
     350SUBROUTINE obs_op_f(step, dim_p, dim_obs_f, state_p, m_state_f)
     351
     352  INTEGER, INTENT(in) :: step               ! Current time step
     353  INTEGER, INTENT(in) :: dim_p              ! PE-local dimension of state
     354  INTEGER, INTENT(in) :: dim_obs_f          ! Dimension of the full observed state
     355  REAL, INTENT(in)    :: state_p(dim_p)     ! PE-local model state
     356  REAL, INTENT(out) :: m_state_f(dim_obs_f) ! Full observed state
     357}}}
     358
     359The routine is called during the analysis step, before the loop over the local analysis domain is entered. It has to perform the operation of the observation operator acting on a state vector, which is provided as `state_p`. The observed state has to be returned in `m_state_f`. It is the observed state corresponding to the 'full' observation vector.
     360
     361Hint:
     362 * The routine is similar to `init_dim_obs` used for the global filters. However, with a domain-decomposed model `m_state_f` will contain parts of the state vector from neighboring model sub-domains. To make these parts accessible, some parallel communication will be necessary (The state information for a neighboring model sub-domain, will be in the memory of the process that handles that sub-domain). The example implementation in `testsuite/dummymodel_1d` uses the function `MPI_AllGatherV` for this communication.
     363
     364=== `U_init_obs_f` (init_obs_f_pdaf.F90) ===
     365
     366This routine is used by all filter algorithms with domain-localization (LSEIK, LETKF) and is independent of the particular algorithm.
     367The routine is only called if the globally adaptive forgetting factor is used (`type_forget=1` in the example implementation). For the local filters there is also the alternative to use locally adaptive forgetting factors (`type_forget=2` in the example implementation)
     368
     369The interface for this routine is:
     370{{{
     371SUBROUTINE init_obs_f(step, dim_obs_f, observation_f)
     372
     373  INTEGER, INTENT(in) :: step                     ! Current time step
     374  INTEGER, INTENT(in) :: dim_obs_f                ! Dimension of full observation vector
     375  REAL, INTENT(out)   :: observation_f(dim_obs_f) ! Full observation vector
     376}}}
     377
     378The routine is called during the analysis step before the loop over the local analysis domains is entered. It has to provide the full vector of observations in `observation_f` for the current time step. The caller is the routine that computes an adaptive forgetting factor (PDAF_set_forget).
     379
     380Hints:
     381 * As for the other 'full' routines: While the global counterpart of this routine (`init_obs`) has to initialize the observation vector only for the local model sub-domain, the 'full' routine has to include observations that spatially belong to neighboring model sub-domains. As an easy choice one can simply initialize a vector of all globally available observations.
     382 * If the adaptive forgetting factor is not used, this routine only has to exist. However, no functionality is required.
     383
     384
     385=== `U_init_obs_l` (init_obs_l_pdaf.F90) ===
     386
     387This routine is used by all filter algorithms with domain-localization (LSEIK, LETKF) and is independent of the particular algorithm.
     388
     389The interface for this routine is:
     390{{{
     391SUBROUTINE init_obs_l(domain_p, step, dim_obs_l, observation_l)
     392
     393  INTEGER, INTENT(in) :: domain_p                 ! Current local analysis domain
     394  INTEGER, INTENT(in) :: step                     ! Current time step
     395  INTEGER, INTENT(in) :: dim_obs_l                ! Local dimension of observation vector
     396  REAL, INTENT(out)   :: observation_l(dim_obs_l) ! Local observation vector
     397}}}
     398
     399The routine is called during the analysis step during the loop over the local analysis domain.
     400It has to provide the vector of observations for the analysis in the local analysis domain with index `domain_p` in `observation_l` for the current time step.
     401
     402Hints:
     403 * For parallel efficiency, the LETKF algorithm is implemented in a way that first the full vectors are initialized. These are then restricted to the local analysis domain during the loop over all local analysis domains. Thus, if the full vector of observations has been initialized before `U_init_obs_l` is executed (e.g. by `U_init_dim_obs_f`), the operations performed in this routine will be to select the part of the full observation vector that is relevant for the current local analysis domain.
     404 * The routine `U_init_dim_obs_l` is executed before this routine. Thus, if that routine already prepares the information which elements of `observation_f` are needed for `observation_l`, this information can be used efficiently here.
     405
     406
     407
     408
     409=== `U_prodRinvA_l` (prodrinva_l_pdaf.F90) ===
     410
     411This routine is used by the local filters (LSEIK and LETKF). There is a slight difference between LSEIK and LETKF for this routine, which is described below.
     412
     413The interface for this routine is:
     414{{{
     415SUBROUTINE prodRinvA_l(domain_p, step, dim_obs_l, dim_ens, obs_l, A_l, C_l)
     416
     417  INTEGER, INTENT(in) :: domain_p             ! Current local analysis domain
     418  INTEGER, INTENT(in) :: step                 ! Current time step
     419  INTEGER, INTENT(in) :: dim_obs_l            ! Dimension of local observation vector
     420  INTEGER, INTENT(in) :: dim_ens              ! Ensemble size
     421  REAL, INTENT(in)    :: obs_l(dim_obs_l)     ! Local vector of observations
     422  REAL, INTENT(inout) :: A_l(dim_obs_l, dim_ens) ! Input matrix from analysis routine
     423  REAL, INTENT(out)   :: C_l(dim_obs_l, dim_ens) ! Output matrix
     424}}}
     425
     426The routine is called during the loop over the local analysis domains. In the algorithm, the product of the inverse of the observation error covariance matrix with some matrix has to be computed. For the SEIK filter this matrix holds the observed part of the ensemble perturbations for the local analysis domain of index `domain_p`. The matrix is provided as `A_l`. The product has to be given as `C_l`.
     427
     428This routine is also the place to perform observation localization. To initialize a vector of weights, the routine `PDAF_local_weight` can be called. The procedure is used in the example implementation and also demonstrated in the template routine.
     429
     430Hints:
     431 * The routine is a local variant of the routine `U_prodRinvA`. Thus if that routine has been implemented before, it can be adapted here for the local filter.
     432 * The routine does not require that the product is implemented as a real matrix-matrix product. Rather, the product can be implemented in its most efficient form. For example, if the observation error covariance matrix is diagonal, only the multiplication of the diagonal with matrix `A_l` has to be implemented.
     433 * The observation vector `obs_l` is provided through the interface for cases where the observation error variance is relative to the actual value of the observations.
     434 * The interface has a difference for SEIK and ETKF: For ETKF the third argument is the ensemble size (`dim_ens`), while for SEIK it is the rank (`rank`) of the covariance matrix (usually ensemble size minus one). In addition, the second dimension of `A_l` and `C_l` has size `dim_ens` for ETKF, while it is `rank` for the SEIK filter. (Practically, one can usually ignore this difference as the fourth argument of the interface can be named arbitrarily in the routine.)
     435
     436
     437=== `U_init_n_domains` (init_n_domains_pdaf.F90) ===
     438
     439This routine is used by all filter algorithms with domain-localization (LSEIK, LETKF) and is independent of the particular algorithm.
     440
     441The interface for this routine is:
     442{{{
     443SUBROUTINE init_n_domains(step, n_domains_p)
     444
     445  INTEGER, INTENT(in)  :: step        ! Current time step
     446  INTEGER, INTENT(out) :: n_domains_p ! Number of analysis domains for local model sub-domain
     447}}}
     448
     449The routine is called during the analysis step before the loop over the local analysis domains is entered.
     450It has to provide the number of local analysis domains. In case of a domain-decomposed model the number of local analysis domain for the model sub-domain of the calling process has to be initialized.
     451
     452Hints:
     453 * As a simple case, if the localization is only performed horizontally, the local analysis domains can be single vertical columns of the model grid. In this case, `n_domains_p` is simply the number of vertical columns in the local model sub-domain.
     454
     455
     456=== `U_init_dim_l` (init_dim_l_pdaf.F90) ===
     457
     458This routine is used by all filter algorithms with domain-localization (LSEIK, LETKF) and is independent of the particular algorithm.
     459
     460The interface for this routine is:
     461{{{
     462SUBROUTINE init_dim_l(step, domain_p, dim_l)
     463
     464  INTEGER, INTENT(in)  :: step        ! Current time step
     465  INTEGER, INTENT(in)  :: domain_p    ! Current local analysis domain
     466  INTEGER, INTENT(out) :: dim_l       ! Local state dimension
     467}}}
     468
     469The routine is called during the loop over the local analysis domains in the analysis step.
     470It has to provide in `dim_l` the dimension of the state vector for the local analysis domain with index `domain_p`.
     471
     472Hints:
     473 * If a local analysis domain is a single vertical column of the model grid, the size of the state in the local analysis domain will be just the number of vertical grid points at this location.
     474
     475
     476=== `U_init_dim_obs_l` (init_dim_obs_l_pdaf.F90) ===
     477
     478This routine is used by all filter algorithms with domain-localization (LSEIK, LETKF) and is independent of the particular algorithm.
     479
     480The interface for this routine is:
     481{{{
     482SUBROUTINE init_dim_obs_l(domain_p, step, dim_obs_f, dim_obs_l)
     483
     484  INTEGER, INTENT(in)  :: domain_p   ! Current local analysis domain
     485  INTEGER, INTENT(in)  :: step       ! Current time step
     486  INTEGER, INTENT(in)  :: dim_obs_f  ! Full dimension of observation vector
     487  INTEGER, INTENT(out) :: dim_obs_l  ! Local dimension of observation vector
     488}}}
     489
     490The routine is called during the loop over the local analysis domains in the analysis step.
     491It has to initialize in `dim_obs_l` the size of the observation vector used for the local analysis domain with index `domain_p`.
     492
     493Some hints:
     494 * Usually, the observations to be considered for a local analysis are those which reside within some distance from the local analysis domain. Thus, if the local analysis domain is a single vertical column of the model grid and if the model grid is a regular ij-grid, then one could use some range of i/j indices to select the observations and determine the local number of them. More generally, one can compute the physical distance of an observation from the local analysis domain and decide on this basis, if the observation has to be considered.
     495 * In the loop over the local analysis domains, the routine is always called before `U_init_obs_l` is executed. Thus, as `U_init_dim_obs_local` has to check which observations should be used for the local analysis domain, one can already initialize an integer array that stores the index of observations to be considered. This index should be the position of the observation in the array `observation_f`. With this, the initialization of the local observation vector in `U_init_obs_l` can be sped up.
     496 * For PDAF, we could not join the routines `U_init_dim_obs_l` and `U_init_obs_l`, because the array for the local observations is allocated internally to PDAF after its size has been determined in `U_init_dim_obs_l`.
     497
     498
     499=== `U_g2l_state` (g2l_state_pdaf.F90) ===
     500
     501This routine is used by all filter algorithms with domain-localization (LSEIK, LETKF) and is independent of the particular algorithm.
     502
     503The interface for this routine is:
     504{{{
     505SUBROUTINE global2local_state(step, domain_p, dim_p, state_p, dim_l, state_l)
     506
     507  INTEGER, INTENT(in) :: step           ! Current time step
     508  INTEGER, INTENT(in) :: domain_p       ! Current local analysis domain
     509  INTEGER, INTENT(in) :: dim_p          ! State dimension for model sub-domain
     510  INTEGER, INTENT(in) :: dim_l          ! Local state dimension
     511  REAL, INTENT(in)    :: state_p(dim_p) ! State vector for model sub-domain
     512  REAL, INTENT(out)   :: state_l(dim_l) ! State vector on local analysis domain
     513}}}
     514
     515The routine is called during the loop over the local analysis domains in the analysis step. It has to provide the local state vector `state_l` that corresponds to the local analysis domain with index `domain_p`. Provided to the routine is the state vector `state_p`. With a domain decomposed model, this is the state for the local model sub-domain.
     516
     517Hints:
     518 * In the simple case that a local analysis domain is a single vertical column of the model grid, the operation in this routine would be to take out of `state_p` the data for the vertical column indexed by `domain_p`.
     519
     520
     521=== `U_l2g_state` (l2g_state_pdaf.F90) ===
     522
     523This routine is used by all filter algorithms with domain-localization (LSEIK, LETKF) and is independent of the particular algorithm.
     524
     525The interface for this routine is:
     526{{{
     527SUBROUTINE l2g_state(step, domain_p, dim_l, state_l, dim_p, state_p)
     528
     529  INTEGER, INTENT(in) :: step           ! Current time step
     530  INTEGER, INTENT(in) :: domain_p       ! Current local analysis domain
     531  INTEGER, INTENT(in) :: dim_p          ! State dimension for model sub-domain
     532  INTEGER, INTENT(in) :: dim_l          ! Local state dimension
     533  REAL, INTENT(in)    :: state_p(dim_p) ! State vector for model sub-domain
     534  REAL, INTENT(out)   :: state_l(dim_l) ! State vector on local analysis domain
     535}}}
     536
     537The routine is called during the loop over the local analysis domains in the analysis step. It has to initialize the part of the global state vector `state_p` that corresponds to the local analysis domain with index `domain_p`. Provided to the routine is the state vector `state_l` for the local analysis domain.
     538
     539Hints:
     540 * In the simple case that a local analysis domain is a single vertical column of the model grid, the operation in this routine would be to write into `state_p` the data for the vertical column indexed by `domain_p`.
     541
     542
     543=== `U_g2l_obs` (g2l_obs_pdaf.F90) ===
     544
     545This routine is used by all filter algorithms with domain-localization (LSEIK, LETKF) and is independent of the particular algorithm.
     546
     547The interface for this routine is:
     548{{{
     549SUBROUTINE g2l_obs(domain_p, step, dim_obs_f, dim_obs_l, mstate_f, mstate_l)
     550
     551  INTEGER, INTENT(in) :: domain_p              ! Current local analysis domain
     552  INTEGER, INTENT(in) :: step                  ! Current time step
     553  INTEGER, INTENT(in) :: dim_obs_f             ! Dimension of full observation vector for model sub-domain
     554  INTEGER, INTENT(in) :: dim_obs_l             ! Dimension of observation vector for local analysis domain
     555  REAL, INTENT(in)    :: mstate_f(dim_obs_f)   ! Full observation vector for model sub-domain
     556  REAL, INTENT(out)   :: mstate_l(dim_obs_l)   ! Observation vector for local analysis domain
     557}}}
     558
     559The routine is called during the loop over the local analysis domains in the analysis step. It has to provide a local observation vector `mstate_l` for the observation domain that corresponds to the local analysis domain with index `domain_p`. Provided to the routine is the full observation vector `mstate_f` from which the local part has to be extracted.
     560
     561Hints:
     562 * The  vector `mstate_f` that is provided to the routine is one of the observed state vectors that are produced by `U_obs_op_full`.
     563 * Some operations performed here are analogous to those required to initialize a local vector of observations in `U_init_obs_l`. If that routine reads first a full vector of observations (e.g. in `U_init_dim_obs_f`), this vector has to be restricted to the relevant observations for the current local analysis domain. For this operation, one can for example initialize an index array when `U_init_dim_obs_l` is executed. (Which happens before `U_global2local_obs`)
     564
     565
     566=== `U_init_obsvar` (init_obsvar_pdaf.F90) ===
     567
     568This routine is used by the global filter algorithms SEIK and  ETKF as well as the local filters LSEIK and LETKF. The routine is only called if the adaptive forgetting factor is used (`type_forget=1` in the example implementation). The difference in this routine between global and local filters is that the global filters use 'global' while the local filters use 'full' quantities.
     569
     570The interface for this routine is:
     571{{{
     572SUBROUTINE init_obsvar(step, dim_obs_f, obs_f, meanvar_f)
     573
     574  INTEGER, INTENT(in) :: step             ! Current time step
     575  INTEGER, INTENT(in) :: dim_obs_f        ! Full dimension of observation vector
     576  REAL, INTENT(in)    :: obs_f(dim_obs_f) ! Full observation vector
     577  REAL, INTENT(out)   :: meanvar_f        ! Mean observation error variance
     578}}}
     579
     580The routine is called in the local filters before the loop over all local analysis domains is entered. The call is by the routine that computes an adaptive forgetting factor (`PDAF_set_forget`).
     581The routine has to initialize an average full observation error variance, which should be consistent with the observation vector initialized in `U_init_ob_f`.
     582
     583
     584Hints:
     585 * For a model with domain-decomposition one might use the mean variance for the model sub-domain of the calling process. Alternatively one can compute a mean variance for the full model domain using MPI communication (e.g. the function `MPI_allreduce`).
     586 * The observation vector `obs_p` is provided to the routine for the case that the observation error variance is relative to the value of the observations.
     587 * If the adaptive forgetting factor is not used, this routine has only to exist for the compilation, but it does not need functionality.
     588
     589
     590=== `U_init_obsvar_l` (init_obsvar_l_pdaf.F90) ===
     591
     592This routine is used by all filter algorithms with domain-localization (LSEIK, LETKF) and is independent of the particular algorithm. The routine is only called if the local adaptive forgetting factor is used (`type_forget=2` in the example implementation).
     593
     594The interface for this routine is:
     595{{{
     596SUBROUTINE init_obsvar_l(domain_p, step, dim_obs_l, obs_l, meanvar_l)
     597
     598  INTEGER, INTENT(in) :: domain_p         ! Current local analysis domain
     599  INTEGER, INTENT(in) :: step             ! Current time step
     600  INTEGER, INTENT(in) :: dim_obs_l        ! Local dimension of observation vector
     601  REAL, INTENT(in)    :: obs_l(dim_obs_l) ! Local observation vector
     602  REAL, INTENT(out)   :: meanvar_l        ! Mean local observation error variance
     603}}}
     604
     605The routine is called in the local filters during the loop over all local analysis domains by the routine that computes a local adaptive forgetting factor (`PDAF_set_forget_local`). The routine has to initialize a local mean observation error variance for all observations used for the analysis in the current local analysis domain.
     606
     607Hints:
     608 * If the local adaptive forgetting factor is not used, this routine has only to exist for the compilation, but it does not need functionality.
     609
     610=== `U_prepoststep` (prepoststep_ens_pdaf.F90) ===
     611
     612The routine has already been described for modifying the model for the ensemble integration and for inserting the analysis step.
     613
     614See the page on [InsertAnalysisStep#U_prepoststepprepoststep_ens_pdaf.F90 inserting the analysis step] for the description of this routine.
     615
     616
     617=== `U_next_observation` (next_observation_pdaf.F90) ===
     618
     619This routine is independent of the filter algorithm used.
     620
     621See the page on [InsertAnalysisStep#U_next_observationnext_observation_pdaf.F90 inserting the analysis step] for the description of this routine.
     622
     623
     624== Execution order of user-supplied routines ==
     625
     626The user-supplied routines are essentially executed in the order they are listed in the interface to `PDAFomi_assimilate_3dvar`. The order can be important as some routines can perform preparatory work for later routines. For example, `U_init_dim_obs_pdafomi` prepares an index array that provides the information for executing the observation operator in `U_obs_op_pdafomi`. How this information is initialized is described in the documentation of OMI.
     627
     628Before the analysis step is called the following routine is executed:
     629 1. [#U_collect_statecollect_state_pdaf.F90 U_collect_state]
     630
     631The analysis step is executed when the ensemble integration of the forecast is completed. During the analysis step the following routines are executed in the given order:
     632 1. [#U_prepoststepprepoststep_ens_pdaf.F90 U_prepoststep] (Call to act on the forecast ensemble, called with negative value of the time step)
     633 1. [#U_init_dim_obs_pdafomicallback_obs_pdafomi.F90 U_init_dim_obs_pdafomi]
     634 1. [#U_obs_op_pdafomicallback_obs_pdafomi.F90 U_obs_op_pdafomi] (multiple calls, one for each ensemble member)
     635
     636Inside the analysis step the interative optimization is computed. This involves the repeated call of the routines:
     637 1. [#U_cvt_enscvt_ens_pdaf.F90 U_cvt_ens]
     638 1. [#U_obs_op_lin_pdafomicallback_obs_pdafomi.F90 U_obs_op_lin_pdafomi]
     639 1. [#U_obs_op_adj_pdafomicallback_obs_pdafomi.F90 U_obs_op_adj_pdafomi]
     640 1. [#U_cvt_adj_enscvt_adj_ens_pdaf.F90 U_cvt_adj_ens]
     641
     642After the iterative optimization the following routines are executes to complte the analysis step:
     643 1. [#U_cvt_enscvt_ens_pdaf.F90 U_cvt_ens] (Call to the control vector transform to compute the final state vector increment
     644 1. [#U_prepoststepprepoststep_ens_pdaf.F90 U_prepoststep] (Call to act on the analysis ensemble, called with (positive) value of the time step)
     645
     646The iterative optimization abovve computes an updated ensemble mean state. Subsequently, the ensemble perturbations are updated using the LESTKF or ESTKF. The execution of the routines for these filters is described for the LESTKF on the [wiki:ImplementAnalysisLocal page on implementing the local filter analysis step] and for the ESTKF on the [wiki:ImplementAnalysisGlobal page on implementing the global filter analysis step].
     647
     648In case of the routine `PDAFomi_assimilate_*`, the following routines are executed after the analysis step:
     649 1. [#U_distribute_statedistribute_state_pdaf.F90 U_distribute_state]
     650 1. [#U_next_observationnext_observation_pdaf.F90 U_next_observation]