Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable Pickling of Tallies #468

Merged
merged 13 commits into from
Oct 29, 2024
Merged
7 changes: 7 additions & 0 deletions doc/source/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ MontePy Changelog
0.5 releases
============

#Next version#
--------------

**Bug Fixes**

* Fixed bug where tally inputs in a file prevented the file from being pickled or copied (:issue:`463`).

0.5.0
--------------

Expand Down
3 changes: 3 additions & 0 deletions montepy/data_inputs/data_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,9 @@ def update_pointers(self, data_inputs):
def __str__(self):
return f"DATA INPUT: {self._tree['classifier']}"

def __repr__(self):
return str(self)

def __split_name(self, input):
"""
Parses the name of the data input as a prefix, number, and a particle classifier.
Expand Down
1 change: 1 addition & 0 deletions montepy/input_parser/parser_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ def gen_wrapper():
# treat any previous errors as being fatal even if it recovered.
if len(self.log) > 0:
return None
self.tokens = {}
return tree

precedence = (("left", SPACE), ("left", TEXT))
Expand Down
2 changes: 1 addition & 1 deletion montepy/input_parser/tally_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def tally(self, p):
ret = {}
for key, node in p.introduction.nodes.items():
ret[key] = node
ret["tally"] = p.tally_specification
ret["data"] = p.tally_specification
return syntax_node.SyntaxNode("data", ret)

@_("tally_numbers", "tally_numbers end_phrase")
Expand Down
7 changes: 4 additions & 3 deletions montepy/mcnp_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -450,9 +450,10 @@ def allowed_keywords(self): # pragma: no cover

def __getstate__(self):
state = self.__dict__.copy()
weakref_key = "_problem_ref"
if weakref_key in state:
del state[weakref_key]
bad_keys = {"_problem_ref", "_parser"}
for key in bad_keys:
if key in state:
del state[key]
return state

def __setstate__(self, crunchy_data):
Expand Down
10 changes: 10 additions & 0 deletions tests/inputs/test.imcnp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,16 @@ m3 1001.80c 2
8016.80c 1
plib=84p
MT3 lwtr.23t h-zr.20t h/zr.28t
C tallies
fc1 Surface current
f1:n,p 1000
fc2 Average surface flux
f2:p 1005
fc4 2-group flux
f4:n 1 2 3
e4 0.625e-6
f6:p 1
f7:n 1
C execution
ksrc 0 0 0
kcode 100000 1.000 50 1050
Expand Down
24 changes: 21 additions & 3 deletions tests/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def data_universe_problem():


def test_original_input(simple_problem):
cell_order = [Message, Title] + [Input] * 17
cell_order = [Message, Title] + [Input] * 26
for i, input_ob in enumerate(simple_problem.original_inputs):
assert isinstance(input_ob, cell_order[i])

Expand Down Expand Up @@ -97,7 +97,25 @@ def test_surface_parsing(simple_problem):
def test_data_card_parsing(simple_problem):
M = material.Material
V = volume.Volume
cards = [M, M, M, "KSRC", "KCODE", "PHYS:P", "MODE", V]
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]
Expand Down Expand Up @@ -178,7 +196,7 @@ def test_write_to_file(simple_problem):
else:
print("Rewritten data", data.data)
print("Original input data", test_problem.data_inputs[i].data)
assert data.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)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_syntax_parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1347,7 +1347,7 @@ def testReadInput(self):
)
mcnp_in = montepy.input_parser.mcnp_input
input_order = [mcnp_in.Message, mcnp_in.Title]
input_order += [mcnp_in.Input] * 17
input_order += [mcnp_in.Input] * 26
for i, input in enumerate(generator):
print(input.input_lines)
print(input_order[i])
Expand Down