DPsim
Loading...
Searching...
No Matches
MNASolver.h
1/* Copyright 2017-2021 Institute for Automation of Complex Power Systems,
2* EONERC, RWTH Aachen University
3*
4* This Source Code Form is subject to the terms of the Mozilla Public
5* License, v. 2.0. If a copy of the MPL was not distributed with this
6* file, You can obtain one at https://mozilla.org/MPL/2.0/.
7*********************************************************************************/
8
9#pragma once
10
11#include <bitset>
12#include <iostream>
13#include <list>
14#include <unordered_map>
15#include <vector>
16
17#include <dpsim-models/AttributeList.h>
18#include <dpsim-models/SimPowerComp.h>
19#include <dpsim-models/SimSignalComp.h>
20#include <dpsim-models/Solver/MNAInterface.h>
21#include <dpsim-models/Solver/MNASwitchInterface.h>
22#include <dpsim-models/Solver/MNASyncGenInterface.h>
23#include <dpsim-models/Solver/MNAVariableCompInterface.h>
24#include <dpsim/Config.h>
25#include <dpsim/DataLogger.h>
26#include <dpsim/MNAStateSpaceExtractor.h>
27#include <dpsim/Solver.h>
28
29/* std::size_t is the largest data type. No container can store
30 * more than std::size_t elements. Define the number of switches
31 * as the log_2 of this value so that we end up with maximally
32 * std::size_t matrices. The overhead of statically defining this
33 * value should be minimal.
34 **/
35#define SWITCH_NUM sizeof(std::size_t) * 8
36
37namespace DPsim {
39template <typename VarType> class MnaSolver : public Solver {
40protected:
41 // #### General simulation settings ####
43 CPS::Domain mDomain;
45 UInt mNumNodes = 0;
47 UInt mNumNetNodes = 0;
61 std::vector<std::pair<UInt, UInt>> mListVariableSystemMatrixEntries;
62
66 typename CPS::SimNode<VarType>::List mNodes;
67
68 // #### MNA specific attributes ####
70 CPS::MNAInterface::List mMNAComponents;
73 CPS::MNASwitchInterface::List mSwitches;
75 CPS::MNAInterface::List mMNAIntfSwitches;
77 CPS::SimSignalComp::List mSimSignalComps;
79 std::bitset<SWITCH_NUM> mCurrentSwitchStatus;
81 CPS::MNASyncGenInterface::List mSyncGen;
82
86 std::vector<const Matrix *> mRightVectorStamps;
87
88 // #### MNA specific attributes related to harmonics / additional frequencies ####
90 std::vector<Matrix> mRightSideVectorHarm;
91
92 // #### MNA specific attributes related to system recomputation
97 CPS::MNAVariableCompInterface::List mVariableComps;
99 CPS::MNAInterface::List mMNAIntfVariableComps;
100
101 // #### Attributes related to switching ####
105 std::vector<SwitchConfiguration> mSwitchEvents;
107 void updateSwitchStatus();
108
109 // #### Attributes related to logging ####
113 std::shared_ptr<DataLogger> mLeftVectorLog;
115 std::shared_ptr<DataLogger> mRightVectorLog;
116
118 std::vector<Real> mFactorizeTimes;
120 std::vector<Real> mSolveTimes;
122 std::vector<Real> mRecomputationTimes;
123
124 // #### State-space extraction ####
127
129 MNAStateSpaceExtractor::Ptr mStateSpaceExtractor;
130
132 MnaSolver(String name, CPS::Domain domain = CPS::Domain::DP,
133 CPS::Logger::Level logLevel = CPS::Logger::Level::info);
134
138 virtual void initializeSystem();
153 void collectVirtualNodes();
154 // TODO: check if this works with AC sources
155 void steadyStateInitialization();
156
160 virtual void createEmptySystemMatrix() = 0;
162 virtual void switchedMatrixEmpty(std::size_t index) = 0;
164 virtual void switchedMatrixEmpty(std::size_t swIdx, Int freqIdx) = 0;
167 std::size_t index,
168 std::vector<std::shared_ptr<CPS::MNAInterface>> &comp) = 0;
170 virtual void switchedMatrixStamp(std::size_t swIdx, Int freqIdx,
171 CPS::MNAInterface::List &components,
172 CPS::MNASwitchInterface::List &switches) {}
173
175
176 // #### Methods to implement for system recomputation over time ####
178 virtual void stampVariableSystemMatrix() = 0;
181 Int timeStepCount) = 0;
183 virtual std::shared_ptr<CPS::Task> createSolveTaskRecomp() = 0;
184
186 virtual void logSystemMatrices() = 0;
187
189 virtual std::shared_ptr<CPS::Task> createSolveTask() = 0;
191 virtual std::shared_ptr<CPS::Task> createLogTask() = 0;
193 virtual std::shared_ptr<CPS::Task> createSolveTaskHarm(UInt freqIdx) = 0;
195 virtual std::shared_ptr<CPS::Task> createStateSpaceExtractionTask() = 0;
196
197 // #### Scheduler Task Methods ####
199 virtual void solve(Real time, Int timeStepCount) = 0;
201 virtual void solveWithHarmonics(Real time, Int timeStepCount,
202 Int freqIdx) = 0;
204 virtual void log(Real time, Int timeStepCount) override;
205
206public:
208 CPS::Attribute<Matrix>::Ptr mLeftSideVector;
209
211 std::vector<CPS::Attribute<Matrix>::Ptr> mLeftSideVectorHarm;
212
214 virtual ~MnaSolver() {
216 SPDLOG_LOGGER_INFO(mSLog, "Number of system matrix recomputations: {:}",
218 };
219
221 virtual void initialize() override;
222
223 // #### Setter and Getter ####
225 virtual void setSystem(const CPS::SystemTopology &system) override;
227 Matrix &leftSideVector() { return **mLeftSideVector; }
229 Matrix &rightSideVector() { return mRightSideVector; }
231 virtual CPS::Task::List getTasks() override;
232
234 void doStateSpaceExtraction(Bool value = true);
235
237 const MNAStateSpaceExtractor &getStateSpaceExtractor() const;
238};
239} // namespace DPsim
std::bitset< SWITCH_NUM > mCurrentSwitchStatus
Current status of all switches encoded as bitset.
Definition MNASolver.h:79
CPS::Domain mDomain
Simulation domain, which can be dynamic phasor (DP) or EMT.
Definition MNASolver.h:43
void identifyTopologyObjects()
Identify Nodes and SimPowerComps and SimSignalComps.
std::vector< Matrix > mRightSideVectorHarm
Source vector of known quantities.
Definition MNASolver.h:90
Matrix mRightSideVector
Source vector of known quantities.
Definition MNASolver.h:84
Bool mStateSpaceExtraction
Enables extraction of the MNA-coupled discrete-time state matrix.
Definition MNASolver.h:126
CPS::SystemTopology mSystem
System topology.
Definition MNASolver.h:64
virtual std::shared_ptr< CPS::Task > createStateSpaceExtractionTask()=0
Create state-space extraction task for this solver implementation.
virtual void switchedMatrixEmpty(std::size_t swIdx, Int freqIdx)=0
Sets all entries in the matrix with the given switch index and frequency index to zero.
virtual ~MnaSolver()
Destructor.
Definition MNASolver.h:214
std::vector< Real > mRecomputationTimes
LU refactorization measurements.
Definition MNASolver.h:122
void initializeSystemWithVariableMatrix()
Initialization of system matrices and source vector.
virtual void initialize() override
Calls subroutines to set up everything that is required before simulation.
Definition MNASolver.cpp:52
virtual void logSystemMatrices()=0
Logging of system matrices and source vector.
virtual void initializeSystem()
Initialization of system matrices and source vector.
std::vector< Real > mFactorizeTimes
LU factorization measurements.
Definition MNASolver.h:118
std::vector< CPS::Attribute< Matrix >::Ptr > mLeftSideVectorHarm
Solution vector of unknown quantities (parallel frequencies)
Definition MNASolver.h:211
Bool hasVariableComponentChanged()
Checks whether the status of variable MNA elements have changed.
CPS::MNAInterface::List mMNAIntfVariableComps
List of variable components if they must be accessed as MNAInterface objects.
Definition MNASolver.h:99
UInt mNumNetMatrixNodeIndices
Number of network nodes, considering individual phases.
Definition MNASolver.h:53
UInt mNumNetNodes
Number of network nodes, single line equivalent.
Definition MNASolver.h:47
virtual void switchedMatrixStamp(std::size_t index, std::vector< std::shared_ptr< CPS::MNAInterface > > &comp)=0
Applies a component stamp to the matrix with the given switch index.
MNAStateSpaceExtractor::Ptr mStateSpaceExtractor
Extractor for the MNA-coupled state-space model.
Definition MNASolver.h:129
virtual void log(Real time, Int timeStepCount) override
Logs left and right vector.
UInt mNumTotalMatrixNodeIndices
Total number of network and virtual nodes, considering individual phases and additional frequencies.
Definition MNASolver.h:59
UInt mNumVirtualMatrixNodeIndices
Number of virtual nodes, considering individual phases.
Definition MNASolver.h:55
CPS::MNASyncGenInterface::List mSyncGen
List of synchronous generators that need iterate to solve the differential equations.
Definition MNASolver.h:81
CPS::MNAInterface::List mMNAIntfSwitches
List of switches if they must be accessed as MNAInterface objects.
Definition MNASolver.h:75
std::shared_ptr< DataLogger > mRightVectorLog
Right side vector logger.
Definition MNASolver.h:115
virtual void solve(Real time, Int timeStepCount)=0
Solves system for single frequency.
virtual std::shared_ptr< CPS::Task > createSolveTaskHarm(UInt freqIdx)=0
Create a solve task for this solver implementation.
UInt mSwitchTimeIndex
Index of the next switching event.
Definition MNASolver.h:103
virtual void switchedMatrixStamp(std::size_t swIdx, Int freqIdx, CPS::MNAInterface::List &components, CPS::MNASwitchInterface::List &switches)
Applies a component and switch stamp to the matrix with the given switch index.
Definition MNASolver.h:170
std::vector< const Matrix * > mRightVectorStamps
List of all right side vector contributions.
Definition MNASolver.h:86
void initializeComponents()
Initialization of individual components.
void updateSwitchStatus()
Collects the status of switches to select correct system matrix.
std::vector< Real > mSolveTimes
Right-hand side solution measurements.
Definition MNASolver.h:120
Int mLastLogTimeStep
Last simulation time step when log was updated.
Definition MNASolver.h:111
std::vector< std::pair< UInt, UInt > > mListVariableSystemMatrixEntries
List of index pairs of varying matrix entries.
Definition MNASolver.h:61
CPS::MNAVariableCompInterface::List mVariableComps
Definition MNASolver.h:97
virtual void solveWithHarmonics(Real time, Int timeStepCount, Int freqIdx)=0
Solves system for multiple frequencies.
Int mNumRecomputations
Number of system matrix recomputations.
Definition MNASolver.h:94
virtual std::shared_ptr< CPS::Task > createSolveTaskRecomp()=0
Create a solve task for recomputation solver.
CPS::MNAInterface::List mMNAComponents
List of MNA components with static stamp into system matrix.
Definition MNASolver.h:70
virtual void stampVariableSystemMatrix()=0
Stamps components into the variable system matrix.
std::shared_ptr< DataLogger > mLeftVectorLog
Left side vector logger.
Definition MNASolver.h:113
UInt mNumMatrixNodeIndices
Number of network and virtual nodes, considering individual phases.
Definition MNASolver.h:51
void initializeSystemWithParallelFrequencies()
Initialization of system matrices and source vector.
void collectVirtualNodes()
UInt mNumHarmMatrixNodeIndices
Number of nodes, excluding the primary frequency.
Definition MNASolver.h:57
UInt mNumNodes
Number of network and virtual nodes, single line equivalent.
Definition MNASolver.h:45
std::vector< SwitchConfiguration > mSwitchEvents
Vector of switch times.
Definition MNASolver.h:105
MnaSolver(String name, CPS::Domain domain=CPS::Domain::DP, CPS::Logger::Level logLevel=CPS::Logger::Level::info)
Constructor should not be called by users but by Simulation.
Definition MNASolver.cpp:21
void assignMatrixNodeIndices()
Assign simulation node index according to index in the vector.
UInt mNumVirtualNodes
Number of virtual nodes, single line equivalent.
Definition MNASolver.h:49
void initializeSystemWithPrecomputedMatrices()
Initialization of system matrices and source vector.
CPS::Attribute< Matrix >::Ptr mLeftSideVector
Solution vector of unknown quantities.
Definition MNASolver.h:208
CPS::SimSignalComp::List mSimSignalComps
List of signal type components that do not directly interact with the MNA solver.
Definition MNASolver.h:77
virtual void createEmptySystemMatrix()=0
Create system matrix.
virtual std::shared_ptr< CPS::Task > createLogTask()=0
Create a solve task for this solver implementation.
const MNAStateSpaceExtractor & getStateSpaceExtractor() const
Read-only access to the MNA state-space extractor.
Definition MNASolver.cpp:44
virtual void solveWithSystemMatrixRecomputation(Real time, Int timeStepCount)=0
Solves the system with variable system matrix.
void doStateSpaceExtraction(Bool value=true)
Enable or disable MNA state-space extraction.
Definition MNASolver.cpp:38
CPS::MNASwitchInterface::List mSwitches
Definition MNASolver.h:73
void createEmptyVectors()
Create left and right side vector.
virtual void switchedMatrixEmpty(std::size_t index)=0
Sets all entries in the matrix with the given switch index to zero.
virtual std::shared_ptr< CPS::Task > createSolveTask()=0
Create a solve task for this solver implementation.
CPS::SimNode< VarType >::List mNodes
List of simulation nodes.
Definition MNASolver.h:66
void initializeStateSpaceExtractor()
Initialization of state-space extraction.
virtual CPS::Task::List getTasks() override
Get tasks for scheduler.
Bool mSystemMatrixRecomputation
Enable recomputation of system matrix during simulation.
Definition Solver.h:64
CPS::Logger::Log mSLog
Logger.
Definition Solver.h:45