17#include <unordered_map>
20#include <dpsim/Config.h>
30#include <dpsim/HipDenseAdapter.h>
31#include <dpsim/HipSG4BatchAdapter.h>
32#include <dpsim/HipSparseBiCGStabAdapter.h>
33#include <dpsim/HipSparseDeviceSolver.h>
35#include <dpsim/HipSparseLURefactorAdapter.h>
43#ifdef WITH_CUDA_SPARSE
51#include <dpsim/GPU/Apple/MetalDenseAdapter.h>
52#include <dpsim/GPU/Apple/MetalSparseBiCGSTABAdapter.h>
86 std::unordered_map<std::bitset<SWITCH_NUM>, std::vector<SparseMatrix>>
89 std::unordered_map<std::bitset<SWITCH_NUM>,
90 std::vector<std::shared_ptr<DirectLinearSolver>>>
156 std::vector<std::unique_ptr<HipSG4BatchAdapter>> mHipSG4Adapters;
157 HipSparseDeviceSolver *mHipSparseDeviceSolver =
nullptr;
158 bool mHipDeviceAssemblyReady =
false;
159 bool mHipSG4AdaptersInitialized =
false;
160 void initializeHipSG4Adapters();
161 void updateHipSG4PreStep(
Real time);
162 void updateHipSG4PostStep(
const Matrix &leftVector);
165 std::chrono::steady_clock::time_point start,
166 std::chrono::steady_clock::time_point end);
182 std::vector<std::shared_ptr<CPS::MNAInterface>> &comp)
override;
193 Int timeStepCount)
override;
225 std::shared_ptr<DirectLinearSolver>
256 :
Task(solver.
mName +
".Solve"), mSolver(solver) {
260 if (it->getRightVector()->get().size() != 0)
266 if (it->getRightVector()->get().size() != 0)
269 for (
auto node : solver.
mNodes) {
276 mSolver.solve(time, timeStepCount);
279 mSolver.solve(time, timeStepCount);
290 :
Task(solver.
mName +
".Solve"), mSolver(solver), mFreqIdx(freqIdx) {
294 if (it->getRightVector()->get().size() != 0)
298 if (it->getRightVector()->get().size() != 0)
301 for (
auto node : solver.
mNodes) {
310 mSolver.solveWithHarmonics(time, timeStepCount, mFreqIdx);
313 mSolver.solveWithHarmonics(time, timeStepCount, mFreqIdx);
325 :
Task(solver.
mName +
".Solve"), mSolver(solver) {
329 if (it->getRightVector()->get().size() != 0)
333 if (it->getRightVector()->get().size() != 0)
336 for (
auto node : solver.
mNodes) {
343 mSolver.solveWithSystemMatrixRecomputation(time, timeStepCount);
344 mSolver.log(time, timeStepCount);
347 mSolver.solveWithSystemMatrixRecomputation(time, timeStepCount);
348 mSolver.log(time, timeStepCount);
359 :
Task(solver.
mName +
".StateSpaceExtraction"), mSolver(solver) {
366 for (const auto &dependency :
367 solver.mStateSpaceExtractor->getAttributeDependencies())
368 mAttributeDependencies.push_back(dependency);
374 mSolver.extractStateSpace(time);
377 mSolver.extractStateSpace(time);
388 :
Task(solver.
mName +
".Log"), mSolver(solver) {
395 mSolver.log(time, timeStepCount);
398 mSolver.log(time, timeStepCount);
spdlog::level::level_enum Level
std::shared_ptr< spdlog::logger > Log
Interface to be implemented by all models used by the MNA solver.
Tasks to be defined by every component.
std::vector< AttributeBase::Ptr > mModifiedAttributes
std::vector< AttributeBase::Ptr > mAttributeDependencies
void setFastExecutor(TaskType *task)
void execute(Real time, Int timeStepCount)
void executeFastImpl(Real time, Int timeStepCount)
LogTask(MnaSolverDirect< VarType > &solver)
void executeFastImpl(Real time, Int timeStepCount)
void execute(Real time, Int timeStepCount)
SolveTaskHarm(MnaSolverDirect< VarType > &solver, UInt freqIdx)
void execute(Real time, Int timeStepCount)
SolveTask(MnaSolverDirect< VarType > &solver)
void executeFastImpl(Real time, Int timeStepCount)
void executeFastImpl(Real time, Int timeStepCount)
SolveTaskRecomp(MnaSolverDirect< VarType > &solver)
void execute(Real time, Int timeStepCount)
std::size_t mDenseRightVectorStampCalls
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.
std::vector< RightVectorStamp > mRightVectorStampOperations
void logFactorizationTime()
Logging of the LU factorization time.
void extractStateSpace(Real time)
Runs state-space extraction using the active linear solver.
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 addRightVectorStamps(Matrix &rightVector)
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 recordProfileMeasurement(const String &phase, std::chrono::steady_clock::time_point start, std::chrono::steady_clock::time_point end)
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::size_t mCompactRightVectorStampCalls
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.
void prepareCompactRightVectorStamps()
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::map< String, ProfileMeasurement > mProfileMeasurements
virtual ~MnaSolverDirect()
Destructor.
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.
Matrix * mLeftSideVectorValue
Cached payload for the per-step solver hot path.
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< 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.
CPS::MNANonlinearCompInterface::List mNonlinearComps
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.
std::vector< CPS::Attribute< Matrix >::Ptr > mRightVectorStamps
List of all right side vector contributions.
CPS::Attribute< Matrix >::Ptr mLeftSideVector
Solution vector of unknown quantities.
CPS::MNASwitchInterface::List mSwitches
CPS::SimNode< VarType >::List mNodes
List of simulation nodes.
static CPS::AttributeBase::Ptr external
Bool mSystemMatrixRecomputationEnabled
Effective system-matrix recomputation setting used by the solver.
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.
CPS::SparseMatrixRow SparseMatrix
std::chrono::nanoseconds total
std::chrono::nanoseconds minimum
std::chrono::nanoseconds maximum
CPS::MNAInterface * compact