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

Bug when attempting to edit a cell geometry #558

Open MicahGale opened 2 hours ago

MicahGale commented 2 hours ago

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:

input = montepy.input_parser.mcnp_input.Input(["1 0 (-1 : 2) 3 imp:n=1"], montepy.input_parser.block_type.BlockType.CELL)
cell = montepy.Cell(input)
surf4 = montepy.surfaces.surface.Surface()
surf4.number = 4
surf5 = montepy.surfaces.surface.Surface()
surf5.number = 5
cell.geometry = (cell.geometry & -surf4) | +surf5

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:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[12], line 1
----> 1 cell.geometry = (cell.geometry & -surf4) | +surf5

File ~/mambaforge/lib/python3.11/site-packages/montepy/utilities.py:160, in make_prop_pointer.<locals>.decorator.<locals>.setter(self, value)
    158     value = base_type(value)
    159 if validator:
--> 160     validator(self, value)
    161 setattr(self, hidden_param, value)

File ~/mambaforge/lib/python3.11/site-packages/montepy/cell.py:30, in _link_geometry_to_cell(self, geom)
     28 def _link_geometry_to_cell(self, geom):
     29     geom._cell = self
---> 30     geom._add_new_children_to_cell(geom)

File ~/mambaforge/lib/python3.11/site-packages/montepy/surfaces/half_space.py:195, in HalfSpace._add_new_children_to_cell(self, other)
    193 for item in container:
    194     if item not in parent:
--> 195         parent.append(item)

File ~/mambaforge/lib/python3.11/site-packages/montepy/numbered_object_collection.py:248, in NumberedObjectCollection.append(self, obj)
    241 """Appends the given object to the end of this collection.
    242
    243 :param obj: the object to add.
    244 :type obj: Numbered_MCNP_Object
    245 :raises NumberConflictError: if this object has a number that is already in use.
    246 """
    247 if not isinstance(obj, self._obj_class):
--> 248     raise TypeError(f"object being appended must be of type: {self._obj_class}")
    249 if obj.number in self.numbers:
    250     raise NumberConflictError(
    251         (
    252             "There was a numbering conflict when attempting to add "
    253             f"{obj} to {type(self)}. Conflict was with {self[obj.number]}"
    254         )
    255     )

TypeError: object being appended must be of type: <class 'montepy.surfaces.surface.Surface'>

Version

Additional context

Add any other context about the problem here.

MicahGale commented 2 hours ago

If you then try to avoid this by

cell.surfaces.append(surf4)
cell.surfaces.append(surf5)

You get:

AttributeError                            Traceback (most recent call last)
Cell In[10], line 1
----> 1 cell.geometry = (cell.geometry & -surf4) | +surf5

File ~/mambaforge/lib/python3.11/site-packages/montepy/utilities.py:160, in make_prop_pointer.<locals>.decorator.<locals>.setter(self, value)
    158     value = base_type(value)
    159 if validator:
--> 160     validator(self, value)
    161 setattr(self, hidden_param, value)

File ~/mambaforge/lib/python3.11/site-packages/montepy/cell.py:30, in _link_geometry_to_cell(self, geom)
     28 def _link_geometry_to_cell(self, geom):
     29     geom._cell = self
---> 30     geom._add_new_children_to_cell(geom)

File ~/mambaforge/lib/python3.11/site-packages/montepy/surfaces/half_space.py:194, in HalfSpace._add_new_children_to_cell(self, other)
    190 for container, parent in zip(
    191     (cells, surfaces), (self._cell.complements, self._cell.surfaces)
    192 ):
    193     for item in container:
--> 194         if item not in parent:
    195             parent.append(item)

File ~/mambaforge/lib/python3.11/site-packages/montepy/numbered_object_collection.py:371, in NumberedObjectCollection.__contains__(self, other)
    370 def __contains__(self, other):
--> 371     return other in self._objects

File ~/mambaforge/lib/python3.11/site-packages/montepy/surfaces/surface.py:305, in Surface.__eq__(self, other)
    303 def __eq__(self, other):
    304     return (
--> 305         self.number == other.number
    306         and self.surface_type == other.surface_type
    307         and self.is_reflecting == other.is_reflecting
    308         and self.is_white_boundary == other.is_white_boundary
    309         and self.surface_constants == other.surface_constants
    310     )

AttributeError: 'int' object has no attribute 'number'