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
dd95729e
Commit
dd95729e
authored
May 04, 2017
by
Georg Martin Reinke
Browse files
villas interface: read/write values as phasors
Former-commit-id:
9667f481
parent
033dc796
Changes
8
Hide whitespace changes
Inline
Side-by-side
Source/Components/ExternalCurrentSource.cpp
View file @
dd95729e
...
...
@@ -2,11 +2,10 @@
using
namespace
DPsim
;
ExternalCurrentSource
::
ExternalCurrentSource
(
std
::
string
name
,
int
src
,
int
dest
,
Real
current
,
Real
phase
)
:
CurrentSource
(
name
,
src
,
dest
,
current
,
phase
)
{
this
->
mPhase
=
phase
;
ExternalCurrentSource
::
ExternalCurrentSource
(
std
::
string
name
,
int
src
,
int
dest
)
:
CurrentSource
(
name
,
src
,
dest
,
0
,
0
)
{
}
void
ExternalCurrentSource
::
setCurrent
(
Real
current
)
{
this
->
currentr
=
current
*
cos
(
this
->
mPhase
)
;
this
->
currenti
=
current
*
sin
(
this
->
mPhase
)
;
void
ExternalCurrentSource
::
setCurrent
(
Real
real
,
Real
imag
)
{
this
->
currentr
=
real
;
this
->
currenti
=
imag
;
}
Source/Components/ExternalCurrentSource.h
View file @
dd95729e
...
...
@@ -12,8 +12,8 @@ namespace DPsim {
public:
ExternalCurrentSource
()
{};
ExternalCurrentSource
(
std
::
string
name
,
int
src
,
int
dest
,
Real
current
,
Real
phase
);
ExternalCurrentSource
(
std
::
string
name
,
int
src
,
int
dest
);
void
setCurrent
(
Real
current
);
void
setCurrent
(
Real
real
,
Real
imag
);
};
}
Source/Components/ExternalVoltageSource.cpp
View file @
dd95729e
...
...
@@ -2,11 +2,10 @@
using
namespace
DPsim
;
ExternalVoltageSource
::
ExternalVoltageSource
(
std
::
string
name
,
int
src
,
int
dest
,
Real
voltage
,
Real
phase
,
int
num
)
:
IdealVoltageSource
(
name
,
src
,
dest
,
voltage
,
phase
,
num
)
{
this
->
mPhase
=
phase
;
ExternalVoltageSource
::
ExternalVoltageSource
(
std
::
string
name
,
int
src
,
int
dest
,
int
num
)
:
IdealVoltageSource
(
name
,
src
,
dest
,
0
,
0
,
num
)
{
}
void
ExternalVoltageSource
::
setVoltage
(
Real
volt
ag
e
)
{
this
->
mVoltageDiffr
=
voltage
*
cos
(
mPhase
)
;
this
->
mVoltageDiffi
=
voltage
*
sin
(
mPhase
)
;
void
ExternalVoltageSource
::
setVoltage
(
Real
real
,
Real
im
ag
)
{
this
->
mVoltageDiffr
=
real
;
this
->
mVoltageDiffi
=
imag
;
}
Source/Components/ExternalVoltageSource.h
View file @
dd95729e
...
...
@@ -12,8 +12,8 @@ namespace DPsim {
public:
ExternalVoltageSource
()
{};
ExternalVoltageSource
(
std
::
string
name
,
int
src
,
int
dest
,
Real
voltage
,
Real
phase
,
int
num
);
ExternalVoltageSource
(
std
::
string
name
,
int
src
,
int
dest
,
int
num
);
void
setVoltage
(
Real
volt
ag
e
);
void
setVoltage
(
Real
real
,
Real
im
ag
);
};
}
Source/Examples/VillasTest.cpp
View file @
dd95729e
...
...
@@ -11,13 +11,13 @@ void DPsim::villasExample()
Logger
log
,
llog
,
rlog
;
std
::
vector
<
BaseComponent
*>
comps
;
ExternalCurrentSource
*
ecs
=
new
ExternalCurrentSource
(
"i1"
,
1
,
0
,
0
,
0
);
ExternalCurrentSource
*
ecs
=
new
ExternalCurrentSource
(
"i1"
,
1
,
0
);
comps
.
push_back
(
ecs
);
comps
.
push_back
(
new
LinearResistor
(
"r1"
,
1
,
2
,
1
));
comps
.
push_back
(
new
LinearResistor
(
"r2"
,
2
,
0
,
1
));
VillasInterface
*
villas
=
new
VillasInterface
(
"/villas1"
);
villas
->
registerCurrentSource
(
ecs
,
0
);
villas
->
registerExportedVoltage
(
{
1
,
2
}
,
0
);
villas
->
registerCurrentSource
(
ecs
,
0
,
1
);
villas
->
registerExportedVoltage
(
1
,
2
,
0
,
1
);
// Set up simulation
Real
timeStep
=
0.01
;
...
...
Source/ExternalInterface.cpp
View file @
dd95729e
...
...
@@ -3,23 +3,14 @@
using
namespace
DPsim
;
void
ExternalInterface
::
registerVoltageSource
(
ExternalVoltageSource
*
evs
,
int
num
)
{
int
sz
=
mExtComponents
.
size
();
if
(
num
>=
sz
)
mExtComponents
.
resize
(
num
+
1
,
NULL
);
mExtComponents
[
num
]
=
evs
;
void
ExternalInterface
::
registerVoltageSource
(
ExternalVoltageSource
*
evs
,
Int
realIdx
,
Int
imagIdx
)
{
mExtComponents
.
push_back
({
evs
,
realIdx
,
imagIdx
});
}
void
ExternalInterface
::
registerCurrentSource
(
ExternalCurrentSource
*
ecs
,
int
num
)
{
int
sz
=
mExtComponents
.
size
();
if
(
num
>=
sz
)
mExtComponents
.
resize
(
num
+
1
,
NULL
);
mExtComponents
[
num
]
=
ecs
;
void
ExternalInterface
::
registerCurrentSource
(
ExternalCurrentSource
*
ecs
,
Int
realIdx
,
Int
imagIdx
)
{
mExtComponents
.
push_back
({
ecs
,
realIdx
,
imagIdx
});
}
void
ExternalInterface
::
registerExportedVoltage
(
VoltDiff
vd
,
int
num
)
{
int
sz
=
mExportedVoltages
.
size
();
if
(
num
>=
sz
)
mExportedVoltages
.
resize
(
num
+
1
,
{
-
1
,
-
1
});
mExportedVoltages
[
num
]
=
vd
;
void
ExternalInterface
::
registerExportedVoltage
(
Int
from
,
Int
to
,
Int
realIdx
,
Int
imagIdx
)
{
mExportedVoltages
.
push_back
({
from
,
to
,
realIdx
,
imagIdx
});
}
Source/ExternalInterface.h
View file @
dd95729e
...
...
@@ -7,8 +7,16 @@
namespace
DPsim
{
struct
VoltDiff
{
int
from
;
int
to
;
Int
from
;
Int
to
;
Int
realIdx
;
Int
imagIdx
;
};
struct
ExtComponent
{
BaseComponent
*
comp
;
Int
realIdx
;
Int
imagIdx
;
};
/** Abstract base class for interfacing the simulator with other data sources or sinks.
* After an ExternalInterface is created, components that should use values
...
...
@@ -19,12 +27,12 @@ namespace DPsim {
*/
class
ExternalInterface
{
protected:
std
::
vector
<
Base
Component
*
>
mExtComponents
;
std
::
vector
<
Ext
Component
>
mExtComponents
;
std
::
vector
<
VoltDiff
>
mExportedVoltages
;
public:
void
registerVoltageSource
(
ExternalVoltageSource
*
evs
,
i
nt
num
);
void
registerCurrentSource
(
ExternalCurrentSource
*
ecs
,
i
nt
num
);
void
registerExportedVoltage
(
VoltDiff
vd
,
i
nt
num
);
void
registerVoltageSource
(
ExternalVoltageSource
*
evs
,
I
nt
realIdx
,
Int
imagIdx
);
void
registerCurrentSource
(
ExternalCurrentSource
*
ecs
,
I
nt
realIdx
,
Int
imagIdx
);
void
registerExportedVoltage
(
Int
from
,
Int
to
,
I
nt
realIdx
,
Int
imagIdx
);
virtual
void
readValues
()
=
0
;
virtual
void
writeValues
(
SystemModel
&
model
)
=
0
;
virtual
~
ExternalInterface
()
{};
...
...
Source/VillasInterface.cpp
View file @
dd95729e
...
...
@@ -31,45 +31,50 @@ void VillasInterface::readValues() {
std
::
cerr
<<
"Fatal error: failed to read sample from shmem interface"
<<
std
::
endl
;
std
::
exit
(
1
);
}
int
sz
=
mExtComponents
.
size
();
if
(
sample
->
length
<
mExtComponents
.
size
())
{
std
::
cerr
<<
"Warning: missing data in received sample"
<<
std
::
endl
;
sz
=
sample
->
length
;
}
for
(
int
i
=
0
;
i
<
sz
;
i
++
)
{
for
(
ExtComponent
extComp
:
mExtComponents
)
{
if
(
extComp
.
realIdx
>=
sample
->
length
||
extComp
.
imagIdx
>=
sample
->
length
)
{
std
::
cerr
<<
"Fatal error: incomplete data received from shmem interface"
<<
std
::
endl
;
std
::
exit
(
1
);
}
// TODO integer format?
if
(
i
<
mExtComponents
.
size
())
{
ExternalVoltageSource
*
evs
=
dynamic_cast
<
ExternalVoltageSource
*>
(
mExtComponents
[
i
]);
if
(
evs
)
evs
->
setVoltage
(
sample
->
data
[
i
].
f
);
Real
real
=
sample
->
data
[
extComp
.
realIdx
].
f
;
Real
imag
=
sample
->
data
[
extComp
.
imagIdx
].
f
;
ExternalCurrentSource
*
ecs
=
dynamic_cast
<
ExternalCurrentSource
*>
(
mExtComponents
[
i
]);
if
(
ecs
)
ecs
->
setCurrent
(
sample
->
data
[
i
].
f
);
}
sample_put
(
sample
);
ExternalCurrentSource
*
ecs
=
dynamic_cast
<
ExternalCurrentSource
*>
(
extComp
.
comp
);
if
(
ecs
)
ecs
->
setCurrent
(
real
,
imag
);
ExternalVoltageSource
*
evs
=
dynamic_cast
<
ExternalVoltageSource
*>
(
extComp
.
comp
);
if
(
evs
)
evs
->
setVoltage
(
real
,
imag
);
}
}
void
VillasInterface
::
writeValues
(
SystemModel
&
model
)
{
struct
sample
*
sample
;
sample_alloc
(
&
mShmem
->
pool
,
&
sample
,
1
);
int
len
=
mExportedVoltages
.
size
();
if
(
sample
->
capacity
<
len
)
{
std
::
cerr
<<
"struct sample returned from pool has to small capacity"
<<
std
::
endl
;
len
=
sample
->
capacity
;
}
sample
->
length
=
len
;
Matrix
lvect
=
model
.
getLeftSideVector
();
for
(
int
i
=
0
;
i
<
len
;
i
++
)
{
Real
f
=
0.0
f
;
VoltDiff
vd
=
mExportedVoltages
[
i
];
if
(
vd
.
from
>
0
)
f
+=
lvect
(
vd
.
from
-
1
,
0
);
if
(
vd
.
to
>
0
)
f
-=
lvect
(
vd
.
to
-
1
,
0
);
sample
->
data
[
i
].
f
=
f
;
Int
len
=
0
;
for
(
auto
vd
:
mExportedVoltages
)
{
Real
real
=
0
,
imag
=
0
;
if
(
vd
.
from
>
0
)
{
real
+=
model
.
getRealFromLeftSideVector
(
vd
.
from
-
1
);
imag
+=
model
.
getImagFromLeftSideVector
(
vd
.
from
-
1
);
}
if
(
vd
.
to
>
0
)
{
real
-=
model
.
getRealFromLeftSideVector
(
vd
.
to
-
1
);
imag
-=
model
.
getImagFromLeftSideVector
(
vd
.
to
-
1
);
}
if
(
vd
.
realIdx
>=
sample
->
capacity
||
vd
.
imagIdx
>=
sample
->
capacity
)
{
std
::
cerr
<<
"fatal error: not enough space in allocated struct sample"
<<
std
::
endl
;
std
::
exit
(
1
);
}
sample
->
data
[
vd
.
realIdx
].
f
=
real
;
sample
->
data
[
vd
.
imagIdx
].
f
=
imag
;
if
(
vd
.
realIdx
>
len
)
len
=
vd
.
realIdx
;
if
(
vd
.
imagIdx
>
len
)
len
=
vd
.
imagIdx
;
}
sample
->
length
=
len
+
1
;
sample
->
sequence
=
mSeq
++
;
clock_gettime
(
CLOCK_REALTIME
,
&
sample
->
ts
.
origin
);
int
ret
=
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