Skip to content

Commit b9dc03a

Browse files
author
davidcorteso
committed
Added material dependence to SD method
1 parent ea2ab03 commit b9dc03a

File tree

5 files changed

+117
-108
lines changed

5 files changed

+117
-108
lines changed

fidimag/common/lib/common_clib.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,11 @@ void llg_stt_cpp(double *dm_dt, double *m, double *h, double *p, double *alpha,
6767
// ----------------------------------------------------------------------------
6868
// From steepest_descent.c
6969

70-
void sd_update_spin (double *spin, double *spin_last,
70+
void sd_update_spin (double *spin, double *spin_last, double *magnetisation,
7171
double *mxH, double *mxmxH, double *mxmxH_last, double *tau,
7272
int* pins, int n);
7373

74-
void sd_compute_step (double *spin, double *spin_last, double *field,
74+
void sd_compute_step (double *spin, double *spin_last, double *magnetisation, double *field,
7575
double *mxH, double *mxmxH, double *mxmxH_last, double *tau,
7676
int *pins, int n, int counter, double tmin, double tmax);
7777

fidimag/common/lib/common_clib.pyx

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
import numpy
2-
# cimport numpy as np
3-
# np.import_array()
41

52
# -----------------------------------------------------------------------------
63

@@ -32,11 +29,12 @@ cdef extern from "common_clib.h":
3229
# -------------------------------------------------------------------------
3330
# From steepest_descent.c
3431

35-
void sd_update_spin (double *spin, double *spin_last,
32+
void sd_update_spin (double *spin, double *spin_last, double *magnetisation,
3633
double *mxH, double *mxmxH, double *mxmxH_last, double *tau,
3734
int* pins, int n)
3835

39-
void sd_compute_step (double *spin, double *spin_last, double *field,
36+
void sd_compute_step (double *spin, double *spin_last, double *magnetisation,
37+
double *field,
4038
double *mxH, double *mxmxH, double *mxmxH_last, double *tau,
4139
int *pins, int n, int counter, double tmin, double tmax)
4240

@@ -107,19 +105,21 @@ def compute_llg_stt_cpp(double [:] dm_dt,
107105

108106
def compute_sd_spin(double [:] spin,
109107
double [:] spin_last,
108+
double [:] magnetisation,
110109
double [:] mxH,
111110
double [:] mxmxH,
112111
double [:] mxmxH_last,
113112
double [:] tau,
114113
int [:] pins,
115114
n):
116115

117-
sd_update_spin(&spin[0], &spin_last[0], &mxH[0],
116+
sd_update_spin(&spin[0], &spin_last[0], &magnetisation[0], &mxH[0],
118117
&mxmxH[0], &mxmxH_last[0], &tau[0], &pins[0], n
119118
)
120119

121120
def compute_sd_step(double [:] spin,
122121
double [:] spin_last,
122+
double [:] magnetisation,
123123
double [:] field,
124124
double [:] mxH,
125125
double [:] mxmxH,
@@ -128,7 +128,8 @@ def compute_sd_step(double [:] spin,
128128
int [:] pins,
129129
n, counter, tmin, tmax):
130130

131-
sd_compute_step(&spin[0], &spin_last[0], &field[0], &mxH[0],
131+
sd_compute_step(&spin[0], &spin_last[0], &magnetisation[0],
132+
&field[0], &mxH[0],
132133
&mxmxH[0], &mxmxH_last[0], &tau[0], &pins[0],
133134
n, counter, tmin, tmax
134135
)

fidimag/common/lib/steepest_descent.c

Lines changed: 99 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -1,123 +1,129 @@
11
#include "common_clib.h"
22
#include "math.h"
33

4-
void sd_update_spin (double *spin, double *spin_last,
4+
void sd_update_spin (double *spin, double *spin_last, double *magnetisation,
55
double *mxH, double *mxmxH, double *mxmxH_last, double *tau,
66
int* pins, int n) {
77

88
// Update the field -------------------------------------------------------
99
#pragma omp parallel for
1010
for (int i = 0; i < n; i++) {
1111

12-
int spin_idx;
13-
double mxH_sq;
14-
double factor_plus;
15-
double factor_minus;
16-
double new_spin[3];
12+
if (magnetisation[i] > 0.0) {
1713

18-
spin_idx = 3 * i;
14+
int spin_idx;
15+
double mxH_sq;
16+
double factor_plus;
17+
double factor_minus;
18+
double new_spin[3];
1919

20-
// Copy spin from previous (counter) step
21-
for (int j = 0; j < 3; j++) {
22-
spin_last[spin_idx + j] = spin[spin_idx + j];
23-
}
20+
spin_idx = 3 * i;
2421

25-
mxH_sq = 0;
26-
for (int j = 0; j < 3; j++) {
27-
mxH_sq += mxH[spin_idx + j] * mxH[spin_idx + j];
28-
}
22+
// Copy spin from previous (counter) step
23+
for (int j = 0; j < 3; j++) {
24+
spin_last[spin_idx + j] = spin[spin_idx + j];
25+
}
26+
27+
mxH_sq = 0;
28+
for (int j = 0; j < 3; j++) {
29+
mxH_sq += mxH[spin_idx + j] * mxH[spin_idx + j];
30+
}
2931

30-
factor_plus = 4 + tau[i] * tau[i] * mxH_sq;
31-
factor_minus = 4 - tau[i] * tau[i] * mxH_sq;
32+
factor_plus = 4 + tau[i] * tau[i] * mxH_sq;
33+
factor_minus = 4 - tau[i] * tau[i] * mxH_sq;
3234

33-
for (int j = 0; j < 3; j++) {
34-
new_spin[j] = factor_minus * spin[spin_idx + j]
35-
- 4 * tau[i] * mxmxH[spin_idx + j];
36-
new_spin[j] = new_spin[j] / factor_plus;
35+
for (int j = 0; j < 3; j++) {
36+
new_spin[j] = factor_minus * spin[spin_idx + j]
37+
- 4 * tau[i] * mxmxH[spin_idx + j];
38+
new_spin[j] = new_spin[j] / factor_plus;
3739

38-
spin[spin_idx + j] = new_spin[j];
39-
}
40-
}
40+
spin[spin_idx + j] = new_spin[j];
41+
}
42+
} // close if Ms or mu_s > 0
43+
} // close for
4144
normalise(spin, pins, n);
4245
}
4346

44-
void sd_compute_step (double *spin, double *spin_last, double *field,
47+
void sd_compute_step (double *spin, double *spin_last, double *magnetisation, double *field,
4548
double *mxH, double *mxmxH, double *mxmxH_last, double *tau,
4649
int *pins, int n, int counter, double tmin, double tmax) {
4750

4851
#pragma omp parallel for
4952
for (int i = 0; i < n; i++) {
5053

51-
int spin_idx;
52-
double ds[3], dy[3];
53-
double num, den, res, sign;
54-
55-
spin_idx = 3 * i;
56-
57-
// Copy mxmxH from previous (counter) step
58-
for (int j = 0; j < 3; j++) {
59-
mxmxH_last[spin_idx + j] = mxmxH[spin_idx + j];
60-
}
61-
62-
// Compute the torques
63-
64-
mxH[spin_idx] = cross_x(spin[spin_idx], spin[spin_idx + 1], spin[spin_idx + 2],
65-
field[spin_idx],
66-
field[spin_idx + 1],
67-
field[spin_idx + 2]);
68-
69-
mxH[spin_idx + 1] = cross_y(spin[spin_idx], spin[spin_idx + 1], spin[spin_idx + 2],
70-
field[spin_idx],
71-
field[spin_idx + 1],
72-
field[spin_idx + 2]);
73-
74-
mxH[spin_idx + 2] = cross_z(spin[spin_idx], spin[spin_idx + 1], spin[spin_idx + 2],
75-
field[spin_idx],
76-
field[spin_idx + 1],
77-
field[spin_idx + 2]);
78-
79-
mxmxH[spin_idx] = cross_x(spin[spin_idx], spin[spin_idx + 1], spin[spin_idx + 2],
80-
mxH[spin_idx], mxH[spin_idx + 1], mxH[spin_idx + 2]);
81-
mxmxH[spin_idx + 1] = cross_y(spin[spin_idx], spin[spin_idx + 1], spin[spin_idx + 2],
82-
mxH[spin_idx], mxH[spin_idx + 1], mxH[spin_idx + 2]);
83-
mxmxH[spin_idx + 2] = cross_z(spin[spin_idx], spin[spin_idx + 1], spin[spin_idx + 2],
84-
mxH[spin_idx], mxH[spin_idx + 1], mxH[spin_idx + 2]);
85-
86-
// Compute terms for the calculation of the time step tau
87-
for (int j = 0; j < 3; j++) {
88-
ds[j] = spin[spin_idx + j] - spin_last[spin_idx + j];
89-
dy[j] = mxmxH[spin_idx + j] - mxmxH_last[spin_idx + j];
90-
}
91-
92-
num = 0;
93-
den = 0;
94-
if (counter % 2 == 0) {
54+
if (magnetisation[i] > 0.0) {
55+
int spin_idx;
56+
double ds[3], dy[3];
57+
double num, den, res, sign;
58+
59+
spin_idx = 3 * i;
60+
61+
// Copy mxmxH from previous (counter) step
9562
for (int j = 0; j < 3; j++) {
96-
num += ds[j] * ds[j];
97-
den += ds[j] * dy[j];
63+
mxmxH_last[spin_idx + j] = mxmxH[spin_idx + j];
9864
}
99-
}
100-
else {
65+
66+
// Compute the torques
67+
68+
mxH[spin_idx] = cross_x(spin[spin_idx], spin[spin_idx + 1], spin[spin_idx + 2],
69+
field[spin_idx],
70+
field[spin_idx + 1],
71+
field[spin_idx + 2]);
72+
73+
mxH[spin_idx + 1] = cross_y(spin[spin_idx], spin[spin_idx + 1], spin[spin_idx + 2],
74+
field[spin_idx],
75+
field[spin_idx + 1],
76+
field[spin_idx + 2]);
77+
78+
mxH[spin_idx + 2] = cross_z(spin[spin_idx], spin[spin_idx + 1], spin[spin_idx + 2],
79+
field[spin_idx],
80+
field[spin_idx + 1],
81+
field[spin_idx + 2]);
82+
83+
mxmxH[spin_idx] = cross_x(spin[spin_idx], spin[spin_idx + 1], spin[spin_idx + 2],
84+
mxH[spin_idx], mxH[spin_idx + 1], mxH[spin_idx + 2]);
85+
mxmxH[spin_idx + 1] = cross_y(spin[spin_idx], spin[spin_idx + 1], spin[spin_idx + 2],
86+
mxH[spin_idx], mxH[spin_idx + 1], mxH[spin_idx + 2]);
87+
mxmxH[spin_idx + 2] = cross_z(spin[spin_idx], spin[spin_idx + 1], spin[spin_idx + 2],
88+
mxH[spin_idx], mxH[spin_idx + 1], mxH[spin_idx + 2]);
89+
90+
// Compute terms for the calculation of the time step tau
10191
for (int j = 0; j < 3; j++) {
102-
num += ds[j] * dy[j];
103-
den += dy[j] * dy[j];
92+
ds[j] = spin[spin_idx + j] - spin_last[spin_idx + j];
93+
dy[j] = mxmxH[spin_idx + j] - mxmxH_last[spin_idx + j];
94+
}
95+
96+
num = 0;
97+
den = 0;
98+
if (counter % 2 == 0) {
99+
for (int j = 0; j < 3; j++) {
100+
num += ds[j] * ds[j];
101+
den += ds[j] * dy[j];
102+
}
103+
}
104+
else {
105+
for (int j = 0; j < 3; j++) {
106+
num += ds[j] * dy[j];
107+
den += dy[j] * dy[j];
108+
}
104109
}
105-
}
106-
107-
// Criteria for the evaluation of tau is in line 96 of:
108-
//https://github.com/MicroMagnum/MicroMagnum/blob/minimizer/src/magnum/micromagnetics/micro_magnetics_solver.py
109-
if (den == 0.0) {
110-
res = tmax;
111-
}
112-
else {
113-
res = num / den;
114-
}
115-
116-
sign = (res > 0) ? 1 : ((res < 0) ? -1 : 0);
117-
tau[i] = fmax(fmin(fabs(res), tmax), tmin) * sign;
118-
119-
// In MuMax3 they only define a specific value:
120-
// https://github.com/mumax/3/blob/master/engine/minimizer.go
121-
// tau[i] = res;
122-
}
110+
111+
// Criteria for the evaluation of tau is in line 96 of:
112+
//https://github.com/MicroMagnum/MicroMagnum/blob/minimizer/src/magnum/micromagnetics/micro_magnetics_solver.py
113+
if (den == 0.0) {
114+
res = tmax;
115+
}
116+
else {
117+
res = num / den;
118+
}
119+
120+
sign = (res > 0) ? 1 : ((res < 0) ? -1 : 0);
121+
tau[i] = fmax(fmin(fabs(res), tmax), tmin) * sign;
122+
123+
// In MuMax3 they only define a specific value:
124+
// https://github.com/mumax/3/blob/master/engine/minimizer.go
125+
// tau[i] = res;
126+
127+
} // Close if Ms or mu_s > 0
128+
} // close for
123129
}
File renamed without changes.

fidimag/common/steepest_descent.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
from __future__ import division
22
import numpy as np
33
import fidimag.extensions.common_clib as clib
4-
import fidimag.common.helper as helper
5-
import fidimag.common.constant as const
64

75
from .minimiser_base import MinimiserBase
86

@@ -63,10 +61,12 @@ def __init__(self, mesh, spin,
6361

6462
# Define
6563
super(SteepestDescent, self).__init__(mesh, spin,
66-
magnetisation, magnetisation_inv, field, pins,
67-
interactions,
68-
name,
69-
data_saver)
64+
magnetisation, magnetisation_inv,
65+
field,
66+
pins,
67+
interactions,
68+
name,
69+
data_saver)
7070

7171
# ---------------------------------------------------------------------
7272
# Variables defined in this SteepestDescent
@@ -194,6 +194,7 @@ def run_step_CLIB(self):
194194
"""
195195

196196
clib.compute_sd_spin(self.spin, self.spin_last,
197+
self._magnetisation,
197198
self.mxH, self.mxmxH, self.mxmxH_last,
198199
self.tau, self._pins,
199200
self.n
@@ -203,6 +204,7 @@ def run_step_CLIB(self):
203204

204205
# Notice that the field is scaled (in the micro class we use Tesla)
205206
clib.compute_sd_step(self.spin, self.spin_last,
207+
self._magnetisation,
206208
self.scale * self.field,
207209
self.mxH, self.mxmxH, self.mxmxH_last,
208210
self.tau, self._pins,

0 commit comments

Comments
 (0)