libMesh
solution_function.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 #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 "div_grad_exact_solution.h"
26 
27 // C++ includes
28 #include <memory>
29 
30 using namespace libMesh;
31 
32 template<unsigned int dim>
33 class SolutionFunction : public FunctionBase<Number>
34 {
35 public:
36 
37  SolutionFunction() = default;
38  ~SolutionFunction() = default;
39 
40  virtual Number operator() (const Point &,
41  const Real = 0)
42  { libmesh_not_implemented(); }
43 
44  virtual void operator() (const Point & p,
45  const Real,
46  DenseVector<Number> & output);
47 
48  virtual Number component(unsigned int component_in,
49  const Point & p,
50  const Real);
51 
52  virtual std::unique_ptr<FunctionBase<Number>> clone() const
53  { return std::make_unique<SolutionFunction>(); }
54 
55 private:
56 
58 };
59 
60 template<>
62  const Real,
63  DenseVector<Number> & output)
64 {
65  output.zero();
66  const Real x=p(0), y=p(1);
67  output(0) = soln(x, y)(0);
68  output(1) = soln(x, y)(1);
69  output(2) = soln.scalar(x, y);
70 }
71 
72 template<>
74  const Real,
75  DenseVector<Number> & output)
76 {
77  output.zero();
78  const Real x=p(0), y=p(1), z=p(2);
79  output(0) = soln(x, y, z)(0);
80  output(1) = soln(x, y, z)(1);
81  output(2) = soln(x, y, z)(2);
82  output(3) = soln.scalar(x, y, z);
83 }
84 
85 template<unsigned int dim>
86 Number SolutionFunction<dim>::component(unsigned int component_in,
87  const Point & p,
88  const Real)
89 {
90  DenseVector<Number> outvec(dim+1);
91  (*this)(p, 0, outvec);
92  return outvec(component_in);
93 }
94 
95 template<unsigned int dim>
96 class SolutionGradient : public FunctionBase<Gradient>
97 {
98 public:
99 
100  SolutionGradient() = default;
101  ~SolutionGradient() = default;
102 
103  virtual Gradient operator() (const Point &, const Real = 0)
104  { libmesh_not_implemented(); }
105 
106  virtual void operator() (const Point & p,
107  const Real,
108  DenseVector<Gradient> & output);
109 
110  virtual Gradient component(unsigned int component_in,
111  const Point & p,
112  const Real);
113 
114  virtual std::unique_ptr<FunctionBase<Gradient>> clone() const
115  { return std::make_unique<SolutionGradient>(); }
116 
117 private:
118 
120 };
121 
122 template<>
124  const Real,
125  DenseVector<Gradient> & output)
126 {
127  output.zero();
128  const Real x=p(0), y=p(1);
129  output(0) = soln.grad(x, y).row(0);
130  output(1) = soln.grad(x, y).row(1);
131  output(2) = -soln(x, y);
132 }
133 
134 template<>
136  const Real,
137  DenseVector<Gradient> & output)
138 {
139  output.zero();
140  const Real x=p(0), y=p(1), z=p(2);
141  output(0) = soln.grad(x, y, z).row(0);
142  output(1) = soln.grad(x, y, z).row(1);
143  output(2) = soln.grad(x, y, z).row(2);
144  output(3) = -soln(x, y, z);
145 }
146 
147 template<unsigned int dim>
148 Gradient SolutionGradient<dim>::component(unsigned int component_in,
149  const Point & p,
150  const Real)
151 {
152  DenseVector<Gradient> outvec(dim+1);
153  (*this)(p, 0, outvec);
154  return outvec(component_in);
155 }
156 
157 #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 Number operator()(const Point &, const Real=0)
virtual void zero() override final
Set every element in the vector to 0.
Definition: dense_vector.h:420
virtual Gradient operator()(const Point &, const Real=0)
virtual std::unique_ptr< FunctionBase< Number > > clone() const
unsigned int dim
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.
DivGradExactSolution soln
DivGradExactSolution soln
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE 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