Skip to content

Commit

Permalink
[Examples] Formatting changes
Browse files Browse the repository at this point in the history
Add requirements to all Python examples. Update formatting to resolve
some PEP8 problems.
  • Loading branch information
bryanwweber committed Dec 30, 2019
1 parent da1087f commit 01e3491
Show file tree
Hide file tree
Showing 61 changed files with 409 additions and 283 deletions.
Expand Up @@ -6,11 +6,12 @@
To test the submechanism, a premixed CO/H2 flame is simulated using the original
mechanism and the submechanism, which demonstrates that the submechanism
contains all of the important species and reactions.
Requires: cantera >= 2.5.0, matplotlib >= 2.0
"""

from timeit import default_timer
import cantera as ct
import numpy as np
import matplotlib.pyplot as plt

all_species = ct.Species.listFromFile('gri30.yaml')
Expand All @@ -22,7 +23,7 @@
if 'C' in comp and 'H' in comp:
# Exclude all hydrocarbon species
continue
if 'N' in comp and comp != {'N':2}:
if 'N' in comp and comp != {'N': 2}:
# Exclude all nitrogen compounds except for N2
continue
if 'Ar' in comp:
Expand Down Expand Up @@ -54,19 +55,21 @@
gas2 = ct.Solution(thermo='IdealGas', kinetics='GasKinetics',
species=species, reactions=reactions)


def solve_flame(gas):
gas.TPX = 373, 0.05*ct.one_atm, 'H2:0.4, CO:0.6, O2:1, N2:3.76'

# Create the flame simulation object
sim = ct.CounterflowPremixedFlame(gas=gas, width=0.2)

sim.reactants.mdot = 0.12 # kg/m^2/s
sim.products.mdot = 0.06 # kg/m^2/s
sim.reactants.mdot = 0.12 # kg/m^2/s
sim.products.mdot = 0.06 # kg/m^2/s

sim.set_refine_criteria(ratio=3, slope=0.1, curve=0.2)
sim.solve(0, auto=True)
return sim


t1 = default_timer()
sim1 = solve_flame(gas1)
t2 = default_timer()
Expand Down
Expand Up @@ -11,6 +11,8 @@
and the associated species, and run the simulations again with these mechanisms
to see whether the reduced mechanisms with a certain number of species are able
to adequately simulate the ignition delay problem.
Requires: cantera >= 2.5.0, matplotlib >= 2.0
"""

import cantera as ct
Expand Down Expand Up @@ -44,8 +46,8 @@
R = sorted(zip(Rmax, gas.reactions()), key=lambda x: -x[0])

# Test reduced mechanisms with different numbers of reactions
C = plt.cm.winter(np.linspace(0,1,5))
for i,N in enumerate([40,50,60,70,80]):
C = plt.cm.winter(np.linspace(0, 1, 5))
for i, N in enumerate([40, 50, 60, 70, 80]):
# Get the N most active reactions
reactions = [r[1] for r in R[:N]]

Expand Down Expand Up @@ -77,7 +79,7 @@
tt.append(1000 * t)
TT.append(r.T)

plt.plot(tt,TT, lw=2, color=C[i],
plt.plot(tt, TT, lw=2, color=C[i],
label='K={0}, R={1}'.format(gas2.n_species, N))
plt.xlabel('Time (ms)')
plt.ylabel('Temperature (K)')
Expand Down
4 changes: 3 additions & 1 deletion interfaces/cython/cantera/examples/kinetics/reaction_path.py
Expand Up @@ -3,8 +3,10 @@
This script uses Graphviz to generate an image. You must have Graphviz installed
and the program 'dot' must be on your path for this example to work.
Graphviz can be obtained from http://www.graphviz.org/ or (possibly) installed
Graphviz can be obtained from https://www.graphviz.org/ or (possibly) installed
using your operating system's package manager.
Requires: cantera >= 2.5.0
"""

import os
Expand Down
6 changes: 4 additions & 2 deletions interfaces/cython/cantera/examples/multiphase/adiabatic.py
@@ -1,6 +1,8 @@
"""
Adiabatic flame temperature and equilibrium composition for a fuel/air mixture
as a function of equivalence ratio, including formation of solid carbon.
Requires: cantera >= 2.5.0, matplotlib >= 2.0
"""

import cantera as ct
Expand Down Expand Up @@ -35,7 +37,7 @@

# create some arrays to hold the data
tad = np.zeros(npoints)
xeq = np.zeros((mix.n_species,npoints))
xeq = np.zeros((mix.n_species, npoints))

for i in range(npoints):
# set the gas state
Expand All @@ -51,7 +53,7 @@

tad[i] = mix.T
print('At phi = {0:12.4g}, Tad = {1:12.4g}'.format(phi[i], tad[i]))
xeq[:,i] = mix.species_moles
xeq[:, i] = mix.species_moles

# write output CSV file for importing into Excel
csv_file = 'adiabatic.csv'
Expand Down
@@ -1,6 +1,8 @@
"""
An equilibrium example with charged species in the gas phase
and multiple condensed phases.
Requires: cantera >= 2.5.0, matplotlib >= 2.0
"""

import cantera as ct
Expand Down
7 changes: 4 additions & 3 deletions interfaces/cython/cantera/examples/onedim/adiabatic_flame.py
@@ -1,10 +1,11 @@
"""
A freely-propagating, premixed hydrogen flat flame with multicomponent
transport properties.
Requires: cantera >= 2.5.0
"""

import cantera as ct
import numpy as np

# Simulation parameters
p = ct.one_atm # pressure [Pa]
Expand Down Expand Up @@ -34,10 +35,10 @@

# Solve with multi-component transport properties
f.transport_model = 'Multi'
f.solve(loglevel) # don't use 'auto' on subsequent solves
f.solve(loglevel) # don't use 'auto' on subsequent solves
f.show_solution()
print('multicomponent flamespeed = {0:7f} m/s'.format(f.u[0]))
f.save('h2_adiabatic.xml','multi', 'solution with multicomponent transport')
f.save('h2_adiabatic.xml', 'multi', 'solution with multicomponent transport')

# write the velocity, temperature, density, and mole fractions to a CSV file
f.write_csv('h2_adiabatic.csv', quiet=False)
7 changes: 4 additions & 3 deletions interfaces/cython/cantera/examples/onedim/burner_flame.py
@@ -1,15 +1,16 @@
"""
A burner-stabilized lean premixed hydrogen-oxygen flame at low pressure.
Requires: cantera >= 2.5.0
"""

import cantera as ct
import numpy as np

p = 0.05 * ct.one_atm
tburner = 373.0
mdot = 0.06
reactants = 'H2:1.5, O2:1, AR:7' # premixed gas composition
width = 0.5 # m
width = 0.5 # m
loglevel = 1 # amount of diagnostic output (0 to 5)

gas = ct.Solution('h2o2.yaml')
Expand All @@ -25,7 +26,7 @@
f.save('h2_burner_flame.xml', 'mix', 'solution with mixture-averaged transport')

f.transport_model = 'Multi'
f.solve(loglevel) # don't use 'auto' on subsequent solves
f.solve(loglevel) # don't use 'auto' on subsequent solves
f.show_solution()
f.save('h2_burner_flame.xml', 'multi', 'solution with multicomponent transport')

Expand Down
6 changes: 3 additions & 3 deletions interfaces/cython/cantera/examples/onedim/burner_ion_flame.py
@@ -1,14 +1,15 @@
"""
A burner-stabilized lean premixed hydrogen-oxygen flame at low pressure.
Requires: cantera >= 2.5.0
"""

import cantera as ct
import numpy as np

p = ct.one_atm
tburner = 600.0
reactants = 'CH4:1.0, O2:2.0, N2:7.52' # premixed gas composition
width = 0.5 # m
width = 0.5 # m
loglevel = 1 # amount of diagnostic output (0 to 5)

gas = ct.Solution('gri30_ion.yaml')
Expand All @@ -26,4 +27,3 @@
f.save('CH4_burner_flame.xml', 'mix', 'solution with mixture-averaged transport')

f.write_csv('CH4_burner_flame.csv', quiet=False)

5 changes: 3 additions & 2 deletions interfaces/cython/cantera/examples/onedim/diffusion_flame.py
@@ -1,9 +1,10 @@
"""
An opposed-flow ethane/air diffusion flame
Requires: cantera >= 2.5.0, matplotlib >= 2.0
"""

import cantera as ct
import numpy as np
import matplotlib.pyplot as plt

# Input parameters
Expand All @@ -16,7 +17,7 @@
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
width = 0.02 # Distance between inlets is 2 cm

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

Expand Down
22 changes: 11 additions & 11 deletions interfaces/cython/cantera/examples/onedim/diffusion_flame_batch.py
@@ -1,5 +1,3 @@
# -*- coding: utf-8 -*-

# This file is part of Cantera. See License.txt in the top-level directory or
# at https://cantera.org/license.txt for license and copyright information.

Expand All @@ -14,14 +12,18 @@
This example can, for example, be used to iterate to a counterflow diffusion flame to an
awkward pressure and strain rate, or to create the basis for a flamelet table.
Requires: cantera >= 2.5.0, matplotlib >= 2.0
"""

import cantera as ct
import numpy as np
import os

import cantera as ct
import matplotlib.pyplot as plt


class FlameExtinguished(Exception):
class FlameExtinguished(Exception):
pass


Expand All @@ -37,7 +39,7 @@ class FlameExtinguished(Exception):

reaction_mechanism = 'h2o2.yaml'
gas = ct.Solution(reaction_mechanism)
width = 18e-3 # 18mm wide
width = 18e-3 # 18mm wide
f = ct.CounterflowDiffusionFlame(gas, width=width)

# Define the operating pressure and boundary conditions
Expand Down Expand Up @@ -186,12 +188,10 @@ def interrupt_extinction(t):

# PART 4: PLOT SOME FIGURES

import matplotlib.pyplot as plt

fig1 = plt.figure()
fig2 = plt.figure()
ax1 = fig1.add_subplot(1,1,1)
ax2 = fig2.add_subplot(1,1,1)
ax1 = fig1.add_subplot(1, 1, 1)
ax2 = fig2.add_subplot(1, 1, 1)
p_selected = p_range[::7]

for p in p_selected:
Expand All @@ -218,8 +218,8 @@ def interrupt_extinction(t):

fig3 = plt.figure()
fig4 = plt.figure()
ax3 = fig3.add_subplot(1,1,1)
ax4 = fig4.add_subplot(1,1,1)
ax3 = fig3.add_subplot(1, 1, 1)
ax4 = fig4.add_subplot(1, 1, 1)
n_selected = range(1, n, 5)
for n in n_selected:
file_name = 'strain_loop_{0:02d}.xml'.format(n)
Expand Down
@@ -1,5 +1,3 @@
# -*- coding: utf-8 -*-

# This file is part of Cantera. See License.txt in the top-level directory or
# at https://cantera.org/license.txt for license and copyright information.

Expand All @@ -10,12 +8,16 @@
The tutorial makes use of the scaling rules derived by Fiala and Sattelmayer
(doi:10.1155/2014/484372). Please refer to this publication for a detailed
explanation. Also, please don't forget to cite it if you make use of it.
Requires: cantera >= 2.5.0, matplotlib >= 2.0
"""

import cantera as ct
import numpy as np
import os

import cantera as ct
import matplotlib.pyplot as plt

# Create directory for output data files
data_directory = 'diffusion_flame_extinction_data/'
if not os.path.exists(data_directory):
Expand All @@ -28,7 +30,7 @@

reaction_mechanism = 'h2o2.yaml'
gas = ct.Solution(reaction_mechanism)
width = 18.e-3 # 18mm wide
width = 18.e-3 # 18mm wide
f = ct.CounterflowDiffusionFlame(gas, width=width)

# Define the operating pressure and boundary conditions
Expand Down Expand Up @@ -133,8 +135,10 @@
break
else:
# Procedure if flame extinguished but abortion criterion is not satisfied
print('Flame extinguished at n = {0}. Restoring n = {1} with alpha = {2}'.format(
n, n_last_burning, alpha[n_last_burning]))
print(
'Flame extinguished at n = {0}. Restoring n = {1} '
'with alpha = {2}'.format(n, n_last_burning, alpha[n_last_burning])
)
# Reduce relative strain rate increase
delta_alpha = delta_alpha / delta_alpha_factor
# Restore last burning solution
Expand All @@ -157,7 +161,6 @@
f.strain_rate('stoichiometric', fuel='H2')))

# Plot the maximum temperature over the maximum axial velocity gradient
import matplotlib.pyplot as plt
plt.figure()
plt.semilogx(a_max, T_max)
plt.xlabel(r'$a_{max}$ [1/s]')
Expand Down
4 changes: 3 additions & 1 deletion interfaces/cython/cantera/examples/onedim/flame_fixed_T.py
@@ -1,6 +1,8 @@
"""
A burner-stabilized, premixed methane/air flat flame with multicomponent
transport properties and a specified temperature profile.
Requires: cantera >= 2.5.0
"""

import cantera as ct
Expand All @@ -14,7 +16,7 @@
comp = 'CH4:0.65, O2:1, N2:3.76' # premixed gas composition

# The solution domain is chosen to be 1 cm
width = 0.01 # m
width = 0.01 # m

loglevel = 1 # amount of diagnostic output (0 to 5)
refine_grid = True # 'True' to enable refinement
Expand Down
Expand Up @@ -2,10 +2,11 @@
Sensitivity analysis for a freely-propagating, premixed methane-air
flame. Computes the sensitivity of the laminar flame speed with respect
to each reaction rate constant.
Requires: cantera >= 2.5.0
"""

import cantera as ct
import numpy as np

# Simulation parameters
p = ct.one_atm # pressure [Pa]
Expand All @@ -14,7 +15,7 @@

width = 0.03 # m

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

Expand Down

0 comments on commit 01e3491

Please sign in to comment.