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

add string attributes; remove old BaseComponent constructors

parent 8d51db66
......@@ -13,6 +13,7 @@ namespace DPsim {
enum AttrType {
AttrReal,
AttrInt,
AttrString, // value should be *std::string, not *char!
};
struct CompAttr {
......@@ -41,10 +42,24 @@ namespace DPsim {
public:
BaseComponent() { }
BaseComponent(std::string name) { this->mName = name; }
BaseComponent(int src, int dest) { this->mNode1 = src - 1; this->mNode2 = dest - 1; }
BaseComponent(std::string name, int src, int dest) { this->mName = name; this->mNode1 = src - 1; this->mNode2 = dest - 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; }
BaseComponent(std::string name, int src, int dest) {
this->mName = name;
this->mNode1 = src - 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() { }
/// get value of node1
......
......@@ -2,8 +2,7 @@
using namespace DPsim;
VoltSourceRes::VoltSourceRes(std::string name, int src, int dest, Real voltage, Real phase, Real resistance) : BaseComponent(src, dest) {
this->mName = name;
VoltSourceRes::VoltSourceRes(std::string name, int src, int dest, Real voltage, Real phase, Real resistance) : BaseComponent(name, src, dest) {
this->mVoltageDiffr = voltage*cos(phase);
this->mVoltageDiffi = voltage*sin(phase);
this->mResistance = resistance;
......
......@@ -2,8 +2,7 @@
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) {
mName = name;
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) {
mResistance = resistance;
mConductance = 1. / resistance;
mVoltageAmp = voltage;
......
......@@ -2,8 +2,7 @@
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) {
mName = name;
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) {
mResistance = resistance;
mConductance = 1. / resistance;
mVoltageAmp = voltage;
......
......@@ -20,7 +20,7 @@ PyTypeObject DPsim::PyComponentType = {
0, /* tp_as_mapping */
0, /* tp_hash */
0, /* tp_call */
0, /* tp_str */
(reprfunc)PyComponent::str, /* tp_str */
0, /* tp_getattro */
0, /* tp_setattro */
0, /* tp_as_buffer */
......@@ -59,62 +59,10 @@ void PyComponent::dealloc(PyComponent* self) {
Py_TYPE(self)->tp_free((PyObject*)self);
}
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;
PyObject* PyComponent::str(PyComponent* self) {
if (!self->comp)
return PyUnicode_FromString("<unitialized Component>");
return PyUnicode_FromString(self->comp->getName().c_str());
}
PyObject* PyComponent::getattr(PyComponent* self, char* name) {
......@@ -173,3 +121,61 @@ int PyComponent::setattr(PyComponent* self, char* name, PyObject *v) {
}
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 {
static PyObject* newfunc(PyTypeObject* type, PyObject *args, PyObject *kwds);
static void dealloc(PyComponent*);
static PyObject* str(PyComponent* self);
static PyObject* getattr(PyComponent* self, char* name);
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