Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
ACS
Public
Power System Simulation and Optimization
DPsim
DPsim
Commits
df9233b3
Commit
df9233b3
authored
Sep 20, 2017
by
Georg Martin Reinke
Browse files
add Python functions to create/register external sources
Former-commit-id:
96bd3101
parent
716667a0
Changes
5
Hide whitespace changes
Inline
Side-by-side
Source/PyComponent.cpp
View file @
df9233b3
#include
"PyComponent.h"
#include
"CIMReader.h"
#include
"Components/ExternalCurrentSource.h"
#include
"Components/ExternalVoltageSource.h"
using
namespace
DPsim
;
...
...
@@ -152,6 +154,32 @@ bool DPsim::compsFromPython(PyObject* list, std::vector<BaseComponent*>& comps)
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
)
{
double
frequency
=
50
;
PyObject
*
list
;
...
...
Source/PyComponent.h
View file @
df9233b3
...
...
@@ -26,6 +26,9 @@ namespace DPsim {
extern
PyTypeObject
PyComponentType
;
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
);
};
...
...
Source/PyInterface.cpp
View file @
df9233b3
#include
"PyComponent.h"
#include
"PyInterface.h"
#include
"ShmemInterface.h"
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
=
{
PyVarObject_HEAD_INIT
(
NULL
,
0
)
"dpsim.Interface"
,
/* tp_name */
...
...
@@ -27,6 +33,13 @@ PyTypeObject DPsim::PyInterfaceType = {
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
)
{
...
...
@@ -35,7 +48,32 @@ void PyInterface::dealloc(PyInterface* 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
};
struct
shmem_conf
conf
;
const
char
*
wname
,
*
rname
;
...
...
Source/PyInterface.h
View file @
df9233b3
...
...
@@ -13,12 +13,14 @@ namespace DPsim {
ExternalInterface
*
intf
;
static
void
dealloc
(
PyInterface
*
);
static
PyObject
*
registerSource
(
PyObject
*
self
,
PyObject
*
args
);
};
extern
PyTypeObject
PyInterfaceType
;
#ifdef __linux__
PyObject
*
pyShmemInterface
(
PyObject
*
self
,
PyObject
*
args
);
PyObject
*
pyShmemInterface
(
PyObject
*
self
,
PyObject
*
args
,
PyObject
*
kwds
);
#endif
};
...
...
Source/PyModule.cpp
View file @
df9233b3
...
...
@@ -9,6 +9,8 @@ using namespace DPsim;
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."
},
{
"ShmemInterface"
,
(
PyCFunction
)
pyShmemInterface
,
METH_VARARGS
|
METH_KEYWORDS
,
"Construct an Interface that communicates via POSIX shared memory."
},
{
0
}
};
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment