forked from open-atmos/PyPartMC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_scenario.py
358 lines (320 loc) · 12.7 KB
/
test_scenario.py
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
####################################################################################################
# This file is a part of PyPartMC licensed under the GNU General Public License v3 (LICENSE file) #
# Copyright (C) 2022-2024 University of Illinois Urbana-Champaign #
# Authors: https://github.com/open-atmos/PyPartMC/graphs/contributors #
####################################################################################################
import copy
import gc
import json
import platform
import pytest
import PyPartMC as ppmc
from .common import ENV_STATE_CTOR_ARG_MINIMAL
from .test_aero_data import AERO_DATA_CTOR_ARG_FULL, AERO_DATA_CTOR_ARG_MINIMAL
from .test_aero_mode import AERO_MODE_CTOR_LOG_NORMAL, AERO_MODE_CTOR_LOG_NORMAL_FULL
from .test_gas_data import GAS_DATA_CTOR_ARG_MINIMAL
SCENARIO_CTOR_ARG_MINIMAL = {
"temp_profile": [{"time": [0]}, {"temp": [273]}],
"pressure_profile": [{"time": [0]}, {"pressure": [1e5]}],
"height_profile": [{"time": [0]}, {"height": [1]}],
"gas_emissions": [{"time": [0]}, {"rate": [0]}, {"SO2": [0]}],
"gas_background": [{"time": [0]}, {"rate": [0]}, {"SO2": [0]}],
"aero_emissions": [
{"time": [0]},
{"rate": [0]},
{"dist": [[AERO_MODE_CTOR_LOG_NORMAL]]},
],
"aero_background": [
{"time": [0]},
{"rate": [0]},
{"dist": [[AERO_MODE_CTOR_LOG_NORMAL]]},
],
"loss_function": "none",
}
SCENARIO_CTOR_ARG_SIMULATION = {
"temp_profile": [{"time": [0]}, {"temp": [273]}],
"pressure_profile": [{"time": [0]}, {"pressure": [1e5]}],
"height_profile": [{"time": [0]}, {"height": [1]}],
"gas_emissions": [{"time": [0]}, {"rate": [1]}, {"SO2": [1e-9]}],
"gas_background": [{"time": [0]}, {"rate": [0]}, {"SO2": [0]}],
"aero_emissions": [
{"time": [0]},
{"rate": [0]},
{"dist": [[AERO_MODE_CTOR_LOG_NORMAL]]},
],
"aero_background": [
{"time": [0]},
{"rate": [0]},
{"dist": [[AERO_MODE_CTOR_LOG_NORMAL]]},
],
"loss_function": "none",
}
class TestScenario:
@staticmethod
@pytest.mark.parametrize(
"params",
(
{
"gas_data_ctor_arg": GAS_DATA_CTOR_ARG_MINIMAL,
"scenario_ctor_arg": SCENARIO_CTOR_ARG_MINIMAL,
},
{
"gas_data_ctor_arg": ("SO2", "NO2"),
"scenario_ctor_arg": {
"temp_profile": [{"time": [0, 1, 2]}, {"temp": [1, 2, 3]}],
"pressure_profile": [{"time": [0, 1, 2]}, {"pressure": [1, 2, 3]}],
"height_profile": [
{"time": [0, 1, 2]},
{"height": [1, 2, 3]},
],
"gas_emissions": [
{"time": [0, 1, 2]},
{"rate": [1, 1, 1]},
{"SO2": [0, 0, 0]},
{"NO2": [0, 0, 0]},
],
"gas_background": [
{"time": [0, 1, 2]},
{"rate": [1, 1, 1]},
{"SO2": [0, 0, 0]},
{"NO2": [0, 0, 0]},
],
"aero_emissions": [
{"time": [0]},
{"rate": [0]},
{"dist": [[AERO_MODE_CTOR_LOG_NORMAL]]},
],
"aero_background": [
{"time": [0]},
{"rate": [0]},
{"dist": [[AERO_MODE_CTOR_LOG_NORMAL]]},
],
"loss_function": "none",
},
},
),
)
def test_ctor(params):
# arrange
aero_data = ppmc.AeroData(AERO_DATA_CTOR_ARG_MINIMAL)
gas_data = ppmc.GasData(params["gas_data_ctor_arg"])
# act
sut = ppmc.Scenario(gas_data, aero_data, params["scenario_ctor_arg"])
# assert
assert sut is not None
@staticmethod
def test_dtor():
# arrange
aero_data = ppmc.AeroData(AERO_DATA_CTOR_ARG_MINIMAL)
gas_data = ppmc.GasData(GAS_DATA_CTOR_ARG_MINIMAL)
# pylint: disable=unused-variable
sut = ppmc.Scenario(gas_data, aero_data, SCENARIO_CTOR_ARG_MINIMAL)
gc.collect()
# act
sut = None
gc.collect()
# assert
pass
@staticmethod
def test_str():
# arrange
aero_data = ppmc.AeroData(AERO_DATA_CTOR_ARG_MINIMAL)
gas_data = ppmc.GasData(GAS_DATA_CTOR_ARG_MINIMAL)
sut = ppmc.Scenario(gas_data, aero_data, SCENARIO_CTOR_ARG_MINIMAL)
# act
json_actual = json.loads(str(sut))
# assert
assert json_actual == SCENARIO_CTOR_ARG_MINIMAL
@staticmethod
def test_init_env_state():
# arrange
aero_data = ppmc.AeroData(AERO_DATA_CTOR_ARG_MINIMAL)
gas_data = ppmc.GasData(GAS_DATA_CTOR_ARG_MINIMAL)
env_state = ppmc.EnvState(ENV_STATE_CTOR_ARG_MINIMAL)
time = 666.0
sut = ppmc.Scenario(gas_data, aero_data, SCENARIO_CTOR_ARG_MINIMAL)
# act
sut.init_env_state(env_state, time)
@staticmethod
@pytest.mark.xfail(strict=True)
@pytest.mark.skipif("sys.platform != 'linux'")
def test_ctor_fails_with_no_values_in_time_array():
# arrange
aero_data = ppmc.AeroData(AERO_DATA_CTOR_ARG_MINIMAL)
gas_data = ppmc.GasData(GAS_DATA_CTOR_ARG_MINIMAL)
ctor_arg = copy.deepcopy(SCENARIO_CTOR_ARG_MINIMAL)
ctor_arg["temp_profile"][0]["time"] = []
# act
_ = ppmc.Scenario(gas_data, aero_data, ctor_arg)
@staticmethod
@pytest.mark.parametrize(
"mode_names",
(
("A", "B"),
pytest.param(
("B", "A"), marks=(pytest.mark.xfail(strict=True),)
), # TODO #213
),
)
def test_multi_mode(mode_names):
# arrange
aero_data = ppmc.AeroData(AERO_DATA_CTOR_ARG_MINIMAL)
gas_data = ppmc.GasData(GAS_DATA_CTOR_ARG_MINIMAL)
scenario_ctor_arg = copy.deepcopy(SCENARIO_CTOR_ARG_MINIMAL)
for entry in ("aero_emissions", "aero_background"):
scenario_ctor_arg[entry][-1]["dist"] = [
[
{
mode_names[0]: AERO_MODE_CTOR_LOG_NORMAL["test_mode"],
mode_names[1]: AERO_MODE_CTOR_LOG_NORMAL["test_mode"],
},
]
]
# act
sut = ppmc.Scenario(gas_data, aero_data, scenario_ctor_arg)
# assert
emissions = sut.aero_emissions(aero_data, 0)
actual_mode_names = tuple(
emissions.mode(i).name for i in range(emissions.n_mode)
)
assert mode_names == actual_mode_names
# TODO #223 : same for background
@staticmethod
@pytest.mark.parametrize("key", ("aero_emissions", "aero_background"))
@pytest.mark.parametrize(
"mode_names",
(
("A", "B"),
pytest.param(
("B", "A"), marks=(pytest.mark.xfail(strict=True),)
), # TODO #213
),
)
def test_time_varying_aero_multimode(key, mode_names):
# arrange
aero_data = ppmc.AeroData(AERO_DATA_CTOR_ARG_FULL)
gas_data = ppmc.GasData(GAS_DATA_CTOR_ARG_MINIMAL)
scenario_ctor_arg = copy.deepcopy(SCENARIO_CTOR_ARG_MINIMAL)
scenario_ctor_arg[key] = [
{"time": [0, 1, 2, 3, 4]},
{"rate": [0, 10, 100, 1000, 10000]},
{
"dist": [
[
{
mode_names[0]: AERO_MODE_CTOR_LOG_NORMAL["test_mode"],
mode_names[1]: AERO_MODE_CTOR_LOG_NORMAL_FULL["test_mode"],
},
]
]
* 5
},
]
# act
scenario = ppmc.Scenario(gas_data, aero_data, scenario_ctor_arg)
attr = {"aero_emissions": "aero_emissions", "aero_background": "aero_dilution"}[
key
]
n_times = getattr(scenario, f"{attr}_n_times")
rate_scale = getattr(
scenario, f"{attr}_rate" + ("_scale" if "emissions" in key else "")
)
times = getattr(scenario, f"{attr}_time")
assert n_times == len(list(scenario_ctor_arg[key][0].values())[0])
for i in range(n_times):
dist = getattr(scenario, key)(aero_data, i)
assert times[i] == list(scenario_ctor_arg[key][0]["time"])[i]
assert rate_scale[i] == list(scenario_ctor_arg[key][1]["rate"])[i]
assert dist.n_mode == len(list(scenario_ctor_arg[key][2]["dist"][i])[0])
for i_mode in range(dist.n_mode):
assert dist.mode(i_mode).name == mode_names[i_mode]
assert (
dist.mode(i_mode).num_conc
== list(scenario_ctor_arg[key][2].values())[0][i][0][
dist.mode(i_mode).name
]["num_conc"]
)
ERR_MSG_2_ELEM_LIST_EXPECTED = (
"PROF_profile expected to be a 2-element list (of single-element dictionaries)"
)
ERR_MSG_ALL_ELEMS_DICTS = (
"PROF_profile expected to contain only single-element dicts"
)
ERR_MSG_PROFILE_LEN_MATCH = (
"PROF_profile 'time' and 'PROF' arrays do not have matching size"
)
ERR_MSG_TIME_KEY_MISSING = (
"PROF_profile first element is expeced to be"
" a single-element dict with 'time' key"
)
ERR_MSG_PROF_KEY_MISSING = (
"PROF_profile second element is expeced to be"
" a single-element dict with 'PROF' key"
)
@staticmethod
@pytest.mark.parametrize("prof", ("height", "temp", "pressure"))
@pytest.mark.parametrize(
"data, msg",
(
({}, ERR_MSG_2_ELEM_LIST_EXPECTED),
([{}, {}, {}], ERR_MSG_2_ELEM_LIST_EXPECTED),
([[], []], ERR_MSG_ALL_ELEMS_DICTS),
([{}, []], ERR_MSG_ALL_ELEMS_DICTS),
([[], {}], ERR_MSG_ALL_ELEMS_DICTS),
([{"1": 1, "2": 2}, {}], ERR_MSG_ALL_ELEMS_DICTS),
([{"1": 1, "2": 2, "3": 3}, {"1": 1, "2": 2}], ERR_MSG_ALL_ELEMS_DICTS),
([{"xtime": ""}, {"PROF": ""}], ERR_MSG_TIME_KEY_MISSING),
([{"time": ""}, {"xPROF": ""}], ERR_MSG_PROF_KEY_MISSING),
([{"time": ""}, {"PROF": []}], ERR_MSG_PROFILE_LEN_MATCH),
([{"time": []}, {"PROF": ""}], ERR_MSG_PROFILE_LEN_MATCH),
([{"time": [1, 2]}, {"PROF": [1, 2, 3]}], ERR_MSG_PROFILE_LEN_MATCH),
),
)
@pytest.mark.skipif(platform.machine() == "arm64", reason="TODO #348")
def test_throws_if_profile_not_of_proper_form(prof, data, msg):
# arrange
aero_data = ppmc.AeroData(AERO_DATA_CTOR_ARG_MINIMAL)
gas_data = ppmc.GasData(GAS_DATA_CTOR_ARG_MINIMAL)
data = copy.deepcopy(data)
if len(data) == 2 and isinstance(data[1], dict) and len(data[1].keys()) == 1:
key = tuple(data[1].keys())[0]
new_key = key.replace("PROF", prof)
if key != new_key:
data[1][new_key] = data[1][key]
del data[1][key]
ctor_arg = copy.deepcopy(SCENARIO_CTOR_ARG_MINIMAL)
ctor_arg[f"{prof}_profile"] = data
# act
with pytest.raises(RuntimeError) as excinfo:
_ = ppmc.Scenario(gas_data, aero_data, ctor_arg)
# assert
assert str(excinfo.value) == msg.replace("PROF", f"{prof}")
@staticmethod
def test_loss_function_chamber():
# arrange
aero_data = ppmc.AeroData(AERO_DATA_CTOR_ARG_MINIMAL)
gas_data = ppmc.GasData(GAS_DATA_CTOR_ARG_MINIMAL)
env_state = ppmc.EnvState(ENV_STATE_CTOR_ARG_MINIMAL)
ctor_arg = copy.deepcopy(SCENARIO_CTOR_ARG_MINIMAL)
ctor_arg["loss_function"] = "chamber"
ctor_arg["chamber_vol"] = 1.0
ctor_arg["area_diffuse"] = 1.0
ctor_arg["area_sedi"] = 1.0
ctor_arg["prefactor_BL"] = 1.0
ctor_arg["exponent_BL"] = 1.0
# act
sut = ppmc.Scenario(gas_data, aero_data, ctor_arg)
sut.init_env_state(env_state, 0.0)
# assert
assert ppmc.loss_rate(sut, 1e-20, 1000.0, aero_data, env_state) > 0.0
@staticmethod
def test_loss_function_constant():
# arrange
aero_data = ppmc.AeroData(AERO_DATA_CTOR_ARG_MINIMAL)
gas_data = ppmc.GasData(GAS_DATA_CTOR_ARG_MINIMAL)
env_state = ppmc.EnvState(ENV_STATE_CTOR_ARG_MINIMAL)
ctor_arg = copy.deepcopy(SCENARIO_CTOR_ARG_MINIMAL)
ctor_arg["loss_function"] = "constant"
# act
sut = ppmc.Scenario(gas_data, aero_data, ctor_arg)
assert ppmc.loss_rate(sut, 1e-20, 1000.0, aero_data, env_state) == 0.001