Description language

Identifier conventions

Identifiers whose names start with the pattern <letters> are special. The following special variable names are supported:

Internal names


This variable contains persistent state. (that survives from one step to the next)


This variable is used by a conditional. May not be re-defined.


The time increment for the present time step.

Its value at the beginning of a step indicates the step size to be used. If a time step of this size cannot be completed, FailStep must be issued.

This variable contains persistent state. (that survives from one step to the next)


Base time of current time step.

The integrator code is responsible for incrementing <t> at the end of a successful step.

This variable contains persistent state. (that survives from one step to the next)

User-controlled values


State identifier under user (=scheme writer) control

This variable contains persistent state. (that survives from one step to the next)




For targets that are incapable of returning state mid-step, these variables are used to store computed state.

See dagrt.function_registry for interpretation of function names. The function namespace and the variable namespace are distinct. No user-defined identifiers should start with dagrt_.


class dagrt.language.Statement(**kwargs)[source]

Assignment Statements

These statements perform updates to the execution state, i.e. the variables.

class dagrt.language.AssignImplicit(assignees, solve_variables, expressions, other_params, solver_id, **kwargs)[source]

A tuple of strings. The names of the variables to assign with the results of solving for solve_variables


A tuple of strings, the names of the variables being solved for


A tuple of expressions, which represent the left hand sides of a system of (possibly nonlinear) equations. The solver will attempt to find a simultaneous root of the system.


A dictionary used to pass extra arguments to the solver, for instance a starting guess


An identifier for the solver that is to be used to solve the system. This identifier is intended to match information about solvers which is supplied by the user.

class dagrt.language.Assign(assignee=None, assignee_subscript=None, expression=None, loops=None, **kwargs)[source]

A list of triples (identifier, start, end) that the assignment should be carried out inside of these loops. No ordering of loop iterations is implied. The loops will typically be nested outer-to-inner, but a target may validly use any order it likes.

class dagrt.language.AssignFunctionCall(assignees, function_id, parameters, kw_parameters=None, **kwargs)[source]

This statement encodes function calls. It should be noted that function calls can also be encoded as expressions involving calls, however the existence of this separate statement is justified by two facts:

  • Some backends (such as Fortran) choose to separate out function calls into individual statements. This statement provides a natural way to encode them.

    See dagrt.codegen.transform.isolate_function_calls for a transform that accomplishes this.

  • Calling functions with multiple return values is not supported as part of dagrt’s language.


A tuple of variables to be passed assigned results from calling function_id.


A list of expressions to be passed as positional arguments.


A dictionary mapping names to expressions to be passed as keyword arguments.

Control Statements

These statements affect the execution of a phase, or cause a phase to interact with user code.

class dagrt.language.YieldState(**kwargs)[source]

A pymbolic expression representing the time at which the state returned is valid.

class dagrt.language.Raise(error_condition, error_message=None, **kwargs)[source]

A (Python) exception type to be raised.


The error message to go along with that exception type.

class dagrt.language.FailStep(**kwargs)[source]

Exits the current step with a failure indication to the controlling program. Execution resumes with the next step as normal.

class dagrt.language.SwitchPhase(next_phase, **kwargs)[source]

The name of the next state to enter

Miscellaneous Statements

class dagrt.language.Nop(**kwargs)[source]

Code Container

class dagrt.language.ExecutionPhase(name, next_phase, statements)[source]

name of this phase


a list of statement IDs that need to be executed to complete a successful execution of one round of this phase, excluding their dependencies.


name of the next state after this one, if no other state is specified by the user.


is a list of statement instances in no particular order.

class dagrt.language.DAGCode(phases, initial_phase)[source]

is a map from time integrator phase names to ExecutionPhase instances


the name of the starting phase


dagrt.language.get_dot_dependency_graph(code, use_stmt_ids=False)[source]

Return a string in the dot language depicting dependencies among kernel statements.

dagrt.language.show_dependency_graph(*args, **kwargs)[source]

Show the dependency graph generated by get_dot_dependency_graph() in a browser. Accepts the same arguments as that function.

Code Creation

class dagrt.language.CodeBuilder(name)[source]

The name of the phase being generated


The set of statements generated for the phase

Language support:

assign(assignees, expression, loops=None)[source]

Generate code for an assignment.

assignees may be a variable, a subscript (if referring to an array), or a tuple of variables. There must be exactly one assignee unless expression is a function call.

loops is a list of tuples of the form (identifier, start_index, stop_index).

assign_implicit(assignees, solve_components, expressions, other_params, solver_id)[source]
yield_state(expression, component_id, time, time_id)[source]

Yield a value.

raise_(error_condition, error_message=None)[source]

Control flow:


Create a new block that is conditionally executed.


Create the “else” portion of a conditionally executed block.

Context manager:


Graph generation:


Return the generated graph as an ExecutionPhase.


next_phase – The name of the default next phase

Convenience functions:


Alias for CodeBuilder.assign().

assign_implicit_1(assignee, solve_component, expression, guess, solver_id=None)[source]

Special case of AssignImplicit when there is 1 component to solve for.


Return a variable name that is not in use also and won’t be returned in the future, regardless of use.


Equivalent to switch_phase(

Function registry

The function registry is used by targets to resolve external functions and invoke user-specified code, including but not limited to ODE right-hand sides.

class dagrt.function_registry.Function(language_to_codegen=None, **kwargs)[source]

A list of names of the return values of the function. Note that these names serve as documentation and as identifiers to be used for variables receiving function results inside generated code implementing the call to the function (e.g. in the Fortran backend). They have no relationship to the names to which the results ultimately get assigned.


The name of the function.


The names of the arguments to the function.


A dictionary mapping argument names to default values.

get_result_kinds(arg_kinds, check)[source]

Return a tuple of the instances for the values this function returns if arguments of the kinds arg_kinds are supplied.

The length of the returned tuple must match the lenght of result_names.

  • arg_kinds – a dictionary mapping numbers (for positional arguments) or identifiers (for keyword arguments) to instances indicating the types of the arguments being passed to the function. Some elements of arg_kinds may be None if their kinds have yet not been determined.

  • check – A bool. If True, none of arg_kinds will be None, and argument kind checking should be performed.

register_codegen(language, codegen_function)[source]

Return a copy of self with codegen_function registered as a code generator for language.

The interface for codegen_function depends on the code generator being used.


Return the code generator for language.


Resolve positional and keyword arguments to an argument list.

See also dagrt.utils.resolve_args().


arg_dict – a dictionary mapping numbers (for positional arguments) or identifiers (for keyword arguments) to values

class dagrt.function_registry.FunctionRegistry(id_to_function=None)[source]

Return a copy of self with function registered.


Return the Function with identifier function_id.


FunctionNotFound – when function_id was not found

register_codegen(function_id, language, codegen)[source]

Register a code generation helper object for target language for the function with identifier function_id.


codegen – an object obeying an interface suitable for code generation for language. This interface depends on the code generator being used.

get_codegen(function_id, language)[source]
class dagrt.function_registry.FunctionNotFound[source]

The default function registry, containing all the built-in functions (see Built-ins).

Registering new functions

dagrt.function_registry.register_ode_rhs(function_registry, output_type_id, identifier=None, input_type_ids=None, input_names=None)[source]

Register a function as an ODE right-hand side.

Functions registered through this call have the following characteristics. First, there is a single return value of the user type whose type identifier is output_type_id. Second, the function has as its first argument a scalar named t. Last, the remaining argument list to the function consists of user type values.

For example, considering the ODE \(y' = f(t, y)\), the following call registers a right-hand side function with name f and user type y:

freg = register_ode_rhs(freg, "y", identifier="f")
  • function_registry – the base function registry

  • output_type_id – a string, the user type ID returned by the call.

  • identifier – the full name of the function. If not provided, defaults to <func> + output_type_id.

  • input_type_ids – a tuple of strings, the identifiers of the user types which are the arguments to the right-hand side function. An automatically added t argument occurs before these arguments. If not provided, defaults to (output_type_id,).

  • input_names – a tuple of strings, the names of the inputs. If not provided, defaults to input_type_ids.


a new FunctionRegistry

dagrt.function_registry.register_function(function_registry, identifier, arg_names, default_dict=None, result_names=(), result_kinds=())[source]

Register a function returning output(s) of fixed kind.

  • function_registry – the base FunctionRegistry

  • identifier – a string, the function identifier

  • arg_names – a list of strings, the names of the arguments

  • default_dict – a dictionary mapping argument names to default values

  • result_names – a list of strings, the names of the output(s)

  • result_kinds – a list of, the kinds of the output(s)


a new FunctionRegistry


The built-in functions are listed below. This also serves as their language documentation.

class dagrt.function_registry.Norm1(language_to_codegen=None, **kwargs)[source]

norm_1(x) returns the 1-norm of x. x is a user type or array.

class dagrt.function_registry.Norm2(language_to_codegen=None, **kwargs)[source]

norm_2(x) returns the 2-norm of x. x is a user type or array.

class dagrt.function_registry.NormInf(language_to_codegen=None, **kwargs)[source]

norm_inf(x) returns the infinity-norm of x. x is a user type or array.

class dagrt.function_registry.ElementwiseAbs(language_to_codegen=None, **kwargs)[source]

elementwise_abs(x) takes the elementwise absolute value of x. x is a user type, array, or scalar.

class dagrt.function_registry.DotProduct(language_to_codegen=None, **kwargs)[source]

dot_product(x, y) return the dot product of x and y. The complex conjugate of x is taken first, if applicable. x and y are either arrays (that must be of the same length) or the same user type.

class dagrt.function_registry.Len(language_to_codegen=None, **kwargs)[source]

len(x) returns the number of degrees of freedom in x. x is a user type or array.

class dagrt.function_registry.IsNaN(language_to_codegen=None, **kwargs)[source]

isnan(x) returns True if and only if there are any NaNs in x. x is a user type, scalar, or array.

class dagrt.function_registry.Array_(language_to_codegen=None, **kwargs)[source]

array(n) returns an empty array with n entries in it. n must be an integer.

class dagrt.function_registry.MatMul(language_to_codegen=None, **kwargs)[source]

matmul(a, b, a_cols, b_cols) returns a 1D array containing the matrix resulting from multiplying the arrays a and b (both interpreted as matrices, with a number of columns a_cols and b_cols respectively).

class dagrt.function_registry.Transpose(language_to_codegen=None, **kwargs)[source]

transpose(a, a_cols) returns a 1D array containing the matrix resulting from transposing the array a (interpreted as a matrix with a_cols columns).

class dagrt.function_registry.LinearSolve(language_to_codegen=None, **kwargs)[source]

linear_solve(a, b, a_cols, b_cols) returns a 1D array containing the matrix resulting from multiplying the matrix inverse of a by b, both interpreted as matrices, with a number of columns a_cols and b_cols respectively.

class dagrt.function_registry.SVD(language_to_codegen=None, **kwargs)[source]

SVD(a, a_cols) returns a 2D array u, a 1D array sigma, and a 2D array vt, representing the (reduced) SVD of a.

class dagrt.function_registry.Print(language_to_codegen=None, **kwargs)[source]

print(arg) prints the given operand to standard output. Returns an integer that may be ignored.