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

add more component constructors and a basic Python example


Former-commit-id: d92a8fd9
parent df9233b3
#!/usr/bin/python3
import dpsim
import os
def doExample(basename, comps, timestep, duration):
sim = dpsim.Simulation(comps, timestep=timestep, duration=duration,
log="Logs/Log_"+basename+".log", llog="Logs/LeftVectorLog_"+basename+".csv",
rlog="Logs/RightVectorLog_"+basename+".csv")
sim.start()
sim.wait()
def simExample1(timestep=1e-3):
comps = [dpsim.VoltSourceRes("v_in", 1, 0, 10, 1),
dpsim.Inductor("l_1", 1, 2, 0.02),
dpsim.Inductor("l_2", 2, 0, 0.1),
dpsim.Inductor("l_3", 2, 3, 0.05),
dpsim.Inductor("r_2", 3, 0, 2)]
basename = "SimulationExample1_" + str(timestep)
doExample(basename, comps, timestep, 0.3)
if __name__ == '__main__':
os.makedirs("Logs", exist_ok=True)
simExample1()
#include "PyComponent.h"
#include "CIMReader.h"
#include "Components.h"
#include "Components/ExternalCurrentSource.h"
#include "Components/ExternalVoltageSource.h"
......@@ -180,6 +181,46 @@ PyObject* DPsim::pyExternalVoltageSource(PyObject* self, PyObject* args) {
return (PyObject*) pyComp;
}
PyObject* DPsim::pyInductor(PyObject* self, PyObject* args) {
const char *name;
double inductance;
int src, dest;
if (!PyArg_ParseTuple(args, "siid", &name, &src, &dest, &inductance))
return nullptr;
PyComponent *pyComp = PyObject_New(PyComponent, &PyComponentType);
pyComp->comp = new Inductor(name, src, dest, inductance);
return (PyObject*) pyComp;
}
PyObject* DPsim::pyLinearResistor(PyObject* self, PyObject* args) {
const char *name;
double resistance;
int src, dest;
if (!PyArg_ParseTuple(args, "siid", &name, &src, &dest, &resistance))
return nullptr;
PyComponent *pyComp = PyObject_New(PyComponent, &PyComponentType);
pyComp->comp = new LinearResistor(name, src, dest, resistance);
return (PyObject*) pyComp;
}
PyObject* DPsim::pyVoltSourceRes(PyObject* self, PyObject* args) {
const char *name;
double resistance;
int src, dest;
Py_complex voltage;
if (!PyArg_ParseTuple(args, "siiDd", &name, &src, &dest, &voltage, &resistance))
return nullptr;
PyComponent *pyComp = PyObject_New(PyComponent, &PyComponentType);
pyComp->comp = new VoltSourceRes(name, src, dest, Complex(voltage.real, voltage.imag), resistance);
return (PyObject*) pyComp;
}
PyObject* DPsim::pyLoadCim(PyObject* self, PyObject* args) {
double frequency = 50;
PyObject *list;
......
......@@ -27,8 +27,13 @@ namespace DPsim {
bool compsFromPython(PyObject* list, std::vector<BaseComponent*>& comps);
// "Constructors" for the component types
PyObject* pyExternalCurrentSource(PyObject* self, PyObject *args);
PyObject* pyExternalVoltageSource(PyObject* self, PyObject *args);
PyObject* pyInductor(PyObject* self, PyObject *args);
PyObject* pyLinearResistor(PyObject* self, PyObject *args);
PyObject* pyVoltSourceRes(PyObject* self, PyObject *args);
PyObject* pyLoadCim(PyObject* self, PyObject* args);
};
......
......@@ -11,7 +11,10 @@ PyMethodDef DPsim::pyModuleMethods[] = {
{"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."},
{"Inductor", pyInductor, METH_VARARGS, "Construct a new inductor."},
{"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."},
{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