Skip to content

Commit 54d9256

Browse files
committed
Cached screening lengths (needs testing); cargo clippy suggestions.
1 parent 996fd3e commit 54d9256

4 files changed

Lines changed: 32 additions & 34 deletions

File tree

‎src/bca.rs‎

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -510,8 +510,8 @@ pub fn calculate_binary_collision(particle_1: &particle::Particle, particle_2: &
510510
_ => x0*a*(theta/2.).sin()
511511
};
512512

513-
let psi = theta.sin().atan2(Ma/Mb + theta.cos());//.abs();
514-
let psi_recoil = theta.sin().atan2(1. - theta.cos());//.abs();
513+
let psi = theta.sin().atan2(Ma/Mb + theta.cos());
514+
let psi_recoil = theta.sin().atan2(1. - theta.cos());
515515
let recoil_energy = 4.*(Ma*Mb)/(Ma + Mb).powi(2)*E0*(theta/2.).sin().powi(2);
516516

517517
Ok(BinaryCollisionResult::new(theta, psi, psi_recoil, recoil_energy, asymptotic_deflection, x0))
@@ -583,18 +583,6 @@ pub fn polynomial_rootfinder(Za: f64, Zb: f64, Ma: f64, Mb: f64, E0: f64, impact
583583
let coefficients = interactions::polynomial_coefficients(relative_energy, impact_parameter, interaction_potential);
584584
let roots = real_polynomial_roots(coefficients.clone(), polynom_complex_threshold).unwrap();
585585

586-
/*
587-
println!("p={}", impact_parameter/ANGSTROM);
588-
589-
for coefficient in &coefficients {
590-
println!("{}", {coefficient});
591-
}
592-
593-
for root in &roots {
594-
println!("{} A", {root});
595-
}
596-
*/
597-
598586
let max_root = roots.iter().cloned().fold(f64::NAN, f64::max);
599587

600588
let inverse_transformed_root = interactions::inverse_transform(max_root, interaction_potential);
@@ -642,8 +630,6 @@ pub fn cpr_rootfinder(Za: f64, Zb: f64, Ma: f64, Mb: f64, E0: f64, impact_parame
642630
let g = |r: f64| -> f64 {interactions::distance_of_closest_approach_function_singularity_free(r, a, Za, Zb, relative_energy, impact_parameter, interaction_potential)*
643631
interactions::scaling_function(r, impact_parameter, interaction_potential)};
644632

645-
//Using upper bound const, ~10, construct upper bound as a plateau near 0 and linear increase away from that
646-
//let upper_bound = f64::max(upper_bound_const*p, upper_bound_const*a);
647633
let upper_bound = impact_parameter + interactions::crossing_point_doca(interaction_potential);
648634

649635
let roots = match derivative_free {
@@ -683,14 +669,16 @@ pub fn newton_rootfinder(Za: f64, Zb: f64, Ma: f64, Mb: f64, E0: f64, impact_par
683669
let f = |r: f64| -> f64 {interactions::distance_of_closest_approach_function(r, a, Za, Zb, relative_energy, impact_parameter, interaction_potential)};
684670
let df = |r: f64| -> f64 {interactions::diff_distance_of_closest_approach_function(r, a, Za, Zb, relative_energy, impact_parameter, interaction_potential)};
685671

686-
//Guess for large reduced energy from Mendenhall and Weller 1991
687-
//For small energies, use pure Newton-Raphson with arbitrary guess of 1
688-
let mut x0 = beta;
689672
let mut xn: f64;
690-
if reduced_energy > 5. {
673+
674+
//Guess for large reduced energy from Mendenhall and Weller 1991
675+
//For small energies, use pure Newton-Raphson with arbitrary guess of beta
676+
let mut x0 = if reduced_energy > 5. {
691677
let inv_er_2 = 0.5/reduced_energy;
692-
x0 = inv_er_2 + (inv_er_2*inv_er_2 + beta*beta).sqrt();
693-
}
678+
inv_er_2 + (inv_er_2*inv_er_2 + beta*beta).sqrt()
679+
} else {
680+
beta
681+
};
694682

695683
//Newton-Raphson to determine distance of closest approach
696684
let mut err: f64 = tolerance + 1.;

‎src/geometry.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -758,7 +758,7 @@ impl Triangle2D {
758758

759759
/// Calculates the shortest distance from this triangle to the point (x, y).
760760
pub fn distance_to(&self, x: f64, y: f64) -> f64 {
761-
let mut distance_to = std::f64::MAX;
761+
let mut distance_to = f64::MAX;
762762

763763
for segment in &self.segments {
764764
let length_2 = (segment.2 - segment.0)*(segment.2 - segment.0) + (segment.3 - segment.1)*(segment.3 - segment.1);

‎src/interactions.rs‎

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use super::*;
2+
use cached::macros::cached;
23

34
/// Analytic solutions to outermost root of the interaction potential.
45
pub fn crossing_point_doca(interaction_potential: InteractionPotential) -> f64 {
@@ -271,17 +272,26 @@ pub fn dphi(xi: f64, interaction_potential: InteractionPotential) -> f64 {
271272
pub fn screening_length(Za: f64, Zb: f64, interaction_potential: InteractionPotential) -> f64 {
272273
match interaction_potential {
273274
//ZBL screening length, Eckstein (4.1.8)
274-
InteractionPotential::ZBL => 0.88534*A0/(Za.powf(0.23) + Zb.powf(0.23)),
275+
InteractionPotential::ZBL => zbl_screening_length(Za as u64, Zb as u64),
275276
//Lindhard/Firsov screening length, Eckstein (4.1.5)
276-
InteractionPotential::MOLIERE | InteractionPotential::KR_C | InteractionPotential::LENZ_JENSEN | InteractionPotential::TRIDYN | InteractionPotential::WW => 0.8853*A0*(Za.sqrt() + Zb.sqrt()).powf(-2./3.),
277-
InteractionPotential::LENNARD_JONES_12_6{..} | InteractionPotential::LENNARD_JONES_65_6{..} => 0.8853*A0*(Za.sqrt() + Zb.sqrt()).powf(-2./3.),
277+
InteractionPotential::MOLIERE | InteractionPotential::KR_C | InteractionPotential::LENZ_JENSEN | InteractionPotential::TRIDYN | InteractionPotential::WW => lindhard_screening_length(Za as u64, Zb as u64),
278+
InteractionPotential::LENNARD_JONES_12_6{..} | InteractionPotential::LENNARD_JONES_65_6{..} => lindhard_screening_length(Za as u64, Zb as u64),
278279
InteractionPotential::MORSE{D, alpha, r0} => alpha,
279-
InteractionPotential::COULOMB{Za: Z1, Zb: Z2} => 0.88534*A0/(Z1.powf(0.23) + Z2.powf(0.23)),
280+
InteractionPotential::COULOMB{Za: Z1, Zb: Z2} => zbl_screening_length(Za as u64, Zb as u64),
280281
InteractionPotential::KRC_MORSE{..} => 0.8853*A0*(Za.sqrt() + Zb.sqrt()).powf(-2./3.),
281282
InteractionPotential::FOUR_EIGHT{..} =>0.8853*A0*(Za.sqrt() + Zb.sqrt()).powf(-2./3.),
282283
}
283284
}
284285

286+
#[cached]
287+
fn zbl_screening_length(Za: u64, Zb: u64) -> f64{
288+
0.88534*A0/((Za as f64).powf(0.23) + (Zb as f64).powf(0.23))
289+
}
290+
#[cached]
291+
fn lindhard_screening_length(Za: u64, Zb: u64) -> f64 {
292+
0.8853*A0*((Za as f64).sqrt() + (Zb as f64).sqrt()).powf(-2./3.)
293+
}
294+
285295
/// Coefficients of inverse-polynomial interaction potentials.
286296
/// Note: these go in order of decreasing orders of r, e.g., [r^n, r^n-1, r^n-2, ... r, 1].
287297
pub fn polynomial_coefficients(relative_energy: f64, impact_parameter: f64, interaction_potential: InteractionPotential) -> Vec<f64> {
@@ -414,11 +424,11 @@ pub fn tungsten_tungsten_cubic_spline(r: f64) -> f64 {
414424

415425
} else if x <= x2 {
416426

417-
let a = [1.389653276380862E4,
418-
-3.596912431628216E4,
419-
3.739206756369099E4,
420-
-1.933748081656593E4,
421-
0.495516793802426E4,
427+
let a = [1.389653276380862E4,
428+
-3.596912431628216E4,
429+
3.739206756369099E4,
430+
-1.933748081656593E4,
431+
0.495516793802426E4,
422432
-0.050264585985867E4];
423433

424434
(a[0] + a[1]*x + a[2]*x.powi(2) + a[3]*x.powi(3) + a[4]*x.powi(4) + a[5]*x.powi(5))*EV

‎src/lib.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ pub extern "C" fn compound_tagged_bca_list_c(input: InputTaggedBCA) -> OutputTag
364364
}
365365

366366
#[no_mangle]
367-
pub extern "C" fn reflect_single_ion_c(num_species_target: &mut c_int, ux: &mut f64, uy: &mut f64, uz: &mut f64, E1: &mut f64, Z1: &mut f64, m1: &mut f64, Ec1: &mut f64, Es1: &mut f64, Z2: *mut f64, m2: *mut f64, Ec2: *mut f64, Es2: *mut f64, Eb2: *mut f64, n2: *mut f64) {
367+
pub unsafe extern "C" fn reflect_single_ion_c(num_species_target: &mut c_int, ux: &mut f64, uy: &mut f64, uz: &mut f64, E1: &mut f64, Z1: &mut f64, m1: &mut f64, Ec1: &mut f64, Es1: &mut f64, Z2: *mut f64, m2: *mut f64, Ec2: *mut f64, Es2: *mut f64, Eb2: *mut f64, n2: *mut f64) {
368368

369369
assert!(E1 > &mut 0.0);
370370

@@ -689,7 +689,7 @@ pub extern "C" fn compound_bca_list_c(input: InputCompoundBCA) -> OutputBCA {
689689
}
690690

691691
#[no_mangle]
692-
pub extern "C" fn compound_bca_list_fortran(num_incident_ions: &mut c_int, track_recoils: &mut bool,
692+
pub unsafe extern "C" fn compound_bca_list_fortran(num_incident_ions: &mut c_int, track_recoils: &mut bool,
693693
ux: *mut f64, uy: *mut f64, uz: *mut f64, E1: *mut f64,
694694
Z1: *mut f64, m1: *mut f64, Ec1: *mut f64, Es1: *mut f64,
695695
num_species_target: &mut c_int,

0 commit comments

Comments
 (0)