Fit - inequality constraint
[1]:
import nexus as nx
#import numpy as np
[2]:
# load example spectrum
detuning, intensity = nx.data.Load("spectrum_equality.txt",
x_index = 0,
intensity_index = 1)
[3]:
#setup fit parameters
isomer = nx.Var(0.8, min = 0, max = 2, fit = True, id="isomer")
# assign to sites
site1 = nx.Hyperfine(
id = "site 1",
weight = 0.5,
isomer = isomer,
magnetic_field = nx.Var(value = 32, min = 0, max = 35, fit = True, id = "mag field 1"),
isotropic = False)
site2 = nx.Hyperfine(
id = "site 2",
weight = 0.5,
isomer = isomer,
magnetic_field = nx.Var(value = 14, min = 0, max = 35, fit = True, id = "mag field 2"),
isotropic = False)
mat_Fe = nx.Material.Template(nx.lib.material.Fe_enriched)
mat_Fe.hyperfine_sites = [site1, site2]
#create sample
lay_Fe = nx.Layer(id = "Fe",
material = mat_Fe,
thickness = 2800)
sample = nx.Sample(id = "sample",
layers = [lay_Fe],
geometry = "f")
beam = nx.Beam()
beam.LinearSigma()
exp = nx.Experiment(beam = beam,
objects = [sample],
isotope = nx.moessbauer.Fe57,
id = "my exp")
energy_spec = nx.EnergySpectrum(exp,
detuning,
intensity_data = intensity,
id = "energy spec")
[4]:
fit = nx.Fit([energy_spec],
inequalities = [lambda: site1.magnetic_field.value < site2.magnetic_field.value])
fit.options.method = "PagmoDiffEvol"
fit()
Run Fit instance with id:
Starting fit with 1 measurement data set(s) and 5 fit parameter(s):
no. | id | initial value | min | max
0 | ES scaling | 88182.9 | 0 | 8.81829e+06
1 | ES backgr | 7777.25 | 0 | 777725
2 | isomer | 0.8 | 0 | 2
3 | mag field 1 | 32 | 0 | 35
4 | mag field 2 | 14 | 0 | 35
Using 0 equality constraint(s) on parameter(s):
Using 1 inequality constraint(s).
Calling Pagmo solver with fit method PagmoDiffEvol
population: 60
iterations: 100
cost = 5.335555e+02
Calling ceres solver with fit method LevMar
Ceres Solver Report: Iterations: 4, Initial cost: 5.335555e+02, Final cost: 3.432344e+02, Termination: CONVERGENCE
Gradient error analysis.
Fit performed with algorithm:
PagmoDiffEvol
Local algorithm:
LevMar
Error analysis:
Gradient
Using 5 fit parameter(s):
no. | id | fit value | +/- std dev | initial value | min | max
0 | ES scaling | 100154 | 223.664 | 88182.9 | 0 | 8.81829e+06
1 | ES backgr | 0 | 170.2 | 7777.25 | 0 | 777725
2 | isomer | 1.00011 | 0.000490228 | 0.8 | 0 | 2
3 | mag field 1 | 9.99901 | 0.00644206 | 32 | 0 | 35
4 | mag field 2 | 33.0033 | 0.00650444 | 14 | 0 | 35
Correlation matrix:
no. | 0 1 2 3 4
-----|---------------------------------------------
0 | 1.000 -0.978 0.001 0.055 -0.002
1 | -0.978 1.000 -0.001 -0.056 0.002
2 | 0.001 -0.001 1.000 0.002 0.001
3 | 0.055 -0.056 0.002 1.000 0.010
4 | -0.002 0.002 0.001 0.010 1.000
Using 0 equality constraint(s) on parameter(s):
and 1 inequality constraint(s).
Total cost = 3.432e+02
cost for each FitMeasurement is:
no. | id | cost | %
0 | energy spec | 3.432e+02 | 100.000
Fit instance with id "" finished.
[5]:
energy_spec.Plot()