DPsim
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 
19 namespace DPsim {
21 class PFSolver : public Solver {
22 protected:
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 = 9;
85  CPS::UInt mIterations;
87  CPS::Real mBaseApparentPower;
89  CPS::Bool isConverged = false;
91  CPS::Bool solutionInitialized = false;
93  CPS::Bool solutionComplexInitialized = false;
94 
96  virtual void generateInitialSolution(Real time,
97  bool keep_last_solution = false) = 0;
99  virtual void calculateMismatch() = 0;
101  virtual void calculateJacobian() = 0;
103  virtual void updateSolution() = 0;
105  virtual void setSolution() = 0;
106 
108  void initialize() override;
110  void initializeComponents();
114  void setBaseApparentPower();
116  void determinePFBusType();
119 
123  CPS::Real G(int i, int j);
125  CPS::Real B(int i, int j);
127  Bool solvePowerflow();
129  CPS::Bool checkConvergence();
131  CPS::String logVector(std::vector<CPS::UInt> indexVector) {
132  std::stringstream result;
133  std::copy(indexVector.begin(), indexVector.end(),
134  std::ostream_iterator<CPS::UInt>(result, " "));
135  return result.str();
136  };
138  CPS::Task::List getTasks() override;
139  // determines power flow bus type for each node according to the components attached to it.
140 public:
142  PFSolver(CPS::String name, CPS::SystemTopology system, Real timeStep,
143  CPS::Logger::Level logLevel);
145  virtual ~PFSolver(){};
146 
148  void setVDNode(CPS::String name);
150  void modifyPowerFlowBusComponent(CPS::String name,
151  CPS::PowerflowBusType powerFlowBusType);
153  void setSolverAndComponentBehaviour(Solver::Behaviour behaviour) override;
154 
155  class SolveTask : public CPS::Task {
156  public:
157  SolveTask(PFSolver &solver)
158  : Task(solver.mName + ".Solve"), mSolver(solver) {
159  mModifiedAttributes.push_back(Scheduler::external);
160  }
161 
162  void execute(Real time, Int timeStepCount);
163 
164  private:
165  PFSolver &mSolver;
166  };
167 };
168 } // 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:147
std::vector< std::shared_ptr< CPS::SP::Ph1::Load > > mLoads
Vector of load components.
Definition: PFSolver.h:67
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
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:131
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:472
void assignMatrixNodeIndices()
Assignment of matrix indices for nodes.
Definition: PFSolver.cpp:68
std::vector< CPS::UInt > mVDBusIndices
Vector with indices of VD buses.
Definition: PFSolver.h:42
void initializeComponents()
Initialization of individual components.
Definition: PFSolver.cpp:82
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:350
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:333
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:422
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:124
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:420
void composeAdmittanceMatrix()
Compose admittance matrix.
Definition: PFSolver.cpp:393
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:260
void setSolverAndComponentBehaviour(Solver::Behaviour behaviour) override
set solver and component to initialization or simulation behaviour
Definition: PFSolver.cpp:365
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:431
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:418
std::vector< CPS::UInt > mPQPVBusIndices
Vector with indices of both PQ and PV buses.
Definition: PFSolver.h:44
Base class for more specific solvers such as MNA, ODE or IDA.
Definition: Solver.h:30
String mName
Name for logging.
Definition: Solver.h:39