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

error with materials in a new mcnp file #458

Closed marco-de-pietri closed 4 months ago

marco-de-pietri commented 4 months ago

Describe the bug

I am trying to reading and writing an mcnp file. An issue arise with writing some materials that include inline comments. The last inline comment is sent into a newline, causing an mcnp syntax issue

To Reproduce

import montepy

problem = montepy.read_input("test")
problem.write_to_file("unchanged", overwrite=True)

Error Message (if any)

none

MCNP input file snippet

original

M5     1001   6.33910e-002 $H   1 AMOUNT(%)  2.0000 AB(%) 99.99
         1002   7.29080e-006 $H   2 AMOUNT(%)  2.0000 AB(%)  0.01
         8016   3.15453e-002 $O  16 weight(%)     N/A ab(%) 99.76
         8017   1.20164e-005 $O  17 weight(%)     N/A ab(%)  0.04
         8017   6.48253e-005 $O  18 weight(%)     N/A ab(%)  0.20
C *

in new file

M5     1001   6.33910e-002 $H   1 AMOUNT(%)  2.0000 AB(%) 99.99
         1002   7.29080e-006 $H   2 AMOUNT(%)  2.0000 AB(%)  0.01
         8016   3.15453e-002 $O  16 weight(%)     N/A ab(%) 99.76
         8017   1.20164e-005 $O  17 weight(%)     N/A ab(%)  0.04
         8017   6.48253e-005
$O  18 weight(%)     N/A ab(%)  0.20
C *

Version

MicahGale commented 4 months ago

To test this I was able to replicate this with:

test model
1 5 -10 -1 imp:n=1

1 SO 0.5

M5     1001   6.33910e-002 $H   1 AMOUNT(%)  2.0000 AB(%) 99.99
         1002   7.29080e-006 $H   2 AMOUNT(%)  2.0000 AB(%)  0.01
         8016   3.15453e-002 $O  16 weight(%)     N/A ab(%) 99.76
         8017   1.20164e-005 $O  17 weight(%)     N/A ab(%)  0.04
         8017   6.48253e-005 $O  18 weight(%)     N/A ab(%)  0.20
C *
mode n

I think the issue is with how trailing comments are moved around. (It's assumed that a final trailing c comment is usually a description of the next input, and is migrated that way.) I think right now it's agnostic to if the comment is a dollar-sign comment.

The tree of the material is:

(Node: data: {'start_pad': (Padding, []), 'classifier': (Classifier: mod: None, prefix: (Value, M, padding: None), number: (Value, 5, padding: None), particles: None), 'keyword': (Value, None, padding: None), 'data': (list: number sequence, [(Value, 1001.0, padding: (Padding, ['   '])), (Value, 0.063391, padding: (Padding, [' ', COMMENT:
$H   1 AMOUNT(%)  2.0000 AB(%) 99.99, '\n', '         '])), (Value, 1002.0, padding: (Padding, ['   '])), (Value, 7.2908e-06, padding: (Padding, [' ', COMMENT:
$H   2 AMOUNT(%)  2.0000 AB(%)  0.01, '\n', '         '])), (Value, 8016.0, padding: (Padding, ['   '])), (Value, 0.0315453, padding: (Padding, [' ', COMMENT:
$O  16 weight(%)     N/A ab(%) 99.76, '\n', '         '])), (Value, 8017.0, padding: (Padding, ['   '])), (Value, 1.20164e-05, padding: (Padding, [' ', COMMENT:
$O  17 weight(%)     N/A ab(%)  0.04, '\n', '         '])), (Value, 8017.0, padding: (Padding, ['   '])), (Value, 6.48253e-05, padding: (Padding, [' ']))])})

and that of the next input, mode:

(Node: data: {'start_pad': (Padding, [COMMENT:
$O  18 weight(%)     N/A ab(%)  0.20, '\n', COMMENT:
C *, '\n']), 'classifier': (Classifier: mod: None, prefix: (Value, mode, padding: None), number: None, particles: None), 'keyword': (Value, None, padding: None), 'data': (list: particle sequence, [(Value, n, padding: None)])})

So the comment is getting moved around unnecessarily here.

MicahGale commented 4 months ago

Thank you @marco-de-pietri for the bug report again. Just as a head's up I will be on vacation until around mid-August, and will likely not get to any issues during that time.

marco-de-pietri commented 4 months ago

Thank you @marco-de-pietri for the bug report again. Just as a head's up I will be on vacation until around mid-August, and will likely not get to any issues during that time.

Thanks for the quick fixes! this tool looks quite promising - enjoy your vacations