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 10 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
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
21 changes: 19 additions & 2 deletions montepy/input_parser/syntax_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -1834,7 +1834,7 @@ def _can_consume_node(self, node, direction, last_edge_shortcut=False):
:type node: ValueNode
:param direction: the direct to go in. Must be in {-1, 1}
:type direction: int
:param last_edge_shortcut: Whether or the previous node in the list was
:param last_edge_shortcut: Whether or not the previous node in the list was
MicahGale marked this conversation as resolved.
Show resolved Hide resolved
part of a different shortcut
:type last_edge_shortcut: bool
:returns: true it can be consumed.
Expand Down Expand Up @@ -1967,7 +1967,24 @@ def _format_jump(self):
return f"{num_jumps.format()}{j}"

def _format_repeat(self, leading_node=None):
if leading_node is not None:
def can_use_last_node(node):
"""Last node can be used if
- it's a basic ValueNode
- it's a different shortcut that concludes itself well.
- it's also a repeat, with the same edge values.
"""
return isinstance(node, ValueNode) or (
isinstance(node, type(self))
and (
leading_node._type != Shortcuts.REPEAT
or (
leading_node._type == Shortcuts.REPEAT
and math.isclose(self.nodes[0].value, node.nodes[-1].value)
)
)
)
MicahGale marked this conversation as resolved.
Show resolved Hide resolved

if can_use_last_node(leading_node):
first_val = ""
num_extra = 0
else:
Expand Down
14 changes: 10 additions & 4 deletions montepy/mcnp_problem.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,18 @@ class MCNP_Problem:
"""
A class to represent an entire MCNP problem in a semantic way.

:param file_name: the path to the file that will be read.
:type file_name: str
.. note::
If a stream is provided. It will not be closed by this function.

:param destination: the path to the input file to read, or a readable stream.
:type destination: io.TextIOBase, str, os.PathLike
"""

def __init__(self, file_name):
self._input_file = MCNP_InputFile(file_name)
def __init__(self, destination):
if hasattr(destination, "read") and callable(getattr(destination, "read")):
self._input_file = MCNP_InputFile.from_open_stream(destination)
elif isinstance(destination, (str, os.PathLike)):
self._input_file = MCNP_InputFile(destination)
self._title = None
self._message = None
self._print_in_data_block = CellDataPrintController()
Expand Down
2 changes: 1 addition & 1 deletion tests/inputs/test_importance.imcnp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ MT3 lwtr.23t
C execution
ksrc 0 0 0
imp:n,p 1 1 1 0 3
imp:e 0 0 0 1 2
imp:e 0 2r 1 r
tjlaboss marked this conversation as resolved.
Show resolved Hide resolved
kcode 100000 1.000 50 1050
phys:p j 1 2j 1
mode n p e
Expand Down
33 changes: 32 additions & 1 deletion tests/test_integration.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Copyright 2024, Battelle Energy Alliance, LLC All Rights Reserved.
import copy
import io
from pathlib import Path

import pytest
import os
Expand All @@ -17,6 +18,8 @@
from montepy.particle import Particle
import numpy as np

from tests import constants


@pytest.fixture(scope="module")
def simple_problem():
Expand Down Expand Up @@ -481,7 +484,7 @@ def test_importance_format_unmutated(importance_problem):
print(output)
assert len(output) == 2
assert "imp:n,p 1 1 1 0 3" == output[0]
assert "imp:e 0 0 0 1 2" == output[1]
assert "imp:e 0 2r 1 r" == output[1]
MicahGale marked this conversation as resolved.
Show resolved Hide resolved


def test_importance_format_mutated(importance_problem):
Expand Down Expand Up @@ -1066,3 +1069,31 @@ def test_alternate_encoding():
montepy.read_input(
os.path.join("tests", "inputs", "bad_encoding.imcnp"), replace=True
)


@pytest.mark.parametrize(
"file",
{"tests/inputs/test_importance.imcnp"},
# set((Path("tests") / "inputs").iterdir())
# - {
# Path("tests") / "inputs" / p
# for p in constants.BAD_INPUTS | constants.IGNORE_FILES
# },
)
def test_read_write_cycle(file):
print(f"Testing against {file} *********************")
problem = montepy.read_input(file)
print(problem.cells._importance._real_tree[montepy.particle.Particle.ELECTRON])
print()
fh = io.StringIO()
problem.write_problem(fh)
fh.seek(0)
# test valid syntax
new_problem = montepy.MCNP_Problem("foo")
# verify lines are similar
fh.seek(0)
lines = [line.rstrip() for line in fh]
[print(line) for line in lines]
with open(file, "r") as gold_fh:
for gold_line, new_line in zip(gold_fh, lines):
assert new_line == gold_line.rstrip().expandtabs(8)
Loading