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/OCE – NEMO

source: NEMO/trunk/src/OCE/stpctl.F90 @ 13009

Last change on this file since 13009 was 13009, checked in by smasson, 4 years ago

trunk: bugfix, introduced in [12933], when jpnij = 1, see 2477

  • Property svn:keywords set to Id
File size: 17.6 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      !!              - Print it each 50 time steps
52      !!              - Stop the run IF problem encountered by setting nstop > 0
53      !!                Problems checked: |ssh| maximum larger than 10 m
54      !!                                  |U|   maximum larger than 10 m/s
55      !!                                  negative sea surface salinity
56      !!
57      !! ** Actions :   "time.step" file = last ocean time-step
58      !!                "run.stat"  file = run statistics
59      !!                 nstop indicator sheared among all local domain
60      !!----------------------------------------------------------------------
61      INTEGER, INTENT(in   ) ::   kt       ! ocean time-step index
62      INTEGER, INTENT(in   ) ::   Kmm      ! ocean time level index
63      !!
64      INTEGER                         ::   ji                                    ! dummy loop indices
65      INTEGER                         ::   idtime, istatus
66      INTEGER , DIMENSION(9)          ::   iareasum, iareamin, iareamax
67      INTEGER , DIMENSION(3,4)        ::   iloc                                  ! min/max loc indices
68      REAL(wp)                        ::   zzz                                   ! local real
69      REAL(wp), DIMENSION(9)          ::   zmax, zmaxlocal
70      LOGICAL                         ::   ll_wrtstp, ll_colruns, ll_wrtruns
71      LOGICAL, DIMENSION(jpi,jpj,jpk) ::   llmsk
72      CHARACTER(len=20)               ::   clname
73      !!----------------------------------------------------------------------
74      IF( nstop > 0 .AND. ngrdstop > -1 )   RETURN   !   stpctl was already called by a child grid
75      !
76      ll_wrtstp  = ( MOD( kt-nit000, sn_cfctl%ptimincr ) == 0 ) .OR. ( kt == nitend )
77      ll_colruns = ll_wrtstp .AND. sn_cfctl%l_runstat .AND. jpnij > 1 
78      ll_wrtruns = ( ll_colruns .OR. jpnij == 1 ) .AND. lwm
79      !
80      IF( kt == nit000 ) THEN
81         !
82         IF( lwp ) THEN
83            WRITE(numout,*)
84            WRITE(numout,*) 'stp_ctl : time-stepping control'
85            WRITE(numout,*) '~~~~~~~'
86         ENDIF
87         !                                ! open time.step    ascii file, done only by 1st subdomain
88         IF( lwm )   CALL ctl_opn( numstp, 'time.step', 'REPLACE', 'FORMATTED', 'SEQUENTIAL', -1, numout, lwp, narea )
89         !
90         IF( ll_wrtruns ) THEN
91            !                             ! open run.stat     ascii file, done only by 1st subdomain
92            CALL ctl_opn( numrun, 'run.stat', 'REPLACE', 'FORMATTED', 'SEQUENTIAL', -1, numout, lwp, narea )
93            !                             ! open run.stat.nc netcdf file, done only by 1st subdomain
94            clname = 'run.stat.nc'
95            IF( .NOT. Agrif_Root() )   clname = TRIM(Agrif_CFixed())//"_"//TRIM(clname)
96            istatus = NF90_CREATE( TRIM(clname), NF90_CLOBBER, nrunid )
97            istatus = NF90_DEF_DIM( nrunid, 'time', NF90_UNLIMITED, idtime )
98            istatus = NF90_DEF_VAR( nrunid, 'abs_ssh_max', NF90_DOUBLE, (/ idtime /), nvarid(1) )
99            istatus = NF90_DEF_VAR( nrunid,   'abs_u_max', NF90_DOUBLE, (/ idtime /), nvarid(2) )
100            istatus = NF90_DEF_VAR( nrunid,       's_min', NF90_DOUBLE, (/ idtime /), nvarid(3) )
101            istatus = NF90_DEF_VAR( nrunid,       's_max', NF90_DOUBLE, (/ idtime /), nvarid(4) )
102            istatus = NF90_DEF_VAR( nrunid,       't_min', NF90_DOUBLE, (/ idtime /), nvarid(5) )
103            istatus = NF90_DEF_VAR( nrunid,       't_max', NF90_DOUBLE, (/ idtime /), nvarid(6) )
104            IF( ln_zad_Aimp ) THEN
105               istatus = NF90_DEF_VAR( nrunid,   'Cf_max', NF90_DOUBLE, (/ idtime /), nvarid(7) )
106               istatus = NF90_DEF_VAR( nrunid,'abs_wi_max',NF90_DOUBLE, (/ idtime /), nvarid(8) )
107            ENDIF
108            istatus = NF90_ENDDEF(nrunid)
109         ENDIF
110         !   
111      ENDIF
112      !
113      !                                   !==              write current time step              ==!
114      !                                   !==  done only by 1st subdomain at writting timestep  ==!
115      IF( lwm .AND. ll_wrtstp ) THEN
116         WRITE ( numstp, '(1x, i8)' )   kt
117         REWIND( numstp )
118      ENDIF
119      !                                   !==            test of local extrema           ==!
120      !                                   !==  done by all processes at every time step  ==!
121      llmsk(:,:,1) = ssmask(:,:) == 1._wp
122      IF( ll_wd ) THEN
123         zmax(1) = MAXVAL( ABS( ssh(:,:,Kmm) + ssh_ref ), mask = llmsk(:,:,1) )   ! ssh max
124      ELSE
125         zmax(1) = MAXVAL( ABS( ssh(:,:,Kmm)           ), mask = llmsk(:,:,1) )   ! ssh max
126      ENDIF
127      llmsk(:,:,:) = umask(:,:,:) == 1._wp
128      zmax(2) = MAXVAL(  ABS( uu(:,:,:,Kmm) ), mask = llmsk )                     ! velocity max (zonal only)
129      llmsk(:,:,:) = tmask(:,:,:) == 1._wp
130      zmax(3) = MAXVAL( -ts(:,:,:,jp_sal,Kmm), mask = llmsk )                     ! minus salinity max
131      zmax(4) = MAXVAL(  ts(:,:,:,jp_sal,Kmm), mask = llmsk )                     !       salinity max
132      IF( ll_colruns ) THEN     ! following variables are used only in the netcdf file
133         zmax(5) = MAXVAL( -ts(:,:,:,jp_tem,Kmm), mask = llmsk )                  ! minus temperature max
134         zmax(6) = MAXVAL(  ts(:,:,:,jp_tem,Kmm), mask = llmsk )                  !       temperature max
135         IF( ln_zad_Aimp ) THEN
136            zmax(7) = MAXVAL(   Cu_adv(:,:,:)   , mask = llmsk )                  ! partitioning coeff. max
137            llmsk(:,:,:) = wmask(:,:,:) == 1._wp
138            zmax(8) = MAXVAL(  ABS( wi(:,:,:) ) , mask = llmsk )                  ! implicit vertical vel. max
139         ELSE
140            zmax(7:8) = 0._wp
141         ENDIF
142      ELSE
143         zmax(5:8) = 0._wp
144      ENDIF
145      zmax(9) = REAL( nstop, wp )                                              ! stop indicator
146      !                                   !==               get global extrema             ==!
147      !                                   !==  done by all processes if writting run.stat  ==!
148      IF( ll_colruns ) THEN
149         zmaxlocal(:) = zmax(:)
150         CALL mpp_max( "stpctl", zmax )          ! max over the global domain
151         nstop = NINT( zmax(9) )                 ! update nstop indicator (now sheared among all local domains)
152      ENDIF
153      !                                   !==              write "run.stat" files              ==!
154      !                                   !==  done only by 1st subdomain at writting timestep  ==!
155      IF( ll_wrtruns ) THEN
156         WRITE(numrun,9500) kt, zmax(1), zmax(2), -zmax(3), zmax(4)
157         istatus = NF90_PUT_VAR( nrunid, nvarid(1), (/ zmax(1)/), (/kt/), (/1/) )
158         istatus = NF90_PUT_VAR( nrunid, nvarid(2), (/ zmax(2)/), (/kt/), (/1/) )
159         istatus = NF90_PUT_VAR( nrunid, nvarid(3), (/-zmax(3)/), (/kt/), (/1/) )
160         istatus = NF90_PUT_VAR( nrunid, nvarid(4), (/ zmax(4)/), (/kt/), (/1/) )
161         istatus = NF90_PUT_VAR( nrunid, nvarid(5), (/-zmax(5)/), (/kt/), (/1/) )
162         istatus = NF90_PUT_VAR( nrunid, nvarid(6), (/ zmax(6)/), (/kt/), (/1/) )
163         IF( ln_zad_Aimp ) THEN
164            istatus = NF90_PUT_VAR( nrunid, nvarid(7), (/ zmax(7)/), (/kt/), (/1/) )
165            istatus = NF90_PUT_VAR( nrunid, nvarid(8), (/ zmax(8)/), (/kt/), (/1/) )
166         ENDIF
167         IF( kt == nitend )   istatus = NF90_CLOSE(nrunid)
168      END IF
169      !                                   !==               error handling               ==!
170      !                                   !==  done by all processes at every time step  ==!
171      !
172      IF(   zmax(1) >   20._wp .OR.   &                   ! too large sea surface height ( > 20 m )
173         &  zmax(2) >   10._wp .OR.   &                   ! too large velocity ( > 10 m/s)
174         &  zmax(3) >=   0._wp .OR.   &                   ! negative or zero sea surface salinity
175         &  zmax(4) >= 100._wp .OR.   &                   ! too large sea surface salinity ( > 100 )
176         &  zmax(4) <    0._wp .OR.   &                   ! too large sea surface salinity (keep this line for sea-ice)
177         &  ISNAN( zmax(1) + zmax(2) + zmax(3) ) .OR.   &               ! NaN encounter in the tests
178         &  ABS(   zmax(1) + zmax(2) + zmax(3) ) > HUGE(1._wp) ) THEN   ! Infinity encounter in the tests
179         !
180         iloc(:,:) = 0
181         IF( ll_colruns ) THEN   ! zmax is global, so it is the same on all subdomains -> no dead lock with mpp_maxloc
182            ! first: close the netcdf file, so we can read it
183            IF( lwm .AND. kt /= nitend )   istatus = NF90_CLOSE(nrunid)
184            ! get global loc on the min/max
185            CALL mpp_maxloc( 'stpctl', ABS(ssh(:,:,         Kmm)), ssmask(:,:  ), zzz, iloc(1:2,1) )   ! mpp_maxloc ok if mask = F
186            CALL mpp_maxloc( 'stpctl', ABS( uu(:,:,:,       Kmm)),  umask(:,:,:), zzz, iloc(1:3,2) )
187            CALL mpp_minloc( 'stpctl',      ts(:,:,:,jp_sal,Kmm) ,  tmask(:,:,:), zzz, iloc(1:3,3) )
188            CALL mpp_maxloc( 'stpctl',      ts(:,:,:,jp_sal,Kmm) ,  tmask(:,:,:), zzz, iloc(1:3,4) )
189            ! find which subdomain has the max.
190            iareamin(:) = jpnij+1   ;   iareamax(:) = 0   ;   iareasum(:) = 0
191            DO ji = 1, 9
192               IF( zmaxlocal(ji) == zmax(ji) ) THEN
193                  iareamin(ji) = narea   ;   iareamax(ji) = narea   ;   iareasum(ji) = 1
194               ENDIF
195            END DO
196            CALL mpp_min( "stpctl", iareamin )         ! min over the global domain
197            CALL mpp_max( "stpctl", iareamax )         ! max over the global domain
198            CALL mpp_sum( "stpctl", iareasum )         ! sum over the global domain
199         ELSE                    ! find local min and max locations:
200            ! if we are here, this means that the subdomain contains some oce points -> no need to test the mask used in maxloc
201            iloc(1:2,1) = MAXLOC( ABS( ssh(:,:,         Kmm)), mask = ssmask(:,:  ) == 1._wp ) + (/ nimpp - 1, njmpp - 1    /)
202            iloc(1:3,2) = MAXLOC( ABS(  uu(:,:,:,       Kmm)), mask =  umask(:,:,:) == 1._wp ) + (/ nimpp - 1, njmpp - 1, 0 /)
203            iloc(1:3,3) = MINLOC(       ts(:,:,:,jp_sal,Kmm) , mask =  tmask(:,:,:) == 1._wp ) + (/ nimpp - 1, njmpp - 1, 0 /)
204            iloc(1:3,4) = MAXLOC(       ts(:,:,:,jp_sal,Kmm) , mask =  tmask(:,:,:) == 1._wp ) + (/ nimpp - 1, njmpp - 1, 0 /)
205            iareamin(:) = narea   ;   iareamax(:) = narea   ;   iareasum(:) = 1         ! this is local information
206         ENDIF
207         !
208         WRITE(ctmp1,*) ' stp_ctl: |ssh| > 20 m  or  |U| > 10 m/s  or  S <= 0  or  S >= 100  or  NaN encounter in the tests'
209         CALL wrt_line( ctmp2, kt, '|ssh| max',  zmax(1), iloc(:,1), iareasum(1), iareamin(1), iareamax(1) )
210         CALL wrt_line( ctmp3, kt, '|U|   max',  zmax(2), iloc(:,2), iareasum(2), iareamin(2), iareamax(2) )
211         CALL wrt_line( ctmp4, kt, 'Sal   min', -zmax(3), iloc(:,3), iareasum(3), iareamin(3), iareamax(3) )
212         CALL wrt_line( ctmp5, kt, 'Sal   max',  zmax(4), iloc(:,4), iareasum(4), iareamin(4), iareamax(4) )
213         IF( Agrif_Root() ) THEN
214            WRITE(ctmp6,*) '      ===> output of last computed fields in output.abort* files'
215         ELSE
216            WRITE(ctmp6,*) '      ===> output of last computed fields in '//TRIM(Agrif_CFixed())//'_output.abort* files'
217         ENDIF
218         !
219         CALL dia_wri_state( Kmm, 'output.abort' )     ! create an output.abort file
220         !
221         IF( ll_colruns .or. jpnij == 1 ) THEN   ! all processes synchronized -> use lwp to print in opened ocean.output files
222            IF(lwp)   CALL ctl_stop( ctmp1, ' ', ctmp2, ctmp3, ctmp4, ctmp5, ' ', ctmp6 )
223         ELSE                                    ! only mpi subdomains with errors are here -> STOP now
224            CALL ctl_stop( 'STOP', ctmp1, ' ', ctmp2, ctmp3, ctmp4, ctmp5, ' ', ctmp6 )
225         ENDIF
226         !
227         IF( nstop == 0 )   nstop = 1 
228         ngrdstop = Agrif_Fixed()
229         !
230      ENDIF
231      !
2329500  FORMAT(' it :', i8, '    |ssh|_max: ', D23.16, ' |U|_max: ', D23.16,' S_min: ', D23.16,' S_max: ', D23.16)
233      !
234   END SUBROUTINE stp_ctl
235
236
237   SUBROUTINE wrt_line( cdline, kt, cdprefix, pval, kloc, ksum, kmin, kmax )
238      !!----------------------------------------------------------------------
239      !!                     ***  ROUTINE wrt_line  ***
240      !!
241      !! ** Purpose :   write information line
242      !!
243      !!----------------------------------------------------------------------
244      CHARACTER(len=*),      INTENT(  out) ::   cdline
245      CHARACTER(len=*),      INTENT(in   ) ::   cdprefix
246      REAL(wp),              INTENT(in   ) ::   pval
247      INTEGER, DIMENSION(3), INTENT(in   ) ::   kloc
248      INTEGER,               INTENT(in   ) ::   kt, ksum, kmin, kmax
249      !
250      CHARACTER(len=80) ::   clsuff
251      CHARACTER(len=9 ) ::   clkt, clsum, clmin, clmax
252      CHARACTER(len=9 ) ::   cli, clj, clk
253      CHARACTER(len=1 ) ::   clfmt
254      CHARACTER(len=4 ) ::   cl4   ! needed to be able to compile with Agrif, I don't know why
255      INTEGER           ::   ifmtk
256      !!----------------------------------------------------------------------
257      WRITE(clkt , '(i9)') kt
258     
259      WRITE(clfmt, '(i1)') INT(LOG10(REAL(jpnij  ,wp))) + 1     ! how many digits to we need to write ? (we decide max = 9)
260      !!! WRITE(clsum, '(i'//clfmt//')') ksum                   ! this is creating a compilation error with AGRIF
261      cl4 = '(i'//clfmt//')'   ;   WRITE(clsum, cl4) ksum
262      WRITE(clfmt, '(i1)') INT(LOG10(REAL(MAX(1,jpnij-1),wp))) + 1    ! how many digits to we need to write ? (we decide max = 9)
263      cl4 = '(i'//clfmt//')'   ;   WRITE(clmin, cl4) kmin-1
264                                   WRITE(clmax, cl4) kmax-1
265      !
266      WRITE(clfmt, '(i1)') INT(LOG10(REAL(jpiglo,wp))) + 1      ! how many digits to we need to write jpiglo? (we decide max = 9)
267      cl4 = '(i'//clfmt//')'   ;   WRITE(cli, cl4) kloc(1)      ! this is ok with AGRIF
268      WRITE(clfmt, '(i1)') INT(LOG10(REAL(jpjglo,wp))) + 1      ! how many digits to we need to write jpjglo? (we decide max = 9)
269      cl4 = '(i'//clfmt//')'   ;   WRITE(clj, cl4) kloc(2)      ! this is ok with AGRIF
270      !
271      IF( ksum == 1 ) THEN   ;   WRITE(clsuff,9100) TRIM(clmin)
272      ELSE                   ;   WRITE(clsuff,9200) TRIM(clsum), TRIM(clmin), TRIM(clmax)
273      ENDIF
274      IF(kloc(3) == 0) THEN
275         ifmtk = INT(LOG10(REAL(jpk,wp))) + 1                   ! how many digits to we need to write jpk? (we decide max = 9)
276         clk = REPEAT(' ', ifmtk)                               ! create the equivalent in blank string
277         WRITE(cdline,9300) TRIM(ADJUSTL(clkt)), TRIM(ADJUSTL(cdprefix)), pval, TRIM(cli), TRIM(clj), clk(1:ifmtk), TRIM(clsuff)
278      ELSE
279         WRITE(clfmt, '(i1)') INT(LOG10(REAL(jpk,wp))) + 1      ! how many digits to we need to write jpk? (we decide max = 9)
280         !!! WRITE(clk, '(i'//clfmt//')') kloc(3)               ! this is creating a compilation error with AGRIF
281         cl4 = '(i'//clfmt//')'   ;   WRITE(clk, cl4) kloc(3)   ! this is ok with AGRIF
282         WRITE(cdline,9400) TRIM(ADJUSTL(clkt)), TRIM(ADJUSTL(cdprefix)), pval, TRIM(cli), TRIM(clj),    TRIM(clk), TRIM(clsuff)
283      ENDIF
284      !
2859100  FORMAT('MPI rank ', a)
2869200  FORMAT('found in ', a, ' MPI tasks, spread out among ranks ', a, ' to ', a)
2879300  FORMAT('kt ', a, ' ', a, ' ', 1pg11.4, ' at i j   ', a, ' ', a, ' ', a, ' ', a)
2889400  FORMAT('kt ', a, ' ', a, ' ', 1pg11.4, ' at i j k ', a, ' ', a, ' ', a, ' ', a)
289      !
290   END SUBROUTINE wrt_line
291
292
293   !!======================================================================
294END MODULE stpctl
Note: See TracBrowser for help on using the repository browser.