// ------------------------------------------------------------------------ // // SPDX-License-Identifier: LGPL-2.1-or-later // Copyright (C) 1998 - 2025 by the deal.II authors // // This file is part of the deal.II library. // // Part of the source code is dual licensed under Apache-2.0 WITH // LLVM-exception OR LGPL-2.1-or-later. Detailed license information // governing the source code and code contributions can be found in // LICENSE.md and CONTRIBUTING.md at the top level directory of deal.II. // // ------------------------------------------------------------------------ #ifndef dealii_function_templates_h #define dealii_function_templates_h #include #include #include #include #include #include DEAL_II_NAMESPACE_OPEN template const unsigned int Function::dimension; template Function::Function( const unsigned int n_components, const typename Function::time_type initial_time) : FunctionTime::time_type>( initial_time) , n_components(n_components) { // avoid the construction of function objects that don't return any // values. This doesn't make much sense in the first place, but will lead // to odd errors later on (happened to me in fact :-) Assert(n_components > 0, ExcZero()); } template Function & Function::operator=(const Function &f) { (void)f; AssertDimension(n_components, f.n_components); return *this; } template RangeNumberType Function::value(const Point &, const unsigned int) const { Assert(false, ExcPureFunctionCalled()); return 0; } template void Function::vector_value(const Point &p, Vector &v) const { AssertDimension(v.size(), this->n_components); for (unsigned int i = 0; i < this->n_components; ++i) v(i) = value(p, i); } template void Function::value_list( const std::vector> &points, std::vector &values, const unsigned int component) const { // check whether component is in the valid range is up to the derived // class Assert(values.size() == points.size(), ExcDimensionMismatch(values.size(), points.size())); for (unsigned int i = 0; i < points.size(); ++i) values[i] = this->value(points[i], component); } template void Function::vector_value_list( const std::vector> &points, std::vector> &values) const { // check whether component is in the valid range is up to the derived // class Assert(values.size() == points.size(), ExcDimensionMismatch(values.size(), points.size())); for (unsigned int i = 0; i < points.size(); ++i) this->vector_value(points[i], values[i]); } template void Function::vector_values( const std::vector> &points, std::vector> &values) const { const unsigned int n = this->n_components; AssertDimension(values.size(), n); for (unsigned int i = 0; i < n; ++i) value_list(points, values[i], i); } template Tensor<1, dim, RangeNumberType> Function::gradient(const Point &, const unsigned int) const { Assert(false, ExcPureFunctionCalled()); return Tensor<1, dim, RangeNumberType>(); } template void Function::vector_gradient( const Point &p, std::vector> &v) const { AssertDimension(v.size(), this->n_components); for (unsigned int i = 0; i < this->n_components; ++i) v[i] = gradient(p, i); } template void Function::gradient_list( const std::vector> &points, std::vector> &gradients, const unsigned int component) const { Assert(gradients.size() == points.size(), ExcDimensionMismatch(gradients.size(), points.size())); for (unsigned int i = 0; i < points.size(); ++i) gradients[i] = gradient(points[i], component); } template void Function::vector_gradient_list( const std::vector> &points, std::vector>> &gradients) const { Assert(gradients.size() == points.size(), ExcDimensionMismatch(gradients.size(), points.size())); for (unsigned int i = 0; i < points.size(); ++i) { Assert(gradients[i].size() == n_components, ExcDimensionMismatch(gradients[i].size(), n_components)); vector_gradient(points[i], gradients[i]); } } template void Function::vector_gradients( const std::vector> &points, std::vector>> &values) const { const unsigned int n = this->n_components; AssertDimension(values.size(), n); for (unsigned int i = 0; i < n; ++i) gradient_list(points, values[i], i); } template RangeNumberType Function::laplacian(const Point &, const unsigned int) const { Assert(false, ExcPureFunctionCalled()); return 0; } template void Function::vector_laplacian( const Point &, Vector &) const { Assert(false, ExcPureFunctionCalled()); } template void Function::laplacian_list( const std::vector> &points, std::vector &laplacians, const unsigned int component) const { // check whether component is in the valid range is up to the derived // class Assert(laplacians.size() == points.size(), ExcDimensionMismatch(laplacians.size(), points.size())); for (unsigned int i = 0; i < points.size(); ++i) laplacians[i] = this->laplacian(points[i], component); } template void Function::vector_laplacian_list( const std::vector> &points, std::vector> &laplacians) const { // check whether component is in the valid range is up to the derived // class Assert(laplacians.size() == points.size(), ExcDimensionMismatch(laplacians.size(), points.size())); for (unsigned int i = 0; i < points.size(); ++i) this->vector_laplacian(points[i], laplacians[i]); } template SymmetricTensor<2, dim, RangeNumberType> Function::hessian(const Point &, const unsigned int) const { Assert(false, ExcPureFunctionCalled()); return SymmetricTensor<2, dim, RangeNumberType>(); } template void Function::vector_hessian( const Point &p, std::vector> &v) const { AssertDimension(v.size(), this->n_components); for (unsigned int i = 0; i < this->n_components; ++i) v[i] = hessian(p, i); } template void Function::hessian_list( const std::vector> &points, std::vector> &hessians, const unsigned int component) const { Assert(hessians.size() == points.size(), ExcDimensionMismatch(hessians.size(), points.size())); for (unsigned int i = 0; i < points.size(); ++i) hessians[i] = hessian(points[i], component); } template void Function::vector_hessian_list( const std::vector> &points, std::vector>> &hessians) const { Assert(hessians.size() == points.size(), ExcDimensionMismatch(hessians.size(), points.size())); for (unsigned int i = 0; i < points.size(); ++i) { Assert(hessians[i].size() == n_components, ExcDimensionMismatch(hessians[i].size(), n_components)); vector_hessian(points[i], hessians[i]); } } template std::size_t Function::memory_consumption() const { // only simple data elements, so use sizeof operator return sizeof(*this); } //--------------------------------------------------------------------------- namespace Functions { template ConstantFunction::ConstantFunction( const RangeNumberType value, const unsigned int n_components) : Function(n_components) , function_value_vector(n_components, value) {} template ConstantFunction::ConstantFunction( const std::vector &values) : Function(values.size()) , function_value_vector(values) {} template ConstantFunction::ConstantFunction( const Vector &values) : Function(values.size()) , function_value_vector(values.size()) { Assert(values.size() == function_value_vector.size(), ExcDimensionMismatch(values.size(), function_value_vector.size())); std::copy(values.begin(), values.end(), function_value_vector.begin()); } template ConstantFunction::ConstantFunction( const RangeNumberType *begin_ptr, const unsigned int n_components) : Function(n_components) , function_value_vector(n_components) { Assert(begin_ptr != nullptr, ExcMessage("Null pointer encountered!")); std::copy(begin_ptr, begin_ptr + n_components, function_value_vector.begin()); } template RangeNumberType ConstantFunction::value( const Point &, const unsigned int component) const { AssertIndexRange(component, this->n_components); return function_value_vector[component]; } template void ConstantFunction::vector_value( const Point &, Vector &return_value) const { Assert(return_value.size() == this->n_components, ExcDimensionMismatch(return_value.size(), this->n_components)); std::copy(function_value_vector.begin(), function_value_vector.end(), return_value.begin()); } template void ConstantFunction::value_list( const std::vector> &points, std::vector &return_values, const unsigned int component) const { // To avoid warning of unused parameter (void)points; AssertIndexRange(component, this->n_components); Assert(return_values.size() == points.size(), ExcDimensionMismatch(return_values.size(), points.size())); std::fill(return_values.begin(), return_values.end(), function_value_vector[component]); } template void ConstantFunction::vector_value_list( const std::vector> &points, std::vector> &return_values) const { Assert(return_values.size() == points.size(), ExcDimensionMismatch(return_values.size(), points.size())); for (unsigned int i = 0; i < points.size(); ++i) { Assert(return_values[i].size() == this->n_components, ExcDimensionMismatch(return_values[i].size(), this->n_components)); std::copy(function_value_vector.begin(), function_value_vector.end(), return_values[i].begin()); }; } template std::size_t ConstantFunction::memory_consumption() const { // Here we assume RangeNumberType is a simple type. return (sizeof(*this) + this->n_components * sizeof(RangeNumberType)); } template Tensor<1, dim, RangeNumberType> ConstantFunction::gradient(const Point &, const unsigned int) const { return Tensor<1, dim, RangeNumberType>(); } template void ConstantFunction::vector_gradient( const Point &, std::vector> &gradients) const { Assert(gradients.size() == this->n_components, ExcDimensionMismatch(gradients.size(), this->n_components)); for (unsigned int c = 0; c < this->n_components; ++c) gradients[c].clear(); } template void ConstantFunction::gradient_list( const std::vector> &points, std::vector> &gradients, const unsigned int /*component*/) const { Assert(gradients.size() == points.size(), ExcDimensionMismatch(gradients.size(), points.size())); for (unsigned int i = 0; i < points.size(); ++i) gradients[i].clear(); } template void ConstantFunction::vector_gradient_list( const std::vector> &points, std::vector>> &gradients) const { Assert(gradients.size() == points.size(), ExcDimensionMismatch(gradients.size(), points.size())); for (unsigned int i = 0; i < points.size(); ++i) { Assert(gradients[i].size() == this->n_components, ExcDimensionMismatch(gradients[i].size(), this->n_components)); for (unsigned int c = 0; c < this->n_components; ++c) gradients[i][c].clear(); }; } template SymmetricTensor<2, dim, RangeNumberType> ConstantFunction::hessian(const Point &, const unsigned int) const { return SymmetricTensor<2, dim, RangeNumberType>(); } template RangeNumberType ConstantFunction::laplacian(const Point &, const unsigned int) const { return 0; } template ZeroFunction::ZeroFunction( const unsigned int n_components) : ConstantFunction(RangeNumberType(), n_components) {} template IdentityFunction::IdentityFunction() : Function(dim) {} template RangeNumberType IdentityFunction::value( const Point &p, const unsigned int component) const { AssertIndexRange(component, this->n_components); return p[component]; } template Tensor<1, dim, RangeNumberType> IdentityFunction::gradient( const Point &, const unsigned int component) const { AssertIndexRange(component, this->n_components); Tensor<1, dim, RangeNumberType> result; result[component] = RangeNumberType(1); return result; } template SymmetricTensor<2, dim, RangeNumberType> IdentityFunction::hessian(const Point &, const unsigned int) const { return SymmetricTensor<2, dim, RangeNumberType>(); } template RangeNumberType IdentityFunction::laplacian(const Point &, const unsigned int) const { return 0; } } // namespace Functions //--------------------------------------------------------------------------- template ComponentSelectFunction::ComponentSelectFunction( const unsigned int selected, const RangeNumberType value, const unsigned int n_components) : Functions::ConstantFunction(value, n_components) , selected_components(std::make_pair(selected, selected + 1)) {} template ComponentSelectFunction::ComponentSelectFunction( const unsigned int selected, const unsigned int n_components) : Functions::ConstantFunction(1., n_components) , selected_components(std::make_pair(selected, selected + 1)) { AssertIndexRange(selected, n_components); } template ComponentSelectFunction::ComponentSelectFunction( const std::pair &selected, const unsigned int n_components) : Functions::ConstantFunction(1., n_components) , selected_components(selected) { Assert(selected_components.first < selected_components.second, ExcMessage("The upper bound of the interval must be larger than " "the lower bound")); Assert(selected_components.second <= n_components, ExcMessage("The upper bound of the interval must be less than " "or equal to the total number of vector components")); } template void ComponentSelectFunction::substitute_function_value_with( const Functions::ConstantFunction &f) { Point p; for (unsigned int i = 0; i < this->function_value_vector.size(); ++i) this->function_value_vector[i] = f.value(p, i); } template void ComponentSelectFunction::vector_value( const Point &, Vector &return_value) const { Assert(return_value.size() == this->n_components, ExcDimensionMismatch(return_value.size(), this->n_components)); return_value = 0; std::copy(this->function_value_vector.begin() + selected_components.first, this->function_value_vector.begin() + selected_components.second, return_value.begin() + selected_components.first); } template void ComponentSelectFunction::vector_value_list( const std::vector> &points, std::vector> &values) const { Assert(values.size() == points.size(), ExcDimensionMismatch(values.size(), points.size())); for (unsigned int i = 0; i < points.size(); ++i) ComponentSelectFunction::vector_value(points[i], values[i]); } template std::size_t ComponentSelectFunction::memory_consumption() const { // No new complex data structure is introduced here, just evaluate how much // more memory is used *inside* the class via sizeof() and add that value to // parent class's memory_consumption() return ( sizeof(*this) - sizeof(Functions::ConstantFunction) + Functions::ConstantFunction::memory_consumption()); } //--------------------------------------------------------------------------- template ScalarFunctionFromFunctionObject:: ScalarFunctionFromFunctionObject( const std::function &)> &fu) : ScalarFunctionFromFunctionObject( [fu](const double t, const Point &x) { (void)t; // we got a function object that only takes 'x', so ignore 't' return fu(x); }) {} template ScalarFunctionFromFunctionObject:: ScalarFunctionFromFunctionObject( const std::function &)> &function_object) : Function(1) , function_object(function_object) {} template RangeNumberType ScalarFunctionFromFunctionObject::value( const Point &p, const unsigned int component) const { (void)component; Assert(component == 0, ExcMessage("This object represents only scalar functions")); return function_object(this->get_time(), p); } template VectorFunctionFromScalarFunctionObject:: VectorFunctionFromScalarFunctionObject( const std::function &)> &function_object, const unsigned int selected_component, const unsigned int n_components) : Function(n_components) , function_object(function_object) , selected_component(selected_component) { AssertIndexRange(selected_component, this->n_components); } template RangeNumberType VectorFunctionFromScalarFunctionObject::value( const Point &p, const unsigned int component) const { AssertIndexRange(component, this->n_components); if (component == selected_component) return function_object(p); else return 0; } template void VectorFunctionFromScalarFunctionObject::vector_value( const Point &p, Vector &values) const { AssertDimension(values.size(), this->n_components); // set everything to zero, and then the right component to its correct // value values = 0; values(selected_component) = function_object(p); } /** * The constructor for VectorFunctionFromTensorFunction which * initiates the return vector to be size n_components. */ template VectorFunctionFromTensorFunction:: VectorFunctionFromTensorFunction( const TensorFunction<1, dim, RangeNumberType> &tensor_function, const unsigned int selected_component, const unsigned int n_components) : Function(n_components) , tensor_function(tensor_function) , selected_component(selected_component) { // Verify that the Tensor<1,dim,RangeNumberType> will fit in the given length // selected_components and not hang over the end of the vector. AssertIndexRange(selected_component + dim - 1, this->n_components); } template inline RangeNumberType VectorFunctionFromTensorFunction::value( const Point &p, const unsigned int component) const { AssertIndexRange(component, this->n_components); // if the requested component is out of the range selected, then we can // return early if ((component < selected_component) || (component >= selected_component + dim)) return 0; // otherwise retrieve the values from the tensor_function to be // placed at the selected_component to // selected_component + dim - 1 elements of the Vector // values and pick the correct one const Tensor<1, dim, RangeNumberType> tensor_value = tensor_function.value(p); return tensor_value[component - selected_component]; } template inline void VectorFunctionFromTensorFunction::vector_value( const Point &p, Vector &values) const { Assert(values.size() == this->n_components, ExcDimensionMismatch(values.size(), this->n_components)); // Retrieve the values from the tensor_function to be placed at // the selected_component to // selected_component + dim - 1 elements of the Vector // values. const Tensor<1, dim, RangeNumberType> tensor_value = tensor_function.value(p); // First we make all elements of values = 0 values = 0; // Second we adjust the desired components to take on the values in // tensor_value. for (unsigned int i = 0; i < dim; ++i) values(i + selected_component) = tensor_value[i]; } /** * Member function vector_value_list is the interface for giving a * list of points (vector >) of which to evaluate * using the vector_value member function. Again, this function is * written so as to not replicate the function definition but passes each * point on to vector_value to be evaluated. */ template void VectorFunctionFromTensorFunction::vector_value_list( const std::vector> &points, std::vector> &value_list) const { Assert(value_list.size() == points.size(), ExcDimensionMismatch(value_list.size(), points.size())); const unsigned int n_points = points.size(); for (unsigned int p = 0; p < n_points; ++p) VectorFunctionFromTensorFunction::vector_value( points[p], value_list[p]); } template inline Tensor<1, dim, RangeNumberType> VectorFunctionFromTensorFunction::gradient( const Point &p, const unsigned int component) const { AssertIndexRange(component, this->n_components); // if the requested component is out of the range selected, then we can // return early if ((component < selected_component) || (component >= selected_component + dim)) return Tensor<1, dim>(); // // otherwise retrieve the values from the tensor_function to be // // placed at the selected_component to // // selected_component + dim - 1 elements of the Vector // // values and pick the correct one const Tensor<2, dim, RangeNumberType> tensor_gradient = tensor_function.gradient(p); return tensor_gradient[component - selected_component]; } template void VectorFunctionFromTensorFunction::vector_gradient( const Point &p, std::vector> &gradients) const { AssertDimension(gradients.size(), this->n_components); for (unsigned int i = 0; i < this->n_components; ++i) gradients[i] = gradient(p, i); } template void VectorFunctionFromTensorFunction::gradient_list( const std::vector> &points, std::vector> &gradients, const unsigned int component) const { Assert(gradients.size() == points.size(), ExcDimensionMismatch(gradients.size(), points.size())); for (unsigned int i = 0; i < points.size(); ++i) gradients[i] = gradient(points[i], component); } template void VectorFunctionFromTensorFunction::vector_gradient_list( const std::vector> &points, std::vector>> &gradients) const { Assert(gradients.size() == points.size(), ExcDimensionMismatch(gradients.size(), points.size())); for (unsigned int i = 0; i < points.size(); ++i) { Assert(gradients[i].size() == this->n_components, ExcDimensionMismatch(gradients[i].size(), this->n_components)); vector_gradient(points[i], gradients[i]); } } template void VectorFunctionFromTensorFunction::vector_gradients( const std::vector> &points, std::vector>> &gradients) const { const unsigned int n = this->n_components; AssertDimension(gradients.size(), n); for (unsigned int i = 0; i < n; ++i) gradient_list(points, gradients[i], i); } template FunctionFromFunctionObjects::FunctionFromFunctionObjects( const unsigned int n_components, const double initial_time) : Function(n_components, initial_time) {} template FunctionFromFunctionObjects::FunctionFromFunctionObjects( const std::vector &)>> &values, const double initial_time) : Function(values.size(), initial_time) { this->set_function_values(values); } template FunctionFromFunctionObjects::FunctionFromFunctionObjects( const std::function &, const unsigned int)> &values, const unsigned int n_components, const double initial_time) : Function(n_components, initial_time) , function_values(values) {} template FunctionFromFunctionObjects::FunctionFromFunctionObjects( const std::vector &)>> &values, const std::vector< std::function(const Point &)>> &gradients, const double initial_time) : Function(values.size(), initial_time) { this->set_function_values(values); this->set_function_gradients(gradients); } template RangeNumberType FunctionFromFunctionObjects::value( const Point &p, const unsigned int component) const { AssertIndexRange(component, this->n_components); Assert(function_values, ExcMessage("Accessing value() in FunctionFromFunctionObjects requires " "setting the std::function objects for the value")); return function_values(p, component); } template Tensor<1, dim, RangeNumberType> FunctionFromFunctionObjects::gradient( const Point &p, const unsigned int component) const { AssertIndexRange(component, this->n_components); Assert(function_gradients, ExcMessage( "Accessing gradient() in FunctionFromFunctionObjects " "requires setting the std::function objects for the gradient")); return function_gradients(p, component); } template void FunctionFromFunctionObjects::set_function_values( const std::vector &)>> &values) { AssertDimension(this->n_components, values.size()); function_values = [values](const auto &p, const auto c) { AssertIndexRange(c, values.size()); return values[c](p); }; } template void FunctionFromFunctionObjects::set_function_gradients( const std::vector< std::function(const Point &)>> &gradients) { AssertDimension(this->n_components, gradients.size()); function_gradients = [gradients](const auto &p, const auto c) { AssertIndexRange(c, gradients.size()); return gradients[c](p); }; } template VectorFunctionFromTensorFunctionObject:: VectorFunctionFromTensorFunctionObject( const std::function(const Point &)> &tensor_function_object, const unsigned int selected_component, const unsigned int n_components) : Function(n_components) , tensor_function_object(tensor_function_object) , selected_component(selected_component) { // Verify that the Tensor<1,dim,RangeNumberType> will fit in the given length // selected_components and not hang over the end of the vector. AssertIndexRange(selected_component + dim - 1, this->n_components); } template inline RangeNumberType VectorFunctionFromTensorFunctionObject::value( const Point &p, const unsigned int component) const { AssertIndexRange(component, this->n_components); // if the requested component is out of the range selected, then we can // return early if ((component < selected_component) || (component >= selected_component + dim)) return 0; // otherwise retrieve the values from the tensor_function to be // placed at the selected_component to // selected_component + dim - 1 elements of the Vector // values and pick the correct one const Tensor<1, dim, RangeNumberType> tensor_value = tensor_function_object(p); return tensor_value[component - selected_component]; } template inline void VectorFunctionFromTensorFunctionObject::vector_value( const Point &p, Vector &values) const { Assert(values.size() == this->n_components, ExcDimensionMismatch(values.size(), this->n_components)); // Retrieve the values from the tensor_function to be placed at // the selected_component to // selected_component + dim - 1 elements of the Vector // values. const Tensor<1, dim, RangeNumberType> tensor_value = tensor_function_object(p); // First we make all elements of values = 0 values = 0; // Second we adjust the desired components to take on the values in // tensor_value. for (unsigned int i = 0; i < dim; ++i) values(i + selected_component) = tensor_value[i]; } /** * Member function vector_value_list is the interface for giving a * list of points (vector >) of which to evaluate * using the vector_value member function. Again, this function is * written so as to not replicate the function definition but passes each * point on to vector_value to be evaluated. */ template void VectorFunctionFromTensorFunctionObject::vector_value_list( const std::vector> &points, std::vector> &value_list) const { Assert(value_list.size() == points.size(), ExcDimensionMismatch(value_list.size(), points.size())); const unsigned int n_points = points.size(); for (unsigned int p = 0; p < n_points; ++p) VectorFunctionFromTensorFunctionObject::vector_value( points[p], value_list[p]); } DEAL_II_NAMESPACE_CLOSE #endif /* dealii_function_templates_h */