source: branches/UKMO/CO6_KD490_amm7_oper_fabm_hem08/NEMOGCM/NEMO/TOP_SRC/FABM/trcini_fabm.F90 @ 9336

Last change on this file since 9336 was 9336, checked in by dford, 2 years ago

Add growth and loss diagnostics.

File size: 19.0 KB
Line 
1MODULE trcini_fabm
2   !!======================================================================
3   !!                         ***  MODULE trcini_fabm  ***
4   !! TOP :   initialisation of the FABM tracers
5   !!======================================================================
6   !! History :   2.0  !  2007-12  (C. Ethe, G. Madec) Original code
7   !!----------------------------------------------------------------------
8#if defined key_fabm
9   !!----------------------------------------------------------------------
10   !!   'key_fabm'                                               FABM tracers
11   !!----------------------------------------------------------------------
12   !! trc_ini_fabm   : FABM model initialisation
13   !!----------------------------------------------------------------------
14   USE par_trc         ! TOP parameters
15   USE oce_trc
16   USE trc
17   USE par_fabm
18   USE trcsms_fabm
19   USE fabm_config,ONLY: fabm_create_model_from_yaml_file
20   USE fabm,ONLY: type_external_variable, fabm_initialize_library
21   USE inputs_fabm,ONLY: initialize_inputs,link_inputs, &
22     type_input_variable,type_input_data,type_river_data, &
23     first_input_data,first_river_data
24#if defined key_git_version
25   USE fabm_version,ONLY: fabm_commit_id=>git_commit_id, &
26                          fabm_branch_name=>git_branch_name
27   USE fabm_types,ONLY: type_version,first_module_version
28#endif
29
30
31   IMPLICIT NONE
32   PRIVATE
33
34#if defined key_git_version
35! #include "gitversion.h90"
36   CHARACTER(len=*),parameter :: git_commit_id = _NEMO_COMMIT_ID_
37   CHARACTER(len=*),parameter :: git_branch_name = _NEMO_BRANCH_
38#endif
39
40   PUBLIC   trc_ini_fabm   ! called by trcini.F90 module
41   PUBLIC   nemo_fabm_init
42
43   !!----------------------------------------------------------------------
44   !! NEMO/TOP 3.3 , NEMO Consortium (2010)
45   !! $Id$
46   !! Software governed by the CeCILL licence     (NEMOGCM/NEMO_CeCILL.txt)
47   !!----------------------------------------------------------------------
48CONTAINS
49
50   SUBROUTINE nemo_fabm_init()
51      INTEGER :: jn
52      INTEGER, PARAMETER :: xml_unit = 1979
53      TYPE (type_input_data),POINTER :: input_data
54      TYPE (type_river_data),POINTER :: river_data
55      CLASS (type_input_variable),POINTER :: input_pointer
56
57      ! Allow FABM to parse fabm.yaml. This ensures numbers of variables are known.
58      call fabm_create_model_from_yaml_file(model)
59
60      jp_fabm = size(model%state_variables)
61      jp_fabm_bottom = size(model%bottom_state_variables)
62      jp_fabm_surface = size(model%surface_state_variables)
63      jp_fabm0 = jptra + 1
64      jp_fabm1 = jptra + jp_fabm
65      jp_fabm_m1=jptra
66      jptra = jptra + jp_fabm
67      jpdia2d = jpdia2d + size(model%horizontal_diagnostic_variables)
68      jpdia3d = jpdia3d + size(model%diagnostic_variables)
69      jpdiabio = jpdiabio + jp_fabm
70
71      !Initialize input data structures.
72      call initialize_inputs
73
74      IF(lwp) WRITE(numout,*) 'DAF: jp_fabm, jp_fabm0, jp_fabm1, jp_fabm_m1, jptra = ', jp_fabm, jp_fabm0, jp_fabm1, jp_fabm_m1, jptra
75     
76      !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
77      jp_fabm_chl1 = fabm_state_index( 'P1_Chl' )
78      jp_fabm_chl2 = fabm_state_index( 'P2_Chl' )
79      jp_fabm_chl3 = fabm_state_index( 'P3_Chl' )
80      jp_fabm_chl4 = fabm_state_index( 'P4_Chl' )
81      jp_fabm_p1c  = fabm_state_index( 'P1_c' )
82      jp_fabm_p1n  = fabm_state_index( 'P1_n' )
83      jp_fabm_p1p  = fabm_state_index( 'P1_p' )
84      jp_fabm_p1s  = fabm_state_index( 'P1_s' )
85      jp_fabm_p2c  = fabm_state_index( 'P2_c' )
86      jp_fabm_p2n  = fabm_state_index( 'P2_n' )
87      jp_fabm_p2p  = fabm_state_index( 'P2_p' )
88      jp_fabm_p3c  = fabm_state_index( 'P3_c' )
89      jp_fabm_p3n  = fabm_state_index( 'P3_n' )
90      jp_fabm_p3p  = fabm_state_index( 'P3_p' )
91      jp_fabm_p4c  = fabm_state_index( 'P4_c' )
92      jp_fabm_p4n  = fabm_state_index( 'P4_n' )
93      jp_fabm_p4p  = fabm_state_index( 'P4_p' )
94      jp_fabm_z4c  = fabm_state_index( 'Z4_c' )
95      jp_fabm_z5c  = fabm_state_index( 'Z5_c' )
96      jp_fabm_z5n  = fabm_state_index( 'Z5_n' )
97      jp_fabm_z5p  = fabm_state_index( 'Z5_p' )
98      jp_fabm_z6c  = fabm_state_index( 'Z6_c' )
99      jp_fabm_z6n  = fabm_state_index( 'Z6_n' )
100      jp_fabm_z6p  = fabm_state_index( 'Z6_p' )
101      jp_fabm_n1p  = fabm_state_index( 'N1_p' )
102      jp_fabm_n3n  = fabm_state_index( 'N3_n' )
103      jp_fabm_n4n  = fabm_state_index( 'N4_n' )
104      jp_fabm_n5s  = fabm_state_index( 'N5_s' )
105      jp_fabm_o2o  = fabm_state_index( 'O2_o' )
106      jp_fabm_o3c  = fabm_state_index( 'O3_c' )
107      jp_fabm_o3a  = fabm_state_index( 'O3_bioalk' )
108      jp_fabm_r4n  = fabm_state_index( 'R4_n' )
109      jp_fabm_r6n  = fabm_state_index( 'R6_n' )
110      jp_fabm_r8n  = fabm_state_index( 'R8_n' )
111      !jp_fabm_netp1= fabm_diag_index( 'P1_netP' )
112      !jp_fabm_netp2= fabm_diag_index( 'P2_netP' )
113      !jp_fabm_netp3= fabm_diag_index( 'P3_netP' )
114      !jp_fabm_netp4= fabm_diag_index( 'P4_netP' )
115      !jp_fabm_o3ph = fabm_diag_index( 'ph_reported_on_total_scale' )
116      !jp_fabm_o3pc = fabm_diag_index( 'mole_concentration_of_carbonate_expressed_as_carbon')
117      jp_fabm_o3ph = fabm_diag_index( 'O3_pH' )
118      jp_fabm_o3pc = fabm_diag_index( 'O3_pCO2')
119      jp_fabmdia_chltot = fabm_diag_index( 'total_chlorophyll_calculator_result' )
120      jp_fabmdia_netpp = fabm_diag_index( 'net_primary_production_result' )
121      jp_fabm_xeps = fabm_diag_index( 'light_xEPS' )
122      jp_fabmdia_phytot = fabm_diag_index( 'total_phytoplankton_result' )
123      jp_fabmdia_pgrow = fabm_diag_index( 'p_grow_sum_result' )
124      jp_fabmdia_ploss = fabm_diag_index( 'p_loss_sum_result' )
125     
126
127      IF(lwp) WRITE(numout,*) 'DAF: jp_fabm_n5s = ', jp_fabm_n5s
128
129      !jp_fabmdia_chltot = fabm_diag_index( 'total_chlorophyll' )
130      !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
131     
132      IF (lwp) THEN
133         ! write field_def_fabm.xml on lead process
134         OPEN(UNIT=xml_unit,FILE='field_def_fabm.xml',ACTION='WRITE',STATUS='REPLACE')
135
136         WRITE (xml_unit,1000) '<field_definition level="1" prec="4" operation="average" enabled=".TRUE." default_value="1.e20" >'
137
138         WRITE (xml_unit,1000) ' <field_group id="ptrc_T" grid_ref="grid_T_3D">'
139         DO jn=1,jp_fabm
140            CALL write_variable_xml(xml_unit,model%state_variables(jn))
141#if defined key_trdtrc
142            CALL write_trends_xml(xml_unit,model%state_variables(jn))
143#endif
144         END DO
145         WRITE (xml_unit,1000) ' </field_group>'
146     
147         WRITE (xml_unit,1000) ' <field_group id="sf_T" grid_ref="grid_T_2D">'
148         DO jn=1,jp_fabm_surface
149            CALL write_variable_xml(xml_unit,model%surface_state_variables(jn))
150         END DO
151         DO jn=1,jp_fabm_bottom
152            CALL write_variable_xml(xml_unit,model%bottom_state_variables(jn))
153         END DO
154         WRITE (xml_unit,1000) ' </field_group>'
155
156         WRITE (xml_unit,1000) ' <field_group id="diad_T" grid_ref="grid_T_2D">'
157         DO jn=1,size(model%diagnostic_variables)
158            CALL write_variable_xml(xml_unit,model%diagnostic_variables(jn),3)
159         END DO
160         DO jn=1,size(model%horizontal_diagnostic_variables)
161            CALL write_variable_xml(xml_unit,model%horizontal_diagnostic_variables(jn))
162         END DO
163         WRITE (xml_unit,1000) ' </field_group>'
164
165         WRITE (xml_unit,1000) ' <field_group id="fabm_scalar" grid_ref="grid_0">'
166         DO jn=1,size(model%conserved_quantities)
167            CALL write_variable_xml(xml_unit,model%conserved_quantities(jn))
168         END DO
169         WRITE (xml_unit,1000) ' </field_group>'
170
171         WRITE (xml_unit,1000) ' <field_group id="fabm_input" grid_ref="grid_T_2D">'
172         input_data => first_input_data
173         DO WHILE (ASSOCIATED(input_data))
174           input_pointer => input_data
175           CALL write_input_xml(xml_unit,input_pointer)
176            input_data => input_data%next
177         END DO
178         river_data => first_river_data
179         DO WHILE (ASSOCIATED(river_data))
180           input_pointer => river_data
181           CALL write_input_xml(xml_unit,input_pointer,3)
182            river_data => river_data%next
183         END DO
184         WRITE (xml_unit,1000) ' </field_group>'
185
186         WRITE (xml_unit,1000) '</field_definition>'
187
188         CLOSE(xml_unit)
189      END IF
190      IF( lk_mpp )   CALL mppsync !Ensure field_def_fabm is ready.
191     
192      MLD_MAX(:,:) = 0.0
193      PGROW_AVG(:,:) = 0.0
194      PLOSS_AVG(:,:) = 0.0
195      PHYT_AVG(:,:) = 0.0
196
1971000 FORMAT (A)
198
199   END SUBROUTINE nemo_fabm_init
200
201   SUBROUTINE write_variable_xml(xml_unit,variable,flag_grid_ref)
202      INTEGER,INTENT(IN) :: xml_unit
203      INTEGER,INTENT(IN),OPTIONAL :: flag_grid_ref
204      CLASS (type_external_variable),INTENT(IN) :: variable
205
206      CHARACTER(LEN=20) :: missing_value,string_dimensions
207      INTEGER :: number_dimensions
208
209      ! Check variable dimension for grid_ref specificaiton.
210      ! Default is to not specify the grid_ref in the field definition.
211      IF (present(flag_grid_ref)) THEN
212          number_dimensions=flag_grid_ref
213      ELSE
214          number_dimensions=-1 !default, don't specify grid_ref
215      ENDIF
216
217      WRITE (missing_value,'(E9.3)') variable%missing_value
218      WRITE (string_dimensions,'(I1)') number_dimensions
219      SELECT CASE (number_dimensions)
220      CASE (3)
221         WRITE (xml_unit,'(A)') '  <field id="'//TRIM(variable%name)//'" long_name="'//TRIM(variable%long_name)//'" unit="'//TRIM(variable%units)//'" default_value="'//TRIM(ADJUSTL(missing_value))//'" grid_ref="grid_T_3D" />'
222      CASE (2)
223         WRITE (xml_unit,'(A)') '  <field id="'//TRIM(variable%name)//'" long_name="'//TRIM(variable%long_name)//'" unit="'//TRIM(variable%units)//'" default_value="'//TRIM(ADJUSTL(missing_value))//'" grid_ref="grid_T_2D"/>'
224      CASE (0)
225         WRITE (xml_unit,'(A)') '  <field id="'//TRIM(variable%name)//'" long_name="'//TRIM(variable%long_name)//'" unit="'//TRIM(variable%units)//'" default_value="'//TRIM(ADJUSTL(missing_value))//'" grid_ref="1point"/>'
226      CASE (-1)
227         WRITE (xml_unit,'(A)') '  <field id="'//TRIM(variable%name)//'" long_name="'//TRIM(variable%long_name)//'" unit="'//TRIM(variable%units)//'" default_value="'//TRIM(ADJUSTL(missing_value))//'" />'
228      CASE default
229         IF(lwp) WRITE(numout,*) ' trc_ini_fabm: Failing to initialise output of variable '//TRIM(variable%name)//': Output of '//TRIM(ADJUSTL(string_dimensions))//'-dimensional variables not supported!!!'
230      END SELECT
231         
232   END SUBROUTINE write_variable_xml
233
234   SUBROUTINE write_trends_xml(xml_unit,variable,flag_grid_ref)
235      INTEGER,INTENT(IN) :: xml_unit
236      INTEGER,INTENT(IN),OPTIONAL :: flag_grid_ref
237      CLASS (type_external_variable),INTENT(IN) :: variable
238
239      INTEGER :: number_dimensions,i
240      CHARACTER(LEN=20) :: missing_value,string_dimensions
241      CHARACTER(LEN=3),DIMENSION(13),PARAMETER :: trd_tags = (/ &
242        'XAD','YAD','ZAD','LDF','BBL','FOR','ZDF','DMP','SMS','ATF', &
243        'RDB','RDN','VMV' /)
244
245      ! Check variable dimension for grid_ref specificaiton.
246      ! Default is to not specify the grid_ref in the field definition.
247      IF (present(flag_grid_ref)) THEN
248          number_dimensions=flag_grid_ref
249      ELSE
250          number_dimensions=-1 !default, don't specify grid_ref
251      ENDIF
252
253      WRITE (missing_value,'(E9.3)') -2.E20
254      WRITE (string_dimensions,'(I1)') number_dimensions
255      !SELECT CASE (number_dimensions)
256      !CASE (3)
257      !  DO i=1,size(trd_tags)
258      !   WRITE (xml_unit,'(A)') '  <field id="'//TRIM(trd_tags(i))//'_'//TRIM(variable%name)//'" long_name="'//TRIM(variable%long_name)//' '//TRIM(trd_tags(i))//' trend" unit="'//TRIM(variable%units)//'/s" default_value="'//TRIM(ADJUSTL(missing_value))//'" grid_ref="grid_T_3D" />'
259      !  END DO
260      !CASE (-1)
261      !  DO i=1,size(trd_tags)
262      !   WRITE (xml_unit,'(A)') '  <field id="'//TRIM(trd_tags(i))//'_'//TRIM(variable%name)//'" long_name="'//TRIM(variable%long_name)//' '//TRIM(trd_tags(i))//' trend" unit="'//TRIM(variable%units)//'/s" default_value="'//TRIM(ADJUSTL(missing_value))//'" />'
263      !  END DO
264      !CASE default
265      !   IF(lwp) WRITE(numout,*) ' trc_ini_fabm: Failing to initialise trends of variable '//TRIM(variable%name)//': Output of '//TRIM(ADJUSTL(string_dimensions))//'-dimensional trends not supported!!!'
266      !END SELECT
267
268   END SUBROUTINE write_trends_xml
269
270   SUBROUTINE write_input_xml(xml_unit,variable,flag_grid_ref)
271      INTEGER,INTENT(IN) :: xml_unit
272      INTEGER,INTENT(IN),OPTIONAL :: flag_grid_ref
273      CLASS(type_input_variable),POINTER,INTENT(IN) :: variable
274
275      INTEGER :: number_dimensions,i
276      CHARACTER(LEN=20) :: missing_value,string_dimensions
277
278      ! Check variable dimension for grid_ref specificaiton.
279      ! Default is to not specify the grid_ref in the field definition.
280      IF (present(flag_grid_ref)) THEN
281          number_dimensions=flag_grid_ref
282      ELSE
283          number_dimensions=-1 !default, don't specify grid_ref
284      ENDIF
285
286      WRITE (missing_value,'(E9.3)') -2.E20
287      WRITE (string_dimensions,'(I1)') number_dimensions
288      SELECT CASE (number_dimensions)
289      CASE (3)
290        WRITE (xml_unit,'(A)') '  <field id="'//'INP_'//TRIM(variable%sf(1)%clvar)//'" long_name="'//TRIM(variable%sf(1)%clvar)//' input" unit="" default_value="'//TRIM(ADJUSTL(missing_value))//'" grid_ref="grid_T_3D" />'
291      CASE (-1)
292        WRITE (xml_unit,'(A)') '  <field id="'//'INP_'//TRIM(variable%sf(1)%clvar)//'" long_name="'//TRIM(variable%sf(1)%clvar)//' input" unit="" default_value="'//TRIM(ADJUSTL(missing_value))//'" />'
293      CASE default
294         IF(lwp) WRITE(numout,*) ' trc_ini_fabm: Failing to initialise input diagnostic of variable '//TRIM(variable%sf(1)%clvar)//': Output of '//TRIM(ADJUSTL(string_dimensions))//'-dimensional diagnostic not supported!!!'
295      END SELECT
296
297   END SUBROUTINE write_input_xml
298
299   SUBROUTINE trc_ini_fabm
300      !!----------------------------------------------------------------------
301      !!                     ***  trc_ini_fabm  *** 
302      !!
303      !! ** Purpose :   initialization for FABM model
304      !!
305      !! ** Method  : - Read the namcfc namelist and check the parameter values
306      !!----------------------------------------------------------------------
307#if defined key_git_version
308      TYPE (type_version),POINTER :: version
309#endif
310      INTEGER :: jn
311
312      !                       ! Allocate FABM arrays
313      IF( trc_sms_fabm_alloc() /= 0 )   CALL ctl_stop( 'STOP', 'trc_ini_fabm: unable to allocate FABM arrays' )
314
315      IF(lwp) WRITE(numout,*)
316      IF(lwp) WRITE(numout,*) ' trc_ini_fabm: initialisation of FABM model'
317      IF(lwp) WRITE(numout,*) ' ~~~~~~~~~~~~~~'
318#if defined key_git_version
319      IF(lwp) WRITE(numout,*) ' NEMO version:   ',git_commit_id,' (',git_branch_name,' branch)'
320      IF(lwp) WRITE(numout,*) ' FABM version:   ',fabm_commit_id,' (',fabm_branch_name,' branch)'
321#endif
322
323      call fabm_initialize_library()
324#if defined key_git_version
325      version => first_module_version
326
327      do while (associated(version))
328         IF(lwp) WRITE(numout,*)  ' '//trim(version%module_name)//' version:   ',trim(version%version_string)
329         version => version%next
330      end do
331#endif
332
333      ! Log mapping of FABM states:
334      IF (lwp) THEN
335         IF (jp_fabm.gt.0) WRITE(numout,*) " FABM tracers:"
336         DO jn=1,jp_fabm
337            WRITE(numout,*) "   State",jn,":",trim(model%state_variables(jn)%name), &
338               " (",trim(model%state_variables(jn)%long_name), &
339               ") [",trim(model%state_variables(jn)%units),"]"
340         ENDDO
341         IF (jp_fabm_surface.gt.0) WRITE(numout,*) "FABM seasurface states:"
342         DO jn=1,jp_fabm_surface
343            WRITE(numout,*) "   State",jn,":",trim(model%surface_state_variables(jn)%name), &
344               " (",trim(model%surface_state_variables(jn)%long_name), &
345               ") [",trim(model%surface_state_variables(jn)%units),"]"
346         ENDDO
347         IF (jp_fabm_bottom.gt.0) WRITE(numout,*) "FABM seafloor states:"
348         DO jn=1,jp_fabm_bottom
349            WRITE(numout,*) "   State",jn,":",trim(model%bottom_state_variables(jn)%name), &
350               " (",trim(model%bottom_state_variables(jn)%long_name), &
351               ") [",trim(model%bottom_state_variables(jn)%units),"]"
352         ENDDO
353      ENDIF
354     
355   END SUBROUTINE trc_ini_fabm
356
357   INTEGER FUNCTION fabm_state_index( state_name )
358      !!----------------------------------------------------------------------
359      !!                     ***  fabm_state_index  *** 
360      !!
361      !! ** Purpose :   return index of a given FABM state variable
362      !!
363      !! ** Method  : - loop through state variables until found
364      !!----------------------------------------------------------------------
365     
366      IMPLICIT NONE
367     
368      CHARACTER(LEN=256), INTENT(IN) :: state_name
369     
370      INTEGER                        :: jn
371
372      !!----------------------------------------------------------------------
373     
374      fabm_state_index = -1
375      !WRITE(numout,*) 'PETE - STATE VARIABLES'
376      DO jn=1,jp_fabm         
377         !WRITE(numout,*) TRIM(model%state_variables(jn)%name)
378         IF (TRIM(model%state_variables(jn)%name) == TRIM(state_name)) THEN
379            fabm_state_index = jn
380            EXIT
381         ENDIF
382      END DO
383      IF (fabm_state_index == -1) THEN
384         CALL ctl_stop( 'Could not find '//TRIM(state_name)//' state variable' )
385      ELSE
386         IF (lwp) WRITE(numout,*) 'DAF: Index for '//TRIM(state_name)//' is: ', fabm_state_index
387      ENDIF
388   
389   END FUNCTION fabm_state_index
390
391   INTEGER FUNCTION fabm_diag_index( diag_name )
392      !!----------------------------------------------------------------------
393      !!                     ***  fabm_state_index  *** 
394      !!
395      !! ** Purpose :   return index of a given FABM diagnostic variable
396      !!
397      !! ** Method  : - loop through diagnostic variables until found
398      !!----------------------------------------------------------------------
399     
400      IMPLICIT NONE
401     
402      CHARACTER(LEN=256), INTENT(IN) :: diag_name
403     
404      INTEGER                        :: jn
405
406      !!----------------------------------------------------------------------
407     
408      fabm_diag_index = -1
409      !WRITE(numout,*) 'PETE - DIAG VARIABLES'
410      DO jn = 1, SIZE(model%diagnostic_variables)
411         !WRITE(numout,*) TRIM(model%diagnostic_variables(jn)%name)
412         !WRITE(numout,*) TRIM(model%diagnostic_variables(jn)%standard_variable%name)
413         IF (TRIM(model%diagnostic_variables(jn)%name) == TRIM(diag_name)) THEN
414            fabm_diag_index = jn
415            EXIT
416         ENDIF
417      END DO
418      IF (fabm_diag_index == -1) THEN
419         CALL ctl_stop( 'Could not find '//TRIM(diag_name)//' diagnostic' )
420      ELSE
421         IF (lwp) WRITE(numout,*) 'DAF: Index for '//TRIM(diag_name)//' is: ', fabm_diag_index
422      ENDIF
423   
424   END FUNCTION fabm_diag_index
425
426#else
427   !!----------------------------------------------------------------------
428   !!   Dummy module                                        No FABM model
429   !!----------------------------------------------------------------------
430CONTAINS
431   SUBROUTINE nemo_fabm_init
432   END SUBROUTINE nemo_fabm_init
433
434   SUBROUTINE trc_ini_fabm            ! Empty routine
435   END SUBROUTINE trc_ini_fabm
436#endif
437
438   !!======================================================================
439END MODULE trcini_fabm
Note: See TracBrowser for help on using the repository browser.