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

source: NEMO/trunk/src/SAS/stpctl.F90

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

trunk: merge dev_r14312_MPI_Interface into the trunk, #2598

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