Code Documentation¶
cansen module¶
-
cansen.cansen.
worker
(sim_index_tup)[source]¶ Worker for multiprocessing of cases.
Parameters: sim_index_tup – Tuple containing the MultiSimulationCase object to be run and the index of current case for status messages. Return res: List of simulation results.
-
cansen.cansen.
main
(filenames, convert, multi, num_proc, version)[source]¶ The main driver function of CanSen.
Parameters: - filenames – Dictionary of filenames related to the simulation.
- convert – Boolean indicating that the user wishes only to convert the input mechanism and quit.
- multi – Boolean indicating multiple cases to be run.
- num_proc – Number of processors to use for multiprocessing.
- version – Version string of CanSen.
-
cansen.cansen.
cansen
(argv)[source]¶ CanSen - the SENKIN-like wrapper for Cantera written in Python.
- Usage:
- -i:
- Specify the simulation input file in SENKIN format. Required.
- -o:
- Specify the text output file. Optional, default:
output.out
- -x:
- Specify the binary save output file. Optional, default:
save.hdf
- -c:
- Specify the chemistry input file, in either CHEMKIN, Cantera
CTI or CTML format. Optional, default:
chem.xml
- -d:
- Specify the thermodynamic database. Optional if the thermodynamic database is specified in the chemistry input file. Otherwise, required.
- –convert:
- Convert the input mechanism to CTI format and quit. If
--convert
is specified, the SENKIN input file is optional. - -m, –multi:
- Run multiple cases from the input file. Optional. If
-m
is used, must specify number of processors to be used (e.g.,-m 4
). If--multi
is specified, CanSen uses the available number of processors by default. - -h, –help:
- Print this help message and quit.
exceptions module¶
Exceptions for CanSen.
-
exception
cansen.exceptions.
KeywordError
(*keywords)[source]¶ Bases:
cansen.exceptions.CanSenError
Raised for errors in keyword parsing.
-
exception
cansen.exceptions.
MultipleProblemError
(*keywords)[source]¶ Bases:
cansen.exceptions.KeywordError
Raised error when multiple problem types are specified.
-
exception
cansen.exceptions.
UnsupportedKeyword
(*keywords)[source]¶ Bases:
Warning
Raised error when a keyword is unsupported.
-
exception
cansen.exceptions.
UndefinedKeywordError
(*keywords)[source]¶ Bases:
cansen.exceptions.KeywordError
Raised for undefined keywords.
-
exception
cansen.exceptions.
MissingReqdKeywordError
(*keywords)[source]¶ Bases:
cansen.exceptions.KeywordError
Raised for missing required keywords.
printer module¶
profiles module¶
-
class
cansen.profiles.
VolumeProfile
(keywords)[source]¶ Bases:
object
Set the velocity of the piston by using a user specified volume profile. The initialization and calling of this class are handled by the
Func1
interface of Cantera. Used with the input keyword VPROSet the initial values of the arrays from the input keywords.
The time and volume are read from the input file and stored in the
keywords
dictionary. The velocity is calculated by assuming a unit area and using the forward difference, calculated bynumpy.diff
. This function is only called once when the class is initialized at the beginning of a problem so it is efficient.Parameters: keywords – Dictionary of keywords read from the input file
-
class
cansen.profiles.
TemperatureProfile
(keywords)[source]¶ Bases:
object
Set the temperature of the reactor by using a user specified temperature profile. The initialization and calling of this class are handled by the
Func1
interface of Cantera. Used with the input keyword TPROSet the initial values of the arrays from the input keywords.
The time and temperature are read from the input file and stored in the
keywords
dictionary as lists. This function is only called once when the class is initialized at the beginning of a problem so it is efficient.
-
class
cansen.profiles.
ICEngineProfile
(keywords)[source]¶ Bases:
object
Set the velocity of the wall according to the parameters of a reciprocating engine. The initialization and calling of this class are handled by the
Func1
interface of Cantera. Used with the input keyword ICEN.Set the initial values of the engine parameters.
The parameters are read from the input file into the
keywords
dictionary.-
__call__
(time)[source]¶ Return the velocity of the piston when called.
The function for the velocity is given by Heywood. See Internal Combustion Engine Model.
Parameters: time – Input float, current simulation time
-
run_cases module¶
-
class
cansen.run_cases.
SimulationCase
(filenames)[source]¶ Bases:
object
Class that sets up and runs a simulation case.
Initialize the simulation case.
Read the SENKIN-format input file is read into the
keywords
dictionary.Parameters: filenames – Dictionary containing the relevant file names for this case. -
setup_case
()[source]¶ Sets up the case to be run. Initializes the
ThermoPhase
,Reactor
, andReactorNet
according to the values from the input file.
-
run_case
()[source]¶ Actually run the case set up by setup_case. Sets binary output file format, then runs the simulation by using :py:`~cantera.ReactorNet.step`.
-
run_simulation
()[source]¶ Helper function that sequentially sets up the simulation case and runs it. Useful for cases where nothing needs to be changed between the setup and run. See setup_case and run_case.
-
reactor_state_printer
(state, end=False)[source]¶ Produce pretty-printed output from the input reactor state.
In this function, we have to explicitly pass in the reactor state instead of using
self.reac
because we might have interpolated to get to the proper time.Parameters: - state – Vector of reactor state information.
- end – Boolean to tell the printer this is the final print operation.
-
-
class
cansen.run_cases.
MultiSimulationCase
(filenames)[source]¶ Bases:
cansen.run_cases.SimulationCase
Class that sets up and runs a simulation case, for multiple.
When multiple cases are run, no output is printed and no simulation data is saved; upon completion, the calculated ignition delay times are written to the output file.
Initialize the simulation case.
Read the SENKIN-format input file is read into the
keywords
dictionary.Parameters: filenames – Dictionary containing the relevant file names for this case.
user_routines module¶
-
class
cansen.user_routines.
VolumeFunctionTime
[source]¶ Bases:
object
Calculate the volume of the reactor as a user specified function of time.
Set up the function to be calculated.
The init function should be used to import any parameters for the volume as a function of time from external files. Do not calculate the volume as a function of time here. Store all of the parameters in the
self
instance. The reason for this is to avoid running the__init__
function on every time step. See the example below.Example to load polynomial parameters from a file:
# Read the file into the list ``self.params``. The lines of # the file are read as strings. with open('file.txt','r') as input_file: self.params = input_file.readlines() # Convert the strings to floats. self.params = [float(param) for param in self.params] self.area = 1 # m**2
-
__call__
(time)[source]¶ Return the velocity of the piston at the given time.
The call function should be where the implementation of the volume as a function of time is done. Cantera expects the velocity to be returned - v = dV/dt * 1/A. Get all of the needed parameters that were stored in the
self
instance. See the example below.Example to use the previously stored polynomial parameters:
volume = (self.params[0] + self.params[1]*time + self.params[2]*time**2 + self.params[3]*time**3) # m**3 dvoldt = (self.params[1] + 2*self.params[2]*time + 3*self.params[3]*time**2) # m**3/s velocity = dvoldt/self.area # m/s return velocity
-
-
class
cansen.user_routines.
TemperatureFunctionTime
[source]¶ Bases:
object
Calculate the temperature of the reactor as a user specified function of time.
Set up the function to be calculated.
The init function should be used to import any parameters for the temperature as a function of time from external files. Do not calculate the temperature as a function of time here. Store all of the parameters in the
self
instance. The reason for this is to avoid running the__init__
function on every time step. See the example below.Example to load polynomial parameters from a file:
# Read the file into the list ``self.params``. The lines of # the file are read as strings. with open('file.txt','r') as input_file: self.params = input_file.readlines() # Convert the strings to floats. self.params = [float(param) for param in self.params]
-
__call__
(time)[source]¶ Return the temperature of the piston at the given time.
The call function should be where the implementation of the temperature as a function of time is done. Get all of the needed parameters that were stored in the
self
instance. See the example below. Note:None
is not a valid return value, so this function does not work as written.Example to use the previously stored polynomial parameters:
temperature = (self.params[0] + self.params[1]*time + self.params[2]*time**2 + self.params[3]*time**3) # K return temperature
-
utils module¶
-
class
cansen.utils.
MyParser
(prog=None, usage=None, description=None, epilog=None, parents=[], formatter_class=<class 'argparse.HelpFormatter'>, prefix_chars='-', fromfile_prefix_chars=None, argument_default=None, conflict_handler='error', add_help=True, allow_abbrev=True)[source]¶ Bases:
argparse.ArgumentParser
Parser that redefines the error printing
-
cansen.utils.
convert_mech
(mech_filename, thermo_filename=None)[source]¶ Convert a mechanism and return a string with the filename.
Convert a CHEMKIN format mechanism to the Cantera CTI format using the Cantera built-in script ck2cti.
Parameters: - mech_filename – Filename of the input CHEMKIN format mechanism. The converted CTI file will have the same name, but with .cti extension.
- thermo_filename – Filename of the thermodynamic database. Optional if the thermodynamic database is present in the mechanism input.
-
cansen.utils.
process_multi_input
(input_filename)[source]¶ Process a formatted input file into multiple cases.
Processes a formatted input file that contains multiple cases into separate temporary files, for individual reading of keywords.
Parameters: input_filename – Filename of the SENKIN input file. Return filenames: List of temporary filenames.
-
cansen.utils.
remove_files
(files)[source]¶ Delete files.
Parameters: files – List of names of files to be removed
-
cansen.utils.
read_input_file
(input_filename)[source]¶ Read a formatted input file and return a dictionary of keywords.
Parameters: input_filename – Filename of the SENKIN input file.
-
cansen.utils.
cli_parser
(argv)[source]¶ Parse command line interface input.
Parameters: argv – List of command line options.
-
cansen.utils.
reactor_interpolate
(interp_time, state1, state2)[source]¶ Linearly interpolate the reactor states to the given input time.
Parameters: - interp_time – Time at which the interpolated values should be calculated
- state1 – Array of the state information at the previous time step.
- state2 – Array of the state information at the current time step.
-
cansen.utils.
equivalence_ratio
(gas, eq_ratio, fuel, oxidizer, complete_products, additional_species)[source]¶ Calculate the mixture mole fractions from the equivalence ratio.
Given the equivalence ratio, fuel mixture, oxidizer mixture, the products of complete combustion, and any additional species for the mixture, return a string containing the mole fractions of the species, suitable for setting the state of the input
ThermoPhase
.Parameters: - gas – Cantera
ThermoPhase
object containing the desired species. - eq_ratio – Equivalence ratio
- fuel – Dictionary of molecules in the fuel mixture and the fraction of each molecule in the fuel mixture
- oxidizer – Dictionary of molecules in the oxidizer mixture and the fraction of each molecule in the oxidizer mixture.
- complete_products – List of species in the products of complete combustion.
- additional_species – Dictionary of molecules that will be added to the mixture. The mole fractions given in this dictionary are as a fraction of the total mixture.
- gas – Cantera