def _get_initial_dy(self, base_residual, ts, ys): """Calculate a step with imr to get dydt at y_n. """ # We want to ignore the most recent two steps (the one being solved # for "now" outside this function and the one we are computing the # derivative at). We also want to ensure nothing is modified in the # solutions list: temp_ts = copy.deepcopy(ts[:-2]) temp_ys = copy.deepcopy(ys[:-2]) # Timestep should be double the timestep used for the previous # step, so that the imr is at y_n. dt_n = 2*(ts[-2] - ts[-3]) # Calculate time step temp_ts, temp_ys = _odeint(base_residual, temp_ts, temp_ys, dt_n, temp_ts[-1] + dt_n, imr_residual) # Check that we got the right times: the midpoint should be at # the step before the most recent time. utils.assert_almost_equal((temp_ts[-1] + temp_ts[-2])/2, ts[-2]) # Now invert imr to get the derivative dy_nph = (temp_ys[-1] - temp_ys[-2])/dt_n # Fill in dummys (as many as we have y values) followed by the # derivative we just calculated. self.dys = [float('nan')] * (len(ys)-1) self.dys[-1] = dy_nph
def _get_initial_dy(self, base_residual, ts, ys): """Calculate a step with imr to get dydt at y_n. """ # We want to ignore the most recent two steps (the one being solved # for "now" outside this function and the one we are computing the # derivative at). We also want to ensure nothing is modified in the # solutions list: temp_ts = copy.deepcopy(ts[:-2]) temp_ys = copy.deepcopy(ys[:-2]) # Timestep should be double the timestep used for the previous # step, so that the imr is at y_n. dt_n = 2 * (ts[-2] - ts[-3]) # Calculate time step temp_ts, temp_ys = _odeint(base_residual, temp_ts, temp_ys, dt_n, temp_ts[-1] + dt_n, imr_residual) # Check that we got the right times: the midpoint should be at # the step before the most recent time. utils.assert_almost_equal((temp_ts[-1] + temp_ts[-2]) / 2, ts[-2]) # Now invert imr to get the derivative dy_nph = (temp_ys[-1] - temp_ys[-2]) / dt_n # Fill in dummys (as many as we have y values) followed by the # derivative we just calculated. self.dys = [float('nan')] * (len(ys) - 1) self.dys[-1] = dy_nph
def check_vector_timestepper(method, tol): def residual(t, y, dydt): return sp.array([-1.0 * sin(t), y[1]]) - dydt tmax = 1.0 ts, ys = odeint(residual, [cos(0.0), exp(0.0)], tmax, dt=0.001, method=method) utils.assert_almost_equal(ys[-1][0], cos(tmax), tol[0]) utils.assert_almost_equal(ys[-1][1], exp(tmax), tol[1])
def check_exp_timestepper(method, tol): def residual(t, y, dydt): return y - dydt tmax = 1.0 dt = 0.001 ts, ys = odeint(exp_residual, [exp(0.0)], tmax, dt=dt, method=method) # plt.plot(ts,ys) # plt.plot(ts, map(exp,ts), '--r') # plt.show() utils.assert_almost_equal(ys[-1], exp(tmax), tol)
def test_t_at_time_point(): tests = [ 0, 1, sRat(1, 2), sRat(3, 2), 1 + sRat(1, 2), sRat(4, 5), ] ts = range(11) for pt in tests: utils.assert_almost_equal(t_at_time_point(ts, pt), 10 - pt)
def recompute_alpha_varying_fields( sph_start, sph_end, t_start, t_end, mag_params): """ Compute effective damping from change in magnetisation and change in applied field. See notes 30/7/13 pg 5. Derivatives are estimated using BDF1 finite differences. """ # Only for normalised problems! assert(mag_params.Ms == 1) # Get some values dt = t_end - t_start m_cart_end = utils.sph2cart(sph_end) h_eff_end = heff(mag_params, t_end, m_cart_end) mxh = sp.cross(m_cart_end, h_eff_end) # Finite difference derivatives dhadt = (mag_params.Hvec(t_start) - mag_params.Hvec(t_end))/dt dedt = (llg_state_energy(sph_end, mag_params, t_end) - llg_state_energy(sph_start, mag_params, t_start) )/dt dmdt = (sp.array(utils.sph2cart(sph_start)) - sp.array(m_cart_end))/dt utils.assert_almost_equal(dedt, sp.dot(m_cart_end, dhadt) + sp.dot(dmdt, h_eff_end), 1e-2) # print(sp.dot(m_cart_end, dhadt), dedt) # Calculate alpha itself using the forumla derived in notes alpha = ((dedt - sp.dot(m_cart_end, dhadt)) / (sp.dot(h_eff_end, sp.cross(m_cart_end, dmdt)))) return alpha
def check_zeeman(m, H, ans): """Helper function for test_zeeman.""" mag_params = utils.MagParameters() mag_params.Hvec = H utils.assert_almost_equal(energy.zeeman_energy(m, mag_params), ans(mag_params))
def test_t_at_time_point(): tests = [0, 1, sRat(1, 2), sRat(3, 2), 1 + sRat(1, 2), sRat(4, 5), ] ts = range(11) for pt in tests: utils.assert_almost_equal(t_at_time_point(ts, pt), 10 - pt)