OGS
MathLib::CubicSolver Class Reference

Detailed Description

Definition at line 19 of file CubicRoots.h.

#include <CubicRoots.h>

Public Member Functions

 CubicSolver (const double a, const double b, const double c, const double d)
std::vector< double > solve ()
double smallestPositiveRealRoot ()

Private Attributes

double a_
double b_
double c_
double d_

Constructor & Destructor Documentation

◆ CubicSolver()

MathLib::CubicSolver::CubicSolver ( const double a,
const double b,
const double c,
const double d )
inline

Definition at line 22 of file CubicRoots.h.

23 : a_(a), b_(b), c_(c), d_(d)
24 {
25 if (std::abs(a_) < 1e-9)
26 {
27 OGS_FATAL("'a' must be non-zero for a cubic equation.");
28 }
29 }
#define OGS_FATAL(...)
Definition Error.h:19

References a_, b_, c_, d_, and OGS_FATAL.

Member Function Documentation

◆ smallestPositiveRealRoot()

double MathLib::CubicSolver::smallestPositiveRealRoot ( )
inline

Definition at line 62 of file CubicRoots.h.

63 {
64 std::vector<double> const roots = solve();
65
66 auto positive_roots =
67 roots | ranges::views::filter([](double root) { return root > 0; });
68
69 // If no positive root exists, return NaN
70 if (ranges::empty(positive_roots))
71 {
72 return std::numeric_limits<double>::quiet_NaN();
73 }
74
75 return ranges::min(positive_roots);
76 }
std::vector< double > solve()
Definition CubicRoots.h:31

References solve().

Referenced by MaterialPropertyLib::PengRobinson::value().

◆ solve()

std::vector< double > MathLib::CubicSolver::solve ( )
inline

Definition at line 31 of file CubicRoots.h.

32 {
33 std::array<double, 3> const roots =
34 boost::math::tools::cubic_roots<double>(a_, b_, c_, d_);
35
36 std::vector<double> adjusted_roots;
37 adjusted_roots.reserve(3);
38
39 double last_valid = std::numeric_limits<double>::quiet_NaN();
40
41 for (auto root : roots)
42 {
43 if (std::isnan(root))
44 {
45 if (!std::isnan(last_valid))
46 {
47 adjusted_roots.push_back(last_valid);
48 }
49 // If we get NaN before any valid root, we just skip it
50 }
51 else
52 {
53 adjusted_roots.push_back(root);
54 last_valid = root;
55 }
56 }
57
58 ranges::sort(adjusted_roots);
59 return adjusted_roots;
60 }

References a_, b_, c_, and d_.

Referenced by smallestPositiveRealRoot().

Member Data Documentation

◆ a_

double MathLib::CubicSolver::a_
private

Definition at line 79 of file CubicRoots.h.

Referenced by CubicSolver(), and solve().

◆ b_

double MathLib::CubicSolver::b_
private

Definition at line 79 of file CubicRoots.h.

Referenced by CubicSolver(), and solve().

◆ c_

double MathLib::CubicSolver::c_
private

Definition at line 79 of file CubicRoots.h.

Referenced by CubicSolver(), and solve().

◆ d_

double MathLib::CubicSolver::d_
private

Definition at line 79 of file CubicRoots.h.

Referenced by CubicSolver(), and solve().


The documentation for this class was generated from the following file: