-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathforwardmodel.py
More file actions
2796 lines (2558 loc) · 160 KB
/
forwardmodel.py
File metadata and controls
2796 lines (2558 loc) · 160 KB
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
# -*- coding: utf-8 -*-
# This file is the slightly adapted CLASS model used in ICLASS (Comment by Peter Bosman)
#
# CLASS
# Copyright (c) 2010-2015 Meteorology and Air Quality section, Wageningen University and Research centre
# Copyright (c) 2011-2015 Jordi Vila-Guerau de Arellano
# Copyright (c) 2011-2015 Chiel van Heerwaarden
# Copyright (c) 2011-2015 Bart van Stratum
# Copyright (c) 2011-2015 Kees van den Dries
#
# This file is part of CLASS
#
# CLASS 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.
#
# CLASS 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 CLASS. If not, see <http://www.gnu.org/licenses/>.
#
import copy as cp
import numpy as np
import sys
import os.path
if os.path.isfile('soilCOSmodel.py'): #with if statement, so you do not need the file soilCOSmodel.py if it is not used
import soilCOSmodel as sCOSm
if os.path.isfile('canopy_model.py'):
import canopy_model as canm
if os.path.isfile('pho_sib4.py'):
import pho_sib4 as sib4
#import ribtol
def esat(T):
return 0.611e3 * np.exp(17.2694 * (T - 273.16) / (T - 35.86))
def qsat(T,p):
return 0.622 * esat(T) / p
class model:
def __init__(self, model_input):
# initialize the different components of the model
self.input = cp.deepcopy(model_input)
self.nr_of_surf_lay_its = 10 #nr of surface layer iterations in init function
def run(self,checkpoint=False,updatevals_surf_lay=True,delete_at_end=True,save_vars_indict=False):
# initialize model variables
self.updatevals_surf_lay = updatevals_surf_lay #init needs it, it is a switch to update variables in the surface layer function (self.Cs and self.ustar) or not
self.save_vars_indict = False #save model variables in dictionary, only needed when doing gradient test in inverse modelling
if checkpoint: #checkpointing is saving values of variables (needed for the adjoint)
self.checkpoint = True
self.cpx_init = [] #a separate set just for init
for t in range((self.nr_of_surf_lay_its)):
self.cpx_init += [{}]
else:
self.checkpoint = False
self.init()
if checkpoint:
self.cpx = [] #I cannot do this before running init, since self.tsteps is calculated there
for t in range((self.tsteps)):
self.cpx += [{}]
if save_vars_indict:
self.save_vars_indict = True #best to do this after init, otherwise time is wasted in saving variables
# time integrate model
for self.t in range(self.tsteps):
# time integrate components
self.timestep()
# delete unnecessary variables from memory
if delete_at_end==True:
self.exitmodel()
def init(self):
# assign variables from input data
# initialize constants
self.Lv = 2.5e6 # heat of vaporization [J kg-1]
self.cp = 1005. # specific heat of dry air [J kg-1 K-1]
self.rho = 1.2 # density of air [kg m-3]
self.k = 0.4 # Von Karman constant [-]
self.g = 9.81 # gravity acceleration [m s-2]
self.Rd = 287. # gas constant for dry air [J kg-1 K-1]
self.Rv = 461.5 # gas constant for moist air [J kg-1 K-1]
self.bolz = 5.67e-8 # Bolzman constant [-]
self.rhow = 1000. # density of water [kg m-3]
self.S0 = 1368. # solar constant [W m-2]
# Read switches
self.sw_ml = self.input.sw_ml # mixed-layer model switch
self.sw_shearwe = self.input.sw_shearwe # shear growth ABL switch
self.sw_fixft = self.input.sw_fixft # Fix the free-troposphere switch
self.sw_wind = self.input.sw_wind # prognostic wind switch
self.sw_sl = self.input.sw_sl # surface layer switch
self.sw_rad = self.input.sw_rad # radiation switch
self.sw_ls = self.input.sw_ls # land surface switch
self.ls_type = self.input.ls_type # land surface paramaterization (js or ags)
if self.ls_type == 'canopy_model':
if not os.path.isfile('canopy_model.py'):
raise Exception('canopy_model selected, but cannot find canopy_model.py')
elif self.ls_type == 'sib4':
if not os.path.isfile('pho_sib4.py'):
raise Exception('sib4 ls_type selected, but cannot find pho_sib4.py')
self.sw_cu = self.input.sw_cu # cumulus parameterization switch
self.soilCOSmodel = None #an instance of the soilCOSmodel. Always define this var, even if no soilCOSmodel used, for use with the inverse modelling framework (gradient test)
self.soilCOSmodeltype = None
if hasattr(self.input,'soilCOSmodeltype'):
self.soilCOSmodeltype = self.input.soilCOSmodeltype # soil COS model switch
if self.soilCOSmodeltype == 'Sun_Ogee':
if not os.path.isfile('soilCOSmodel.py'):
raise Exception('Sun_Ogee model selected, but cannot find soilCOSmodel.py')
self.sw_dynamicsl_border = True
if hasattr(self.input,'sw_dynamicsl_border'):
self.sw_dynamicsl_border = self.input.sw_dynamicsl_border
self.sw_use_ribtol = True #use ribtol, the more complex way of surface layer calculations
if hasattr(self.input,'sw_use_ribtol'):
self.sw_use_ribtol = self.input.sw_use_ribtol
# self.use_rsl = False #use roughness sublayer
# if hasattr(self.input,'use_rsl'):
# self.use_rsl = self.input.use_rsl
self.sw_advfp = False #switch for prescribed advection to take place over full profile (also in Free troposphere), only in ML if False
if hasattr(self.input,'sw_advfp'):
self.sw_advfp = self.input.sw_advfp
self.sw_printwarnings = True #print or hide warnings
if hasattr(self.input,'sw_printwarnings'):
self.sw_printwarnings = self.input.sw_printwarnings
self.sw_useWilson = False
if hasattr(self.input,'sw_useWilson'):
self.sw_useWilson = self.input.sw_useWilson #switch to use Wilson or Businger Dyer for flux gradient relationships
self.sw_model_stable_con = True
if hasattr(self.input,'sw_model_stable_con'):
self.sw_model_stable_con = self.input.sw_model_stable_con #switch to use Businger Dyer or return nan for psih and psim for flux gradient relationships in stable conditions
# A-Gs constants and settings
# Plant type: -C3- -C4-
if hasattr(self.input,'CO2comp298'):
self.CO2comp298 = self.input.CO2comp298
else:
self.CO2comp298 = [68.5, 4.3 ] # CO2 compensation concentration [mg m-3]
if hasattr(self.input,'Q10CO2'):
self.Q10CO2 = self.input.Q10CO2
else:
self.Q10CO2 = [1.5, 1.5 ] # function parameter to calculate CO2 compensation concentration [-]
if hasattr(self.input,'gm298'):
self.gm298 = self.input.gm298
else:
self.gm298 = [7.0, 17.5 ] # mesophyill conductance at 298 K [mm s-1]
if hasattr(self.input,'Ammax298'):
self.Ammax298 = self.input.Ammax298
else:
self.Ammax298 = [2.2, 1.7 ] # CO2 maximal primary productivity [mg m-2 s-1]
if hasattr(self.input,'Q10gm'):
self.Q10gm = self.input.Q10gm
else:
self.Q10gm = [2.0, 2.0 ] # function parameter to calculate mesophyll conductance [-]
if hasattr(self.input,'T1gm'):
self.T1gm = self.input.T1gm
else:
self.T1gm = [278., 286. ] # reference temperature to calculate mesophyll conductance gm [K]
if hasattr(self.input,'T2gm'):
self.T2gm = self.input.T2gm
else:
self.T2gm = [301., 309. ] # reference temperature to calculate mesophyll conductance gm [K]
if hasattr(self.input,'Q10Am'):
self.Q10Am = self.input.Q10Am
else:
self.Q10Am = [2.0, 2.0 ] # function parameter to calculate maximal primary profuctivity Ammax
if hasattr(self.input,'T1Am'):
self.T1Am = self.input.T1Am
else:
self.T1Am = [281., 286. ] # reference temperature to calculate maximal primary profuctivity Ammax [K]
if hasattr(self.input,'T2Am'):
self.T2Am = self.input.T2Am
else:
self.T2Am = [311., 311. ] # reference temperature to calculate maximal primary profuctivity Ammax [K]
if hasattr(self.input,'f0'):
self.f0 = self.input.f0
else:
self.f0 = [0.89, 0.85 ] # maximum value Cfrac [-]
if hasattr(self.input,'ad'):
self.ad = self.input.ad
else:
self.ad = [0.07, 0.15 ] # regression coefficient to calculate Cfrac [kPa-1]
if hasattr(self.input,'alpha0'):
self.alpha0 = self.input.alpha0
else:
self.alpha0 = [0.017, 0.014 ] # initial low light conditions [mg J-1]
if hasattr(self.input,'Kx'):
self.Kx = self.input.Kx
else:
self.Kx = [0.7, 0.7 ] # extinction coefficient PAR [-]
if hasattr(self.input,'gmin'):
self.gmin = self.input.gmin
else:
self.gmin = [0.25e-3, 0.25e-3] # cuticular (minimum) conductance [mm s-1]
if self.sw_ls:
if self.ls_type == 'ags':
if hasattr(self.input,'PARfract'):
self.PARfract = self.input.PARfract
else:
self.PARfract = 0.5 #fraction of incoming shortwave radiation that is PAR (at the vegetation) [-]
self.mco2 = 44.; # molecular weight CO2 [g mol -1]
self.mcos = 12. + 16. + 32.07; # molecular weight COS [g mol -1]
self.mair = 28.9; # molecular weight air [g mol -1]
self.mh2o = 18 # molecular weight water [g mol -1]
self.nuco2q = 1.6; # ratio molecular viscosity water to carbon dioxide
self.Cw = 0.0016; # constant water stress correction (eq. 13 Jacobs et al. 2007) [-]
self.wmax = 0.55; # upper reference value soil water [-]
self.wmin = 0.005; # lower reference value soil water [-]
if hasattr(self.input,'R10'):
self.R10 = self.input.R10
else:
self.R10 = 0.23; # respiration at 10 C [mg CO2 m-2 s-1]
if hasattr(self.input,'E0'):
self.E0 = self.input.E0
else:
self.E0 = 53.3e3; # activation energy [53.3 kJ kmol-1]
# initialize mixed-layer
self.h = self.input.h # initial ABL height [m]
self.Ps = self.input.Ps # surface pressure [Pa]
self.divU = self.input.divU # horizontal large-scale divergence of wind [s-1]
self.ws = None # large-scale vertical velocity [m s-1]
self.wf = None # mixed-layer growth due to radiative divergence [m s-1]
self.fc = self.input.fc # coriolis parameter [s-1]
self.we = -1. # entrainment velocity [m s-1]
# Temperature
self.theta = self.input.theta # initial mixed-layer potential temperature [K]
self.deltatheta = self.input.deltatheta # initial temperature jump at h [K]
self.gammatheta = self.input.gammatheta # free atmosphere potential temperature lapse rate [K m-1]
if hasattr(self.input,'gammatheta2'):
self.gammatheta2 = self.input.gammatheta2
self.htrans = self.input.htrans #above this height [m], use gammatheta2, otherwise gammatheta
else:
self.gammatheta2 = self.gammatheta #if gammatheta2 not given, take equal to gammatheta
self.htrans = 1000000. #the value does not matter, we just need a value for it
self.advtheta = self.input.advtheta # advection of heat [K s-1]
self.beta = self.input.beta # entrainment ratio for virtual heat [-]
self.wtheta = self.input.wtheta # surface kinematic heat flux [K m s-1]
self.wthetae = None # entrainment kinematic heat flux [K m s-1]
self.wstar = 0. # convective velocity scale [m s-1]
# 2m diagnostic variables
self.T2m = None # 2m temperature [K]
self.q2m = None # 2m specific humidity [kg kg-1]
self.e2m = None # 2m vapor pressure [Pa]
self.esat2m = None # 2m saturated vapor pressure [Pa]
self.u2m = None # 2m u-wind [m s-1]
self.v2m = None # 2m v-wind [m s-1]
# Surface variables
self.thetasurf = self.input.theta # surface potential temperature [K]
self.thetavsurf = None # surface virtual potential temperature [K]
self.qsurf = None # surface specific humidity [g kg-1]
# Mixed-layer top variables
self.P_h = None # Mixed-layer top pressure [pa]
self.T_h = None # Mixed-layer top absolute temperature [K]
self.q2_h = None # Mixed-layer top specific humidity variance [kg2 kg-2]
self.CO22_h = None # Mixed-layer top CO2 variance [ppm2]
self.RH_h = None # Mixed-layer top relavtive humidity [-]
self.dz_h = None # Transition layer thickness [-]
self.lcl = None # Lifting condensation level [m]
# Virtual temperatures and fluxes
self.thetav = None # initial mixed-layer virtual potential temperature [K]
self.deltathetav= None # initial virtual temperature jump at h [K]
self.wthetav = None # surface kinematic virtual heat flux [K m s-1]
self.wthetave = None # entrainment kinematic virtual heat flux [K m s-1]
# Moisture
self.q = self.input.q # initial mixed-layer specific humidity [kg kg-1]
self.deltaq = self.input.deltaq # initial specific humidity jump at h [kg kg-1]
self.gammaq = self.input.gammaq # free atmosphere specific humidity lapse rate [kg kg-1 m-1]
self.advq = self.input.advq # advection of moisture [kg kg-1 s-1]
self.wq = self.input.wq # surface kinematic moisture flux [kg kg-1 m s-1]
self.wqe = None # entrainment moisture flux [kg kg-1 m s-1]
self.wqM = None # moisture cumulus mass flux [kg kg-1 m s-1]
self.qsatvar = None # mixed-layer saturated specific humidity [kg kg-1]
self.esatvar = None # mixed-layer saturated vapor pressure [Pa]
self.e = None # mixed-layer vapor pressure [Pa]
self.qsatsurf = None # surface saturated specific humidity [g kg-1]
self.dqsatdT = None # slope saturated specific humidity curve [g kg-1 K-1]
# Wind
self.u = self.input.u # initial mixed-layer u-wind speed [m s-1]
self.deltau = self.input.deltau # initial u-wind jump at h [m s-1]
self.gammau = self.input.gammau # free atmosphere u-wind speed lapse rate [s-1]
self.advu = self.input.advu # advection of u-wind [m s-2]
self.v = self.input.v # initial mixed-layer u-wind speed [m s-1]
self.deltav = self.input.deltav # initial u-wind jump at h [m s-1]
self.gammav = self.input.gammav # free atmosphere v-wind speed lapse rate [s-1]
self.advv = self.input.advv # advection of v-wind [m s-2]
# Tendencies
self.htend = None # tendency of CBL [m s-1]
self.thetatend = None # tendency of mixed-layer potential temperature [K s-1]
self.deltathetatend = None # tendency of potential temperature jump at h [K s-1]
self.qtend = None # tendency of mixed-layer specific humidity [kg kg-1 s-1]
self.deltaqtend = None # tendency of specific humidity jump at h [kg kg-1 s-1]
self.CO2tend = None # tendency of CO2 humidity [ppm]
self.COStend = None # tendency of COS [ppb]
self.deltaCO2tend = None # tendency of CO2 jump at h [ppm s-1]
self.deltaCOStend = None # tendency of COS jump at h [ppb s-1]
self.utend = None # tendency of u-wind [m s-1 s-1]
self.deltautend = None # tendency of u-wind jump at h [m s-1 s-1]
self.vtend = None # tendency of v-wind [m s-1 s-1]
self.deltavtend = None # tendency of v-wind jump at h [m s-1 s-1]
self.dztend = None # tendency of transition layer thickness [m s-1]
# initialize surface layer
self.ustar = self.input.ustar # surface friction velocity [m s-1]
self.uw = None # surface momentum flux in u-direction [m2 s-2]
self.vw = None # surface momentum flux in v-direction [m2 s-2]
self.z0m = self.input.z0m # roughness length for momentum [m]
self.z0h = self.input.z0h # roughness length for scalars [m]
if hasattr(self.input,'Cs'):
self.Cs = self.input.Cs # drag coefficient for scalars [-]
else:
self.Cs = 1e12 # drag coefficient for scalars [-]
#Cm is calculated before it used, no need to specify it here, the output only stores the variable if self.sw_sl is True (in the original CLASS from 2019, there was a statement self.Cm = 1e12 in the init function, and Cm was always stored).
self.L = None # Obukhov length [m]
self.Rib = None # bulk Richardson number [-]
self.ra = None # aerodynamic resistance [s m-1]
# initialize radiation
self.lat = self.input.lat # latitude [deg]
self.lon = self.input.lon # longitude [deg]
self.doy = self.input.doy # day of the year [-]
self.tstart = self.input.tstart # time of the day [-]
self.cc = self.input.cc # cloud cover fraction [-]
self.Swin = None # incoming short wave radiation [W m-2]
self.Swout = None # outgoing short wave radiation [W m-2]
self.Lwin = None # incoming long wave radiation [W m-2]
self.Lwout = None # outgoing long wave radiation [W m-2]
self.sinlea = None
self.Q = self.input.Q # net radiation [W m-2], this value is not used if sw_rad == True. But if sw_rad == False and sw_ls == True and ls_type = 'ags', the model will crash as Swin is None.
self.dFz = self.input.dFz # cloud top radiative divergence [W m-2]
# initialize land surface
self.wg = self.input.wg # volumetric water content top soil layer [m3 m-3]
self.w2 = self.input.w2 # volumetric water content deeper soil layer [m3 m-3]
self.Tsoil = self.input.Tsoil # temperature top soil layer [K]
self.T2 = self.input.T2 # temperature deeper soil layer [K]
self.a = self.input.a # Clapp and Hornberger retention curve parameter a [-]
self.b = self.input.b # Clapp and Hornberger retention curve parameter b [-]
self.p = self.input.p # Clapp and Hornberger retention curve parameter p [-]
self.CGsat = self.input.CGsat # saturated soil conductivity for heat
self.wsat = self.input.wsat # saturated volumetric water content ECMWF config [-]
self.wfc = self.input.wfc # volumetric water content field capacity [-]
self.wwilt = self.input.wwilt # volumetric water content wilting point [-]
self.C1sat = self.input.C1sat
self.C2ref = self.input.C2ref
self.c_beta = self.input.c_beta # Curvature plant water-stress factor (0..1) [-]
self.LAI = self.input.LAI # leaf area index [-]
self.gD = self.input.gD # correction factor transpiration for VPD [-]
self.rsmin = self.input.rsmin # minimum resistance transpiration [s m-1]
self.rssoilmin = self.input.rssoilmin # minimum resistance soil evaporation [s m-1]
self.alpha = self.input.alpha # surface albedo [-]
self.rs = 1.e6 # resistance transpiration [s m-1]
self.rssoil = 1.e6 # resistance soil [s m-1]
self.Ts = self.input.Ts # surface temperature [K]
self.cveg = self.input.cveg # vegetation fraction [-]
self.Wmax = self.input.Wmax # thickness of water layer on wet vegetation [m]
if self.ls_type != 'canopy_model':
self.Wl = self.input.Wl # equivalent water layer depth for wet vegetation [m]
self.cliq = None # wet fraction [-]
self.Lambda = self.input.Lambda # thermal diffusivity skin layer [-]
self.Tsoiltend = None # soil temperature tendency [K s-1]
self.wgtend = None # soil moisture tendency [m3 m-3 s-1]
self.Wltend = None # equivalent liquid water tendency [m s-1]
self.H = None # sensible heat flux [W m-2]
self.LE = None # evapotranspiration [W m-2]
self.LEliq = None # open water evaporation [W m-2]
self.LEveg = None # transpiration [W m-2]
self.LEsoil = None # soil evaporation [W m-2]
self.LEpot = None # potential evaporation [W m-2]
self.LEref = None # reference evaporation using rs = rsmin / LAI [W m-2]
self.G = None # ground heat flux [W m-2]
# initialize A-Gs surface scheme
self.c3c4 = self.input.c3c4 # plant type ('c3' or 'c4')
if hasattr(self.input,'ags_C_mode'): #only if using the normal a-gs, not if using the canopy model
self.ags_C_mode = self.input.ags_C_mode # which mixing ratios to use in ags, 'surf' or 'MXL'
if self.ags_C_mode == 'surf' and self.sw_sl == False:
raise Exception('When ags_C_mode set to \'surf\', turn on the surface layer')
else:
self.ags_C_mode = 'MXL'
# initialize cumulus parameterization
self.sw_cu = self.input.sw_cu # Cumulus parameterization switch
self.dz_h = self.input.dz_h # Transition layer thickness [m]
self.ac = 0. # Cloud core fraction [-]
self.M = 0. # Cloud core mass flux [m s-1]
self.wqM = 0. # Cloud core moisture flux [kg kg-1 m s-1]
# initialize time variables
self.tsteps = int(np.floor(self.input.runtime / self.input.dt))
self.dt = self.input.dt
self.t = 0
# CO2,COS and canopy
self.CO2 = self.input.CO2 # initial mixed-layer CO2 [ppm]
self.COS = self.input.COS # initial mixed-layer COS [ppb]
if hasattr(self.input,'sca_sto'):
self.sca_sto = self.input.sca_sto #dimensionless coefficient for multiplying stomatal conductance with
else:
self.sca_sto = 1.0
self.COSmeasuring_height = 10. #assume 10 if not given
if hasattr(self.input,'COSmeasuring_height'):
self.COSmeasuring_height = self.input.COSmeasuring_height # height COS mixing rat measurements [m]
if self.COSmeasuring_height < self.z0h:
raise Exception('measuring height below z0h')
self.COSmeasuring_height2 = 10.
if hasattr(self.input,'COSmeasuring_height2'):
self.COSmeasuring_height2 = self.input.COSmeasuring_height2 # height COS mixing rat measurements [m], in case of a second set of obs
if self.COSmeasuring_height2 < self.z0h:
raise Exception('measuring height below z0h')
self.COSmeasuring_height3 = 10.
if hasattr(self.input,'COSmeasuring_height3'):
self.COSmeasuring_height3 = self.input.COSmeasuring_height3 # height COS mixing rat measurements [m] , in case of a third set of obs
if self.COSmeasuring_height3 < self.z0h:
raise Exception('measuring height below z0h')
self.COSmeasuring_height4 = 10.
if hasattr(self.input,'COSmeasuring_height4'):
self.COSmeasuring_height4 = self.input.COSmeasuring_height4 # height COS mixing rat measurements [m] , in case of a fourth set of obs
if self.COSmeasuring_height4 < self.z0h:
raise Exception('measuring height below z0h')
self.CO2measuring_height = 10.
if hasattr(self.input,'CO2measuring_height'):
self.CO2measuring_height = self.input.CO2measuring_height # height CO2 mixing rat measurements [m]
if self.CO2measuring_height < self.z0h:
raise Exception('measuring height below z0h')
self.CO2measuring_height2 = 10.
if hasattr(self.input,'CO2measuring_height2'):
self.CO2measuring_height2 = self.input.CO2measuring_height2 # height CO2 mixing rat measurements [m] , in case of a second set of obs
if self.CO2measuring_height2 < self.z0h:
raise Exception('measuring height below z0h')
self.CO2measuring_height3 = 10.
if hasattr(self.input,'CO2measuring_height3'):
self.CO2measuring_height3 = self.input.CO2measuring_height3 # height CO2 mixing rat measurements [m], in case of a third set of obs
if self.CO2measuring_height3 < self.z0h:
raise Exception('measuring height below z0h')
self.CO2measuring_height4 = 10.
if hasattr(self.input,'CO2measuring_height4'):
self.CO2measuring_height4 = self.input.CO2measuring_height4 # height CO2 mixing rat measurements [m], in case of a fourth set of obs
if self.CO2measuring_height4 < self.z0h:
raise Exception('measuring height below z0h')
self.Tmeasuring_height = 10.
if hasattr(self.input,'Tmeasuring_height'):
self.Tmeasuring_height = self.input.Tmeasuring_height # height temperature measurements [m]
if self.Tmeasuring_height < self.z0h:
raise Exception('measuring height below z0h')
self.Tmeasuring_height2 = 10.
if hasattr(self.input,'Tmeasuring_height2'):
self.Tmeasuring_height2 = self.input.Tmeasuring_height2 # height temperature measurements, in case of a 2nd set of obs [m]
if self.Tmeasuring_height2 < self.z0h:
raise Exception('measuring height below z0h')
self.Tmeasuring_height3 = 10.
if hasattr(self.input,'Tmeasuring_height3'):
self.Tmeasuring_height3 = self.input.Tmeasuring_height3 # height temperature measurements, in case of a 3th set of obs [m]
if self.Tmeasuring_height3 < self.z0h:
raise Exception('measuring height below z0h')
self.Tmeasuring_height4 = 10.
if hasattr(self.input,'Tmeasuring_height4'):
self.Tmeasuring_height4 = self.input.Tmeasuring_height4 # height temperature measurements, in case of a 4th set of obs [m]
if self.Tmeasuring_height4 < self.z0h:
raise Exception('measuring height below z0h')
self.Tmeasuring_height5 = 10.
if hasattr(self.input,'Tmeasuring_height5'):
self.Tmeasuring_height5 = self.input.Tmeasuring_height5 # height temperature measurements, in case of a 5th set of obs [m]
if self.Tmeasuring_height5 < self.z0h:
raise Exception('measuring height below z0h')
self.Tmeasuring_height6 = 10.
if hasattr(self.input,'Tmeasuring_height6'):
self.Tmeasuring_height6 = self.input.Tmeasuring_height6 # height temperature measurements, in case of a 6th set of obs [m]
if self.Tmeasuring_height6 < self.z0h:
raise Exception('measuring height below z0h')
self.Tmeasuring_height7 = 10.
if hasattr(self.input,'Tmeasuring_height7'):
self.Tmeasuring_height7 = self.input.Tmeasuring_height7 # height temperature measurements, in case of a 7th set of obs [m]
if self.Tmeasuring_height7 < self.z0h:
raise Exception('measuring height below z0h')
self.qmeasuring_height = 10.
if hasattr(self.input,'qmeasuring_height'):
self.qmeasuring_height = self.input.qmeasuring_height # height spec. hum. measurements [m]
if self.qmeasuring_height < self.z0h:
raise Exception('measuring height below z0h')
self.qmeasuring_height2 = 10.
if hasattr(self.input,'qmeasuring_height2'):
self.qmeasuring_height2 = self.input.qmeasuring_height2 # height spec. hum. measurements [m], in case of a second set of obs
if self.qmeasuring_height2 < self.z0h:
raise Exception('measuring height below z0h')
self.qmeasuring_height3 = 10.
if hasattr(self.input,'qmeasuring_height3'):
self.qmeasuring_height3 = self.input.qmeasuring_height3 # height spec. hum. measurements [m], in case of a 3th set of obs
if self.qmeasuring_height3 < self.z0h:
raise Exception('measuring height below z0h')
self.qmeasuring_height4 = 10.
if hasattr(self.input,'qmeasuring_height4'):
self.qmeasuring_height4 = self.input.qmeasuring_height4 # height spec. hum. measurements [m], in case of a 4th set of obs
if self.qmeasuring_height4 < self.z0h:
raise Exception('measuring height below z0h')
self.qmeasuring_height5 = 10.
if hasattr(self.input,'qmeasuring_height5'):
self.qmeasuring_height5 = self.input.qmeasuring_height5 # height spec. hum. measurements [m], in case of a 5th set of obs
if self.qmeasuring_height5 < self.z0h:
raise Exception('measuring height below z0h')
self.qmeasuring_height6 = 10.
if hasattr(self.input,'qmeasuring_height6'):
self.qmeasuring_height6 = self.input.qmeasuring_height6 # height spec. hum. measurements [m], in case of a 6th set of obs
if self.qmeasuring_height6 < self.z0h:
raise Exception('measuring height below z0h')
self.qmeasuring_height7 = 10.
if hasattr(self.input,'qmeasuring_height7'):
self.qmeasuring_height7 = self.input.qmeasuring_height7 # height spec. hum. measurements [m], in case of a 7th set of obs
if self.qmeasuring_height7 < self.z0h:
raise Exception('measuring height below z0h')
self.deltaCO2 = self.input.deltaCO2 # initial CO2 jump at h [ppm]
self.deltaCOS = self.input.deltaCOS # initial COS jump at h [ppb]
self.gammaCO2 = self.input.gammaCO2 # free atmosphere CO2 lapse rate [ppm m-1]
self.gammaCOS = self.input.gammaCOS # free atmosphere COS lapse rate [ppb m-1]
self.advCO2 = self.input.advCO2 # advection of CO2 [ppm s-1]
self.advCOS = self.input.advCOS # advection of COS [ppb s-1]
fac = self.mair / (self.rho*self.mco2) # Conversion factor mgCO2 m-2 s-1 to ppm m s-1
self.wCO2 = self.input.wCO2 * fac # surface kinematic CO2 flux [ppm m s-1]
self.wCOS = self.input.wCOS # surface kinematic COS flux [ppb m s-1] #used in surf layer module before calculated in ags (via call to land surface)
self.wCO2A = 0 # surface assimulation CO2 flux [ppm m s-1]
self.wCO2R = 0 # surface respiration CO2 flux [ppm m s-1]
self.wCO2e = None # entrainment CO2 flux [ppm m s-1]
self.wCO2M = 0 # CO2 mass flux [ppm m s-1]
self.wCOSM = 0 # COS mass flux [ppb m s-1]
if self.ls_type == 'ags':
self.gciCOS = self.input.gciCOS # COS canopy scale internal conductance [m/s]
if self.ls_type == 'canopy_model':
self.incl_H2Ocan = True
if hasattr(self.input,'incl_H2O'):
self.incl_H2Ocan = self.input.incl_H2Ocan
if hasattr(self.input,'ra_veg'):
self.ra_veg = self.input.ra_veg #resistance of vegetation for canopy model s m-1, only for K_mode='int_resistance'
else:
self.ra_veg = None
if hasattr(self.input,'dt_can'):
self.dt_can = self.input.dt_can #resistance of vegetation for canopy model s m-1, only for K_mode='int_resistance'
if self.dt/self.dt_can != np.floor(self.dt/self.dt_can):
raise Exception('dt should be a multiple of dt_can')
else:
self.dt_can = self.dt
self.calc_sun_shad = self.input.calc_sun_shad
if self.calc_sun_shad:
self.prescr_fPARdif = self.input.prescr_fPARdif
if self.prescr_fPARdif:
self.fPARdif = self.input.fPARdif #fraction of diffuse PAR, either a fixed number or an array the size of the number of timesteps
if not self.sw_ls: #allow for prescribing time-dependent fluxes if land surface not used. For all the scalars, an initial or constant flux is already read in before.
#Make sure that the prescribed initial value of the flux (e.g. wtheta) is identical to the first value of the flux read in here (e.g. first value of wtheta_input) !!
if hasattr(self.input,'wtheta_input'):
self.wtheta_input = self.input.wtheta_input
if len(self.wtheta_input) != self.tsteps: #check wether dimensions are ok
raise Exception('Wrong length of wtheta_input')
if hasattr(self.input,'wq_input'):
self.wq_input = self.input.wq_input
if len(self.wq_input) != self.tsteps:
raise Exception('Wrong length of wq_input')
if hasattr(self.input,'wCO2_input'):
self.wCO2_input = self.input.wCO2_input * fac
if len(self.wCO2_input) != self.tsteps:
raise Exception('Wrong length of wCO2_input')
if hasattr(self.input,'wCOS_input'):
self.wCOS_input = self.input.wCOS_input
if len(self.wCOS_input) != self.tsteps:
raise Exception('Wrong length of wCOS_input')
# Some sanity checks for valid input
if (self.c_beta is None):
self.c_beta = 0 # Zero curvature; linear response
assert(self.c_beta >= 0 or self.c_beta <= 1)
# initialize output
self.out = model_output(self,self.tsteps)
self.statistics(call_from_init=True)
# calculate initial diagnostic variables
if(self.sw_rad):
self.run_radiation(call_from_init=True)
if(self.sw_sl):
for i in range(self.nr_of_surf_lay_its):
self.run_surface_layer(call_from_init=True,iterationnumber=i)
if(self.sw_ls):
self.run_land_surface(call_from_init=True)
if(self.sw_cu):
self.run_mixed_layer(call_from_init=True)
self.run_cumulus(call_from_init=True)
if(self.sw_ml):
self.run_mixed_layer(call_from_init=True)
def timestep(self):
if not self.sw_ls: #only if land surface not on, otherwise fluxes are calculated
if hasattr(self,'wtheta_input'):
self.wtheta = self.wtheta_input[self.t]
if hasattr(self,'wq_input'):
self.wq = self.wq_input[self.t]
if hasattr(self,'wCO2_input'):
self.wCO2 = self.wCO2_input[self.t] #fac already applied in init
if hasattr(self,'wCOS_input'):
self.wCOS = self.wCOS_input[self.t]
self.statistics()
# run radiation model
if(self.sw_rad):
self.run_radiation()
# run surface layer model
if(self.sw_sl):
self.run_surface_layer()
# run land surface model
if(self.sw_ls):
self.run_land_surface()
# run cumulus parameterization
if(self.sw_cu):
self.run_cumulus()
# run mixed-layer model
if(self.sw_ml):
self.run_mixed_layer()
# store output before time integration
self.store()
# time integrate land surface model
if(self.sw_ls):
self.integrate_land_surface()
# time integrate mixed-layer model
if(self.sw_ml):
self.integrate_mixed_layer()
def statistics(self,call_from_init=False):
self.vars_stat= {}
if self.checkpoint:
if call_from_init:
self.cpx_init[0]['stat_q'] = self.q
self.cpx_init[0]['stat_theta'] = self.theta
self.cpx_init[0]['stat_wq'] = self.wq
self.cpx_init[0]['stat_deltatheta'] = self.deltatheta
self.cpx_init[0]['stat_deltaq'] = self.deltaq
self.cpx_init[0]['stat_t'] = self.t
self.cpx_init[0]['stat_p_lcl_end'] = [] #this is special, for the while loop
self.cpx_init[0]['stat_T_lcl_end'] = []
else:
self.cpx[self.t]['stat_q'] = self.q
self.cpx[self.t]['stat_theta'] = self.theta
self.cpx[self.t]['stat_wq'] = self.wq
self.cpx[self.t]['stat_deltatheta'] = self.deltatheta
self.cpx[self.t]['stat_deltaq'] = self.deltaq
self.cpx[self.t]['stat_t'] = self.t
self.cpx[self.t]['stat_p_lcl_end'] = []
self.cpx[self.t]['stat_T_lcl_end'] = []
# Calculate virtual temperatures
self.thetav = self.theta + 0.61 * self.theta * self.q
self.wthetav = self.wtheta + 0.61 * self.theta * self.wq
self.deltathetav = (self.theta + self.deltatheta) * (1. + 0.61 * (self.q + self.deltaq)) - self.theta * (1. + 0.61 * self.q)
# Mixed-layer top properties
self.P_h = self.Ps - self.rho * self.g * self.h
self.T_h = self.theta - self.g/self.cp * self.h
#self.P_h = self.Ps / np.exp((self.g * self.h)/(self.Rd * self.theta))
#self.T_h = self.theta / (self.Ps / self.P_h)**(self.Rd/self.cp)
qsat_variable = qsat(self.T_h, self.P_h)
self.RH_h = self.q / qsat_variable
# Find lifting condensation level iteratively
if(self.t == 0):
self.lcl = self.h
RHlcl = 0.5
else:
RHlcl = 0.9998
itmax = 50 #Peter Bosman: I have increased this to from 30 to 50
it = 0
while(((RHlcl <= 0.9999) or (RHlcl >= 1.0001)) and it<itmax):
self.lcl += (1.-RHlcl)*1000.
p_lcl = self.Ps - self.rho * self.g * self.lcl
T_lcl = self.theta - self.g/self.cp * self.lcl
RHlcl = self.q / qsat(T_lcl, p_lcl)
it += 1
if self.checkpoint:
if call_from_init:
self.cpx_init[0]['stat_p_lcl_end'] += [p_lcl]
self.cpx_init[0]['stat_T_lcl_end'] += [T_lcl]
else:
self.cpx[self.t]['stat_p_lcl_end'] += [p_lcl]
self.cpx[self.t]['stat_T_lcl_end'] += [T_lcl]
if(it == itmax):
if self.sw_printwarnings:
print("LCL calculation not converged!!")
print("RHlcl = %f, zlcl=%f"%(RHlcl, self.lcl))
if self.checkpoint:
if call_from_init:
self.cpx_init[0]['stat_qsat_variable_end'] = qsat_variable
self.cpx_init[0]['stat_T_h_end'] = self.T_h
self.cpx_init[0]['stat_P_h_end'] = self.P_h
self.cpx_init[0]['stat_it_end'] = it
else:
self.cpx[self.t]['stat_qsat_variable_end'] = qsat_variable
self.cpx[self.t]['stat_T_h_end'] = self.T_h
self.cpx[self.t]['stat_P_h_end'] = self.P_h
self.cpx[self.t]['stat_it_end'] = it
if self.save_vars_indict:
the_locals = cp.deepcopy(locals()) #to prevent error 'dictionary changed size during iteration'
for variablename in the_locals: #note that the self variables are not included
if str(variablename) != 'self':
self.vars_stat.update({variablename: the_locals[variablename]})
def run_cumulus(self,call_from_init=False):
self.vars_rc= {}
if self.checkpoint:
if call_from_init:
self.cpx_init[0]['rc_wthetav'] = self.wthetav #subscript rc from run_cumulus
self.cpx_init[0]['rc_deltaq'] = self.deltaq
self.cpx_init[0]['rc_dz_h'] = self.dz_h
self.cpx_init[0]['rc_wstar'] = self.wstar
self.cpx_init[0]['rc_wqe'] = self.wqe
self.cpx_init[0]['rc_wqM'] = self.wqM
self.cpx_init[0]['rc_h'] = self.h
self.cpx_init[0]['rc_deltaCO2'] = self.deltaCO2
self.cpx_init[0]['rc_deltaCOS'] = self.deltaCOS
self.cpx_init[0]['rc_wCO2e'] = self.wCO2e
self.cpx_init[0]['rc_wCO2M'] = self.wCO2M
self.cpx_init[0]['rc_wCOSe'] = self.wCOSe
self.cpx_init[0]['rc_wCOSM'] = self.wCOSM
self.cpx_init[0]['rc_q'] = self.q
self.cpx_init[0]['rc_T_h'] = self.T_h
self.cpx_init[0]['rc_P_h'] = self.P_h
else:
self.cpx[self.t]['rc_wthetav'] = self.wthetav #subscript rc from run_cumulus
self.cpx[self.t]['rc_deltaq'] = self.deltaq
self.cpx[self.t]['rc_dz_h'] = self.dz_h
self.cpx[self.t]['rc_wstar'] = self.wstar
self.cpx[self.t]['rc_wqe'] = self.wqe
self.cpx[self.t]['rc_wqM'] = self.wqM
self.cpx[self.t]['rc_h'] = self.h
self.cpx[self.t]['rc_deltaCO2'] = self.deltaCO2
self.cpx[self.t]['rc_deltaCOS'] = self.deltaCOS
self.cpx[self.t]['rc_wCO2e'] = self.wCO2e
self.cpx[self.t]['rc_wCO2M'] = self.wCO2M
self.cpx[self.t]['rc_wCOSe'] = self.wCOSe
self.cpx[self.t]['rc_wCOSM'] = self.wCOSM
self.cpx[self.t]['rc_q'] = self.q
self.cpx[self.t]['rc_T_h'] = self.T_h
self.cpx[self.t]['rc_P_h'] = self.P_h
# Calculate mixed-layer top relative humidity variance (Neggers et. al 2006/7)
if(self.wthetav > 0):
self.q2_h = -(self.wqe + self.wqM ) * self.deltaq * self.h / (self.dz_h * self.wstar)
self.CO22_h = -(self.wCO2e+ self.wCO2M) * self.deltaCO2 * self.h / (self.dz_h * self.wstar)
self.COS2_h = -(self.wCOSe+ self.wCOSM) * self.deltaCOS * self.h / (self.dz_h * self.wstar)
else:
self.q2_h = 0.
self.CO22_h = 0.
self.COS2_h = 0.
if self.checkpoint:
if call_from_init:
self.cpx_init[0]['rc_q2_h_middle'] = self.q2_h
self.cpx_init[0]['rc_CO22_h_middle'] = self.CO22_h
self.cpx_init[0]['rc_COS2_h_middle'] = self.COS2_h
else:
self.cpx[self.t]['rc_q2_h_middle'] = self.q2_h
self.cpx[self.t]['rc_CO22_h_middle'] = self.CO22_h
self.cpx[self.t]['rc_COS2_h_middle'] = self.COS2_h
if self.q2_h <= 0.:
self.q2_h = 1.e-200 #This I (Peter Bosman) added to prevent problems with values becoming infinity, see below, that gives problems to adjoint.
if self.CO22_h <= 0.:
self.CO22_h = 1.e-200 #This I (Peter Bosman) added to prevent problems with the derivative of self.wCO2M below, derivative not defined when self.CO22_h = 0
if self.COS2_h <= 0.:
self.COS2_h = 1.e-200 #This I (Peter Bosman) added
# calculate cloud core fraction (ac), mass flux (M) and moisture flux (wqM)
qsat_variable_rc = qsat(self.T_h, self.P_h)
self.ac = max(0., 0.5 + (0.36 * np.arctan(1.55 * ((self.q - qsat_variable_rc) / self.q2_h**0.5))))
#if self.q2_h == 0., ((self.q - qsat_variable_rc) / self.q2_h**0.5) goes to infinity, but arctan (inf) exists (=pi/2). But problems for adjoint...
self.M = self.ac * self.wstar
#note that if q - qsat > 0 and the variance of q is small, ac will be large and thus M large -> BL might shrink
self.wqM = self.M * self.q2_h**0.5
# Only calculate CO2 mass-flux if mixed-layer top jump is negative
if(self.deltaCO2 < 0):
self.wCO2M = self.M * self.CO22_h**0.5
else:
self.wCO2M = 0.
if(self.deltaCOS < 0):
self.wCOSM = self.M * self.COS2_h**0.5
else:
self.wCOSM = 0.
if self.checkpoint:
if call_from_init:
self.cpx_init[0]['rc_q2_h_end'] = self.q2_h
self.cpx_init[0]['rc_ac_end'] = self.ac
self.cpx_init[0]['rc_M_end'] = self.M
self.cpx_init[0]['rc_CO22_h_end'] = self.CO22_h
self.cpx_init[0]['rc_COS2_h_end'] = self.COS2_h
self.cpx_init[0]['rc_qsat_variable_rc_end'] = qsat_variable_rc
else:
self.cpx[self.t]['rc_q2_h_end'] = self.q2_h
self.cpx[self.t]['rc_ac_end'] = self.ac
self.cpx[self.t]['rc_M_end'] = self.M
self.cpx[self.t]['rc_CO22_h_end'] = self.CO22_h
self.cpx[self.t]['rc_COS2_h_end'] = self.COS2_h
self.cpx[self.t]['rc_qsat_variable_rc_end'] = qsat_variable_rc
if self.save_vars_indict:
the_locals = cp.deepcopy(locals()) #to prevent error 'dictionary changed size during iteration'
for variablename in the_locals: #note that the self variables are not included
if str(variablename) != 'self':
self.vars_rc.update({variablename: the_locals[variablename]})
def run_mixed_layer(self,call_from_init=False):
self.vars_rml= {}
if self.checkpoint:
if call_from_init:
self.cpx_init[0]['rml_h'] = self.h #subscript rml from run_mixed_layer
self.cpx_init[0]['rml_ustar'] = self.ustar
self.cpx_init[0]['rml_u'] = self.u
self.cpx_init[0]['rml_v'] = self.v
self.cpx_init[0]['rml_deltatheta'] = self.deltatheta
self.cpx_init[0]['rml_deltathetav'] = self.deltathetav
self.cpx_init[0]['rml_thetav'] = self.thetav
self.cpx_init[0]['rml_wthetav'] = self.wthetav
self.cpx_init[0]['rml_deltaq'] = self.deltaq
self.cpx_init[0]['rml_deltaCO2'] = self.deltaCO2
self.cpx_init[0]['rml_deltaCOS'] = self.deltaCOS
self.cpx_init[0]['rml_deltau'] = self.deltau
self.cpx_init[0]['rml_deltav'] = self.deltav
self.cpx_init[0]['rml_wtheta'] = self.wtheta
self.cpx_init[0]['rml_wq'] = self.wq
self.cpx_init[0]['rml_wqM'] = self.wqM
self.cpx_init[0]['rml_wCO2'] = self.wCO2
self.cpx_init[0]['rml_wCO2M'] = self.wCO2M
self.cpx_init[0]['rml_wCOS'] = self.wCOS
self.cpx_init[0]['rml_wCOSM'] = self.wCOSM
self.cpx_init[0]['rml_ac'] = self.ac
self.cpx_init[0]['rml_lcl'] = self.lcl
self.cpx_init[0]['rml_gammatheta'] = self.gammatheta
self.cpx_init[0]['rml_gammatheta2'] = self.gammatheta2
self.cpx_init[0]['rml_htrans'] = self.htrans
self.cpx_init[0]['rml_gammaq'] = self.gammaq
self.cpx_init[0]['rml_gammaCO2'] = self.gammaCO2
self.cpx_init[0]['rml_gammaCOS'] = self.gammaCOS
self.cpx_init[0]['rml_gammau'] = self.gammau
self.cpx_init[0]['rml_gammav'] = self.gammav
self.cpx_init[0]['rml_M'] = self.M
self.cpx_init[0]['rml_divU'] = self.divU
self.cpx_init[0]['rml_fc'] = self.fc
self.cpx_init[0]['rml_dFz'] = self.dFz
self.cpx_init[0]['rml_beta'] = self.beta
else:
self.cpx[self.t]['rml_h'] = self.h #subscript rml from run_mixed_layer
self.cpx[self.t]['rml_ustar'] = self.ustar
self.cpx[self.t]['rml_u'] = self.u
self.cpx[self.t]['rml_v'] = self.v
self.cpx[self.t]['rml_deltatheta'] = self.deltatheta
self.cpx[self.t]['rml_deltathetav'] = self.deltathetav
self.cpx[self.t]['rml_thetav'] = self.thetav
self.cpx[self.t]['rml_wthetav'] = self.wthetav
self.cpx[self.t]['rml_deltaq'] = self.deltaq
self.cpx[self.t]['rml_deltaCO2'] = self.deltaCO2
self.cpx[self.t]['rml_deltaCOS'] = self.deltaCOS
self.cpx[self.t]['rml_deltau'] = self.deltau
self.cpx[self.t]['rml_deltav'] = self.deltav
self.cpx[self.t]['rml_wtheta'] = self.wtheta
self.cpx[self.t]['rml_wq'] = self.wq
self.cpx[self.t]['rml_wqM'] = self.wqM
self.cpx[self.t]['rml_wCO2'] = self.wCO2
self.cpx[self.t]['rml_wCO2M'] = self.wCO2M
self.cpx[self.t]['rml_wCOS'] = self.wCOS
self.cpx[self.t]['rml_wCOSM'] = self.wCOSM
self.cpx[self.t]['rml_ac'] = self.ac
self.cpx[self.t]['rml_lcl'] = self.lcl
self.cpx[self.t]['rml_gammatheta'] = self.gammatheta
self.cpx[self.t]['rml_gammatheta2'] = self.gammatheta2
self.cpx[self.t]['rml_htrans'] = self.htrans
self.cpx[self.t]['rml_gammaq'] = self.gammaq
self.cpx[self.t]['rml_gammaCO2'] = self.gammaCO2
self.cpx[self.t]['rml_gammaCOS'] = self.gammaCOS
self.cpx[self.t]['rml_gammau'] = self.gammau
self.cpx[self.t]['rml_gammav'] = self.gammav
self.cpx[self.t]['rml_M'] = self.M
self.cpx[self.t]['rml_divU'] = self.divU
self.cpx[self.t]['rml_fc'] = self.fc
self.cpx[self.t]['rml_dFz'] = self.dFz
self.cpx[self.t]['rml_beta'] = self.beta
if(not self.sw_sl):
# decompose ustar along the wind components
self.uw = - np.sign(self.u) * (self.ustar ** 4. / (self.v ** 2. / self.u ** 2. + 1.)) ** (0.5)
self.vw = - np.sign(self.v) * (self.ustar ** 4. / (self.u ** 2. / self.v ** 2. + 1.)) ** (0.5)
# calculate large-scale vertical velocity (subsidence)
self.ws = -self.divU * self.h
# calculate compensation to fix the free troposphere in case of subsidence
if(self.sw_fixft):
if self.h <= self.htrans:
w_th_ft = self.gammatheta * self.ws
else:
w_th_ft = self.gammatheta2 * self.ws
w_q_ft = self.gammaq * self.ws
w_CO2_ft = self.gammaCO2 * self.ws
w_COS_ft = self.gammaCOS * self.ws
else:
w_th_ft = 0.
w_q_ft = 0.
w_CO2_ft = 0.
w_COS_ft = 0.
# calculate mixed-layer growth due to cloud top radiative divergence
self.wf = self.dFz / (self.rho * self.cp * self.deltatheta)
# calculate convective velocity scale w*
if(self.wthetav > 0.):
self.wstar = ((self.g * self.h * self.wthetav) / self.thetav)**(1./3.)
else:
self.wstar = 1e-6;
# Virtual heat entrainment flux
self.wthetave = -self.beta * self.wthetav
# compute mixed-layer tendencies
if(self.sw_shearwe):
self.we = (-self.wthetave + 5. * self.ustar ** 3. * self.thetav / (self.g * self.h)) / self.deltathetav #see p11 of supplementary material of Vila et al 2012 (Nature paper), beta equation is part of this. See also paper notes 5, calculation we in CLASS
else:
self.we = -self.wthetave / self.deltathetav
# Don't allow boundary layer shrinking if wtheta < 0
if self.checkpoint:
if call_from_init:
self.cpx_init[0]['rml_we_middle'] = self.we
else:
self.cpx[self.t]['rml_we_middle'] = self.we
if(self.we < 0):
self.we = 0.
# Calculate entrainment fluxes
self.wthetae = -self.we * self.deltatheta
self.wqe = -self.we * self.deltaq
self.wCO2e = -self.we * self.deltaCO2
self.wCOSe = -self.we * self.deltaCOS
self.htend = self.we + self.ws + self.wf - self.M
self.thetatend = (self.wtheta - self.wthetae ) / self.h + self.advtheta
self.qtend = (self.wq - self.wqe - self.wqM ) / self.h + self.advq
self.CO2tend = (self.wCO2 - self.wCO2e - self.wCO2M) / self.h + self.advCO2
self.COStend = (self.wCOS - self.wCOSe - self.wCOSM) / self.h + self.advCOS
if self.h <= self.htrans:
self.deltathetatend = self.gammatheta * (self.we + self.wf - self.M) - self.thetatend + w_th_ft
else:
self.deltathetatend = self.gammatheta2 * (self.we + self.wf - self.M) - self.thetatend + w_th_ft
self.deltaqtend = self.gammaq * (self.we + self.wf - self.M) - self.qtend + w_q_ft #first term is the change in the value of q just above the BL, the second term is the change in the value of q in the ML
self.deltaCO2tend = self.gammaCO2 * (self.we + self.wf - self.M) - self.CO2tend + w_CO2_ft
self.deltaCOStend = self.gammaCOS * (self.we + self.wf - self.M) - self.COStend + w_COS_ft
if self.sw_advfp:
self.deltathetatend += self.advtheta #this way advection cancels out for the jump tendencies, since advection is also added to the mixed layer tendencies
#(assumption is advection equal at all heights)
self.deltaqtend += self.advq
self.deltaCO2tend += self.advCO2
self.deltaCOStend += self.advCOS