FSIBT / PyMCNP

Python tools for MCNP
https://pymcnp.readthedocs.io/en/latest/
Other
6 stars 0 forks source link

Internal NameError when parsing a valid input file. #3

Open MicahGale opened 1 month ago

MicahGale commented 1 month ago

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:

import pymcnp
prob = 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")
---------------------------------------------------------------------------
FileNotFoundError                         Traceback (most recent call last)
Cell In[8], line 1
----> 1 prob = 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, in Inp.from_mcnp_file(cls, filename)
    114 """
    115 ``from_mcnp_file`` generates ``Inp`` objects from INP files.
    116
   (...)
    124     ``Inp`` object.
    125 """
    127 source = ""
--> 128 with open(filename) as file:
    129     source = "".join(file.readlines())
    131 return cls.from_mcnp(source)

FileNotFoundError: [Errno 2] No such file or directory: '/home/mgale/dev/montepy/tests/inputs/test.imcnp'

In [9]: prob = pymcnp.Inp.from_mcnp_file("../montepy/tests/inputs/test.imcnp")
---------------------------------------------------------------------------
MCNPSemanticError                         Traceback (most recent call last)
Cell In[9], line 1
----> 1 prob = 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, in Inp.from_mcnp_file(cls, filename)
    128 with open(filename) as file:
    129     source = "".join(file.readlines())
--> 131 return cls.from_mcnp(source)

File ~/mambaforge/envs/pymcnp/lib/python3.12/site-packages/pymcnp/files/inp/inp.py:90, in Inp.from_mcnp(source)
     88 index = list(lines.deque).index("")
     89 cell_source = "\n".join(lines.popl() for _ in range(0, index))
---> 90 cell_block = cells.Cells.from_mcnp(cell_source)
     92 lines.popl()
     94 # Processing Surface Cards

File ~/mambaforge/envs/pymcnp/lib/python3.12/site-packages/pymcnp/files/inp/cells.py:55, in Cells.from_mcnp(source)
     53         continue
     54     else:
---> 55         block.append(Cell.from_mcnp(line))
     57 return block

File ~/mambaforge/envs/pymcnp/lib/python3.12/site-packages/pymcnp/files/inp/cell.py:1448, in Cell.from_mcnp(source, line)
   1445         geometry.append(tokens.popl())
   1446         pass
-> 1448 geometry = Cell.CellGeometry(" ".join(geometry))
   1450 # Processing Options
   1451 options = []

File ~/mambaforge/envs/pymcnp/lib/python3.12/site-packages/pymcnp/files/inp/cell.py:80, in Cell.CellGeometry.__init__(self, formula)
     77 inp_stack = re.findall(r"#|:| : |[()]| [()]|[()] | [()] | |[+-]?\d+", formula)
     79 if "".join(inp_stack) != formula:
---> 80     raise errors.MCNPSemanticError(errors.MCNPSemanticCodes.INVALID_CELL_GEOMETRY)
     82 for token in inp_stack:
     83     if re.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")
---------------------------------------------------------------------------
MCNPSemanticError                         Traceback (most recent call last)
Cell In[10], line 1
----> 1 prob = 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, in Inp.from_mcnp_file(cls, filename)
    128 with open(filename) as file:
    129     source = "".join(file.readlines())
--> 131 return cls.from_mcnp(source)

File ~/mambaforge/envs/pymcnp/lib/python3.12/site-packages/pymcnp/files/inp/inp.py:90, in Inp.from_mcnp(source)
     88 index = list(lines.deque).index("")
     89 cell_source = "\n".join(lines.popl() for _ in range(0, index))
---> 90 cell_block = cells.Cells.from_mcnp(cell_source)
     92 lines.popl()
     94 # Processing Surface Cards

File ~/mambaforge/envs/pymcnp/lib/python3.12/site-packages/pymcnp/files/inp/cells.py:55, in Cells.from_mcnp(source)
     53         continue
     54     else:
---> 55         block.append(Cell.from_mcnp(line))
     57 return block

File ~/mambaforge/envs/pymcnp/lib/python3.12/site-packages/pymcnp/files/inp/cell.py:1464, in Cell.from_mcnp(source, line)
   1461             values.append(tokens.popl())
   1462             pass
-> 1464     option = Cell.CellOption.from_mcnp(f"{keyword}={" ".join(values)}")
   1465     options.append(option)
   1467 options = tuple(options)

File ~/mambaforge/envs/pymcnp/lib/python3.12/site-packages/pymcnp/files/inp/cell.py:469, in Cell.CellOption.from_mcnp(source)
    466 if tokens:
    467     raise errors.MCNPSyntaxError(errors.MCNPSyntaxCodes.TOOLONG_CELL_OPTION)
--> 469 return Cell.CellOption(keyword, value, suffix=suffix, designator=designator)

File ~/mambaforge/envs/pymcnp/lib/python3.12/site-packages/pymcnp/files/inp/cell.py:266, in Cell.CellOption.__init__(self, keyword, value, suffix, designator)
    264 match keyword:
    265     case Cell.CellOption.CellKeyword.IMPORTANCE:
--> 266         obj = Cell.Importance(value, designator)
    267     case Cell.CellOption.CellKeyword.VOLUME:
    268         obj = Cell.Volume(value)

File ~/mambaforge/envs/pymcnp/lib/python3.12/site-packages/pymcnp/files/inp/cell.py:548, in Cell.Importance.__init__(self, importance, designator)
    532 """
    533 ``__init__`` initializes ``Importance``.
    534
   (...)
    544     MCNPSemanticError: INVALID_CELL_OPTION_VALUE.
    545 """
    547 if importance is None or not (importance >= 0):
--> 548     raise errors.MCNPSemanticError(errors.MCNPSemanticCodes.INVALID_CELL_OPTION_VALUE)
    550 if designator is None:
    551     raise errors.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

BitterB0NG0 commented 1 month ago

I've resolved the NameError and MCNPSemanticError errors with commit fe4593630d425071ee23881d8ee559a907aed89e, but PyMCNP has other issues with the input file. PyMCNP does not recognize the following syntax:

Those cards syntax are not mentioned in the manual, so PyMCNP only accepts the following:

Do you have any references for the syntax used in the input file?

MicahGale commented 1 month ago
MicahGale commented 1 month ago

MCNP syntax is defined by more by the exceptions and the rule, and it has been really frustrating trying to truly figure out what is, and what isn't valid. I've previously asked the MCNP developers to write a more rigorous syntax, and it sounds like they aren't able to due to how MCNP works internally, there is no formal grammar.