Note
Run this example online :
1 temperature fitΒΆ
Quickly fit an experimental spectrum with a one-temperature model,
directly from SpectrumFactory
,
with fit_spectrum()
The method requires a fitting model. An example model is provided in radis.tools.fitting
:
LTEModel()
. Other models can be used; such as in
the multi-temperature fit example
More advanced tools for interactive fitting of multi-dimensional, multi-slabs
spectra can be found in fitroom
.
Finally, the GPU-accelerated example shows
how to obtain real-time interactive spectra.
from radis import SpectrumFactory, load_spec
Here we get an experimental spectrum from RADIS test cases. Use your own instead.
from radis.test.utils import getTestFile, setup_test_line_databases
setup_test_line_databases()
# fit range
wlmin = 4167
wlmax = 4180
s_exp = (
load_spec(getTestFile("CO2_measured_spectrum_4-5um.spec"))
.crop(wlmin, wlmax, "nm")
.normalize()
.sort()
.offset(-0.2, "nm")
)
Customize the LTEModel()
for our case: we add a slit
(non fittable parameter) and normalize it
from radis.tools.fitting import LTEModel
def LTEModel_withslitnorm(factory, fit_parameters, fixed_parameters):
s = LTEModel(factory, fit_parameters, fixed_parameters)
# we could also have added a fittable parameter, such as an offset,
# or made the slit width a fittable parameter.
# ... any paramter in model_input will be fitted.
# s.offset(model_input["offset"], 'nm')
s.apply_slit(1.4, "nm")
return s.take("radiance").normalize()
using fit_spectrum()
import astropy.units as u
sf = SpectrumFactory(
wlmin * u.nm,
wlmax * u.nm,
wstep=0.001, # cm-1
pressure=1 * 1e-3, # bar
cutoff=1e-25,
isotope="1,2",
path_length=10, # cm-1
mole_fraction=1,
truncation=1, # cm-1
)
sf.warnings["MissingSelfBroadeningWarning"] = "ignore"
sf.warnings["HighTemperatureWarning"] = "ignore"
sf.load_databank("HITRAN-CO2-TEST")
s_best, best = sf.fit_spectrum(
s_exp.take("radiance"),
model=LTEModel_withslitnorm,
fit_parameters={
"Tgas": 300,
# "offset": 0
},
bounds={
"Tgas": [300, 2000],
# "offset": [-1, 1],
},
plot=True,
solver_options={
"maxiter": 15, # π increase to let the fit converge
"ftol": 1e-15,
},
verbose=2,
)
# plot_diff(s_exp, s_best) # , show_ruler=True)
Using database: HITRAN-CO2-TEST
'HITRAN-CO2-TEST':
{'info': 'HITRAN 2016 database, CO2, 1 main isotope (CO2-626), bandhead: 2380-2398 cm-1 (4165-4200 nm)', 'path': ['/home/docs/checkouts/readthedocs.org/user_builds/radis/envs/master/lib/python3.8/site-packages/radis/test/files/hitran_co2_626_bandhead_4165_4200nm.par'], 'format': 'hitran', 'parfuncfmt': 'hapi', 'levelsfmt': 'radis'}
Generating cache file /home/docs/checkouts/readthedocs.org/user_builds/radis/envs/master/lib/python3.8/site-packages/radis/test/files/hitran_co2_626_bandhead_4165_4200nm.h5 with metadata :
{'last_modification': 'Sun Aug 28 21:55:18 2022', 'wavenum_min': 2380.019436, 'wavenum_max': 2399.965532}
Reference databank (2391.79-2399.14cm-1) has 0 lines in range (2391.69-2399.15cm-1) for isotope 2. Change your range or isotope options
/home/docs/checkouts/readthedocs.org/user_builds/radis/envs/master/lib/python3.8/site-packages/radis/misc/log.py:54: UserWarning:
Reference databank (2391.79-2399.14cm-1) has 0 lines in range (2391.69-2399.15cm-1) for isotope 2. Change your range or isotope options
Calculating Equilibrium Spectrum
Physical Conditions
----------------------------------------
Tgas 300 K
Trot 300 K
Tvib 300 K
isotope 1,2
mole_fraction 1
molecule CO2
overpopulation None
path_length 10 cm
pressure_mbar 1.0 mbar
rot_distribution boltzmann
self_absorption True
state X
vib_distribution boltzmann
wavenum_max 2399.1537 cm-1
wavenum_min 2391.6923 cm-1
Computation Parameters
----------------------------------------
Tref 296 K
add_at_used
broadening_method voigt
cutoff 1e-25 cm-1/(#.cm-2)
dbformat hitran
dbpath /home/docs/checkouts/readthedocs.org/user_builds/radis/envs/master/lib/python3.8/site-packages/radis...
folding_thresh 1e-06
include_neighbouring_lines True
memory_mapping_engine auto
neighbour_lines 0 cm-1
optimization simple
parfuncfmt hapi
parsum_mode full summation
pseudo_continuum_threshold 0
sparse_ldm auto
truncation 1 cm-1
waveunit cm-1
wstep 0.001 cm-1
zero_padding -1
----------------------------------------
/home/docs/checkouts/readthedocs.org/user_builds/radis/envs/master/lib/python3.8/site-packages/radis/misc/warning.py:354: LinestrengthCutoffWarning:
Estimated error after discarding lines is large: 0.07%. Consider reducing cutoff
0.03s - Spectrum calculated
/home/docs/checkouts/readthedocs.org/user_builds/radis/envs/master/lib/python3.8/site-packages/radis/misc/warning.py:354: LinestrengthCutoffWarning:
Estimated error after discarding lines is large: 0.07%. Consider reducing cutoff
------------------------------
TYPICAL FIT CALCULATION TIME:
Fit (in progress) profiler :
spectrum_calculation 0.025s ββββββββββββββββ
check_line_databank 0.001s
reinitialize 0.002s β
copy_database 0.000s
memory_usage_warning 0.002s β
reset_population 0.000s
scaled_eq_linestrength 0.002s β
applied_linestrength_cutoff 0.001s
calc_lineshift 0.001s
calc_hwhm 0.003s ββ
generate_wavenumber_arrays 0.000s
calc_line_broadening 0.011s βββββββ
precompute_LDM_lineshapes 0.003s β
LDM_Initialized_vectors 0.000s
LDM_closest_matching_line 0.000s
LDM_Distribute_lines 0.005s βββ
LDM_convolve 0.003s β
calc_other_spectral_quan 0.003s β
generate_spectrum_obj 0.000s
------------------------------
/home/docs/checkouts/readthedocs.org/user_builds/radis/envs/master/lib/python3.8/site-packages/radis/misc/warning.py:354: LinestrengthCutoffWarning:
Estimated error after discarding lines is large: 0.07%. Consider reducing cutoff
Now starting the fitting process:
---------------------------------
Tgas=1150.0, Residual: 0.0105 π
Tgas=1170.0, Residual: 0.0098 π
Tgas=1150.0, Residual: 0.0105
Tgas=1170.0, Residual: 0.0098 π
Tgas=2000.0, Residual: 0.0128
Tgas=1980.0, Residual: 0.0128
Tgas=1396.0, Residual: 0.0034 π
Tgas=1416.0, Residual: 0.0031 π
Tgas=1396.0, Residual: 0.0034
Tgas=1416.0, Residual: 0.0031 π
Tgas=1396.0, Residual: 0.0034
Tgas=1416.0, Residual: 0.0031 π
Tgas=1396.0, Residual: 0.0034
Tgas=1416.0, Residual: 0.0031 π
Tgas=1396.0, Residual: 0.0034
Tgas=1416.0, Residual: 0.0031 π
Tgas=1396.0, Residual: 0.0034
Tgas=1416.0, Residual: 0.0031 π
Tgas=1396.0, Residual: 0.0034
Tgas=1416.0, Residual: 0.0031 π
Tgas=1396.0, Residual: 0.0034
Tgas=1416.0, Residual: 0.0031 π
Tgas=1396.0, Residual: 0.0034
Tgas=1416.0, Residual: 0.0031 π
Tgas=1397.0, Residual: 0.0034
Tgas=1417.0, Residual: 0.0030 π
Tgas=1402.0, Residual: 0.0033
Tgas=1422.0, Residual: 0.0030 π
Tgas=1420.0, Residual: 0.0030
Tgas=1440.0, Residual: 0.0028 π
Tgas=1458.0, Residual: 0.0027 π
Tgas=1478.0, Residual: 0.0028
Init ['Tgas'] = [1150.]['']
Final ['Tgas'] = [1458.]['']
fun: 0.0027300270273360972
hess_inv: <1x1 LbfgsInvHessProduct with dtype=float64>
jac: array([3.1783589e-06])
message: 'CONVERGENCE: NORM_OF_PROJECTED_GRADIENT_<=_PGTOL'
nfev: 32
nit: 4
njev: 16
status: 0
success: True
x: array([1457.50267416])
Best ['Tgas'] = [1457.50267416][''] reached at iteration 32/32
Total running time of the script: ( 0 minutes 3.451 seconds)