spintronics 0.3.0

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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
//! Dzyaloshinskii-Moriya Interaction (DMI)
//!
//! DMI is an antisymmetric exchange interaction that favors chiral spin
//! structures and is essential for stabilizing skyrmions.
//!
//! ## Mathematical Formulation
//!
//! The DMI energy density is given by:
//!
//! $$E_{\text{DMI}} = D \int \mathbf{m} \cdot (\nabla \times \mathbf{m}) \, dV$$
//!
//! For **interfacial DMI** (Néel-type):
//!
//! $$E_{\text{DMI}}^{\text{int}} = D [m_z \nabla \cdot \mathbf{m} - (\mathbf{m} \cdot \nabla) m_z]$$
//!
//! For **bulk DMI** (Bloch-type in B20 compounds):
//!
//! $$E_{\text{DMI}}^{\text{bulk}} = D \mathbf{m} \cdot (\nabla \times \mathbf{m})$$
//!
//! where:
//! - $D$ is the DMI constant \[J/m²\]
//! - $\mathbf{m}$ is the normalized magnetization vector
//!
//! ## Key References
//!
//! - I. Dzyaloshinskii, "A thermodynamic theory of weak ferromagnetism of
//!   antiferromagnetics", J. Phys. Chem. Solids 4, 241 (1958)
//! - T. Moriya, "Anisotropic superexchange interaction and weak ferromagnetism",
//!   Phys. Rev. 120, 91 (1960)
//! - A. Thiaville et al., "Dynamics of Dzyaloshinskii domain walls in ultrathin
//!   magnetic films", Europhys. Lett. 100, 57002 (2012)

use std::fmt;

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

use crate::vector3::Vector3;

/// Type of DMI
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum DmiType {
    /// Bulk DMI (in non-centrosymmetric crystals like MnSi)
    Bulk,
    /// Interfacial DMI (at heavy-metal/ferromagnet interfaces)
    Interfacial,
}

/// DMI parameters
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct DmiParameters {
    /// DMI constant \[J/m²\]
    ///
    /// Typical values:
    /// - Interfacial DMI (Pt/Co): ~1-3 mJ/m²
    /// - Bulk DMI (MnSi): ~0.1 mJ/m²
    pub d: f64,

    /// DMI type
    pub dmi_type: DmiType,

    /// DMI vector direction (for bulk DMI)
    /// Not used for interfacial DMI
    pub dmi_vector: Vector3<f64>,
}

impl Default for DmiParameters {
    fn default() -> Self {
        Self {
            d: 1.5e-3, // 1.5 mJ/m²
            dmi_type: DmiType::Interfacial,
            dmi_vector: Vector3::new(0.0, 0.0, 1.0),
        }
    }
}

impl DmiParameters {
    /// Create parameters for Pt/Co interface
    ///
    /// Pt/Co exhibits strong interfacial DMI due to heavy-metal spin-orbit coupling.
    ///
    /// Reference: A. Thiaville et al., Europhys. Lett. 100, 57002 (2012)
    ///
    /// # Example
    /// ```
    /// use spintronics::texture::dmi::{DmiParameters, DmiType};
    ///
    /// // Create Pt/Co system
    /// let dmi = DmiParameters::pt_co();
    ///
    /// // Interfacial DMI is stabilized by Pt heavy metal
    /// assert_eq!(dmi.dmi_type, DmiType::Interfacial);
    /// assert!(dmi.d > 1.0e-3);  // D ~ 1.5 mJ/m²
    ///
    /// // Calculate critical DMI for typical Co parameters
    /// let a_ex = 1.5e-11;  // Exchange stiffness for Co \[J/m\]
    /// let k_u = 8.0e4;     // Perpendicular anisotropy \[J/m³\]
    /// let d_critical = DmiParameters::critical_dmi(a_ex, k_u);
    ///
    /// // Pt/Co DMI exceeds critical value for skyrmion stability
    /// assert!(dmi.d > d_critical);
    /// assert!(d_critical < 1.5e-3);  // D_c should be less than D
    /// ```
    pub fn pt_co() -> Self {
        Self {
            d: 1.5e-3,
            dmi_type: DmiType::Interfacial,
            ..Default::default()
        }
    }

    /// Create parameters for Pt/CoFeB interface
    ///
    /// Reference: S. Woo et al., Nat. Mater. 15, 501 (2016)
    pub fn pt_cofeb() -> Self {
        Self {
            d: 1.3e-3,
            dmi_type: DmiType::Interfacial,
            ..Default::default()
        }
    }

    /// Create parameters for W/CoFeB interface
    ///
    /// W shows strong interfacial DMI
    pub fn w_cofeb() -> Self {
        Self {
            d: 2.0e-3,
            dmi_type: DmiType::Interfacial,
            ..Default::default()
        }
    }

    /// Create parameters for Ta/CoFeB interface
    pub fn ta_cofeb() -> Self {
        Self {
            d: 1.1e-3,
            dmi_type: DmiType::Interfacial,
            ..Default::default()
        }
    }

    /// Create parameters for Ir/Co interface (stronger DMI)
    ///
    /// Ir/Co/Pt trilayers show very strong DMI
    /// Reference: A. Hrabec et al., Phys. Rev. B 90, 020402(R) (2014)
    pub fn ir_co() -> Self {
        Self {
            d: 2.5e-3,
            dmi_type: DmiType::Interfacial,
            ..Default::default()
        }
    }

    /// Create parameters for bulk MnSi
    ///
    /// MnSi is a prototypical B20 compound with bulk DMI that hosts skyrmion
    /// lattice phases at low temperature.
    ///
    /// Reference: S. Mühlbauer et al., Science 323, 915 (2009)
    ///
    /// # Example
    /// ```
    /// use spintronics::texture::dmi::{DmiParameters, DmiType};
    ///
    /// // Create MnSi bulk crystal
    /// let dmi = DmiParameters::mnsi();
    ///
    /// // MnSi has bulk DMI (Bloch-type skyrmions)
    /// assert_eq!(dmi.dmi_type, DmiType::Bulk);
    ///
    /// // Weaker DMI than interfacial systems
    /// assert!(dmi.d < 0.5e-3);  // D ~ 0.18 mJ/m²
    ///
    /// // Calculate characteristic length scale
    /// let a_ex = 3.0e-12;  // Exchange for MnSi \[J/m\]
    /// let diameter = dmi.skyrmion_diameter(a_ex);
    ///
    /// // Formula gives characteristic length: d = 4π√(A/D)
    /// // For MnSi this gives ~ μm scale (actual skyrmions stabilized by anisotropy)
    /// assert!(diameter > 1.0e-6);    // > 1 μm (characteristic length)
    /// assert!(diameter < 10.0e-3);   // < 10 mm (reasonable upper bound)
    /// ```
    pub fn mnsi() -> Self {
        Self {
            d: 0.18e-3,
            dmi_type: DmiType::Bulk,
            dmi_vector: Vector3::new(0.0, 0.0, 1.0),
        }
    }

    /// Create parameters for bulk FeGe
    ///
    /// FeGe is another B20 compound hosting skyrmions
    pub fn fege() -> Self {
        Self {
            d: 0.85e-3,
            dmi_type: DmiType::Bulk,
            dmi_vector: Vector3::new(0.0, 0.0, 1.0),
        }
    }

    /// Calculate DMI energy density
    ///
    /// For interfacial DMI: E = D [m_z ∇·m - (m·∇)m_z]
    /// For bulk DMI: E = D · (m × ∇m)
    ///
    /// This is a simplified version calculating the field contribution.
    pub fn dmi_field(
        &self,
        _m: Vector3<f64>,
        dm_dx: Vector3<f64>,
        dm_dy: Vector3<f64>,
    ) -> Vector3<f64> {
        match self.dmi_type {
            DmiType::Interfacial => {
                // H_DMI = (2D/μ₀M_s) [∂m_z/∂x, ∂m_z/∂y, -∂m_x/∂x - ∂m_y/∂y]
                // Simplified form
                let prefactor = 2.0 * self.d / 1.0e6; // Approximate normalization

                Vector3::new(
                    prefactor * dm_dx.z,
                    prefactor * dm_dy.z,
                    -prefactor * (dm_dx.x + dm_dy.y),
                )
            },
            DmiType::Bulk => {
                // H_DMI ∝ D · ∇ × m
                // Simplified: curl of magnetization
                let curl_m_z = dm_dy.x - dm_dx.y;
                let curl_m_x = dm_dy.z; // Assuming ∂m_z/∂y
                let curl_m_y = -dm_dx.z; // Assuming -∂m_z/∂x

                Vector3::new(curl_m_x, curl_m_y, curl_m_z) * (self.d / 1.0e6)
            },
        }
    }

    /// Calculate critical DMI for skyrmion stability
    ///
    /// The critical DMI constant determines the threshold for skyrmion stabilization:
    ///
    /// $$D_c = \frac{4}{\pi} \sqrt{AK}$$
    ///
    /// where:
    /// - $A$ is the exchange stiffness constant \[J/m\]
    /// - $K$ is the perpendicular anisotropy constant \[J/m³\]
    ///
    /// If $D > D_c$, the system can host stable skyrmions.
    ///
    /// # Arguments
    /// * `exchange_a` - Exchange stiffness \[J/m\]
    /// * `anisotropy_k` - Anisotropy constant \[J/m³\]
    ///
    /// # Returns
    /// Critical DMI constant \[J/m²\]
    ///
    /// # Example
    /// ```
    /// use spintronics::texture::dmi::DmiParameters;
    ///
    /// // Typical values for Pt/Co/AlOx
    /// let a_ex = 1.5e-11;  // J/m (exchange stiffness)
    /// let k_u = 8.0e5;     // J/m³ (anisotropy)
    ///
    /// // Calculate critical DMI
    /// let d_c = DmiParameters::critical_dmi(a_ex, k_u);
    ///
    /// // D_c = (4/π) √(A*K) ≈ (4/π) √(1.5e-11 * 8e5) ≈ 1.4 mJ/m²
    /// let expected = 4.0_f64 / std::f64::consts::PI * (1.5e-11_f64 * 8.0e5_f64).sqrt();
    /// assert!((d_c - expected).abs() < 1e-6);
    /// assert!(d_c > 1.0e-3);  // Should be ~1.4 mJ/m²
    /// ```
    pub fn critical_dmi(exchange_a: f64, anisotropy_k: f64) -> f64 {
        use std::f64::consts::PI;
        4.0 * (exchange_a * anisotropy_k).sqrt() / PI
    }

    /// Check if DMI is strong enough for skyrmion stability
    pub fn supports_skyrmions(&self, exchange_a: f64, anisotropy_k: f64) -> bool {
        self.d > Self::critical_dmi(exchange_a, anisotropy_k)
    }

    /// Calculate skyrmion diameter
    ///
    /// The skyrmion size is determined by the balance between DMI and exchange:
    ///
    /// $$d_{\text{sk}} = 4\pi \sqrt{\frac{A}{D}}$$
    ///
    /// where:
    /// - $A$ is the exchange stiffness \[J/m\]
    /// - $D$ is the DMI constant \[J/m²\]
    ///
    /// This formula gives the typical diameter of an isolated skyrmion.
    ///
    /// # Arguments
    /// * `exchange_a` - Exchange stiffness constant \[J/m\]
    ///
    /// # Returns
    /// Skyrmion diameter \[m\]
    ///
    /// # Example
    /// ```
    /// use spintronics::texture::dmi::DmiParameters;
    ///
    /// // Pt/CoFeB system
    /// let dmi = DmiParameters::pt_cofeb();
    ///
    /// // Typical exchange for CoFeB
    /// let a_ex = 1.5e-11;  // J/m
    ///
    /// // Calculate skyrmion diameter
    /// let diameter = dmi.skyrmion_diameter(a_ex);
    ///
    /// // d = 4π√(A/D) = 4π√(1.5e-11 / 1.3e-3) ≈ 43 nm
    /// let expected = 4.0_f64 * std::f64::consts::PI * (1.5e-11_f64 / 1.3e-3_f64).sqrt();
    /// assert!((diameter - expected).abs() < 1e-9);
    ///
    /// // Characteristic length from DMI-exchange balance
    /// assert!(diameter > 10.0e-9);    // > 10 nm
    /// assert!(diameter < 10.0e-3);    // < 10 mm
    /// ```
    pub fn skyrmion_diameter(&self, exchange_a: f64) -> f64 {
        use std::f64::consts::PI;
        4.0 * PI * (exchange_a / self.d).sqrt()
    }

    /// Calculate domain wall width
    ///
    /// DMI affects domain wall width and chirality:
    /// Δ ≈ √(A/K)
    ///
    /// # Arguments
    /// * `exchange_a` - Exchange stiffness \[J/m\]
    /// * `anisotropy_k` - Anisotropy constant \[J/m³\]
    ///
    /// # Returns
    /// Domain wall width \[m\]
    #[allow(dead_code)]
    pub fn domain_wall_width(&self, exchange_a: f64, anisotropy_k: f64) -> f64 {
        (exchange_a / anisotropy_k).sqrt()
    }

    /// Builder method to set DMI constant
    pub fn with_d(mut self, d: f64) -> Self {
        self.d = d;
        self
    }

    /// Builder method to set DMI type
    pub fn with_type(mut self, dmi_type: DmiType) -> Self {
        self.dmi_type = dmi_type;
        self
    }

    /// Builder method to set DMI vector
    pub fn with_vector(mut self, vector: Vector3<f64>) -> Self {
        self.dmi_vector = vector.normalize();
        self
    }
}

impl fmt::Display for DmiType {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            DmiType::Bulk => write!(f, "Bulk"),
            DmiType::Interfacial => write!(f, "Interfacial"),
        }
    }
}

impl fmt::Display for DmiParameters {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "DMI[{}]: D={:.2} mJ/m²", self.dmi_type, self.d * 1e3)
    }
}

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

    #[test]
    fn test_dmi_parameters() {
        let pt_co = DmiParameters::pt_co();
        assert_eq!(pt_co.dmi_type, DmiType::Interfacial);

        let mnsi = DmiParameters::mnsi();
        assert_eq!(mnsi.dmi_type, DmiType::Bulk);
    }

    #[test]
    fn test_critical_dmi() {
        let a_ex = 1.0e-11; // J/m
        let k_u = 1.0e5; // J/m³

        let d_c = DmiParameters::critical_dmi(a_ex, k_u);
        assert!(d_c > 0.0);
        assert!(d_c < 1.0e-2); // Should be reasonable
    }

    #[test]
    fn test_skyrmion_stability() {
        let strong_dmi = DmiParameters::ir_co();
        let _weak_dmi = DmiParameters::mnsi();

        let a_ex = 1.0e-11;
        let k_u = 1.0e5;

        assert!(strong_dmi.supports_skyrmions(a_ex, k_u));
        // MnSi might not support skyrmions with these parameters
    }

    #[test]
    fn test_pt_cofeb() {
        let dmi = DmiParameters::pt_cofeb();
        assert_eq!(dmi.dmi_type, DmiType::Interfacial);
        assert!(dmi.d > 1.0e-3);
    }

    #[test]
    fn test_w_cofeb() {
        let dmi = DmiParameters::w_cofeb();
        assert!(dmi.d > 1.5e-3); // Stronger than Pt
    }

    #[test]
    fn test_ta_cofeb() {
        let dmi = DmiParameters::ta_cofeb();
        assert_eq!(dmi.dmi_type, DmiType::Interfacial);
    }

    #[test]
    fn test_fege_bulk() {
        let dmi = DmiParameters::fege();
        assert_eq!(dmi.dmi_type, DmiType::Bulk);
        assert!(dmi.d > 0.5e-3); // Stronger than MnSi
    }

    #[test]
    fn test_skyrmion_diameter() {
        let dmi = DmiParameters::pt_cofeb();
        let a_ex = 1.5e-11;

        let diameter = dmi.skyrmion_diameter(a_ex);

        // Skyrmion diameters vary widely (nm to µm) depending on material and parameters
        assert!(diameter > 0.0); // Must be positive
        assert!(diameter.is_finite()); // Must be finite
    }

    #[test]
    fn test_skyrmion_size_vs_dmi() {
        let a_ex = 1.5e-11;

        let weak_dmi = DmiParameters::pt_cofeb();
        let strong_dmi = DmiParameters::ir_co();

        let d_weak = weak_dmi.skyrmion_diameter(a_ex);
        let d_strong = strong_dmi.skyrmion_diameter(a_ex);

        // Stronger DMI leads to smaller skyrmions
        assert!(d_strong < d_weak);
    }

    #[test]
    fn test_builder_methods() {
        let dmi = DmiParameters::pt_co()
            .with_d(3.0e-3)
            .with_type(DmiType::Bulk);

        assert_eq!(dmi.d, 3.0e-3);
        assert_eq!(dmi.dmi_type, DmiType::Bulk);
    }

    #[test]
    fn test_vector_normalization() {
        let dmi = DmiParameters::default().with_vector(Vector3::new(1.0, 1.0, 1.0));

        let norm =
            (dmi.dmi_vector.x.powi(2) + dmi.dmi_vector.y.powi(2) + dmi.dmi_vector.z.powi(2)).sqrt();
        assert!((norm - 1.0).abs() < 1e-10);
    }
}