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

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

Add required diagnostics.

File size: 18.9 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
124      IF(lwp) WRITE(numout,*) 'DAF: jp_fabm_n5s = ', jp_fabm_n5s
125
126      !jp_fabmdia_chltot = fabm_diag_index( 'total_chlorophyll' )
127      !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
128     
129      IF (lwp) THEN
130         ! write field_def_fabm.xml on lead process
131         OPEN(UNIT=xml_unit,FILE='field_def_fabm.xml',ACTION='WRITE',STATUS='REPLACE')
132
133         WRITE (xml_unit,1000) '<field_definition level="1" prec="4" operation="average" enabled=".TRUE." default_value="1.e20" >'
134
135         WRITE (xml_unit,1000) ' <field_group id="ptrc_T" grid_ref="grid_T_3D">'
136         DO jn=1,jp_fabm
137            CALL write_variable_xml(xml_unit,model%state_variables(jn))
138#if defined key_trdtrc
139            CALL write_trends_xml(xml_unit,model%state_variables(jn))
140#endif
141         END DO
142         WRITE (xml_unit,1000) ' </field_group>'
143     
144         WRITE (xml_unit,1000) ' <field_group id="sf_T" grid_ref="grid_T_2D">'
145         DO jn=1,jp_fabm_surface
146            CALL write_variable_xml(xml_unit,model%surface_state_variables(jn))
147         END DO
148         DO jn=1,jp_fabm_bottom
149            CALL write_variable_xml(xml_unit,model%bottom_state_variables(jn))
150         END DO
151         WRITE (xml_unit,1000) ' </field_group>'
152
153         WRITE (xml_unit,1000) ' <field_group id="diad_T" grid_ref="grid_T_2D">'
154         DO jn=1,size(model%diagnostic_variables)
155            CALL write_variable_xml(xml_unit,model%diagnostic_variables(jn),3)
156         END DO
157         DO jn=1,size(model%horizontal_diagnostic_variables)
158            CALL write_variable_xml(xml_unit,model%horizontal_diagnostic_variables(jn))
159         END DO
160         WRITE (xml_unit,1000) ' </field_group>'
161
162         WRITE (xml_unit,1000) ' <field_group id="fabm_scalar" grid_ref="grid_0">'
163         DO jn=1,size(model%conserved_quantities)
164            CALL write_variable_xml(xml_unit,model%conserved_quantities(jn))
165         END DO
166         WRITE (xml_unit,1000) ' </field_group>'
167
168         WRITE (xml_unit,1000) ' <field_group id="fabm_input" grid_ref="grid_T_2D">'
169         input_data => first_input_data
170         DO WHILE (ASSOCIATED(input_data))
171           input_pointer => input_data
172           CALL write_input_xml(xml_unit,input_pointer)
173            input_data => input_data%next
174         END DO
175         river_data => first_river_data
176         DO WHILE (ASSOCIATED(river_data))
177           input_pointer => river_data
178           CALL write_input_xml(xml_unit,input_pointer,3)
179            river_data => river_data%next
180         END DO
181         WRITE (xml_unit,1000) ' </field_group>'
182
183         WRITE (xml_unit,1000) '</field_definition>'
184
185         CLOSE(xml_unit)
186      END IF
187      IF( lk_mpp )   CALL mppsync !Ensure field_def_fabm is ready.
188     
189      MLD_MAX(:,:) = 0.0
190      PGROW_AVG(:,:) = 0.0
191      PLOSS_AVG(:,:) = 0.0
192      PHYT_AVG(:,:) = 0.0
193
1941000 FORMAT (A)
195
196   END SUBROUTINE nemo_fabm_init
197
198   SUBROUTINE write_variable_xml(xml_unit,variable,flag_grid_ref)
199      INTEGER,INTENT(IN) :: xml_unit
200      INTEGER,INTENT(IN),OPTIONAL :: flag_grid_ref
201      CLASS (type_external_variable),INTENT(IN) :: variable
202
203      CHARACTER(LEN=20) :: missing_value,string_dimensions
204      INTEGER :: number_dimensions
205
206      ! Check variable dimension for grid_ref specificaiton.
207      ! Default is to not specify the grid_ref in the field definition.
208      IF (present(flag_grid_ref)) THEN
209          number_dimensions=flag_grid_ref
210      ELSE
211          number_dimensions=-1 !default, don't specify grid_ref
212      ENDIF
213
214      WRITE (missing_value,'(E9.3)') variable%missing_value
215      WRITE (string_dimensions,'(I1)') number_dimensions
216      SELECT CASE (number_dimensions)
217      CASE (3)
218         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" />'
219      CASE (2)
220         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"/>'
221      CASE (0)
222         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"/>'
223      CASE (-1)
224         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))//'" />'
225      CASE default
226         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!!!'
227      END SELECT
228         
229   END SUBROUTINE write_variable_xml
230
231   SUBROUTINE write_trends_xml(xml_unit,variable,flag_grid_ref)
232      INTEGER,INTENT(IN) :: xml_unit
233      INTEGER,INTENT(IN),OPTIONAL :: flag_grid_ref
234      CLASS (type_external_variable),INTENT(IN) :: variable
235
236      INTEGER :: number_dimensions,i
237      CHARACTER(LEN=20) :: missing_value,string_dimensions
238      CHARACTER(LEN=3),DIMENSION(13),PARAMETER :: trd_tags = (/ &
239        'XAD','YAD','ZAD','LDF','BBL','FOR','ZDF','DMP','SMS','ATF', &
240        'RDB','RDN','VMV' /)
241
242      ! Check variable dimension for grid_ref specificaiton.
243      ! Default is to not specify the grid_ref in the field definition.
244      IF (present(flag_grid_ref)) THEN
245          number_dimensions=flag_grid_ref
246      ELSE
247          number_dimensions=-1 !default, don't specify grid_ref
248      ENDIF
249
250      WRITE (missing_value,'(E9.3)') -2.E20
251      WRITE (string_dimensions,'(I1)') number_dimensions
252      !SELECT CASE (number_dimensions)
253      !CASE (3)
254      !  DO i=1,size(trd_tags)
255      !   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" />'
256      !  END DO
257      !CASE (-1)
258      !  DO i=1,size(trd_tags)
259      !   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))//'" />'
260      !  END DO
261      !CASE default
262      !   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!!!'
263      !END SELECT
264
265   END SUBROUTINE write_trends_xml
266
267   SUBROUTINE write_input_xml(xml_unit,variable,flag_grid_ref)
268      INTEGER,INTENT(IN) :: xml_unit
269      INTEGER,INTENT(IN),OPTIONAL :: flag_grid_ref
270      CLASS(type_input_variable),POINTER,INTENT(IN) :: variable
271
272      INTEGER :: number_dimensions,i
273      CHARACTER(LEN=20) :: missing_value,string_dimensions
274
275      ! Check variable dimension for grid_ref specificaiton.
276      ! Default is to not specify the grid_ref in the field definition.
277      IF (present(flag_grid_ref)) THEN
278          number_dimensions=flag_grid_ref
279      ELSE
280          number_dimensions=-1 !default, don't specify grid_ref
281      ENDIF
282
283      WRITE (missing_value,'(E9.3)') -2.E20
284      WRITE (string_dimensions,'(I1)') number_dimensions
285      SELECT CASE (number_dimensions)
286      CASE (3)
287        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" />'
288      CASE (-1)
289        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))//'" />'
290      CASE default
291         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!!!'
292      END SELECT
293
294   END SUBROUTINE write_input_xml
295
296   SUBROUTINE trc_ini_fabm
297      !!----------------------------------------------------------------------
298      !!                     ***  trc_ini_fabm  *** 
299      !!
300      !! ** Purpose :   initialization for FABM model
301      !!
302      !! ** Method  : - Read the namcfc namelist and check the parameter values
303      !!----------------------------------------------------------------------
304#if defined key_git_version
305      TYPE (type_version),POINTER :: version
306#endif
307      INTEGER :: jn
308
309      !                       ! Allocate FABM arrays
310      IF( trc_sms_fabm_alloc() /= 0 )   CALL ctl_stop( 'STOP', 'trc_ini_fabm: unable to allocate FABM arrays' )
311
312      IF(lwp) WRITE(numout,*)
313      IF(lwp) WRITE(numout,*) ' trc_ini_fabm: initialisation of FABM model'
314      IF(lwp) WRITE(numout,*) ' ~~~~~~~~~~~~~~'
315#if defined key_git_version
316      IF(lwp) WRITE(numout,*) ' NEMO version:   ',git_commit_id,' (',git_branch_name,' branch)'
317      IF(lwp) WRITE(numout,*) ' FABM version:   ',fabm_commit_id,' (',fabm_branch_name,' branch)'
318#endif
319
320      call fabm_initialize_library()
321#if defined key_git_version
322      version => first_module_version
323
324      do while (associated(version))
325         IF(lwp) WRITE(numout,*)  ' '//trim(version%module_name)//' version:   ',trim(version%version_string)
326         version => version%next
327      end do
328#endif
329
330      ! Log mapping of FABM states:
331      IF (lwp) THEN
332         IF (jp_fabm.gt.0) WRITE(numout,*) " FABM tracers:"
333         DO jn=1,jp_fabm
334            WRITE(numout,*) "   State",jn,":",trim(model%state_variables(jn)%name), &
335               " (",trim(model%state_variables(jn)%long_name), &
336               ") [",trim(model%state_variables(jn)%units),"]"
337         ENDDO
338         IF (jp_fabm_surface.gt.0) WRITE(numout,*) "FABM seasurface states:"
339         DO jn=1,jp_fabm_surface
340            WRITE(numout,*) "   State",jn,":",trim(model%surface_state_variables(jn)%name), &
341               " (",trim(model%surface_state_variables(jn)%long_name), &
342               ") [",trim(model%surface_state_variables(jn)%units),"]"
343         ENDDO
344         IF (jp_fabm_bottom.gt.0) WRITE(numout,*) "FABM seafloor states:"
345         DO jn=1,jp_fabm_bottom
346            WRITE(numout,*) "   State",jn,":",trim(model%bottom_state_variables(jn)%name), &
347               " (",trim(model%bottom_state_variables(jn)%long_name), &
348               ") [",trim(model%bottom_state_variables(jn)%units),"]"
349         ENDDO
350      ENDIF
351     
352   END SUBROUTINE trc_ini_fabm
353
354   INTEGER FUNCTION fabm_state_index( state_name )
355      !!----------------------------------------------------------------------
356      !!                     ***  fabm_state_index  *** 
357      !!
358      !! ** Purpose :   return index of a given FABM state variable
359      !!
360      !! ** Method  : - loop through state variables until found
361      !!----------------------------------------------------------------------
362     
363      IMPLICIT NONE
364     
365      CHARACTER(LEN=256), INTENT(IN) :: state_name
366     
367      INTEGER                        :: jn
368
369      !!----------------------------------------------------------------------
370     
371      fabm_state_index = -1
372      !WRITE(numout,*) 'PETE - STATE VARIABLES'
373      DO jn=1,jp_fabm         
374         !WRITE(numout,*) TRIM(model%state_variables(jn)%name)
375         IF (TRIM(model%state_variables(jn)%name) == TRIM(state_name)) THEN
376            fabm_state_index = jn
377            EXIT
378         ENDIF
379      END DO
380      IF (fabm_state_index == -1) THEN
381         CALL ctl_stop( 'Could not find '//TRIM(state_name)//' state variable' )
382      ELSE
383         IF (lwp) WRITE(numout,*) 'DAF: Index for '//TRIM(state_name)//' is: ', fabm_state_index
384      ENDIF
385   
386   END FUNCTION fabm_state_index
387
388   INTEGER FUNCTION fabm_diag_index( diag_name )
389      !!----------------------------------------------------------------------
390      !!                     ***  fabm_state_index  *** 
391      !!
392      !! ** Purpose :   return index of a given FABM diagnostic variable
393      !!
394      !! ** Method  : - loop through diagnostic variables until found
395      !!----------------------------------------------------------------------
396     
397      IMPLICIT NONE
398     
399      CHARACTER(LEN=256), INTENT(IN) :: diag_name
400     
401      INTEGER                        :: jn
402
403      !!----------------------------------------------------------------------
404     
405      fabm_diag_index = -1
406      !WRITE(numout,*) 'PETE - DIAG VARIABLES'
407      DO jn = 1, SIZE(model%diagnostic_variables)
408         !WRITE(numout,*) TRIM(model%diagnostic_variables(jn)%name)
409         !WRITE(numout,*) TRIM(model%diagnostic_variables(jn)%standard_variable%name)
410         IF (TRIM(model%diagnostic_variables(jn)%name) == TRIM(diag_name)) THEN
411            fabm_diag_index = jn
412            EXIT
413         ENDIF
414      END DO
415      IF (fabm_diag_index == -1) THEN
416         CALL ctl_stop( 'Could not find '//TRIM(diag_name)//' diagnostic' )
417      ELSE
418         IF (lwp) WRITE(numout,*) 'DAF: Index for '//TRIM(diag_name)//' is: ', fabm_diag_index
419      ENDIF
420   
421   END FUNCTION fabm_diag_index
422
423#else
424   !!----------------------------------------------------------------------
425   !!   Dummy module                                        No FABM model
426   !!----------------------------------------------------------------------
427CONTAINS
428   SUBROUTINE nemo_fabm_init
429   END SUBROUTINE nemo_fabm_init
430
431   SUBROUTINE trc_ini_fabm            ! Empty routine
432   END SUBROUTINE trc_ini_fabm
433#endif
434
435   !!======================================================================
436END MODULE trcini_fabm
Note: See TracBrowser for help on using the repository browser.