forked from pressel/pycles
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Initialization.pyx
1773 lines (1453 loc) · 68.7 KB
/
Initialization.pyx
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
#!python
#cython: boundscheck=False
#cython: wraparound=True
#cython: initializedcheck=False
#cython: cdivision=True
import netCDF4 as nc
import numpy as np
cimport numpy as np
from scipy.interpolate import PchipInterpolator,pchip_interpolate
cimport ParallelMPI
from NetCDFIO cimport NetCDFIO_Stats
cimport Grid
cimport PrognosticVariables
cimport DiagnosticVariables
from thermodynamic_functions cimport exner_c, entropy_from_thetas_c, thetas_t_c, qv_star_c, thetas_c, thetali_c
cimport ReferenceState
from Forcing cimport AdjustedMoistAdiabat
from Thermodynamics cimport LatentHeat
from libc.math cimport sqrt, fmin, cos, exp, fabs
include 'parameters.pxi'
# import matplotlib.pyplot as plt
def InitializationFactory(namelist):
casename = namelist['meta']['casename']
if casename == 'SullivanPatton':
return InitSullivanPatton
elif casename == 'StableBubble':
return InitStableBubble
elif casename == 'SaturatedBubble':
return InitSaturatedBubble
elif casename == 'Bomex':
return InitBomex
elif casename == 'Gabls':
return InitGabls
elif casename == 'DYCOMS_RF01':
return InitDYCOMS_RF01
elif casename == 'DYCOMS_RF02':
return InitDYCOMS_RF02
elif casename == 'SMOKE':
return InitSmoke
elif casename == 'Rico':
return InitRico
elif casename == 'Isdac':
return InitIsdac
elif casename == 'IsdacCC':
return InitIsdacCC
elif casename == 'Mpace':
return InitMpace
elif casename == 'Sheba':
return InitSheba
elif casename == 'CGILS':
return InitCGILS
elif casename == 'ZGILS':
return InitZGILS
else:
pass
def InitStableBubble(namelist, Grid.Grid Gr,PrognosticVariables.PrognosticVariables PV,
ReferenceState.ReferenceState RS, Th, NetCDFIO_Stats NS, ParallelMPI.ParallelMPI Pa, LatentHeat LH):
#Generate reference profiles
RS.Pg = 1.0e5
RS.Tg = 300.0
RS.qtg = 0.0
#Set velocities for Galilean transformation
RS.u0 = 0.0
RS.v0 = 0.0
RS.initialize(Gr, Th, NS, Pa)
#Get the variable number for each of the velocity components
cdef:
Py_ssize_t u_varshift = PV.get_varshift(Gr,'u')
Py_ssize_t v_varshift = PV.get_varshift(Gr,'v')
Py_ssize_t w_varshift = PV.get_varshift(Gr,'w')
Py_ssize_t s_varshift = PV.get_varshift(Gr,'s')
Py_ssize_t i,j,k
Py_ssize_t ishift, jshift
Py_ssize_t ijk
double t
double dist
t_min = 9999.9
for i in xrange(Gr.dims.nlg[0]):
ishift = i * Gr.dims.nlg[1] * Gr.dims.nlg[2]
for j in xrange(Gr.dims.nlg[1]):
jshift = j * Gr.dims.nlg[2]
for k in xrange(Gr.dims.nlg[2]):
ijk = ishift + jshift + k
PV.values[u_varshift + ijk] = 0.0
PV.values[v_varshift + ijk] = 0.0
PV.values[w_varshift + ijk] = 0.0
dist = np.sqrt(((Gr.x_half[i + Gr.dims.indx_lo[0]]/1000.0 - 25.6)/4.0)**2.0 + ((Gr.z_half[k + Gr.dims.indx_lo[2]]/1000.0 - 3.0)/2.0)**2.0)
dist = fmin(dist,1.0)
t = (300.0 )*exner_c(RS.p0_half[k]) - 15.0*( cos(np.pi * dist) + 1.0) /2.0
PV.values[s_varshift + ijk] = Th.entropy(RS.p0_half[k],t,0.0,0.0,0.0)
return
def InitSaturatedBubble(namelist,Grid.Grid Gr,PrognosticVariables.PrognosticVariables PV,
ReferenceState.ReferenceState RS, Th, NetCDFIO_Stats NS, ParallelMPI.ParallelMPI Pa, LatentHeat LH ):
#Generate reference profiles
RS.Pg = 1.0e5
RS.qtg = 0.02
#RS.Tg = 300.0
thetas_sfc = 320.0
qt_sfc = 0.0196 #RS.qtg
RS.qtg = qt_sfc
#Set velocities for Galilean transformation
RS.u0 = 0.0
RS.v0 = 0.0
def theta_to_T(p0_,thetas_,qt_):
T1 = Tt
T2 = Tt + 1.
pv1 = Th.get_pv_star(T1)
pv2 = Th.get_pv_star(T2)
qs1 = qv_star_c(p0_, RS.qtg,pv1)
ql1 = np.max([0.0,qt_ - qs1])
L1 = Th.get_lh(T1)
f1 = thetas_ - thetas_t_c(p0_,T1,qt_,qt_-ql1,ql1,L1)
delta = np.abs(T1 - T2)
while delta >= 1e-12:
L2 = Th.get_lh(T2)
pv2 = Th.get_pv_star(T2)
qs2 = qv_star_c(p0_, RS.qtg, pv2)
ql2 = np.max([0.0,qt_ - qs2])
f2 = thetas_ - thetas_t_c(p0_,T2,qt_,qt_-ql2,ql2,L2)
Tnew = T2 - f2 * (T2 - T1)/(f2 - f1)
T1 = T2
T2 = Tnew
f1 = f2
delta = np.abs(T1 - T2)
return T2, ql2
RS.Tg, ql = theta_to_T(RS.Pg,thetas_sfc,qt_sfc)
RS.initialize(Gr, Th, NS, Pa)
#Get the variable number for each of the velocity components
cdef:
Py_ssize_t u_varshift = PV.get_varshift(Gr,'u')
Py_ssize_t v_varshift = PV.get_varshift(Gr,'v')
Py_ssize_t w_varshift = PV.get_varshift(Gr,'w')
Py_ssize_t s_varshift = PV.get_varshift(Gr,'s')
Py_ssize_t qt_varshift = PV.get_varshift(Gr,'qt')
Py_ssize_t i,j,k
Py_ssize_t ishift, jshift
Py_ssize_t ijk
double t
double dist
double thetas
for i in xrange(Gr.dims.nlg[0]):
ishift = i * Gr.dims.nlg[1] * Gr.dims.nlg[2]
for j in xrange(Gr.dims.nlg[1]):
jshift = j * Gr.dims.nlg[2]
for k in xrange(Gr.dims.nlg[2]):
ijk = ishift + jshift + k
dist = np.sqrt(((Gr.x_half[i + Gr.dims.indx_lo[0]]/1000.0 - 10.0)/2.0)**2.0 + ((Gr.z_half[k + Gr.dims.indx_lo[2]]/1000.0 - 2.0)/2.0)**2.0)
dist = np.minimum(1.0,dist)
thetas = RS.Tg
thetas += 2.0 * np.cos(np.pi * dist / 2.0)**2.0
PV.values[s_varshift + ijk] = entropy_from_thetas_c(thetas,RS.qtg)
PV.values[u_varshift + ijk] = 0.0 - RS.u0
PV.values[v_varshift + ijk] = 0.0 - RS.v0
PV.values[w_varshift + ijk] = 0.0
PV.values[qt_varshift + ijk] = RS.qtg
return
def InitSullivanPatton(namelist,Grid.Grid Gr,PrognosticVariables.PrognosticVariables PV,
ReferenceState.ReferenceState RS, Th, NetCDFIO_Stats NS, ParallelMPI.ParallelMPI Pa, LatentHeat LH ):
#Generate the reference profiles
RS.Pg = 1.0e5 #Pressure at ground
RS.Tg = 300.0 #Temperature at ground
RS.qtg = 0.0 #Total water mixing ratio at surface
RS.u0 = 1.0 # velocities removed in Galilean transformation
RS.v0 = 0.0
RS.initialize(Gr, Th, NS, Pa)
#Get the variable number for each of the velocity components
np.random.seed(Pa.rank)
cdef:
Py_ssize_t u_varshift = PV.get_varshift(Gr,'u')
Py_ssize_t v_varshift = PV.get_varshift(Gr,'v')
Py_ssize_t w_varshift = PV.get_varshift(Gr,'w')
Py_ssize_t s_varshift = PV.get_varshift(Gr,'s')
Py_ssize_t i,j,k
Py_ssize_t ishift, jshift, e_varshift
Py_ssize_t ijk
double [:] theta = np.empty((Gr.dims.nlg[2]),dtype=np.double,order='c')
double t
#Generate initial perturbations (here we are generating more than we need)
cdef double [:] theta_pert = np.random.random_sample(Gr.dims.npg)
cdef double theta_pert_
for k in xrange(Gr.dims.nlg[2]):
if Gr.zl_half[k] <= 974.0:
theta[k] = 300.0
elif Gr.zl_half[k] <= 1074.0:
theta[k] = 300.0 + (Gr.zl_half[k] - 974.0) * 0.08
else:
theta[k] = 308.0 + (Gr.zl_half[k] - 1074.0) * 0.003
cdef double [:] p0 = RS.p0_half
#Now loop and set the initial condition
for i in xrange(Gr.dims.nlg[0]):
ishift = i * Gr.dims.nlg[1] * Gr.dims.nlg[2]
for j in xrange(Gr.dims.nlg[1]):
jshift = j * Gr.dims.nlg[2]
for k in xrange(Gr.dims.nlg[2]):
ijk = ishift + jshift + k
PV.values[u_varshift + ijk] = 1.0 - RS.u0
PV.values[v_varshift + ijk] = 0.0 - RS.v0
PV.values[w_varshift + ijk] = 0.0
#Now set the entropy prognostic variable including a potential temperature perturbation
if Gr.zl_half[k] < 200.0:
theta_pert_ = (theta_pert[ijk] - 0.5)* 0.1
else:
theta_pert_ = 0.0
t = (theta[k] + theta_pert_)*exner_c(RS.p0_half[k])
PV.values[s_varshift + ijk] = Th.entropy(RS.p0_half[k],t,0.0,0.0,0.0)
if 'e' in PV.name_index:
e_varshift = PV.get_varshift(Gr, 'e')
for i in xrange(Gr.dims.nlg[0]):
ishift = i * Gr.dims.nlg[1] * Gr.dims.nlg[2]
for j in xrange(Gr.dims.nlg[1]):
jshift = j * Gr.dims.nlg[2]
for k in xrange(Gr.dims.nlg[2]):
ijk = ishift + jshift + k
PV.values[e_varshift + ijk] = 0.0
return
def InitBomex(namelist,Grid.Grid Gr,PrognosticVariables.PrognosticVariables PV,
ReferenceState.ReferenceState RS, Th, NetCDFIO_Stats NS, ParallelMPI.ParallelMPI Pa, LatentHeat LH ):
#First generate the reference profiles
RS.Pg = 1.015e5 #Pressure at ground
RS.Tg = 300.4 #Temperature at ground
RS.qtg = 0.02245 #Total water mixing ratio at surface
RS.initialize(Gr, Th, NS, Pa)
try:
random_seed_factor = namelist['initialization']['random_seed_factor']
except:
random_seed_factor = 1
np.random.seed(Pa.rank * random_seed_factor)
#Get the variable number for each of the velocity components
cdef:
Py_ssize_t u_varshift = PV.get_varshift(Gr,'u')
Py_ssize_t v_varshift = PV.get_varshift(Gr,'v')
Py_ssize_t w_varshift = PV.get_varshift(Gr,'w')
Py_ssize_t s_varshift = PV.get_varshift(Gr,'s')
Py_ssize_t qt_varshift = PV.get_varshift(Gr,'qt')
Py_ssize_t i,j,k
Py_ssize_t ishift, jshift
Py_ssize_t ijk, e_varshift
double temp
double qt_
double [:] thetal = np.empty((Gr.dims.nlg[2]),dtype=np.double,order='c')
double [:] qt = np.empty((Gr.dims.nlg[2]),dtype=np.double,order='c')
double [:] u = np.empty((Gr.dims.nlg[2]),dtype=np.double,order='c')
Py_ssize_t count
theta_pert = (np.random.random_sample(Gr.dims.npg )-0.5)*0.1
qt_pert = (np.random.random_sample(Gr.dims.npg )-0.5)*0.025/1000.0
for k in xrange(Gr.dims.nlg[2]):
#Set Thetal profile
if Gr.zl_half[k] <= 520.:
thetal[k] = 298.7
if Gr.zl_half[k] > 520.0 and Gr.zl_half[k] <= 1480.0:
thetal[k] = 298.7 + (Gr.zl_half[k] - 520) * (302.4 - 298.7)/(1480.0 - 520.0)
if Gr.zl_half[k] > 1480.0 and Gr.zl_half[k] <= 2000:
thetal[k] = 302.4 + (Gr.zl_half[k] - 1480.0) * (308.2 - 302.4)/(2000.0 - 1480.0)
if Gr.zl_half[k] > 2000.0:
thetal[k] = 308.2 + (Gr.zl_half[k] - 2000.0) * (311.85 - 308.2)/(3000.0 - 2000.0)
#Set qt profile
if Gr.zl_half[k] <= 520:
qt[k] = 17.0 + (Gr.zl_half[k]) * (16.3-17.0)/520.0
if Gr.zl_half[k] > 520.0 and Gr.zl_half[k] <= 1480.0:
qt[k] = 16.3 + (Gr.zl_half[k] - 520.0)*(10.7 - 16.3)/(1480.0 - 520.0)
if Gr.zl_half[k] > 1480.0 and Gr.zl_half[k] <= 2000.0:
qt[k] = 10.7 + (Gr.zl_half[k] - 1480.0) * (4.2 - 10.7)/(2000.0 - 1480.0)
if Gr.zl_half[k] > 2000.0:
qt[k] = 4.2 + (Gr.zl_half[k] - 2000.0) * (3.0 - 4.2)/(3000.0 - 2000.0)
#Change units to kg/kg
qt[k]/= 1000.0
#Set u profile
if Gr.zl_half[k] <= 700.0:
u[k] = -8.75
if Gr.zl_half[k] > 700.0:
u[k] = -8.75 + (Gr.zl_half[k] - 700.0) * (-4.61 - -8.75)/(3000.0 - 700.0)
#Set velocities for Galilean transformation
RS.v0 = 0.0
RS.u0 = 0.5 * (np.amax(u)+np.amin(u))
#Now loop and set the initial condition
#First set the velocities
count = 0
for i in xrange(Gr.dims.nlg[0]):
ishift = i * Gr.dims.nlg[1] * Gr.dims.nlg[2]
for j in xrange(Gr.dims.nlg[1]):
jshift = j * Gr.dims.nlg[2]
for k in xrange(Gr.dims.nlg[2]):
ijk = ishift + jshift + k
PV.values[u_varshift + ijk] = u[k] - RS.u0
PV.values[v_varshift + ijk] = 0.0 - RS.v0
PV.values[w_varshift + ijk] = 0.0
if Gr.zl_half[k] <= 1600.0:
temp = (thetal[k] + (theta_pert[count])) * exner_c(RS.p0_half[k])
qt_ = qt[k]+qt_pert[count]
else:
temp = (thetal[k]) * exner_c(RS.p0_half[k])
qt_ = qt[k]
PV.values[s_varshift + ijk] = Th.entropy(RS.p0_half[k],temp,qt_,0.0,0.0)
PV.values[qt_varshift + ijk] = qt_
count += 1
if 'e' in PV.name_index:
e_varshift = PV.get_varshift(Gr, 'e')
for i in xrange(Gr.dims.nlg[0]):
ishift = i * Gr.dims.nlg[1] * Gr.dims.nlg[2]
for j in xrange(Gr.dims.nlg[1]):
jshift = j * Gr.dims.nlg[2]
for k in xrange(Gr.dims.nlg[2]):
ijk = ishift + jshift + k
PV.values[e_varshift + ijk] = 1.0-Gr.zl_half[k]/3000.0
return
def InitGabls(namelist,Grid.Grid Gr, PrognosticVariables.PrognosticVariables PV,
ReferenceState.ReferenceState RS, Th, NetCDFIO_Stats NS, ParallelMPI.ParallelMPI Pa, LatentHeat LH ):
#Generate the reference profiles
RS.Pg = 1.0e5 #Pressure at ground
RS.Tg = 265.0 #Temperature at ground
RS.qtg = 0.0 #Total water mixing ratio at surface
RS.u0 = 8.0 # velocities removed in Galilean transformation
RS.v0 = 0.0
RS.initialize(Gr, Th, NS, Pa)
#Get the variable number for each of the velocity components
np.random.seed(Pa.rank)
cdef:
Py_ssize_t u_varshift = PV.get_varshift(Gr,'u')
Py_ssize_t v_varshift = PV.get_varshift(Gr,'v')
Py_ssize_t w_varshift = PV.get_varshift(Gr,'w')
Py_ssize_t s_varshift = PV.get_varshift(Gr,'s')
Py_ssize_t i,j,k
Py_ssize_t ishift, jshift, e_varshift
Py_ssize_t ijk
double [:] theta = np.empty((Gr.dims.nlg[2]),dtype=np.double,order='c')
double t
#Generate initial perturbations (here we are generating more than we need)
cdef double [:] theta_pert = np.random.random_sample(Gr.dims.npg)
cdef double theta_pert_
for k in xrange(Gr.dims.nlg[2]):
if Gr.zl_half[k] <= 100.0:
theta[k] = 265.0
else:
theta[k] = 265.0 + (Gr.zl_half[k] - 100.0) * 0.01
cdef double [:] p0 = RS.p0_half
#Now loop and set the initial condition
#First set the velocities
for i in xrange(Gr.dims.nlg[0]):
ishift = i * Gr.dims.nlg[1] * Gr.dims.nlg[2]
for j in xrange(Gr.dims.nlg[1]):
jshift = j * Gr.dims.nlg[2]
for k in xrange(Gr.dims.nlg[2]):
ijk = ishift + jshift + k
PV.values[u_varshift + ijk] = 8.0 - RS.u0
PV.values[v_varshift + ijk] = 0.0 - RS.v0
PV.values[w_varshift + ijk] = 0.0
#Now set the entropy prognostic variable including a potential temperature perturbation
if Gr.zl_half[k] < 50.0:
theta_pert_ = (theta_pert[ijk] - 0.5)* 0.1
else:
theta_pert_ = 0.0
t = (theta[k] + theta_pert_)*exner_c(RS.p0_half[k])
PV.values[s_varshift + ijk] = Th.entropy(RS.p0_half[k],t,0.0,0.0,0.0)
if 'e' in PV.name_index:
e_varshift = PV.get_varshift(Gr, 'e')
for i in xrange(Gr.dims.nlg[0]):
ishift = i * Gr.dims.nlg[1] * Gr.dims.nlg[2]
for j in xrange(Gr.dims.nlg[1]):
jshift = j * Gr.dims.nlg[2]
for k in xrange(Gr.dims.nlg[2]):
ijk = ishift + jshift + k
if Gr.zl_half[k] <= 250.0:
PV.values[e_varshift + ijk] = 0.4*(1.0-Gr.zl_half[k]/250.0)**3.0
else:
PV.values[e_varshift + ijk] = 0.0
return
def InitDYCOMS_RF01(namelist,Grid.Grid Gr,PrognosticVariables.PrognosticVariables PV,
ReferenceState.ReferenceState RS, Th, NetCDFIO_Stats NS, ParallelMPI.ParallelMPI Pa , LatentHeat LH):
"""
Initialize the DYCOMS_RF01 case described in
Bjorn Stevens, Chin-Hoh Moeng, Andrew S. Ackerman, Christopher S. Bretherton, Andreas Chlond, Stephan de Roode,
James Edwards, Jean-Christophe Golaz, Hongli Jiang, Marat Khairoutdinov, Michael P. Kirkpatrick, David C. Lewellen,
Adrian Lock, Frank Müller, David E. Stevens, Eoin Whelan, and Ping Zhu, 2005: Evaluation of Large-Eddy Simulations
via Observations of Nocturnal Marine Stratocumulus. Mon. Wea. Rev., 133, 1443–1462.
doi: http://dx.doi.org/10.1175/MWR2930.1
:param Gr: Grid cdef extension class
:param PV: PrognosticVariables cdef extension class
:param RS: ReferenceState cdef extension class
:param Th: Thermodynamics class
:return: None
"""
# Generate Reference Profiles
RS.Pg = 1017.8 * 100.0
RS.qtg = 9.0/1000.0
RS.u0 = 7.0
RS.v0 = -5.5
# Use an exner function with values for Rd, and cp given in Stevens 2004 to compute temperature given $\theta_l$
RS.Tg = 289.0 * (RS.Pg/p_tilde)**(287.0/1015.0)
RS.initialize(Gr ,Th, NS, Pa)
#Set up $\tehta_l$ and $\qt$ profiles
cdef:
Py_ssize_t i
Py_ssize_t j
Py_ssize_t k
Py_ssize_t ijk, ishift, jshift
Py_ssize_t istride = Gr.dims.nlg[1] * Gr.dims.nlg[2]
Py_ssize_t jstride = Gr.dims.nlg[2]
Py_ssize_t u_varshift = PV.get_varshift(Gr,'u')
Py_ssize_t v_varshift = PV.get_varshift(Gr,'v')
Py_ssize_t w_varshift = PV.get_varshift(Gr,'w')
Py_ssize_t s_varshift = PV.get_varshift(Gr,'s')
Py_ssize_t qt_varshift = PV.get_varshift(Gr,'qt')
double [:] thetal = np.zeros((Gr.dims.nlg[2],),dtype=np.double,order='c')
double [:] qt = np.zeros((Gr.dims.nlg[2],),dtype=np.double,order='c')
Py_ssize_t e_varshift
for k in xrange(Gr.dims.nlg[2]):
if Gr.zl_half[k] <=840.0:
thetal[k] = 289.0
qt[k] = 9.0/1000.0
if Gr.zl_half[k] > 840.0:
thetal[k] = 297.5 + (Gr.zl_half[k] - 840.0)**(1.0/3.0)
qt[k] = 1.5/1000.0
def compute_thetal(p_,T_,ql_):
theta_ = T_ / (p_/p_tilde)**(287.0/1015.0)
return theta_ * exp(-2.47e6 * ql_ / (1015.0 * T_))
def sat_adjst(p_,thetal_,qt_):
'''
Use saturation adjustment scheme to compute temperature and ql given thetal and qt.
:param p: pressure [Pa]
:param thetal: liquid water potential temperature [K]
:param qt: total water specific humidity
:return: T, ql
'''
#Compute temperature
t_1 = thetal_ * (p_/p_tilde)**(287.0/1015.0)
#Compute saturation vapor pressure
pv_star_1 = Th.get_pv_star(t_1)
#Compute saturation mixing ratio
qs_1 = qv_star_c(p_,qt_,pv_star_1)
if qt_ <= qs_1:
#If not saturated return temperature and ql = 0.0
return t_1, 0.0
else:
ql_1 = qt_ - qs_1
f_1 = thetal_ - compute_thetal(p_,t_1,ql_1)
t_2 = t_1 + 2.47e6*ql_1/1015.0
pv_star_2 = Th.get_pv_star(t_2)
qs_2 = qv_star_c(p_,qt_,pv_star_2)
ql_2 = qt_ - qs_2
while fabs(t_2 - t_1) >= 1e-9:
pv_star_2 = Th.get_pv_star(t_2)
qs_2 = qv_star_c(p_,qt_,pv_star_2)
ql_2 = qt_ - qs_2
f_2 = thetal_ - compute_thetal(p_, t_2, ql_2)
t_n = t_2 - f_2 * (t_2 - t_1)/(f_2 - f_1)
t_1 = t_2
t_2 = t_n
f_1 = f_2
return t_2, ql_2
#Generate initial perturbations (here we are generating more than we need)
np.random.seed(Pa.rank)
cdef double [:] theta_pert = np.random.random_sample(Gr.dims.npg)
cdef double theta_pert_
for i in xrange(Gr.dims.nlg[0]):
ishift = istride * i
for j in xrange(Gr.dims.nlg[1]):
jshift = jstride * j
for k in xrange(Gr.dims.nlg[2]):
ijk = ishift + jshift + k
PV.values[ijk + u_varshift] = 0.0
PV.values[ijk + v_varshift] = 0.0
PV.values[ijk + w_varshift] = 0.0
PV.values[ijk + qt_varshift] = qt[k]
#Now set the entropy prognostic variable including a potential temperature perturbation
if Gr.zl_half[k] < 200.0:
theta_pert_ = (theta_pert[ijk] - 0.5)* 0.1
else:
theta_pert_ = 0.0
T,ql = sat_adjst(RS.p0_half[k],thetal[k] + theta_pert_,qt[k])
PV.values[ijk + s_varshift] = Th.entropy(RS.p0_half[k], T, qt[k], ql, 0.0)
if 'e' in PV.name_index:
e_varshift = PV.get_varshift(Gr, 'e')
for i in xrange(Gr.dims.nlg[0]):
ishift = i * Gr.dims.nlg[1] * Gr.dims.nlg[2]
for j in xrange(Gr.dims.nlg[1]):
jshift = j * Gr.dims.nlg[2]
for k in xrange(Gr.dims.nlg[2]):
ijk = ishift + jshift + k
if Gr.zl_half[k] < 200.0:
PV.values[e_varshift + ijk] = 0.0
return
def InitDYCOMS_RF02(namelist,Grid.Grid Gr,PrognosticVariables.PrognosticVariables PV,
ReferenceState.ReferenceState RS, Th, NetCDFIO_Stats NS, ParallelMPI.ParallelMPI Pa, LatentHeat LH ):
# Generate Reference Profiles
RS.Pg = 1017.8 * 100.0
RS.qtg = 9.0/1000.0
RS.u0 = 5.0
RS.v0 = -5.5
cdef double cp_ref = 1004.0
cdef double L_ref = 2.5e6
# Use an exner function with values for Rd, and cp given in Stevens 2004 to compute temperature given $\theta_l$
RS.Tg = 288.3 * (RS.Pg/p_tilde)**(287.0/cp_ref)
RS.initialize(Gr ,Th, NS, Pa)
#Set up $\tehta_l$ and $\qt$ profiles
cdef:
Py_ssize_t i
Py_ssize_t j
Py_ssize_t k
Py_ssize_t ijk, ishift, jshift
Py_ssize_t istride = Gr.dims.nlg[1] * Gr.dims.nlg[2]
Py_ssize_t jstride = Gr.dims.nlg[2]
Py_ssize_t u_varshift = PV.get_varshift(Gr,'u')
Py_ssize_t v_varshift = PV.get_varshift(Gr,'v')
Py_ssize_t w_varshift = PV.get_varshift(Gr,'w')
Py_ssize_t s_varshift = PV.get_varshift(Gr,'s')
Py_ssize_t qt_varshift = PV.get_varshift(Gr,'qt')
double [:] thetal = np.zeros((Gr.dims.nlg[2],),dtype=np.double,order='c')
double [:] qt = np.zeros((Gr.dims.nlg[2],),dtype=np.double,order='c')
double [:] u = np.zeros((Gr.dims.nlg[2],),dtype=np.double,order='c')
double [:] v = np.zeros((Gr.dims.nlg[2],),dtype=np.double,order='c')
for k in xrange(Gr.dims.nlg[2]):
if Gr.zl_half[k] <=795.0:
thetal[k] = 288.3
qt[k] = 9.45/1000.0
if Gr.zl_half[k] > 795.0:
thetal[k] = 295.0 + (Gr.zl_half[k] - 795.0)**(1.0/3.0)
qt[k] = (5.0 - 3.0 * (1.0 - np.exp(-(Gr.zl_half[k] - 795.0)/500.0)))/1000.0
v[k] = -9.0 + 5.6 * Gr.zl_half[k]/1000.0 - RS.v0
u[k] = 3.0 + 4.3*Gr.zl_half[k]/1000.0 - RS.u0
def compute_thetal(p_,T_,ql_):
theta_ = T_ / (p_/p_tilde)**(287.0/cp_ref)
return theta_ * exp(-L_ref * ql_ / (cp_ref * T_))
def sat_adjst(p_,thetal_,qt_):
'''
Use saturation adjustment scheme to compute temperature and ql given thetal and qt.
:param p: pressure [Pa]
:param thetal: liquid water potential temperature [K]
:param qt: total water specific humidity
:return: T, ql
'''
#Compute temperature
t_1 = thetal_ * (p_/p_tilde)**(287.0/cp_ref)
#Compute saturation vapor pressure
pv_star_1 = Th.get_pv_star(t_1)
#Compute saturation mixing ratio
qs_1 = qv_star_c(p_,qt_,pv_star_1)
if qt_ <= qs_1:
#If not saturated return temperature and ql = 0.0
return t_1, 0.0
else:
ql_1 = qt_ - qs_1
f_1 = thetal_ - compute_thetal(p_,t_1,ql_1)
t_2 = t_1 + L_ref*ql_1/cp_ref
pv_star_2 = Th.get_pv_star(t_2)
qs_2 = qv_star_c(p_,qt_,pv_star_2)
ql_2 = qt_ - qs_2
while fabs(t_2 - t_1) >= 1e-9:
pv_star_2 = Th.get_pv_star(t_2)
qs_2 = qv_star_c(p_,qt_,pv_star_2)
ql_2 = qt_ - qs_2
f_2 = thetal_ - compute_thetal(p_, t_2, ql_2)
t_n = t_2 - f_2 * (t_2 - t_1)/(f_2 - f_1)
t_1 = t_2
t_2 = t_n
f_1 = f_2
return t_2, ql_2
#Generate initial perturbations (here we are generating more than we need)
np.random.seed(Pa.rank)
cdef double [:] theta_pert = np.random.random_sample(Gr.dims.npg)
cdef double theta_pert_
for i in xrange(Gr.dims.nlg[0]):
ishift = istride * i
for j in xrange(Gr.dims.nlg[1]):
jshift = jstride * j
for k in xrange(Gr.dims.nlg[2]):
ijk = ishift + jshift + k
PV.values[ijk + u_varshift] = u[k]
PV.values[ijk + v_varshift] = v[k]
PV.values[ijk + w_varshift] = 0.0
PV.values[ijk + qt_varshift] = qt[k]
#Now set the entropy prognostic variable including a potential temperature perturbation
if Gr.zl_half[k] < 795.0:
theta_pert_ = (theta_pert[ijk] - 0.5)* 0.1
else:
theta_pert_ = 0.0
T,ql = sat_adjst(RS.p0_half[k],thetal[k] + theta_pert_,qt[k])
PV.values[ijk + s_varshift] = Th.entropy(RS.p0_half[k], T, qt[k], ql, 0.0)
return
def InitSmoke(namelist,Grid.Grid Gr,PrognosticVariables.PrognosticVariables PV,
ReferenceState.ReferenceState RS, Th, NetCDFIO_Stats NS, ParallelMPI.ParallelMPI Pa, LatentHeat LH ):
'''
Initialization for the smoke cloud case
Bretherton, C. S., and coauthors, 1999:
An intercomparison of radiatively- driven entrainment and turbulence in a smoke cloud,
as simulated by different numerical models. Quart. J. Roy. Meteor. Soc., 125, 391-423. Full text copy.
:param Gr:
:param PV:
:param RS:
:param Th:
:param NS:
:param Pa:
:return:
'''
RS.Pg = 1000.0 * 100.0
RS.qtg = 0.0
RS.u0 = 0.0
RS.v0 = 0.0
RS.Tg = 288.0
RS.initialize(Gr ,Th, NS, Pa)
#Get the variable number for each of the velocity components
np.random.seed(Pa.rank)
cdef:
Py_ssize_t u_varshift = PV.get_varshift(Gr, 'u')
Py_ssize_t v_varshift = PV.get_varshift(Gr, 'v')
Py_ssize_t w_varshift = PV.get_varshift(Gr, 'w')
Py_ssize_t s_varshift = PV.get_varshift(Gr, 's')
Py_ssize_t smoke_varshift = PV.get_varshift(Gr, 'smoke')
Py_ssize_t i,j,k
Py_ssize_t ishift, jshift, e_varshift
Py_ssize_t ijk
double [:] theta = np.empty((Gr.dims.nlg[2]), dtype=np.double, order='c')
double [:] smoke = np.empty((Gr.dims.nlg[2]), dtype=np.double, order='c')
double t
#Generate initial perturbations (here we are generating more than we need)
cdef double [:] theta_pert = np.random.random_sample(Gr.dims.npg)
cdef double theta_pert_
for k in xrange(Gr.dims.nlg[2]):
if Gr.zl_half[k] <= 687.5:
theta[k] = 288.0
smoke[k] = 1.0
elif Gr.zl_half[k] >= 687.5 and Gr.zl_half[k] <= 712.5:
theta[k] = 288.0 + (Gr.zl_half[k] - 687.5) * 0.28
smoke[k] = 1.0 - 0.04 * (Gr.zl_half[k] - 687.5)
print k, Gr.zl_half[k], smoke[k]
else:
theta[k] = 295.0 + (Gr.zl_half[k] - 712.5) * 1e-4
smoke[k] = 0.0
cdef double [:] p0 = RS.p0_half
#Now loop and set the initial condition
for i in xrange(Gr.dims.nlg[0]):
ishift = i * Gr.dims.nlg[1] * Gr.dims.nlg[2]
for j in xrange(Gr.dims.nlg[1]):
jshift = j * Gr.dims.nlg[2]
for k in xrange(Gr.dims.nlg[2]):
ijk = ishift + jshift + k
PV.values[u_varshift + ijk] = 0.0 - RS.u0
PV.values[v_varshift + ijk] = 0.0 - RS.v0
PV.values[w_varshift + ijk] = 0.0
#Now set the entropy prognostic variable including a potential temperature perturbation
if Gr.zl_half[k] < 700.0:
theta_pert_ = (theta_pert[ijk] - 0.5)* 0.1
else:
theta_pert_ = 0.0
t = (theta[k] + theta_pert_)*exner_c(RS.p0_half[k])
PV.values[s_varshift + ijk] = Th.entropy(RS.p0_half[k],t,0.0,0.0,0.0)
PV.values[smoke_varshift + ijk] = smoke[k]
if 'e' in PV.name_index:
e_varshift = PV.get_varshift(Gr, 'e')
for i in xrange(Gr.dims.nlg[0]):
ishift = i * Gr.dims.nlg[1] * Gr.dims.nlg[2]
for j in xrange(Gr.dims.nlg[1]):
jshift = j * Gr.dims.nlg[2]
for k in xrange(Gr.dims.nlg[2]):
ijk = ishift + jshift + k
if Gr.zl_half[k] < 700.0:
PV.values[e_varshift + ijk] = 0.1
else:
PV.values[e_varshift + ijk] = 0.0
return
def InitRico(namelist,Grid.Grid Gr,PrognosticVariables.PrognosticVariables PV,
ReferenceState.ReferenceState RS, Th, NetCDFIO_Stats NS, ParallelMPI.ParallelMPI Pa, LatentHeat LH ):
#First generate the reference profiles
RS.Pg = 1.0154e5 #Pressure at ground
RS.Tg = 299.8 #Temperature at ground
pvg = Th.get_pv_star(RS.Tg)
RS.qtg = eps_v * pvg/(RS.Pg - pvg) #Total water mixing ratio at surface = qsat
RS.initialize(Gr, Th, NS, Pa)
#Get the variable number for each of the velocity components
np.random.seed(Pa.rank)
cdef:
Py_ssize_t u_varshift = PV.get_varshift(Gr,'u')
Py_ssize_t v_varshift = PV.get_varshift(Gr,'v')
Py_ssize_t w_varshift = PV.get_varshift(Gr,'w')
Py_ssize_t s_varshift = PV.get_varshift(Gr,'s')
Py_ssize_t qt_varshift = PV.get_varshift(Gr,'qt')
Py_ssize_t i,j,k
Py_ssize_t ishift, jshift
Py_ssize_t ijk, e_varshift
double temp
double qt_
double [:] theta = np.empty((Gr.dims.nlg[2]),dtype=np.double,order='c')
double [:] qt = np.empty((Gr.dims.nlg[2]),dtype=np.double,order='c')
double [:] u = np.empty((Gr.dims.nlg[2]),dtype=np.double,order='c')
double [:] v = np.empty((Gr.dims.nlg[2]),dtype=np.double,order='c')
Py_ssize_t count
theta_pert = (np.random.random_sample(Gr.dims.npg )-0.5)*0.1
qt_pert = (np.random.random_sample(Gr.dims.npg )-0.5) * 2.5e-5
for k in xrange(Gr.dims.nlg[2]):
#Set Thetal profile
if Gr.zl_half[k] <= 740.0:
theta[k] = 297.9
else:
theta[k] = 297.9 + (317.0-297.9)/(4000.0-740.0)*(Gr.zl_half[k] - 740.0)
#Set qt profile
if Gr.zl_half[k] <= 740.0:
qt[k] = 16.0 + (13.8 - 16.0)/740.0 * Gr.zl_half[k]
elif Gr.zl_half[k] > 740.0 and Gr.zl_half[k] <= 3260.0:
qt[k] = 13.8 + (2.4 - 13.8)/(3260.0-740.0) * (Gr.zl_half[k] - 740.0)
else:
qt[k] = 2.4 + (1.8-2.4)/(4000.0-3260.0)*(Gr.zl_half[k] - 3260.0)
#Change units to kg/kg
qt[k]/= 1000.0
#Set u profile
u[k] = -9.9 + 2.0e-3 * Gr.zl_half[k]
#set v profile
v[k] = -3.8
#Set velocities for Galilean transformation
RS.v0 = -3.8
RS.u0 = 0.5 * (np.amax(u)+np.amin(u))
#Now loop and set the initial condition
#First set the velocities
count = 0
for i in xrange(Gr.dims.nlg[0]):
ishift = i * Gr.dims.nlg[1] * Gr.dims.nlg[2]
for j in xrange(Gr.dims.nlg[1]):
jshift = j * Gr.dims.nlg[2]
for k in xrange(Gr.dims.nlg[2]):
ijk = ishift + jshift + k
PV.values[u_varshift + ijk] = u[k] - RS.u0
PV.values[v_varshift + ijk] = v[k] - RS.v0
PV.values[w_varshift + ijk] = 0.0
if Gr.zl_half[k] <= 740.0:
temp = (theta[k] + (theta_pert[count])) * exner_c(RS.p0_half[k])
qt_ = qt[k]+qt_pert[count]
else:
temp = (theta[k]) * exner_c(RS.p0_half[k])
qt_ = qt[k]
PV.values[s_varshift + ijk] = Th.entropy(RS.p0_half[k],temp,qt_,0.0,0.0)
PV.values[qt_varshift + ijk] = qt_
count += 1
if 'e' in PV.name_index:
e_varshift = PV.get_varshift(Gr, 'e')
for i in xrange(Gr.dims.nlg[0]):
ishift = i * Gr.dims.nlg[1] * Gr.dims.nlg[2]
for j in xrange(Gr.dims.nlg[1]):
jshift = j * Gr.dims.nlg[2]
for k in xrange(Gr.dims.nlg[2]):
ijk = ishift + jshift + k
if Gr.zl_half[k] <= 740.0:
PV.values[e_varshift + ijk] = 0.1
return
def InitIsdac(namelist, Grid.Grid Gr,PrognosticVariables.PrognosticVariables PV,
ReferenceState.ReferenceState RS, Th, NetCDFIO_Stats NS, ParallelMPI.ParallelMPI Pa, LatentHeat LH):
'''
Initialize the ISDAC case described in Ovchinnikov et al. (2014):
Intercomparison of large-eddy simulations of Arctic mixed-phase clouds:
Importance of ice size distribution assumptions
:param Gr: Grid cdef extension class
:param PV: PrognosticVariables cdef extension class
:param RS: ReferenceState cdef extension class
:param Th: Thermodynamics class
:return: None
'''
#First generate the reference profiles
RS.Pg = 1.02e5 #Pressure at ground
RS.Tg = 267.0 #Temperature at ground
RS.qtg = 0.0015 #Total water mixing ratio at surface
RS.initialize(Gr, Th, NS, Pa)
#Get the variable number for each of the velocity components
cdef:
Py_ssize_t i
Py_ssize_t j
Py_ssize_t k
Py_ssize_t ijk, ishift, jshift
Py_ssize_t istride = Gr.dims.nlg[1] * Gr.dims.nlg[2]
Py_ssize_t jstride = Gr.dims.nlg[2]
Py_ssize_t u_varshift = PV.get_varshift(Gr,'u')
Py_ssize_t v_varshift = PV.get_varshift(Gr,'v')
Py_ssize_t w_varshift = PV.get_varshift(Gr,'w')
Py_ssize_t s_varshift = PV.get_varshift(Gr,'s')
Py_ssize_t qt_varshift = PV.get_varshift(Gr,'qt')
double [:] thetal = np.zeros((Gr.dims.nlg[2],),dtype=np.double,order='c')
double [:] qt = np.zeros((Gr.dims.nlg[2],),dtype=np.double,order='c')
double [:] v = np.zeros((Gr.dims.nlg[2],),dtype=np.double,order='c')
for k in xrange(Gr.dims.nlg[2]):
#Set thetal and qt profile
if Gr.zl_half[k] < 400.0:
thetal[k] = 265.0 + 0.004 * (Gr.zl_half[k] - 400.0)
qt[k] = 1.5 - 0.00075 * (Gr.zl_half[k] - 400.0)
if Gr.zl_half[k] >= 400.0 and Gr.zl_half[k] < 825.0:
thetal[k] = 265.0
qt[k] = 1.5
if Gr.zl_half[k] >= 825.0 and Gr.zl_half[k] < 2045.0:
thetal[k] = 266.0 + (Gr.zl_half[k] - 825.0) ** 0.3
qt[k] = 1.2
if Gr.zl_half[k] >= 2045.0:
thetal[k] = 271.0 + (Gr.zl_half[k] - 2000.0) ** 0.33
qt[k] = 0.5 - 0.000075 * (Gr.zl_half[k] - 2045.0)
#Change units to kg/kg
qt[k]/= 1000.0
#Set v profile
v[k] = -2.0 + 0.003 * Gr.zl_half[k]
#Set velocities for Galilean transformation
RS.u0 = -7.0
RS.v0 = 0.5 * (np.amax(v)+np.amin(v))
#Generate initial perturbations (here we are generating more than we need)
cdef double [:] theta_pert = np.random.random_sample(Gr.dims.npg)
cdef double theta_pert_
#Now loop and set the initial condition
for i in xrange(Gr.dims.nlg[0]):
ishift = istride * i
for j in xrange(Gr.dims.nlg[1]):
jshift = jstride * j
for k in xrange(Gr.dims.nlg[2]):
ijk = ishift + jshift + k
PV.values[ijk + u_varshift] = -7.0 - RS.u0
PV.values[ijk + v_varshift] = v[k] - RS.v0
PV.values[ijk + w_varshift] = 0.0
PV.values[ijk + qt_varshift] = qt[k]
#Now set the entropy prognostic variable including a potential temperature perturbation
if Gr.zl_half[k] < 825.0:
theta_pert_ = (theta_pert[ijk] - 0.5)* 0.1
else:
theta_pert_ = 0.0
T,ql = sat_adjst(RS.p0_half[k],thetal[k] + theta_pert_,qt[k], Th)
PV.values[ijk + s_varshift] = Th.entropy(RS.p0_half[k], T, qt[k], ql, 0.0)
return
def InitIsdacCC(namelist, Grid.Grid Gr,PrognosticVariables.PrognosticVariables PV,
ReferenceState.ReferenceState RS, Th, NetCDFIO_Stats NS, ParallelMPI.ParallelMPI Pa, LatentHeat LH):
'''
Idealized ISDAC setup initialization based on the ISDAC case described in Ovchinnikov et al. (2014):
Intercomparison of large-eddy simulations of Arctic mixed-phase clouds:
Importance of ice size distribution assumptions
:param Gr: Grid cdef extension class
:param PV: PrognosticVariables cdef extension class
:param RS: ReferenceState cdef extension class
:param Th: Thermodynamics class
:return: None