From aa9721dbe985debfe993e096d15fb4420740a548 Mon Sep 17 00:00:00 2001 From: CyberDrudge Date: Tue, 12 Feb 2019 23:03:38 +0530 Subject: [PATCH] Use Cantera Error in leftover examples 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. --- .../examples/onedim/diffusion_flame_batch.py | 29 ++++++++++++------- .../onedim/diffusion_flame_extinction.py | 5 ++-- 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/interfaces/cython/cantera/examples/onedim/diffusion_flame_batch.py b/interfaces/cython/cantera/examples/onedim/diffusion_flame_batch.py index ed84644d70..168071c768 100644 --- a/interfaces/cython/cantera/examples/onedim/diffusion_flame_batch.py +++ b/interfaces/cython/cantera/examples/onedim/diffusion_flame_batch.py @@ -12,7 +12,7 @@ (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. """ @@ -20,6 +20,11 @@ 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): @@ -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 @@ -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', @@ -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 @@ -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)) diff --git a/interfaces/cython/cantera/examples/onedim/diffusion_flame_extinction.py b/interfaces/cython/cantera/examples/onedim/diffusion_flame_extinction.py index 862633b03c..3545b88378 100644 --- a/interfaces/cython/cantera/examples/onedim/diffusion_flame_extinction.py +++ b/interfaces/cython/cantera/examples/onedim/diffusion_flame_extinction.py @@ -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,