libMesh
solution_function.h
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2024 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 #ifndef SOLUTION_FUNCTION_H
19 #define SOLUTION_FUNCTION_H
20 
21 // libMesh includes
22 #include "libmesh/function_base.h"
23 
24 // Example includes
25 #include "laplace_exact_solution.h"
26 
27 // C++ includes
28 #include <memory>
29 
30 using namespace libMesh;
31 
32 class SolutionFunction : public FunctionBase<Number>
33 {
34 public:
35 
36  SolutionFunction (const unsigned int u_var) :
37  _u_var(u_var) {}
38 
39  ~SolutionFunction() = default;
40 
41  virtual Number operator() (const Point &,
42  const Real = 0)
43  { libmesh_not_implemented(); }
44 
45  virtual void operator() (const Point & p,
46  const Real,
47  DenseVector<Number> & output)
48  {
49  output.zero();
50  const Real x=p(0), y=p(1), z=p(2);
51  // libMesh assumes each component of the vector-valued variable is stored
52  // contiguously.
53  output(_u_var) = soln(0, x, y, z);
54  output(_u_var+1) = soln(1, x, y, z);
55  output(_u_var+2) = soln(2, x, y, z);
56  }
57 
58  virtual Number component(unsigned int component_in,
59  const Point & p,
60  const Real)
61  {
62  const Real x=p(0), y=p(1), z=p(2);
63  return soln(component_in, x, y, z);
64  }
65 
66  virtual std::unique_ptr<FunctionBase<Number>> clone() const
67  { return std::make_unique<SolutionFunction>(_u_var); }
68 
69 private:
70 
71  const unsigned int _u_var;
73 };
74 
75 //FIXME: PB: We ought to be able to merge the above class with this one
76 // through templating, but I'm being lazy.
77 class SolutionGradient : public FunctionBase<Gradient>
78 {
79 public:
80 
81  SolutionGradient(const unsigned int u_var)
82  : _u_var(u_var) {}
83  ~SolutionGradient() = default;
84 
85  virtual Gradient operator() (const Point &, const Real = 0)
86  { libmesh_not_implemented(); }
87 
88  virtual void operator() (const Point & p,
89  const Real,
90  DenseVector<Gradient>& output)
91  {
92  output.zero();
93  const Real x=p(0), y=p(1), z=p(2);
94  output(_u_var) = soln(0, x, y, z);
95  output(_u_var+1) = soln(1, x, y, z);
96  output(_u_var+2) = soln(2, x, y, z);
97  }
98 
99  virtual Gradient component(unsigned int component_in,
100  const Point & p,
101  const Real)
102  {
103  const Real x=p(0), y=p(1), z=p(2);
104  return soln(component_in, x, y, z);
105  }
106 
107  virtual std::unique_ptr<FunctionBase<Gradient>> clone() const
108  { return std::make_unique<SolutionGradient>(_u_var); }
109 
110 private:
111 
112  const unsigned int _u_var;
114 };
115 
116 #endif // SOLUTION_FUNCTION_H
virtual Number component(unsigned int component_in, const Point &p, const Real)
virtual std::unique_ptr< FunctionBase< Gradient > > clone() const
virtual void zero() override final
Set every element in the vector to 0.
Definition: dense_vector.h:398
virtual std::unique_ptr< FunctionBase< Number > > clone() const
SolutionFunction(const unsigned int u_var)
virtual Gradient component(unsigned int component_in, const Point &p, const Real)
This class defines a vector in LIBMESH_DIM dimensional Real or Complex space.
The libMesh namespace provides an interface to certain functionality in the library.
const unsigned int _u_var
const unsigned int _u_var
SolutionGradient(const unsigned int u_var)
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
LaplaceExactGradient soln
Base class for functors that can be evaluated at a point and (optionally) time.
A Point defines a location in LIBMESH_DIM dimensional Real space.
Definition: point.h:39
LaplaceExactSolution soln