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

Materials Are Not Updated In New Input File #465

Closed tjlaboss closed 3 months ago

tjlaboss commented 4 months ago

Describe the bug

When materials are added to or modified within an input file, the material cards are not updated. Old materials are still written, and new materials are not written. However, the cell is updated correctly.

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:

# Copy the Material using MontePy
# It should work, but it doesn't.

import montepy

deck1 = montepy.read_input("deck1.i")
deck2 = montepy.read_input("deck2.i")

# Assign a material from deck1 to deck2
new_mat = deck1.materials[4444]
new_mat.link_to_problem(deck2)
deck2.cells[2].material = new_mat
deck2.cells[2].mass_density = deck1.cells[2].mass_density
deck2.materials.remove(deck2.materials[2])
deck2.materials.append(new_mat)
print(deck2.materials)
deck2.write_to_file("deck3.i", overwrite=True)

Output

Materials: [1, 3, 4444]
./bugreport/copy_the_material.py:17: LineExpansionWarning: The input starting on Line 9 of: deck3.i expanded. 
The new input is:
         9| 2 4444 -4.44
        10|       -1005
        11|      imp:n=1
        12|      imp:p=0.5 
The value has expanded, and may change formatting. The original value was 2, new value is 4444.
  deck2.write_to_file("deck3.i", overwrite=True)

MCNP input file snippet

See comment below.

Version

Additional context

Add any other context about the problem here.

tjlaboss commented 4 months ago

deck1.i

MWE of Material not added: Source
C cells
c # hidden vertical Do not touch 
c
1 1 20
         -1000  $ dollar comment
    imp:n,p=1 U=350 trcl=5
c Cell filled with the material to copy
2 4444 -4.44
      -1005
     imp:n=1
     imp:p=0.5
c
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
C Material to Copy
C ================
m4444 92238.01c 8 92235.01c 5

deck2.i

MWE of Material not added
C cells
c # hidden vertical Do not touch 
c
1 1 20
         -1000  $ dollar comment
    imp:n,p=1 U=350 trcl=5
c Cell to fill with the new material
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

deck3.i

MWE of Material not added
C cells
c # hidden vertical Do not touch
c
1 1 20
         -1000  $ dollar comment
        imp:n,p=1 U=350 trcl=5
c Cell to fill with the new material
2 4444 -4.44
      -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
MicahGale commented 4 months ago

Material needs to be added to problem.data_inputs as well. There is an open issue about this as well that I can't look up right now.

tjlaboss commented 4 months ago

Issue #93

The related problem I've found is that updating the Material.material_components does not update the material written out. I will test if editing data_inputs solves that as well.

tjlaboss commented 4 months ago

The card in materials and in data_inputs is the same instance:

>>> id(deck2.data_inputs[1])
139756433280640
>>> id(deck2.materials[2])
139756433280640
>>> deck2.data_inputs[1] is deck2.materials[2]
True

Editing the material_components of Material 2 in one location obviously updates them in the other, but somehow the original composition is still written out.

Workaround: bring in a separate Material object and renumber it:

deck1.materials[4444].link_to_problem(deck2)
deck2.materials.append(deck1.materials[4444])
deck2.materials.remove(deck2.materials[2])
deck2.data_inputs[1] = deck1.materials[4444]
deck2.data_inputs[1].number = 2
deck2.write_to_file("deck4.i", overwrite=True)

deck4.i (data block)

C data
C materials
C UO2 5 atpt enriched
m1        92235.80c           5 & 
92238.80c          95  
C
C Material to Copy
C ================
m2    92238.01c 8 92235.01c 5
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
MicahGale commented 3 months ago

Ok so yes this would be solved by #93.

I'm entirely following the second part. Could you open a new issue about that?

tjlaboss commented 3 months ago

New issue: #470

tjlaboss commented 3 months ago

Resolved by the combination of #488 and #482.