xtalforge-core 0.0.2

Core crystal-structure types for xtalforge: elements, lattices, sites and structures.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
//! Size limits: arithmetic estimates made before anything is allocated.
//!
//! Two levels: a soft limit warns, a hard limit refuses. Profiles are
//! `Standard` (8 GB class), `Generous` (every limit doubled) and `WarnOnly`
//! (never refuses). The numbers are the Crystal-Builder table (Task 1.3).

use std::fmt;

use crate::Error;
use crate::lattice::Lattice;

/// Undo history sites allowed in whole-structure steps.
pub const UNDO_ATOMS: u64 = 2_000_000;
/// Most recent undo steps that are never dropped.
pub const UNDO_KEEP: u64 = 5;
/// Analytic Hessian memory estimate per atom, in MiB.
pub const HESSIAN_MIB_PER_ATOM: f64 = 13.0;

/// Limit profile.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum Profile {
    /// 8 GB-class machine.
    #[default]
    Standard,
    /// Every limit doubled.
    Generous,
    /// Warn only; never refuses.
    WarnOnly,
}

impl Profile {
    /// Parses a profile name, falling back to Standard for unknown values.
    #[must_use]
    pub fn parse(name: &str) -> Self {
        match name {
            "generous" => Self::Generous,
            "warn_only" | "warn-only" => Self::WarnOnly,
            _ => Self::Standard,
        }
    }

    /// Stable profile name.
    #[must_use]
    pub const fn name(self) -> &'static str {
        match self {
            Self::Standard => "standard",
            Self::Generous => "generous",
            Self::WarnOnly => "warn_only",
        }
    }
}

/// A size budget.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Budget {
    /// Atoms drawn for a display range.
    Drawn,
    /// Atoms in a supercell.
    Supercell,
    /// Points in a porosity grid.
    Grid,
    /// Atoms in a carbon build.
    Carbon,
    /// Atoms in a nanotube build.
    Nanotube,
    /// Atoms in a graphene build.
    Graphene,
    /// Atoms in a phonon supercell.
    Phonons,
    /// Atoms in an analytic Hessian.
    Hessian,
}

impl Budget {
    /// Parses a budget name; unknown names are an error.
    ///
    /// # Errors
    ///
    /// Returns [`Error::InvalidInput`] for an unknown budget.
    pub fn parse(name: &str) -> Result<Self, Error> {
        match name {
            "drawn" => Ok(Self::Drawn),
            "supercell" => Ok(Self::Supercell),
            "grid" => Ok(Self::Grid),
            "carbon" => Ok(Self::Carbon),
            "nanotube" => Ok(Self::Nanotube),
            "graphene" => Ok(Self::Graphene),
            "phonons" => Ok(Self::Phonons),
            "hessian" => Ok(Self::Hessian),
            _ => Err(Error::InvalidInput(format!("unknown limit budget: {name}"))),
        }
    }

    fn table(self) -> (u64, u64) {
        match self {
            Self::Drawn => (100_000, 400_000),
            Self::Supercell => (50_000, 200_000),
            Self::Grid => (1_500_000, 4_000_000),
            Self::Carbon => (10_000, 30_000),
            Self::Nanotube => (50_000, 200_000),
            Self::Graphene => (50_000, 200_000),
            Self::Phonons => (2_000, 8_000),
            Self::Hessian => (100, 300),
        }
    }
}

/// Limit level of a verdict.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum LimitLevel {
    /// Inside the soft limit.
    Ok,
    /// Over the soft limit but under the hard limit.
    Warn,
    /// Over the hard limit.
    Refuse,
}

/// Result of a size check.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Verdict {
    /// Limit level.
    pub level: LimitLevel,
    /// Estimated count.
    pub estimate: u64,
    /// Human-readable sentence; empty when the estimate fits.
    pub sentence: String,
}

impl LimitLevel {
    /// Stable level name.
    #[must_use]
    pub const fn name(self) -> &'static str {
        match self {
            Self::Ok => "ok",
            Self::Warn => "warn",
            Self::Refuse => "refuse",
        }
    }
}

impl Verdict {
    /// Whether the operation is refused.
    #[must_use]
    pub fn refused(&self) -> bool {
        self.level == LimitLevel::Refuse
    }

    /// Whether the operation is warned or refused.
    #[must_use]
    pub fn warned(&self) -> bool {
        self.level != LimitLevel::Ok
    }
}

impl fmt::Display for Verdict {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        if self.sentence.is_empty() {
            write!(formatter, "{}", self.estimate)
        } else {
            formatter.write_str(&self.sentence)
        }
    }
}

/// Limits model with a profile.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct Limits {
    profile: Profile,
}

impl Limits {
    /// Creates limits with a profile.
    #[must_use]
    pub const fn new(profile: Profile) -> Self {
        Self { profile }
    }

    /// Profile used by these limits.
    #[must_use]
    pub const fn profile(&self) -> Profile {
        self.profile
    }

    /// `(soft, hard)` for a budget; `hard` is `None` under `WarnOnly`.
    #[must_use]
    pub fn bounds(&self, budget: Budget) -> (u64, Option<u64>) {
        let (soft, hard) = budget.table();
        match self.profile {
            Profile::Standard => (soft, Some(hard)),
            Profile::Generous => (2 * soft, Some(2 * hard)),
            Profile::WarnOnly => (soft, None),
        }
    }

    /// Judges an estimate against a budget.
    #[must_use]
    pub fn judge(
        &self,
        budget: Budget,
        estimate: u64,
        what: &str,
        fits: impl Fn(u64) -> String,
    ) -> Verdict {
        let (soft, hard) = self.bounds(budget);
        if estimate <= soft {
            return Verdict {
                level: LimitLevel::Ok,
                estimate,
                sentence: String::new(),
            };
        }
        if let Some(hard) = hard
            && estimate > hard
        {
            return Verdict {
                level: LimitLevel::Refuse,
                estimate,
                sentence: format!(
                    "{} over the {} the Large structures setting allows; {}",
                    what.replace("{}", &count(estimate)),
                    count(hard),
                    fits(hard)
                ),
            };
        }
        Verdict {
            level: LimitLevel::Warn,
            estimate,
            sentence: format!(
                "{} over the {} that is comfortable; {}",
                what.replace("{}", &count(estimate)),
                count(soft),
                fits(soft)
            ),
        }
    }
}

/// Formats a count as a sentence says it: `52,000` or `1.4 M`.
#[must_use]
pub fn count(value: u64) -> String {
    if value >= 1_000_000 {
        format!("{:.1} M", value as f64 / 1e6)
    } else {
        let text = value.to_string();
        let mut out = String::new();
        for (index, character) in text.chars().rev().enumerate() {
            if index > 0 && index % 3 == 0 {
                out.push(',');
            }
            out.push(character);
        }
        out.chars().rev().collect()
    }
}

/// Bounds for a profile and budget name.
///
/// # Errors
///
/// Returns [`Error::InvalidInput`] for an unknown budget.
pub fn limits_bounds(profile: &str, budget: &str) -> Result<(u64, Option<u64>), Error> {
    let limits = Limits::new(Profile::parse(profile));
    Ok(limits.bounds(Budget::parse(budget)?))
}

/// Generic limit check by profile, budget and estimate.
///
/// # Errors
///
/// Returns [`Error::InvalidInput`] for an unknown budget.
pub fn limits_check(profile: &str, budget: &str, estimate: u64) -> Result<Verdict, Error> {
    let limits = Limits::new(Profile::parse(profile));
    let budget = Budget::parse(budget)?;
    Ok(limits.judge(
        budget,
        estimate,
        "the operation would have {} items",
        |limit| format!("up to {} items fit", count(limit)),
    ))
}

/// Atoms drawn for a display range: cell atoms times the range volume.
#[must_use]
pub fn drawn_atoms(n_atoms: u64, ranges: [(f64, f64); 3]) -> u64 {
    let volume = ranges
        .iter()
        .map(|(low, high)| (high - low).max(0.0))
        .product::<f64>();
    (n_atoms as f64 * volume).round() as u64
}

/// `|det P|` for a 3x3 transformation matrix.
///
/// # Errors
///
/// Returns [`Error::InvalidInput`] for a singular matrix or non-finite values.
pub fn multiplier(p: [[f64; 3]; 3]) -> Result<u64, Error> {
    if !p.iter().flatten().all(|value| value.is_finite()) {
        return Err(Error::InvalidInput(
            "transformation matrix contains non-finite values".to_owned(),
        ));
    }
    let [[a, b, c], [d, e, f], [g, h, i]] = p;
    let determinant = a * (e * i - f * h) - b * (d * i - f * g) + c * (d * h - e * g);
    if determinant.abs() < 1e-9 {
        return Err(Error::InvalidInput(
            "transformation matrix is singular".to_owned(),
        ));
    }
    Ok(determinant.abs().round() as u64)
}

/// Atoms in the cell a transformation builds from `n_atoms`.
///
/// # Errors
///
/// Returns the errors of [`multiplier`].
pub fn supercell_atoms(n_atoms: u64, p: [[f64; 3]; 3]) -> Result<u64, Error> {
    Ok(n_atoms.saturating_mul(multiplier(p)?))
}

/// Grid shape from cell lengths and a target spacing.
///
/// # Errors
///
/// Returns [`Error::InvalidInput`] for a non-positive or non-finite spacing.
pub fn grid_shape(lattice: &Lattice, spacing: f64) -> Result<[u32; 3], Error> {
    if !spacing.is_finite() || spacing <= 0.0 {
        return Err(Error::InvalidInput(format!(
            "grid spacing must be positive and finite, got {spacing}"
        )));
    }
    let spacing = spacing.max(0.05);
    Ok(lattice
        .lengths()
        .map(|length| (length / spacing).round().max(8.0) as u32))
}

/// Points in a porosity grid.
///
/// # Errors
///
/// Returns the errors of [`grid_shape`].
pub fn grid_points(lattice: &Lattice, spacing: f64) -> Result<u64, Error> {
    let [a, b, c] = grid_shape(lattice, spacing)?;
    Ok(u64::from(a) * u64::from(b) * u64::from(c))
}

fn cube_that_fits(per_cell: f64, limit: u64, cells: &str) -> String {
    let k = ((limit as f64 / per_cell.max(1.0)).powf(1.0 / 3.0) + 1e-9).floor() as u64;
    if k < 1 {
        format!("one cell alone is {} atoms", count(per_cell.round() as u64))
    } else {
        format!("{k} x {k} x {k}{cells} is the largest that fits")
    }
}

/// Checks the atoms a display range would draw.
#[must_use]
pub fn check_drawn(limits: &Limits, n_atoms: u64, ranges: [(f64, f64); 3]) -> Verdict {
    let estimate = drawn_atoms(n_atoms, ranges);
    limits.judge(
        Budget::Drawn,
        estimate,
        "the picture would draw {} atoms",
        |limit| cube_that_fits(n_atoms as f64, limit, " cells"),
    )
}

/// Checks a supercell transformation.
///
/// # Errors
///
/// Returns the errors of [`supercell_atoms`].
pub fn check_supercell(limits: &Limits, n_atoms: u64, p: [[f64; 3]; 3]) -> Result<Verdict, Error> {
    let estimate = supercell_atoms(n_atoms, p)?;
    Ok(limits.judge(
        Budget::Supercell,
        estimate,
        "the new cell would have {} atoms",
        |limit| cube_that_fits(n_atoms as f64, limit, ""),
    ))
}

/// Checks a porosity grid.
///
/// # Errors
///
/// Returns the errors of [`grid_points`].
pub fn check_grid(limits: &Limits, lattice: &Lattice, spacing: f64) -> Result<Verdict, Error> {
    let estimate = grid_points(lattice, spacing)?;
    Ok(limits.judge(
        Budget::Grid,
        estimate,
        "the grid would have {} points",
        |limit| {
            let mut step = (spacing / 0.05).ceil() * 0.05;
            step = step.max(0.05);
            while grid_points(lattice, step).unwrap_or(u64::MAX) > limit && step < 5.0 {
                step += 0.05;
            }
            format!(
                "at {step:.2} A it would have {}",
                grid_points(lattice, step)
                    .map(count)
                    .unwrap_or_else(|_| "an invalid count".to_owned())
            )
        },
    ))
}

/// Checks a phonon supercell.
///
/// # Errors
///
/// Returns the errors of [`supercell_atoms`].
pub fn check_phonons(limits: &Limits, n_atoms: u64, p: [[f64; 3]; 3]) -> Result<Verdict, Error> {
    let estimate = supercell_atoms(n_atoms, p)?;
    Ok(limits.judge(
        Budget::Phonons,
        estimate,
        "the phonon supercell would have {} atoms",
        |limit| {
            format!(
                "{}, or Gamma only needs none",
                cube_that_fits(n_atoms as f64, limit, "")
            )
        },
    ))
}

/// Checks an analytic Hessian over `n_atoms` free atoms.
#[must_use]
pub fn check_hessian(limits: &Limits, n_atoms: u64) -> Verdict {
    limits.judge(
        Budget::Hessian,
        n_atoms,
        &format!(
            "an analytic Hessian over {{}} atoms would hold about {:.1} GB",
            n_atoms as f64 * HESSIAN_MIB_PER_ATOM / 1024.0
        ),
        |limit| {
            format!(
                "up to {} atoms fit, and finite differences need no more than one evaluation does",
                count(limit)
            )
        },
    )
}