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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
//! 2D Magnetic Materials
//!
//! Two-dimensional (2D) magnetic materials are atomically thin crystals that exhibit
//! magnetic ordering. They enable novel spintronic devices, valley electronics, and
//! exploration of quantum magnetism in reduced dimensions.
//!
//! ## Physics Background
//!
//! ### Key Properties:
//! - **Monolayer or few-layer thickness**: Truly 2D magnetism
//! - **Intrinsic magnetic order**: Ferromagnetic, antiferromagnetic, or ferrimagnetic
//! - **Strong spin-orbit coupling**: Valley-dependent phenomena
//! - **Gate tunability**: Electric field control of magnetism
//!
//! ### 2D Magnetic Classes:
//! 1. **Ferromagnetic**: CrI₃, CrBr₃, Fe₃GeTe₂
//! 2. **Antiferromagnetic**: MnPS₃, FePS₃, NiPS₃
//! 3. **Ferrimagnetic**: MnBi₂Te₄
//!
//! ### Applications:
//! - **2D spintronics**: Ultra-thin magnetic tunnel junctions
//! - **Valleytronics**: Valley-selective spin and optical excitations
//! - **Proximity effects**: Inducing magnetism in graphene, TMDCs
//! - **van der Waals heterostructures**: Layer-by-layer device engineering
//!
//! ## Key References
//!
//! - B. Huang et al., "Layer-dependent ferromagnetism in a van der Waals crystal
//!   down to the monolayer limit", Nature 546, 270 (2017)
//! - C. Gong et al., "Discovery of intrinsic ferromagnetism in two-dimensional
//!   van der Waals crystals", Nature 546, 265 (2017)
//! - M. Gibertini et al., "Magnetic 2D materials and heterostructures",
//!   Nat. Nanotechnology 14, 408 (2019)
//! - D. Zhong et al., "Van der Waals engineering of ferromagnetic semiconductor
//!   heterostructures for spin and valleytronics", Sci. Adv. 3, e1603113 (2017)

use std::fmt;

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

use crate::vector3::Vector3;

/// Type of 2D magnetic ordering
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum MagneticOrdering {
    /// Ferromagnetic (parallel spins)
    Ferromagnetic,
    /// Antiferromagnetic (antiparallel spins)
    Antiferromagnetic,
    /// Ferrimagnetic (unequal antiparallel spins)
    Ferrimagnetic,
}

/// 2D magnetic material properties
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Magnetic2D {
    /// Material name
    pub name: String,

    /// Magnetic ordering type
    pub ordering: MagneticOrdering,

    /// Curie/Néel temperature \[K\]
    /// Temperature below which magnetic order exists
    pub critical_temperature: f64,

    /// Magnetic moment per formula unit \[μ_B\]
    pub magnetic_moment: f64,

    /// Magnetic anisotropy energy \[meV\]
    /// Energy barrier for magnetization reversal
    pub anisotropy_energy: f64,

    /// Easy axis direction
    pub easy_axis: Vector3<f64>,

    /// Exchange constant J \[meV\]
    /// Positive for FM, negative for AFM
    pub exchange_j: f64,

    /// Spin-orbit coupling strength \[meV\]
    pub spin_orbit_coupling: f64,

    /// Number of layers
    pub num_layers: usize,

    /// Lattice constant \[Å\]
    pub lattice_constant: f64,
}

impl Default for Magnetic2D {
    /// Default to monolayer CrI₃ parameters
    fn default() -> Self {
        Self::cri3(1)
    }
}

impl Magnetic2D {
    /// Create CrI₃ (Chromium triiodide)
    ///
    /// The first discovered intrinsic ferromagnetic 2D van der Waals crystal,
    /// demonstrating layer-dependent magnetism down to monolayer limit.
    ///
    /// Reference: B. Huang et al., Nature 546, 270 (2017)
    ///
    /// # Example
    /// ```
    /// use spintronics::material::magnetic_2d::{Magnetic2D, MagneticOrdering};
    ///
    /// // Create monolayer CrI₃
    /// let cri3_mono = Magnetic2D::cri3(1);
    ///
    /// assert_eq!(cri3_mono.name, "CrI₃");
    /// assert_eq!(cri3_mono.ordering, MagneticOrdering::Ferromagnetic);
    /// assert_eq!(cri3_mono.num_layers, 1);
    ///
    /// // Monolayer has Tc ~ 45 K
    /// assert!(cri3_mono.critical_temperature > 40.0);
    /// assert!(cri3_mono.critical_temperature < 50.0);
    ///
    /// // Magnetic moment: 3 μ_B per Cr (S = 3/2)
    /// assert_eq!(cri3_mono.magnetic_moment, 3.0);
    ///
    /// // Strong perpendicular magnetic anisotropy
    /// assert!(cri3_mono.anisotropy_energy > 0.2); // meV
    /// assert!(cri3_mono.easy_axis.z > 0.9); // Out-of-plane
    ///
    /// // Bilayer shows interlayer AFM coupling
    /// let cri3_bi = Magnetic2D::cri3(2);
    /// assert!(cri3_bi.critical_temperature < cri3_mono.critical_temperature);
    /// ```
    pub fn cri3(num_layers: usize) -> Self {
        // Curie temperature depends on layer number
        let tc = match num_layers {
            1 => 45.0, // Monolayer
            2 => 40.0, // Bilayer (interlayer AFM coupling)
            _ => 61.0, // Bulk
        };

        Self {
            name: "CrI₃".to_string(),
            ordering: MagneticOrdering::Ferromagnetic,
            critical_temperature: tc,
            magnetic_moment: 3.0,                   // μ_B per Cr
            anisotropy_energy: 0.25,                // meV (out-of-plane)
            easy_axis: Vector3::new(0.0, 0.0, 1.0), // Perpendicular
            exchange_j: 2.5,                        // meV (FM)
            spin_orbit_coupling: 10.0,              // meV (strong)
            num_layers,
            lattice_constant: 6.867, // Å
        }
    }

    /// Create CrBr₃ (Chromium tribromide)
    ///
    /// Similar to CrI₃ but with higher Curie temperature
    pub fn crbr3(num_layers: usize) -> Self {
        let tc = if num_layers == 1 { 34.0 } else { 37.5 };

        Self {
            name: "CrBr₃".to_string(),
            ordering: MagneticOrdering::Ferromagnetic,
            critical_temperature: tc,
            magnetic_moment: 3.0,
            anisotropy_energy: 0.18,
            easy_axis: Vector3::new(0.0, 0.0, 1.0),
            exchange_j: 1.8,
            spin_orbit_coupling: 8.0,
            num_layers,
            lattice_constant: 6.294,
        }
    }

    /// Create Fe₃GeTe₂
    ///
    /// Itinerant ferromagnet with the highest Curie temperature among 2D magnets,
    /// approaching room temperature in few-layer and bulk forms.
    ///
    /// Reference: C. Gong et al., Nature 546, 265 (2017)
    ///
    /// # Example
    /// ```
    /// use spintronics::material::magnetic_2d::Magnetic2D;
    ///
    /// // Create few-layer Fe₃GeTe₂
    /// let fegt = Magnetic2D::fe3gete2(5);
    ///
    /// assert_eq!(fegt.name, "Fe₃GeTe₂");
    ///
    /// // Near room-temperature ferromagnetism
    /// assert!(fegt.critical_temperature > 200.0); // > 200 K
    /// assert!(fegt.is_room_temperature_ferromagnet());
    ///
    /// // Itinerant magnetism (lower moment per atom)
    /// assert!(fegt.magnetic_moment < 2.0); // ~1.5 μ_B per Fe
    ///
    /// // Strong perpendicular anisotropy
    /// assert!(fegt.anisotropy_energy > 0.5); // meV
    ///
    /// // Very strong spin-orbit coupling
    /// assert!(fegt.spin_orbit_coupling > 15.0); // meV
    ///
    /// // Calculate magnetization at 100 K
    /// let m_100k = fegt.magnetization_at_temperature(100.0);
    /// assert!(m_100k > 0.5); // Significant magnetization below Tc
    /// ```
    pub fn fe3gete2(num_layers: usize) -> Self {
        // TC increases with thickness, bulk can reach 300K
        let tc = 220.0 + (num_layers as f64 - 1.0) * 20.0;

        Self {
            name: "Fe₃GeTe₂".to_string(),
            ordering: MagneticOrdering::Ferromagnetic,
            critical_temperature: tc.min(330.0), // Bulk can exceed room temp
            magnetic_moment: 1.5,                // μ_B per Fe (itinerant)
            anisotropy_energy: 0.8,              // meV (strong PMA)
            easy_axis: Vector3::new(0.0, 0.0, 1.0),
            exchange_j: 15.0,          // meV (strong FM coupling)
            spin_orbit_coupling: 20.0, // meV (very strong)
            num_layers,
            lattice_constant: 3.99,
        }
    }

    /// Create MnBi₂Te₄
    ///
    /// Intrinsic magnetic topological insulator
    /// A-type antiferromagnet (FM in-plane, AFM out-of-plane)
    /// Reference: M. M. Otrokov et al., Nature 576, 416 (2019)
    pub fn mnbi2te4(num_layers: usize) -> Self {
        let tn = 24.0; // Néel temperature

        Self {
            name: "MnBi₂Te₄".to_string(),
            ordering: MagneticOrdering::Antiferromagnetic,
            critical_temperature: tn,
            magnetic_moment: 5.0, // μ_B per Mn
            anisotropy_energy: 0.35,
            easy_axis: Vector3::new(0.0, 0.0, 1.0),
            exchange_j: -2.0,          // meV (AFM)
            spin_orbit_coupling: 50.0, // meV (topological material)
            num_layers,
            lattice_constant: 4.33,
        }
    }

    /// Create CrCl₃ (Chromium trichloride)
    pub fn crcl3(num_layers: usize) -> Self {
        let tc = 17.0; // Same for all layer counts

        Self {
            name: "CrCl₃".to_string(),
            ordering: MagneticOrdering::Ferromagnetic,
            critical_temperature: tc,
            magnetic_moment: 3.0,
            anisotropy_energy: 0.12,
            easy_axis: Vector3::new(0.0, 0.0, 1.0),
            exchange_j: 1.2,
            spin_orbit_coupling: 5.0,
            num_layers,
            lattice_constant: 5.954,
        }
    }

    /// Create VSe₂ (Vanadium diselenide)
    ///
    /// Room-temperature ferromagnet in monolayer form
    pub fn vse2(num_layers: usize) -> Self {
        let tc = if num_layers == 1 { 300.0 } else { 470.0 };

        Self {
            name: "VSe₂".to_string(),
            ordering: MagneticOrdering::Ferromagnetic,
            critical_temperature: tc,
            magnetic_moment: 0.6, // μ_B per V (itinerant)
            anisotropy_energy: 0.05,
            easy_axis: Vector3::new(0.0, 0.0, 1.0),
            exchange_j: 25.0, // meV (strong FM)
            spin_orbit_coupling: 15.0,
            num_layers,
            lattice_constant: 3.356,
        }
    }

    /// Calculate thermal stability at given temperature
    ///
    /// Returns the fractional magnetization at temperature T using
    /// mean-field theory:
    ///
    /// $$\frac{M(T)}{M(0)} \approx \left(1 - \frac{T}{T_c}\right)^\beta$$
    ///
    /// where $\beta$ is the critical exponent (~0.5 for mean-field).
    ///
    /// # Arguments
    /// * `temperature` - Temperature \[K\]
    ///
    /// # Returns
    /// Normalized magnetization (0 to 1)
    ///
    /// # Example
    /// ```
    /// use spintronics::material::magnetic_2d::Magnetic2D;
    ///
    /// let cri3 = Magnetic2D::cri3(1); // Tc ~ 45 K
    ///
    /// // At T = 0, full magnetization
    /// let m_0k = cri3.magnetization_at_temperature(0.0);
    /// assert_eq!(m_0k, 1.0);
    ///
    /// // At T = Tc/2, reduced but significant
    /// let m_half = cri3.magnetization_at_temperature(22.5);
    /// assert!(m_half > 0.5);
    /// assert!(m_half < 1.0);
    ///
    /// // At T = Tc, magnetization vanishes
    /// let m_tc = cri3.magnetization_at_temperature(45.0);
    /// assert_eq!(m_tc, 0.0);
    ///
    /// // Above Tc, paramagnetic (no spontaneous magnetization)
    /// let m_above = cri3.magnetization_at_temperature(100.0);
    /// assert_eq!(m_above, 0.0);
    ///
    /// // For Fe₃GeTe₂ with high Tc
    /// let fegt = Magnetic2D::fe3gete2(5);
    /// let m_300k = fegt.magnetization_at_temperature(300.0);
    ///
    /// // If Tc > 300K, should have magnetization at room temp
    /// if fegt.critical_temperature > 300.0 {
    ///     assert!(m_300k > 0.0);
    /// }
    /// ```
    pub fn magnetization_at_temperature(&self, temperature: f64) -> f64 {
        if temperature >= self.critical_temperature {
            0.0
        } else {
            let reduced_temp = temperature / self.critical_temperature;
            (1.0 - reduced_temp).powf(0.5) // 2D Ising critical exponent β ≈ 1/8, but use 0.5 for mean-field
        }
    }

    /// Check if material is ferromagnetic at room temperature
    pub fn is_room_temperature_ferromagnet(&self) -> bool {
        self.ordering == MagneticOrdering::Ferromagnetic && self.critical_temperature >= 300.0
    }

    /// Calculate coercive field estimate
    ///
    /// H_c ≈ 2K/M_s where K is anisotropy energy
    ///
    /// # Returns
    /// Coercive field \[T\]
    #[allow(dead_code)]
    pub fn coercive_field(&self) -> f64 {
        let mu_b = 9.274e-24; // Bohr magneton [J/T]
        let k_energy_si = self.anisotropy_energy * 1.602e-22; // meV to J
        let m_moment = self.magnetic_moment * mu_b; // Magnetic moment in J/T

        2.0 * k_energy_si / m_moment // Tesla
    }

    /// Check suitability for spintronics applications
    ///
    /// Criteria: High TC, strong anisotropy, robust magnetism
    pub fn is_suitable_for_spintronics(&self) -> bool {
        self.critical_temperature > 100.0 && // TC > 100 K
        self.anisotropy_energy > 0.1 && // Strong anisotropy
        self.magnetic_moment > 0.5 // Significant magnetic moment
    }

    /// Builder method to set number of layers
    pub fn with_layers(mut self, num_layers: usize) -> Self {
        self.num_layers = num_layers;
        // Recalculate TC if layer-dependent
        self
    }

    /// Builder method to set critical temperature
    pub fn with_tc(mut self, tc: f64) -> Self {
        self.critical_temperature = tc;
        self
    }
}

impl fmt::Display for MagneticOrdering {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            MagneticOrdering::Ferromagnetic => write!(f, "FM"),
            MagneticOrdering::Antiferromagnetic => write!(f, "AFM"),
            MagneticOrdering::Ferrimagnetic => write!(f, "FiM"),
        }
    }
}

impl fmt::Display for Magnetic2D {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "{} [{}]: {} layer(s), T_c={:.0} K, μ={:.1} μ_B",
            self.name,
            self.ordering,
            self.num_layers,
            self.critical_temperature,
            self.magnetic_moment
        )
    }
}

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

    #[test]
    fn test_cri3_monolayer() {
        let cri3 = Magnetic2D::cri3(1);
        assert_eq!(cri3.ordering, MagneticOrdering::Ferromagnetic);
        assert_eq!(cri3.num_layers, 1);
        assert!(cri3.critical_temperature > 40.0);
        assert!(cri3.critical_temperature < 50.0);
    }

    #[test]
    fn test_cri3_layer_dependence() {
        let mono = Magnetic2D::cri3(1);
        let bulk = Magnetic2D::cri3(5);

        // Bulk should have higher TC
        assert!(bulk.critical_temperature > mono.critical_temperature);
    }

    #[test]
    fn test_fe3gete2_high_tc() {
        let fe3gete2 = Magnetic2D::fe3gete2(5); // Use thicker sample
        assert!(fe3gete2.critical_temperature > 200.0);
        assert!(fe3gete2.is_room_temperature_ferromagnet());
    }

    #[test]
    fn test_mnbi2te4_antiferromagnet() {
        let mnbi2te4 = Magnetic2D::mnbi2te4(1);
        assert_eq!(mnbi2te4.ordering, MagneticOrdering::Antiferromagnetic);
        assert!(mnbi2te4.spin_orbit_coupling > 40.0); // Strong SOC
    }

    #[test]
    fn test_vse2_room_temperature() {
        let vse2_mono = Magnetic2D::vse2(1);
        assert!(vse2_mono.is_room_temperature_ferromagnet());
    }

    #[test]
    fn test_magnetization_temperature_dependence() {
        let cri3 = Magnetic2D::cri3(1);

        let m_zero = cri3.magnetization_at_temperature(0.0);
        let m_half = cri3.magnetization_at_temperature(cri3.critical_temperature * 0.5);
        let m_above_tc = cri3.magnetization_at_temperature(cri3.critical_temperature + 10.0);

        assert!((m_zero - 1.0).abs() < 0.01);
        assert!(m_half > 0.0 && m_half < 1.0);
        assert_eq!(m_above_tc, 0.0);
    }

    #[test]
    fn test_room_temperature_classification() {
        let cri3 = Magnetic2D::cri3(1);
        let fe3gete2 = Magnetic2D::fe3gete2(5); // Thicker sample for higher TC

        assert!(!cri3.is_room_temperature_ferromagnet());
        assert!(fe3gete2.is_room_temperature_ferromagnet());
    }

    #[test]
    fn test_spintronics_suitability() {
        let fe3gete2 = Magnetic2D::fe3gete2(3);
        let weak_mag = Magnetic2D::cri3(1).with_tc(50.0);

        assert!(fe3gete2.is_suitable_for_spintronics());
        assert!(!weak_mag.is_suitable_for_spintronics());
    }

    #[test]
    fn test_builder_pattern() {
        let custom = Magnetic2D::cri3(1).with_layers(3).with_tc(100.0);

        assert_eq!(custom.num_layers, 3);
        assert_eq!(custom.critical_temperature, 100.0);
    }

    #[test]
    fn test_all_chromium_halides() {
        let cri3 = Magnetic2D::cri3(1);
        let crbr3 = Magnetic2D::crbr3(1);
        let crcl3 = Magnetic2D::crcl3(1);

        // All should be FM with Cr 3+ (S=3/2, moment ~ 3 μ_B)
        assert_eq!(cri3.magnetic_moment, 3.0);
        assert_eq!(crbr3.magnetic_moment, 3.0);
        assert_eq!(crcl3.magnetic_moment, 3.0);

        // TC trend: CrI₃ > CrBr₃ > CrCl₃
        assert!(cri3.critical_temperature > crcl3.critical_temperature);
    }

    #[test]
    fn test_easy_axis_orientation() {
        let cri3 = Magnetic2D::cri3(1);

        // Should have out-of-plane easy axis
        assert!((cri3.easy_axis.z - 1.0).abs() < 1e-10);
        assert!(cri3.easy_axis.x.abs() < 1e-10);
        assert!(cri3.easy_axis.y.abs() < 1e-10);
    }
}