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

Uncaught iterator error in Fill #601

Open
MicahGale opened this issue Dec 3, 2024 · 2 comments · May be fixed by #695
Open

Uncaught iterator error in Fill #601

MicahGale opened this issue Dec 3, 2024 · 2 comments · May be fixed by #695
Labels
bugs A deviation from expected behavior that does not reach the level of being reportable as an "Error".

Comments

@MicahGale
Copy link
Collaborator

MicahGale commented Dec 3, 2024

Describe the bug

A clear and concise description of what the bug is.

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:

problem = montepy.read_input("foo.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:

../../mambaforge/lib/python3.12/site-packages/montepy/input_parser/input_reader.py:31: in read_input
    problem.parse_input(replace=replace)
../../mambaforge/lib/python3.12/site-packages/montepy/mcnp_problem.py:367: in parse_input
    obj = obj_parser(input)
../../mambaforge/lib/python3.12/site-packages/montepy/mcnp_object.py:45: in wrapped
    add_line_number_to_exception(e, self)
../../mambaforge/lib/python3.12/site-packages/montepy/errors.py:232: in add_line_number_to_exception
    raise error
../../mambaforge/lib/python3.12/site-packages/montepy/mcnp_object.py:41: in wrapped
    return func(*args, **kwargs)
../../mambaforge/lib/python3.12/site-packages/montepy/cell.py:95: in __init__
    self._parse_keyword_modifiers()
../../mambaforge/lib/python3.12/site-packages/montepy/mcnp_object.py:45: in wrapped
    add_line_number_to_exception(e, self)
../../mambaforge/lib/python3.12/site-packages/montepy/errors.py:232: in add_line_number_to_exception
    raise error
../../mambaforge/lib/python3.12/site-packages/montepy/mcnp_object.py:41: in wrapped
    return func(*args, **kwargs)
../../mambaforge/lib/python3.12/site-packages/montepy/cell.py:119: in _parse_keyword_modifiers
    input = input_class(in_cell_block=True, key=key, value=value)
../../mambaforge/lib/python3.12/site-packages/montepy/mcnp_object.py:45: in wrapped
    add_line_number_to_exception(e, self)
../../mambaforge/lib/python3.12/site-packages/montepy/errors.py:232: in add_line_number_to_exception
    raise error
../../mambaforge/lib/python3.12/site-packages/montepy/mcnp_object.py:41: in wrapped
    return func(*args, **kwargs)
../../mambaforge/lib/python3.12/site-packages/montepy/data_inputs/fill.py:46: in __init__
    self._parse_cell_input(key, value)
../../mambaforge/lib/python3.12/site-packages/montepy/mcnp_object.py:45: in wrapped
    add_line_number_to_exception(e, self)
../../mambaforge/lib/python3.12/site-packages/montepy/errors.py:232: in add_line_number_to_exception
    raise error
../../mambaforge/lib/python3.12/site-packages/montepy/mcnp_object.py:41: in wrapped
    return func(*args, **kwargs)
../../mambaforge/lib/python3.12/site-packages/montepy/data_inputs/fill.py:144: in _parse_cell_input
    get_universe(value)
../../mambaforge/lib/python3.12/site-packages/montepy/data_inputs/fill.py:92: in get_universe
    self._parse_matrix(value)
../../mambaforge/lib/python3.12/site-packages/montepy/mcnp_object.py:45: in wrapped
    add_line_number_to_exception(e, self)
../../mambaforge/lib/python3.12/site-packages/montepy/errors.py:256: in add_line_number_to_exception
    raise error.with_traceback(trace)
../../mambaforge/lib/python3.12/site-packages/montepy/mcnp_object.py:41: in wrapped
    return func(*args, **kwargs)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

self = Fill: set_in_cell: True, in_cell: True old_number: None, old_transform: None old_numbers: [[[23 23 23 ... 23 23 23]
  ...e: None, universes: None, transform: None Multi_universe: True hidden_transform: None Min/Max: [-14 -14 -14] [14 14 14]
value = (Node: fill: {'classifier': (Classifier: mod: None, prefix: (Value, fill, padding: None), number: None, particles: Non..., padding: (Padding, [' '])), (Value, 23.0, padding: (Padding, [' '])), (Value, 23.0, padding: (Padding, [' ']))]))])})

    def _parse_matrix(self, value):
        """
        Parses a matrix fill of universes.

        :param value: the value in the cell
        :type value: str
        """
        self._multi_universe = True
        words = value["data"]
        self._min_index = np.zeros((3,), dtype=np.dtype(int))
        self._max_index = np.zeros((3,), dtype=np.dtype(int))
        limits_iter = (
            it.islice(words, 0, None, 3),
            it.islice(words, 1, None, 3),
            it.islice(words, 2, None, 3),
        )
        for axis, min_val, seperator, max_val in zip(
            Fill.DIMENSIONS.values(), *limits_iter
        ):
            for val, limit_holder in zip(
                (min_val, max_val), (self._min_index, self._max_index)
            ):
                try:
                    val._convert_to_int()
                    limit_holder[axis] = val.value
                except ValueError as e:
                    raise ValueError(
                        f"The lattice limits must be an integer. {val.value} was given"
                    )
        for min_val, max_val in zip(self.min_index, self.max_index):
            if min_val > max_val:
                raise ValueError(
                    "The minimum value must be smaller than the max value."
                    f"Min: {min_val}, Max: {max_val}, Input: {value.format()}"
                )
        self._old_numbers = np.zeros(self._sizes, dtype=np.dtype(int))
        words = iter(words[9:])
        for i in self._axis_range(0):
            for j in self._axis_range(1):
                for k in self._axis_range(2):
>                   val = next(words)
E                   StopIteration:
E
E                   Error came from Fill: Universe: None, transform: None from an unknown file.

MCNP input file snippet

MWE in progress see below

Version

  • Version 1.0.0a1dev306
@MicahGale MicahGale added question Further information is requested alpha testing Issues that came up during alpha testing bugs A deviation from expected behavior that does not reach the level of being reportable as an "Error". labels Dec 3, 2024
@MicahGale MicahGale self-assigned this Dec 3, 2024
@MicahGale
Copy link
Collaborator Author

I found the issue: the issue is when the input is underdefined.

in [7]: import montepy
In [8]: cell = montepy.Cell("1 0 2 fill=0:1 0:1 0:2 1")
---------------------------------------------------------------------------
StopIteration                             Traceback (most recent call last)
Cell In[8], line 1
----> 1 cell = montepy.Cell("1 0 2 fill=0:1 0:1 0:2 1")

File ~/mambaforge/lib/python3.12/site-packages/montepy/mcnp_object.py:51, in _ExceptionContextAdder._wrap_attr_call.<locals>.wrapped(*args, **kwargs)
     49 self._handling_exception = True
     50 try:
---> 51     add_line_number_to_exception(e, self)
     52 finally:
     53     del self._handling_exception

File ~/mambaforge/lib/python3.12/site-packages/montepy/errors.py:227, in add_line_number_to_exception(error, broken_robot)
    225 # avoid calling this n times recursively
    226 if hasattr(error, "montepy_handled"):
--> 227     raise error
    228 error.montepy_handled = True
    229 args = error.args

File ~/mambaforge/lib/python3.12/site-packages/montepy/mcnp_object.py:43, in _ExceptionContextAdder._wrap_attr_call.<locals>.wrapped(*args, **kwargs)
     40 @functools.wraps(func)
     41 def wrapped(*args, **kwargs):
     42     try:
---> 43         return func(*args, **kwargs)
     44     except Exception as e:
     45         if len(args) > 0 and isinstance(args[0], MCNP_Object):

File ~/mambaforge/lib/python3.12/site-packages/montepy/cell.py:146, in Cell.__init__(self, input, number)
    144     self._is_atom_dens = not self._density_node.is_negative
    145 self._parse_geometry()
--> 146 self._parse_keyword_modifiers()

File ~/mambaforge/lib/python3.12/site-packages/montepy/mcnp_object.py:51, in _ExceptionContextAdder._wrap_attr_call.<locals>.wrapped(*args, **kwargs)
     49 self._handling_exception = True
     50 try:
---> 51     add_line_number_to_exception(e, self)
     52 finally:
     53     del self._handling_exception

File ~/mambaforge/lib/python3.12/site-packages/montepy/errors.py:227, in add_line_number_to_exception(error, broken_robot)
    225 # avoid calling this n times recursively
    226 if hasattr(error, "montepy_handled"):
--> 227     raise error
    228 error.montepy_handled = True
    229 args = error.args

File ~/mambaforge/lib/python3.12/site-packages/montepy/mcnp_object.py:43, in _ExceptionContextAdder._wrap_attr_call.<locals>.wrapped(*args, **kwargs)
     40 @functools.wraps(func)
     41 def wrapped(*args, **kwargs):
     42     try:
---> 43         return func(*args, **kwargs)
     44     except Exception as e:
     45         if len(args) > 0 and isinstance(args[0], MCNP_Object):

File ~/mambaforge/lib/python3.12/site-packages/montepy/cell.py:166, in Cell._parse_keyword_modifiers(self)
    164 key = str(value["classifier"]).lower()
    165 found_class_prefixes.add(value["classifier"].prefix.value.lower())
--> 166 input = input_class(in_cell_block=True, key=key, value=value)
    167 if not getattr(self, attr).set_in_cell_block:
    168     setattr(self, attr, input)

File ~/mambaforge/lib/python3.12/site-packages/montepy/mcnp_object.py:51, in _ExceptionContextAdder._wrap_attr_call.<locals>.wrapped(*args, **kwargs)
     49 self._handling_exception = True
     50 try:
---> 51     add_line_number_to_exception(e, self)
     52 finally:
     53     del self._handling_exception

File ~/mambaforge/lib/python3.12/site-packages/montepy/errors.py:227, in add_line_number_to_exception(error, broken_robot)
    225 # avoid calling this n times recursively
    226 if hasattr(error, "montepy_handled"):
--> 227     raise error
    228 error.montepy_handled = True
    229 args = error.args

File ~/mambaforge/lib/python3.12/site-packages/montepy/mcnp_object.py:43, in _ExceptionContextAdder._wrap_attr_call.<locals>.wrapped(*args, **kwargs)
     40 @functools.wraps(func)
     41 def wrapped(*args, **kwargs):
     42     try:
---> 43         return func(*args, **kwargs)
     44     except Exception as e:
     45         if len(args) > 0 and isinstance(args[0], MCNP_Object):

File ~/mambaforge/lib/python3.12/site-packages/montepy/data_inputs/fill.py:51, in Fill.__init__(self, input, in_cell_block, key, value)
     49 if self.in_cell_block:
     50     if key:
---> 51         self._parse_cell_input(key, value)
     52 elif input:
     53     self._old_numbers = []

File ~/mambaforge/lib/python3.12/site-packages/montepy/mcnp_object.py:51, in _ExceptionContextAdder._wrap_attr_call.<locals>.wrapped(*args, **kwargs)
     49 self._handling_exception = True
     50 try:
---> 51     add_line_number_to_exception(e, self)
     52 finally:
     53     del self._handling_exception

File ~/mambaforge/lib/python3.12/site-packages/montepy/errors.py:227, in add_line_number_to_exception(error, broken_robot)
    225 # avoid calling this n times recursively
    226 if hasattr(error, "montepy_handled"):
--> 227     raise error
    228 error.montepy_handled = True
    229 args = error.args

File ~/mambaforge/lib/python3.12/site-packages/montepy/mcnp_object.py:43, in _ExceptionContextAdder._wrap_attr_call.<locals>.wrapped(*args, **kwargs)
     40 @functools.wraps(func)
     41 def wrapped(*args, **kwargs):
     42     try:
---> 43         return func(*args, **kwargs)
     44     except Exception as e:
     45         if len(args) > 0 and isinstance(args[0], MCNP_Object):

File ~/mambaforge/lib/python3.12/site-packages/montepy/data_inputs/fill.py:150, in Fill._parse_cell_input(self, key, value)
    147         self._hidden_transform = True
    149 else:
--> 150     get_universe(value)

File ~/mambaforge/lib/python3.12/site-packages/montepy/data_inputs/fill.py:98, in Fill._parse_cell_input.<locals>.get_universe(value)
     96 def get_universe(value):
     97     if ":" in value["data"].nodes:
---> 98         self._parse_matrix(value)
     99     else:
    100         data = value["data"]

File ~/mambaforge/lib/python3.12/site-packages/montepy/mcnp_object.py:51, in _ExceptionContextAdder._wrap_attr_call.<locals>.wrapped(*args, **kwargs)
     49 self._handling_exception = True
     50 try:
---> 51     add_line_number_to_exception(e, self)
     52 finally:
     53     del self._handling_exception

File ~/mambaforge/lib/python3.12/site-packages/montepy/errors.py:251, in add_line_number_to_exception(error, broken_robot)
    249 args = (message,) + args[1:]
    250 error.args = args
--> 251 raise error.with_traceback(trace)

File ~/mambaforge/lib/python3.12/site-packages/montepy/mcnp_object.py:43, in _ExceptionContextAdder._wrap_attr_call.<locals>.wrapped(*args, **kwargs)
     40 @functools.wraps(func)
     41 def wrapped(*args, **kwargs):
     42     try:
---> 43         return func(*args, **kwargs)
     44     except Exception as e:
     45         if len(args) > 0 and isinstance(args[0], MCNP_Object):

File ~/mambaforge/lib/python3.12/site-packages/montepy/data_inputs/fill.py:193, in Fill._parse_matrix(self, value)
    191 for j in self._axis_range(1):
    192     for k in self._axis_range(2):
--> 193         val = next(words)
    194         try:
    195             val._convert_to_int()

StopIteration:

Error came from Fill: Universe: None, transform: None from an unknown file.

So really this exception just needs to be caught and then re-raise as a MalformedInputError

@MicahGale MicahGale removed alpha testing Issues that came up during alpha testing question Further information is requested labels Mar 14, 2025
MicahGale added a commit that referenced this issue Mar 14, 2025
@MicahGale MicahGale linked a pull request Mar 14, 2025 that will close this issue
11 tasks
@MicahGale
Copy link
Collaborator Author

The plot thickens. I don't believe you need to specify every universe in the matrix. See section 5.5.5.3.1 of the 6.3.1 manual. The multi-universe fill is given by:

Cell-card Form: FILL $i_1:i_2$ $j_1:j_2$ $k_1:k_2$ $n_{1,1,1}$ $n_{2,1,1} . . . n_{i_1,j_1,k_1}. . . n_{i_2,j_2,k_2}$

$n_{i,j,k}$ Number of the universe with which to fill each existing lattice element (for
fully specified fill). Each element in the array corresponds to an element in
the lattice. The portion of the lattice covered by the array is filled and the
rest of the lattice does not exist ( 3 ).

Note 3: There are two nj values that can be used in the lattice array that have special meanings. A zero in the
level-zero (real world) lattice means that the lattice element does not exist, making it possible, in effect,
to specify a non-rectangular array. If the array value is the same as the number of the universe of the
lattice, that element is not filled with any universe but with the material specified on the cell card for the
lattice cell. Therefore, using the universe number of a real world lattice as an nj value to fill that element
with the cell material is not possible.

I think this means you can underspecify all matrix components. Am I right @tjlaboss.

@MicahGale MicahGale removed their assignment Mar 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bugs A deviation from expected behavior that does not reach the level of being reportable as an "Error".
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant