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-2025 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(), rootType::nan, 0);
62  }
63 
64  // This is assumed not to over- or under-flow. If it does, all bets are off.
65  scalar disc = b*b/4 - a*c;
66 
67  // Ensure disc is not negative if the roots are known to be real
68  // even if round-off error might cause disc to be slightly negative
69  if (real && disc < 0)
70  {
71  disc = 0;
72  }
73 
74  // How many roots of what types are available?
75  const bool oneReal = disc == 0;
76  const bool twoReal = disc > 0;
77  // const bool twoComplex = disc < 0;
78 
79  if (oneReal)
80  {
81  const Roots<1> r = linearEqn(a, b/2).roots();
82  return Roots<2>(r, r);
83  }
84  else if (twoReal)
85  {
86  const scalar x = - b/2 - sign(b)*sqrt(disc);
87  return Roots<2>(linearEqn(- a, x).roots(), linearEqn(- x, c).roots());
88  }
89  else // if (twoComplex)
90  {
91  return Roots<2>(rootType::complex, 0);
92  }
93 }
94 
95 // ************************************************************************* //
Templated storage for the roots of polynomial equations, plus flags to indicate the nature of the roo...
Definition: Roots.H:67
Linear equation of the form a*x + b = 0.
Definition: linearEqn.H:51
Roots< 1 > roots() const
Get the roots.
Definition: linearEqnI.H:89
scalar c() const
Definition: quadraticEqnI.H:65
Roots< 2 > roots(const bool real=false) const
Get the roots.
Definition: quadraticEqn.C:31
scalar a() const
Definition: quadraticEqnI.H:53
scalar b() const
Definition: quadraticEqnI.H:59
dimensionedScalar sign(const dimensionedScalar &ds)
void sqrt(LagrangianPatchField< scalar > &f, const LagrangianPatchField< scalar > &f1)