
I have prepared a simpler test code to demonstrate the issue. Here I define one field ("gas, "n_e") through the function _e_dens. I then define a field parameter lmin through a call to set_field_parameter. Inside the function _e_dens I put a simple if..else loop which should print a message when lmin is read. Here is the code:
""" pl_test_set_param.py """ import numpy as np import yt from yt.utilities.cosmology import Cosmology from yt import derived_field import matplotlib.pyplot as plt from yt.fields.api import ValidateParameter from yt import YTArray, YTQuantity
def _e_dens(field, data): from yt.units import mass_hydrogen if data.has_field_parameter("lmin"): lmin = data.get_field_parameter("lmin") print("From _e_dens lmin=",lmin) else: lmin = YTQuantity(0.,"kpc") print("From _e_dens not read lmin",lmin)
return data[("gas","density")]/mass_hydrogen
cosmo = Cosmology() LFLY = YTArray(1.0/cosmo.hubble_constant.value,"Mpc").in_cgs() TFLY = (2./3./cosmo.hubble_constant).in_cgs() MFLY = YTArray(5.23e12/cosmo.hubble_constant.value,"Msun").in_cgs() uo = {"length_unit":LFLY, "time_unit":TFLY, "mass_unit":MFLY}
dir='/g100_work/INA20_C6T28_0/virgo/v0000/Run0000/' f_root='virgoS12_str_hdf5_plt_cnt_' f_index='0015' fname = dir+f_root+f_index ds=yt.load(fname,units_override=uo) dd=ds.all_data()
dd.add_field(("gas", "n_e"), function=_e_dens, units="cm**(-3)", sampling_type='cell', display_name="Electron density", force_override=True)
lmin=YTQuantity(3.2,"kpc") dd.set_field_parameter("lmin", lmin)
plt_ne = yt.SlicePlot(ds, 'x', 'n_e', width=((120,'kpc'),(200,'kpc'))) plt_ne.annotate_title('Electron density') plt_ne.set_font({'family': 'monospace', 'style': 'normal','size': 36}) """ Electron density limits in cm^-3. """ ne_l = 1.e-6 ne_u = 1.e-1 plt_ne.set_zlim('n_e', ne_l, ne_u) plt_ne.save('n_e_v0000_15_v2.png') #========================
The code correctly creates a slice of n_e. However from the if..else loop inside _e_dens I get the following output: ... From _e_dens not read lmin 0.0 kpc From _e_dens not read lmin 0.0 kpc From _e_dens not read lmin 0.0 kpc ...
demonstrating that the field parameter lmin is not read from inside _e_dens, as it should. Again, I would appreciate any fresh insight.