Skip to content

Commit 8dcc0cd

Browse files
committed
Merge in OpenMP fixes, etc
2 parents 92a18dd + fa32f5f commit 8dcc0cd

File tree

14 files changed

+311
-57
lines changed

14 files changed

+311
-57
lines changed

examples/mc/skx.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def run(mesh):
1414
mc = MonteCarlo(mesh, name='test1')
1515
mc.set_m(random_m)
1616
J = 50*const.k_B
17-
mc.set_options(H=[0,0,0.0], J=J, D=0.27*J, T=4.0)
17+
mc.set_options(H=[0,0,1.0], J=J, D=0.27*J, T=4.0)
1818
mc.run(steps=100000, save_m_steps=None, save_vtk_steps=50000, save_data_steps=50000)
1919

2020
if __name__=='__main__':

examples/micromagnetic/PRB_88_184422/fidimag/prb.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def test_prb88_184422():
3232

3333
sim.set_m((0, 0, 1))
3434

35-
sim.add(UniformExchange(A))
35+
sim.add(UniformExchange(A=A))
3636
sim.add(DMI(-D, type='interfacial'))
3737
sim.add(UniaxialAnisotropy(K, axis=(0, 0, 1)))
3838

examples/micromagnetic/dw_stt/main.py

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
import matplotlib.pyplot as plt
44

55
import numpy as np
6-
from micro import Sim
7-
from common import CuboidMesh
6+
from fidimag.micro import Sim
7+
from fidimag.common import CuboidMesh
88

99
# The energies, we can use DMI in a future simulation
10-
from micro import UniformExchange
11-
from micro import UniaxialAnisotropy
10+
from fidimag.micro import UniformExchange
11+
from fidimag.micro import UniaxialAnisotropy
1212
# from micro import DMI
1313

1414
mu0 = 4 * np.pi * 1e-7
@@ -21,11 +21,11 @@ def init_m(pos):
2121
x = pos[0]
2222

2323
if x < 400:
24-
return (1, 0, 0)
24+
return (-1, 0, 0)
2525
elif 400 <= x < 500:
2626
return (0, 1, 1)
2727
else:
28-
return (-1, 0, 0)
28+
return (1, 0, 0)
2929

3030

3131
def relax_system(mesh):
@@ -38,7 +38,7 @@ def relax_system(mesh):
3838
sim.driver.alpha = 0.5
3939
sim.driver.gamma = 2.211e5
4040
sim.Ms = 8.6e5
41-
sim.do_precession = False
41+
sim.driver.do_precession = False
4242

4343
# The initial state passed as a function
4444
sim.set_m(init_m)
@@ -59,8 +59,8 @@ def relax_system(mesh):
5959
# ONE_DEGREE_PER_NS = 17453292.52
6060

6161
# Start relaxation and save the state in m0.npy
62-
sim.relax(dt=1e-14, stopping_dmdt=0.00001, max_steps=5000,
63-
save_m_steps=None, save_vtk_steps=None)
62+
sim.relax(dt=1e-14, stopping_dmdt=0.01, max_steps=5000,
63+
save_m_steps=None, save_vtk_steps=50)
6464

6565
np.save('m0.npy', sim.spin)
6666

@@ -106,8 +106,7 @@ def deal_plot(_list, output_file):
106106

107107

108108
# THIS NEEDS REVISION
109-
def deal_plot_dynamics(spin, m_component='mz',
110-
output_file):
109+
def deal_plot_dynamics(spin, m_component='mz', output_file='output.pdf'):
111110
"""
112111
The dynamics of the m_component of the i-th spin
113112
of the chain, from the magnetisation snapshot
@@ -140,10 +139,10 @@ def deal_plot_dynamics(spin, m_component='mz',
140139
def excite_system(mesh):
141140

142141
# Specify the stt dynamics in the simulation
143-
sim = Sim(mesh, name='dyn', driver='llg_stt')
142+
sim = Sim(mesh, name='dyn2', driver='llg_stt')
144143

145144
sim.driver.set_tols(rtol=1e-12, atol=1e-14)
146-
sim.driver.alpha = 0.05
145+
sim.driver.alpha = 0.2
147146
sim.driver.gamma = 2.211e5
148147
sim.Ms = 8.6e5
149148

@@ -163,16 +162,16 @@ def excite_system(mesh):
163162

164163
# Set the current in the x direction, in A / m
165164
# beta is the parameter in the STT torque
166-
sim.jx = -1e12
167-
sim.beta = 1
165+
sim.driver.jx = -1e12
166+
sim.driver.beta = 0.01
168167

169168
# The simulation will run for 5 ns and save
170169
# 500 snapshots of the system in the process
171170
ts = np.linspace(0, 5e-9, 501)
172171

173172
for t in ts:
174173
print 'time', t
175-
sim.run_until(t)
174+
sim.driver.run_until(t)
176175
sim.save_vtk()
177176
sim.save_m()
178177

examples/micromagnetic/vortex/main.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
import matplotlib.pyplot as plt
44

55
import numpy as np
6-
from micro import Sim
7-
from common import CuboidMesh
8-
from micro import UniformExchange, Demag
9-
from micro import Zeeman, TimeZeeman
6+
from fidimag.micro import Sim
7+
from fidimag.common import CuboidMesh
8+
from fidimag.micro import UniformExchange, Demag
9+
from fidimag.micro import Zeeman, TimeZeeman
1010
from fidimag.common.fileio import DataReader
1111

1212
mu0 = 4 * np.pi * 1e-7
@@ -46,7 +46,7 @@ def relax_system(mesh):
4646
sim.driver.alpha = 0.5
4747
sim.driver.gamma = 2.211e5
4848
sim.Ms = spatial_Ms
49-
sim.do_precession = False
49+
sim.driver.do_precession = False
5050

5151
sim.set_m(init_m)
5252
# sim.set_m(np.load('m0.npy'))
@@ -55,7 +55,7 @@ def relax_system(mesh):
5555
exch = UniformExchange(A=A)
5656
sim.add(exch)
5757

58-
demag = Demag()
58+
demag = Demag(pbc_2d=True)
5959
sim.add(demag)
6060

6161
mT = 795.7747154594767
@@ -84,7 +84,7 @@ def excite_system(mesh):
8484
exch = UniformExchange(A=A)
8585
sim.add(exch)
8686

87-
demag = Demag()
87+
demag = Demag(pbc_2d=True)
8888
sim.add(demag)
8989

9090
mT = 795.7747154594767
@@ -110,9 +110,9 @@ def gaussian_fun(t):
110110

111111
mesh = CuboidMesh(nx=80, ny=80, nz=2, dx=2.5, dy=2.5, dz=5.0, unit_length=1e-9)
112112

113-
# relax_system(mesh)
113+
relax_system(mesh)
114114

115-
excite_system(mesh)
115+
#excite_system(mesh)
116116

117117
# apply_field1(mesh)
118118
# deal_plot()

fidimag/atomistic/lib/mc.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,8 @@ void run_step_mc(mt19937_state *state, double *spin, double *new_spin,
165165
delta_E += compute_deltaE_exchange_DMI_hexagonal(
166166
&spin[0], &new_spin[0], &ngbs[0], n_ngbs, J, D, i, new_i);
167167
} else {
168-
delta_E += compute_deltaE_exchange_DMI(&spin[0], &new_spin[0], &ngbs[0], n_ngbs,
169-
&nngbs[0], J, J1, D, D1, i, new_i);
168+
delta_E += compute_deltaE_exchange_DMI(&spin[0], &new_spin[0], &ngbs[0],
169+
&nngbs[0], n_ngbs, J, J1, D, D1, i, new_i);
170170
}
171171

172172
update = 0;

fidimag/common/dipolar/demag.c

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#include "demagcoef.h"
66

77

8-
double Nxxdipole(double x, double y, double z) {
8+
inline double Nxxdipole(double x, double y, double z) {
99
double x2 = x * x;
1010
double y2 = y * y;
1111
double z2 = z * z;
@@ -16,7 +16,7 @@ double Nxxdipole(double x, double y, double z) {
1616
return -(2 * x2 - y2 - z2) / (R * R * r);
1717
}
1818

19-
double Nxydipole(double x, double y, double z) {
19+
inline double Nxydipole(double x, double y, double z) {
2020
double R = x * x + y * y + z * z;
2121
if (R == 0)
2222
return 0.0;
@@ -54,9 +54,16 @@ void compute_dipolar_tensors(fft_demag_plan *plan) {
5454
int lenx = plan->lenx;
5555
int leny = plan->leny;
5656
int lenz = plan->lenz;
57-
int lenxy = lenx * leny;
58-
59-
57+
int lenxy = lenx * leny;
58+
// Parallelising this like this
59+
// means that z should be the largest index
60+
// in order to get better performance from threading.
61+
// The data writing is not very clever here; we're
62+
// going to be invalidating the cache a lot. It would be better
63+
// maybe to split this into six seperate loops for each component
64+
// of the tensor in order that each thread is working on a smaller
65+
// memory address range
66+
#pragma omp parallel for private(j, i, x, y, z, id) schedule(dynamic, 32)
6067
for (k = 0; k < lenz; k++) {
6168
for (j = 0; j < leny; j++) {
6269
for (i = 0; i < lenx; i++) {
@@ -81,7 +88,7 @@ void compute_dipolar_tensors(fft_demag_plan *plan) {
8188
void compute_demag_tensors(fft_demag_plan *plan) {
8289

8390
int i, j, k, id;
84-
double x, y, z;
91+
double x, y, z, radius_sq;
8592

8693
int nx = plan->nx;
8794
int ny = plan->ny;
@@ -97,7 +104,7 @@ void compute_demag_tensors(fft_demag_plan *plan) {
97104

98105
double length = pow(dx*dy*dz, 1/3.0);
99106
double asymptotic_radius_sq = pow(26.0*length,2.0);
100-
107+
#pragma omp parallel for private(j, i, id, x, y, z, radius_sq) schedule(dynamic, 32)
101108
for (k = 0; k < lenz; k++) {
102109
for (j = 0; j < leny; j++) {
103110
for (i = 0; i < lenx; i++) {
@@ -107,7 +114,7 @@ void compute_demag_tensors(fft_demag_plan *plan) {
107114
y = (j - ny + 1) * dy;
108115
z = (k - nz + 1) * dz;
109116

110-
double radius_sq = x*x+y*y+z*z;
117+
radius_sq = x*x+y*y+z*z;
111118

112119
if (radius_sq>asymptotic_radius_sq){
113120
//printf("%g %g %g %g %g %g\n",x,y,z,dx,dy,dz);
@@ -338,7 +345,7 @@ void compute_fields(fft_demag_plan *restrict plan, double *restrict spin, double
338345
//print_r("hz", plan->hz, plan->total_length);
339346

340347
double scale = -1.0 / plan->total_length;
341-
348+
#pragma omp parallel for private(j, i, id1, id2) schedule(dynamic, 32)
342349
for (k = 0; k < nz; k++) {
343350
for (j = 0; j < ny; j++) {
344351
for (i = 0; i < nx; i++) {

fidimag/micro/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
from .sim import Sim
22
from .demag import Demag
33
from .exchange import UniformExchange
4+
from .exchange_rkky import ExchangeRKKY
45
from .zeeman import Zeeman, TimeZeeman
56
from .anisotropy import UniaxialAnisotropy
67
from .dmi import DMI
78
from .baryakhtar import LLBar, LLBarFull
89
from .simple_demag import SimpleDemag
10+

fidimag/micro/dmi.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ class DMI(Energy):
5252
5353
D :: DMI vector norm which can be specified as an int, float, (X * n)
5454
array (X=6 for bulk DMI and X=4 for interfacial DMI), (n) array
55-
or spatially dependent scalar field function.
55+
or spatially dependent scalar field function. The units are
56+
Joules / ( meter **2 ).
5657
5758
int, float: D will have the same magnitude for every NN of the
5859
spins at every mesh node, given by this magnitude

fidimag/micro/exchange.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,15 @@
66
class UniformExchange(Energy):
77

88
"""
9+
UniformExchange(A, name='UniformExchange')
10+
911
Compute the exchange field in micromagnetics.
12+
13+
Inputs:
14+
A: float
15+
A is the exchange stiffness constant measured in
16+
Joules / Meter (J / M)
17+
1018
"""
1119

1220
def __init__(self, A, name='UniformExchange'):

fidimag/micro/exchange_rkky.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import fidimag.extensions.micro_clib as micro_clib
2+
from .energy import Energy
3+
#from constant import mu_0
4+
5+
6+
class ExchangeRKKY(Energy):
7+
8+
"""
9+
RKKYExchange(sigma, Delta, z_bottom=0, z_top = -1, name='RKKYExchange')
10+
11+
Compute the RKKY-style exchange interaction defined by
12+
13+
E = sigma/Delta *(1-m_t * m_b)
14+
15+
where E is the energy density, sigma is the surface exchange coefficient between the two surfaces,
16+
Delata is the space thickness. m_t and m_b are the unit vectors of the top and bottom layers, respectively.
17+
18+
Inputs:
19+
sigma: float
20+
sigma is the surface exchange stiffness constant.
21+
Delta: float
22+
Delta is the the space thickness in Meter.
23+
z_bottom: int
24+
z_bottom is the index of the bottom layer
25+
z_top: int
26+
z_top is the index of the top layer.
27+
28+
"""
29+
30+
def __init__(self, sigma, Delta=1e-9, z_bottom=0, z_top = -1, name='RKKYExchange'):
31+
self.sigma = sigma/Delta
32+
self.name = name
33+
self.z_bottom = z_bottom
34+
self.z_top = z_top
35+
self.jac = True
36+
37+
def setup(self, mesh, spin, Ms):
38+
super(ExchangeRKKY, self).setup(mesh, spin, Ms)
39+
if self.z_top<0:
40+
self.z_top+= self.nz
41+
42+
43+
def compute_field(self, t=0, spin=None):
44+
if spin is not None:
45+
m = spin
46+
else:
47+
m = self.spin
48+
49+
micro_clib.compute_exchange_field_micro_rkky(m,
50+
self.field,
51+
self.energy,
52+
self.Ms_inv,
53+
self.sigma,
54+
self.nx,
55+
self.ny,
56+
self.nz,
57+
self.z_bottom,
58+
self.z_top
59+
)
60+
61+
return self.field

0 commit comments

Comments
 (0)