ode_example_functions.h
Go to the documentation of this file.
1 //LIC// ====================================================================
2 //LIC// This file forms part of oomph-lib, the object-oriented,
3 //LIC// multi-physics finite-element library, available
4 //LIC// at http://www.oomph-lib.org.
5 //LIC//
6 //LIC// Copyright (C) 2006-2022 Matthias Heil and Andrew Hazel
7 //LIC//
8 //LIC// This library is free software; you can redistribute it and/or
9 //LIC// modify it under the terms of the GNU Lesser General Public
10 //LIC// License as published by the Free Software Foundation; either
11 //LIC// version 2.1 of the License, or (at your option) any later version.
12 //LIC//
13 //LIC// This library is distributed in the hope that it will be useful,
14 //LIC// but WITHOUT ANY WARRANTY; without even the implied warranty of
15 //LIC// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 //LIC// Lesser General Public License for more details.
17 //LIC//
18 //LIC// You should have received a copy of the GNU Lesser General Public
19 //LIC// License along with this library; if not, write to the Free Software
20 //LIC// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 //LIC// 02110-1301 USA.
22 //LIC//
23 //LIC// The authors may be contacted at oomph-lib@maths.man.ac.uk.
24 //LIC//
25 //LIC//====================================================================
26 #ifndef OOMPH_ODE_EXAMPLE_FUNCTIONS_H
27 #define OOMPH_ODE_EXAMPLE_FUNCTIONS_H
28 
29 #include "generic.h"
30 #include "ode.h"
31 
32 namespace oomph
33 {
34  using namespace MathematicalConstants;
35  using namespace StringConversion;
36 
37 
38  namespace deriv_functions
39  {
40 
41  inline Vector<double> cos(const double& time, const Vector<double>& x)
42  {
43  Vector<double> values(1);
44  values[0] = std::cos(time);
45  return values;
46  }
47  inline Vector<double> dcos(const double& t, const Vector<double>& x,
48  const Vector<double>& u)
49  {
50  Vector<double> deriv(1, 0.0);
51  deriv[0] = -1*std::sin(t);
52  return deriv;
53  }
54 
55 
56  inline Vector<double> sin(const double& time, const Vector<double>& x)
57  {
58  Vector<double> values(1);
59  values[0] = std::sin(time);
60  return values;
61  }
62  inline Vector<double> dsin(const double& t, const Vector<double>& x,
63  const Vector<double>& u)
64  {
65  Vector<double> deriv(1, 0.0);
66  deriv[0] = std::cos(t);
67  return deriv;
68  }
69 
70 
71  inline Vector<double> exp(const double& time, const Vector<double>& x)
72  {
73  Vector<double> values(1);
74  values[0] = std::exp(time);
75  return values;
76  }
77  inline Vector<double> dexp(const double& t, const Vector<double>& x,
78  const Vector<double>& u)
79  {
80  Vector<double> deriv(1, 0.0);
81  deriv[0] = u[0];
82  return deriv;
83  }
84 
85 
86  // A polynomial of degree 2
87  inline Vector<double> poly2(const double& time, const Vector<double>& x)
88  {
89  double b0 = 0.5, b1 = 0, b2 = 1;
90 
91  Vector<double> values(1);
92  values[0] = b2*time*time + b1*time +b0;
93  return values;
94  }
95  inline Vector<double> dpoly2(const double& t, const Vector<double>& x,
96  const Vector<double>& u)
97  {
98  double b1 = 0, b2 = 1;
99 
100  Vector<double> deriv(1, 0.0);
101  deriv[0] = 2*t*b2 + b1;
102  return deriv;
103  }
104 
105 
106  // A polynomial of degree 3
107  inline Vector<double> poly3(const double& time, const Vector<double>& x)
108  {
109  double a0 = 0.5, a1 = 0, a2 = 0, a3 = 1;
110 
111  Vector<double> values(1);
112  values[0] = a3*time*time*time + a2*time*time + a1*time +a0;
113  return values;
114  }
115  inline Vector<double> dpoly3(const double& t, const Vector<double>& x,
116  const Vector<double>& u)
117  {
118  double a1 = 0, a2 = 0, a3 = 1;
119 
120  Vector<double> deriv(1, 0.0);
121  deriv[0] = 3*t*t*a3 + 2*t*a2 + a1;
122  return deriv;
123  }
124 
125  // stiff ode, example from Iserles pg. 54
126  inline Vector<double> stiff_test(const double& time, const Vector<double>& x)
127  {
128  Vector<double> x1(2), x2(2);
129  x1[0] = 0; x1[1] = 0;
130  x2[0] = 1.0; x2[1] = 999.0/10;
131 
132  Vector<double> values(2);
133  values[0] = std::exp(-100*time)*x1[0] + std::exp(-0.1*time)*x2[0];
134  values[1] = std::exp(-100*time)*x1[1] + std::exp(-0.1*time)*x2[1];
135  return values;
136  }
137  inline Vector<double> dstiff_test(const double& t, const Vector<double>& x,
138  const Vector<double>& u)
139  {
140  Vector<double> deriv(2, 0.0);
141  deriv[0] = -100*u[0] + u[1];
142  deriv[1] = 0*u[0] - 0.1*u[1];
143  return deriv;
144  }
145 
148  {
149  public:
152  {
153  Beta = 0.5;
154  Omega = 2;
155  }
156 
158  virtual ~DampedOscillation() {}
159 
161  Vector<double> operator()(const double& t, const Vector<double>& x) const
162  {
163  Vector<double> values(1);
164  values[0] = std::exp(-Beta*t) * std::sin(Omega*t);
165  return values;
166  }
167 
169  Vector<double> derivative(const double& t, const Vector<double>& x,
170  const Vector<double>& u) const
171  {
172  Vector<double> deriv(1, 0.0);
173  deriv[0] = -Beta * std::exp(-Beta*t) * std::sin(Omega*t)
174  + Omega * std::exp(-Beta*t) * std::cos(Omega*t);
175  return deriv;
176  }
177 
178  double Beta;
179  double Omega;
180  };
181 
184  {
185  public:
188  {
189  Lambda = 100;
190  Y_intial = 1;
191  }
192 
194  virtual ~SimpleStiffTest() {}
195 
197  Vector<double> operator()(const double& t, const Vector<double>& x) const
198  {
199  Vector<double> values(1);
200  values[0] = std::exp(-Lambda * t) * Y_intial;
201  return values;
202  }
203 
205  Vector<double> derivative(const double& t, const Vector<double>& x,
206  const Vector<double>& u) const
207  {
208  Vector<double> deriv(1, 0.0);
209  deriv[0] = -Lambda * u[0];
210  return deriv;
211  }
212 
213  double Lambda;
214  double Y_intial;
215  };
216 
219  {
220  public:
223  {
224  Lambda = -100;
225  }
226 
228  virtual ~OrderReductionTest() {}
229 
231  Vector<double> operator()(const double& t, const Vector<double>& x) const
232  {
233  Vector<double> values(1);
234  values[0] = std::sin(t);
235  return values;
236  }
237 
239  Vector<double> derivative(const double& t, const Vector<double>& x,
240  const Vector<double>& u) const
241  {
242  Vector<double> deriv(1, 0.0);
243  deriv[0] = Lambda*u[0] - Lambda*std::sin(t) + std::cos(t);
244  return deriv;
245  }
246 
247  double Lambda;
248  };
249 
250  }
251 
252  namespace ODEFactories
253  {
254 
256  {
257  if(exact_name == "damped_oscillation")
258  {
260  }
261  else if(exact_name == "simple_stiff")
262  {
264  }
265  else if(exact_name == "order_reduction")
266  {
268  }
269  else if(exact_name == "strong_order_reduction")
270  {
272  }
273 
276 
277  if(exact_name == "sin")
278  {
279  fpt = &deriv_functions::sin;
280  dfpt = &deriv_functions::dsin;
281  }
282  else if(exact_name == "cos")
283  {
284  fpt = &deriv_functions::cos;
285  dfpt = &deriv_functions::dcos;
286  }
287  else if(exact_name == "exp")
288  {
289  fpt = &deriv_functions::exp;
290  dfpt = &deriv_functions::dexp;
291  }
292  else if(exact_name == "poly3")
293  {
294  fpt = &deriv_functions::poly3;
295  dfpt = &deriv_functions::dpoly3;
296  }
297  else if(exact_name == "stiff_test")
298  {
301  }
302  else if(exact_name == "poly2")
303  {
304  fpt = &deriv_functions::poly2;
305  dfpt = &deriv_functions::dpoly2;
306  }
307  else
308  {
309  throw OomphLibError("Unrecognised exact solution " + exact_name,
312  }
313 
314  return new SolutionFunctor(fpt, dfpt);
315  }
316 
317  }
318 
319 
320 } // End of oomph namespace
321 
322 #endif
AnnoyingScalar cos(const AnnoyingScalar &x)
Definition: AnnoyingScalar.h:136
AnnoyingScalar sin(const AnnoyingScalar &x)
Definition: AnnoyingScalar.h:137
Definition: oomph_definitions.h:222
Definition: oomph_utilities.h:1109
Vector< double >(* TimeSpaceToDoubleVectFctPt)(const double &t, const Vector< double > &x)
General function of space and time which returns a vector of doubles.
Definition: oomph_utilities.h:1118
Vector< double >(* TimeSpaceValueToDoubleVectFctPt)(const double &t, const Vector< double > &x, const Vector< double > &u)
Definition: oomph_utilities.h:1123
Definition: oomph_utilities.h:1166
A damped oscillation solution.
Definition: ode_example_functions.h:148
Vector< double > derivative(const double &t, const Vector< double > &x, const Vector< double > &u) const
Derivative call.
Definition: ode_example_functions.h:169
Vector< double > operator()(const double &t, const Vector< double > &x) const
Function call.
Definition: ode_example_functions.h:161
DampedOscillation()
Constructor.
Definition: ode_example_functions.h:151
double Beta
Definition: ode_example_functions.h:178
virtual ~DampedOscillation()
Virtual destructor.
Definition: ode_example_functions.h:158
double Omega
Definition: ode_example_functions.h:179
Another stiff solution: Atkinson pg. 158, also example 8.2 pg 129.
Definition: ode_example_functions.h:219
OrderReductionTest()
Constructor.
Definition: ode_example_functions.h:222
virtual ~OrderReductionTest()
Virtual destructor.
Definition: ode_example_functions.h:228
Vector< double > derivative(const double &t, const Vector< double > &x, const Vector< double > &u) const
Derivative call.
Definition: ode_example_functions.h:239
double Lambda
Definition: ode_example_functions.h:247
Vector< double > operator()(const double &t, const Vector< double > &x) const
Function call.
Definition: ode_example_functions.h:231
Another stiff solution: Atkinson equation (8.1) pg 128.
Definition: ode_example_functions.h:184
virtual ~SimpleStiffTest()
Virtual destructor.
Definition: ode_example_functions.h:194
SimpleStiffTest()
Constructor.
Definition: ode_example_functions.h:187
Vector< double > derivative(const double &t, const Vector< double > &x, const Vector< double > &u) const
Derivative call.
Definition: ode_example_functions.h:205
double Lambda
Definition: ode_example_functions.h:213
double Y_intial
Definition: ode_example_functions.h:214
Vector< double > operator()(const double &t, const Vector< double > &x) const
Function call.
Definition: ode_example_functions.h:197
double Lambda
Lame parameters.
Definition: axisym_linear_elasticity/cylinder/cylinder.cc:52
Vector< double > x1(const Vector< double > &coord)
Cartesian coordinates centered at the point (0.5,1)
Definition: poisson/poisson_with_singularity/two_d_poisson.cc:86
Vector< double > x2(const Vector< double > &coord)
Cartesian coordinates centered at the point (1.5,1)
Definition: poisson/poisson_with_singularity/two_d_poisson.cc:102
double Beta
Definition: ff_step.cc:156
std::string string(const unsigned &i)
Definition: oomph_definitions.cc:286
SolutionFunctorBase * exact_solutions_factory(const std::string &exact_name)
Definition: ode_example_functions.h:255
double Omega
Definition: osc_ring_sarah_asymptotics.h:43
Vector< double > dexp(const double &t, const Vector< double > &x, const Vector< double > &u)
Definition: ode_example_functions.h:77
Vector< double > dpoly2(const double &t, const Vector< double > &x, const Vector< double > &u)
Definition: ode_example_functions.h:95
Vector< double > dcos(const double &t, const Vector< double > &x, const Vector< double > &u)
Definition: ode_example_functions.h:47
Vector< double > dstiff_test(const double &t, const Vector< double > &x, const Vector< double > &u)
Definition: ode_example_functions.h:137
Vector< double > dsin(const double &t, const Vector< double > &x, const Vector< double > &u)
Definition: ode_example_functions.h:62
Vector< double > poly2(const double &time, const Vector< double > &x)
Definition: ode_example_functions.h:87
Vector< double > dpoly3(const double &t, const Vector< double > &x, const Vector< double > &u)
Definition: ode_example_functions.h:115
Vector< double > cos(const double &time, const Vector< double > &x)
Definition: ode_example_functions.h:41
Vector< double > poly3(const double &time, const Vector< double > &x)
Definition: ode_example_functions.h:107
Vector< double > stiff_test(const double &time, const Vector< double > &x)
Definition: ode_example_functions.h:126
Vector< double > sin(const double &time, const Vector< double > &x)
Definition: ode_example_functions.h:56
Vector< double > exp(const double &time, const Vector< double > &x)
Definition: ode_example_functions.h:71
DRAIG: Change all instances of (SPATIAL_DIM) to (DIM-1).
Definition: AnisotropicHookean.h:10
list x
Definition: plotDoE.py:28
t
Definition: plotPSD.py:36
#define OOMPH_EXCEPTION_LOCATION
Definition: oomph_definitions.h:61
#define OOMPH_CURRENT_FUNCTION
Definition: oomph_definitions.h:86