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)
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)

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=[], **kwargs)

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)

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)

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

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

A (Python) exception type to be raised.


The error message to go along with that exception type.

class dagrt.language.FailStep(**kwargs)

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)

The name of the next state to enter

Miscellaneous Statements
class dagrt.language.Nop(**kwargs)

Code Container

class dagrt.language.ExecutionPhase(name, next_phase, statements)

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)

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)

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

dagrt.language.show_dependency_graph(*args, **kwargs)

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)

The name of the phase being generated


The set of statements generated for the phase

Language support:

assign(assignees, expression, loops=[])

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)
yield_state(expression, component_id, time, time_id)

Yield a value.

raise_(error_condition, error_message=None)

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)

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(

Code generation


Python code generator

class dagrt.codegen.python.CodeGenerator(class_name, class_preamble=None, function_registry=None)



Code Generator

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)

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)

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)

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)

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)

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)
class dagrt.function_registry.FunctionNotFound

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)

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=())

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)

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)

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)

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

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

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)

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)

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)

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)

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)

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)

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)

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)

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


The module provides symbol kind information and kind inference.

Symbol kinds

class, exclude=None, **kwargs)

Base class for kinds encountered in the dagrt language.

class, exclude=None, **kwargs)

A boolean.

class, exclude=None, **kwargs)

An integer.


A (real or complex) floating-point value.


Whether the value is definitely real-valued


A variable-sized one-dimensional scalar array.


Whether the value is definitely real-valued


Represents user state belonging to a normed vector space.


A unique identifier for this type

Symbol kind inference


A mapping from symbol names to kinds for a program.


a mapping from symbol names to SymbolKind instances, for global symbols


a nested mapping [phase_name][symbol_name] to SymbolKind instances

__call__(names, phases)

Infer the kinds of all the symbols in a program.

  • names – a list of phase names

  • phases – a list of iterables, each yielding the statements in a phase


a SymbolKindTable


UnableToInferKind – kind inference could not complete sucessfully

Helper functions, function_registry=None)

Run kind inference on a dagrt.language.DAGCode.


a SymbolKindTable

Collect all of the of UserType identifiers in a table.


skt – a SymbolKindTable


a set of strings



isolate_function_arguments() should be called before this.


Turn IfThenElse expressions into values that are computed as a result of an If statement. This is useful for targets that do not support ternary operators.


Random usefulness

class dagrt.codegen.utils.KeyToUniqueNameMap(start={}, forced_prefix='', key_translate_func=<function make_identifier_from_name>, name_generator=None)

Maps keys to unique names that are created on-the-fly.

Before a unique name is created, a base name is first created. The base name consists of a forced prefix followed by a string that results from translating the key with a supplied function. The mapped value is then created from the base name.


text – a multi-line string


a list of lines

dagrt.codegen.utils.wrap_line_base(line, level=0, width=80, indentation='    ', pad_func=<function <lambda>>, lex_func=functools.partial(<function split>, posix=False))

The input is a line of code at the given indentation level. Return the list of lines that results from wrapping the line to the given width. Lines subsequent to the first line in the returned list are padded with extra indentation. The initial indentation level is not included in the input or output lines.

The pad_func argument is a function that adds line continuations. The lex_func argument returns the list of tokens in the line.

Dumping ground of miscellaneous helpfulness.

exception dagrt.utils.TODO
class dagrt.utils.TemporaryDirectory(suffix='', prefix='tmp', dirname=None)

Create and return a temporary directory. This has the same behavior as mkdtemp but can be used as a context manager. For example:

with TemporaryDirectory() as tmpdir:

Upon exiting the context, the directory and everything contained in it are removed.

dagrt.utils.get_unique_name(prefix, *name_sets)

Return a name that begins with prefix and is not found in any of the name sets.

dagrt.utils.get_variables(expr, include_function_symbols=False)

Returns the set of names of variables used in the expression.


Check if the given name corresponds to a state variable.

dagrt.utils.resolve_args(arg_names, default_dict, arg_dict)

Resolve positional and keyword arguments to a single argument list.

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

  • default_dict – a dictionary mapping argument names to default values

  • arg_names – names of the positional arguments

numpy-based interpretation

exception dagrt.exec_numpy.FailStepException
class dagrt.exec_numpy.TransitionEvent(next_phase)
class dagrt.exec_numpy.NumpyInterpreter(code, function_map)

A numpy-targeting interpreter for the time integration language defined in dagrt.language.

set_up(t_start, dt_start, context)

context – a dictionary mapping identifiers to their values

run(t_end=None, max_steps=None)

Generates events.