Repository navigation
Description
Summary
A magnetic model created on a shared refl1d calculator can get a wrong reflectivity curve. This happens when the new model is given the unique_name of an earlier model that is unreachable but has not been garbage-collected yet (Model_0 in the example below). The curve is wrong in the unpolarized calculation and in every spin channel. It is not a small numerical difference: it is 25–45% off a model with identical parameters.
No error or warning is raised, so fits and plots silently use the wrong curve.
Reproduction
The script below runs on master / 423-variable-resolution with refl1d 1.0.1.
import numpy as np
from easyreflectometry.calculators import CalculatorFactory
from easyreflectometry.model import Model
from easyreflectometry.sample import Layer, LayerMagnetism, Material, Multilayer, Sample
def film_model(name, interface):
superphase = Layer(material=Material(0, 0, name=f'Vacuum {name}'), thickness=0, roughness=0, name=f'Super {name}')
layer = Layer(material=Material(4.0, 0, name=f'Film {name}'), thickness=100, roughness=3,
magnetism=LayerMagnetism(rho_m=1.5, theta_m=270.0), name=f'Layer {name}')
subphase = Layer(material=Material(2.07, 0, name=f'Si {name}'), thickness=0, roughness=3, name=f'Sub {name}')
sample = Sample(Multilayer(superphase), Multilayer(layer), Multilayer(subphase), name=f'Sample {name}')
return Model(sample=sample, scale=1, background=0, name=name, interface=interface)
def pp_curve_of_a_released_model(interface, q):
first = film_model('first', interface)
return first.unique_name, interface().reflectivity_profile_channel(q, first.unique_name, 'pp')
# `first` is released on return, but not yet garbage-collected
q = np.linspace(0.01, 0.3, 40)
interface = CalculatorFactory()
interface.switch('refl1d')
first_name, reference = pp_curve_of_a_released_model(interface, q)
second = film_model('second', interface) # identical parameters
curve = interface().reflectivity_profile_channel(q, second.unique_name, 'pp')
print('unique names:', first_name, second.unique_name)
print('max relative difference:', np.max(np.abs(curve / reference - 1)))
third = film_model('third', interface) # created while `second` is alive
print('third (fresh name):', third.unique_name,
np.max(np.abs(interface().reflectivity_profile_channel(q, third.unique_name, 'pp') / reference - 1)))Output:
unique names: Model_0 Model_0
max relative difference: 0.25675614277192804
third (fresh name): Model_1 0.0
third has the same parameters but a fresh name, and it matches the reference exactly. So the wrong curve belongs to the model that reused the name, not to the reference.
Scope
All runs below use the same two identical models. The first one is released before the second is created.
| Case | Name reused | gc.collect() before 2nd model |
Max relative error |
|---|---|---|---|
| magnetic, refl1d, unpolarized | yes | no | 0.257 |
magnetic, refl1d, pp |
yes | no | 0.257 |
magnetic, refl1d, mm |
yes | no | 0.455 |
| magnetic, refl1d, all three above | yes | yes | 0.0 |
| non-magnetic, refl1d | yes | no | 0.0 |
| non-magnetic, refnx | yes | no | 0.0 |
So the trigger needs all of the following:
- a model with layer magnetism (refl1d),
- a name reused while the previous owner of that name is still uncollected (typically held in a reference cycle),
- the shared calculator.
If the old model is collected first (gc.collect()), the result is correct.
Where it shows up
- Any code path that drops a model and builds a new one on the same
CalculatorFactory. Examples: removing and re-adding a model in aProject/GUI session, rebuilding a model from a dict, or test helpers that create a "truth" model in a function and fit a second model afterwards. - It was found while writing a polarized masked-fit regression test for variable resolution fitting #424. A helper function generated data from a truth model and returned it. The model fitted afterwards reused
Model_0, and the fit converged toscale = 0.93instead of1.0. The test now keeps the truth model alive as a workaround.
Possible cause (not confirmed)
Refl1dWrapper keeps its state keyed by name: layer slabs in storage['layer'], magnetic values in _layer_magnetism, and _polarized_cache. The names come from the global_object map. A reused name may therefore pick up, or be overwritten by, the stale entries of the uncollected model. Another possibility is that the old model's cleanup runs against the new model's entries. The non-magnetic path is unaffected, which points at the magnetism-specific state rather than the generic layer/item storage.
Two questions to answer:
- Why does the
global_objectmap hand out a name whose previous owner is still alive (only unreachable)? - Which wrapper state survives under that name?
Expected behaviour
Two models with identical parameters give identical curves, whatever names previous models had and whenever those models are collected.
Metadata
Metadata
Assignees
Labels
Projects
- StatusShow more project fieldsDone
Activity
426 fix wrong curve (#428)
🎉 Release: merge develop into master (#442)
Release 2.0.0 (#454)