Skip to content

Commit 2d1ca76

Browse files
committed
Update to rcpr sans truncation threshold
1 parent db0a527 commit 2d1ca76

7 files changed

Lines changed: 44 additions & 14 deletions

File tree

‎Cargo.toml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ rayon = "1.10.0"
2424
geo = {version = "0.25", optional = false}
2525
indicatif = {version = "0.18.6", features=["rayon"]}
2626
serde = { version = "1.0.163", features = ["derive"] }
27-
rcpr = { git = "https://github.com/drobnyjt/rcpr", branch="main", optional=true}
27+
rcpr = { git = "https://github.com/drobnyjt/rcpr", branch="trucnation_fixes", optional=true}
2828
ndarray = {version = "0.17.2", features = ["serde"], optional = true}
2929
parry3d-f64 = {optional = true, version="0.2.0"}
3030
pyo3 = {version = "0.29.0", optional=true}

‎examples/f_kporha_et_al_benchmark.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def input_file(ion, target, incident_energy, angle, number_ions=1000, pot="WW",
2727
else:
2828
pot_name = "MORSE"
2929

30-
cpr = {'CPR': {'n0': 2, 'nmax': 32, 'epsilon': 5e-4, 'complex_threshold': 1E-9, 'truncation_threshold': 1E-9, 'far_from_zero': 1e3, 'interval_limit': 1E-4, 'derivative_free': True}}
30+
cpr = {'CPR': {'n0': 2, 'nmax': 32, 'epsilon': 5e-4, 'complex_threshold': 1E-9, 'far_from_zero': 1e3, 'interval_limit': 1E-4, 'derivative_free': True}}
3131
options = {
3232
'name': f'input_file_{ion["symbol"]}_{target["symbol"]}_{np.round(angle, 1)}_{np.round(incident_energy/1000, 4)}_{pot_name}_{Es}',
3333
'track_trajectories': False, # whether to track trajectories for plotting; memory intensive

‎examples/test_morse.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def run_morse_potential(energy, index, num_samples=10000, run_sim=True):
2929
mean_free_path_model = "LIQUID"
3030
interaction_potential = [[{{"MORSE"={{D=5.4971E-20, r0=2.782E-10, alpha=1.4198E10}}}}]]
3131
scattering_integral = [["GAUSS_LEGENDRE"]]
32-
root_finder = [[{{"CPR"={{n0={n0}, nmax={nmax}, epsilon={epsilon}, complex_threshold=1E-9, truncation_threshold=1E-9, far_from_zero=1E22, interval_limit={interval_limit}, derivative_free=true}}}}]]
32+
root_finder = [[{{"CPR"={{n0={n0}, nmax={nmax}, epsilon={epsilon}, complex_threshold=1E-9, far_from_zero=1E22, interval_limit={interval_limit}, derivative_free=true}}}}]]
3333
num_threads = 4
3434
num_chunks = 10
3535
@@ -91,7 +91,7 @@ def run_krc_morse_potential(energy, index, num_samples=10000, run_sim=True):
9191
mean_free_path_model = "LIQUID"
9292
interaction_potential = [[{{"KRC_MORSE"={{D=5.4971E-20, r0=2.782E-10, alpha=1.4198E10, k=8E10, x0=0.75E-10}}}}]]
9393
scattering_integral = [["GAUSS_LEGENDRE"]]
94-
root_finder = [[{{"CPR"={{n0=3, nmax={nmax}, epsilon={epsilon}, complex_threshold=1E-9, truncation_threshold=1E-9, far_from_zero=1E22, interval_limit={interval_limit}, derivative_free=true}}}}]]
94+
root_finder = [[{{"CPR"={{n0=3, nmax={nmax}, epsilon={epsilon}, complex_threshold=1E-9, far_from_zero=1E22, interval_limit={interval_limit}, derivative_free=true}}}}]]
9595
num_threads = 6
9696
num_chunks = 1
9797

‎src/bca.rs‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -412,8 +412,8 @@ fn distance_of_closest_approach(particle_1: &particle::Particle, particle_2: &pa
412412
Rootfinder::POLYNOMIAL{complex_threshold} => polynomial_rootfinder(Za, Zb, Ma, Mb, E0, p, interaction_potential, complex_threshold)
413413
.with_context(|| format!("Numerical error: Polynomial rootfinder failed for {} at {} eV with p = {} A.", interaction_potential, E0/EV, p/ANGSTROM))
414414
.unwrap(),
415-
Rootfinder::CPR{n0, nmax, epsilon, complex_threshold, truncation_threshold, far_from_zero, interval_limit, derivative_free} =>
416-
cpr_rootfinder(Za, Zb, Ma, Mb, E0, p, interaction_potential, n0, nmax, epsilon, complex_threshold, truncation_threshold, far_from_zero, interval_limit, derivative_free)
415+
Rootfinder::CPR{n0, nmax, epsilon, complex_threshold, far_from_zero, interval_limit, derivative_free} =>
416+
cpr_rootfinder(Za, Zb, Ma, Mb, E0, p, interaction_potential, n0, nmax, epsilon, complex_threshold, far_from_zero, interval_limit, derivative_free)
417417
.with_context(|| format!("Numerical error: CPR rootfinder failed for {} at {} eV with p = {} A.", interaction_potential, E0/EV, p/ANGSTROM))
418418
.unwrap(),
419419
Rootfinder::NEWTON{max_iterations, tolerance} => newton_rootfinder(Za, Zb, Ma, Mb, E0, p, interaction_potential, max_iterations, tolerance)
@@ -620,15 +620,14 @@ fn transform(x: f64) -> f64 {
620620
/// `epsilon`: absolute tolerance of Chebyshev interpolant.
621621
/// `complex_threshold`: slightly-complex roots with an imaginary part below this value are considered real.
622622
/// `far_from_zero`: if the distance of closest approach function, evaluated over an interval [a, b] on the Lobatto grid, is always greater than this value, it is assumed that there are no roots in the interval [a, b].
623-
/// `truncation_threshold`: trailing terms of the Chebyshev interpolant with coefficients smaller than this value are ignored.
624623
/// `interval_limit`: if subdivision produces an interval smaller than this value, the root-finder will panic.
625624
/// `derivative_free`: if false, use Newton's method to polish roots from the CPR. If true, use the secant method.
626625
///
627626
/// # Returns
628627
/// Returns the distance of closest approach (reduced by a) or an error if the root-finder failed.
629628
pub fn cpr_rootfinder(Za: f64, Zb: f64, Ma: f64, Mb: f64, E0: f64, impact_parameter: f64,
630629
interaction_potential: InteractionPotential, n0: usize, nmax: usize, epsilon: f64,
631-
complex_threshold: f64, truncation_threshold: f64, far_from_zero: f64,
630+
complex_threshold: f64, far_from_zero: f64,
632631
interval_limit: f64, derivative_free: bool) -> Result <f64, anyhow::Error> {
633632

634633
//Lindhard screening length and reduced energy
@@ -650,7 +649,6 @@ pub fn cpr_rootfinder(Za: f64, Zb: f64, Ma: f64, Mb: f64, E0: f64, impact_parame
650649
n0,
651650
nmax,
652651
complex_threshold,
653-
truncation_threshold,
654652
far_from_zero,
655653
interval_limit
656654
);

‎src/enums.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ impl PartialEq for ScatteringIntegral {
241241
pub enum Rootfinder {
242242
/// Newton root-finder with user-defined `max_iterations` and `tolerance`.
243243
NEWTON{max_iterations: usize, tolerance: f64},
244-
CPR{n0: usize, nmax: usize, epsilon: f64, complex_threshold: f64, truncation_threshold: f64,
244+
CPR{n0: usize, nmax: usize, epsilon: f64, complex_threshold: f64,
245245
far_from_zero: f64, interval_limit: f64, derivative_free: bool},
246246
POLYNOMIAL{complex_threshold: f64},
247247
DEFAULTNEWTON,
@@ -251,7 +251,7 @@ impl fmt::Display for Rootfinder {
251251
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
252252
match *self {
253253
Rootfinder::NEWTON{max_iterations, tolerance} => write!(f, "Newton-Raphson Rootfinder with maximum {} iterations and toleance = {}", max_iterations, tolerance),
254-
Rootfinder::CPR{n0, nmax, epsilon, complex_threshold, truncation_threshold, far_from_zero, interval_limit, derivative_free} =>
254+
Rootfinder::CPR{n0, nmax, epsilon, complex_threshold, far_from_zero, interval_limit, derivative_free} =>
255255
write!(f, "Chebyshev-Proxy Rootfinder with {}-polishing", match derivative_free { true => "Secant", false => "Newton"}),
256256
Rootfinder::POLYNOMIAL{complex_threshold} => write!(f, "Frobenius Companion Matrix Polynomial Real Rootfinder with a complex tolerance of {}", complex_threshold),
257257
Rootfinder::DEFAULTNEWTON => write!(f, "Newton-Raphson Rootfinder with maximum {} iterations and toleance = {}", 100, 1E-3),

‎src/lib.rs‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,9 @@ mod libRustBCA {
145145

146146
#[pymodule_export]
147147
use super::rustbca_local_py;
148+
149+
#[pymodule_export]
150+
use super::scattering_integral_morse;
148151
}
149152

150153
#[cfg(feature = "python")]
@@ -2129,6 +2132,7 @@ pub fn compound_reflection_coefficient<'py>(ion: &Bound<'py, PyDict>, targets: V
21292132
/// E. Kadric et al., IEEE Transactions on Computers 65 11
21302133
/// doi: 10.1109/TC.2016.2532874
21312134
#[cfg(feature = "python")]
2135+
#[allow(dead_code)]
21322136
fn moller_knuth_two_sum(a: f64, b: f64) -> (f64, f64) {
21332137
let s = a + b;
21342138
let b_prime = s - a;
@@ -2169,6 +2173,36 @@ fn scattering_integrals(Za: f64, Zb: f64, Ma: f64, Mb: f64, E0: f64, p: f64, n_g
21692173
Ok((theta_gm, theta_gl, theta_mw, theta_magic))
21702174
}
21712175

2176+
#[cfg(feature = "python")]
2177+
#[pyfunction]
2178+
#[pyo3(signature = (Za, Zb, Ma, Mb, E0, p, d, alpha, r0, n0=2, nmax=64, epsilon=1e-6, complex_threshold=0.0, far_from_zero=1e22, interval_limit=1e-3, n_gl_points=100, interaction_potential="KR_C"))]
2179+
fn scattering_integral_morse(Za: f64, Zb: f64, Ma: f64, Mb: f64, E0: f64, p: f64, d: f64, alpha: f64, r0: f64, n0: usize, nmax: usize, epsilon: f64, complex_threshold: f64, far_from_zero: f64, interval_limit: f64, n_gl_points: usize, interaction_potential: &str) -> PyResult<(f64, f64, f64)> {
2180+
let E0 = E0*EV;
2181+
let p = p*ANGSTROM;
2182+
let d = d*EV;
2183+
let alpha = alpha/ANGSTROM;
2184+
let r0 = r0*ANGSTROM;
2185+
2186+
let potential = InteractionPotential::MORSE{D: d, alpha: alpha, r0: r0};
2187+
let screening_length = interactions::screening_length(Za, Zb, potential);
2188+
2189+
let x0 = bca::cpr_rootfinder(
2190+
Za, Zb, Ma, Mb, E0, p,
2191+
potential, n0, nmax, epsilon,
2192+
complex_threshold,
2193+
far_from_zero, interval_limit, true
2194+
).map_err(|e| PyValueError::new_err(format!(
2195+
"Failed to find root: {}; p={}, E={}", e, p/ANGSTROM, E0/EV
2196+
)))?;
2197+
2198+
//Compute center of mass deflection angle with each algorithm
2199+
let theta_gm = bca::gauss_mehler(Za, Zb, Ma, Mb, E0, p, x0, screening_length, potential, n_gl_points);
2200+
let theta_gl = bca::gauss_legendre(Za, Zb, Ma, Mb, E0, p, x0, screening_length, potential);
2201+
2202+
2203+
Ok((x0, theta_gm, theta_gl))
2204+
}
2205+
21722206
#[cfg(feature = "python")]
21732207
#[pyfunction]
21742208
#[pyo3(signature=(input, geometry_mode="1D"))]

‎src/tests.rs‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -788,15 +788,13 @@ fn test_momentum_conservation() {
788788
#[cfg(not(feature = "cpr_rootfinder"))]
789789
let rootfinders = vec![Rootfinder::NEWTON{max_iterations: 100, tolerance: 1E-3}; 4];
790790

791-
//[[{"CPR"={n0=2, nmax=100, epsilon=1E-9, complex_threshold=1E-3, truncation_threshold=1E-9, far_from_zero=1E9, interval_limit=1E-12, derivative_free=true}}]]
792791
#[cfg(feature = "cpr_rootfinder")]
793792
rootfinders.push(
794793
Rootfinder::CPR{
795794
n0: 2,
796795
nmax: 100,
797796
epsilon: 1e-9,
798797
complex_threshold: 1e-3,
799-
truncation_threshold: 1e-9,
800798
far_from_zero: 1e9,
801799
interval_limit:1e-12,
802800
derivative_free: true
@@ -1171,7 +1169,7 @@ fn test_quadrature() {
11711169

11721170
//If cpr_rootfinder is enabled, compare Newton to CPR - they should be nearly identical
11731171
#[cfg(feature = "cpr_rootfinder")]
1174-
if let Ok(x0_cpr) = bca::cpr_rootfinder(Za, Zb, Ma, Mb, E0, p, interaction_potential, 2, 10000, 1E-6, 1E-6, 1E-9, 1E9, 1E-13, true) {
1172+
if let Ok(x0_cpr) = bca::cpr_rootfinder(Za, Zb, Ma, Mb, E0, p, interaction_potential, 2, 10000, 1E-6, 1E-6, 1E-9, 1E9, true) {
11751173
println!("CPR: {} Newton: {}", x0_cpr, x0_newton);
11761174
assert!(approx_eq!(f64, x0_newton, x0_cpr, epsilon=1E-3));
11771175
};

0 commit comments

Comments
 (0)