Loading...
Searching...
No Matches
Expression.h
// Copyright (C) 2020-2025 Jack S. Hale, Michal Habera and Garth N. Wells
//
// This file is part of DOLFINx (https://www.fenicsproject.org)
//
// SPDX-License-Identifier: LGPL-3.0-or-later
#pragma once
#include "Constant.h"
#include "Function.h"
#include <algorithm>
#include <array>
#include <concepts>
#include <dolfinx/common/types.h>
#include <dolfinx/mesh/Mesh.h>
#include <functional>
#include <memory>
#include <span>
#include <utility>
#include <vector>
namespace dolfinx::fem
{
template <dolfinx::scalar T, std::floating_point U>
class Function;
template <dolfinx::scalar T, std::floating_point U = dolfinx::scalar_value_t<T>>
class Expression
{
public:
using scalar_type = T;
using geometry_type = U;
Expression(const std::vector<std::shared_ptr<
const Function<scalar_type, geometry_type>>>& coefficients,
const std::vector<std::shared_ptr<const Constant<scalar_type>>>&
constants,
std::span<const geometry_type> X,
std::array<std::size_t, 2> Xshape,
std::function<void(scalar_type*, const scalar_type*,
const scalar_type*, const geometry_type*,
const int*, const uint8_t*, void*)>
fn,
const std::vector<std::size_t>& value_shape,
std::shared_ptr<const FunctionSpace<geometry_type>> argument_space
= nullptr)
: _argument_space(argument_space), _coefficients(coefficients),
_constants(constants), _fn(fn), _value_shape(value_shape),
_x_ref(std::vector<geometry_type>(X.begin(), X.end()), Xshape)
{
for (auto& c : _coefficients)
{
assert(c);
if (c->function_space()->mesh()
!= _coefficients.front()->function_space()->mesh())
{
throw std::runtime_error("Coefficients not all defined on same mesh.");
}
}
}
Expression(Expression&& e) = default;
virtual ~Expression() = default;
std::shared_ptr<const FunctionSpace<geometry_type>> argument_space() const
{
return _argument_space;
};
const std::vector<
std::shared_ptr<const Function<scalar_type, geometry_type>>>&
coefficients() const
{
return _coefficients;
}
const std::vector<std::shared_ptr<const Constant<scalar_type>>>&
constants() const
{
return _constants;
}
std::vector<int> coefficient_offsets() const
{
std::vector<int> n{0};
for (auto& c : _coefficients)
{
if (!c)
throw std::runtime_error("Not all form coefficients have been set.");
n.push_back(n.back() + c->function_space()->element()->space_dimension());
}
return n;
}
const std::function<void(scalar_type*, const scalar_type*, const scalar_type*,
const geometry_type*, const int*, const uint8_t*,
void*)>&
kernel() const
{
return _fn;
}
int value_size() const
{
return std::reduce(_value_shape.begin(), _value_shape.end(), 1,
std::multiplies{});
}
const std::vector<std::size_t>& value_shape() const { return _value_shape; }
std::pair<std::vector<geometry_type>, std::array<std::size_t, 2>> X() const
{
return _x_ref;
}
private:
// Function space for Argument
std::shared_ptr<const FunctionSpace<geometry_type>> _argument_space;
// Coefficients associated with the Expression
std::vector<std::shared_ptr<const Function<scalar_type, geometry_type>>>
_coefficients;
// Constants associated with the Expression
std::vector<std::shared_ptr<const Constant<scalar_type>>> _constants;
// Function to evaluate the Expression
std::function<void(scalar_type*, const scalar_type*, const scalar_type*,
const geometry_type*, const int*, const uint8_t*, void*)>
_fn;
// Shape of the evaluated Expression
std::vector<std::size_t> _value_shape;
// Evaluation points on reference cell
std::pair<std::vector<geometry_type>, std::array<std::size_t, 2>> _x_ref;
};
} // namespace dolfinx::fem
dolfinx::fem::Constant
Constant (in space) value which can be attached to a Form.
Definition Constant.h:22
dolfinx::fem::Expression::geometry_type
U geometry_type
Geometry type of the points.
Definition Expression.h:50
dolfinx::fem::Expression::X
std::pair< std::vector< geometry_type >, std::array< std::size_t, 2 > > X() const
Evaluation point coordinates on the reference cell.
Definition Expression.h:166
dolfinx::fem::Expression::coefficients
const std::vector< std::shared_ptr< const Function< scalar_type, geometry_type > > > & coefficients() const
Expression coefficients.
Definition Expression.h:115
dolfinx::fem::Expression::argument_space
std::shared_ptr< const FunctionSpace< geometry_type > > argument_space() const
Argument function space.
Definition Expression.h:106
dolfinx::fem::Expression::kernel
const std::function< void(scalar_type *, const scalar_type *, const scalar_type *, const geometry_type *, const int *, const uint8_t *, void *)> & kernel() const
Function for tabulating the Expression.
Definition Expression.h:150
dolfinx::fem::Expression::constants
const std::vector< std::shared_ptr< const Constant< scalar_type > > > & constants() const
Expression constants.
Definition Expression.h:124
dolfinx::fem::Expression::coefficient_offsets
std::vector< int > coefficient_offsets() const
Offset for each coefficient expansion array on a cell.
Definition Expression.h:134
dolfinx::fem::Expression::value_size
int value_size() const
Value size of the Expression result.
Definition Expression.h:156
dolfinx::fem::Expression::Expression
Expression(Expression &&e)=default
Move constructor.
dolfinx::fem::Expression::Expression
Expression(const std::vector< std::shared_ptr< const Function< scalar_type, geometry_type > > > &coefficients, const std::vector< std::shared_ptr< const Constant< scalar_type > > > &constants, std::span< const geometry_type > X, std::array< std::size_t, 2 > Xshape, std::function< void(scalar_type *, const scalar_type *, const scalar_type *, const geometry_type *, const int *, const uint8_t *, void *)> fn, const std::vector< std::size_t > &value_shape, std::shared_ptr< const FunctionSpace< geometry_type > > argument_space=nullptr)
Create an Expression.
Definition Expression.h:69
dolfinx::fem::Expression::~Expression
virtual ~Expression()=default
Destructor.
dolfinx::fem::Expression::scalar_type
T scalar_type
Scalar type.
Definition Expression.h:47
dolfinx::fem::Expression::value_shape
const std::vector< std::size_t > & value_shape() const
Value shape of of Expression result (at a point),.
Definition Expression.h:163
dolfinx::fem::FunctionSpace
This class represents a finite element function space defined by a mesh, a finite element,...
Definition FunctionSpace.h:34
dolfinx::fem::Function
Definition Function.h:47
dolfinx::fem
Finite element method functionality.
Definition assemble_expression_impl.h:23