New URL for NEMO forge!   http://forge.nemo-ocean.eu

Since March 2022 along with NEMO 4.2 release, the code development moved to a self-hosted GitLab.
This present forge is now archived and remained online for history.
restart.F90 in NEMO/trunk/src/OCE/IOM – NEMO

source: NEMO/trunk/src/OCE/IOM/restart.F90

Last change on this file was 15141, checked in by smasson, 3 years ago

trunk: avoid implicit loops in diawri

  • Property svn:keywords set to Id
File size: 20.3 KB
Line 
1MODULE restart
2   !!======================================================================
3   !!                     ***  MODULE  restart  ***
4   !! Ocean restart :  write the ocean restart file
5   !!======================================================================
6   !! History :  OPA  !  1999-11  (M. Imbard)  Original code
7   !!   NEMO     1.0  !  2002-08  (G. Madec)  F90: Free form
8   !!            2.0  !  2006-07  (S. Masson)  use IOM for restart
9   !!            3.3  !  2010-04  (M. Leclair, G. Madec)  modified LF-RA
10   !!            - -  !  2010-10  (C. Ethe, G. Madec) TRC-TRA merge (T-S in 4D)
11   !!            3.7  !  2014-01  (G. Madec) suppression of curl and hdiv from the restart
12   !!             -   !  2014-12  (G. Madec) remove KPP scheme
13   !!            4.1  !  2020-11  (S. Techene, G. Madec)  move ssh initiatlisation in rst_read_ssh
14   !!             -   !                                   add restart in Shallow Water Eq. case
15   !!----------------------------------------------------------------------
16
17   !!----------------------------------------------------------------------
18   !!   rst_opn       : open the ocean restart file for writting
19   !!   rst_write     : write the ocean restart file
20   !!   rst_read_open : open the restart file for reading
21   !!   rst_read      : read the ocean restart file
22   !!   rst_read_ssh  : ssh set from restart or domcfg.nc file or usr_def_istat_ssh
23   !!----------------------------------------------------------------------
24   USE oce            ! ocean dynamics and tracers
25   USE dom_oce        ! ocean space and time domain
26   USE sbc_ice        ! only lk_si3
27   USE phycst         ! physical constants
28   USE eosbn2         ! equation of state
29   USE wet_dry        ! Wetting/Drying flux limiting
30   USE usrdef_istate, ONLY : usr_def_istate_ssh   ! user defined ssh initial state
31   USE trdmxl_oce     ! ocean active mixed layer tracers trends variables
32   USE diu_bulk       ! ???
33   !
34   USE in_out_manager ! I/O manager
35   USE iom            ! I/O module
36   USE lib_mpp        ! distribued memory computing library
37
38   IMPLICIT NONE
39   PRIVATE
40
41   PUBLIC   rst_opn         ! called by step.F90
42   PUBLIC   rst_write       ! called by step.F90
43   PUBLIC   rst_read_open   ! called in rst_read_ssh
44   PUBLIC   rst_read        ! called by istate.F90
45   PUBLIC   rst_read_ssh    ! called by domain.F90
46   
47   !! * Substitutions
48#  include "do_loop_substitute.h90"
49#  include "domzgr_substitute.h90"
50   !!----------------------------------------------------------------------
51   !! NEMO/OCE 4.0 , NEMO Consortium (2018)
52   !! $Id$
53   !! Software governed by the CeCILL license (see ./LICENSE)
54   !!----------------------------------------------------------------------
55CONTAINS
56
57   SUBROUTINE rst_opn( kt )
58      !!---------------------------------------------------------------------
59      !!                   ***  ROUTINE rst_opn  ***
60      !!
61      !! ** Purpose : + initialization (should be read in the namelist) of nitrst
62      !!              + open the restart when we are one time step before nitrst
63      !!                   - restart header is defined when kt = nitrst-1
64      !!                   - restart data  are written when kt = nitrst
65      !!              + define lrst_oce to .TRUE. when we need to define or write the restart
66      !!----------------------------------------------------------------------
67      INTEGER, INTENT(in) ::   kt     ! ocean time-step
68      !!
69      CHARACTER(LEN=20)   ::   clkt     ! ocean time-step deine as a character
70      CHARACTER(LEN=50)   ::   clname   ! ocean output restart file name
71      CHARACTER(lc)       ::   clpath   ! full path to ocean output restart file
72      CHARACTER(LEN=52)   ::   clpname   ! ocean output restart file name including prefix for AGRIF
73      CHARACTER(LEN=256)  ::   clinfo    ! info character
74      !!----------------------------------------------------------------------
75      !
76      IF( kt == nit000 ) THEN   ! default definitions
77         lrst_oce = .FALSE.
78         IF( ln_rst_list ) THEN
79            nrst_lst = 1
80            nitrst = nn_stocklist( nrst_lst )
81         ELSE
82            nitrst = nitend
83         ENDIF
84      ENDIF
85
86      IF( .NOT. ln_rst_list .AND. nn_stock == -1 )   RETURN   ! we will never do any restart
87
88      ! frequency-based restart dumping (nn_stock)
89      IF( .NOT. ln_rst_list .AND. MOD( kt - 1, nn_stock ) == 0 ) THEN
90         ! we use kt - 1 and not kt - nit000 to keep the same periodicity from the beginning of the experiment
91         nitrst = kt + nn_stock - 1                  ! define the next value of nitrst for restart writing
92         IF( nitrst > nitend )   nitrst = nitend   ! make sure we write a restart at the end of the run
93      ENDIF
94      ! to get better performances with NetCDF format:
95      ! we open and define the ocean restart file one time step before writing the data (-> at nitrst - 1)
96      ! except if we write ocean restart files every time step or if an ocean restart file was writen at nitend - 1
97      IF( kt == nitrst - 1 .OR. nn_stock == 1 .OR. ( kt == nitend .AND. .NOT. lrst_oce ) ) THEN
98         IF( nitrst <= nitend .AND. nitrst > 0 ) THEN
99            ! beware of the format used to write kt (default is i8.8, that should be large enough...)
100            IF( nitrst > 999999999 ) THEN   ;   WRITE(clkt, *       ) nitrst
101            ELSE                            ;   WRITE(clkt, '(i8.8)') nitrst
102            ENDIF
103            ! create the file
104            clname = TRIM(cexper)//"_"//TRIM(ADJUSTL(clkt))//"_"//TRIM(cn_ocerst_out)
105            clpath = TRIM(cn_ocerst_outdir)
106            IF( clpath(LEN_TRIM(clpath):) /= '/' ) clpath = TRIM(clpath) // '/'
107            IF(lwp) THEN
108               WRITE(numout,*)
109               IF(.NOT.lwxios) THEN
110                  WRITE(numout,*) '             open ocean restart NetCDF file: ',TRIM(clpath)//TRIM(clname)
111                  IF ( snc4set%luse )      WRITE(numout,*) '             opened for NetCDF4 chunking and compression'
112                  IF( kt == nitrst - 1 ) THEN   ;   WRITE(numout,*) '             kt = nitrst - 1 = ', kt
113                  ELSE                          ;   WRITE(numout,*) '             kt = '             , kt
114                  ENDIF
115               ENDIF
116            ENDIF
117            !
118            IF(.NOT.lwxios) THEN
119               CALL iom_open( TRIM(clpath)//TRIM(clname), numrow, ldwrt = .TRUE. )
120            ELSE
121#if defined key_xios
122               cw_ocerst_cxt = "rstw_"//TRIM(ADJUSTL(clkt))
123               IF( TRIM(Agrif_CFixed()) == '0' ) THEN
124                  clpname = clname
125               ELSE
126                  clpname = TRIM(Agrif_CFixed())//"_"//clname
127               ENDIF
128               numrow = iom_xios_setid(TRIM(clpath)//TRIM(clpname))
129               CALL iom_init( cw_ocerst_cxt, kdid = numrow, ld_closedef = .false. )
130               CALL iom_swap(      cxios_context          )
131#else
132               clinfo = 'Can not use XIOS in rst_opn'
133               CALL ctl_stop(TRIM(clinfo))
134#endif
135            ENDIF
136            lrst_oce = .TRUE.
137         ENDIF
138      ENDIF
139      !
140   END SUBROUTINE rst_opn
141
142
143   SUBROUTINE rst_write( kt, Kbb, Kmm )
144      !!---------------------------------------------------------------------
145      !!                   ***  ROUTINE rstwrite  ***
146      !!
147      !! ** Purpose :   Write restart fields in NetCDF format
148      !!
149      !! ** Method  :   Write in numrow when kt == nitrst in NetCDF
150      !!              file, save fields which are necessary for restart
151      !!
152      !!                NB: ssh is written here (rst_write)
153      !!                    but is read or set in rst_read_ssh
154      !!----------------------------------------------------------------------
155      INTEGER, INTENT(in) ::   kt         ! ocean time-step
156      INTEGER, INTENT(in) ::   Kbb, Kmm   ! ocean time level indices
157      !!----------------------------------------------------------------------
158      !
159         CALL iom_rstput( kt, nitrst, numrow, 'rdt'    , rn_Dt       )   ! dynamics time step
160      !
161      IF( .NOT.lwxios )   CALL iom_delay_rst( 'WRITE', 'OCE', numrow )   ! save only ocean delayed global communication variables
162      !
163      IF( .NOT.ln_diurnal_only ) THEN
164         CALL iom_rstput( kt, nitrst, numrow, 'sshb', ssh(:,:        ,Kbb) )     ! before fields
165         CALL iom_rstput( kt, nitrst, numrow, 'ub'  , uu(:,:,:       ,Kbb) )
166         CALL iom_rstput( kt, nitrst, numrow, 'vb'  , vv(:,:,:       ,Kbb) )
167         CALL iom_rstput( kt, nitrst, numrow, 'tb'  , ts(:,:,:,jp_tem,Kbb) )
168         CALL iom_rstput( kt, nitrst, numrow, 'sb'  , ts(:,:,:,jp_sal,Kbb) )
169         !
170#if ! defined key_RK3
171         CALL iom_rstput( kt, nitrst, numrow, 'sshn', ssh(:,:        ,Kmm) )     ! now fields
172         CALL iom_rstput( kt, nitrst, numrow, 'un'  , uu(:,:,:       ,Kmm) )
173         CALL iom_rstput( kt, nitrst, numrow, 'vn'  , vv(:,:,:       ,Kmm) )
174         CALL iom_rstput( kt, nitrst, numrow, 'tn'  , ts(:,:,:,jp_tem,Kmm) )
175         CALL iom_rstput( kt, nitrst, numrow, 'sn'  , ts(:,:,:,jp_sal,Kmm) )
176         IF( .NOT.lk_SWE )   CALL iom_rstput( kt, nitrst, numrow, 'rhop', rhop )
177#endif
178      ENDIF
179
180      IF( ln_diurnal )   CALL iom_rstput( kt, nitrst, numrow, 'Dsst', x_dsst )
181      IF( kt == nitrst ) THEN
182         IF( .NOT.lwxios ) THEN
183            CALL iom_close( numrow )     ! close the restart file (only at last time step)
184         ELSE
185            CALL iom_context_finalize(      cw_ocerst_cxt          )
186            iom_file(numrow)%nfid       = 0
187            numrow = 0
188         ENDIF
189!!gm         IF( .NOT. lk_trdmld )   lrst_oce = .FALSE.
190!!gm  not sure what to do here   ===>>>  ask to Sebastian
191         lrst_oce = .FALSE.
192         IF( ln_rst_list ) THEN
193            nrst_lst = MIN(nrst_lst + 1, SIZE(nn_stocklist,1))
194            nitrst   = nn_stocklist( nrst_lst )
195         ENDIF
196      ENDIF
197      !
198   END SUBROUTINE rst_write
199
200
201   SUBROUTINE rst_read_open
202      !!----------------------------------------------------------------------
203      !!                   ***  ROUTINE rst_read_open  ***
204      !!
205      !! ** Purpose :   Open read files for NetCDF restart
206      !!
207      !! ** Method  :   Use a non-zero, positive value of numror to assess whether or not
208      !!                the file has already been opened
209      !!----------------------------------------------------------------------
210      LOGICAL             ::   llok
211      CHARACTER(len=lc)   ::   clpath   ! full path to ocean output restart file
212      CHARACTER(len=lc+2) ::   clpname  ! file name including agrif prefix
213      !!----------------------------------------------------------------------
214      !
215      IF( numror <= 0 ) THEN
216         IF(lwp) THEN                                             ! Contol prints
217            WRITE(numout,*)
218            WRITE(numout,*) 'rst_read : read oce NetCDF restart file'
219            IF ( snc4set%luse )      WRITE(numout,*) 'rst_read : configured with NetCDF4 support'
220            WRITE(numout,*) '~~~~~~~~'
221         ENDIF
222         lxios_sini = .FALSE.
223         clpath = TRIM(cn_ocerst_indir)
224         IF( clpath(LEN_TRIM(clpath):) /= '/' ) clpath = TRIM(clpath) // '/'
225         CALL iom_open( TRIM(clpath)//cn_ocerst_in, numror )
226! are we using XIOS to read the data? Part above will have to modified once XIOS
227! can handle checking if variable is in the restart file (there will be no need to open
228! restart)
229         lrxios = lrxios.AND.lxios_sini
230
231         IF( lrxios) THEN
232             cr_ocerst_cxt = 'oce_rst'
233             IF(lwp) WRITE(numout,*) 'Enable restart reading by XIOS'
234!            IF( TRIM(Agrif_CFixed()) == '0' ) THEN
235!               clpname = cn_ocerst_in
236!            ELSE
237!               clpname = TRIM(Agrif_CFixed())//"_"//cn_ocerst_in
238!            ENDIF
239             CALL iom_init( cr_ocerst_cxt, kdid = numror, ld_closedef = .TRUE. )
240             CALL iom_swap(      cxios_context          )
241         ENDIF
242
243      ENDIF
244
245   END SUBROUTINE rst_read_open
246
247
248   SUBROUTINE rst_read( Kbb, Kmm )
249      !!----------------------------------------------------------------------
250      !!                   ***  ROUTINE rst_read  ***
251      !!
252      !! ** Purpose :   Read velocity and T-S fields in the restart file
253      !!
254      !! ** Method  :   Read in restart.nc fields which are necessary for restart
255      !!
256      !!                NB: restart file openned           in DOM/domain.F90:dom_init
257      !!                    before field in restart tested in DOM/domain.F90:dom_init
258      !!                    (sshb)
259      !!
260      !!                NB: ssh is read or set in rst_read_ssh
261      !!----------------------------------------------------------------------
262      INTEGER          , INTENT(in) ::   Kbb, Kmm   ! ocean time level indices
263      INTEGER  ::   jk
264      REAL(wp), DIMENSION(jpi, jpj, jpk) :: w3d
265      REAL(wp), ALLOCATABLE, DIMENSION(:,:,:)   :: zgdept       ! 3D workspace for QCO
266      !!----------------------------------------------------------------------
267      !
268      IF(.NOT.lrxios )   CALL iom_delay_rst( 'READ', 'OCE', numror )   ! read only ocean delayed global communication variables
269      !
270      !                             !*  Diurnal DSST
271      IF( ln_diurnal )   CALL iom_get( numror, jpdom_auto, 'Dsst' , x_dsst )
272      IF( ln_diurnal_only ) THEN
273         IF(lwp) WRITE( numout, * ) &
274         &   "rst_read:- ln_diurnal_only set, setting rhop=rho0"
275         rhop = rho0
276         CALL iom_get( numror, jpdom_auto, 'tn'     , w3d )
277         ts(:,:,1,jp_tem,Kmm) = w3d(:,:,1)
278         RETURN
279      ENDIF
280      !
281#if defined key_RK3
282      !                             !*  Read Kbb fields   (NB: in RK3 Kmm = Kbb = Nbb)
283      IF(lwp) WRITE(numout,*) '           Kbb u, v and T-S fields read in the restart file'
284      CALL iom_get( numror, jpdom_auto, 'ub', uu(:,:,:       ,Kbb), cd_type = 'U', psgn = -1._wp )
285      CALL iom_get( numror, jpdom_auto, 'vb', vv(:,:,:       ,Kbb), cd_type = 'V', psgn = -1._wp )
286      CALL iom_get( numror, jpdom_auto, 'tb', ts(:,:,:,jp_tem,Kbb) )
287      CALL iom_get( numror, jpdom_auto, 'sb', ts(:,:,:,jp_sal,Kbb) )
288#else
289      !                             !*  Read Kmm fields   (MLF only)
290      IF(lwp) WRITE(numout,*)    '           Kmm u, v and T-S fields read in the restart file'
291      CALL iom_get( numror, jpdom_auto, 'un', uu(:,:,:       ,Kmm), cd_type = 'U', psgn = -1._wp )
292      CALL iom_get( numror, jpdom_auto, 'vn', vv(:,:,:       ,Kmm), cd_type = 'V', psgn = -1._wp )
293      CALL iom_get( numror, jpdom_auto, 'tn', ts(:,:,:,jp_tem,Kmm) )
294      CALL iom_get( numror, jpdom_auto, 'sn', ts(:,:,:,jp_sal,Kmm) )
295      !
296      IF( l_1st_euler ) THEN        !*  Euler restart   (MLF only)
297         IF(lwp) WRITE(numout,*) '           Kbb u, v and T-S fields set to Kmm values'
298         uu(:,:,:  ,Kbb) = uu(:,:,:  ,Kmm)         ! all before fields set to now values
299         vv(:,:,:  ,Kbb) = vv(:,:,:  ,Kmm)
300         ts(:,:,:,:,Kbb) = ts(:,:,:,:,Kmm)
301         !
302      ELSE                          !* Leap frog restart   (MLF only)
303         IF(lwp) WRITE(numout,*) '           Kbb u, v and T-S fields read in the restart file'
304         CALL iom_get( numror, jpdom_auto, 'ub', uu(:,:,:       ,Kbb), cd_type = 'U', psgn = -1._wp )
305         CALL iom_get( numror, jpdom_auto, 'vb', vv(:,:,:       ,Kbb), cd_type = 'V', psgn = -1._wp )
306         CALL iom_get( numror, jpdom_auto, 'tb', ts(:,:,:,jp_tem,Kbb) )
307         CALL iom_get( numror, jpdom_auto, 'sb', ts(:,:,:,jp_sal,Kbb) )
308      ENDIF
309#endif
310      !
311      IF( .NOT.lk_SWE ) THEN
312         IF( iom_varid( numror, 'rhop', ldstop = .FALSE. ) > 0 ) THEN
313            CALL iom_get( numror, jpdom_auto, 'rhop'   , rhop )   ! now    potential density
314         ELSE
315#if defined key_qco
316            ALLOCATE( zgdept(jpi,jpj,jpk) )
317            DO jk = 1, jpk
318               zgdept(:,:,jk) = gdept(:,:,jk,Kmm)
319            END DO
320            CALL eos( ts(:,:,:,:,Kmm), rhd, rhop, zgdept )
321            DEALLOCATE( zgdept )
322#else
323            CALL eos( ts(:,:,:,:,Kmm), rhd, rhop, gdept(:,:,:,Kmm) )
324#endif
325         ENDIF
326      ENDIF
327      !
328   END SUBROUTINE rst_read
329
330
331   SUBROUTINE rst_read_ssh( Kbb, Kmm, Kaa )
332      !!---------------------------------------------------------------------
333      !!                   ***  ROUTINE rst_read_ssh  ***
334      !!
335      !! ** Purpose :   ssh initialization of the sea surface height (ssh)
336      !!
337      !! ** Method  :   set ssh from restart or read configuration, or user_def
338      !!              * ln_rstart = T
339      !!                   USE of IOM library to read ssh in the restart file
340      !!                   Leap-Frog: Kbb and Kmm are read except for l_1st_euler=T
341      !!
342      !!              * otherwise
343      !!                   call user defined ssh or
344      !!                   set to -ssh_ref in wet and drying case with domcfg.nc
345      !!
346      !!              NB: ssh_b/n are written by restart.F90
347      !!----------------------------------------------------------------------
348      INTEGER, INTENT(in) ::   Kbb, Kmm, Kaa   ! ocean time level indices
349      !
350      INTEGER ::   ji, jj, jk
351      !!----------------------------------------------------------------------
352      !
353      IF(lwp) THEN
354         WRITE(numout,*)
355         WRITE(numout,*) 'rst_read_ssh : ssh initialization'
356         WRITE(numout,*) '~~~~~~~~~~~~ '
357      ENDIF
358      !
359      !                            !=============================!
360      IF( ln_rstart ) THEN         !==  Read the restart file  ==!
361         !                         !=============================!
362         !
363#if defined key_RK3
364         !                                     !*  RK3: Read ssh at Kbb
365         IF(lwp) WRITE(numout,*)
366         IF(lwp) WRITE(numout,*)    '      Kbb sea surface height read in the restart file'
367         CALL iom_get( numror, jpdom_auto, 'sshb'   , ssh(:,:,Kbb) )
368         !
369         !                                     !*  RK3: Set ssh at Kmm for AGRIF
370         ssh(:,:,Kmm) = 0._wp
371#else
372         !                                     !*  MLF: Read ssh at Kmm
373         IF(lwp) WRITE(numout,*)
374         IF(lwp) WRITE(numout,*)    '      Kmm sea surface height read in the restart file'
375         CALL iom_get( numror, jpdom_auto, 'sshn'   , ssh(:,:,Kmm) )
376         !
377         IF( l_1st_euler ) THEN                !*  MLF: Euler at first time-step
378            IF(lwp) WRITE(numout,*)
379            IF(lwp) WRITE(numout,*) '      Euler first time step : ssh(Kbb) = ssh(Kmm)'
380            ssh(:,:,Kbb) = ssh(:,:,Kmm)
381            !
382         ELSE                                  !*  MLF: read ssh at Kbb
383            IF(lwp) WRITE(numout,*)
384            IF(lwp) WRITE(numout,*) '      Kbb sea surface height read in the restart file'
385            CALL iom_get( numror, jpdom_auto, 'sshb', ssh(:,:,Kbb) )
386         ENDIF
387#endif
388         !                         !============================!
389      ELSE                         !==  Initialize at "rest"  ==!
390         !                         !============================!
391         !
392         IF(lwp) WRITE(numout,*)
393         IF(lwp) WRITE(numout,*)    '      initialization at rest'
394         !
395         IF( ll_wd ) THEN                      !* wet and dry
396            !
397            IF( ln_read_cfg  ) THEN                 ! read configuration : ssh_ref is read in domain_cfg file
398!!st  why ssh is not masked : i.e. ssh(:,:,Kmm) = -ssh_ref*ssmask(:,:),
399!!st  since at the 1st time step lbclnk will be applied on ssh at Kaa but not initially at Kbb and Kmm
400               ssh(:,:,Kbb) = -ssh_ref
401               !
402               DO_2D( nn_hls, nn_hls, nn_hls, nn_hls )
403                  IF( ht_0(ji,jj)-ssh_ref <  rn_wdmin1 ) THEN   ! if total depth is less than min depth
404                     ssh(ji,jj,Kbb) = rn_wdmin1 - ht_0(ji,jj)
405                  ENDIF
406               END_2D
407            ELSE                                    ! user define configuration case 
408               CALL usr_def_istate_ssh( tmask, ssh(:,:,Kbb) )
409            ENDIF
410            !
411         ELSE                                  !* user defined configuration
412            CALL usr_def_istate_ssh( tmask, ssh(:,:,Kbb) )
413            !
414         ENDIF
415         !
416#if defined key_RK3
417         ssh(:,:,Kmm) = 0._wp                  !* RK3: set Kmm to 0 for AGRIF
418#else
419         ssh(:,:,Kmm) = ssh(:,:,Kbb)           !* MLF: set now values from to before ones
420#endif
421      ENDIF
422      !
423      !                            !==========================!
424      ssh(:,:,Kaa) = 0._wp         !==  Set to 0 for AGRIF  ==!
425      !                            !==========================!
426      !
427   END SUBROUTINE rst_read_ssh
428
429   !!=====================================================================
430END MODULE restart
Note: See TracBrowser for help on using the repository browser.