pub trait Device: Send + Sync {
// Required methods
fn name(&self) -> &'static str;
fn is_available(&self) -> bool;
fn step_llg_rk4(
&self,
spins: &mut [[f64; 3]],
h_eff: &[[f64; 3]],
alpha: f64,
dt: f64,
) -> Result<()>;
fn step_llg_rk4_multi(
&self,
spins: &mut [[f64; 3]],
h_eff: &[[f64; 3]],
alpha: f64,
dt: f64,
n_steps: usize,
) -> Result<()>;
fn zeeman_energy(
&self,
spins: &[[f64; 3]],
h: &[[f64; 3]],
ms: f64,
) -> Result<f64>;
// Provided method
fn max_spins(&self) -> usize { ... }
}Expand description
Device abstraction for LLG simulation on CPU or GPU backends.
All operations work on flat &mut [[f64; 3]] arrays so the trait can be
satisfied identically by CPU code (which mutates in place) and GPU code
(which copies to/from device memory). The trait is object-safe: no
generic methods, no Self in arguments other than &self, and no
Sized requirements — so Box<dyn Device> works.
Required Methods§
Sourcefn name(&self) -> &'static str
fn name(&self) -> &'static str
Short device kind name, e.g. "cpu", "cuda".
This is the discriminator used by user code that wants to dispatch on backend identity (e.g. for logging or feature gating). For richer descriptions, downcast to the concrete type.
Sourcefn is_available(&self) -> bool
fn is_available(&self) -> bool
Whether the device backend is currently functional.
CPU is always true. CUDA is true only if the runtime is available
and a device handle was successfully obtained. In the v0.9.0
skeleton, [CudaDevice::is_available] always returns false.
Sourcefn step_llg_rk4(
&self,
spins: &mut [[f64; 3]],
h_eff: &[[f64; 3]],
alpha: f64,
dt: f64,
) -> Result<()>
fn step_llg_rk4( &self, spins: &mut [[f64; 3]], h_eff: &[[f64; 3]], alpha: f64, dt: f64, ) -> Result<()>
Evolve spins by one RK4 step under the given per-spin effective
field. Each spin is renormalised to unit length after the step.
§Arguments
spins— per-spin magnetisation direction, mutated in placeh_eff— per-spin effective field [T], frozen during the stepalpha— Gilbert damping (dimensionless)dt— time step [s]
§Errors
Returns crate::error::Error::DimensionMismatch if spins.len() != h_eff.len(). GPU backends may additionally return
crate::error::Error::NumericalError for runtime / kernel-launch
failures.
Sourcefn step_llg_rk4_multi(
&self,
spins: &mut [[f64; 3]],
h_eff: &[[f64; 3]],
alpha: f64,
dt: f64,
n_steps: usize,
) -> Result<()>
fn step_llg_rk4_multi( &self, spins: &mut [[f64; 3]], h_eff: &[[f64; 3]], alpha: f64, dt: f64, n_steps: usize, ) -> Result<()>
Evolve spins by n_steps RK4 steps with the same per-spin h_eff
(frozen field — no field update between steps).
Convenience wrapper for the common “relax under fixed field” pattern; GPU implementations can amortise device transfers.
Sourcefn zeeman_energy(
&self,
spins: &[[f64; 3]],
h: &[[f64; 3]],
ms: f64,
) -> Result<f64>
fn zeeman_energy( &self, spins: &[[f64; 3]], h: &[[f64; 3]], ms: f64, ) -> Result<f64>
Compute the total Zeeman energy of the system:
E = -μ₀ · M_s · Σᵢ (mᵢ · hᵢ)
§Arguments
spins— per-spin (unit) magnetisation directionh— per-spin effective field [T]ms— saturation magnetisation [A/m]
§Errors
Returns crate::error::Error::DimensionMismatch if spins.len() != h.len().
Provided Methods§
Sourcefn max_spins(&self) -> usize
fn max_spins(&self) -> usize
Maximum number of simultaneously-evolvable spins this device can accept in a single batch.
CPU devices return usize::MAX (limited only by host RAM). GPU
devices may report a lower bound based on device-memory capacity.
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".