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.
stpctl.F90 in NEMO/branches/2020/r12377_ticket2386/src/OCE – NEMO

source: NEMO/branches/2020/r12377_ticket2386/src/OCE/stpctl.F90 @ 13694

Last change on this file since 13694 was 13694, checked in by andmirek, 3 years ago

Ticket #2386: merge with trunk rev 13688

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