@@ -238,7 +238,7 @@ def init_vector_func_fast(m0, mesh, double[:] field, norm=False, *args):
238
238
#
239
239
# - Methods that are redefined in inherited classes are only specified in the
240
240
# base class
241
- # - These can be declared in a .pxd file:
241
+ # - These can be declared in a .pxd file:
242
242
243
243
cdef extern from " c_energy.h" :
244
244
@@ -250,27 +250,29 @@ cdef extern from "c_energy.h":
250
250
void compute_field(double t)
251
251
double compute_energy()
252
252
void setup(int nx, int ny, int nz, double dx, double dy, double dz,
253
- double unit_length, double * spin,
254
- double * Ms, double * Ms_inv)
255
-
256
- bool set_up
257
- int nx, ny, nz, n
258
- double dx, dy, dz
259
- double unit_length
260
- double * spin
261
- double * Ms
262
- double * Ms_inv
263
- double * field
264
- double * energy
265
- double * coordinates
266
- int * ngbs
253
+ double unit_length, double * spin, double * Ms, double * Ms_inv,
254
+ double * coordinates, int * ngbs,
255
+ double * energy, double * field
256
+ )
257
+
258
+ # bool set_up
259
+ # int nx, ny, nz, n
260
+ # double dx, dy, dz
261
+ # double unit_length
262
+ # double *spin
263
+ # double *Ms
264
+ # double *Ms_inv
265
+ # double *field
266
+ # double *energy
267
+ # double *coordinates
268
+ # int *ngbs
267
269
268
270
cdef cppclass ExchangeEnergy(Energy):
269
271
ExchangeEnergy() except +
270
272
271
273
void init(double * A)
272
274
273
- double * A
275
+ # double *A
274
276
275
277
# cdef extern from "c_energy.cpp":
276
278
# pass
@@ -285,17 +287,17 @@ cdef extern from "c_energy.h":
285
287
# __cinit__ and __dealloc__ methods which are guaranteed to be called exactly
286
288
# once upon creation and deletion of the Python instance.
287
289
288
- cdef class PyEnergy:
289
- cdef Energy * thisptr
290
- # Try cinit:
291
- def __cinit__ (self ):
292
- # Should we allocate?
293
- # self.thisptr = new ExchangeEnergy ()
294
- print (" In Python A" )
295
-
296
- # We need to inherit from a cdef-ined base class to make the inheritance to
297
- # work. This way the constructors are called properly
298
- cdef class PyExchangeEnergy(PyEnergy ):
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 ):
299
301
cdef ExchangeEnergy * derivedptr
300
302
# Try cinit:
301
303
def __cinit__ (self , double [:] A ):
@@ -304,9 +306,8 @@ cdef class PyExchangeEnergy(PyEnergy):
304
306
self .derivedptr = new ExchangeEnergy()
305
307
self .derivedptr.init(& A[0 ])
306
308
307
- if self .thisptr:
308
- print (" in B: deallocating old A" )
309
- del self .thisptr
309
+ def __dealloc__ (self ):
310
+ del self .derivedptr
310
311
311
312
# DEBUG: check contents of the A array
312
313
# def printA(self):
@@ -315,21 +316,20 @@ cdef class PyExchangeEnergy(PyEnergy):
315
316
# lst.append(self.derivedptr.A[i])
316
317
# print(lst)
317
318
318
- # We could use another constructor if we use this method:
319
- # def __cinit__(self):
320
- # if type(self) is PyExchangeEnergy:
321
- # self.thisptr = new ExchangeEnergy()
322
- # def __dealloc__(self):
323
- # if type(self) is PyExchangeEnergy:
324
- # del self.thisptr
325
-
326
319
# Necessary?:
327
320
def compute_field (self , t ):
328
321
self .derivedptr.compute_field(t)
322
+
329
323
def compute_energy (self , time ):
330
324
return self .derivedptr.compute_energy()
325
+
331
326
def setup (self , nx , ny , nz , dx , dy , dz , unit_length ,
332
- double [:] spin , double [:] Ms , double [:] Ms_inv ):
333
- return self .derivedptr.setup(nx, ny, nz, dx, dy, dz, unit_length ,
334
- & spin[ 0 ], & Ms[ 0 ], & Ms_inv[ 0 ])
327
+ double [:] spin , double [:] Ms , double [:] Ms_inv ,
328
+ double [:, :] coordinates , int [:, :] neighbours ,
329
+ double [:] energy , double [:] field ):
335
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
+ )
0 commit comments