Skip to content

Internal NameError when parsing a valid input file. #3

Description

@MicahGale

Describe the bug

When I was trying to play around with an MCNP input file I received a NameError that seems unrelated to a problem with the file.

To Reproduce

A short code snippet of what you have ran. Please change or remove any specific values or anything that can't be public. For example:

importpymcnpprob=pymcnp.Inp.from_mcnp_file("/home/mgale/dev/montepy/tests/inputs/test.imcnp")

Error Message (if any)

If an error message was printed please include the entire stacktrace. If it includes any specific values please change or remove them. For example:

prob=pymcnp.Inp.from_mcnp_file("/home/mgale/dev/montepy/tests/inputs/test.imcnp")
---------------------------------------------------------------------------FileNotFoundErrorTraceback (mostrecentcalllast)
CellIn[8], line1---->1prob=pymcnp.Inp.from_mcnp_file("/home/mgale/dev/montepy/tests/inputs/test.imcnp")
File~/mambaforge/envs/pymcnp/lib/python3.12/site-packages/pymcnp/files/inp/inp.py:128, inInp.from_mcnp_file(cls, filename)
114""" 115 ``from_mcnp_file`` generates ``Inp`` objects from INP files. 116 (...) 124 ``Inp`` object. 125 """127source=""-->128withopen(filename) asfile:
129source="".join(file.readlines())
131returncls.from_mcnp(source)
FileNotFoundError: [Errno2] Nosuchfileordirectory: '/home/mgale/dev/montepy/tests/inputs/test.imcnp'In [9]: prob=pymcnp.Inp.from_mcnp_file("../montepy/tests/inputs/test.imcnp")
---------------------------------------------------------------------------MCNPSemanticErrorTraceback (mostrecentcalllast)
CellIn[9], line1---->1prob=pymcnp.Inp.from_mcnp_file("../montepy/tests/inputs/test.imcnp")
File~/mambaforge/envs/pymcnp/lib/python3.12/site-packages/pymcnp/files/inp/inp.py:131, inInp.from_mcnp_file(cls, filename)
128withopen(filename) asfile:
129source="".join(file.readlines())
-->131returncls.from_mcnp(source)
File~/mambaforge/envs/pymcnp/lib/python3.12/site-packages/pymcnp/files/inp/inp.py:90, inInp.from_mcnp(source)
88index=list(lines.deque).index("")
89cell_source="\n".join(lines.popl() for_inrange(0, index))
--->90cell_block=cells.Cells.from_mcnp(cell_source)
92lines.popl()
94# Processing Surface CardsFile~/mambaforge/envs/pymcnp/lib/python3.12/site-packages/pymcnp/files/inp/cells.py:55, inCells.from_mcnp(source)
53continue54else:
--->55block.append(Cell.from_mcnp(line))
57returnblockFile~/mambaforge/envs/pymcnp/lib/python3.12/site-packages/pymcnp/files/inp/cell.py:1448, inCell.from_mcnp(source, line)
1445geometry.append(tokens.popl())
1446pass->1448geometry=Cell.CellGeometry(" ".join(geometry))
1450# Processing Options1451options= []
File~/mambaforge/envs/pymcnp/lib/python3.12/site-packages/pymcnp/files/inp/cell.py:80, inCell.CellGeometry.__init__(self, formula)
77inp_stack=re.findall(r"#|:| : |[()]| [()]|[()] | [()] | |[+-]?\d+", formula)
79if"".join(inp_stack) !=formula:
--->80raiseerrors.MCNPSemanticError(errors.MCNPSemanticCodes.INVALID_CELL_GEOMETRY)
82fortokenininp_stack:
83ifre.match(r"[+-]?\d+", token):
84# Processing Surface Number<class'str'>: (<class'NameError'>, NameError("name 'line' is not defined"))
In [10]: prob=pymcnp.Inp.from_mcnp_file("../montepy/tests/inputs/test_universe.imcnp")
---------------------------------------------------------------------------MCNPSemanticErrorTraceback (mostrecentcalllast)
CellIn[10], line1---->1prob=pymcnp.Inp.from_mcnp_file("../montepy/tests/inputs/test_universe.imcnp")
File~/mambaforge/envs/pymcnp/lib/python3.12/site-packages/pymcnp/files/inp/inp.py:131, inInp.from_mcnp_file(cls, filename)
128withopen(filename) asfile:
129source="".join(file.readlines())
-->131returncls.from_mcnp(source)
File~/mambaforge/envs/pymcnp/lib/python3.12/site-packages/pymcnp/files/inp/inp.py:90, inInp.from_mcnp(source)
88index=list(lines.deque).index("")
89cell_source="\n".join(lines.popl() for_inrange(0, index))
--->90cell_block=cells.Cells.from_mcnp(cell_source)
92lines.popl()
94# Processing Surface CardsFile~/mambaforge/envs/pymcnp/lib/python3.12/site-packages/pymcnp/files/inp/cells.py:55, inCells.from_mcnp(source)
53continue54else:
--->55block.append(Cell.from_mcnp(line))
57returnblockFile~/mambaforge/envs/pymcnp/lib/python3.12/site-packages/pymcnp/files/inp/cell.py:1464, inCell.from_mcnp(source, line)
1461values.append(tokens.popl())
1462pass->1464option=Cell.CellOption.from_mcnp(f"{keyword}={" ".join(values)}")
1465options.append(option)
1467options=tuple(options)
File~/mambaforge/envs/pymcnp/lib/python3.12/site-packages/pymcnp/files/inp/cell.py:469, inCell.CellOption.from_mcnp(source)
466iftokens:
467raiseerrors.MCNPSyntaxError(errors.MCNPSyntaxCodes.TOOLONG_CELL_OPTION)
-->469returnCell.CellOption(keyword, value, suffix=suffix, designator=designator)
File~/mambaforge/envs/pymcnp/lib/python3.12/site-packages/pymcnp/files/inp/cell.py:266, inCell.CellOption.__init__(self, keyword, value, suffix, designator)
264matchkeyword:
265caseCell.CellOption.CellKeyword.IMPORTANCE:
-->266obj=Cell.Importance(value, designator)
267caseCell.CellOption.CellKeyword.VOLUME:
268obj=Cell.Volume(value)
File~/mambaforge/envs/pymcnp/lib/python3.12/site-packages/pymcnp/files/inp/cell.py:548, inCell.Importance.__init__(self, importance, designator)
532""" 533 ``__init__`` initializes ``Importance``. 534 (...) 544 MCNPSemanticError: INVALID_CELL_OPTION_VALUE. 545 """547ifimportanceisNoneornot (importance>=0):
-->548raiseerrors.MCNPSemanticError(errors.MCNPSemanticCodes.INVALID_CELL_OPTION_VALUE)
550ifdesignatorisNone:
551raiseerrors.MCNPSemanticError(errors.MCNPSemanticCodes.INVALID_CELL_OPTION_DESIGNATOR)
<class'str'>: (<class'NameError'>, NameError("name 'line' is not defined"))

MCNP input file snippet

This is taken from the MontePy test suite

MCNP Test Model for MOAA
C cells
c
1 1 20
-1000 $ dollar comment
imp:n,p=1 trcl=5
u=1
2 2 8
-1005
imp:n=1
imp:p=0.5
lat 1 fill= 0:1 0:1 0:0 1 0 R 1 (5)
3 3 -1
1000 1005 -1010
imp:n,p=1
99 0
1010
imp:n,p=0
fill=1
5 0 #99
imp:n,p=3 fill=1 (1 0.0 0.0)
c foo end comment
C surfaces
1000 SO 1
1005 RCC 0 1.5 -0.5 0 0 1 0.25
1010 SO 3
C data
C materials
C UO2 5 atpt enriched
m1 92235.80c 5 &
92238.80c 95
c testing comments
sc1 This is a high quality source comment
Fc5 Such a good tally comment.
C Iron
m2 26054.80c 5.85
26056.80c 91.75
26057.80c 2.12
26058.80c 0.28
C water
m3 1001.80c 2
8016.80c 1
MT3 lwtr.23t
TR5 0.0 0.0 1.0
C execution
ksrc 0 0 0
kcode 100000 1.000 50 1050
phys:p j 1 2j 1
mode n p
vol NO 2J 1 1.5 J
E0 1 10 100

Version

  • Version 0.1.dev253+gea48739.d20241010
  • python 3.12

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions