db::ComputeTag Struct Reference

Marks a DataBoxTag as being a compute item that executes a function. More...

#include <Tag.hpp>

Detailed Description

Marks a DataBoxTag as being a compute item that executes a function.


Compute items come in two forms: mutating and non-mutating. Mutating compute items modify a stored value in order to reduce the number of memory allocations done. For example, if a function would return a Variables or Tensor<DataVector...> and is called every time step, then it would be preferable to use a mutating compute item so that the values in the already allocated memory can just be changed. In contrast, non-mutating compute items simply return the new value after a call (if the value is out-of-date), which is fine for infrequently called compute items or ones that do not allocate data on the heap.

A compute item tag contains a member named function that is either a function pointer, or a static constexpr function. The compute item tag must also have a label, same as the DataBox tags, and a type alias argument_tags that is a typelist of the tags that will be retrieved from the DataBox and whose data will be passed to the function (pointer). Mutating compute item tags must also contain a type alias named return_type that is the type the function is mutating. The type must be default constructible.


Most non-mutating compute item tags will look similar to:

struct Tag4Compute : Tag4, db::ComputeTag {
using base = Tag4;
using return_type = double;
static constexpr auto function = multiply_by_two;
using argument_tags = tmpl::list<Tag0>;
struct Tag5 : db::SimpleTag {
using type = std::string;

Note that the arguments can be empty:

struct Lambda1Compute : Lambda1, db::ComputeTag {
using base = Lambda1;
using return_type = double;
static constexpr void function(const gsl::not_null<double*> result) {
*result = 7.0;
using argument_tags = tmpl::list<>;

Mutating compute item tags are of the form:

struct MutateVariablesCompute : MutateVariables, db::ComputeTag {
using base = MutateVariables;
static constexpr auto function = mutate_variables;
using return_type = Variables<
tmpl::list<test_databox_tags::ScalarTag, test_databox_tags::VectorTag>>;
using argument_tags = tmpl::list<Tag0>;

where the function is:

void mutate_variables(
const gsl::not_null<Variables<tmpl::list<test_databox_tags::ScalarTag,
const double value) {
if (t->number_of_grid_points() != 10) {
*t = Variables<
tmpl::list<test_databox_tags::ScalarTag, test_databox_tags::VectorTag>>(
10, 0.0);
for (auto& p : get<test_databox_tags::ScalarTag>(*t)) {
p = 2.0 * value;
for (auto& p : get<test_databox_tags::VectorTag>(*t)) {
p = 3.0 * value;

You can also have function be a function instead of a function pointer, which offers a lot of simplicity for very simple compute items.

struct Lambda0Compute : Lambda0, db::ComputeTag {
using base = Lambda0;
using return_type = double;
static constexpr void function(const gsl::not_null<double*> result,
const double a) {
*result = 3.0 * a;
using argument_tags = tmpl::list<Tag0>;
See also
DataBox SimpleTag tag_name PrefixTag

The documentation for this struct was generated from the following file:
Marks a DataBoxTag as being a compute item that executes a function.
Definition: Tag.hpp:109
Tags for the DataBox inherit from this type.
Definition: Tag.hpp:23
Require a pointer to not be a nullptr
Definition: Gsl.hpp:183