9#include <dpsim/MNASolverDirect.h>
10#include <dpsim/SequentialScheduler.h>
17template <
typename VarType>
19 CPS::Logger::Level logLevel)
20 :
MnaSolver<VarType>(name, domain, logLevel) {
24template <
typename VarType>
29template <
typename VarType>
35template <
typename VarType>
37 std::size_t index, std::vector<std::shared_ptr<CPS::MNAInterface>> &comp) {
38 auto bit = std::bitset<SWITCH_NUM>(index);
40 for (
auto component : comp) {
41 component->mnaApplySystemMatrixStamp(sys);
43 for (UInt i = 0; i <
mSwitches.size(); ++i)
44 mSwitches[i]->mnaApplySwitchSystemMatrixStamp(bit[i], sys, 0);
49 auto start = std::chrono::steady_clock::now();
51 auto end = std::chrono::steady_clock::now();
52 std::chrono::duration<Real> diff = end - start;
56template <
typename VarType>
65 SPDLOG_LOGGER_INFO(
mSLog,
66 "Number of variable Elements: {}"
67 "\nNumber of MNA components: {}",
74 SPDLOG_LOGGER_INFO(
mSLog,
"Base matrix with only static elements: {}",
82 SPDLOG_LOGGER_INFO(
mSLog,
"Stamping variable elements");
86 SPDLOG_LOGGER_INFO(
mSLog,
"Initial system matrix with variable elements {}",
95 auto start = std::chrono::steady_clock::now();
97 auto end = std::chrono::steady_clock::now();
98 std::chrono::duration<Real> diff = end - start;
102template <
typename VarType>
104 Real time, Int timeStepCount) {
118 auto start = std::chrono::steady_clock::now();
121 auto end = std::chrono::steady_clock::now();
122 std::chrono::duration<Real> diff = end - start;
126 for (UInt nodeIdx = 0; nodeIdx <
mNumNetNodes; ++nodeIdx)
132template <
typename VarType>
143 auto start = std::chrono::steady_clock::now();
146 auto end = std::chrono::steady_clock::now();
147 std::chrono::duration<Real> diff = end - start;
162 for (std::size_t i = 0; i < (1ULL <<
mSwitches.size()); i++) {
163 auto bit = std::bitset<SWITCH_NUM>(i);
177 for (UInt i = 0; i < std::pow(2,
mSwitches.size()); ++i) {
178 for (Int freq = 0; freq <
mSystem.mFrequencies.size(); ++freq) {
179 auto bit = std::bitset<SWITCH_NUM>(i);
192 for (std::size_t i = 0; i < (1ULL <<
mSwitches.size()); i++) {
193 auto bit = std::bitset<SWITCH_NUM>(i);
202template <
typename VarType>
204 return std::make_shared<MnaSolverDirect<VarType>::SolveTask>(*this);
207template <
typename VarType>
209 return std::make_shared<MnaSolverDirect<VarType>::SolveTaskRecomp>(*this);
212template <
typename VarType>
213std::shared_ptr<CPS::Task>
215 return std::make_shared<MnaSolverDirect<VarType>::SolveTaskHarm>(*
this,
219template <
typename VarType>
221 return std::make_shared<MnaSolverDirect<VarType>::LogTask>(*this);
224template <
typename VarType>
237 std::chrono::steady_clock::time_point start;
239 start = std::chrono::steady_clock::now();
245 auto end = std::chrono::steady_clock::now();
246 std::chrono::duration<Real> diff = end - start;
261 UInt numCompsRequireIter;
264 numCompsRequireIter = 0;
266 if (syncGen->requiresIteration())
267 numCompsRequireIter++;
270 if (numCompsRequireIter > 0) {
280 syncGen->correctorStep();
287 auto start = std::chrono::steady_clock::now();
291 auto end = std::chrono::steady_clock::now();
292 std::chrono::duration<Real> diff = end - start;
301 }
while (numCompsRequireIter > 0);
305 for (UInt nodeIdx = 0; nodeIdx <
mNumNetNodes; ++nodeIdx)
311template <
typename VarType>
327 for (UInt i = 0; i < mSwitchedMatrices[std::bitset<SWITCH_NUM>(0)].size();
329 SPDLOG_LOGGER_INFO(
mSLog,
"System matrix for frequency: {:d} \n{:s}", i,
330 Logger::matrixToString(
335 SPDLOG_LOGGER_INFO(
mSLog,
"Right side vector for frequency: {:d} \n{:s}",
340 SPDLOG_LOGGER_INFO(
mSLog,
"Summarizing matrices: ");
341 SPDLOG_LOGGER_INFO(
mSLog,
"Base matrix with only static elements: {}",
343 SPDLOG_LOGGER_INFO(
mSLog,
"Initial system matrix with variable elements {}",
345 SPDLOG_LOGGER_INFO(
mSLog,
"Right side vector: {}",
349 SPDLOG_LOGGER_INFO(
mSLog,
"System matrix: \n{}",
352 SPDLOG_LOGGER_INFO(
mSLog,
"Initial switch status: {:s}",
356 SPDLOG_LOGGER_INFO(
mSLog,
"Switching System matrix {:s} \n{:s}",
357 sys.first.to_string(),
358 Logger::matrixToString(sys.second[0]));
379 SPDLOG_LOGGER_INFO(
mSLog,
"Cumulative solve times: {:.12f}", solveSum);
380 SPDLOG_LOGGER_INFO(
mSLog,
"Average solve time: {:.12f}",
381 solveSum /
static_cast<double>(
mSolveTimes.size()));
382 SPDLOG_LOGGER_INFO(
mSLog,
"Maximum solve time: {:.12f}", solveMax);
386template <
typename VarType>
389 SPDLOG_LOGGER_INFO(
mSLog,
"LU factorization time: {:.12f}", meas);
393template <
typename VarType>
395 Real recompSum = 0.0;
396 Real recompMax = 0.0;
399 if (meas > recompMax)
404 SPDLOG_LOGGER_INFO(
mSLog,
"Cumulative refactorization times: {:.12f}",
406 SPDLOG_LOGGER_INFO(
mSLog,
"Average refactorization time: {:.12f}",
408 SPDLOG_LOGGER_INFO(
mSLog,
"Maximum refactorization time: {:.12f}",
410 SPDLOG_LOGGER_INFO(
mSLog,
"Number of refactorizations: {:d}",
415template <
typename VarType>
416std::shared_ptr<DirectLinearSolver>
418 CPS::Logger::Log
mSLog) {
420 case DirectLinearSolverImpl::DenseLU:
421 return std::make_shared<DenseLUAdapter>(
mSLog);
422 case DirectLinearSolverImpl::SparseLU:
423 return std::make_shared<SparseLUAdapter>(
mSLog);
425 case DirectLinearSolverImpl::KLU:
426 return std::make_shared<KLUAdapter>(
mSLog);
429 case DirectLinearSolverImpl::CUDADense:
430 return std::make_shared<GpuDenseAdapter>(
mSLog);
431#ifdef WITH_CUDA_SPARSE
432 case DirectLinearSolverImpl::CUDASparse:
433 return std::make_shared<GpuSparseAdapter>(
mSLog);
436 case DirectLinearSolverImpl::CUDAMagma:
437 return std::make_shared<GpuMagmaAdapter>(
mSLog);
445template <
typename VarType>
447 DirectLinearSolverImpl implementation) {
451template <
typename VarType>
Solver class using Modified Nodal Analysis (MNA).
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.
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.
CPS::SystemTopology mSystem
System topology.
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.
UInt mNumTotalMatrixNodeIndices
Total number of network and virtual nodes, considering individual phases and additional frequencies.
CPS::MNASyncGenInterface::List mSyncGen
List of synchronous generators that need iterate to solve the differential equations.
std::vector< const Matrix * > mRightVectorStamps
List of all right side vector contributions.
void updateSwitchStatus()
Collects the status of switches to select correct system matrix.
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.
UInt mNumMatrixNodeIndices
Number of network and virtual nodes, considering individual phases.
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.
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 mLogSolveTimes
Collect step time for logging.
Bool mFrequencyParallel
Activates parallelized computation of frequencies.