quadraticEqn.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2017-2018 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "linearEqn.H"
27 #include "quadraticEqn.H"
28 
29 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
30 
32 {
33  /*
34 
35  This function solves a quadraticEqn equation of the following form:
36 
37  a*x^2 + b*x + c = 0
38  x^2 + B*x + C = 0
39 
40  The quadraticEqn formula is as follows:
41 
42  x = - B/2 +- sqrt(B*B - 4*C)/2
43 
44  If the sqrt generates a complex number, this provides the result. If not
45  then the real root with the smallest floating point error is calculated.
46 
47  x0 = - B/2 - sign(B)*sqrt(B*B - 4*C)/2
48 
49  The other root is the obtained using an identity.
50 
51  x1 = C/x0
52 
53  */
54 
55  const scalar a = this->a();
56  const scalar b = this->b();
57  const scalar c = this->c();
58 
59  if (a == 0)
60  {
61  return Roots<2>(linearEqn(b, c).roots(), roots::nan, 0);
62  }
63 
64  // This is assumed not to over- or under-flow. If it does, all bets are off.
65  const scalar disc = b*b/4 - a*c;
66 
67  // How many roots of what types are available?
68  const bool oneReal = disc == 0;
69  const bool twoReal = disc > 0;
70  // const bool twoComplex = disc < 0;
71 
72  if (oneReal)
73  {
74  const Roots<1> r = linearEqn(a, b/2).roots();
75  return Roots<2>(r, r);
76  }
77  else if (twoReal)
78  {
79  const scalar x = - b/2 - sign(b)*sqrt(disc);
80  return Roots<2>(linearEqn(- a, x).roots(), linearEqn(- x, c).roots());
81  }
82  else // if (twoComplex)
83  {
84  return Roots<2>(roots::complex, 0);
85  }
86 }
87 
88 // ************************************************************************* //
dimensionedScalar sign(const dimensionedScalar &ds)
Roots< 2 > roots() const
Get the roots.
Definition: quadraticEqn.C:31
dimensionedScalar sqrt(const dimensionedScalar &ds)
Templated storage for the roots of polynomial equations, plus flags to indicate the nature of the roo...
Definition: Roots.H:68
scalar c() const
Definition: quadraticEqnI.H:65
scalar a() const
Definition: quadraticEqnI.H:53
Linear equation of the form a*x + b = 0.
Definition: linearEqn.H:48
scalar b() const
Definition: quadraticEqnI.H:59
Roots< 1 > roots() const
Get the roots.
Definition: linearEqnI.H:89