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/trunk/src/SWE – NEMO

source: NEMO/trunk/src/SWE/stpctl.F90 @ 14318

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

trunk: stpctl cleaning, #2602

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