Changes between Initial Version and Version 1 of OnlineFlexible_PDAF3


Ignore:
Timestamp:
May 25, 2025, 5:29:06 PM (7 days ago)
Author:
lnerger
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • OnlineFlexible_PDAF3

    v1 v1  
     1= Modifying the model code for the 'flexible' implementation variant =
     2
     3{{{
     4#!html
     5<div class="wiki-toc">
     6<h4>Online Mode: Implementation Guide</h4>
     7<ol><li><a href="OnlineImplementationGuide_PDAF3">Main page</a></li>
     8<li><a href="OnlineAdaptParallelization_PDAF3">Adapting the parallelization</a></li>
     9<li><a href="OnlineInitPdaf_PDAF3">Initializing PDAF</a></li>
     10<li><a href="OnlineModifyModelforEnsembleIntegration_PDAF3">Modifications for ensemble integration</a></li>
     11  <li>&nbsp;&nbsp;&nbsp;Flexible implementation variant</li>
     12<li><a href="ImplementationofAnalysisStep_PDAF3">Implementing the analysis step</a></li>
     13<li><a href="OnlineAddingMemoryandTimingInformation_PDAF3">Memory and timing information</a></li>
     14</ol>
     15</div>
     16}}}
     17
     18[[PageOutline(2-3,Contents of this page)]]
     19
     20== Overview ==
     21
     22For the ''flexible'' implementation variant of the assimilation system, one has to modify the model code so that each model task can compute several integrations of model states successively. The required extensions are described below.
     23
     24Because the flexible implementation variant is more complex to implement it can be easier if one first implements the ''fully parallel'' variant and hen modifies this to obtain the code structure for the ''flexible'' variant.
     25
     26
     27== External ensemble loop ==
     28
     29Figure 1 compares the code structure of the ''fully parallel'' and ''flexible'' implementation variants. While for the ''fully parallel'' variant only the routine `assimilate_pdaf` has to be added in the model time stepping loop, the additional ensemble loop, a call to `PDAF_get_fcst_info`, and a check of the exit flag have to be added.
     30
     31[[Image(//pics/DAextension_flexible_PDAF3.png)]]
     32[[BR]]**Figure 1:** Comparison of the code structure for the ''fully parallel'' and ''flexible'' implementation variants. The ''flexible'' variant required more changes to the code to enable that each model task can integrate more than one ensemble state. Note that the ''fully parallel'' variant defines the number of time steps as `isteps`, which is the total number of steps in the program, while the ''flexible'' variant uses `nsteps`, which is the number of steps in one forecast phase.
     33
     34In the source code, the modification can be realized in the following way:
     35{{{
     36  pdaf_modelloop: DO 
     37
     38     ! PDAF: Get forecast information
     39     CALL PDAF_get_fcst_info(nsteps, timenow, doexit)
     40
     41     ! Check exit flag
     42     IF (doexit==1) EXIT pdaf_modelloop
     43
     44     ! Model time stepping loop, for example
     45     DO i = 1, nsteps
     46
     47        ... Time stepping code of the model ...
     48
     49        ! Let PDAF check forecast progress and perform analysis
     50        CALL assimilate_pdaf()
     51     END DO 
     52
     53  END DO pdaf_modelloop
     54}}}
     55This example is taken from the template implementation in `templates/online_flexible/`. The unconditional DO loop (while loop) allows for an arbirary number of repetitions. The exit of the loop is controlled by the exit flag `doexit`, which is obtained from `PDAF_get_fcst_info`. The variable `nsteps`, also obtained from PDAF_get_fcst_info, defines the number of time steps to be computed during the current forecast phase. This value has to be used in the model time stepping loop.
     56
     57To complete the forecasting there are four further steps required:
     581. We need to initialize the ensemble forecasting. Thus, we need to set how many time steps need to be done until the first observations are assimilated. In addition, we need to write the ensemble state vector values into the model fields. For these operations, we call the routine `PDAF_init_forecast` in the routine `init_pdaf`.
     591. We need to adapt for model code for the additional ensemble loop and related control structure with calling `PDAF_get_fcst_info` as described above.
     601. To enable that the analysis can be performed, we then have to insert the routine `assimilate_pdaf` into the model code.
     611. To be able to test whether the forecasting works correctly we also recommend to implement the routine `collect_state_pdaf` at this point. The routine is used in `assimilate_pdaf` to write the forecasted model fields into a state vector.
     62
     63These steps are described below. The code changes for the ''flexible'' parallelization variant can usually be obtained by adapting the code for the ''fully parallel'' implementation variant.
     64
     65
     66== Calling `PDAF_init_forecast` ==
     67
     68The call to `PDAF_init_forecast` is identical for the ''fully parallel'' and ''flexible'' implementation variants.
     69
     70The routine is called at the end of the routine `init_pdaf` that was discussed on the [InitPdaf page on initializing PDAF]. The main purpose of this routine is to initialize the model fields for all model tasks from the ensemlbe of state vectors. This is done in the call-back routine `distribute_state_pdaf`. The routine also calls the call-back routine `next_observation_pdaf` to set the number of time steps for the initial forecast phase and an exit flag. These values used are internally by PDAF to control the forecast phase. Further, the routine calls the call-back-routine `prepoststep_pdaf`. This pre/postep routine provides the user access to the initial ensemble.
     71
     72The interface of `PDAF_init_forecast` is:
     73{{{
     74  SUBROUTINE PDAF_init_forecast(next_observation_pdaf, distribute_state_pdaf, &
     75                                prepoststep_pdaf, status)
     76}}}
     77with the arguments (where the bold names show the arguments relevant to the user for the ''fully-parallel'' variant):
     78 * [#next_observation_pdafnext_observation_pdaf.F90 next_observation_pdaf]:[[BR]] The name of a user supplied routine that initializes the variables `nsteps`, `timenow`, and `doexit`
     79 * [#distribute_state_pdafdistribute_state_pdaf.F90 distribute_state_pdaf]:[[BR]] The name of a user supplied routine that initializes the model fields from the array holding the ensemble of model state vectors
     80 * [#prepoststep_pdafprepoststep_ens_pdaf.F90 prepoststep_pdaf]:[[BR]] The name of a user supplied routine that is called before and after the analysis step. Here the user has the possibility to access the state ensemble and can e.g. compute estimated variances or can write the ensemble states the state estimate into files.
     81 * `status`, `integer, intent(inout)`:[[BR]] The status flag. It is zero, if the routine is exited without errors. We recommend to check the value.
     82
     83The user-supplied routines are described further below.
     84
     85
     86== Adding `PDAF_get_fcst_info` ==
     87
     88The interface is the following:
     89{{{
     90  SUBROUTINE PDAF_get_fcst_info(nsteps, timenow, doexit)
     91
     92      INTEGER, INTENT(out) :: stepnow  ! The current time step
     93      INTEGER, INTENT(out) :: nsteps   ! The number of time steps of the next forecast phase
     94      INTEGER, INTENT(out) :: doexit   ! Exit flag: (1) exit, (0) continue data assimilation
     95}}}
     96
     97These variables are intialized by `next_observation_pdaf` that was called by `PDAF_init_forecast` and returned here to the user code. At later forecast phases in the assimilation processing, the analysis routine `PDAF3_assimilation` (or more specifalize routines) will case `next_observation_pdaf`.
     98
     99== Inserting `assimilate_pdaf` into the model code ==
     100
     101The right place to insert the routine `assimilate_pdaf` into the model code is the same as for the ''fully parallel'' implementation variant. Usually this is at the end of the model time stepping loop, thus when the model completed the computation of a single time step. In most cases, this is just before the 'END DO' in the model source code. However, there might be special cases, where the model does some additional operations so that the call to `assimilate_pdaf` should be insered somewhat earlier.
     102
     103== Using `assimilate_pdaf` ==
     104
     105The purpose of `assimilate_pdaf` is to call the universal PDAF-core routine `PDAF3_assimilate` (or a more specific varant of this routine).  The arguments of `PDAF3_assimilate` are mainly the names of user-supplied call-back routines, except from an argument for the status flag. These names are specified in `assimilate_pdaf` as 'external'.
     106
     107The routine `assimilate_pdaf` is called at each time step of the model forecast. This allows to, e.g., apply [wiki:IncrementalAnalysisUpdates incremental analysis updates].
     108
     109Details on the implementations of the user-routines for the analysis step ar provided in the [wiki:ImplementationofAnalysisStep Page on implementating the analysis step].
     110
     111Relevant for the forecasting is that `PDAF3_assimilate` calls the user-supplied routine `collect_state_pdaf` which writes forecasted model fields into a state vector from PDAF's ensemble array. We recommend to implement the functionality in `collect_state_pdaf` at this point, even though we explain `PDAF3_assimilation` only later.
     112
     113== Completing `collect_state_pdaf`  ==
     114
     115The routine `collect_state_pdaf` (In file collect_state_pdaf.F90 in the tutorials and template), is called during the forecast phase for each state vector of the ensemble to write the forecasted model fields back into a state vector. This is the reserve operation to what is done in `distribute_state_pdaf`. The routine is executed by all processes that belong to model tasks.
     116
     117The full interface of this call-back routine is explained further below with the other user-supplied routines. When the functionality is implemented, one can test the forecasting. In particular one can already test funtionality in `prepoststep_pdaf` because PDAF will provide this routine with the ensemble array that was filled with the information from model fields by calls to `collect_state_pdaf`.
     118
     119== User-supplied routines ==
     120
     121Here, we discuss the user-supplied routines that are arguments of `PDAF_init_forecast`.
     122
     123In the section titles below we provide the name of the template file in parentheses.
     124
     125
     126
     127=== `next_observation_pdaf` (next_observation_pdaf.F90) ===
     128
     129The interface is:
     130{{{
     131SUBROUTINE next_observation_pdaf(stepnow, nsteps, doexit, timenow)
     132
     133  INTEGER, INTENT(in)  :: stepnow  ! The current time step provided by PDAF
     134  INTEGER, INTENT(out) :: nsteps   ! The number of time steps of the next forecast phase
     135  INTEGER, INTENT(out) :: doexit   ! Exit flag: (1) exit, (0) continue data assimilation
     136  REAL, INTENT(out)    :: timenow  ! Current (physical) model (physical) time. Not used by PDAF itself.
     137}}}
     138
     139The routine is called by `PDAF_init_forecast` and later at the beginning of each forecast phase by `PDAF3_assimilate`, or similar routines. It is executed by all processes that participate in the model integrations.
     140
     141Based on the information of the current time step, the routine has to define the number of time steps `nsteps` for the next forecast phase and the exit flag `doexit`. In the user code, one accesses these variables with `PDAF_get_fcst_info` as explained above.
     142
     143Some hints:
     144 * We assume that the time interval between successive observations is known. Then, `nsteps` can be simply initialized by dividing the time interval by the size of the time step.
     145 * It is up to the user to define `timenow` because it is only used in the user-code. The user can either ignore it (setting to to 0.0), or could use it freely to indicate the model time. At the first call to `next_observation_pdaf` the variable `timenow` can be initialized with the current model time. At the next call a forecast phase has been completed. Thus, the new value of `timenow` follows from the timer interval for the previous forecast phase and can be incremented accordingly. In the user code, one can access `timenow` with a call to [wiki:PDAF_get_fcst_info].
     146 * Inside PDAF, `doexit` is used as follows: If `nsteps=0` or `doexit=1` is set, the ensemble state will not be distributed by PDAF (thus `distribute_state`, see below, is not called). If one intends to proceed with ensemble forecasting, one has to set `nsteps` to a value >0 and `doexit=0`. 
     147 * The total number of time steps set for the assimilation experiment (variable `isteps` in Fig. 1) defines when the model integrations end. If `nsteps` is set so that it specifies a time step larger than `isteps` not all of these steps will be computed.
     148
     149=== `distribute_state_pdaf` (distribute_state_pdaf.F90) ===
     150
     151The interface for this routine is
     152{{{
     153SUBROUTINE distribute_state(dim_p, state_p)
     154
     155  INTEGER, INTENT(in) :: dim_p           ! Size of state vector for process-local sub-domain
     156  REAL, INTENT(inout) :: state_p(dim_p)  ! State vector for process-local sub-domain
     157}}}
     158
     159The routine is called by `PDAF_init_forecast` and later at the beginning of each forecast phase by `PDAF3_assimilate`, or similar routines. It is executed by all processes that participate in the model integrations.
     160
     161PDAF calls this routine providing the state vector `state_p` and its size `dim_p`. The routine has to write the information from the state vector into the model field arrays.
     162
     163Some hints:
     164 * The user has defined the setup of the state vector, so that it is know which model fields are stored at which position in the state vector and in which order, for example, a 3-dimensional model field is stored. This was already used in `init_ens_pdaf`.
     165 * The code usually consist of (nested) loops through each of the model fields, one field at a time. In the loop each element of the state vector is written into an element of a model field array.
     166 * If the model is not parallelized, `state_p` will contain a full state vector. If the model is parallelized using domain decomposition, `state_p` will contain the part of the state vector that corresponds to the model sub-domain for the calling process.
     167
     168
     169=== `prepoststep_pdaf` (prepoststep_ens_pdaf.F90) ===
     170
     171The interface for this routine is
     172{{{
     173SUBROUTINE prepoststep(step, dim_p, dim_ens, dim_ens_p, dim_obs_p, &
     174                       state_p, Ainv, ens_p, flag)
     175
     176  INTEGER, INTENT(in) :: step        ! Current time step
     177                         ! (When the routine is called before the analysis -step is provided.)
     178  INTEGER, INTENT(in) :: dim_p       ! Process-local state dimension
     179  INTEGER, INTENT(in) :: dim_ens     ! Size of state ensemble
     180  INTEGER, INTENT(in) :: dim_ens_p   ! Process-local ensemble size (not relevant for fully parallel)
     181  INTEGER, INTENT(in) :: dim_obs_p   ! Process-local dimension of observation vector
     182  REAL, INTENT(inout) :: state_p(dim_p) ! Process-local state vector
     183                                     ! The array 'state_p' is not generally not initialized.
     184                                     ! It can be used freely in this routine.
     185  REAL, INTENT(inout) :: Ainv(dim_ens-1, dim_ens-1) ! Inverse of matrix A )only for special cases)
     186  REAL, INTENT(inout) :: ens_p(dim_p, dim_ens)      ! State ensemble for process-local sub-domain
     187  INTEGER, INTENT(in) :: flag        ! PDAF status flag
     188}}}
     189
     190The routine `prepoststep_pdaf` is called once at the beginning of the assimilation process. In addition, it is called during the assimilation cycles before the analysis step and after the ensemble transformation. The routine is called by all filter processes (that is `filterpe=.true.`).
     191
     192With this routine, PDAF provides the user the full access to the ensemble of model states. Thus, user-controlled pre- and post-step operations can be performed.  For example the forecast and the analysis states and ensemble covariance matrix can be analyzed, e.g. by computing the estimated variances. If the smoother is used, also the smoothed ensembles can be analyzed. In addition, the estimates can be written to disk.
     193
     194The routine also allows to perform adjustments to the state vectors, e.g. for balances, in the ensemble.
     195
     196Hints:
     197 * The state vector, `state_p`, is usually not initialized because the ensemble DA methods focus on computing the ensemble and not its mean state. Thus the user would need to compute the ensemble mean, if this is required. This array can be used freely in `prepoststep_pdaf`.
     198 * The interface of `prepoststep_pdaf` does not include the array of smoothed ensembles. In order to access the smoother ensemble array, one has to set a pointer to it using a call to the routine `PDAF_get_smootherens` (see page on [AuxiliaryRoutines auxiliary routines]).
     199
     200
     201
     202=== `collect_state_pdaf` (collect_state_pdaf.F90) ===
     203
     204The interface for this routine is
     205{{{
     206SUBROUTINE collect_state(dim_p, state_p)
     207
     208  INTEGER, INTENT(in) :: dim_p           ! State dimension for process-local model sub-domain
     209  REAL, INTENT(inout) :: state_p(dim_p)  ! State vector for process-local model sub-domain
     210}}}
     211
     212The routine is called by `PDAF3_assimilate`, or similar assimilate routine, for each ensemble state for which a forecast was computed . It is executed by all processes that participate in the model integrations.
     213
     214PDAF calls this routine providing the state vector `state_p` and its size `dim_p`. The routine has to write the information from the model fields into this state vector. This is the reverse operation to that of the routine `distribute_state_pdaf`.
     215
     216Hint:
     217* If the model is not parallelized, `state_p` will contain a full state vector. If the model is parallelized using domain decomposition, `state_p` will contain the part of the state vector that corresponds to the model sub-domain for the calling process.
     218
     219== Compiling and testing ==
     220
     221One can test the program without having implemented the actual assimilation step. In the template code `template/online_flexible` all required user-supplied routines are included, but they do not contain functionality. However, one can use them to be able to compile and run the assimilation program for testing. In particular one can check if the ensemble forecasting works correctly.
     222
     223The flow of the modified model is now the following: `distribute_state_pdaf` provides each model task with a model state that is to be integrated. Having computed the forecast, `collect_state_pdaf` writes the propagated model fields back into a state vector from the ensemble array of PDAF. This array is then provided to `prepoststep_pdaf`. Thus, one can check if the forecasting works correctly. In `prepoststep_pdaf` one can access the ensemble. Here one can, e.g. compute the ensemble mean - thus the state estimate of the ensemble assimilation - and write this into a file.
     224