Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/src/utils/traction_wire/Element.cpp
169678 views
1
/****************************************************************************/
2
// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3
// Copyright (C) 2001-2025 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
/****************************************************************************/
14
/// @file Element.cpp
15
/// @author Jakub Sevcik (RICE)
16
/// @author Jan Prikryl (RICE)
17
/// @date 2019-12-15
18
///
19
/// @note based on work 2017 Ahmad Khaled, Ahmad Essam, Omnia Zakaria, Mary Nader
20
///
21
// Representation of electric circuit elements: resistors, voltage and current sources
22
/****************************************************************************/
23
#include <cfloat>
24
#include <cmath>
25
#include <utils/common/MsgHandler.h>
26
#include "Element.h"
27
#include "Node.h"
28
29
30
// ===========================================================================
31
// method definitions
32
// ===========================================================================
33
Element::Element(std::string name, ElementType type, double value) {
34
this->id = -2;
35
this->name = name;
36
this->type = type;
37
this->isenabled = true;
38
this->resistance = 0;
39
this->current = 0;
40
this->voltage = 0;
41
this->powerWanted = NAN;
42
switch (type) {
43
case CURRENT_SOURCE_traction_wire:
44
this->current = value;
45
break;
46
case VOLTAGE_SOURCE_traction_wire:
47
this->voltage = value;
48
break;
49
case RESISTOR_traction_wire:
50
this->resistance = value;
51
break;
52
default:
53
WRITE_ERRORF(TL("Undefined element type for '%'."), name);
54
break;
55
}
56
this->pNode = nullptr;
57
this->nNode = nullptr;
58
}
59
60
void Element::setVoltage(double voltageIn) {
61
this->voltage = voltageIn;
62
}
63
void Element::setCurrent(double currentIn) {
64
this->current = currentIn;
65
}
66
void Element::setResistance(double resistanceIn) {
67
if (resistanceIn <= 1e-6) {
68
this->resistance = 1e-6;
69
} else {
70
this->resistance = resistanceIn;
71
}
72
}
73
void Element::setPowerWanted(double powerWantedIn) {
74
this->powerWanted = powerWantedIn;
75
}
76
double Element::getVoltage() {
77
if (!this->isenabled) {
78
return DBL_MAX;
79
}
80
if (getType() == Element::ElementType::VOLTAGE_SOURCE_traction_wire) {
81
return voltage;
82
}
83
return this->pNode->getVoltage() - this->nNode->getVoltage();
84
}
85
double Element::getCurrent() {
86
if (!this->isenabled) {
87
return DBL_MAX;
88
}
89
switch (this->type) {
90
case Element::ElementType::RESISTOR_traction_wire:
91
return -1 * getVoltage() / resistance;
92
case Element::ElementType::CURRENT_SOURCE_traction_wire:
93
case Element::ElementType::VOLTAGE_SOURCE_traction_wire:
94
return current;
95
default:
96
return 0;
97
}
98
}
99
double Element::getResistance() {
100
return this->resistance;
101
}
102
double Element::getPowerWanted() {
103
return this->powerWanted;
104
}
105
double Element::getPower() {
106
return -1 * getCurrent() * getVoltage();
107
}
108
int Element::getId() {
109
110
return this->id;
111
}
112
Node* Element::getPosNode() {
113
return this->pNode;
114
}
115
Node* Element::getNegNode() {
116
return this->nNode;
117
}
118
119
Element::ElementType Element::getType() {
120
return this->type;
121
}
122
std::string Element::getName() {
123
return this->name;
124
}
125
126
void Element::setPosNode(Node* node) {
127
this->pNode = node;
128
129
}
130
void Element::setNegNode(Node* node) {
131
this->nNode = node;
132
}
133
void Element::setId(int newId) {
134
this->id = newId;
135
}
136
137
// if node == pNode, return nNode, else if node == nNode return pNode, else return nullptr
138
Node* Element::getTheOtherNode(Node* node) {
139
if (node == pNode) {
140
return nNode;
141
} else if (node == nNode) {
142
return pNode;
143
} else {
144
return nullptr;
145
}
146
}
147
148
bool Element::isEnabled() {
149
return isenabled;
150
}
151
152
void Element::setEnabled(bool newIsEnabled) {
153
this->isenabled = newIsEnabled;
154
}
155
156
void Element::setType(ElementType ET) {
157
this->type = ET;
158
}
159
160