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