Skip to content

Commit 95ffa84

Browse files
authored
Merge pull request #819 from kordusas/feature/525-detect-empty-lines
Feature/525 detect empty lines
2 parents 90eb816 + 8429ea3 commit 95ffa84

File tree

7 files changed

+97
-8
lines changed

7 files changed

+97
-8
lines changed

doc/source/changelog.rst

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,16 @@ MontePy Changelog
1111

1212
**Features Added**
1313

14-
* Allow multiple universe fills to accept 2D MNCP lattices (:issue:`719`)
14+
* Added checking for additional input after the ``data`` block, and raising a warning if it exists (:issue:`525`).
15+
* Allow multiple universe fills to accept 2D MNCP lattices (:issue:`719`).
1516
* Make `LatticeType.RECTANGULAR` and `LatticeType.HEXAHEDRAL` synonymous (:issue:`808`).
1617
* Allow setting ``cell.fill.universes`` with a numpy array of universe IDs (:issue:`736`).
1718

1819
**Bugs Fixed**
1920

2021
* Fixed bug where lines that were the allowed length was raising a ``LineOverRunWarning`` when read by MontePy (:issue:`517`).
2122
* Added descriptive TypeError messages (:issue:`801`)
23+
* Fixed a bug that caused to write an extra termination line between the data block and the cell data section in the MCNP input. (:pull:`819`) (:issue:`703`).
2224

2325
**Documentation**
2426

@@ -37,7 +39,6 @@ MontePy Changelog
3739

3840
1.1.3
3941
--------------
40-
4142
**Features Added**
4243

4344
* Added Boundary condition type to the representation of a ``montepy.Surface`` (e.g., ``repr(surf)``) (:issue:`682`).
@@ -51,6 +52,7 @@ MontePy Changelog
5152
* Fixed bug where lines that were the allowed length was raising a ``LineOverRunWarning`` when read by MontePy (:issue:`517`).
5253
* Added descriptive TypeError messages (:issue:`801`)
5354

55+
5456
1.1.2
5557
--------------
5658

montepy/exceptions.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@ def __init__(self, message):
1010
self.message = message
1111

1212

13+
class UndefinedBlock(UserWarning):
14+
"""Raised when additional blocks exist after the default data block."""
15+
16+
def __init__(self, message):
17+
self.message = message
18+
19+
1320
class MalformedInputError(ValueError):
1421
"""Raised when there is an error with the MCNP input not related to the parser."""
1522

montepy/input_parser/input_syntax_reader.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ def read_data(fh, mcnp_version, block_type=None, recursion=False):
146146

147147
def flush_block():
148148
nonlocal block_counter, block_type
149+
# keep parsing while there is input or termination has not been triggered
149150
if len(input_raw_lines) > 0:
150151
yield from flush_input()
151152
block_counter += 1
@@ -154,6 +155,17 @@ def flush_block():
154155

155156
def flush_input():
156157
nonlocal input_raw_lines
158+
# IF 3 BLOCKS are parsed, the rest should be ignored with a warning and print 3 lines
159+
if block_counter >= 3:
160+
joined_lines = "\n".join(input_raw_lines[0:3])
161+
msg = f"Unexpected input after line {current_file.lineno - 1}\n line content: {joined_lines}\n"
162+
warnings.warn(
163+
msg,
164+
UndefinedBlock,
165+
stacklevel=6,
166+
)
167+
return
168+
157169
start_line = current_file.lineno + 1 - len(input_raw_lines)
158170
input = Input(
159171
input_raw_lines,
@@ -191,6 +203,7 @@ def flush_input():
191203
and input_raw_lines
192204
):
193205
yield from flush_input()
206+
194207
# die if it is a vertical syntax format
195208
start_o_line = line[0:BLANK_SPACE_CONTINUE]
196209
# eliminate comments, and inputs that use # for other syntax

montepy/mcnp_problem.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -612,14 +612,16 @@ def _write_to_stream(self, inp):
612612
warning.handled = True
613613
for line in lines:
614614
inp.write(line + "\n")
615-
if terminate:
615+
616+
# writing cell data in DATA BLOCK if the last written object inherits DataInputAbstract and there is cell data to write
617+
if objects is self.data_inputs:
618+
for line in self.cells._run_children_format_for_mcnp(
619+
self.data_inputs, self.mcnp_version
620+
):
621+
inp.write(line + "\n")
622+
elif terminate:
616623
inp.write("\n")
617-
for line in self.cells._run_children_format_for_mcnp(
618-
self.data_inputs, self.mcnp_version
619-
):
620-
inp.write(line + "\n")
621624

622-
inp.write("\n")
623625
self._handle_warnings(warning_catch)
624626

625627
def _handle_warnings(self, warning_queue):

tests/constants.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"test_extra_data_param.imcnp",
1616
"test_extra_data_imp.imcnp",
1717
"number_conflict_pin_cell.imcnp",
18+
"test_pin_cell_extra_block_warning.imcnp",
1819
}
1920
IGNORE_FILES = {
2021
"testReadRec1.imcnp",
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
Made up PWR pin Cell Problem
2+
c uranium rod
3+
1 1 10.0 -1 101 -102 imp:n=1
4+
c gas gap
5+
2 0 1 -2 101 -102 imp:n=1
6+
c cladding
7+
3 10 5.0 2 -3 101 -102 imp:n=1
8+
c water cell
9+
5 2 1.0 2 101 -102
10+
103 -104
11+
105 -106 imp:n=1
12+
13+
1 CZ 0.39
14+
2 CZ 0.40
15+
3 CZ 0.46
16+
101 PZ -0.63
17+
102 PZ 0.63
18+
103 PX -0.63
19+
104 PX 0.63
20+
105 PY -0.63
21+
106 PY 0.63
22+
23+
M1 92235.80c 0.04
24+
92238.80c 0.96
25+
M2 1001.80c 0.66
26+
8016.80c 0.33
27+
M10 40090.80c 0.515
28+
40091.80c 0.112
29+
40092.80c 0.171
30+
40094.80c 0.174
31+
40096.80c 0.028
32+
mode n
33+
nps 1e3
34+
35+
M101 92235.80c 0.04
36+
92238.80c 0.96

tests/test_syntax_parsing.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from montepy.input_parser.shortcuts import Shortcuts
1414
from montepy.input_parser import syntax_node
1515
from montepy.particle import Particle
16+
from montepy.exceptions import UndefinedBlock
1617
import warnings
1718

1819

@@ -1472,6 +1473,33 @@ def test_jump_and_a_hop(self):
14721473
str(jump)
14731474
repr(jump)
14741475

1476+
def test_extra_block_warning(self):
1477+
# Define constants for better readability and maintainability
1478+
INPUT_FILE = "tests/inputs/test_pin_cell_extra_block_warning.imcnp"
1479+
EXPECTED_LINE = 34
1480+
EXPECTED_CONTENT = "M101 92235.80c 0.04\n 92238.80c 0.96\n"
1481+
EXPECTED_WARNING_TYPE = UndefinedBlock
1482+
1483+
# Setup: Use the common pattern for defining the generator
1484+
generator = input_syntax_reader.read_input_syntax(MCNP_InputFile(INPUT_FILE))
1485+
1486+
# Check that the expected warning is raised
1487+
with pytest.warns(EXPECTED_WARNING_TYPE) as recs:
1488+
# Exhaust generator so parser runs and warnings are emitted
1489+
for _ in generator:
1490+
pass
1491+
1492+
# Assertions
1493+
assert len(recs) == 1, f"Expected exactly one warning, but got {len(recs)}"
1494+
warning = recs[0]
1495+
assert issubclass(
1496+
warning.category, EXPECTED_WARNING_TYPE
1497+
), f"Expected warning type {EXPECTED_WARNING_TYPE}, but got {warning.category}"
1498+
assert (
1499+
str(warning.message)
1500+
== f"Unexpected input after line {EXPECTED_LINE}\n line content: {EXPECTED_CONTENT}"
1501+
), f"Expected warning message 'Unexpected input after line {EXPECTED_LINE}\n line content: {EXPECTED_CONTENT}', but got '{str(warning.message)}'"
1502+
14751503

14761504
class TestClassifierNode:
14771505
def test_classifier_init(self):

0 commit comments

Comments
 (0)