DPsim
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/Solver.h>
27 
28 /* std::size_t is the largest data type. No container can store
29  * more than std::size_t elements. Define the number of switches
30  * as the log_2 of this value so that we end up with maximally
31  * std::size_t matrices. The overhead of statically defining this
32  * value should be minimal.
33  **/
34 #define SWITCH_NUM sizeof(std::size_t) * 8
35 
36 namespace DPsim {
38 template <typename VarType> class MnaSolver : public Solver {
39 protected:
40  // #### General simulation settings ####
42  CPS::Domain mDomain;
44  UInt mNumNodes = 0;
46  UInt mNumNetNodes = 0;
48  UInt mNumVirtualNodes = 0;
60  std::vector<std::pair<UInt, UInt>> mListVariableSystemMatrixEntries;
61 
65  typename CPS::SimNode<VarType>::List mNodes;
66 
67  // #### MNA specific attributes ####
69  CPS::MNAInterface::List mMNAComponents;
72  CPS::MNASwitchInterface::List mSwitches;
74  CPS::MNAInterface::List mMNAIntfSwitches;
76  CPS::SimSignalComp::List mSimSignalComps;
78  std::bitset<SWITCH_NUM> mCurrentSwitchStatus;
80  CPS::MNASyncGenInterface::List mSyncGen;
81 
85  std::vector<const Matrix *> mRightVectorStamps;
86 
87  // #### MNA specific attributes related to harmonics / additional frequencies ####
89  std::vector<Matrix> mRightSideVectorHarm;
90 
91  // #### MNA specific attributes related to system recomputation
96  CPS::MNAVariableCompInterface::List mVariableComps;
98  CPS::MNAInterface::List mMNAIntfVariableComps;
99 
100  // #### Attributes related to switching ####
104  std::vector<SwitchConfiguration> mSwitchEvents;
106  void updateSwitchStatus();
107 
108  // #### Attributes related to logging ####
112  std::shared_ptr<DataLogger> mLeftVectorLog;
114  std::shared_ptr<DataLogger> mRightVectorLog;
115 
117  std::vector<Real> mFactorizeTimes;
119  std::vector<Real> mSolveTimes;
121  std::vector<Real> mRecomputationTimes;
122 
124  MnaSolver(String name, CPS::Domain domain = CPS::Domain::DP,
125  CPS::Logger::Level logLevel = CPS::Logger::Level::info);
126 
130  virtual void initializeSystem();
143  void collectVirtualNodes();
144  // TODO: check if this works with AC sources
145  void steadyStateInitialization();
146 
150  virtual void createEmptySystemMatrix() = 0;
152  virtual void switchedMatrixEmpty(std::size_t index) = 0;
154  virtual void switchedMatrixEmpty(std::size_t swIdx, Int freqIdx) = 0;
156  virtual void switchedMatrixStamp(
157  std::size_t index,
158  std::vector<std::shared_ptr<CPS::MNAInterface>> &comp) = 0;
160  virtual void switchedMatrixStamp(std::size_t swIdx, Int freqIdx,
161  CPS::MNAInterface::List &components,
162  CPS::MNASwitchInterface::List &switches) {}
165 
166  // #### Methods to implement for system recomputation over time ####
168  virtual void stampVariableSystemMatrix() = 0;
170  virtual void solveWithSystemMatrixRecomputation(Real time,
171  Int timeStepCount) = 0;
173  virtual std::shared_ptr<CPS::Task> createSolveTaskRecomp() = 0;
174 
176  virtual void logSystemMatrices() = 0;
177 
179  virtual std::shared_ptr<CPS::Task> createSolveTask() = 0;
181  virtual std::shared_ptr<CPS::Task> createLogTask() = 0;
183  virtual std::shared_ptr<CPS::Task> createSolveTaskHarm(UInt freqIdx) = 0;
184 
185  // #### Scheduler Task Methods ####
187  virtual void solve(Real time, Int timeStepCount) = 0;
189  virtual void solveWithHarmonics(Real time, Int timeStepCount,
190  Int freqIdx) = 0;
192  virtual void log(Real time, Int timeStepCount) override;
193 
194 public:
197 
199  std::vector<CPS::Attribute<Matrix>::Ptr> mLeftSideVectorHarm;
200 
202  virtual ~MnaSolver() {
204  SPDLOG_LOGGER_INFO(mSLog, "Number of system matrix recomputations: {:}",
206  };
207 
209  virtual void initialize() override;
210 
211  // #### Setter and Getter ####
213  virtual void setSystem(const CPS::SystemTopology &system) override;
215  Matrix &leftSideVector() { return **mLeftSideVector; }
217  Matrix &rightSideVector() { return mRightSideVector; }
219  virtual CPS::Task::List getTasks() override;
220 };
221 } // namespace DPsim
Solver class using Modified Nodal Analysis (MNA).
Definition: MNASolver.h:38
std::bitset< SWITCH_NUM > mCurrentSwitchStatus
Current status of all switches encoded as bitset.
Definition: MNASolver.h:78
CPS::Domain mDomain
Simulation domain, which can be dynamic phasor (DP) or EMT.
Definition: MNASolver.h:42
virtual std::shared_ptr< CPS::Task > createSolveTaskHarm(UInt freqIdx)=0
Create a solve task for this solver implementation.
void identifyTopologyObjects()
Identify Nodes and SimPowerComps and SimSignalComps.
Definition: MNASolver.cpp:315
std::vector< Matrix > mRightSideVectorHarm
Source vector of known quantities.
Definition: MNASolver.h:89
Matrix mRightSideVector
Source vector of known quantities.
Definition: MNASolver.h:83
CPS::SystemTopology mSystem
System topology.
Definition: MNASolver.h:63
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:202
std::vector< Real > mRecomputationTimes
LU refactorization measurements.
Definition: MNASolver.h:121
void initializeSystemWithVariableMatrix()
Initialization of system matrices and source vector.
Definition: MNASolver.cpp:269
virtual void initialize() override
Calls subroutines to set up everything that is required before simulation.
Definition: MNASolver.cpp:35
virtual void logSystemMatrices()=0
Logging of system matrices and source vector.
virtual void initializeSystem()
Initialization of system matrices and source vector.
Definition: MNASolver.cpp:198
std::vector< Real > mFactorizeTimes
LU factorization measurements.
Definition: MNASolver.h:117
std::vector< CPS::Attribute< Matrix >::Ptr > mLeftSideVectorHarm
Solution vector of unknown quantities (parallel frequencies)
Definition: MNASolver.h:199
Bool hasVariableComponentChanged()
Checks whether the status of variable MNA elements have changed.
Definition: MNASolver.cpp:296
CPS::MNAInterface::List mMNAIntfVariableComps
List of variable components if they must be accessed as MNAInterface objects.
Definition: MNASolver.h:98
UInt mNumNetMatrixNodeIndices
Number of network nodes, considering individual phases.
Definition: MNASolver.h:52
UInt mNumNetNodes
Number of network nodes, single line equivalent.
Definition: MNASolver.h:46
virtual void log(Real time, Int timeStepCount) override
Logs left and right vector.
Definition: MNASolver.cpp:624
UInt mNumTotalMatrixNodeIndices
Total number of network and virtual nodes, considering individual phases and additional frequencies.
Definition: MNASolver.h:58
UInt mNumVirtualMatrixNodeIndices
Number of virtual nodes, considering individual phases.
Definition: MNASolver.h:54
CPS::MNASyncGenInterface::List mSyncGen
List of synchronous generators that need iterate to solve the differential equations.
Definition: MNASolver.h:80
CPS::MNAInterface::List mMNAIntfSwitches
List of switches if they must be accessed as MNAInterface objects.
Definition: MNASolver.h:74
virtual std::shared_ptr< CPS::Task > createSolveTaskRecomp()=0
Create a solve task for recomputation solver.
std::shared_ptr< DataLogger > mRightVectorLog
Right side vector logger.
Definition: MNASolver.h:114
virtual void solve(Real time, Int timeStepCount)=0
Solves system for single frequency.
UInt mSwitchTimeIndex
Index of the next switching event.
Definition: MNASolver.h:102
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:160
std::vector< const Matrix * > mRightVectorStamps
List of all right side vector contributions.
Definition: MNASolver.h:85
void initializeComponents()
Initialization of individual components.
void updateSwitchStatus()
Collects the status of switches to select correct system matrix.
Definition: MNASolver.cpp:309
std::vector< Real > mSolveTimes
Right-hand side solution measurements.
Definition: MNASolver.h:119
Int mLastLogTimeStep
Last simulation time step when log was updated.
Definition: MNASolver.h:110
std::vector< std::pair< UInt, UInt > > mListVariableSystemMatrixEntries
List of index pairs of varying matrix entries.
Definition: MNASolver.h:60
CPS::MNAVariableCompInterface::List mVariableComps
Definition: MNASolver.h:96
virtual std::shared_ptr< CPS::Task > createLogTask()=0
Create a solve task for this solver implementation.
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:93
CPS::MNAInterface::List mMNAComponents
List of MNA components with static stamp into system matrix.
Definition: MNASolver.h:69
virtual void stampVariableSystemMatrix()=0
Stamps components into the variable system matrix.
std::shared_ptr< DataLogger > mLeftVectorLog
Left side vector logger.
Definition: MNASolver.h:112
virtual std::shared_ptr< CPS::Task > createSolveTask()=0
Create a solve task for this solver implementation.
UInt mNumMatrixNodeIndices
Number of network and virtual nodes, considering individual phases.
Definition: MNASolver.h:50
void initializeSystemWithParallelFrequencies()
Initialization of system matrices and source vector.
Definition: MNASolver.cpp:218
void collectVirtualNodes()
Definition: MNASolver.cpp:422
UInt mNumHarmMatrixNodeIndices
Number of nodes, excluding the primary frequency.
Definition: MNASolver.h:56
UInt mNumNodes
Number of network and virtual nodes, single line equivalent.
Definition: MNASolver.h:44
std::vector< SwitchConfiguration > mSwitchEvents
Vector of switch times.
Definition: MNASolver.h:104
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:19
void assignMatrixNodeIndices()
Assign simulation node index according to index in the vector.
Definition: MNASolver.cpp:361
UInt mNumVirtualNodes
Number of virtual nodes, single line equivalent.
Definition: MNASolver.h:48
void initializeSystemWithPrecomputedMatrices()
Initialization of system matrices and source vector.
Definition: MNASolver.cpp:238
CPS::Attribute< Matrix >::Ptr mLeftSideVector
Solution vector of unknown quantities.
Definition: MNASolver.h:196
CPS::SimSignalComp::List mSimSignalComps
List of signal type components that do not directly interact with the MNA solver.
Definition: MNASolver.h:76
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.
virtual void createEmptySystemMatrix()=0
Create system matrix.
virtual void solveWithSystemMatrixRecomputation(Real time, Int timeStepCount)=0
Solves the system with variable system matrix.
CPS::MNASwitchInterface::List mSwitches
Definition: MNASolver.h:72
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.
CPS::SimNode< VarType >::List mNodes
List of simulation nodes.
Definition: MNASolver.h:65
virtual CPS::Task::List getTasks() override
Get tasks for scheduler.
Definition: MNASolver.cpp:584
Base class for more specific solvers such as MNA, ODE or IDA.
Definition: Solver.h:30
Bool mSystemMatrixRecomputation
Enable recomputation of system matrix during simulation.
Definition: Solver.h:62
CPS::Logger::Log mSLog
Logger.
Definition: Solver.h:43