forked from iterorganization/IMAS-Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_ids_validate.py
More file actions
343 lines (280 loc) · 11.6 KB
/
Copy pathtest_ids_validate.py
File metadata and controls
343 lines (280 loc) · 11.6 KB
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
import logging
from unittest.mock import Mock
import numpy as np
import pytest
from imas import DBEntry, IDSFactory
from imas.exception import ValidationError
from imas.ids_defs import (
IDS_TIME_MODE_HETEROGENEOUS,
IDS_TIME_MODE_HOMOGENEOUS,
IDS_TIME_MODE_INDEPENDENT,
MEMORY_BACKEND,
)
from imas.test.test_helpers import fill_consistent
@pytest.fixture(autouse=True)
def raise_on_logged_warnings(caplog):
"""Catch warnings logged by validate() and fail the testcase if there are any."""
yield
records = [
rec for rec in caplog.get_records("call") if rec.levelno >= logging.WARNING
]
if records:
pytest.fail(f"Warning(s) encountered during test: {records}")
def test_validate_time_mode():
cp = IDSFactory().core_profiles()
with pytest.raises(ValidationError):
cp.validate()
for time_mode in [
IDS_TIME_MODE_HOMOGENEOUS,
IDS_TIME_MODE_HETEROGENEOUS,
IDS_TIME_MODE_INDEPENDENT,
]:
cp.ids_properties.homogeneous_time = time_mode
cp.validate()
def test_validate_time_coordinate_homogeneous():
cp = IDSFactory("3.39.0").core_profiles()
cp.ids_properties.homogeneous_time = IDS_TIME_MODE_HOMOGENEOUS
cp.time = np.array([1.0, 2.0])
cp.profiles_1d.resize(2)
cp.validate()
cp.profiles_1d.resize(3)
with pytest.raises(ValidationError):
# non-matching size
cp.validate()
def test_validate_time_coordinate_heterogeneous_core_profiles():
cp = IDSFactory("3.39.0").core_profiles()
cp.ids_properties.homogeneous_time = IDS_TIME_MODE_HETEROGENEOUS
cp.profiles_1d.resize(2)
with pytest.raises(ValidationError):
cp.validate() # Unset cp.profiles_1d.time
cp.profiles_1d[0].time = 1.0
with pytest.raises(ValidationError):
cp.validate() # Unset cp.profiles_1d.time
cp.profiles_1d[1].time = 2.0
cp.validate()
def test_validate_time_mode_heterogeneous_pf_active():
pfa = IDSFactory("3.39.0").pf_active()
pfa.ids_properties.homogeneous_time = IDS_TIME_MODE_HETEROGENEOUS
pfa.coil.resize(1)
pfa.coil[0].current.data = np.linspace(0, 1, 10)
with pytest.raises(ValidationError):
pfa.validate()
pfa.coil[0].current.time = np.linspace(0, 1, 9) # one too short
with pytest.raises(ValidationError):
pfa.validate()
pfa.coil[0].current.time = np.linspace(0, 1, 10)
pfa.validate()
pfa.ids_properties.homogeneous_time = IDS_TIME_MODE_HOMOGENEOUS
with pytest.raises(ValidationError):
pfa.validate()
pfa.time = np.linspace(0, 1, 10)
pfa.validate()
def test_validate_time_mode_independent():
cp = IDSFactory("3.39.0").core_profiles()
cp.ids_properties.homogeneous_time = IDS_TIME_MODE_INDEPENDENT
cp.validate()
cp.profiles_1d.resize(1)
with pytest.raises(ValidationError):
cp.validate()
def test_fixed_size_coordinates_two():
mag = IDSFactory("3.39.0").magnetics()
mag.ids_properties.homogeneous_time = IDS_TIME_MODE_INDEPENDENT
mag.b_field_pol_probe.resize(1)
mag.validate()
for bandwidth_size in [0, 1, 2, 3, 4, 1000]:
mag.b_field_pol_probe[0].bandwidth_3db = np.linspace(0, 1, bandwidth_size)
if bandwidth_size in (0, 2):
mag.validate()
else: # coordinate1 = 1...2, so only size 0 or 2 is allowed
with pytest.raises(ValidationError):
mag.validate()
def test_fixed_size_coordinates_three():
wall = IDSFactory("3.39.0").wall()
wall.ids_properties.homogeneous_time = IDS_TIME_MODE_HOMOGENEOUS
wall.time = np.linspace(0, 1, 10)
wall.global_quantities.electrons.particle_flux_from_wall = np.ones((3, 10))
wall.validate()
for size in [1, 2, 4, 5]:
wall.global_quantities.electrons.particle_flux_from_wall = np.ones((size, 10))
with pytest.raises(ValidationError):
wall.validate()
def test_validate_indirect_coordinates():
"""Test indirect coordinates like
coordinate1=coordinate_system(process(i1)/coordinate_index)/coordinate(1)
"""
amns = IDSFactory("3.39.0").amns_data()
amns.ids_properties.homogeneous_time = IDS_TIME_MODE_INDEPENDENT
amns.process.resize(1)
amns.process[0].charge_state.resize(1)
amns.process[0].charge_state[0].table_1d = np.ones(10)
with pytest.raises(ValidationError):
# unset amns.process[0].coordinate_index
amns.validate()
# create some coordinate systems
amns.coordinate_system.resize(3)
for i in range(3):
amns.coordinate_system[i].coordinate.resize(6)
for j in range(6):
amns.coordinate_system[i].coordinate[j].label = f"label_{i}_{j}"
values = np.linspace(0, 1, 1 + i + j)
amns.coordinate_system[i].coordinate[j].values = values
amns.process[0].coordinate_index = 1
amns.process[0].charge_state[0].table_1d = np.ones(1)
amns.validate()
for i in range(6):
shape = [1, 2, 3, 4, 5, 6]
shape[i] = shape[i] + 1
amns.process[0].charge_state[0].table_6d = np.ones(shape)
with pytest.raises(ValidationError):
amns.validate()
amns.process[0].charge_state[0].table_6d = np.ones((1, 2, 3, 4, 5, 6))
amns.validate()
amns.process[0].coordinate_index = 3
with pytest.raises(ValidationError):
amns.validate()
amns.process[0].charge_state[0].table_1d = np.ones(3)
amns.process[0].charge_state[0].table_6d = np.ones((3, 4, 5, 6, 7, 8))
amns.validate()
def test_validate_exclusive_references():
distr = IDSFactory("3.39.0").distributions()
distr.ids_properties.homogeneous_time = IDS_TIME_MODE_HOMOGENEOUS
distr.time = np.array([1.0])
distr.distribution.resize(1)
distr.distribution[0].profiles_2d.resize(1)
distr.distribution[0].profiles_2d[0].density = np.ones((2, 3))
with pytest.raises(ValidationError):
distr.validate()
distr.distribution[0].profiles_2d[0].grid.r = np.linspace(0, 1, 2)
distr.distribution[0].profiles_2d[0].grid.z = np.linspace(0, 1, 3)
distr.validate()
distr.distribution[0].profiles_2d[0].grid.rho_tor_norm = np.linspace(0, 1, 2)
with pytest.raises(ValidationError):
distr.validate() # either grid/r or grid/rho_tor_norm can be defined
distr.distribution[0].profiles_2d[0].grid.r = np.array([])
distr.validate()
def test_validate_reference_or_fixed_size():
waves = IDSFactory("3.39.0").waves()
waves.ids_properties.homogeneous_time = IDS_TIME_MODE_HOMOGENEOUS
waves.time = np.array([1.0])
waves.coherent_wave.resize(1)
waves.coherent_wave[0].beam_tracing.resize(1)
waves.coherent_wave[0].beam_tracing[0].beam.resize(1)
waves.validate()
beam = waves.coherent_wave[0].beam_tracing[0].beam[0]
# n_tor coordinate1=beam.length OR 1...1
beam.wave_vector.n_tor = np.array([1], dtype=np.int32)
waves.validate()
beam.wave_vector.n_tor = np.array([1, 2], dtype=np.int32)
with pytest.raises(ValidationError):
waves.validate() # beam.length has length 0
beam.length = np.array([0.4, 0.5])
waves.validate()
def test_validate_coordinate_same_as():
ml = IDSFactory("3.39.0").mhd_linear()
ml.ids_properties.homogeneous_time = IDS_TIME_MODE_HOMOGENEOUS
ml.time = np.array([1.0])
ml.time_slice.resize(1)
ml.validate()
ml.time_slice[0].toroidal_mode.resize(1)
tor_mode = ml.time_slice[0].toroidal_mode[0]
tor_mode.plasma.grid.dim1 = np.ones(4)
tor_mode.plasma.stress_maxwell.imaginary = np.ones((4, 5, 6))
with pytest.raises(ValidationError):
# The imaginary component has coordinate2/3_same_as the real component
# but the real component is still empty
ml.validate()
tor_mode.plasma.stress_maxwell.real = np.ones((4, 5, 6))
ml.validate()
tor_mode.plasma.stress_maxwell.real = np.ones((4, 1, 6))
with pytest.raises(ValidationError):
ml.validate() # dimension 2 does not match
tor_mode.plasma.stress_maxwell.real = np.ones((4, 5, 1))
with pytest.raises(ValidationError):
ml.validate() # dimension 3 does not match
@pytest.mark.parametrize(
"env_value, should_validate",
[
("1", False),
("yes", False),
("asdf", False),
("0", True),
("", True),
(None, True),
],
)
def test_validate_on_put(monkeypatch, env_value, should_validate):
dbentry = DBEntry(MEMORY_BACKEND, "test", 1, 1)
dbentry.create()
ids = dbentry.factory.core_profiles()
ids.ids_properties.homogeneous_time = IDS_TIME_MODE_HOMOGENEOUS
ids.time = np.array([1.0])
validate_mock = Mock()
monkeypatch.setattr("imas.ids_toplevel.IDSToplevel.validate", validate_mock)
if env_value is None:
monkeypatch.delenv("IMAS_AL_DISABLE_VALIDATE", raising=False)
else:
monkeypatch.setenv("IMAS_AL_DISABLE_VALIDATE", env_value)
dbentry.put(ids)
assert validate_mock.call_count == 1 * should_validate
dbentry.put_slice(ids)
assert validate_mock.call_count == 2 * should_validate
def test_validate_ignore_nested_aos():
# Ignore coordinates inside an AoS outside our tree, see IMAS-4675
equilibrium = IDSFactory("3.39.0").equilibrium()
equilibrium.ids_properties.homogeneous_time = IDS_TIME_MODE_HOMOGENEOUS
equilibrium.time = np.array([1.0])
equilibrium.time_slice.resize(1)
equilibrium.validate()
equilibrium.time_slice[0].ggd.resize(1)
# Coordinate of equilibrium time_slice(itime)/ggd = grids_ggd(itime)/grid
# where grids_ggd is a (dynamic) AoS outside our tree, so this coordinate check
# should be ignored:
equilibrium.validate()
@pytest.fixture
def alternative_coordinates_cp():
"""Test alternative coordinates introduced in DDv4 with IMAS-4725."""
cp = IDSFactory("4.0.0").new("core_profiles")
cp.ids_properties.homogeneous_time = IDS_TIME_MODE_HETEROGENEOUS
cp.profiles_1d.resize(1)
cp.profiles_1d[0].time = 1.0
cp.validate()
return cp
# Alternatives for core_profiles profiles_1d/grid/rho_tor_norm
ALTERNATIVES = ["rho_tor_norm", "rho_tor", "psi", "volume", "area", "surface"]
ALTERNATIVES += ["rho_pol_norm"]
@pytest.fixture(params=ALTERNATIVES)
def alternative(request):
return request.param
def test_single_alternative_coordinate_filled(alternative_coordinates_cp, alternative):
alternative_coordinates_cp.profiles_1d[0].grid[alternative] = np.ones(3)
alternative_coordinates_cp.validate()
def test_multiples_alternative_coordinates_filled(alternative_coordinates_cp):
grid = alternative_coordinates_cp.profiles_1d[0].grid
grid.rho_tor_norm = np.ones(3)
grid.rho_tor = np.ones(2)
with pytest.raises(ValidationError):
alternative_coordinates_cp.validate() # sizes don't match
grid.rho_tor = np.ones(3)
alternative_coordinates_cp.validate() # now they match again
# Add a third one
grid.volume = np.ones(5)
with pytest.raises(ValidationError):
alternative_coordinates_cp.validate() # sizes don't match
grid.volume = np.ones(3)
alternative_coordinates_cp.validate() # now they match again
def test_validate_with_alternative_coordinates(alternative_coordinates_cp, alternative):
grid = alternative_coordinates_cp.profiles_1d[0].grid
alternative_coordinates_cp.profiles_1d[0].electrons.temperature = np.ones(4)
with pytest.raises(ValidationError):
alternative_coordinates_cp.validate() # no coordinates allocated
# Set to wrong size:
grid[alternative] = np.ones(3)
with pytest.raises(ValidationError):
alternative_coordinates_cp.validate()
# Now set to correct size
grid[alternative] = np.ones(4)
alternative_coordinates_cp.validate()
def test_validate_random_fill(ids_name):
ids = IDSFactory().new(ids_name)
fill_consistent(ids)
ids.validate()