Skip to content
Snippets Groups Projects
Commit c4303330 authored by Giovanni Bussi's avatar Giovanni Bussi
Browse files

Using C++ interface in python

I think python should use the C++ interface rather than the C one.
The reason is that the C++ interface included in PLUMED 2.5
rethrows exceptions and thus can be used to load
a shared library compiled with a different compiler or
linked against a different standard library.

In perspective, this will allow compiling PLUMED with
a different compiler/library than that used to compile
python itself.
parent f328b57f
No related branches found
No related tags found
No related merge requests found
...@@ -22,9 +22,11 @@ ...@@ -22,9 +22,11 @@
# #
# This create cython wrappers to the main bits of the PLUMED libraray # This create cython wrappers to the main bits of the PLUMED libraray
# #
cdef extern from "Plumed.h" :
ctypedef struct plumed: cdef extern from "Plumed.h" namespace "PLMD":
pass cdef cppclass Plumed:
plumed plumed_create() Plumed() except +
void plumed_cmd(plumed p, const char*key, const void*val) except + void cmd(const char*key, const void*val) except +
void plumed_finalize(plumed p) except + void cmd(const char*key) except +
...@@ -29,25 +29,20 @@ import numpy as np ...@@ -29,25 +29,20 @@ import numpy as np
cimport numpy as np cimport numpy as np
cdef class Plumed: cdef class Plumed:
cdef cplumed.plumed c_plumed cdef cplumed.Plumed c_plumed
def __cinit__(self): def __cinit__(self):
self.c_plumed = cplumed.plumed_create() #new cplumed.Plumed()
cdef int pres = 8 cdef int pres = 8
cplumed.plumed_cmd(self.c_plumed, "setRealPrecision", <void*>&pres ) self.c_plumed.cmd( "setRealPrecision", <void*>&pres )
def __dealloc__(self):
cplumed.plumed_finalize(self.c_plumed)
def cmd_ndarray_real(self, ckey, val): def cmd_ndarray_real(self, ckey, val):
cdef double [:] abuffer = val.ravel() cdef double [:] abuffer = val.ravel()
cplumed.plumed_cmd(self.c_plumed, ckey, <void*>&abuffer[0]) self.c_plumed.cmd( ckey, <void*>&abuffer[0])
def cmd_ndarray_int(self, ckey, val): def cmd_ndarray_int(self, ckey, val):
cdef long [:] abuffer = val.ravel() cdef long [:] abuffer = val.ravel()
cplumed.plumed_cmd(self.c_plumed, ckey, <void*>&abuffer[0]) self.c_plumed.cmd( ckey, <void*>&abuffer[0])
cdef cmd_float(self, ckey, double val ): cdef cmd_float(self, ckey, double val ):
cplumed.plumed_cmd(self.c_plumed, ckey, <void*>&val ) self.c_plumed.cmd( ckey, <void*>&val )
cdef cmd_int(self, ckey, int val): cdef cmd_int(self, ckey, int val):
cplumed.plumed_cmd(self.c_plumed, ckey, <void*>&val) self.c_plumed.cmd( ckey, <void*>&val)
def cmd( self, key, val=None ): def cmd( self, key, val=None ):
cdef bytes py_bytes = key.encode() cdef bytes py_bytes = key.encode()
cdef char* ckey = py_bytes cdef char* ckey = py_bytes
...@@ -55,7 +50,7 @@ cdef class Plumed: ...@@ -55,7 +50,7 @@ cdef class Plumed:
cdef np.int_t[:] ibuffer cdef np.int_t[:] ibuffer
cdef np.float64_t[:] dbuffer cdef np.float64_t[:] dbuffer
if val is None : if val is None :
cplumed.plumed_cmd( self.c_plumed, ckey, NULL ) self.c_plumed.cmd( ckey, NULL )
elif isinstance(val, (int,long) ): elif isinstance(val, (int,long) ):
if key=="getDataRank" : if key=="getDataRank" :
raise ValueError("when using cmd with getDataRank option value must a size one ndarray") raise ValueError("when using cmd with getDataRank option value must a size one ndarray")
...@@ -74,6 +69,6 @@ cdef class Plumed: ...@@ -74,6 +69,6 @@ cdef class Plumed:
elif isinstance(val, basestring ) : elif isinstance(val, basestring ) :
py_bytes = val.encode() py_bytes = val.encode()
cval = py_bytes cval = py_bytes
cplumed.plumed_cmd( self.c_plumed, ckey, <void*>cval ) self.c_plumed.cmd( ckey, <void*>cval )
else : else :
raise ValueError("Unknown value type ({})".format(str(type(val)))) raise ValueError("Unknown value type ({})".format(str(type(val))))
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment