OGS
MathLib::CubicSolver Class Reference

Detailed Description

Definition at line 25 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 28 of file CubicRoots.h.

29 : a_(a), b_(b), c_(c), d_(d)
30 {
31 if (std::abs(a_) < 1e-9)
32 {
33 OGS_FATAL("'a' must be non-zero for a cubic equation.");
34 }
35 }
#define OGS_FATAL(...)
Definition Error.h:26

References a_, and OGS_FATAL.

Member Function Documentation

◆ smallestPositiveRealRoot()

double MathLib::CubicSolver::smallestPositiveRealRoot ( )
inline

Definition at line 68 of file CubicRoots.h.

69 {
70 std::vector<double> const roots = solve();
71
72 auto positive_roots =
73 roots | ranges::views::filter([](double root) { return root > 0; });
74
75 // If no positive root exists, return NaN
76 if (ranges::empty(positive_roots))
77 {
78 return std::numeric_limits<double>::quiet_NaN();
79 }
80
81 return ranges::min(positive_roots);
82 }
std::vector< double > solve()
Definition CubicRoots.h:37

References solve().

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

◆ solve()

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

Definition at line 37 of file CubicRoots.h.

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

References a_, b_, c_, and d_.

Referenced by smallestPositiveRealRoot().

Member Data Documentation

◆ a_

double MathLib::CubicSolver::a_
private

Definition at line 85 of file CubicRoots.h.

Referenced by CubicSolver(), and solve().

◆ b_

double MathLib::CubicSolver::b_
private

Definition at line 85 of file CubicRoots.h.

Referenced by solve().

◆ c_

double MathLib::CubicSolver::c_
private

Definition at line 85 of file CubicRoots.h.

Referenced by solve().

◆ d_

double MathLib::CubicSolver::d_
private

Definition at line 85 of file CubicRoots.h.

Referenced by solve().


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