forked from open-atmos/PyPartMC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_aero_dist.py
238 lines (196 loc) · 6.9 KB
/
test_aero_dist.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
####################################################################################################
# This file is a part of PyPartMC licensed under the GNU General Public License v3 (LICENSE file) #
# Copyright (C) 2022 University of Illinois Urbana-Champaign #
# Authors: https://github.com/open-atmos/PyPartMC/graphs/contributors #
####################################################################################################
import copy
import gc
import platform
import numpy as np
import pytest
import PyPartMC as ppmc
from PyPartMC import si
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_COAGULATION,
AERO_MODE_CTOR_LOG_NORMAL_FULL,
)
AERO_DIST_CTOR_ARG_MINIMAL = [
AERO_MODE_CTOR_LOG_NORMAL,
]
AERO_DIST_CTOR_ARG_FULL = [
AERO_MODE_CTOR_LOG_NORMAL_FULL,
]
AERO_DIST_CTOR_ARG_COAGULATION = [
AERO_MODE_CTOR_LOG_NORMAL_COAGULATION,
]
AERO_DIST_CTOR_ARG_AVERAGE = [
{
"test_mode_A": {
"mass_frac": [{"SO4": [1]}],
"diam_type": "geometric",
"mode_type": "mono",
"num_conc": 1e12 / si.m**3,
"diam": 2 * si.um,
},
"test_mode_B": {
"mass_frac": [{"BC": [1]}],
"diam_type": "geometric",
"mode_type": "mono",
"num_conc": 1e12 / si.m**3,
"diam": 0.2 * si.um,
},
}
]
@pytest.fixture(name="sut_minimal")
def sut_minimal_fixture():
aero_data = ppmc.AeroData(AERO_DATA_CTOR_ARG_MINIMAL)
sut = ppmc.AeroDist(aero_data, AERO_DIST_CTOR_ARG_MINIMAL)
aero_data = None
gc.collect()
return sut
# pylint: disable=too-few-public-methods
class TestAeroDist:
@staticmethod
def test_ctor():
# arrange
aero_data = ppmc.AeroData(AERO_DATA_CTOR_ARG_MINIMAL)
# act
sut = ppmc.AeroDist(aero_data, AERO_DIST_CTOR_ARG_MINIMAL)
# assert
assert sut is not None
@staticmethod
@pytest.mark.parametrize("n_modes", (2, 3))
@pytest.mark.parametrize(
"order",
(
range,
pytest.param(
lambda n_modes: reversed(range(n_modes)),
marks=(pytest.mark.xfail(strict=True),),
), # TODO #213
),
)
def test_ctor_multimode(n_modes, order):
# arrange
aero_data = ppmc.AeroData(AERO_DATA_CTOR_ARG_MINIMAL)
mode_data = AERO_DIST_CTOR_ARG_MINIMAL[0]["test_mode"]
num_concs = np.random.rand(n_modes)
modes = {}
for k in range(n_modes):
mode_data["num_conc"] = num_concs[k]
modes[k] = copy.deepcopy(mode_data)
# act
sut = ppmc.AeroDist(
aero_data, [{f"mode_{k}": modes[k] for k in order(n_modes)}]
)
# assert
assert sut.n_mode == n_modes
assert sut.num_conc == np.sum(num_concs)
for i in range(sut.n_mode):
assert sut.mode(i).type == modes[i]["mode_type"]
assert sut.mode(i).num_conc == modes[i]["num_conc"]
assert sut.mode(i).name == f"mode_{tuple(order(n_modes))[i]}"
@staticmethod
def test_ctor_modes_in_order(n_modes=4):
# arrange
aero_data = ppmc.AeroData(AERO_DATA_CTOR_ARG_MINIMAL)
mode_data = AERO_DIST_CTOR_ARG_MINIMAL[0]["test_mode"]
num_concs = np.random.rand(n_modes)
modes = {}
for k in range(n_modes):
mode_data["num_conc"] = num_concs[k]
modes[k] = copy.deepcopy(mode_data)
mode_map = {f"mode_{k}": modes[k] for k in range(n_modes)}
# act
sut = ppmc.AeroDist(aero_data, [mode_map])
# assert
expected_modes = tuple(mode_map.keys())
actual_modes = tuple(sut.mode(i).name for i in range(sut.n_mode))
assert expected_modes == actual_modes
@staticmethod
@pytest.mark.parametrize("idx", (-1, 500))
@pytest.mark.skipif(platform.machine() == "arm64", reason="TODO #348")
def test_get_mode_out_of_range(sut_minimal, idx):
# act
try:
_ = sut_minimal.mode(idx)
except IndexError:
return
# assert
assert False
@staticmethod
def test_get_mode_result_lifetime(
sut_minimal,
):
# arrange
mode = sut_minimal.mode(0)
mode_type = mode.type
# act
sut_minimal = None
gc.collect()
# assert
assert mode.type == mode_type
@staticmethod
def test_get_mode_is_a_copy(sut_minimal):
# arrange
new_type = "mono"
mode_idx = 0
mode = sut_minimal.mode(mode_idx)
assert mode.type != new_type
# act
mode.type = new_type
# assert
assert sut_minimal.mode(mode_idx).type != new_type
@staticmethod
def test_get_source_names():
# arrange
aero_data = ppmc.AeroData(AERO_DATA_CTOR_ARG_FULL)
_ = ppmc.AeroDist(aero_data, AERO_DIST_CTOR_ARG_AVERAGE)
# act
sources = aero_data.sources
# assert
keys = tuple(AERO_DIST_CTOR_ARG_AVERAGE[0].keys())
for i, key in enumerate(keys):
assert sources[i] == key # pylint: disable=unsubscriptable-object
@staticmethod
@pytest.mark.skipif(platform.machine() == "arm64", reason="TODO #348")
def test_ctor_multimode_error_on_repeated_mode_names():
# arrange
aero_data = ppmc.AeroData(AERO_DATA_CTOR_ARG_MINIMAL)
# act
with pytest.raises(Exception) as exc_info:
ppmc.AeroDist(aero_data, AERO_DIST_CTOR_ARG_MINIMAL * 2)
# assert
assert str(exc_info.value) == "Mode names must be unique"
@staticmethod
@pytest.mark.skipif(platform.machine() == "arm64", reason="TODO #348")
def test_ctor_error_on_repeated_massfrac_keys():
# arrange
aero_data = ppmc.AeroData(AERO_DATA_CTOR_ARG_MINIMAL)
fishy_ctor_arg = copy.deepcopy(AERO_DIST_CTOR_ARG_MINIMAL)
fishy_ctor_arg[0]["test_mode"]["mass_frac"].append(
fishy_ctor_arg[0]["test_mode"]["mass_frac"][0]
)
# act
with pytest.raises(Exception) as exc_info:
ppmc.AeroDist(aero_data, fishy_ctor_arg)
# assert
assert str(exc_info.value) == "mass_frac keys must be unique"
@staticmethod
def test_ctor_sampled_mode():
# arrange
aero_data = ppmc.AeroData(AERO_DATA_CTOR_ARG_MINIMAL)
ctor_arg = copy.deepcopy(AERO_DIST_CTOR_ARG_MINIMAL)
ctor_arg[0]["test_mode"]["mode_type"] = "sampled"
ctor_arg[0]["test_mode"]["size_dist"] = [
{"diam": [1, 2, 3, 4]},
{"num_conc": [1, 2, 3]},
]
# act
sut = ppmc.AeroDist(aero_data, ctor_arg)
# assert
assert sut.mode(0).num_conc == sum(
ctor_arg[0]["test_mode"]["size_dist"][1]["num_conc"]
)