Skip to content

Commit

Permalink
Added method to add interactions to sim C class using cython wrappers…
Browse files Browse the repository at this point in the history
… to c++ methods. Finished sim C class. Added some tests
  • Loading branch information
davidcortesortuno committed Jun 11, 2019
1 parent 46b765d commit e8b15b0
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 12 deletions.
60 changes: 54 additions & 6 deletions fidimag/common/c_clib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ cdef extern from "c_energy.h":
# double *energy
# double *coordinates
# int *ngbs
int interaction_id

cdef cppclass ExchangeEnergy(Energy):
ExchangeEnergy() except +
Expand Down Expand Up @@ -314,6 +315,13 @@ cdef class PyEnergy:
&energy[0], &field[0]
)

def add_interaction_to_sim(self, PyMicroSim sim):
sim.thisptr.add_interaction(<void *> self.thisptr,
self.thisptr.interaction_id)

def get_interaction_id(self):
return self._thisptr.interaction_id


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

def __dealloc__(self):
del self._thisptr


# Simulation class ------------------------------------------------------------

cdef extern from "c_micro_sim.h":

cdef cppclass MicroSim:
# except +: Without this declaration, C++ exceptions originating from
# the constructor will not be handled by Cython.
MicroSim() except +

void setup(int nx, int ny, int nz, double dx, double dy, double dz,
double unit_length, double *coordinates, int *ngbs,
double *spin, double *Ms, double *Ms_inv,
double *energy, double *field, int *pins
)

void add_interaction(void * interaction, int int_id)
void print_interactions_id()


cdef class PyMicroSim(object):
cdef MicroSim *thisptr
# Try cinit:
def __cinit__(self):

self.thisptr = new MicroSim()

def __dealloc__(self):
del self.thisptr

# DEBUG: check contents of the A array
# def printA(self):
# lst = []
# for i in range(4):
# lst.append(self.derivedptr.A[i])
# print(lst)
def setup(self, nx, ny, nz, dx, dy, dz, unit_length,
double [:, :] coordinates, int [:, :] neighbours,
double [:] spin, double [:] Ms, double [:] Ms_inv,
double [:] energy, double [:] field, int [:] pins
):

return self.thisptr.setup(nx, ny, nz, dx, dy, dz, unit_length,
&coordinates[0, 0], &neighbours[0, 0],
&spin[0], &Ms[0], &Ms_inv[0],
&energy[0], &field[0], &pins[0]
)

def add_interaction(self, Interaction):
Interaction.add_interaction_to_sim(self)
self.thisptr.print_interactions_id()

8 changes: 6 additions & 2 deletions native/include/c_energy.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@

class Energy {
public:
Energy() {std::cout << "In A; at " << this << "\n";};
Energy() {};
virtual ~Energy() {std::cout << "Killing A\n";};
int interaction_id;
bool set_up;
int nx, ny, nz, n;
double dx, dy, dz;
Expand All @@ -27,7 +28,10 @@ class Energy {

class ExchangeEnergy : public Energy {
public:
ExchangeEnergy() {std::cout << "In B; at " << this << "\n";};
ExchangeEnergy() {
std::cout << "Instatiating ExchangeEnergy class; at " << this << "\n";
this->interaction_id = 1;
};
~ExchangeEnergy() {std::cout << "Killing B\n";};
void init(double *A) {
this->set_up = false;
Expand Down
10 changes: 7 additions & 3 deletions native/include/c_micro_sim.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class MicroSim {
double *Ms_inv;
double *energy;
double *field;
double *pins;
int *pins;

// Array with interactions
// void * interactions;
Expand All @@ -34,11 +34,15 @@ class MicroSim {
void setup(int nx, int ny, int nz, double dx, double dy, double dz,
double unit_length, double *coordinates, int *ngbs,
double *spin, double *Ms, double *Ms_inv,
double *energy, double *field, double *pins
double *energy, double *field, int *pins
);

void add_interaction(void * interaction);
void add_interaction(void * interaction, int int_id);

void print_interactions_id() {
for(int i : interactions_id) std::cout << i << "\n";
for(auto i : interactions) std::cout << i << "\n";
}
};

enum EnergyTermIDs {
Expand Down
2 changes: 1 addition & 1 deletion native/src/c_micro_sim.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
void MicroSim::setup(int nx, int ny, int nz, double dx, double dy, double dz,
double unit_length, double *coordinates, int *ngbs,
double *spin, double *Ms, double *Ms_inv,
double *energy, double *field, double *pins
double *energy, double *field, int *pins
) {

this->nx = nx;
Expand Down
4 changes: 4 additions & 0 deletions tests/test_C_classes.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from fidimag.extensions.c_clib import PyExchangeEnergy
from fidimag.extensions.c_clib import PyMicroSim
from fidimag.common import CuboidMesh

import time
Expand Down Expand Up @@ -38,4 +39,7 @@
print(field)
print(energy)

sim_C = PyMicroSim()
sim_C.add_interaction(Exch)

# time.sleep(5)

0 comments on commit e8b15b0

Please sign in to comment.