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

Fix issue with shortcut recompression #520

Merged
merged 75 commits into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
75 commits
Select commit Hold shift + click to select a range
9ab65f6
Implemented reading input from a stream.
MicahGale Aug 28, 2024
97683b9
Test read/write all input files.
MicahGale Aug 28, 2024
c5534ae
Made verification tests harder.
MicahGale Aug 28, 2024
c260d75
Replicated #499 by forcing a recompression.
MicahGale Aug 28, 2024
94233d4
Dumped whole file to stdout.
MicahGale Aug 28, 2024
e377297
Expand tabs and update test to new inputs.
MicahGale Aug 28, 2024
37cf930
tried to use interpolation for test.
MicahGale Aug 28, 2024
0eab3c9
Actually replicated #499 with multiple shortcuts in a line.
MicahGale Aug 28, 2024
2f92864
Updated test again with new importance gold.
MicahGale Aug 28, 2024
c577a88
Fixed logic to detect when you can smash repeats together.
MicahGale Aug 28, 2024
c4b56ac
Updated logic to test for matching value with valueNode.
MicahGale Aug 28, 2024
4f71d4a
Undid debugging.
MicahGale Aug 28, 2024
4284c03
Updated logic to always check for edge node matching.
MicahGale Aug 28, 2024
4c7265e
Updated tests to cover more edge cases.
MicahGale Aug 29, 2024
1529886
Made logic clearer and more pythonic.
MicahGale Aug 29, 2024
5e0f8ab
Removed implicit importance to avoid parrot out bug.
MicahGale Aug 29, 2024
2d3aa54
Fixed bug in validate for cylinder_on_axis.
MicahGale Aug 29, 2024
8ff6b69
Made it possible to have integer interpolations.
MicahGale Aug 29, 2024
73800b8
Started framework for geometry tree recompressing interpolations.
MicahGale Aug 29, 2024
b8b8b3b
Switched to preserving shortcut type for geometry stuff.
MicahGale Aug 29, 2024
0582457
Exposed shortcut type.
MicahGale Aug 29, 2024
dd16501
Made system to make a shortcutNode from scratch kind of.
MicahGale Aug 29, 2024
e9d2e4e
Formatted shortcuts inside of GeometryTree
MicahGale Aug 29, 2024
5d17fd1
Updated tests to avoid complex reads and implicit importances.
MicahGale Aug 29, 2024
ba4ce30
Replicated #352.
MicahGale Aug 30, 2024
0f79257
Fixed test to expect three leading comments.
MicahGale Aug 30, 2024
fabfb52
Made system to ignore default parameters for trailing comments.
MicahGale Aug 30, 2024
f033d97
Avoid cross-block trailing comment transfer.
MicahGale Aug 30, 2024
3edca07
Updated delete trailing comment to match new get_trailing_comment.
MicahGale Aug 30, 2024
08a0c98
Skip empty nodes in the tail.
MicahGale Aug 30, 2024
bd3e31f
Updated test to be more thurough)
MicahGale Aug 31, 2024
081d4db
Updated ClassifierNode handling of padding for trailing and str.
MicahGale Aug 31, 2024
5029946
Updated get trailing comment logic to handle valueNodes in tree.
MicahGale Aug 31, 2024
f87c875
Updated complex logic for deleteing trailing comments.
MicahGale Aug 31, 2024
ee08583
Updated geometry expansion tests to be more exhaustive.
MicahGale Aug 31, 2024
aa06f22
Fixed test to be looser.
MicahGale Aug 31, 2024
3f7f277
fixed bugs in M in geometry.
MicahGale Aug 31, 2024
a00afe4
Added more edge cases.
MicahGale Aug 31, 2024
ab65de6
Started debugging.
MicahGale Aug 31, 2024
97bca51
Made system for tracking left and right shortuts
MicahGale Sep 2, 2024
24ca152
Updated shortcut finding logic to find shortcuts in the middle.
MicahGale Sep 2, 2024
a9517e8
Close enough with padding.
MicahGale Sep 2, 2024
aca2f85
Updated changelog with #520 PR.
MicahGale Sep 2, 2024
59058d0
Avoided marking shortcuts overriding old shortcut.
MicahGale Sep 2, 2024
b6ec5d4
Fixed over-appending leaves to the new list.
MicahGale Sep 2, 2024
b42f4a8
Added tests for mark_last_leaf_shortcut.
MicahGale Sep 3, 2024
847d2d2
Started testing for flatten_shortcut.
MicahGale Sep 3, 2024
06a44cb
Finished test for flatten_shortcuts.
MicahGale Sep 3, 2024
bfa81a8
Fixed test typos.
MicahGale Sep 3, 2024
0e15135
Fixed tests because I can't count at all.
MicahGale Sep 3, 2024
f7d140d
Fixed bug with excess depth in call.
MicahGale Sep 3, 2024
058b757
Cleaned up with @tjlaboss feedback.
MicahGale Sep 3, 2024
f7ff8ba
Added test replicate new edge case from @tjlaboss.
MicahGale Sep 3, 2024
9483f89
Added more exhaustive shortcut tests.
MicahGale Sep 3, 2024
6b6cdcb
Expanded using last node on formatting to all shortcuts, and handled …
MicahGale Sep 3, 2024
82d7c9e
Fixed test to be all caps.
MicahGale Sep 3, 2024
9430561
Updated logic for multiply shortcuts using neigbors.
MicahGale Sep 3, 2024
5a8b34a
Created system to track starting value for interpolate in geometry tr…
MicahGale Sep 3, 2024
628cd9d
Added doc strings for new functions.
MicahGale Sep 3, 2024
0848559
Merge branch 'develop' into 499-shortcuts-recompressed-incorrectly
MicahGale Sep 10, 2024
843db18
Merge branch 'develop' into 499-shortcuts-recompressed-incorrectly
tjlaboss Sep 10, 2024
c43e8cf
Triggered edge case from #526 #520
MicahGale Sep 10, 2024
128c651
Update gold value with new line.
MicahGale Sep 10, 2024
bfdb083
Tried to move the trailing comments around the special tree for now.
MicahGale Sep 10, 2024
92f52e4
Merge branch 'develop' into 499-shortcuts-recompressed-incorrectly
MicahGale Sep 10, 2024
9a88b22
Updated test with new input model.
MicahGale Sep 10, 2024
9acdc7f
Fixed potential repr bug.
MicahGale Sep 10, 2024
39a12c4
Tried to figure out where this stupid valuenode comment is coming from.
MicahGale Sep 10, 2024
35ffa26
Made test more robust.
MicahGale Sep 10, 2024
ff6b81f
Found where the syntax node actually lives; needs work.
MicahGale Sep 10, 2024
b8b1a2c
Removed forsed failure.
MicahGale Sep 11, 2024
9da343a
Setup system to track which particles were paired up.
MicahGale Sep 11, 2024
5882881
Updated to use actual trees, and ensured all affected trees cleared.
MicahGale Sep 11, 2024
1b559bc
Merge branch 'develop' into 499-shortcuts-recompressed-incorrectly
MicahGale Sep 11, 2024
a30549b
Fixed bug where the parameters weren't actually loaded into syntax tree.
MicahGale Sep 11, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions doc/source/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,17 @@ MontePy Changelog
#Next Version#
--------------

**Features Added**

* Added support for reading an input from either file paths or streams (file handles) with ``montepy.read_input`` (:issue:`519`).

**Bug Fixes**

* Fixed a bug where ``problem.materials.append_renumber`` would double add a material to ``problem.data_inputs`` (:issue:`516`).
* Fixed bug where material-level library specifications (e.g., ``m1 plib=84p``) could not be fully parsed (:issue:`521`).
* Fixed bug with shortcuts right after another shortcut (e.g., ``1 2M 3R``) not being properly recompressed on export (:issue:`499`).
* Fixed bug with shortcuts in cell geometry definitions not being recompressed on export (:issue:`489`).
* Fixed bug where leading comments were not always transferred to the appropriate input. (:issue:`352`, :issue:`526`).

**Performance Improvement**

Expand Down
2 changes: 1 addition & 1 deletion montepy/cell.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ def _parse_keyword_modifiers(self):
break
if (class_pref == "imp" and not has_imp) or class_pref != "imp":
tree = getattr(self, attr)._tree
self._tree["parameters"].append(tree)
self._tree["parameters"].append(tree, True)

def _load_blank_modifiers(self):
"""
Expand Down
45 changes: 45 additions & 0 deletions montepy/data_inputs/importance.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class Importance(CellModifierInput):
def __init__(self, input=None, in_cell_block=False, key=None, value=None):
self._particle_importances = {}
self._real_tree = {}
self._part_combos = []
super().__init__(input, in_cell_block, key, value)
if self.in_cell_block:
if key:
Expand All @@ -39,6 +40,7 @@ def __init__(self, input=None, in_cell_block=False, key=None, value=None):
raise ValueError(
f"Cell importance must be a number ≥ 0. {val.value} was given"
)
self._part_combos.append(self.particle_classifiers)
for particle in self.particle_classifiers:
self._particle_importances[particle] = value
elif input:
Expand All @@ -52,6 +54,7 @@ def __init__(self, input=None, in_cell_block=False, key=None, value=None):
raise MalformedInputError(
input, f"Importances must be ≥ 0 value: {node} given"
)
self._part_combos.append(self.particle_classifiers)
for particle in self.particle_classifiers:
self._particle_importances[particle] = copy.deepcopy(self._tree)
self._real_tree[particle] = copy.deepcopy(self._tree)
Expand Down Expand Up @@ -340,6 +343,48 @@ def _try_combine_values(self, new_vals, particle_pairings):
def _update_cell_values(self):
pass

@property
def trailing_comment(self):
"""
The trailing comments and padding of an input.

Generally this will be blank as these will be moved to be a leading comment for the next input.

:returns: the trailing ``c`` style comments and intermixed padding (e.g., new lines)
:rtype: list
"""
last_tree = list(self._real_tree.values())[-1]
if last_tree:
return last_tree.get_trailing_comment()

def _delete_trailing_comment(self):
for part, tree in reversed(self._real_tree.items()):
tree._delete_trailing_comment()
self.__delete_common_trailing(part)
break

def __delete_common_trailing(self, part):
to_delete = {part}
for combo_set in self._part_combos:
if part in combo_set:
to_delete |= combo_set
for part in to_delete:
self._particle_importances[part]["data"]._delete_trailing_comment()

def _grab_beginning_comment(self, new_padding):
last_tree = None
last_padding = None
for part, tree in self._particle_importances.items():
if last_padding is not None and last_tree is not None:
last_tree._grab_beginning_comment(last_padding)
self.__delete_common_trailing(part)
last_padding = tree.get_trailing_comment()
last_tree = tree
if new_padding:
next(iter(self._particle_importances.values()))[
"start_pad"
]._grab_beginning_comment(new_padding)


def _generate_default_data_tree(particle):
list_node = syntax_node.ListNode("number sequence")
Expand Down
2 changes: 1 addition & 1 deletion montepy/data_inputs/universe_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ def __repr__(self):
f"UNIVERSE: in_cell: {self._in_cell_block}"
f" set_in_block: {self.set_in_cell_block}, "
f"Universe : {self._universe}, "
f"Old Numbers: {self._old_numbers}"
f"Old Numbers: {self._old_numbers if hasattr(self, '_old_numbers') else ''}"
)
return ret

Expand Down
2 changes: 1 addition & 1 deletion montepy/data_inputs/volume.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ def __repr__(self):
ret = (
f"VOLUME: in_cell: {self._in_cell_block}, calc_by_mcnp: {self.is_mcnp_calculated},"
f" set_in_block: {self.set_in_cell_block}, "
f"Volume : {self._volume}"
f"Volume : {self._volume if hasattr(self, '_volume') else ''}"
)
return ret

Expand Down
8 changes: 7 additions & 1 deletion montepy/input_parser/cell_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,19 +112,25 @@ def geometry_term(self, p):
"geometry_term LOG_INTERPOLATE padding number_phrase",
)
def geometry_term(self, p):
shortcut = syntax_node.ShortcutNode(p)
shortcut = syntax_node.ShortcutNode(p, data_type=int)
node_iter = iter(shortcut.nodes)
if isinstance(p.geometry_term, syntax_node.GeometryTree):
left = p.geometry_term
left.mark_last_leaf_shortcut(shortcut.type)
else:
left = next(node_iter)
left_type = None
if isinstance(left, syntax_node.ValueNode) and left == shortcut.nodes[0]:
left_type = shortcut.type
for node in node_iter:
new_tree = syntax_node.GeometryTree(
"intersection",
{"left": left, "operator": syntax_node.PaddingNode(), "right": node},
"*",
left,
node,
left_short_type=left_type,
right_short_type=shortcut.type,
)
left = new_tree
return new_tree
Expand Down
10 changes: 6 additions & 4 deletions montepy/input_parser/input_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@
from montepy.constants import DEFAULT_VERSION


def read_input(input_file, mcnp_version=DEFAULT_VERSION, replace=True):
def read_input(destination, mcnp_version=DEFAULT_VERSION, replace=True):
"""
Reads the specified MCNP Input file.

The MCNP version must be a three component tuple e.g., (6, 2, 0) and (5, 1, 60).

.. note::
if a stream is provided. It will not be closed by this function.

:param input_file: the path to the input file to read.
:type input_file: str
:param destination: the path to the input file to read, or a readable stream.
:type destination: io.TextIOBase, str, os.PathLike
:param mcnp_version: The version of MCNP that the input is intended for.
:type mcnp_version: tuple
:returns: The MCNP_Problem instance representing this file.
Expand All @@ -24,7 +26,7 @@ def read_input(input_file, mcnp_version=DEFAULT_VERSION, replace=True):
:raises BrokenObjectLinkError: If a reference is made to an object that is not in the input file.
:raises UnknownElement: If an isotope is specified for an unknown element.
"""
problem = mcnp_problem.MCNP_Problem(input_file)
problem = mcnp_problem.MCNP_Problem(destination)
problem.mcnp_version = mcnp_version
problem.parse_input(replace=replace)
return problem
4 changes: 2 additions & 2 deletions montepy/input_parser/material_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ def material(self, p):
for key, node in p.introduction.nodes.items():
ret[key] = node
ret["data"] = p.isotopes
if hasattr(p, "parameters"):
ret["parameters"] = p.parameters
if len(p) > 2:
ret["parameters"] = p[2]
return syntax_node.SyntaxNode("data", ret)

@_("isotope_fractions", "number_sequence", "isotope_hybrid_fractions")
Expand Down
Loading