DPsim
Loading...
Searching...
No Matches
MNASolver.h
Go to the documentation of this file.
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
25#include <dpsim/Config.h>
26#include <dpsim/DataLogger.h>
28#include <dpsim/Solver.h>
29
30/* std::size_t is the largest data type. No container can store
31 * more than std::size_t elements. Define the number of switches
32 * as the log_2 of this value so that we end up with maximally
33 * std::size_t matrices. The overhead of statically defining this
34 * value should be minimal.
35 **/
36#define SWITCH_NUM sizeof(std::size_t) * 8
37
38namespace DPsim {
40template <typename VarType> class MnaSolver : public Solver {
41protected:
42 // #### General simulation settings ####
62 std::vector<std::pair<UInt, UInt>> mListVariableSystemMatrixEntries;
63
68
69 // #### MNA specific attributes ####
80 std::bitset<SWITCH_NUM> mCurrentSwitchStatus;
84
88 // Keep the source attribute alive for as long as its RHS stamp is used.
89 std::vector<CPS::Attribute<Matrix>::Ptr> mRightVectorStamps;
91 std::vector<std::vector<UInt>> mRightVectorStampIndices;
94
95 // #### MNA specific attributes related to harmonics / additional frequencies ####
97 std::vector<Matrix> mRightSideVectorHarm;
98
99 // #### MNA specific attributes related to system recomputation
105 std::vector<Bool> mVariableCompAlwaysDirty;
108
109 // #### Attributes related to switching ####
113 std::vector<SwitchConfiguration> mSwitchEvents;
115 void updateSwitchStatus();
116
117 // #### Attributes related to logging ####
121 std::shared_ptr<DataLogger> mLeftVectorLog;
123 std::shared_ptr<DataLogger> mRightVectorLog;
124
126 std::vector<Real> mFactorizeTimes;
128 std::vector<Real> mSolveTimes;
130 std::vector<Real> mRecomputationTimes;
131
132 // #### State-space extraction ####
135
138
141 CPS::Logger::Level logLevel = CPS::Logger::Level::info);
142
146 void addRightVectorStamp(const CPS::MNAInterface::Ptr &component);
148 void addRightVectorStamps(Matrix &rightVector, Int stampColumn = -1) const;
150 virtual void initializeSystem();
167 void collectVirtualNodes();
168 // TODO: check if this works with AC sources
170
174 virtual void createEmptySystemMatrix() = 0;
176 virtual void switchedMatrixEmpty(std::size_t index) = 0;
178 virtual void switchedMatrixEmpty(std::size_t swIdx, Int freqIdx) = 0;
181 std::size_t index,
182 std::vector<std::shared_ptr<CPS::MNAInterface>> &comp) = 0;
184 virtual void switchedMatrixStamp(std::size_t swIdx, Int freqIdx,
185 CPS::MNAInterface::List &components,
187
189
190 // #### Methods to implement for system recomputation over time ####
192 virtual void stampVariableSystemMatrix() = 0;
195 Int timeStepCount) = 0;
197 virtual std::shared_ptr<CPS::Task> createSolveTaskRecomp() = 0;
198
200 virtual void logSystemMatrices() = 0;
201
203 virtual std::shared_ptr<CPS::Task> createSolveTask() = 0;
205 virtual std::shared_ptr<CPS::Task> createLogTask() = 0;
207 virtual std::shared_ptr<CPS::Task> createSolveTaskHarm(UInt freqIdx) = 0;
209 virtual std::shared_ptr<CPS::Task> createStateSpaceExtractionTask() = 0;
210
211 // #### Scheduler Task Methods ####
213 virtual void solve(Real time, Int timeStepCount) = 0;
215 virtual void solveWithHarmonics(Real time, Int timeStepCount,
216 Int freqIdx) = 0;
218 virtual void log(Real time, Int timeStepCount) override;
219
220public:
225
227 std::vector<CPS::Attribute<Matrix>::Ptr> mLeftSideVectorHarm;
228
230 virtual ~MnaSolver() {
232 SPDLOG_LOGGER_INFO(mSLog, "Number of system matrix recomputations: {:}",
234 };
235
237 virtual void initialize() override;
238
239 // #### Setter and Getter ####
241 virtual void setSystem(const CPS::SystemTopology &system) override;
247 virtual CPS::Task::List getTasks() override;
248
250 void doStateSpaceExtraction(Bool value = true);
251
254};
255} // namespace DPsim
AttributePointer< Attribute< T > > Ptr
Definition Attribute.h:249
spdlog::level::level_enum Level
Definition Logger.h:33
std::vector< Ptr > List
std::shared_ptr< MNAInterface > Ptr
std::vector< Ptr > List
Definition SimNode.h:35
std::vector< Ptr > List
std::vector< Ptr > List
Definition Task.h:28
std::shared_ptr< MNAStateSpaceExtractor > Ptr
std::bitset< SWITCH_NUM > mCurrentSwitchStatus
Current status of all switches encoded as bitset.
Definition MNASolver.h:80
virtual void setSystem(const CPS::SystemTopology &system) override
Definition MNASolver.cpp:36
CPS::Domain mDomain
Simulation domain, which can be dynamic phasor (DP) or EMT.
Definition MNASolver.h:44
void resolveSystemMatrixRecomputationMode()
Resolve the requested system-matrix recomputation mode.
Matrix & rightSideVector()
Definition MNASolver.h:245
void identifyTopologyObjects()
Identify Nodes and SimPowerComps and SimSignalComps.
std::vector< Matrix > mRightSideVectorHarm
Source vector of known quantities.
Definition MNASolver.h:97
void addRightVectorStamps(Matrix &rightVector, Int stampColumn=-1) const
Accumulate component RHS stamps, using sparse indices when available.
Definition MNASolver.cpp:56
void steadyStateInitialization()
Matrix mRightSideVector
Source vector of known quantities.
Definition MNASolver.h:86
Bool mStateSpaceExtraction
Enables extraction of the MNA-coupled discrete-time state matrix.
Definition MNASolver.h:134
CPS::SystemTopology mSystem
System topology.
Definition MNASolver.h:65
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.
Matrix & leftSideVector()
Definition MNASolver.h:243
Matrix * mLeftSideVectorValue
Cached payload for the per-step solver hot path.
Definition MNASolver.h:224
virtual ~MnaSolver()
Destructor.
Definition MNASolver.h:230
std::vector< Bool > mVariableCompAlwaysDirty
Definition MNASolver.h:105
std::vector< Real > mRecomputationTimes
LU refactorization measurements.
Definition MNASolver.h:130
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:91
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:126
std::vector< CPS::Attribute< Matrix >::Ptr > mLeftSideVectorHarm
Solution vector of unknown quantities (parallel frequencies)
Definition MNASolver.h:227
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:107
std::vector< std::vector< UInt > > mRightVectorStampIndices
Sparse row indices for stamps that can provide their structural support.
Definition MNASolver.h:91
UInt mNumNetMatrixNodeIndices
Number of network nodes, considering individual phases.
Definition MNASolver.h:54
UInt mNumNetNodes
Number of network nodes, single line equivalent.
Definition MNASolver.h:48
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:137
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:60
UInt mNumVirtualMatrixNodeIndices
Number of virtual nodes, considering individual phases.
Definition MNASolver.h:56
CPS::MNASyncGenInterface::List mSyncGen
List of synchronous generators that need iterate to solve the differential equations.
Definition MNASolver.h:82
CPS::MNAInterface::List mMNAIntfSwitches
List of switches if they must be accessed as MNAInterface objects.
Definition MNASolver.h:76
std::shared_ptr< DataLogger > mRightVectorLog
Right side vector logger.
Definition MNASolver.h:123
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:111
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:184
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:128
Int mLastLogTimeStep
Last simulation time step when log was updated.
Definition MNASolver.h:119
std::vector< std::pair< UInt, UInt > > mListVariableSystemMatrixEntries
List of index pairs of varying matrix entries.
Definition MNASolver.h:62
CPS::MNAVariableCompInterface::List mVariableComps
Definition MNASolver.h:104
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:101
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:71
virtual void stampVariableSystemMatrix()=0
Stamps components into the variable system matrix.
std::shared_ptr< DataLogger > mLeftVectorLog
Left side vector logger.
Definition MNASolver.h:121
UInt mNumMatrixNodeIndices
Number of network and virtual nodes, considering individual phases.
Definition MNASolver.h:52
void initializeSystemWithParallelFrequencies()
Initialization of system matrices and source vector.
CPS::MNANonlinearCompInterface::List mNonlinearComps
Definition MNASolver.h:83
void collectVirtualNodes()
UInt mNumHarmMatrixNodeIndices
Number of nodes, excluding the primary frequency.
Definition MNASolver.h:58
UInt mNumNodes
Number of network and virtual nodes, single line equivalent.
Definition MNASolver.h:46
std::vector< SwitchConfiguration > mSwitchEvents
Vector of switch times.
Definition MNASolver.h:113
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:24
void assignMatrixNodeIndices()
Assign simulation node index according to index in the vector.
UInt mNumVirtualNodes
Number of virtual nodes, single line equivalent.
Definition MNASolver.h:50
std::vector< CPS::Attribute< Matrix >::Ptr > mRightVectorStamps
List of all right side vector contributions.
Definition MNASolver.h:89
void addRightVectorStamp(const CPS::MNAInterface::Ptr &component)
Add a component's RHS stamp and cache any known structural row indices.
Definition MNASolver.cpp:41
void initializeSystemWithPrecomputedMatrices()
Initialization of system matrices and source vector.
CPS::Attribute< Matrix >::Ptr mLeftSideVector
Solution vector of unknown quantities.
Definition MNASolver.h:222
CPS::SimSignalComp::List mSimSignalComps
List of signal type components that do not directly interact with the MNA solver.
Definition MNASolver.h:78
virtual void createEmptySystemMatrix()=0
Create system matrix.
std::vector< Bool > mRightVectorStampUsesIndices
Whether each RHS stamp has valid sparse row indices.
Definition MNASolver.h:93
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:83
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:77
CPS::MNASwitchInterface::List mSwitches
Definition MNASolver.h:74
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:67
void initializeStateSpaceExtractor()
Initialization of state-space extraction.
virtual CPS::Task::List getTasks() override
Get tasks for scheduler.
Bool mSystemMatrixRecomputationEnabled
Effective system-matrix recomputation setting used by the solver.
Definition Solver.h:82
CPS::Logger::Log mSLog
Logger.
Definition Solver.h:60
Solver(String name, CPS::Logger::Level logLevel)
Definition Solver.h:90
CPS::Real Real
Definition Definitions.h:18
CPS::String String
Definition Definitions.h:20
CPS::Int Int
Definition Definitions.h:22
CPS::Matrix Matrix
Definition Definitions.h:24
CPS::Bool Bool
Definition Definitions.h:21
CPS::UInt UInt
Definition Definitions.h:23