Skip to content

Commit

Permalink
[1D] Update Python examples to use YAML output instead of XML
Browse files Browse the repository at this point in the history
  • Loading branch information
speth committed Oct 17, 2021
1 parent 2150565 commit 016248c
Show file tree
Hide file tree
Showing 10 changed files with 29 additions and 33 deletions.
4 changes: 2 additions & 2 deletions interfaces/cython/cantera/examples/onedim/adiabatic_flame.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
f.write_hdf('adiabatic_flame.h5', group='mix', mode='w',
description='solution with mixture-averaged transport')
except ImportError:
f.save('adiabatic_flame.xml', 'mix',
f.save('adiabatic_flame.yaml', 'mix',
'solution with mixture-averaged transport')

f.show_solution()
Expand All @@ -49,7 +49,7 @@
f.write_hdf('adiabatic_flame.h5', group='multi',
description='solution with multicomponent transport')
except ImportError:
f.save('adiabatic_flame.xml', 'multi',
f.save('adiabatic_flame.yaml', 'multi',
'solution with multicomponent transport')

# write the velocity, temperature, density, and mole fractions to a CSV file
Expand Down
4 changes: 2 additions & 2 deletions interfaces/cython/cantera/examples/onedim/burner_flame.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
f.write_hdf('burner_flame.h5', group='mix', mode='w',
description='solution with mixture-averaged transport')
except ImportError:
f.save('burner_flame.xml', 'mix', 'solution with mixture-averaged transport')
f.save('burner_flame.yaml', 'mix', 'solution with mixture-averaged transport')

f.transport_model = 'Multi'
f.solve(loglevel) # don't use 'auto' on subsequent solves
Expand All @@ -37,6 +37,6 @@
f.write_hdf('burner_flame.h5', group='multi',
description='solution with multicomponent transport')
except ImportError:
f.save('burner_flame.xml', 'multi', 'solution with multicomponent transport')
f.save('burner_flame.yaml', 'multi', 'solution with multicomponent transport')

f.write_csv('burner_flame.csv', quiet=False)
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
# save to HDF container file if h5py is installed
f.write_hdf('diffusion_flame.h5', mode='w')
except ImportError:
f.save('diffusion_flame.xml')
f.save('diffusion_flame.yaml')

# write the velocity, temperature, and mole fractions to a CSV file
f.write_csv('diffusion_flame.csv', quiet=False)
Expand Down
22 changes: 10 additions & 12 deletions interfaces/cython/cantera/examples/onedim/diffusion_flame_batch.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,10 @@ def interrupt_extinction(t):
description=('Initial hydrogen-oxygen counterflow flame '
'at 1 bar and low strain rate'))
else:
file_name = 'initial_solution.xml'
file_name = 'initial_solution.yaml'
f.save(os.path.join(data_directory, file_name), name='solution',
description='Cantera version ' + ct.__version__ +
', reaction mechanism ' + reaction_mechanism)
description='Initial hydrogen-oxygen counterflow flame '
'at 1 bar and low strain rate')


# PART 2: BATCH PRESSURE LOOP
Expand Down Expand Up @@ -142,10 +142,9 @@ def interrupt_extinction(t):
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'
file_name = 'pressure_loop_' + format(p, '05.1f') + '.yaml'
f.save(os.path.join(data_directory, file_name), name='solution', loglevel=1,
description='Cantera version ' + ct.__version__ +
', reaction mechanism ' + reaction_mechanism)
description='pressure = {0} bar'.format(p))
p_previous = p
except ct.CanteraError as e:
print('Error occurred while solving:', e, 'Try next pressure level')
Expand Down Expand Up @@ -176,7 +175,7 @@ def interrupt_extinction(t):
if hdf_output:
f.read_hdf(file_name, group='initial_solution')
else:
file_name = 'initial_solution.xml'
file_name = 'initial_solution.yaml'
f.restore(filename=os.path.join(data_directory, file_name), name='solution', loglevel=0)

# Counter to identify the loop
Expand Down Expand Up @@ -207,10 +206,9 @@ def interrupt_extinction(t):
f.write_hdf(file_name, group=group, quiet=False,
description='strain rate iteration {}'.format(n))
else:
file_name = 'strain_loop_' + format(n, '02d') + '.xml'
file_name = 'strain_loop_' + format(n, '02d') + '.yaml'
f.save(os.path.join(data_directory, file_name), name='solution', loglevel=1,
description='Cantera version ' + ct.__version__ +
', reaction mechanism ' + reaction_mechanism)
description='strain rate iteration {}'.format(n))
except FlameExtinguished:
print('Flame extinguished')
break
Expand All @@ -232,7 +230,7 @@ def interrupt_extinction(t):
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)
file_name = 'pressure_loop_{0:05.1f}.yaml'.format(p)
f.restore(filename=os.path.join(data_directory, file_name), name='solution', loglevel=0)

# Plot the temperature profiles for selected pressures
Expand Down Expand Up @@ -263,7 +261,7 @@ def interrupt_extinction(t):
group = 'strain_loop/{0:02d}'.format(n)
f.read_hdf(file_name, group=group)
else:
file_name = 'strain_loop_{0:02d}.xml'.format(n)
file_name = 'strain_loop_{0:02d}.yaml'.format(n)
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,9 @@
description=('Initial solution'))
else:
# Save to data directory
file_name = 'initial_solution.xml'
file_name = 'initial_solution.yaml'
f.save(os.path.join(data_directory, file_name), name='solution',
description='Cantera version ' + ct.__version__ +
', reaction mechanism ' + reaction_mechanism)
description="Initial solution")


# PART 2: COMPUTE EXTINCTION STRAIN
Expand Down Expand Up @@ -138,11 +137,10 @@
group = 'extinction/{0:04d}'.format(n)
f.write_hdf(file_name, group=group, quiet=True)
else:
file_name = 'extinction_{0:04d}.xml'.format(n)
file_name = 'extinction_{0:04d}.yaml'.format(n)
f.save(os.path.join(data_directory, file_name),
name='solution', loglevel=0,
description='Cantera version ' + ct.__version__ +
', reaction mechanism ' + reaction_mechanism)
description=f"Solution at alpha = {alpha[-1]}")
T_max.append(np.max(f.T))
a_max.append(np.max(np.abs(np.gradient(f.velocity) / np.gradient(f.grid))))
print('Flame burning at alpha = {:8.4F}. Proceeding to the next iteration, '
Expand All @@ -157,7 +155,7 @@
group = 'extinction/{0:04d}'.format(n)
f.write_hdf(file_name, group=group, quiet=True)
else:
file_name = 'extinction_{0:04d}.xml'.format(n)
file_name = 'extinction_{0:04d}.yaml'.format(n)
f.save(os.path.join(data_directory, file_name), name='solution', loglevel=0)
print('Flame extinguished at alpha = {0:8.4F}.'.format(alpha[-1]),
'Abortion criterion satisfied.')
Expand All @@ -176,7 +174,7 @@
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)
file_name = 'extinction_{0:04d}.yaml'.format(n_last_burning)
f.restore(os.path.join(data_directory, file_name),
name='solution', loglevel=0)

Expand All @@ -187,7 +185,7 @@
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)
file_name = 'extinction_{0:04d}.yaml'.format(n_last_burning)
f.restore(os.path.join(data_directory, file_name),
name='solution', loglevel=0)
print('----------------------------------------------------------------------')
Expand Down
4 changes: 2 additions & 2 deletions interfaces/cython/cantera/examples/onedim/flame_fixed_T.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
f.write_hdf('flame_fixed_T.h5', group='mix', mode='w',
description='solution with mixture-averaged transport')
except ImportError:
f.save('flame_fixed_T.xml','mixav',
f.save('flame_fixed_T.yaml','mixav',
'solution with mixture-averaged transport')

print('\n\n switching to multicomponent transport...\n\n')
Expand All @@ -75,7 +75,7 @@
f.write_hdf('flame_fixed_T.h5', group='multi',
description='solution with multicomponent transport')
except ImportError:
f.save('flame_fixed_T.xml','multi',
f.save('flame_fixed_T.yaml','multi',
'solution with multicomponent transport')

# write the velocity, temperature, density, and mole fractions to a CSV file
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@
f.write_hdf('ion_burner_flame.h5', group='ion', mode='w',
description='solution with ionized gas transport')
except ImportError:
f.save('ion_burner_flame.xml', 'mix', 'solution with mixture-averaged transport')
f.save('ion_burner_flame.yaml', 'mix', 'solution with mixture-averaged transport')

f.write_csv('ion_burner_flame.csv', quiet=False)
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
f.write_hdf('ion_free_flame.h5', group='ion', mode='w',
description='solution with ionized gas transport')
except ImportError:
f.save('ion_free_flame.xml', 'ion', 'solution with ionized gas transport')
f.save('ion_free_flame.yaml', 'ion', 'solution with ionized gas transport')

f.show_solution()
print('mixture-averaged flamespeed = {0:7f} m/s'.format(f.velocity[0]))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
if hdf_output:
outfile = 'stagnation_flame.h5'
else:
outfile = 'stagnation_flame.xml'
outfile = 'stagnation_flame.yaml'
if os.path.exists(outfile):
os.remove(outfile)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,13 @@
# show the solution
sim.show_solution()

# save the solution in XML format. The 'restore' method can be used to restart
# save the full solution. The 'restore' method can be used to restart
# a simulation from a solution stored in this form.
try:
sim.write_hdf('catalytic_combustion.h5', group='soln1', mode='w',
description='catalytic combustion example')
except ImportError:
sim.save("catalytic_combustion.xml", "soln1")
sim.save("catalytic_combustion.yaml", "soln1")

# save selected solution components in a CSV file for plotting in
# Excel or MATLAB.
Expand Down

0 comments on commit 016248c

Please sign in to comment.