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