def test_exch_field_oommf(A=1e-11, Ms=2.6e5): """ Compare the exchange field from Fidimag with an equivalent OOMMF simulation. OOMMF field data is taken from an OVF file. """ mesh = CuboidMesh(nx=10, ny=3, nz=2, dx=0.5, unit_length=1e-9) sim = Sim(mesh) sim.Ms = Ms exch = UniformExchange(A=A) sim.add(exch) def init_m(pos): x, y, z = pos return (np.sin(x) + y + 2.3 * z, np.cos(x) + y + 1.3 * z, 0) sim.set_m(init_m) field = exch.compute_field() # An equivalent initial magnetisation for OOMMF # The spatial variables are rescale since they are in nm init_m0 = (r'return [list [expr {sin($x * 1e9) + $y * 1e9 + $z * 2.3e9}] ' + r' [expr {cos($x * 1e9) + $y * 1e9 + $z * 1.3e9}] ' + r'0 ' + r'] ') field_oommf = compute_exch_field(mesh, Ms=Ms, init_m0=init_m0, A=A) mx0, mx1, mx2 = compare_fields(field_oommf, field) # Test if the maximum relative errors between both simulations # is small enough, for every field component assert max([mx0, mx1, mx2]) < 1e-12
def test_exch_field_oommf(A=1e-11, Ms=2.6e5): mesh = CuboidMesh(nx=10, ny=3, nz=2, dx=0.5, unit_length=1e-9) sim = Sim(mesh) sim.Ms = Ms exch = UniformExchange(A=A) sim.add(exch) def init_m(pos): x, y, z = pos return (np.sin(x) + y + 2.3 * z, np.cos(x) + y + 1.3 * z, 0) sim.set_m(init_m) field = exch.compute_field() init_m0 = """ return [list [expr {sin($x*1e9)+$y*1e9+$z*2.3e9}] [expr {cos($x*1e9)+$y*1e9+$z*1.3e9}] 0] """ omf_file = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'omfs', 'test_exch_field_oommf.ohf' ) ovf = OMF2(omf_file) field_oommf = ovf.get_all_mags() #field_oommf = compute_exch_field(mesh, Ms=Ms, init_m0=init_m0, A=A) mx0, mx1, mx2 = compare_fields(field_oommf, field) assert max([mx0, mx1, mx2]) < 1e-12
def test_exch_field_oommf(A=1e-11, Ms=2.6e5): """ Compare the exchange field from Fidimag with an equivalent OOMMF simulation. OOMMF field data is taken from an OVF file. """ mesh = CuboidMesh(nx=10, ny=3, nz=2, dx=0.5, unit_length=1e-9) sim = Sim(mesh) sim.Ms = Ms exch = UniformExchange(A=A) sim.add(exch) def init_m(pos): x, y, z = pos return (np.sin(x) + y + 2.3 * z, np.cos(x) + y + 1.3 * z, 0) sim.set_m(init_m) field = exch.compute_field() # An equivalent initial magnetisation for OOMMF # The spatial variables are rescale since they are in nm init_m0 = ( r'return [list [expr {sin($x * 1e9) + $y * 1e9 + $z * 2.3e9}] ' + r' [expr {cos($x * 1e9) + $y * 1e9 + $z * 1.3e9}] ' + r'0 ' + r'] ') field_oommf = compute_exch_field(mesh, Ms=Ms, init_m0=init_m0, A=A) mx0, mx1, mx2 = compare_fields(field_oommf, field) # Test if the maximum relative errors between both simulations # is small enough, for every field component assert max([mx0, mx1, mx2]) < 1e-12
def test_exch_field_oommf(A=1e-11, Ms=2.6e5): mesh = CuboidMesh(nx=10, ny=3, nz=2, dx=0.5, unit_length=1e-9) sim = Sim(mesh) sim.Ms = Ms exch = UniformExchange(A=A) sim.add(exch) def init_m(pos): x, y, z = pos return (np.sin(x) + y + 2.3 * z, np.cos(x) + y + 1.3 * z, 0) sim.set_m(init_m) field = exch.compute_field() init_m0 = """ return [list [expr {sin($x*1e9)+$y*1e9+$z*2.3e9}] [expr {cos($x*1e9)+$y*1e9+$z*1.3e9}] 0] """ omf_file = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'omfs', 'test_exch_field_oommf.ohf' ) ovf = OMF2(omf_file) field_oommf = ovf.get_all_mags() #field_oommf = compute_exch_field(mesh, Ms=Ms, init_m0=init_m0, A=A) mx0, mx1, mx2 = compare_fields(field_oommf, field) assert max([mx0, mx1, mx2]) < 1e-12
def relax_system(mesh): sim = Sim(mesh, name='relax') sim.driver.set_tols(rtol=1e-10, atol=1e-10) sim.driver.alpha = 0.1 sim.driver.gamma = 2.211e5 sim.Ms = spatial_Ms print(sim.Ms) sim.set_m(init_m) A = 1.3e-11 exch = UniformExchange(A=A) sim.add(exch) demag = Demag() sim.add(demag) dmi = DMI(D=4e-3) sim.add(dmi) dmi2 = DMI(D=2e-3, dmi_type="interfacial") sim.add(dmi2) anis = UniaxialAnisotropy(-3e4, axis=(0, 0, 1)) sim.add(anis) sim.relax(dt=1e-13, stopping_dmdt=5e4, max_steps=5000, save_m_steps=100, save_vtk_steps=50) #np.save('m0.npy', sim.spin) fd = demag.compute_field(sim.spin) fe = exch.compute_field(sim.spin) fdmi = dmi.compute_field(sim.spin) fdmi2 = dmi2.compute_field(sim.spin) fanis = anis.compute_field(sim.spin) np.savetxt( "test_fields.txt", np.transpose([ np.concatenate((sim.Ms, sim.Ms, sim.Ms, [0.0])), np.concatenate((sim.spin, [100])), np.concatenate((fd, [demag.compute_energy()])), np.concatenate((fe, [exch.compute_energy()])), np.concatenate((fdmi, [dmi.compute_energy()])), np.concatenate((fdmi2, [dmi2.compute_energy()])), np.concatenate((fanis, [anis.compute_energy()])) ]), header= "Generated by Fidimag. Size=20x5x3, 2.5nm x 2.5nm x 3nm, Ms=8.0e5A/m, A=1.3e-11 J/m," + " D=4e-3 J/m^2, D_int=2e-3 J/m^2, Ku=-3e4 J/m^3 axis=(0,0,1).\n Ms " + "".ljust(20) + " m0 " + "".ljust(20) + "demag" + "".ljust(20) + "exch" + "".ljust(22) + "dmi" + "".ljust(22) + "dmi_interfacial" + "".ljust(22) + "anis")
def test_with_oommf_spatial_Ms(A=1e-11): def spatial_Ms(pos): x, y = pos[0], pos[1] if x**2 + y**2 < 5**2: return 2e4 else: return 0 init_m0 = ( r'return [list [expr {sin($x * 1e9) + $y * 1e9 + $z * 2.3e9}] ' + r' [expr {cos($x * 1e9) + $y * 1e9 + $z * 1.3e9}] ' + r'0 ' + r'] ') init_Ms = """ if { ($x * $x + $y * $y) < 5e-9 * 5e-9 } { return 2e4 } else { return 0 } """ mesh = CuboidMesh(nx=12, ny=10, nz=2, dx=0.5, unit_length=1e-9) sim = Sim(mesh) sim.Ms = spatial_Ms def init_m(pos): x, y, z = pos return (np.sin(x) + y + 2.3 * z, np.cos(x) + y + 1.3 * z, 0) sim.set_m(init_m) exch = UniformExchange(A=A) sim.add(exch) demag = Demag() sim.add(demag) field = exch.compute_field() field_oommf = compute_exch_field(mesh, init_m0=init_m0, A=A, spatial_Ms=init_Ms) mx0, mx1, mx2 = compare_fields(field_oommf, field) assert max([mx0, mx1, mx2]) < 1e-12 field = demag.compute_field() field_oommf = compute_demag_field(mesh, spatial_Ms=init_Ms, init_m0=init_m0) mx0, mx1, mx2 = compare_fields(field_oommf, field) assert max([mx0, mx1, mx2]) < 1e-11
def test_with_oommf_spatial_Ms(A=1e-11): def spatial_Ms(pos): x, y = pos[0], pos[1] if x ** 2 + y ** 2 < 5 ** 2: return 2e4 else: return 0 init_m0 = (r'return [list [expr {sin($x * 1e9) + $y * 1e9 + $z * 2.3e9}] ' + r' [expr {cos($x * 1e9) + $y * 1e9 + $z * 1.3e9}] ' + r'0 ' + r'] ') init_Ms = """ if { ($x * $x + $y * $y) < 5e-9 * 5e-9 } { return 2e4 } else { return 0 } """ mesh = CuboidMesh(nx=12, ny=10, nz=2, dx=0.5, unit_length=1e-9) sim = Sim(mesh) sim.Ms = spatial_Ms def init_m(pos): x, y, z = pos return (np.sin(x) + y + 2.3 * z, np.cos(x) + y + 1.3 * z, 0) sim.set_m(init_m) exch = UniformExchange(A=A) sim.add(exch) demag = Demag() sim.add(demag) field = exch.compute_field() field_oommf = compute_exch_field( mesh, init_m0=init_m0, A=A, spatial_Ms=init_Ms) mx0, mx1, mx2 = compare_fields(field_oommf, field) assert max([mx0, mx1, mx2]) < 1e-12 field = demag.compute_field() field_oommf = compute_demag_field( mesh, spatial_Ms=init_Ms, init_m0=init_m0) mx0, mx1, mx2 = compare_fields(field_oommf, field) assert max([mx0, mx1, mx2]) < 1e-11
def test_exch_1d(do_plot=False): # Initiate the 1D mesh and magnetisation as before mesh = CuboidMesh(nx=100, ny=1, nz=1) sim = Sim(mesh) sim.set_m(init_m) # Simplify the magnetic parameters mu0 = 4 * np.pi * 1e-7 sim.Ms = 1.0 / mu0 exch = UniformExchange(1) sim.add(exch) # Compute the exchange field and reshape it in order # to leave every row as the [f_x, f_y, f_z] array # for every spin field = exch.compute_field() field.shape = (-1, 3) # We know that the field in x is always zero ( see the # analytical calculation at the beginning) assert max(abs(field[:, 0])) == 0 # These are the analytical values for the exchange field in y,z # In this case, k=0.1 , then 2 * k^2 evaluates as 0.02 xs = np.linspace(0, 99, 100) epy = -0.02 * np.sin(0.1 * xs) epz = -0.02 * np.cos(0.1 * xs) # Compare the analytical value # of the y component of the exchange field, with Fidimag's # result (second column of the reshaped field array) # WARNING: NOTICE that we are not considering the extremes since # there is a wrong expression in the border of the exchange field # with NO PBCs. We must FIX this test! assert max(abs(epy[1:-1] - field[1:-1, 1])) < 3e-5 if do_plot: plt.plot(xs, field[:, 1], "-.", label="my", color='DarkGreen') plt.plot(xs, field[:, 2], "-.", label="mz", color='DarkGreen') plt.plot(xs, epy, "--", label="analytical", color='b') plt.plot(xs, epz, "--", color='r') plt.xlabel("xs") plt.ylabel("field") plt.legend() plt.savefig("exchange_field.pdf")
def test_demag_field_oommf_large(Ms=8e5, A=1.3e-11): mesh = CuboidMesh(nx=150, ny=50, nz=1, dx=2.5, dy=2.5, dz=3, unit_length=1e-9) sim = Sim(mesh) sim.Ms = Ms exch = UniformExchange(A=A) sim.add(exch) demag = Demag() sim.add(demag) def init_m(pos): x, y, z = pos return (np.sin(x) + y + 2.3 * z, np.cos(x) + y + 1.3 * z, 0) sim.set_m(init_m) demag_field = demag.compute_field() exch_field = exch.compute_field() #exact = demag.compute_exact() init_m0 = """ return [list [expr {sin($x*1e9)+$y*1e9+$z*2.3e9}] [expr {cos($x*1e9)+$y*1e9+$z*1.3e9}] 0] """ #demag_oommf = compute_demag_field(mesh, Ms=Ms, init_m0=init_m0) #exch_oommf = compute_exch_field(mesh, Ms=Ms, init_m0=init_m0, A=A) omf_file = os.path.join(os.path.dirname(os.path.abspath(__file__)),'omfs','test_demag_field_oommf_large_Demag.ohf') ovf = OMF2(omf_file) demag_oommf = ovf.get_all_mags() omf_file = os.path.join(os.path.dirname(os.path.abspath(__file__)),'omfs','test_demag_field_oommf_large_Exchange.ohf') ovf = OMF2(omf_file) exch_oommf = ovf.get_all_mags() mx0, mx1, mx2 = compare_fields(demag_oommf, demag_field) #print mx0, mx1, mx2 assert max([mx0,mx1,mx2])< 5e-10 mx0, mx1, mx2 = compare_fields(exch_oommf, exch_field) #print mx0, mx1, mx2 assert max([mx0, mx1, mx2]) < 1e-11
def test_demag_field_oommf_large(Ms=8e5, A=1.3e-11): mesh = CuboidMesh(nx=150, ny=50, nz=1, dx=2.5, dy=2.5, dz=3, unit_length=1e-9) sim = Sim(mesh) sim.Ms = Ms exch = UniformExchange(A=A) sim.add(exch) demag = Demag() sim.add(demag) def init_m(pos): x, y, z = pos return (np.sin(x) + y + 2.3 * z, np.cos(x) + y + 1.3 * z, 0) sim.set_m(init_m) demag_field = demag.compute_field() exch_field = exch.compute_field() #exact = demag.compute_exact() init_m0 = """ return [list [expr {sin($x*1e9)+$y*1e9+$z*2.3e9}] [expr {cos($x*1e9)+$y*1e9+$z*1.3e9}] 0] """ #demag_oommf = compute_demag_field(mesh, Ms=Ms, init_m0=init_m0) #exch_oommf = compute_exch_field(mesh, Ms=Ms, init_m0=init_m0, A=A) omf_file = os.path.join(os.path.dirname(os.path.abspath(__file__)),'omfs','test_demag_field_oommf_large_Demag.ohf') ovf = OMF2(omf_file) demag_oommf = ovf.get_all_mags() omf_file = os.path.join(os.path.dirname(os.path.abspath(__file__)),'omfs','test_demag_field_oommf_large_Exchange.ohf') ovf = OMF2(omf_file) exch_oommf = ovf.get_all_mags() mx0, mx1, mx2 = compare_fields(demag_oommf, demag_field) #print mx0, mx1, mx2 assert max([mx0,mx1,mx2])< 5e-10 mx0, mx1, mx2 = compare_fields(exch_oommf, exch_field) #print mx0, mx1, mx2 assert max([mx0, mx1, mx2]) < 1e-11
def test_demag_field_oommf_large(Ms=8e5, A=1.3e-11): mesh = CuboidMesh(nx=150, ny=50, nz=1, dx=2.5, dy=2.5, dz=3, unit_length=1e-9) sim = Sim(mesh) sim.Ms = Ms exch = UniformExchange(A=A) sim.add(exch) demag = Demag() sim.add(demag) def init_m(pos): x, y, z = pos return (np.sin(x) + y + 2.3 * z, np.cos(x) + y + 1.3 * z, 0) sim.set_m(init_m) demag_field = demag.compute_field() exch_field = exch.compute_field() #exact = demag.compute_exact() init_m0 = ( r'return [list [expr {sin($x * 1e9) + $y * 1e9 + $z * 2.3e9}] ' + r' [expr {cos($x * 1e9) + $y * 1e9 + $z * 1.3e9}] ' + r'0 ' + r'] ') demag_oommf = compute_demag_field(mesh, Ms=Ms, init_m0=init_m0) exch_oommf = compute_exch_field(mesh, Ms=Ms, init_m0=init_m0, A=A) mx0, mx1, mx2 = compare_fields(demag_oommf, demag_field) #print mx0, mx1, mx2 assert max([mx0, mx1, mx2]) < 5e-10 mx0, mx1, mx2 = compare_fields(exch_oommf, exch_field) #print mx0, mx1, mx2 assert max([mx0, mx1, mx2]) < 1e-11
def test_demag_field_oommf_large(Ms=8e5, A=1.3e-11): mesh = CuboidMesh(nx=150, ny=50, nz=1, dx=2.5, dy=2.5, dz=3, unit_length=1e-9) sim = Sim(mesh) sim.Ms = Ms exch = UniformExchange(A=A) sim.add(exch) demag = Demag() sim.add(demag) def init_m(pos): x, y, z = pos return (np.sin(x) + y + 2.3 * z, np.cos(x) + y + 1.3 * z, 0) sim.set_m(init_m) demag_field = demag.compute_field() exch_field = exch.compute_field() #exact = demag.compute_exact() init_m0 = (r'return [list [expr {sin($x * 1e9) + $y * 1e9 + $z * 2.3e9}] ' + r' [expr {cos($x * 1e9) + $y * 1e9 + $z * 1.3e9}] ' + r'0 ' + r'] ') demag_oommf = compute_demag_field(mesh, Ms=Ms, init_m0=init_m0) exch_oommf = compute_exch_field(mesh, Ms=Ms, init_m0=init_m0, A=A) mx0, mx1, mx2 = compare_fields(demag_oommf, demag_field) #print mx0, mx1, mx2 assert max([mx0, mx1, mx2]) < 5e-10 mx0, mx1, mx2 = compare_fields(exch_oommf, exch_field) #print mx0, mx1, mx2 assert max([mx0, mx1, mx2]) < 1e-11
def test_with_oommf_spatial_Ms(A=1e-11): def spatial_Ms(pos): x, y = pos[0], pos[1] if x**2 + y**2 < 5**2: return 2e4 else: return 0 init_m0 = """ return [list [expr {sin($x*1e9)+$y*1e9+$z*2.3e9}] [expr {cos($x*1e9)+$y*1e9+$z*1.3e9}] 0] """ init_Ms = """ if { $x*$x + $y*$y < 5e-9*5e-9 } { return 2e4 } else { return 0 } """ mesh = CuboidMesh(nx=12, ny=10, nz=2, dx=0.5, unit_length=1e-9) sim = Sim(mesh) sim.Ms = spatial_Ms exch = UniformExchange(A=A) sim.add(exch) demag = Demag() sim.add(demag) def init_m(pos): x, y, z = pos return (np.sin(x) + y + 2.3 * z, np.cos(x) + y + 1.3 * z, 0) sim.set_m(init_m) field = exch.compute_field() #field_oommf = compute_exch_field(mesh, init_m0=init_m0, A=A, spatial_Ms=init_Ms) omf_file = os.path.join(os.path.dirname(os.path.abspath(__file__)),'omfs','test_with_oommf_spatial_Ms_Exchange.ohf') ovf = OMF2(omf_file) field_oommf = ovf.get_all_mags() mx0, mx1, mx2 = compare_fields(field_oommf, field) assert max([mx0, mx1, mx2]) < 1e-12 field = demag.compute_field() #field_oommf = compute_demag_field(mesh, spatial_Ms=init_Ms, init_m0=init_m0) omf_file = os.path.join(os.path.dirname(os.path.abspath(__file__)),'omfs','test_with_oommf_spatial_Ms_Demag.ohf') ovf = OMF2(omf_file) field_oommf = ovf.get_all_mags() mx0, mx1, mx2 = compare_fields(field_oommf, field) assert max([mx0, mx1, mx2]) < 1e-11
def test_with_oommf_spatial_Ms(A=1e-11): def spatial_Ms(pos): x, y = pos[0], pos[1] if x**2 + y**2 < 5**2: return 2e4 else: return 0 init_m0 = """ return [list [expr {sin($x*1e9)+$y*1e9+$z*2.3e9}] [expr {cos($x*1e9)+$y*1e9+$z*1.3e9}] 0] """ init_Ms = """ if { $x*$x + $y*$y < 5e-9*5e-9 } { return 2e4 } else { return 0 } """ mesh = CuboidMesh(nx=12, ny=10, nz=2, dx=0.5, unit_length=1e-9) sim = Sim(mesh) sim.Ms = spatial_Ms exch = UniformExchange(A=A) sim.add(exch) demag = Demag() sim.add(demag) def init_m(pos): x, y, z = pos return (np.sin(x) + y + 2.3 * z, np.cos(x) + y + 1.3 * z, 0) sim.set_m(init_m) field = exch.compute_field() #field_oommf = compute_exch_field(mesh, init_m0=init_m0, A=A, spatial_Ms=init_Ms) omf_file = os.path.join(os.path.dirname(os.path.abspath(__file__)),'omfs','test_with_oommf_spatial_Ms_Exchange.ohf') ovf = OMF2(omf_file) field_oommf = ovf.get_all_mags() mx0, mx1, mx2 = compare_fields(field_oommf, field) assert max([mx0, mx1, mx2]) < 1e-12 field = demag.compute_field() #field_oommf = compute_demag_field(mesh, spatial_Ms=init_Ms, init_m0=init_m0) omf_file = os.path.join(os.path.dirname(os.path.abspath(__file__)),'omfs','test_with_oommf_spatial_Ms_Demag.ohf') ovf = OMF2(omf_file) field_oommf = ovf.get_all_mags() mx0, mx1, mx2 = compare_fields(field_oommf, field) assert max([mx0, mx1, mx2]) < 1e-11