source: branches/UKMO/CO6_KD490_amm7_oper_fabm/NEMOGCM/NEMO/TOP_SRC/FABM/trcini_fabm.F90 @ 8050

Last change on this file since 8050 was 8050, checked in by dford, 3 years ago

Re-clear svn keywords following merge.

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