Commit df9233b3 authored by Georg Martin Reinke's avatar Georg Martin Reinke
Browse files

add Python functions to create/register external sources


Former-commit-id: 96bd3101
parent 716667a0
#include "PyComponent.h" #include "PyComponent.h"
#include "CIMReader.h" #include "CIMReader.h"
#include "Components/ExternalCurrentSource.h"
#include "Components/ExternalVoltageSource.h"
using namespace DPsim; using namespace DPsim;
...@@ -152,6 +154,32 @@ bool DPsim::compsFromPython(PyObject* list, std::vector<BaseComponent*>& comps) ...@@ -152,6 +154,32 @@ bool DPsim::compsFromPython(PyObject* list, std::vector<BaseComponent*>& comps)
return true; return true;
} }
PyObject* DPsim::pyExternalCurrentSource(PyObject* self, PyObject* args) {
const char *name;
int src, dest;
Py_complex initCurrent;
if (!PyArg_ParseTuple(args, "siiD", &name, &src, &dest, &initCurrent))
return nullptr;
PyComponent *pyComp = PyObject_New(PyComponent, &PyComponentType);
pyComp->comp = new ExternalCurrentSource(name, src, dest, Complex(initCurrent.real, initCurrent.imag));
return (PyObject*) pyComp;
}
PyObject* DPsim::pyExternalVoltageSource(PyObject* self, PyObject* args) {
const char *name;
int src, dest, num;
Py_complex initVoltage;
if (!PyArg_ParseTuple(args, "siiDi", &name, &src, &dest, &initVoltage, &num))
return nullptr;
PyComponent *pyComp = PyObject_New(PyComponent, &PyComponentType);
pyComp->comp = new ExternalVoltageSource(name, src, dest, Complex(initVoltage.real, initVoltage.imag), num);
return (PyObject*) pyComp;
}
PyObject* DPsim::pyLoadCim(PyObject* self, PyObject* args) { PyObject* DPsim::pyLoadCim(PyObject* self, PyObject* args) {
double frequency = 50; double frequency = 50;
PyObject *list; PyObject *list;
......
...@@ -26,6 +26,9 @@ namespace DPsim { ...@@ -26,6 +26,9 @@ namespace DPsim {
extern PyTypeObject PyComponentType; extern PyTypeObject PyComponentType;
bool compsFromPython(PyObject* list, std::vector<BaseComponent*>& comps); bool compsFromPython(PyObject* list, std::vector<BaseComponent*>& comps);
PyObject* pyExternalCurrentSource(PyObject* self, PyObject *args);
PyObject* pyExternalVoltageSource(PyObject* self, PyObject *args);
PyObject* pyLoadCim(PyObject* self, PyObject* args); PyObject* pyLoadCim(PyObject* self, PyObject* args);
}; };
......
#include "PyComponent.h"
#include "PyInterface.h" #include "PyInterface.h"
#include "ShmemInterface.h" #include "ShmemInterface.h"
using namespace DPsim; using namespace DPsim;
static PyMethodDef PyInterface_methods[] = {
{"register_source", PyInterface::registerSource, METH_VARARGS, "Registers an external source to use this interface."},
{0},
};
PyTypeObject DPsim::PyInterfaceType = { PyTypeObject DPsim::PyInterfaceType = {
PyVarObject_HEAD_INIT(NULL, 0) PyVarObject_HEAD_INIT(NULL, 0)
"dpsim.Interface", /* tp_name */ "dpsim.Interface", /* tp_name */
...@@ -27,6 +33,13 @@ PyTypeObject DPsim::PyInterfaceType = { ...@@ -27,6 +33,13 @@ PyTypeObject DPsim::PyInterfaceType = {
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_DEFAULT |
Py_TPFLAGS_BASETYPE, /* tp_flags */ Py_TPFLAGS_BASETYPE, /* tp_flags */
"An interface to an external source/sink of data.", /* tp_doc */ "An interface to an external source/sink of data.", /* tp_doc */
0, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
0, /* tp_iter */
0, /* tp_iternext */
PyInterface_methods, /* tp_methods */
}; };
void PyInterface::dealloc(PyInterface* self) { void PyInterface::dealloc(PyInterface* self) {
...@@ -35,7 +48,32 @@ void PyInterface::dealloc(PyInterface* self) { ...@@ -35,7 +48,32 @@ void PyInterface::dealloc(PyInterface* self) {
Py_TYPE(self)->tp_free((PyObject*) self); Py_TYPE(self)->tp_free((PyObject*) self);
} }
PyObject* pyShmemInterface(PyObject *self, PyObject *args, PyObject *kwds) { PyObject* PyInterface::registerSource(PyObject* self, PyObject* args) {
PyObject *obj;
int realIdx, imagIdx;
PyInterface* pyIntf = (PyInterface*) self;
if (!PyArg_ParseTuple(args, "Oii", &obj, &realIdx, &imagIdx))
return nullptr;
if (!PyObject_TypeCheck(obj, &PyComponentType)) {
PyErr_SetString(PyExc_TypeError, "First argument must be a Component");
return nullptr;
}
PyComponent *pyComp = (PyComponent*) obj;
if (ExternalCurrentSource *ecs = dynamic_cast<ExternalCurrentSource*>(pyComp->comp)) {
pyIntf->intf->registerCurrentSource(ecs, realIdx, imagIdx);
} else if (ExternalVoltageSource *evs = dynamic_cast<ExternalVoltageSource*>(pyComp->comp)) {
pyIntf->intf->registerVoltageSource(evs, realIdx, imagIdx);
} else {
PyErr_SetString(PyExc_TypeError, "First argument must be an external source");
return nullptr;
}
Py_INCREF(Py_None);
return Py_None;
}
PyObject* DPsim::pyShmemInterface(PyObject *self, PyObject *args, PyObject *kwds) {
static char *kwlist[] = {"queuelen", "samplelen", "polling", nullptr}; static char *kwlist[] = {"queuelen", "samplelen", "polling", nullptr};
struct shmem_conf conf; struct shmem_conf conf;
const char *wname, *rname; const char *wname, *rname;
......
...@@ -13,12 +13,14 @@ namespace DPsim { ...@@ -13,12 +13,14 @@ namespace DPsim {
ExternalInterface *intf; ExternalInterface *intf;
static void dealloc(PyInterface*); static void dealloc(PyInterface*);
static PyObject* registerSource(PyObject* self, PyObject* args);
}; };
extern PyTypeObject PyInterfaceType; extern PyTypeObject PyInterfaceType;
#ifdef __linux__ #ifdef __linux__
PyObject* pyShmemInterface(PyObject *self, PyObject *args); PyObject* pyShmemInterface(PyObject *self, PyObject *args, PyObject *kwds);
#endif #endif
}; };
......
...@@ -9,6 +9,8 @@ using namespace DPsim; ...@@ -9,6 +9,8 @@ using namespace DPsim;
PyMethodDef DPsim::pyModuleMethods[] = { PyMethodDef DPsim::pyModuleMethods[] = {
{"load_cim", pyLoadCim, METH_VARARGS, "Load a network from CIM file(s)."}, {"load_cim", pyLoadCim, METH_VARARGS, "Load a network from CIM file(s)."},
{"ExternalCurrentSource", pyExternalCurrentSource, METH_VARARGS, "Construct a new external current source."},
{"ExternalVoltageSource", pyExternalVoltageSource, METH_VARARGS, "Construct a new external voltage source."},
{"ShmemInterface", (PyCFunction)pyShmemInterface, METH_VARARGS|METH_KEYWORDS, "Construct an Interface that communicates via POSIX shared memory."}, {"ShmemInterface", (PyCFunction)pyShmemInterface, METH_VARARGS|METH_KEYWORDS, "Construct an Interface that communicates via POSIX shared memory."},
{0} {0}
}; };
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment