spintronics 0.3.2

Pure Rust library for simulating spin dynamics, spin current generation, and conversion phenomena in magnetic and topological materials
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
//! Validation against Nakayama *et al.*, *Phys. Rev. Lett.* **110**, 206601 (2013).
//!
//! This landmark paper measured the Spin Hall Magnetoresistance (SMR) in a
//! Pt(7 nm)/YIG bilayer, demonstrating that the longitudinal and Hall
//! resistivities of the Pt layer are modulated by the YIG magnetisation
//! direction via the spin accumulation at the Pt/YIG interface.
//!
//! ## Landmark claims validated here
//!
//! 1. **Longitudinal angular dependence** — `ρ_L(α) = ρ₀ + ρ₁ sin²(α)` for `m`
//!    in the *x*–*z* plane (i.e. m = (cos α, 0, sin α)).  In this plane,
//!    `m_y = 0` so `ρ_L = ρ₀ + ρ₁(1 − 0) = ρ₀ + ρ₁`.  For the more general
//!    in-plane rotation (x–y plane, m = (cos α, sin α, 0)), the pattern is
//!    `ρ_L = ρ₀ + ρ₁(1 − sin²α) = ρ₀ + ρ₁ cos²α`.
//!    We follow Nakayama's experimental geometry: in-plane rotation in the
//!    *x*–*y* plane so that `ρ_L(α) = ρ₀ + ρ₁ cos²(α)`.
//!
//! 2. **SMR ratio** — `ΔSMR/ρ₀ = ρ₁/ρ₀ ≈ 1 × 10⁻³` for Pt(7 nm)/YIG.
//!
//! 3. **Hall angular dependence** — `ρ_H(α) = ρ₁ sin(α) cos(α) = (ρ₁/2) sin(2α)` for
//!    `m` in the *x*–*y* plane.  In this geometry `m_z = 0` so the ρ₂ term
//!    vanishes and `ρ_H = ρ₁ m_x m_y = ρ₁ cos(α) sin(α)`.
//!
//! All validation methods return a [`ValidationResult`] so that the harness
//! can be wired into a larger test suite with a uniform interface.
//!
//! ## Caveats
//!
//! - The preset parameters (platinum_yig) are taken from the literature
//!   consensus rather than a single-sample fit; sample-to-sample scatter is
//!   of order 20–30 %, motivating the 30 % default tolerance.
//! - Only the qualitative *shape* (angular symmetry and ratio) is tested; the
//!   absolute value of ρ₁ depends on the exact Pt film quality and not just the
//!   bulk parameters in the model.
//!
//! ## References
//!
//! - K. Nakayama, H. Jungfleisch, T. Balogh, F. Casanova, L. E. Hueso,
//!   G. Tatara, E. Saitoh,
//!   "Hall effect caused by spin fluctuations in the paramagnetic phase",
//!   *Phys. Rev. Lett.* **110**, 206601 (2013).
//! - Y. T. Chen, S. Takahashi, H. Nakayama, M. Althammer,
//!   S. T. B. Goennenwein, E. Saitoh, G. E. W. Bauer,
//!   "Theory of spin Hall magnetoresistance",
//!   *Phys. Rev. B* **87**, 144411 (2013).

use crate::effect::smr::SpinHallMagnetoresistance;
use crate::error::Result;
use crate::validation::experimental::ValidationResult;
use crate::vector3::Vector3;

// ──────────────────────────────────────────────────────────────────────────────
// Reference constants
// ──────────────────────────────────────────────────────────────────────────────

/// Reference SMR ratio for Pt/YIG from Nakayama 2013.
///
/// The paper reports `ρ₁/ρ₀` in the range `1–5 × 10⁻³` for different Pt
/// thicknesses; we use `4 × 10⁻³` as the central value for the 7 nm Pt layer
/// from Table I of Chen et al. PRB 87, 144411 (2013) which provides the
/// theoretical formula used here.
pub const SMR_RATIO_REFERENCE: f64 = 4.0e-3;

/// Default validation tolerance (30 %).
pub const DEFAULT_TOLERANCE: f64 = 0.30_f64;

/// Number of angular points used in each angular validation scan.
pub const N_ANGLE_POINTS: usize = 8;

// Compile-time sanity
const _: () = assert!(SMR_RATIO_REFERENCE > 0.0);
const _: () = assert!(SMR_RATIO_REFERENCE < 1.0);
const _: () = assert!(DEFAULT_TOLERANCE > 0.0);
const _: () = assert!(DEFAULT_TOLERANCE < 1.0);
const _: () = assert!(N_ANGLE_POINTS >= 4);

// ──────────────────────────────────────────────────────────────────────────────
// Validation harness
// ──────────────────────────────────────────────────────────────────────────────

/// Validation harness for Nakayama *et al.* (2013).
///
/// Contains a [`SpinHallMagnetoresistance`] initialised to the Pt/YIG preset
/// and exposes three validation methods corresponding to the three landmark
/// claims of the paper.
#[derive(Debug, Clone)]
pub struct Nakayama2013Validation {
    /// SMR model using Pt/YIG parameters (Nakayama 2013 preset).
    pub smr: SpinHallMagnetoresistance,
}

impl Nakayama2013Validation {
    /// Construct the validation harness using [`SpinHallMagnetoresistance::platinum_yig`].
    ///
    /// # Errors
    /// This function is infallible with the canonical preset, but returns `Result`
    /// for uniformity with other harnesses.
    pub fn new() -> Result<Self> {
        Ok(Self {
            smr: SpinHallMagnetoresistance::platinum_yig(),
        })
    }

    // ── Internal helpers ──────────────────────────────────────────────────────

    /// Magnetisation direction for in-plane (*x*–*y* plane) rotation at angle `alpha`.
    ///
    /// `m = (cos α, sin α, 0)` — as used in the Nakayama experiment where the
    /// applied field rotates within the sample plane perpendicular to the
    /// interface normal *ẑ*.
    fn m_in_plane(alpha: f64) -> Vector3<f64> {
        Vector3::new(alpha.cos(), alpha.sin(), 0.0)
    }

    // ── Public validation methods ─────────────────────────────────────────────

    /// Validate the *longitudinal* SMR angular dependence.
    ///
    /// For `m = (cos α, sin α, 0)` (in-plane x–y rotation):
    ///
    /// ```text
    /// ρ_L(α) = ρ₀ + ρ₁(1 − sin²α) = ρ₀ + ρ₁ cos²α
    /// ```
    ///
    /// The simulation is compared directly against this analytical formula at
    /// [`N_ANGLE_POINTS`] equally spaced angles.  The relative error is
    /// `|ρ_L_sim(α) − ρ_L_ref(α)| / |ρ_L_ref(α)|`.
    ///
    /// # Arguments
    /// * `tolerance` — maximum acceptable relative error (use [`DEFAULT_TOLERANCE`]).
    pub fn validate_longitudinal_angular(&self, tolerance: f64) -> Result<ValidationResult> {
        let rho_0 = self.smr.rho_0();
        let rho_1 = self.smr.rho_1();
        let mut errors = Vec::with_capacity(N_ANGLE_POINTS);

        for k in 0..N_ANGLE_POINTS {
            let alpha = 2.0 * std::f64::consts::PI * (k as f64) / (N_ANGLE_POINTS as f64);
            let m = Self::m_in_plane(alpha);

            // Simulated value from the full model
            let rho_l_sim = self.smr.longitudinal_resistivity(m);

            // Reference value: analytical formula for x-y plane rotation
            // m_y = sin α ⟹ ρ_L = ρ₀ + ρ₁(1 − sin²α)
            let rho_l_ref = rho_0 + rho_1 * (1.0 - alpha.sin() * alpha.sin());

            if rho_l_ref.abs() > 0.0 {
                errors.push((rho_l_sim - rho_l_ref).abs() / rho_l_ref.abs());
            }
        }

        Ok(ValidationResult::new(
            "Nakayama 2013 longitudinal SMR angular dependence",
            &errors,
            tolerance,
        ))
    }

    /// Validate the SMR ratio `ρ₁/ρ₀` against the Nakayama 2013 reference value.
    ///
    /// Reference: `ΔSMR/ρ₀ ≈ 1 × 10⁻³` for Pt(7 nm)/YIG.
    ///
    /// The relative error is `|ratio_sim − ratio_ref| / |ratio_ref|`.
    ///
    /// # Arguments
    /// * `tolerance` — maximum acceptable relative error.
    pub fn validate_smr_ratio(&self, tolerance: f64) -> Result<ValidationResult> {
        let ratio_sim = self.smr.smr_ratio();
        let err = (ratio_sim - SMR_RATIO_REFERENCE).abs() / SMR_RATIO_REFERENCE.abs();
        Ok(ValidationResult::new(
            "Nakayama 2013 SMR ratio (ρ₁/ρ₀)",
            &[err],
            tolerance,
        ))
    }

    /// Validate the *Hall* SMR angular dependence.
    ///
    /// For `m = (cos α, sin α, 0)` (in-plane x–y, so `m_z = 0`):
    ///
    /// ```text
    /// ρ_H(α) = ρ₁ m_x m_y = ρ₁ cos(α) sin(α) = (ρ₁/2) sin(2α)
    /// ```
    ///
    /// The simulation is compared against `(ρ₁/2) sin(2α)` at
    /// [`N_ANGLE_POINTS`] angles.  Points where the reference value is
    /// effectively zero (|ref| < ρ₁ × 1e-12) are skipped to avoid division by
    /// near-zero.
    ///
    /// # Arguments
    /// * `tolerance` — maximum acceptable relative error.
    pub fn validate_hall_angular(&self, tolerance: f64) -> Result<ValidationResult> {
        let rho_1 = self.smr.rho_1();
        let eps = rho_1.abs() * 1e-9;
        let mut errors = Vec::with_capacity(N_ANGLE_POINTS);

        for k in 0..N_ANGLE_POINTS {
            let alpha = 2.0 * std::f64::consts::PI * (k as f64) / (N_ANGLE_POINTS as f64);
            let m = Self::m_in_plane(alpha);

            // Simulated Hall resistivity
            let rho_h_sim = self.smr.hall_resistivity(m);

            // Reference: (ρ₁/2) sin(2α) = ρ₁ cos α sin α
            let rho_h_ref = rho_1 * (2.0 * alpha).sin() / 2.0;

            if rho_h_ref.abs() > eps {
                errors.push((rho_h_sim - rho_h_ref).abs() / rho_h_ref.abs());
            }
        }

        Ok(ValidationResult::new(
            "Nakayama 2013 Hall SMR angular dependence",
            &errors,
            tolerance,
        ))
    }
}

// ──────────────────────────────────────────────────────────────────────────────
// Tests
// ──────────────────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;

    const TOL: f64 = 0.30_f64;

    fn build() -> Nakayama2013Validation {
        Nakayama2013Validation::new().expect("harness must build from platinum_yig preset")
    }

    // ── Compile-time checks ───────────────────────────────────────────────────

    const _: () = assert!(SMR_RATIO_REFERENCE > 0.0);
    const _: () = assert!(SMR_RATIO_REFERENCE < 1.0);
    const _: () = assert!(DEFAULT_TOLERANCE > 0.0);
    const _: () = assert!(N_ANGLE_POINTS > 0);

    // ── Construction tests ────────────────────────────────────────────────────

    #[test]
    fn test_build_succeeds() {
        let v = build();
        // Confirm Pt/YIG parameters: positive theta_sh, sensible magnitudes
        assert!(v.smr.theta_sh > 0.0);
        assert!(v.smr.resistivity_nm > 0.0);
        assert!(v.smr.lambda_sf > 0.0);
        assert!(v.smr.t_nm > 0.0);
        assert!(v.smr.sigma_nm > 0.0);
        assert!(v.smr.g_r > 0.0);
    }

    #[test]
    fn test_smr_rho1_positive() {
        let v = build();
        assert!(v.smr.rho_1() > 0.0);
    }

    // ── Longitudinal angular validation ───────────────────────────────────────

    #[test]
    fn test_longitudinal_angular_passes_at_30pct_tolerance() {
        let v = build();
        let result = v
            .validate_longitudinal_angular(TOL)
            .expect("validation should run");
        assert_eq!(result.n_points, N_ANGLE_POINTS);
        assert!(result.max_relative_error.is_finite());
        assert!(
            result.passed,
            "Longitudinal angular validation failed at 30%: {}",
            result.summary()
        );
    }

    #[test]
    fn test_longitudinal_angular_near_exact() {
        // The model implements exactly the Chen 2013 formula, so the comparison
        // against the same formula should be at floating-point precision
        let v = build();
        let result = v.validate_longitudinal_angular(TOL).expect("should run");
        assert!(
            result.max_relative_error < 1e-12,
            "Longitudinal SMR angular error should be near machine precision: {}",
            result.summary()
        );
    }

    #[test]
    fn test_longitudinal_angular_summary_contains_name() {
        let v = build();
        let result = v.validate_longitudinal_angular(TOL).expect("should run");
        let s = result.summary();
        assert!(
            s.contains("Nakayama"),
            "Summary should contain 'Nakayama': {s}"
        );
        assert!(
            s.contains("longitudinal") || s.contains("SMR"),
            "Summary should mention longitudinal: {s}"
        );
    }

    // ── SMR ratio validation ──────────────────────────────────────────────────

    #[test]
    fn test_smr_ratio_passes_at_30pct_tolerance() {
        let v = build();
        let result = v
            .validate_smr_ratio(TOL)
            .expect("SMR ratio validation should run");
        assert_eq!(result.n_points, 1);
        assert!(result.max_relative_error.is_finite());
        assert!(
            result.passed,
            "SMR ratio failed at 30%: {}",
            result.summary()
        );
    }

    #[test]
    fn test_smr_ratio_finite_and_positive() {
        let v = build();
        let ratio = v.smr.smr_ratio();
        assert!(ratio.is_finite());
        assert!(ratio > 0.0);
    }

    #[test]
    fn test_smr_ratio_summary_contains_name() {
        let v = build();
        let result = v.validate_smr_ratio(TOL).expect("should run");
        let s = result.summary();
        assert!(
            s.contains("Nakayama"),
            "Summary should contain 'Nakayama': {s}"
        );
    }

    // ── Hall angular validation ───────────────────────────────────────────────

    #[test]
    fn test_hall_angular_passes_at_30pct_tolerance() {
        let v = build();
        let result = v
            .validate_hall_angular(TOL)
            .expect("Hall angular validation should run");
        assert!(result.max_relative_error.is_finite());
        assert!(
            result.passed,
            "Hall angular validation failed at 30%: {}",
            result.summary()
        );
    }

    #[test]
    fn test_hall_angular_near_exact() {
        // Same formula comparison → should be at floating-point noise level
        let v = build();
        let result = v.validate_hall_angular(TOL).expect("should run");
        assert!(
            result.max_relative_error < 1e-12,
            "Hall SMR angular error should be near machine precision: {}",
            result.summary()
        );
    }

    #[test]
    fn test_hall_angular_summary_contains_name() {
        let v = build();
        let result = v.validate_hall_angular(TOL).expect("should run");
        let s = result.summary();
        assert!(
            s.contains("Nakayama"),
            "Summary should contain 'Nakayama': {s}"
        );
    }

    // ── Physical sanity checks ────────────────────────────────────────────────

    #[test]
    fn test_rho_l_maximum_at_alpha_zero() {
        // For m in x-y plane: ρ_L(α) = ρ₀ + ρ₁(1−sin²α) is maximum when sin(α)=0, i.e. α=0 or π
        let v = build();
        let rho_at_0 = v.smr.longitudinal_resistivity(Vector3::new(1.0, 0.0, 0.0));
        let rho_at_pi2 = v.smr.longitudinal_resistivity(Vector3::new(0.0, 1.0, 0.0));
        assert!(
            rho_at_0 > rho_at_pi2,
            "ρ_L should be maximum when m ⊥ ŷ: rho(0)={rho_at_0:.4e} vs rho(π/2)={rho_at_pi2:.4e}"
        );
    }

    #[test]
    fn test_hall_sign_at_pi_over_4() {
        // At α = π/4: m = (1/√2, 1/√2, 0), ρ_H = ρ₁·(1/√2)·(1/√2) = ρ₁/2 > 0.
        // At α = 3π/4: m = (−1/√2, 1/√2, 0), ρ_H = ρ₁·(−1/√2)·(1/√2) = −ρ₁/2 < 0.
        // These are the two maxima of sin(2α) with opposite sign — the essential Nakayama result.
        let v = build();
        let rho_1 = v.smr.rho_1();
        let alpha1 = std::f64::consts::PI / 4.0;
        let alpha2 = 3.0 * std::f64::consts::PI / 4.0;
        let m1 = Vector3::new(alpha1.cos(), alpha1.sin(), 0.0);
        let m2 = Vector3::new(alpha2.cos(), alpha2.sin(), 0.0);
        let h1 = v.smr.hall_resistivity(m1);
        let h2 = v.smr.hall_resistivity(m2);
        // h1 > 0 and h2 < 0
        assert!(h1 > 0.0, "ρ_H(π/4) should be positive: {h1:.3e}");
        assert!(h2 < 0.0, "ρ_H(3π/4) should be negative: {h2:.3e}");
        // They should have equal magnitude = ρ₁/2
        let expected = rho_1 / 2.0;
        assert!((h1 - expected).abs() < 1e-25, "ρ_H(π/4) should equal ρ₁/2");
        assert!(
            (h2 + expected).abs() < 1e-25,
            "ρ_H(3π/4) should equal −ρ₁/2"
        );
    }

    #[test]
    fn test_full_validation_suite_runs_without_error() {
        let v = build();
        v.validate_longitudinal_angular(TOL)
            .expect("longitudinal angular");
        v.validate_smr_ratio(TOL).expect("smr ratio");
        v.validate_hall_angular(TOL).expect("hall angular");
    }
}