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
24#include <dpsim/Config.h>
25#include <dpsim/DataLogger.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 ####
61 std::vector<std::pair<UInt, UInt>> mListVariableSystemMatrixEntries;
62
67
68 // #### MNA specific attributes ####
79 std::bitset<SWITCH_NUM> mCurrentSwitchStatus;
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
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
130
133 CPS::Logger::Level logLevel = CPS::Logger::Level::info);
134
138 virtual void initializeSystem();
155 void collectVirtualNodes();
156 // TODO: check if this works with AC sources
158
162 virtual void createEmptySystemMatrix() = 0;
164 virtual void switchedMatrixEmpty(std::size_t index) = 0;
166 virtual void switchedMatrixEmpty(std::size_t swIdx, Int freqIdx) = 0;
169 std::size_t index,
170 std::vector<std::shared_ptr<CPS::MNAInterface>> &comp) = 0;
172 virtual void switchedMatrixStamp(std::size_t swIdx, Int freqIdx,
173 CPS::MNAInterface::List &components,
175
177
178 // #### Methods to implement for system recomputation over time ####
180 virtual void stampVariableSystemMatrix() = 0;
183 Int timeStepCount) = 0;
185 virtual std::shared_ptr<CPS::Task> createSolveTaskRecomp() = 0;
186
188 virtual void logSystemMatrices() = 0;
189
191 virtual std::shared_ptr<CPS::Task> createSolveTask() = 0;
193 virtual std::shared_ptr<CPS::Task> createLogTask() = 0;
195 virtual std::shared_ptr<CPS::Task> createSolveTaskHarm(UInt freqIdx) = 0;
197 virtual std::shared_ptr<CPS::Task> createStateSpaceExtractionTask() = 0;
198
199 // #### Scheduler Task Methods ####
201 virtual void solve(Real time, Int timeStepCount) = 0;
203 virtual void solveWithHarmonics(Real time, Int timeStepCount,
204 Int freqIdx) = 0;
206 virtual void log(Real time, Int timeStepCount) override;
207
208public:
211
213 std::vector<CPS::Attribute<Matrix>::Ptr> mLeftSideVectorHarm;
214
216 virtual ~MnaSolver() {
218 SPDLOG_LOGGER_INFO(mSLog, "Number of system matrix recomputations: {:}",
220 };
221
223 virtual void initialize() override;
224
225 // #### Setter and Getter ####
227 virtual void setSystem(const CPS::SystemTopology &system) override;
233 virtual CPS::Task::List getTasks() override;
234
236 void doStateSpaceExtraction(Bool value = true);
237
240};
241} // namespace DPsim
AttributePointer< Attribute< T > > Ptr
Definition Attribute.h:250
spdlog::level::level_enum Level
Definition Logger.h:33
std::vector< Ptr > List
std::vector< Ptr > List
Definition SimNode.h:33
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:79
virtual void setSystem(const CPS::SystemTopology &system) override
Definition MNASolver.cpp:35
CPS::Domain mDomain
Simulation domain, which can be dynamic phasor (DP) or EMT.
Definition MNASolver.h:43
void resolveSystemMatrixRecomputationMode()
Resolve the requested system-matrix recomputation mode.
Matrix & rightSideVector()
Definition MNASolver.h:231
void identifyTopologyObjects()
Identify Nodes and SimPowerComps and SimSignalComps.
std::vector< Matrix > mRightSideVectorHarm
Source vector of known quantities.
Definition MNASolver.h:90
void steadyStateInitialization()
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.
Matrix & leftSideVector()
Definition MNASolver.h:229
virtual ~MnaSolver()
Destructor.
Definition MNASolver.h:216
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:54
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:213
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:172
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:23
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:210
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:46
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:40
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 mSystemMatrixRecomputationEnabled
Effective system-matrix recomputation setting used by the solver.
Definition Solver.h:77
CPS::Logger::Log mSLog
Logger.
Definition Solver.h:55
Solver(String name, CPS::Logger::Level logLevel)
Definition Solver.h:83
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