Saving, loading, and restarting 1D calculations#

A freely-propagating, premixed methane-air flame. Examples of saving and loading a flame and restarting with different initial guesses.

Requires: cantera >= 3.0

Tags: Python combustion 1D flow flame speed premixed flame saving output

************ Solving on 8 point grid with energy equation enabled ************

..............................................................................
Attempt Newton solution of steady-state problem...    failure.
Take 10 timesteps     3.204e-05      4.719
Attempt Newton solution of steady-state problem...    failure.
Take 10 timesteps     0.0008211      3.688
Attempt Newton solution of steady-state problem...    failure.
Take 10 timesteps     0.0004384      5.248
Attempt Newton solution of steady-state problem...    failure.
Take 10 timesteps     9.753e-06      6.445
Attempt Newton solution of steady-state problem...    failure.
Take 10 timesteps       0.00025      4.322
Attempt Newton solution of steady-state problem...    failure.
Take 10 timesteps      0.004271      3.634
Attempt Newton solution of steady-state problem...    success.

Problem solved on [9] point grid(s).
Expanding domain to accommodate flame thickness. New width: 0.06 m
##############################################################################
Refining grid in flame.
    New points inserted after grid points 1 2 3 4 5 6
    to resolve C C2H2 C2H3 C2H4 C2H5 C2H6 C3H8 CH CH2 CH2(S) CH2CHO CH2CO CH2O CH2OH CH3 CH3CHO CH3O CH3OH CH4 CO CO2 H H2 H2O H2O2 HCCO HCN HCNO HCO HNCO HO2 N2 N2O NCO NO NO2 O O2 OH T velocity
##############################################################################

*********** Solving on 15 point grid with energy equation enabled ************

..............................................................................
Attempt Newton solution of steady-state problem...    failure.
Take 10 timesteps     2.136e-05      5.714
Attempt Newton solution of steady-state problem...    failure.
Take 10 timesteps     6.082e-05      5.615
Attempt Newton solution of steady-state problem...    failure.
Take 10 timesteps      0.001559      4.299
Attempt Newton solution of steady-state problem...    failure.
Take 10 timesteps     2.601e-05      6.442
Attempt Newton solution of steady-state problem...    failure.
Take 10 timesteps     0.0001481      5.394
Attempt Newton solution of steady-state problem...    failure.
Take 10 timesteps     0.0002109        5.3
Attempt Newton solution of steady-state problem...    failure.
Take 10 timesteps     8.341e-06      6.336
Attempt Newton solution of steady-state problem...    failure.
Take 10 timesteps     0.0002138      4.653
Attempt Newton solution of steady-state problem...    failure.
Take 10 timesteps      0.003653      3.449
Attempt Newton solution of steady-state problem...    success.

Problem solved on [15] point grid(s).

..............................................................................
grid refinement disabled.

******************** Solving with grid refinement enabled ********************

..............................................................................
Attempt Newton solution of steady-state problem...    success.

Problem solved on [15] point grid(s).

..............................................................................
##############################################################################
Refining grid in flame.
    New points inserted after grid points 0 3 4 5 6 7 8
    to resolve C C2H2 C2H3 C2H4 C2H5 C2H6 C3H8 CH CH2 CH2(S) CH2CHO CH2CO CH2O CH2OH CH3 CH3CHO CH3O CH3OH CH4 CO CO2 H H2 H2O H2O2 HCCO HCN HCNO HCO HNCO HO2 N2 N2O NCO NO NO2 O O2 OH T point 0 velocity
##############################################################################

..............................................................................
Attempt Newton solution of steady-state problem...    failure.
Take 10 timesteps     0.0001709      4.732
Attempt Newton solution of steady-state problem...    failure.
Take 10 timesteps     1.711e-05      6.447
Attempt Newton solution of steady-state problem...    failure.
Take 10 timesteps     0.0001461      4.951
Attempt Newton solution of steady-state problem...    success.

Problem solved on [22] point grid(s).

..............................................................................
##############################################################################
Refining grid in flame.
    New points inserted after grid points 3 5 6 7 8 9 10 11 12
    to resolve C C2H2 C2H3 C2H4 C2H5 C2H6 C3H7 C3H8 CH CH2 CH2(S) CH2CHO CH2CO CH2O CH2OH CH3 CH3CHO CH3O CH3OH CH4 CO CO2 H H2 H2O H2O2 HCCO HCN HCNO HCO HNCO HO2 N N2 N2O NCO NO NO2 O O2 OH T point 3 velocity
##############################################################################

..............................................................................
Attempt Newton solution of steady-state problem...    failure.
Take 10 timesteps     0.0001709      4.886
Attempt Newton solution of steady-state problem...    success.

Problem solved on [31] point grid(s).

..............................................................................
##############################################################################
Refining grid in flame.
    New points inserted after grid points 9 10 11 12 13 14 15 16 17 27 28
    to resolve C C2H C2H2 C2H3 C2H4 C2H5 C2H6 C3H7 C3H8 CH CH2 CH2(S) CH2CHO CH2CO CH2O CH2OH CH3 CH3CHO CH3O CH3OH CH4 CO CO2 H H2 H2O H2O2 HCCO HCCOH HCN HCNO HCO HNCO HO2 N N2 N2O NCO NH3 NO NO2 O O2 OH T velocity
##############################################################################

..............................................................................
Attempt Newton solution of steady-state problem...    failure.
Take 10 timesteps     0.0001139      5.278
Attempt Newton solution of steady-state problem...    success.

Problem solved on [42] point grid(s).

..............................................................................
##############################################################################
Refining grid in flame.
    New points inserted after grid points 12 13 14 15 16 17 18 19 20 21 40
    to resolve C C2H C2H2 C2H3 C2H4 C2H5 C2H6 C3H7 C3H8 CH CH2 CH2(S) CH2CHO CH2CO CH2O CH2OH CH3 CH3CHO CH3O CH3OH CH4 CO CO2 H H2 H2O H2O2 HCCO HCCOH HCN HCNO HCO HNCO HO2 N N2 N2O NCO NO NO2 O O2 OH T point 40 velocity
##############################################################################

..............................................................................
Attempt Newton solution of steady-state problem...    success.

Problem solved on [53] point grid(s).

..............................................................................
##############################################################################
Refining grid in flame.
    New points inserted after grid points 14 15 16 17 18 19 20 21 22 23 24 50
    to resolve C C2H C2H2 C2H3 C2H4 C2H5 C2H6 C3H7 C3H8 CH CH2 CH2(S) CH2CHO CH2CO CH2O CH2OH CH3 CH3CHO CH3O CH3OH CH4 CO CO2 H H2 H2O H2O2 HCCO HCCOH HCN HCNO HCO HNCO HO2 N N2 N2O NCO NO NO2 O O2 OH T velocity
##############################################################################

..............................................................................
Attempt Newton solution of steady-state problem...    success.

Problem solved on [65] point grid(s).

..............................................................................
##############################################################################
Refining grid in flame.
    New points inserted after grid points 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
    to resolve C C2H C2H2 C2H3 C2H4 C2H5 C2H6 C3H7 C3H8 CH CH2 CH2(S) CH2CHO CH2CO CH2O CH2OH CH3 CH3CHO CH3O CH3OH CH4 CO H H2 H2O H2O2 HCCO HCCOH HCN HCO HNCO HO2 N2 NO2 O O2 OH T velocity
##############################################################################

..............................................................................
Attempt Newton solution of steady-state problem...    success.

Problem solved on [82] point grid(s).

..............................................................................
##############################################################################
Refining grid in flame.
    New points inserted after grid points 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
    to resolve C C2H2 C2H3 C2H4 C2H5 C2H6 C3H7 C3H8 CH CH2 CH2(S) CH2CHO CH2CO CH2O CH2OH CH3 CH3CHO CH3O CH3OH H H2O2 HCCO HCCOH HCN HCO HO2 NO2 O OH
##############################################################################

..............................................................................
Attempt Newton solution of steady-state problem...    success.

Problem solved on [105] point grid(s).

..............................................................................
##############################################################################
Refining grid in flame.
    New points inserted after grid points 43 44 45 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
    to resolve C2H2 C2H3 C2H4 C2H5 C2H6 C3H8 CH CH2 CH2(S) CH2OH CH3 CH3CHO HCCO HCO
##############################################################################

..............................................................................
Attempt Newton solution of steady-state problem...    success.

Problem solved on [125] point grid(s).

..............................................................................
no new points needed in flame

Flame speed                    = 34.42 cm/s
Maximum temperature            = 2139 K
Peak OH mole fraction location = 2.17 cm
Solved with 125 grid points

Save YAML
Save CSV
Save HDF

Restore solution from YAML

Flame speed                    = 34.42 cm/s
Maximum temperature            = 2139 K
Peak OH mole fraction location = 2.17 cm
Solved with 125 grid points

Restore solution from HDF

Flame speed                    = 34.42 cm/s
Maximum temperature            = 2139 K
Peak OH mole fraction location = 2.17 cm
Solved with 125 grid points

Load initial guess from CSV file directly

Flame speed                    = 34.42 cm/s
Maximum temperature            = 2139 K
Peak OH mole fraction location = 2.17 cm
Solved with 125 grid points

Load initial guess from HDF file directly

Flame speed                    = 34.42 cm/s
Maximum temperature            = 2139 K
Peak OH mole fraction location = 2.17 cm
Solved with 125 grid points

Load initial guess from HDF file via SolutionArray

Flame speed                    = 34.42 cm/s
Maximum temperature            = 2139 K
Peak OH mole fraction location = 2.17 cm
Solved with 125 grid points

Load initial guess from CSV file via Pandas

Flame speed                    = 34.42 cm/s
Maximum temperature            = 2139 K
Peak OH mole fraction location = 2.17 cm
Solved with 125 grid points

Load initial guess from CSV file via Pandas and SolutionArray

Flame speed                    = 34.42 cm/s
Maximum temperature            = 2139 K
Peak OH mole fraction location = 2.17 cm
Solved with 125 grid points

Load initial guess from CSV file via Pandas, with modifications.

Modify the Pandas dataframe, removing half the grid points

..............................................................................
Attempt Newton solution of steady-state problem...    success.

Problem solved on [63] point grid(s).

..............................................................................
##############################################################################
Refining grid in flame.
    New points inserted after grid points 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 48 49 50 51 57 58 59 60
    to resolve C C2H2 C2H3 C2H4 C2H5 C2H6 C3H8 CH CH2 CH2(S) CH2CHO CH2CO CH2OH CH3 CH3CHO CH3O CH3OH H H2 H2O2 HCCO HCCOH HCN HCO HNCO HO2 NO NO2 O
##############################################################################

..............................................................................
Attempt Newton solution of steady-state problem...    success.

Problem solved on [112] point grid(s).

..............................................................................
##############################################################################
Refining grid in flame.
    New points inserted after grid points 87 110
    to resolve point 110 point 87
##############################################################################

..............................................................................
Attempt Newton solution of steady-state problem...    success.

Problem solved on [114] point grid(s).

..............................................................................
no new points needed in flame

Flame speed                    = 34.52 cm/s
Maximum temperature            = 2138 K
Peak OH mole fraction location = 2.17 cm
Solved with 114 grid points

Modify the Pandas dataframe, removing half the grid points and all but the first 20 species

..............................................................................
Attempt Newton solution of steady-state problem...    failure.
Take 10 timesteps     1.875e-06      7.819
Attempt Newton solution of steady-state problem...    failure.
Take 10 timesteps     1.424e-05      7.292
Attempt Newton solution of steady-state problem...    failure.
Take 10 timesteps     0.0001081      6.311
Attempt Newton solution of steady-state problem...    failure.
Take 10 timesteps     0.0008211      5.045
Attempt Newton solution of steady-state problem...    failure.
Take 10 timesteps      0.006235       4.24
Attempt Newton solution of steady-state problem...    failure.
Take 10 timesteps        0.1065      2.757
Attempt Newton solution of steady-state problem...    success.

Problem solved on [63] point grid(s).

..............................................................................
##############################################################################
Refining grid in flame.
    New points inserted after grid points 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 48 49 50 51 57 58 59 60
    to resolve C C2H2 C2H3 C2H4 C2H5 C2H6 C3H8 CH CH2 CH2(S) CH2CHO CH2CO CH2OH CH3 CH3CHO CH3O CH3OH H H2 H2O2 HCCO HCCOH HCN HCO HNCO HO2 NO NO2 O
##############################################################################

..............................................................................
Attempt Newton solution of steady-state problem...    success.

Problem solved on [112] point grid(s).

..............................................................................
##############################################################################
Refining grid in flame.
    New points inserted after grid points 87 110
    to resolve point 110 point 87
##############################################################################

..............................................................................
Attempt Newton solution of steady-state problem...    success.

Problem solved on [114] point grid(s).

..............................................................................
no new points needed in flame

Flame speed                    = 34.52 cm/s
Maximum temperature            = 2138 K
Peak OH mole fraction location = 2.17 cm
Solved with 114 grid points

Modify the Pandas dataframe, removing half the grid points, and raise the T by 50 K

..............................................................................
Attempt Newton solution of steady-state problem...    success.

Problem solved on [63] point grid(s).

..............................................................................
##############################################################################
Refining grid in flame.
    New points inserted after grid points 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 42 44 48 49 51 57 58 59 60
    to resolve C C2H2 C2H3 C2H4 C2H5 C2H6 C3H8 CH CH2 CH2(S) CH2CHO CH2CO CH2O CH2OH CH3 CH3CHO CH3O CH3OH H H2 H2O2 HCCO HCN HCO HO2 NO NO2 O
##############################################################################

..............................................................................
Attempt Newton solution of steady-state problem...    success.

Problem solved on [107] point grid(s).

..............................................................................
##############################################################################
Refining grid in flame.
    New points inserted after grid points 16 36 105
    to resolve C2H5 CH3O point 105
##############################################################################

..............................................................................
Attempt Newton solution of steady-state problem...    success.

Problem solved on [110] point grid(s).

..............................................................................
no new points needed in flame

Flame speed                    = 44.72 cm/s
Maximum temperature            = 2168 K
Peak OH mole fraction location = 2.17 cm
Solved with 110 grid points

All done

import sys
from pathlib import Path
import cantera as ct
try:
    import pandas as pd
except ImportError:
    pd = None


# Simulation parameters
p = ct.one_atm  # pressure [Pa]
Tin = 300.0  # unburned gas temperature [K]
reactants = "CH4:0.45, O2:1.0, N2:3.76"

width = 0.03  # m

# Solution object used to compute mixture properties
gas = ct.Solution("gri30.yaml")
gas.TPX = Tin, p, reactants

# Flame object
f = ct.FreeFlame(gas, width=width)
# These refine criteria are for a relatively quick convergence for the example
# and may not be suitable for a publication-quality flame speed calculation
refine_criteria = {"ratio": 3, "slope": 0.1, "curve": 0.2}
f.set_refine_criteria(**refine_criteria)
f.solve(loglevel=1, auto=True)

def describe(flame):
    """Print a short description of the flame, with a few properties."""
    print(f"\nFlame speed                    = {flame.velocity[0] * 100 :.2f} cm/s")
    print(f"Maximum temperature            = {flame.T.max() :.0f} K")
    # Find the location of the peak OH mole fraction
    oh_index = flame.gas.species_index("OH")
    grid_index = flame.X[oh_index].argmax()
    oh_peak = flame.grid[grid_index]
    print(f"Peak OH mole fraction location = {oh_peak * 100 :.2f} cm")
    print(f"Solved with {flame.grid.size} grid points\n")


describe(f)

# Save the flame in a few different formats

output_path = Path() / "flame_initial_guess_data"
output_path.mkdir(parents=True, exist_ok=True)

print("Save YAML")
yaml_filepath = output_path / "flame.yaml"
f.save(yaml_filepath, name="solution", description="Initial methane flame")

print("Save CSV")
csv_filepath = output_path / "flame.csv"
f.save(csv_filepath, basis="mole", overwrite=True)

if "native" in ct.hdf_support():
    # HDF is not a required dependency
    hdf_filepath = output_path / "flame.h5"
    hdf_filepath.unlink(missing_ok=True)
    f.save(hdf_filepath, name="freeflame", description="Initial methane flame")
    print("Save HDF\n")
else:
    print(f"Skipping HDF: Cantera compiled without HDF support\n")
    hdf_filepath = None

# Restore the flame from different formats

print("Restore solution from YAML")
gas.TPX = Tin, p, reactants
f2 = ct.FreeFlame(gas, width=width)
f2.restore(yaml_filepath, name="solution")
describe(f2)

if hdf_filepath:
    print("Restore solution from HDF")
    gas.TPX = Tin, p, reactants
    f2 = ct.FreeFlame(gas, width=width)
    f2.restore(hdf_filepath, name="freeflame")
    describe(f2)

# Restore the flame via initial guess

print("Load initial guess from CSV file directly")
gas.TPX = Tin, p, reactants  # set the gas T back to the inlet before making new flame
f2 = ct.FreeFlame(gas, width=width)
f2.set_initial_guess(data=csv_filepath)
describe(f2)

if hdf_filepath:
    print("Load initial guess from HDF file directly")
    gas.TPX = Tin, p, reactants  # set the gas T back to the inlet before making new flame
    f2 = ct.FreeFlame(gas, width=width)
    f2.set_initial_guess(data=hdf_filepath, group="freeflame")
    describe(f2)

    print("Load initial guess from HDF file via SolutionArray")
    arr2 = ct.SolutionArray(gas)
    # the flame domain needs to be specified as subgroup
    arr2.restore(hdf_filepath, name="freeflame", sub="flame")
    gas.TPX = Tin, p, reactants  # set the gas T back to the inlet before making new flame
    f2 = ct.FreeFlame(gas, width=width)
    f2.set_initial_guess(data=arr2)
    describe(f2)

if pd is None:
    # skip remaining examples, as optional dependency 'pandas' is not installed
    print("All done")
    sys.exit()

print("Load initial guess from CSV file via Pandas")
df = pd.read_csv(csv_filepath)
gas.TPX = Tin, p, reactants  # set the gas T back to the inlet before making new flame
f2 = ct.FreeFlame(gas, width=width)
f2.set_initial_guess(data=df)
describe(f2)

print("Load initial guess from CSV file via Pandas and SolutionArray")
df = pd.read_csv(csv_filepath)
arr2 = ct.SolutionArray(gas)
arr2.from_pandas(df)
gas.TPX = Tin, p, reactants  # set the gas T back to the inlet before making new flame
f2 = ct.FreeFlame(gas, width=width)
f2.set_initial_guess(data=arr2)
describe(f2)

# Restart flame simulations with modified initial guesses

print("Load initial guess from CSV file via Pandas, with modifications.\n")
df = pd.read_csv(csv_filepath)
print("Modify the Pandas dataframe, removing half the grid points")
df_pruned = df[::2]  # remove half of the grid points
gas.TPX = Tin, p, reactants  # set the gas T back to the inlet before making new flame
f2 = ct.FreeFlame(gas, width=width)
f2.set_refine_criteria(**refine_criteria)
f2.set_initial_guess(data=df_pruned)
f2.solve()
# We wouldn't expect the flame solutions to be exactly the same
describe(f2)

print(
    "Modify the Pandas dataframe, removing half the grid points "
    "and all but the first 20 species"
)
df_pruned = df.iloc[::2, :24]
gas.TPX = Tin, p, reactants  # set the gas T back to the inlet before making new flame
f2 = ct.FreeFlame(gas, width=width)
f2.set_refine_criteria(**refine_criteria)
f2.set_initial_guess(data=df_pruned)
f2.solve()
# We wouldn't expect the flame solutions to be exactly the same
describe(f2)

print(
    "Modify the Pandas dataframe, removing half the grid points, "
    "and raise the T by 50 K"
)
df_pruned = df.iloc[::2]
# set the gas T back to the (new) inlet before making new flame
gas.TPX = (Tin + 50, p, reactants)
f2 = ct.FreeFlame(gas, width=width)
f2.set_refine_criteria(**refine_criteria)
f2.set_initial_guess(data=df_pruned)
f2.solve()
# We expect these flames to be different because we raised the temperature.
describe(f2)

print("All done")

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

Gallery generated by Sphinx-Gallery