cleopy 0.57.4__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- cleopy/__init__.py +22 -0
- cleopy/cxx2py.py +136 -0
- cleopy/editconfigfile.py +61 -0
- cleopy/gbxboundariesbinary_src/__init__.py +16 -0
- cleopy/gbxboundariesbinary_src/create_gbxboundaries.py +197 -0
- cleopy/gbxboundariesbinary_src/read_gbxboundaries.py +350 -0
- cleopy/geninitconds.py +144 -0
- cleopy/initsuperdropsbinary_src/__init__.py +15 -0
- cleopy/initsuperdropsbinary_src/attrsgen.py +206 -0
- cleopy/initsuperdropsbinary_src/crdgens.py +101 -0
- cleopy/initsuperdropsbinary_src/create_initsuperdrops.py +360 -0
- cleopy/initsuperdropsbinary_src/dryrgens.py +31 -0
- cleopy/initsuperdropsbinary_src/probdists.py +239 -0
- cleopy/initsuperdropsbinary_src/read_initsuperdrops.py +465 -0
- cleopy/initsuperdropsbinary_src/rgens.py +81 -0
- cleopy/readbinary.py +120 -0
- cleopy/readconfigfile.py +61 -0
- cleopy/sdmout_src/__init__.py +16 -0
- cleopy/sdmout_src/massmoms.py +83 -0
- cleopy/sdmout_src/pygbxsdat.py +59 -0
- cleopy/sdmout_src/pysetuptxt.py +90 -0
- cleopy/sdmout_src/pyzarr.py +158 -0
- cleopy/sdmout_src/sdtracing.py +141 -0
- cleopy/sdmout_src/superdrops.py +428 -0
- cleopy/sdmout_src/supersdata.py +228 -0
- cleopy/sdmout_src/thermodata.py +146 -0
- cleopy/sdmout_src/thermoeqns.py +129 -0
- cleopy/sdmout_src/timedata.py +54 -0
- cleopy/thermobinary_src/__init__.py +16 -0
- cleopy/thermobinary_src/create_thermodynamics.py +256 -0
- cleopy/thermobinary_src/read_thermodynamics.py +498 -0
- cleopy/thermobinary_src/thermodyngen.py +56 -0
- cleopy/thermobinary_src/thermogen.py +466 -0
- cleopy/thermobinary_src/windsgen.py +228 -0
- cleopy/writebinary.py +218 -0
- cleopy-0.57.4.dist-info/METADATA +47 -0
- cleopy-0.57.4.dist-info/RECORD +39 -0
- cleopy-0.57.4.dist-info/WHEEL +4 -0
- cleopy-0.57.4.dist-info/licenses/LICENSE.md +43 -0
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
"""
|
|
2
|
+
----- CLEO -----
|
|
3
|
+
File: attrsgen.py
|
|
4
|
+
Project: initsuperdropsbinary_src
|
|
5
|
+
Created Date: Friday 13th October 2023
|
|
6
|
+
Author: Clara Bayley (CB)
|
|
7
|
+
Additional Contributors:
|
|
8
|
+
-----
|
|
9
|
+
License: BSD 3-Clause "New" or "Revised" License
|
|
10
|
+
https://opensource.org/licenses/BSD-3-Clause
|
|
11
|
+
-----
|
|
12
|
+
Copyright (c) 2023 MPI-M, Clara Bayley
|
|
13
|
+
-----
|
|
14
|
+
File Description:
|
|
15
|
+
attrsgen generates multiple superdroplet
|
|
16
|
+
attributes given individual generators
|
|
17
|
+
"""
|
|
18
|
+
|
|
19
|
+
import numpy as np
|
|
20
|
+
|
|
21
|
+
from ..gbxboundariesbinary_src import read_gbxboundaries as rgrid
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
class AttrsGenerator:
|
|
25
|
+
"""class for functions to generate attributes of
|
|
26
|
+
superdroplets given the generators for independent
|
|
27
|
+
attributes e.g. for radius and coord3 in substantation
|
|
28
|
+
of class"""
|
|
29
|
+
|
|
30
|
+
def __init__(
|
|
31
|
+
self, radiigen, dryradiigen, xiprobdist, coord3gen, coord1gen, coord2gen
|
|
32
|
+
):
|
|
33
|
+
self.radiigen = radiigen # generates radius (solute + water)
|
|
34
|
+
self.dryradiigen = dryradiigen # generates dry radius (-> solute mass)
|
|
35
|
+
self.xiprobdist = xiprobdist # droplet size distribution (-> multiplicity)
|
|
36
|
+
|
|
37
|
+
self.coord3gen = coord3gen # generates spatial coordinate
|
|
38
|
+
self.coord1gen = coord1gen
|
|
39
|
+
self.coord2gen = coord2gen
|
|
40
|
+
|
|
41
|
+
self.ncoordsgen = sum(x is not None for x in [coord3gen, coord2gen, coord1gen])
|
|
42
|
+
|
|
43
|
+
def mass_solutes(self, radii, RHO_SOL):
|
|
44
|
+
"""return the mass [Kg] of the solute in superdroplets given their
|
|
45
|
+
dry radii [m] and solute density [Kg m^3]"""
|
|
46
|
+
|
|
47
|
+
dryradii = self.dryradiigen(radii) # [m]
|
|
48
|
+
dryradii = np.where(radii < dryradii, radii, dryradii)
|
|
49
|
+
msols = 4.0 / 3.0 * np.pi * (dryradii**3) * RHO_SOL
|
|
50
|
+
|
|
51
|
+
return msols # [Kg]
|
|
52
|
+
|
|
53
|
+
def multiplicities(self, radii, NUMCONC, samplevol):
|
|
54
|
+
"""Calculate the multiplicity of the dry radius of each
|
|
55
|
+
superdroplet given it's probability such that the total number
|
|
56
|
+
concentration [m^-3] of real droplets in the volume, vol, [m^3]
|
|
57
|
+
is about 'numconc'. Raise an error if any of the calculated
|
|
58
|
+
mulitiplicities are zero"""
|
|
59
|
+
|
|
60
|
+
totxi = NUMCONC * samplevol
|
|
61
|
+
prob = self.xiprobdist(radii, totxi) # normalised prob distrib
|
|
62
|
+
xi = np.rint(prob * totxi)
|
|
63
|
+
|
|
64
|
+
if any(xi == 0):
|
|
65
|
+
num = len(xi[xi == 0])
|
|
66
|
+
errmsg = (
|
|
67
|
+
"ERROR, "
|
|
68
|
+
+ str(num)
|
|
69
|
+
+ " out of "
|
|
70
|
+
+ str(len(xi))
|
|
71
|
+
+ " SDs"
|
|
72
|
+
+ " created with multiplicity = 0. Consider increasing numconc"
|
|
73
|
+
+ " or changing range of radii sampled."
|
|
74
|
+
)
|
|
75
|
+
raise ValueError(errmsg)
|
|
76
|
+
|
|
77
|
+
return np.array(xi, dtype=np.uint)
|
|
78
|
+
|
|
79
|
+
def check_coordsgen_matches_modeldimension(self, nspacedims):
|
|
80
|
+
if nspacedims != self.ncoordsgen:
|
|
81
|
+
errmsg = (
|
|
82
|
+
str(self.ncoordsgen)
|
|
83
|
+
+ " coord generators specified "
|
|
84
|
+
+ "but nspacedims = "
|
|
85
|
+
+ str(nspacedims)
|
|
86
|
+
)
|
|
87
|
+
raise ValueError(errmsg)
|
|
88
|
+
|
|
89
|
+
def check_totalnumconc(
|
|
90
|
+
self,
|
|
91
|
+
multiplicities,
|
|
92
|
+
NUMCONC,
|
|
93
|
+
samplevol,
|
|
94
|
+
numconc_tolerance,
|
|
95
|
+
):
|
|
96
|
+
"""check number concentration of real droplets calculated from
|
|
97
|
+
multiplicities is same as input value for number conc. within fractional difference (error)
|
|
98
|
+
given by the numconc_tolerance.
|
|
99
|
+
Also check the total number of real droplets lies within 0.1% or more
|
|
100
|
+
of the expected value given the input number conc and sample volume"""
|
|
101
|
+
nreals = np.rint(NUMCONC * samplevol)
|
|
102
|
+
calcnreals = np.rint(np.sum(multiplicities))
|
|
103
|
+
calcnumconc = np.rint(calcnreals / samplevol)
|
|
104
|
+
|
|
105
|
+
if abs(np.rint(NUMCONC) - calcnumconc) / NUMCONC > numconc_tolerance:
|
|
106
|
+
errmsg = (
|
|
107
|
+
"total real droplet concentration"
|
|
108
|
+
+ " {:0g} != numconc, {:0g} within error tolerance".format(
|
|
109
|
+
calcnumconc, NUMCONC
|
|
110
|
+
)
|
|
111
|
+
)
|
|
112
|
+
raise ValueError(errmsg)
|
|
113
|
+
|
|
114
|
+
nreals_tolerance = max(0.001, numconc_tolerance)
|
|
115
|
+
if abs(nreals - calcnreals) / nreals > nreals_tolerance:
|
|
116
|
+
errmsg = "total no. real droplets, {:0g},".format(
|
|
117
|
+
calcnreals
|
|
118
|
+
) + " not consistent with sample volume {:.3g} m^3".format(samplevol)
|
|
119
|
+
raise ValueError(errmsg)
|
|
120
|
+
|
|
121
|
+
def print_totalconc(self, multiplicities, radii, mass_solutes, RHO_SOL, samplevol):
|
|
122
|
+
"""print statement of total num conc and mass conc"""
|
|
123
|
+
|
|
124
|
+
def totmass(radius, msol, RHO_SOL):
|
|
125
|
+
"""total mass of droplets represented by a superdroplet
|
|
126
|
+
droplet totmass = mass of water + solute"""
|
|
127
|
+
|
|
128
|
+
RHO_L = 998.203 # density of liquid water [kg/m^3]
|
|
129
|
+
massconst = 4.0 / 3.0 * np.pi * radius * radius * radius * RHO_L
|
|
130
|
+
density_factor = 1.0 - RHO_L / RHO_SOL
|
|
131
|
+
totmass = msol * density_factor + massconst
|
|
132
|
+
|
|
133
|
+
return totmass * 1000 # [g]
|
|
134
|
+
|
|
135
|
+
numconc = np.sum(multiplicities) / samplevol / 1e6 # [cm^-3]
|
|
136
|
+
totmass = np.sum(totmass(radii, mass_solutes, RHO_SOL) * multiplicities)
|
|
137
|
+
massconc = totmass / samplevol # [g/m^3]
|
|
138
|
+
|
|
139
|
+
msg = (
|
|
140
|
+
"--- total droplet concentration = "
|
|
141
|
+
+ "{:1g}cm^-3 => {:.1g}g/m^3".format(numconc, massconc)
|
|
142
|
+
+ ", in {:.3g}m^3 volume --- ".format(samplevol)
|
|
143
|
+
)
|
|
144
|
+
|
|
145
|
+
print(msg)
|
|
146
|
+
|
|
147
|
+
def generate_attributes(
|
|
148
|
+
self,
|
|
149
|
+
nsupers,
|
|
150
|
+
RHO_SOL,
|
|
151
|
+
NUMCONC,
|
|
152
|
+
gridboxbounds,
|
|
153
|
+
numconc_tolerance=0.0,
|
|
154
|
+
isprint=False,
|
|
155
|
+
):
|
|
156
|
+
"""generate superdroplets (SDs) attributes that have dimensions
|
|
157
|
+
by calling the appropraite generating functions"""
|
|
158
|
+
|
|
159
|
+
gbxvol = rgrid.calc_domainvol(
|
|
160
|
+
gridboxbounds[0:2], gridboxbounds[2:4], gridboxbounds[4:]
|
|
161
|
+
) # [m^3]
|
|
162
|
+
radii = self.radiigen(nsupers) # [m]
|
|
163
|
+
|
|
164
|
+
mass_solutes = self.mass_solutes(radii, RHO_SOL) # [Kg]
|
|
165
|
+
|
|
166
|
+
multiplicities = self.multiplicities(radii, NUMCONC, gbxvol)
|
|
167
|
+
|
|
168
|
+
if nsupers > 0:
|
|
169
|
+
self.check_totalnumconc(multiplicities, NUMCONC, gbxvol, numconc_tolerance)
|
|
170
|
+
if isprint:
|
|
171
|
+
self.print_totalconc(
|
|
172
|
+
multiplicities, radii, mass_solutes, RHO_SOL, gbxvol
|
|
173
|
+
)
|
|
174
|
+
|
|
175
|
+
return multiplicities, radii, mass_solutes # units [], [m], [Kg], [m]
|
|
176
|
+
|
|
177
|
+
def generate_coords(self, nsupers, nspacedims, gridboxbounds):
|
|
178
|
+
"""generate superdroplets (SDs) attributes that have dimensions
|
|
179
|
+
by calling the appropraite generating functions"""
|
|
180
|
+
|
|
181
|
+
self.check_coordsgen_matches_modeldimension(nspacedims)
|
|
182
|
+
|
|
183
|
+
coord3, coord1, coord2 = np.array([]), np.array([]), np.array([])
|
|
184
|
+
|
|
185
|
+
if self.coord3gen:
|
|
186
|
+
coord3range = [
|
|
187
|
+
gridboxbounds[0],
|
|
188
|
+
gridboxbounds[1],
|
|
189
|
+
] # [min,max] coord3 to sample within
|
|
190
|
+
coord3 = self.coord3gen(nsupers, coord3range)
|
|
191
|
+
|
|
192
|
+
if self.coord1gen:
|
|
193
|
+
coord1range = [
|
|
194
|
+
gridboxbounds[2],
|
|
195
|
+
gridboxbounds[3],
|
|
196
|
+
] # [min,max] coord1 to sample within
|
|
197
|
+
coord1 = self.coord1gen(nsupers, coord1range)
|
|
198
|
+
|
|
199
|
+
if self.coord2gen:
|
|
200
|
+
coord2range = [
|
|
201
|
+
gridboxbounds[4],
|
|
202
|
+
gridboxbounds[5],
|
|
203
|
+
] # [min,max] coord2 to sample within
|
|
204
|
+
coord2 = self.coord2gen(nsupers, coord2range)
|
|
205
|
+
|
|
206
|
+
return coord3, coord1, coord2 # units [m], [m], [m]
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
"""
|
|
2
|
+
----- CLEO -----
|
|
3
|
+
File: crdgens.py
|
|
4
|
+
Project: initsuperdropsbinary_src
|
|
5
|
+
Created Date: Friday 13th October 2023
|
|
6
|
+
Author: Clara Bayley (CB)
|
|
7
|
+
Additional Contributors:
|
|
8
|
+
-----
|
|
9
|
+
License: BSD 3-Clause "New" or "Revised" License
|
|
10
|
+
https://opensource.org/licenses/BSD-3-Clause
|
|
11
|
+
-----
|
|
12
|
+
Copyright (c) 2023 MPI-M, Clara Bayley
|
|
13
|
+
-----
|
|
14
|
+
File Description:
|
|
15
|
+
various ways of generating spatial
|
|
16
|
+
coordinates for superdroplet initial
|
|
17
|
+
conditions
|
|
18
|
+
"""
|
|
19
|
+
|
|
20
|
+
import numpy as np
|
|
21
|
+
|
|
22
|
+
from ..gbxboundariesbinary_src import read_gbxboundaries as rgrid
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def nsupers_at_domain_base(grid_filename, constants_filename, nsupers, zlim):
|
|
26
|
+
"""create dict for sd initialisation where nsupers
|
|
27
|
+
only occur in gridboxes with upper bound <= zlim"""
|
|
28
|
+
|
|
29
|
+
COORD0 = rgrid.get_COORD0_from_constsfile(constants_filename)
|
|
30
|
+
gbxbounds, ndims = rgrid.read_dimless_gbxboundaries_binary(
|
|
31
|
+
grid_filename, COORD0=COORD0, return_ndims=True, isprint=False
|
|
32
|
+
)
|
|
33
|
+
nsupersdict = {}
|
|
34
|
+
for ii in gbxbounds.keys():
|
|
35
|
+
gbx_zupper = gbxbounds[ii][1] # z upper bound of gridbox
|
|
36
|
+
if gbx_zupper <= zlim:
|
|
37
|
+
nsupersdict[ii] = nsupers
|
|
38
|
+
else:
|
|
39
|
+
nsupersdict[ii] = 0
|
|
40
|
+
|
|
41
|
+
return nsupersdict
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def nsupers_at_domain_top(grid_filename, constants_filename, nsupers, zlim):
|
|
45
|
+
"""create dict for sd initialisation where nsupers
|
|
46
|
+
only occur in gridboxes with lower bound >= zlim"""
|
|
47
|
+
|
|
48
|
+
COORD0 = rgrid.get_COORD0_from_constsfile(constants_filename)
|
|
49
|
+
gbxbounds, ndims = rgrid.read_dimless_gbxboundaries_binary(
|
|
50
|
+
grid_filename, COORD0=COORD0, return_ndims=True, isprint=False
|
|
51
|
+
)
|
|
52
|
+
nsupersdict = {}
|
|
53
|
+
for ii in gbxbounds.keys():
|
|
54
|
+
gbx_zlower = gbxbounds[ii][0] # z lower bound of gridbox
|
|
55
|
+
if gbx_zlower >= zlim:
|
|
56
|
+
nsupersdict[ii] = nsupers
|
|
57
|
+
else:
|
|
58
|
+
nsupersdict[ii] = 0
|
|
59
|
+
|
|
60
|
+
return nsupersdict
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
class MonoCoordGen:
|
|
64
|
+
"""method to generate superdroplets with
|
|
65
|
+
coord all equal to coord0"""
|
|
66
|
+
|
|
67
|
+
def __init__(self, coord0):
|
|
68
|
+
self.coord0 = coord0
|
|
69
|
+
|
|
70
|
+
def __call__(self, nsupers, coordrange):
|
|
71
|
+
"""Returns coord for nsupers all
|
|
72
|
+
with the value of coord0"""
|
|
73
|
+
|
|
74
|
+
if self.coord0 >= coordrange[0] and self.coord0 < coordrange[1]:
|
|
75
|
+
attrs = np.full(nsupers, self.coord0)
|
|
76
|
+
else:
|
|
77
|
+
attrs = np.array([])
|
|
78
|
+
|
|
79
|
+
return attrs
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
class SampleCoordGen:
|
|
83
|
+
"""method to generate 'nsupers'
|
|
84
|
+
no. of superdroplets' coord [m]
|
|
85
|
+
by sampling in range bewteen coordspan"""
|
|
86
|
+
|
|
87
|
+
def __init__(self, random):
|
|
88
|
+
self.random = random
|
|
89
|
+
|
|
90
|
+
def __call__(self, nsupers, coordrange):
|
|
91
|
+
"""Returns coord3 for nsupers
|
|
92
|
+
sampled from coord3span [m]"""
|
|
93
|
+
|
|
94
|
+
if not self.random:
|
|
95
|
+
coord = np.linspace(coordrange[0], coordrange[1], nsupers, endpoint=False)
|
|
96
|
+
else:
|
|
97
|
+
coord = np.random.uniform(
|
|
98
|
+
low=coordrange[0], high=coordrange[1], size=nsupers
|
|
99
|
+
)
|
|
100
|
+
|
|
101
|
+
return coord # units [m]
|
|
@@ -0,0 +1,360 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Copyright (c) 2024 MPI-M, Clara Bayley
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
----- CLEO -----
|
|
6
|
+
File: create_initsuperdrops.py
|
|
7
|
+
Project: initsuperdropsbinary_src
|
|
8
|
+
Created Date: Friday 13th October 2023
|
|
9
|
+
Author: Clara Bayley (CB)
|
|
10
|
+
Additional Contributors:
|
|
11
|
+
-----
|
|
12
|
+
License: BSD 3-Clause "New" or "Revised" License
|
|
13
|
+
https://opensource.org/licenses/BSD-3-Clause
|
|
14
|
+
-----
|
|
15
|
+
File Description:
|
|
16
|
+
"""
|
|
17
|
+
|
|
18
|
+
import numpy as np
|
|
19
|
+
from os.path import isfile
|
|
20
|
+
from .. import cxx2py, readconfigfile, writebinary
|
|
21
|
+
from ..gbxboundariesbinary_src.read_gbxboundaries import (
|
|
22
|
+
read_dimless_gbxboundaries_binary,
|
|
23
|
+
)
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
class ManyAttrs:
|
|
27
|
+
"""store for lists of each attribute for all superdroplets"""
|
|
28
|
+
|
|
29
|
+
def __init__(self):
|
|
30
|
+
self.sdgbxindex = []
|
|
31
|
+
self.xi = []
|
|
32
|
+
self.radius = []
|
|
33
|
+
self.msol = []
|
|
34
|
+
self.coord3 = []
|
|
35
|
+
self.coord1 = []
|
|
36
|
+
self.coord2 = []
|
|
37
|
+
|
|
38
|
+
def set_attrlists(self, a, b, c, d, e, f, g):
|
|
39
|
+
self.sdgbxindex = a
|
|
40
|
+
self.xi = b
|
|
41
|
+
self.radius = c
|
|
42
|
+
self.msol = d
|
|
43
|
+
self.coord3 = e
|
|
44
|
+
self.coord1 = f
|
|
45
|
+
self.coord2 = g
|
|
46
|
+
|
|
47
|
+
def extend_attrlists(self, mia):
|
|
48
|
+
"""use an instance of ManyAttrs (mia) to
|
|
49
|
+
extend lists in this instance"""
|
|
50
|
+
self.sdgbxindex.extend(mia.sdgbxindex)
|
|
51
|
+
self.xi.extend(mia.xi)
|
|
52
|
+
self.radius.extend(mia.radius)
|
|
53
|
+
self.msol.extend(mia.msol)
|
|
54
|
+
self.coord3.extend(mia.coord3)
|
|
55
|
+
self.coord1.extend(mia.coord1)
|
|
56
|
+
self.coord2.extend(mia.coord2)
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def initsupers_inputsdict(config_filename, constants_filename):
|
|
60
|
+
"""create values from constants file & config file
|
|
61
|
+
required as inputs to create initial
|
|
62
|
+
superdroplet conditions"""
|
|
63
|
+
|
|
64
|
+
consts = cxx2py.read_cxxconsts_into_floats(constants_filename)
|
|
65
|
+
mconsts = cxx2py.derive_more_floats(consts)
|
|
66
|
+
config = readconfigfile.read_configparams_into_floats(config_filename)
|
|
67
|
+
|
|
68
|
+
inputs = {
|
|
69
|
+
# for creating SD attribute distirbutions
|
|
70
|
+
"nspacedims": int(config["nspacedims"]),
|
|
71
|
+
"RHO_L": consts["RHO_L"], # liquid density [Kg/m^3]
|
|
72
|
+
"RHO_SOL": consts["RHO_SOL"], # solute density [Kg/m^3]
|
|
73
|
+
# for de-dimensionalising attributes
|
|
74
|
+
"R0": consts["R0"], # droplet radius lengthscale [m]
|
|
75
|
+
"RHO0": mconsts["RHO0"], # characteristic density scale [Kg/m^3]
|
|
76
|
+
"MASS0": mconsts["MASS0"], # characteristic mass scale [Kg]
|
|
77
|
+
"COORD0": mconsts["COORD0"], # z coordinate lengthscale [m]
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
return inputs
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
def is_sdgbxindex_correct(gridboxbounds, coord3, coord1, coord2, gbxindex, sdgbxindex):
|
|
84
|
+
"""rasises error if superdroplet coords [m] lie outside gridbox bounds [m]
|
|
85
|
+
or if gridbox index not equal to superdroplet's."""
|
|
86
|
+
|
|
87
|
+
errmsg = None
|
|
88
|
+
for i, coord in enumerate([coord3, coord1, coord2]):
|
|
89
|
+
if (coord < gridboxbounds[2 * i]).any():
|
|
90
|
+
errmsg = (
|
|
91
|
+
"superdroplet coord"
|
|
92
|
+
+ str(i + 1)
|
|
93
|
+
+ " below lower"
|
|
94
|
+
+ " limit of gridbox it's associated with"
|
|
95
|
+
)
|
|
96
|
+
elif (coord >= gridboxbounds[2 * i + 1]).any():
|
|
97
|
+
errmsg = (
|
|
98
|
+
"superdroplet coord"
|
|
99
|
+
+ str(i + 1)
|
|
100
|
+
+ " above upper"
|
|
101
|
+
+ " limit of gridbox it's associated with"
|
|
102
|
+
)
|
|
103
|
+
if errmsg:
|
|
104
|
+
raise ValueError(errmsg)
|
|
105
|
+
|
|
106
|
+
elif (sdgbxindex != gbxindex).any():
|
|
107
|
+
errmsg = (
|
|
108
|
+
"superdroplet gridbox index not the same as"
|
|
109
|
+
+ " gridbox it should be associated with"
|
|
110
|
+
)
|
|
111
|
+
raise ValueError(errmsg)
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
def dimless_superdropsattrs(
|
|
115
|
+
nsupers,
|
|
116
|
+
initattrsgen,
|
|
117
|
+
inputs,
|
|
118
|
+
gbxindex,
|
|
119
|
+
gridboxbounds,
|
|
120
|
+
NUMCONC,
|
|
121
|
+
numconc_tolerance=0.0,
|
|
122
|
+
isprint=False,
|
|
123
|
+
):
|
|
124
|
+
"""use superdroplet attribute generator "initattrsgen"
|
|
125
|
+
and settings from config and consts files to
|
|
126
|
+
make dimensionless superdroplet attributes"""
|
|
127
|
+
|
|
128
|
+
# generate attributes
|
|
129
|
+
sdgbxindex = [gbxindex] * nsupers
|
|
130
|
+
xi, radius, msol = initattrsgen.generate_attributes(
|
|
131
|
+
nsupers,
|
|
132
|
+
inputs["RHO_SOL"],
|
|
133
|
+
NUMCONC,
|
|
134
|
+
gridboxbounds,
|
|
135
|
+
numconc_tolerance=numconc_tolerance,
|
|
136
|
+
isprint=isprint,
|
|
137
|
+
)
|
|
138
|
+
coord3, coord1, coord2 = initattrsgen.generate_coords(
|
|
139
|
+
nsupers, inputs["nspacedims"], gridboxbounds
|
|
140
|
+
)
|
|
141
|
+
is_sdgbxindex_correct(gridboxbounds, coord3, coord1, coord2, gbxindex, sdgbxindex)
|
|
142
|
+
|
|
143
|
+
# de-dimsionalise attributes
|
|
144
|
+
radius = radius / inputs["R0"]
|
|
145
|
+
msol = msol / inputs["MASS0"]
|
|
146
|
+
coord3 = coord3 / inputs["COORD0"]
|
|
147
|
+
coord1 = coord1 / inputs["COORD0"]
|
|
148
|
+
coord2 = coord2 / inputs["COORD0"]
|
|
149
|
+
|
|
150
|
+
attrs4gbx = ManyAttrs()
|
|
151
|
+
attrs4gbx.set_attrlists(sdgbxindex, xi, radius, msol, coord3, coord1, coord2)
|
|
152
|
+
|
|
153
|
+
return attrs4gbx
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
def create_allsuperdropattrs(
|
|
157
|
+
nsupersdict,
|
|
158
|
+
initattrsgen,
|
|
159
|
+
gbxbounds,
|
|
160
|
+
inputs,
|
|
161
|
+
NUMCONC,
|
|
162
|
+
numconc_tolerance=0.0,
|
|
163
|
+
isprint=False,
|
|
164
|
+
):
|
|
165
|
+
"""returns lists for attributes of all SDs in domain called attrs"""
|
|
166
|
+
|
|
167
|
+
attrs = ManyAttrs() # lists of attrs for SDs in domain
|
|
168
|
+
|
|
169
|
+
for gbxindex, gridboxbounds in gbxbounds.items():
|
|
170
|
+
nsupers = nsupersdict[gbxindex]
|
|
171
|
+
attrs4gbx = dimless_superdropsattrs(
|
|
172
|
+
nsupers,
|
|
173
|
+
initattrsgen,
|
|
174
|
+
inputs,
|
|
175
|
+
gbxindex,
|
|
176
|
+
gridboxbounds,
|
|
177
|
+
NUMCONC,
|
|
178
|
+
numconc_tolerance=numconc_tolerance,
|
|
179
|
+
isprint=isprint,
|
|
180
|
+
) # lists of attrs for SDs in gridbox
|
|
181
|
+
|
|
182
|
+
attrs.extend_attrlists(attrs4gbx)
|
|
183
|
+
|
|
184
|
+
return attrs
|
|
185
|
+
|
|
186
|
+
|
|
187
|
+
def set_arraydtype(arr, dtype):
|
|
188
|
+
og = type(arr[0])
|
|
189
|
+
if og != dtype:
|
|
190
|
+
arr = np.array(arr, dtype=dtype)
|
|
191
|
+
|
|
192
|
+
warning = (
|
|
193
|
+
"WARNING! dtype of attributes is being changed!"
|
|
194
|
+
+ " from "
|
|
195
|
+
+ str(og)
|
|
196
|
+
+ " to "
|
|
197
|
+
+ str(dtype)
|
|
198
|
+
)
|
|
199
|
+
raise ValueError(warning)
|
|
200
|
+
|
|
201
|
+
return arr
|
|
202
|
+
|
|
203
|
+
|
|
204
|
+
def ctype_compatible_attrs(attrs):
|
|
205
|
+
"""make list from arrays of SD attributes that are
|
|
206
|
+
compatible with c type expected by SDM e.g. unsigned
|
|
207
|
+
long ints for xi, doubles for radius and msol"""
|
|
208
|
+
|
|
209
|
+
datatypes = [np.uintc, np.uint, np.double, np.double]
|
|
210
|
+
datatypes += [np.double] * 3 # coords datatype
|
|
211
|
+
|
|
212
|
+
attrs.sdgbxindex = list(set_arraydtype(attrs.sdgbxindex, datatypes[0]))
|
|
213
|
+
attrs.xi = list(set_arraydtype(attrs.xi, datatypes[1]))
|
|
214
|
+
attrs.radius = list(set_arraydtype(attrs.radius, datatypes[2]))
|
|
215
|
+
attrs.msol = list(set_arraydtype(attrs.msol, datatypes[3]))
|
|
216
|
+
|
|
217
|
+
datalist = attrs.sdgbxindex + attrs.xi + attrs.radius + attrs.msol
|
|
218
|
+
|
|
219
|
+
if any(attrs.coord3):
|
|
220
|
+
# make coord3 compatible if there is data for it (>= 1-D model)
|
|
221
|
+
attrs.coord3 = list(set_arraydtype(attrs.coord3, datatypes[4]))
|
|
222
|
+
datalist += attrs.coord3
|
|
223
|
+
|
|
224
|
+
if any(attrs.coord1):
|
|
225
|
+
# make coord1 compatible if there is data for it and coord3 (>= 2-D model)
|
|
226
|
+
attrs.coord1 = list(set_arraydtype(attrs.coord1, datatypes[4]))
|
|
227
|
+
datalist += attrs.coord1
|
|
228
|
+
|
|
229
|
+
if any(attrs.coord2):
|
|
230
|
+
# make coord2 compatible if there is data for it and coord3 and coord2 (>= 3-D model)
|
|
231
|
+
attrs.coord2 = list(set_arraydtype(attrs.coord2, datatypes[4]))
|
|
232
|
+
datalist += attrs.coord2
|
|
233
|
+
|
|
234
|
+
return datalist, datatypes
|
|
235
|
+
|
|
236
|
+
|
|
237
|
+
def check_datashape(data, ndata, nspacedims):
|
|
238
|
+
"""make sure each superdroplet attribute in data has length stated
|
|
239
|
+
in ndata and that this length is compatible with the nummber of
|
|
240
|
+
attributes and superdroplets expected given ndata"""
|
|
241
|
+
|
|
242
|
+
err = ""
|
|
243
|
+
if any([n != ndata[0] for n in ndata[: 4 + nspacedims]]):
|
|
244
|
+
err += (
|
|
245
|
+
"\n------ ERROR! -----\n"
|
|
246
|
+
+ "not all variables in data are same length, ndata = "
|
|
247
|
+
+ str(ndata[: 4 + nspacedims])
|
|
248
|
+
+ "\n---------------------\n"
|
|
249
|
+
)
|
|
250
|
+
|
|
251
|
+
if len(data) != np.sum(ndata):
|
|
252
|
+
err += (
|
|
253
|
+
"inconsistent dimensions of data: "
|
|
254
|
+
+ str(np.shape(data))
|
|
255
|
+
+ ", and"
|
|
256
|
+
+ " data per attribute: "
|
|
257
|
+
+ str(ndata)
|
|
258
|
+
+ ". data should be 1D with"
|
|
259
|
+
+ " shape: num_attributes * nsupers. data should be list of"
|
|
260
|
+
+ " [nsupers]*num_attributes."
|
|
261
|
+
)
|
|
262
|
+
|
|
263
|
+
if err:
|
|
264
|
+
raise ValueError(err)
|
|
265
|
+
|
|
266
|
+
|
|
267
|
+
def nsupers_pergridboxdict(nsupers, gbxbounds):
|
|
268
|
+
if isinstance(nsupers, int):
|
|
269
|
+
nsupersdict = {}
|
|
270
|
+
for key in gbxbounds.keys():
|
|
271
|
+
nsupersdict[key] = nsupers
|
|
272
|
+
return nsupersdict
|
|
273
|
+
|
|
274
|
+
elif isinstance(nsupers, dict):
|
|
275
|
+
if nsupers.keys() != gbxbounds.keys():
|
|
276
|
+
errmsg = "keys for nsupers dict don't match gridbox indexes"
|
|
277
|
+
raise ValueError(errmsg)
|
|
278
|
+
else:
|
|
279
|
+
return nsupers
|
|
280
|
+
|
|
281
|
+
else:
|
|
282
|
+
errmsg = "nsupers must be either dict or int"
|
|
283
|
+
raise ValueError(errmsg)
|
|
284
|
+
|
|
285
|
+
|
|
286
|
+
def write_initsuperdrops_binary(
|
|
287
|
+
initsupers_filename,
|
|
288
|
+
initattrsgen,
|
|
289
|
+
config_filename,
|
|
290
|
+
constants_filename,
|
|
291
|
+
grid_filename,
|
|
292
|
+
nsupers,
|
|
293
|
+
NUMCONC,
|
|
294
|
+
numconc_tolerance=0.0,
|
|
295
|
+
isprint=False,
|
|
296
|
+
):
|
|
297
|
+
"""de-dimensionalise attributes in initattrsgen and then write to
|
|
298
|
+
to a binary file, "initsupers_filename", with some metadata"""
|
|
299
|
+
|
|
300
|
+
if not isfile(grid_filename):
|
|
301
|
+
errmsg = (
|
|
302
|
+
"grid_filename not found, but must be"
|
|
303
|
+
+ " created before initsupersfile can be"
|
|
304
|
+
)
|
|
305
|
+
raise ValueError(errmsg)
|
|
306
|
+
|
|
307
|
+
inputs = initsupers_inputsdict(config_filename, constants_filename)
|
|
308
|
+
gbxbounds = read_dimless_gbxboundaries_binary(
|
|
309
|
+
grid_filename, COORD0=inputs["COORD0"], isprint=False
|
|
310
|
+
)
|
|
311
|
+
nsupersdict = nsupers_pergridboxdict(nsupers, gbxbounds)
|
|
312
|
+
|
|
313
|
+
attrs = create_allsuperdropattrs(
|
|
314
|
+
nsupersdict,
|
|
315
|
+
initattrsgen,
|
|
316
|
+
gbxbounds,
|
|
317
|
+
inputs,
|
|
318
|
+
NUMCONC,
|
|
319
|
+
numconc_tolerance=numconc_tolerance,
|
|
320
|
+
isprint=isprint,
|
|
321
|
+
)
|
|
322
|
+
|
|
323
|
+
ndata = [
|
|
324
|
+
len(dt)
|
|
325
|
+
for dt in [
|
|
326
|
+
attrs.sdgbxindex,
|
|
327
|
+
attrs.xi,
|
|
328
|
+
attrs.radius,
|
|
329
|
+
attrs.msol,
|
|
330
|
+
attrs.coord3,
|
|
331
|
+
attrs.coord1,
|
|
332
|
+
attrs.coord2,
|
|
333
|
+
]
|
|
334
|
+
]
|
|
335
|
+
|
|
336
|
+
data, datatypes = ctype_compatible_attrs(attrs)
|
|
337
|
+
check_datashape(data, ndata, inputs["nspacedims"])
|
|
338
|
+
|
|
339
|
+
units = [b" ", b" ", b"m", b"g"]
|
|
340
|
+
units += [b"m"] * 3 # coords units
|
|
341
|
+
|
|
342
|
+
scale_factors = [1.0, 1.0, inputs["R0"], inputs["MASS0"]]
|
|
343
|
+
scale_factors += [inputs["COORD0"]] * 3 # coords scale factors
|
|
344
|
+
scale_factors = np.asarray(scale_factors, dtype=np.double)
|
|
345
|
+
|
|
346
|
+
metastr = "Variables in this file are Superdroplet attributes:"
|
|
347
|
+
if initattrsgen.coord3gen:
|
|
348
|
+
if initattrsgen.coord1gen:
|
|
349
|
+
if initattrsgen.coord2gen:
|
|
350
|
+
metastr += " [sdgbxindex, xi, radius, msol, coord3, coord1, coord2]"
|
|
351
|
+
else:
|
|
352
|
+
metastr += " [sdgbxindex, xi, radius, msol, coord3, coord1]"
|
|
353
|
+
else:
|
|
354
|
+
metastr += " [sdgbxindex, xi, radius, msol, coord3]"
|
|
355
|
+
else:
|
|
356
|
+
metastr += " [sdgbxindex, xi, radius, msol]"
|
|
357
|
+
|
|
358
|
+
writebinary.writebinary(
|
|
359
|
+
initsupers_filename, data, ndata, datatypes, units, scale_factors, metastr
|
|
360
|
+
)
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Copyright (c) 2024 MPI-M, Clara Bayley
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
----- CLEO -----
|
|
6
|
+
File: dryrgens.py
|
|
7
|
+
Project: initsuperdropsbinary_src
|
|
8
|
+
Created Date: Wednesday 27th December 2023
|
|
9
|
+
Author: Clara Bayley (CB)
|
|
10
|
+
Additional Contributors:
|
|
11
|
+
-----
|
|
12
|
+
License: BSD 3-Clause "New" or "Revised" License
|
|
13
|
+
https://opensource.org/licenses/BSD-3-Clause
|
|
14
|
+
-----
|
|
15
|
+
File Description:
|
|
16
|
+
various ways of generating dryradii of
|
|
17
|
+
superdroplets for their initial conditions
|
|
18
|
+
"""
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
class ScaledRadiiGen:
|
|
22
|
+
"""method to generate superdroplet dryradii that
|
|
23
|
+
are the radii divided by a scale factor 'sf'"""
|
|
24
|
+
|
|
25
|
+
def __init__(self, scale_factor):
|
|
26
|
+
self.sf = scale_factor
|
|
27
|
+
|
|
28
|
+
def __call__(self, radii):
|
|
29
|
+
"""Returns dryradii for nsupers [m]"""
|
|
30
|
+
|
|
31
|
+
return radii / self.sf # units [m]
|