Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
Circuit.h
Go to the documentation of this file.
1/****************************************************************************/
2// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3// Copyright (C) 2001-2026 German Aerospace Center (DLR) and others.
4// This program and the accompanying materials are made available under the
5// terms of the Eclipse Public License 2.0 which is available at
6// https://www.eclipse.org/legal/epl-2.0/
7// This Source Code may also be made available under the following Secondary
8// Licenses when the conditions for such availability set forth in the Eclipse
9// Public License 2.0 are satisfied: GNU General Public License, version 2
10// or later which is available at
11// https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13/****************************************************************************/
24// Representation of electric circuit of overhead wires
25/****************************************************************************/
26#pragma once
27#include <config.h>
28
29#include <vector>
30#ifdef HAVE_EIGEN
31#ifdef _MSC_VER
32#pragma warning(push)
33#pragma warning(disable: 4127 4464 5031)
34#endif
35// avoid warnings in clang
36#ifdef __clang__
37#pragma clang system_header
38#endif
39#include <Eigen/Dense>
40#include <Eigen/Geometry>
41#include <Eigen/Sparse>
42#ifdef _MSC_VER
43#pragma warning(pop)
44#endif
45#endif
46
47#include "Element.h"
48
49// ===========================================================================
50// class declarations
51// ===========================================================================
52class Node;
53
54
55// ===========================================================================
56// class definitions
57// ===========================================================================
62class Circuit {
63
64public:
66 static void enforceCurrentLimits(const bool val) {
67 myCurrentLimits = val;
68 }
69
70private:
71
72 std::vector<Node*>* nodes;
73 std::vector<Element*>* elements;
74 std::vector<Element*>* voltageSources;
75
76 int lastId;
78
81
92 double alphaBest;
93
95 static bool myCurrentLimits;
96public:
115private:
117
118public:
119 Node* getNode(std::string name);
120 Element* getElement(std::string name);
121 Node* getNode(int id);
122 Element* getVoltageSource(int id);
123 std::vector<Element*>* getCurrentSources();
124
130 std::string& getCurrentsOfCircuitSource(std::string& currents);
131
132 void lock();
133 void unlock();
134
136 double getAlphaBest() {
137 return alphaBest;
138 }
139
142 return alphaReason;
143 }
144
145private:
146
147 Element* getElement(int id);
148 /*
149 * detects removable nodes = sets node variable "isremovable" to true if node is removable and adds id of such node to "removable_ids" vector
150 * node is denoted as removable if it is connected just to 2 elements and both of them are resistor
151 * the reason is that in such case there are two serial resistor and we can only sum their resistance value
152 *
153 * "removable_ids" vector is sort from the least to the greatest
154 */
155 void detectRemovableNodes(std::vector<int>* removable_ids);
156
157 void deployResults(double* vals, std::vector<int>* removable_ids);
158
159#ifdef HAVE_EIGEN
160 /*
161 * creates all of the equations that represent the circuit
162 * in the form Ax = B(1/x) where A and B are matrices
163 * @param eqn : A
164 * @param vals : B
165 */
166 bool createEquationsNRmethod(double*& eqs, double*& vals, std::vector<int>* removable_ids);
167
168 /*
169 * creates the nodal equation of the node 'node' GV = I
170 * in the form Ax = B(1/x) where A is a matrix with one row
171 * @param node : the node to be analyzed
172 * @param eqn : A
173 * @param val : B
174 */
175 bool createEquationNRmethod(Node* node, double* eqn, double& val, std::vector<int>* removable_ids);
176
186 bool createEquation(Element* vsource, double* eqn, double& val);
187
188 /*
189 * removes the "colToRemove"-th column from matrix "matrix"
190 */
191 void removeColumn(Eigen::MatrixXd& matrix, const int colToRemove);
192
193 /*
194 * solves the system of nonlinear equations Ax = B(1/x)
195 * @param eqn : A
196 * @param vals : B
197 */
198 bool solveEquationsNRmethod(double* eqn, double* vals, std::vector<int>*);
199
200 bool _solveNRmethod();
201
202#endif
203public:
204
205 // a Constructor, same functionality as "init" functions
206 Circuit();
207 // RICE_CHECK: Is this a traction substation current limit, global for all substations?
209 Circuit(double currentLimit);
210
211 // adds an element with name "name", type "type" and value "value" to positive node "pNode" and negative node "nNode""
212 Element* addElement(std::string name, double value, Node* pNode, Node* nNode, Element::ElementType et);
213
214 void eraseElement(Element* element);
215
216 // adds a node with name "name"
217 Node* addNode(std::string name);
218
219 // erases a node with name "name"
220 void eraseNode(Node* node);
221
222 // gets current through element "name"
223 double getCurrent(std::string name);
224
225 // gets voltage across element or node "name"
226 double getVoltage(std::string name);
227
228 // gets the resistance of an element.
229 double getResistance(std::string name);
230
231 // gets the number of voltage sources in the circuit.
233
234 // checks if the circuit's connections are correct.
235 bool checkCircuit(std::string substationId = "");
236
237#ifdef HAVE_EIGEN
238 // solves the circuit and deploys the results
239 bool solve();
240#endif
241
242 // cleans up after superposition.
243 void cleanUpSP();
244
245 //replaces unusedNode with newNode everywhere in the circuit, modifies the ids of other nodes and elements, decreases the id by one and deletes unusedNode
246 void replaceAndDeleteNode(Node* unusedNode, Node* newNode);
247
248 // returns lastId
249 int getLastId() {
250 return lastId;
251 };
252
253 // decreases lastId by one
255 lastId--;
256 };
257
260 void setCurrentLimit(double myCurrentLimit) {
261 circuitCurrentLimit = myCurrentLimit;
262 };
263
266 return circuitCurrentLimit;
267 };
268};
double getAlphaBest()
return alphaBest variable, the best alpha scaling value
Definition Circuit.h:136
std::vector< Node * > * nodes
Definition Circuit.h:72
std::vector< Element * > * getCurrentSources()
Definition Circuit.cpp:179
Node * addNode(std::string name)
Definition Circuit.cpp:43
double getVoltage(std::string name)
Definition Circuit.cpp:77
int getNumVoltageSources()
Definition Circuit.cpp:978
Element * addElement(std::string name, double value, Node *pNode, Node *nNode, Element::ElementType et)
Definition Circuit.cpp:786
int lastId
Definition Circuit.h:76
void eraseNode(Node *node)
Definition Circuit.cpp:63
void cleanUpSP()
Definition Circuit.cpp:888
double getCurrent(std::string name)
Definition Circuit.cpp:69
void unlock()
Definition Circuit.cpp:194
int getLastId()
Definition Circuit.h:249
Element * getElement(std::string name)
Definition Circuit.cpp:117
double circuitCurrentLimit
The electric current limit of the voltage sources.
Definition Circuit.h:80
double getTotalCurrentOfCircuitSources()
The sum of voltage source currents in the circuit.
Definition Circuit.cpp:157
void lock()
Definition Circuit.cpp:190
void detectRemovableNodes(std::vector< int > *removable_ids)
Definition Circuit.cpp:760
double getCurrentLimit()
@ brief Get the electric current limit of this circuit.
Definition Circuit.h:265
std::vector< Element * > * elements
Definition Circuit.h:73
void replaceAndDeleteNode(Node *unusedNode, Node *newNode)
Definition Circuit.cpp:838
Element * getVoltageSource(int id)
Definition Circuit.cpp:140
alphaFlag alphaReason
Definition Circuit.h:116
double getTotalPowerOfCircuitSources()
The sum of voltage source powers in the circuit.
Definition Circuit.cpp:149
void deployResults(double *vals, std::vector< int > *removable_ids)
Definition Circuit.cpp:489
double getResistance(std::string name)
Definition Circuit.cpp:91
double alphaBest
Best alpha scaling value.
Definition Circuit.h:92
bool checkCircuit(std::string substationId="")
Definition Circuit.cpp:901
std::vector< Element * > * voltageSources
Definition Circuit.h:74
alphaFlag
Flag of alpha scaling parameter.
Definition Circuit.h:105
@ ALPHA_VOLTAGE_LIMITS
The scaling alpha is applied (is not one] due to voltage limits.
Definition Circuit.h:111
@ ALPHA_NOT_APPLIED
The scaling alpha is not applied (is one)
Definition Circuit.h:107
@ ALPHA_CURRENT_LIMITS
The scaling alpha is applied (is not one) due to current limits.
Definition Circuit.h:109
@ ALPHA_NOT_CONVERGING
The Newton-Rhapson method has reached maximum iterations and no solution of circuit has been found wi...
Definition Circuit.h:113
alphaFlag getAlphaReason()
return the reason why alpha scaling value has been used
Definition Circuit.h:141
void decreaseLastId()
Definition Circuit.h:254
Node * getNode(std::string name)
Definition Circuit.cpp:99
static bool myCurrentLimits
whether per-substation overhead-wire current limits should be enforced
Definition Circuit.h:95
bool iscleaned
Definition Circuit.h:77
static void enforceCurrentLimits(const bool val)
whether per-substation overhead-wire current limits should be enforced
Definition Circuit.h:66
void setCurrentLimit(double myCurrentLimit)
Set the electric current limit of this circuit.
Definition Circuit.h:260
std::string & getCurrentsOfCircuitSource(std::string &currents)
List of currents of voltage sources as a string.
Definition Circuit.cpp:166
void eraseElement(Element *element)
Definition Circuit.cpp:830
ElementType
Definition Element.h:53
Definition Node.h:39