forked from AquaticEcoDynamics/libaed2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaed2_totals.F90
383 lines (333 loc) · 16.3 KB
/
aed2_totals.F90
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
!###############################################################################
! !
! .----------------. .----------------. .----------------. !
! | .--------------. || .--------------. || .--------------. | !
! | | _________ | || | ____ | || | _________ | | !
! | | | _ _ | | || | .' `. | || | | _ _ | | | !
! | | |_/ | | \_| | || | / .--. \ | || | |_/ | | \_| | | !
! | | | | | || | | | | | | || | | | | | !
! | | _| |_ | || | \ `--' / | || | _| |_ | | !
! | | |_____| | || | `.____.' | || | |_____| | | !
! | | | || | | || | | | !
! | '--------------' || '--------------' || '--------------' | !
! '----------------' '----------------' '----------------' !
! !
!###############################################################################
!# #
!# aed2_totals.F90 #
!# #
!# Developed by : #
!# AquaticEcoDynamics (AED) Group #
!# School of Agriculture and Environment #
!# The University of Western Australia #
!# #
!# http://aquatic.science.uwa.edu.au/ #
!# #
!# Copyright 2013 - 2019 - The University of Western Australia #
!# #
!# GLM is free software: you can redistribute it and/or modify #
!# it under the terms of the GNU General Public License as published by #
!# the Free Software Foundation, either version 3 of the License, or #
!# (at your option) any later version. #
!# #
!# GLM is distributed in the hope that it will be useful, #
!# but WITHOUT ANY WARRANTY; without even the implied warranty of #
!# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
!# GNU General Public License for more details. #
!# #
!# You should have received a copy of the GNU General Public License #
!# along with this program. If not, see <http://www.gnu.org/licenses/>. #
!# #
!# ----------------------------------------------------------------------- #
!# #
!# Created July 2012 #
!# #
!###############################################################################
#include "aed2.h"
!
MODULE aed2_totals
!-------------------------------------------------------------------------------
! aed2_totals --- totals model
!
! The AED module "totals" contains only diagnostic variables to provide totals
! of other variables (eg tn, tss) or derived metrics of interest (eg turbidity)
!-------------------------------------------------------------------------------
USE aed2_core
IMPLICIT NONE
PRIVATE
!
PUBLIC aed2_totals_data_t
!
TYPE,extends(aed2_model_data_t) :: aed2_totals_data_t
!# Variable identifiers
INTEGER :: id_totals_tn, id_totals_tkn, id_totals_tp, id_tfe, id_tal, &
id_totals_toc, id_totals_tss, id_totals_turbidity, &
id_totals_light, id_totals_par, id_totals_uv, id_totals_extc
INTEGER :: num_tn, num_tkn, num_tp, num_toc, num_tss, num_turb, num_tfe, num_tal
INTEGER :: id_par, id_extc
INTEGER,ALLOCATABLE :: id_dep_tn(:), id_dep_tkn(:), id_dep_tp(:), &
id_dep_toc(:), id_dep_tss(:), id_dep_turb(:), &
id_dep_tfe(:), id_dep_tal(:)
AED_REAL,ALLOCATABLE :: tn_varscale(:), tkn_varscale(:), tp_varscale(:), &
toc_varscale(:), tss_varscale(:), turb_varscale(:), &
tfe_varscale(:), tal_varscale(:)
!# Model parameters
LOGICAL :: outputLight, outputMetals
CONTAINS
PROCEDURE :: define => aed2_define_totals
PROCEDURE :: calculate => aed2_calculate_totals
! PROCEDURE :: mobility => aed2_mobility_totals
! PROCEDURE :: light_extinction => aed2_light_extinction_totals
! PROCEDURE :: delete => aed2_delete_totals
END TYPE
! MODULE GLOBALS
INTEGER :: diag_level = 10
!===============================================================================
CONTAINS
!###############################################################################
SUBROUTINE aed2_define_totals(data, namlst)
!-------------------------------------------------------------------------------
! Initialise the AED model
!
! Here, the aed namelist is read and te variables exported
! by the model are registered with AED2.
!-------------------------------------------------------------------------------
!ARGUMENTS
INTEGER,INTENT(in) :: namlst
CLASS (aed2_totals_data_t),INTENT(inout) :: data
!
!LOCALS
INTEGER :: status
INTEGER :: i, num_tn,num_tkn,num_tp,num_toc,num_tss,num_turb,num_tfe,num_tal
! %% NAMELIST
CHARACTER(len=40) :: tn_vars(100) = ''
CHARACTER(len=40) :: tkn_vars(100) = ''
CHARACTER(len=40) :: tp_vars(100) = ''
CHARACTER(len=40) :: toc_vars(100) = ''
CHARACTER(len=40) :: tss_vars(100) = ''
CHARACTER(len=40) :: turb_vars(100) = ''
CHARACTER(len=40) :: tfe_vars(10) = ''
CHARACTER(len=40) :: tal_vars(10) = ''
AED_REAL :: tn_varscale(100) = 1.0
AED_REAL :: tkn_varscale(100) = 1.0
AED_REAL :: tp_varscale(100) = 1.0
AED_REAL :: toc_varscale(100) = 1.0
AED_REAL :: tss_varscale(100) = 1.0
AED_REAL :: turb_varscale(100) = 1.0
AED_REAL :: tfe_varscale(10) = 1.0
AED_REAL :: tal_varscale(10) = 1.0
LOGICAL :: outputLight = .FALSE.
! %% END NAMELIST
NAMELIST /aed2_totals/ tn_vars, tn_varscale, tkn_vars, tkn_varscale, &
tp_vars, tp_varscale, toc_vars, toc_varscale, &
tss_vars, tss_varscale, turb_vars, turb_varscale, &
tfe_vars, tfe_varscale, tal_vars, tal_varscale, &
outputLight
!
!-------------------------------------------------------------------------------
!BEGIN
print *," aed2_totals initialization"
! Read the namelist
read(namlst,nml=aed2_totals,iostat=status)
IF (status /= 0) STOP 'Error reading namelist aed2_totals'
data%outputLight = outputLight
num_tn = 0 ; num_tkn = 0 ; num_tp = 0 ; num_toc = 0
num_tss = 0 ; num_turb = 0 ; num_tfe = 0 ; num_tal = 0
DO i=1,100 ; IF (tn_vars(i) .EQ. '' ) THEN ; num_tn = i-1 ; EXIT ; ENDIF ; ENDDO
DO i=1,100 ; IF (tkn_vars(i) .EQ. '' ) THEN ; num_tkn = i-1 ; EXIT ; ENDIF ; ENDDO
DO i=1,100 ; IF (tp_vars(i) .EQ. '' ) THEN ; num_tp = i-1 ; EXIT ; ENDIF ; ENDDO
DO i=1,100 ; IF (toc_vars(i) .EQ. '' ) THEN ; num_toc = i-1 ; EXIT ; ENDIF ; ENDDO
DO i=1,100 ; IF (tss_vars(i) .EQ. '' ) THEN ; num_tss = i-1 ; EXIT ; ENDIF ; ENDDO
DO i=1,100 ; IF (turb_vars(i).EQ. '' ) THEN ; num_turb= i-1 ; EXIT ; ENDIF ; ENDDO
DO i=1,100 ; IF (tfe_vars(i) .EQ. '' ) THEN ; num_tfe = i-1 ; EXIT ; ENDIF ; ENDDO
DO i=1,100 ; IF (tal_vars(i) .EQ. '' ) THEN ; num_tal = i-1 ; EXIT ; ENDIF ; ENDDO
ALLOCATE(data%id_dep_tn(num_tn)) ; ALLOCATE(data%tn_varscale(num_tn))
ALLOCATE(data%id_dep_tkn(num_tkn)) ; ALLOCATE(data%tkn_varscale(num_tkn))
ALLOCATE(data%id_dep_tp(num_tp)) ; ALLOCATE(data%tp_varscale(num_tp))
ALLOCATE(data%id_dep_toc(num_toc)) ; ALLOCATE(data%toc_varscale(num_toc))
ALLOCATE(data%id_dep_tss(num_tss)) ; ALLOCATE(data%tss_varscale(num_tss))
ALLOCATE(data%id_dep_turb(num_turb)) ; ALLOCATE(data%turb_varscale(num_turb))
ALLOCATE(data%id_dep_tfe(num_tfe)) ; ALLOCATE(data%tfe_varscale(num_tfe))
ALLOCATE(data%id_dep_tal(num_tal)) ; ALLOCATE(data%tal_varscale(num_tal))
data%num_tn = num_tn
data%num_tkn = num_tkn
data%num_tp = num_tp
data%num_toc = num_toc
data%num_tss = num_tss
data%num_turb = num_turb
data%num_tfe = num_tfe
data%num_tal = num_tal
! Register external state variable dependencies
DO i=1,data%num_tn
data%id_dep_tn(i) = aed2_locate_variable(tn_vars(i))
data%tn_varscale(i) = tn_varscale(i)
!print*,' TN : ', TRIM(tn_vars(i)), ' * ', data%tn_varscale(i)
ENDDO
DO i=1,data%num_tkn
data%id_dep_tkn(i) = aed2_locate_variable(tkn_vars(i))
data%tkn_varscale(i) = tkn_varscale(i)
!print*,' TKN : ', TRIM(tkn_vars(i)), ' * ', data%tkn_varscale(i)
ENDDO
DO i=1,data%num_tp
data%id_dep_tp(i) = aed2_locate_variable(tp_vars(i))
data%tp_varscale(i) = tp_varscale(i)
!print*,' TP : ', TRIM(tp_vars(i)), ' * ', data%tp_varscale(i)
ENDDO
DO i=1,data%num_toc
data%id_dep_toc(i) = aed2_locate_variable(toc_vars(i))
data%toc_varscale(i) = toc_varscale(i)
!print*,' TOC : ', TRIM(toc_vars(i)), ' * ', data%toc_varscale(i)
ENDDO
DO i=1,data%num_tss
data%id_dep_tss(i) = aed2_locate_variable(tss_vars(i))
data%tss_varscale(i) = tss_varscale(i)
!print*,' TSS : ', TRIM(tss_vars(i)), ' * ', data%tss_varscale(i)
ENDDO
DO i=1,data%num_turb
data%id_dep_turb(i) = aed2_locate_variable(turb_vars(i))
data%turb_varscale(i) = turb_varscale(i)
!print*,' TURB : ', TRIM(turb_vars(i)), ' * ', data%turb_varscale(i)
ENDDO
DO i=1,data%num_tfe
data%id_dep_tfe(i) = aed2_locate_variable(tfe_vars(i))
data%tfe_varscale(i) = tfe_varscale(i)
!print*,' TFE : ', TRIM(tfe_vars(i)), ' * ', data%tfe_varscale(i)
ENDDO
DO i=1,data%num_tal
data%id_dep_tal(i) = aed2_locate_variable(tal_vars(i))
data%tal_varscale(i) = tal_varscale(i)
!print*,' TAL : ', TRIM(tal_vars(i)), ' * ', data%tal_varscale(i)
ENDDO
! Register environmental dependencies
IF (data%outputLight) THEN
data%id_par = aed2_locate_global('par')
data%id_extc = aed2_locate_global('extc_coef')
END IF
! Register diagnostic variables
IF (data%num_tn>0) THEN
data%id_totals_tn = aed2_define_diag_variable('tn', &
'mmol/m**3', 'Total Nitrogen')
ENDIF
IF (data%num_tkn>0) THEN
data%id_totals_tkn = aed2_define_diag_variable('tkn', &
'mmol/m**3', 'Total Kjedhal Nitrogen')
ENDIF
IF (data%num_tp>0) THEN
data%id_totals_tp = aed2_define_diag_variable('tp', &
'mmol/m**3', 'Total Phosphorus')
ENDIF
IF (data%num_toc>0) THEN
data%id_totals_toc = aed2_define_diag_variable('toc', &
'mmol/m**3', 'Total Organic Carbon')
ENDIF
IF (data%num_tss>0) THEN
data%id_totals_tss = aed2_define_diag_variable('tss', &
'mg/L', 'Total Suspended Solids')
ENDIF
IF (data%num_turb>0) THEN
data%id_totals_turbidity = aed2_define_diag_variable('turbidity', &
'NTU', 'Turbidity')
ENDIF
IF (data%num_tfe>0) THEN
data%id_tfe = aed2_define_diag_variable('tfe', &
'mmol/m**3', 'Total Iron')
ENDIF
IF (data%num_tal>0) THEN
data%id_tal = aed2_define_diag_variable('tal', &
'mmol/m**3', 'Total Aluminium')
ENDIF
IF (data%outputLight) THEN
data%id_totals_light = aed2_define_diag_variable('light', &
'W/m2', 'Shortwave light flux')
data%id_totals_par = aed2_define_diag_variable('par', &
'W/m2', 'Photosynthetically active light flux')
data%id_totals_uv = aed2_define_diag_variable('uv', &
'W/m2', 'Ultraviolet light flux')
data%id_totals_extc = aed2_define_diag_variable('extc', &
'W/m2', 'Light extinction coefficient')
ENDIF
END SUBROUTINE aed2_define_totals
!+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
!###############################################################################
SUBROUTINE aed2_calculate_totals(data,column,layer_idx)
!-------------------------------------------------------------------------------
! Right hand sides of aed2_totals model
!-------------------------------------------------------------------------------
!ARGUMENTS
CLASS (aed2_totals_data_t),INTENT(in) :: data
TYPE (aed2_column_t),INTENT(inout) :: column(:)
INTEGER,INTENT(in) :: layer_idx
!
!LOCALS
INTEGER :: i, count
AED_REAL :: val, tot
!-------------------------------------------------------------------------------
!BEGIN
! Sum over TN variables
IF (data%num_tn>0) THEN
tot = 0.
count = ubound(data%id_dep_tn,1)
DO i=1,count ; val = _STATE_VAR_(data%id_dep_tn(i)); tot = tot + (val*data%tn_varscale(i)) ; ENDDO
_DIAG_VAR_(data%id_totals_tn) = tot
ENDIF
! Sum over TKN variables
IF (data%num_tkn>0) THEN
tot = 0.
count = ubound(data%id_dep_tkn,1)
DO i=1,count ; val = _STATE_VAR_(data%id_dep_tkn(i)); tot = tot + (val*data%tkn_varscale(i)) ; ENDDO
_DIAG_VAR_(data%id_totals_tkn) = tot
ENDIF
! Sum over TP variables
IF (data%num_tp>0) THEN
tot = 0.
count = ubound(data%id_dep_tp,1)
DO i=1,count ; val = _STATE_VAR_(data%id_dep_tp(i)); tot = tot + (val*data%tp_varscale(i)) ; ENDDO
_DIAG_VAR_(data%id_totals_tp) = tot
ENDIF
! Sum over TOC variables
IF (data%num_toc>0) THEN
tot = 0.
count = ubound(data%id_dep_toc,1)
DO i=1,count ; val = _STATE_VAR_(data%id_dep_toc(i)); tot = tot + (val*data%toc_varscale(i)) ; ENDDO
_DIAG_VAR_(data%id_totals_toc) = tot
ENDIF
! Sum over TSS variables
IF (data%num_tss>0) THEN
tot = 0.
count = ubound(data%id_dep_tss,1)
DO i=1,count ; val = _STATE_VAR_(data%id_dep_tss(i)); tot = tot + (val*data%tss_varscale(i)) ; ENDDO
_DIAG_VAR_(data%id_totals_tss) = tot
ENDIF
! Sum over Turbidity variables
IF (data%num_turb>0) THEN
tot = 0.
count = ubound(data%id_dep_turb,1)
DO i=1,count ; val = _STATE_VAR_(data%id_dep_turb(i)); tot = tot + (val*data%turb_varscale(i)) ; ENDDO
_DIAG_VAR_(data%id_totals_turbidity) = tot
ENDIF
! Sum over TFe variables
IF (data%num_tfe>0) THEN
tot = 0.
count = ubound(data%id_dep_tfe,1)
DO i=1,count ; val = _STATE_VAR_(data%id_dep_tfe(i)); tot = tot + (val*data%tfe_varscale(i)) ; ENDDO
_DIAG_VAR_(data%id_tfe) = tot
ENDIF
! Sum over TAl variables
IF (data%num_tal>0) THEN
tot = 0.
count = ubound(data%id_dep_tal,1)
DO i=1,count ; val = _STATE_VAR_(data%id_dep_tal(i)); tot = tot + (val*data%tal_varscale(i)) ; ENDDO
_DIAG_VAR_(data%id_tal) = tot
ENDIF
! Light and Extinction Coefficient
IF (data%outputLight) THEN
val = _STATE_VAR_(data%id_par)
_DIAG_VAR_(data%id_totals_light) = val/0.45
_DIAG_VAR_(data%id_totals_par) = val
_DIAG_VAR_(data%id_totals_uv) = (val/0.45)*0.03
val = _STATE_VAR_(data%id_extc)
_DIAG_VAR_(data%id_totals_extc) = val
END IF
END SUBROUTINE aed2_calculate_totals
!+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
END MODULE aed2_totals