Eclipse SUMO - Simulation of Urban MObility
Circuit.h
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2001-2022 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 // avoid warnings in clang
32 #ifdef __clang__
33 #pragma clang system_header
34 #endif
35 #ifdef WIN32
36 #pragma warning(push, 0)
37 #endif
38 #include "Eigen/Dense"
39 #include "Eigen/Geometry"
40 #include "Eigen/Sparse"
41 #ifdef WIN32
42 #pragma warning(pop)
43 #endif
44 #endif
45 
46 #include "Element.h"
47 
48 // ===========================================================================
49 // class declarations
50 // ===========================================================================
51 class Node;
52 
53 
54 // ===========================================================================
55 // class definitions
56 // ===========================================================================
61 class Circuit {
62 
63 private:
64 
65  std::vector<Node*>* nodes;
66  std::vector<Element*>* elements;
67  std::vector<Element*>* voltageSources;
68 
69  int lastId;
70  bool iscleaned;
71 
74 
85  double alphaBest;
86 public:
95  enum alphaFlag {
104  };
105 private:
107 
108 public:
109  Node* getNode(std::string name);
110  Element* getElement(std::string name);
111  Node* getNode(int id);
112  Element* getVoltageSource(int id);
113  std::vector<Element*>* getCurrentSources();
114 
120  std::string& getCurrentsOfCircuitSource(std::string& currents);
121 
122  void lock();
123  void unlock();
124 
126  double getAlphaBest() {
127  return alphaBest;
128  };
129 
132  return alphaReason;
133  };
134 
135 private:
136 
137  Element* getElement(int id);
138  /*
139  * detects removable nodes = sets node variable "isremovable" to true if node is removable and adds id of such node to "removable_ids" vector
140  * node is denoted as removable if it is connected just to 2 elements and both of them are resistor
141  * the reason is that in such case there are two serial resistor and we can only sum their resistance value
142  *
143  * "removable_ids" vector is sort from the least to the greatest
144  */
145  void detectRemovableNodes(std::vector<int>* removable_ids);
146 
147  void deployResults(double* vals, std::vector<int>* removable_ids);
148 
149 #ifdef HAVE_EIGEN
150  /*
151  * creates all of the equations that represent the circuit
152  * in the form Ax = B(1/x) where A and B are matricies
153  * @param eqn : A
154  * @param vals : B
155  */
156  bool createEquationsNRmethod(double*& eqs, double*& vals, std::vector<int>* removable_ids);
157 
158  /*
159  * creates the nodal equation of the node 'node' GV = I
160  * in the form Ax = B(1/x) where A is a matrix with one row
161  * @param node : the node to be analyzed
162  * @param eqn : A
163  * @param val : B
164  */
165  bool createEquationNRmethod(Node* node, double* eqn, double& val, std::vector<int>* removable_ids);
166 
176  bool createEquation(Element* vsource, double* eqn, double& val);
177 
178  /*
179  * removes the "colToRemove"-th column from matrix "matrix"
180  */
181  void removeColumn(Eigen::MatrixXd& matrix, const int colToRemove);
182 
183  /*
184  * solves the system of nonlinear equations Ax = B(1/x)
185  * @param eqn : A
186  * @param vals : B
187  */
188  bool solveEquationsNRmethod(double* eqn, double* vals, std::vector<int>*);
189 
190  bool _solveNRmethod();
191 
192 #endif
193 public:
194 
195  // a Constructor, same functionality as "init" functions
196  Circuit();
197  // RICE_CHECK: Is this a traction substation current limit, global for all substations?
199  Circuit(double currentLimit);
200 
201  // adds an element with name "name", type "type" and value "value" to positive node "pNode" and negative node "nNode""
202  Element* addElement(std::string name, double value, Node* pNode, Node* nNode, Element::ElementType et);
203 
204  void eraseElement(Element* element);
205 
206  // adds a node with name "name"
207  Node* addNode(std::string name);
208 
209  // erases a node with name "name"
210  void eraseNode(Node* node);
211 
212  // gets current through element "name"
213  double getCurrent(std::string name);
214 
215  // gets voltage across element or node "name"
216  double getVoltage(std::string name);
217 
218  // gets the resistance of an element.
219  double getResistance(std::string name);
220 
221  // gets the number of voltage sources in the circuit.
222  int getNumVoltageSources();
223 
224  // checks if the circuit's connections are correct.
225  bool checkCircuit(std::string substationId = "");
226 
227 #ifdef HAVE_EIGEN
228  // solves the circuit and deploys the results
229  bool solve();
230 #endif
231 
232  // cleans up after superposition.
233  void cleanUpSP();
234 
235  //replaces unusedNode with newNode everywhere in the circuit, modifies the ids of other nodes and elements, descreases the id by one and deletes unusedNode
236  void replaceAndDeleteNode(Node* unusedNode, Node* newNode);
237 
238  // returns lastId
239  int getLastId() {
240  return lastId;
241  };
242 
243  // decreases lastId by one
245  lastId--;
246  };
247 
250  void setCurrentLimit(double myCurrentLimit) {
251  circuitCurrentLimit = myCurrentLimit;
252  };
253 
255  double getCurrentLimit() {
256  return circuitCurrentLimit;
257  };
258 };
double getAlphaBest()
return alphaBest variable, the best alpha scaling value
Definition: Circuit.h:126
std::vector< Node * > * nodes
Definition: Circuit.h:65
std::vector< Element * > * getCurrentSources()
Definition: Circuit.cpp:191
Node * addNode(std::string name)
Definition: Circuit.cpp:55
double getVoltage(std::string name)
Definition: Circuit.cpp:89
int getNumVoltageSources()
Definition: Circuit.cpp:995
Element * addElement(std::string name, double value, Node *pNode, Node *nNode, Element::ElementType et)
Definition: Circuit.cpp:803
int lastId
Definition: Circuit.h:69
void eraseNode(Node *node)
Definition: Circuit.cpp:75
void cleanUpSP()
Definition: Circuit.cpp:905
double getCurrent(std::string name)
Definition: Circuit.cpp:81
void unlock()
Definition: Circuit.cpp:206
int getLastId()
Definition: Circuit.h:239
Element * getElement(std::string name)
Definition: Circuit.cpp:129
Circuit()
Definition: Circuit.cpp:597
double circuitCurrentLimit
The electric current limit of the voltage sources.
Definition: Circuit.h:73
double getTotalCurrentOfCircuitSources()
The sum of voltage source currents in the circuit.
Definition: Circuit.cpp:169
void lock()
Definition: Circuit.cpp:202
void detectRemovableNodes(std::vector< int > *removable_ids)
Definition: Circuit.cpp:777
double getCurrentLimit()
@ brief Get the electric current limit of this circuit.
Definition: Circuit.h:255
std::vector< Element * > * elements
Definition: Circuit.h:66
void replaceAndDeleteNode(Node *unusedNode, Node *newNode)
Definition: Circuit.cpp:855
Element * getVoltageSource(int id)
Definition: Circuit.cpp:152
alphaFlag alphaReason
Definition: Circuit.h:106
double getTotalPowerOfCircuitSources()
The sum of voltage source powers in the circuit.
Definition: Circuit.cpp:161
void deployResults(double *vals, std::vector< int > *removable_ids)
Definition: Circuit.cpp:504
double getResistance(std::string name)
Definition: Circuit.cpp:103
double alphaBest
Best alpha scaling value.
Definition: Circuit.h:85
bool checkCircuit(std::string substationId="")
Definition: Circuit.cpp:918
std::vector< Element * > * voltageSources
Definition: Circuit.h:67
alphaFlag
Flag of alpha scaling parameter.
Definition: Circuit.h:95
@ ALPHA_VOLTAGE_LIMITS
The scaling alpha is applied (is not one] due to voltage limits.
Definition: Circuit.h:101
@ ALPHA_NOT_APPLIED
The scaling alpha is not applied (is one)
Definition: Circuit.h:97
@ ALPHA_CURRENT_LIMITS
The scaling alpha is applied (is not one) due to current limits.
Definition: Circuit.h:99
@ ALPHA_NOT_CONVERGING
The Newton-Rhapson method has reached maximum iterations and no solution of circuit has been found wi...
Definition: Circuit.h:103
alphaFlag getAlphaReason()
return the reason why alpha scaling value has been used
Definition: Circuit.h:131
Node * getNode(std::string name)
Definition: Circuit.cpp:111
void descreaseLastId()
Definition: Circuit.h:244
bool iscleaned
Definition: Circuit.h:70
void setCurrentLimit(double myCurrentLimit)
Set the electric current limit of this circuit.
Definition: Circuit.h:250
std::string & getCurrentsOfCircuitSource(std::string &currents)
List of currents of voltage sources as a string.
Definition: Circuit.cpp:178
void eraseElement(Element *element)
Definition: Circuit.cpp:847
ElementType
Definition: Element.h:53
Definition: Node.h:39