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.

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")
)

Out:

File CO2_measured_spectrum_4-5um.spec has a deprecrated structure (key _q_conv removed in 0.9.30). Fixed this time, but regenerate database for faster loading.

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)
  • plot 1T fit
  • plot 1T fit

Out:

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/latest/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/latest/lib/python3.8/site-packages/radis/test/files/hitran_co2_626_bandhead_4165_4200nm.h5 with metadata :
{'last_modification': 'Sun Sep 26 18:43:09 2021', '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/latest/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          None
   broadening_method    voigt
   cutoff               1e-25 cm-1/(#.cm-2)
   dbformat             hitran
   dbpath               /home/docs/checkouts/readthedocs.org/user_builds/radis/envs/latest/lib/python3.8/site-packages/radis...
   dlm_log_pG           0.1375350788016573
   dlm_log_pL           0.20180288881201608
   export_rovib_fraction  False
   folding_thresh       1e-06
   hdf5_engine          pytables
   include_neighbouring_lines  True
   levelsfmt            radis
   neighbour_lines      0 cm-1
   optimization         simple
   parfuncfmt           hapi
   parfuncpath          None
   parsum_mode          full summation
   pseudo_continuum_threshold  0
   truncation           1 cm-1
   wavenum_max_calc     2399.1537 cm-1
   wavenum_min_calc     2391.6923 cm-1
   waveunit             cm-1
   wstep                0.001 cm-1
   zero_padding         -1
Information
----------------------------------------
   chunksize            None
   db_use_cached        True
   export_lines         False
   export_populations   None
   load_energies        False
   lvl_use_cached       True
   total_lines          100
   warning_broadening_threshold  0.01
   warning_linestrength_cutoff  0.01
----------------------------------------
/home/docs/checkouts/readthedocs.org/user_builds/radis/envs/latest/lib/python3.8/site-packages/radis/misc/warning.py:350: LinestrengthCutoffWarning:

Estimated error after discarding lines is large: 0.07%. Consider reducing cutoff

0.02s - Spectrum calculated
/home/docs/checkouts/readthedocs.org/user_builds/radis/envs/latest/lib/python3.8/site-packages/radis/misc/warning.py:350: LinestrengthCutoffWarning:

Estimated error after discarding lines is large: 0.07%. Consider reducing cutoff

------------------------------
TYPICAL FIT CALCULATION TIME:
Fit (in progress) profiler :
    spectrum_calculation      0.017s β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ
        check_line_databank              0.001s
        reinitialize                     0.002s β–ˆ
            copy_database                    0.000s
            memory_usage_warning             0.001s β–ˆ
            reset_population                 0.000s
        scaled_eq_linestrength           0.001s β–ˆ
        applied_linestrength_cutoff      0.001s β–ˆ
        calc_lineshift                   0.001s
        calc_hwhm                        0.003s β–ˆβ–ˆ
        generate_wavenumber_arrays       0.000s
        calc_line_broadening             0.007s β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ
            precompute_DLM_lineshapes        0.002s β–ˆ
            DLM_Initialized_vectors          0.000s
            DLM_closest_matching_line        0.000s
            DLM_Distribute_lines             0.000s
            DLM_convolve                     0.004s β–ˆβ–ˆβ–ˆβ–ˆ
        calc_other_spectral_quan         0.001s β–ˆ
        generate_spectrum_obj            0.000s
------------------------------
/home/docs/checkouts/readthedocs.org/user_builds/radis/envs/latest/lib/python3.8/site-packages/radis/misc/warning.py:350: LinestrengthCutoffWarning:

Estimated error after discarding lines is large: 0.07%. Consider reducing cutoff


Now starting the fitting process:
---------------------------------

Tgas=1150.0, Residual: 0.0104  πŸ†
Tgas=1170.0, Residual: 0.0098  πŸ†
Tgas=1150.0, Residual: 0.0104
Tgas=1170.0, Residual: 0.0098  πŸ†
Tgas=2000.0, Residual: 0.0128
Tgas=1980.0, Residual: 0.0128
Tgas=1395.0, Residual: 0.0034  πŸ†
Tgas=1415.0, Residual: 0.0030  πŸ†
Tgas=1395.0, Residual: 0.0034
Tgas=1415.0, Residual: 0.0030  πŸ†
Tgas=1395.0, Residual: 0.0034
Tgas=1415.0, Residual: 0.0030  πŸ†
Tgas=1395.0, Residual: 0.0034
Tgas=1415.0, Residual: 0.0030  πŸ†
Tgas=1395.0, Residual: 0.0034
Tgas=1415.0, Residual: 0.0030  πŸ†
Tgas=1395.0, Residual: 0.0034
Tgas=1415.0, Residual: 0.0030  πŸ†
Tgas=1395.0, Residual: 0.0034
Tgas=1415.0, Residual: 0.0030  πŸ†
Tgas=1395.0, Residual: 0.0034
Tgas=1415.0, Residual: 0.0030  πŸ†
Tgas=1396.0, Residual: 0.0034
Tgas=1416.0, Residual: 0.0030  πŸ†
Tgas=1397.0, Residual: 0.0033
Tgas=1417.0, Residual: 0.0030  πŸ†
Tgas=1401.0, Residual: 0.0033
Tgas=1421.0, Residual: 0.0030  πŸ†
Tgas=1419.0, Residual: 0.0030
Tgas=1439.0, Residual: 0.0028  πŸ†
Tgas=1453.0, Residual: 0.0027  πŸ†
Tgas=1473.0, Residual: 0.0028
Init ['Tgas'] = [1150.]['']
Final ['Tgas'] = [1453.]['']
      fun: 0.002727708655538984
 hess_inv: <1x1 LbfgsInvHessProduct with dtype=float64>
      jac: array([2.76499054e-06])
  message: 'CONVERGENCE: NORM_OF_PROJECTED_GRADIENT_<=_PGTOL'
     nfev: 32
      nit: 4
     njev: 16
   status: 0
  success: True
        x: array([1453.4899474])
Best ['Tgas'] = [1453.4899474][''] reached at iteration 32/32

Total running time of the script: ( 0 minutes 3.310 seconds)

Gallery generated by Sphinx-Gallery