Skip to content

Commit e8b15b0

Browse files
Added method to add interactions to sim C class using cython wrappers to c++ methods. Finished sim C class. Added some tests
1 parent 46b765d commit e8b15b0

File tree

5 files changed

+72
-12
lines changed

5 files changed

+72
-12
lines changed

fidimag/common/c_clib.pyx

Lines changed: 54 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,7 @@ cdef extern from "c_energy.h":
267267
# double *energy
268268
# double *coordinates
269269
# int *ngbs
270+
int interaction_id
270271

271272
cdef cppclass ExchangeEnergy(Energy):
272273
ExchangeEnergy() except +
@@ -314,6 +315,13 @@ cdef class PyEnergy:
314315
&energy[0], &field[0]
315316
)
316317

318+
def add_interaction_to_sim(self, PyMicroSim sim):
319+
sim.thisptr.add_interaction(<void *> self.thisptr,
320+
self.thisptr.interaction_id)
321+
322+
def get_interaction_id(self):
323+
return self._thisptr.interaction_id
324+
317325

318326
cdef class PyExchangeEnergy(PyEnergy):
319327
cdef ExchangeEnergy *_thisptr
@@ -324,12 +332,52 @@ cdef class PyExchangeEnergy(PyEnergy):
324332
self._thisptr = self.thisptr = new ExchangeEnergy()
325333
self._thisptr.init(&A[0])
326334

335+
def __dealloc__(self):
336+
del self._thisptr
337+
338+
339+
# Simulation class ------------------------------------------------------------
340+
341+
cdef extern from "c_micro_sim.h":
342+
343+
cdef cppclass MicroSim:
344+
# except +: Without this declaration, C++ exceptions originating from
345+
# the constructor will not be handled by Cython.
346+
MicroSim() except +
347+
348+
void setup(int nx, int ny, int nz, double dx, double dy, double dz,
349+
double unit_length, double *coordinates, int *ngbs,
350+
double *spin, double *Ms, double *Ms_inv,
351+
double *energy, double *field, int *pins
352+
)
353+
354+
void add_interaction(void * interaction, int int_id)
355+
void print_interactions_id()
356+
357+
358+
cdef class PyMicroSim(object):
359+
cdef MicroSim *thisptr
360+
# Try cinit:
361+
def __cinit__(self):
362+
363+
self.thisptr = new MicroSim()
364+
327365
def __dealloc__(self):
328366
del self.thisptr
329367

330-
# DEBUG: check contents of the A array
331-
# def printA(self):
332-
# lst = []
333-
# for i in range(4):
334-
# lst.append(self.derivedptr.A[i])
335-
# print(lst)
368+
def setup(self, nx, ny, nz, dx, dy, dz, unit_length,
369+
double [:, :] coordinates, int [:, :] neighbours,
370+
double [:] spin, double [:] Ms, double [:] Ms_inv,
371+
double [:] energy, double [:] field, int [:] pins
372+
):
373+
374+
return self.thisptr.setup(nx, ny, nz, dx, dy, dz, unit_length,
375+
&coordinates[0, 0], &neighbours[0, 0],
376+
&spin[0], &Ms[0], &Ms_inv[0],
377+
&energy[0], &field[0], &pins[0]
378+
)
379+
380+
def add_interaction(self, Interaction):
381+
Interaction.add_interaction_to_sim(self)
382+
self.thisptr.print_interactions_id()
383+

native/include/c_energy.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33

44
class Energy {
55
public:
6-
Energy() {std::cout << "In A; at " << this << "\n";};
6+
Energy() {};
77
virtual ~Energy() {std::cout << "Killing A\n";};
8+
int interaction_id;
89
bool set_up;
910
int nx, ny, nz, n;
1011
double dx, dy, dz;
@@ -27,7 +28,10 @@ class Energy {
2728

2829
class ExchangeEnergy : public Energy {
2930
public:
30-
ExchangeEnergy() {std::cout << "In B; at " << this << "\n";};
31+
ExchangeEnergy() {
32+
std::cout << "Instatiating ExchangeEnergy class; at " << this << "\n";
33+
this->interaction_id = 1;
34+
};
3135
~ExchangeEnergy() {std::cout << "Killing B\n";};
3236
void init(double *A) {
3337
this->set_up = false;

native/include/c_micro_sim.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class MicroSim {
2222
double *Ms_inv;
2323
double *energy;
2424
double *field;
25-
double *pins;
25+
int *pins;
2626

2727
// Array with interactions
2828
// void * interactions;
@@ -34,11 +34,15 @@ class MicroSim {
3434
void setup(int nx, int ny, int nz, double dx, double dy, double dz,
3535
double unit_length, double *coordinates, int *ngbs,
3636
double *spin, double *Ms, double *Ms_inv,
37-
double *energy, double *field, double *pins
37+
double *energy, double *field, int *pins
3838
);
3939

40-
void add_interaction(void * interaction);
40+
void add_interaction(void * interaction, int int_id);
4141

42+
void print_interactions_id() {
43+
for(int i : interactions_id) std::cout << i << "\n";
44+
for(auto i : interactions) std::cout << i << "\n";
45+
}
4246
};
4347

4448
enum EnergyTermIDs {

native/src/c_micro_sim.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
void MicroSim::setup(int nx, int ny, int nz, double dx, double dy, double dz,
66
double unit_length, double *coordinates, int *ngbs,
77
double *spin, double *Ms, double *Ms_inv,
8-
double *energy, double *field, double *pins
8+
double *energy, double *field, int *pins
99
) {
1010

1111
this->nx = nx;

tests/test_C_classes.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from fidimag.extensions.c_clib import PyExchangeEnergy
2+
from fidimag.extensions.c_clib import PyMicroSim
23
from fidimag.common import CuboidMesh
34

45
import time
@@ -38,4 +39,7 @@
3839
print(field)
3940
print(energy)
4041

42+
sim_C = PyMicroSim()
43+
sim_C.add_interaction(Exch)
44+
4145
# time.sleep(5)

0 commit comments

Comments
 (0)