mirror of
https://github.com/Cantera/cantera.git
synced 2026-08-19 01:14:44 -05:00
[samples] Tweak support of HDF output
This commit is contained in:
committed by
Ray Speth
parent
0d243c640a
commit
c15b447f48
@@ -33,7 +33,7 @@ try:
|
||||
# save to HDF container file if h5py is installed
|
||||
f.write_hdf('adiabatic_flame.h5', group='mix', mode='w',
|
||||
description='solution with mixture-averaged transport')
|
||||
except:
|
||||
except ImportError:
|
||||
f.save('adiabatic_flame.xml', 'mix',
|
||||
'solution with mixture-averaged transport')
|
||||
|
||||
@@ -48,7 +48,7 @@ print('multicomponent flamespeed = {0:7f} m/s'.format(f.velocity[0]))
|
||||
try:
|
||||
f.write_hdf('adiabatic_flame.h5', group='multi',
|
||||
description='solution with multicomponent transport')
|
||||
except:
|
||||
except ImportError:
|
||||
f.save('adiabatic_flame.xml', 'multi',
|
||||
'solution with multicomponent transport')
|
||||
|
||||
|
||||
@@ -27,7 +27,7 @@ try:
|
||||
# save to HDF container file if h5py is installed
|
||||
f.write_hdf('burner_flame.h5', group='mix', mode='w',
|
||||
description='solution with mixture-averaged transport')
|
||||
except:
|
||||
except ImportError:
|
||||
f.save('burner_flame.xml', 'mix', 'solution with mixture-averaged transport')
|
||||
|
||||
f.transport_model = 'Multi'
|
||||
@@ -36,7 +36,7 @@ f.show_solution()
|
||||
try:
|
||||
f.write_hdf('burner_flame.h5', group='multi',
|
||||
description='solution with multicomponent transport')
|
||||
except:
|
||||
except ImportError:
|
||||
f.save('burner_flame.xml', 'multi', 'solution with multicomponent transport')
|
||||
|
||||
f.write_csv('burner_flame.csv', quiet=False)
|
||||
|
||||
@@ -53,7 +53,7 @@ f.show_solution()
|
||||
try:
|
||||
# save to HDF container file if h5py is installed
|
||||
f.write_hdf('diffusion_flame.h5', mode='w')
|
||||
except:
|
||||
except ImportError:
|
||||
f.save('diffusion_flame.xml')
|
||||
|
||||
# write the velocity, temperature, and mole fractions to a CSV file
|
||||
|
||||
@@ -16,26 +16,26 @@ awkward pressure and strain rate, or to create the basis for a flamelet table.
|
||||
Requires: cantera >= 2.5.0, matplotlib >= 2.0
|
||||
"""
|
||||
|
||||
import numpy as np
|
||||
import os
|
||||
from importlib.util import find_spec
|
||||
import importlib
|
||||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
import cantera as ct
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
|
||||
class FlameExtinguished(Exception):
|
||||
pass
|
||||
|
||||
|
||||
hdf_output = find_spec('h5py') is not None
|
||||
hdf_output = importlib.util.find_spec('h5py') is not None
|
||||
|
||||
if not hdf_output:
|
||||
# Create directory for output data files
|
||||
data_directory = 'diffusion_flame_batch_data/'
|
||||
data_directory = 'diffusion_flame_batch_data'
|
||||
if not os.path.exists(data_directory):
|
||||
os.makedirs(data_directory)
|
||||
fig_name = '{}figure_{{0}}.png'.format(data_directory)
|
||||
fig_name = os.path.join(data_directory, 'figure_{0}.png')
|
||||
else:
|
||||
fig_name = 'diffusion_flame_batch_{0}.png'
|
||||
|
||||
@@ -84,12 +84,12 @@ f.solve(loglevel=0, auto=True)
|
||||
if hdf_output:
|
||||
# save to HDF container file if h5py is installed
|
||||
file_name = 'diffusion_flame_batch.h5'
|
||||
f.write_hdf(file_name, group='initial_solution', mode='w',
|
||||
f.write_hdf(file_name, group='initial_solution', mode='w', quiet=False,
|
||||
description=('Initial hydrogen-oxygen counterflow flame '
|
||||
'at 1 bar and low strain rate'))
|
||||
else:
|
||||
file_name = 'initial_solution.xml'
|
||||
f.save(data_directory + file_name, name='solution',
|
||||
f.save(os.path.join(data_directory, file_name), name='solution',
|
||||
description='Cantera version ' + ct.__version__ +
|
||||
', reaction mechanism ' + reaction_mechanism)
|
||||
|
||||
@@ -138,12 +138,12 @@ for p in p_range:
|
||||
# Try solving the flame
|
||||
f.solve(loglevel=0)
|
||||
if hdf_output:
|
||||
group = 'pressure_loop_{:05.1f}'.format(p)
|
||||
f.write_hdf(file_name, group=group,
|
||||
group = 'pressure_loop/{:05.1f}'.format(p)
|
||||
f.write_hdf(file_name, group=group, quiet=False,
|
||||
description='pressure = {0} bar'.format(p))
|
||||
else:
|
||||
file_name = 'pressure_loop_' + format(p, '05.1f') + '.xml'
|
||||
f.save(data_directory + file_name, name='solution', loglevel=1,
|
||||
f.save(os.path.join(data_directory, file_name), name='solution', loglevel=1,
|
||||
description='Cantera version ' + ct.__version__ +
|
||||
', reaction mechanism ' + reaction_mechanism)
|
||||
p_previous = p
|
||||
@@ -153,7 +153,7 @@ for p in p_range:
|
||||
if hdf_output:
|
||||
f.read_hdf(file_name, group=group)
|
||||
else:
|
||||
f.restore(filename=data_directory + file_name, name='solution',
|
||||
f.restore(filename=os.path.join(data_directory, file_name), name='solution',
|
||||
loglevel=0)
|
||||
|
||||
|
||||
@@ -177,7 +177,7 @@ if hdf_output:
|
||||
f.read_hdf(file_name, group='initial_solution')
|
||||
else:
|
||||
file_name = 'initial_solution.xml'
|
||||
f.restore(filename=data_directory + file_name, name='solution', loglevel=0)
|
||||
f.restore(filename=os.path.join(data_directory, file_name), name='solution', loglevel=0)
|
||||
|
||||
# Counter to identify the loop
|
||||
n = 0
|
||||
@@ -203,12 +203,12 @@ while np.max(f.T) > temperature_limit_extinction:
|
||||
# Try solving the flame
|
||||
f.solve(loglevel=0)
|
||||
if hdf_output:
|
||||
group = 'strain_loop_{:02d}'.format(n)
|
||||
f.write_hdf(file_name, group=group,
|
||||
group = 'strain_loop/{:02d}'.format(n)
|
||||
f.write_hdf(file_name, group=group, quiet=False,
|
||||
description='strain rate iteration {}'.format(n))
|
||||
else:
|
||||
file_name = 'strain_loop_' + format(n, '02d') + '.xml'
|
||||
f.save(data_directory + file_name, name='solution', loglevel=1,
|
||||
f.save(os.path.join(data_directory, file_name), name='solution', loglevel=1,
|
||||
description='Cantera version ' + ct.__version__ +
|
||||
', reaction mechanism ' + reaction_mechanism)
|
||||
except FlameExtinguished:
|
||||
@@ -229,11 +229,11 @@ p_selected = p_range[::7]
|
||||
|
||||
for p in p_selected:
|
||||
if hdf_output:
|
||||
group = 'pressure_loop_{0:05.1f}'.format(p)
|
||||
group = 'pressure_loop/{0:05.1f}'.format(p)
|
||||
f.read_hdf(file_name, group=group)
|
||||
else:
|
||||
file_name = 'pressure_loop_{0:05.1f}.xml'.format(p)
|
||||
f.restore(filename=data_directory + file_name, name='solution', loglevel=0)
|
||||
f.restore(filename=os.path.join(data_directory, file_name), name='solution', loglevel=0)
|
||||
|
||||
# Plot the temperature profiles for selected pressures
|
||||
ax1.plot(f.grid / f.grid[-1], f.T, label='{0:05.1f} bar'.format(p))
|
||||
@@ -260,11 +260,12 @@ ax4 = fig4.add_subplot(1, 1, 1)
|
||||
n_selected = range(1, n, 5)
|
||||
for n in n_selected:
|
||||
if hdf_output:
|
||||
group = 'strain_loop_{0:02d}'.format(n)
|
||||
group = 'strain_loop/{0:02d}'.format(n)
|
||||
f.read_hdf(file_name, group=group)
|
||||
else:
|
||||
file_name = 'strain_loop_{0:02d}.xml'.format(n)
|
||||
f.restore(filename=data_directory + file_name, name='solution', loglevel=0)
|
||||
f.restore(filename=os.path.join(data_directory, file_name),
|
||||
name='solution', loglevel=0)
|
||||
a_max = f.strain_rate('max') # the maximum axial strain rate
|
||||
|
||||
# Plot the temperature profiles for the strain rate loop (selected)
|
||||
|
||||
@@ -12,18 +12,19 @@ explanation. Also, please don't forget to cite it if you make use of it.
|
||||
Requires: cantera >= 2.5.0, matplotlib >= 2.0
|
||||
"""
|
||||
|
||||
import numpy as np
|
||||
import os
|
||||
from importlib.util import find_spec
|
||||
|
||||
import cantera as ct
|
||||
import importlib
|
||||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
hdf_output = find_spec('h5py') is not None
|
||||
import cantera as ct
|
||||
|
||||
|
||||
hdf_output = importlib.util.find_spec('h5py') is not None
|
||||
|
||||
if not hdf_output:
|
||||
# Create directory for output data files
|
||||
data_directory = 'diffusion_flame_extinction_data/'
|
||||
data_directory = 'diffusion_flame_extinction_data'
|
||||
if not os.path.exists(data_directory):
|
||||
os.makedirs(data_directory)
|
||||
|
||||
@@ -60,12 +61,12 @@ f.solve(loglevel=0, auto=True)
|
||||
|
||||
if hdf_output:
|
||||
file_name = 'diffusion_flame_extinction.h5'
|
||||
f.write_hdf(file_name, group='initial_solution', mode='w',
|
||||
f.write_hdf(file_name, group='initial_solution', mode='w', quiet=False,
|
||||
description=('Initial solution'))
|
||||
else:
|
||||
# Save to data directory
|
||||
file_name = 'initial_solution.xml'
|
||||
f.save(data_directory + file_name, name='solution',
|
||||
f.save(os.path.join(data_directory, file_name), name='solution',
|
||||
description='Cantera version ' + ct.__version__ +
|
||||
', reaction mechanism ' + reaction_mechanism)
|
||||
|
||||
@@ -133,12 +134,13 @@ while True:
|
||||
# Flame is still burning, so proceed to next strain rate
|
||||
n_last_burning = n
|
||||
if hdf_output:
|
||||
group = 'extinction_{0:04d}'.format(n)
|
||||
f.write_hdf(file_name, group=group,
|
||||
group = 'extinction/{0:04d}'.format(n)
|
||||
f.write_hdf(file_name, group=group, quiet=False,
|
||||
description='extinction iteration'.format(n))
|
||||
else:
|
||||
file_name = 'extinction_{0:04d}.xml'.format(n)
|
||||
f.save(data_directory + file_name, name='solution', loglevel=0,
|
||||
f.save(os.path.join(data_directory, file_name),
|
||||
name='solution', loglevel=0,
|
||||
description='Cantera version ' + ct.__version__ +
|
||||
', reaction mechanism ' + reaction_mechanism)
|
||||
T_max.append(np.max(f.T))
|
||||
@@ -158,11 +160,12 @@ while True:
|
||||
delta_alpha = delta_alpha / delta_alpha_factor
|
||||
# Restore last burning solution
|
||||
if hdf_output:
|
||||
group = 'extinction_{0:04d}'.format(n_last_burning)
|
||||
group = 'extinction/{0:04d}'.format(n_last_burning)
|
||||
f.read_hdf(file_name, group=group)
|
||||
else:
|
||||
file_name = 'extinction_{0:04d}.xml'.format(n_last_burning)
|
||||
f.restore(data_directory + file_name, name='solution', loglevel=0)
|
||||
f.restore(os.path.join(data_directory, file_name),
|
||||
name='solution', loglevel=0)
|
||||
|
||||
|
||||
# Print some parameters at the extinction point
|
||||
@@ -187,4 +190,4 @@ plt.ylabel(r'$T_{max}$ [K]')
|
||||
if hdf_output:
|
||||
plt.savefig('diffusion_flame_extinction_T_max_a_max.png')
|
||||
else:
|
||||
plt.savefig(data_directory + 'figure_T_max_a_max.png')
|
||||
plt.savefig(os.path.join(data_directory, 'figure_T_max_a_max.png'))
|
||||
|
||||
@@ -59,7 +59,7 @@ try:
|
||||
# save to HDF container file if h5py is installed
|
||||
f.write_hdf('flame_fixed_T.h5', group='mix', mode='w',
|
||||
description='solution with mixture-averaged transport')
|
||||
except:
|
||||
except ImportError:
|
||||
f.save('flame_fixed_T.xml','mixav',
|
||||
'solution with mixture-averaged transport')
|
||||
|
||||
@@ -71,7 +71,7 @@ f.solve(loglevel, refine_grid)
|
||||
try:
|
||||
f.write_hdf('flame_fixed_T.h5', group='multi',
|
||||
description='solution with multicomponent transport')
|
||||
except:
|
||||
except ImportError:
|
||||
f.save('flame_fixed_T.xml','multi',
|
||||
'solution with multicomponent transport')
|
||||
|
||||
|
||||
@@ -28,7 +28,7 @@ try:
|
||||
# save to HDF container file if h5py is installed
|
||||
f.write_hdf('ion_burner_flame.h5', group='ion', mode='w',
|
||||
description='solution with ionized gas transport')
|
||||
except:
|
||||
except ImportError:
|
||||
f.save('ion_burner_flame.xml', 'mix', 'solution with mixture-averaged transport')
|
||||
|
||||
f.write_csv('ion_burner_flame.csv', quiet=False)
|
||||
|
||||
@@ -33,7 +33,7 @@ try:
|
||||
# save to HDF container file if h5py is installed
|
||||
f.write_hdf('ion_free_flame.h5', group='ion', mode='w',
|
||||
description='solution with ionized gas transport')
|
||||
except:
|
||||
except ImportError:
|
||||
f.save('ion_free_flame.xml', 'ion', 'solution with ionized gas transport')
|
||||
|
||||
f.show_solution()
|
||||
|
||||
@@ -17,12 +17,13 @@ been previously. (To see this, try setting prune to zero.)
|
||||
Requires: cantera >= 2.5.0
|
||||
"""
|
||||
|
||||
import cantera as ct
|
||||
import os
|
||||
from importlib.util import find_spec
|
||||
import importlib
|
||||
|
||||
import cantera as ct
|
||||
|
||||
|
||||
hdf_output = find_spec('h5py') is not None
|
||||
hdf_output = importlib.util.find_spec('h5py') is not None
|
||||
|
||||
# parameter values
|
||||
p = 0.05 * ct.one_atm # pressure
|
||||
|
||||
@@ -116,7 +116,7 @@ sim.show_solution()
|
||||
try:
|
||||
sim.write_hdf('catalytic_combustion.h5', group='soln1', mode='w',
|
||||
description='catalytic combustion example')
|
||||
except:
|
||||
except ImportError:
|
||||
sim.save("catalytic_combustion.xml", "soln1")
|
||||
|
||||
# save selected solution components in a CSV file for plotting in
|
||||
|
||||
Reference in New Issue
Block a user