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 CPS::TopologicalNode::List mPQBusesOrig;
39 CPS::TopologicalNode::List mPVBusesOrig;
41 std::vector<CPS::UInt> mPQBusIndices;
43 std::vector<CPS::UInt> mPVBusIndices;
45 std::vector<CPS::UInt> mVDBusIndices;
47 std::vector<CPS::UInt> mPQPVBusIndices;
48
50 CPS::SparseMatrixCompRow mY;
51
53 CPS::Matrix mJ;
55 CPS::Vector mX;
57 CPS::Vector mF;
58
62 std::vector<std::shared_ptr<CPS::SP::Ph1::Transformer>> mTransformers;
64 std::vector<std::shared_ptr<CPS::SP::Ph1::SolidStateTransformer>>
67 std::vector<std::shared_ptr<CPS::SP::Ph1::SynchronGenerator>>
70 std::vector<std::shared_ptr<CPS::SP::Ph1::Load>> mLoads;
72 std::vector<std::shared_ptr<CPS::SP::Ph1::PiLine>> mLines;
74 std::vector<std::shared_ptr<CPS::SP::Ph1::Shunt>> mShunts;
76 std::vector<std::shared_ptr<CPS::SP::Ph1::NetworkInjection>> mExternalGrids;
78 std::vector<std::shared_ptr<CPS::SP::Ph1::AvVoltageSourceInverterDQ>>
81 std::map<CPS::TopologicalNode::Ptr, CPS::Real> mBaseVoltageAtNode;
82
84 Real mTolerance = 1e-8;
86 CPS::UInt mMaxIterations = 20;
88 CPS::UInt mIterations;
90 CPS::Bool mEnforceReactiveLimits = false;
92 CPS::UInt mMaxOuterIterations = 10;
102 CPS::Real mBaseApparentPowerFallback = 100e6;
104 CPS::Bool isConverged = false;
106 CPS::Bool solutionInitialized = false;
108 CPS::Bool solutionComplexInitialized = false;
109
111 CPS::Bool mKeepLastSolution = false;
112
114 virtual void generateInitialSolution(Real time,
115 bool keep_last_solution = false) = 0;
117 virtual void calculateMismatch() = 0;
119 virtual void calculateJacobian() = 0;
121 virtual void updateSolution() = 0;
123 virtual void setSolution() = 0;
124
126 void initialize() override;
134 void determinePFBusType();
138 void reclassifyBuses();
142 virtual void clearReactiveLimitState() {}
144 CPS::Real componentBaseVoltage(CPS::TopologicalPowerComp::Ptr comp,
145 CPS::TopologicalNode::Ptr node);
148
152 CPS::Real G(int i, int j);
154 CPS::Real B(int i, int j);
156 Bool solvePowerflow();
158 Bool runNewtonRaphson();
160 virtual CPS::Bool enforceReactiveLimits() { return false; }
162 virtual void setUpJacobianStorage();
164 virtual void solveJacobianSystem();
166 CPS::Bool checkConvergence();
168 CPS::String logVector(std::vector<CPS::UInt> indexVector) {
169 std::stringstream result;
170 std::copy(indexVector.begin(), indexVector.end(),
171 std::ostream_iterator<CPS::UInt>(result, " "));
172 return result.str();
173 };
174
175 CPS::Task::List getTasks() override;
176 // determines power flow bus type for each node according to the components attached to it.
177public:
179 PFSolver(CPS::String name, CPS::SystemTopology system, Real timeStep,
180 CPS::Logger::Level logLevel);
182 virtual ~PFSolver(){};
183
185 void setVDNode(CPS::String name);
187 void modifyPowerFlowBusComponent(CPS::String name,
188 CPS::PowerflowBusType powerFlowBusType);
190 void setSolverAndComponentBehaviour(Solver::Behaviour behaviour) override;
191
192 void setKeepLastSolution(CPS::Bool keepLastSolution) {
193 mKeepLastSolution = keepLastSolution;
194 }
195
197 void setEnforceReactiveLimits(CPS::Bool value) {
199 }
200
202 void setBaseVoltageLooseTolerance(CPS::Real tolerance) {
203 mBaseVoltageLooseTolerance = tolerance;
204 }
205
207 void setBaseVoltageStrictTolerance(CPS::Real tolerance) {
208 mBaseVoltageStrictTolerance = tolerance;
209 }
210
211 CPS::Bool getKeepLastSolution() const { return mKeepLastSolution; }
212
213 void setBaseApparentPowerFallback(CPS::Real baseApparentPowerFallback) {
214 mBaseApparentPowerFallback = baseApparentPowerFallback;
215 }
216
217 CPS::Real getBaseApparentPowerFallback() const {
219 }
220
221 void setMaxIterations(CPS::UInt maxIterations) {
222 mMaxIterations = maxIterations;
223 }
224
225 CPS::UInt getMaxIterations() const { return mMaxIterations; }
226
227 class SolveTask : public CPS::Task {
228 public:
229 SolveTask(PFSolver &solver)
230 : Task(solver.mName + ".Solve"), mSolver(solver) {
231 mModifiedAttributes.push_back(Scheduler::external);
232 }
233
234 void execute(Real time, Int timeStepCount);
235
236 private:
237 PFSolver &mSolver;
238 };
239};
240} // 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:158
void reclassifyBuses()
Re-derive index vectors and resize the system after PV<->PQ switching.
Definition PFSolver.cpp:294
CPS::Real mBaseApparentPowerFallback
Fallback base apparent power if no generator or transformer rating is found.
Definition PFSolver.h:102
Bool runNewtonRaphson()
Run a single Newton-Raphson solve with the current bus classification.
Definition PFSolver.cpp:576
void resetToOriginalClassification()
Restore the original PV/PQ classification before a fresh solve.
Definition PFSolver.cpp:286
std::vector< std::shared_ptr< CPS::SP::Ph1::Load > > mLoads
Vector of load components.
Definition PFSolver.h:70
CPS::Bool mKeepLastSolution
Use last converged solution as initial guess.
Definition PFSolver.h:111
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:73
void setBaseVoltageStrictTolerance(CPS::Real tolerance)
Override the tolerance between authoritative base-voltage sources within a zone.
Definition PFSolver.h:207
UInt mNumVDBuses
Number of PV nodes.
Definition PFSolver.h:28
Real mTolerance
Solver tolerance.
Definition PFSolver.h:84
CPS::String logVector(std::vector< CPS::UInt > indexVector)
Logging for integer vectors.
Definition PFSolver.h:168
std::vector< std::shared_ptr< CPS::SP::Ph1::PiLine > > mLines
Vector of line components.
Definition PFSolver.h:72
CPS::Task::List getTasks() override
Get tasks for scheduler.
Definition PFSolver.cpp:650
void assignMatrixNodeIndices()
Assignment of matrix indices for nodes.
Definition PFSolver.cpp:79
std::vector< CPS::UInt > mVDBusIndices
Vector with indices of VD buses.
Definition PFSolver.h:45
virtual void setUpJacobianStorage()
Allocate Jacobian storage; dense by default, sparse subclass overrides.
Definition PFSolver.cpp:69
void initializeComponents()
Initialization of individual components.
Definition PFSolver.cpp:93
std::vector< std::shared_ptr< CPS::SP::Ph1::SynchronGenerator > > mSynchronGenerators
Vector of synchronous generator components.
Definition PFSolver.h:68
void propagateAndVerifyBaseVoltage()
Determine, verify and propagate each node's base voltage per electrical zone.
Definition PFSolver.cpp:333
std::vector< CPS::UInt > mPQBusIndices
Vector with indices of PQ buses.
Definition PFSolver.h:41
void modifyPowerFlowBusComponent(CPS::String name, CPS::PowerflowBusType powerFlowBusType)
Allows to modify the powerflow bus type of a specific component.
Definition PFSolver.cpp:491
std::vector< std::shared_ptr< CPS::SP::Ph1::NetworkInjection > > mExternalGrids
Vector of external grid components.
Definition PFSolver.h:76
std::vector< std::shared_ptr< CPS::SP::Ph1::SolidStateTransformer > > mSolidStateTransformers
Vector of solid state transformer components.
Definition PFSolver.h:65
std::vector< CPS::UInt > mPVBusIndices
Vector with indices of PV buses.
Definition PFSolver.h:43
void setVDNode(CPS::String name)
Set a node to VD using its name.
Definition PFSolver.cpp:474
std::vector< std::shared_ptr< CPS::SP::Ph1::Transformer > > mTransformers
Vector of transformer components.
Definition PFSolver.h:62
std::vector< std::shared_ptr< CPS::SP::Ph1::Shunt > > mShunts
Vector of shunt components.
Definition PFSolver.h:74
CPS::Bool checkConvergence()
Check whether below tolerance.
Definition PFSolver.cpp:563
CPS::Matrix mJ
Jacobian matrix.
Definition PFSolver.h:53
CPS::UInt mMaxIterations
Maximum number of iterations.
Definition PFSolver.h:86
CPS::Real componentBaseVoltage(CPS::TopologicalPowerComp::Ptr comp, CPS::TopologicalNode::Ptr node)
Base voltage a single component reports for node, or 0 if unknown.
Definition PFSolver.cpp:302
CPS::Vector mX
Solution vector.
Definition PFSolver.h:55
std::vector< std::shared_ptr< CPS::SP::Ph1::AvVoltageSourceInverterDQ > > mAverageVoltageSourceInverters
Vector of average voltage source inverters.
Definition PFSolver.h:79
void rebuildBusIndexAggregates()
Rebuild index vectors + counts from the PQ/PV/VD node lists.
Definition PFSolver.cpp:260
CPS::Bool solutionInitialized
Flag whether solution vectors are initialized.
Definition PFSolver.h:106
void setEnforceReactiveLimits(CPS::Bool value)
Enable generator reactive-limit enforcement (PV<->PQ outer loop)
Definition PFSolver.h:197
CPS::Real mBaseApparentPower
Base power of per-unit system.
Definition PFSolver.h:100
void setBaseApparentPower()
Set apparent base power of per-unit system.
Definition PFSolver.cpp:135
virtual void calculateMismatch()=0
Calculate mismatch.
CPS::SparseMatrixCompRow mY
Admittance matrix.
Definition PFSolver.h:50
void setBaseVoltageLooseTolerance(CPS::Real tolerance)
Raise for grids with legitimate large voltage drop (e.g. untapped feeders)
Definition PFSolver.h:202
CPS::UInt mMaxQLimitSwitchesPerBus
Maximum number of PV<->PQ switches per bus before it is frozen (anti-oscillation)
Definition PFSolver.h:94
CPS::Bool solutionComplexInitialized
Flag whether complex solution vectors are initialized.
Definition PFSolver.h:108
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:561
void composeAdmittanceMatrix()
Compose admittance matrix.
Definition PFSolver.cpp:534
CPS::TopologicalNode::List mPQBusesOrig
Original PQ/PV classification (snapshot before Q-limit switching)
Definition PFSolver.h:38
CPS::Bool isConverged
Convergence flag.
Definition PFSolver.h:104
CPS::Vector mF
Vector of mismatch values.
Definition PFSolver.h:57
CPS::UInt mIterations
Actual number of iterations.
Definition PFSolver.h:88
CPS::UInt mMaxOuterIterations
Maximum number of Q-limit outer iterations.
Definition PFSolver.h:92
virtual CPS::Bool enforceReactiveLimits()
Switch generators violating their Q limits between PV/PQ; base impl is a no-op.
Definition PFSolver.h:160
PFSolver(CPS::String name, CPS::SystemTopology system, Real timeStep, CPS::Logger::Level logLevel)
Constructor to be used in simulation examples.
Definition PFSolver.cpp:17
CPS::SystemTopology mSystem
System list.
Definition PFSolver.h:60
virtual void calculateJacobian()=0
Calculate the Jacobian.
void setSolverAndComponentBehaviour(Solver::Behaviour behaviour) override
set solver and component to initialization or simulation behaviour
Definition PFSolver.cpp:506
std::map< CPS::TopologicalNode::Ptr, CPS::Real > mBaseVoltageAtNode
Map providing determined base voltages for each node.
Definition PFSolver.h:81
virtual void setSolution()=0
Set final solution.
void initialize() override
Initialization of the solver.
Definition PFSolver.cpp:24
CPS::Real mBaseVoltageLooseTolerance
Relative tolerance for non-authoritative (e.g. Load) base-voltage candidates vs. the zone's rating.
Definition PFSolver.h:96
virtual void generateInitialSolution(Real time, bool keep_last_solution=false)=0
Generate initial solution for current time step.
CPS::Real mBaseVoltageStrictTolerance
Relative tolerance between authoritative base-voltage sources (generator/transformer/network-injectio...
Definition PFSolver.h:98
virtual void updateSolution()=0
Update solution in each iteration.
Bool solvePowerflow()
Solves the powerflow problem.
Definition PFSolver.cpp:613
CPS::Bool mEnforceReactiveLimits
Enforce generator reactive-power limits via PV<->PQ outer-loop switching.
Definition PFSolver.h:90
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
virtual void clearReactiveLimitState()
Clear Q-limit bookkeeping; overridden by PFSolverPowerPolar.
Definition PFSolver.h:142
CPS::Real G(int i, int j)
Gets the real part of admittance matrix element.
Definition PFSolver.cpp:559
std::vector< CPS::UInt > mPQPVBusIndices
Vector with indices of both PQ and PV buses.
Definition PFSolver.h:47
String mName
Name for logging.
Definition Solver.h:39