Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Large diffs are not rendered by default.

13 changes: 7 additions & 6 deletions fidimag/micro/baryakhtar.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,10 @@ def get_chi(self):
def set_chi(self, chi):
"""
"""
self._chi = chi
for i in self.interactions:
if i.name == 'chi_relax':
i.chi = chi
i.set_chi(chi)

chi = property(get_chi, set_chi)

Expand Down Expand Up @@ -76,7 +77,7 @@ def sundials_rhs(self, t, y, ydot):

class LLBar(MicroDriver):

def __init__(self, mesh, spin, Ms, field, pins,
def __init__(self, mesh, spin, Ms, Ms_inv, field, pins,
interactions,
name,
data_saver,
Expand All @@ -85,11 +86,11 @@ def __init__(self, mesh, spin, Ms, field, pins,
):

# Inherit from the driver class
super().__init__(mesh, spin, Ms, field,
super().__init__(mesh, spin, Ms, Ms_inv, field,
pins, interactions, name,
data_saver,
integrator='cvode_bdf',
use_jac=False
integrator=integrator,
use_jac=use_jac
)

self.lap = Laplace(mesh)
Expand All @@ -112,7 +113,7 @@ def sundials_rhs(self, t, y, ydot):

clib.compute_llg_rhs_baryakhtar_reduced(ydot,
self.spin,
self.field,
self.field_perp,
delta_h,
self.alpha,
self.beta,
Expand Down
14 changes: 8 additions & 6 deletions fidimag/micro/lib/baryakhtar/llg.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,16 @@ void llg_rhs_baryakhtar(double *dm_dt, double *m, double *h, double *delta_h,

double coeff = -gamma;

dm_dt[i] = dm_dt[j] = dm_dt[k] = 0;
if (do_precession){
dm_dt[i] = coeff*cross_x(m[i],m[j],m[k],h[i],h[j],h[k]);
dm_dt[j] = coeff*cross_y(m[i],m[j],m[k],h[i],h[j],h[k]);
dm_dt[k] = coeff*cross_z(m[i],m[j],m[k],h[i],h[j],h[k]);
}

dm_dt[i] += gamma*(alpha[i]*h[i] - beta*delta_h[i]);
dm_dt[j] += gamma*(alpha[i]*h[j] - beta*delta_h[j]);
dm_dt[k] += gamma*(alpha[i]*h[k] - beta*delta_h[k]);
dm_dt[i] += gamma*(alpha[id]*h[i] - beta*delta_h[i]);
dm_dt[j] += gamma*(alpha[id]*h[j] - beta*delta_h[j]);
dm_dt[k] += gamma*(alpha[id]*h[k] - beta*delta_h[k]);

}

Expand All @@ -52,15 +53,16 @@ void llg_rhs_baryakhtar_reduced(double *dm_dt, double *m, double *hp, double *de

double coeff = -gamma;

dm_dt[i] = dm_dt[j] = dm_dt[k] = 0;
if (do_precession){
dm_dt[i] = coeff*cross_x(m[i],m[j],m[k],hp[i],hp[j],hp[k]);
dm_dt[j] = coeff*cross_y(m[i],m[j],m[k],hp[i],hp[j],hp[k]);
dm_dt[k] = coeff*cross_z(m[i],m[j],m[k],hp[i],hp[j],hp[k]);
}

double hpx = alpha[i]*hp[i]-beta*delta_hp[i];
double hpy = alpha[i]*hp[j]-beta*delta_hp[j];
double hpz = alpha[i]*hp[k]-beta*delta_hp[k];
double hpx = alpha[id]*hp[i]-beta*delta_hp[i];
double hpy = alpha[id]*hp[j]-beta*delta_hp[j];
double hpz = alpha[id]*hp[k]-beta*delta_hp[k];

double mm = m[i]*m[i] + m[j]*m[j] + m[k]*m[k];
double mh = m[i]*hpx + m[j]*hpy + m[k]*hpz;
Expand Down
127 changes: 34 additions & 93 deletions fidimag/micro/lib/baryakhtar/util.c
Original file line number Diff line number Diff line change
@@ -1,123 +1,64 @@
#include "baryakhtar_clib.h"

/* Vector fields are x0, y0, z0, x1, y1, z1, ... and the cells are numbered with x running fastest, as everywhere else. */
void compute_laplace_m(double *m, double *field, double *Ms, double dx,
double dy, double dz, int nx, int ny, int nz) {
int nyz = ny * nz;
int n1 = nx * nyz, n2 = 2 * n1;

double ax = 1.0 / (dx * dx), ay = 1.0 / (dy * dy), az = 1.0 / (dz * dz);
const int nxy = nx * ny;
const double ax = 1.0 / (dx * dx), ay = 1.0 / (dy * dy), az = 1.0 / (dz * dz);

#pragma omp parallel for
for (int i = 0; i < nx; i++) {
for (int k = 0; k < nz; k++) {
for (int j = 0; j < ny; j++) {
for (int k = 0; k < nz; k++) {

int id = 0;
int index = nyz * i + nz * j + k;

double fx = 0, fy = 0, fz = 0;

if (Ms[index] == 0.0) {
field[index] = 0;
field[index + n1] = 0;
field[index + n2] = 0;
continue;
}

if (k > 0) {
id = index - 1;
if (Ms[id] > 0) {
fx += az * (m[id] - m[index]);
fy += az * (m[id + n1] - m[index + n1]);
fz += az * (m[id + n2] - m[index + n2]);
for (int i = 0; i < nx; i++) {
const int index = k * nxy + j * nx + i;
double f[3] = {0, 0, 0};

if (Ms[index] > 0.0) {
// (a cell without material is not a neighbour of anything)
int ids[6], count = 0;
double a[6];
if (i > 0) { ids[count] = index - 1; a[count++] = ax; }
if (i < nx - 1) { ids[count] = index + 1; a[count++] = ax; }
if (j > 0) { ids[count] = index - nx; a[count++] = ay; }
if (j < ny - 1) { ids[count] = index + nx; a[count++] = ay; }
if (k > 0) { ids[count] = index - nxy; a[count++] = az; }
if (k < nz - 1) { ids[count] = index + nxy; a[count++] = az; }
for (int q = 0; q < count; q++) {
if (Ms[ids[q]] > 0.0)
for (int c = 0; c < 3; c++) f[c] += a[q] * (m[3 * ids[q] + c] - m[3 * index + c]);
}
}

if (j > 0) {
id = index - nz;
if (Ms[id] > 0) {
fx += ay * (m[id] - m[index]);
fy += ay * (m[id + n1] - m[index + n1]);
fz += ay * (m[id + n2] - m[index + n2]);
}
}

if (i > 0) {
id = index - nyz;
if (Ms[id] > 0) {
fx += ax * (m[id] - m[index]);
fy += ax * (m[id + n1] - m[index + n1]);
fz += ax * (m[id + n2] - m[index + n2]);
}
}

if (i < nx - 1) {
id = index + nyz;
if (Ms[id] > 0) {
fx += ax * (m[id] - m[index]);
fy += ax * (m[id + n1] - m[index + n1]);
fz += ax * (m[id + n2] - m[index + n2]);
}
}

if (j < ny - 1) {
id = index + nz;
if (Ms[id] > 0) {
fx += ay * (m[id] - m[index]);
fy += ay * (m[id + n1] - m[index + n1]);
fz += ay * (m[id + n2] - m[index + n2]);
}
}

if (k < nz - 1) {
id = index + 1;
if (Ms[id] > 0) {
fx += az * (m[id] - m[index]);
fy += az * (m[id + n1] - m[index + n1]);
fz += az * (m[id + n2] - m[index + n2]);
}
}

field[index] = fx;
field[index + n1] = fy;
field[index + n2] = fz;
for (int c = 0; c < 3; c++) field[3 * index + c] = f[c];
}
}

}
}

void compute_relaxation_field_c(double *m, double *field, double *Ms,
double chi_inv, int n) {

#pragma omp parallel for
for (int i = 0; i < n; i++) {
int j = i + n;
int k = j + n;

double relax = Ms[i] * chi_inv / 2.0;
double mm = m[i] * m[i] + m[j] * m[j] + m[k] * m[k];

field[i] = relax * (1 - mm) * m[i];
field[j] = relax * (1 - mm) * m[j];
field[k] = relax * (1 - mm) * m[k];
for (int id = 0; id < n; id++) {
const double *s = m + 3 * id;
const double relax = Ms[id] * chi_inv / 2.0;
const double mm = s[0] * s[0] + s[1] * s[1] + s[2] * s[2];

for (int c = 0; c < 3; c++) field[3 * id + c] = relax * (1 - mm) * s[c];
}
}

void compute_perp_field_c(double *m, double *field, double *field_p, int n) {

#pragma omp parallel for
for (int i = 0; i < n; i++) {
int j = i + n;
int k = j + n;

double mm = m[i] * m[i] + m[j] * m[j] + m[k] * m[k];
double hm = field[i] * m[i] + field[j] * m[j] + field[k] * m[k];
for (int id = 0; id < n; id++) {
const double *s = m + 3 * id;
const double *h = field + 3 * id;

field_p[i] = field[i] - hm * m[i] / mm;
field_p[j] = field[j] - hm * m[j] / mm;
field_p[k] = field[k] - hm * m[k] / mm;
const double mm = s[0] * s[0] + s[1] * s[1] + s[2] * s[2];
const double hm = h[0] * s[0] + h[1] * s[1] + h[2] * s[2];

// (a cell with no spin has no perpendicular field)
for (int c = 0; c < 3; c++) field_p[3 * id + c] = mm > 0.0 ? h[c] - hm * s[c] / mm : 0.0;
}
}
11 changes: 7 additions & 4 deletions fidimag/micro/relax.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ class Relaxation:
def __init__(self, chi, name='relax'):
self.chi = chi
self.name = name
self.jac = False

def set_chi(self, chi):
"""Change the susceptibility (the field uses its inverse)"""
self.chi = chi
self.chi_inv = 0 if chi == 0.0 else 1.0 / chi

def setup(self, mesh, m, Ms):
self.mesh = mesh
Expand All @@ -24,10 +30,7 @@ def setup(self, mesh, m, Ms):
self.Ms = Ms
self.field = np.zeros(3 * mesh.n)

if self.chi == 0.0:
self.chi_inv = 0
else:
self.chi_inv = 1.0 / self.chi
self.set_chi(self.chi)

def compute_field(self, t=0):

Expand Down
Loading
Loading