LCOV - code coverage report
Current view: top level - src/utils/traction_wire - Element.cpp (source / functions) Coverage Total Hit
Test: lcov.info Lines: 82.0 % 89 73
Test Date: 2024-11-20 15:55:46 Functions: 86.4 % 22 19

            Line data    Source code
       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              : /****************************************************************************/
      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          424 : Element::Element(std::string name, ElementType type, double value) {
      34          424 :     this->id = -2;
      35          424 :     this->name = name;
      36          424 :     this->type = type;
      37          424 :     this->isenabled = true;
      38          424 :     this->resistance = 0;
      39          424 :     this->current = 0;
      40          424 :     this->voltage = 0;
      41          424 :     this->powerWanted = NAN;
      42          424 :     switch (type) {
      43          180 :         case CURRENT_SOURCE_traction_wire:
      44          180 :             this->current = value;
      45          180 :             break;
      46           11 :         case VOLTAGE_SOURCE_traction_wire:
      47           11 :             this->voltage = value;
      48           11 :             break;
      49          233 :         case RESISTOR_traction_wire:
      50          233 :             this->resistance = value;
      51          233 :             break;
      52            0 :         default:
      53            0 :             WRITE_ERRORF(TL("Undefined element type for '%'."), name);
      54            0 :             break;
      55              :     }
      56          424 :     this->pNode = nullptr;
      57          424 :     this->nNode = nullptr;
      58          424 : }
      59              : 
      60            0 : void Element::setVoltage(double voltageIn) {
      61            0 :     this->voltage = voltageIn;
      62            0 : }
      63          993 : void Element::setCurrent(double currentIn) {
      64          993 :     this->current = currentIn;
      65          993 : }
      66          360 : void Element::setResistance(double resistanceIn) {
      67          360 :     if (resistanceIn <= 1e-6) {
      68            0 :         this->resistance = 1e-6;
      69              :     } else {
      70          360 :         this->resistance = resistanceIn;
      71              :     }
      72          360 : }
      73          180 : void Element::setPowerWanted(double powerWantedIn) {
      74          180 :     this->powerWanted = powerWantedIn;
      75          180 : }
      76         1102 : double Element::getVoltage() {
      77         1102 :     if (!this->isenabled) {
      78              :         return DBL_MAX;
      79              :     }
      80         1102 :     if (getType() == Element::ElementType::VOLTAGE_SOURCE_traction_wire) {
      81          922 :         return voltage;
      82              :     }
      83          180 :     return this->pNode->getVoltage() - this->nNode->getVoltage();
      84              : }
      85          753 : double Element::getCurrent() {
      86          753 :     if (!this->isenabled) {
      87              :         return DBL_MAX;
      88              :     }
      89          753 :     switch (this->type) {
      90            0 :         case Element::ElementType::RESISTOR_traction_wire:
      91            0 :             return -1 * getVoltage() / resistance;
      92          753 :         case Element::ElementType::CURRENT_SOURCE_traction_wire:
      93              :         case Element::ElementType::VOLTAGE_SOURCE_traction_wire:
      94          753 :             return current;
      95              :         default:
      96              :             return 0;
      97              :     }
      98              : }
      99        11213 : double Element::getResistance() {
     100        11213 :     return this->resistance;
     101              : }
     102         2046 : double Element::getPowerWanted() {
     103         2046 :     return      this->powerWanted;
     104              : }
     105          191 : double Element::getPower() {
     106          191 :     return      -1 * getCurrent() * getVoltage();
     107              : }
     108         5803 : int Element::getId() {
     109              : 
     110         5803 :     return this->id;
     111              : }
     112         4405 : Node* Element::getPosNode() {
     113         4405 :     return this->pNode;
     114              : }
     115         1558 : Node* Element::getNegNode() {
     116         1558 :     return this->nNode;
     117              : }
     118              : 
     119         9926 : Element::ElementType Element::getType() {
     120         9926 :     return this->type;
     121              : }
     122         5754 : std::string Element::getName() {
     123         5754 :     return this->name;
     124              : }
     125              : 
     126          799 : void Element::setPosNode(Node* node) {
     127          799 :     this->pNode = node;
     128              : 
     129          799 : }
     130          424 : void Element::setNegNode(Node* node) {
     131          424 :     this->nNode = node;
     132          424 : }
     133           11 : void Element::setId(int newId) {
     134           11 :     this->id = newId;
     135           11 : }
     136              : 
     137              : // if node == pNode, return nNode, else if node == nNode return pNode, else return nullptr
     138        10430 : Node* Element::getTheOtherNode(Node* node) {
     139        10430 :     if (node == pNode) {
     140         6347 :         return nNode;
     141         4083 :     } else if (node == nNode) {
     142              :         return pNode;
     143              :     } else {
     144            0 :         return nullptr;
     145              :     }
     146              : }
     147              : 
     148         1946 : bool Element::isEnabled() {
     149         1946 :     return isenabled;
     150              : }
     151              : 
     152            0 : void Element::setEnabled(bool newIsEnabled) {
     153            0 :     this->isenabled = newIsEnabled;
     154            0 : }
     155              : 
     156            0 : void Element::setType(ElementType ET) {
     157            0 :     this->type = ET;
     158            0 : }
        

Generated by: LCOV version 2.0-1