def test_visualTestBaseline(plot=True, *args, **kwargs): s = load_spec(getTestFile("CO_Tgas1500K_mole_fraction0.01.spec"), binary=True) s.update('radiance_noslit', verbose=False) s.apply_slit(0.1) s = Radiance_noslit(s) assert s.units['radiance'] == 'mW/cm2/sr/nm' s2 = sub_baseline(s, 2e-4, -2e-4, name='sub_arb_baseline') if plot: plot_diff(s, s2)
def test_multiplyAndAddition(*args, **kwargs): s = load_spec(getTestFile("CO_Tgas1500K_mole_fraction0.01.spec"), binary=True) s.update('radiance_noslit', verbose=False) s.apply_slit(0.1) s = Radiance(s) assert s.units['radiance'] == 'mW/cm2/sr/nm' s_bis = add_constant(s, 1, 'mW/cm2/sr/nm') w, Idiff = get_diff(s_bis, s, 'radiance') test = Idiff[1] - 1 assert np.all(test < 1e-10) s_ter = multiply(multiply(s, 50), 1 / 50) # plot_diff(s_ter, s_5) diff = get_diff(s_ter, s, 'radiance') ratio = abs(np.trapz(diff[1], x=diff[0]) / s.get_integral('radiance')) assert ratio < 1e-10
def test_offset(plot=True): s = load_spec(getTestFile("CO_Tgas1500K_mole_fraction0.01.spec"), binary=True) s.update('radiance_noslit', verbose=False) s.apply_slit(0.1) s2 = offset(s, 10, 'nm', name='offset_10nm') if plot: plot_diff(s, s2) assert np.allclose(s2.get_wavelength(which='convoluted'), s.get_wavelength(which='convoluted') + 10) assert np.allclose(s2.get_wavelength(which='non_convoluted'), s.get_wavelength(which='non_convoluted') + 10) # Test inplace version s.offset(10, 'nm') assert np.allclose(s2.get_wavelength(which='convoluted'), s.get_wavelength(which='convoluted'))
wmin = 2000 wmax = 2400 dv = 0.002 T = 3000.0 #K p = 0.1 #bar broadening_max_width = 5 # lineshape broadening width, ext spectra_default = {} spectra_DLM = {} spectra_DLM_opt = {} Nlines = [101, 1001, 10017, 100725, 1813040, 1813040] plt.ion() for N in Nlines: spectra_default[N] = load_spec( '../spec/spectra_default_[{0}].spec'.format(N), binary=True) spectra_DLM_opt[N] = load_spec( '../spec/spectra_DLM_opt[{0}].spec'.format(N), binary=True) # %% Plot last spectra (opt) fig, [ax0, ax1] = plot_diff(spectra_default[N], spectra_DLM_opt[N], 'abscoeff') ax0.set_ylim((0, 0.5)) ax1.set_ylim((-0.005, 0.005)) axins = ax0.inset_axes([0.06, 0.3, 0.3, 0.4]) axins.ticklabel_format(useOffset=False, axis='x') axins.tick_params(which='major', labelsize=14) axins.get_yaxis().set_visible(False) axins.plot(*spectra_default[N].get('abscoeff', wunit='cm-1'), color='k', lw=2)
#%% Calculate Reference #%% spectra_default = {} # calculated with Whiting's Voigt approximatino spectra_convolve = {} # calculated with no approximatino ##broadening_max_widths = [0.05, 0.1, 1, 5, 10, 15, 20, 25, 50] broadening_max_widths = [0.1,50] Nlines = 1e4 #%% Calculate normal for width in broadening_max_widths: spectra_default[width] = load_spec('../spec_fig78/spectra_default[{0}].spec'.format(width), binary=True) spectra_convolve[width] = load_spec('../spec_fig78/spectra_convolve[{0}].spec'.format(width), binary=True) spectra_DLM_opt = load_spec('../spec_fig78/spectra_DLM_opt.spec') plt.ion() ##fig, [ax0, ax1] = plot_diff(spectra_default[broadening_max_widths[1]], spectra_DLM_opt, 'abscoeff', figsize=(10,6)) fig, [ax0, ax1] = plot_diff(spectra_default[broadening_max_widths[1]], spectra_DLM_opt, 'abscoeff', figsize=(10,6)) ax0.set_ylim((0, 0.9)) ax1.set_ylim((-0.0025, 0.0025)) from mpl_toolkits.axes_grid1.inset_locator import inset_axes
def test_validation_vs_specair(rtol=1e-2, verbose=True, plot=False, *args, **kwargs): ''' Test RADIS output on CO IR bands against SPECAIR Test is only performed on integrals of absorption coefficient RADIS doesnt actually match Specair exactly, but this is due to line intensity differences (Specair has no rovibrational specific intensities) rather than differences in populations calculations, as evidenced by the partition functions comparison in the RADIS presentation article. ''' setup_test_line_databases() # add HITRAN-CO-TEST in ~/.radis if not there # %% Specair calculation # ----------- specair_300_300 = load_spec(getValidationCase( join('test_validation_vs_specair_noneqCO_data', 'specair_CO_IR_Tvib300_Trot300.spec')), binary=True) specair_300_2000 = load_spec(getValidationCase( join('test_validation_vs_specair_noneqCO_data', 'specair_CO_IR_Tvib300_Trot2000.spec')), binary=True) specair_2000_300 = load_spec(getValidationCase( join('test_validation_vs_specair_noneqCO_data', 'specair_CO_IR_Tvib2000_Trot300.spec')), binary=True) article_version = False # just for the article # %% Compare with RADIS # ---------- wstep = 0.002 pl = SpectrumFactory( wavelength_min=4400, wavelength_max=4900, mole_fraction=1, pressure=0.01, # bar path_length=1, # we dont care for abscoeff anyway parallel=False, cutoff=1e-30, wstep=wstep, isotope=1, # '1,2,3', medium='vacuum') # 0.2) pl.warnings['MissingSelfBroadeningWarning'] = 'ignore' if article_version: pl.load_databank('HITEMP-CO-DUNHAM') else: pl.load_databank('HITRAN-CO-TEST') # Available on all systems, convenient for fast testing, but you # will be missing some small lines for T ~ 2000 K . print( 'Using HITRAN: small lines will be missing at T ~ 2000K. Use HITEMP if you want them' ) s_300_300 = pl.eq_spectrum(300, name='RADIS') s_2000_300 = pl.non_eq_spectrum(Tvib=2000, Trot=300, Ttrans=300, name='RADIS') s_300_2000 = pl.non_eq_spectrum(Tvib=300, Trot=2000, Ttrans=2000, name='RADIS') # %% Test # Compare integrals b1 = np.isclose(specair_300_300.get_integral('abscoeff'), s_300_300.get_integral('abscoeff'), rtol=rtol) b1 *= np.isclose(specair_2000_300.get_integral('abscoeff'), s_2000_300.get_integral('abscoeff'), rtol=rtol) b1 *= np.isclose(specair_300_2000.get_integral('abscoeff'), s_300_2000.get_integral('abscoeff'), rtol=rtol) # Compare partition functions to hardcoded values b2 = np.isclose(s_2000_300.lines.Q, 139, atol=1) # Specair: 139 b2 *= np.isclose(s_300_2000.lines.Q, 727, atol=1) # Specair: 727 if verbose: printm( '>>> comparing RADIS vs SPECAIR on CO: integrals of abscoeff is are close' + ' to within {0:.1f}%: {1} ({2:.1f}%, {3:.1f}%, {4:.1f}%)'.format( rtol * 100, bool(b1), abs( specair_300_300.get_integral('abscoeff') / s_300_300.get_integral('abscoeff') - 1) * 100, abs( specair_2000_300.get_integral('abscoeff') / s_2000_300.get_integral('abscoeff') - 1) * 100, abs( specair_300_2000.get_integral('abscoeff') / s_300_2000.get_integral('abscoeff') - 1) * 100, )) printm('>>> comparing RADIS vs SPECAIR on CO: partition functions ' + 'are equal to round error: {0}'.format(bool(b2))) if plot: plot_diff( specair_300_300, s_300_300, title=r'T$_\mathregular{vib}$ 300 K, T$_\mathregular{rot}$ 300 K', diff_window=int( 0.02 // wstep ), # compensate for small shifts in both codes. we're comparing intensities here. lw_multiplier=1, #0.75, wunit='nm_vac', plot_medium=True, ) plt.xlim((4500, 4900)) if article_version: plt.savefig( 'out/test_validation_vs_specair_noneqCO_Tvib300_Trot300.png') plt.savefig( 'out/test_validation_vs_specair_noneqCO_Tvib300_Trot300.pdf') plot_diff( specair_2000_300, s_2000_300, title=r'T$_\mathregular{vib}$ 2000 K, T$_\mathregular{rot}$ 300 K', diff_window=int( 0.02 // wstep ), # compensate for small shifts in both codes. we're comparing intensities here. lw_multiplier=1, #0.75, wunit='nm_vac', plot_medium=True, ) plt.xlim((4500, 4900)) if article_version: plt.savefig( 'out/test_validation_vs_specair_noneqCO_Tvib2000_Trot300.png') plt.savefig( 'out/test_validation_vs_specair_noneqCO_Tvib2000_Trot300.pdf') plot_diff( specair_300_2000, s_300_2000, title=r'T$_\mathregular{vib}$ 300 K, T$_\mathregular{rot}$ 2000 K', diff_window=int( 0.02 // wstep ), # compensate for small shifts in both codes. we're comparing intensities here. lw_multiplier=1, #0.75, wunit='nm_vac', plot_medium=True, ) plt.xlim((4500, 4900)) if article_version: plt.savefig( 'out/test_validation_vs_specair_noneqCO_Tvib300_Trot2000.png') plt.savefig( 'out/test_validation_vs_specair_noneqCO_Tvib300_Trot2000.pdf') return bool(b1 * b2)
# -*- coding: utf-8 -*- """ Created on Tue Jul 31 23:45:17 2018 @author: erwan See Issue #5 """ from radis import load_spec, MergeSlabs from radis.misc.arrays import count_nans s1 = load_spec('cruden_2611K_1.spec') s2 = load_spec('cruden_2611K_2.spec') assert count_nans(s1._q['emissivity_noslit']) == 0 assert count_nans(s2._q['emissivity_noslit']) == 0 s = MergeSlabs(s1, s2, resample_wavespace='full', out_of_bounds='transparent') assert count_nans(s._q['emissivity_noslit']) == 0
return r'$\mathregular{{{m:s}\cdot 10^{{{e:d}}} }}$'.format(m=m, e=int(e)) def str_fmt(x, n=0): " Format x into nice Latex rounding to n" # power = int(np.log10(round_to_n(x, 0))) # f_SF = round_to_n(x, n) * pow(10, -power) # return as_si(f_SF, power) return as_si(x, n) #%% s_ref = load_spec('../spec/' + case + '.spec', binary=True) s_opt = load_spec('../spec/' + case + '_optim.spec', binary=True) s_dlm = load_spec('../spec/h2o_2200K_1000-2000cm-1_2200K_DLM.spec', binary=True) s_ref.update() s_opt.update() # compare times t_ref = s_ref.conditions['calculation_time'] t_opt = s_opt.conditions['calculation_time'] t_dlm = s_dlm.conditions['calculation_time'] #%% plt.ion() #%% _, [ax0, ax1] = plot_diff( s_ref,
s.get_wavelength(which='convoluted')) if __name__ == '__main__': from radis import load_spec, get_diff, plot_diff from radis.test.utils import getTestFile import numpy as np import pytest test_multiplyAndAddition() test_offset() # An implement of Spectrum Algebra # Reload: s = load_spec(getTestFile('CO_Tgas1500K_mole_fraction0.01.spec')) s.update() # Test addition of Spectra s.plot(lw=2, nfig='Merge: s//s') (s // s).plot(nfig='same') # Test substraction of Spectra s_tr = Transmittance_noslit(s) assert (s_tr - 1.0 * s_tr).get_integral('transmittance_noslit') == 0 # TODO: add test # @EP: the test fails at the moment because multiply only works with radiance, # and MergeSlabs only works with non convoluted quantities # Do we want that? Up for discussion...