Example #1
0
def run_simulation(simname, meshfile, hmatrix=None, tol=0.000001):

    T_start = time.time()

    sim = nmag.Simulation(name=simname, phi_BEM=hmatrix)
    Ni = nmag.MagMaterial(name="Ni",
                          Ms=SI(493380, "A/m"),
                          exchange_coupling=SI(7.2e-12, "J/m"),
                          llg_damping=1.0)
    ps = SI(1e-12, "s")
    sim.load_mesh(meshfile, [("thinfilm", Ni)], unit_length=SI(1e-9, "m"))
    sim.set_m([0.05, 0.02, 1.00])
    sim.set_params(ts_abs_tol=tol, ts_rel_tol=tol)
    sim.set_H_ext([0, 0, 0], SI('A/m'))
    dt = SI(5e-12, "s")

    T_end = time.time()
    T_setup = T_end - T_start

    T_start = time.time()
    sim.relax(save=[('averages', every('time', 10 * ps))])
    T_end = time.time()
    T_sim = T_end - T_start
    sim.save_data(['m'])

    number_nodes = len(sim.mesh.points)
    mem_rss = get_memory_of_process() / 1024.0

    return [number_nodes, mem_rss, T_setup, T_sim]
Example #2
0
def run_simulation(simname,meshfile,hmatrix=None,tol=0.000001):
    
    T_start = time.time()
    
    sim = nmag.Simulation(name=simname,phi_BEM=hmatrix)
    Ni = nmag.MagMaterial(name="Ni",
                          Ms=SI(493380, "A/m"),
                          exchange_coupling=SI(7.2e-12, "J/m"),
                          llg_damping=1.0)
    ps = SI(1e-12,"s")
    sim.load_mesh(meshfile, [("thinfilm", Ni)], unit_length=SI(1e-9, "m"))
    sim.set_m([0.05,0.02,1.00])
    sim.set_params(ts_abs_tol=tol,ts_rel_tol=tol)
    sim.set_H_ext([0,0,0], SI('A/m'))
    dt = SI(5e-12, "s")

    T_end = time.time()
    T_setup = T_end - T_start

    T_start = time.time()
    sim.relax(save=[('averages',every('time', 10*ps))])
    T_end = time.time()
    T_sim = T_end - T_start
    sim.save_data(['m'])

    number_nodes = len(sim.mesh.points)
    mem_rss =  get_memory_of_process() / 1024.0
    

    return [number_nodes,mem_rss,T_setup,T_sim]
Example #3
0
def simulation(
    sim_name=None, initial_m=None, Hs=[None], save=[], do=[], have_j=False, damping_Py=0.01, damping_FePt=0.1
):
    if have_j:
        have_j = 1.0
    else:
        have_j = 0.0

    # FeNi, Permalloy
    mat_Py = nmag.MagMaterial(
        name="Py",
        Ms=SI(0.86e6, "A/m"),
        exchange_coupling=SI(13.0e-12, "J/m"),
        llg_damping=damping_Py,
        llg_polarisation=have_j * SI(1.0),
        llg_xi=SI(0.01),
    )

    anis_FePt = nmag.uniaxial_anisotropy(axis=[1, 0, 0], K1=SI(2.5e6, "J/m^3"))

    mat_FePt = nmag.MagMaterial(
        name="FePt",
        Ms=SI(1e6, "A/m"),
        exchange_coupling=SI(10.0e-12, "J/m"),
        anisotropy=anis_FePt,
        llg_damping=damping_FePt,
        llg_polarisation=have_j * SI(1.0),
        llg_xi=SI(0.01),
    )

    s = nmag.Simulation(sim_name)

    s.set_local_magnetic_coupling(mat_Py, mat_FePt, SI(2.0e-4, "N/A^2"))

    s.load_mesh(
        mesh_filename,
        [
            ("HARD-LEFT", mat_FePt),
            ("SOFT-LEFT", mat_Py),
            ("HARD-CENTER", mat_FePt),
            ("SOFT-RIGHT", mat_Py),
            ("HARD-RIGHT", mat_FePt),
        ],
        unit_length=SI(1e-9, "m"),
    )

    if type(initial_m) == str:  # Set the initial magnetisation
        s.load_m_from_h5file(initial_m)  # a) from file if a string is provided

    else:
        s.set_m(initial_m[0], "m_Py")
        s.set_m(initial_m[1], "m_FePt")

    s.hysteresis(Hs, save=save, do=do, convergence_check=every(5, "step"))
    return s
Example #4
0
def run_simulation(periodic=False, use_hlib=False):
  phi_BEM = (nmag.default_hmatrix_setup if use_hlib else None)
  if periodic:
    pts = [[i*(15.0 + 0.1), 0, 0] for i in (-3, -2, -1, 1, 2, 3)]
    pbc = nmag.SetLatticePoints(vectorlist=pts, scalefactor=nm)
    sim = nmag.Simulation("periodic", periodic_bc=pbc.structure,
                          phi_BEM=phi_BEM)

  else:
    sim = nmag.Simulation("full", phi_BEM=phi_BEM)

  mat = nmag.MagMaterial("Py",
                         Ms=SI(0.86e6, "A/m"),
                         exchange_coupling=SI(13e-12, "J/m"),
                         llg_damping=0.5)

  mesh_file = "periodic.nmesh.h5" if periodic else "full.nmesh.h5"
  sim.load_mesh(mesh_file, [("mesh", mat)], unit_length=nm)

  sim.set_m([1, 1, 1])

  sim.relax(do=[("exit", at("time", 0.5*ns))],
            save=[("averages", every("time", 10*ps))])
Example #5
0
H_pulse_data = sim.get_subfield("H_ext")

# Do the same for the bias field
sim.set_H_ext(bias_H)
H_bias_data = sim.get_subfield("H_ext")

# Now the applied field at time t will be expressed as
#   H_bias_data + f(t)*H_pulse_data,
# where f(t) is the time dependence of the pulse
def update_H_ext(t_su):
    ut = float(sinc_omega*(t_su*ps - sinc_t0*ps))
    t_amp = math.sin(ut)/ut if abs(ut) > 1e-10 else 1.0

    if abs(t_amp) > 1e-4:
        H_value = H_bias_data + t_amp*H_pulse_data
    else:
        H_value = map(lambda x: float(x/SI("A/m")), bias_H)

    fieldname = 'H_ext'
    sim._fields.set_subfield(None, H_value, SI("A/m"),
                             fieldname=fieldname, auto_normalise=False)
    (mwe, field) = sim._master_mwes_and_fields_by_name[fieldname]
    ocaml.lam_set_field(sim._lam, field, "v_" + fieldname)
    print "*",

sim.pre_rhs_funs.append(update_H_ext)

sim.set_params(stopping_dm_dt=0) # Never stop for convergence
sim.relax(save=[('fields', every('time', 10*ps))],
          do=[('exit', at('stage_time', 10240*ps))])
Example #6
0
# Obtain the initial magnetisation configuration (S-state) if necessary
m0_file = "m0.h5"
if "relax" in sys.argv:
  s = new_simulation('relaxation', alpha=1.0)
  Hs = nmag.vector_set(direction=[1, 1, 1],
                       norm_list=[1.00, 0.95, [], 0.0],
                       units=2e6*SI("A/m"))

  s.set_m([1, 1, 1])
  s.hysteresis(Hs)
  s.save_restart_file(m0_file)
  del s

# Run the dynamic simulation for field 1
if "field1" in sys.argv:
  s = new_simulation('field1')
  s.load_m_from_h5file(m0_file)
  s.set_H_ext([-24.6, 4.3, 0.0], mT/mu0)
  s.relax(save=[('averages', every('time', SI(10e-12, "s"))),
                ('fields', at('convergence'))])

# Run the dynamic simulation for field 2
if "field2" in sys.argv:
  s = new_simulation('field2')
  s.load_m_from_h5file(m0_file)
  s.set_H_ext([-35.5, -6.3, 0.0], mT/mu0)
  s.relax(save=[('averages', every('time', SI(10e-12, "s"))),
                ('fields', at('convergence'))])

Example #7
0
def do_simulation():

  import time
  import nmag

  #import nsim.logtools
  #import logging
  #
  #nsim.logtools.setGlobalLogLevel(logging.DEBUG)
  
  from nmag import SI, mesh
  import os

  mat1 = nmag.MagMaterial(name="mat1",
                          Ms=SI(0.8e6, "A/m"),
                          exchange_coupling=SI(13.0e-12, "J/m"),
                          llg_gamma_G=SI(0.2211e6, "m/A s"),
                          llg_damping=0.5)

  mat2 = nmag.MagMaterial(name="mat2",
                          Ms=SI(0.8e6, "A/m"),
                          exchange_coupling=SI(13.0e-12, "J/m"),
                          llg_gamma_G=SI(0.2211e6, "m/A s"),
                          llg_damping=0.5)

  sim1 = nmag.Simulation(name="simulation1")
  sim1.load_mesh("bar.nmesh.h5", 
                [("one", mat1), ("two", mat2)],
                unit_length=SI(1e-9,"m"))

  sim2 = nmag.Simulation(name="simulation2")
  sim2.load_mesh("bar.nmesh.h5",
                [("one", mat1), ("two", mat1)],
                unit_length=SI(1e-9,"m"))

  import math
  angle_deg = 45
  angle_rad = angle_deg/360.*2*math.pi

  sim2.set_m([math.cos(angle_rad), 0, math.sin(angle_rad)])
  sim2.set_params(ts_rel_tol=2.7e-05 , ts_abs_tol=2.7e-05)

  def norm(p):
    return math.sqrt(p[0]**2 + p[1]**2 + p[2]**2)

  def dist(p1, p2):
    return math.sqrt((p1[0]-p2[0])**2 + (p1[1]-p2[1])**2 + (p1[2]-p2[2])**2)

  # First we check consistency between the meshes
  ps1 = sim1.mesh.points
  ps2 = sim2.mesh.points
  if len(ps1) != len(ps2): raise "Mesh size inconsistency!"
  for i in range(len(ps1)):
    p1, p2 = (ps1[i], ps2[i])
    d = dist(p1, p2)
    if d > 1e-12:
      raise "Mesh coords inconsistency!"

  def build_key(p):
    return ",".join(["%8f" % round(pi, 6) for pi in p])

  # Now we can assume that the indexing is the same
  i = 0
  pcs = {}
  for p in ps1:
    pcs[build_key(p)] = i
    i += 1

  max_ds = []
  max_drels = []

  def consistency_check(_):
    m2 = sim2.get_subfield("m_mat1")

    def m(p):
      return m2[pcs[build_key([pi*1e9 for pi in p])]]

    sim1.set_m(m, "m_mat1")
    sim1.set_m(m, "m_mat2")

    Hd1 = sim1.get_subfield("H_demag")
    Hd2 = sim2.get_subfield("H_demag")
    if len(Hd1) != len(Hd2):
      raise "Demag field size mismatch!"
    n = len(Hd1)
    max_d = None
    max_drel = None
    for i in range(n):
      d = dist(Hd1[i], Hd2[i])
      if max_d == None or d > max_d:
        max_d = d
        max_drel = d/(norm(Hd1[i]) + norm(Hd2[i]))

    max_ds.append(max_d)
    max_drels.append(max_drel)

    msg = "Deviation in the demag fields: " \
          "absolute %s, percentual %s" % (max_d, max_drel)

    out(msg)

  from nmag import every
  sim2.relax(do=[(consistency_check, every("time", SI(50e-12, "s")))], save=[])

  assert max(max_drels) < 0.0011, "Test failed: demag differs by more than 0.1 %"
  assert max(max_ds) < 1000.0, "Test failed: demag differs by more than 1000 A/m"
Example #8
0
import nmag
from nmag import SI, every, at

mat_Py1 = nmag.MagMaterial(name="Py1",
                           Ms=SI(0.86e6, "A/m"),
                           exchange_coupling=SI(13.0e-12, "J/m"),
                           llg_damping=0.5)

mat_Py2 = nmag.MagMaterial(name="Py2",
                           Ms=SI(0.86e6, "A/m"),
                           exchange_coupling=SI(13.0e-12, "J/m"),
                           llg_damping=0.5)

#Example 1: both materials have same magnetisation

sim = nmag.Simulation()

sim.load_mesh("sphere_in_box.nmesh.h5", [("Py1", mat_Py1), ("Py2", mat_Py2)],
              unit_length=SI(15e-9, "m"))

sim.set_m([1, 0, 0])

sim.relax(save=[('averages', 'fields', 'restart',
                 every('step', 1000) | at('convergence'))])
# Set Demag tolerances.
ksp_tols = {"DBC.rtol": 1e-7,
            "DBC.atol": 1e-7,
            "DBC.maxits": 1000000,
            "NBC.rtol": 1e-7,
            "NBC.atol": 1e-7,
            "NBC.maxits": 1000000,
            "PC.rtol": 1e-3,
            "PC.atol": 1e-6,
            "PC.maxits": 1000000}

# Create the simulation object
sim = nmag.Simulation(ksp_tolerances=ksp_tols)

# Load the mesh
sim.load_mesh(mesh_name, [("Permalloy", Py)], unit_length=SI(1e-9, "m"))

# Load the initial magnetisation
sim.load_m_from_h5file(relax_name + ".h5")

# Set the applied magnetic field
sim.set_H_ext(H_direction_normalized, SI(H, 'A/m'))

# Set convergence parameters
sim.set_params(stopping_dm_dt=0.0, ts_abs_tol=1e-7, ts_rel_tol=1e-7)

# Save the information ever 5ps, and exit after 20ns.
sim.relax(save=[('fields', every('time', SI(dt, "s")))],
          do=[('exit', at('time', SI(T, "s")))])
Example #10
0
H_max = 17 * 1e3 * 1e3 / (4 * math.pi)  # koe to A/m

Hs = nmag.vector_set(direction=[0, 0, 1],
                     norm_list=[-1.0, 1.0],
                     units=H_max * SI('A/m'))


def my_save(sim):
    # only save M, m, H_ext
    sim.save_data(fields=['M', 'm', 'H_ext'])


def print_time(sim):
    sim_time = float(sim.time / SI(1e-12, 's'))
    print('----SIM Time %f ps----' % sim_time)


# start time
start_time = time.time()

sim.set_H_ext([0, 0, -H_max], SI('A/m'))
sim.relax(save=[(my_save, at('convergence'))],
          do=[(print_time, every('time', SI(50e-12, 's')) | at('convergence'))
              ])
sim.set_H_ext([0, 0, H_max], SI('A/m'))
sim.relax(save=[(my_save, at('convergence'))],
          do=[(print_time, every('time', SI(50e-12, 's')) | at('convergence'))
              ])

use_time = (time.time() - start_time) / 60
print('Use Time %f min' % use_time)
Example #11
0
import nmag
from nmag import SI, si

# Create the simulation object
sim = nmag.Simulation()

# Define the magnetic material (data from OOMMF materials file)
Fe = nmag.MagMaterial(name="Fe",
                      Ms=SI(1700e3, "A/m"),
                      exchange_coupling=SI(21e-12, "J/m"),
                      anisotropy=nmag.cubic_anisotropy(axis1=[1, 0, 0],
                                                       axis2=[0, 1, 0],
                                                       K1=SI(48e3, "J/m^3")))

# Load the mesh
sim.load_mesh("cube.nmesh", [("cube", Fe)], unit_length=SI(1e-9, "m"))

# Set the initial magnetisation
sim.set_m([0, 0, 1])

# Launch the hysteresis loop
Hs = nmag.vector_set(direction=[1.0, 0, 0.0001],
                     norm_list=[0, 1, [], 19, 19.1, [], 21, 22, [], 50],
                     units=0.001 * si.Tesla / si.mu0)
from nmag import every, at
sim.hysteresis(Hs,
               save=[('averages', 'fields', at('stage_end')),
                     ('restart', at('stage_end') | every('step', 1000))])
Example #12
0
# We model a bar 100 nm x 100 nm x 10 nm where a vortex sits in the center.
# This is part II: we load the vortex from file and apply a spin-polarised current

import nmag
from nmag import SI, every, at

# Define the material
mat_Py = nmag.MagMaterial(name="Py",
                          Ms=SI(0.8e6,"A/m"),
                          exchange_coupling=SI(13.0e-12, "J/m"),
                          llg_gamma_G=SI(0.2211e6, "m/A s"),
                          llg_polarisation=1.0,
                          llg_xi=0.05,
                          llg_damping=0.1)

# Define the simulation object and load the mesh
sim = nmag.Simulation()
sim.load_mesh("pyfilm.nmesh.h5", [("Py", mat_Py)], unit_length=SI(1e-9,"m"))

# Set the initial magnetisation: part II uses the one saved by part I
sim.load_m_from_h5file("vortex_m.h5")
sim.set_current_density([1e12, 0, 0], unit=SI("A/m^2"))

sim.set_params(stopping_dm_dt=0.0) # * WE * decide when the simulation should stop!

sim.relax(save=[('fields', at('convergence') | every('time', SI(1.0e-9, "s"))),
                ('averages', every('time', SI(0.05e-9, "s")) | at('stage_end'))],
          do = [('exit', at('time', SI(10e-9, "s")))])
Example #13
0
  # Set additional parameters for the time-integration and run the simulation
  sim.set_params(stopping_dm_dt=stopping_dm_dt,
                 ts_rel_tol=1e-7, ts_abs_tol=1e-7)
  sim.relax(save=save, do=do)
  return sim

# If the initial magnetisation has not been calculated and saved into
# the file relaxed_m_file, then do it now!
if not os.path.exists(relaxed_m_file):
  # Initial direction for the magnetisation
  def m0(p):
      x, y, z = p
      tmp = min(1.0, max(-1.0, float(SI(x, "m")/(mesh_unit*hl))))
      angle = 0.5*math.pi*tmp
      return [math.sin(angle), math.cos(angle), 0.0]

  save = [('fields', at('step', 0) | at('stage_end')),
          ('averages', every('time', SI(5e-12, 's')))]

  sim = run_simulation(sim_name="relaxation", initial_m=m0,
                       damping=0.5, j=0.0, save=save,
                       stopping_dm_dt=1.0*degrees_per_ns)
  sim.save_restart_file(relaxed_m_file)
  del sim

# Now we simulate the magnetisation dynamics
save = [('averages', every('time', SI(9e-12, 's')))]
do   = [('exit', at('time', SI(6e-9, 's')))]
run_simulation(sim_name="dynamics", initial_m=relaxed_m_file, damping=0.02,
               j=0.1e12, P=1.0, save=save, do=do, stopping_dm_dt=0.0)
Example #14
0
ksp_tols = {
    "DBC.rtol": 1e-7,
    "DBC.atol": 1e-7,
    "DBC.maxits": 1000000,
    "NBC.rtol": 1e-7,
    "NBC.atol": 1e-7,
    "NBC.maxits": 1000000,
    "PC.rtol": 1e-3,
    "PC.atol": 1e-6,
    "PC.maxits": 1000000
}

# Create the simulation object
sim = nmag.Simulation(ksp_tolerances=ksp_tols)

# Load the mesh
sim.load_mesh(mesh_name, [("Permalloy", Py)], unit_length=SI(1e-9, "m"))

# Load the initial magnetisation
sim.load_m_from_h5file(relax_name + ".h5")

# Set the applied magnetic field
sim.set_H_ext(H_direction_normalized, SI(H, 'A/m'))

# Set convergence parameters
sim.set_params(stopping_dm_dt=0.0, ts_abs_tol=1e-7, ts_rel_tol=1e-7)

# Save the information ever 5ps, and exit after 20ns.
sim.relax(save=[('fields', every('time', SI(dt, "s")))],
          do=[('exit', at('time', SI(T, "s")))])
Example #15
0
# example 2 of the nmag documentation
# without demag but with external field

mat_Py = nmag.MagMaterial(name="Py",
                          Ms=SI(0.86e6, "A/m"),
                          exchange_coupling=SI(13.0e-12, "J/m"),
                          llg_damping=0.1)

L = 30.0e-9
H = 10.0e-9
W = 10.0e-9


def m0(r):
    mx = 2 * r[0] / L - 1
    my = 2 * r[1] / W - 1
    mz = 1
    return [mx, my, mz]


sim = nmag.Simulation("bar", do_demag=False)
sim.load_mesh("bar.nmesh.h5", [("Py", mat_Py)], unit_length=SI(1e-9, "m"))
sim.set_H_ext([1, 0, 0], SI(0.43e6, "A/m"))
sim.set_m(m0)

sim.set_params(stopping_dm_dt=1 * degrees_per_ns,
               ts_rel_tol=1e-6,
               ts_abs_tol=1e-6)
sim.relax(save=[('averages', every('time', SI(5e-11, "s")))])
Example #16
0
    sim.set_H_ext(H_ext, unit=disturb_amplitude)


c = float(nm / SI("m"))


def set_disturbance(sim):
    def H_ext(r):
        x, y, z = [xi / c - x0i for xi, x0i in zip(r, disturb_origin)]
        # x, y and z are (floats) in nanometers
        d = math.sqrt(x * x + y * y + z * z)
        if x < disturb_thickness:
            return disturb_direction
        else:
            return [0.0, 0.0, 0.0]

    sim.set_H_ext(H_ext, unit=disturb_amplitude)


s = simulate_nanowire("dynamics", 0.05)
s.load_m_from_h5file(m0_filename)
s.relax(
    save=[("fields", every("time", 0.5 * ps))],
    do=[
        (set_disturbance, at("time", 0 * ps)),
        (set_to_zero, at("time", disturb_duration)),
        ("exit", at("time", 5000 * ps)),
    ],
)
Example #17
0
from nmag import SI, every, at

sim = nmag.Simulation()

# define magnetic material Cobalt (data from OOMMF materials file)
Co = nmag.MagMaterial(name="Co",
                      Ms=SI(1400e3, "A/m"),
                      exchange_coupling=SI(30e-12, "J/m"),
                      anisotropy=nmag.uniaxial_anisotropy(axis=[0,0,1], K1=SI(520e3, "J/m^3")))

# define magnetic material Permalley
Py = nmag.MagMaterial(name="Py",
                      Ms=SI(860e3,"A/m"),
                      exchange_coupling=SI(13.0e-12, "J/m"))

# load mesh
sim.load_mesh("two_cubes.nmesh.h5",
              [("cube1", Py),("cube2", Co)],
              unit_length=SI(1e-9,"m")
              ) 

# set initial magnetisation along the 
# positive x axis for both cubes, slightly off in z-direction
sim.set_m([0.999847695156, 0, 0.01745240643731])

ns = SI(1e-9, "s") # corresponds to one nanosecond

sim.relax(save = [('averages', every('time', 0.01*ns)),
                  ('fields', every('time', 0.05*ns) | at('convergence'))])

Example #18
0
import nmag
from nmag import SI, every, at

mat_Py1 = nmag.MagMaterial(name="Py1",
                          Ms=SI(0.86e6,"A/m"),
                          exchange_coupling=SI(13.0e-12, "J/m"),
                          llg_damping=0.5)

mat_Py2 = nmag.MagMaterial(name="Py2",
                          Ms=SI(0.86e6,"A/m"),
                          exchange_coupling=SI(13.0e-12, "J/m"),
                          llg_damping=0.5)

#Example 1: both materials have same magnetisation

sim = nmag.Simulation()

sim.load_mesh("sphere_in_box.nmesh.h5",
              [("Py1", mat_Py1),("Py2", mat_Py2)],
              unit_length=SI(15e-9,"m"))

sim.set_m([1,0,0])

sim.relax(save = [('averages','fields','restart', every('step', 1000) | at('convergence'))])
Example #19
0
        u = min(1.0, max(0.0, (x - x0) / (x1 - x0)))
        angle = math.pi * (1.0 - u)
        return [math.cos(angle), math.sin(angle), 0]

    def m0_FePt(r):
        return m0_Py(r)

    def save_m(s):
        s.save_restart_file(relaxed_m_file)

    s = simulation(
        sim_name="relax",
        damping_Py=1.0,
        damping_FePt=1.0,
        initial_m=[m0_Py, m0_FePt],
        save=[("averages", every(1000, "step")), ("fields", at("step", 0) | at("stage_end"))],
        do=[(save_m, at("stage_end"))],
    )
    del s


def set_current(j):
    def j_setter(sim):
        sim.set_current_density([j, 0.0, 0.0], unit=SI("A/m^2"))

    return j_setter


ns = SI(1e-9, "s")
simulation(
    sim_name="stt",
Example #20
0
#the computation of the demagnetising field
sim = nmag.Simulation(do_demag = False)

# define magnetic material so that Js = mu0*Ms = 1 T
Py = nmag.MagMaterial(name="Py",
                      Ms=1.0*si.Tesla/si.mu0,
                      exchange_coupling=SI(13.0e-12, "J/m"),
                      llg_damping = SI(0.0)
                      )
# load mesh
sim.load_mesh("sphere1.nmesh.h5",
              [("sphere", Py)],
              unit_length=SI(1e-9,"m")
             )

# set initial magnetisation
sim.set_m([1,1,1])

# set external field
Hs = nmag.vector_set(direction=[0.,0.,1.],
                     norm_list=[1.0],
                     units=1e6*SI('A/m')
                    )

ps = SI(1e-12, "s")  # ps corresponds to one picosecond

# let the magnetisation precess around the direction of the applied field
sim.hysteresis(Hs,
               save=[('averages', every('time', 0.1*ps))],
               do=[('exit', at('time', 300*ps))])
Example #21
0
sim = nmag.Simulation()

# define magnetic material Cobalt (data from OOMMF materials file)
Co = nmag.MagMaterial(name="Co",
                      Ms=SI(1400e3, "A/m"),
                      exchange_coupling=SI(30e-12, "J/m"),
                      anisotropy=nmag.uniaxial_anisotropy(axis=[0, 0, 1],
                                                          K1=SI(
                                                              520e3, "J/m^3")))

# define magnetic material Permalley
Py = nmag.MagMaterial(name="Py",
                      Ms=SI(860e3, "A/m"),
                      exchange_coupling=SI(13.0e-12, "J/m"))

# load mesh
sim.load_mesh("two_cubes.nmesh.h5", [("cube1", Py), ("cube2", Co)],
              unit_length=SI(1e-9, "m"))

# set initial magnetisation along the
# positive x axis for both cubes, slightly off in z-direction
sim.set_m([0.999847695156, 0, 0.01745240643731])

ns = SI(1e-9, "s")  # corresponds to one nanosecond

sim.relax(
    save=[('averages',
           every('time', 0.01 *
                 ns)), ('fields',
                        every('time', 0.05 * ns) | at('convergence'))])
Example #22
0
    v = (1.0e-9, dz, -dy)
    vn = (1.0e-9**2 + dy * dy + dz * dz)**0.5
    return tuple(vi / vn for vi in v)


sim.set_m(m0)

sim.set_H_ext([0, 0, 0], SI("A/m"))

# Direction of the polarization
sim.model.quantities["sl_fix"].set_value(Value([0, 1, 0]))

# Current density
sim.model.quantities["sl_current_density"].set_value(Value(SI(0.1e12,
                                                              "A/m^2")))

if do_relaxation:
    print "DOING RELAXATION"
    sim.relax(save=[("fields", at("time", 0 * ps) | at("convergence"))])
    sim.save_m_to_file(relaxed_m)
    sys.exit(0)

else:
    print "DOING DYNAMICS"
    sim.load_m_from_h5file(relaxed_m)
    sim.set_params(stopping_dm_dt=0 * degrees_per_ns)
    sim.relax(save=[("averages", every("time", 5 * ps))],
              do=[("exit", at("time", 10000 * ps))])

# ipython()
Example #23
0
import nmag
from nmag import SI, every, at

# define magnetic material
Py = nmag.MagMaterial(name="Py",
                      Ms=SI(1e6,"A/m"),
                      exchange_coupling=SI(13.0e-12, "J/m")
                      )

#create simulation object
sim = nmag.Simulation()

# load mesh
sim.load_mesh("prism.nmesh.h5", [("no-periodic", Py)], unit_length=SI(1e-9,"m") )

# set initial magnetisation
sim.set_m([1.,1.,1.])

# loop over the applied field
s = SI(1,"s")

sim.relax(save=[('averages','fields', every('time',5e-12*s) | at('convergence'))])

Example #24
0
import nmag
from nmag import SI, every, at

ocaml.init_hlib("/rhome/ak8w07/mumag/nmag/lib/libhmatrix-1.3.so")


# create simulation object
hp = {}
hp["nfdeg"] = 3
hp["nmin"] = 20
hp["p"] = 4
hp["eta"] = 2.0
hp["eps"] = 0.00001

sim = nmag.Simulation(name="bar_relax", use_hlib=True, use_pvode=False, hlib_params=hp)

mat_Py = nmag.MagMaterial(name="Py", Ms=SI(0.86e6, "A/m"), exchange_coupling=SI(13.0e-12, "J/m"), llg_damping=0.5)


sim.load_mesh("bar30_30_100.nmesh.h5", [("Py", mat_Py)], unit_length=SI(1e-9, "m"))

sim.set_m([1, 0, 1])

ps = SI(1e-12, "s")
sim.relax(save=[("averages", every("time", 5 * ps)), ("fields", at("convergence"))])
Example #25
0
  v = (1.0e-9, dz, -dy)
  vn = (1.0e-9**2 + dy*dy + dz*dz)**0.5
  return tuple(vi/vn for vi in v)

sim.set_m(m0)

sim.set_H_ext([0, 0, 0], SI("A/m"))

# Direction of the polarization
sim.model.quantities["sl_fix"].set_value(Value([0, 1, 0]))

# Current density
sim.model.quantities["sl_current_density"].set_value(Value(SI(0.1e12, "A/m^2")))


if do_relaxation:
  print "DOING RELAXATION"
  sim.relax(save=[("fields", at("time", 0*ps) | at("convergence"))])
  sim.save_m_to_file(relaxed_m)
  sys.exit(0)

else:
  print "DOING DYNAMICS"
  sim.load_m_from_h5file(relaxed_m)
  sim.set_params(stopping_dm_dt=0*degrees_per_ns)
  sim.relax(save=[("averages", every("time", 5*ps))],
            do=[("exit", at("time", 10000*ps))])

#ipython()

Example #26
0
sim.load_mesh('%s' % mesh_file, [('Mat_Hard', Mat_Hard),
                                 ('Mat_Soft', Mat_Soft)],
              unit_length=SI(1e-9, 'm'))

sim.set_m([0, 0, 1])

Hs = nmag.vector_set(direction=[0, 0, 1],
                     norm_list=[0.0, -0.01, [], -1.0],
                     units=H_max * SI('A/m'))


def my_save(sim):
    # only save M, m, H_ext
    sim.save_data(fields=['M', 'm', 'H_ext'])


def print_time(sim):
    sim_time = float(sim.time / SI(1e-12, 's'))
    print('----SIM Time %f ps----' % sim_time)


# start time
start_time = time.time()

sim.hysteresis(Hs,
               save=[(my_save, at('convergence'))],
               do=[(print_time,
                    every('time', SI(50e-12, 's')) | at('convergence'))])

use_time = (time.time() - start_time) / 60
print('Use Time %f min' % use_time)
Example #27
0
import nmag
from nmag import SI, si

# Create the simulation object
sim = nmag.Simulation()

# Define the magnetic material (data from OOMMF materials file)
Fe = nmag.MagMaterial(name="Fe",
                      Ms=SI(1700e3, "A/m"),
                      exchange_coupling=SI(21e-12, "J/m"),
                      anisotropy=nmag.cubic_anisotropy(axis1=[1, 0, 0],
                                                       axis2=[0, 1, 0],
                                                       K1=SI(48e3, "J/m^3")))

# Load the mesh
sim.load_mesh("cube.nmesh", [("cube", Fe)], unit_length=SI(1e-9, "m"))

# Set the initial magnetisation
sim.set_m([0, 0, 1])

# Launch the hysteresis loop
Hs = nmag.vector_set(direction=[1.0, 0, 0.0001],
                     norm_list=[0, 1, [], 19, 19.1, [], 21, 22, [], 50],
                     units=0.001*si.Tesla/si.mu0)
from nmag import every, at
sim.hysteresis(Hs, save=[('averages', 'fields', at('stage_end')),
                         ('restart', at('stage_end') | every('step', 1000))])
Example #28
0
sim.load_mesh("bar30_30_100.nmesh.h5",
              [("Py", mat_Py)],
              unit_length=SI(1e-9,"m"))

sim.set_m([1,0,1])


#Need to call advance time to create timestepper

ps = SI(1e-12, "s") 

sim.advance_time(0*ps)





sim.get_timestepper().set_params(rel_tolerance=1e-8,abs_tolerance=1e-8)

starttime = time.time()

sim.relax(save = [('averages', every('time', 5*ps)),
                  ('fields', at('convergence'))])


stoptime = time.time()

print "Time spent is %5.2f seconds" % (stoptime-starttime)


Example #29
0
from nmag import SI, every, at

mat_Py = nmag.MagMaterial(name="Py",
                          Ms=SI(0.86e6, "A/m"),
                          exchange_coupling=SI(13.0e-12, "J/m"),
                          llg_damping=SI(0.5, ""))

sim = nmag.Simulation()

sim.load_mesh("bar30_30_100.nmesh.h5", [("Py", mat_Py)],
              unit_length=SI(1e-9, "m"))

sim.set_m([1, 0, 1])

#Need to call advance time to create timestepper

ps = SI(1e-12, "s")

sim.advance_time(0 * ps)

sim.get_timestepper().set_params(rel_tolerance=1e-6, abs_tolerance=1e-6)

starttime = time.time()

sim.relax(save=[('averages',
                 every('time', 5 * ps)), ('fields', at('convergence'))])

stoptime = time.time()

print "Time spent is %5.2f seconds" % (stoptime - starttime)
Example #30
0
    # ^^^ this is a technicality: functions have to return pure number.
    #     H_setter returns the field in units of gaussian_amplitude.
    #     We then compute H, the float components in such units.

    def H_setter(r):
        x, y, z = r
        return ([H[0], H[1], H[2]+amplitude]
                if x < 6.5e-9 else H)
        # ^^^ apply the pulse only to the first dot

    H_value = H_setter if abs(amplitude) > 1e-4 else H
    # ^^^ to speed up things

    fieldname = 'H_ext'
    sim._fields.set_subfield(None, # subfieldname
                             H_value,
                             gaussian_amplitude,
                             fieldname=fieldname,
                             auto_normalise=False)
    (mwe, field) = sim._master_mwes_and_fields_by_name[fieldname]
    ocaml.lam_set_field(sim._lam, field, "v_" + fieldname)
    print "*",

sim.pre_rhs_funs.append(update_H_ext)

sim.set_params(stopping_dm_dt=0) # Never stop for convergence
sim.relax(save=[#('averages', every('time', 2.5*ps)),
                ('fields', every('time', 20*ps))],
          do=[('exit', at('stage_time', 5000*ps))])

Example #31
0
# We model a bar 100 nm x 100 nm x 10 nm where a vortex sits in the center.
# This is part II: we load the vortex from file and apply a spin-polarised current

import nmag
from nmag import SI, every, at

# Define the material
mat_Py = nmag.MagMaterial(name="Py",
                          Ms=SI(0.8e6, "A/m"),
                          exchange_coupling=SI(13.0e-12, "J/m"),
                          llg_gamma_G=SI(0.2211e6, "m/A s"),
                          llg_polarisation=1.0,
                          llg_xi=0.05,
                          llg_damping=0.1)

# Define the simulation object and load the mesh
sim = nmag.Simulation()
sim.load_mesh("pyfilm.nmesh.h5", [("Py", mat_Py)], unit_length=SI(1e-9, "m"))

# Set the initial magnetisation: part II uses the one saved by part I
sim.load_m_from_h5file("vortex_m.h5")
sim.set_current_density([1e12, 0, 0], unit=SI("A/m^2"))

sim.set_params(
    stopping_dm_dt=0.0)  # * WE * decide when the simulation should stop!

sim.relax(save=[('fields', at('convergence') | every('time', SI(1.0e-9, "s"))),
                ('averages', every('time', SI(0.05e-9, "s")) | at('stage_end'))
                ],
          do=[('exit', at('time', SI(10e-9, "s")))])
Example #32
0
              unit_length=SI(1e-9, 'm'))
# set initial magnetisation
sim.set_m([0, 0, 1])


def my_save(sim):
    # only save these Terms and Subfields
    H_ext = sim.get_subfield_average_siv('H_ext')
    print('Save fields of stage %d with H_ext %s' %(sim.stage, str(H_ext)))
    sim.save_data(fields=['id', 'time', 'step', 'stage_time', 'stage_step', 'H_ext', 'M', 'm'])


def my_stage(sim):
    # run the function before every stage start
    H_ext = sim.get_subfield_average_siv('H_ext')
    print('Set new H_ext of stage %d with H_ext %s' %(sim.stage, str(H_ext)))
    def set_H(position):
        x = position[0]
        y = position[1]
        z = position[2]
        return H_ext
    sim.set_H_ext(set_H, unit=SI(1,'A/m'))

# start time
start_time = time.time()
# start hysteresis
sim.hysteresis(Hs, save=[(my_save, at('convergence'))],
               do=[(my_stage, every('stage', 1) & at('stage_step', 0))])
# print simulate time
use_time = (time.time() - start_time) / 60
print('Use Time %f min' % use_time)
Example #33
0
import nmag
from nmag import SI, every, at, si

#create simulation object and switch off
#the computation of the demagnetising field
sim = nmag.Simulation(do_demag=False)

# define magnetic material so that Js = mu0*Ms = 1 T
Py = nmag.MagMaterial(name="Py",
                      Ms=1.0 * si.Tesla / si.mu0,
                      exchange_coupling=SI(13.0e-12, "J/m"),
                      llg_damping=SI(0.0))
# load mesh
sim.load_mesh("sphere1.nmesh.h5", [("sphere", Py)], unit_length=SI(1e-9, "m"))

# set initial magnetisation
sim.set_m([1, 1, 1])

# set external field
Hs = nmag.vector_set(direction=[0., 0., 1.],
                     norm_list=[1.0],
                     units=1e6 * SI('A/m'))

ps = SI(1e-12, "s")  # ps corresponds to one picosecond

# let the magnetisation precess around the direction of the applied field
sim.hysteresis(Hs,
               save=[('averages', every('time', 0.1 * ps))],
               do=[('exit', at('time', 300 * ps))])
Example #34
0
y_lattice = 10.01  # between repeated copies of the simualtion cell
z_lattice = 0.0

# list to store the lattice points where the periodic
# copies of the simulation cell will be placed
lattice_points = []

for xi in range(-4, 6):
    for yi in range(-19, 21):
        lattice_points.append(
            [xi * x_lattice, yi * y_lattice, 0.0 * z_lattice])

# create data structure pbc for this macro geometry
pbc = nmag.SetLatticePoints(vectorlist=lattice_points,
                            scalefactor=SI(1e-9, 'm'))

#create simulation object, passing macro geometry data structure
sim = nmag.Simulation(periodic_bc=pbc.structure)

# load mesh
sim.load_mesh("prism.nmesh.h5", [("repeated-prism-2D", Py)],
              unit_length=SI(1e-9, "m"))

# set initial magnetisation
sim.set_m([1., 1., 1.])

# loop over the applied field
s = SI(1, "s")
sim.relax(save=[('averages', 'fields',
                 every('time', 5e-12 * s) | at('convergence'))])
Example #35
0
import nmag
from nmag import SI, every

# This basic simulation gives us an idea about the relaxation properties
# of the system.

# We find that it takes about 200 frames to do one full oscillation,
# which corresponds to a frequency of about f=1 GHz.

#create simulation object
sim = nmag.Simulation()

# define magnetic material
Py = nmag.MagMaterial(name='Py',
                      Ms=SI(1e6, 'A/m'),
                      llg_damping=0.02,
                      exchange_coupling=SI(13.0e-12, 'J/m'))

# load mesh
sim.load_mesh('rod.nmesh.h5', [('rod', Py)], unit_length=SI(1e-9, 'm'))

# set initial magnetisation
sim.set_m([1, 0, 0.1])

# set external field
sim.set_H_ext([0, 0, 0], SI('A/m'))

sim.relax(save=[('fields', every('time', SI(5e-12, "s")))])
Example #36
0
# Do the same for the bias field
sim.set_H_ext(bias_H)
H_bias_data = sim.get_subfield("H_ext")

# Now the applied field at time t will be expressed as
#   H_bias_data + f(t)*H_pulse_data,
# where f(t) is the time dependence of the pulse
def update_H_ext(t_su):
    ut = float(sinc_omega*(t_su*ps - sinc_t0))
    t_amp = math.sin(ut)/ut if abs(ut) > 1e-10 else 1.0

    if abs(t_amp) > 1e-4:
        H_value = H_bias_data + t_amp*H_pulse_data
    else:
        H_value = map(lambda x: float(x/SI("A/m")), bias_H)

    fieldname = 'H_ext'
    sim._fields.set_subfield(None, H_value, SI("A/m"),
                             fieldname=fieldname, auto_normalise=False)
    (mwe, field) = sim._master_mwes_and_fields_by_name[fieldname]
    ocaml.lam_set_field(sim._lam, field, "v_" + fieldname)
    print "*",

sim.pre_rhs_funs.append(update_H_ext)

sim.set_params(stopping_dm_dt=0) # Never stop for convergence
sim.relax(save=[#('averages', every('time', 2.5*ps)),
                ('field_m', every('time', 1*ps))],
          do=[('exit', at('stage_time', 5000*ps))])

Example #37
0
import nmag
from nmag import SI, every

# This basic simulation gives us an idea about the relaxation properties
# of the system.

# We find that it takes about 200 frames to do one full oscillation,
# which corresponds to a frequency of about f=1 GHz.

#create simulation object
sim = nmag.Simulation()

# define magnetic material
Py = nmag.MagMaterial(name = 'Py',
                      Ms = SI(1e6, 'A/m'),
                      llg_damping=0.02,
                      exchange_coupling = SI(13.0e-12, 'J/m'))

# load mesh
sim.load_mesh('rod.nmesh.h5',
              [('rod', Py)],
              unit_length = SI(1e-9, 'm'))

# set initial magnetisation
sim.set_m([1,0,0.1])

# set external field
sim.set_H_ext([0,0,0], SI('A/m'))

sim.relax(save =[('fields', every('time', SI(5e-12,"s")))])
Example #38
0
s.load_mesh(mesh_filename, [('bar', permalloy)], unit_length=1*nm)

def set_disturbance(is_on):
  c = float(nm/SI('m'))
  def setter(sim):
    def H_ext(r):
      x, y, z = [xi/c - x0i 
                 for xi, x0i in zip(r, disturb_origin)]
                # x, y and z are (floats) in nanometers
      d = math.sqrt(x*x + y*y + z*z)
      if is_on and d < disturb_radius:
        return disturb_direction
      else:
        return [0.0, 0.0, 0.0]
    sim.set_H_ext(H_ext, unit=disturb_amplitude)

  return setter

if not relaxed:
  s.set_m([1, 0, 0])
  s.relax()
  s.save_restart_file(m0_filename)

else:
  s.load_m_from_h5file(m0_filename)
  s.relax(save=[('field_m', every('time', 0.5*ps))],
          do=[(set_disturbance(True), at('time', 0*ps)),
              (set_disturbance(False), at('time', disturb_duration)),
              ('exit', at('time', 1000*ps))])

Example #39
0
sim.load_mesh("bar30_30_100.nmesh.h5",
              [("Py", mat_Py)],
              unit_length=SI(1e-9,"m"))

sim.set_m([1,0,1])


#Need to call advance time to create timestepper

ps = SI(1e-12, "s") 

sim.advance_time(0*ps)





sim.get_timestepper().set_params(rel_tolerance=1e-6,abs_tolerance=1e-6)

starttime = time.time()

sim.relax(save = [('averages', every('time', 5*ps)),
                  ('fields', at('convergence'))])


stoptime = time.time()

print "Time spent is %5.2f seconds" % (stoptime-starttime)


Example #40
0
sim.load_mesh(meshfile, [("Py", mat_Py)],unit_length=SI(1e-9,"m"))

import math
angle_deg = 45
angle_rad = angle_deg/360.*2*math.pi
sim.set_m([math.cos(angle_rad), 0, math.sin(angle_rad)])
sim.set_params(ts_rel_tol=1e-06 , ts_abs_tol=1.0e-06,stopping_dm_dt = 10*si.degrees_per_ns)

ps = SI(1e-12, "s") 


time_initialising += time.time()
time_loop = -time.time()

sim.relax(save = [('averages', every('time', 25*ps)|at('convergence')),
                  ('fields',at('convergence'))])

time_loop += time.time()

time_simulating = time_loop - nmag.timing['save_data']
print "Setup took %g seconds" % time_initialising
print "Simulation loop took %g seconds" % time_loop
print "Writing data took: %g seconds" % nmag.timing['save_data']
print "Timestepper took: %g seconds" % time_simulating
print "Total time: %g seconds" % time_total

def out(line, header=False, file="bigbar_timings.log"):
    import os
    if header and os.path.exists(file): return
    f = open(file, "a")