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  Copyright (C) 2020 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 \*---------------------------------------------------------------------------*/
28 
29 #include "linearEqn.H"
30 #include "quadraticEqn.H"
31 
32 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
33 
35 {
36  const scalar a = this->a();
37  const scalar b = this->b();
38  const scalar c = this->c();
39 
40  // Check the leading term in the quadratic eqn exists
41  if (mag(a) < VSMALL)
42  {
43  return Roots<2>(linearEqn(b, c).roots(), roots::nan, 0);
44  }
45 
46  // (JLM:p. 2246) [discriminant = b*b/4 - a*c]
47  const scalar w = a*c;
48  const scalar numDiscr = fma(-a, c, w) + fma(b, b/4, -w);
49  const scalar discr = (mag(numDiscr) > VSMALL) ? numDiscr : 0;
50 
51  // Find how many roots of what types are available
52  const bool twoReal = discr > 0;
53  const bool twoComplex = discr < 0;
54  //const bool oneReal = discr == 0;
55 
56  if (twoReal)
57  {
58  // (F:Exp. 8.9)
59  const scalar x = -b/2 - sign(b)*sqrt(discr);
60  return Roots<2>(linearEqn(-a, x).roots(), linearEqn(-x, c).roots());
61  }
62  else if (twoComplex)
63  {
64  const Roots<1> xRe(roots::type::complex, -b/2/a);
65  const Roots<1> xIm(roots::type::complex, sign(b)*sqrt(mag(discr))/a);
66  return Roots<2>(xRe, xIm);
67  }
68  else // (oneReal)
69  {
70  const Roots<1> r(linearEqn(a, b/2).roots());
71  return Roots<2>(r, r);
72  }
73 }
74 
75 // ************************************************************************* //
Foam::roots::complex
Definition: Roots.H:57
Foam::roots::nan
Definition: Roots.H:60
Foam::quadraticEqn::roots
Roots< 2 > roots() const
Return the roots of the quadratic equation with no particular order.
Definition: quadraticEqn.C:34
Foam::sign
dimensionedScalar sign(const dimensionedScalar &ds)
Definition: dimensionedScalar.C:166
Foam::quadraticEqn::b
scalar b() const
Definition: quadraticEqnI.H:61
Foam::linearEqn
Container to encapsulate various operations for linear equation of the forms with real coefficients:
Definition: linearEqn.H:60
linearEqn.H
Foam::quadraticEqn::a
scalar a() const
Definition: quadraticEqnI.H:55
Foam::sqrt
dimensionedScalar sqrt(const dimensionedScalar &ds)
Definition: dimensionedScalar.C:144
Foam::mag
dimensioned< typename typeOfMag< Type >::type > mag(const dimensioned< Type > &dt)
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