def euler_step(torque, dt, m, hext, Jc): """Takes one step using the Euler method torque: function to calculate torque from m, hext and Jc dt: time step m: moment unit vector hext: external field Jc: current density """ t = torque(m, hext, Jc) return normalize(m + dt * t)
def rk23_step(torque, dt, m, hext, Jc): """ Takes one step using the Bogacki-Shampine method (Runga-Kutta RK23) torque: function to calculate torque from m, hext and Jc dt: time step m: moment unit vector hext: external field Jc: current density """ k1 = torque(m, hext, Jc) k2 = torque(m + dt * k1 / 2.0, hext, Jc) k3 = torque(m + 3.0 * dt * k2 / 2.0, hext, Jc) m = m + 2.0 * dt * k1 / 9.0 + dt * k2 / 3.0 + 4 * dt * k3 / 9.0 return normalize(m)
def huen_step(torque, dt, m, hext, Jc): """ Takes one step using Huen's method torque: function to calculate torque from m, hext and Jc dt: time step m: moment unit vector hext: external field Jc: current density """ k1 = torque(m) m1 = m + dt * k1 k2 = torque(m1) m = m + dt * (k1 + k2) / 2.0 return normalize(m)
def rk4_step(torque, dt, m, hext, Jc): """ Takes one step using the Classic 4th order Runga-Kutta method torque: function to calculate torque from m, hext and Jc dt: time step m: moment unit vector hext: external field Jc: current density """ k1 = torque(m, hext, Jc) k2 = torque(m + dt * k1 / 2.0, hext, Jc) k3 = torque(m + dt * k2 / 2.0, hext, Jc) k4 = torque(m + dt * k3, hext, Jc) m = m + dt * (k1 + 2.0 * k2 + 2.0 * k3 + k4) / 6.0 return normalize(m)
def reset(self): """ Resets the kernel to the initial conditions """ self.m = normalize(self.m0) self.t = 0.0