Line data Source code
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 : 37 : /** 38 : * This class provides a wrapper with which to evaluate a 39 : * (libMesh-style) function pointer in a FunctionBase-compatible 40 : * interface. All overridden virtual functions are documented in 41 : * fem_function_base.h. 42 : * 43 : * \author Roy Stogner 44 : * \date 2015 45 : */ 46 : template <typename Output=Number> 47 : class WrappedFunctor : public FEMFunctionBase<Output> 48 : { 49 : public: 50 : 51 : /** 52 : * Constructor to wrap FunctionBase functors in a FEMFunctionBase 53 : * compatible shim. 54 : */ 55 1832835 : WrappedFunctor (const FunctionBase<Output> & func) 56 1832835 : : _func(func.clone()) 57 1588284 : { } 58 : 59 : /** 60 : * Constructor to wrap scalar-valued function pointers. 61 : */ 62 4824 : 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 4824 : _func(std::make_unique<WrappedFunction<Output>>(sys, fptr, parameters, varnum)) {} 70 : 71 : /** 72 : * This class can't be copy constructed or assigned because it 73 : * contains a unique_ptr member. 74 : */ 75 : WrappedFunctor (const WrappedFunctor &) = delete; 76 : WrappedFunctor & operator= (const WrappedFunctor &) = delete; 77 : 78 : /** 79 : * The remaining 5 special functions can be defaulted. 80 : */ 81 : WrappedFunctor (WrappedFunctor &&) = default; 82 : WrappedFunctor & operator= (WrappedFunctor &&) = default; 83 1748270 : virtual ~WrappedFunctor () = default; 84 : 85 : /** 86 : * Any post-construction initialization 87 : */ 88 51806 : virtual void init () override { _func->init(); } 89 : 90 : /** 91 : * Tell the context we don't need anything from it 92 : */ 93 : virtual void init_context (const FEMContext & c) override; 94 : 95 1427586 : virtual std::unique_ptr<FEMFunctionBase<Output>> clone () const override 96 : { 97 1427586 : return std::make_unique<WrappedFunctor<Output>>(*_func); 98 : } 99 : 100 0 : virtual Output operator() (const FEMContext &, 101 : const Point & p, 102 : const Real time = 0.) override 103 0 : { return _func->operator()(p, time); } 104 : 105 0 : virtual void operator() (const FEMContext &, 106 : const Point & p, 107 : const Real time, 108 : DenseVector<Output> & output) override 109 0 : { _func->operator() (p, time, output); } 110 : 111 182140234 : virtual Output component (const FEMContext &, 112 : unsigned int i, 113 : const Point & p, 114 : Real time=0.) override 115 182140234 : { return _func->component(i, p, time); } 116 : 117 : protected: 118 : 119 : std::unique_ptr<FunctionBase<Output>> _func; 120 : }; 121 : 122 : 123 : template <typename Output> 124 1081243 : void WrappedFunctor<Output>::init_context (const FEMContext & c) 125 : { 126 2167645 : for (auto dim : c.elem_dimensions()) 127 : { 128 2239070 : for (auto v : make_range(c.n_vars())) 129 : { 130 : FEAbstract * fe; 131 37923 : c.get_element_fe(v, fe, dim); 132 37923 : fe->get_nothing(); 133 : } 134 : } 135 1081243 : } 136 : 137 : 138 : } // namespace libMesh 139 : 140 : #endif // LIBMESH_WRAPPED_FUNCTOR_H