11#include <dpsim/MNASolverPlugin.h>
12#include <dpsim/SequentialScheduler.h>
19template <
typename VarType>
20MnaSolverPlugin<VarType>::MnaSolverPlugin(String pluginName, String name,
22 CPS::Logger::Level logLevel)
23 :
MnaSolverDirect<VarType>(name, domain, logLevel), mPluginName(pluginName),
24 mPlugin(nullptr), mDlHandle(nullptr) {}
26template <
typename VarType> MnaSolverPlugin<VarType>::~MnaSolverPlugin() {
27 if (mPlugin !=
nullptr) {
30 if (mDlHandle !=
nullptr) {
35extern "C" void pluginLogger(
const char *str) {
36 CPS::Logger::Log log = CPS::Logger::get(
"Plugin", CPS::Logger::Level::debug,
37 CPS::Logger::Level::debug);
41template <
typename VarType>
65 if (mPlugin->lu_decomp(&matrix) != 0) {
66 SPDLOG_LOGGER_ERROR(this->
mSLog,
"error recomputing decomposition");
75 std::vector<SparseMatrix> hMat;
78 SPDLOG_LOGGER_ERROR(this->
mSLog,
79 "System matrix recomputation not supported");
83 nnz = hMat[0].nonZeros();
88 String pluginFileName = mPluginName +
".so";
90 if ((mDlHandle = dlopen(pluginFileName.c_str(), RTLD_NOW)) ==
nullptr) {
91 SPDLOG_LOGGER_ERROR(this->
mSLog,
"error opening dynamic library {}: {}",
92 mPluginName, dlerror());
97 dlsym(mDlHandle,
"get_mna_plugin");
98 if (get_mna_plugin == NULL) {
99 SPDLOG_LOGGER_ERROR(this->
mSLog,
"error reading symbol from library {}: {}",
100 mPluginName, dlerror());
104 if ((mPlugin = get_mna_plugin(mPluginName.c_str())) ==
nullptr) {
105 SPDLOG_LOGGER_ERROR(this->
mSLog,
"error getting plugin class");
109 mPlugin->log = pluginLogger;
112 .values = hMat[0].valuePtr(),
113 .rowIndex = hMat[0].outerIndexPtr(),
114 .colIndex = hMat[0].innerIndexPtr(),
119 if (mPlugin->init(&matrix) != 0) {
120 SPDLOG_LOGGER_ERROR(this->
mSLog,
"error initializing plugin");
129 for (
auto task : comp->mnaTasks()) {
133 for (
auto node : this->
mNodes) {
134 for (
auto task : node->mnaTasks())
139 for (
auto task : comp->getTasks()) {
148template <
typename VarType>
162 (
double *)this->leftSideVector().data());
165 for (UInt nodeIdx = 0; nodeIdx < this->
mNumNetNodes; ++nodeIdx)
Solver class using Modified Nodal Analysis (MNA).
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.
Matrix mRightSideVector
Source vector of known quantities.
virtual void initialize() override
Calls subroutines to set up everything that is required before simulation.
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::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.
void updateSwitchStatus()
Collects the status of switches to select correct system matrix.
Int mNumRecomputations
Number of system matrix recomputations.
CPS::MNAInterface::List mMNAComponents
List of MNA components with static stamp into system matrix.
CPS::Attribute< Matrix >::Ptr mLeftSideVector
Solution vector of unknown quantities.
CPS::SimSignalComp::List mSimSignalComps
List of signal type components that do not directly interact with the MNA solver.
CPS::SimNode< VarType >::List mNodes
List of simulation nodes.
CPS::Task::List getTasks() override
Get tasks for scheduler.
CPS::Logger::Log mSLog
Logger.
void solve(Real time, Int timeStepCount) override
Solves system for single frequency.
void recomputeSystemMatrix(Real time) override
Recomputes systems matrix.
void initialize() override
Initialize cuSparse-library.
Bool mSystemMatrixRecomputation
Enable recomputation of system matrix during simulation.
Bool mIsInInitialization
Determines if solver is in initialization phase, which requires different behavior.