forked from AquaticEcoDynamics/libaed2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
aed2_phytoplankton.F90
1320 lines (1187 loc) · 60.9 KB
/
aed2_phytoplankton.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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
!###############################################################################
! !
! .----------------. .----------------. .----------------. !
! | .--------------. || .--------------. || .--------------. | !
! | | ______ | || | ____ ____ | || | ____ ____ | | !
! | | |_ __ \ | || | |_ || _| | || | |_ _||_ _| | | !
! | | | |__) | | || | | |__| | | || | \ \ / / | | !
! | | | ___/ | || | | __ | | || | \ \/ / | | !
! | | _| |_ | || | _| | | |_ | || | _| |_ | | !
! | | |_____| | || | |____||____| | || | |______| | | !
! | | | || | | || | | | !
! | '--------------' || '--------------' || '--------------' | !
! '----------------' '----------------' '----------------' !
! !
!###############################################################################
!# #
!# aed2_phytoplankton.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 August 2011 #
!# #
!###############################################################################
#include "aed2.h"
MODULE aed2_phytoplankton
!-------------------------------------------------------------------------------
! aed2_phytoplankton --- phytoplankton biogeochemical model
!-------------------------------------------------------------------------------
USE aed2_core
! USE aed2_util,ONLY : find_free_lun, &
! exp_integral, &
! aed2_bio_temp_function, &
! fTemp_function, &
! water_viscosity, &
! in_zone_set,
! CSV_TYPE, NML_TYPE
USE aed2_util
USE aed2_bio_utils
IMPLICIT NONE
PRIVATE ! By default make everything private
PUBLIC aed2_phytoplankton_data_t
TYPE,extends(aed2_model_data_t) :: aed2_phytoplankton_data_t
!# Variable identifiers
INTEGER,ALLOCATABLE :: id_p(:)
INTEGER,ALLOCATABLE :: id_in(:)
INTEGER,ALLOCATABLE :: id_ip(:)
INTEGER,ALLOCATABLE :: id_rho(:)
INTEGER,ALLOCATABLE :: id_NtoP(:)
INTEGER,ALLOCATABLE :: id_vvel(:)
INTEGER,ALLOCATABLE :: id_fT(:), id_fI(:), id_fNit(:), &
id_fPho(:), id_fSil(:), id_fSal(:)
INTEGER :: id_Pexctarget,id_Pmorttarget,id_Pupttarget(1:2)
INTEGER :: id_Nexctarget,id_Nmorttarget,id_Nupttarget(1:4)
INTEGER :: id_Cexctarget,id_Cmorttarget,id_Cupttarget
INTEGER :: id_Siexctarget,id_Simorttarget,id_Siupttarget
INTEGER :: id_DOupttarget, id_l_resus, id_Psed_phy
INTEGER :: id_par, id_I_0, id_extc, id_sedzone
INTEGER :: id_tem, id_sal, id_dz, id_dens
INTEGER :: id_GPP, id_NCP, id_PPR, id_NPR, id_dPAR
INTEGER :: id_TPHY, id_TCHLA, id_TIN, id_TIP
INTEGER :: id_MPB, id_d_MPB, id_d_BPP, id_d_BCP, id_d_mpbv
INTEGER :: id_NUP, id_NUP2, id_PUP, id_CUP
!# Model parameters
INTEGER :: num_phytos
TYPE(phyto_data),DIMENSION(:),ALLOCATABLE :: phytos
! LOGICAL :: do_exc,do_mort,do_upt, do_N2uptake
LOGICAL :: do_Puptake, do_Nuptake, do_Cuptake
LOGICAL :: do_Siuptake, do_DOuptake, do_N2uptake
LOGICAL :: do_Pmort, do_Nmort, do_Cmort, do_Simort
LOGICAL :: do_Pexc, do_Nexc, do_Cexc, do_Siexc
INTEGER :: do_mpb, n_zones
AED_REAL :: R_mpbg, R_mpbr, I_Kmpb, mpb_max, theta_mpb_growth, theta_mpb_resp
INTEGER :: nnup, npup
AED_REAL :: dic_per_n
AED_REAL :: min_rho,max_rho
AED_REAL,ALLOCATABLE :: resuspension(:), active_zones(:)
CONTAINS
PROCEDURE :: define => aed2_define_phytoplankton
PROCEDURE :: calculate => aed2_calculate_phytoplankton
PROCEDURE :: calculate_benthic => aed2_calculate_benthic_phytoplankton
PROCEDURE :: mobility => aed2_mobility_phytoplankton
PROCEDURE :: light_extinction => aed2_light_extinction_phytoplankton
! PROCEDURE :: delete => aed2_delete_phytoplankton
END TYPE
! MODULE GLOBALS
INTEGER :: diag_level = 10
AED_REAL :: dtlim = 0.9 * 3600
LOGICAL :: extra_diag = .false.
!===============================================================================
CONTAINS
!###############################################################################
INTEGER FUNCTION load_csv(dbase,pd)
!-------------------------------------------------------------------------------
USE aed2_csv_reader
!-------------------------------------------------------------------------------
!ARGUMENTS
CHARACTER(len=*),INTENT(in) :: dbase
TYPE(phyto_nml_data) :: pd(MAX_PHYTO_TYPES)
!
!LOCALS
INTEGER :: unit, nccols, ccol
CHARACTER(len=32),POINTER,DIMENSION(:) :: csvnames
CHARACTER(len=32) :: name
TYPE(AED_SYMBOL),DIMENSION(:),ALLOCATABLE :: values
INTEGER :: idx_col = 0
LOGICAL :: meh
INTEGER :: ret = 0
!
!BEGIN
!-------------------------------------------------------------------------------
unit = aed_csv_read_header(dbase, csvnames, nccols)
IF (unit <= 0) THEN
load_csv = -1
RETURN !# No file found
ENDIF
ALLOCATE(values(nccols))
DO WHILE ( aed_csv_read_row(unit, values) )
DO ccol=2,nccols
pd(ccol)%p_name = csvnames(ccol)
CALL copy_name(values(1), name)
SELECT CASE (name)
CASE ('p0') ; pd(ccol)%p0 = extract_double(values(ccol))
CASE ('w_p') ; pd(ccol)%w_p = extract_double(values(ccol))
CASE ('Xcc') ; pd(ccol)%Xcc = extract_double(values(ccol))
CASE ('R_growth') ; pd(ccol)%R_growth = extract_double(values(ccol))
CASE ('fT_Method') ; pd(ccol)%fT_Method = extract_integer(values(ccol))
CASE ('theta_growth') ; pd(ccol)%theta_growth = extract_double(values(ccol))
CASE ('T_std') ; pd(ccol)%T_std = extract_double(values(ccol))
CASE ('T_opt') ; pd(ccol)%T_opt = extract_double(values(ccol))
CASE ('T_max') ; pd(ccol)%T_max = extract_double(values(ccol))
CASE ('lightModel') ; pd(ccol)%lightModel = extract_integer(values(ccol))
CASE ('I_K') ; pd(ccol)%I_K = extract_double(values(ccol))
CASE ('I_S') ; pd(ccol)%I_S = extract_double(values(ccol))
CASE ('KePHY') ; pd(ccol)%KePHY = extract_double(values(ccol))
CASE ('f_pr') ; pd(ccol)%f_pr = extract_double(values(ccol))
CASE ('R_resp') ; pd(ccol)%R_resp = extract_double(values(ccol))
CASE ('theta_resp') ; pd(ccol)%theta_resp = extract_double(values(ccol))
CASE ('k_fres') ; pd(ccol)%k_fres = extract_double(values(ccol))
CASE ('k_fdom') ; pd(ccol)%k_fdom = extract_double(values(ccol))
CASE ('salTol') ; pd(ccol)%salTol = extract_integer(values(ccol))
CASE ('S_bep') ; pd(ccol)%S_bep = extract_double(values(ccol))
CASE ('S_maxsp') ; pd(ccol)%S_maxsp = extract_double(values(ccol))
CASE ('S_opt') ; pd(ccol)%S_opt = extract_double(values(ccol))
CASE ('simDINUptake') ; pd(ccol)%simDINUptake = extract_integer(values(ccol))
CASE ('simDONUptake') ; pd(ccol)%simDONUptake = extract_integer(values(ccol))
CASE ('simNFixation') ; pd(ccol)%simNFixation = extract_integer(values(ccol))
CASE ('simINDynamics') ; pd(ccol)%simINDynamics = extract_integer(values(ccol))
CASE ('N_o') ; pd(ccol)%N_o = extract_double(values(ccol))
CASE ('K_N') ; pd(ccol)%K_N = extract_double(values(ccol))
CASE ('X_ncon') ; pd(ccol)%X_ncon = extract_double(values(ccol))
CASE ('X_nmin') ; pd(ccol)%X_nmin = extract_double(values(ccol))
CASE ('X_nmax') ; pd(ccol)%X_nmax = extract_double(values(ccol))
CASE ('R_nuptake') ; pd(ccol)%R_nuptake = extract_double(values(ccol))
CASE ('k_nfix') ; pd(ccol)%k_nfix = extract_double(values(ccol))
CASE ('R_nfix') ; pd(ccol)%R_nfix = extract_double(values(ccol))
CASE ('simDIPUptake') ; pd(ccol)%simDIPUptake = extract_integer(values(ccol))
CASE ('simIPDynamics') ; pd(ccol)%simIPDynamics = extract_integer(values(ccol))
CASE ('P_0') ; pd(ccol)%P_0 = extract_double(values(ccol))
CASE ('K_P') ; pd(ccol)%K_P = extract_double(values(ccol))
CASE ('X_pcon') ; pd(ccol)%X_pcon = extract_double(values(ccol))
CASE ('X_pmin') ; pd(ccol)%X_pmin = extract_double(values(ccol))
CASE ('X_pmax') ; pd(ccol)%X_pmax = extract_double(values(ccol))
CASE ('R_puptake') ; pd(ccol)%R_puptake = extract_double(values(ccol))
CASE ('simSiUptake') ; pd(ccol)%simSiUptake = extract_integer(values(ccol))
CASE ('Si_0') ; pd(ccol)%Si_0 = extract_double(values(ccol))
CASE ('K_Si') ; pd(ccol)%K_Si = extract_double(values(ccol))
CASE ('X_sicon') ; pd(ccol)%X_sicon = extract_double(values(ccol))
CASE DEFAULT ; print *, 'Unknown row "', TRIM(name), '"'
END SELECT
ENDDO
ENDDO
meh = aed_csv_close(unit)
!# don't care if close fails
IF (ASSOCIATED(csvnames)) DEALLOCATE(csvnames)
IF (ALLOCATED(values)) DEALLOCATE(values)
load_csv = ret
END FUNCTION load_csv
!+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
!###############################################################################
SUBROUTINE aed2_phytoplankton_load_params(data, dbase, count, list, settling, resuspension)
!-------------------------------------------------------------------------------
!ARGUMENTS
CLASS (aed2_phytoplankton_data_t),INTENT(inout) :: data
CHARACTER(len=*),INTENT(in) :: dbase
INTEGER,INTENT(in) :: count
INTEGER,INTENT(in) :: list(*)
INTEGER,INTENT(in) :: settling(*)
AED_REAL,INTENT(in) :: resuspension(*)
!
!LOCALS
INTEGER :: status
INTEGER :: i,tfil
AED_REAL :: minNut
TYPE(phyto_nml_data) :: pd(MAX_PHYTO_TYPES)
NAMELIST /phyto_data/ pd
!-------------------------------------------------------------------------------
!BEGIN
SELECT CASE (param_file_type(dbase))
CASE (CSV_TYPE)
status = load_csv(dbase, pd)
CASE (NML_TYPE)
tfil = find_free_lun()
open(tfil,file=dbase, status='OLD',iostat=status)
IF (status /= 0) STOP 'Cannot open phyto_data namelist file'
read(tfil,nml=phyto_data,iostat=status)
close(tfil)
CASE DEFAULT
print *,'Unknown file type "',TRIM(dbase),'"'; status=1
END SELECT
IF (status /= 0) STOP 'Error reading namelist phyto_data'
data%num_phytos = count
ALLOCATE(data%phytos(count))
ALLOCATE(data%id_p(count)) ; data%id_p(:) = 0
ALLOCATE(data%id_in(count)) ; data%id_in(:) = 0
ALLOCATE(data%id_ip(count)) ; data%id_ip(:) = 0
ALLOCATE(data%id_rho(count)) ; data%id_rho(:) = 0
ALLOCATE(data%id_NtoP(count)) ; data%id_NtoP(:) = 0
IF (extra_diag) THEN
ALLOCATE(data%id_fT(count)) ; data%id_fT(:) = 0
ALLOCATE(data%id_fI(count)) ; data%id_fI(:) = 0
ALLOCATE(data%id_fNit(count)) ; data%id_fNit(:) = 0
ALLOCATE(data%id_fPho(count)) ; data%id_fPho(:) = 0
ALLOCATE(data%id_fSil(count)) ; data%id_fSil(:) = 0
ALLOCATE(data%id_fSal(count)) ; data%id_fSal(:) = 0
ALLOCATE(data%id_vvel(count)) ; data%id_vvel(:) = 0
ENDIF
DO i=1,count
! Assign parameters from database to simulated groups
data%phytos(i)%p_name = pd(list(i))%p_name
data%phytos(i)%p0 = pd(list(i))%p0
data%phytos(i)%w_p = pd(list(i))%w_p/secs_per_day
data%phytos(i)%settling = settling(i)
data%phytos(i)%resuspension = resuspension(i)
data%phytos(i)%Xcc = pd(list(i))%Xcc
data%phytos(i)%R_growth = pd(list(i))%R_growth/secs_per_day
data%phytos(i)%fT_Method = pd(list(i))%fT_Method
data%phytos(i)%theta_growth = pd(list(i))%theta_growth
data%phytos(i)%T_std = pd(list(i))%T_std
data%phytos(i)%T_opt = pd(list(i))%T_opt
data%phytos(i)%T_max = pd(list(i))%T_max
data%phytos(i)%lightModel = pd(list(i))%lightModel
data%phytos(i)%I_K = pd(list(i))%I_K
data%phytos(i)%I_S = pd(list(i))%I_S
data%phytos(i)%KePHY = pd(list(i))%KePHY
data%phytos(i)%f_pr = pd(list(i))%f_pr
data%phytos(i)%R_resp = pd(list(i))%R_resp/secs_per_day
data%phytos(i)%theta_resp = pd(list(i))%theta_resp
data%phytos(i)%k_fres = pd(list(i))%k_fres
data%phytos(i)%k_fdom = pd(list(i))%k_fdom
data%phytos(i)%salTol = pd(list(i))%salTol
data%phytos(i)%S_bep = pd(list(i))%S_bep
data%phytos(i)%S_maxsp = pd(list(i))%S_maxsp
data%phytos(i)%S_opt = pd(list(i))%S_opt
data%phytos(i)%simDINUptake = pd(list(i))%simDINUptake
data%phytos(i)%simDONUptake = pd(list(i))%simDONUptake
data%phytos(i)%simNFixation = pd(list(i))%simNFixation
data%phytos(i)%simINDynamics= pd(list(i))%simINDynamics
data%phytos(i)%N_o = pd(list(i))%N_o
data%phytos(i)%K_N = pd(list(i))%K_N
data%phytos(i)%X_ncon = pd(list(i))%X_ncon
data%phytos(i)%X_nmin = pd(list(i))%X_nmin
data%phytos(i)%X_nmax = pd(list(i))%X_nmax
data%phytos(i)%R_nuptake = pd(list(i))%R_nuptake/secs_per_day
data%phytos(i)%k_nfix = pd(list(i))%k_nfix
data%phytos(i)%R_nfix = pd(list(i))%R_nfix/secs_per_day
data%phytos(i)%simDIPUptake = pd(list(i))%simDIPUptake
data%phytos(i)%simIPDynamics= pd(list(i))%simIPDynamics
data%phytos(i)%P_0 = pd(list(i))%P_0
data%phytos(i)%K_P = pd(list(i))%K_P
data%phytos(i)%X_pcon = pd(list(i))%X_pcon
data%phytos(i)%X_pmin = pd(list(i))%X_pmin
data%phytos(i)%X_pmax = pd(list(i))%X_pmax
data%phytos(i)%R_puptake = pd(list(i))%R_puptake/secs_per_day
data%phytos(i)%simSiUptake = pd(list(i))%simSiUptake
data%phytos(i)%Si_0 = pd(list(i))%Si_0
data%phytos(i)%K_Si = pd(list(i))%K_Si
data%phytos(i)%X_sicon = pd(list(i))%X_sicon
data%phytos(i)%c1 = 0.0124/60. ! From Chung et al (2014)
data%phytos(i)%c3 = 0.0230/60. ! "
data%phytos(i)%f1 = 0.675 ! Ross and Sharples (2007)
data%phytos(i)%f2 = 0.750 ! "
data%phytos(i)%d_phy = 1e-5
! Register group as a state variable
data%id_p(i) = aed2_define_variable( &
TRIM(data%phytos(i)%p_name), &
'mmol/m**3', &
'phytoplankton '//TRIM(data%phytos(i)%p_name), &
pd(list(i))%p_initial, &
minimum=pd(list(i))%p0, &
maximum=1e4, &
mobility = data%phytos(i)%w_p)
! Register rho (density) group as a state variable, if required
IF (data%phytos(i)%settling == _MOB_STOKES_) THEN
data%id_rho(i) = aed2_define_variable( &
TRIM(data%phytos(i)%p_name)//'_rho', &
'kg/m**3', &
'phytoplankton '//TRIM(data%phytos(i)%p_name)//'_rho', &
(data%min_rho+data%max_rho)/2., &
minimum=data%min_rho, &
maximum=data%max_rho, &
mobility = data%phytos(i)%w_p)
ENDIF
! Register internal nitrogen group as a state variable, if required
IF (data%phytos(i)%simINDynamics /= 0) THEN
IF(data%phytos(i)%simINDynamics == 1)THEN
minNut = data%phytos(i)%p0*data%phytos(i)%X_ncon
ELSE
minNut = data%phytos(i)%p0*data%phytos(i)%X_nmin
ENDIF
! Register IN group as a state variable
data%id_in(i) = aed2_define_variable( &
TRIM(data%phytos(i)%p_name)//'_IN', &
'mmol/m**3', &
'phytoplankton '//TRIM(data%phytos(i)%p_name)//'_IN', &
pd(list(i))%p_initial*data%phytos(i)%X_ncon, &
minimum=minNut, &
mobility = data%phytos(i)%w_p)
ELSE
IF (data%phytos(i)%settling == _MOB_MOTILE_) THEN
data%phytos(i)%settling = _MOB_CONST_
print *,'Motility can not be simulated as no IN'
ENDIF
ENDIF
! Register internal phosphorus group as a state variable, if required
IF (data%phytos(i)%simIPDynamics /= 0) THEN
IF(data%phytos(i)%simIPDynamics == 1)THEN
minNut = data%phytos(i)%p0*data%phytos(i)%X_pcon
ELSE
minNut = data%phytos(i)%p0*data%phytos(i)%X_pmin
ENDIF
! Register IP group as a state variable
data%id_ip(i) = aed2_define_variable( &
TRIM(data%phytos(i)%p_name)//'_IP', &
'mmol/m**3', &
'phytoplankton '//TRIM(data%phytos(i)%p_name)//'_IP', &
pd(list(i))%p_initial*data%phytos(i)%X_pcon, &
minimum=minNut, &
mobility = data%phytos(i)%w_p)
ENDIF
! Group specific diagnostic variables
data%id_NtoP(i) = aed2_define_diag_variable( TRIM(data%phytos(i)%p_name)//'_NtoP','-', 'internal n:p ratio')
IF (extra_diag) THEN
data%id_fI(i) = aed2_define_diag_variable( TRIM(data%phytos(i)%p_name)//'_fI', '-', 'fI (0-1)')
data%id_fNit(i) = aed2_define_diag_variable( TRIM(data%phytos(i)%p_name)//'_fNit', '-', 'fNit (0-1)')
data%id_fPho(i) = aed2_define_diag_variable( TRIM(data%phytos(i)%p_name)//'_fPho', '-', 'fPho (0-1)')
data%id_fSil(i) = aed2_define_diag_variable( TRIM(data%phytos(i)%p_name)//'_fSil', '-', 'fSil (0-1)')
data%id_fT(i) = aed2_define_diag_variable( TRIM(data%phytos(i)%p_name)//'_fT', '-', 'fT (>0)')
data%id_fSal(i) = aed2_define_diag_variable( TRIM(data%phytos(i)%p_name)//'_fSal', '-', 'fSal (>1)')
! Register vertical velocity diagnostic, where relevant
IF (data%phytos(i)%settling == _MOB_STOKES_ .OR. &
data%phytos(i)%settling == _MOB_MOTILE_) THEN
data%id_vvel(i) = aed2_define_diag_variable( TRIM(data%phytos(i)%p_name)//'_vvel', 'm/day', 'vertical velocity')
ENDIF
ENDIF
ENDDO
END SUBROUTINE aed2_phytoplankton_load_params
!+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
!###############################################################################
SUBROUTINE aed2_define_phytoplankton(data, namlst)
!-------------------------------------------------------------------------------
! Initialise the phytoplankton biogeochemical model
!
! Here, the aed2_p_m namelist is read and te variables exported
! by the model are registered with AED2.
!-------------------------------------------------------------------------------
!ARGUMENTS
CLASS (aed2_phytoplankton_data_t),INTENT(inout) :: data
INTEGER,INTENT(in) :: namlst
!
!LOCALS
INTEGER :: status,i
! %% NAMELIST
! Default settings
INTEGER :: num_phytos = 0
INTEGER :: the_phytos(MAX_PHYTO_TYPES) = 0
INTEGER :: settling(MAX_PHYTO_TYPES) = _MOB_CONST_
AED_REAL :: resuspension(MAX_PHYTO_TYPES) = 0.
CHARACTER(len=64) :: resus_link='NCS_resus'
CHARACTER(len=64) :: p_excretion_target_variable='OGM_dop'
CHARACTER(len=64) :: p_mortality_target_variable='OGM_pop'
CHARACTER(len=64) :: p1_uptake_target_variable='PHS_frp'
CHARACTER(len=64) :: p2_uptake_target_variable=''
CHARACTER(len=64) :: n_excretion_target_variable='OGM_don'
CHARACTER(len=64) :: n_mortality_target_variable='OGM_pon'
CHARACTER(len=64) :: n1_uptake_target_variable='NIT_nit'
CHARACTER(len=64) :: n2_uptake_target_variable='NIT_amm'
CHARACTER(len=64) :: n3_uptake_target_variable=''
CHARACTER(len=64) :: n4_uptake_target_variable=''
CHARACTER(len=64) :: c_excretion_target_variable='OGM_doc'
CHARACTER(len=64) :: c_mortality_target_variable='OGM_poc'
CHARACTER(len=64) :: c_uptake_target_variable=''
CHARACTER(len=64) :: do_uptake_target_variable='OXY_oxy'
CHARACTER(len=64) :: si_excretion_target_variable=''
CHARACTER(len=64) :: si_mortality_target_variable=''
CHARACTER(len=64) :: si_uptake_target_variable=''
CHARACTER(len=128) :: dbase='aed2_phyto_pars.nml'
AED_REAL :: zerolimitfudgefactor = 0.9 * 3600
AED_REAL :: R_mpbg = 0.
AED_REAL :: R_mpbr = 0.
AED_REAL :: I_Kmpb = 100.
AED_REAL :: mpb_max = 1000.
AED_REAL :: theta_mpb_growth = 1.05
AED_REAL :: theta_mpb_resp = 1.05
AED_REAL :: min_rho = 900.
AED_REAL :: max_rho = 1200.
LOGICAL :: extra_debug = .false.
INTEGER :: do_mpb = 0
INTEGER :: n_zones = 0
AED_REAL :: active_zones(1000) = 0
! %% END NAMELIST
NAMELIST /aed2_phytoplankton/ num_phytos, the_phytos, settling,resuspension,&
p_excretion_target_variable,p_mortality_target_variable, &
p1_uptake_target_variable, p2_uptake_target_variable, &
n_excretion_target_variable,n_mortality_target_variable, &
n1_uptake_target_variable,n2_uptake_target_variable, &
n3_uptake_target_variable,n4_uptake_target_variable, &
c_excretion_target_variable,c_mortality_target_variable, &
c_uptake_target_variable, do_uptake_target_variable, &
si_excretion_target_variable,si_mortality_target_variable, &
si_uptake_target_variable, &
dbase, zerolimitfudgefactor, extra_debug, extra_diag, &
do_mpb, R_mpbg, R_mpbr, I_Kmpb, mpb_max, min_rho, max_rho, &
resus_link, n_zones, active_zones, diag_level, &
theta_mpb_growth,theta_mpb_resp
!-----------------------------------------------------------------------
!BEGIN
print *," aed2_phytoplankton initialization"
! Read the namelist, and set module parameters
read(namlst,nml=aed2_phytoplankton,iostat=status)
IF (status /= 0) STOP 'Error reading namelist aed2_phytoplankton'
dtlim = zerolimitfudgefactor
IF( extra_debug ) extra_diag = .true. ! legacy use of extra_debug
! Set module parameters
data%min_rho = min_rho ; data%max_rho = max_rho
data%do_mpb = do_mpb
data%R_mpbg = R_mpbg/secs_per_day ; data%R_mpbr = R_mpbr/secs_per_day
data%I_Kmpb = I_Kmpb ; data%mpb_max = mpb_max
data%theta_mpb_growth = theta_mpb_growth ; data%theta_mpb_resp = theta_mpb_resp
ALLOCATE(data%resuspension(num_phytos)); data%resuspension = resuspension(1:num_phytos)
data%n_zones = n_zones
IF( n_zones>0 ) THEN
ALLOCATE(data%active_zones(n_zones))
DO i=1,n_zones
data%active_zones(i) = active_zones(i)
ENDDO
ENDIF
! Store species parameter values in our own derived type
! NB: all rates must be provided in values per day,
! and are converted in here to values per second.
CALL aed2_phytoplankton_load_params(data,dbase,num_phytos,the_phytos,settling,resuspension)
CALL aed2_bio_temp_function(data%num_phytos, &
data%phytos%theta_growth, &
data%phytos%T_std, &
data%phytos%T_opt, &
data%phytos%T_max, &
data%phytos%aTn, &
data%phytos%bTn, &
data%phytos%kTn, &
data%phytos%p_name)
! Register microphytbenthos as a state variable
IF (data%do_mpb>0) THEN
data%id_mpb = aed2_define_sheet_variable( 'mpb', &
'mmol/m**2', &
'microphytobenthos biomass', &
0.001, &
minimum=0.001)
ENDIF
! Register link to nutrient pools, if variable names are provided in namelist.
data%do_Pexc = p_excretion_target_variable .NE. ''
IF (data%do_Pexc) THEN
data%id_Pexctarget = aed2_locate_variable(p_excretion_target_variable)
ENDIF
data%do_Nexc = n_excretion_target_variable .NE. ''
IF (data%do_Pexc) THEN
data%id_Nexctarget = aed2_locate_variable(n_excretion_target_variable)
ENDIF
data%do_Cexc = c_excretion_target_variable .NE. ''
IF (data%do_Pexc) THEN
data%id_Cexctarget = aed2_locate_variable(c_excretion_target_variable)
ENDIF
data%do_Siexc = si_excretion_target_variable .NE. ''
IF (data%do_Siexc) THEN
data%id_Siexctarget = aed2_locate_variable(si_excretion_target_variable)
ENDIF
data%do_Pmort = p_mortality_target_variable .NE. ''
IF (data%do_Pmort) THEN
data%id_Pmorttarget = aed2_locate_variable(p_mortality_target_variable)
ENDIF
data%do_Nmort = n_mortality_target_variable .NE. ''
IF (data%do_Nmort) THEN
data%id_Nmorttarget = aed2_locate_variable(n_mortality_target_variable)
ENDIF
data%do_Cmort = c_mortality_target_variable .NE. ''
IF (data%do_Cmort) THEN
data%id_Cmorttarget = aed2_locate_variable(c_mortality_target_variable)
ENDIF
data%do_Simort = si_mortality_target_variable .NE. ''
IF (data%do_Simort) THEN
data%id_Simorttarget = aed2_locate_variable(si_mortality_target_variable)
ENDIF
data%npup = 0
IF (p1_uptake_target_variable .NE. '') data%npup = 1
IF (p2_uptake_target_variable .NE. '') data%npup = 2
data%do_Puptake = .FALSE.
IF (data%npup>0) data%do_Puptake=.TRUE.
IF (data%do_Puptake) THEN
IF (data%npup>0) THEN ; data%id_Pupttarget(1) = aed2_locate_variable(p1_uptake_target_variable) ; ENDIF
IF (data%npup>1) THEN ; data%id_Pupttarget(2) = aed2_locate_variable(p2_uptake_target_variable) ; ENDIF
ENDIF
data%nnup = 0
IF (n1_uptake_target_variable .NE. '') data%nnup = 1
IF (n2_uptake_target_variable .NE. '') data%nnup = 2
IF (n3_uptake_target_variable .NE. '') data%nnup = 3
IF (n4_uptake_target_variable .NE. '') data%nnup = 4
data%do_Nuptake = .false.
IF (data%nnup>0) data%do_Nuptake=.true.
IF (data%do_Nuptake) THEN
IF (data%nnup>0) THEN ; data%id_Nupttarget(1) = aed2_locate_variable( n1_uptake_target_variable) ; ENDIF
IF (data%nnup>1) THEN ; data%id_Nupttarget(2) = aed2_locate_variable( n2_uptake_target_variable) ; ENDIF
IF (data%nnup>2) THEN ; data%id_Nupttarget(3) = aed2_locate_variable( n3_uptake_target_variable) ; ENDIF
IF (data%nnup>3) THEN ; data%id_Nupttarget(4) = aed2_locate_variable( n4_uptake_target_variable) ; ENDIF
ENDIF
data%do_Cuptake = c_uptake_target_variable .NE. ''
IF (data%do_Cuptake) THEN
data%id_Cupttarget = aed2_locate_variable( c_uptake_target_variable)
ENDIF
data%do_DOuptake = do_uptake_target_variable .NE. ''
IF (data%do_DOuptake) THEN
data%id_DOupttarget = aed2_locate_variable( do_uptake_target_variable)
ENDIF
data%do_Siuptake = si_uptake_target_variable .NE. ''
IF (data%do_Siuptake) THEN
data%id_Siupttarget = aed2_locate_variable( si_uptake_target_variable)
ENDIF
!-- sedimentation link variable
data%id_Psed_phy = aed2_define_diag_variable('Psed_phy','mmol/m**2/s','PHY sedimentation')
!-- resuspension link variable
IF ( .NOT.resus_link .EQ. '' ) THEN
data%id_l_resus = aed2_locate_global_sheet(TRIM(resus_link))
ELSE
data%id_l_resus = 0
data%resuspension = 0.
ENDIF
! Register diagnostic variables
data%id_GPP = aed2_define_diag_variable('GPP','mmol/m**3/d', 'gross primary production')
data%id_NCP = aed2_define_diag_variable('NCP','mmol/m**3/d', 'net community production')
IF (extra_diag) data%id_PPR = aed2_define_diag_variable('PPR','-','phytoplankton p/r ratio (gross)')
IF (extra_diag) data%id_NPR = aed2_define_diag_variable('NPR','-','phytoplankton p/r ratio (net)')
data%id_NUP = aed2_define_diag_variable('NUP_no3','mmol/m**3/d','nitrogen (NO3) uptake')
data%id_NUP2= aed2_define_diag_variable('NUP_nh4','mmol/m**3/d','nitrogen (NH4) uptake')
data%id_PUP = aed2_define_diag_variable('PUP','mmol/m**3/d','phosphorous uptake')
data%id_CUP = aed2_define_diag_variable('CUP','mmol/m**3/d','carbon uptake')
IF (extra_diag) data%id_dPAR = aed2_define_diag_variable('PAR','W/m**2', 'photosynthetically active radiation')
data%id_TCHLA = aed2_define_diag_variable('TCHLA','ug/L', 'total chlorophyll-a')
IF (extra_diag) data%id_TPHY = aed2_define_diag_variable('TPHYS','mmol/m**3', 'total phytoplankton')
data%id_TIN = aed2_define_diag_variable('IN','mmol/m**3', 'total phy nitrogen')
data%id_TIP = aed2_define_diag_variable('IP','mmol/m**3', 'total phy phosphorus')
IF(do_mpb>0) THEN
data%id_d_MPB = aed2_define_sheet_diag_variable('MPB','mmol/m**2', 'microphytobenthos density')
data%id_d_BPP = aed2_define_sheet_diag_variable('BPP','mmol/m**2/d', 'benthic gross productivity')
data%id_d_BCP = aed2_define_sheet_diag_variable('BCP','mmol/m**2/d', 'benthic net productivity')
data%id_d_mpbv= aed2_define_sheet_diag_variable('MPBV','mmol/m**2/d', 'mpb vertical exchange')
ENDIF
! Register environmental dependencies
data%id_tem = aed2_locate_global('temperature')
data%id_sal = aed2_locate_global('salinity')
data%id_par = aed2_locate_global('par')
data%id_I_0 = aed2_locate_global_sheet('par_sf')
data%id_dz = aed2_locate_global('layer_ht')
data%id_extc = aed2_locate_global('extc_coef')
data%id_dens = aed2_locate_global('density')
data%id_sedzone = aed2_locate_global_sheet('sed_zone')
END SUBROUTINE aed2_define_phytoplankton
!+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
!###############################################################################
SUBROUTINE aed2_calculate_phytoplankton(data,column,layer_idx)
!------------------------------------------------------------------------------+
! Right hand sides of phytoplankton biogeochemical model
!------------------------------------------------------------------------------+
!ARGUMENTS
CLASS (aed2_phytoplankton_data_t),INTENT(in) :: data
TYPE (aed2_column_t),INTENT(inout) :: column(:)
INTEGER,INTENT(in) :: layer_idx
!
!LOCALS
AED_REAL :: phy, tphy, tin, tip, tchla
AED_REAL :: INi, IPi
AED_REAL :: pup
AED_REAL :: no3up,nh4up
AED_REAL :: cup, rsiup
AED_REAL :: temp, par, Io, salinity, extc, dz
AED_REAL :: primprod(data%num_phytos), exudation(data%num_phytos), &
a_nfix(data%num_phytos), respiration(data%num_phytos)
AED_REAL :: cuptake(data%num_phytos), cexcretion(data%num_phytos), cmortality(data%num_phytos)
AED_REAL :: nuptake(data%num_phytos,1:4), nexcretion(data%num_phytos), nmortality(data%num_phytos)
AED_REAL :: puptake(data%num_phytos,1:2), pexcretion(data%num_phytos), pmortality(data%num_phytos)
AED_REAL :: siuptake(data%num_phytos), siexcretion(data%num_phytos), simortality(data%num_phytos)
AED_REAL :: fT, fNit, fPho, fSil, fI, fXl, fSal, PNf
AED_REAL :: upTot,net_cuptake
INTEGER :: phy_i,c
AED_REAL :: flux, available
!------------------------------------------------------------------------------+
!BEGIN
! Retrieve current environmental conditions.
temp = _STATE_VAR_(data%id_tem) ! local temperature
salinity = _STATE_VAR_(data%id_sal) ! local salinity
par = _STATE_VAR_(data%id_par) ! local photosynth. active radiation
Io = _STATE_VAR_S_(data%id_I_0) ! surface short wave radiation
! Retrieve current (local) state variable values.
pup = 0.
IF (data%do_Puptake) pup = _STATE_VAR_(data%id_Pupttarget(1))
no3up = 0.
nh4up = 0.
IF (data%do_Nuptake) THEN
no3up = _STATE_VAR_(data%id_Nupttarget(1))
nh4up = _STATE_VAR_(data%id_Nupttarget(2))
ENDIF
cup = 0.
IF (data%do_Cuptake) cup = _STATE_VAR_(data%id_Cupttarget)
rsiup = 0.
IF (data%do_Siuptake) rsiup = _STATE_VAR_(data%id_Siupttarget)
tphy = 0.0
tchla = 0.0
tin = 0.0
tip = 0.0
INi = 0.
IPi = 0.
!---------------------------------------------------------------------------+
DO phy_i=1,data%num_phytos
primprod(phy_i) = zero_
exudation(phy_i) = zero_
a_nfix(phy_i) = zero_
respiration(phy_i) = zero_
cuptake(phy_i) = zero_
cexcretion(phy_i) = zero_
cmortality(phy_i) = zero_
nuptake(phy_i,:) = zero_
nexcretion(phy_i) = zero_
nmortality(phy_i) = zero_
puptake(phy_i,:) = zero_
pexcretion(phy_i) = zero_
pmortality(phy_i) = zero_
! Retrieve this phytoplankton group
phy = _STATE_VAR_(data%id_p(phy_i))
!------------------------------------------------------------------------+
! Get the temperature limitation function
fT = fTemp_function(data%phytos(phy_i)%fT_Method, &
data%phytos(phy_i)%T_max, &
data%phytos(phy_i)%T_std, &
data%phytos(phy_i)%theta_growth, &
data%phytos(phy_i)%aTn, &
data%phytos(phy_i)%bTn, &
data%phytos(phy_i)%kTn,temp)
!------------------------------------------------------------------------+
! Get the light and nutrient limitation.
! NITROGEN.
fNit = 0.0
IF(data%phytos(phy_i)%simINDynamics /= 0) THEN
! IN variable available
INi = _STATE_VAR_(data%id_in(phy_i))
ELSE
! Assumed constant IN:
INi = phy*data%phytos(phy_i)%X_ncon
END IF
! Estimate fN limitation from IN or ext N value
IF(data%phytos(phy_i)%simINDynamics > 1) THEN
IF (phy > data%phytos(phy_i)%p0) THEN
fNit = INi / phy
fNit = phyto_fN(data%phytos,phy_i,IN=fNit)
ENDIF
IF (phy > zero_ .AND. phy <= data%phytos(phy_i)%p0) THEN
fNit = phyto_fN(data%phytos,phy_i,din=no3up+nh4up)
ENDIF
ELSE
fNit = phyto_fN(data%phytos,phy_i,din=no3up+nh4up)
ENDIF
IF (data%phytos(phy_i)%simNFixation /= 0) THEN
! Nitrogen fixer: apply no N limitation. N Fixation ability
! depends on DIN concentration
a_nfix = (one_ - fNit)
fNit = one_
ENDIF
! PHOSPHOROUS.
fPho = zero_
IF (data%phytos(phy_i)%simIPDynamics /= 0) THEN
! IP variable available
IPi = _STATE_VAR_(data%id_ip(phy_i))
ELSE
! Assumed constant IP:
IPi = phy*data%phytos(phy_i)%X_pcon
END IF
! Estimate fP limitation from IP or ext P value
IF (data%phytos(phy_i)%simIPDynamics > 1) THEN
IF (phy > data%phytos(phy_i)%p0) THEN
fPho = IPi / phy
fPho = phyto_fP(data%phytos,phy_i,IP=fPho)
ENDIF
IF (phy > zero_ .AND. phy <= data%phytos(phy_i)%p0) THEN
fPho = phyto_fP(data%phytos,phy_i,frp=pup)
ENDIF
ELSE
fPho = phyto_fP(data%phytos,phy_i,frp=pup)
ENDIF
! SILICA.
fSil = phyto_fSi(data%phytos,phy_i,rsiup)
! LIGHT
extc = _STATE_VAR_(data%id_extc)
! dz = 0.5 !MH: to fix
dz = _STATE_VAR_(data%id_dz)
fI = photosynthesis_irradiance(data%phytos(phy_i)%lightModel, &
data%phytos(phy_i)%I_K, data%phytos(phy_i)%I_S, par, extc, Io, dz)
! fI = 0.1
! METAL AND TOXIC EFFECTS
fXl = 1.0
!------------------------------------------------------------------------+
! Primary production rate
primprod(phy_i) = data%phytos(phy_i)%R_growth * fT * findMin(fI,fNit,fPho,fSil) * fxl
! Adjust primary production rate for nitrogen fixers
IF (data%phytos(phy_i)%simNFixation /= 0) THEN
! Nitrogen fixing species, and the growth rate to must be reduced
! to compensate for the increased metabolic cost of this process
primprod(phy_i) = primprod(phy_i) * (data%phytos(phy_i)%k_nfix + &
(1.0-a_nfix(phy_i))*(1.0-data%phytos(phy_i)%k_nfix))
ENDIF
!------------------------------------------------------------------------+
! Respiration and general metabolic loss
respiration(phy_i) = bio_respiration(data%phytos(phy_i)%R_resp,data%phytos(phy_i)%theta_resp,temp)
! Salinity stress effect on respiration (or growth)
fSal = phyto_salinity(data%phytos,phy_i,salinity)
IF( data%phytos(phy_i)%salTol >= 4) THEN
primprod(phy_i) = primprod(phy_i) * fSal ! growth limtation rather than mortality enhancement
ELSE
respiration(phy_i) = respiration(phy_i) * fSal
ENDIF
! photo-exudation
exudation(phy_i) = primprod(phy_i)*data%phytos(phy_i)%f_pr
! Limit respiration if at the min biomass to prevent
! leak in the C mass balance
IF (phy <= data%phytos(phy_i)%p0) THEN
respiration(phy_i) = zero_
exudation(phy_i) = zero_
ENDIF
! write(*,"(4X,'limitations (fT,fI,fN,fP,fSi,Io, par, mu): ',9F9.2)")fT,fI,fNit,fPho,fSil,Io,par,primprod*secs_per_day
!------------------------------------------------------------------------+
! Carbon uptake and excretion
cuptake(phy_i) = -primprod(phy_i) * phy
cexcretion(phy_i) = (data%phytos(phy_i)%k_fdom*(1.0-data%phytos(phy_i)%k_fres)*respiration(phy_i)+exudation(phy_i)) * phy
cmortality(phy_i) = ((1.0-data%phytos(phy_i)%k_fdom)*(1.0-data%phytos(phy_i)%k_fres)*respiration(phy_i)) * phy
! Nitrogen uptake and excretion
CALL phyto_internal_nitrogen(data%phytos,phy_i,data%do_N2uptake,phy,INi,primprod(phy_i),&
fT,no3up,nh4up,a_nfix(phy_i),respiration(phy_i),exudation(phy_i),PNf,&
nuptake(phy_i,:),nexcretion(phy_i),nmortality(phy_i))
! Phosphorus uptake and excretion
CALL phyto_internal_phosphorus(data%phytos,phy_i,data%npup,phy,IPi,primprod(phy_i),&
fT,pup,respiration(phy_i),exudation(phy_i),&
puptake(phy_i,:),pexcretion(phy_i),pmortality(phy_i))
! Silica uptake and excretion
IF (data%phytos(phy_i)%simSiUptake > 0) THEN
siuptake(phy_i) =-data%phytos(phy_i)%X_sicon * primprod(phy_i) * phy
siexcretion(phy_i) = data%phytos(phy_i)%X_sicon * (data%phytos(phy_i)%k_fdom*respiration(phy_i)+exudation(phy_i)) * phy
simortality(phy_i) = data%phytos(phy_i)%X_sicon * ((1.0-data%phytos(phy_i)%k_fdom)*respiration(phy_i)) * phy
ELSE
siuptake(phy_i) = zero_
siexcretion(phy_i) = zero_
simortality(phy_i) = zero_
ENDIF
!------------------------------------------------------------------------+
! Diagnostic info
_DIAG_VAR_(data%id_NtoP(phy_i)) = INi/IPi
IF (extra_diag) THEN
_DIAG_VAR_(data%id_fT(phy_i)) = fT
_DIAG_VAR_(data%id_fI(phy_i)) = fI
_DIAG_VAR_(data%id_fNit(phy_i)) = fNit
_DIAG_VAR_(data%id_fPho(phy_i)) = fPho
_DIAG_VAR_(data%id_fSil(phy_i)) = fSil
_DIAG_VAR_(data%id_fSal(phy_i)) = fSal
ENDIF
END DO
!---------------------------------------------------------------------------+
! Check uptake values for availability to prevent -ve numbers
! pup - p available
! no3up - no3 available
! nh4up - nh4 available
! cup - c available
! rsiup - Si available
IF (data%do_Puptake) THEN
upTot = sum(puptake(:,1))*dtlim
IF ( abs(upTot) > 1.e-10 .and. upTot >= pup ) THEN
DO phy_i=1,data%num_phytos
puptake(phy_i,1) = (pup*0.99/dtlim) * (puptake(phy_i,1)/upTot)
ENDDO
ENDIF
ENDIF
IF (data%do_Nuptake) THEN
upTot = sum(nuptake(:,1))*dtlim
IF ( abs(upTot) > 1.e-10 .and. upTot >= no3up ) THEN
DO phy_i=1,data%num_phytos
nuptake(phy_i,1) = (no3up*0.99/dtlim) * (nuptake(phy_i,1)/upTot)
ENDDO
ENDIF
upTot = sum(nuptake(:,2))*dtlim
IF ( abs(upTot) > 1.e-10 .and. upTot >= nh4up ) THEN
DO phy_i=1,data%num_phytos
nuptake(phy_i,2) = (nh4up*0.99/dtlim) * (nuptake(phy_i,2)/upTot)
ENDDO
ENDIF
ENDIF
IF (data%do_Cuptake) THEN
upTot = sum(cuptake)*dtlim
IF ( abs(upTot) > 1.e-10 .and. upTot >= cup ) THEN
DO phy_i=1,data%num_phytos
cuptake(phy_i) = (cup*0.99/dtlim) * (cuptake(phy_i)/upTot)
ENDDO
ENDIF
ENDIF
! IF (data%do_DOuptake) THEN
! !
! ENDIF
IF (data%do_Siuptake) THEN
upTot = sum(siuptake)*dtlim
IF ( abs(upTot) > 1.e-10 .and. upTot >= rsiup ) THEN
DO phy_i=1,data%num_phytos
siuptake(phy_i) = (rsiup*0.99/dtlim) * (siuptake(phy_i)/upTot)
ENDDO
ENDIF
ENDIF
!---------------------------------------------------------------------------+
! SET TEMPORAL DERIVATIVES FOR ODE SOLVER
net_cuptake = zero_
DO phy_i=1,data%num_phytos
!------------------------------------------------------------------------+
!# PHYTOPLANKTON PRODUCTION & RESPIRATION
phy = _STATE_VAR_(data%id_p(phy_i))
flux = (primprod(phy_i) - respiration(phy_i) - exudation(phy_i)) * phy
available = MAX(zero_, phy - data%phytos(phy_i)%p0)
IF ( -flux*dtlim > available ) flux = -0.99*available/dtlim
_FLUX_VAR_(data%id_p(phy_i)) = _FLUX_VAR_(data%id_p(phy_i)) + (flux)
!------------------------------------------------------------------------+
!# PHYTOPLANKTON INTERNAL NITROGEN
IF (data%phytos(phy_i)%simINDynamics /= 0) THEN
! _FLUX_VAR_(data%id_in(phy_i)) = _FLUX_VAR_(data%id_in(phy_i)) + &
! ( (-sum(nuptake) - nexcretion(phy_i) - nmortality(phy_i) )*INi )
INi = _STATE_VAR_(data%id_in(phy_i))
flux = (-sum(nuptake(phy_i,:)) - nexcretion(phy_i) - nmortality(phy_i))
available = MAX(zero_, INi - data%phytos(phy_i)%X_nmin*phy)
IF ( -flux*dtlim > available ) flux = -0.99*available/dtlim
_FLUX_VAR_(data%id_in(phy_i)) = _FLUX_VAR_(data%id_in(phy_i)) + (flux)
ENDIF
!------------------------------------------------------------------------+
!# PHYTOPLANKTON INTERNAL PHOSPHORUS
IF (data%phytos(phy_i)%simIPDynamics /= 0) THEN
! _FLUX_VAR_(data%id_ip(phy_i)) = _FLUX_VAR_(data%id_ip(phy_i)) + &
! ( (-sum(puptake(phy_i,:)) - pexcretion(phy_i) - pmortality(phy_i)) )
IPi = _STATE_VAR_(data%id_ip(phy_i))
flux = (-sum(puptake(phy_i,:)) - pexcretion(phy_i) - pmortality(phy_i))
available = MAX(zero_, IPi - data%phytos(phy_i)%X_pmin*phy)
IF ( -flux*dtlim > available ) flux = -0.99*available/dtlim
_FLUX_VAR_(data%id_ip(phy_i)) = _FLUX_VAR_(data%id_ip(phy_i)) + (flux)
ENDIF
!------------------------------------------------------------------------+
!# PHYTOPLANKTON CELL DENSITY
IF ( data%id_rho(phy_i)>0 ) THEN
! density increases during carbohydrate creation (daytime)
flux = zero_
IF( par>zero_ ) THEN
flux = data%phytos(phy_i)%c1 * &
(one_ - EXP(-par/data%phytos(phy_i)%I_K) ) - data%phytos(phy_i)%c3
ELSE
! darkness
flux = -data%phytos(phy_i)%c3
ENDIF
_FLUX_VAR_(data%id_rho(phy_i)) = _FLUX_VAR_(data%id_rho(phy_i)) + flux
! check maximum/minimum density are not exceeded
IF( _STATE_VAR_(data%id_rho(phy_i))>data%max_rho ) THEN
_FLUX_VAR_(data%id_rho(phy_i)) =zero_
_STATE_VAR_(data%id_rho(phy_i))=data%max_rho
ENDIF
IF( _STATE_VAR_(data%id_rho(phy_i))<data%min_rho ) THEN