libMesh
analytic_function.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_ANALYTIC_FUNCTION_H
21 #define LIBMESH_ANALYTIC_FUNCTION_H
22 
23 // Local Includes
24 #include "libmesh/function_base.h"
25 
26 // C++ includes
27 #include <cstddef>
28 
29 namespace libMesh
30 {
31 
32 // Forward Declarations
33 template <typename T>
34 class DenseVector;
35 
47 template <typename Output=Number>
48 class AnalyticFunction : public FunctionBase<Output>
49 {
50 public:
51 
53  typedef Output (*OutputFunction)(const Point & p, const Real time);
54 
60 
62  typedef void (*OutputVectorFunction)(DenseVector<Output> & output,
63  const Point & p,
64  const Real time);
69 
73  AnalyticFunction (AnalyticFunction &&) = default;
74  AnalyticFunction (const AnalyticFunction &) = default;
75  AnalyticFunction & operator= (const AnalyticFunction &) = default;
77  virtual ~AnalyticFunction () = default;
78 
85 
90 
91  virtual void init () override;
92 
93  virtual void clear () override;
94 
95  virtual std::unique_ptr<FunctionBase<Output>> clone () const override;
96 
97  virtual Output operator() (const Point & p,
98  const Real time=0.) override;
99 
100  virtual void operator() (const Point & p,
101  const Real time,
102  DenseVector<Output> & output) override;
103 };
104 
105 
106 
107 // ------------------------------------------------------------
108 // AnalyticFunction inline methods
109 template <typename Output>
110 inline
112  const Real time)
113 {
114  libmesh_assert(this->initialized());
115  libmesh_assert_msg(_number_fptr,
116  "You must construct AnalyticFunction with a scalar-valued "
117  "function in order to use this operator() override.");
118  return (this->_number_fptr(p, time));
119 }
120 
121 
122 
123 template <typename Output>
124 inline
126  const Real time,
127  DenseVector<Output> & output)
128 {
129  libmesh_assert(this->initialized());
130  libmesh_assert_msg(_vector_fptr,
131  "You must construct AnalyticFunction with a vector-valued "
132  "function in order to use this operator() override.");
133  this->_vector_fptr(output, p, time);
134  return;
135 }
136 
137 
138 
139 template <typename Output>
141  FunctionBase<Output> (),
142  _number_fptr (fptr),
143  _vector_fptr (nullptr)
144 {
146  this->_initialized = true;
147 }
148 
149 
150 
151 template <typename Output>
152 inline
154  FunctionBase<Output> (),
155  _number_fptr (nullptr),
156  _vector_fptr (fptr)
157 {
159  this->_initialized = true;
160 }
161 
162 
163 
164 template <typename Output>
166 {
167  // dumb double-test
168  libmesh_assert ((_number_fptr != nullptr) || (_vector_fptr != nullptr));
169 
170  // definitely ready
171  this->_initialized = true;
172 }
173 
174 
175 
176 template <typename Output>
177 inline
179 {
180  // We probably need a method to reset these later...
181  _number_fptr = nullptr;
182  _vector_fptr = nullptr;
183 
184  // definitely not ready
185  this->_initialized = false;
186 }
187 
188 
189 
190 template <typename Output>
191 inline
192 std::unique_ptr<FunctionBase<Output>>
194 {
195  return _number_fptr ?
196  std::make_unique<AnalyticFunction<Output>>(_number_fptr) :
197  std::make_unique<AnalyticFunction<Output>>(_vector_fptr);
198 }
199 
200 
201 } // namespace libMesh
202 
203 
204 #endif // LIBMESH_ANALYTIC_FUNCTION_H
OutputFunction _number_fptr
Pointer to user-provided function that computes the boundary values when an analytical expression is ...
virtual Output operator()(const Point &p, const Real time=0.) override
bool _initialized
When init() was called so that everything is ready for calls to operator() (...), then this bool is t...
Output(* OutputFunction)(const Point &p, const Real time)
Scalar return value function pointer type.
The libMesh namespace provides an interface to certain functionality in the library.
OutputVectorFunction _vector_fptr
Pointer to user-provided vector valued function.
Wraps a function pointer into a FunctionBase object.
Number fptr(const Point &p, const Parameters &, const std::string &libmesh_dbg_var(sys_name), const std::string &unknown_name)
Definition: projection.C:81
virtual std::unique_ptr< FunctionBase< Output > > clone() const override
libmesh_assert(ctx)
virtual ~AnalyticFunction()=default
void(* OutputVectorFunction)(DenseVector< Output > &output, const Point &p, const Real time)
Vector return value function pointer type.
AnalyticFunction(OutputFunction fptr)
Constructor.
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
virtual void init() override
The actual initialization process.
virtual void clear() override
Clears the function.
bool initialized()
Checks that library initialization has been done.
Definition: libmesh.C:324
Base class for functors that can be evaluated at a point and (optionally) time.
AnalyticFunction & operator=(const AnalyticFunction &)=default
A Point defines a location in LIBMESH_DIM dimensional Real space.
Definition: point.h:39