Skip to content

Commit 46dcf12

Browse files
authored
Merge pull request #340 from lcpp-org/new_rcpr
New rcpr
2 parents e1e006f + 2959eca commit 46dcf12

7 files changed

Lines changed: 95 additions & 16 deletions

File tree

‎examples/f_kporha_et_al_benchmark.py‎

Lines changed: 2 additions & 2 deletions
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
@@ -190,7 +190,7 @@ def tungsten_sputtering(ion, energy, angle, num_ions=1000, run_sim=False, pot="W
190190
colors = []
191191

192192
num_ions = 10000
193-
run_sim = False
193+
run_sim = True
194194

195195
tungsten["Es"] = 8.79
196196
tungsten["Eb"] = 0.0

‎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

‎examples/xenon_sputtering_benchmark.py‎

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def input_file(ion, target, incident_energy, angle, number_ions=1000):
1414

1515
mfp = (target["n"]/10**30)**(-1./3.)
1616

17-
cpr = {'CPR': {'n0': 2, 'nmax': 32, 'epsilon': 1e-3, 'complex_threshold': 1E-9, 'truncation_threshold': 1E-12, 'far_from_zero': 1e3, 'interval_limit': 1E-3, 'derivative_free': True}}
17+
cpr = {'CPR': {'n0': 2, 'nmax': 32, 'epsilon': 1e-3, 'complex_threshold': 1E-9, 'far_from_zero': 1e3, 'interval_limit': 1E-3, 'derivative_free': True}}
1818
options = {
1919
'name': f'input_file_{ion["symbol"]}_{target["symbol"]}_{np.round(angle, 1)}_{np.round(incident_energy/1000, 4)}',
2020
'track_trajectories': False, # whether to track trajectories for plotting; memory intensive
@@ -196,7 +196,7 @@ def input_file(ion, target, incident_energy, angle, number_ions=1000):
196196

197197
num_ions = 10000
198198
num_energies = 25
199-
run_sim = True
199+
run_sim = False
200200
energies = np.logspace(np.log10(25), np.log10(1600), num_energies)
201201
angle = 0.0
202202

@@ -231,8 +231,57 @@ def input_file(ion, target, incident_energy, angle, number_ions=1000):
231231

232232
plt.plot(energies, Y_Xe_Mo, label='RustBCA Default')
233233
plt.gca().set_xscale('log')
234+
plt.gca().set_yscale('log')
234235
plt.legend()
235236
plt.xlabel('E [eV]')
236237
plt.ylabel('Y [at/ion]')
237238
plt.title('Xe on Mo Sputtering Yields')
239+
240+
# Xe on Ti
241+
242+
data = np.array([
243+
[0.06339144215530546, 0.1428571428571308],
244+
[29.920760697305866, 0.39030612244896945],
245+
[44.88114104595877, 0.5459183673469292],
246+
[59.968304278922346, 0.6530612244897864],
247+
[69.79397781299527, 0.5586734693877453],
248+
[0.1267828843106198, 0.33928571428570375],
249+
[30.047543581616488, 0.7755102040816233],
250+
[44.8811410459588, 1.0714285714285632],
251+
[59.96830427892236, 1.2602040816326459],
252+
[69.79397781299525, 1.1938775510204005],
253+
[0.25356576862123426, 0.4846938775510101],
254+
[29.920760697305866, 1.030612244897951],
255+
[45.00792393026942, 1.4285714285714222],
256+
[59.96830427892236, 1.6173469387755044],
257+
[69.98415213946117, 1.6632653061224434],
258+
[0.06339144215530546, 0.8061224489795826],
259+
[29.984152139461166, 1.5255102040816264],
260+
[45.00792393026942, 2.056122448979588],
261+
[59.96830427892236, 2.4693877551020385],
262+
[69.85736925515056, 2.6377551020408143],
263+
[0.12678288431062157, 1.0076530612244818],
264+
[29.98415213946116, 1.834183673469383],
265+
[44.94453248811412, 2.461734693877548],
266+
[59.96830427892236, 3.229591836734694],
267+
[69.92076069730587, 3.357142857142858],
268+
])
269+
270+
run_sim = True
271+
num_angles = 15
272+
num_ions = 1000
273+
angles = np.linspace(0, 89, num_angles)
274+
energies = [200, 400, 600, 1000, 1400]
275+
titanium['Eb'] = 3.0
276+
277+
plt.figure()
278+
for energy in energies:
279+
Y_Xe_Ti = np.zeros(num_angles)
280+
for index, angle in enumerate(angles):
281+
input_data = input_file(xenon, titanium, energy, angle, num_ions)
282+
if run_sim: rustbca_py(input_data, geometry_mode="0D")
283+
sputtered = np.genfromtxt(f'{input_data["options"]["name"]}sputtered.output', delimiter=',')
284+
Y_Xe_Ti[index] = np.shape(sputtered)[0]/num_ions
285+
plt.plot(angles, Y_Xe_Ti, label=f'{energy} eV')
286+
plt.scatter(data[:, 0], data[:, 1])
238287
plt.show()

‎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)