gotran.codegeneration package¶
Submodules¶
gotran.codegeneration.algorithmcomponents module¶
-
class
gotran.codegeneration.algorithmcomponents.
JacobianComponent
(ode, function_name='compute_jacobian', result_name='jac', params=None)[source]¶ Bases:
gotran.codegeneration.codecomponent.CodeComponent
An ODEComponent which keeps all expressions for the Jacobian of the rhs
-
class
gotran.codegeneration.algorithmcomponents.
JacobianActionComponent
(jacobian, with_body=True, function_name='compute_jacobian_action', result_name='jac_action', params=None)[source]¶ Bases:
gotran.codegeneration.codecomponent.CodeComponent
Jacobian action component which returns the expressions for Jac*x
-
class
gotran.codegeneration.algorithmcomponents.
FactorizedJacobianComponent
(jacobian, function_name='lu_factorize', params=None)[source]¶ Bases:
gotran.codegeneration.codecomponent.CodeComponent
Class to generate expressions for symbolicaly factorizing a jacobian
-
class
gotran.codegeneration.algorithmcomponents.
ForwardBackwardSubstitutionComponent
(factorized, function_name='forward_backward_subst', result_name='dx', residual_name='F', params=None)[source]¶ Bases:
gotran.codegeneration.codecomponent.CodeComponent
Class to generate a forward backward substiution algorithm for symbolically factorized jacobian
-
class
gotran.codegeneration.algorithmcomponents.
LinearizedDerivativeComponent
(ode, function_name='linear_derivatives', result_names=['linearized', 'rhs'], only_linear=True, include_rhs=False, nonlinear_last=False, params=None)[source]¶ Bases:
gotran.codegeneration.codecomponent.CodeComponent
A component for all linear and linearized derivatives
-
class
gotran.codegeneration.algorithmcomponents.
CommonSubExpressionODE
(ode)[source]¶ Bases:
gotran.model.ode.ODE
Class which flattens the component structue of an ODE to just one. It uses common sub expressions as intermediates to reduce complexity of the derivative expressions.
-
gotran.codegeneration.algorithmcomponents.
componentwise_derivative
(ode, indices, params=None, result_name='dy')[source]¶ Return an ODEComponent holding the expressions for the ith state derivative
Parameters: - ode (gotran.ODE) – The finalized ODE for which the ith derivative should be computed
- indices (int, list of ints) – The index
- params (dict) – Parameters determining how the code should be generated
- result_name (str) – The name of the result variable
-
gotran.codegeneration.algorithmcomponents.
linearized_derivatives
(ode, function_name='linear_derivatives', result_names=['linearized', 'dy'], only_linear=True, include_rhs=False, nonlinear_last=False, params=None)[source]¶ Return an ODEComponent holding the linearized derivative expressions
Parameters: - ode (gotran.ODE) – The ODE for which derivatives should be linearized
- function_name (str) – The name of the function which should be generated
- result_names (str) – The name of the variable storing the linearized derivatives and the rhs evaluation if that is included.
- only_linear (bool) – If True, only linear terms will be linearized
- include_rhs (bool) – If True, rhs evaluation will be included in the generated code.
- nonlinear_last (bool) – If True the nonlinear expressions are added last after a comment
- params (dict) – Parameters determining how the code should be generated
-
gotran.codegeneration.algorithmcomponents.
jacobian_expressions
(ode, function_name='compute_jacobian', result_name='jac', params=None)[source]¶ Return an ODEComponent holding expressions for the jacobian
Parameters: - ode (gotran.ODE) – The ODE for which the jacobian expressions should be computed
- function_name (str) – The name of the function which should be generated
- result_name (str) – The name of the variable storing the jacobian result
- params (dict) – Parameters determining how the code should be generated
-
gotran.codegeneration.algorithmcomponents.
jacobian_action_expressions
(jacobian, with_body=True, function_name='compute_jacobian_action', result_name='jac_action', params=None)[source]¶ Return an ODEComponent holding expressions for the jacobian action
Parameters: - jacobian (gotran.JacobianComponent) – The ODEComponent holding expressions for the jacobian
- with_body (bool) – If true, the body for computing the jacobian will be included
- function_name (str) – The name of the function which should be generated
- result_name (str) – The name of the variable storing the jacobian diagonal result
- params (dict) – Parameters determining how the code should be generated
-
gotran.codegeneration.algorithmcomponents.
factorized_jacobian_expressions
(jacobian, function_name='lu_factorize', params=None)[source]¶ Return an ODEComponent holding expressions for the factorized jacobian
Parameters: - jacobian (gotran.JacobianComponent) – The ODEComponent holding expressions for the jacobian
- params (dict) – Parameters determining how the code should be generated
-
gotran.codegeneration.algorithmcomponents.
forward_backward_subst_expressions
(factorized, function_name='forward_backward_subst', result_name='dx', residual_name='F', params=None)[source]¶ Return an ODEComponent holding expressions for the forward backward substitions for a factorized jacobian
Parameters: - factorized (gotran.FactorizedJacobianComponent) – The ODEComponent holding expressions for the factorized jacobian
- function_name (str) – The name of the function which should be generated
- result_name (str) – The name of the result (increment)
- residual_name (str) – The name of the residual
- params (dict) – Parameters determining how the code should be generated
-
gotran.codegeneration.algorithmcomponents.
diagonal_jacobian_expressions
(jacobian, function_name='compute_diagonal_jacobian', result_name='diag_jac', params=None)[source]¶ Return an ODEComponent holding expressions for the diagonal jacobian
Parameters: - jacobian (gotran.JacobianComponent) – The Jacobian of the ODE
- function_name (str) – The name of the function which should be generated
- result_name (str) – The name of the variable storing the jacobian diagonal result
- params (dict) – Parameters determining how the code should be generated
-
gotran.codegeneration.algorithmcomponents.
rhs_expressions
(ode, function_name='rhs', result_name='dy', params=None)[source]¶ Return a code component with body expressions for the right hand side
Parameters: - ode (gotran.ODE) – The finalized ODE
- function_name (str) – The name of the function which should be generated
- result_name (str) – The name of the variable storing the rhs result
- params (dict) – Parameters determining how the code should be generated
-
gotran.codegeneration.algorithmcomponents.
diagonal_jacobian_action_expressions
(diagonal_jacobian, with_body=True, function_name='compute_diagonal_jacobian_action', result_name='diag_jac_action', params=None)[source]¶ Return an ODEComponent holding expressions for the diagonal jacobian action
Parameters: - diagonal_jacobian (gotran.DiagonalJacobianComponent) – The ODEComponent holding expressions for the diagonal jacobian
- with_body (bool) – If true, the body for computing the jacobian will be included
- function_name (str) – The name of the function which should be generated
- result_name (str) – The name of the variable storing the jacobian diagonal result
- params (dict) – Parameters determining how the code should be generated
-
gotran.codegeneration.algorithmcomponents.
monitored_expressions
(ode, monitored, function_name='monitored_expressions', result_name='monitored', params=None)[source]¶ Return a code component with body expressions to calculate monitored expressions
Parameters: - ode (gotran.ODE) – The finalized ODE for which the monitored expression should be computed
- monitored (tuple, list) – A tuple/list of strings containing the name of the monitored expressions
- function_name (str) – The name of the function which should be generated
- result_name (str) – The name of the variable storing the rhs result
- params (dict) – Parameters determining how the code should be generated
gotran.codegeneration.avoidsympycontractions module¶
gotran.codegeneration.codecomponent module¶
-
class
gotran.codegeneration.codecomponent.
CodeComponent
(name, ode, function_name, description, params=None, use_default_arguments=True, additional_arguments=None, **results)[source]¶ Bases:
gotran.model.odecomponent.ODEComponent
A wrapper class around an ODE. Its primary purpose is to help generate code.
The class alows extraction and manipulation of the ODE expressions.
-
add_indexed_expression
(basename, indices, expr, add_offset=False, dependent=None)[source]¶ Add an indexed expression using a basename and the indices
Parameters: - basename (str) – The basename of the indexed expression
- indices (int, tuple of int) – The fixed indices identifying the expression
- expr (sympy.Basic, scalar) – The expression.
- add_offset (bool) – Add offset to indices
- dependent (gotran.ODEObject) – If given the count of this expression will follow as a fractional count based on the count of the dependent object
-
gotran.codegeneration.codegenerators module¶
-
class
gotran.codegeneration.codegenerators.
PythonCodeGenerator
(params=None, ns='math')[source]¶ Bases:
gotran.codegeneration.codegenerators.BaseCodeGenerator
-
float_types
= {'double': 'float_', 'single': 'float32'}¶
-
function_code
(comp, indent=0, include_signature=True)[source]¶ Generate code for a single function given by a CodeComponent
-
language
= 'python'¶
-
monitor_name_to_index_code
(ode, monitored, indent=0)[source]¶ Return code for index handling for monitored
-
to_code
(expr, name)¶
-
-
class
gotran.codegeneration.codegenerators.
CCodeGenerator
(params=None)[source]¶ Bases:
gotran.codegeneration.codegenerators.BaseCodeGenerator
-
closure_end
= '}'¶
-
closure_start
= '{'¶
-
comment
= '//'¶
-
componentwise_code
(ode, indent=0, default_arguments=None, include_signature=True, return_body_lines=False)[source]¶
-
float_types
= {'double': 'double', 'single': 'float'}¶
-
function_code
(comp, indent=0, default_arguments=None, include_signature=True, return_body_lines=False)[source]¶
-
indent
= 2¶
-
indent_str
= ' '¶
-
index
(i)¶
-
language
= 'C'¶
-
line_cont
= ''¶
-
line_ending
= ';'¶
-
monitor_name_to_index_code
(ode, monitored, indent=0)[source]¶ Return code for index handling for monitored
-
to_code
(expr, name)¶
-
-
class
gotran.codegeneration.codegenerators.
CppCodeGenerator
(params=None)[source]¶ Bases:
gotran.codegeneration.codegenerators.CCodeGenerator
-
language
= 'C++'¶
-
to_code
(expr, name)¶
-
-
class
gotran.codegeneration.codegenerators.
MatlabCodeGenerator
(params=None)[source]¶ Bases:
gotran.codegeneration.codegenerators.BaseCodeGenerator
A Matlab Code generator
-
closure_end
= 'end'¶
-
closure_start
= ''¶
-
code_dict
(ode, monitored=None, include_init=True)[source]¶ Generates a dict of code snippets
Parameters: - ode (gotran.ODE) – The ODE for which code will be generated
- monitored (list) – A list of name of monitored intermediates for which evaluation code will be generated.
- include_init (bool) – If True, code for initializing the states and parameters will be generated.
- include_index_map (bool) – If True, code for mapping a str to a index for the corresponding, state, parameters or monitored will be generated.
- indent (int) – The indentation level for the generated code
-
comment
= '%'¶
-
indent
= 2¶
-
indent_str
= ' '¶
-
index
(i)¶
-
language
= 'Matlab'¶
-
line_cont
= '...'¶
-
line_ending
= ';'¶
-
to_code
(expr, name)¶
-
-
class
gotran.codegeneration.codegenerators.
CUDACodeGenerator
(params=None)[source]¶ Bases:
gotran.codegeneration.codegenerators.CCodeGenerator
-
function_code
(comp, indent=0, default_arguments=None, include_signature=True, return_body_lines=False)[source]¶
-
language
= 'CUDA'¶
-
-
class
gotran.codegeneration.codegenerators.
JuliaCodeGenerator
(params=None)[source]¶ Bases:
gotran.codegeneration.codegenerators.BaseCodeGenerator
A Julia Code generator
-
closure_end
= 'end'¶
-
closure_start
= ''¶
-
comment
= '#'¶
-
function_code
(comp, indent=0, include_signature=True)[source]¶ Generate code for a single function given by a CodeComponent
-
indent
= 4¶
-
indent_str
= ' '¶
-
index
(i)¶
-
language
= 'Julia'¶
-
line_cont
= ''¶
-
line_ending
= ''¶
-
monitor_name_to_index_code
(ode, monitored, indent=0)[source]¶ Return code for index handling for monitored
-
to_code
(expr, name)¶
-
gotran.codegeneration.compilemodule module¶
-
gotran.codegeneration.compilemodule.
compile_module
(ode, language='C', monitored=None, generation_params=None, additional_declarations=None, jacobian_declaration_template=None)[source]¶ JIT compile an ode
Parameters: - ode (gotran.ODE, str) – The gotran ode
- language (str (optional)) – The language of the generated code Defaults : ‘C’ [‘C’, ‘Python’]
- monitored (list) – A list of names of intermediates of the ODE. Code for monitoring the intermediates will be generated.
- generation_params (dict) – Parameters controling the code generation
gotran.codegeneration.latexcodegenerator module¶
-
class
gotran.codegeneration.latexcodegenerator.
LatexCodeGenerator
(ode, params=None)[source]¶ Bases:
object
-
format_component_label
(label)[source]¶ Return a LaTeX-formatted string of an ODE component group label.
-
format_description
(description, name)[source]¶ If auto-format flag is set, attempt to automatically format description, setting references to states and parameters in math mode.
-
format_expr
(expr)[source]¶ Return a LaTeX-formatted string for a sympy expression. E.g.: >>> LatexCodeGenerator.format_expr(“exp(i*pi) + 1”) ‘e^{i pi} + 1’
-
format_global_options
(option_template, params=None)[source]¶ Inject additional options into the global option template
-
format_options
(exclude=None, override=None, params=None)[source]¶ Wrap options around a LaTeX string, excluding specified elements. If override is not empty, only those elements will be used, ignoring exclude.
-
format_packages
(package_list)[source]¶ Return list of packages and options as a LaTeX-formatted string. Assumes package list is on the form ((“package1”, “[options1]”), …).
-
format_param_table_row
(param)[source]¶ Return a LaTeX-formatted string for a longtable row describing a parameter. E.g.: >>> LatexCodeGenerator.format_param_table_row( … Parameter(“g_earth”, … ScalarParam(9.81, unit=”m/s**2”, … description=”Surface gravity”)) ‘ $g_{earth}$hspace{0.5cm} & $9.81 mathrm{frac{m}{s^{2}}}$ hspace{0.5cm} & Surface gravity’
-
format_state_table_row
(state)[source]¶ Return a LaTeX-formatted string for a longtable row describing a state’s initial value. E.g.: >>> LatexCodeGenerator.format_state_table_row(“amu”, … “Atomic mass unit”, 931.46, “MeV/c**2”) ‘ $amu$hspace{0.5cm} & $931.46 mathrm{frac{MeV}{c^{2}}}$ hspace{0.5cm} & Atomic mass unit’
-
format_unit
(unit)[source]¶ Return sympified and LaTeX-formatted string describing given unit. E.g.: >>> LatexCodeGenerator.format_unit(“m/s**2”) ‘mathrm{frac{m}{s^{2}}}’
-
generate_components
(params=None)[source]¶ Return a LaTeX-formatted string of the ODE’s derivative components and intermediate calculations.
-
generate_parameter_table
(params=None)[source]¶ Return a LaTeX-formatted string for a longtable describing the ODE’s parameters.
-
generate_state_table
(params=None)[source]¶ Return a LaTeX-formatted string for a longtable describing the ode’s states.
-
packages
= [('fullpage', ''), ('longtable,tabu', ''), ('multicol', ''), ('amsmath', ''), ('mathpazo', ''), ('flexisym', '[mathpazo]'), ('breqn', '')]¶
-
print_settings
= {}¶
-
gotran.codegeneration.oderepresentation module¶
-
class
gotran.codegeneration.oderepresentation.
ODERepresentation
(ode, name='', **optimization)[source]¶ Bases:
object
Intermediate ODE representation where various optimizations can be performed.
-
class_name
¶
-
iter_dy_body
()[source]¶ Return an interator over dy_body lines
If using intermediates it will define these, if using cse extraction these will be returned
-
name
¶
-
symbol_subs
¶ Return a subs dict for all ODE Objects (states, parameters)
-
used_in_linear_dy
¶
-
used_in_monitoring
¶
-
used_in_single_dy
¶
-
gotran.codegeneration.solvercomponents module¶
-
class
gotran.codegeneration.solvercomponents.
ExplicitEuler
(ode, function_name='forward_explicit_euler', params=None)[source]¶ Bases:
gotran.codegeneration.codecomponent.CodeComponent
An ODEComponent which compute one step of the explicit Euler algorithm
-
gotran.codegeneration.solvercomponents.
explicit_euler_solver
(ode, function_name='forward_explicit_euler', params=None)[source]¶ Return an ODEComponent holding expressions for the explicit Euler method
Parameters: - ode (gotran.ODE) – The ODE for which the jacobian expressions should be computed
- function_name (str) – The name of the function which should be generated
- params (dict) – Parameters determining how the code should be generated
-
class
gotran.codegeneration.solvercomponents.
RushLarsen
(ode, function_name='forward_rush_larsen', delta=1e-08, params=None)[source]¶ Bases:
gotran.codegeneration.codecomponent.CodeComponent
An ODEComponent which compute one step of the Rush Larsen algorithm
-
gotran.codegeneration.solvercomponents.
rush_larsen_solver
(ode, function_name='forward_rush_larsen', delta=1e-08, params=None)[source]¶ Return an ODEComponent holding expressions for the Rush Larsen method
Parameters: - ode (gotran.ODE) – The ODE for which the jacobian expressions should be computed
- function_name (str) – The name of the function which should be generated
- delta (float) – Value to safeguard the evaluation of the rush larsen step.
- params (dict) – Parameters determining how the code should be generated
-
class
gotran.codegeneration.solvercomponents.
GeneralizedRushLarsen
(ode, function_name='forward_generalized_rush_larsen', delta=1e-08, params=None)[source]¶ Bases:
gotran.codegeneration.codecomponent.CodeComponent
An ODEComponent which compute one step of the Rush Larsen algorithm
-
gotran.codegeneration.solvercomponents.
generalized_rush_larsen_solver
(ode, function_name='forward_generalized_rush_larsen', delta=1e-08, params=None)[source]¶ Return an ODEComponent holding expressions for the generalized Rush Larsen method
Parameters: - ode (gotran.ODE) – The ODE for which the jacobian expressions should be computed
- function_name (str) – The name of the function which should be generated
- delta (float) – Value to safeguard the evaluation of the rush larsen step.
- params (dict) – Parameters determining how the code should be generated
-
class
gotran.codegeneration.solvercomponents.
SimplifiedImplicitEuler
(ode, function_name='forward_simplified_implicit_euler', numeric_jacobian=False, params=None)[source]¶ Bases:
gotran.codegeneration.codecomponent.CodeComponent
An ODEComponent which compute one step of a simplified Implicit Euler algorithm
-
gotran.codegeneration.solvercomponents.
simplified_implicit_euler_solver
(ode, function_name='forward_simplified_implicit_euler', numeric_jacobian=False, params=None)[source]¶ Return an ODEComponent holding expressions for the simplified implicit Euler method
Parameters: - ode (gotran.ODE) – The ODE for which the jacobian expressions should be computed
- function_name (str) – The name of the function which should be generated
- numeric_jacobian (bool) – If True use numeric calculated diagonal jacobian.
- params (dict) – Parameters determining how the code should be generated
gotran.codegeneration.sympy_cse module¶
Tools for doing common subexpression elimination.
-
gotran.codegeneration.sympy_cse.
cse
(exprs, symbols=None, optimizations=None, postprocess=None, order='canonical')[source]¶ Perform common subexpression elimination on an expression.
Parameters: - exprs (list of sympy expressions, or a single sympy expression) – The expressions to reduce.
- symbols (infinite iterator yielding unique Symbols) – The symbols used to label the common subexpressions which are pulled
out. The
numbered_symbols
generator is useful. The default is a stream of symbols of the form “x0”, “x1”, etc. This must be an infinite iterator. - optimizations (list of (callable, callable) pairs) – The (preprocessor, postprocessor) pairs of external optimization functions. Optionally ‘basic’ can be passed for a set of predefined basic optimizations. Such ‘basic’ optimizations were used by default in old implementation, however they can be really slow on larger expressions. Now, no pre or post optimizations are made by default.
- postprocess (a function which accepts the two return values of cse and) – returns the desired form of output from cse, e.g. if you want the replacements reversed the function might be the following lambda: lambda r, e: return reversed(r), e
- order (string, 'none' or 'canonical') – The order by which Mul and Add arguments are processed. If set to ‘canonical’, arguments will be canonically ordered. If set to ‘none’, ordering will be faster but dependent on expressions hashes, thus machine dependent and variable. For large expressions where speed is a concern, use the setting order=’none’.
Returns: - replacements (list of (Symbol, expression) pairs) – All of the common subexpressions that were replaced. Subexpressions earlier in this list might show up in subexpressions later in this list.
- reduced_exprs (list of sympy expressions) – The reduced expressions with all of the replacements above.
-
gotran.codegeneration.sympy_cse.
cse_separate
(r, e)[source]¶ Move expressions that are in the form (symbol, expr) out of the expressions and sort them into the replacements using the reps_toposort.
Examples
>>> from sympy.simplify.cse_main import cse_separate >>> from sympy.abc import x, y, z >>> from sympy import cos, exp, cse, Eq, symbols >>> x0, x1 = symbols('x:2') >>> eq = (x + 1 + exp((x + 1)/(y + 1)) + cos(y + 1)) >>> cse([eq, Eq(x, z + 1), z - 2], postprocess=cse_separate) in [ ... [[(x0, y + 1), (x, z + 1), (x1, x + 1)], ... [x1 + exp(x1/x0) + cos(x0), z - 2]], ... [[(x1, y + 1), (x, z + 1), (x0, x + 1)], ... [x0 + exp(x0/x1) + cos(x1), z - 2]]] ... True
-
gotran.codegeneration.sympy_cse.
opt_cse
(exprs, order='canonical')[source]¶ Find optimization opportunities in Adds, Muls, Pows and negative coefficient Muls
Parameters: - exprs (list of sympy expressions) – The expressions to optimize.
- order (string, 'none' or 'canonical') – The order by which Mul and Add arguments are processed. For large expressions where speed is a concern, use the setting order=’none’.
Returns: opt_subs – The expression substitutions which can be useful to optimize CSE.
Return type: dictionary of expression substitutions
Examples
>>> from sympy.simplify.cse_main import opt_cse >>> from sympy.abc import x >>> opt_subs = opt_cse([x**-2]) >>> print(opt_subs) {x**(-2): 1/(x**2)}
-
gotran.codegeneration.sympy_cse.
postprocess_for_cse
(expr, optimizations)[source]¶ Postprocess an expression after common subexpression elimination to return the expression to canonical sympy form.
Parameters: - expr (sympy expression) – The target expression to transform.
- optimizations (list of (callable, callable) pairs, optional) – The (preprocessor, postprocessor) pairs. The postprocessors will be applied in reversed order to undo the effects of the preprocessors correctly.
Returns: expr – The transformed expression.
Return type: sympy expression
-
gotran.codegeneration.sympy_cse.
preprocess_for_cse
(expr, optimizations)[source]¶ Preprocess an expression to optimize for common subexpression elimination.
Parameters: - expr (sympy expression) – The target expression to optimize.
- optimizations (list of (callable, callable) pairs) – The (preprocessor, postprocessor) pairs.
Returns: expr – The transformed expression.
Return type: sympy expression
-
gotran.codegeneration.sympy_cse.
reps_toposort
(r)[source]¶ Sort replacements r so (k1, v1) appears before (k2, v2) if k2 is in v1’s free symbols. This orders items in the way that cse returns its results (hence, in order to use the replacements in a substitution option it would make sense to reverse the order).
Examples
>>> from sympy.simplify.cse_main import reps_toposort >>> from sympy.abc import x, y >>> from sympy import Eq >>> for l, r in reps_toposort([(x, y + 1), (y, 2)]): ... print(Eq(l, r)) ... y == 2 x == y + 1
-
gotran.codegeneration.sympy_cse.
tree_cse
(exprs, symbols, opt_subs=None, order='canonical')[source]¶ Perform raw CSE on expression tree, taking opt_subs into account.
Parameters: - exprs (list of sympy expressions) – The expressions to reduce.
- symbols (infinite iterator yielding unique Symbols) – The symbols used to label the common subexpressions which are pulled out.
- opt_subs (dictionary of expression substitutions) – The expressions to be substituted before any CSE action is performed.
- order (string, 'none' or 'canonical') – The order by which Mul and Add arguments are processed. For large expressions where speed is a concern, use the setting order=’none’.
Module contents¶
-
class
gotran.codegeneration.
CodeComponent
(name, ode, function_name, description, params=None, use_default_arguments=True, additional_arguments=None, **results)[source]¶ Bases:
gotran.model.odecomponent.ODEComponent
A wrapper class around an ODE. Its primary purpose is to help generate code.
The class alows extraction and manipulation of the ODE expressions.
-
add_indexed_expression
(basename, indices, expr, add_offset=False, dependent=None)[source]¶ Add an indexed expression using a basename and the indices
Parameters: - basename (str) – The basename of the indexed expression
- indices (int, tuple of int) – The fixed indices identifying the expression
- expr (sympy.Basic, scalar) – The expression.
- add_offset (bool) – Add offset to indices
- dependent (gotran.ODEObject) – If given the count of this expression will follow as a fractional count based on the count of the dependent object
-
-
class
gotran.codegeneration.
JacobianComponent
(ode, function_name='compute_jacobian', result_name='jac', params=None)[source]¶ Bases:
gotran.codegeneration.codecomponent.CodeComponent
An ODEComponent which keeps all expressions for the Jacobian of the rhs
-
class
gotran.codegeneration.
JacobianActionComponent
(jacobian, with_body=True, function_name='compute_jacobian_action', result_name='jac_action', params=None)[source]¶ Bases:
gotran.codegeneration.codecomponent.CodeComponent
Jacobian action component which returns the expressions for Jac*x
-
class
gotran.codegeneration.
FactorizedJacobianComponent
(jacobian, function_name='lu_factorize', params=None)[source]¶ Bases:
gotran.codegeneration.codecomponent.CodeComponent
Class to generate expressions for symbolicaly factorizing a jacobian
-
class
gotran.codegeneration.
ForwardBackwardSubstitutionComponent
(factorized, function_name='forward_backward_subst', result_name='dx', residual_name='F', params=None)[source]¶ Bases:
gotran.codegeneration.codecomponent.CodeComponent
Class to generate a forward backward substiution algorithm for symbolically factorized jacobian
-
class
gotran.codegeneration.
LinearizedDerivativeComponent
(ode, function_name='linear_derivatives', result_names=['linearized', 'rhs'], only_linear=True, include_rhs=False, nonlinear_last=False, params=None)[source]¶ Bases:
gotran.codegeneration.codecomponent.CodeComponent
A component for all linear and linearized derivatives
-
class
gotran.codegeneration.
CommonSubExpressionODE
(ode)[source]¶ Bases:
gotran.model.ode.ODE
Class which flattens the component structue of an ODE to just one. It uses common sub expressions as intermediates to reduce complexity of the derivative expressions.
-
gotran.codegeneration.
componentwise_derivative
(ode, indices, params=None, result_name='dy')[source]¶ Return an ODEComponent holding the expressions for the ith state derivative
Parameters: - ode (gotran.ODE) – The finalized ODE for which the ith derivative should be computed
- indices (int, list of ints) – The index
- params (dict) – Parameters determining how the code should be generated
- result_name (str) – The name of the result variable
-
gotran.codegeneration.
linearized_derivatives
(ode, function_name='linear_derivatives', result_names=['linearized', 'dy'], only_linear=True, include_rhs=False, nonlinear_last=False, params=None)[source]¶ Return an ODEComponent holding the linearized derivative expressions
Parameters: - ode (gotran.ODE) – The ODE for which derivatives should be linearized
- function_name (str) – The name of the function which should be generated
- result_names (str) – The name of the variable storing the linearized derivatives and the rhs evaluation if that is included.
- only_linear (bool) – If True, only linear terms will be linearized
- include_rhs (bool) – If True, rhs evaluation will be included in the generated code.
- nonlinear_last (bool) – If True the nonlinear expressions are added last after a comment
- params (dict) – Parameters determining how the code should be generated
-
gotran.codegeneration.
jacobian_expressions
(ode, function_name='compute_jacobian', result_name='jac', params=None)[source]¶ Return an ODEComponent holding expressions for the jacobian
Parameters: - ode (gotran.ODE) – The ODE for which the jacobian expressions should be computed
- function_name (str) – The name of the function which should be generated
- result_name (str) – The name of the variable storing the jacobian result
- params (dict) – Parameters determining how the code should be generated
-
gotran.codegeneration.
jacobian_action_expressions
(jacobian, with_body=True, function_name='compute_jacobian_action', result_name='jac_action', params=None)[source]¶ Return an ODEComponent holding expressions for the jacobian action
Parameters: - jacobian (gotran.JacobianComponent) – The ODEComponent holding expressions for the jacobian
- with_body (bool) – If true, the body for computing the jacobian will be included
- function_name (str) – The name of the function which should be generated
- result_name (str) – The name of the variable storing the jacobian diagonal result
- params (dict) – Parameters determining how the code should be generated
-
gotran.codegeneration.
factorized_jacobian_expressions
(jacobian, function_name='lu_factorize', params=None)[source]¶ Return an ODEComponent holding expressions for the factorized jacobian
Parameters: - jacobian (gotran.JacobianComponent) – The ODEComponent holding expressions for the jacobian
- params (dict) – Parameters determining how the code should be generated
-
gotran.codegeneration.
forward_backward_subst_expressions
(factorized, function_name='forward_backward_subst', result_name='dx', residual_name='F', params=None)[source]¶ Return an ODEComponent holding expressions for the forward backward substitions for a factorized jacobian
Parameters: - factorized (gotran.FactorizedJacobianComponent) – The ODEComponent holding expressions for the factorized jacobian
- function_name (str) – The name of the function which should be generated
- result_name (str) – The name of the result (increment)
- residual_name (str) – The name of the residual
- params (dict) – Parameters determining how the code should be generated
-
gotran.codegeneration.
diagonal_jacobian_expressions
(jacobian, function_name='compute_diagonal_jacobian', result_name='diag_jac', params=None)[source]¶ Return an ODEComponent holding expressions for the diagonal jacobian
Parameters: - jacobian (gotran.JacobianComponent) – The Jacobian of the ODE
- function_name (str) – The name of the function which should be generated
- result_name (str) – The name of the variable storing the jacobian diagonal result
- params (dict) – Parameters determining how the code should be generated
-
gotran.codegeneration.
rhs_expressions
(ode, function_name='rhs', result_name='dy', params=None)[source]¶ Return a code component with body expressions for the right hand side
Parameters: - ode (gotran.ODE) – The finalized ODE
- function_name (str) – The name of the function which should be generated
- result_name (str) – The name of the variable storing the rhs result
- params (dict) – Parameters determining how the code should be generated
-
gotran.codegeneration.
diagonal_jacobian_action_expressions
(diagonal_jacobian, with_body=True, function_name='compute_diagonal_jacobian_action', result_name='diag_jac_action', params=None)[source]¶ Return an ODEComponent holding expressions for the diagonal jacobian action
Parameters: - diagonal_jacobian (gotran.DiagonalJacobianComponent) – The ODEComponent holding expressions for the diagonal jacobian
- with_body (bool) – If true, the body for computing the jacobian will be included
- function_name (str) – The name of the function which should be generated
- result_name (str) – The name of the variable storing the jacobian diagonal result
- params (dict) – Parameters determining how the code should be generated
-
gotran.codegeneration.
monitored_expressions
(ode, monitored, function_name='monitored_expressions', result_name='monitored', params=None)[source]¶ Return a code component with body expressions to calculate monitored expressions
Parameters: - ode (gotran.ODE) – The finalized ODE for which the monitored expression should be computed
- monitored (tuple, list) – A tuple/list of strings containing the name of the monitored expressions
- function_name (str) – The name of the function which should be generated
- result_name (str) – The name of the variable storing the rhs result
- params (dict) – Parameters determining how the code should be generated
-
class
gotran.codegeneration.
JacobianComponent
(ode, function_name='compute_jacobian', result_name='jac', params=None)[source] Bases:
gotran.codegeneration.codecomponent.CodeComponent
An ODEComponent which keeps all expressions for the Jacobian of the rhs
-
class
gotran.codegeneration.
JacobianActionComponent
(jacobian, with_body=True, function_name='compute_jacobian_action', result_name='jac_action', params=None)[source] Bases:
gotran.codegeneration.codecomponent.CodeComponent
Jacobian action component which returns the expressions for Jac*x
-
class
gotran.codegeneration.
FactorizedJacobianComponent
(jacobian, function_name='lu_factorize', params=None)[source] Bases:
gotran.codegeneration.codecomponent.CodeComponent
Class to generate expressions for symbolicaly factorizing a jacobian
-
class
gotran.codegeneration.
ForwardBackwardSubstitutionComponent
(factorized, function_name='forward_backward_subst', result_name='dx', residual_name='F', params=None)[source] Bases:
gotran.codegeneration.codecomponent.CodeComponent
Class to generate a forward backward substiution algorithm for symbolically factorized jacobian
-
class
gotran.codegeneration.
LinearizedDerivativeComponent
(ode, function_name='linear_derivatives', result_names=['linearized', 'rhs'], only_linear=True, include_rhs=False, nonlinear_last=False, params=None)[source] Bases:
gotran.codegeneration.codecomponent.CodeComponent
A component for all linear and linearized derivatives
-
class
gotran.codegeneration.
CommonSubExpressionODE
(ode)[source] Bases:
gotran.model.ode.ODE
Class which flattens the component structue of an ODE to just one. It uses common sub expressions as intermediates to reduce complexity of the derivative expressions.
-
gotran.codegeneration.
componentwise_derivative
(ode, indices, params=None, result_name='dy')[source] Return an ODEComponent holding the expressions for the ith state derivative
Parameters: - ode (gotran.ODE) – The finalized ODE for which the ith derivative should be computed
- indices (int, list of ints) – The index
- params (dict) – Parameters determining how the code should be generated
- result_name (str) – The name of the result variable
-
gotran.codegeneration.
linearized_derivatives
(ode, function_name='linear_derivatives', result_names=['linearized', 'dy'], only_linear=True, include_rhs=False, nonlinear_last=False, params=None)[source] Return an ODEComponent holding the linearized derivative expressions
Parameters: - ode (gotran.ODE) – The ODE for which derivatives should be linearized
- function_name (str) – The name of the function which should be generated
- result_names (str) – The name of the variable storing the linearized derivatives and the rhs evaluation if that is included.
- only_linear (bool) – If True, only linear terms will be linearized
- include_rhs (bool) – If True, rhs evaluation will be included in the generated code.
- nonlinear_last (bool) – If True the nonlinear expressions are added last after a comment
- params (dict) – Parameters determining how the code should be generated
-
gotran.codegeneration.
jacobian_expressions
(ode, function_name='compute_jacobian', result_name='jac', params=None)[source] Return an ODEComponent holding expressions for the jacobian
Parameters: - ode (gotran.ODE) – The ODE for which the jacobian expressions should be computed
- function_name (str) – The name of the function which should be generated
- result_name (str) – The name of the variable storing the jacobian result
- params (dict) – Parameters determining how the code should be generated
-
gotran.codegeneration.
jacobian_action_expressions
(jacobian, with_body=True, function_name='compute_jacobian_action', result_name='jac_action', params=None)[source] Return an ODEComponent holding expressions for the jacobian action
Parameters: - jacobian (gotran.JacobianComponent) – The ODEComponent holding expressions for the jacobian
- with_body (bool) – If true, the body for computing the jacobian will be included
- function_name (str) – The name of the function which should be generated
- result_name (str) – The name of the variable storing the jacobian diagonal result
- params (dict) – Parameters determining how the code should be generated
-
gotran.codegeneration.
factorized_jacobian_expressions
(jacobian, function_name='lu_factorize', params=None)[source] Return an ODEComponent holding expressions for the factorized jacobian
Parameters: - jacobian (gotran.JacobianComponent) – The ODEComponent holding expressions for the jacobian
- params (dict) – Parameters determining how the code should be generated
-
gotran.codegeneration.
forward_backward_subst_expressions
(factorized, function_name='forward_backward_subst', result_name='dx', residual_name='F', params=None)[source] Return an ODEComponent holding expressions for the forward backward substitions for a factorized jacobian
Parameters: - factorized (gotran.FactorizedJacobianComponent) – The ODEComponent holding expressions for the factorized jacobian
- function_name (str) – The name of the function which should be generated
- result_name (str) – The name of the result (increment)
- residual_name (str) – The name of the residual
- params (dict) – Parameters determining how the code should be generated
-
gotran.codegeneration.
diagonal_jacobian_expressions
(jacobian, function_name='compute_diagonal_jacobian', result_name='diag_jac', params=None)[source] Return an ODEComponent holding expressions for the diagonal jacobian
Parameters: - jacobian (gotran.JacobianComponent) – The Jacobian of the ODE
- function_name (str) – The name of the function which should be generated
- result_name (str) – The name of the variable storing the jacobian diagonal result
- params (dict) – Parameters determining how the code should be generated
-
gotran.codegeneration.
rhs_expressions
(ode, function_name='rhs', result_name='dy', params=None)[source] Return a code component with body expressions for the right hand side
Parameters: - ode (gotran.ODE) – The finalized ODE
- function_name (str) – The name of the function which should be generated
- result_name (str) – The name of the variable storing the rhs result
- params (dict) – Parameters determining how the code should be generated
-
gotran.codegeneration.
diagonal_jacobian_action_expressions
(diagonal_jacobian, with_body=True, function_name='compute_diagonal_jacobian_action', result_name='diag_jac_action', params=None)[source] Return an ODEComponent holding expressions for the diagonal jacobian action
Parameters: - diagonal_jacobian (gotran.DiagonalJacobianComponent) – The ODEComponent holding expressions for the diagonal jacobian
- with_body (bool) – If true, the body for computing the jacobian will be included
- function_name (str) – The name of the function which should be generated
- result_name (str) – The name of the variable storing the jacobian diagonal result
- params (dict) – Parameters determining how the code should be generated
-
gotran.codegeneration.
monitored_expressions
(ode, monitored, function_name='monitored_expressions', result_name='monitored', params=None)[source] Return a code component with body expressions to calculate monitored expressions
Parameters: - ode (gotran.ODE) – The finalized ODE for which the monitored expression should be computed
- monitored (tuple, list) – A tuple/list of strings containing the name of the monitored expressions
- function_name (str) – The name of the function which should be generated
- result_name (str) – The name of the variable storing the rhs result
- params (dict) – Parameters determining how the code should be generated
-
class
gotran.codegeneration.
PythonCodeGenerator
(params=None, ns='math')[source]¶ Bases:
gotran.codegeneration.codegenerators.BaseCodeGenerator
-
float_types
= {'double': 'float_', 'single': 'float32'}¶
-
function_code
(comp, indent=0, include_signature=True)[source]¶ Generate code for a single function given by a CodeComponent
-
language
= 'python'¶
-
monitor_name_to_index_code
(ode, monitored, indent=0)[source]¶ Return code for index handling for monitored
-
to_code
(expr, name)¶
-
-
class
gotran.codegeneration.
CCodeGenerator
(params=None)[source]¶ Bases:
gotran.codegeneration.codegenerators.BaseCodeGenerator
-
closure_end
= '}'¶
-
closure_start
= '{'¶
-
comment
= '//'¶
-
componentwise_code
(ode, indent=0, default_arguments=None, include_signature=True, return_body_lines=False)[source]¶
-
float_types
= {'double': 'double', 'single': 'float'}¶
-
function_code
(comp, indent=0, default_arguments=None, include_signature=True, return_body_lines=False)[source]¶
-
indent
= 2¶
-
indent_str
= ' '¶
-
index
(i)¶
-
language
= 'C'¶
-
line_cont
= ''¶
-
line_ending
= ';'¶
-
monitor_name_to_index_code
(ode, monitored, indent=0)[source]¶ Return code for index handling for monitored
-
to_code
(expr, name)¶
-
-
class
gotran.codegeneration.
CppCodeGenerator
(params=None)[source]¶ Bases:
gotran.codegeneration.codegenerators.CCodeGenerator
-
language
= 'C++'¶
-
to_code
(expr, name)¶
-
-
class
gotran.codegeneration.
MatlabCodeGenerator
(params=None)[source]¶ Bases:
gotran.codegeneration.codegenerators.BaseCodeGenerator
A Matlab Code generator
-
closure_end
= 'end'¶
-
closure_start
= ''¶
-
code_dict
(ode, monitored=None, include_init=True)[source]¶ Generates a dict of code snippets
Parameters: - ode (gotran.ODE) – The ODE for which code will be generated
- monitored (list) – A list of name of monitored intermediates for which evaluation code will be generated.
- include_init (bool) – If True, code for initializing the states and parameters will be generated.
- include_index_map (bool) – If True, code for mapping a str to a index for the corresponding, state, parameters or monitored will be generated.
- indent (int) – The indentation level for the generated code
-
comment
= '%'¶
-
indent
= 2¶
-
indent_str
= ' '¶
-
index
(i)¶
-
language
= 'Matlab'¶
-
line_cont
= '...'¶
-
line_ending
= ';'¶
-
to_code
(expr, name)¶
-
-
class
gotran.codegeneration.
CUDACodeGenerator
(params=None)[source]¶ Bases:
gotran.codegeneration.codegenerators.CCodeGenerator
-
function_code
(comp, indent=0, default_arguments=None, include_signature=True, return_body_lines=False)[source]¶
-
language
= 'CUDA'¶
-
-
class
gotran.codegeneration.
JuliaCodeGenerator
(params=None)[source]¶ Bases:
gotran.codegeneration.codegenerators.BaseCodeGenerator
A Julia Code generator
-
closure_end
= 'end'¶
-
closure_start
= ''¶
-
comment
= '#'¶
-
function_code
(comp, indent=0, include_signature=True)[source]¶ Generate code for a single function given by a CodeComponent
-
indent
= 4¶
-
indent_str
= ' '¶
-
index
(i)¶
-
language
= 'Julia'¶
-
line_cont
= ''¶
-
line_ending
= ''¶
-
monitor_name_to_index_code
(ode, monitored, indent=0)[source]¶ Return code for index handling for monitored
-
to_code
(expr, name)¶
-
-
class
gotran.codegeneration.
LatexCodeGenerator
(ode, params=None)[source]¶ Bases:
object
-
format_component_label
(label)[source]¶ Return a LaTeX-formatted string of an ODE component group label.
-
format_description
(description, name)[source]¶ If auto-format flag is set, attempt to automatically format description, setting references to states and parameters in math mode.
-
format_expr
(expr)[source]¶ Return a LaTeX-formatted string for a sympy expression. E.g.: >>> LatexCodeGenerator.format_expr(“exp(i*pi) + 1”) ‘e^{i pi} + 1’
-
format_global_options
(option_template, params=None)[source]¶ Inject additional options into the global option template
-
format_options
(exclude=None, override=None, params=None)[source]¶ Wrap options around a LaTeX string, excluding specified elements. If override is not empty, only those elements will be used, ignoring exclude.
-
format_packages
(package_list)[source]¶ Return list of packages and options as a LaTeX-formatted string. Assumes package list is on the form ((“package1”, “[options1]”), …).
-
format_param_table_row
(param)[source]¶ Return a LaTeX-formatted string for a longtable row describing a parameter. E.g.: >>> LatexCodeGenerator.format_param_table_row( … Parameter(“g_earth”, … ScalarParam(9.81, unit=”m/s**2”, … description=”Surface gravity”)) ‘ $g_{earth}$hspace{0.5cm} & $9.81 mathrm{frac{m}{s^{2}}}$ hspace{0.5cm} & Surface gravity’
-
format_state_table_row
(state)[source]¶ Return a LaTeX-formatted string for a longtable row describing a state’s initial value. E.g.: >>> LatexCodeGenerator.format_state_table_row(“amu”, … “Atomic mass unit”, 931.46, “MeV/c**2”) ‘ $amu$hspace{0.5cm} & $931.46 mathrm{frac{MeV}{c^{2}}}$ hspace{0.5cm} & Atomic mass unit’
-
format_unit
(unit)[source]¶ Return sympified and LaTeX-formatted string describing given unit. E.g.: >>> LatexCodeGenerator.format_unit(“m/s**2”) ‘mathrm{frac{m}{s^{2}}}’
-
generate_components
(params=None)[source]¶ Return a LaTeX-formatted string of the ODE’s derivative components and intermediate calculations.
-
generate_parameter_table
(params=None)[source]¶ Return a LaTeX-formatted string for a longtable describing the ODE’s parameters.
-
generate_state_table
(params=None)[source]¶ Return a LaTeX-formatted string for a longtable describing the ode’s states.
-
packages
= [('fullpage', ''), ('longtable,tabu', ''), ('multicol', ''), ('amsmath', ''), ('mathpazo', ''), ('flexisym', '[mathpazo]'), ('breqn', '')]¶
-
print_settings
= {}¶
-