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

Internal NameError when parsing a valid input file. #3

Open
MicahGale opened this issue Oct 10, 2024 · 3 comments
Open

Internal NameError when parsing a valid input file. #3

MicahGale opened this issue Oct 10, 2024 · 3 comments

Comments

@MicahGale
Copy link

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

  • Version 0.1.dev253+gea48739.d20241010
  • python 3.12
BitterB0NG0 added a commit that referenced this issue Oct 15, 2024
@BitterB0NG0
Copy link
Member

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

  • FILL=0:1 0:1 0:0 1 0 R 1 (5)
  • TR5 0.0 0.0 1.0
  • FILL=1 (1 0.0 0.0)
  • PHYS:P j 1 2j 1

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

  • FILL = n (q)
  • FILL = n (o1 o2 o3 xx’ yx’ zx’ xy’ yy’ zy’ xz’ yz’ zz’ m)
  • FILL = i1:i2 j1:j2 k1:k2 n111 n211 ... ni1j1k1 ... ni2j2k2
  • TRn o1 o2 o3 xx’ yx’ zx’ xy’ yy’ zy’ xz’ yz’ zz’ m
  • PHYS:p emcpf ides nocoh ispn nodop J fism

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

@MicahGale
Copy link
Author

MicahGale commented Oct 15, 2024

  • The 0 R syntax is allowed essentially anywhere by section 4.4.5.1.

  • The transform with a multi-universe fill is allowed under note 1 of section 5.5.5.3.1.

  • No rotation matrix for the transform is allowed under note 3 of Section 5.5.3

  • Similarly the rotation matrix is optional in fill given the same note (referred to in section 5.5.5.3.1)

  • All PHYS:P options are optional as they have default values in section 5.7.2.3. Jump is allowed anywhere default values are provided in the aforementioned section 4.4.5.1

@MicahGale
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants