Commit 0ac69ef5 authored by Georg Martin Reinke's avatar Georg Martin Reinke
Browse files

add string attributes; remove old BaseComponent constructors


Former-commit-id: f9dcc3ea
parent f4c1e8e7
...@@ -13,6 +13,7 @@ namespace DPsim { ...@@ -13,6 +13,7 @@ namespace DPsim {
enum AttrType { enum AttrType {
AttrReal, AttrReal,
AttrInt, AttrInt,
AttrString, // value should be *std::string, not *char!
}; };
struct CompAttr { struct CompAttr {
...@@ -41,10 +42,24 @@ namespace DPsim { ...@@ -41,10 +42,24 @@ namespace DPsim {
public: public:
BaseComponent() { } BaseComponent() { }
BaseComponent(std::string name) { this->mName = name; } BaseComponent(std::string name, int src, int dest) {
BaseComponent(int src, int dest) { this->mNode1 = src - 1; this->mNode2 = dest - 1; } this->mName = name;
BaseComponent(std::string name, int src, int dest) { this->mName = name; this->mNode1 = src - 1; this->mNode2 = dest - 1; } this->mNode1 = src - 1;
BaseComponent(std::string name, int node1, int node2, int node3) { this->mName = name; this->mNode1 = node1 - 1; this->mNode2 = node2 - 1; this->mNode3 = node3 - 1; } this->mNode2 = dest - 1;
attrMap["name"] = {AttrString, &this->mName};
attrMap["node1"] = {AttrInt, &this->mNode1};
attrMap["node2"] = {AttrInt, &this->mNode2};
}
BaseComponent(std::string name, int node1, int node2, int node3) {
this->mName = name;
this->mNode1 = node1 - 1;
this->mNode2 = node2 - 1;
this->mNode3 = node3 - 1;
attrMap["name"] = {AttrString, &this->mName};
attrMap["node1"] = {AttrInt, &this->mNode1};
attrMap["node2"] = {AttrInt, &this->mNode2};
attrMap["node3"] = {AttrInt, &this->mNode3};
}
virtual ~BaseComponent() { } virtual ~BaseComponent() { }
/// get value of node1 /// get value of node1
......
...@@ -2,8 +2,7 @@ ...@@ -2,8 +2,7 @@
using namespace DPsim; using namespace DPsim;
VoltSourceRes::VoltSourceRes(std::string name, int src, int dest, Real voltage, Real phase, Real resistance) : BaseComponent(src, dest) { VoltSourceRes::VoltSourceRes(std::string name, int src, int dest, Real voltage, Real phase, Real resistance) : BaseComponent(name, src, dest) {
this->mName = name;
this->mVoltageDiffr = voltage*cos(phase); this->mVoltageDiffr = voltage*cos(phase);
this->mVoltageDiffi = voltage*sin(phase); this->mVoltageDiffi = voltage*sin(phase);
this->mResistance = resistance; this->mResistance = resistance;
......
...@@ -2,8 +2,7 @@ ...@@ -2,8 +2,7 @@
using namespace DPsim; using namespace DPsim;
VoltSourceResFreq::VoltSourceResFreq(std::string name, int src, int dest, Real voltage, Real phase, Real resistance, Real omegaSource, Real switchTime, Real rampTime) : BaseComponent(src, dest) { VoltSourceResFreq::VoltSourceResFreq(std::string name, int src, int dest, Real voltage, Real phase, Real resistance, Real omegaSource, Real switchTime, Real rampTime) : BaseComponent(name, src, dest) {
mName = name;
mResistance = resistance; mResistance = resistance;
mConductance = 1. / resistance; mConductance = 1. / resistance;
mVoltageAmp = voltage; mVoltageAmp = voltage;
......
...@@ -2,8 +2,7 @@ ...@@ -2,8 +2,7 @@
using namespace DPsim; using namespace DPsim;
VoltSourceResFreqEMT::VoltSourceResFreqEMT(std::string name, int src, int dest, Real voltage, Real phase, Real resistance, Real omegaSource, Real switchTime, Real rampTime) : BaseComponent(src, dest) { VoltSourceResFreqEMT::VoltSourceResFreqEMT(std::string name, int src, int dest, Real voltage, Real phase, Real resistance, Real omegaSource, Real switchTime, Real rampTime) : BaseComponent(name, src, dest) {
mName = name;
mResistance = resistance; mResistance = resistance;
mConductance = 1. / resistance; mConductance = 1. / resistance;
mVoltageAmp = voltage; mVoltageAmp = voltage;
......
...@@ -20,7 +20,7 @@ PyTypeObject DPsim::PyComponentType = { ...@@ -20,7 +20,7 @@ PyTypeObject DPsim::PyComponentType = {
0, /* tp_as_mapping */ 0, /* tp_as_mapping */
0, /* tp_hash */ 0, /* tp_hash */
0, /* tp_call */ 0, /* tp_call */
0, /* tp_str */ (reprfunc)PyComponent::str, /* tp_str */
0, /* tp_getattro */ 0, /* tp_getattro */
0, /* tp_setattro */ 0, /* tp_setattro */
0, /* tp_as_buffer */ 0, /* tp_as_buffer */
...@@ -59,62 +59,10 @@ void PyComponent::dealloc(PyComponent* self) { ...@@ -59,62 +59,10 @@ void PyComponent::dealloc(PyComponent* self) {
Py_TYPE(self)->tp_free((PyObject*)self); Py_TYPE(self)->tp_free((PyObject*)self);
} }
bool DPsim::compsFromPython(PyObject* list, std::vector<BaseComponent*>& comps) { PyObject* PyComponent::str(PyComponent* self) {
if (!PyList_Check(list)) if (!self->comp)
return false; return PyUnicode_FromString("<unitialized Component>");
for (int i = 0; i < PyList_Size(list); i++) { return PyUnicode_FromString(self->comp->getName().c_str());
PyObject* obj = PyList_GetItem(list, i);
if (!PyObject_TypeCheck(obj, &PyComponentType)) {
comps.clear();
return false;
}
PyComponent* pyComp = (PyComponent*) obj;
comps.push_back(pyComp->comp);
}
return true;
}
PyObject* DPsim::pyLoadCim(PyObject* self, PyObject* args) {
double frequency = 50;
PyObject *list;
PyBytesObject *filename;
CIMReader *reader;
if (PyArg_ParseTuple(args, "O&|d", PyUnicode_FSConverter, &filename, &frequency)) {
reader = new CIMReader(2*PI*frequency);
reader->addFile(PyBytes_AsString((PyObject*) filename));
Py_DECREF(filename);
} else if (PyArg_ParseTuple(args, "O|d", &list, &frequency)) {
PyErr_Clear();
if (!PyList_Check(list)) {
PyErr_SetString(PyExc_TypeError, "First argument must be filename or list of filenames");
return nullptr;
}
reader = new CIMReader(2*PI*frequency);
for (int i = 0; i < PyList_Size(list); i++) {
if (!PyUnicode_FSConverter(PyList_GetItem(list, i), &filename)) {
delete reader;
PyErr_SetString(PyExc_TypeError, "First argument must be filename or list of filenames");
return nullptr;
}
reader->addFile(PyBytes_AsString((PyObject*) filename));
Py_DECREF(filename);
}
} else {
PyErr_SetString(PyExc_TypeError, "First argument must be filename or list of filenames");
return nullptr;
}
reader->parseFiles();
std::vector<BaseComponent*> comps = reader->getComponents();
list = PyList_New(comps.size());
for (int i = 0; i < comps.size(); i++) {
PyComponent* pyComp = PyObject_New(PyComponent, &PyComponentType);
PyObject_Init((PyObject*) pyComp, &PyComponentType);
pyComp->comp = comps[i];
PyList_SET_ITEM(list, i, (PyObject*) pyComp);
}
delete reader;
return list;
} }
PyObject* PyComponent::getattr(PyComponent* self, char* name) { PyObject* PyComponent::getattr(PyComponent* self, char* name) {
...@@ -173,3 +121,61 @@ int PyComponent::setattr(PyComponent* self, char* name, PyObject *v) { ...@@ -173,3 +121,61 @@ int PyComponent::setattr(PyComponent* self, char* name, PyObject *v) {
} }
return 0; return 0;
} }
bool DPsim::compsFromPython(PyObject* list, std::vector<BaseComponent*>& comps) {
if (!PyList_Check(list))
return false;
for (int i = 0; i < PyList_Size(list); i++) {
PyObject* obj = PyList_GetItem(list, i);
if (!PyObject_TypeCheck(obj, &PyComponentType)) {
comps.clear();
return false;
}
PyComponent* pyComp = (PyComponent*) obj;
comps.push_back(pyComp->comp);
}
return true;
}
PyObject* DPsim::pyLoadCim(PyObject* self, PyObject* args) {
double frequency = 50;
PyObject *list;
PyBytesObject *filename;
CIMReader *reader;
if (PyArg_ParseTuple(args, "O&|d", PyUnicode_FSConverter, &filename, &frequency)) {
reader = new CIMReader(2*PI*frequency);
reader->addFile(PyBytes_AsString((PyObject*) filename));
Py_DECREF(filename);
} else if (PyArg_ParseTuple(args, "O|d", &list, &frequency)) {
PyErr_Clear();
if (!PyList_Check(list)) {
PyErr_SetString(PyExc_TypeError, "First argument must be filename or list of filenames");
return nullptr;
}
reader = new CIMReader(2*PI*frequency);
for (int i = 0; i < PyList_Size(list); i++) {
if (!PyUnicode_FSConverter(PyList_GetItem(list, i), &filename)) {
delete reader;
PyErr_SetString(PyExc_TypeError, "First argument must be filename or list of filenames");
return nullptr;
}
reader->addFile(PyBytes_AsString((PyObject*) filename));
Py_DECREF(filename);
}
} else {
PyErr_SetString(PyExc_TypeError, "First argument must be filename or list of filenames");
return nullptr;
}
reader->parseFiles();
std::vector<BaseComponent*> comps = reader->getComponents();
list = PyList_New(comps.size());
for (int i = 0; i < comps.size(); i++) {
PyComponent* pyComp = PyObject_New(PyComponent, &PyComponentType);
PyObject_Init((PyObject*) pyComp, &PyComponentType);
pyComp->comp = comps[i];
PyList_SET_ITEM(list, i, (PyObject*) pyComp);
}
delete reader;
return list;
}
...@@ -17,6 +17,8 @@ namespace DPsim { ...@@ -17,6 +17,8 @@ namespace DPsim {
static PyObject* newfunc(PyTypeObject* type, PyObject *args, PyObject *kwds); static PyObject* newfunc(PyTypeObject* type, PyObject *args, PyObject *kwds);
static void dealloc(PyComponent*); static void dealloc(PyComponent*);
static PyObject* str(PyComponent* self);
static PyObject* getattr(PyComponent* self, char* name); static PyObject* getattr(PyComponent* self, char* name);
static int setattr(PyComponent *self, char* name, PyObject *v); static int setattr(PyComponent *self, char* name, PyObject *v);
}; };
......
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