DPsim
Loading...
Searching...
No Matches
PFSolver.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 <cmath>
12#include <iterator>
13
14#include "dpsim-models/Components.h"
15#include "dpsim-models/SystemTopology.h"
16#include <dpsim/Scheduler.h>
17#include <dpsim/Solver.h>
18
19namespace DPsim {
21class PFSolver : public Solver {
22protected:
24 UInt mNumPQBuses = 0;
26 UInt mNumPVBuses = 0;
28 UInt mNumVDBuses = 0;
30 UInt mNumUnknowns = 0;
32 CPS::TopologicalNode::List mPQBuses;
34 CPS::TopologicalNode::List mPVBuses;
36 CPS::TopologicalNode::List mVDBuses;
38 std::vector<CPS::UInt> mPQBusIndices;
40 std::vector<CPS::UInt> mPVBusIndices;
42 std::vector<CPS::UInt> mVDBusIndices;
44 std::vector<CPS::UInt> mPQPVBusIndices;
45
47 CPS::SparseMatrixCompRow mY;
48
50 CPS::Matrix mJ;
52 CPS::Vector mX;
54 CPS::Vector mF;
55
59 std::vector<std::shared_ptr<CPS::SP::Ph1::Transformer>> mTransformers;
61 std::vector<std::shared_ptr<CPS::SP::Ph1::SolidStateTransformer>>
64 std::vector<std::shared_ptr<CPS::SP::Ph1::SynchronGenerator>>
67 std::vector<std::shared_ptr<CPS::SP::Ph1::Load>> mLoads;
69 std::vector<std::shared_ptr<CPS::SP::Ph1::PiLine>> mLines;
71 std::vector<std::shared_ptr<CPS::SP::Ph1::Shunt>> mShunts;
73 std::vector<std::shared_ptr<CPS::SP::Ph1::NetworkInjection>> mExternalGrids;
75 std::vector<std::shared_ptr<CPS::SP::Ph1::AvVoltageSourceInverterDQ>>
78 std::map<CPS::TopologicalNode::Ptr, CPS::Real> mBaseVoltageAtNode;
79
81 Real mTolerance = 1e-8;
83 CPS::UInt mMaxIterations = 20;
85 CPS::UInt mIterations;
89 CPS::Bool isConverged = false;
91 CPS::Bool solutionInitialized = false;
93 CPS::Bool solutionComplexInitialized = false;
94
96 CPS::Bool mKeepLastSolution = false;
97
99 virtual void generateInitialSolution(Real time,
100 bool keep_last_solution = false) = 0;
102 virtual void calculateMismatch() = 0;
104 virtual void calculateJacobian() = 0;
106 virtual void updateSolution() = 0;
108 virtual void setSolution() = 0;
109
111 void initialize() override;
119 void determinePFBusType();
122
126 CPS::Real G(int i, int j);
128 CPS::Real B(int i, int j);
130 Bool solvePowerflow();
132 virtual void setUpJacobianStorage();
134 virtual void solveJacobianSystem();
136 CPS::Bool checkConvergence();
138 CPS::String logVector(std::vector<CPS::UInt> indexVector) {
139 std::stringstream result;
140 std::copy(indexVector.begin(), indexVector.end(),
141 std::ostream_iterator<CPS::UInt>(result, " "));
142 return result.str();
143 };
144
145 CPS::Task::List getTasks() override;
146 // determines power flow bus type for each node according to the components attached to it.
147public:
149 PFSolver(CPS::String name, CPS::SystemTopology system, Real timeStep,
150 CPS::Logger::Level logLevel);
152 virtual ~PFSolver(){};
153
155 void setVDNode(CPS::String name);
157 void modifyPowerFlowBusComponent(CPS::String name,
158 CPS::PowerflowBusType powerFlowBusType);
160 void setSolverAndComponentBehaviour(Solver::Behaviour behaviour) override;
161
162 void setKeepLastSolution(CPS::Bool keepLastSolution) {
163 mKeepLastSolution = keepLastSolution;
164 }
165
166 CPS::Bool getKeepLastSolution() const { return mKeepLastSolution; }
167
168 class SolveTask : public CPS::Task {
169 public:
170 SolveTask(PFSolver &solver)
171 : Task(solver.mName + ".Solve"), mSolver(solver) {
172 mModifiedAttributes.push_back(Scheduler::external);
173 }
174
175 void execute(Real time, Int timeStepCount);
176
177 private:
178 PFSolver &mSolver;
179 };
180};
181} // namespace DPsim
Tasks to be defined by every component.
Definition Task.h:25
Solver class using the nonlinear powerflow (PF) formulation.
Definition PFSolver.h:21
void determinePFBusType()
Determine bus type for all buses.
Definition PFSolver.cpp:157
std::vector< std::shared_ptr< CPS::SP::Ph1::Load > > mLoads
Vector of load components.
Definition PFSolver.h:67
CPS::Bool mKeepLastSolution
Use last converged solution as initial guess.
Definition PFSolver.h:96
CPS::TopologicalNode::List mPQBuses
Vector of nodes characterized as PQ buses.
Definition PFSolver.h:32
UInt mNumPQBuses
Number of PQ nodes.
Definition PFSolver.h:24
virtual void solveJacobianSystem()
Solve the linearized system mJ*mX = mF into mX; sparse subclass overrides.
Definition PFSolver.cpp:72
UInt mNumVDBuses
Number of PV nodes.
Definition PFSolver.h:28
Real mTolerance
Solver tolerance.
Definition PFSolver.h:81
CPS::String logVector(std::vector< CPS::UInt > indexVector)
Logging for integer vectors.
Definition PFSolver.h:138
std::vector< std::shared_ptr< CPS::SP::Ph1::PiLine > > mLines
Vector of line components.
Definition PFSolver.h:69
CPS::Task::List getTasks() override
Get tasks for scheduler.
Definition PFSolver.cpp:520
void assignMatrixNodeIndices()
Assignment of matrix indices for nodes.
Definition PFSolver.cpp:78
std::vector< CPS::UInt > mVDBusIndices
Vector with indices of VD buses.
Definition PFSolver.h:42
virtual void setUpJacobianStorage()
Allocate Jacobian storage; dense by default, sparse subclass overrides.
Definition PFSolver.cpp:68
void initializeComponents()
Initialization of individual components.
Definition PFSolver.cpp:92
std::vector< std::shared_ptr< CPS::SP::Ph1::SynchronGenerator > > mSynchronGenerators
Vector of synchronous generator components.
Definition PFSolver.h:65
std::vector< CPS::UInt > mPQBusIndices
Vector with indices of PQ buses.
Definition PFSolver.h:38
void modifyPowerFlowBusComponent(CPS::String name, CPS::PowerflowBusType powerFlowBusType)
Allows to modify the powerflow bus type of a specific component.
Definition PFSolver.cpp:395
std::vector< std::shared_ptr< CPS::SP::Ph1::NetworkInjection > > mExternalGrids
Vector of external grid components.
Definition PFSolver.h:73
std::vector< std::shared_ptr< CPS::SP::Ph1::SolidStateTransformer > > mSolidStateTransformers
Vector of solid state transformer components.
Definition PFSolver.h:62
std::vector< CPS::UInt > mPVBusIndices
Vector with indices of PV buses.
Definition PFSolver.h:40
void setVDNode(CPS::String name)
Set a node to VD using its name.
Definition PFSolver.cpp:378
std::vector< std::shared_ptr< CPS::SP::Ph1::Transformer > > mTransformers
Vector of transformer components.
Definition PFSolver.h:59
std::vector< std::shared_ptr< CPS::SP::Ph1::Shunt > > mShunts
Vector of shunt components.
Definition PFSolver.h:71
CPS::Bool checkConvergence()
Check whether below tolerance.
Definition PFSolver.cpp:467
CPS::Matrix mJ
Jacobian matrix.
Definition PFSolver.h:50
CPS::UInt mMaxIterations
Maximum number of iterations.
Definition PFSolver.h:83
CPS::Vector mX
Solution vector.
Definition PFSolver.h:52
std::vector< std::shared_ptr< CPS::SP::Ph1::AvVoltageSourceInverterDQ > > mAverageVoltageSourceInverters
Vector of average voltage source inverters.
Definition PFSolver.h:76
CPS::Bool solutionInitialized
Flag whether solution vectors are initialized.
Definition PFSolver.h:91
CPS::Real mBaseApparentPower
Base power of per-unit system.
Definition PFSolver.h:87
void setBaseApparentPower()
Set apparent base power of per-unit system.
Definition PFSolver.cpp:134
virtual void calculateMismatch()=0
Calculate mismatch.
CPS::SparseMatrixCompRow mY
Admittance matrix.
Definition PFSolver.h:47
CPS::Bool solutionComplexInitialized
Flag whether complex solution vectors are initialized.
Definition PFSolver.h:93
UInt mNumPVBuses
Number of PV nodes.
Definition PFSolver.h:26
CPS::Real B(int i, int j)
Gets the imaginary part of admittance matrix element.
Definition PFSolver.cpp:465
void composeAdmittanceMatrix()
Compose admittance matrix.
Definition PFSolver.cpp:438
CPS::Bool isConverged
Convergence flag.
Definition PFSolver.h:89
CPS::Vector mF
Vector of mismatch values.
Definition PFSolver.h:54
CPS::UInt mIterations
Actual number of iterations.
Definition PFSolver.h:85
PFSolver(CPS::String name, CPS::SystemTopology system, Real timeStep, CPS::Logger::Level logLevel)
Constructor to be used in simulation examples.
Definition PFSolver.cpp:16
CPS::SystemTopology mSystem
System list.
Definition PFSolver.h:57
virtual void calculateJacobian()=0
Calculate the Jacobian.
void determineNodeBaseVoltages()
Determine base voltages for each node.
Definition PFSolver.cpp:270
void setSolverAndComponentBehaviour(Solver::Behaviour behaviour) override
set solver and component to initialization or simulation behaviour
Definition PFSolver.cpp:410
std::map< CPS::TopologicalNode::Ptr, CPS::Real > mBaseVoltageAtNode
Map providing determined base voltages for each node.
Definition PFSolver.h:78
virtual void setSolution()=0
Set final solution.
void initialize() override
Initialization of the solver.
Definition PFSolver.cpp:23
virtual void generateInitialSolution(Real time, bool keep_last_solution=false)=0
Generate initial solution for current time step.
virtual void updateSolution()=0
Update solution in each iteration.
Bool solvePowerflow()
Solves the powerflow problem.
Definition PFSolver.cpp:476
CPS::TopologicalNode::List mVDBuses
Vector of nodes characterized as VD buses.
Definition PFSolver.h:36
CPS::TopologicalNode::List mPVBuses
Vector of nodes characterized as PV buses.
Definition PFSolver.h:34
UInt mNumUnknowns
Number of unknowns, defining system dimension.
Definition PFSolver.h:30
CPS::Real G(int i, int j)
Gets the real part of admittance matrix element.
Definition PFSolver.cpp:463
std::vector< CPS::UInt > mPQPVBusIndices
Vector with indices of both PQ and PV buses.
Definition PFSolver.h:44
String mName
Name for logging.
Definition Solver.h:39