Skip to content

Commit 00e1d08

Browse files
committed
Added some comments and minor cleanup
1 parent 5e9daf8 commit 00e1d08

1 file changed

Lines changed: 15 additions & 10 deletions

File tree

‎src/interactions.rs‎

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ pub fn crossing_point_doca(interaction_potential: InteractionPotential) -> f64 {
1010
InteractionPotential::WW => 50.*ANGSTROM,
1111
_ => 10.*ANGSTROM,
1212
}
13-
1413
}
1514

1615
fn smootherstep(x: f64, k: f64, x0: f64) -> f64 {
@@ -272,25 +271,31 @@ pub fn dphi(xi: f64, interaction_potential: InteractionPotential) -> f64 {
272271
pub fn screening_length(Za: f64, Zb: f64, interaction_potential: InteractionPotential) -> f64 {
273272
match interaction_potential {
274273
//ZBL screening length, Eckstein (4.1.8)
275-
InteractionPotential::ZBL => zbl_screening_length(Za as u64, Zb as u64),
274+
InteractionPotential::ZBL => zbl_screening_length_lookup(Za as u64, Zb as u64),
276275
//Lindhard/Firsov screening length, Eckstein (4.1.5)
277276
InteractionPotential::MOLIERE | InteractionPotential::KR_C | InteractionPotential::LENZ_JENSEN | InteractionPotential::TRIDYN | InteractionPotential::WW => lindhard_screening_length_lookup(Za as u64, Zb as u64),
278277
InteractionPotential::LENNARD_JONES_12_6{..} | InteractionPotential::LENNARD_JONES_65_6{..} => lindhard_screening_length_lookup(Za as u64, Zb as u64),
279278
InteractionPotential::MORSE{D, alpha, r0} => alpha,
280-
InteractionPotential::COULOMB{Za: Z1, Zb: Z2} => zbl_screening_length(Za as u64, Zb as u64),
279+
InteractionPotential::COULOMB{Za: Z1, Zb: Z2} => zbl_screening_length_lookup(Za as u64, Zb as u64),
281280
InteractionPotential::KRC_MORSE{..} => lindhard_screening_length_lookup(Za as u64, Zb as u64),
282281
InteractionPotential::FOUR_EIGHT{..} => lindhard_screening_length_lookup(Za as u64, Zb as u64),
283282
}
284283
}
285284

285+
// It turns out it's faster (~10% speedup) to just generate every possible screening length as a lookup table
286+
// LazyLock is a thread-safe value that is initialized whenever it is first accessed
287+
// It will block other threads while it runs, but it should run extremely quickly and only once
286288
const Z_MAX: usize = 120;
287289
static LINDHARD_SCREENING_LENGTH_TABLE: LazyLock<[f64; Z_MAX*Z_MAX]> = LazyLock::new(
288290
||
289291
std::array::from_fn(
290292
|i| {
293+
// standard 2D to 1D array indexing
294+
// I always have to look this up; copied from here
295+
// (e.g. https://stackoverflow.com/questions/5494974/convert-1d-array-index-to-2d-array-index)
291296
let Za = i / Z_MAX;
292297
let Zb = i % Z_MAX;
293-
lindhard_screening_length(Za as u64, Zb as u64)
298+
lindhard_screening_length(Za as f64, Zb as f64)
294299
}
295300
)
296301
);
@@ -300,24 +305,24 @@ static ZBL_SCREENING_LENGTH_TABLE: LazyLock<[f64; Z_MAX*Z_MAX]> = LazyLock::new(
300305
|i| {
301306
let Za = i / Z_MAX;
302307
let Zb = i % Z_MAX;
303-
zbl_screening_length(Za as u64, Zb as u64)
308+
zbl_screening_length(Za as f64, Zb as f64)
304309
}
305310
)
306311
);
307312

308-
pub fn zbl_screening_length(Za: u64, Zb: u64) -> f64{
309-
0.88534*A0/((Za as f64).powf(0.23) + (Zb as f64).powf(0.23))
313+
pub fn zbl_screening_length(Za: f64, Zb: f64) -> f64{
314+
0.88534*A0/(Za.powf(0.23) + Zb.powf(0.23))
310315
}
311316

312-
pub fn lindhard_screening_length(Za: u64, Zb: u64) -> f64 {
313-
0.8853*A0*((Za as f64).sqrt() + (Zb as f64).sqrt()).powf(-2./3.)
317+
pub fn lindhard_screening_length(Za: f64, Zb: f64) -> f64 {
318+
0.8853*A0*(Za.sqrt() + Zb.sqrt()).powf(-2./3.)
314319
}
315320

316321
pub fn lindhard_screening_length_lookup(Za: u64, Zb: u64) -> f64 {
317322
LINDHARD_SCREENING_LENGTH_TABLE[Za as usize * Z_MAX + Zb as usize]
318323
}
319324

320-
pub fn lindhard_screening_length_lookup(Za: u64, Zb: u64) -> f64{
325+
pub fn zbl_screening_length_lookup(Za: u64, Zb: u64) -> f64{
321326
ZBL_SCREENING_LENGTH_TABLE[Za as usize * Z_MAX + Zb as usize]
322327
}
323328

0 commit comments

Comments
 (0)