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>>>
119 std::vector<std::shared_ptr<CPS::MNAInterface>> &comp)
override;
126 Int timeStepCount)
override;
142 void solve(Real time, Int timeStepCount)
override;
154 std::shared_ptr<DirectLinearSolver>
161 CPS::Logger::Level logLevel = CPS::Logger::Level::info);
185 : Task(solver.
mName +
".Solve"), mSolver(solver) {
188 if (it->getRightVector()->get().size() != 0)
189 mAttributeDependencies.push_back(it->getRightVector());
191 for (
auto node : solver.
mNodes) {
192 mModifiedAttributes.push_back(node->mVoltage);
197 void execute(Real time, Int timeStepCount) {
198 mSolver.solve(time, timeStepCount);
209 : Task(solver.
mName +
".Solve"), mSolver(solver), mFreqIdx(freqIdx) {
212 if (it->getRightVector()->get().size() != 0)
213 mAttributeDependencies.push_back(it->getRightVector());
215 for (
auto node : solver.
mNodes) {
216 mModifiedAttributes.push_back(node->mVoltage);
219 mModifiedAttributes.push_back(leftVec);
223 void execute(Real time, Int timeStepCount) {
224 mSolver.solveWithHarmonics(time, timeStepCount, mFreqIdx);
236 : Task(solver.
mName +
".Solve"), mSolver(solver) {
239 if (it->getRightVector()->get().size() != 0)
240 mAttributeDependencies.push_back(it->getRightVector());
243 if (it->getRightVector()->get().size() != 0)
244 mAttributeDependencies.push_back(it->getRightVector());
246 for (
auto node : solver.
mNodes) {
247 mModifiedAttributes.push_back(node->mVoltage);
252 void execute(Real time, Int timeStepCount) {
253 mSolver.solveWithSystemMatrixRecomputation(time, timeStepCount);
254 mSolver.log(time, timeStepCount);
265 : Task(solver.
mName +
".Log"), mSolver(solver) {
267 mModifiedAttributes.push_back(Scheduler::external);
270 void execute(Real time, Int timeStepCount) {
271 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 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.
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.
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.
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.
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.
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.