DOF (Degree-of-Freedom) Arrays

class meshmode.dof_array.DOFArray(actx, data)

This array type holds degree-of-freedom arrays for use with Discretization, with one entry in the DOFArray for each ElementGroupBase. The arrays contained within a DOFArray are expected to be logically two-dimensional, with shape (nelements, ndofs_per_element), where nelements is the same as nelements of the associated group. ndofs_per_element is typically, but not necessarily, the same as nunit_dofs of the associated group. The entries in this array are further arrays managed by array_context.

One main purpose of this class is to describe the data structure, i.e. when a DOFArray occurs inside of further numpy object array, the level representing the array of element groups can be recognized (by people and programs).


An meshmode.array_context.ArrayContext.


The (assumed uniform) numpy.dtype of the group arrays contained in this instance.


The following methods and attributes are implemented to mimic the functionality of ndarrays. They require the DOFArray to be thaw()ed.


This object supports arithmetic, comparisons, and logic operators.


DOFArray instances support elementwise <, >, <=, >=. (numpy object arrays containing arrays do not.)

Basic in-place operations are also supported. Note that not all array types provided by meshmode.array_context.ArrayContext implementations support in-place operations. Those based on lazy evaluation are a salient example.


arg – can be a Number or another DOFArray.



DOFArray instances can be pickled and unpickled while the context manager array_context_for_pickling is active. If, for an array to be pickled, the ArrayContext given to array_context_for_pickling() does not agree with array_context, the array is frozen and rethawed. If array_context is None, the DOFArray is thaw()ed into the array context given to array_context_for_pickling().

meshmode.dof_array.obj_or_dof_array_vectorize(f, ary)

Works like obj_array_vectorize(), but recurses on object arrays and also tolerates one final ‘layer’ of DOFArrays.

meshmode.dof_array.obj_or_dof_array_vectorize_n_args(f, *args)

Apply the function f elementwise to all entries of any object arrays or DOFArrays in args. All such arrays are expected to have the same shape (but this is not checked). Equivalent to an appropriately-looped execution of:

result[idx] = f(obj_array_arg1[idx], arg2, obj_array_arg3[idx])

Return an array of the same shape as the arguments consisting of the return values of f. If the elements of arrays found in args are further object arrays, recurse. If a DOFArray is found, apply f to its entries. If non-object-arrays are found, apply f to those.

meshmode.dof_array.thaw(actx, ary)

Call thaw() on the element group arrays making up the DOFArray, using actx.

Vectorizes over object arrays of DOFArrays.

Return type



Call freeze() on the element group arrays making up the DOFArray, using the ArrayContext in ary.

Vectorizes over object arrays of DOFArrays.


ary (Union[meshmode.dof_array.DOFArray, numpy.ndarray]) –

Return type



Convert a DOFArray into a “flat” array of degrees of freedom, where the resulting type of the array is given by the DOFArray.array_context.

Array elements are laid out contiguously, with the element group index varying slowest, element index next, and intra-element DOF index fastest.

Vectorizes over object arrays of DOFArrays.


ary (Union[meshmode.dof_array.DOFArray, numpy.ndarray]) –

Return type


meshmode.dof_array.unflatten(actx, discr, ary, ndofs_per_element_per_group=None)

Convert a ‘flat’ array returned by flatten() back to a DOFArray.

  • ndofs_per_element – Optional. If given, an iterable of numbers representing the number of degrees of freedom per element, overriding the numbers provided by the element groups in discr. May be used (for example) to handle DOFArrays that have only one DOF per element, representing some per-element quantity.

  • actx (meshmode.array_context.ArrayContext) –

  • ary (Union[Any, numpy.ndarray]) –

  • ndofs_per_element_per_group (Optional[Iterable[int]]) –

Return type


Vectorizes over object arrays.


For the current thread, set the array context to be used for pickling and unpickling DOFArrays to actx.

New in version 2021.x.


actx (meshmode.array_context.ArrayContext) –

Array Contexts

meshmode.array_context.make_loopy_program(domains, statements, kernel_data=None, name='mm_actx_kernel')

Return a loopy.LoopKernel suitable for use with ArrayContext.call_loopy().

class meshmode.array_context.CommonSubexpressionTag

A tag that is applicable to arrays indicating that this same array may be evaluated multiple times, and that the implementation should eliminate those redundant evaluations if possible.

New in version 2021.2.

class meshmode.array_context.ArrayContext

An interface that allows a Discretization to create and interact with arrays of degrees of freedom without fully specifying their types.

New in version 2020.2.

abstract empty(shape, dtype)
abstract zeros(shape, dtype)
abstract from_numpy(array)

the numpy.ndarray array converted to the array context’s array type. The returned array will be thaw()ed.


array (numpy.ndarray) –

abstract to_numpy(array)

array, an array recognized by the context, converted to a numpy.ndarray. array must be thaw()ed.

call_loopy(program, **kwargs)

Execute the loopy program program on the arguments kwargs.

program is a loopy.LoopKernel or loopy.LoopKernel. It is expected to not yet be transformed for execution speed. It must have loopy.Options.return_dict set.


a dict of outputs from the program, each an array understood by the context.


Provides access to a namespace that serves as a work-alike to numpy. The actual level of functionality provided is up to the individual array context implementation, however the functions and objects available under this namespace must not behave differently from numpy.

As a baseline, special functions available through loopy (e.g. sin, exp) are accessible through this interface.

Callables accessible through this namespace vectorize over object arrays, including meshmode.dof_array.DOFArray.

abstract freeze(array)

Return a version of the context-defined array array that is ‘frozen’, i.e. suitable for long-term storage and reuse. Frozen arrays do not support arithmetic. For example, in the context of Array, this might mean stripping the array of an associated command queue, whereas in a lazily-evaluated context, it might mean that the array is evaluated and stored.

Freezing makes the array independent of this ArrayContext; it is permitted to thaw() it in a different one, as long as that context understands the array format.

abstract thaw(array)

Take a ‘frozen’ array and return a new array representing the data in array that is able to perform arithmetic and other operations, using the execution resources of this context. In the context of Array, this might mean that the array is equipped with a command queue, whereas in a lazily-evaluated context, it might mean that the returned array is a symbol bound to the data in array.

The returned array may not be used with other contexts while thawed.

abstract tag(tags, array)

If the array type used by the array context is capable of capturing metadata, return a version of array with the tags applied. array itself is not modified.

New in version 2021.2.


tags (Union[Sequence[pytools.tag.Tag], pytools.tag.Tag]) –

abstract tag_axis(iaxis, tags, array)

If the array type used by the array context is capable of capturing metadata, return a version of array in which axis number iaxis has the tags applied. array itself is not modified.

New in version 2021.2.


tags (Union[Sequence[pytools.tag.Tag], pytools.tag.Tag]) –

class meshmode.array_context.PyOpenCLArrayContext(queue, allocator=None, wait_event_queue_length=None)

A ArrayContext that uses pyopencl.array.Array instances for DOF arrays.


A pyopencl.Context.


A pyopencl.CommandQueue.


A PyOpenCL memory allocator. Can also be None (default) or False to use the default allocator. Please note that running with the default allocator allocates and deallocates OpenCL buffers directly. If lots of arrays are created (e.g. as results of computation), the associated cost may become significant. Using e.g. as the allocator can help avoid this cost.


Parametrize tests for pytest to use a pyopencl array context.

Performs device enumeration analogously to

Using the line:

from meshmode.array_context import pytest_generate_tests_for_pyopencl             as pytest_generate_tests

in your pytest test scripts allows you to use the arguments ctx_factory, device, or platform in your test functions, and they will automatically be run for each OpenCL device/platform in the system, as appropriate.

It also allows you to specify the PYOPENCL_TEST environment variable for device selection.