@@ -255,6 +255,7 @@ cdef extern from "c_energy.h":
255
255
double * energy, double * field
256
256
)
257
257
258
+ # # Not using these variables from Cython:
258
259
# bool set_up
259
260
# int nx, ny, nz, n
260
261
# double dx, dy, dz
@@ -271,7 +272,6 @@ cdef extern from "c_energy.h":
271
272
ExchangeEnergy() except +
272
273
273
274
void init(double * A)
274
-
275
275
# double *A
276
276
277
277
# cdef extern from "c_energy.cpp":
@@ -287,49 +287,49 @@ cdef extern from "c_energy.h":
287
287
# __cinit__ and __dealloc__ methods which are guaranteed to be called exactly
288
288
# once upon creation and deletion of the Python instance.
289
289
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
302
320
# Try cinit:
303
321
def __cinit__ (self , double [:] A ):
304
322
print (" In Python B" )
305
323
306
- self .derivedptr = new ExchangeEnergy()
307
- self .derivedptr .init(& A[0 ])
324
+ self ._thisptr = self .thisptr = new ExchangeEnergy()
325
+ self ._thisptr .init(& A[0 ])
308
326
309
327
def __dealloc__ (self ):
310
- del self .derivedptr
328
+ del self .thisptr
311
329
312
330
# DEBUG: check contents of the A array
313
331
# def printA(self):
314
332
# lst = []
315
333
# for i in range(4):
316
334
# lst.append(self.derivedptr.A[i])
317
335
# 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
- )
0 commit comments