-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathtest_integration.py
1168 lines (998 loc) · 38.6 KB
/
test_integration.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
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Copyright 2024, Battelle Energy Alliance, LLC All Rights Reserved.
import copy
import io
from pathlib import Path
import pytest
import os
import montepy
from montepy.data_inputs import material, volume
from montepy.input_parser.mcnp_input import (
Input,
Jump,
Message,
Title,
)
from montepy.errors import *
from montepy.particle import Particle
import numpy as np
from tests import constants
@pytest.fixture(scope="module")
def simple_problem():
return montepy.read_input(os.path.join("tests", "inputs", "test.imcnp"))
@pytest.fixture(scope="module")
def importance_problem():
return montepy.read_input(os.path.join("tests", "inputs", "test_importance.imcnp"))
@pytest.fixture(scope="module")
def universe_problem():
return montepy.read_input(os.path.join("tests", "inputs", "test_universe.imcnp"))
@pytest.fixture(scope="module")
def data_universe_problem():
return montepy.read_input(
os.path.join("tests", "inputs", "test_universe_data.imcnp")
)
def test_original_input(simple_problem):
cell_order = [Message, Title] + [Input] * 29
for i, input_ob in enumerate(simple_problem.original_inputs):
assert isinstance(input_ob, cell_order[i])
def test_original_input_dos():
dos_problem = montepy.read_input(os.path.join("tests", "inputs", "test_dos.imcnp"))
cell_order = [Message, Title] + [Input] * 18
for i, input_ob in enumerate(dos_problem.original_inputs):
assert isinstance(input_ob, cell_order[i])
def test_original_input_tabs():
problem = montepy.read_input(os.path.join("tests", "inputs", "test_tab.imcnp"))
cell_order = [Message, Title] + [Input] * 17
for i, input_ob in enumerate(problem.original_inputs):
assert isinstance(input_ob, cell_order[i])
# TODO formalize this or see if this is covered by other tests.
def test_lazy_comments_check(simple_problem):
material2 = simple_problem.materials[2]
for comment in material2._tree.comments:
print(repr(comment))
print(material2._tree.get_trailing_comment())
def test_moving_trail_comments(universe_problem):
problem = copy.deepcopy(universe_problem)
mat = problem.materials[2]
idx = problem.data_inputs.index(mat)
dat = problem.data_inputs[idx - 1]
assert len(mat.comments) == 1
assert len(mat.leading_comments) == 1
assert len(dat.leading_comments) == 0
assert len(dat.comments) == 0
def test_material_parsing(simple_problem):
mat_numbers = [1, 2, 3]
for i, mat in enumerate(simple_problem.materials):
assert mat.number == mat_numbers[i]
def test_surface_parsing(simple_problem):
surf_numbers = [1000, 1005, 1010, 1015, 1020, 1025]
for i, surf in enumerate(simple_problem.surfaces):
assert surf.number == surf_numbers[i]
def test_data_card_parsing(simple_problem):
M = material.Material
V = volume.Volume
cards = [
M,
M,
M,
"FC1 SURFACE CURRENT",
"F1:N,P",
"FC2 AVERAGE SURFACE FLUX",
"F2:P",
"FC4 2-GROUP FLUX",
"F4:N",
"E4",
"F6:P",
"F7:N",
"KSRC",
"KCODE",
"PHYS:P",
"MODE",
V,
]
for i, card in enumerate(simple_problem.data_inputs):
if isinstance(cards[i], str):
assert card.classifier.format().upper().rstrip() == cards[i]
else:
assert isinstance(card, cards[i])
if i == 2:
assert card.thermal_scattering is not None
def test_cells_parsing_linking(simple_problem):
cell_numbers = [1, 2, 3, 99, 5]
mats = simple_problem.materials
mat_answer = [mats[1], mats[2], mats[3], None, None]
surfs = simple_problem.surfaces
surf_answer = [
{surfs[1000]},
{surfs[1005], *surfs[1015:1026]},
set(surfs[1000:1011]),
{surfs[1010]},
set(),
]
cells = simple_problem.cells
complements = [set()] * 4 + [{cells[99]}]
for i, cell in enumerate(simple_problem.cells):
print(cell)
print(surf_answer[i])
assert cell.number == cell_numbers[i]
assert cell.material == mat_answer[i]
surfaces = set(cell.surfaces)
assert surfaces.union(surf_answer[i]) == surfaces
assert set(cell.complements).union(complements[i]) == complements[i]
def test_message(simple_problem):
lines = ["this is a message", "it should show up at the beginning", "foo"]
for i, line in enumerate(simple_problem.message.lines):
assert line == lines[i]
def test_title(simple_problem):
answer = "MCNP Test Model for MOAA"
assert answer == simple_problem.title.title
def test_read_card_recursion():
problem = montepy.read_input("tests/inputs/testReadRec1.imcnp")
assert len(problem.cells) == 1
assert len(problem.surfaces) == 1
assert montepy.particle.Particle.PHOTON in problem.mode
def test_problem_str(simple_problem):
output = str(simple_problem)
assert "MCNP problem for: tests/inputs/test.imcnp" in output
def test_write_to_file(simple_problem):
out = "foo.imcnp"
try:
problem = copy.deepcopy(simple_problem)
problem.write_to_file(out)
with open(out, "r") as fh:
for line in fh:
print(line.rstrip())
test_problem = montepy.read_input(out)
for i, cell in enumerate(simple_problem.cells):
num = cell.number
assert num == test_problem.cells[num].number
for attr in {"universe", "volume"}:
print(f"testing the attribute: {attr}")
gold = getattr(simple_problem.cells[num], attr)
test = getattr(test_problem.cells[num], attr)
if attr in {"universe"} and gold is not None:
gold, test = (gold.number, test.number)
assert gold == test
for i, surf in enumerate(simple_problem.surfaces):
num = surf.number
assert surf.number == test_problem.surfaces[num].number
for i, data in enumerate(simple_problem.data_inputs):
if isinstance(data, material.Material):
assert data.number == test_problem.data_inputs[i].number
if data.thermal_scattering is not None:
assert test_problem.data_inputs[i].thermal_scattering is not None
elif isinstance(data, volume.Volume):
assert str(data) == str(test_problem.data_inputs[i])
else:
print("Rewritten data", data.data)
print("Original input data", test_problem.data_inputs[i].data)
assert str(data.data) == str(test_problem.data_inputs[i].data)
finally:
if os.path.exists(out):
os.remove(out)
def test_cell_material_setter(simple_problem):
cell = copy.deepcopy(simple_problem.cells[1])
mat = simple_problem.materials[2]
cell.material = mat
assert cell.material == mat
cell.material = None
assert cell.material is None
with pytest.raises(TypeError):
cell.material = 5
def test_problem_cells_setter(simple_problem):
problem = copy.deepcopy(simple_problem)
cells = copy.deepcopy(simple_problem.cells)
cells.remove(cells[1])
with pytest.raises(TypeError):
problem.cells = 5
with pytest.raises(TypeError):
problem.cells = [5]
with pytest.raises(TypeError):
problem.cells.append(5)
problem.cells = cells
assert problem.cells.objects == cells.objects
problem.cells = list(cells)
assert problem.cells[2] == cells[2]
# test that cell modifiers are still there
problem.cells._importance.format_for_mcnp_input((6, 2, 0))
def test_problem_test_setter(simple_problem):
problem = copy.deepcopy(simple_problem)
sample_title = "This is a title"
problem.title = sample_title
assert problem.title.title == sample_title
with pytest.raises(TypeError):
problem.title = 5
def test_problem_children_adder(simple_problem):
problem = copy.deepcopy(simple_problem)
BT = montepy.input_parser.block_type.BlockType
in_str = "5 SO 5.0"
card = montepy.input_parser.mcnp_input.Input([in_str], BT.SURFACE)
surf = montepy.surfaces.surface_builder.surface_builder(card)
in_str = "M5 6000.70c 1.0"
card = montepy.input_parser.mcnp_input.Input([in_str], BT.SURFACE)
mat = montepy.data_inputs.material.Material(card)
in_str = "TR1 0 0 1"
input = montepy.input_parser.mcnp_input.Input([in_str], BT.DATA)
transform = montepy.data_inputs.transform.Transform(input)
surf.transform = transform
cell_num = 1000
cell = montepy.Cell()
cell.material = mat
cell.geometry = -surf
cell.mass_density = 1.0
cell.number = cell_num
cell.universe = problem.universes[350]
problem.cells.append(cell)
problem.add_cell_children_to_problem()
assert surf in problem.surfaces
assert mat in problem.materials
assert mat in problem.data_inputs
assert transform in problem.transforms
assert transform in problem.data_inputs
for cell_num in [1, cell_num]:
print(cell_num)
if cell_num == 1000:
with pytest.warns(LineExpansionWarning):
output = problem.cells[cell_num].format_for_mcnp_input((6, 2, 0))
else:
output = problem.cells[cell_num].format_for_mcnp_input((6, 2, 0))
print(output)
assert "U=350" in "\n".join(output).upper()
def test_problem_mcnp_version_setter(simple_problem):
problem = copy.deepcopy(simple_problem)
with pytest.raises(ValueError):
problem.mcnp_version = (4, 5, 3)
problem.mcnp_version = (6, 2, 5)
assert problem.mcnp_version == (6, 2, 5)
def test_problem_duplicate_surface_remover():
problem = montepy.read_input("tests/inputs/test_redundant_surf.imcnp")
nums = list(problem.surfaces.numbers)
survivors = (
nums[0:3] + nums[5:8] + [nums[9]] + nums[11:13] + [nums[13]] + [nums[-2]]
)
problem.remove_duplicate_surfaces(1e-4)
assert list(problem.surfaces.numbers) == survivors
cell_surf_answer = "-1 3 -6"
assert cell_surf_answer in problem.cells[2].format_for_mcnp_input((6, 2, 0))[1]
def test_surface_periodic():
problem = montepy.read_input("tests/inputs/test_surfaces.imcnp")
surf = problem.surfaces[1]
periodic = problem.surfaces[2]
assert surf.periodic_surface == periodic
assert "1 -2 SO" in surf.format_for_mcnp_input((6, 2, 0))[0]
surf.periodic_surface = problem.surfaces[3]
assert surf.periodic_surface == problem.surfaces[3]
del surf.periodic_surface
assert surf.periodic_surface is None
with pytest.raises(TypeError):
surf.periodic_surface = 5
def test_surface_transform():
problem = montepy.read_input("tests/inputs/test_surfaces.imcnp")
surf = problem.surfaces[1]
transform = problem.data_inputs[0]
del surf.periodic_surface
surf.transform = transform
assert surf.transform == transform
assert "1 1 SO" in surf.format_for_mcnp_input((6, 2, 0))[0]
del surf.transform
assert surf.transform is None
assert "1 SO" in surf.format_for_mcnp_input((6, 2, 0))[0]
with pytest.raises(TypeError):
surf.transform = 5
def test_materials_setter(simple_problem):
problem = copy.deepcopy(simple_problem)
with pytest.raises(TypeError):
problem.materials = 5
with pytest.raises(TypeError):
problem.materials = [5]
size = len(problem.materials)
problem.materials = list(problem.materials)
assert len(problem.materials) == size
problem.materials = problem.materials
assert len(problem.materials) == size
def test_reverse_pointers(simple_problem):
problem = simple_problem
complements = list(problem.cells[99].cells_complementing_this)
assert problem.cells[5] in complements
assert len(complements) == 1
cells = list(problem.materials[1].cells)
assert problem.cells[1] in cells
assert len(cells) == 1
cells = list(problem.surfaces[1005].cells)
assert problem.cells[2] in problem.surfaces[1005].cells
assert len(cells) == 2
def test_surface_card_pass_through():
problem = montepy.read_input("tests/inputs/test_surfaces.imcnp")
surf = problem.surfaces[1]
# Test input pass through
answer = ["1 -2 SO -5"]
assert surf.format_for_mcnp_input((6, 2, 0)) == answer
# Test changing periodic surface
new_prob = copy.deepcopy(problem)
surf = new_prob.surfaces[1]
new_prob.surfaces[2].number = 5
assert int(surf.format_for_mcnp_input((6, 2, 0))[0].split()[1]) == -5
# Test changing transform
new_prob = copy.deepcopy(problem)
surf = new_prob.surfaces[4]
surf.transform.number = 5
assert int(surf.format_for_mcnp_input((6, 2, 0))[0].split()[1]) == 5
# test changing surface constants
new_prob = copy.deepcopy(problem)
surf = new_prob.surfaces[4]
surf.location = 2.5
assert float(surf.format_for_mcnp_input((6, 2, 0))[0].split()[-1]) == 2.5
def test_surface_broken_link():
with pytest.raises(montepy.errors.MalformedInputError):
montepy.read_input("tests/inputs/test_broken_surf_link.imcnp")
with pytest.raises(montepy.errors.MalformedInputError):
montepy.read_input("tests/inputs/test_broken_transform_link.imcnp")
def test_material_broken_link():
with pytest.raises(montepy.errors.BrokenObjectLinkError):
problem = montepy.read_input("tests/inputs/test_broken_mat_link.imcnp")
def test_cell_surf_broken_link():
with pytest.raises(montepy.errors.BrokenObjectLinkError):
problem = montepy.read_input("tests/inputs/test_broken_cell_surf_link.imcnp")
def test_cell_complement_broken_link():
with pytest.raises(montepy.errors.BrokenObjectLinkError):
problem = montepy.read_input("tests/inputs/test_broken_complement.imcnp")
def test_cell_card_pass_through(simple_problem):
problem = copy.deepcopy(simple_problem)
cell = problem.cells[1]
# test input pass-through
answer = [
"C cells",
"c # hidden vertical Do not touch",
"c",
"1 1 20",
" -1000 $ dollar comment",
" imp:n,p=1 U=350 trcl=5 ",
]
assert cell.format_for_mcnp_input((6, 2, 0)) == answer
# test surface change
new_prob = copy.deepcopy(problem)
new_prob.surfaces[1000].number = 5
cell = new_prob.cells[1]
output = cell.format_for_mcnp_input((6, 2, 0))
print(output)
assert int(output[4].split("$")[0]) == -5
# test mass density printer
cell.mass_density = 10.0
with pytest.warns(LineExpansionWarning):
output = cell.format_for_mcnp_input((6, 2, 0))
print(output)
assert pytest.approx(float(output[3].split()[2])) == -10
# ensure that surface number updated
# Test material number change
new_prob = copy.deepcopy(problem)
new_prob.materials[1].number = 5
cell = new_prob.cells[1]
output = cell.format_for_mcnp_input((6, 2, 0))
assert int(output[3].split()[1]) == 5
def test_thermal_scattering_pass_through(simple_problem):
problem = copy.deepcopy(simple_problem)
mat = problem.materials[3]
therm = mat.thermal_scattering
mat.number = 5
assert therm.format_for_mcnp_input((6, 2, 0)) == ["MT5 lwtr.23t h-zr.20t h/zr.28t"]
def test_cutting_comments_parse():
problem = montepy.read_input("tests/inputs/breaking_comments.imcnp")
comments = problem.cells[1].comments
assert len(comments) == 3
assert "this is a cutting comment" in list(comments)[2].contents
comments = problem.materials[2].comments
assert len(comments) == 2
def test_cutting_comments_print_no_mutate():
problem = montepy.read_input("tests/inputs/breaking_comments.imcnp")
cell = problem.cells[1]
output = cell.format_for_mcnp_input((6, 2, 0))
assert len(output) == 5
assert "c this is a cutting comment" == output[3]
material2 = problem.materials[2]
output = material2.format_for_mcnp_input((6, 2, 0))
assert len(output) == 5
assert "c 26057.80c 2.12" == output[3]
def test_cutting_comments_print_mutate():
problem = montepy.read_input("tests/inputs/breaking_comments.imcnp")
cell = problem.cells[1]
cell.number = 8
output = cell.format_for_mcnp_input((6, 2, 0))
print(output)
assert len(output) == 5
assert "c this is a cutting comment" == output[3]
material2 = problem.materials[2]
material2.number = 5
output = material2.format_for_mcnp_input((6, 2, 0))
print(output)
assert len(output) == 5
assert "c 26057.80c 2.12" == output[3]
def test_comments_setter(simple_problem):
cell = copy.deepcopy(simple_problem.cells[1])
comment = simple_problem.surfaces[1000].comments[0]
cell.leading_comments = [comment]
assert cell.comments[0] == comment
cell.leading_comments = comment
assert cell.comments[0] == comment
with pytest.raises(TypeError):
cell.leading_comments = [5]
with pytest.raises(TypeError):
cell.leading_comments = 5
def test_problem_linker():
cell = montepy.Cell()
with pytest.raises(TypeError):
cell.link_to_problem(5)
def test_importance_parsing(importance_problem, simple_problem):
cell = importance_problem.cells[1]
assert cell.importance.neutron == 1.0
assert cell.importance.photon == 1.0
assert cell.importance.electron == 0.0
cell = simple_problem.cells[1]
assert cell.importance.neutron == 1.0
assert cell.importance.photon == 1.0
def test_importance_format_unmutated(importance_problem):
imp = importance_problem.cells._importance
output = imp.format_for_mcnp_input((6, 2, 0))
print(output)
assert len(output) == 4
assert "imp:n,p 1 1 1 0 3" == output[1]
assert "imp:e 0 2r 1 r" == output[3]
def test_importance_format_mutated(importance_problem):
problem = copy.deepcopy(importance_problem)
imp = problem.cells._importance
problem.cells[1].importance.neutron = 0.5
with pytest.warns(LineExpansionWarning):
output = imp.format_for_mcnp_input((6, 2, 0))
print(output)
assert len(output) == 6
assert "imp:n 0.5 1 1 0 3" in output
assert "c special comment related to #520" == output[2]
def test_importance_write_unmutated(importance_problem):
fh = io.StringIO()
importance_problem.write_problem(fh)
found_np = False
found_e = False
fh.seek(0)
for line in fh.readlines():
print(line.rstrip())
if "imp:n,p 1" in line:
found_np = True
elif "imp:e" in line:
found_e = True
assert found_np
assert found_e
fh.close()
def test_importance_write_mutated(importance_problem):
fh = io.StringIO()
problem = copy.deepcopy(importance_problem)
problem.cells[1].importance.neutron = 0.5
with pytest.warns(LineExpansionWarning):
problem.write_problem(fh)
found_n = False
found_e = False
fh.seek(0)
for line in fh.readlines():
print(line.rstrip())
if "imp:n 0.5" in line:
found_n = True
elif "imp:e" in line:
found_e = True
assert found_n
assert found_e
fh.close()
def test_importance_write_cell(importance_problem):
for state in ["no change", "new unmutated cell", "new mutated cell"]:
fh = io.StringIO()
problem = copy.deepcopy(importance_problem)
if "new" in state:
cell = copy.deepcopy(problem.cells[5])
cell.number = 999
problem.cells.append(cell)
problem.print_in_data_block["imp"] = False
if "new" in state:
with pytest.warns(LineExpansionWarning):
problem.write_problem(fh)
else:
problem.write_problem(fh)
found_np = False
found_e = False
found_data_np = False
fh.seek(0)
for line in fh.readlines():
print(line.rstrip())
if "imp:n,p=1" in line:
found_np = True
elif "imp:e=1" in line:
found_e = True
elif "imp:e 1" in line.lower():
found_data_np = True
assert found_np
assert found_e
assert not found_data_np
fh.close()
def test_importance_write_data(simple_problem):
fh = io.StringIO()
problem = copy.deepcopy(simple_problem)
problem.print_in_data_block["imp"] = True
problem.write_problem(fh)
found_n = False
found_p = False
fh.seek(0)
for line in fh:
print(line.rstrip())
if "imp:n 1" in line:
found_n = True
if "imp:p 1 0.5" in line:
found_p = True
assert found_n
assert found_p
fh.close()
def test_avoid_blank_cell_modifier_write(simple_problem):
fh = io.StringIO()
problem = copy.deepcopy(simple_problem)
problem.print_in_data_block["U"] = True
problem.print_in_data_block["FILL"] = True
problem.print_in_data_block["LAT"] = True
problem.cells[5].fill.transform = None
problem.cells[5].fill.universe = None
problem.cells[1].universe = problem.universes[0]
for cell in problem.cells:
del cell.volume
problem.cells.allow_mcnp_volume_calc = True
problem.write_problem(fh)
found_universe = False
found_lattice = False
found_vol = False
found_fill = False
fh.seek(0)
for line in fh.readlines():
print(line.rstrip())
if "U " in line:
found_universe = True
if "LAT " in line:
found_lattice = True
if "FILL " in line:
found_fill = True
if "VOL " in line:
found_vol = True
assert not found_universe
assert not found_lattice
assert not found_vol
assert not found_fill
fh.close()
def test_set_mode(importance_problem):
problem = copy.deepcopy(importance_problem)
problem.set_mode("e p")
particles = {Particle.ELECTRON, Particle.PHOTON}
assert len(problem.mode) == 2
for part in particles:
assert part in problem.mode
def test_set_equal_importance(importance_problem):
problem = copy.deepcopy(importance_problem)
problem.cells.set_equal_importance(0.5, [5])
for cell in problem.cells:
for particle in problem.mode:
if cell.number != 5:
print(cell.number, particle)
assert cell.importance[particle] == 0.5
for particle in problem.mode:
print(5, particle)
assert problem.cells[5].importance[particle] == 0.0
problem.cells.set_equal_importance(0.75, [problem.cells[99]])
for cell in problem.cells:
for particle in problem.mode:
if cell.number != 99:
print(cell.number, particle)
assert cell.importance[particle] == 0.75
for particle in problem.mode:
print(99, particle)
assert problem.cells[99].importance[particle] == 0.0
with pytest.raises(TypeError):
problem.cells.set_equal_importance("5", [5])
with pytest.raises(ValueError):
problem.cells.set_equal_importance(-0.5, [5])
with pytest.raises(TypeError):
problem.cells.set_equal_importance(5, "a")
with pytest.raises(TypeError):
problem.cells.set_equal_importance(5, ["a"])
def test_check_volume_calculated(simple_problem):
assert not simple_problem.cells[1].volume_mcnp_calc
def test_redundant_volume():
with pytest.raises(montepy.errors.MalformedInputError):
montepy.read_input(os.path.join("tests", "inputs", "test_vol_redundant.imcnp"))
def test_delete_vol(simple_problem):
problem = copy.deepcopy(simple_problem)
del problem.cells[1].volume
assert not problem.cells[1].volume_is_set
def test_enable_mcnp_vol_calc(simple_problem):
problem = copy.deepcopy(simple_problem)
problem.cells.allow_mcnp_volume_calc = True
assert problem.cells.allow_mcnp_volume_calc
assert "NO" not in str(problem.cells._volume)
problem.cells.allow_mcnp_volume_calc = False
assert "NO" in str(problem.cells._volume)
with pytest.raises(TypeError):
problem.cells.allow_mcnp_volume_calc = 5
def test_cell_multi_volume():
in_str = "1 0 -1 VOL=1 VOL 5"
with pytest.raises(ValueError):
montepy.Cell(Input([in_str], montepy.input_parser.block_type.BlockType.CELL))
def test_universe_cell_parsing(simple_problem):
answers = [350] + [0] * 4
for cell, answer in zip(simple_problem.cells, answers):
print(cell, answer)
assert cell.universe.number == answer
def test_universe_fill_data_parsing(data_universe_problem):
answers = [350, 0, 0, 1]
for cell, answer in zip(data_universe_problem.cells, answers):
print(cell, answer)
assert cell.universe.number == answer
for cell in data_universe_problem.cells:
print(cell)
if cell.number != 99:
assert not cell.not_truncated
else:
assert cell.not_truncated
assert data_universe_problem.cells[99].not_truncated
answers = [None, None, 350, None, None]
for cell, answer in zip(data_universe_problem.cells, answers):
print(cell.number, cell.fill.universe, answer)
if answer is None:
assert cell.fill.universe is None
else:
assert cell.fill.universe.number == answer
def test_universe_cells1(data_universe_problem):
answers = {350: [1], 0: [2, 3, 5], 1: [99]}
for uni_number, cell_answers in answers.items():
for cell, answer in zip(
data_universe_problem.universes[uni_number].cells, cell_answers
):
assert cell.number == answer
def test_cell_not_truncate_setter(simple_problem):
problem = copy.deepcopy(simple_problem)
cell = problem.cells[1]
cell.not_truncated = True
assert cell.not_truncated
with pytest.raises(ValueError):
cell = problem.cells[2]
cell.not_truncated = True
def test_universe_setter(simple_problem):
problem = copy.deepcopy(simple_problem)
universe = problem.universes[350]
cell = problem.cells[3]
cell.universe = universe
assert cell.universe == universe
assert cell.universe.number == 350
with pytest.raises(TypeError):
cell.universe = 5
def test_universe_cell_formatter(simple_problem):
problem = copy.deepcopy(simple_problem)
universe = problem.universes[350]
cell = problem.cells[3]
cell.universe = universe
cell.not_truncated = True
with pytest.warns(LineExpansionWarning):
output = cell.format_for_mcnp_input((6, 2, 0))
assert "U=-350" in " ".join(output)
def test_universe_data_formatter(data_universe_problem):
problem = copy.deepcopy(data_universe_problem)
# test unmutated
output = problem.cells._universe.format_for_mcnp_input((6, 2, 0))
print(output)
assert "u 350 2J -1" in output
universe = problem.universes[350]
# test mutated
cell = problem.cells[3]
cell.universe = universe
cell.not_truncated = True
with pytest.warns(LineExpansionWarning):
output = problem.cells._universe.format_for_mcnp_input((6, 2, 0))
print(output)
assert "u 350 J -350 -1" in output
# test appending a new mutated cell
new_cell = copy.deepcopy(cell)
new_cell.number = 1000
new_cell.universe = universe
new_cell.not_truncated = False
problem.cells.append(new_cell)
with pytest.warns(LineExpansionWarning):
output = problem.cells._universe.format_for_mcnp_input((6, 2, 0))
print(output)
assert "u 350 J -350 -1 J 350 " in output
# test appending a new UNmutated cell
problem = copy.deepcopy(data_universe_problem)
cell = problem.cells[3]
new_cell = copy.deepcopy(cell)
new_cell.number = 1000
new_cell.universe = universe
new_cell.not_truncated = False
# lazily implement pulling cell in from other model
new_cell._mutated = False
new_cell._universe._mutated = False
problem.cells.append(new_cell)
with pytest.warns(LineExpansionWarning):
output = problem.cells._universe.format_for_mcnp_input((6, 2, 0))
print(output)
assert "u 350 2J -1 J 350 " in output
def test_universe_number_collision():
problem = montepy.read_input(
os.path.join("tests", "inputs", "test_universe_data.imcnp")
)
with pytest.raises(montepy.errors.NumberConflictError):
problem.universes[0].number = 350
with pytest.raises(montepy.errors.NumberConflictError):
problem.universes[350].number = 0
def test_universe_repr(simple_problem):
uni = simple_problem.universes[0]
output = repr(uni)
assert "Number: 0" in output
assert "Problem: set" in output
assert "Cells: [2" in output
def test_lattice_format_data(simple_problem):
problem = copy.deepcopy(simple_problem)
cells = problem.cells
cells[1].lattice = 1
cells[99].lattice = 2
answer = "LAT 1 2J 2"
output = cells._lattice.format_for_mcnp_input((6, 2, 0))
assert answer in output[0]
def test_lattice_push_to_cells(simple_problem):
problem = copy.deepcopy(simple_problem)
lattices = [1, 2, Jump(), Jump()]
card = Input(
["Lat " + " ".join(list(map(str, lattices)))],
montepy.input_parser.block_type.BlockType.DATA,
)
lattice = montepy.data_inputs.lattice_input.LatticeInput(card)
lattice.link_to_problem(problem)
lattice.push_to_cells()
for cell, answer in zip(problem.cells, lattices):
print(cell.number, answer)
if isinstance(answer, int):
assert cell.lattice.value == answer
else:
assert cell.lattice is None
def test_universe_problem_parsing(universe_problem):
for cell in universe_problem.cells:
if cell.number == 1:
assert cell.universe.number == 1
else:
assert cell.universe.number == 0
def test_importance_end_repeat(universe_problem):
problem = copy.deepcopy(universe_problem)
for cell in problem.cells:
if cell.number in {99, 5}:
cell.importance.photon = 1.0
else:
cell.importance.photon = 0.0
problem.print_in_data_block["IMP"] = True
output = problem.cells._importance.format_for_mcnp_input((6, 2, 0))
# OG value was 0.5 so 0.0 is correct.
assert "imp:p 0 0.0" in output
def test_fill_parsing(universe_problem):
answers = [None, np.array([[[1], [0]], [[0], [1]]]), None, 1, 1]
for cell, answer in zip(universe_problem.cells, answers):
if answer is None:
assert cell.fill.universe is None
elif isinstance(answer, np.ndarray):
assert cell.fill.multiple_universes
assert (cell.fill.min_index == np.array([0.0, 0.0, 0.0])).all()
assert (cell.fill.max_index == np.array([1.0, 1.0, 0.0])).all()
assert cell.fill.universes[0][0][0].number == answer[0][0][0]
assert cell.fill.universes[1][1][0].number == answer[1][1][0]
assert cell.fill.transform == universe_problem.transforms[5]
else:
assert cell.fill.universe.number == answer
def test_fill_transform_setter(universe_problem):
problem = copy.deepcopy(universe_problem)
transform = problem.transforms[5]
cell = problem.cells[5]
cell.fill.transform = transform
assert cell.fill.transform == transform
assert not cell.fill.hidden_transform
cell.fill.transform = None
assert cell.fill.transform is None
with pytest.raises(TypeError):
cell.fill.transform = "hi"
cell.fill.transform = transform
del cell.fill.transform
assert cell.fill.transform is None
def test_fill_cell_format(simple_problem, universe_problem):
problem = copy.deepcopy(universe_problem)
fill = problem.cells[5].fill
output = fill.format_for_mcnp_input((6, 2, 0))
answer = "fill=1 (1 0.0 0.0)"
assert output[0] == answer
# test *fill
fill.transform.is_in_degrees = True
output = fill.format_for_mcnp_input((6, 2, 0))
answer = "*fill=1 (1 0.0 0.0)"
assert output[0] == answer
# test changing the transform
fill.transform.displacement_vector[0] = 2.0
output = fill.format_for_mcnp_input((6, 2, 0))
assert output[0] == "*fill=1 (2 0.0 0.0)"
# test without transform
fill.transform = None
answer = "fill=1 "
output = fill.format_for_mcnp_input((6, 2, 0))
assert output[0] == answer
# test with no fill
fill.universe = None
output = fill.format_for_mcnp_input((6, 2, 0))
assert len(output) == 0
# test with complex universe lattice fill
fill = problem.cells[2].fill
output = fill.format_for_mcnp_input((6, 2, 0))
answers = ["fill= 0:1 0:1 0:0 1 0 R 1 (5)"]
assert output == answers
problem.print_in_data_block["FILL"] = True
# test that complex fill is not printed in data block
with pytest.raises(ValueError):
problem.cells._fill.format_for_mcnp_input((6, 2, 0))
problem = copy.deepcopy(simple_problem)
problem.cells[5].fill.transform = None
problem.print_in_data_block["FILL"] = True
output = problem.cells._fill.format_for_mcnp_input((6, 2, 0))
assert output == ["FILL 4J 350 "]
def test_universe_cells_claim(universe_problem):
problem = copy.deepcopy(universe_problem)
universe = problem.universes[1]
universe.claim(problem.cells[2])
assert problem.cells[2].universe == universe
universe = montepy.Universe(5)
problem.universes.append(universe)
universe.claim(problem.cells)
for cell in problem.cells:
assert cell.universe == universe
with pytest.raises(TypeError):
universe.claim("hi")
with pytest.raises(TypeError):
universe.claim(["hi"])
def test_universe_cells2(universe_problem):