idaholab / MontePy

MontePy is the most user friendly Python library (API) to read, edit, and write MCNP input files.
https://www.montepy.org/
MIT License
30 stars 6 forks source link

Parsing error with Data Inputs having starting padding #171

Closed MicahGale closed 8 months ago

MicahGale commented 1 year ago

Found by @BrennaCarbno.

The actual error is:

File ~/dev/mcnpy/mcnpy/input_parser/input_reader.py:25, in read_input(input_file, mcnp_version)
     23 problem = mcnp_problem.MCNP_Problem(input_file)
     24 problem.mcnp_version = mcnp_version
---> 25 problem.parse_input()
     26 return problem

File ~/dev/mcnpy/mcnpy/mcnp_problem.py:283, in MCNP_Problem.parse_input(self, check_input)
    281         continue
    282     else:
--> 283         raise e
    284 if isinstance(obj, Material):
    285     self._materials.append(obj)

File ~/dev/mcnpy/mcnpy/mcnp_problem.py:268, in MCNP_Problem.parse_input(self, check_input)
    266 if len(input.input_lines) > 0:
    267     try:
--> 268         obj = obj_parser(input)
    269         obj.link_to_problem(self)
    270         obj_container.append(obj)

File ~/dev/mcnpy/mcnpy/data_inputs/data_parser.py:43, in parse_data(input)
     30 def parse_data(input):
     31     """
     32     Parses the data input as the appropriate object if it is supported.
     33
   (...)
     40     :rtype: DataInput
     41     """
---> 43     base_input = data_input.DataInput(input, fast_parse=True)
     44     prefix = base_input.prefix
     46     for data_class in PREFIX_MATCHES:

File ~/dev/mcnpy/mcnpy/data_inputs/data_input.py:36, in DataInputAbstract.__init__(self, input, fast_parse)
     34 words = data_lines[0].split()
     35 input = Input([words[0]], input.block_type)
---> 36 super().__init__(input, self._classifier_parser)
     37 self.__split_name(input)

File ~/dev/mcnpy/mcnpy/mcnp_object.py:51, in MCNP_Object.__init__(self, input, parser)
     47     raise ValueError(
     48         f"Error parsing object of type: {type(self)}: {e.args[0]}"
     49     )
     50 if self._tree is None:
---> 51     raise ParsingError(
     52         input,
     53         "",
     54         parser.log.clear_queue(),
     55     )
     56 if "parameters" in self._tree:
     57     self._parameters = self._tree["parameters"]

ParsingError:     , line 0

    >    0| c
          | ^ not expected here.
There was an error parsing "c".
sly: Syntax error at line 1, token=COMMENT

This is taken out of context due to the classifier parser not knowing it. This seems to be an issue with the classifier parsing phase only.

The full input (with some obfuscation) is:

c ******************************************************************************
c *******************************Data Cards*************************************
c ******************************************************************************
c
c
c data file=fuel
c ******************** Begin Fuel Data *****************************************
c
c
c fuel
c     material number 2001 total atom density =  
m2001
      92235.50c     5.0E-01
MicahGale commented 1 year ago

created branch 130-parsing-error-with-data-inputs-having-starting-padding to address this issue

MicahGale commented 1 year ago

mentioned in commit 306377e135819c479a632f03250f5e147f2cf739

MicahGale commented 1 year ago

In GitLab by @tjlaboss on Aug 3, 2023, 13:05

I've been able to reproduce this error on a few files, including the following MWE.

MCNPy Data Card Padding Bug MWE
C 
C Cell cards
c N  M  D      Out  In  +11 -12   Imp
  1  1 -10.713      -1  +11 -12   imp:n=1     $ Fuel
  2  0          +1  -2  +11 -12   imp:n=1     $ Gap
  3  3  -6.56   +2  -3  +11 -12   imp:n=1     $ Clad
  4  4  -0.726  +3  -4  +11 -12   imp:n=1     $ Water 
 11  0          +4      +11 -12   imp:n=0     $ Radial Graveyard
 12  0                  +12       imp:n=0     $ Upper Graveyard
 13  0                      -11   imp:n=0     $ Lower Graveyard
C End Cell Cards

C Surface Cards
C Radial surfaces
1 cz   0.41148  $ Fuel Pellets and Spacers
2 cz   0.41656  $ Inner Clad Radius
3 cz   0.47498  $ Outer Clad, End Cap, Bellows, LVDT
4 cz  10.5     $ water boundary
C
C Axial surfaces
11 pz -50
12 pz +50
C End Surface Cards

C Data Cards
C ^^^^^^^^^^^   this breaks MCNPy
C Material Cards
C 
C UO2 @ 4.95 wt%
m1       8016.01c 4.585223e-2
        92235.01c 1.151431e-3
        92238.01c 2.183053e-2
C
c Zirconium
m3      40090.01c  2.18865E-02
        40091.01c  4.77292E-03
        40092.01c  7.29551E-03
        40094.01c  7.39335E-03
        40096.01c  1.19110E-03
C
C Water
m4     8016.01c 1
       1001.01c 2
mt4 h-h2o.53t
C
C End Material Cards
C ------------------
C Begin Tally Cards
fc4 mesh tally that causes error
fmesh4:n 
           geom=xyz  
           origin= -2.5 -2.5 -25
           imesh= 0  iints=5
           jmesh= 0  jints=5
           kmesh= 0  kints=10
           emesh=1   $ <-- This breaks MCNPTools.
C
C End Tally Cards
C ---------------
C Source Cards
sdef   pos=1 1 0
       par=n
       erg=d1
sc1 Watt fission spectrum
sp1 -3
nps 4e4
MicahGale commented 1 year ago

This was attempted to be fixed in !89. Could you pull and confirm it's still an error?

MicahGale commented 1 year ago

In GitLab by @tjlaboss on Aug 3, 2023, 15:27

Forsooth, it workest 👍🏻

MicahGale commented 1 year ago

I had similar errors still popping up with @BrennaCarbno's models so I'm going to keep this open I figure out the root cause of those issues.

MicahGale commented 1 year ago

Confirmed these issues are independent in #133. Going to close.