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 std::vector<SparseMatrix> hMat;
77 if (this->mSystemMatrixRecomputation) {
78 SPDLOG_LOGGER_ERROR(this->mSLog,
"System matrix recomputation not supported");
81 hMat = this->mSwitchedMatrices[std::bitset<SWITCH_NUM>(0)];
82 nnz = hMat[0].nonZeros();
87 String pluginFileName = mPluginName +
".so";
89 if ((mDlHandle = dlopen(pluginFileName.c_str(), RTLD_NOW)) ==
nullptr) {
90 SPDLOG_LOGGER_ERROR(this->mSLog,
"error opening dynamic library {}: {}",
91 mPluginName, dlerror());
96 dlsym(mDlHandle,
"get_mna_plugin");
97 if (get_mna_plugin == NULL) {
98 SPDLOG_LOGGER_ERROR(this->mSLog,
"error reading symbol from library {}: {}",
99 mPluginName, dlerror());
103 if ((mPlugin = get_mna_plugin(mPluginName.c_str())) ==
nullptr) {
104 SPDLOG_LOGGER_ERROR(this->mSLog,
"error getting plugin class");
108 mPlugin->log = pluginLogger;
111 .values = hMat[0].valuePtr(),
112 .rowIndex = hMat[0].outerIndexPtr(),
113 .colIndex = hMat[0].innerIndexPtr(),
118 if (mPlugin->init(&matrix) != 0) {
119 SPDLOG_LOGGER_ERROR(this->mSLog,
"error initializing plugin");
127 for (
auto comp : this->mMNAComponents) {
128 for (
auto task : comp->mnaTasks()) {
132 for (
auto node : this->mNodes) {
133 for (
auto task : node->mnaTasks())
137 for (
auto comp : this->mSimSignalComps) {
138 for (
auto task : comp->getTasks()) {
147 template <
typename VarType>
150 this->mRightSideVector.setZero();
154 for (
const auto &stamp : this->mRightVectorStamps)
155 this->mRightSideVector += *stamp;
157 if (!this->mIsInInitialization)
158 this->updateSwitchStatus();
160 mPlugin->solve((
double *)this->mRightSideVector.data(),
161 (
double *)this->leftSideVector().data());
164 for (UInt nodeIdx = 0; nodeIdx < this->mNumNetNodes; ++nodeIdx)
165 this->mNodes[nodeIdx]->mnaUpdateVoltage(**(this->mLeftSideVector));
Solver class using Modified Nodal Analysis (MNA).
Solver class using Modified Nodal Analysis (MNA).