11 #include <dpsim/MNASolverPlugin.h>
12 #include <dpsim/SequentialScheduler.h>
14 using namespace DPsim;
19 template <
typename VarType>
22 CPS::Logger::Level logLevel)
23 :
MnaSolverDirect<VarType>(name, domain, logLevel), mPluginName(pluginName),
24 mPlugin(nullptr), mDlHandle(nullptr) {}
27 if (mPlugin !=
nullptr) {
30 if (mDlHandle !=
nullptr) {
35 extern "C" void pluginLogger(
const char *str) {
36 CPS::Logger::Log log = CPS::Logger::get(
"Plugin", CPS::Logger::Level::debug,
37 CPS::Logger::Level::debug);
41 template <
typename VarType>
44 this->mVariableSystemMatrix = this->mBaseSystemMatrix;
47 for (
auto sw : this->mMNAIntfSwitches)
48 sw->mnaApplySystemMatrixStamp(this->mVariableSystemMatrix);
51 for (
auto comp : this->mMNAIntfVariableComps)
52 comp->mnaApplySystemMatrixStamp(this->mVariableSystemMatrix);
54 int size = this->mRightSideVector.rows();
55 int nnz = this->mVariableSystemMatrix.nonZeros();
57 .values = this->mVariableSystemMatrix.valuePtr(),
58 .rowIndex = this->mVariableSystemMatrix.outerIndexPtr(),
59 .colIndex = this->mVariableSystemMatrix.innerIndexPtr(),
65 if (mPlugin->lu_decomp(&matrix) != 0) {
66 SPDLOG_LOGGER_ERROR(this->mSLog,
"error recomputing decomposition");
69 ++this->mNumRecomputations;
74 int size = this->mRightSideVector.rows();
75 auto hMat = this->mSwitchedMatrices[std::bitset<SWITCH_NUM>(0)];
76 int nnz = hMat[0].nonZeros();
80 String pluginFileName = mPluginName +
".so";
82 if ((mDlHandle = dlopen(pluginFileName.c_str(), RTLD_NOW)) ==
nullptr) {
83 SPDLOG_LOGGER_ERROR(this->mSLog,
"error opening dynamic library {}: {}",
84 mPluginName, dlerror());
89 dlsym(mDlHandle,
"get_mna_plugin");
90 if (get_mna_plugin == NULL) {
91 SPDLOG_LOGGER_ERROR(this->mSLog,
"error reading symbol from library {}: {}",
92 mPluginName, dlerror());
96 if ((mPlugin = get_mna_plugin(mPluginName.c_str())) ==
nullptr) {
97 SPDLOG_LOGGER_ERROR(this->mSLog,
"error getting plugin class");
101 mPlugin->log = pluginLogger;
104 .values = hMat[0].valuePtr(),
105 .rowIndex = hMat[0].outerIndexPtr(),
106 .colIndex = hMat[0].innerIndexPtr(),
111 if (mPlugin->init(&matrix) != 0) {
112 SPDLOG_LOGGER_ERROR(this->mSLog,
"error initializing plugin");
120 for (
auto comp : this->mMNAComponents) {
121 for (
auto task : comp->mnaTasks()) {
125 for (
auto node : this->mNodes) {
126 for (
auto task : node->mnaTasks())
130 for (
auto comp : this->mSimSignalComps) {
131 for (
auto task : comp->getTasks()) {
140 template <
typename VarType>
143 this->mRightSideVector.setZero();
147 for (
const auto &stamp : this->mRightVectorStamps)
148 this->mRightSideVector += *stamp;
150 if (!this->mIsInInitialization)
151 this->updateSwitchStatus();
153 mPlugin->solve((
double *)this->mRightSideVector.data(),
154 (
double *)this->leftSideVector().data());
157 for (UInt nodeIdx = 0; nodeIdx < this->mNumNetNodes; ++nodeIdx)
158 this->mNodes[nodeIdx]->mnaUpdateVoltage(**(this->mLeftSideVector));
Solver class using Modified Nodal Analysis (MNA).
Solver class using Modified Nodal Analysis (MNA).