15#include <unordered_map>
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>
25#include <dpsim/KLUAdapter.h>
27#include <dpsim/SparseLUAdapter.h>
29#include <dpsim/GpuDenseAdapter.h>
30#ifdef WITH_CUDA_SPARSE
31#include <dpsim/GpuSparseAdapter.h>
34#include <dpsim/GpuMagmaAdapter.h>
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>
46enum DirectLinearSolverImpl {
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>>>
126 std::vector<std::shared_ptr<CPS::MNAInterface>> &comp)
override;
133 Int timeStepCount)
override;
153 void solve(Real time, Int timeStepCount)
override;
165 std::shared_ptr<DirectLinearSolver>
172 CPS::Logger::Level logLevel = CPS::Logger::Level::info);
196 : Task(solver.
mName +
".Solve"), mSolver(solver) {
199 if (it->getRightVector()->get().size() != 0)
200 mAttributeDependencies.push_back(it->getRightVector());
202 for (
auto node : solver.
mNodes) {
203 mModifiedAttributes.push_back(node->mVoltage);
208 void execute(Real time, Int timeStepCount) {
209 mSolver.solve(time, timeStepCount);
220 : Task(solver.
mName +
".Solve"), mSolver(solver), mFreqIdx(freqIdx) {
223 if (it->getRightVector()->get().size() != 0)
224 mAttributeDependencies.push_back(it->getRightVector());
226 for (
auto node : solver.
mNodes) {
227 mModifiedAttributes.push_back(node->mVoltage);
230 mModifiedAttributes.push_back(leftVec);
234 void execute(Real time, Int timeStepCount) {
235 mSolver.solveWithHarmonics(time, timeStepCount, mFreqIdx);
247 : Task(solver.
mName +
".Solve"), mSolver(solver) {
250 if (it->getRightVector()->get().size() != 0)
251 mAttributeDependencies.push_back(it->getRightVector());
254 if (it->getRightVector()->get().size() != 0)
255 mAttributeDependencies.push_back(it->getRightVector());
257 for (
auto node : solver.
mNodes) {
258 mModifiedAttributes.push_back(node->mVoltage);
263 void execute(Real time, Int timeStepCount) {
264 mSolver.solveWithSystemMatrixRecomputation(time, timeStepCount);
265 mSolver.log(time, timeStepCount);
276 : Task(solver.
mName +
".StateSpaceExtraction"), mSolver(solver) {
278 mModifiedAttributes.push_back(Scheduler::external);
281 void execute(Real time, Int timeStepCount) { mSolver.extractStateSpace(); }
291 : Task(solver.
mName +
".Log"), mSolver(solver) {
293 mModifiedAttributes.push_back(Scheduler::external);
296 void execute(Real time, Int timeStepCount) {
297 mSolver.log(time, timeStepCount);
Tasks to be defined by every component.
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.
std::vector< Matrix > mRightSideVectorHarm
Source vector of known quantities.
Matrix mRightSideVector
Source vector of known quantities.
Bool mStateSpaceExtraction
Enables extraction of the MNA-coupled discrete-time state matrix.
std::vector< Real > mRecomputationTimes
LU refactorization measurements.
std::vector< Real > mFactorizeTimes
LU factorization measurements.
std::vector< CPS::Attribute< Matrix >::Ptr > mLeftSideVectorHarm
Solution vector of unknown quantities (parallel frequencies)
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.
UInt mNumNetNodes
Number of network nodes, single line equivalent.
MNAStateSpaceExtractor::Ptr mStateSpaceExtractor
Extractor for the MNA-coupled state-space model.
CPS::MNASyncGenInterface::List mSyncGen
List of synchronous generators that need iterate to solve the differential equations.
CPS::MNAInterface::List mMNAIntfSwitches
List of switches if they must be accessed as MNAInterface objects.
std::vector< const Matrix * > mRightVectorStamps
List of all right side vector contributions.
std::vector< Real > mSolveTimes
Right-hand side solution measurements.
std::vector< std::pair< UInt, UInt > > mListVariableSystemMatrixEntries
List of index pairs of varying matrix entries.
CPS::MNAVariableCompInterface::List mVariableComps
Int mNumRecomputations
Number of system matrix recomputations.
CPS::MNAInterface::List mMNAComponents
List of MNA components with static stamp into system matrix.
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.
CPS::Attribute< Matrix >::Ptr mLeftSideVector
Solution vector of unknown quantities.
CPS::MNASwitchInterface::List mSwitches
CPS::SimNode< VarType >::List mNodes
List of simulation nodes.
String mName
Name for logging.
Bool mSystemMatrixRecomputation
Enable recomputation of system matrix during simulation.
CPS::Logger::Log mSLog
Logger.
Bool mIsInInitialization
Determines if solver is in initialization phase, which requires different behavior.
Bool mFrequencyParallel
Activates parallelized computation of frequencies.