Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Co-simulation examples #263

Draft
wants to merge 42 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
18a8552
Implemented SP_Ph1_CurrentSource
pipeacosta Sep 29, 2023
21f58e6
First version of the dpsim-mqtt-distributed-villas example
pipeacosta Oct 12, 2022
2cec8ec
Cleaned commented code in dpsim-mqtt-distributed-villas.py
pipeacosta Oct 12, 2022
7da3537
Fixed relative path in docker-compose of mqtt example
pipeacosta Oct 20, 2022
3895055
Minor improvements to the dpsim-mqtt example
pipeacosta Oct 20, 2022
7709385
First version of SocketExample.cpp
pipeacosta Dec 23, 2022
49c50e0
First version of SocketCosimExample
pipeacosta Feb 3, 2023
467af6b
Improvements to SocketExample, first working version
pipeacosta Feb 3, 2023
384cacf
Specify name and type of signal to which subscription is made
pipeacosta Feb 3, 2023
df93085
Implemented first version of co-simulation using sockets in Python
pipeacosta Feb 3, 2023
ff1da65
Improvements to SocketCosimExample: added hooks and start script
pipeacosta Feb 8, 2023
c85dc32
Improvements to the example dpsim-villas-cosim: first working version
pipeacosta Feb 8, 2023
92c1903
Implemented dpsim-villas-cosim-base for comparison
pipeacosta Feb 8, 2023
f94ab97
First version of the weakly-coupled co-simulation example
pipeacosta Feb 8, 2023
824ab10
Changed RealTimeSimulation to Simulation in cosim examples
pipeacosta Mar 21, 2023
6363691
Minor changes to the dpsim-villas-weakly-coupled-cosim Notebook
pipeacosta Mar 21, 2023
3afc150
First version of co-simulation examples with Python coordination
pipeacosta Apr 1, 2023
275df7f
Rename co-simulation example
pipeacosta Apr 9, 2023
66c9e50
Implemented second subsystem (second partition)
pipeacosta Jun 3, 2023
25b10fd
Completed a first version of the co-simulation example
pipeacosta Jun 11, 2023
50c5ae7
Created a simplified test of DPSim attributes and how they
pipeacosta Jun 11, 2023
c375509
Update dpsim-mqtt to support the new VILLASnode interface
pipeacosta Oct 17, 2023
f5fbefd
Update dpsim-mqtt-distributed-villas to support the new VILLASnode
pipeacosta Oct 17, 2023
21c1cf0
Improved import/export calls in dpsim-villas-cosim.py
pipeacosta Oct 17, 2023
044c31b
Corrected a co-simulation example name in CMakeLists
pipeacosta Oct 23, 2023
02dec44
Merge branch 'SP-Ph1-CurrentSource' into cosim-attributes
pipeacosta Nov 6, 2023
bd22a7f
Fixed attribute exchange in C++ cosimulation
pipeacosta Nov 8, 2023
61330ca
Merge branch 'cosim-examples-villas' into cosim-examples
pipeacosta Nov 9, 2023
ba991c5
Merge branch 'cosim-examples-attributes' into cosim-examples
pipeacosta Nov 9, 2023
6a29f67
Refactor: file names and locations
pipeacosta Nov 9, 2023
18b6e65
Refactoring co-simulation example's filenames and locations
pipeacosta Nov 30, 2023
413c406
First working version of co-simulation using attributes
pipeacosta Nov 30, 2023
514b0ed
Fixed interface voltage update in EMT_Ph1_CurrentSource
pipeacosta Dec 8, 2023
f7a02e9
Improve and clean emt-cosim-attributes.py example
pipeacosta Dec 8, 2023
6a28fcc
Implemented new attribute method deriveComplex
pipeacosta Dec 8, 2023
3ff388a
Improvements to the co-simulation using the VILLASnode interace
pipeacosta Dec 8, 2023
35fcec4
First version of attributes co-simulation using ZOH extrapolation
pipeacosta Feb 19, 2024
7401a23
First version of attributes co-simulation using linear extrapolation
pipeacosta Feb 20, 2024
b48062c
Adapt paths to make the CI happy
pipeacosta Feb 20, 2024
7e75432
Add deriveComplex function to Attributes.cpp example
pipeacosta Feb 21, 2024
ab26dd9
First working version of co-simulation between
pipeacosta Mar 17, 2024
5c96c00
Add results of DPsim-Matlab co-simulation to the Notebook
pipeacosta Mar 24, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions configs/start_SocketCosimExample.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/bash

CPS_LOG_PREFIX="[Left ] " build/dpsim-villas/examples/cxx/SocketCosimExample 0 & P1=$!
CPS_LOG_PREFIX="[Right] " build/dpsim-villas/examples/cxx/SocketCosimExample 1 & P2=$!

for job in $P1 $P2; do
wait $job || exit 1
done
17 changes: 17 additions & 0 deletions dpsim-models/include/dpsim-models/Attribute.h
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,23 @@ namespace CPS {
return derive<U>(getter, setter);
}

/**
* Convenience method for building a complex attribute from a real attribute
* @return a new complex attribute whose real part always equal value of `this`
* */
template <typename U = T, std::enable_if_t<std::is_same_v<Real, U>, bool> = true>
AttributePointer<Attribute<Complex>> deriveComplex()
// requires std::same_as<T, CPS::Complex> //CPP20
{
AttributeUpdateTask<CPS::Complex, CPS::Real>::Actor getter = [](std::shared_ptr<Complex> &dependent, Attribute<Real>::Ptr dependency) {
*dependent = CPS::Complex(dependency->get(), 0);
};
AttributeUpdateTask<CPS::Complex, CPS::Real>::Actor setter = [](std::shared_ptr<Complex> &dependent, Attribute<Real>::Ptr dependency) {
dependency->set(dependent->real());
};
return derive<CPS::Complex>(getter, setter);
}

};

/**
Expand Down
1 change: 1 addition & 0 deletions dpsim-models/include/dpsim-models/Components.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <dpsim-models/Config.h>

#include <dpsim-models/SP/SP_Ph1_AvVoltageSourceInverterDQ.h>
#include <dpsim-models/SP/SP_Ph1_CurrentSource.h>
#include <dpsim-models/SP/SP_Ph1_RXLine.h>
#include <dpsim-models/SP/SP_Ph1_VoltageSourceInverter.h>
#include <dpsim-models/SP/SP_Ph1_PiLine.h>
Expand Down
64 changes: 64 additions & 0 deletions dpsim-models/include/dpsim-models/SP/SP_Ph1_CurrentSource.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/* Copyright 2017-2021 Institute for Automation of Complex Power Systems,
* EONERC, RWTH Aachen University
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*********************************************************************************/

#pragma once

#include <dpsim-models/MNASimPowerComp.h>
#include <dpsim-models/Solver/MNAInterface.h>
#include <dpsim-models/Base/Base_Ph1_CurrentSource.h>
#include <dpsim-models/Task.h>

namespace CPS {
namespace SP {
namespace Ph1 {
/// \brief Dynamic phasor ideal current source
///
/// A positive current is flowing out of node1 and into node2.
class CurrentSource :
public MNASimPowerComp<Complex>,
public SharedFactory<CurrentSource> {
public:
const Attribute<Complex>::Ptr mCurrentRef;
/// Defines UID, name and logging level
CurrentSource(String uid, String name, Logger::Level loglevel = Logger::Level::off);
/// Defines name and logging level
CurrentSource(String name, Logger::Level logLevel = Logger::Level::off)
: CurrentSource(name, name, logLevel) { }
/// Defines name, component parameters and logging level
CurrentSource(String name, Complex current,
Logger::Level logLevel = Logger::Level::off);

void setParameters(Complex current);

SimPowerComp<Complex>::Ptr clone(String copySuffix);

// #### General ####
/// Initializes component from power flow data
void initializeFromNodesAndTerminals(Real frequency);

// #### MNA section ####
///
void mnaCompInitialize(Real omega, Real timeStep, Attribute<Matrix>::Ptr leftVector);
/// Stamps system matrix
void mnaCompApplySystemMatrixStamp(SparseMatrixRow& systemMatrix) { }
/// Stamps right side (source) vector
void mnaCompApplyRightSideVectorStamp(Matrix& rightVector);
///
void mnaCompUpdateVoltage(const Matrix& leftVector);

/// Add MNA pre step dependencies
void mnaCompAddPreStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes) override;
/// Add MNA post step dependencies
void mnaCompAddPostStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes, Attribute<Matrix>::Ptr &leftVector) override;
void mnaCompPreStep(Real time, Int timeStepCount) override;
void mnaCompPostStep(Real time, Int timeStepCount, Attribute<Matrix>::Ptr &leftVector) override;

};
}
}
}
1 change: 1 addition & 0 deletions dpsim-models/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ list(APPEND MODELS_SOURCES
EMT/EMT_Ph3_SynchronGeneratorTrStab.cpp

SP/SP_Ph1_VoltageSource.cpp
SP/SP_Ph1_CurrentSource.cpp
SP/SP_Ph1_Capacitor.cpp
SP/SP_Ph1_Inductor.cpp
SP/SP_Ph1_Resistor.cpp
Expand Down
2 changes: 1 addition & 1 deletion dpsim-models/src/EMT/EMT_Ph1_Capacitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ SimPowerComp<Real>::Ptr EMT::Ph1::Capacitor::clone(String name) {
}

void EMT::Ph1::Capacitor::initializeFromNodesAndTerminals(Real frequency) {

Real omega = 2 * PI * frequency;
Complex impedance = { 0, - 1. / (omega * **mCapacitance) };
(**mIntfVoltage)(0,0) = (initialSingleVoltage(1) - initialSingleVoltage(0)).real();
Expand All @@ -42,6 +41,7 @@ void EMT::Ph1::Capacitor::initializeFromNodesAndTerminals(Real frequency) {
(**mIntfCurrent)(0,0),
initialSingleVoltage(0).real(),
initialSingleVoltage(1).real());
mSLog->flush();
}

void EMT::Ph1::Capacitor::mnaCompInitialize(Real omega, Real timeStep, Attribute<Matrix>::Ptr leftVector) {
Expand Down
6 changes: 3 additions & 3 deletions dpsim-models/src/EMT/EMT_Ph1_CurrentSource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ void EMT::Ph1::CurrentSource::mnaCompPostStep(Real time, Int timeStepCount, Attr

void EMT::Ph1::CurrentSource::mnaCompUpdateVoltage(const Matrix& leftVector) {
(**mIntfVoltage)(0,0) = 0;
if (terminalNotGrounded(0))
(**mIntfVoltage)(0,0) = Math::realFromVectorElement(leftVector, matrixNodeIndex(0));
if (terminalNotGrounded(1))
(**mIntfVoltage)(0,0) = (**mIntfVoltage)(0,0) - Math::realFromVectorElement(leftVector, matrixNodeIndex(1));
(**mIntfVoltage)(0,0) = Math::realFromVectorElement(leftVector, matrixNodeIndex(0));
if (terminalNotGrounded(0))
(**mIntfVoltage)(0,0) = (**mIntfVoltage)(0,0) - Math::realFromVectorElement(leftVector, matrixNodeIndex(0));
}
3 changes: 2 additions & 1 deletion dpsim-models/src/EMT/EMT_Ph1_Inductor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ SimPowerComp<Real>::Ptr EMT::Ph1::Inductor::clone(String name) {
}

void EMT::Ph1::Inductor::initializeFromNodesAndTerminals(Real frequency) {

Real omega = 2 * PI * frequency;
Complex impedance = { 0, omega * **mInductance };
(**mIntfVoltage)(0,0) = (initialSingleVoltage(1) - initialSingleVoltage(0)).real();
Expand All @@ -42,6 +42,7 @@ void EMT::Ph1::Inductor::initializeFromNodesAndTerminals(Real frequency) {
(**mIntfCurrent)(0,0),
initialSingleVoltage(0).real(),
initialSingleVoltage(1).real());
mSLog->flush();
}

void EMT::Ph1::Inductor::mnaCompInitialize(Real omega, Real timeStep, Attribute<Matrix>::Ptr leftVector) {
Expand Down
1 change: 1 addition & 0 deletions dpsim-models/src/EMT/EMT_Ph1_Resistor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ void EMT::Ph1::Resistor::initializeFromNodesAndTerminals(Real frequency) {
(**mIntfCurrent)(0,0),
initialSingleVoltage(0).real(),
initialSingleVoltage(1).real());
mSLog->flush();
}

void EMT::Ph1::Resistor::mnaCompInitialize(Real omega, Real timeStep, Attribute<Matrix>::Ptr leftVector) {
Expand Down
94 changes: 94 additions & 0 deletions dpsim-models/src/SP/SP_Ph1_CurrentSource.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/* Copyright 2017-2021 Institute for Automation of Complex Power Systems,
* EONERC, RWTH Aachen University
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*********************************************************************************/

#include <dpsim-models/SP/SP_Ph1_CurrentSource.h>

using namespace CPS;

SP::Ph1::CurrentSource::CurrentSource(String uid, String name, Logger::Level logLevel)
: MNASimPowerComp<Complex>(uid, name, true, true, logLevel),
mCurrentRef(mAttributes->createDynamic<Complex>("I_ref")) {
setTerminalNumber(2);
**mIntfVoltage = MatrixComp::Zero(1,1);
**mIntfCurrent = MatrixComp::Zero(1,1);
}

SP::Ph1::CurrentSource::CurrentSource(String name, Complex current, Logger::Level logLevel)
: CurrentSource(name, logLevel) {
setParameters(current);
}

void SP::Ph1::CurrentSource::setParameters(Complex current) {
**mCurrentRef = current;
mParametersSet = true;
}

SimPowerComp<Complex>::Ptr SP::Ph1::CurrentSource::clone(String name) {
auto copy = CurrentSource::make(name, mLogLevel);
copy->setParameters(**mCurrentRef);
return copy;
}

void SP::Ph1::CurrentSource::initializeFromNodesAndTerminals(Real frequency) {

(**mIntfVoltage)(0,0) = initialSingleVoltage(0) - initialSingleVoltage(1);
(**mIntfCurrent)(0,0) = **mCurrentRef;

SPDLOG_LOGGER_INFO(mSLog,
"\n--- Initialization from powerflow ---"
"\nVoltage across: {:s}"
"\nCurrent: {:s}"
"\nTerminal 0 voltage: {:s}"
"\nTerminal 1 voltage: {:s}"
"\n--- Initialization from powerflow finished ---",
Logger::phasorToString((**mIntfVoltage)(0,0)),
Logger::phasorToString((**mIntfCurrent)(0,0)),
Logger::phasorToString(initialSingleVoltage(0)),
Logger::phasorToString(initialSingleVoltage(1)));
}

void SP::Ph1::CurrentSource::mnaCompInitialize(Real omega, Real timeStep, Attribute<Matrix>::Ptr leftVector) {
updateMatrixNodeIndices();
(**mIntfCurrent)(0,0) = **mCurrentRef;
}

void SP::Ph1::CurrentSource::mnaCompAddPreStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes) {
attributeDependencies.push_back(mCurrentRef);
modifiedAttributes.push_back(mRightVector);
modifiedAttributes.push_back(mIntfCurrent);
}

void SP::Ph1::CurrentSource::mnaCompPreStep(Real time, Int timeStepCount) {
mnaCompApplyRightSideVectorStamp(**mRightVector);
}

void SP::Ph1::CurrentSource::mnaCompApplyRightSideVectorStamp(Matrix& rightVector) {
(**mIntfCurrent)(0,0) = **mCurrentRef;

if (terminalNotGrounded(0))
Math::setVectorElement(rightVector, matrixNodeIndex(0), -(**mIntfCurrent)(0,0));
if (terminalNotGrounded(1))
Math::setVectorElement(rightVector, matrixNodeIndex(1), (**mIntfCurrent)(0,0));
}

void SP::Ph1::CurrentSource::mnaCompAddPostStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes, Attribute<Matrix>::Ptr &leftVector) {
attributeDependencies.push_back(leftVector);
modifiedAttributes.push_back(mIntfVoltage);
}

void SP::Ph1::CurrentSource::mnaCompPostStep(Real time, Int timeStepCount, Attribute<Matrix>::Ptr &leftVector) {
mnaCompUpdateVoltage(**leftVector);
}

void SP::Ph1::CurrentSource::mnaCompUpdateVoltage(const Matrix& leftVector) {
(**mIntfVoltage)(0,0) = 0;
if (terminalNotGrounded(0))
(**mIntfVoltage)(0,0) = Math::complexFromVectorElement(leftVector, matrixNodeIndex(0));
if (terminalNotGrounded(1))
(**mIntfVoltage)(0,0) = (**mIntfVoltage)(0,0) - Math::complexFromVectorElement(leftVector, matrixNodeIndex(1));
}
2 changes: 2 additions & 0 deletions dpsim-villas/examples/cxx/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ set(SHMEM_SOURCES
FileExample.cpp
MqttExample.cpp
SharedMemExample.cpp
SocketExample.cpp
CosimSocketExample.cpp
#ShmemExample.cpp
#ShmemDistributedReference.cpp
#ShmemDistributedDirect.cpp
Expand Down
Loading
Loading