Exemplo n.º 1
0
def _test_scipy_advance_time():
    mesh = df.UnitIntervalMesh(10)
    sim = Simulation(mesh, Ms=1, unit_length=1e-9, integrator_backend="scipy")
    sim.set_m((1, 0, 0))
    sim.advance_time(1e-12)
    sim.advance_time(2e-12)
    sim.advance_time(2e-12)
    sim.advance_time(2e-12)
Exemplo n.º 2
0
def test_sim_ode(do_plot=False):
    mesh = df.BoxMesh(df.Point(0, 0, 0), df.Point(2, 2, 2), 1, 1, 1)
    sim = Sim(mesh, 8.6e5, unit_length=1e-9, pbc='2d')
    sim.alpha = alpha
    sim.set_m((1, 0, 0))

    sim.set_tol(1e-12, 1e-14)

    H0 = 1e5
    sim.add(Zeeman((0, 0, H0)))

    dt = 1e-12
    ts = np.linspace(0, 500 * dt, 100)

    precession_coeff = sim.gamma / (1 + alpha**2)
    mz_ref = np.tanh(precession_coeff * alpha * H0 * ts)

    mzs = []
    length_error = []
    for t in ts:
        sim.advance_time(t)
        mm = sim.m.copy()

        mm.shape = (3, -1)
        mx, my, mz = mm[:, 0]  # same as m_average for this macrospin problem
        mzs.append(mz)
        length = np.sqrt(mx**2 + my**2 + mz**2)
        length_error.append(abs(length - 1.0))

    if do_plot:
        ts_ns = ts * 1e9
        plt.plot(ts_ns, mzs, "b.", label="computed")
        plt.plot(ts_ns, mz_ref, "r-", label="analytical")
        plt.xlabel("time (ns)")
        plt.ylabel("mz")
        plt.title("integrating a macrospin")
        plt.legend()
        plt.savefig(os.path.join(MODULE_DIR, "test_sim_ode.png"))

    print("Deviation = {}, total value={}".format(np.max(np.abs(mzs - mz_ref)),
                                                  mz_ref))

    assert np.max(np.abs(mzs - mz_ref)) < 1e-9
    assert np.max(length_error) < 1e-9
Exemplo n.º 3
0
def compare_with_analytic_solution(alpha=0.5, max_t=1e-9):
    """
    Compares the C/dolfin/odeint solution to the analytical one.

    """
    print "Running comparison with alpha={0}.".format(alpha)

    # define 3d mesh
    x0 = y0 = z0 = 0
    x1 = y1 = z1 = 10e-9
    nx = ny = nz = 1
    mesh = dolfin.BoxMesh(dolfin.Point(x0, x1, y0), dolfin.Point(y1, z0, z1),
                          nx, ny, nz)

    sim = Simulation(mesh, Ms=1)
    sim.alpha = alpha
    sim.set_m((1, 0, 0))
    sim.add(Zeeman((0, 0, 1e6)))

    # plug in an integrator with lower tolerances
    sim.set_tol(abstol=1e-12, reltol=1e-12)

    ts = numpy.linspace(0, max_t, num=100)
    ys = numpy.array([(sim.advance_time(t), sim.m.copy())[1] for t in ts])
    tsfine = numpy.linspace(0, max_t, num=1000)
    m_analytical = make_analytic_solution(1e6, alpha, sim.gamma)
    save_plot(ts, ys, tsfine, m_analytical, alpha)

    TOLERANCE = 1e-6  # tolerance on Ubuntu 11.10, VM Hans, 25/02/2012

    rel_diff_maxs = list()
    for i in range(len(ts)):
        m = numpy.mean(ys[i].reshape((3, -1)), axis=1)
        m_ref = m_analytical(ts[i])
        diff = numpy.abs(m - m_ref)
        diff_max = numpy.max(diff)
        rel_diff_max = numpy.max(diff / numpy.max(m_ref))
        rel_diff_maxs.append(rel_diff_max)

        print "t= {0:.3g}, diff_max= {1:.3g}.".format(ts[i], diff_max)

        msg = "Diff at t= {0:.3g} too large.\nAllowed {1:.3g}. Got {2:.3g}."
        assert diff_max < TOLERANCE, msg.format(ts[i], TOLERANCE, diff_max)
    print "Maximal relative difference: "
    print numpy.max(numpy.array(rel_diff_maxs))