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()
../../_images/tutorial_optimization_nb_fit_equality_constraint_3_0.png

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 |             88182.9 |                0 |      8.81829e+06
    1 |                    ES backgr |             7777.25 |                0 |           777725
    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.795305e+04, Final cost: 2.348090e+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 |              99814 |       183.948 |       88182.9 |            0 |  8.81829e+06
    1 |                    ES backgr |            1152.35 |       138.011 |       7777.25 |            0 |       777725
    2 |                       isomer |            1.00012 |   0.000419557 |           0.8 |            0 |            2
    3 |                  mag field 1 |            33.0025 |    0.00392666 |            32 |           25 |           35

Correlation matrix:

  no. |        0        1        2        3
 -----|------------------------------------
    0 |    1.000   -0.977    0.001    0.041
    1 |   -0.977    1.000   -0.001   -0.042
    2 |    0.001   -0.001    1.000    0.002
    3 |    0.041   -0.042    0.002    1.000

Using 1 equality constraint(s) on parameter(s):

  no. |                           id |               value
    0 |                  mag field 2 |              10.003

and 0 inequality constraint(s).

Total cost = 2.348e+02

cost for each FitMeasurement is:

  no. |                           id |                cost |                   %
    0 |                  energy spec |           2.348e+02 |             100.000

Fit instance with id "test fit" finished.

../../_images/tutorial_optimization_nb_fit_equality_constraint_3_2.png