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
033dc796
Commit
033dc796
authored
May 03, 2017
by
Georg Martin Reinke
Browse files
implement sending voltage values back to VILLASnode
Former-commit-id:
dabcba83
parent
6e218cb3
Changes
6
Hide whitespace changes
Inline
Side-by-side
Source/Examples/VillasTest.cpp
View file @
033dc796
...
...
@@ -14,9 +14,10 @@ void DPsim::villasExample()
ExternalCurrentSource
*
ecs
=
new
ExternalCurrentSource
(
"i1"
,
1
,
0
,
0
,
0
);
comps
.
push_back
(
ecs
);
comps
.
push_back
(
new
LinearResistor
(
"r1"
,
1
,
2
,
1
));
comps
.
push_back
(
new
LinearResistor
(
"r
1
"
,
2
,
0
,
1
));
comps
.
push_back
(
new
LinearResistor
(
"r
2
"
,
2
,
0
,
1
));
VillasInterface
*
villas
=
new
VillasInterface
(
"/villas1"
);
villas
->
registerCurrentSource
(
ecs
,
0
);
villas
->
registerExportedVoltage
({
1
,
2
},
0
);
// Set up simulation
Real
timeStep
=
0.01
;
...
...
Source/ExternalInterface.cpp
View file @
033dc796
...
...
@@ -5,21 +5,21 @@ using namespace DPsim;
void
ExternalInterface
::
registerVoltageSource
(
ExternalVoltageSource
*
evs
,
int
num
)
{
int
sz
=
mExtComponents
.
size
();
if
(
num
>=
sz
)
{
mExtComponents
.
reserve
(
num
+
1
);
for
(
int
i
=
sz
;
i
<
num
;
i
++
)
mExtComponents
[
i
]
=
NULL
;
}
if
(
num
>=
sz
)
mExtComponents
.
resize
(
num
+
1
,
NULL
);
mExtComponents
[
num
]
=
evs
;
}
void
ExternalInterface
::
registerCurrentSource
(
ExternalCurrentSource
*
ecs
,
int
num
)
{
int
sz
=
mExtComponents
.
size
();
if
(
num
>=
sz
)
{
mExtComponents
.
reserve
(
num
+
1
);
for
(
int
i
=
sz
;
i
<
num
;
i
++
)
mExtComponents
[
i
]
=
NULL
;
}
if
(
num
>=
sz
)
mExtComponents
.
resize
(
num
+
1
,
NULL
);
mExtComponents
[
num
]
=
ecs
;
}
void
ExternalInterface
::
registerExportedVoltage
(
VoltDiff
vd
,
int
num
)
{
int
sz
=
mExportedVoltages
.
size
();
if
(
num
>=
sz
)
mExportedVoltages
.
resize
(
num
+
1
,
{
-
1
,
-
1
});
mExportedVoltages
[
num
]
=
vd
;
}
Source/ExternalInterface.h
View file @
033dc796
...
...
@@ -6,6 +6,10 @@
#include "Components/ExternalVoltageSource.h"
namespace
DPsim
{
struct
VoltDiff
{
int
from
;
int
to
;
};
/** Abstract base class for interfacing the simulator with other data sources or sinks.
* After an ExternalInterface is created, components that should use values
* from this interface can be registered with it using the appropiate
...
...
@@ -16,10 +20,13 @@ namespace DPsim {
class
ExternalInterface
{
protected:
std
::
vector
<
BaseComponent
*>
mExtComponents
;
std
::
vector
<
VoltDiff
>
mExportedVoltages
;
public:
void
registerVoltageSource
(
ExternalVoltageSource
*
evs
,
int
num
);
void
registerCurrentSource
(
ExternalCurrentSource
*
ecs
,
int
num
);
void
registerExportedVoltage
(
VoltDiff
vd
,
int
num
);
virtual
void
readValues
()
=
0
;
virtual
void
writeValues
(
SystemModel
&
model
)
=
0
;
virtual
~
ExternalInterface
()
{};
};
}
Source/Simulation.cpp
View file @
033dc796
...
...
@@ -122,6 +122,10 @@ int Simulation::step(Logger& logger)
(
*
it
)
->
postStep
(
mSystemModel
);
}
for
(
auto
it
=
mExternalInterfaces
.
begin
();
it
!=
mExternalInterfaces
.
end
();
++
it
)
{
(
*
it
)
->
writeValues
(
mSystemModel
);
}
if
(
mCurrentSwitchTimeIndex
<
mSwitchEventVector
.
size
())
{
if
(
mTime
>=
mSwitchEventVector
[
mCurrentSwitchTimeIndex
].
switchTime
)
{
switchSystemMatrix
(
mSwitchEventVector
[
mCurrentSwitchTimeIndex
].
systemIndex
);
...
...
Source/VillasInterface.cpp
View file @
033dc796
...
...
@@ -15,6 +15,7 @@ VillasInterface::VillasInterface(const char* name) {
std
::
perror
(
"Failed to open/map shared memory object"
);
std
::
exit
(
1
);
}
mSeq
=
0
;
}
VillasInterface
::~
VillasInterface
()
{
...
...
@@ -49,3 +50,32 @@ void VillasInterface::readValues() {
sample_put
(
sample
);
}
}
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
;
}
sample
->
sequence
=
mSeq
++
;
clock_gettime
(
CLOCK_REALTIME
,
&
sample
->
ts
.
origin
);
int
ret
=
0
;
while
(
ret
==
0
)
ret
=
shmem_shared_write
(
mShmem
,
&
sample
,
1
);
if
(
ret
<
0
)
{
std
::
cerr
<<
"Failed to write samples to shmem interface"
<<
std
::
endl
;
}
}
Source/VillasInterface.h
View file @
033dc796
#pragma once
#include <atomic>
#include <unordered_map>
#include <villas/shmem.h>
...
...
@@ -15,11 +14,13 @@ namespace DPsim {
private:
const
char
*
mShmemName
;
struct
shmem_shared
*
mShmem
;
int
mSeq
;
void
*
mBase
;
public:
VillasInterface
(
const
char
*
name
);
~
VillasInterface
();
virtual
void
readValues
();
virtual
void
writeValues
(
SystemModel
&
model
);
};
};
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