Skip to content

Commit 5677941

Browse files
Updated exchange class and started sim c++ class
1 parent fa53fec commit 5677941

File tree

3 files changed

+79
-38
lines changed

3 files changed

+79
-38
lines changed

fidimag/common/c_clib.pyx

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,7 @@ cdef extern from "c_energy.h":
255255
double *energy, double *field
256256
)
257257

258+
# # Not using these variables from Cython:
258259
# bool set_up
259260
# int nx, ny, nz, n
260261
# double dx, dy, dz
@@ -271,7 +272,6 @@ cdef extern from "c_energy.h":
271272
ExchangeEnergy() except +
272273

273274
void init(double *A)
274-
275275
# double *A
276276

277277
# cdef extern from "c_energy.cpp":
@@ -287,49 +287,49 @@ cdef extern from "c_energy.h":
287287
# __cinit__ and __dealloc__ methods which are guaranteed to be called exactly
288288
# once upon creation and deletion of the Python instance.
289289

290-
#cdef class PyEnergy:
291-
# cdef Energy *thisptr
292-
# # Try cinit:
293-
# def __cinit__(self):
294-
# # Should we allocate?
295-
# # self.thisptr = new Energy()
296-
# print("In Python A")
297-
298-
# No need to replicate C++ class structure; we make the ExchangeEnergy a
299-
# base class in Python (which we know inherits from Energy in C++)
300-
cdef class PyExchangeEnergy(object):
301-
cdef ExchangeEnergy *derivedptr
290+
cdef class PyEnergy:
291+
cdef Energy *thisptr
292+
def __cinit__(self):
293+
# No need to allocate memory, we are not using the C++ base class
294+
# self.thisptr = new Energy()
295+
if type(self) != PyEnergy:
296+
return
297+
print("In Python A")
298+
299+
# Necessary?:
300+
def compute_field(self, t):
301+
self.thisptr.compute_field(t)
302+
303+
def compute_energy(self, time):
304+
return self.thisptr.compute_energy()
305+
306+
def setup(self, nx, ny, nz, dx, dy, dz, unit_length,
307+
double [:] spin, double [:] Ms, double [:] Ms_inv,
308+
double [:, :] coordinates, int [:, :] neighbours,
309+
double [:] energy, double [:] field):
310+
311+
return self.thisptr.setup(nx, ny, nz, dx, dy, dz, unit_length,
312+
&spin[0], &Ms[0], &Ms_inv[0],
313+
&coordinates[0, 0], &neighbours[0, 0],
314+
&energy[0], &field[0]
315+
)
316+
317+
318+
cdef class PyExchangeEnergy(PyEnergy):
319+
cdef ExchangeEnergy *_thisptr
302320
# Try cinit:
303321
def __cinit__(self, double [:] A):
304322
print("In Python B")
305323

306-
self.derivedptr = new ExchangeEnergy()
307-
self.derivedptr.init(&A[0])
324+
self._thisptr = self.thisptr = new ExchangeEnergy()
325+
self._thisptr.init(&A[0])
308326

309327
def __dealloc__(self):
310-
del self.derivedptr
328+
del self.thisptr
311329

312330
# DEBUG: check contents of the A array
313331
# def printA(self):
314332
# lst = []
315333
# for i in range(4):
316334
# lst.append(self.derivedptr.A[i])
317335
# print(lst)
318-
319-
# Necessary?:
320-
def compute_field(self, t):
321-
self.derivedptr.compute_field(t)
322-
323-
def compute_energy(self, time):
324-
return self.derivedptr.compute_energy()
325-
326-
def setup(self, nx, ny, nz, dx, dy, dz, unit_length,
327-
double [:] spin, double [:] Ms, double [:] Ms_inv,
328-
double [:, :] coordinates, int [:, :] neighbours,
329-
double [:] energy, double [:] field):
330-
331-
return self.derivedptr.setup(nx, ny, nz, dx, dy, dz, unit_length,
332-
&spin[0], &Ms[0], &Ms_inv[0],
333-
&coordinates[0, 0], &neighbours[0, 0],
334-
&energy[0], &field[0]
335-
)

fidimag/micro/exchange.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import fidimag.extensions.micro_clib as micro_clib
22
from .energy import Energy
3-
#from constant import mu_0
43

54

65
class UniformExchange(Energy):
@@ -9,12 +8,12 @@ class UniformExchange(Energy):
98
UniformExchange(A, name='UniformExchange')
109
1110
Compute the exchange field in micromagnetics.
12-
11+
1312
Inputs:
1413
A: float
15-
A is the exchange stiffness constant measured in
14+
A is the exchange stiffness constant measured in
1615
Joules / Meter (J / M)
17-
16+
1817
"""
1918

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

native/include/c_micro_sim.h

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#include<cmath>
2+
#include<iostream>
3+
#include<vector>
4+
// #define MAX_ENERGY_TERMS 20
5+
6+
class MicroSim {
7+
public:
8+
MicroSim() {std::cout << "Instatiating MicroSim class; at " << this << "\n";};
9+
virtual ~MicroSim() {std::cout << "Killing MicroSim\n";};
10+
bool set_up;
11+
12+
// From the mesh
13+
int nx, ny, nz, n;
14+
double dx, dy, dz;
15+
double unit_length;
16+
double *coordinates;
17+
int *ngbs;
18+
19+
// Arrays of material properties
20+
double *spin;
21+
double *Ms;
22+
double *Ms_inv;
23+
double *energy;
24+
double *field;
25+
double *pins;
26+
27+
// Array with interactions
28+
// void * interactions;
29+
// int interactions_id[MAX_ENERGY_TERMS];
30+
std::vector<void *> interactions;
31+
std::vector<int> interactions_id;
32+
33+
// Methods
34+
void setup(int nx, int ny, int nz, double dx, double dy, double dz,
35+
double unit_length, double *coordinates, int *ngbs,
36+
double *spin, double *Ms, double *Ms_inv,
37+
double *energy, double *field, double *pins
38+
);
39+
40+
void add_interaction(void * interaction);
41+
42+
};

0 commit comments

Comments
 (0)