DPsim
Loading...
Searching...
No Matches
DiakopticsSolver.h
Go to the documentation of this file.
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
15#include <dpsim/DataLogger.h>
17#include <dpsim/Solver.h>
18
19#include <atomic>
20#include <unordered_map>
21
22namespace DPsim {
23template <typename VarType>
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;
53 // #### MNA specific attributes related to system recomputation
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:
119
122
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:
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);
145 Bool hasVariableComponentChanged();
146
147 private:
149 Subnet &mSubnet;
150 };
151
152 // TODO better name
153 class PreSolveTask : public CPS::Task {
154 public:
156 : Task(solver.mName + ".PreSolve"), mSolver(solver) {
159 }
160
161 void execute(Real time, Int timeStepCount);
162
163 private:
165 };
166
167 class SolveTask : public CPS::Task {
168 public:
170 : Task(solver.mName + ".Solve_" + std::to_string(net)), mSolver(solver),
171 mSubnet(solver.mSubnets[net]) {
173 mModifiedAttributes.push_back(mSubnet.leftVector);
174 }
175
176 void execute(Real time, Int timeStepCount);
177
178 private:
180 Subnet &mSubnet;
181 };
182
183 class PostSolveTask : public CPS::Task {
184 public:
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:
200 };
201
202 class LogTask : public CPS::Task {
203 public:
205 : Task(solver.mName + ".Log"), mSolver(solver) {
206 for (auto &net : solver.mSubnets) {
207 mAttributeDependencies.push_back(net.leftVector);
208 }
210 }
211
212 void execute(Real time, Int timeStepCount);
213
214 private:
216 };
217};
218} // namespace DPsim
AttributePointer< Attribute< T > > Ptr
Definition Attribute.h:250
Base class of objects having attributes to access member variables.
std::vector< Ptr > List
spdlog::level::level_enum Level
Definition Logger.h:33
std::vector< Ptr > List
std::vector< Ptr > List
Definition SimNode.h:33
std::vector< Ptr > List
std::vector< Ptr > List
Tasks to be defined by every component.
Definition Task.h:25
std::vector< AttributeBase::Ptr > mModifiedAttributes
Definition Task.h:53
std::vector< AttributeBase::Ptr > mAttributeDependencies
Definition Task.h:52
Task()
Definition Task.h:30
std::vector< Ptr > List
Definition Task.h:28
std::string mName
Definition Task.h:51
LogTask(DiakopticsSolver< VarType > &solver)
void execute(Real time, Int timeStepCount)
PostSolveTask(DiakopticsSolver< VarType > &solver)
PreSolveTask(DiakopticsSolver< VarType > &solver)
SolveTask(DiakopticsSolver< VarType > &solver, UInt net)
SubnetSolveTask(DiakopticsSolver< VarType > &solver, UInt net)
CPS::Task::List getTasks() override
Get tasks for scheduler.
const CPS::Attribute< Matrix >::Ptr mOrigLeftSideVector
Solutions of the split systems.
DiakopticsSolver(String name, CPS::SystemTopology system, CPS::IdentifiedObject::List tearComponents, Real timeStep, CPS::Logger::Level logLevel)
const CPS::Attribute< Matrix >::Ptr mMappedTearCurrents
Currents through the removed network (as "seen" from the other subnets)
static CPS::AttributeBase::Ptr external
Definition Scheduler.h:63
Solver(String name, CPS::Logger::Level logLevel)
Definition Solver.h:83
Eigen::PartialPivLU< Matrix > LUFactorized
Definition Definitions.h:92
Eigen::SparseMatrix< Real, Eigen::RowMajor > SparseMatrixRow
Sparse matrix for real numbers (row major).
Definition Definitions.h:74
CPS::Real Real
Definition Definitions.h:18
CPS::String String
Definition Definitions.h:20
CPS::Int Int
Definition Definitions.h:22
CPS::Matrix Matrix
Definition Definitions.h:24
CPS::Bool Bool
Definition Definitions.h:21
CPS::SparseMatrixRow SparseMatrix
Definition Definitions.h:26
CPS::UInt UInt
Definition Definitions.h:23