restart.F90 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. MODULE 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. !!----------------------------------------------------------------------
  12. !!----------------------------------------------------------------------
  13. !! rst_opn : open the ocean restart file
  14. !! rst_write : write the ocean restart file
  15. !! rst_read : read the ocean restart file
  16. !!----------------------------------------------------------------------
  17. USE oce ! ocean dynamics and tracers
  18. USE dom_oce ! ocean space and time domain
  19. USE phycst ! physical constants
  20. USE in_out_manager ! I/O manager
  21. USE iom ! I/O module
  22. USE eosbn2 ! equation of state (eos bn2 routine)
  23. USE trdmxl_oce ! ocean active mixed layer tracers trends variables
  24. USE divcur ! hor. divergence and curl (div & cur routines)
  25. IMPLICIT NONE
  26. PRIVATE
  27. PUBLIC rst_opn ! routine called by step module
  28. PUBLIC rst_write ! routine called by step module
  29. PUBLIC rst_read ! routine called by istate module
  30. PUBLIC rst_read_open ! routine called in rst_read and (possibly) in dom_vvl_init
  31. !! * Substitutions
  32. # include "domzgr_substitute.h90"
  33. # include "vectopt_loop_substitute.h90"
  34. !!----------------------------------------------------------------------
  35. !! NEMO/OPA 3.3 , NEMO Consortium (2010)
  36. !! $Id: restart.F90 4990 2014-12-15 16:42:49Z timgraham $
  37. !! Software governed by the CeCILL licence (NEMOGCM/NEMO_CeCILL.txt)
  38. !!----------------------------------------------------------------------
  39. CONTAINS
  40. SUBROUTINE rst_opn( kt )
  41. !!---------------------------------------------------------------------
  42. !! *** ROUTINE rst_opn ***
  43. !!
  44. !! ** Purpose : + initialization (should be read in the namelist) of nitrst
  45. !! + open the restart when we are one time step before nitrst
  46. !! - restart header is defined when kt = nitrst-1
  47. !! - restart data are written when kt = nitrst
  48. !! + define lrst_oce to .TRUE. when we need to define or write the restart
  49. !!----------------------------------------------------------------------
  50. INTEGER, INTENT(in) :: kt ! ocean time-step
  51. !!
  52. CHARACTER(LEN=20) :: clkt ! ocean time-step deine as a character
  53. CHARACTER(LEN=50) :: clname ! ocean output restart file name
  54. CHARACTER(lc) :: clpath ! full path to ocean output restart file
  55. !!----------------------------------------------------------------------
  56. !
  57. IF( kt == nit000 ) THEN ! default definitions
  58. lrst_oce = .FALSE.
  59. IF( ln_rst_list ) THEN
  60. nrst_lst = 1
  61. nitrst = nstocklist( nrst_lst )
  62. ELSE
  63. nitrst = nitend
  64. ENDIF
  65. ENDIF
  66. ! frequency-based restart dumping (nn_stock)
  67. IF( .NOT. ln_rst_list .AND. MOD( kt - 1, nstock ) == 0 ) THEN
  68. ! we use kt - 1 and not kt - nit000 to keep the same periodicity from the beginning of the experiment
  69. nitrst = kt + nstock - 1 ! define the next value of nitrst for restart writing
  70. IF( nitrst > nitend ) nitrst = nitend ! make sure we write a restart at the end of the run
  71. ENDIF
  72. ! to get better performances with NetCDF format:
  73. ! we open and define the ocean restart file one time step before writing the data (-> at nitrst - 1)
  74. ! except if we write ocean restart files every time step or if an ocean restart file was writen at nitend - 1
  75. IF( kt == nitrst - 1 .OR. nstock == 1 .OR. ( kt == nitend .AND. .NOT. lrst_oce ) ) THEN
  76. IF( nitrst <= nitend .AND. nitrst > 0 ) THEN
  77. ! beware of the format used to write kt (default is i8.8, that should be large enough...)
  78. IF( nitrst > 999999999 ) THEN ; WRITE(clkt, * ) nitrst
  79. ELSE ; WRITE(clkt, '(i8.8)') nitrst
  80. ENDIF
  81. ! create the file
  82. clname = TRIM(cexper)//"_"//TRIM(ADJUSTL(clkt))//"_"//TRIM(cn_ocerst_out)
  83. clpath = TRIM(cn_ocerst_outdir)
  84. IF( clpath(LEN_TRIM(clpath):) /= '/' ) clpath = TRIM(clpath) // '/'
  85. IF(lwp) THEN
  86. WRITE(numout,*)
  87. SELECT CASE ( jprstlib )
  88. CASE ( jprstdimg ) ; WRITE(numout,*) &
  89. ' open ocean restart binary file: ',TRIM(clpath)//clname
  90. CASE DEFAULT ; WRITE(numout,*) &
  91. ' open ocean restart NetCDF file: ',TRIM(clpath)//clname
  92. END SELECT
  93. IF ( snc4set%luse ) WRITE(numout,*) ' opened for NetCDF4 chunking and compression'
  94. IF( kt == nitrst - 1 ) THEN ; WRITE(numout,*) ' kt = nitrst - 1 = ', kt
  95. ELSE ; WRITE(numout,*) ' kt = ' , kt
  96. ENDIF
  97. ENDIF
  98. !
  99. CALL iom_open( TRIM(clpath)//TRIM(clname), numrow, ldwrt = .TRUE., kiolib = jprstlib )
  100. lrst_oce = .TRUE.
  101. ENDIF
  102. ENDIF
  103. !
  104. END SUBROUTINE rst_opn
  105. SUBROUTINE rst_write( kt )
  106. !!---------------------------------------------------------------------
  107. !! *** ROUTINE rstwrite ***
  108. !!
  109. !! ** Purpose : Write restart fields in the format corresponding to jprstlib
  110. !!
  111. !! ** Method : Write in numrow when kt == nitrst in NetCDF
  112. !! file, save fields which are necessary for restart
  113. !!----------------------------------------------------------------------
  114. INTEGER, INTENT(in) :: kt ! ocean time-step
  115. !!----------------------------------------------------------------------
  116. CALL iom_rstput( kt, nitrst, numrow, 'rdt' , rdt ) ! dynamics time step
  117. CALL iom_rstput( kt, nitrst, numrow, 'rdttra1', rdttra(1) ) ! surface tracer time step
  118. CALL iom_rstput( kt, nitrst, numrow, 'ub' , ub ) ! before fields
  119. CALL iom_rstput( kt, nitrst, numrow, 'vb' , vb )
  120. CALL iom_rstput( kt, nitrst, numrow, 'tb' , tsb(:,:,:,jp_tem) )
  121. CALL iom_rstput( kt, nitrst, numrow, 'sb' , tsb(:,:,:,jp_sal) )
  122. CALL iom_rstput( kt, nitrst, numrow, 'rotb' , rotb )
  123. CALL iom_rstput( kt, nitrst, numrow, 'hdivb' , hdivb )
  124. CALL iom_rstput( kt, nitrst, numrow, 'sshb' , sshb )
  125. !
  126. CALL iom_rstput( kt, nitrst, numrow, 'un' , un ) ! now fields
  127. CALL iom_rstput( kt, nitrst, numrow, 'vn' , vn )
  128. CALL iom_rstput( kt, nitrst, numrow, 'tn' , tsn(:,:,:,jp_tem) )
  129. CALL iom_rstput( kt, nitrst, numrow, 'sn' , tsn(:,:,:,jp_sal) )
  130. CALL iom_rstput( kt, nitrst, numrow, 'rotn' , rotn )
  131. CALL iom_rstput( kt, nitrst, numrow, 'hdivn' , hdivn )
  132. CALL iom_rstput( kt, nitrst, numrow, 'sshn' , sshn )
  133. CALL iom_rstput( kt, nitrst, numrow, 'rhop' , rhop )
  134. #if defined key_zdfkpp
  135. CALL iom_rstput( kt, nitrst, numrow, 'rhd' , rhd )
  136. #endif
  137. IF( kt == nitrst ) THEN
  138. CALL iom_close( numrow ) ! close the restart file (only at last time step)
  139. !!gm IF( .NOT. lk_trdmld ) lrst_oce = .FALSE.
  140. !!gm not sure what to do here ===>>> ask to Sebastian
  141. lrst_oce = .FALSE.
  142. IF( ln_rst_list ) THEN
  143. nrst_lst = MIN(nrst_lst + 1, SIZE(nstocklist,1))
  144. nitrst = nstocklist( nrst_lst )
  145. ENDIF
  146. lrst_oce = .FALSE.
  147. ENDIF
  148. !
  149. END SUBROUTINE rst_write
  150. SUBROUTINE rst_read_open
  151. !!----------------------------------------------------------------------
  152. !! *** ROUTINE rst_read_open ***
  153. !!
  154. !! ** Purpose : Open read files for restart (format fixed by jprstlib )
  155. !!
  156. !! ** Method : Use a non-zero, positive value of numror to assess whether or not
  157. !! the file has already been opened
  158. !!----------------------------------------------------------------------
  159. INTEGER :: jlibalt = jprstlib
  160. LOGICAL :: llok
  161. CHARACTER(lc) :: clpath ! full path to ocean output restart file
  162. !!----------------------------------------------------------------------
  163. !
  164. IF( numror <= 0 ) THEN
  165. IF(lwp) THEN ! Contol prints
  166. WRITE(numout,*)
  167. SELECT CASE ( jprstlib )
  168. CASE ( jpnf90 ) ; WRITE(numout,*) 'rst_read : read oce NetCDF restart file'
  169. CASE ( jprstdimg ) ; WRITE(numout,*) 'rst_read : read oce binary restart file'
  170. END SELECT
  171. IF ( snc4set%luse ) WRITE(numout,*) 'rst_read : configured with NetCDF4 support'
  172. WRITE(numout,*) '~~~~~~~~'
  173. ENDIF
  174. clpath = TRIM(cn_ocerst_indir)
  175. IF( clpath(LEN_TRIM(clpath):) /= '/' ) clpath = TRIM(clpath) // '/'
  176. IF ( jprstlib == jprstdimg ) THEN
  177. ! eventually read netcdf file (monobloc) for restarting on different number of processors
  178. ! if {cn_ocerst_in}.nc exists, then set jlibalt to jpnf90
  179. INQUIRE( FILE = TRIM(cn_ocerst_indir)//'/'//TRIM(cn_ocerst_in)//'.nc', EXIST = llok )
  180. IF ( llok ) THEN ; jlibalt = jpnf90 ; ELSE ; jlibalt = jprstlib ; ENDIF
  181. ENDIF
  182. CALL iom_open( TRIM(clpath)//cn_ocerst_in, numror, kiolib = jlibalt )
  183. ENDIF
  184. END SUBROUTINE rst_read_open
  185. SUBROUTINE rst_read
  186. !!----------------------------------------------------------------------
  187. !! *** ROUTINE rst_read ***
  188. !!
  189. !! ** Purpose : Read files for restart (format fixed by jprstlib )
  190. !!
  191. !! ** Method : Read in restart.nc file fields which are necessary for restart
  192. !!----------------------------------------------------------------------
  193. REAL(wp) :: zrdt, zrdttra1
  194. INTEGER :: jk
  195. LOGICAL :: llok
  196. !!----------------------------------------------------------------------
  197. CALL rst_read_open ! open restart for reading (if not already opened)
  198. ! Check dynamics and tracer time-step consistency and force Euler restart if changed
  199. IF( iom_varid( numror, 'rdt', ldstop = .FALSE. ) > 0 ) THEN
  200. CALL iom_get( numror, 'rdt', zrdt )
  201. IF( zrdt /= rdt ) neuler = 0
  202. ENDIF
  203. IF( iom_varid( numror, 'rdttra1', ldstop = .FALSE. ) > 0 ) THEN
  204. CALL iom_get( numror, 'rdttra1', zrdttra1 )
  205. IF( zrdttra1 /= rdttra(1) ) neuler = 0
  206. ENDIF
  207. !
  208. IF( iom_varid( numror, 'ub', ldstop = .FALSE. ) > 0 ) THEN
  209. CALL iom_get( numror, jpdom_autoglo, 'ub' , ub ) ! before fields
  210. CALL iom_get( numror, jpdom_autoglo, 'vb' , vb )
  211. CALL iom_get( numror, jpdom_autoglo, 'tb' , tsb(:,:,:,jp_tem) )
  212. CALL iom_get( numror, jpdom_autoglo, 'sb' , tsb(:,:,:,jp_sal) )
  213. CALL iom_get( numror, jpdom_autoglo, 'rotb' , rotb )
  214. CALL iom_get( numror, jpdom_autoglo, 'hdivb' , hdivb )
  215. CALL iom_get( numror, jpdom_autoglo, 'sshb' , sshb )
  216. ELSE
  217. neuler = 0
  218. ENDIF
  219. !
  220. CALL iom_get( numror, jpdom_autoglo, 'un' , un ) ! now fields
  221. CALL iom_get( numror, jpdom_autoglo, 'vn' , vn )
  222. CALL iom_get( numror, jpdom_autoglo, 'tn' , tsn(:,:,:,jp_tem) )
  223. CALL iom_get( numror, jpdom_autoglo, 'sn' , tsn(:,:,:,jp_sal) )
  224. CALL iom_get( numror, jpdom_autoglo, 'sshn' , sshn )
  225. IF( iom_varid( numror, 'rotn', ldstop = .FALSE. ) > 0 ) THEN
  226. CALL iom_get( numror, jpdom_autoglo, 'rotn' , rotn )
  227. CALL iom_get( numror, jpdom_autoglo, 'hdivn' , hdivn )
  228. ELSE
  229. CALL div_cur( 0 ) ! Horizontal divergence & Relative vorticity
  230. ENDIF
  231. IF( iom_varid( numror, 'rhop', ldstop = .FALSE. ) > 0 ) THEN
  232. CALL iom_get( numror, jpdom_autoglo, 'rhop' , rhop ) ! now potential density
  233. ELSE
  234. CALL eos ( tsn, rhd, rhop, fsdept_n(:,:,:) )
  235. ENDIF
  236. #if defined key_zdfkpp
  237. IF( iom_varid( numror, 'rhd', ldstop = .FALSE. ) > 0 ) THEN
  238. CALL iom_get( numror, jpdom_autoglo, 'rhd' , rhd ) ! now in situ density anomaly
  239. ELSE
  240. CALL eos( tsn, rhd, fsdept_n(:,:,:) ) ! compute rhd
  241. ENDIF
  242. #endif
  243. !
  244. IF( neuler == 0 ) THEN ! Euler restart (neuler=0)
  245. tsb (:,:,:,:) = tsn (:,:,:,:) ! all before fields set to now values
  246. ub (:,:,:) = un (:,:,:)
  247. vb (:,:,:) = vn (:,:,:)
  248. rotb (:,:,:) = rotn (:,:,:)
  249. hdivb(:,:,:) = hdivn(:,:,:)
  250. sshb (:,:) = sshn (:,:)
  251. IF( lk_vvl ) THEN
  252. DO jk = 1, jpk
  253. fse3t_b(:,:,jk) = fse3t_n(:,:,jk)
  254. END DO
  255. ENDIF
  256. ENDIF
  257. !
  258. END SUBROUTINE rst_read
  259. !!=====================================================================
  260. END MODULE restart