forked from open-atmos/PyPartMC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_aero_state.py
592 lines (483 loc) · 17.9 KB
/
test_aero_state.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
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
####################################################################################################
# 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 gc
import platform
import numpy as np
import pytest
import PyPartMC as ppmc
from .test_aero_data import AERO_DATA_CTOR_ARG_FULL, AERO_DATA_CTOR_ARG_MINIMAL
from .test_aero_dist import (
AERO_DIST_CTOR_ARG_AVERAGE,
AERO_DIST_CTOR_ARG_FULL,
AERO_DIST_CTOR_ARG_MINIMAL,
)
from .test_aero_mode import AERO_MODE_CTOR_SAMPLED
from .test_env_state import ENV_STATE_CTOR_ARG_MINIMAL
AERO_STATE_CTOR_ARG_MINIMAL = 44, "nummass_source"
# pylint: disable=R0904
@pytest.fixture(name="sut_minimal")
def sut_minimal_fixture():
aero_data = ppmc.AeroData(AERO_DATA_CTOR_ARG_MINIMAL)
aero_dist = ppmc.AeroDist(aero_data, AERO_DIST_CTOR_ARG_MINIMAL)
sut = ppmc.AeroState(aero_data, *AERO_STATE_CTOR_ARG_MINIMAL)
_ = sut.dist_sample(aero_dist, 1.0, 0.0, True, True)
aero_data = None
gc.collect()
return sut
@pytest.fixture(name="sut_full")
def sut_full_fixture():
aero_data = ppmc.AeroData(AERO_DATA_CTOR_ARG_FULL)
aero_dist = ppmc.AeroDist(aero_data, AERO_DIST_CTOR_ARG_FULL)
sut = ppmc.AeroState(aero_data, *AERO_STATE_CTOR_ARG_MINIMAL)
_ = sut.dist_sample(aero_dist, 1.0, 0.0, True, True)
aero_data = None
gc.collect()
return sut
@pytest.fixture(name="sut_average")
def sut_average_fixture():
aero_data = ppmc.AeroData(AERO_DATA_CTOR_ARG_FULL)
aero_dist = ppmc.AeroDist(aero_data, AERO_DIST_CTOR_ARG_AVERAGE)
sut = ppmc.AeroState(aero_data, *AERO_STATE_CTOR_ARG_MINIMAL)
_ = sut.dist_sample(aero_dist, 1.0, 0.0, True, True)
aero_data = None
gc.collect()
return sut
class TestAeroState:
@staticmethod
def test_ctor():
# arrange
aero_data = ppmc.AeroData(AERO_DATA_CTOR_ARG_MINIMAL)
# act
sut = ppmc.AeroState(aero_data, *AERO_STATE_CTOR_ARG_MINIMAL)
# assert
assert sut is not None
@staticmethod
@pytest.mark.skipif(platform.machine() == "arm64", reason="TODO #348")
def test_ctor_fails_on_unknown_weighting():
# arrange
aero_data = ppmc.AeroData(AERO_DATA_CTOR_ARG_MINIMAL)
name = "kopytko"
# act
with pytest.raises(RuntimeError) as excinfo:
_ = ppmc.AeroState(aero_data, 1, name)
# assert
assert (
str(excinfo.value)
== f"unknown weighting scheme '{name}', valid options are: "
+ "flat, flat_source, nummass, nummass_source"
)
@staticmethod
@pytest.mark.parametrize("n_part", (44, 666))
def test_len(n_part):
# arrange
aero_data = ppmc.AeroData(AERO_DATA_CTOR_ARG_MINIMAL)
aero_dist = ppmc.AeroDist(aero_data, AERO_DIST_CTOR_ARG_MINIMAL)
sut = ppmc.AeroState(aero_data, n_part, "nummass_source")
_ = sut.dist_sample(aero_dist, 1.0, 0.0, True, True)
# act
size = len(sut)
# assert
assert int(size) > n_part * 0.5
assert int(size) < n_part * 2
@staticmethod
def test_total_num_conc(sut_minimal):
# act
total_num_conc = sut_minimal.total_num_conc
# assert
assert isinstance(total_num_conc, float)
assert total_num_conc > 0
@staticmethod
def test_total_mass_conc(sut_minimal):
# act
total_mass_conc = sut_minimal.total_mass_conc
# assert
assert isinstance(total_mass_conc, float)
assert total_mass_conc > 0
@staticmethod
def test_num_concs(sut_minimal):
# act
num_concs = sut_minimal.num_concs
# assert
assert isinstance(num_concs, list)
assert len(num_concs) == len(sut_minimal)
@staticmethod
def test_masses(sut_minimal):
# act
masses = sut_minimal.masses()
# assert
assert isinstance(masses, list)
assert len(masses) == len(sut_minimal)
@staticmethod
def test_masses_include(sut_full):
aero_data = ppmc.AeroData(AERO_DATA_CTOR_ARG_FULL)
# act
so4_ind = aero_data.spec_by_name("SO4")
masses = sut_full.masses(include=["SO4"])
masses_so4 = np.zeros(len(sut_full))
for i_part in range(len(sut_full)):
masses_so4[i_part] = sut_full.particle(i_part).species_masses[so4_ind]
# assert
assert isinstance(masses, list)
assert len(masses) == len(sut_full)
np.testing.assert_allclose(masses_so4, masses)
@staticmethod
def test_masses_exclude(sut_full):
# act
masses = sut_full.masses(exclude=["SO4"])
masses_so4 = np.zeros(len(sut_full))
for i_part in range(len(sut_full)):
masses_so4[i_part] = sut_full.particle(i_part).species_mass(1)
# assert
assert isinstance(masses, list)
assert len(masses) == len(sut_full)
np.testing.assert_allclose(masses_so4, masses)
@staticmethod
def test_masses_include_exclude(sut_full):
# act
masses = sut_full.masses(include=["SO4"], exclude=["SO4"])
# assert
assert isinstance(masses, list)
assert len(masses) == len(sut_full)
assert np.sum(masses) == 0.0
@staticmethod
def test_volumes(sut_minimal):
# act
volumes = sut_minimal.volumes()
# assert
assert isinstance(volumes, list)
assert len(volumes) == len(sut_minimal)
@staticmethod
def test_volumes_include(sut_full):
aero_data = ppmc.AeroData(AERO_DATA_CTOR_ARG_FULL)
# act
so4_ind = aero_data.spec_by_name("SO4")
volumes = sut_full.volumes(include=["SO4"])
vol_so4 = np.zeros(len(sut_full))
for i_part in range(len(sut_full)):
vol_so4[i_part] = sut_full.particle(i_part).volumes[so4_ind]
# assert
assert isinstance(volumes, list)
assert len(volumes) == len(sut_full)
np.testing.assert_allclose(vol_so4, volumes)
@staticmethod
def test_volumes_include_exclude(sut_full):
# act
volumes = sut_full.volumes(include=["SO4"], exclude=["SO4"])
# assert
assert isinstance(volumes, list)
assert len(volumes) == len(sut_full)
assert np.sum(volumes) == 0.0
@staticmethod
def test_volumes_exclude(sut_full):
aero_data = ppmc.AeroData(AERO_DATA_CTOR_ARG_FULL)
# act
so4_ind = aero_data.spec_by_name("SO4")
volumes = sut_full.volumes(exclude=["SO4"])
vol_so4 = np.zeros(len(sut_full))
for i_part in range(len(sut_full)):
vol_so4[i_part] = (
np.sum(sut_full.particle(i_part).volumes)
- sut_full.particle(i_part).volumes[so4_ind]
)
# assert
assert isinstance(volumes, list)
assert len(volumes) == len(sut_full)
np.testing.assert_allclose(vol_so4, volumes)
@staticmethod
def test_dry_diameters(sut_minimal):
# act
dry_diameters = sut_minimal.dry_diameters
# assert
assert isinstance(dry_diameters, list)
assert len(dry_diameters) == len(sut_minimal)
@staticmethod
def test_mobility_diameters(sut_minimal):
# act
env_state = ppmc.EnvState(ENV_STATE_CTOR_ARG_MINIMAL)
env_state.set_temperature(300)
env_state.pressure = 1e5
diameters = sut_minimal.mobility_diameters(env_state)
# assert
assert isinstance(diameters, list)
assert len(diameters) == len(sut_minimal)
assert (np.asarray(diameters) > 0).all()
@staticmethod
def test_diameters(sut_minimal):
# act
diameters = sut_minimal.diameters()
# assert
assert isinstance(diameters, list)
assert len(diameters) == len(sut_minimal)
@staticmethod
def test_ids(sut_minimal):
# act
ids = sut_minimal.ids
# assert
assert isinstance(ids, list)
assert len(ids) == len(sut_minimal)
assert (np.asarray(ids) > 0).all()
@staticmethod
def test_crit_rel_humids(sut_full):
# arrange
args = {"rel_humidity": 0.8, **ENV_STATE_CTOR_ARG_MINIMAL}
env_state = ppmc.EnvState(args)
env_state.set_temperature(300)
# act
crit_rel_humids = sut_full.crit_rel_humids(env_state)
# assert
assert isinstance(crit_rel_humids, list)
assert len(crit_rel_humids) == len(sut_full)
assert (np.asarray(crit_rel_humids) > 1).all()
assert (np.asarray(crit_rel_humids) < 1.2).all()
@staticmethod
def test_make_dry(sut_minimal):
# act
sut_minimal.make_dry()
masses = sut_minimal.masses(include=["H2O"])
assert (np.asarray(masses) == 0).all()
@staticmethod
def test_mixing_state(sut_minimal):
# act
mixing_state = sut_minimal.mixing_state()
# assert
assert isinstance(mixing_state, tuple)
assert len(mixing_state) == 3
@staticmethod
@pytest.mark.parametrize("n_bin", (1, 123))
def test_bin_average_comp(sut_average, n_bin):
# arrange
bin_grid = ppmc.BinGrid(n_bin, "log", 1e-9, 1e-4)
# act
sut_average.bin_average_comp(bin_grid)
so4_masses = np.array(sut_average.masses(include=["SO4"]))
bc_masses = np.array(sut_average.masses(include=["BC"]))
# assert
if n_bin == 1:
assert np.all(
np.isclose(so4_masses / bc_masses, so4_masses[0] / bc_masses[0])
)
else:
assert np.logical_xor(so4_masses > 0, bc_masses > 0).all()
@staticmethod
def test_get_particle(sut_minimal):
# act
particle = sut_minimal.particle(1)
# assert
assert isinstance(particle, ppmc.AeroParticle)
@staticmethod
def test_get_random_particle(sut_minimal):
# act
particle = sut_minimal.rand_particle()
# assert
assert isinstance(particle, ppmc.AeroParticle)
@staticmethod
def test_check_correct_particle(sut_minimal):
# act
i_part = 20
particle = sut_minimal.particle(i_part)
diameters = sut_minimal.diameters()
# assert
assert particle.diameter == diameters[i_part]
@staticmethod
def test_different_particles(sut_minimal):
# act
i_part = 20
particle_1 = sut_minimal.particle(i_part)
particle_2 = sut_minimal.particle(i_part + 1)
# assert
assert particle_1.diameter != particle_2.diameter
@staticmethod
@pytest.mark.parametrize("idx", (-1, 500))
@pytest.mark.skipif(platform.machine() == "arm64", reason="TODO #348")
def test_get_particle_out_of_range(sut_minimal, idx):
# act
try:
_ = sut_minimal.particle(idx)
except IndexError:
return
# assert
assert False
@staticmethod
def test_remove_particle(sut_minimal):
diameters = sut_minimal.diameters()
sut_minimal.remove_particle(len(sut_minimal) - 1)
assert diameters[0:-1] == sut_minimal.diameters()
@staticmethod
def test_zero(sut_minimal):
# act
sut_minimal.zero()
# assert
assert len(sut_minimal) == 0
@staticmethod
def test_add_particle(sut_minimal):
particle = sut_minimal.particle(1)
aero_data = ppmc.AeroData(AERO_DATA_CTOR_ARG_MINIMAL)
sut = ppmc.AeroState(aero_data, *AERO_STATE_CTOR_ARG_MINIMAL)
sut.add_particle(particle)
assert len(sut) == 1
assert sut.particle(0).diameter == sut_minimal.particle(1).diameter
@staticmethod
def test_add_particles(sut_minimal):
aero_data = ppmc.AeroData(AERO_DATA_CTOR_ARG_MINIMAL)
aero_dist = ppmc.AeroDist(aero_data, AERO_DIST_CTOR_ARG_MINIMAL)
delta = ppmc.AeroState(aero_data, *AERO_STATE_CTOR_ARG_MINIMAL)
_ = delta.dist_sample(aero_dist, 1.0, 0.0, True, True)
num_conc = sut_minimal.total_num_conc
num_conc_delta = delta.total_num_conc
sut_minimal.add_particles(delta)
assert np.isclose(sut_minimal.total_num_conc, (num_conc + num_conc_delta))
@staticmethod
def test_add(sut_minimal):
aero_data = ppmc.AeroData(AERO_DATA_CTOR_ARG_MINIMAL)
aero_dist = ppmc.AeroDist(aero_data, AERO_DIST_CTOR_ARG_MINIMAL)
delta = ppmc.AeroState(aero_data, *AERO_STATE_CTOR_ARG_MINIMAL)
_ = delta.dist_sample(aero_dist, 1.0, 0.0, True, True)
num_conc = sut_minimal.total_num_conc
num_conc_delta = delta.total_num_conc
sut_minimal.add(delta)
assert np.isclose(sut_minimal.total_num_conc, 0.5 * (num_conc + num_conc_delta))
@staticmethod
def test_sample_particles(sut_minimal):
# arrange
aero_data = ppmc.AeroData(AERO_DATA_CTOR_ARG_MINIMAL)
sut = ppmc.AeroState(aero_data, *AERO_STATE_CTOR_ARG_MINIMAL)
sut.copy_weight(sut_minimal)
# act
num_conc = sut_minimal.total_num_conc
samp_prob = 0.25
sut_minimal.sample_particles(sut, samp_prob)
# assert
assert len(sut) > 0
assert sut.total_num_conc > 0.5 * samp_prob * num_conc
assert sut.total_num_conc < sut_minimal.total_num_conc
assert np.isclose(sut.total_num_conc + sut_minimal.total_num_conc, num_conc)
@staticmethod
def test_sample(sut_minimal):
# arrange
aero_data = ppmc.AeroData(AERO_DATA_CTOR_ARG_MINIMAL)
sut = ppmc.AeroState(aero_data, *AERO_STATE_CTOR_ARG_MINIMAL)
# act
num_conc = sut_minimal.total_num_conc
samp_prob = 0.1
sut_minimal.sample(sut, samp_prob)
# assert
assert len(sut) > 0
assert sut.total_num_conc > 0.5 * samp_prob * num_conc
assert np.isclose(
(
samp_prob * sut.total_num_conc
+ (1 - samp_prob) * sut_minimal.total_num_conc
),
num_conc,
)
@staticmethod
def test_copy_weight(sut_minimal):
aero_data = ppmc.AeroData(AERO_DATA_CTOR_ARG_MINIMAL)
sut = ppmc.AeroState(aero_data, *AERO_STATE_CTOR_ARG_MINIMAL)
sut.copy_weight(sut_minimal)
sut.add_particle(sut_minimal.particle(0))
# pylint: disable=unsubscriptable-object
assert sut.num_concs[0] == sut_minimal.num_concs[0]
@staticmethod
@pytest.mark.parametrize(
"args",
(
(1.0, 0.0, True, True),
pytest.param((), id="default args"),
),
)
@pytest.mark.parametrize(
"weighting",
(
"flat_source",
"flat",
"nummass_source",
"nummass",
),
)
def test_dist_sample(args, weighting):
# arrange
n_part = 44
aero_data = ppmc.AeroData(AERO_DATA_CTOR_ARG_MINIMAL)
aero_dist = ppmc.AeroDist(aero_data, AERO_DIST_CTOR_ARG_MINIMAL)
sut = ppmc.AeroState(aero_data, n_part, weighting)
# act
n_added = sut.dist_sample(aero_dist, *args)
# assert
assert n_added > n_part * 0.5
assert n_added < n_part * 2
@staticmethod
def test_dist_sample_sampled():
# arrange
aero_data = ppmc.AeroData(AERO_DATA_CTOR_ARG_MINIMAL)
aero_dist = ppmc.AeroDist(aero_data, [AERO_MODE_CTOR_SAMPLED])
sut = ppmc.AeroState(aero_data, *AERO_STATE_CTOR_ARG_MINIMAL)
# act
_ = sut.dist_sample(aero_dist, 1.0, 0.0, True, True)
# assert
assert (
np.array(sut.diameters())
>= AERO_MODE_CTOR_SAMPLED["test_mode"]["size_dist"][0]["diam"][0]
).all()
assert (
np.array(sut.diameters())
<= AERO_MODE_CTOR_SAMPLED["test_mode"]["size_dist"][0]["diam"][-1]
).all()
@staticmethod
def test_dist_sample_mono():
# arrange
aero_data = ppmc.AeroData(AERO_DATA_CTOR_ARG_MINIMAL)
diam = 2e-6
aero_dist = ppmc.AeroDist(
aero_data,
[
{
"test_mode": {
"mass_frac": [{"H2O": [1]}],
"diam_type": "geometric",
"mode_type": "mono",
"num_conc": 1e12,
"diam": diam,
}
}
],
)
sut = ppmc.AeroState(aero_data, *AERO_STATE_CTOR_ARG_MINIMAL)
# act
_ = sut.dist_sample(aero_dist, 1.0, 0.0, True, True)
# assert
assert np.isclose(np.array(sut.diameters()), diam).all()
@staticmethod
@pytest.mark.parametrize(
"args",
(
((True, True), (True, False)),
((True, True), (False, True)),
((True, True), (False, False)),
((False, False), (True, False)),
((False, False), (False, True)),
((False, False), (True, True)),
((True, False), (False, False)),
((True, False), (False, True)),
((False, True), (False, False)),
((False, True), (True, False)),
),
)
@pytest.mark.skipif(platform.machine() == "arm64", reason="TODO #348")
def test_dist_sample_different_halving(args):
# arrange
aero_data = ppmc.AeroData(AERO_DATA_CTOR_ARG_MINIMAL)
aero_dist = ppmc.AeroDist(aero_data, [AERO_MODE_CTOR_SAMPLED])
sut = ppmc.AeroState(aero_data, *AERO_STATE_CTOR_ARG_MINIMAL)
# act
with pytest.raises(RuntimeError) as excinfo:
_ = sut.dist_sample(aero_dist, 1.0, 0.0, *args[0])
_ = sut.dist_sample(aero_dist, 1.0, 0.0, *args[1])
# assert
assert (
str(excinfo.value)
== "dist_sample() called with different halving/doubling settings then in last call"
)