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/dev_r13327_KERNEL-06_2_techene_e3/src/SWE – NEMO

source: NEMO/branches/2020/dev_r13327_KERNEL-06_2_techene_e3/src/SWE/stpctl.F90 @ 13604

Last change on this file since 13604 was 13604, checked in by techene, 4 years ago

#2385 cleaning, relevant features moved to OCE, bug corrected in dynvor

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