Skip to content
Open
Show file tree
Hide file tree
Changes from 47 commits
Commits
Show all changes
73 commits
Select commit Hold shift + click to select a range
6c7d2ec
Started exploring what a test file would look like
osanstrong Aug 20, 2025
f3bf765
Merge branch 'celeritas-project:develop' into ferrari
osanstrong Aug 20, 2025
b6c3db4
Enumerated certain test cases for ferrari solver
osanstrong Aug 20, 2025
77c9964
Refined set of test case decsriptions, should be ready to actually de…
osanstrong Aug 20, 2025
db6f40b
Added harness class to FerrariSolver test, to hopefully streamline pr…
osanstrong Aug 20, 2025
feba8db
started adding tests using harness
osanstrong Aug 21, 2025
5776732
Added test cases for quartic solver and dominant cubic solver
osanstrong Aug 22, 2025
4b93605
Added Ferrari Solver
osanstrong Sep 23, 2025
483794c
Added ferrari test file to celeritas test lists
osanstrong Sep 23, 2025
261f9aa
Started fixing bugs in ferrari solver
osanstrong Sep 23, 2025
4f5ebeb
Added missing semicolon, comma
osanstrong Sep 23, 2025
f89b787
fixed syntax issues, added temporary debugging statements
osanstrong Sep 23, 2025
4affdce
fixed two flipped signs on test polynomials, and one which worked but…
osanstrong Sep 23, 2025
7a5f923
Added separate quadratic solver to also provide negative real roots, …
osanstrong Sep 23, 2025
a82f53d
Removed hacky print statements for debugging
osanstrong Sep 23, 2025
49504b1
corrected typo saying 'two solutions' to a quartic instead of four
osanstrong Oct 5, 2025
fa5d229
Generalized quartic solver harness and made ferrari solver test harne…
osanstrong Oct 5, 2025
7213cf8
Renamed FerrariSolver to QuarticSolver
osanstrong Oct 5, 2025
bbdf205
Merge branch 'celeritas-project:develop' into ferrari
osanstrong Oct 5, 2025
94c6673
Merge branch 'ferrari' of github.com:osanstrong/celeritas into ferrari
osanstrong Oct 5, 2025
9b56efe
Added precommit file format checks
osanstrong Oct 6, 2025
01cae24
Removed unused field for e/a
osanstrong Oct 6, 2025
bdcff63
Removed accidentally created/misnamed bcmake config file
osanstrong Oct 8, 2025
d567eb7
Switched special case equality/zero checks to use SoftZero instead of…
osanstrong Oct 20, 2025
7fba091
Removed redundant zero-e operator definition
osanstrong Oct 21, 2025
63918fd
Added citation for Algorithm 1010
osanstrong Oct 21, 2025
4079736
Renamed Roots2 alias to Real2 for consistency with other project files
osanstrong Oct 21, 2025
a7fbc4c
Moved logic for special biquadratic case to helper function
osanstrong Oct 21, 2025
d17c83b
Adjusted formatting of equations in documentation
osanstrong Oct 21, 2025
7540c6b
Removed unnecessary TODO statements
osanstrong Oct 21, 2025
2420744
Replaced self-square notation with ipow<2>
osanstrong Oct 21, 2025
0acf806
Replaced method to add in sorted list iwth method to add, and subsequ…
osanstrong Oct 21, 2025
f7bf2fa
Started adding typed testing to replace individually declared tests i…
osanstrong Oct 21, 2025
9f42f7e
Changed quartic unit tests to a templated test framework to use TYPED…
osanstrong Oct 21, 2025
1a4b6a2
Replaced cubic helper function tests with quartic tests which ensure …
osanstrong Oct 21, 2025
52e241c
Privatized helper functions in FerrariSolver
osanstrong Oct 21, 2025
dc23441
Added real_type wrappers to avoid potential double casting issues for…
osanstrong Oct 21, 2025
7b3b886
Cleaned up definition docstrings
osanstrong Oct 21, 2025
08f6a8e
Replaced manual lists of no_intersection() with lists of valid inters…
osanstrong Oct 21, 2025
9232367
Removed unused qb squared variable
osanstrong Oct 21, 2025
90f4eb6
Replaced x**n with x^n for readability
osanstrong Oct 21, 2025
0302df9
Updated docstrings for consistency
osanstrong Oct 22, 2025
21bc819
Fix manual placement indexing to work as described
osanstrong Oct 22, 2025
5cfa4e3
Added citation to Ferrari-Cardano method from mathematical handbook
osanstrong Nov 11, 2025
21edada
Removed odd spacing from equations
osanstrong Nov 11, 2025
624e2db
Refactored FerrariSolver to be constructed with tolerance and take ab…
osanstrong Nov 11, 2025
5c6dbdf
Renamed vague 'TestTypes' to specifically 'QuarticSolver' for clarity…
osanstrong Nov 11, 2025
6dd516f
Shifted certain unit tests which were moreso testing precision, chang…
osanstrong Nov 29, 2025
68d750a
Changed ferrari solver to use Numeric Recipes cubic solver to match A…
osanstrong Nov 29, 2025
0e54a60
Refactored FerrariSolver to use operator with option to specify surfa…
osanstrong Dec 1, 2025
a97c130
Refactored QuarticSolver test for clarity
osanstrong Dec 1, 2025
181397e
Made internal functions and tolerance member functions/data for each …
osanstrong Dec 3, 2025
957773e
Fix typo of description of surface three root test
osanstrong Dec 3, 2025
12c8c53
Fixed soft zero to be definitively constructed as member data
osanstrong Dec 4, 2025
00fa4d8
Removed unwise tolerance for cases near tangential intersections in s…
osanstrong Dec 4, 2025
de5dc0f
Nudged certain unit tests away from edges, where they were more actin…
osanstrong Dec 4, 2025
8ceafa7
Refactored if possible, or removed if not, surface unit tests which t…
osanstrong Dec 4, 2025
860902b
Removed Cardano from name of Ferrari solver, as it does not exclusive…
osanstrong Dec 5, 2025
21f6f86
Moved surface solution implementation to surface solution operator, a…
osanstrong Dec 5, 2025
dd90f38
Added expectation that a is not zero, as cubic special case makes e z…
osanstrong Dec 5, 2025
a7fe938
Removed sorting from FerrariSolver, added sorter to quartic tests for…
osanstrong Dec 5, 2025
cd455cc
Removed surface state argument in general solver case; expectation wi…
osanstrong Dec 5, 2025
1c5498f
Removed unused iostream include
osanstrong Dec 5, 2025
46d079b
Renamed UTIL section to HELPER FUNCTIONS
osanstrong Dec 5, 2025
dbef116
Removed separator between ININE DEFINITIONS section and the first fun…
osanstrong Dec 5, 2025
bc1272d
Moved separator bar to after TYPED_TEST_SUITE call
osanstrong Dec 5, 2025
c538234
Merge branch 'celeritas-project:develop' into ferrari
osanstrong Dec 5, 2025
e487c26
Merge remote-tracking branch 'upstream/develop' into ferrari
sethrj Dec 5, 2025
be90c5c
Remove orange specific code
sethrj Dec 5, 2025
4dd5fe3
Move quartic solver to corecel/math
sethrj Dec 5, 2025
535aa29
Add doc
sethrj Dec 5, 2025
3164480
Fix docs
sethrj Dec 5, 2025
58bf4c5
Revert "Fix docs"
sethrj Dec 5, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
347 changes: 347 additions & 0 deletions src/orange/surf/detail/FerrariSolver.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,347 @@
//------------------------------- -*- C++ -*- -------------------------------//
// Copyright Celeritas contributors: see top-level COPYRIGHT file for details
// SPDX-License-Identifier: (Apache-2.0 OR MIT)
//---------------------------------------------------------------------------//
//! \file orange/surf/detail/FerrariSolver.hh
//---------------------------------------------------------------------------//
#pragma once

#include <cmath>
#include <iostream>

#include "corecel/Types.hh"
#include "corecel/cont/Array.hh"
#include "corecel/math/Algorithms.hh"
#include "corecel/math/PolyEvaluator.hh"
#include "corecel/math/SoftEqual.hh"
#include "orange/OrangeTypes.hh"
#include "orange/surf/detail/QuadraticSolver.hh"

namespace celeritas
{
namespace detail
{
//---------------------------------------------------------------------------//
/*!
* Find positive, real, non-zero roots for quartic functions using the
* Ferrari-Cardano method\citet{polyanin-ferrari-2007,
* https://doi.org/10.1201/9781420010510}.
*
* The quartic equation
* \f[
a x^4 + b x^3 + c x^2 + d x + e = 0
* \f]
* has four solutions mathematically, but we only require solutions which are
* both real and positive. This equation is also subject to multiple cases of
* catastrophic precision-limitation-based error both fundamentally and as a
* consequence of the particular algorithm chosen. This solver implements the
* Ferrari-Cardano method, which is well-established and simple, but more
* prone to numerical error than contemporary methods to be explored such as
* Algorithm 1010\citet{orellana-alg1010-2020,
* https://doi.org/10.1145/3386241}.
*
* \return An Intersections array where each item is either a positive valid
* intersection or the sentinel result \c no_intersection().
*/
class FerrariSolver
{
public:
//!@{
//! \name Type aliases
using Intersections = Array<real_type, 4>;
using Real2 = Array<real_type, 2>;
using Real5 = Array<real_type, 5>;
//!@}

// Solve with manually specified surface state
static inline CELER_FUNCTION Intersections solve_general(
Real5 abcde,
SurfaceState on_surface,
real_type tolerance = Tolerance<real_type>::sqrt_quadratic());

public:
// Construct w/ given tolerance. Uses quadratic tolerance by default.
inline CELER_FUNCTION
FerrariSolver(real_type tolerance = Tolerance<real_type>::sqrt_quadratic());

// Solver for fully general case
inline CELER_FUNCTION Intersections operator()(Real5 abcde) const;

private:
//// DATA ////
// Soft zero for biquadratic and degenerate cubic detection
static inline SoftZero<real_type> const soft_zero_;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be member data, not static

Suggested change
static inline SoftZero<real_type> const soft_zero_;
SoftZero<real_type> const soft_zero_;


//// UTIL ////
// Try to place real at given index in list, return next free index
static inline CELER_FUNCTION int
place_root(Intersections& roots, real_type new_root, int free_index);

// Find roots of special reduced quartic which is biquadratic
static inline CELER_FUNCTION Intersections
calc_biquadratic_roots(real_type qb, real_type p, real_type r);

// Find dominant root of normalized cubic
static inline CELER_FUNCTION real_type
dominant_root_normalized_cubic(real_type b, real_type c, real_type d);

// Find real quadratic roots
static inline CELER_FUNCTION Real2
real_roots_normalized_quadratic(real_type b, real_type c);
};

//---------------------------------------------------------------------------//
// INLINE DEFINITIONS
//---------------------------------------------------------------------------//
/*!
* Find all positive roots for quartic surfaces using Ferrari-Cardano method.
*
* This method allows the user to manually specify if the particle starts on
* the surface, and use 0 instead of e for accuracy.
*/
CELER_FUNCTION auto FerrariSolver::solve_general(Real5 abcde,
SurfaceState on_surface,
real_type tolerance)
-> Intersections
{
FerrariSolver solve(tolerance);
auto selected_abcde = abcde;
if (on_surface == SurfaceState::on)
{
auto [a, b, c, d, e] = abcde;
selected_abcde = {a, b, c, d, 0};
}
return solve(selected_abcde);
}

//---------------------------------------------------------------------------//
/*!
* Construct a solver instance with a specified tolerance for degenerate cases,
* such as the particle starting on the surface.
*/
CELER_FUNCTION
FerrariSolver::FerrariSolver(real_type tolerance)
{
SoftZero soft_zero_{tolerance};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To construct as member data:

Suggested change
SoftZero soft_zero_{tolerance};
FerrariSolver::FerrariSolver(real_type tolerance)
: soft_zero_{tolerance}
{

}

//---------------------------------------------------------------------------//
/*!
* Find all positive roots of the polynomial with given a, b, c, d, e.
*
* The polynomial takes the form:
* \f[
ax^4 + bx^3 + cx^2 + dx + e = 0.
*\f]
* Where the given array abcde corresponds to {a, b, c, d, e}.
* Replaces negative or complex roots with sentry value no_intersection().
*/
CELER_FUNCTION auto FerrariSolver::operator()(Real5 abcde) const
-> Intersections
{
// Normalize coefficients
auto [a, b, c, d, e] = abcde;
real_type ba = b / a, ca = c / a, da = d / a, ea = e / a;
constexpr real_type half{0.5};
real_type qb = real_type{0.25} * ba;

// Incomplete quartic
real_type p = PolyEvaluator{-half * ca, 0, 3}(qb);
real_type q = PolyEvaluator{half * da, -ca, 0, 4}(qb);
real_type r = PolyEvaluator{-ea, da, -ca, 0, 3}(qb);

// Edge case: equation is biquadratic
if (soft_zero_(q))
{
return calc_biquadratic_roots(qb, p, r);
}

// One real root of subsidiary cubic
real_type z0 = FerrariSolver::dominant_root_normalized_cubic(
p, r, p * r - half * q * q);

real_type s2 = 2 * p + 2 * z0;
if (s2 >= 0)
{
real_type s = std::sqrt(s2);
real_type t;
if (soft_zero_(s))
{
t = z0 * z0 + r;
}
else
{
t = -q / s;
}
auto const [r0, r1] = real_roots_normalized_quadratic(s * half, z0 + t);
auto const [r2, r3]
= real_roots_normalized_quadratic(-s * half, z0 - t);

Intersections roots(no_intersection(),
no_intersection(),
no_intersection(),
no_intersection());
int idx = 0;
idx = place_root(roots, r0 - qb, idx);
idx = place_root(roots, r1 - qb, idx);
idx = place_root(roots, r2 - qb, idx);
idx = place_root(roots, r3 - qb, idx);

sort(&roots[0], &roots[idx]);

return roots;
}
else
{
return Intersections(no_intersection(),
no_intersection(),
no_intersection(),
no_intersection());
}
}

//---------------------------------------------------------------------------//
/*!
* Attempt to put a value into the given list at given index, returning where
* to place the next item.
*
* If the given value is no_intersection() or is not positive, does not place
* the root, and returns the same index for the next one.
*/
CELER_FUNCTION int FerrariSolver::place_root(Intersections& roots,
real_type new_root,
int free_index)
{
if (!(new_root == no_intersection() || new_root <= 0))
{
roots[free_index] = new_root;
free_index += 1;
}
return free_index;
}

//---------------------------------------------------------------------------//
/*!
* Solve special case of Ferrari where reduced quartic is also biquadratic.
*
* In this special case, the normal solution won't work, and must instead be
* solved as a quadratic equation: The square roots of each quadratic solution
* then go on to form potential quartic solutions, for up to four roots.
*/
CELER_FUNCTION auto
FerrariSolver::calc_biquadratic_roots(real_type qb, real_type p, real_type r)
-> Intersections
{
auto ir = real_roots_normalized_quadratic(-p, -r);
Intersections roots(no_intersection(),
no_intersection(),
no_intersection(),
no_intersection());
int idx = 0;
if (ir[1] != no_intersection() && ir[1] > 0)
{
real_type sqrt_ir1 = std::sqrt(ir[1]);
real_type from_pos1 = sqrt_ir1 - qb;
idx = place_root(roots, from_pos1, idx);
if (from_pos1 > 0)
{
idx = place_root(roots, -sqrt_ir1 - qb, idx);
}
}
if (ir[0] != no_intersection() && ir[0] > 0)
{
real_type sqrt_ir0 = std::sqrt(ir[0]);
real_type from_pos0 = sqrt_ir0 - qb;
idx = place_root(roots, from_pos0, idx);
if (from_pos0 > 0)
{
idx = place_root(roots, -sqrt_ir0 - qb, idx);
}
}
sort(&roots[0], &roots[idx]);
return roots;
}

//---------------------------------------------------------------------------//
/*!
* Solve for the dominant root of a cubic function.
*
* Specifically, the cubic function
* \f[
a x^3 + b x^2 + c x + d
* \f]
* where a is assumed to already be 1, and is not provided to the
* function.
*
* \return The dominant real root of the given cubic equation.
*/
CELER_FUNCTION real_type FerrariSolver::dominant_root_normalized_cubic(
real_type b, real_type c, real_type d)
{
constexpr real_type half = real_type{0.5};
constexpr real_type third = real_type{1} / real_type{3};
real_type third_b = b * third;

// Intermediate values
real_type f = third * c - ipow<2>(third_b);
real_type g = PolyEvaluator{d, -c, 0, 2}(third_b);
real_type h = real_type{0.25} * ipow<2>(g) + ipow<3>(f);

if (soft_zero_(f) && soft_zero_(g) && soft_zero_(h))
{
return -std::cbrt(d);
}
else if (h <= 0)
{
real_type j = std::sqrt(-f);
real_type k = std::acos(-half * g / ipow<3>(j));
real_type m = std::cos(third * k);
return 2 * j * m - third_b;
}
else
{
real_type sqrt_h = std::sqrt(h);
real_type s = std::cbrt(-half * g + sqrt_h);
real_type u = std::cbrt(-half * g - sqrt_h);
return s + u - third_b;
}
}

//---------------------------------------------------------------------------//
/*!
* Solve for the real roots of a quadratic function.
*
* Specifically, the quadratic function
* \f[
a x^2 + (hb*2) x + c
* \f]
* where a is assumed to already be 1 and not provided.
*
* \return A pair of roots. If roots are imaginary, returns 2x
* no_intersection().
*/
CELER_FUNCTION auto
FerrariSolver::real_roots_normalized_quadratic(real_type hb, real_type c)
-> Real2
{
real_type qb2 = ipow<2>(hb);
if (soft_zero_(qb2 - c))
{
// One critical root
return Real2(-hb, no_intersection());
}
else if (qb2 > c)
{
// Two real roots
real_type ht = std::sqrt(qb2 - c);
return Real2(-hb - ht, -hb + ht);
}
else
{
return Real2(no_intersection(), no_intersection());
}
}

//---------------------------------------------------------------------------//
} // namespace detail
} // namespace celeritas
1 change: 1 addition & 0 deletions test/orange/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ celeritas_add_device_test(surf/LocalSurfaceVisitor)

# Surface details
celeritas_add_test(surf/detail/QuadraticSolver.test.cc)
celeritas_add_test(surf/detail/QuarticSolver.test.cc)
celeritas_add_test(surf/detail/SurfaceTranslator.test.cc)
celeritas_add_test(surf/detail/SurfaceTransformer.test.cc)
celeritas_add_test(surf/detail/InvoluteSolver.test.cc)
Expand Down
Loading
Loading