libMesh
wrapped_functor.h
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2025 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 
34 namespace libMesh
35 {
36 
46 template <typename Output=Number>
47 class WrappedFunctor : public FEMFunctionBase<Output>
48 {
49 public:
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;
76  WrappedFunctor & operator= (const WrappedFunctor &) = delete;
77 
81  WrappedFunctor (WrappedFunctor &&) = default;
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 
117 protected:
118 
119  std::unique_ptr<FunctionBase<Output>> _func;
120 };
121 
122 
123 template <typename Output>
125 {
126  for (auto dim : c.elem_dimensions())
127  {
128  for (auto v : make_range(c.n_vars()))
129  {
130  FEAbstract * fe;
131  c.get_element_fe(v, fe, dim);
132  fe->get_nothing();
133  }
134  }
135 }
136 
137 
138 } // namespace libMesh
139 
140 #endif // LIBMESH_WRAPPED_FUNCTOR_H
Wrap a libMesh-style function pointer into a FunctionBase object.
This class provides the ability to map between arbitrary, user-defined strings and several data types...
Definition: parameters.h:67
unsigned int dim
WrappedFunctor & operator=(const WrappedFunctor &)=delete
virtual Output component(const FEMContext &, unsigned int i, const Point &p, Real time=0.) override
The libMesh namespace provides an interface to certain functionality in the library.
virtual std::unique_ptr< FEMFunctionBase< Output > > clone() const override
Number fptr(const Point &p, const Parameters &, const std::string &libmesh_dbg_var(sys_name), const std::string &unknown_name)
Definition: projection.C:80
This class provides a wrapper with which to evaluate a (libMesh-style) function pointer in a Function...
virtual Output operator()(const FEMContext &, const Point &p, const Real time=0.) override
std::unique_ptr< FunctionBase< Output > > _func
Manages consistently variables, degrees of freedom, and coefficient vectors.
Definition: system.h:96
virtual void init() override
Any post-construction initialization.
This class provides all data required for a physics package (e.g.
Definition: fem_context.h:62
virtual ~WrappedFunctor()=default
void get_nothing() const
Definition: fe_abstract.h:269
virtual void init_context(const FEMContext &c) override
Tell the context we don&#39;t need anything from it.
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.
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:140
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...
Definition: fem_context.h:277
WrappedFunctor(const FunctionBase< Output > &func)
Constructor to wrap FunctionBase functors in a FEMFunctionBase compatible shim.
This class forms the foundation from which generic finite elements may be derived.
Definition: fe_abstract.h:99
unsigned int n_vars() const
Number of variables in solution.
Definition: diff_context.h:100
Base class for functors that can be evaluated at a point and (optionally) time.
FEMFunctionBase is a base class from which users can derive in order to define "function-like" object...
A Point defines a location in LIBMESH_DIM dimensional Real space.
Definition: point.h:39
const std::set< unsigned char > & elem_dimensions() const
Definition: fem_context.h:951