From e8b15b029795406b89513ce7b3c8ad0510a4cc20 Mon Sep 17 00:00:00 2001 From: davidcortesortuno Date: Tue, 11 Jun 2019 01:19:11 +0100 Subject: [PATCH] Added method to add interactions to sim C class using cython wrappers to c++ methods. Finished sim C class. Added some tests --- fidimag/common/c_clib.pyx | 60 ++++++++++++++++++++++++++++++++---- native/include/c_energy.h | 8 +++-- native/include/c_micro_sim.h | 10 ++++-- native/src/c_micro_sim.cpp | 2 +- tests/test_C_classes.py | 4 +++ 5 files changed, 72 insertions(+), 12 deletions(-) diff --git a/fidimag/common/c_clib.pyx b/fidimag/common/c_clib.pyx index 04f42cb6..2c00dbfb 100644 --- a/fidimag/common/c_clib.pyx +++ b/fidimag/common/c_clib.pyx @@ -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 + @@ -314,6 +315,13 @@ cdef class PyEnergy: &energy[0], &field[0] ) + def add_interaction_to_sim(self, PyMicroSim sim): + sim.thisptr.add_interaction( self.thisptr, + self.thisptr.interaction_id) + + def get_interaction_id(self): + return self._thisptr.interaction_id + cdef class PyExchangeEnergy(PyEnergy): cdef ExchangeEnergy *_thisptr @@ -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() + diff --git a/native/include/c_energy.h b/native/include/c_energy.h index 3b442ac1..47d72bf8 100644 --- a/native/include/c_energy.h +++ b/native/include/c_energy.h @@ -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; @@ -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; diff --git a/native/include/c_micro_sim.h b/native/include/c_micro_sim.h index 706883ae..39f99b67 100644 --- a/native/include/c_micro_sim.h +++ b/native/include/c_micro_sim.h @@ -22,7 +22,7 @@ class MicroSim { double *Ms_inv; double *energy; double *field; - double *pins; + int *pins; // Array with interactions // void * interactions; @@ -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 { diff --git a/native/src/c_micro_sim.cpp b/native/src/c_micro_sim.cpp index 1eb53cfc..65b93f29 100644 --- a/native/src/c_micro_sim.cpp +++ b/native/src/c_micro_sim.cpp @@ -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; diff --git a/tests/test_C_classes.py b/tests/test_C_classes.py index 18f1024d..6810502e 100644 --- a/tests/test_C_classes.py +++ b/tests/test_C_classes.py @@ -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 @@ -38,4 +39,7 @@ print(field) print(energy) +sim_C = PyMicroSim() +sim_C.add_interaction(Exch) + # time.sleep(5)