-
Notifications
You must be signed in to change notification settings - Fork 647
Expand file tree
/
Copy pathtest_cell.py
More file actions
441 lines (354 loc) · 12.9 KB
/
test_cell.py
File metadata and controls
441 lines (354 loc) · 12.9 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
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
import lxml.etree as ET
import numpy as np
from uncertainties import ufloat
import openmc
import pytest
from tests.unit_tests import assert_unbounded
from openmc.data import atomic_mass, AVOGADRO
def test_contains():
# Cell with specified region
s = openmc.XPlane()
c = openmc.Cell(region=+s)
assert (1.0, 0.0, 0.0) in c
assert (-1.0, 0.0, 0.0) not in c
# Cell with no region
c = openmc.Cell()
assert (10.0, -4., 2.0) in c
def test_id():
openmc.Cell(cell_id=0)
with pytest.raises(ValueError):
openmc.Cell(cell_id=-1)
def test_repr(cell_with_lattice):
cells, mats, univ, lattice = cell_with_lattice
repr(cells[0]) # cell with distributed materials
repr(cells[1]) # cell with material
repr(cells[2]) # cell with lattice
# Empty cell
c = openmc.Cell()
repr(c)
# Empty cell with volume
c.volume = 3.0
repr(c)
# Empty cell with uncertain volume
c.volume = ufloat(3.0, 0.2)
repr(c)
def test_bounding_box():
zcyl = openmc.ZCylinder()
c = openmc.Cell(region=-zcyl)
ll, ur = c.bounding_box
assert ll == pytest.approx((-1., -1., -np.inf))
assert ur == pytest.approx((1., 1., np.inf))
# Cell with no region specified
c = openmc.Cell()
assert_unbounded(c)
def test_clone():
m = openmc.Material()
cyl = openmc.ZCylinder()
c = openmc.Cell(fill=m, region=-cyl)
# Check cloning with all optional params as the defaults
c2 = c.clone()
assert c2.id != c.id
assert c2.fill != c.fill
assert c2.region != c.region
c3 = c.clone(clone_materials=False)
assert c3.id != c.id
assert c3.fill == c.fill
assert c3.region != c.region
c4 = c.clone(clone_regions=False)
assert c4.id != c.id
assert c4.fill != c.fill
assert c4.region == c.region
# Add optional properties to the original cell to ensure they're cloned successfully
c.temperature = 650.
c.translation = (1., 2., 3.)
c.rotation = (4., 5., 6.)
c.volume = 100
c5 = c.clone(clone_materials=False, clone_regions=False)
assert c5.id != c.id
assert c5.fill == c.fill
assert c5.region == c.region
assert c5.temperature == c.temperature
assert c5.volume == c.volume
assert all(c5.translation == c.translation)
assert all(c5.rotation == c.rotation)
# Mutate the original to ensure the changes are not seen in the clones
c.fill = openmc.Material()
c.region = +openmc.ZCylinder()
c.translation = (-1., -2., -3.)
c.rotation = (-4., -5., -6.)
c.temperature = 1
c.volume = 1
assert c5.fill != c.fill
assert c5.region != c.region
assert c5.temperature != c.temperature
assert c5.volume != c.volume
assert all(c5.translation != c.translation)
assert all(c5.rotation != c.rotation)
def test_temperature(cell_with_lattice):
# Make sure temperature propagates through universes
m = openmc.Material()
s = openmc.XPlane()
c1 = openmc.Cell(fill=m, region=+s)
c2 = openmc.Cell(fill=m, region=-s)
u1 = openmc.Universe(cells=[c1, c2])
c = openmc.Cell(fill=u1)
c.temperature = 400.0
assert c1.temperature == 400.0
assert c2.temperature == 400.0
with pytest.raises(ValueError):
c.temperature = -100.
c.temperature = None
assert c1.temperature == None
assert c2.temperature == None
# distributed temperature
cells, _, _, _ = cell_with_lattice
c = cells[0]
c.temperature = (300., 600., 900.)
def test_densities(cell_with_lattice):
# Make sure density propagates through universes
m = openmc.Material()
s = openmc.XPlane()
c1 = openmc.Cell(fill=m, region=+s)
c2 = openmc.Cell(fill=m, region=-s)
u1 = openmc.Universe(cells=[c1, c2])
c = openmc.Cell(fill=u1)
c.density = 1.
assert c1.density == 1.
assert c2.density == 1.
with pytest.raises(ValueError):
c.density = -1.
c.density = None
assert c1.density == None
assert c2.density == None
# distributed density
cells, _, _, _ = cell_with_lattice
c = cells[0]
c.density = (1., 2., 3.)
def test_rotation():
u = openmc.Universe()
c = openmc.Cell(fill=u)
c.rotation = (180.0, 0.0, 0.0)
assert np.allclose(c.rotation_matrix, [
[1., 0., 0.],
[0., -1., 0.],
[0., 0., -1.]
])
c.rotation = (0.0, 90.0, 0.0)
assert np.allclose(c.rotation_matrix, [
[0., 0., -1.],
[0., 1., 0.],
[1., 0., 0.]
])
def test_get_nuclides(uo2):
c = openmc.Cell(fill=uo2)
nucs = c.get_nuclides()
assert nucs == ['U235', 'O16']
def test_volume_setting():
c = openmc.Cell()
# Test ordinary volume and uncertain volume
c.volume = 3
c.volume = ufloat(3, 0.7)
# Allow volume to be set to 0.0
c.volume = 0.0
c.volume = ufloat(0.0, 0.1)
# Test errors for negative volume
with pytest.raises(ValueError):
c.volume = -1.0
with pytest.raises(ValueError):
c.volume = ufloat(-0.05, 0.1)
def test_atoms_material_cell(uo2, water):
""" Test if correct number of atoms is returned.
Also check if Cell.atoms still works after volume/material was changed
"""
c = openmc.Cell(fill=uo2)
c.volume = 2.0
expected_nucs = ['U235', 'O16']
# Precalculate the expected number of atoms
M = (atomic_mass('U235') + 2 * atomic_mass('O16')) / 3
expected_atoms = [
1/3 * uo2.density/M * AVOGADRO * 2.0, # U235
2/3 * uo2.density/M * AVOGADRO * 2.0 # O16
]
tuples = c.atoms.items()
for nuc, atom_num, t in zip(expected_nucs, expected_atoms, tuples):
assert nuc == t[0]
assert atom_num == pytest.approx(t[1])
# Change volume and check if OK
c.volume = 3.0
expected_atoms = [
1/3 * uo2.density/M * AVOGADRO * 3.0, # U235
2/3 * uo2.density/M * AVOGADRO * 3.0 # O16
]
tuples = c.atoms.items()
for nuc, atom_num, t in zip(expected_nucs, expected_atoms, tuples):
assert nuc == t[0]
assert atom_num == pytest.approx(t[1])
# Change material and check if OK
c.fill = water
expected_nucs = ['H1', 'O16']
M = (2 * atomic_mass('H1') + atomic_mass('O16')) / 3
expected_atoms = [
2/3 * water.density/M * AVOGADRO * 3.0, # H1
1/3 * water.density/M * AVOGADRO * 3.0 # O16
]
tuples = c.atoms.items()
for nuc, atom_num, t in zip(expected_nucs, expected_atoms, tuples):
assert nuc == t[0]
assert atom_num == pytest.approx(t[1])
def test_atoms_distribmat_cell(uo2, water):
""" Test if correct number of atoms is returned for a cell with
'distribmat' fill
"""
c = openmc.Cell(fill=[uo2, water])
c.volume = 6.0
# Calculate the expected number of atoms
expected_nucs = ['U235', 'O16', 'H1']
M_uo2 = (atomic_mass('U235') + 2 * atomic_mass('O16')) / 3
M_water = (2 * atomic_mass('H1') + atomic_mass('O16')) / 3
expected_atoms = [
1/3 * uo2.density/M_uo2 * AVOGADRO * 3.0, # U235
(2/3 * uo2.density/M_uo2 * AVOGADRO * 3.0 +
1/3 * water.density/M_water * AVOGADRO * 3.0), # O16
2/3 * water.density/M_water * AVOGADRO * 3.0 # H1
]
tuples = c.atoms.items()
for nuc, atom_num, t in zip(expected_nucs, expected_atoms, tuples):
assert nuc == t[0]
assert atom_num == pytest.approx(t[1])
def test_atoms_errors(cell_with_lattice):
cells, mats, univ, lattice = cell_with_lattice
# Material Cell with no volume
with pytest.raises(ValueError):
cells[1].atoms
# Cell with lattice
cells[2].volume = 3
with pytest.raises(ValueError):
cells[2].atoms
# Cell with volume but with void fill
cells[1].volume = 2
cells[1].fill = None
with pytest.raises(ValueError):
cells[1].atoms
def test_nuclide_densities(uo2):
c = openmc.Cell(fill=uo2)
expected_nucs = ['U235', 'O16']
expected_density = [1.0, 2.0]
tuples = list(c.get_nuclide_densities().values())
for nuc, density, t in zip(expected_nucs, expected_density, tuples):
assert nuc == t[0]
assert density == pytest.approx(t[1])
# Empty cell
c = openmc.Cell()
assert not c.get_nuclide_densities()
def test_get_all_universes(cell_with_lattice):
# Cell with nested universes
c1 = openmc.Cell()
u1 = openmc.Universe(cells=[c1])
c2 = openmc.Cell(fill=u1)
u2 = openmc.Universe(cells=[c2])
c3 = openmc.Cell(fill=u2)
univs = set(c3.get_all_universes().values())
assert not (univs ^ {u1, u2})
# Cell with lattice
cells, mats, univ, lattice = cell_with_lattice
univs = set(cells[-1].get_all_universes().values())
assert not (univs ^ {univ})
def test_get_all_materials(cell_with_lattice):
# Normal cell
m = openmc.Material()
c = openmc.Cell(fill=m)
test_mats = set(c.get_all_materials().values())
assert not(test_mats ^ {m})
# Cell filled with distributed materials
cells, mats, univ, lattice = cell_with_lattice
c = cells[0]
test_mats = set(c.get_all_materials().values())
assert not (test_mats ^ set(m for m in c.fill if m is not None))
# Cell filled with universe
c = cells[-1]
test_mats = set(c.get_all_materials().values())
assert not (test_mats ^ set(mats))
def test_to_xml_element(cell_with_lattice):
cells, mats, univ, lattice = cell_with_lattice
c = cells[-1]
root = ET.Element('geometry')
elem = c.create_xml_subelement(root)
assert elem.tag == 'cell'
assert elem.get('id') == str(c.id)
assert elem.get('region') is None
surf_elem = root.find('surface')
assert surf_elem.get('id') == str(cells[0].region.surface.id)
c = cells[0]
c.temperature = 900.0
c.volume = 1.0
elem = c.create_xml_subelement(root)
assert elem.get('region') == str(c.region)
assert elem.get('temperature') == str(c.temperature)
assert elem.get('volume') == str(c.volume)
@pytest.mark.parametrize("rotation", [
(90, 45, 0),
[[1.0, 0.0, 0.0], [0.0, 0.0, 1.0], [0.0, -1.0, 0.0]]
])
def test_rotation_from_xml(rotation):
# Make sure rotation attribute (matrix) round trips through XML correctly
s = openmc.ZCylinder(r=10.0)
cell = openmc.Cell(region=-s)
cell.rotation = rotation
root = ET.Element('geometry')
elem = cell.create_xml_subelement(root)
new_cell = openmc.Cell.from_xml_element(
elem, {s.id: s}, {'void': None}, openmc.Universe
)
np.testing.assert_allclose(new_cell.rotation, cell.rotation)
def test_dagmccell_from_xml_element():
"""DAGMCCell.from_xml_element parses material, temperature, density,
and volume; rejects unsupported attributes."""
mat = openmc.Material(1)
mat.add_nuclide('U235', 1.0)
mats = {'1': mat}
# In practice, from_xml_element is always called from DAGMCUniverse
# during XML parsing. A placeholder universe is used here so the test
# can exercise the method directly without a real DAGMC model file.
placeholder_univ = openmc.DAGMCUniverse('model.h5m')
# material + temperature + density round-trip
xml = '<cell id="5" name="fuel" material="1" temperature="900.0" density="10.5"/>'
cell = openmc.DAGMCCell.from_xml_element(ET.fromstring(xml), mats, placeholder_univ)
assert cell.id == 5
assert cell.name == 'fuel'
assert cell.fill is mat
assert cell.density == 10.5
assert cell.temperature == 900.0
# volume round-trip
xml = '<cell id="6" material="1" volume="42.0"/>'
cell = openmc.DAGMCCell.from_xml_element(ET.fromstring(xml), mats, placeholder_univ)
assert cell.volume == 42.0
# forbidden: region, fill, universe
for tag, val in [('region', '-1'), ('fill', '2'), ('universe', '0')]:
xml = f'<cell id="7" material="1" {tag}="{val}"/>'
with pytest.raises(ValueError, match=tag):
openmc.DAGMCCell.from_xml_element(ET.fromstring(xml), mats, placeholder_univ)
# forbidden: translation, rotation
for tag, val in [('translation', '1 0 0'), ('rotation', '0 0 90')]:
xml = f'<cell id="7" material="1" {tag}="{val}"/>'
with pytest.raises(ValueError, match=tag):
openmc.DAGMCCell.from_xml_element(ET.fromstring(xml), mats, placeholder_univ)
# missing material raises
xml = '<cell id="8"/>'
with pytest.raises(ValueError, match='material'):
openmc.DAGMCCell.from_xml_element(ET.fromstring(xml), mats, placeholder_univ)
def test_plot(run_in_tmpdir):
zcyl = openmc.ZCylinder()
c = openmc.Cell(region=-zcyl)
# create a universe before the plot
u_before = openmc.Universe()
# create a plot of the cell
c.plot()
# create a universe after the plot
u_after = openmc.Universe()
# ensure that calling the plot method doesn't
# affect the universe ID space
assert u_before.id + 1 == u_after.id
def test_xml_text_size_check():
from openmc._xml import _check_text_size, _LXML_MAX_TEXT_SIZE
with pytest.raises(ValueError, match="exceeds the lxml limit"):
_check_text_size("x" * _LXML_MAX_TEXT_SIZE, "test element")