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 "curl_curl_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 &, const Real = 0)
42  { libmesh_not_implemented(); }
43 
44  virtual void operator() (const Point & p,
45  const Real,
46  DenseVector<Number> & output)
47  {
48  output.zero();
49  const Real x=p(0), y=p(1), z=p(2);
50  // libMesh assumes each component of the vector-valued variable is stored
51  // contiguously.
52  output(_u_var) = soln(x, y, z)(0);
53  output(_u_var+1) = soln(x, y, z)(1);
54  output(_u_var+2) = soln(x, y, z)(2);
55  }
56 
57  virtual Number component(unsigned int component_in,
58  const Point & p,
59  const Real)
60  {
61  const Real x=p(0), y=p(1), z=p(2);
62  return soln(x, y, z)(component_in);
63  }
64 
65  virtual std::unique_ptr<FunctionBase<Number>> clone() const
66  { return std::make_unique<SolutionFunction>(_u_var); }
67 
68 private:
69 
70  const unsigned int _u_var;
72 };
73 
74 class SolutionGradient : public FunctionBase<Gradient>
75 {
76 public:
77 
78  SolutionGradient(const unsigned int u_var)
79  : _u_var(u_var) {}
80  ~SolutionGradient() = default;
81 
82  virtual Gradient operator() (const Point &,
83  const Real = 0)
84  { libmesh_not_implemented(); }
85 
86  virtual void operator() (const Point & p,
87  const Real,
88  DenseVector<Gradient> & output)
89  {
90  output.zero();
91  const Real x=p(0), y=p(1), z=p(2);
92  output(_u_var) = soln.grad(x, y, z).row(0);
93  output(_u_var+1) = soln.grad(x, y, z).row(1);
94  output(_u_var+2) = soln.grad(x, y, z).row(2);
95  }
96 
97  virtual Gradient component(unsigned int component_in,
98  const Point & p,
99  const Real)
100  {
101  const Real x=p(0), y=p(1), z=p(2);
102  return soln.grad(x, y, z).row(component_in);
103  }
104 
105  virtual std::unique_ptr<FunctionBase<Gradient>> clone() const
106  { return std::make_unique<SolutionGradient>(_u_var); }
107 
108 private:
109 
110  const unsigned int _u_var;
112 };
113 
114 #endif // SOLUTION_FUNCTION_H
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)
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.
SolutionGradient(const unsigned int u_var)
virtual Number component(unsigned int component_in, const Point &p, const Real)
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
virtual Gradient component(unsigned int component_in, const Point &p, const Real)
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