Skip to content

Commit

Permalink
Use Cantera Error in leftover examples
Browse files Browse the repository at this point in the history
Update diffusion_flame_batch.py and diffusion_flame_extinction.py
to use CanteraError where appropriate. Define a new
FlameExtinguished exception to distinguish between extinction and
other failures. This allows things like OSErrors to still be
raised to the user while dealing with exceptions we can handle.
Closes #569.
  • Loading branch information
CyberDrudge authored and bryanwweber committed Feb 18, 2019
1 parent 9d4c0ed commit aa9721d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 13 deletions.
29 changes: 19 additions & 10 deletions interfaces/cython/cantera/examples/onedim/diffusion_flame_batch.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,19 @@
(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.
This example can e.g. be used to iterate to a counterflow diffusion flame to an
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.
"""

import cantera as ct
import numpy as np
import os


class FlameExtinguished(Exception):
pass


# Create directory for output data files
data_directory = 'diffusion_flame_batch_data/'
if not os.path.exists(data_directory):
Expand Down Expand Up @@ -49,12 +54,16 @@

# Define a limit for the maximum temperature below which the flame is
# considered as extinguished and the computation is aborted
# This increases the speed of refinement is enabled
# This increases the speed of refinement, if enabled
temperature_limit_extinction = 900 # K


def interrupt_extinction(t):
if np.max(f.T) < temperature_limit_extinction:
raise Exception('Flame extinguished')
raise FlameExtinguished('Flame extinguished')
return 0.


f.set_interrupt(interrupt_extinction)

# Initialize and solve
Expand Down Expand Up @@ -116,7 +125,7 @@ def interrupt_extinction(t):
description='Cantera version ' + ct.__version__ +
', reaction mechanism ' + reaction_mechanism)
p_previous = p
except Exception as e:
except ct.CanteraError as e:
print('Error occurred while solving:', e, 'Try next pressure level')
# If solution failed: Restore the last successful solution and continue
f.restore(filename=data_directory + file_name, name='solution',
Expand Down Expand Up @@ -167,11 +176,11 @@ def interrupt_extinction(t):
f.save(data_directory + file_name, name='solution', loglevel=1,
description='Cantera version ' + ct.__version__ +
', reaction mechanism ' + reaction_mechanism)
except Exception as e:
if e.args[0] == 'Flame extinguished':
print('Flame extinguished')
else:
print('Error occurred while solving:', e)
except FlameExtinguished:
print('Flame extinguished')
break
except ct.CanteraError as e:
print('Error occurred while solving:', e)
break


Expand Down Expand Up @@ -215,7 +224,7 @@ def interrupt_extinction(t):
for n in n_selected:
file_name = 'strain_loop_{0:02d}.xml'.format(n)
f.restore(filename=data_directory + file_name, name='solution', loglevel=0)
a_max = f.strain_rate('max') # the maximum axial strain rate
a_max = f.strain_rate('max') # the maximum axial strain rate

# Plot the temperature profiles for the strain rate loop (selected)
ax3.plot(f.grid / f.grid[-1], f.T, label='{0:.2e} 1/s'.format(a_max))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,10 @@
f.set_profile('lambda', normalized_grid, f.L * strain_factor ** exp_lam_a)
try:
f.solve(loglevel=0)
except Exception as e:
# Throw Exception if solution fails
except ct.CanteraError as e:
print('Error: Did not converge at n =', n, e)
if np.max(f.T) > temperature_limit_extinction:
# Flame still burning, so go to next strain rate
# Flame is still burning, so proceed to next strain rate
n_last_burning = n
file_name = 'extinction_{0:04d}.xml'.format(n)
f.save(data_directory + file_name, name='solution', loglevel=0,
Expand Down

0 comments on commit aa9721d

Please sign in to comment.