libMesh
Loading...
Searching...
No Matches
wrapped_functor.h
Go to the documentation of this file.
1// The libMesh Finite Element Library.
2// Copyright (C) 2002-2026 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner
3
4// This library is free software; you can redistribute it and/or
5// modify it under the terms of the GNU Lesser General Public
6// License as published by the Free Software Foundation; either
7// version 2.1 of the License, or (at your option) any later version.
8
9// This library is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12// Lesser General Public License for more details.
13
14// You should have received a copy of the GNU Lesser General Public
15// License along with this library; if not, write to the Free Software
16// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
18
19
20#ifndef LIBMESH_WRAPPED_FUNCTOR_H
21#define LIBMESH_WRAPPED_FUNCTOR_H
22
23// Local Includes
24#include "libmesh/fem_function_base.h"
25#include "libmesh/function_base.h"
26#include "libmesh/int_range.h"
27#include "libmesh/point.h"
28#include "libmesh/wrapped_function.h"
29
30// C++ includes
31#include <cstddef>
32#include <memory>
33
34namespace libMesh
35{
36
46template <typename Output=Number>
47class WrappedFunctor : public FEMFunctionBase<Output>
48{
49public:
50
56 : _func(func.clone())
57 { }
58
62 WrappedFunctor (const System & sys,
63 Output fptr(const Point & p,
64 const Parameters & parameters,
65 const std::string & sys_name,
66 const std::string & unknown_name) = nullptr,
67 const Parameters * parameters = nullptr,
68 unsigned int varnum=0) :
69 _func(std::make_unique<WrappedFunction<Output>>(sys, fptr, parameters, varnum)) {}
70
75 WrappedFunctor (const WrappedFunctor &) = delete;
77
83 virtual ~WrappedFunctor () = default;
84
88 virtual void init () override { _func->init(); }
89
93 virtual void init_context (const FEMContext & c) override;
94
95 virtual std::unique_ptr<FEMFunctionBase<Output>> clone () const override
96 {
97 return std::make_unique<WrappedFunctor<Output>>(*_func);
98 }
99
100 virtual Output operator() (const FEMContext &,
101 const Point & p,
102 const Real time = 0.) override
103 { return _func->operator()(p, time); }
104
105 virtual void operator() (const FEMContext &,
106 const Point & p,
107 const Real time,
108 DenseVector<Output> & output) override
109 { _func->operator() (p, time, output); }
110
111 virtual Output component (const FEMContext &,
112 unsigned int i,
113 const Point & p,
114 Real time=0.) override
115 { return _func->component(i, p, time); }
116
117protected:
118
119 std::unique_ptr<FunctionBase<Output>> _func;
120};
121
122
123template <typename Output>
125{
126 // If the context was constructed with an active var subset (e.g. projector
127 // projecting only selected variables), then only those FE objects are
128 // guaranteed to exist.
129 const std::vector<unsigned int> * active = c.active_vars();
130
131 auto check_and_init = [&c](unsigned int v, unsigned short dim)
132 {
133 FEAbstract * fe = nullptr;
134 c.get_element_fe(v, fe, dim);
135 libmesh_assert_msg(fe, "FEAbstract pointer is null for variable "
136 << v << " in dimension " << dim);
137 fe->get_nothing();
138 };
139
140 for (const auto dim : c.elem_dimensions())
141 {
142 if (active)
143 for (const auto v : *active)
144 check_and_init(v, dim);
145 else
146 for (const auto v : make_range(c.n_vars()))
147 check_and_init(v, dim);
148 }
149}
150
151
152} // namespace libMesh
153
154#endif // LIBMESH_WRAPPED_FUNCTOR_H
unsigned int dim
Defines a dense vector for use in Finite Element-type computations.
unsigned int n_vars() const
Number of variables in solution.
This class forms the foundation from which generic finite elements may be derived.
void get_nothing() const
This class provides all data required for a physics package (e.g.
Definition fem_context.h:63
const std::set< unsigned char > & elem_dimensions() const
const std::vector< unsigned int > * active_vars() const
Return a pointer to the vector of active variables being computed for, or a null pointer if all varia...
void get_element_fe(unsigned int var, FEGenericBase< OutputShape > *&fe) const
Accessor for interior finite element object for variable var for the largest dimension in the mesh.
FEMFunctionBase is a base class from which users can derive in order to define "function-like" object...
Base class for functors that can be evaluated at a point and (optionally) time.
This class provides the ability to map between arbitrary, user-defined strings and several data types...
Definition parameters.h:75
A Point defines a location in LIBMESH_DIM dimensional Real space.
Definition point.h:40
Manages consistently variables, degrees of freedom, and coefficient vectors.
Definition system.h:100
Wrap a libMesh-style function pointer into a FunctionBase object.
This class provides a wrapper with which to evaluate a (libMesh-style) function pointer in a Function...
WrappedFunctor(const FunctionBase< Output > &func)
Constructor to wrap FunctionBase functors in a FEMFunctionBase compatible shim.
WrappedFunctor(WrappedFunctor &&)=default
The remaining 5 special functions can be defaulted.
WrappedFunctor(const System &sys, Output fptr(const Point &p, const Parameters &parameters, const std::string &sys_name, const std::string &unknown_name)=nullptr, const Parameters *parameters=nullptr, unsigned int varnum=0)
Constructor to wrap scalar-valued function pointers.
virtual std::unique_ptr< FEMFunctionBase< Output > > clone() const override
virtual Output operator()(const FEMContext &, const Point &p, const Real time=0.) override
std::unique_ptr< FunctionBase< Output > > _func
virtual ~WrappedFunctor()=default
WrappedFunctor & operator=(const WrappedFunctor &)=delete
virtual void init_context(const FEMContext &c) override
Tell the context we don't need anything from it.
virtual Output component(const FEMContext &, unsigned int i, const Point &p, Real time=0.) override
virtual void init() override
Any post-construction initialization.
WrappedFunctor(const WrappedFunctor &)=delete
This class can't be copy constructed or assigned because it contains a unique_ptr member.
The libMesh namespace provides an interface to certain functionality in the library.
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
IntRange< T > make_range(T beg, T end)
The 2-parameter make_range() helper function returns an IntRange<T> when both input parameters are of...
Definition int_range.h:176
Number fptr(const Point &p, const Parameters &, const std::string &libmesh_dbg_var(sys_name), const std::string &unknown_name)
Definition projection.C:81