Commit 7d3c887b authored by Georg Martin Reinke's avatar Georg Martin Reinke
Browse files

more documentation/tweaks for Interface


Former-commit-id: 6e50b0ff
parent 253d2712
...@@ -6,7 +6,7 @@ comps = [dpsim.VoltSourceRes("v_s", 1, 0, 10000+0j, 1), ...@@ -6,7 +6,7 @@ comps = [dpsim.VoltSourceRes("v_s", 1, 0, 10000+0j, 1),
dpsim.Inductor("l_1", 1, 2, 1e-3)] dpsim.Inductor("l_1", 1, 2, 1e-3)]
evs = dpsim.ExternalVoltageSource("v_ext", 2, 0, 0+0j, 1) evs = dpsim.ExternalVoltageSource("v_ext", 2, 0, 0+0j, 1)
comps.append(evs) comps.append(evs)
intf = dpsim.ShmemInterface("/dpsim12", "/dpsim21", samplelen=2) intf = dpsim.open_shmem_interface("/dpsim12", "/dpsim21", samplelen=2)
intf.register_source(evs, 0, 1) intf.register_source(evs, 0, 1)
intf.export_current(evs, 0, 1) intf.export_current(evs, 0, 1)
sim = dpsim.Simulation(comps, duration=1, llog="lvector-shmem1.csv") sim = dpsim.Simulation(comps, duration=1, llog="lvector-shmem1.csv")
......
...@@ -5,7 +5,7 @@ import dpsim ...@@ -5,7 +5,7 @@ import dpsim
comps = [dpsim.LinearResistor("r_1", 1, 0, 1)] comps = [dpsim.LinearResistor("r_1", 1, 0, 1)]
ecs = dpsim.ExternalCurrentSource("i_ext", 1, 0, 0+0j) ecs = dpsim.ExternalCurrentSource("i_ext", 1, 0, 0+0j)
comps.append(ecs) comps.append(ecs)
intf = dpsim.ShmemInterface("/dpsim21", "/dpsim12", samplelen=2) intf = dpsim.open_shmem_interface("/dpsim21", "/dpsim12", samplelen=2)
intf.register_source(ecs, 0, 1) intf.register_source(ecs, 0, 1)
intf.export_voltage(1, 0, 0, 1) intf.export_voltage(1, 0, 0, 1)
sim = dpsim.Simulation(comps, duration=1, llog="lvector-shmem2.csv") sim = dpsim.Simulation(comps, duration=1, llog="lvector-shmem2.csv")
......
...@@ -5,51 +5,20 @@ ...@@ -5,51 +5,20 @@
using namespace DPsim; using namespace DPsim;
static PyMethodDef PyInterface_methods[] = {
{"export_current", PyInterface::exportCurrent, METH_VARARGS, "Registers a current to be exported on this interface."},
{"export_voltage", PyInterface::exportVoltage, METH_VARARGS, "Registers a voltage to be exported on this interface."},
{"register_source", PyInterface::registerSource, METH_VARARGS, "Registers an external source to use this interface."},
{0},
};
PyTypeObject DPsim::PyInterfaceType = {
PyVarObject_HEAD_INIT(NULL, 0)
"dpsim.Interface", /* tp_name */
sizeof(PyInterface), /* tp_basicsize */
0, /* tp_itemsize */
(destructor)PyInterface::dealloc, /* tp_dealloc */
0, /* tp_print */
0, /* tp_getattr */
0, /* tp_setattr */
0, /* tp_reserved */
0, /* tp_repr */
0, /* tp_as_number */
0, /* tp_as_sequence */
0, /* tp_as_mapping */
0, /* tp_hash */
0, /* tp_call */
0, /* tp_str */
0, /* tp_getattro */
0, /* tp_setattro */
0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT |
Py_TPFLAGS_BASETYPE, /* tp_flags */
"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) {
if (self->intf) if (self->intf)
delete self->intf; delete self->intf;
Py_TYPE(self)->tp_free((PyObject*) self); Py_TYPE(self)->tp_free((PyObject*) self);
} }
const char* pyDocInterfaceRegisterSource =
"register_source(source, real_idx, imag_idx)\n"
"Register a source with this interface, causing it to use values received from "
"this interface as its current or voltage value.\n"
"\n"
":param source: The ``ExternalCurrentSource`` or ``ExternalVoltageSource`` to be registered.\n"
":param real_idx: Index of the real part of the current or voltage.\n"
":param imag_idx: Index of the imaginary part of the current or voltage.\n";
PyObject* PyInterface::registerSource(PyObject* self, PyObject* args) { PyObject* PyInterface::registerSource(PyObject* self, PyObject* args) {
PyObject *obj; PyObject *obj;
int realIdx, imagIdx; int realIdx, imagIdx;
...@@ -75,6 +44,13 @@ PyObject* PyInterface::registerSource(PyObject* self, PyObject* args) { ...@@ -75,6 +44,13 @@ PyObject* PyInterface::registerSource(PyObject* self, PyObject* args) {
return Py_None; return Py_None;
} }
const char* pyDocInterfaceExportCurrent =
"export_current(comp, real_idx, imag_idx)\n"
"Register a current to be written to this interface after every timestep.\n"
"\n"
":param comp: The current flowing through this `Component` is exported. Note that only component types for which this curcent can easily be determined may be passed here.\n"
":param real_idx: Index where the real part of the current is written.\n"
":param imag_idx: Index where the imaginary part of the current is written.\n";
PyObject* PyInterface::exportCurrent(PyObject* self, PyObject* args) { PyObject* PyInterface::exportCurrent(PyObject* self, PyObject* args) {
int realIdx, imagIdx; int realIdx, imagIdx;
PyObject* obj; PyObject* obj;
...@@ -92,6 +68,16 @@ PyObject* PyInterface::exportCurrent(PyObject* self, PyObject* args) { ...@@ -92,6 +68,16 @@ PyObject* PyInterface::exportCurrent(PyObject* self, PyObject* args) {
return Py_None; return Py_None;
} }
const char* pyDocInterfaceExportVoltage =
"export_voltage(from, to, real_idx, imag_idx)"
"Register a voltage between two nodes to be written to this interface after every timestep.\n"
"\n"
":param from: Number of the positive node of the voltage to be exported. The same "
"numbering systems as for the component constructors is used, e.g. 0 is the "
"ground, and indices for other nodes start at 1.\n"
":param to: Number of the negative node of the voltage to be exported.\n"
":param real_idx: Index where the real part of the voltage is written.\n"
":param imag_idx: Index where the imaginary part of the voltage is written.\n";
PyObject* PyInterface::exportVoltage(PyObject* self, PyObject* args) { PyObject* PyInterface::exportVoltage(PyObject* self, PyObject* args) {
int from, to, realIdx, imagIdx; int from, to, realIdx, imagIdx;
PyInterface* pyIntf = (PyInterface*) self; PyInterface* pyIntf = (PyInterface*) self;
...@@ -103,7 +89,26 @@ PyObject* PyInterface::exportVoltage(PyObject* self, PyObject* args) { ...@@ -103,7 +89,26 @@ PyObject* PyInterface::exportVoltage(PyObject* self, PyObject* args) {
return Py_None; return Py_None;
} }
PyObject* DPsim::pyShmemInterface(PyObject *self, PyObject *args, PyObject *kwds) { const char* DPsim::pyDocOpenShmemInterface =
"open_shmem_interface(wname, rname, queuelen=512, samplelen=64, polling=False)\n"
"Opens a set of shared memory regions to use as an interface for communication. The communication type / format of VILLASNode's shmem node is used; see its documentation for more information on the internals.\n"
"\n"
"For this interface type, the indices passed to the methods specify the indices "
"in the array of values that is passed in each timestep. The sending program "
"must be configured to use the same indices as well.\n"
"\n"
":param wname: Name of the POSIX shared memory object where values are written. "
"Must start with a ``/``.\n"
":param rname: Name of the POSIX shared memory object where values are read from. "
"Must start with a ``/``.\n"
":param queuelen: Length of the outgoing queue as an integer number of samples.\n"
":param samplelen: Number of values that are passed as a \"sample\" each timestep. "
"Must be greater or equal to the maximum index passed to the export methods.\n"
":param polling: If True, no POSIX CV will be used to signal writes to the "
"interface, meaning that polling will have to be used. This may increase "
"performance at the cost of wasted CPU time.\n"
":returns: A new `Interface` object.\n";
PyObject* DPsim::pyOpenShmemInterface(PyObject *self, PyObject *args, PyObject *kwds) {
static char *kwlist[] = {"wname", "rname", "queuelen", "samplelen", "polling", nullptr}; static char *kwlist[] = {"wname", "rname", "queuelen", "samplelen", "polling", nullptr};
struct shmem_conf conf; struct shmem_conf conf;
const char *wname, *rname; const char *wname, *rname;
...@@ -119,3 +124,47 @@ PyObject* DPsim::pyShmemInterface(PyObject *self, PyObject *args, PyObject *kwds ...@@ -119,3 +124,47 @@ PyObject* DPsim::pyShmemInterface(PyObject *self, PyObject *args, PyObject *kwds
pyIntf->intf = new ShmemInterface(wname, rname, &conf); pyIntf->intf = new ShmemInterface(wname, rname, &conf);
return (PyObject*) pyIntf; return (PyObject*) pyIntf;
} }
static PyMethodDef PyInterface_methods[] = {
{"export_current", PyInterface::exportCurrent, METH_VARARGS, pyDocInterfaceExportCurrent},
{"export_voltage", PyInterface::exportVoltage, METH_VARARGS, pyDocInterfaceExportVoltage},
{"register_source", PyInterface::registerSource, METH_VARARGS, pyDocInterfaceRegisterSource},
{0},
};
const char* pyDocInterface =
"A connection to an external program, using which simulation data is exchanged.\n"
"\n"
"Currently, only an interface using POSIX shared memory is implemented (see `open_shmem_interface`).\n";
PyTypeObject DPsim::PyInterfaceType = {
PyVarObject_HEAD_INIT(NULL, 0)
"dpsim.Interface", /* tp_name */
sizeof(PyInterface), /* tp_basicsize */
0, /* tp_itemsize */
(destructor)PyInterface::dealloc, /* tp_dealloc */
0, /* tp_print */
0, /* tp_getattr */
0, /* tp_setattr */
0, /* tp_reserved */
0, /* tp_repr */
0, /* tp_as_number */
0, /* tp_as_sequence */
0, /* tp_as_mapping */
0, /* tp_hash */
0, /* tp_call */
0, /* tp_str */
0, /* tp_getattro */
0, /* tp_setattro */
0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT |
Py_TPFLAGS_BASETYPE, /* tp_flags */
pyDocInterface, /* tp_doc */
0, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
0, /* tp_iter */
0, /* tp_iternext */
PyInterface_methods, /* tp_methods */
};
...@@ -22,7 +22,8 @@ namespace DPsim { ...@@ -22,7 +22,8 @@ namespace DPsim {
extern PyTypeObject PyInterfaceType; extern PyTypeObject PyInterfaceType;
#ifdef __linux__ #ifdef __linux__
PyObject* pyShmemInterface(PyObject *self, PyObject *args, PyObject *kwds); extern const char* pyDocOpenShmemInterface;
PyObject* pyOpenShmemInterface(PyObject *self, PyObject *args, PyObject *kwds);
#endif #endif
}; };
......
...@@ -9,11 +9,11 @@ using namespace DPsim; ...@@ -9,11 +9,11 @@ using namespace DPsim;
PyMethodDef DPsim::pyModuleMethods[] = { PyMethodDef DPsim::pyModuleMethods[] = {
{"load_cim", pyLoadCim, METH_VARARGS, pyDocLoadCim}, {"load_cim", pyLoadCim, METH_VARARGS, pyDocLoadCim},
{"open_shmem_interface", (PyCFunction)pyOpenShmemInterface, METH_VARARGS|METH_KEYWORDS, pyDocOpenShmemInterface},
{"ExternalCurrentSource", pyExternalCurrentSource, METH_VARARGS, "Construct a new external current source."}, {"ExternalCurrentSource", pyExternalCurrentSource, METH_VARARGS, "Construct a new external current source."},
{"ExternalVoltageSource", pyExternalVoltageSource, METH_VARARGS, "Construct a new external voltage source."}, {"ExternalVoltageSource", pyExternalVoltageSource, METH_VARARGS, "Construct a new external voltage source."},
{"Inductor", pyInductor, METH_VARARGS, "Construct a new inductor."}, {"Inductor", pyInductor, METH_VARARGS, "Construct a new inductor."},
{"LinearResistor", pyLinearResistor, METH_VARARGS, "Construct a new resistor."}, {"LinearResistor", pyLinearResistor, METH_VARARGS, "Construct a new resistor."},
{"ShmemInterface", (PyCFunction)pyShmemInterface, METH_VARARGS|METH_KEYWORDS, "Construct an Interface that communicates via POSIX shared memory."},
{"VoltSourceRes", pyVoltSourceRes, METH_VARARGS, "Construct a new voltage source with a resistance."}, {"VoltSourceRes", pyVoltSourceRes, METH_VARARGS, "Construct a new voltage source with a resistance."},
{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