Fit - equality constraint
[1]:
import nexus as nx
import numpy as np
[2]:
# load example specturm
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")
mag_1 = nx.Var(value = 32, min = 25, max = 35, fit = True, id = "mag field 1")
# create dependent fit parameter
# we assume that the magnetic fields of the two sites have a difference of 23 T
# mag2 = mag1 - 23
def equality_mag2():
return mag_1.value - 23
mag_2 = nx.Var(value = 14, equality = equality_mag2, id = "mag field 2")
# assign to sites
site1 = nx.Hyperfine(
id = "site 1",
weight = 0.5,
isomer = isomer,
magnetic_field = mag_1,
isotropic = False)
site2 = nx.Hyperfine(
id = "site 2",
weight = 0.5,
isomer = isomer,
magnetic_field = mag_2,
isotropic = False)
#create sample
lay_Fe = nx.Layer(id = "Fe",
material = nx.Material.Template(nx.lib.material.Fe_enriched),
thickness = 3000)
lay_Fe.material.hyperfine_sites = [site1, site2]
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")
energy_spec()
energy_spec.Plot()
fit = nx.Fit([energy_spec], id = "test fit")
fit()
energy_spec.Plot()
Run Fit instance with id: test fit
Starting fit with 1 measurement data set(s) and 4 fit parameter(s):
no. | id | initial value | min | max
0 | ES scaling | 88229.4 | 1e-16 | 8.82294e+06
1 | ES backgr | 7794.22 | 0 | 779422
2 | isomer | 0.8 | 0 | 2
3 | mag field 1 | 32 | 25 | 35
Using 1 equality constraint(s) on parameter(s):
no. | id | value
0 | mag field 2 | 9
Using 0 inequality constraint(s).
Calling ceres solver with fit method LevMar
Ceres Solver Report: Iterations: 6, Initial cost: 8.814602e+04, Final cost: 2.033602e+02, Termination: CONVERGENCE
Gradient error analysis.
Fit performed with algorithm:
LevMar
Error analysis:
Gradient
Using 4 fit parameter(s):
no. | id | fit value | +/- std dev | initial value | min | max
0 | ES scaling | 99955.8 | 172.754 | 88229.4 | 1e-16 | 8.82294e+06
1 | ES backgr | 1034.29 | 129.968 | 7794.22 | 0 | 779422
2 | isomer | 1.00014 | 0.000385908 | 0.8 | 0 | 2
3 | mag field 1 | 32.9996 | 0.00361608 | 32 | 25 | 35
Values at boundaries:
Correlation matrix:
no. | 0 1 2 3
-----|------------------------------------
0 | 1.000 -0.977 0.001 0.039
1 | -0.977 1.000 -0.001 -0.040
2 | 0.001 -0.001 1.000 0.002
3 | 0.039 -0.040 0.002 1.000
Using 1 equality constraint(s) on parameter(s):
no. | id | value
0 | mag field 2 | 10.000
and 0 inequality constraint(s).
Total cost = 1.014e+00
cost for each FitMeasurement is:
no. | id | cost | %
0 | energy spec | 1.014e+00 | 100.000
Fit instance with id "test fit" finished.