njoy / ENDFtk

Toolkit for reading and interacting with ENDF-6 formatted files
Other
33 stars 5 forks source link

Issue when chaining commands #190

Open thomasms opened 10 months ago

thomasms commented 10 months ago

Using U235 from ENDF-B-VIII.0 (n-092_U_235.endf) with the njoy file below I notice some issues when trying to parse the output (tape50) in ENDFtk.

moder
20 -30
reconr
-30 -21 /
'reconstructed u235' /
9228 /
0.001 /
0 /
broadr
-30 -21 -22
9228 /
0.01 /
300 /
0 /
heatr
-30 -22 -23 0 /
9228 3 /
302 318 402 /
moder
-23 50
stop

It seems strange to me, although perhaps I have made a mistake :), that the following produces a seg fault.

import ENDFtk as tk
x = tk.tree.Tape.from_file('./tape50').materials.front().section(3,2).parse()

whereas doing this in the more verbose way as below, produces no error.

import ENDFtk as tk

tape = tk.tree.Tape.from_file('./tape50')
mat = tape.materials.front()
mf3_mt2 = mat.section(3, 2).parse()
print(mf3_mt2.energies.to_list())

Note my system is: python3.11 with ENDFtk compiled from source on Ubuntu 20.04 with gcc 9.4.0.

Am I misunderstanding the ENDFtk API or could this be a bug?

whaeck commented 10 months ago

This seems to be an issue with the garbage collection in python.

The tk.tree.Tape.from_file('./tape50') command returns a newly constructed object (there is no "owner" of this object to keep a reference to it). Since commands are being chained onto it, a handle to it was never stored. I assume the garbage collector just cleaned it up.

I'm looking through the pybind11 documentation to see if there is anything I can do to resolve this. However, I think the best solution in this case is to just not chain from what is a newly created object that has no reference on the C++ side of things.