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_SPARSE
25#include <dpsim/SparseLUAdapter.h>
26#endif
27#ifdef WITH_KLU
28#include <dpsim/KLUAdapter.h>
29#endif
30#ifdef WITH_CUDA
31#include <dpsim/GpuDenseAdapter.h>
32#ifdef WITH_CUDA_SPARSE
33#include <dpsim/GpuSparseAdapter.h>
34#endif
35#ifdef WITH_MAGMA
36#include <dpsim/GpuMagmaAdapter.h>
37#endif
38#endif
39#include <dpsim-models/AttributeList.h>
40#include <dpsim-models/SimPowerComp.h>
41#include <dpsim-models/SimSignalComp.h>
42#include <dpsim-models/Solver/MNASwitchInterface.h>
43#include <dpsim-models/Solver/MNAVariableCompInterface.h>
44#include <dpsim/MNASolver.h>
45
46namespace DPsim {
47
48enum DirectLinearSolverImpl {
49 Undef = 0,
50 KLU,
51 SparseLU,
52 DenseLU,
53 CUDADense,
54 CUDASparse,
55 CUDAMagma,
56 Plugin
57};
58
60template <typename VarType> class MnaSolverDirect : public MnaSolver<VarType> {
61
62protected:
63 // #### Data structures for precomputed switch matrices (optionally with parallel frequencies) ####
65 std::unordered_map<std::bitset<SWITCH_NUM>, std::vector<SparseMatrix>>
68 std::unordered_map<std::bitset<SWITCH_NUM>,
69 std::vector<std::shared_ptr<DirectLinearSolver>>>
71
72 // #### Data structures for system recomputation over time ####
74 SparseMatrix mBaseSystemMatrix;
78 std::shared_ptr<DirectLinearSolver> mDirectLinearSolverVariableSystemMatrix;
80 DirectLinearSolverImpl mImplementationInUse;
83
88
89 using MnaSolver<VarType>::mSwitches;
90 using MnaSolver<VarType>::mMNAIntfSwitches;
91 using MnaSolver<VarType>::mMNAComponents;
92 using MnaSolver<VarType>::mVariableComps;
94 using MnaSolver<VarType>::mRightSideVector;
95 using MnaSolver<VarType>::mLeftSideVector;
97 using MnaSolver<VarType>::mRightVectorStamps;
98 using MnaSolver<VarType>::mNumNetNodes;
99 using MnaSolver<VarType>::mNodes;
100 using MnaSolver<VarType>::mIsInInitialization;
101 using MnaSolver<VarType>::mRightSideVectorHarm;
102 using MnaSolver<VarType>::mLeftSideVectorHarm;
103 using MnaSolver<VarType>::mFrequencyParallel;
104 using MnaSolver<VarType>::mSLog;
107 using MnaSolver<VarType>::mNumRecomputations;
108 using MnaSolver<VarType>::mSyncGen;
109 using MnaSolver<VarType>::mFactorizeTimes;
110 using MnaSolver<VarType>::mSolveTimes;
111 using MnaSolver<VarType>::mRecomputationTimes;
114 using MnaSolver<VarType>::mStateSpaceExtractor;
115
116 // #### General
118 void createEmptySystemMatrix() override;
119
120 // #### Methods for precomputed switch matrices (optionally with parallel frequencies) ####
122 void switchedMatrixEmpty(std::size_t index) override;
124 void switchedMatrixEmpty(std::size_t swIdx, Int freqIdx) override;
127 std::size_t index,
128 std::vector<std::shared_ptr<CPS::MNAInterface>> &comp) override;
129
130 // #### Methods for system recomputation over time ####
132 void stampVariableSystemMatrix() override;
135 Int timeStepCount) override;
137 std::shared_ptr<CPS::Task> createSolveTaskRecomp() override;
139 virtual void recomputeSystemMatrix(Real time);
141 void extractStateSpace(Real time);
142
143 // #### Scheduler Task Methods ####
145 std::shared_ptr<CPS::Task> createSolveTask() override;
147 std::shared_ptr<CPS::Task> createLogTask() override;
149 std::shared_ptr<CPS::Task> createSolveTaskHarm(UInt freqIdx) override;
151 std::shared_ptr<CPS::Task> createStateSpaceExtractionTask() override;
153 void logSystemMatrices() override;
155 void solve(Real time, Int timeStepCount) override;
157 void solveWithHarmonics(Real time, Int timeStepCount, Int freqIdx) override;
158
160 void logSolveTime();
165
167 std::shared_ptr<DirectLinearSolver>
168 createDirectSolverImplementation(CPS::Logger::Log mSLog);
169
170public:
173 MnaSolverDirect(String name, CPS::Domain domain = CPS::Domain::DP,
174 CPS::Logger::Level logLevel = CPS::Logger::Level::info);
175
177 virtual ~MnaSolverDirect() = default;
178
180 void
181 setDirectLinearSolverImplementation(DirectLinearSolverImpl implementation);
182
185 DirectLinearSolverConfiguration &configuration) override;
186
188 void logLUTimes() override;
189
191 int mIter = 0;
192
193 // #### MNA Solver Tasks ####
195 class SolveTask : public CPS::Task {
196 public:
197 SolveTask(MnaSolverDirect<VarType> &solver)
198 : Task(solver.mName + ".Solve"), mSolver(solver) {
199
200 for (auto it : solver.mMNAComponents) {
201 if (it->getRightVector()->get().size() != 0)
202 mAttributeDependencies.push_back(it->getRightVector());
203 }
204 for (auto node : solver.mNodes) {
205 mModifiedAttributes.push_back(node->mVoltage);
206 }
207 mModifiedAttributes.push_back(solver.mLeftSideVector);
208 }
209
210 void execute(Real time, Int timeStepCount) {
211 mSolver.solve(time, timeStepCount);
212 }
213
214 private:
216 };
217
219 class SolveTaskHarm : public CPS::Task {
220 public:
221 SolveTaskHarm(MnaSolverDirect<VarType> &solver, UInt freqIdx)
222 : Task(solver.mName + ".Solve"), mSolver(solver), mFreqIdx(freqIdx) {
223
224 for (auto it : solver.mMNAComponents) {
225 if (it->getRightVector()->get().size() != 0)
226 mAttributeDependencies.push_back(it->getRightVector());
227 }
228 for (auto node : solver.mNodes) {
229 mModifiedAttributes.push_back(node->mVoltage);
230 }
231 for (auto leftVec : solver.mLeftSideVectorHarm) {
232 mModifiedAttributes.push_back(leftVec);
233 }
234 }
235
236 void execute(Real time, Int timeStepCount) {
237 mSolver.solveWithHarmonics(time, timeStepCount, mFreqIdx);
238 }
239
240 private:
242 UInt mFreqIdx;
243 };
244
246 class SolveTaskRecomp : public CPS::Task {
247 public:
248 SolveTaskRecomp(MnaSolverDirect<VarType> &solver)
249 : Task(solver.mName + ".Solve"), mSolver(solver) {
250
251 for (auto it : solver.mMNAComponents) {
252 if (it->getRightVector()->get().size() != 0)
253 mAttributeDependencies.push_back(it->getRightVector());
254 }
255 for (auto it : solver.mMNAIntfVariableComps) {
256 if (it->getRightVector()->get().size() != 0)
257 mAttributeDependencies.push_back(it->getRightVector());
258 }
259 for (auto node : solver.mNodes) {
260 mModifiedAttributes.push_back(node->mVoltage);
261 }
262 mModifiedAttributes.push_back(solver.mLeftSideVector);
263 }
264
265 void execute(Real time, Int timeStepCount) {
266 mSolver.solveWithSystemMatrixRecomputation(time, timeStepCount);
267 mSolver.log(time, timeStepCount);
268 }
269
270 private:
272 };
273
275 class StateSpaceExtractionTask : public CPS::Task {
276 public:
277 StateSpaceExtractionTask(MnaSolverDirect<VarType> &solver)
278 : Task(solver.mName + ".StateSpaceExtraction"), mSolver(solver) {
279 mAttributeDependencies.push_back(solver.mLeftSideVector);
280 mModifiedAttributes.push_back(Scheduler::external);
281 }
282
283 void execute(Real time, Int timeStepCount) {
284 mSolver.extractStateSpace(time);
285 }
286
287 private:
289 };
290
292 class LogTask : public CPS::Task {
293 public:
294 LogTask(MnaSolverDirect<VarType> &solver)
295 : Task(solver.mName + ".Log"), mSolver(solver) {
296 mAttributeDependencies.push_back(solver.mLeftSideVector);
297 mModifiedAttributes.push_back(Scheduler::external);
298 }
299
300 void execute(Real time, Int timeStepCount) {
301 mSolver.log(time, timeStepCount);
302 }
303
304 private:
306 };
307};
308} // 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 logSolveTime()
Logging of the right-hand-side solution time.
virtual ~MnaSolverDirect()=default
Destructor.
void logFactorizationTime()
Logging of the LU factorization time.
void extractStateSpace(Real time)
Runs state-space extraction using the active linear solver.
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:23
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