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.
trcsms_fabm.F90 in NEMO/branches/UKMO/AMM15_v3_6_STABLE_new_ersem/NEMOGCM/NEMO/TOP_SRC/FABM – NEMO

source: NEMO/branches/UKMO/AMM15_v3_6_STABLE_new_ersem/NEMOGCM/NEMO/TOP_SRC/FABM/trcsms_fabm.F90 @ 15508

Last change on this file since 15508 was 15508, checked in by jcastill, 8 months ago

Corrections for FABM 1.0

File size: 25.3 KB
Line 
1MODULE trcsms_fabm
2   !!======================================================================
3   !!                         ***  MODULE trcsms_fabm  ***
4   !! TOP :   Main module of the FABM tracers
5   !!======================================================================
6   !! History :   1.0  !  2015-04  (PML) Original code
7   !! History :   1.1  !  2020-06  (PML) Update to FABM 1.0, improved performance
8   !!----------------------------------------------------------------------
9#if defined key_fabm
10   !!----------------------------------------------------------------------
11   !!   'key_fabm'                                               FABM tracers
12   !!----------------------------------------------------------------------
13   !! trc_sms_fabm       : FABM model main routine
14   !! trc_sms_fabm_alloc : allocate arrays specific to FABM sms
15   !!----------------------------------------------------------------------
16   USE par_trc         ! TOP parameters
17   USE oce_trc         ! Ocean variables
18   USE trc             ! TOP variables
19   USE trcbc
20   USE trd_oce
21   USE trdtrc
22#if defined key_trdtrc && defined key_iomput
23   USE trdtrc_oce
24#endif
25
26   USE oce, only: tsn  ! Needed?
27   USE sbc_oce, only: lk_oasis,fr_i
28   USE dom_oce
29   USE zdf_oce
30   USE iom
31   USE xios
32   USE cpl_oasis3
33   USE st2D_fabm
34   USE inputs_fabm
35   USE vertical_movement_fabm
36   USE zdfmxl
37   USE asmbgc, ONLY: mld_choice_bgc
38   USE lbclnk
39
40   !USE fldread         !  time interpolation
41
42   IMPLICIT NONE
43
44#  include "domzgr_substitute.h90"
45#  include "vectopt_loop_substitute.h90"
46
47   PRIVATE
48
49   PUBLIC   trc_sms_fabm       ! called by trcsms.F90 module
50   PUBLIC   trc_sms_fabm_alloc ! called by trcini_fabm.F90 module
51   PUBLIC   trc_sms_fabm_check_mass
52   PUBLIC   st2d_fabm_nxt ! 2D state intergration
53   PUBLIC   compute_fabm ! Compute FABM sources, sinks and diagnostics
54
55   ! Work arrays
56   REAL(wp), ALLOCATABLE, DIMENSION(:,:) :: flux          ! Cross-interface flux of pelagic variables (# m-2 s-1)
57   REAL(wp), ALLOCATABLE, DIMENSION(:,:) :: current_total ! Totals of conserved quantities
58
59   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:)   :: ext     ! Light extinction coefficient (m-1)
60
61   ! State repair counters
62   INTEGER, SAVE :: repair_interior_count = 0
63   INTEGER, SAVE :: repair_surface_count  = 0
64   INTEGER, SAVE :: repair_bottom_count   = 0
65
66   ! Coupler parameters
67   INTEGER, PUBLIC :: nn_adv  ! Vertical advection scheme for sinking/floating/movement
68                              ! (1: 1st order upwind, 3: 3rd order TVD)
69
70   ! Arrays for environmental variables that are computed by the coupler
71   REAL(wp), PUBLIC, TARGET, ALLOCATABLE, DIMENSION(:,:,:) :: prn,rho
72   REAL(wp), PUBLIC, TARGET, ALLOCATABLE, DIMENSION(:,:) :: taubot
73   REAL(wp), PUBLIC, TARGET :: daynumber_in_year
74
75   ! Flag indicating whether model%start has been called (will be done on-demand)
76   LOGICAL, SAVE :: started = .false.
77
78   !!----------------------------------------------------------------------
79   !! NEMO/TOP 3.3 , NEMO Consortium (2010)
80   !! $Id$
81   !! Software governed by the CeCILL licence     (NEMOGCM/NEMO_CeCILL.txt)
82   !!----------------------------------------------------------------------
83CONTAINS
84
85   SUBROUTINE trc_sms_fabm( kt )
86      !!----------------------------------------------------------------------
87      !!                     ***  trc_sms_fabm  ***
88      !!
89      !! ** Purpose :   main routine of FABM model
90      !!
91      !! ** Method  : -
92      !!----------------------------------------------------------------------
93      !
94      INTEGER, INTENT(in) ::   kt   ! ocean time-step index
95      INTEGER :: jn, jk
96      REAL(wp), POINTER, DIMENSION(:,:,:) :: ztrfabm, pdat
97      REAL(wp), DIMENSION(jpi,jpj)    :: vint
98
99!!----------------------------------------------------------------------
100      !
101      IF( nn_timing == 1 )  CALL timing_start('trc_sms_fabm')
102      !
103      IF(lwp) WRITE(numout,*)
104      IF(lwp) WRITE(numout,'(a,i0,a,i4.4,a,i2.2,a,i2.2,a,i5,a)') &
105          ' trc_sms_fabm:  FABM model, iteration ',kt,' ', &
106          nyear,'-',nmonth,'-',nday,' ',nsec_day," secs"
107      IF(lwp) WRITE(numout,*) ' ~~~~~~~~~~~~~~'
108
109      IF (.NOT. started) CALL nemo_fabm_start
110
111      CALL update_inputs( kt )
112
113      CALL compute_fabm( kt )
114
115      CALL compute_vertical_movement( kt, nn_adv )
116
117      CALL st2d_fabm_nxt( kt )
118     
119      CALL asmdiags_fabm( kt )
120
121      IF( l_trdtrc )  CALL wrk_alloc( jpi, jpj, jpk, ztrfabm )
122
123      CALL trc_bc_read  ( kt )       ! tracers: surface and lateral Boundary Conditions
124      CALL trc_rnf_fabm ( kt ) ! River forcings
125
126      ! Send 3D diagnostics to output (these apply to time "n")
127      DO jn = 1, size(model%interior_diagnostic_variables)
128         IF (model%interior_diagnostic_variables(jn)%save) THEN
129            ! Save 3D field
130            pdat => model%get_interior_diagnostic_data(jn)
131            CALL iom_put(model%interior_diagnostic_variables(jn)%name, pdat)
132
133            ! Save depth integral if selected for output in XIOS
134            IF (iom_use(TRIM(model%interior_diagnostic_variables(jn)%name)//'_VINT')) THEN
135               vint = 0._wp
136               DO jk = 1, jpkm1
137                  vint = vint + pdat(:,:,jk) * fse3t(:,:,jk) * tmask(:,:,jk)
138               END DO
139               CALL iom_put(TRIM(model%interior_diagnostic_variables(jn)%name)//'_VINT', vint)
140            END IF
141         END IF
142      END DO
143
144      ! Send 2D diagnostics to output (these apply to time "n")
145      DO jn = 1, size(model%horizontal_diagnostic_variables)
146         IF (model%horizontal_diagnostic_variables(jn)%save) &
147             CALL iom_put( model%horizontal_diagnostic_variables(jn)%name, model%get_horizontal_diagnostic_data(jn))
148      END DO
149
150      IF( l_trdtrc ) THEN      ! Save the trends in the mixed layer
151          DO jn = jp_fabm0, jp_fabm1
152            ztrfabm(:,:,:) = tra(:,:,:,jn)
153            CALL trd_trc( ztrfabm, jn, jptra_sms, kt )   ! save trends
154          END DO
155          CALL wrk_dealloc( jpi, jpj, jpk, ztrfabm )
156      END IF
157      !
158      IF( nn_timing == 1 )  CALL timing_stop('trc_sms_fabm')
159
160   END SUBROUTINE trc_sms_fabm
161   
162   SUBROUTINE asmdiags_fabm( kt )
163      INTEGER, INTENT(IN) :: kt
164      INTEGER :: ji,jj,jk,jkmax
165      REAL(wp), DIMENSION(jpi,jpj,jpk) :: pgrow_3d, ploss_3d
166      REAL(wp), DIMENSION(jpi,jpj)     :: zmld
167     
168      IF (kt == nittrc000) THEN
169         MLD_MAX(:,:) = 0.0
170      ENDIF
171      PGROW_AVG(:,:) = 0.0
172      PLOSS_AVG(:,:) = 0.0
173      PHYT_AVG(:,:)  = 0.0
174       
175      pgrow_3d(:,:,:) = model%get_interior_diagnostic_data(jp_fabm_pgrow)
176      ploss_3d(:,:,:) = model%get_interior_diagnostic_data(jp_fabm_ploss)
177     
178      SELECT CASE( mld_choice_bgc )
179      CASE ( 1 )                   ! Turbocline/mixing depth [W points]
180         zmld(:,:) = hmld(:,:)
181      CASE ( 2 )                   ! Density criterion (0.01 kg/m^3 change from 10m) [W points]
182         zmld(:,:) = hmlp(:,:)
183      CASE ( 3 )                   ! Kara MLD [Interpolated]
184#if defined key_karaml
185         IF ( ln_kara ) THEN
186            zmld(:,:) = hmld_kara(:,:)
187         ELSE
188            CALL ctl_stop( ' Kara mixed layer requested for BGC assimilation,', &
189               &           ' but ln_kara=.false.' )
190         ENDIF
191#else
192         CALL ctl_stop( ' Kara mixed layer requested for BGC assimilation,', &
193            &           ' but is not defined' )
194#endif
195      CASE ( 4 )                   ! Temperature criterion (0.2 K change from surface) [T points]
196         zmld(:,:) = hmld_tref(:,:)
197      CASE ( 5 )                   ! Density criterion (0.01 kg/m^3 change from 10m) [T points]
198         zmld(:,:) = hmlpt(:,:)
199      END SELECT
200   
201      DO jj = 2, jpjm1
202         DO ji = 2, jpim1
203            !
204            jkmax = jpk-1
205            DO jk = jpk-1, 1, -1
206               IF ( ( zmld(ji,jj) >  gdepw_n(ji,jj,jk)   ) .AND. &
207                  & ( zmld(ji,jj) <= gdepw_n(ji,jj,jk+1) ) ) THEN
208                  zmld(ji,jj) = gdepw_n(ji,jj,jk+1)
209                  jkmax = jk
210               ENDIF
211            END DO
212            !
213            DO jk = 1, jkmax
214               PHYT_AVG(ji,jj) = PHYT_AVG(ji,jj) + &
215                  &              trn(ji,jj,jk,jp_fabm_m1+jp_fabm_p1n) + &
216                  &              trn(ji,jj,jk,jp_fabm_m1+jp_fabm_p2n) + &
217                  &              trn(ji,jj,jk,jp_fabm_m1+jp_fabm_p3n) + &
218                  &              trn(ji,jj,jk,jp_fabm_m1+jp_fabm_p4n)
219               IF ( pgrow_3d(ji,jj,jk) .GT. 0.0 ) THEN
220                  PGROW_AVG(ji,jj) = PGROW_AVG(ji,jj) + &
221                     &               pgrow_3d(ji,jj,jk)
222               ENDIF
223               IF ( ploss_3d(ji,jj,jk) .GT. 0.0 ) THEN
224                  PLOSS_AVG(ji,jj) = PLOSS_AVG(ji,jj) + &
225                     &               ploss_3d(ji,jj,jk)
226               ENDIF
227            END DO
228           
229            PHYT_AVG(ji,jj)  = PHYT_AVG(ji,jj)  / REAL(jkmax)
230            PGROW_AVG(ji,jj) = PGROW_AVG(ji,jj) / REAL(jkmax)
231            PLOSS_AVG(ji,jj) = PLOSS_AVG(ji,jj) / REAL(jkmax)
232   
233            IF ( zmld(ji,jj) .GT. MLD_MAX(ji,jj) ) THEN
234               MLD_MAX(ji,jj) = zmld(ji,jj)
235            ENDIF
236            !
237         END DO
238      END DO
239     
240      PHYT_AVG(:,:)  = PHYT_AVG(:,:)  * tmask(:,:,1)
241      PGROW_AVG(:,:) = PGROW_AVG(:,:) * tmask(:,:,1)
242      PLOSS_AVG(:,:) = PLOSS_AVG(:,:) * tmask(:,:,1)
243      MLD_MAX(:,:)   = MLD_MAX(:,:)   * tmask(:,:,1)
244     
245   END SUBROUTINE asmdiags_fabm
246
247   SUBROUTINE compute_fabm( kt )
248      INTEGER, INTENT(in) :: kt   ! ocean time-step index
249
250      INTEGER :: ji,jj,jk,jn
251      LOGICAL :: valid, repaired
252      REAL(wp) :: zalfg,zztmpx,zztmpy
253
254      ! Validate current model state (setting argument to .TRUE. enables repair=clipping)
255      CALL check_state(.TRUE., valid, repaired)
256      IF (.NOT. valid) THEN
257         WRITE(numout,*) "Invalid value in FABM encountered in area ",narea,"!!!"
258#if defined key_iomput
259         CALL xios_finalize                ! end mpp communications with xios
260         IF( lk_oasis ) CALL cpl_finalize    ! end coupling and mpp communications with OASIS
261#else
262         IF( lk_oasis ) THEN
263            CALL cpl_finalize              ! end coupling and mpp communications with OASIS
264         ELSE
265            IF( lk_mpp )   CALL mppstop    ! end mpp communications
266         ENDIF
267#endif
268      END IF
269      IF (repaired) THEN
270         WRITE(numout,*) "Total interior repairs up to now on process",narea,":",repair_interior_count
271         WRITE(numout,*) "Total surface repairs up to now on process",narea,":",repair_surface_count
272         WRITE(numout,*) "Total bottom repairs up to now on process",narea,":",repair_bottom_count
273      ENDIF
274
275      daynumber_in_year=(fjulday-fjulstartyear+1)*1._wp
276
277      ! Compute the now hydrostatic pressure
278      ! copied from istate.F90
279      ! ------------------------------------
280
281      IF (ALLOCATED(rho)) rho = rau0 * ( 1._wp + rhd )
282
283      IF (ALLOCATED(prn)) THEN
284         zalfg = 0.5e-4_wp * grav ! FABM wants dbar, convert from Pa (and multiply with 0.5 to average 2 cell thicknesses below)
285         prn(:,:,1) = 10.1325_wp + zalfg * fse3t(:,:,1) * rho(:,:,1)
286         DO jk = 2, jpkm1                                              ! Vertical integration from the surface
287            prn(:,:,jk) = prn(:,:,jk-1) + zalfg * ( &
288                        fse3t(:,:,jk-1) * rho(:,:,jk-1)  &
289                        + fse3t(:,:,jk) * rho(:,:,jk) )
290         END DO
291      END IF
292
293      ! Compute the bottom stress
294      ! copied from diawri.F90
295      ! ------------------------------------
296
297      IF (ALLOCATED(taubot)) THEN
298         taubot(:,:) = 0._wp
299         DO jj = 2, jpjm1
300            DO ji = fs_2, fs_jpim1   ! vector opt.
301                  zztmpx = (  bfrua(ji  ,jj) * un(ji  ,jj,mbku(ji  ,jj))  &
302                        &  + bfrua(ji-1,jj) * un(ji-1,jj,mbku(ji-1,jj))  )
303                  zztmpy = (  bfrva(ji,  jj) * vn(ji,jj  ,mbkv(ji,jj  ))  &
304                        &  + bfrva(ji,jj-1) * vn(ji,jj-1,mbkv(ji,jj-1))  )
305                  taubot(ji,jj) = 0.5_wp * rau0 * SQRT( zztmpx * zztmpx + zztmpy * zztmpy ) * tmask(ji,jj,1)
306                  !
307            ENDDO
308         ENDDO
309      ENDIF
310
311! Remove because get_light routines were removed in FABM 1.0
312! and replaced by prepare_inputs
313!      ! Compute light extinction
314!      DO jk=1,jpk
315!          DO jj=1,jpj
316!            call get_light_extinction(model,1,jpi,jj,jk,ext)
317!         END DO
318!      END DO
319!
320!      ! Compute light field (stored among FABM's internal diagnostics)
321!      DO jj=1,jpj
322!          DO ji=1,jpi
323!            call get_light(model,1,jpk,ji,jj)
324!         END DO
325!      END DO
326
327      ! TODO: retrieve 3D shortwave and store in etot3
328
329      ! Zero rate array of interface-attached state variables
330      fabm_st2Da = 0._wp
331
332      ! Compute interfacial source terms and fluxes
333      DO jj=2,jpjm1
334         ! Process bottom (get_bottom_sources increments rather than sets, so zero flux array first)
335         flux = 0._wp
336         CALL model%get_bottom_sources(fs_2,fs_jpim1,jj,flux,fabm_st2Da(fs_2:fs_jpim1,jj,jp_fabm_surface+1:))
337         DO jn=1,jp_fabm
338            ! Divide bottom fluxes by height of bottom layer and add to source terms.
339            DO ji=fs_2,fs_jpim1
340               tra(ji,jj,mbkt(ji,jj),jp_fabm_m1+jn) = tra(ji,jj,mbkt(ji,jj),jp_fabm_m1+jn) + flux(ji,jn)/fse3t(ji,jj,mbkt(ji,jj))
341            END DO
342         END DO
343
344         ! Process surface (get_surface_sources increments rather than sets, so zero flux array first)
345         flux = 0._wp
346         CALL model%get_surface_sources(fs_2,fs_jpim1,jj,flux,fabm_st2Da(fs_2:fs_jpim1,jj,1:jp_fabm_surface))
347         ! Divide surface fluxes by height of surface layer and add to source terms.
348         DO jn=1,jp_fabm
349            DO ji=fs_2,fs_jpim1
350               tra(ji,jj,1,jp_fabm_m1+jn) = tra(ji,jj,1,jp_fabm_m1+jn) + flux(ji,jn)/fse3t(ji,jj,1)
351            END DO
352         END DO
353      END DO
354
355      ! Compute interior source terms (NB get_interior_sources increments rather than sets)
356      DO jk=1,jpkm1
357         DO jj=2,jpjm1
358            CALL model%get_interior_sources(fs_2,fs_jpim1,jj,jk,tra(fs_2:fs_jpim1,jj,jk,jp_fabm0:jp_fabm1))
359         END DO
360      END DO
361
362      CALL model%finalize_outputs()
363   END SUBROUTINE compute_fabm
364
365   SUBROUTINE check_state(repair, valid, repaired)
366      LOGICAL, INTENT(IN)  :: repair
367      LOGICAL, INTENT(OUT) :: valid, repaired
368
369      INTEGER :: jj, jk
370      LOGICAL :: valid_int, valid_sf, valid_bt
371
372      valid = .TRUE.     ! Whether the model state is valid after this subroutine returns
373      repaired = .FALSE. ! Whether the model state has been repaired by this subroutine
374      DO jk=1,jpkm1
375         DO jj=2,jpjm1
376            CALL model%check_interior_state(fs_2, fs_jpim1, jj, jk, repair, valid_int)
377            IF (repair .AND. .NOT. valid_int) THEN
378               repair_interior_count = repair_interior_count + 1
379               repaired = .TRUE.
380            END IF
381            IF (.NOT. (valid_int .OR. repair)) valid = .FALSE.
382         END DO
383      END DO
384      DO jj=2,jpjm1
385         CALL model%check_surface_state(fs_2, fs_jpim1, jj, repair, valid_sf)
386         IF (repair .AND. .NOT. valid_sf) THEN
387            repair_surface_count = repair_surface_count + 1
388            repaired = .TRUE.
389         END IF
390         IF (.NOT. (valid_sf .AND. valid_bt) .AND. .NOT. repair) valid = .FALSE.
391         CALL model%check_bottom_state(fs_2, fs_jpim1, jj, repair, valid_bt)
392         IF (repair .AND. .NOT. valid_bt) THEN
393            repair_bottom_count = repair_bottom_count + 1
394            repaired = .TRUE.
395         END IF
396         IF (.NOT. (valid_sf .AND. valid_bt) .AND. .NOT. repair) valid = .FALSE.
397      END DO
398   END SUBROUTINE
399
400   SUBROUTINE trc_sms_fabm_check_mass()
401      REAL(wp) :: total(SIZE(model%conserved_quantities))
402      INTEGER :: ji,jk,jj,jn
403
404      total = 0._wp
405
406      IF (.NOT. started) CALL nemo_fabm_start
407
408      DO jk=1,jpkm1
409         DO jj=2,jpjm1
410            CALL model%get_interior_conserved_quantities(fs_2,fs_jpim1,jj,jk,current_total)
411            DO jn=1,SIZE(model%conserved_quantities)
412               DO ji=fs_2,fs_jpim1
413                  total(jn) = total(jn) + cvol(ji,jj,jk) * current_total(ji,jn) * tmask_i(ji,jj)
414               END DO
415            END DO
416         END DO
417      END DO
418
419      DO jj=2,jpjm1
420         CALL model%get_horizontal_conserved_quantities(fs_2,fs_jpim1,jj,current_total)
421         DO jn=1,SIZE(model%conserved_quantities)
422            DO ji=fs_2,fs_jpim1
423               total(jn) = total(jn) + e1e2t(ji,jj) * current_total(ji,jn) * tmask_i(ji,jj)
424            END DO
425         END DO
426      END DO
427
428      IF( lk_mpp ) CALL mpp_sum(total,SIZE(model%conserved_quantities))
429
430      DO jn=1,SIZE(model%conserved_quantities)
431         IF(lwp) WRITE(numout,*) 'FABM '//TRIM(model%conserved_quantities(jn)%name),total(jn),TRIM(model%conserved_quantities(jn)%units)//'*m3'
432      END DO
433
434   END SUBROUTINE trc_sms_fabm_check_mass
435
436   SUBROUTINE st2d_fabm_nxt( kt )
437      !!----------------------------------------------------------------------
438      !!                     ***  st2d_fabm_nxt  ***
439      !!
440      !! ** Purpose :   routine to integrate 2d states in time
441      !!
442      !! ** Method  :   based on integration of 3D passive tracer fields
443      !!                implemented in TOP_SRC/TRP/trcnxt.F90, plus
444      !!                tra_nxt_fix in OPA_SRC/TRA/tranxt.F90. Similar to
445      !!                time integration of sea surface height in
446      !!                OPA_SRC/DYN/sshwzv.F90.
447      !!----------------------------------------------------------------------
448      !
449      INTEGER, INTENT(in) ::   kt   ! ocean time-step index
450      REAL(wp) :: z2dt
451      INTEGER :: jn
452
453!!----------------------------------------------------------------------
454      !
455      IF( neuler == 0 .AND. kt == nittrc000 ) THEN
456          z2dt = rdt                  ! set time step size (Euler)
457      ELSE
458          z2dt = 2._wp * rdt          ! set time step size (Leapfrog)
459      ENDIF
460
461      ! Forward Euler time step to compute "now"
462      DO jn=1,jp_fabm_surface+jp_fabm_bottom
463         fabm_st2Da(:,:,jn) = (fabm_st2db(:,:,jn) + z2dt * fabm_st2da(:,:,jn)) * tmask(:,:,1)
464      ENDDO
465
466      IF( neuler == 0 .AND. kt == nittrc000 ) THEN        ! Euler time-stepping at first time-step
467         !                                                ! (only swap)
468         fabm_st2Dn(:,:,:) = fabm_st2Da(:,:,:)
469         !
470      ELSE
471         ! Update now state + Asselin filter time stepping
472         fabm_st2Db(:,:,:) = (1._wp - 2._wp*atfp) * fabm_st2Dn(:,:,:) + &
473             atfp * ( fabm_st2Db(:,:,:) + fabm_st2Da(:,:,:) )
474         fabm_st2Dn(:,:,:) = fabm_st2Da(:,:,:)
475      ENDIF
476
477   END SUBROUTINE st2d_fabm_nxt
478
479   INTEGER FUNCTION trc_sms_fabm_alloc()
480      INTEGER :: jn
481      !!----------------------------------------------------------------------
482      !!              ***  ROUTINE trc_sms_fabm_alloc  ***
483      !!----------------------------------------------------------------------
484      !
485      ! ALLOCATE here the arrays specific to FABM
486      ALLOCATE( lk_rad_fabm(jp_fabm))
487      IF (model%variable_needs_values(fabm_standard_variables%pressure)) ALLOCATE(prn(jpi, jpj, jpk))
488      IF (ALLOCATED(prn) .or. model%variable_needs_values(fabm_standard_variables%density)) ALLOCATE(rho(jpi, jpj, jpk))
489      IF (model%variable_needs_values(fabm_standard_variables%bottom_stress)) ALLOCATE(taubot(jpi, jpj))
490      ! ALLOCATE( tab(...) , STAT=trc_sms_fabm_alloc )
491
492      ! Allocate arrays to hold state for surface-attached and bottom-attached state variables
493      ALLOCATE(fabm_st2Dn(jpi, jpj, jp_fabm_surface+jp_fabm_bottom))
494      ALLOCATE(fabm_st2Da(jpi, jpj, jp_fabm_surface+jp_fabm_bottom))
495      ALLOCATE(fabm_st2Db(jpi, jpj, jp_fabm_surface+jp_fabm_bottom))
496
497      ! Work array to hold surface and bottom fluxes
498      ALLOCATE(flux(fs_2:fs_jpim1,jp_fabm))
499
500      ! Allocate work arrays for vertical movement
501      ALLOCATE(w_ct(fs_2:fs_jpim1,1:jpkm1,jp_fabm))
502      ALLOCATE(current_total(fs_2:fs_jpim1,SIZE(model%conserved_quantities)))
503#if defined key_trdtrc && defined key_iomput
504      IF( lk_trdtrc ) ALLOCATE(tr_vmv(jpi,jpj,jpk,jp_fabm))
505      IF( lk_trdtrc ) ALLOCATE(tr_inp(jpi,jpj,jpk))
506#endif
507
508      trc_sms_fabm_alloc = 0      ! set to zero if no array to be allocated
509      !
510      IF( trc_sms_fabm_alloc /= 0 ) CALL ctl_warn('trc_sms_fabm_alloc : failed to allocate arrays')
511      !
512
513      ! Provide FABM with domain extents
514      CALL model%set_domain(jpi, jpj, jpk, rdt)
515      CALL model%set_domain_start(fs_2, 2, 1)
516      CALL model%set_domain_stop(fs_jpim1, jpjm1, jpkm1)
517
518      ! Provide FABM with the vertical indices of the bottom, and the land-sea mask.
519      CALL model%set_bottom_index(mbkt)  ! NB mbkt extents should match dimension lengths provided to set_domain
520      CALL model%set_mask(tmask,tmask(:,:,1)) ! NB tmask extents should match dimension lengths provided to set_domain
521
522      ! Initialize state and send pointers to state data to FABM
523      ! We mask land points in states with zeros, as per with NEMO "convention"
524      ! NB we cannot call model%initialize_*_state at this point, because model%start has not been called yet.
525      DO jn=1,jp_fabm
526         trn(:,:,:,jp_fabm_m1+jn) = model%interior_state_variables(jn)%initial_value * tmask
527         CALL model%link_interior_state_data(jn,trn(:,:,:,jp_fabm_m1+jn))
528      END DO
529      DO jn=1,jp_fabm_surface
530         fabm_st2Dn(:,:,jn) = model%surface_state_variables(jn)%initial_value * tmask(:,:,1)
531         CALL model%link_surface_state_data(jn,fabm_st2Dn(:,:,jn))
532      END DO
533      DO jn=1,jp_fabm_bottom
534         fabm_st2Dn(:,:,jp_fabm_surface+jn) = model%bottom_state_variables(jn)%initial_value * tmask(:,:,1)
535         CALL model%link_bottom_state_data(jn,fabm_st2Dn(:,:,jp_fabm_surface+jn))
536      END DO
537
538      ! Send pointers to environmental data to FABM
539      CALL model%link_interior_data(fabm_standard_variables%depth, fsdept(:,:,:))
540      CALL model%link_interior_data(fabm_standard_variables%temperature, tsn(:,:,:,jp_tem))
541      CALL model%link_interior_data(fabm_standard_variables%practical_salinity, tsn(:,:,:,jp_sal))
542      IF (ALLOCATED(rho)) CALL model%link_interior_data(fabm_standard_variables%density, rho(:,:,:))
543      IF (ALLOCATED(prn)) CALL model%link_interior_data(fabm_standard_variables%pressure, prn)
544      IF (ALLOCATED(taubot)) CALL model%link_horizontal_data(fabm_standard_variables%bottom_stress, taubot(:,:))
545      CALL model%link_interior_data(fabm_standard_variables%cell_thickness, fse3t(:,:,:))
546      CALL model%link_horizontal_data(fabm_standard_variables%latitude, gphit)
547      CALL model%link_horizontal_data(fabm_standard_variables%longitude, glamt)
548      CALL model%link_scalar(fabm_standard_variables%number_of_days_since_start_of_the_year, daynumber_in_year)
549      CALL model%link_horizontal_data(fabm_standard_variables%wind_speed, wndm(:,:))
550      CALL model%link_horizontal_data(fabm_standard_variables%surface_downwelling_shortwave_flux, qsr(:,:))
551      CALL model%link_horizontal_data(fabm_standard_variables%bottom_depth_below_geoid, bathy(:,:))
552      CALL model%link_horizontal_data(fabm_standard_variables%ice_area_fraction, fr_i(:,:))
553
554      ! Obtain user-specified input variables (read from NetCDF file)
555      CALL link_inputs
556      CALL update_inputs(nit000, .FALSE.)
557
558      ! Set mask for negativity corrections to the relevant states
559      lk_rad_fabm(:) = .FALSE.
560      DO jn=1,jp_fabm
561        IF (model%interior_state_variables(jn)%minimum >= 0._wp) THEN
562          lk_rad_fabm(jn) = .TRUE.
563          IF(lwp) WRITE(numout,*) 'FABM clipping for '//TRIM(model%interior_state_variables(jn)%name)//' activated.'
564        END IF
565      END DO
566
567      ! Mask land points in states with zeros, not nice, but coherent
568      ! with NEMO "convention":
569      DO jn=jp_fabm0,jp_fabm1
570        WHERE (tmask==0._wp)
571          trn(:,:,:,jn)=0._wp
572        END WHERE
573      END DO
574      DO jn=1,jp_fabm_surface+jp_fabm_bottom
575        WHERE (tmask(:,:,1)==0._wp)
576          fabm_st2Dn(:,:,jn)=0._wp
577        END WHERE
578      END DO
579
580      ! Copy initial condition for interface-attached state variables to "previous" state field
581      ! NB NEMO does this itself for pelagic state variables (trb) in TOP_SRC/trcini.F90.
582      fabm_st2Db = fabm_st2Dn
583
584      ! Initialise repair counters
585      repair_interior_count = 0
586      repair_surface_count = 0
587      repair_bottom_count = 0
588
589   END FUNCTION trc_sms_fabm_alloc
590
591   SUBROUTINE nemo_fabm_start()
592      INTEGER :: jn
593
594      ! Make FABM aware of diagnostics that are not needed [not included in output]
595      ! This works only after iom has completely initialised, because it depends on iom_use
596      DO jn=1,size(model%interior_diagnostic_variables)
597         model%interior_diagnostic_variables(jn)%save = iom_use(model%interior_diagnostic_variables(jn)%name) &
598            .or. iom_use(TRIM(model%interior_diagnostic_variables(jn)%name)//'_VINT')
599      END DO
600      DO jn=1,size(model%horizontal_diagnostic_variables)
601         model%horizontal_diagnostic_variables(jn)%save = iom_use(model%horizontal_diagnostic_variables(jn)%name)
602      END DO
603
604      ! Check whether FABM has all required data
605      ! [after this, the save attribute of diagnostic variables can no longer change!]
606      CALL model%start()
607
608      started = .TRUE.
609   END SUBROUTINE
610
611#else
612   !!----------------------------------------------------------------------
613   !!   Dummy module                                        No FABM model
614   !!----------------------------------------------------------------------
615CONTAINS
616   SUBROUTINE trc_sms_fabm( kt )             ! Empty routine
617      INTEGER, INTENT( in ) ::   kt
618      WRITE(*,*) 'trc_sms_fabm: You should not have seen this print! error?', kt
619   END SUBROUTINE trc_sms_fabm
620#endif
621
622   !!======================================================================
623END MODULE trcsms_fabm
Note: See TracBrowser for help on using the repository browser.