Cantera / cantera

Chemical kinetics, thermodynamics, and transport tool suite
https://cantera.org
Other
581 stars 342 forks source link

FlameBase.set_initial_guess does not work for counterflow flames when using restart data #1674

Closed speth closed 2 weeks ago

speth commented 3 months ago

Problem description

The FlameBase.set_initial_guess method does not work for counterflow flame configurations (for example, CounterflowDiffusionFlame.

Steps to reproduce


from pathlib import Path
import cantera as ct

p = ct.one_atm  # pressure
tin_f = 300.0  # fuel inlet temperature
tin_o = 300.0  # oxidizer inlet temperature
mdot_o = 0.72  # kg/m^2/s
mdot_f = 0.24  # kg/m^2/s

comp_o = "O2:0.21, N2:0.78, AR:0.01"  # air composition
comp_f = "C2H6:1"  # fuel composition

width = 0.02  # Distance between inlets is 2 cm

loglevel = 1  # amount of diagnostic output (0 to 5)

gas = ct.Solution("gri30.yaml")
gas.TP = gas.T, p

for i in range(2):
    f = ct.CounterflowDiffusionFlame(gas, width=width)

    # Set the state of the two inlets
    f.fuel_inlet.mdot = mdot_f
    f.fuel_inlet.X = comp_f
    f.fuel_inlet.T = tin_f

    f.oxidizer_inlet.mdot = mdot_o
    f.oxidizer_inlet.X = comp_o
    f.oxidizer_inlet.T = tin_o

    # Set the boundary emissivities
    f.boundary_emissivities = 0.0, 0.0
    # Turn radiation off
    f.radiation_enabled = False

    f.set_refine_criteria(ratio=4, slope=0.2, curve=0.3, prune=0.04)

    # Use the prior solution as an intial guess
    if i > 0:
        f.set_initial_guess(data=restart_file_name)

    # Solve the problem
    f.solve(loglevel, auto=True)

    # Save the result to .csv to be used as the next initial guess
    restart_file_name = Path() / "restart.csv"
    f.save(
        restart_file_name,
        overwrite=True,
    )

Behavior

The above code fails on the second iteration through the loop with the exception:

Cell In[4], line 41
     39 # Use the prior solution as an intial guess
     40 if i > 0:
---> 41     f.set_initial_guess(data=restart_file_name)
     43 # Solve the problem
     44 f.solve(loglevel, auto=True)

File [onedim.py:1253], in CounterflowDiffusionFlame.set_initial_guess(self, data, group)
   1246 def set_initial_guess(self, data=None, group=None):
   1247     """
   1248     Set the initial guess for the solution. By default, the initial guess
   1249     is generated by assuming infinitely-fast chemistry. Alternatively, a
   1250     previously calculated result can be supplied as an initial guess via
   1251     'data' and 'key' inputs (see `FlameBase.set_initial_guess`).
   1252     """
-> 1253     super().set_initial_guess(data=data, group=group)
   1254     if data:
   1255         return

File [onedim.py:163], in FlameBase.set_initial_guess(self, data, group, *args, **kwargs)
    159 right = self.domains[2]
    161 if isinstance(left, Inlet1D) and isinstance(right, Inlet1D):
    162     # find stagnation plane
--> 163     i = np.flatnonzero(self.velocity > 0)[-1]
    165     # adjust temperatures
    166     grid = arr.grid

IndexError: index -1 is out of bounds for axis 0 with size 0

System information

Additional context

Originally reported on the Cantera Users' Group