DPsim
Loading...
Searching...
No Matches
MNASolverDirect.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 <memory>
15#include <unordered_map>
16#include <vector>
17
18#include <dpsim/Config.h>
19#include <dpsim/DataLogger.h>
20#include <dpsim/DenseLUAdapter.h>
21#include <dpsim/DirectLinearSolver.h>
22#include <dpsim/DirectLinearSolverConfiguration.h>
23#include <dpsim/Solver.h>
24#ifdef WITH_KLU
25#include <dpsim/KLUAdapter.h>
26#endif
27#include <dpsim/SparseLUAdapter.h>
28#ifdef WITH_CUDA
29#include <dpsim/GpuDenseAdapter.h>
30#ifdef WITH_CUDA_SPARSE
31#include <dpsim/GpuSparseAdapter.h>
32#endif
33#ifdef WITH_MAGMA
34#include <dpsim/GpuMagmaAdapter.h>
35#endif
36#endif
37#include <dpsim-models/AttributeList.h>
38#include <dpsim-models/SimPowerComp.h>
39#include <dpsim-models/SimSignalComp.h>
40#include <dpsim-models/Solver/MNASwitchInterface.h>
41#include <dpsim-models/Solver/MNAVariableCompInterface.h>
42#include <dpsim/MNASolver.h>
43
44namespace DPsim {
45
46enum DirectLinearSolverImpl {
47 Undef = 0,
48 KLU,
49 SparseLU,
50 DenseLU,
51 CUDADense,
52 CUDASparse,
53 CUDAMagma,
54 Plugin
55};
56
58template <typename VarType> class MnaSolverDirect : public MnaSolver<VarType> {
59
60protected:
61 // #### Data structures for precomputed switch matrices (optionally with parallel frequencies) ####
63 std::unordered_map<std::bitset<SWITCH_NUM>, std::vector<SparseMatrix>>
66 std::unordered_map<std::bitset<SWITCH_NUM>,
67 std::vector<std::shared_ptr<DirectLinearSolver>>>
69
70 // #### Data structures for system recomputation over time ####
72 SparseMatrix mBaseSystemMatrix;
76 std::shared_ptr<DirectLinearSolver> mDirectLinearSolverVariableSystemMatrix;
78 DirectLinearSolverImpl mImplementationInUse;
81
86
87 using MnaSolver<VarType>::mSwitches;
88 using MnaSolver<VarType>::mMNAIntfSwitches;
89 using MnaSolver<VarType>::mMNAComponents;
90 using MnaSolver<VarType>::mVariableComps;
92 using MnaSolver<VarType>::mRightSideVector;
93 using MnaSolver<VarType>::mLeftSideVector;
95 using MnaSolver<VarType>::mRightVectorStamps;
96 using MnaSolver<VarType>::mNumNetNodes;
97 using MnaSolver<VarType>::mNodes;
98 using MnaSolver<VarType>::mIsInInitialization;
100 using MnaSolver<VarType>::mLeftSideVectorHarm;
101 using MnaSolver<VarType>::mFrequencyParallel;
102 using MnaSolver<VarType>::mSLog;
105 using MnaSolver<VarType>::mNumRecomputations;
106 using MnaSolver<VarType>::mSyncGen;
107 using MnaSolver<VarType>::mFactorizeTimes;
108 using MnaSolver<VarType>::mSolveTimes;
109 using MnaSolver<VarType>::mRecomputationTimes;
112 using MnaSolver<VarType>::mStateSpaceExtractor;
113
114 // #### General
116 void createEmptySystemMatrix() override;
117
118 // #### Methods for precomputed switch matrices (optionally with parallel frequencies) ####
120 void switchedMatrixEmpty(std::size_t index) override;
122 void switchedMatrixEmpty(std::size_t swIdx, Int freqIdx) override;
125 std::size_t index,
126 std::vector<std::shared_ptr<CPS::MNAInterface>> &comp) override;
127
128 // #### Methods for system recomputation over time ####
130 void stampVariableSystemMatrix() override;
133 Int timeStepCount) override;
135 std::shared_ptr<CPS::Task> createSolveTaskRecomp() override;
137 virtual void recomputeSystemMatrix(Real time);
139 void extractStateSpace();
140
141 // #### Scheduler Task Methods ####
143 std::shared_ptr<CPS::Task> createSolveTask() override;
145 std::shared_ptr<CPS::Task> createLogTask() override;
147 std::shared_ptr<CPS::Task> createSolveTaskHarm(UInt freqIdx) override;
149 std::shared_ptr<CPS::Task> createStateSpaceExtractionTask() override;
151 void logSystemMatrices() override;
153 void solve(Real time, Int timeStepCount) override;
155 void solveWithHarmonics(Real time, Int timeStepCount, Int freqIdx) override;
156
158 void logSolveTime();
163
165 std::shared_ptr<DirectLinearSolver>
166 createDirectSolverImplementation(CPS::Logger::Log mSLog);
167
168public:
171 MnaSolverDirect(String name, CPS::Domain domain = CPS::Domain::DP,
172 CPS::Logger::Level logLevel = CPS::Logger::Level::info);
173
175 virtual ~MnaSolverDirect() = default;
176
178 void
179 setDirectLinearSolverImplementation(DirectLinearSolverImpl implementation);
180
183 DirectLinearSolverConfiguration &configuration) override;
184
186 void logLUTimes() override;
187
189 int mIter = 0;
190
191 // #### MNA Solver Tasks ####
193 class SolveTask : public CPS::Task {
194 public:
195 SolveTask(MnaSolverDirect<VarType> &solver)
196 : Task(solver.mName + ".Solve"), mSolver(solver) {
197
198 for (auto it : solver.mMNAComponents) {
199 if (it->getRightVector()->get().size() != 0)
200 mAttributeDependencies.push_back(it->getRightVector());
201 }
202 for (auto node : solver.mNodes) {
203 mModifiedAttributes.push_back(node->mVoltage);
204 }
205 mModifiedAttributes.push_back(solver.mLeftSideVector);
206 }
207
208 void execute(Real time, Int timeStepCount) {
209 mSolver.solve(time, timeStepCount);
210 }
211
212 private:
214 };
215
217 class SolveTaskHarm : public CPS::Task {
218 public:
219 SolveTaskHarm(MnaSolverDirect<VarType> &solver, UInt freqIdx)
220 : Task(solver.mName + ".Solve"), mSolver(solver), mFreqIdx(freqIdx) {
221
222 for (auto it : solver.mMNAComponents) {
223 if (it->getRightVector()->get().size() != 0)
224 mAttributeDependencies.push_back(it->getRightVector());
225 }
226 for (auto node : solver.mNodes) {
227 mModifiedAttributes.push_back(node->mVoltage);
228 }
229 for (auto leftVec : solver.mLeftSideVectorHarm) {
230 mModifiedAttributes.push_back(leftVec);
231 }
232 }
233
234 void execute(Real time, Int timeStepCount) {
235 mSolver.solveWithHarmonics(time, timeStepCount, mFreqIdx);
236 }
237
238 private:
240 UInt mFreqIdx;
241 };
242
244 class SolveTaskRecomp : public CPS::Task {
245 public:
246 SolveTaskRecomp(MnaSolverDirect<VarType> &solver)
247 : Task(solver.mName + ".Solve"), mSolver(solver) {
248
249 for (auto it : solver.mMNAComponents) {
250 if (it->getRightVector()->get().size() != 0)
251 mAttributeDependencies.push_back(it->getRightVector());
252 }
253 for (auto it : solver.mMNAIntfVariableComps) {
254 if (it->getRightVector()->get().size() != 0)
255 mAttributeDependencies.push_back(it->getRightVector());
256 }
257 for (auto node : solver.mNodes) {
258 mModifiedAttributes.push_back(node->mVoltage);
259 }
260 mModifiedAttributes.push_back(solver.mLeftSideVector);
261 }
262
263 void execute(Real time, Int timeStepCount) {
264 mSolver.solveWithSystemMatrixRecomputation(time, timeStepCount);
265 mSolver.log(time, timeStepCount);
266 }
267
268 private:
270 };
271
273 class StateSpaceExtractionTask : public CPS::Task {
274 public:
275 StateSpaceExtractionTask(MnaSolverDirect<VarType> &solver)
276 : Task(solver.mName + ".StateSpaceExtraction"), mSolver(solver) {
277 mAttributeDependencies.push_back(solver.mLeftSideVector);
278 mModifiedAttributes.push_back(Scheduler::external);
279 }
280
281 void execute(Real time, Int timeStepCount) { mSolver.extractStateSpace(); }
282
283 private:
285 };
286
288 class LogTask : public CPS::Task {
289 public:
290 LogTask(MnaSolverDirect<VarType> &solver)
291 : Task(solver.mName + ".Log"), mSolver(solver) {
292 mAttributeDependencies.push_back(solver.mLeftSideVector);
293 mModifiedAttributes.push_back(Scheduler::external);
294 }
295
296 void execute(Real time, Int timeStepCount) {
297 mSolver.log(time, timeStepCount);
298 }
299
300 private:
302 };
303};
304} // namespace DPsim
Tasks to be defined by every component.
Definition Task.h:25
std::shared_ptr< CPS::Task > createSolveTask() override
Create a solve task for this solver implementation.
void stampVariableSystemMatrix() override
Stamps components into the variable system matrix.
void extractStateSpace()
Runs state-space extraction using the active linear solver.
void logSolveTime()
Logging of the right-hand-side solution time.
virtual ~MnaSolverDirect()=default
Destructor.
void logFactorizationTime()
Logging of the LU factorization time.
void solve(Real time, Int timeStepCount) override
Solves system for single frequency.
void logRecomputationTime()
Logging of the LU refactorization time.
virtual void recomputeSystemMatrix(Real time)
Recomputes systems matrix.
SparseMatrix mVariableSystemMatrix
System matrix including stamp of static and variable elements.
SparseMatrix mBaseSystemMatrix
System matrix including all static elements.
std::unordered_map< std::bitset< SWITCH_NUM >, std::vector< SparseMatrix > > mSwitchedMatrices
Map of system matrices where the key is the bitset describing the switch states.
std::shared_ptr< DirectLinearSolver > createDirectSolverImplementation(CPS::Logger::Log mSLog)
Returns a pointer to an object of type DirectLinearSolver.
void setDirectLinearSolverConfiguration(DirectLinearSolverConfiguration &configuration) override
Sets the linear solver configuration.
DirectLinearSolverImpl mImplementationInUse
LU factorization indicator.
std::shared_ptr< DirectLinearSolver > mDirectLinearSolverVariableSystemMatrix
LU factorization of variable system matrix.
void setDirectLinearSolverImplementation(DirectLinearSolverImpl implementation)
Sets the linear solver to "implementation" and creates an object.
Bool mSystemMatrixChanged
Tracks active MNA system-matrix changes from the last solve step.
std::shared_ptr< CPS::Task > createSolveTaskHarm(UInt freqIdx) override
Create a solve task for this solver implementation.
std::shared_ptr< CPS::Task > createSolveTaskRecomp() override
Create a solve task for recomputation solver.
void logSystemMatrices() override
Logging of system matrices and source vector.
Bool mVariableComponentChanged
Tracks variable-component changes from the last solve step.
void createEmptySystemMatrix() override
Create system matrix.
std::shared_ptr< CPS::Task > createLogTask() override
Create a solve task for this solver implementation.
void logLUTimes() override
log LU decomposition times
void switchedMatrixEmpty(std::size_t index) override
Sets all entries in the matrix with the given switch index to zero.
void solveWithSystemMatrixRecomputation(Real time, Int timeStepCount) override
Solves the system with variable system matrix.
std::shared_ptr< CPS::Task > createStateSpaceExtractionTask() override
Create state-space extraction task for this solver implementation.
void switchedMatrixStamp(std::size_t index, std::vector< std::shared_ptr< CPS::MNAInterface > > &comp) override
Applies a component stamp to the matrix with the given switch index.
std::unordered_map< std::bitset< SWITCH_NUM >, std::vector< std::shared_ptr< DirectLinearSolver > > > mDirectLinearSolvers
Map of direct linear solvers related to the system matrices.
MnaSolverDirect(String name, CPS::Domain domain=CPS::Domain::DP, CPS::Logger::Level logLevel=CPS::Logger::Level::info)
DirectLinearSolverConfiguration mConfigurationInUse
LU factorization configuration.
void solveWithHarmonics(Real time, Int timeStepCount, Int freqIdx) override
Solves system for multiple frequencies.
std::bitset< SWITCH_NUM > mCurrentSwitchStatus
Current status of all switches encoded as bitset.
Definition MNASolver.h:79
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
std::vector< Real > mRecomputationTimes
LU refactorization measurements.
Definition MNASolver.h:122
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 mNumNetNodes
Number of network nodes, single line equivalent.
Definition MNASolver.h:47
MNAStateSpaceExtractor::Ptr mStateSpaceExtractor
Extractor for the MNA-coupled state-space model.
Definition MNASolver.h:129
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::vector< const Matrix * > mRightVectorStamps
List of all right side vector contributions.
Definition MNASolver.h:86
std::vector< Real > mSolveTimes
Right-hand side solution measurements.
Definition MNASolver.h:120
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
Int mNumRecomputations
Number of system matrix recomputations.
Definition MNASolver.h:94
CPS::MNAInterface::List mMNAComponents
List of MNA components with static stamp into system matrix.
Definition MNASolver.h:70
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
CPS::Attribute< Matrix >::Ptr mLeftSideVector
Solution vector of unknown quantities.
Definition MNASolver.h:208
CPS::MNASwitchInterface::List mSwitches
Definition MNASolver.h:73
CPS::SimNode< VarType >::List mNodes
List of simulation nodes.
Definition MNASolver.h:66
String mName
Name for logging.
Definition Solver.h:39
Bool mSystemMatrixRecomputation
Enable recomputation of system matrix during simulation.
Definition Solver.h:64
CPS::Logger::Log mSLog
Logger.
Definition Solver.h:45
Bool mIsInInitialization
Determines if solver is in initialization phase, which requires different behavior.
Definition Solver.h:59
Bool mFrequencyParallel
Activates parallelized computation of frequencies.
Definition Solver.h:49