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.
icerst.F90 in NEMO/branches/2020/dev_12905_xios_restart/src/ICE – NEMO

source: NEMO/branches/2020/dev_12905_xios_restart/src/ICE/icerst.F90 @ 12969

Last change on this file since 12969 was 12969, checked in by andmirek, 4 years ago

ticket #2462: read restart with XIOS independently for each component

  • Property svn:keywords set to Id
File size: 15.5 KB
RevLine 
[8586]1MODULE icerst
2   !!======================================================================
3   !!                     ***  MODULE  icerst  ***
4   !!   sea-ice :  write/read the ice restart file
5   !!======================================================================
[9604]6   !! History:   4.0  !  2018     (many people)      SI3 [aka Sea Ice cube]
[8586]7   !!----------------------------------------------------------------------
[9570]8#if defined key_si3
[8586]9   !!----------------------------------------------------------------------
[9570]10   !!   'key_si3'                                       SI3 sea-ice model
[8586]11   !!----------------------------------------------------------------------
12   !!   ice_rst_opn   : open  restart file
13   !!   ice_rst_write : write restart file
14   !!   ice_rst_read  : read  restart file
15   !!----------------------------------------------------------------------
[11536]16   USE ice            ! sea-ice: variables
[8586]17   USE dom_oce        ! ocean domain
[11536]18   USE phycst  , ONLY : rt0
[8948]19   USE sbc_oce , ONLY : nn_fsbc, ln_cpl
[11536]20   USE iceistate      ! sea-ice: initial state
21   USE icectl         ! sea-ice: control
[8586]22   !
23   USE in_out_manager ! I/O manager
24   USE iom            ! I/O manager library
25   USE lib_mpp        ! MPP library
26   USE lib_fortran    ! fortran utilities (glob_sum + no signed zero)
27
28   IMPLICIT NONE
29   PRIVATE
30
31   PUBLIC   ice_rst_opn     ! called by icestp
32   PUBLIC   ice_rst_write   ! called by icestp
33   PUBLIC   ice_rst_read    ! called by ice_init
34
35   !!----------------------------------------------------------------------
[9598]36   !! NEMO/ICE 4.0 , NEMO Consortium (2018)
[10069]37   !! $Id$
[10068]38   !! Software governed by the CeCILL license (see ./LICENSE)
[8586]39   !!----------------------------------------------------------------------
40CONTAINS
41
42   SUBROUTINE ice_rst_opn( kt )
43      !!----------------------------------------------------------------------
44      !!                    ***  ice_rst_opn  ***
45      !!
46      !! ** purpose  :   open restart file
47      !!----------------------------------------------------------------------
48      INTEGER, INTENT(in) ::   kt       ! number of iteration
49      !
50      CHARACTER(len=20)   ::   clkt     ! ocean time-step define as a character
51      CHARACTER(len=50)   ::   clname   ! ice output restart file name
52      CHARACTER(len=256)  ::   clpath   ! full path to ice output restart file
[12957]53      CHARACTER(LEN=52)   ::   clpname   ! ocean output restart file name including prefix for AGRIF
[8586]54      !!----------------------------------------------------------------------
55      !
56      IF( kt == nit000 )   lrst_ice = .FALSE.   ! default definition
57
[11536]58      IF( ln_rst_list .OR. nn_stock /= -1 ) THEN
[8586]59      ! in order to get better performances with NetCDF format, we open and define the ice restart file
60      ! one ice time step before writing the data (-> at nitrst - 2*nn_fsbc + 1), except if we write ice
61      ! restart files every ice time step or if an ice restart file was writen at nitend - 2*nn_fsbc + 1
[11536]62      IF( kt == nitrst - 2*nn_fsbc + 1 .OR. nn_stock == nn_fsbc    &
[8586]63         &                             .OR. ( kt == nitend - nn_fsbc + 1 .AND. .NOT. lrst_ice ) ) THEN
64         IF( nitrst <= nitend .AND. nitrst > 0 ) THEN
65            ! beware of the format used to write kt (default is i8.8, that should be large enough...)
66            IF( nitrst > 99999999 ) THEN   ;   WRITE(clkt, *       ) nitrst
67            ELSE                           ;   WRITE(clkt, '(i8.8)') nitrst
68            ENDIF
69            ! create the file
70            clname = TRIM(cexper)//"_"//TRIM(ADJUSTL(clkt))//"_"//TRIM(cn_icerst_out)
71            clpath = TRIM(cn_icerst_outdir) 
72            IF( clpath(LEN_TRIM(clpath):) /= '/' ) clpath = TRIM(clpath)//'/'
73            IF(lwp) THEN
74               WRITE(numout,*)
[10425]75               WRITE(numout,*) '             open ice restart NetCDF file: ',TRIM(clpath)//clname
[8586]76               IF( kt == nitrst - 2*nn_fsbc + 1 ) THEN
77                  WRITE(numout,*) '             kt = nitrst - 2*nn_fsbc + 1 = ', kt,' date= ', ndastp
78               ELSE
79                  WRITE(numout,*) '             kt = '                         , kt,' date= ', ndastp
80               ENDIF
81            ENDIF
82            !
[12957]83            IF(.NOT.lwxios) THEN
84               CALL iom_open( TRIM(clpath)//TRIM(clname), numriw, ldwrt = .TRUE., kdlev = jpl, cdcomp = 'ICE' )
85            ELSE
86#if defined key_iomput
87               cwixios_context = "rstwi_"//TRIM(ADJUSTL(clkt))
88               IF( TRIM(Agrif_CFixed()) == '0' ) THEN
89                  clpname = clname
90               ELSE
91                  clpname = TRIM(Agrif_CFixed())//"_"//clname
92               ENDIF
[12961]93               CALL iom_init( cwixios_context, TRIM(clpath)//TRIM(clpname), ld_tmppatch = .false.,&
94                                                                            ld_closedef = .FALSE. )
[12957]95               CALL iom_swap(      cxios_context          )
96#else
97               clinfo = 'Can not use XIOS in rst_opn'
98               CALL ctl_stop(TRIM(clinfo))
99#endif
100            ENDIF
[8586]101            lrst_ice = .TRUE.
102         ENDIF
103      ENDIF
[11536]104      ENDIF
[8586]105      !
106      IF( ln_icectl )   CALL ice_prt( kt, iiceprt, jiceprt, 1, ' - Beginning the time step - ' )   ! control print
[8817]107      !
[8586]108   END SUBROUTINE ice_rst_opn
109
110
111   SUBROUTINE ice_rst_write( kt )
112      !!----------------------------------------------------------------------
113      !!                    ***  ice_rst_write  ***
114      !!
115      !! ** purpose  :   write restart file
116      !!----------------------------------------------------------------------
117      INTEGER, INTENT(in) ::   kt     ! number of iteration
118      !!
[10425]119      INTEGER ::   jk    ! dummy loop indices
[8586]120      INTEGER ::   iter
121      CHARACTER(len=25) ::   znam
122      CHARACTER(len=2)  ::   zchar, zchar1
[8817]123      REAL(wp), DIMENSION(jpi,jpj,jpl) ::   z3d   ! 3D workspace
[8586]124      !!----------------------------------------------------------------------
125
126      iter = kt + nn_fsbc - 1   ! ice restarts are written at kt == nitrst - nn_fsbc + 1
127
128      IF( iter == nitrst ) THEN
129         IF(lwp) WRITE(numout,*)
130         IF(lwp) WRITE(numout,*) 'ice_rst_write : write ice restart file  kt =', kt
131         IF(lwp) WRITE(numout,*) '~~~~~~~~~~~~~~'         
132      ENDIF
133
134      ! Write in numriw (if iter == nitrst)
135      ! ------------------
136      !                                                                        ! calendar control
[12957]137      IF( lwxios ) CALL iom_swap(      cwixios_context         )
138      CALL iom_rstput( iter, nitrst, numriw, 'nn_fsbc', REAL( nn_fsbc, wp ), ldxios = lwxios )      ! time-step
139      CALL iom_rstput( iter, nitrst, numriw, 'kt_ice' , REAL( iter   , wp ), ldxios = lwxios )      ! date
[10425]140      CALL iom_delay_rst( 'WRITE', 'ICE', numriw )   ! save only ice delayed global communication variables
[8586]141
[8817]142      ! Prognostic variables
[12957]143      CALL iom_rstput( iter, nitrst, numriw, 'v_i'  , v_i,   ldxios = lwxios )
144      CALL iom_rstput( iter, nitrst, numriw, 'v_s'  , v_s,   ldxios = lwxios )
145      CALL iom_rstput( iter, nitrst, numriw, 'sv_i' , sv_i,  ldxios = lwxios )
146      CALL iom_rstput( iter, nitrst, numriw, 'a_i'  , a_i,   ldxios = lwxios )
147      CALL iom_rstput( iter, nitrst, numriw, 't_su' , t_su,  ldxios = lwxios )
148      CALL iom_rstput( iter, nitrst, numriw, 'u_ice', u_ice, ldxios = lwxios )
149      CALL iom_rstput( iter, nitrst, numriw, 'v_ice', v_ice, ldxios = lwxios )
150      CALL iom_rstput( iter, nitrst, numriw, 'oa_i' , oa_i,  ldxios = lwxios )
151      CALL iom_rstput( iter, nitrst, numriw, 'a_ip' , a_ip,  ldxios = lwxios )
152      CALL iom_rstput( iter, nitrst, numriw, 'v_ip' , v_ip,  ldxios = lwxios )
[9271]153      ! Snow enthalpy
154      DO jk = 1, nlay_s 
155         WRITE(zchar1,'(I2.2)') jk
156         znam = 'e_s'//'_l'//zchar1
157         z3d(:,:,:) = e_s(:,:,jk,:)
[12957]158         CALL iom_rstput( iter, nitrst, numriw, znam , z3d, ldxios = lwxios )
[9271]159      END DO
160      ! Ice enthalpy
[8817]161      DO jk = 1, nlay_i 
162         WRITE(zchar1,'(I2.2)') jk
[9123]163         znam = 'e_i'//'_l'//zchar1
[8817]164         z3d(:,:,:) = e_i(:,:,jk,:)
[12957]165         CALL iom_rstput( iter, nitrst, numriw, znam , z3d, ldxios = lwxios )
[8586]166      END DO
[8948]167      ! fields needed for Met Office (Jules) coupling
168      IF( ln_cpl ) THEN
[12957]169         CALL iom_rstput( iter, nitrst, numriw, 'cnd_ice', cnd_ice, ldxios = lwxios )
170         CALL iom_rstput( iter, nitrst, numriw, 't1_ice' , t1_ice,  ldxios = lwxios )
[8933]171      ENDIF
[12957]172      IF( lwxios ) CALL iom_swap(      cxios_context         )
[8933]173      !
174
[8586]175      ! close restart file
176      ! ------------------
177      IF( iter == nitrst ) THEN
[12957]178         IF(.NOT.lwxios) THEN
179            CALL iom_close( numriw )
180         ELSE
181            CALL iom_context_finalize(      cwixios_context          )
182         ENDIF
[8586]183         lrst_ice = .FALSE.
184      ENDIF
185      !
186   END SUBROUTINE ice_rst_write
187
188
[12377]189   SUBROUTINE ice_rst_read( Kbb, Kmm, Kaa )
[8586]190      !!----------------------------------------------------------------------
191      !!                    ***  ice_rst_read  ***
192      !!
193      !! ** purpose  :   read restart file
194      !!----------------------------------------------------------------------
[12377]195      INTEGER, INTENT(in) :: Kbb, Kmm, Kaa ! ocean time level indices
[10425]196      INTEGER           ::   jk
[8817]197      LOGICAL           ::   llok
[11536]198      INTEGER           ::   id0, id1, id2, id3, id4   ! local integer
[8586]199      CHARACTER(len=25) ::   znam
200      CHARACTER(len=2)  ::   zchar, zchar1
[8817]201      REAL(wp)          ::   zfice, ziter
[12957]202      CHARACTER(lc)     ::   clpname
[8817]203      REAL(wp), DIMENSION(jpi,jpj,jpl) ::   z3d   ! 3D workspace
[8586]204      !!----------------------------------------------------------------------
205
206      IF(lwp) THEN
207         WRITE(numout,*)
208         WRITE(numout,*) 'ice_rst_read: read ice NetCDF restart file'
209         WRITE(numout,*) '~~~~~~~~~~~~'
210      ENDIF
211
[12957]212      lxios_sini = .FALSE.
[12649]213      CALL iom_open ( TRIM(cn_icerst_indir)//'/'//cn_icerst_in, numrir )
[8586]214
[12969]215      lrixios = lrxios.AND.lxios_sini
216      IF(lwp) write(numout, *) 'USE XIOS to read restart: ', lrixios
[12957]217      IF(lwp) CALL FLUSH(numout)
[12969]218      IF( lrixios) THEN
[12957]219          crixios_context = 'si3_rst'
220          IF(lwp) WRITE(numout,*) 'Enable restart reading by XIOS for SI3'
221          IF( TRIM(Agrif_CFixed()) == '0' ) THEN
222             clpname = cn_icerst_in
223          ELSE
224             clpname = TRIM(Agrif_CFixed())//"_"//cn_icerst_in   
225          ENDIF
[12961]226          CALL iom_init( crixios_context, fname = TRIM(cn_icerst_indir)//'/'//TRIM(clpname), &
227                                          idfp = iom_file(numrir)%nfid, ld_tmppatch = .TRUE. )
[12957]228      ENDIF
229
[11536]230      ! test if v_i exists
231      id0 = iom_varid( numrir, 'v_i' , ldstop = .FALSE. )
[8586]232
[11536]233      !                    ! ------------------------------ !
234      IF( id0 > 0 ) THEN   ! == case of a normal restart == !
235         !                 ! ------------------------------ !
[12969]236         IF(lrixios) CALL iom_swap(crixios_context) 
[11536]237         ! Time info
[12969]238         CALL iom_get( numrir, 'nn_fsbc', zfice, ldxios = lrixios )
239         CALL iom_get( numrir, 'kt_ice' , ziter, ldxios = lrixios )   
[11536]240         IF(lwp) WRITE(numout,*) '   read ice restart file at time step    : ', ziter
241         IF(lwp) WRITE(numout,*) '   in any case we force it to nit000 - 1 : ', nit000 - 1
[8586]242
[11536]243         ! Control of date
244         IF( ( nit000 - NINT(ziter) ) /= 1 .AND. ABS( nrstdt ) == 1 )   &
245            &     CALL ctl_stop( 'ice_rst_read ===>>>> : problem with nit000 in ice restart',  &
246            &                   '   verify the file or rerun with the value 0 for the',        &
247            &                   '   control of time parameter  nrstdt' )
248         IF( NINT(zfice) /= nn_fsbc          .AND. ABS( nrstdt ) == 1 )   &
249            &     CALL ctl_stop( 'ice_rst_read ===>>>> : problem with nn_fsbc in ice restart',  &
250            &                   '   verify the file or rerun with the value 0 for the',         &
251            &                   '   control of time parameter  nrstdt' )
[10425]252
[11536]253         ! --- mandatory fields --- !
[12969]254         CALL iom_get( numrir, jpdom_autoglo, 'v_i'  , v_i,   ldxios = lrixios )
255         CALL iom_get( numrir, jpdom_autoglo, 'v_s'  , v_s,   ldxios = lrixios )
256         CALL iom_get( numrir, jpdom_autoglo, 'sv_i' , sv_i,  ldxios = lrixios )
257         CALL iom_get( numrir, jpdom_autoglo, 'a_i'  , a_i,   ldxios = lrixios )
258         CALL iom_get( numrir, jpdom_autoglo, 't_su' , t_su,  ldxios = lrixios )
259         CALL iom_get( numrir, jpdom_autoglo, 'u_ice', u_ice, ldxios = lrixios )
260         CALL iom_get( numrir, jpdom_autoglo, 'v_ice', v_ice, ldxios = lrixios )
[11536]261         ! Snow enthalpy
262         DO jk = 1, nlay_s
263            WRITE(zchar1,'(I2.2)') jk
264            znam = 'e_s'//'_l'//zchar1
[12969]265            CALL iom_get( numrir, jpdom_autoglo, znam , z3d, ldxios = lrixios )
[11536]266            e_s(:,:,jk,:) = z3d(:,:,:)
267         END DO
268         ! Ice enthalpy
269         DO jk = 1, nlay_i
270            WRITE(zchar1,'(I2.2)') jk
271            znam = 'e_i'//'_l'//zchar1
[12969]272            CALL iom_get( numrir, jpdom_autoglo, znam , z3d, ldxios = lrixios )
[11536]273            e_i(:,:,jk,:) = z3d(:,:,:)
274         END DO
275         ! -- optional fields -- !
276         ! ice age
277         id1 = iom_varid( numrir, 'oa_i' , ldstop = .FALSE. )
278         IF( id1 > 0 ) THEN                       ! fields exist
[12969]279            CALL iom_get( numrir, jpdom_autoglo, 'oa_i', oa_i, ldxios = lrixios )
[11536]280         ELSE                                     ! start from rest
281            IF(lwp) WRITE(numout,*) '   ==>>   previous run without ice age output then set it to zero'
282            oa_i(:,:,:) = 0._wp
283         ENDIF
284         ! melt ponds
285         id2 = iom_varid( numrir, 'a_ip' , ldstop = .FALSE. )
286         IF( id2 > 0 ) THEN                       ! fields exist
[12969]287            CALL iom_get( numrir, jpdom_autoglo, 'a_ip' , a_ip, ldxios = lrixios )
288            CALL iom_get( numrir, jpdom_autoglo, 'v_ip' , v_ip, ldxios = lrixios )
[11536]289         ELSE                                     ! start from rest
290            IF(lwp) WRITE(numout,*) '   ==>>   previous run without melt ponds output then set it to zero'
291            a_ip(:,:,:) = 0._wp
292            v_ip(:,:,:) = 0._wp
293         ENDIF
294         ! fields needed for Met Office (Jules) coupling
295         IF( ln_cpl ) THEN
296            id3 = iom_varid( numrir, 'cnd_ice' , ldstop = .FALSE. )
297            id4 = iom_varid( numrir, 't1_ice'  , ldstop = .FALSE. )
298            IF( id3 > 0 .AND. id4 > 0 ) THEN         ! fields exist
[12969]299               CALL iom_get( numrir, jpdom_autoglo, 'cnd_ice', cnd_ice, ldxios = lrixios )
300               CALL iom_get( numrir, jpdom_autoglo, 't1_ice' , t1_ice,  ldxios = lrixios )
[11536]301            ELSE                                     ! start from rest
302               IF(lwp) WRITE(numout,*) '   ==>>   previous run without conductivity output then set it to zero'
303               cnd_ice(:,:,:) = 0._wp
304               t1_ice (:,:,:) = rt0
305            ENDIF
306         ENDIF
[10425]307
[11536]308         CALL iom_delay_rst( 'READ', 'ICE', numrir )   ! read only ice delayed global communication variables
[12969]309         IF(lrixios) CALL iom_swap(cxios_context)
[11536]310         !                 ! ---------------------------------- !
311      ELSE                 ! == case of a simplified restart == !
312         !                 ! ---------------------------------- !
313         CALL ctl_warn('ice_rst_read: you are using a simplified ice restart')
314         !
315         CALL ice_istate_init
[12377]316         CALL ice_istate( nit000, Kbb, Kmm, Kaa )
[11536]317         !
318         IF( .NOT.ln_iceini .OR. .NOT.ln_iceini_file ) &
319            &   CALL ctl_stop('STOP', 'ice_rst_read: you need ln_ice_ini=T and ln_iceini_file=T')
320         !
[8933]321      ENDIF
[8586]322
323   END SUBROUTINE ice_rst_read
324
325#else
326   !!----------------------------------------------------------------------
[9570]327   !!   Default option :       Empty module           NO SI3 sea-ice model
[8586]328   !!----------------------------------------------------------------------
329#endif
330
331   !!======================================================================
332END MODULE icerst
Note: See TracBrowser for help on using the repository browser.