DPsim
Loading...
Searching...
No Matches
DiakopticsSolver.h
1/* Copyright 2017-2021 Institute for Automation of Complex Power Systems,
2 * EONERC, RWTH Aachen University
3 *
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at https://mozilla.org/MPL/2.0/.
7 *********************************************************************************/
8
9#pragma once
10
11#include <dpsim-models/AttributeList.h>
12#include <dpsim-models/SimSignalComp.h>
13#include <dpsim-models/Solver/MNAInterface.h>
14#include <dpsim-models/Solver/MNAVariableCompInterface.h>
15#include <dpsim/DataLogger.h>
16#include <dpsim/DirectLinearSolver.h>
17#include <dpsim/Solver.h>
18
19#include <atomic>
20#include <unordered_map>
21
22namespace DPsim {
23template <typename VarType>
24class DiakopticsSolver : public Solver, public CPS::AttributeList {
25private:
26 struct Subnet {
28 typename CPS::SimNode<VarType>::List nodes;
30 CPS::MNAInterface::List components;
32 UInt sysSize;
34 UInt mCmplOff;
36 UInt mRealNetNodeNum;
38 UInt mVirtualNodeNum;
40 UInt sysOff;
42 std::shared_ptr<DirectLinearSolver> directLinearSolver;
44 SparseMatrix systemMatrix;
46 std::vector<std::pair<UInt, UInt>> listVariableEntries;
48 std::vector<UInt> tearColumns;
50 std::vector<const Matrix *> rightVectorStamps;
52 CPS::Attribute<Matrix>::Ptr leftVector;
53 // #### MNA specific attributes related to system recomputation
55 CPS::MNAVariableCompInterface::List mVariableComps;
56 };
57
58 Real mSystemFrequency;
60 CPS::SystemTopology mSystem;
61
63 std::shared_ptr<DataLogger> mLeftVectorLog;
65 std::shared_ptr<DataLogger> mRightVectorLog;
66
67 std::vector<Subnet> mSubnets;
68 std::unordered_map<typename CPS::SimNode<VarType>::Ptr, Subnet *>
69 mNodeSubnetMap;
70 typename CPS::SimPowerComp<VarType>::List mTearComponents;
71 CPS::SimSignalComp::List mSimSignalComps;
72
73 Matrix mRightSideVector;
74 Matrix mLeftSideVector;
76 Matrix mSystemMatrix;
78 Matrix mSystemInverseTearTopology;
80 Matrix mTearTopology;
82 CPS::SparseMatrixRow mTearImpedance;
84 CPS::LUFactorized mTotalTearImpedance;
86 Matrix mTearSchur;
89 std::atomic<bool> mTearSchurNeedsRebuild{false};
91 Matrix mTearCurrents;
93 Matrix mTearVoltages;
95 CPS::PhaseType mPhaseType;
96
97 void init(CPS::SystemTopology &system);
98
99 void initSubnets(const std::vector<CPS::SystemTopology> &subnets);
100 void collectVirtualNodes(int net);
101 void assignMatrixNodeIndices(int net);
102 void setSubnetSize(int net, UInt nodes);
103
104 void setLogColumns();
105
106 void createMatrices();
107 void createTearMatrices(UInt totalSize);
108
109 void initComponents();
110
111 void initMatrices();
112 void applyTearComponentStamp(UInt compIdx);
113
114 void log(Real time, Int timeStepCount) override;
115
116public:
118 const CPS::Attribute<Matrix>::Ptr mMappedTearCurrents;
119
121 const CPS::Attribute<Matrix>::Ptr mOrigLeftSideVector;
122
123 DiakopticsSolver(String name, CPS::SystemTopology system,
124 CPS::IdentifiedObject::List tearComponents, Real timeStep,
125 CPS::Logger::Level logLevel);
126
127 CPS::Task::List getTasks() override;
128
129 class SubnetSolveTask : public CPS::Task {
130 public:
131 SubnetSolveTask(DiakopticsSolver<VarType> &solver, UInt net)
132 : Task(solver.mName + ".SubnetSolve_" + std::to_string(net)),
133 mSolver(solver), mSubnet(solver.mSubnets[net]) {
134 for (auto it : mSubnet.components) {
135 if (it->getRightVector()->get().size() != 0) {
136 mAttributeDependencies.push_back(it->getRightVector());
137 }
138 }
139 mModifiedAttributes.push_back(solver.mOrigLeftSideVector);
140 }
141
142 void execute(Real time, Int timeStepCount);
143 void recomputeSubnetMatrix(Real time);
146
147 private:
148 DiakopticsSolver<VarType> &mSolver;
149 Subnet &mSubnet;
150 };
151
152 // TODO better name
153 class PreSolveTask : public CPS::Task {
154 public:
155 PreSolveTask(DiakopticsSolver<VarType> &solver)
156 : Task(solver.mName + ".PreSolve"), mSolver(solver) {
157 mAttributeDependencies.push_back(solver.mOrigLeftSideVector);
158 mModifiedAttributes.push_back(solver.mMappedTearCurrents);
159 }
160
161 void execute(Real time, Int timeStepCount);
162
163 private:
164 DiakopticsSolver<VarType> &mSolver;
165 };
166
167 class SolveTask : public CPS::Task {
168 public:
169 SolveTask(DiakopticsSolver<VarType> &solver, UInt net)
170 : Task(solver.mName + ".Solve_" + std::to_string(net)), mSolver(solver),
171 mSubnet(solver.mSubnets[net]) {
172 mAttributeDependencies.push_back(solver.mMappedTearCurrents);
173 mModifiedAttributes.push_back(mSubnet.leftVector);
174 }
175
176 void execute(Real time, Int timeStepCount);
177
178 private:
179 DiakopticsSolver<VarType> &mSolver;
180 Subnet &mSubnet;
181 };
182
183 class PostSolveTask : public CPS::Task {
184 public:
185 PostSolveTask(DiakopticsSolver<VarType> &solver)
186 : Task(solver.mName + ".PostSolve"), mSolver(solver) {
187 for (auto &net : solver.mSubnets) {
188 mAttributeDependencies.push_back(net.leftVector);
189 for (UInt node = 0; node < net.mRealNetNodeNum; ++node) {
190 mModifiedAttributes.push_back(net.nodes[node]->attribute("v"));
191 }
192 }
193 mModifiedAttributes.push_back(Scheduler::external);
194 }
195
196 void execute(Real time, Int timeStepCount);
197
198 private:
199 DiakopticsSolver<VarType> &mSolver;
200 };
201
202 class LogTask : public CPS::Task {
203 public:
204 LogTask(DiakopticsSolver<VarType> &solver)
205 : Task(solver.mName + ".Log"), mSolver(solver) {
206 for (auto &net : solver.mSubnets) {
207 mAttributeDependencies.push_back(net.leftVector);
208 }
209 mModifiedAttributes.push_back(Scheduler::external);
210 }
211
212 void execute(Real time, Int timeStepCount);
213
214 private:
215 DiakopticsSolver<VarType> &mSolver;
216 };
217};
218} // namespace DPsim
Base class of objects having attributes to access member variables.
Tasks to be defined by every component.
Definition Task.h:25
Bool hasVariableComponentChanged()
Check whether status of variable MNA elements has changed.
CPS::Task::List getTasks() override
Get tasks for scheduler.
const CPS::Attribute< Matrix >::Ptr mOrigLeftSideVector
Solutions of the split systems.
const CPS::Attribute< Matrix >::Ptr mMappedTearCurrents
Currents through the removed network (as "seen" from the other subnets)
String mName
Name for logging.
Definition Solver.h:39