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-2020 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 /****************************************************************************/
19 // Representation of electric circuit of overhead wires
21 /****************************************************************************/
22 #pragma once
23 
24 #include <vector>
25 #ifdef HAVE_EIGEN
26 #include "Eigen/Dense"
27 #include "Eigen/Geometry"
28 #endif
29 #include "Node.h"
30 #include "Element.h"
31 
38 class Circuit {
39 
40 private:
41 
42  vector<Node*>* nodes;
43  vector<Element*>* elements;
44  vector<Element*>* voltageSources;
45 
46  int lastId;
47  bool iscleaned;
48 
49 public:
50  Node* getNode(string name);
51  Element* getElement(string name);
52  Node* getNode(int id);
53  Element* getVoltageSource(int id);
54  vector<Element*>* getCurrentSources();
55 
56  void lock();
57  void unlock();
58 
69  double alphaBest;
70 
71 private:
72  Element* getElement(int id);
73 
74  /*
75  * detects removable nodes = sets node variable "isremovable" to true if node is removable and adds id of such node to "removable_ids" vector
76  * node is denoted as removable if it is connected just to 2 elements and both of them are resistor
77  * the reason is that in such case there are two serial resistor and we can only sum their resistance value
78  *
79  * "removable_ids" vector is sort from the least to the greatest
80  */
81  void detectRemovableNodes(std::vector<int>* removable_ids);
82 
83  void deployResults(double* vals, std::vector<int>* removable_ids);
84 
85 #ifdef HAVE_EIGEN
86  /*
87  * creates all of the equations that represent the circuit
88  * in the form Ax = B(1/x) where A and B are matricies
89  * @param eqn : A
90  * @param vals : B
91  */
92  bool createEquationsNRmethod(double*& eqs, double*& vals, std::vector<int>* removable_ids);
93 
94 
95  /*
96  * creates the nodal equation of the node 'node' GV = I
97  * in the form Ax = B(1/x) where A is a matrix with one row
98  * @param node : the node to be analyzed
99  * @param eqn : A
100  * @param val : B
101  */
102  bool createEquationNRmethod(Node* node, double* eqn, double& val, std::vector<int>* removable_ids);
103 
113  bool createEquation(Element* vsource, double* eqn, double& val);
114 
115  /*
116  * removes the "colToRemove"-th column from matrix "matrix"
117  */
118  void removeColumn(Eigen::MatrixXd& matrix, const int colToRemove);
119 
120  /*
121  * solves the system of nonlinear equations Ax = B(1/x)
122  * @param eqn : A
123  * @param vals : B
124  */
125  bool solveEquationsNRmethod(double* eqn, double* vals, std::vector<int>*);
126 
127  bool _solveNRmethod();
128 
129 #endif
130 public:
131 
132  // a Constructor, same functionality as "init" functions
133  Circuit();
134 
135  // adds an element with name "name", type "type" and value "value" to positive node "pNode" and negative node "nNode""
136  Element* addElement(string name, double value, Node* pNode, Node* nNode, Element::ElementType et);
137 
138  void eraseElement(Element* element);
139 
140  // adds a node with name "name"
141  Node* addNode(string name);
142 
143  // erases a node with name "name"
144  void eraseNode(Node* node);
145 
146  // gets current through element "name"
147  double getCurrent(string name);
148 
149  // gets voltage across element or node "name"
150  double getVoltage(string name);
151 
152  // gets the resistance of an element.
153  double getResistance(string name);
154 
155  // gets the number of voltage sources in the circuit.
156  int getNumVoltageSources();
157 
158  // checks if the circuit's connections are correct.
159  bool checkCircuit(std::string substationId = "");
160 
161 #ifdef HAVE_EIGEN
162  // solves the circuit and deploys the results
163  bool solve();
164 #endif
165 
166  // cleans up after superposition.
167  void cleanUpSP();
168 
169  //replaces unusedNode with newNode everywhere in the circuit, modifies the ids of other nodes and elements, descreases the id by one and deletes unusedNode
170  void replaceAndDeleteNode(Node* unusedNode, Node* newNode);
171 
172  // returns lastId
173  int getLastId() {
174  return lastId;
175  };
176 
177  // decreases lastId by one
179  lastId--;
180  };
181 };
vector< Element * > * voltageSources
Definition: Circuit.h:44
double getCurrent(string name)
Definition: Circuit.cpp:68
vector< Node * > * nodes
Definition: Circuit.h:42
int getNumVoltageSources()
Definition: Circuit.cpp:841
vector< Element * > * getCurrentSources()
Definition: Circuit.cpp:154
vector< Element * > * elements
Definition: Circuit.h:43
int lastId
Definition: Circuit.h:46
Element * addElement(string name, double value, Node *pNode, Node *nNode, Element::ElementType et)
Definition: Circuit.cpp:659
void eraseNode(Node *node)
Definition: Circuit.cpp:62
void cleanUpSP()
Definition: Circuit.cpp:751
void unlock()
Definition: Circuit.cpp:169
int getLastId()
Definition: Circuit.h:173
Circuit()
Definition: Circuit.cpp:492
void lock()
Definition: Circuit.cpp:165
void detectRemovableNodes(std::vector< int > *removable_ids)
Definition: Circuit.cpp:638
void replaceAndDeleteNode(Node *unusedNode, Node *newNode)
Definition: Circuit.cpp:701
Node * getNode(string name)
Definition: Circuit.cpp:98
double getVoltage(string name)
Definition: Circuit.cpp:76
Element * getVoltageSource(int id)
Definition: Circuit.cpp:145
void deployResults(double *vals, std::vector< int > *removable_ids)
Definition: Circuit.cpp:401
Element * getElement(string name)
Definition: Circuit.cpp:117
double alphaBest
Best alpha scaling value.
Definition: Circuit.h:69
bool checkCircuit(std::string substationId="")
Definition: Circuit.cpp:764
Node * addNode(string name)
Definition: Circuit.cpp:41
double getResistance(string name)
Definition: Circuit.cpp:90
void descreaseLastId()
Definition: Circuit.h:178
bool iscleaned
Definition: Circuit.h:47
void eraseElement(Element *element)
Definition: Circuit.cpp:693
ElementType
Definition: Element.h:45
Definition: Node.h:31