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

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

Initialization#

# 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)
************ Solving on 8 point grid with energy equation enabled ************

..............................................................................

Attempt Newton solution of steady-state problem.
Newton steady-state solve failed.

Attempt 10 timesteps.
Final timestep info: dt= 3.204e-05  log(ss)= 4.767

Attempt Newton solution of steady-state problem.
Newton steady-state solve failed.

Attempt 10 timesteps.
Final timestep info: dt= 0.0008211  log(ss)= 3.856

Attempt Newton solution of steady-state problem.
Newton steady-state solve failed.

Attempt 10 timesteps.
Final timestep info: dt= 0.01403    log(ss)= 3.199

Attempt Newton solution of steady-state problem.
Newton steady-state solve failed.

Attempt 10 timesteps.
Final timestep info: dt= 0.0002633  log(ss)= 4.977

Attempt Newton solution of steady-state problem.
Newton steady-state solve failed.

Attempt 10 timesteps.
Final timestep info: dt= 1.172e-05  log(ss)= 6.645

Attempt Newton solution of steady-state problem.
Newton steady-state solve failed.

Attempt 10 timesteps.
Final timestep info: dt= 6.673e-05  log(ss)= 5.494

Attempt Newton solution of steady-state problem.
Newton steady-state solve failed.

Attempt 10 timesteps.
Final timestep info: dt= 0.002565   log(ss)= 3.829

Attempt Newton solution of steady-state problem.
Newton steady-state solve failed.

Attempt 10 timesteps.
Final timestep info: dt= 0.04383    log(ss)= 1.445

Attempt Newton solution of steady-state problem.
Newton steady-state solve succeeded.

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

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

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

..............................................................................

Attempt Newton solution of steady-state problem.
Newton steady-state solve succeeded.

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

..............................................................................
##############################################################################
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
##############################################################################

..............................................................................

Attempt Newton solution of steady-state problem.
Newton steady-state solve failed.

Attempt 10 timesteps.
Final timestep info: dt= 0.0001709  log(ss)= 4.735

Attempt Newton solution of steady-state problem.
Newton steady-state solve failed.

Attempt 10 timesteps.
Final timestep info: dt= 8.553e-06  log(ss)= 6.542

Attempt Newton solution of steady-state problem.
Newton steady-state solve succeeded.

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 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 0 velocity
##############################################################################

..............................................................................

Attempt Newton solution of steady-state problem.
Newton steady-state solve failed.

Attempt 10 timesteps.
Final timestep info: dt= 0.0001709  log(ss)= 4.876

Attempt Newton solution of steady-state problem.
Newton steady-state solve succeeded.

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 13
    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 point 3 velocity
##############################################################################

..............................................................................

Attempt Newton solution of steady-state problem.
Newton steady-state solve failed.

Attempt 10 timesteps.
Final timestep info: dt= 7.594e-05  log(ss)= 5.452

Attempt Newton solution of steady-state problem.
Newton steady-state solve succeeded.

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

..............................................................................
##############################################################################
Refining grid in flame.
    New points inserted after grid points 9 10 11 12 13 14 15 16 17 18 20
    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.
Newton steady-state solve succeeded.

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

..............................................................................
##############################################################################
Refining grid in flame.
    New points inserted after grid points 11 12 13 14 15 16 17 18 19 20 21 39
    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.
Newton steady-state solve succeeded.

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

..............................................................................
##############################################################################
Refining grid in flame.
    New points inserted after grid points 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 52
    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 NO NO2 O O2 OH T velocity
##############################################################################

..............................................................................

Attempt Newton solution of steady-state problem.
Newton steady-state solve succeeded.

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

..............................................................................
##############################################################################
Refining grid in flame.
    New points inserted after grid points 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 71
    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 point 71
##############################################################################

..............................................................................

Attempt Newton solution of steady-state problem.
Newton steady-state solve succeeded.

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

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

..............................................................................

Attempt Newton solution of steady-state problem.
Newton steady-state solve succeeded.

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

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

Flame speed                    = 34.45 cm/s
Maximum temperature            = 2138 K
Peak OH mole fraction location = 1.12 cm
Solved with 117 grid points

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",
       overwrite=True)

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",
           overwrite=True)
    print("Save HDF\n")
else:
    print(f"Skipping HDF: Cantera compiled without HDF support\n")
    hdf_filepath = None
Save YAML
Save CSV
Save HDF

Restore the flame from different formats#

Restore solution from YAML

gas.TPX = Tin, p, reactants
f2 = ct.FreeFlame(gas, width=width)
f2.restore(yaml_filepath, name="solution")
describe(f2)
Flame speed                    = 34.45 cm/s
Maximum temperature            = 2138 K
Peak OH mole fraction location = 1.12 cm
Solved with 117 grid points

Restore solution from HDF

Flame speed                    = 34.45 cm/s
Maximum temperature            = 2138 K
Peak OH mole fraction location = 1.12 cm
Solved with 117 grid points

Restore the flame via initial guess#

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)
Flame speed                    = 34.45 cm/s
Maximum temperature            = 2138 K
Peak OH mole fraction location = 1.12 cm
Solved with 117 grid points

Load initial guess from HDF file directly

if hdf_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=hdf_filepath, group="freeflame")
    describe(f2)
Flame speed                    = 34.45 cm/s
Maximum temperature            = 2138 K
Peak OH mole fraction location = 1.12 cm
Solved with 117 grid points

Load initial guess from HDF file via SolutionArray

if hdf_filepath:
    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)
Flame speed                    = 34.45 cm/s
Maximum temperature            = 2138 K
Peak OH mole fraction location = 1.12 cm
Solved with 117 grid points
if pd is None:
    # skip remaining examples, as optional dependency 'pandas' is not installed
    print("All done")
    sys.exit()

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)
Flame speed                    = 34.45 cm/s
Maximum temperature            = 2138 K
Peak OH mole fraction location = 1.12 cm
Solved with 117 grid points

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)
Flame speed                    = 34.45 cm/s
Maximum temperature            = 2138 K
Peak OH mole fraction location = 1.12 cm
Solved with 117 grid points

Restart flame simulations with modified initial guesses#

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

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()
..............................................................................

Attempt Newton solution of steady-state problem.
Newton steady-state solve succeeded.

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

..............................................................................
##############################################################################
Refining grid in flame.
    New points inserted after grid points 4 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 46 47 48 50 51 53 54 55 56
    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.
Newton steady-state solve succeeded.

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

..............................................................................
##############################################################################
Refining grid in flame.
    New points inserted after grid points 35 106
    to resolve C2H5 point 106
##############################################################################

..............................................................................

Attempt Newton solution of steady-state problem.
Newton steady-state solve succeeded.

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

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

We wouldn’t expect the flame solutions to be exactly the same

describe(f2)
Flame speed                    = 34.66 cm/s
Maximum temperature            = 2139 K
Peak OH mole fraction location = 1.12 cm
Solved with 110 grid points

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()
..............................................................................

Attempt Newton solution of steady-state problem.
Newton steady-state solve failed.

Attempt 10 timesteps.
Final timestep info: dt= 2.813e-06  log(ss)= 7.869

Attempt Newton solution of steady-state problem.
Newton steady-state solve failed.

Attempt 10 timesteps.
Final timestep info: dt= 1.424e-05  log(ss)= 7.133

Attempt Newton solution of steady-state problem.
Newton steady-state solve failed.

Attempt 10 timesteps.
Final timestep info: dt= 0.0001081  log(ss)= 6.07

Attempt Newton solution of steady-state problem.
Newton steady-state solve failed.

Attempt 10 timesteps.
Final timestep info: dt= 0.001232   log(ss)= 5.125

Attempt Newton solution of steady-state problem.
Newton steady-state solve failed.

Attempt 10 timesteps.
Final timestep info: dt= 0.01403    log(ss)= 3.639

Attempt Newton solution of steady-state problem.
Newton steady-state solve succeeded.

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

..............................................................................
##############################################################################
Refining grid in flame.
    New points inserted after grid points 4 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 46 47 48 50 51 53 54 55 56
    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.
Newton steady-state solve succeeded.

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

..............................................................................
##############################################################################
Refining grid in flame.
    New points inserted after grid points 35 106
    to resolve C2H5 point 106
##############################################################################

..............................................................................

Attempt Newton solution of steady-state problem.
Newton steady-state solve succeeded.

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

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

We wouldn’t expect the flame solutions to be exactly the same

describe(f2)
Flame speed                    = 34.66 cm/s
Maximum temperature            = 2139 K
Peak OH mole fraction location = 1.12 cm
Solved with 110 grid points

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()
..............................................................................

Attempt Newton solution of steady-state problem.
Newton steady-state solve succeeded.

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

..............................................................................
##############################################################################
Refining grid in flame.
    New points inserted after grid points 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 40 41 42 43 46 47 48 50 51 53 54 55 56
    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.
Newton steady-state solve succeeded.

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

..............................................................................
##############################################################################
Refining grid in flame.
    New points inserted after grid points 34 35 40 42 104
    to resolve C2H5 C3H8 point 104
##############################################################################

..............................................................................

Attempt Newton solution of steady-state problem.
Newton steady-state solve succeeded.

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

..............................................................................
##############################################################################
Refining grid in flame.
    New points inserted after grid points 108
    to resolve NO
##############################################################################

..............................................................................

Attempt Newton solution of steady-state problem.
Newton steady-state solve succeeded.

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

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

We expect these flames to be different because we raised the temperature.

describe(f2)
Flame speed                    = 44.82 cm/s
Maximum temperature            = 2169 K
Peak OH mole fraction location = 1.12 cm
Solved with 112 grid points

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

Gallery generated by Sphinx-Gallery