source: CONFIG/UNIFORM/v7/IPSLCM7/SOURCES/NEMO/stpctl.F90 @ 6883

Last change on this file since 6883 was 6883, checked in by cetlod, 12 days ago

CM7_work : Bugfix to ensure NEMO-PISCES restartability

File size: 18.5 KB
Line 
1MODULE stpctl
2   !!======================================================================
3   !!                       ***  MODULE  stpctl  ***
4   !! Ocean run control :  gross check of the ocean time stepping
5   !!======================================================================
6   !! History :  OPA  ! 1991-03  (G. Madec) Original code
7   !!            6.0  ! 1992-06  (M. Imbard)
8   !!            8.0  ! 1997-06  (A.M. Treguier)
9   !!   NEMO     1.0  ! 2002-06  (G. Madec)  F90: Free form and module
10   !!            2.0  ! 2009-07  (G. Madec)  Add statistic for time-spliting
11   !!            3.7  ! 2016-09  (G. Madec)  Remove solver
12   !!            4.0  ! 2017-04  (G. Madec)  regroup global communications
13   !!----------------------------------------------------------------------
14
15   !!----------------------------------------------------------------------
16   !!   stp_ctl      : Control the run
17   !!----------------------------------------------------------------------
18   USE oce             ! ocean dynamics and tracers variables
19   USE dom_oce         ! ocean space and time domain variables
20   USE zdf_oce ,  ONLY : ln_zad_Aimp       ! ocean vertical physics variables
21   USE wet_dry,   ONLY : ll_wd, ssh_ref    ! reference depth for negative bathy
22   
23   USE diawri          ! Standard run outputs       (dia_wri_state routine)
24   USE in_out_manager  ! I/O manager
25   USE lbclnk          ! ocean lateral boundary conditions (or mpp link)
26   USE lib_mpp         ! distributed memory computing
27   USE eosbn2, ONLY: ln_SEOS, rn_b0
28   !
29   USE netcdf          ! NetCDF library
30   IMPLICIT NONE
31   PRIVATE
32
33   PUBLIC stp_ctl           ! routine called by step.F90
34
35   INTEGER, PARAMETER         ::   jpvar = 8
36   INTEGER                    ::   nrunid   ! netcdf file id
37   INTEGER, DIMENSION(jpvar)  ::   nvarid   ! netcdf variable id
38   !!----------------------------------------------------------------------
39   !! NEMO/OCE 4.0 , NEMO Consortium (2018)
40   !! $Id: stpctl.F90 15023 2021-06-18 14:35:25Z gsamson $
41   !! Software governed by the CeCILL license (see ./LICENSE)
42   !!----------------------------------------------------------------------
43#  include "single_precision_substitute.h90"
44#  include "single_precision_substitute.h90"
45CONTAINS
46
47   SUBROUTINE stp_ctl( kt, Kmm )
48      !!----------------------------------------------------------------------
49      !!                    ***  ROUTINE stp_ctl  ***
50      !!
51      !! ** Purpose :   Control the run
52      !!
53      !! ** Method  : - Save the time step in numstp
54      !!              - Stop the run IF problem encountered by setting nstop > 0
55      !!                Problems checked: |ssh| maximum larger than 10 m
56      !!                                  |U|   maximum larger than 10 m/s
57      !!                                  negative sea surface salinity
58      !!
59      !! ** Actions :   "time.step" file = last ocean time-step
60      !!                "run.stat"  file = run statistics
61      !!                 nstop indicator sheared among all local domain
62      !!----------------------------------------------------------------------
63      INTEGER, INTENT(in   ) ::   kt       ! ocean time-step index
64      INTEGER, INTENT(in   ) ::   Kmm      ! ocean time level index
65      !!
66      INTEGER, PARAMETER              ::   jptst = 4
67      INTEGER                         ::   ji                                    ! dummy loop indices
68      INTEGER                         ::   idtime, istatus
69      INTEGER , DIMENSION(jptst)      ::   iareasum, iareamin, iareamax
70      INTEGER , DIMENSION(3,jptst)    ::   iloc                                  ! min/max loc indices
71      REAL(dp)                        ::   zzz, zminsal, zmaxsal                 ! local real
72      REAL(dp), DIMENSION(jpvar+1)    ::   zmax
73      REAL(dp), DIMENSION(jptst)      ::   zmaxlocal
74      LOGICAL                         ::   ll_wrtstp, ll_colruns, ll_wrtruns, ll_0oce
75      LOGICAL, DIMENSION(jpi,jpj,jpk) ::   llmsk
76      CHARACTER(len=20)               ::   clname
77      !!----------------------------------------------------------------------
78      IF( nstop > 0 .AND. ngrdstop > -1 )   RETURN   !   stpctl was already called by a child grid
79      !
80      ll_wrtstp  = ( MOD( kt-nit000, sn_cfctl%ptimincr ) == 0 ) .OR. ( kt == nitend )
81      ll_colruns = sn_cfctl%l_runstat .AND. ll_wrtstp .AND. jpnij > 1
82      ll_wrtruns = sn_cfctl%l_runstat .AND. ll_wrtstp .AND. lwm
83      !
84      IF( kt == nit000 ) THEN
85         !
86         IF( lwp ) THEN
87            WRITE(numout,*)
88            WRITE(numout,*) 'stp_ctl : time-stepping control'
89            WRITE(numout,*) '~~~~~~~'
90         ENDIF
91         !                                ! open time.step    ascii file, done only by 1st subdomain
92         IF( lwm )   CALL ctl_opn( numstp, 'time.step', 'REPLACE', 'FORMATTED', 'SEQUENTIAL', -1, numout, lwp, narea )
93         !
94         IF( ll_wrtruns ) THEN
95            !                             ! open run.stat     ascii file, done only by 1st subdomain
96            CALL ctl_opn( numrun, 'run.stat', 'REPLACE', 'FORMATTED', 'SEQUENTIAL', -1, numout, lwp, narea )
97            !                             ! open run.stat.nc netcdf file, done only by 1st subdomain
98         ENDIF
99         !
100      ENDIF
101      !
102      !                                   !==              write current time step              ==!
103      !                                   !==  done only by 1st subdomain at writting timestep  ==!
104      IF( lwm .AND. ll_wrtstp ) THEN
105         WRITE ( numstp, '(1x, i8)' )   kt
106         REWIND( numstp )
107      ENDIF
108      !                                   !==            test of local extrema           ==!
109      !                                   !==  done by all processes at every time step  ==!
110      !
111      llmsk(     1:nn_hls,:,:) = .FALSE.                                          ! exclude halos from the checked region
112      llmsk(Nie0+1:   jpi,:,:) = .FALSE.
113      llmsk(:,     1:nn_hls,:) = .FALSE.
114      llmsk(:,Nje0+1:   jpj,:) = .FALSE.
115      !
116      llmsk(Nis0:Nie0,Njs0:Nje0,1) = ssmask(Nis0:Nie0,Njs0:Nje0) == 1._wp         ! define only the inner domain
117      !
118      ll_0oce = .NOT. ANY( llmsk(:,:,1) )                                         ! no ocean point in the inner domain?
119      !
120      IF( ll_wd ) THEN
121         zmax(1) = MAXVAL( ABS( ssh(:,:,Kmm) + ssh_ref ), mask = llmsk(:,:,1) )   ! ssh max
122      ELSE
123         zmax(1) = MAXVAL( ABS( ssh(:,:,Kmm)           ), mask = llmsk(:,:,1) )   ! ssh max
124      ENDIF
125      llmsk(Nis0:Nie0,Njs0:Nje0,:) = umask(Nis0:Nie0,Njs0:Nje0,:) == 1._wp        ! define only the inner domain
126      zmax(2) = MAXVAL(  ABS( uu(:,:,:,Kmm) ), mask = llmsk )                     ! velocity max (zonal only)
127      llmsk(Nis0:Nie0,Njs0:Nje0,:) = tmask(Nis0:Nie0,Njs0:Nje0,:) == 1._wp        ! define only the inner domain
128      zmax(3) = MAXVAL( -ts(:,:,:,jp_sal,Kmm), mask = llmsk )                     ! minus salinity max
129      zmax(4) = MAXVAL(  ts(:,:,:,jp_sal,Kmm), mask = llmsk )                     !       salinity max
130      IF( ll_colruns .OR. jpnij == 1 ) THEN     ! following variables are used only in the netcdf file
131         zmax(5) = MAXVAL( -ts(:,:,:,jp_tem,Kmm), mask = llmsk )                  ! minus temperature max
132         zmax(6) = MAXVAL(  ts(:,:,:,jp_tem,Kmm), mask = llmsk )                  !       temperature max
133         IF( ln_zad_Aimp ) THEN
134            zmax(7) = MAXVAL(   Cu_adv(:,:,:)   , mask = llmsk )                  ! partitioning coeff. max
135            llmsk(:,:,:) = wmask(:,:,:) == 1._wp
136            zmax(8) = MAXVAL(  ABS( wi(:,:,:) ) , mask = llmsk )                  ! implicit vertical vel. max
137         ELSE
138            zmax(7:8) = 0._wp
139         ENDIF
140      ELSE
141         zmax(5:8) = 0._wp
142      ENDIF
143      zmax(jpvar+1) = REAL( nstop, wp )                                           ! stop indicator
144      !
145      !                                   !==               get global extrema             ==!
146      !                                   !==  done by all processes if writting run.stat  ==!
147      IF( ll_colruns ) THEN
148         zmaxlocal(:) = zmax(1:jptst)
149         CALL mpp_max( "stpctl", zmax )          ! max over the global domain: ok even of ll_0oce = .true.
150         nstop = NINT( zmax(jpvar+1) )           ! update nstop indicator (now sheared among all local domains)
151      ELSE
152         ! if no ocean point: MAXVAL returns -HUGE => we must overwrite this value to avoid error handling bellow.
153         IF( ll_0oce )   zmax(1:jptst) = (/ 0._wp, 0._wp, -1._wp, 1._wp /)   ! default "valid" values...
154      ENDIF
155      !
156      zmax(3) = -zmax(3)                              ! move back from max(-zz) to min(zz) : easier to manage!
157      zmax(5) = -zmax(5)                              ! move back from max(-zz) to min(zz) : easier to manage!
158      IF( ll_colruns ) zmaxlocal(3) = -zmaxlocal(3)   ! move back from max(-zz) to min(zz) : easier to manage!
159      !
160      !                                   !==              write "run.stat" files              ==!
161      !                                   !==  done only by 1st subdomain at writting timestep  ==!
162      IF( ll_wrtruns ) THEN
163         WRITE(numrun,9500) kt, zmax(1:jptst)
164         IF( jpnij == 1 ) CALL FLUSH(numrun)
165      END IF
166      !                                   !==               error handling               ==!
167      !                                   !==  done by all processes at every time step  ==!
168      !
169      IF ( ln_SEOS.AND.(rn_b0==0._wp) ) THEN             ! Discard checks on salinity
170         zmaxsal =  HUGE(1._wp)                               ! if not used in eos
171         zminsal = -HUGE(1._wp)
172      ELSE
173         zmaxsal = 100._wp
174         zminsal =   0._wp
175      ENDIF 
176      !
177      IF(  zmax(1) >   20._wp .OR.   &                        ! too large sea surface height ( > 20 m )
178         & zmax(2) >   10._wp .OR.   &                        ! too large velocity ( > 10 m/s)
179         & zmax(3) <= zminsal .OR.   &                        ! negative or zero sea surface salinity
180         & zmax(4) >= zmaxsal .OR.   &                        ! too large sea surface salinity ( > 100 )
181         & zmax(4) <  zminsal .OR.   &                        ! too large sea surface salinity (keep this line for sea-ice)
182         & ISNAN( SUM(zmax(1:jptst)) ) .OR.   &               ! NaN encounter in the tests
183         & ABS(   SUM(zmax(1:jptst)) ) > HUGE(1._wp) ) THEN   ! Infinity encounter in the tests
184         !
185         iloc(:,:) = 0
186         IF( ll_colruns ) THEN   ! zmax is global, so it is the same on all subdomains -> no dead lock with mpp_maxloc
187            ! first: close the netcdf file, so we can read it
188            ! get global loc on the min/max
189            llmsk(Nis0:Nie0,Njs0:Nje0,1) = ssmask(Nis0:Nie0,Njs0:Nje0 ) == 1._wp         ! define only the inner domain
190            CALL mpp_maxloc( 'stpctl', ABS(ssh(:,:,         Kmm)), llmsk(:,:,1), zzz, iloc(1:2,1) )   ! mpp_maxloc ok if mask = F
191            llmsk(Nis0:Nie0,Njs0:Nje0,:) = umask(Nis0:Nie0,Njs0:Nje0,:) == 1._wp        ! define only the inner domain
192 CALL mpp_maxloc( 'stpctl', CASTDP(ABS( uu(:,:,:, Kmm))), llmsk(:,:,:), zzz, iloc(1:3,2) )
193            llmsk(Nis0:Nie0,Njs0:Nje0,:) = tmask(Nis0:Nie0,Njs0:Nje0,:) == 1._wp        ! define only the inner domain
194            CALL mpp_minloc( 'stpctl',      ts(:,:,:,jp_sal,Kmm) , llmsk(:,:,:), zzz, iloc(1:3,3) )
195            CALL mpp_maxloc( 'stpctl',      CASTDP(ts(:,:,:,jp_sal,Kmm)) , llmsk(:,:,:), zzz, iloc(1:3,4) )
196            ! find which subdomain has the max.
197            iareamin(:) = jpnij+1   ;   iareamax(:) = 0   ;   iareasum(:) = 0
198            DO ji = 1, jptst
199               IF( zmaxlocal(ji) == zmax(ji) ) THEN
200                  iareamin(ji) = narea   ;   iareamax(ji) = narea   ;   iareasum(ji) = 1
201               ENDIF
202            END DO
203            CALL mpp_min( "stpctl", iareamin )         ! min over the global domain
204            CALL mpp_max( "stpctl", iareamax )         ! max over the global domain
205            CALL mpp_sum( "stpctl", iareasum )         ! sum over the global domain
206         ELSE                    ! find local min and max locations:
207            ! if we are here, this means that the subdomain contains some oce points -> no need to test the mask used in maxloc
208            llmsk(Nis0:Nie0,Njs0:Nje0,1) = ssmask(Nis0:Nie0,Njs0:Nje0 ) == 1._wp        ! define only the inner domain
209            iloc(1:2,1) = MAXLOC( ABS( ssh(:,:,         Kmm)), mask = llmsk(:,:,1) )
210            llmsk(Nis0:Nie0,Njs0:Nje0,:) = umask(Nis0:Nie0,Njs0:Nje0,:) == 1._wp        ! define only the inner domain
211            iloc(1:3,2) = MAXLOC( ABS(  uu(:,:,:,       Kmm)), mask = llmsk(:,:,:) )
212            llmsk(Nis0:Nie0,Njs0:Nje0,:) = tmask(Nis0:Nie0,Njs0:Nje0,:) == 1._wp        ! define only the inner domain
213            iloc(1:3,3) = MINLOC(       ts(:,:,:,jp_sal,Kmm) , mask = llmsk(:,:,:) )
214            iloc(1:3,4) = MAXLOC(       ts(:,:,:,jp_sal,Kmm) , mask = llmsk(:,:,:) )
215            DO ji = 1, jptst   ! local domain indices ==> global domain indices, excluding halos
216               iloc(1:2,ji) = (/ mig0(iloc(1,ji)), mjg0(iloc(2,ji)) /)
217            END DO
218            iareamin(:) = narea   ;   iareamax(:) = narea   ;   iareasum(:) = 1         ! this is local information
219         ENDIF
220         !
221         WRITE(ctmp1,*) ' stp_ctl: |ssh| > 20 m  or  |U| > 10 m/s  or  S <= 0  or  S >= 100  or  NaN encounter in the tests'
222         CALL wrt_line( ctmp2, kt, '|ssh| max', CASTSP(zmax(1)), iloc(:,1), iareasum(1), iareamin(1), iareamax(1) )
223         CALL wrt_line( ctmp3, kt, '|U|   max', CASTSP(zmax(2)), iloc(:,2), iareasum(2), iareamin(2), iareamax(2) )
224         CALL wrt_line( ctmp4, kt, 'Sal   min', CASTSP(zmax(3)), iloc(:,3), iareasum(3), iareamin(3), iareamax(3) )
225         CALL wrt_line( ctmp5, kt, 'Sal   max', CASTSP(zmax(4)), iloc(:,4), iareasum(4), iareamin(4), iareamax(4) )
226         IF( Agrif_Root() ) THEN
227            WRITE(ctmp6,*) '      ===> output of last computed fields in output.abort* files'
228         ELSE
229            WRITE(ctmp6,*) '      ===> output of last computed fields in '//TRIM(Agrif_CFixed())//'_output.abort* files'
230         ENDIF
231         !
232         CALL dia_wri_state( Kmm, 'output.abort' )     ! create an output.abort file
233         !
234         IF( ll_colruns .OR. jpnij == 1 ) THEN   ! all processes synchronized -> use lwp to print in opened ocean.output files
235            IF(lwp) THEN   ;   CALL ctl_stop( ctmp1, ' ', ctmp2, ctmp3, ctmp4, ctmp5, ' ', ctmp6 )
236            ELSE           ;   nstop = MAX(1, nstop)   ! make sure nstop > 0 (automatically done when calling ctl_stop)
237            ENDIF
238         ELSE                                    ! only mpi subdomains with errors are here -> STOP now
239            CALL ctl_stop( 'STOP', ctmp1, ' ', ctmp2, ctmp3, ctmp4, ctmp5, ' ', ctmp6 )
240         ENDIF
241         !
242      ENDIF
243      !
244      IF( nstop > 0 ) THEN                                                  ! an error was detected and we did not abort yet...
245         ngrdstop = Agrif_Fixed()                                           ! store which grid got this error
246         IF( .NOT. ll_colruns .AND. jpnij > 1 )   CALL ctl_stop( 'STOP' )   ! we must abort here to avoid MPI deadlock
247      ENDIF
248      !
2499500  FORMAT(' it :', i8, '    |ssh|_max: ', D23.16, ' |U|_max: ', D23.16,' S_min: ', D23.16,' S_max: ', D23.16)
250      !
251   END SUBROUTINE stp_ctl
252
253
254   SUBROUTINE wrt_line( cdline, kt, cdprefix, pval, kloc, ksum, kmin, kmax )
255      !!----------------------------------------------------------------------
256      !!                     ***  ROUTINE wrt_line  ***
257      !!
258      !! ** Purpose :   write information line
259      !!
260      !!----------------------------------------------------------------------
261      CHARACTER(len=*),      INTENT(  out) ::   cdline
262      CHARACTER(len=*),      INTENT(in   ) ::   cdprefix
263      REAL(wp),              INTENT(in   ) ::   pval
264      INTEGER, DIMENSION(3), INTENT(in   ) ::   kloc
265      INTEGER,               INTENT(in   ) ::   kt, ksum, kmin, kmax
266      !
267      CHARACTER(len=80) ::   clsuff
268      CHARACTER(len=9 ) ::   clkt, clsum, clmin, clmax
269      CHARACTER(len=9 ) ::   cli, clj, clk
270      CHARACTER(len=1 ) ::   clfmt
271      CHARACTER(len=4 ) ::   cl4   ! needed to be able to compile with Agrif, I don't know why
272      INTEGER           ::   ifmtk
273      !!----------------------------------------------------------------------
274      WRITE(clkt , '(i9)') kt
275     
276      WRITE(clfmt, '(i1)') INT(LOG10(REAL(jpnij  ,wp))) + 1     ! how many digits to we need to write ? (we decide max = 9)
277      !!! WRITE(clsum, '(i'//clfmt//')') ksum                   ! this is creating a compilation error with AGRIF
278      cl4 = '(i'//clfmt//')'   ;   WRITE(clsum, cl4) ksum
279      WRITE(clfmt, '(i1)') INT(LOG10(REAL(MAX(1,jpnij-1),wp))) + 1    ! how many digits to we need to write ? (we decide max = 9)
280      cl4 = '(i'//clfmt//')'   ;   WRITE(clmin, cl4) kmin-1
281                                   WRITE(clmax, cl4) kmax-1
282      !
283      WRITE(clfmt, '(i1)') INT(LOG10(REAL(jpiglo,wp))) + 1      ! how many digits to we need to write jpiglo? (we decide max = 9)
284      cl4 = '(i'//clfmt//')'   ;   WRITE(cli, cl4) kloc(1)      ! this is ok with AGRIF
285      WRITE(clfmt, '(i1)') INT(LOG10(REAL(jpjglo,wp))) + 1      ! how many digits to we need to write jpjglo? (we decide max = 9)
286      cl4 = '(i'//clfmt//')'   ;   WRITE(clj, cl4) kloc(2)      ! this is ok with AGRIF
287      !
288      IF( ksum == 1 ) THEN   ;   WRITE(clsuff,9100) TRIM(clmin)
289      ELSE                   ;   WRITE(clsuff,9200) TRIM(clsum), TRIM(clmin), TRIM(clmax)
290      ENDIF
291      IF(kloc(3) == 0) THEN
292         ifmtk = INT(LOG10(REAL(jpk,wp))) + 1                   ! how many digits to we need to write jpk? (we decide max = 9)
293         clk = REPEAT(' ', ifmtk)                               ! create the equivalent in blank string
294         WRITE(cdline,9300) TRIM(ADJUSTL(clkt)), TRIM(ADJUSTL(cdprefix)), pval, TRIM(cli), TRIM(clj), clk(1:ifmtk), TRIM(clsuff)
295      ELSE
296         WRITE(clfmt, '(i1)') INT(LOG10(REAL(jpk,wp))) + 1      ! how many digits to we need to write jpk? (we decide max = 9)
297         !!! WRITE(clk, '(i'//clfmt//')') kloc(3)               ! this is creating a compilation error with AGRIF
298         cl4 = '(i'//clfmt//')'   ;   WRITE(clk, cl4) kloc(3)   ! this is ok with AGRIF
299         WRITE(cdline,9400) TRIM(ADJUSTL(clkt)), TRIM(ADJUSTL(cdprefix)), pval, TRIM(cli), TRIM(clj),    TRIM(clk), TRIM(clsuff)
300      ENDIF
301      !
3029100  FORMAT('MPI rank ', a)
3039200  FORMAT('found in ', a, ' MPI tasks, spread out among ranks ', a, ' to ', a)
3049300  FORMAT('kt ', a, ' ', a, ' ', 1pg11.4, ' at i j   ', a, ' ', a, ' ', a, ' ', a)
3059400  FORMAT('kt ', a, ' ', a, ' ', 1pg11.4, ' at i j k ', a, ' ', a, ' ', a, ' ', a)
306      !
307   END SUBROUTINE wrt_line
308
309
310   !!======================================================================
311END MODULE stpctl
Note: See TracBrowser for help on using the repository browser.