Calculators
The calculator translates an EasyReflectometry model into a backend engine (refl1d or refnx) and computes reflectivity.
Polarized reflectivity
With the refl1d calculator and include_magnetism enabled, all four
spin channels are available:
polarized_reflectivity_profiles(x_array, model_id)returns the reflectivity of all four channels as a dictionary keyed'pp','pm','mp','mm'(in that order).polarization_channelselects which channelreflectity_profile— and hence fitting — uses (default'pp').magnetic_sld_profile(model_id)returns the nuclear and magnetic scattering length density profiles as a tuplez,sld(z),rhoM(z)(magnetic SLD) andthetaM(z)(magnetic angle).
Note that polarization_channel is state of the currently active
calculator instance, not of a model or dataset: it affects every
subsequent calculation using that calculator, and switching calculators
via the factory constructs a fresh instance, which resets the channel
(along with include_magnetism). Calculators without magnetism support
(refnx) raise NotImplementedError when magnetism is enabled.
polarization
POLARIZATION_CHANNEL_TO_INDEX = {PolarizationChannel.PP: 3, PolarizationChannel.PM: 2, PolarizationChannel.MP: 1, PolarizationChannel.MM: 0}
module-attribute
PolarizationChannel
Bases: str, Enum
Spin cross-section channels for polarized neutron reflectometry.
The accepted spellings are exactly the enum values ('pp', 'pm', 'mp', 'mm'); uppercase strings are rejected.
Source code in src/easyreflectometry/calculators/polarization.py
7 8 9 10 11 12 13 14 15 16 17 | |
calculator_base
Model
Bases: BaseCore
Model is the class that represents the experiment.
It is used to store the information about the experiment and to perform the calculations.
Source code in src/easyreflectometry/model/model.py
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 | |
guide_field_angle
property
writable
Field angle about the beam axis in degrees (refl1d Aguide); only magnetic calculators use it.
applied_field
property
writable
Applied field in tesla (refl1d H); only magnetic calculators use it.
total_thickness
property
Read-only parameter: the summed thickness of the film layers.
The superphase (first layer of the sample) and the subphase (last layer) are semi-infinite and excluded. The parameter is dependent: it tracks the layer thicknesses, never enters a fit and cannot be set; it can be referenced from constraint expressions and inequality constraints like any other parameter.
has_magnetism
property
Whether any layer in the sample carries magnetic properties.
is_default
property
writable
Whether this model was created as a default placeholder.
resolution_function
property
writable
Return the resolution function.
__init__(sample=None, scale=None, background=None, resolution_function=None, name='Model', color=COLORS[0], unique_name=None, interface=None, guide_field_angle=None, applied_field=None)
Constructor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
unique_name
|
Optional[str]
|
By default, None. |
None
|
color
|
str
|
By default, COLORS[0]. |
COLORS[0]
|
sample
|
Union[Sample, None]
|
The sample being modelled. By default, None. |
None
|
scale
|
Union[Parameter, Number, None]
|
Scaling factor of profile. By default, None. |
None
|
background
|
Union[Parameter, Number, None]
|
Linear background magnitude. By default, None. |
None
|
name
|
str
|
Name of the model. By default, 'Model'. |
'Model'
|
resolution_function
|
Union[ResolutionFunction, None]
|
Resolution function. By default, None. |
None
|
guide_field_angle
|
Union[Parameter, Number, None]
|
Field angle about the beam axis in degrees (refl1d |
None
|
applied_field
|
Union[Parameter, Number, None]
|
Applied field in tesla (refl1d |
None
|
interface
|
Calculator interface. By default, None. |
None
|
Source code in src/easyreflectometry/model/model.py
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 | |
refresh_derived()
Re-derive the model's computed parameters from the current layer structure.
Cheap (a few string operations) and idempotent: the dependency is
only rebuilt when the set of contributing layers changed. Called on
every access to :attr:total_thickness, so layers added or removed
through any path (Model.add_assemblies, assembly.layers.append,
...) are picked up without explicit notification.
Source code in src/easyreflectometry/model/model.py
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 | |
add_assemblies(*assemblies)
Add assemblies to the model sample.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*assemblies
|
list[BaseAssembly]
|
Assemblies to add to model sample. |
()
|
Source code in src/easyreflectometry/model/model.py
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 | |
duplicate_assembly(index)
Duplicate a given item or layer in a sample.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
index
|
int
|
Index of the item or layer to duplicate. |
required |
Source code in src/easyreflectometry/model/model.py
281 282 283 284 285 286 287 288 289 | |
remove_assembly(index)
Remove an assembly from the model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
index
|
int
|
Index of the item to remove. |
required |
Source code in src/easyreflectometry/model/model.py
291 292 293 294 295 296 297 298 299 | |
generate_bindings()
Propagate the interface over the sample tree, then register this model's resolution function on the calculator.
Runs on every interface assignment and on every engine switch (the project regenerates bindings on a fresh calculator then), so the new calculator always knows this model's resolution.
Source code in src/easyreflectometry/model/model.py
334 335 336 337 338 339 340 341 342 343 | |
to_dict(skip=None)
Serialize the model, encoding the resolution function and interface name.
Source code in src/easyreflectometry/model/model.py
368 369 370 371 372 373 374 375 376 377 378 379 380 381 | |
as_dict(skip=None)
Compatibility alias for :meth:to_dict.
Source code in src/easyreflectometry/model/model.py
383 384 385 | |
as_orso()
The sample as an ORSO simple-model (sample.model) dictionary.
Slab representation: lengths in angstrom, SLDs in 1/angstrom^2,
repeating multilayers via the inline N ( ... ) stack syntax.
Returns:
| Type | Description |
|---|---|
dict
|
The ORSO model-language dictionary (the content of an .ort file's
|
Source code in src/easyreflectometry/model/model.py
387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 | |
from_dict(passed_dict)
classmethod
Create a Model from a dictionary.
Source code in src/easyreflectometry/model/model.py
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 | |
ResolutionFunction
Source code in src/easyreflectometry/model/resolution_functions.py
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | |
smearing(q)
abstractmethod
Return the resolution as sigma (standard deviation) at each q.
Source code in src/easyreflectometry/model/resolution_functions.py
35 36 37 38 | |
from_dict(data)
classmethod
Smearing function.
Source code in src/easyreflectometry/model/resolution_functions.py
43 44 45 46 47 48 49 50 51 52 53 54 55 56 | |
CalculatorBase
A calculator: a thin layer over its wrapper, which reads the models it serves.
Every non-abstract subclass registers itself in _calculators and so
becomes available in :class:CalculatorFactory.
Source code in src/easyreflectometry/calculators/calculator_base.py
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 | |
models
property
The models served, keyed by unique_name.
supports_magnetism
property
Whether this calculator backend can model magnetic samples.
include_magnetism
property
writable
Include magnetism.
polarization_channel
property
writable
The spin channel ('pp', 'pm', 'mp' or 'mm') used by reflectity_profile when magnetism is enabled.
Note: this state belongs to the currently-active calculator instance; switching calculators via the factory constructs a fresh instance and resets it.
__init_subclass__(is_abstract=False, **kwargs)
Register every subclass, unless is_abstract, so that the factory can create it.
Source code in src/easyreflectometry/calculators/calculator_base.py
23 24 25 26 27 | |
__init__()
Init function.
Source code in src/easyreflectometry/calculators/calculator_base.py
29 30 31 | |
bind(obj)
Called for every object of a sample tree attached to this calculator; only models are kept.
Source code in src/easyreflectometry/calculators/calculator_base.py
35 36 37 38 | |
register_model(model)
Serve model.
Source code in src/easyreflectometry/calculators/calculator_base.py
40 41 42 | |
discard_model(model_id)
Stop serving the model with model_id.
Source code in src/easyreflectometry/calculators/calculator_base.py
44 45 46 | |
reset_storage()
Stop serving every model.
Source code in src/easyreflectometry/calculators/calculator_base.py
48 49 50 | |
reconcile_magnetism()
Switch include_magnetism off when the served models have just stopped being magnetic.
Source code in src/easyreflectometry/calculators/calculator_base.py
57 58 59 | |
reflectity_profile(x_array, model_id, resolution_function=None)
Determines the reflectivity profile for the given range and model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x_array
|
np.ndarray
|
Points to be calculated at. |
required |
model_id
|
str
|
The model id. |
required |
resolution_function
|
ResolutionFunction | None
|
Resolution to smear with; by default the one registered for the
model through :meth: |
None
|
Source code in src/easyreflectometry/calculators/calculator_base.py
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 | |
reflectivity_profile_channel(x_array, model_id, channel, resolution_function=None)
Determine the reflectivity profile of one explicit spin channel.
Unlike polarization_channel (global calculator state), the channel is an
argument, so several channels can be evaluated against the same model —
one per dataset in a simultaneous multi-channel fit.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x_array
|
np.ndarray
|
Points to be calculated at. |
required |
model_id
|
str
|
The model id. |
required |
channel
|
PolarizationChannel | str
|
One of 'pp', 'pm', 'mp', 'mm' (or the corresponding enum member). |
required |
resolution_function
|
ResolutionFunction | None
|
Resolution to smear with; by default the one registered for the model. |
None
|
Returns:
| Type | Description |
|---|---|
np.ndarray
|
Reflectivity of the requested channel at q. |
Source code in src/easyreflectometry/calculators/calculator_base.py
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 | |
polarized_reflectivity_profiles(x_array, model_id, resolution_function=None)
Determines the reflectivity profiles of all four spin channels for the given range and model.
Requires include_magnetism to be enabled and a calculator that supports it (refl1d).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x_array
|
np.ndarray
|
Points to be calculated at. |
required |
model_id
|
str
|
The model id. |
required |
resolution_function
|
ResolutionFunction | None
|
Resolution to smear with; by default the one registered for the model. |
None
|
Returns:
| Type | Description |
|---|---|
dict[str, np.ndarray]
|
Reflectivity per spin channel, keyed 'pp', 'pm', 'mp', 'mm' (in that order). |
Source code in src/easyreflectometry/calculators/calculator_base.py
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 | |
sld_profile(model_id)
Return the scattering length density profile.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_id
|
str
|
The model id. |
required |
Returns:
| Type | Description |
|---|---|
tuple[np.ndarray, np.ndarray]
|
z and sld(z). |
Source code in src/easyreflectometry/calculators/calculator_base.py
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 | |
magnetic_sld_profile(model_id)
Return the nuclear and magnetic scattering length density profiles.
Requires include_magnetism to be enabled and a calculator that supports it (refl1d).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_id
|
str
|
The model id. |
required |
Returns:
| Type | Description |
|---|---|
tuple[np.ndarray, np.ndarray, np.ndarray, np.ndarray]
|
z, sld(z), magnetic sld rhoM(z) and magnetic angle thetaM(z). |
Source code in src/easyreflectometry/calculators/calculator_base.py
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 | |
set_resolution_function(resolution_function, model_id=None)
Set the resolution function of one model, or the default for all others.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
resolution_function
|
ResolutionFunction
|
The resolution function. |
required |
model_id
|
str | None
|
The model id. |
None
|
Source code in src/easyreflectometry/calculators/calculator_base.py
185 186 187 188 189 190 191 192 193 194 195 196 | |
resolution_function_for(model_id=None)
The resolution function used when calculating the model with model_id.
Source code in src/easyreflectometry/calculators/calculator_base.py
198 199 200 | |