Skip to content

Commit

Permalink
Fix problems with tests and new names in smelib
Browse files Browse the repository at this point in the history
  • Loading branch information
AWehrhahn committed Dec 8, 2021
1 parent 10b4130 commit 5efc8ba
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 50 deletions.
4 changes: 2 additions & 2 deletions src/pysme/nlte.py
Original file line number Diff line number Diff line change
Expand Up @@ -1022,7 +1022,7 @@ def update_coefficients(self, sme, dll, lfs_nlte):

# Reset the departure coefficient every time, just to be sure
# It would be more efficient to just Update the values, but this doesn't take long
dll.ResetNLTE()
dll.ResetDepartureCoefficients()

if self.first:
self.first = False
Expand Down Expand Up @@ -1057,7 +1057,7 @@ def update_coefficients(self, sme, dll, lfs_nlte):
# loop through the list of relevant _lines_, substitute both their levels into the main b matrix
# Make sure both levels have corrections available
if lr[0] != -1 and lr[1] != -1:
dll.InputNLTE(bmat[:, lr].T, li)
dll.InputDepartureCoefficients(bmat[:, lr].T, li)

for elem in marked_for_removal:
self.remove_nlte(elem)
Expand Down
7 changes: 6 additions & 1 deletion src/pysme/sme_synth.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,12 @@ def GetOpacity(self, switch, species=None, key=None):
ACOOL: C1, Mg1, Al1, Si1, Fe1, CH, NH, OH
ALUKE: N1, O1, Mg2, Si2, Ca2
"""
return _smelib.GetOpacity(switch, key=key, species=species)
kwargs = {}
if key is not None:
kwargs["key"] = key
if species is not None:
kwargs["species "] = species
return _smelib.GetOpacity(switch, **kwargs)

def Ionization(self, ion=0):
"""
Expand Down
2 changes: 1 addition & 1 deletion src/pysme/smelib/_smelib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -618,7 +618,7 @@ static PyObject *smelib_InputDepartureCoefficients(PyObject *self, PyObject *arg
PyObject *bmatrix_obj = NULL;
PyArrayObject *bmatrix_arr = NULL;

if (!PyArg_ParseTuple(args, "iO", &linenumber, &bmatrix_obj))
if (!PyArg_ParseTuple(args, "Oi", &bmatrix_obj, &linenumber))
return NULL;

bmatrix_arr = (PyArrayObject *)PyArray_FROM_OTF(bmatrix_obj, NPY_DOUBLE, NPY_ARRAY_IN_ARRAY);
Expand Down
3 changes: 0 additions & 3 deletions src/pysme/solve.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,6 @@ def _residuals(
save = not isJacobian and self.filename is not None
reuse_wavelength_grid = isJacobian
radial_velocity_mode = "robust" if not isJacobian else "fast"
# method = "parallel" if isJacobian else "sequential"
method = "sequential"

# change parameters
for name, value in zip(self.parameter_names, param):
Expand All @@ -187,7 +185,6 @@ def _residuals(
passLineList=False,
updateLineList=self.update_linelist,
radial_velocity_mode=radial_velocity_mode,
method=method,
)
except AtmosphereError as ae:
# Something went wrong (left the grid? Don't go there)
Expand Down
58 changes: 24 additions & 34 deletions test/test_dll.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,32 +93,21 @@ def test_basic(libsme, wfirst, wlast, vw_scale):
libsme.SetVWscale(vw_scale)
libsme.SetH2broad()

print(
libsme.file,
libsme.wfirst,
libsme.wlast,
libsme.vw_scale,
libsme.h2broad,
)

# assert libsme.file
assert libsme.wfirst == wfirst
assert libsme.wlast == wlast
assert libsme.vw_scale == vw_scale
assert libsme.h2broad
# assert libsme.wfirst == wfirst
# assert libsme.wlast == wlast
# assert libsme.vw_scale == vw_scale
# assert libsme.h2broad


def test_linelist(libsme, linelist):
"""Test linelist behaviour"""
libsme.InputLineList(linelist)
outlist = libsme.OutputLineList()

print(libsme.linelist)
fmt = " Out: {0:10.4f},{2:7.4f},{1:7.3f},{3:5.2f},{4:6.2f},{5:8.3f}"
for i in range(len(linelist)):
outline = [x for x in outlist[i]]
outline[1] = np.log10(outline[1])
print(fmt.format(*outline))

# TODO
# libsme.UpdateLineList()
Expand All @@ -133,12 +122,6 @@ def test_atmosphere(libsme, atmo, teff, grav, vturb):
libsme.InputModel(teff, grav, vturb, atmo)

# TODO test different geometries

assert libsme.teff == teff
assert libsme.grav == grav
assert libsme.vturb == vturb
assert libsme.ndepth == len(atmo[atmo.depth])

with pytest.raises(ValueError):
libsme.InputModel(-1000, grav, vturb, atmo)

Expand Down Expand Up @@ -190,29 +173,36 @@ def test_transf(
libsme.InputWaveRange(wfirst, wlast)
libsme.Opacity()

nw, wave, synth, cont = libsme.Transf(mu, accrt, accwt)

print(nw, wave, synth, cont)
nw, wave, synth, cont = libsme.Transf(mu, accrt=accrt, accwi=accwt)
assert nw == 47

density = libsme.GetDensity()
print(atmo.rho)
print(density)
assert np.allclose(density, atmo.rho, rtol=1e-1, equal_nan=True)

xne = libsme.GetNelec()
print(xne)
print(atmo.xne)
assert np.allclose(xne, atmo.xne, rtol=2e-1)

xna = libsme.GetNatom()
print(xne)
print(atmo.xne)
assert np.allclose(xna, atmo.xna, rtol=1e-1)

libsme.GetLineOpacity(linelist.wlcent[0])
libsme.GetLineRange()
for switch in range(-3, 13):
if switch != 8:
print(switch)
libsme.GetOpacity(switch)
for switch in [
"COPSTD",
"COPRED",
"COPBLU",
"AHYD",
"AH2P",
"AHMIN",
"SIGH",
"AHE1",
"AHE2",
"AHEMIN",
"SIGHE",
# "ACOOL",
# "ALUKE",
"AHOT",
"SIGEL",
"SIGH2",
]:
libsme.GetOpacity(switch)
18 changes: 9 additions & 9 deletions test/test_nlte.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def test_dll(lfs_atmo, lfs_nlte):
sme.nlte.set_nlte(elem, "marcs2012p_t1.0_Ca.grd")

libsme = SME_DLL()
libsme.ResetNLTE()
libsme.ResetDepartureCoefficients()

syn = Synthesizer(None, lfs_atmo=lfs_atmo, lfs_nlte=lfs_nlte)
sme = syn.get_atmosphere(sme)
Expand All @@ -117,34 +117,34 @@ def test_dll(lfs_atmo, lfs_nlte):
for lr, li in zip(linerefs, lineindices):
if lr[0] != -1 and lr[1] != -1:
counter += 1
libsme.InputNLTE(bmat[:, lr].T, li)
libsme.InputDepartureCoefficients(bmat[:, lr].T, li)

flags = libsme.GetNLTEflags()
assert np.any(flags)
assert np.count_nonzero(flags) == counter
assert len(flags) == len(sme.linelist)

idx = np.where(flags)[0][0]
coeffs = libsme.GetNLTE(idx)
coeffs = libsme.GetDepartureCoefficients(idx)
assert coeffs is not None

# If we reset NLTE no flags should be set
libsme.ResetNLTE()
libsme.ResetDepartureCoefficients()
flags = libsme.GetNLTEflags()
assert not np.any(flags)
assert len(flags) == len(sme.linelist)

with pytest.raises(TypeError):
libsme.InputNLTE(None, 0)
libsme.InputDepartureCoefficients(None, 0)

with pytest.raises(TypeError):
libsme.InputNLTE(bmat[:, [0, 1]].T, 0.1)
libsme.InputDepartureCoefficients(bmat[:, [0, 1]].T, 0.1)

with pytest.raises(ValueError):
libsme.InputNLTE([0, 1], 10)
libsme.InputDepartureCoefficients([0, 1], 10)

with pytest.raises(ValueError):
libsme.InputNLTE(bmat[:, [0, 1]].T, -10)
with pytest.raises(RuntimeError):
libsme.InputDepartureCoefficients(bmat[:, [0, 1]].T, -10)


@pytest.fixture
Expand Down

0 comments on commit 5efc8ba

Please sign in to comment.