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.
Files changed (39) hide show
  1. cleopy/__init__.py +22 -0
  2. cleopy/cxx2py.py +136 -0
  3. cleopy/editconfigfile.py +61 -0
  4. cleopy/gbxboundariesbinary_src/__init__.py +16 -0
  5. cleopy/gbxboundariesbinary_src/create_gbxboundaries.py +197 -0
  6. cleopy/gbxboundariesbinary_src/read_gbxboundaries.py +350 -0
  7. cleopy/geninitconds.py +144 -0
  8. cleopy/initsuperdropsbinary_src/__init__.py +15 -0
  9. cleopy/initsuperdropsbinary_src/attrsgen.py +206 -0
  10. cleopy/initsuperdropsbinary_src/crdgens.py +101 -0
  11. cleopy/initsuperdropsbinary_src/create_initsuperdrops.py +360 -0
  12. cleopy/initsuperdropsbinary_src/dryrgens.py +31 -0
  13. cleopy/initsuperdropsbinary_src/probdists.py +239 -0
  14. cleopy/initsuperdropsbinary_src/read_initsuperdrops.py +465 -0
  15. cleopy/initsuperdropsbinary_src/rgens.py +81 -0
  16. cleopy/readbinary.py +120 -0
  17. cleopy/readconfigfile.py +61 -0
  18. cleopy/sdmout_src/__init__.py +16 -0
  19. cleopy/sdmout_src/massmoms.py +83 -0
  20. cleopy/sdmout_src/pygbxsdat.py +59 -0
  21. cleopy/sdmout_src/pysetuptxt.py +90 -0
  22. cleopy/sdmout_src/pyzarr.py +158 -0
  23. cleopy/sdmout_src/sdtracing.py +141 -0
  24. cleopy/sdmout_src/superdrops.py +428 -0
  25. cleopy/sdmout_src/supersdata.py +228 -0
  26. cleopy/sdmout_src/thermodata.py +146 -0
  27. cleopy/sdmout_src/thermoeqns.py +129 -0
  28. cleopy/sdmout_src/timedata.py +54 -0
  29. cleopy/thermobinary_src/__init__.py +16 -0
  30. cleopy/thermobinary_src/create_thermodynamics.py +256 -0
  31. cleopy/thermobinary_src/read_thermodynamics.py +498 -0
  32. cleopy/thermobinary_src/thermodyngen.py +56 -0
  33. cleopy/thermobinary_src/thermogen.py +466 -0
  34. cleopy/thermobinary_src/windsgen.py +228 -0
  35. cleopy/writebinary.py +218 -0
  36. cleopy-0.57.4.dist-info/METADATA +47 -0
  37. cleopy-0.57.4.dist-info/RECORD +39 -0
  38. cleopy-0.57.4.dist-info/WHEEL +4 -0
  39. cleopy-0.57.4.dist-info/licenses/LICENSE.md +43 -0
@@ -0,0 +1,256 @@
1
+ """
2
+ ----- CLEO -----
3
+ File: create_thermodynamics.py
4
+ Project: thermobinary_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
+ """
16
+
17
+ import numpy as np
18
+ from os.path import isfile
19
+ from .. import cxx2py, readconfigfile, writebinary
20
+ from ..gbxboundariesbinary_src.read_gbxboundaries import (
21
+ read_dimless_gbxboundaries_binary,
22
+ )
23
+
24
+
25
+ def thermoinputsdict(config_filename, constants_filename):
26
+ """create values from constants file & config file
27
+ required as inputs to create initial
28
+ superdroplet conditions"""
29
+
30
+ consts = cxx2py.read_cxxconsts_into_floats(constants_filename)
31
+ mconsts = cxx2py.derive_more_floats(consts)
32
+ config = readconfigfile.read_configparams_into_floats(config_filename)
33
+
34
+ inputs = {
35
+ # for creating thermodynamic profiles
36
+ "G": consts["G"],
37
+ "CP_DRY": consts["CP_DRY"],
38
+ "RHO_DRY": consts["RHO_DRY"], # dry air density [Kg/m^3]
39
+ "RGAS_DRY": mconsts["RGAS_DRY"],
40
+ "RGAS_V": mconsts["RGAS_V"],
41
+ "Mr_ratio": mconsts["Mr_ratio"],
42
+ "COUPLTSTEP": config["COUPLTSTEP"],
43
+ "T_END": config["T_END"],
44
+ # for de-dimensionalising attributes
45
+ "W0": consts["W0"],
46
+ "P0": consts["P0"],
47
+ "TEMP0": consts["TEMP0"],
48
+ "RHO0": mconsts["RHO0"], # characteristic density scale [Kg/m^3]
49
+ "CP0": mconsts["CP0"],
50
+ "COORD0": mconsts["COORD0"], # z coordinate lengthscale [m]
51
+ # for reading dimless thermodynamics
52
+ "nspacedims": config["nspacedims"],
53
+ }
54
+
55
+ inputs["ntime"] = int(np.ceil(inputs["T_END"] / inputs["COUPLTSTEP"])) + 1
56
+
57
+ return inputs
58
+
59
+
60
+ class DimlessThermodynamics:
61
+ def __init__(self, inputs=False, config_filename="", constants_filename=""):
62
+ if not inputs:
63
+ inputs = thermoinputsdict(config_filename, constants_filename)
64
+
65
+ # scale_factors to de-dimensionalise data
66
+ self.PRESS0 = inputs["P0"]
67
+ self.TEMP0 = inputs["TEMP0"]
68
+ self.qvap0 = 1.0
69
+ self.qcond0 = 1.0
70
+ self.VEL0 = inputs["W0"]
71
+
72
+ def makedimless(self, THERMO):
73
+ thermodata = {
74
+ "press": THERMO["PRESS"] / self.PRESS0,
75
+ "temp": THERMO["TEMP"] / self.TEMP0,
76
+ "qvap": THERMO["qvap"],
77
+ "qcond": THERMO["qcond"],
78
+ "wvel": THERMO["WVEL"] / self.VEL0,
79
+ "uvel": THERMO["UVEL"] / self.VEL0,
80
+ "vvel": THERMO["VVEL"] / self.VEL0,
81
+ }
82
+
83
+ sfs = [self.PRESS0, self.TEMP0, 1.0, 1.0]
84
+ sfs += [self.VEL0] * 3
85
+
86
+ return thermodata, sfs
87
+
88
+ def redimensionalise(self, thermo):
89
+ THERMODATA = {
90
+ "press": thermo["press"] * self.PRESS0,
91
+ "temp": thermo["temp"] * self.TEMP0,
92
+ "qvap": thermo["qvap"],
93
+ "qcond": thermo["qcond"],
94
+ }
95
+ if "wvel" in thermo.keys():
96
+ THERMODATA["wvel"] = thermo["wvel"] * self.VEL0
97
+ if "uvel" in thermo.keys():
98
+ THERMODATA["uvel"] = thermo["uvel"] * self.VEL0
99
+ if "vvel" in thermo.keys():
100
+ THERMODATA["vvel"] = thermo["vvel"] * self.VEL0
101
+
102
+ return THERMODATA
103
+
104
+
105
+ def set_arraydtype(arr, dtype):
106
+ if any(arr):
107
+ og = type(arr[0])
108
+ if og != dtype:
109
+ arr = np.array(arr, dtype=dtype)
110
+
111
+ warning = (
112
+ "WARNING! dtype of attributes is being changed!"
113
+ + " from "
114
+ + str(og)
115
+ + " to "
116
+ + str(dtype)
117
+ )
118
+ raise ValueError(warning)
119
+
120
+ return list(arr)
121
+
122
+
123
+ def ctype_compatible_thermodynamics(thermodata):
124
+ """check type of gridbox boundaries data is compatible
125
+ with c type double. If not, change type and raise error"""
126
+
127
+ datatypes = [np.double] * 7
128
+
129
+ for k, key in enumerate(thermodata.keys()):
130
+ thermodata[key] = set_arraydtype(thermodata[key], datatypes[k])
131
+
132
+ return thermodata, datatypes
133
+
134
+
135
+ def check_datashape(thermodata, ndata, ndims, ntime):
136
+ """make sure each superdroplet attribute in data has length stated
137
+ in ndata and that this length is compatible with the nummber of
138
+ attributes and superdroplets expected given ndata"""
139
+
140
+ # expected lengths of data defined on gridbox centres or faces
141
+ cen = int(ntime * np.prod(ndims))
142
+ zface = int(ntime * ndims[2] * ndims[1] * (ndims[0] + 1))
143
+ xface = int(ntime * ndims[2] * (ndims[1] + 1) * ndims[0])
144
+ yface = int(ntime * (ndims[2] + 1) * ndims[1] * ndims[0])
145
+
146
+ vars = ["press", "temp", "qvap", "qcond"]
147
+ for var in vars:
148
+ lenvar = len(thermodata[var])
149
+ if lenvar != cen:
150
+ err = (
151
+ "\n------ ERROR! -----\n"
152
+ + str(lenvar)
153
+ + " "
154
+ + var
155
+ + " in thermodynamics data is not the"
156
+ + " expected length: ntimesteps*ngridboxes = "
157
+ + str(cen)
158
+ + "\n---------------------\n"
159
+ )
160
+ raise ValueError(err)
161
+
162
+ vars = ["wvel", "uvel", "vvel"]
163
+ for var, face in zip(vars, [zface, xface, yface]):
164
+ lenvar = len(thermodata[var])
165
+ if np.logical_and(lenvar, lenvar != face):
166
+ err = (
167
+ "\n------ ERROR! -----\n"
168
+ + str(lenvar)
169
+ + " "
170
+ + var
171
+ + " in thermodynamics data is not the"
172
+ + " expected length: ntimesteps*nfaces = "
173
+ + str(face)
174
+ + "\n---------------------\n"
175
+ )
176
+ raise ValueError(err)
177
+
178
+ lens = [len(d) for d in thermodata.values()]
179
+ if lens != ndata:
180
+ err = (
181
+ "inconsistent dimensions of thermodynamic data: "
182
+ + str(lens)
183
+ + " compared with given lengths: "
184
+ + str(ndata)
185
+ )
186
+ raise ValueError(err)
187
+
188
+
189
+ def write_thermodynamics_binary(
190
+ thermofiles, thermodyngen, config_filename, constants_filename, grid_filename
191
+ ):
192
+ """write binarys for thermodynamic data over time on C staggered
193
+ grid. So that pressure, temperature, qvap and qcond are defined at
194
+ centres of gridboxes, whereas wind velocities are defined at faces"""
195
+
196
+ if not isfile(grid_filename):
197
+ errmsg = (
198
+ "grid_filename not found, but must be"
199
+ + " created before initsupersfile can be"
200
+ )
201
+ raise ValueError(errmsg)
202
+
203
+ inputs = thermoinputsdict(config_filename, constants_filename)
204
+ gbxbounds, ndims = read_dimless_gbxboundaries_binary(
205
+ grid_filename, COORD0=inputs["COORD0"], return_ndims=True, isprint=False
206
+ )
207
+ thermodata = thermodyngen.generate_thermodyn(gbxbounds, ndims, inputs["ntime"])
208
+
209
+ dth = DimlessThermodynamics(inputs=inputs)
210
+ thermodata, scale_factors = dth.makedimless(thermodata)
211
+
212
+ ndata = [len(dt) for dt in thermodata.values()]
213
+
214
+ thermodata, datatypes = ctype_compatible_thermodynamics(thermodata)
215
+ check_datashape(thermodata, ndata, ndims, inputs["ntime"])
216
+
217
+ units = [b"P", b"K", b" ", b" "]
218
+ units += [b"m"] * 3 # velocity units
219
+ scale_factors = np.asarray(scale_factors, dtype=np.double)
220
+
221
+ varat = ["centres"] * 4 + ["z-faces", "x-faces", "y-faces"]
222
+ for v, var in enumerate(thermodata.keys()):
223
+ if thermodata[var] != []:
224
+ metastr = (
225
+ "This file is flattened array of "
226
+ + var
227
+ + " variable"
228
+ + " for "
229
+ + str(inputs["ntime"])
230
+ + " timesteps defined at"
231
+ + " grid "
232
+ + varat[v]
233
+ + " for grid with dims: "
234
+ + str(ndims)
235
+ + " (ie. file contains "
236
+ + str(ndata[v])
237
+ + " datapoints"
238
+ + " for "
239
+ + var
240
+ + " defined at gridbox "
241
+ + varat[v]
242
+ + " over "
243
+ + str(inputs["ntime"])
244
+ + " time steps)"
245
+ )
246
+ filename = f"{thermofiles.stem}_{var}{thermofiles.suffix}"
247
+ filename = thermofiles.parent / filename
248
+ writebinary.writebinary(
249
+ filename,
250
+ thermodata[var],
251
+ [ndata[v]],
252
+ [datatypes[v]],
253
+ [units[v]],
254
+ [scale_factors[v]],
255
+ metastr,
256
+ )