yt-project / yt

Main yt repository
http://yt-project.org
Other
461 stars 276 forks source link

BUG: some RAMSES fields return different values depending on order of operations #4880

Closed V-Nathir closed 4 months ago

V-Nathir commented 5 months ago

With RAMSES datasets, the outcome of some fields depend on previous fields having been accessed or not.

For example, the following script yields two different results depending on whether the line querying sp["gas", "metallicity"] is commented out or not.

Code for reproduction

import yt
import numpy as np
ds = yt.load(PATH_TO_OUTPUT)  # may need adapting
center = ds.arr([0.48722672, 0.50215806, 0.50503517], "code_length")
sp = ds.sphere(center, (100, "kpc"))
# sp["gas", "metallicity"]  # commenting this line generates another result. 
cooling = sp["gas", "cooling_metal"]
print(cooling.min(), cooling.max(), np.isnan(cooling).sum())

Results

# When sp["gas", "metallicity"] is commented out
2.0029169125304782e-101 cm**3*erg/s     8.56002136482532e-22 cm**3*erg/s 0
# When it is not
1.3095962168963732e-105 cm**3*erg/s      6.197260629648163e-20 cm**3*erg/s 0

Expected outcome

The two cases should yield the same results.

Extra information

In fields.py script we can find the function def create_cooling_fields(self) -> bool: that has this line for computing "cooling_metal" cool = ( cool * data["gas", "metallicity"] / 0.02 ) # Ramses uses Zsolar=0.02 If the values of metallicity are checked (if they are >1 ) there are two possibilities: 1) you call in your code data["gas", "metallicity"]first and then data["gas", "cooling_metal"], the result is ok. 2) you call directly data["gas", "cooling_metal"] the result is wrong; metallicities are >1.

I think the FieldDetector saves values into metallicity and those values do not overwrite with the correct ones. I do not know if more fields have the same issue.

Cheers!

welcome[bot] commented 5 months ago

Hi, and welcome to yt! Thanks for opening your first issue. We have an issue template that helps us to gather relevant information to help diagnosing and fixing the issue.

matthewturk commented 5 months ago

@V-Nathir are the incorrect values always the same?

V-Nathir commented 5 months ago

I did a quick check and I would say yes. The incorrect values are always the same.

matthewturk commented 5 months ago

Thank you, that's very helpful to know. If they are always the same, it's deterministic; if they are not bitwise identical, it might be coming from uninitialized memory.

V-Nathir commented 4 months ago

Hi all, the bug is solved. I did the proper cheeks and looks good again. The solution provided by @cphyc in #4907 seems to work. Thanks a lot!!

image image image