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
32 stars 7 forks source link

append_renumber() validates against old problem #466

Closed tjlaboss closed 3 months ago

tjlaboss commented 4 months ago

Describe the bug

When copying numbered objects from one MCNP_Problem to another, one can use NumberedObjectCollection.append_renumber(Numbered_MCNP_Object) to avoid number conflicts--unless the new number in the new problem is already in use in the old problem. This results in a fatal NumberConflictError.

Note that this only happens if a renumbering is necessary.

To Reproduce

prob1 = montepy.read_input("deck1.i")
prob2 = montepy.read_input("deck2.i")
prob2.append_renumber(prob1.materials[2])

Error Message (if any)

Traceback (most recent call last):
  File "./bugreport/montepy/numbered_object_collection.py", line 234, in append_renumber
    self.append(obj)
  File "./bugreport/montepy/numbered_object_collection.py", line 202, in append
    raise NumberConflictError(
montepy.errors.NumberConflictError: There was a numbering conflict when attempting to add MATERIAL: 2, ['iron'] to <class 'montepy.materials.Materials'>. Conflict was with MATERIAL: 2, ['iron']

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "./bugreport/mwe.py", line 9, in <module>
    deck2.materials.append_renumber(m2)
  File "./montepy/numbered_object_collection.py", line 237, in append_renumber
    obj.number = number
  File "./bugreport/montepy/utilities.py", line 105, in setter
    validator(self, value)
  File "./bugreport/montepy/data_inputs/material.py", line 20, in _number_validator
    self._problem.materials.check_number(number)
  File "./bugreport/montepy/numbered_object_collection.py", line 98, in check_number
    raise NumberConflictError(
montepy.errors.NumberConflictError: Number 3 is already in use for the collection: <class 'montepy.materials.Materials'> by MATERIAL: 3, ['hydrogen', 'oxygen']

MCNP input file snippet

See first comment.

Version

Additional context

Workaround: link the new numbered object to the new problem before calling append_renumber().

NumberedObjectCollection.append_renumber() automatically links the appended numbered object to the new problem (numbered_object_collection.py#L233-L241). It may be sufficient just to move this operation above the try...except.

tjlaboss commented 4 months ago

deck1.i

MWE of NumberConflictError: Source
C cells
c # hidden vertical Do not touch 
c
1 1 20
         -1000  $ dollar comment
    imp:n,p=1 U=350 trcl=5
2 2 8
      -1005
     imp:n=1
     imp:p=0.5
3 3 -1
      1000 1005 -1010
     imp:n,p=1
99 0
      1010
     imp:n,p=0
5 0 
      #99
      imp:n,p=3 fill=350 (1 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 Iron
m2        26054.80c        5.85
          26056.80c       91.75
          26057.80c        2.12
          26058.80c        0.28
C water
C foo
m3        1001.80c           2
           8016.80c           1
MT3 lwtr.23t h-zr.20t h/zr.28t
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

deck2.i

MWE of NumberConflictError: Destination
C cells
c # hidden vertical Do not touch 
c
1 1 20
         -1000  $ dollar comment
    imp:n,p=1 U=350 trcl=5
2 2 8
      -1005
     imp:n=1
     imp:p=0.5
3 1 20
      1000 1005 -1010
     imp:n,p=1
99 0
      1010
     imp:n,p=0
5 0 
      #99
      imp:n,p=3 fill=350 (1 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 Iron
m2        26054.80c        5.85
          26056.80c       91.75
          26057.80c        2.12
          26058.80c        0.28
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
MicahGale commented 3 months ago

Your MWE code seems to be incomplete.

tjlaboss commented 3 months ago

Added python snippet