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 @ 15480

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

Changes as in the git branch NEMO-FABMv1-ERSEM

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