Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Cantera Error in leftover examples #596

Merged
merged 1 commit into from
Feb 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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