Skip to content

Commit

Permalink
change battery values
Browse files Browse the repository at this point in the history
  • Loading branch information
AhmedAredah committed May 25, 2023
1 parent d122f91 commit fb5e6b4
Show file tree
Hide file tree
Showing 9 changed files with 270 additions and 170 deletions.
28 changes: 20 additions & 8 deletions src/TrainDefintion/EnergyConsumption.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ namespace EC {
static double DefaultLocomotiveBatteryMaxCharge_HydogenHybrid = 4000.0;

/** The default locomotive battery initial charge for the electric locomotive. */
static double DefaultLocomotiveBatteryInitialCharge_Electric = 0.9;
static double DefaultLocomotiveBatteryInitialCharge_Electric = 0.6;
/** The default locomotive battery initial charge for the diesel hybrid locomotive. */
static double DefaultLocomotiveBatteryInitialCharge_DieselHybrid = 0.9;
static double DefaultLocomotiveBatteryInitialCharge_DieselHybrid = 0.6;
/** The default locomotive battery initial charge for the biodiesel hybrid locomotive. */
static double DefaultLocomotiveBatteryInitialCharge_BioDieselHybrid = 0.9;
static double DefaultLocomotiveBatteryInitialCharge_BioDieselHybrid = 0.6;

static double DefaultLocomotiveBatteryInitialCharge_HydrogenHybrid = 0.1;
static double DefaultLocomotiveBatteryInitialCharge_HydrogenHybrid = 0.6;

/** The default locomotive tank maximum capacity in Liters.
Tank capacity of 5,300 gallons. */
Expand All @@ -60,19 +60,25 @@ namespace EC {
static constexpr double DefaultLocomotiveBatteryCRate = 2.0;
/** (Immutable) the default locomotive battery max state of charge in
rechage state for diesel generator.*/
static constexpr double DefaultLocomotiveBatteryRechargeMaxSOC_Diesel = 0.5;
static constexpr double DefaultLocomotiveBatteryRechargeMaxSOC_Diesel = 0.65;
/** (Immutable) the default locomotive battery max state of charge in
rechage state for any generator other than diesel.*/
static constexpr double DefaultLocomotiveBatteryRechargeMaxSOC_Other = 0.5;//0.7
static constexpr double DefaultLocomotiveBatteryRechargeMaxSOC_Other = 0.65;
/** (Immutable) the default locomotive battery min state of charge in which
it requires recharge once reached state for diesel generator.*/
static constexpr double DefaultLocomotiveBatteryRechargeMinSOC_Diesel = 0.1;
static constexpr double DefaultLocomotiveBatteryRechargeMinSOC_Diesel = 0.20;
/** (Immutable) the default locomotive battery min state of charge in which
it requires recharge once reached state for any generator except diesel.*/
static constexpr double DefaultLocomotiveBatteryRechargeMinSOC_Other = 0.1;//0.5
static constexpr double DefaultLocomotiveBatteryRechargeMinSOC_Other = 0.50;//0.5

/** required generator power percentage. */
static constexpr double requiredGeneratorPower[] = {1,1,0.8,0.6,0.4,0.2,0.1,0.0};

static constexpr double DefaultLocomotivePowerReduction_DieselHybrid = 0.8;

static constexpr double DefaultLocomotivePowerReduction_BioDieselHybrid = 0.8;

static constexpr double DefaultLocomotivePowerReduction_HydrogenHybrid = 0.5;
// ##################################################################
// # end: define locomotive default values #
// ##################################################################
Expand Down Expand Up @@ -167,6 +173,10 @@ namespace EC {
*/
double getGeneratorEff(TrainTypes::PowerType powerType, double powerAtWheelProportion);

double getBatteryEff(TrainTypes::PowerType powerType);

std::pair<double,double> getMaxEffeciencyRange(TrainTypes::PowerType powerType);

/**
* Gets required generator power percentage for recharging the battery
*
Expand All @@ -190,6 +200,8 @@ namespace EC {
* @returns The emissions.
*/
double getEmissions(double fuelConsumption);

double getLocomotivePowerReductionFactor(TrainTypes::PowerType powerType);
}


Expand Down
302 changes: 167 additions & 135 deletions src/TrainDefintion/Locomotive.cpp

Large diffs are not rendered by default.

18 changes: 17 additions & 1 deletion src/TrainDefintion/Locomotive.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#ifndef NeTrainSim_Locomotive_h
#define NeTrainSim_Locomotive_h

#include <functional>
#include <string>
#include <iostream>
//#include "../util/List.h"
Expand Down Expand Up @@ -86,6 +87,7 @@ class Locomotive : public TrainComponent{
/** (Immutable) the gravitation acceleration */
const double g = 9.8067;

double usedPowerPortion = 0.0;


/**
Expand Down Expand Up @@ -344,9 +346,23 @@ class Locomotive : public TrainComponent{
*
* @param timeStep
* @param trainSpeed
* @param LocomotiveVirtualTractivePower
*
* @return
*/
double getMaxRechargeEnergy(double timeStep, double trainSpeed);
double getMaxRechargeEnergy(double timeStep, double trainSpeed, double LocomotiveVirtualTractivePower);

double getUsedPowerPortion(double trainSpeed, double LocomotiveVirtualTractivePower);

void rechargeBatteryByMaxFlow(double timeStep, double trainSpeed, double powerPortion,
double fuelConversionFactor, double fuelDensity,
double LocomotiveVirtualTractivePower,
std::function<std::pair<bool, double>(double, double, double)> ConsumeFuelFunc);

std::pair<bool, double> consumeEnergyFromHybridTechnology(double timeStep, double trainSpeed, double powerPortion,
double EC_kwh, double fuelConversionFactor,
double fuelDensity, double LocomotiveVirtualTractivePower,
std::function<std::pair<bool, double>(double, double, double)> ConsumeFuelFunc);

/**
* Define throttle levels
Expand Down
43 changes: 29 additions & 14 deletions src/TrainDefintion/Train.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -907,28 +907,37 @@ bool Train::consumeEnergy(double& timeStep, double trainSpeed, Vector<double>& u
this->ActiveLocos.at(i)->resetPowerRestriction();
// calculate the amount of energy consumption
double averageSpeed = (this->currentSpeed + this->previousSpeed) / (double)2.0;
EC_kwh = this->ActiveLocos.at(i)->getEnergyConsumption(usedTractivePower.at(i),
double UsedTractiveP = usedTractivePower.at(i);
EC_kwh = this->ActiveLocos.at(i)->getEnergyConsumption(UsedTractiveP,
this->currentAcceleration, averageSpeed, timeStep);

// consume/recharge fuel from/to the locomotive if it still has fuel or can be rechargable
auto out = this->ActiveLocos.at(i)->consumeFuel(timeStep, trainSpeed, EC_kwh, usedTractivePower.at(i));
auto out = this->ActiveLocos.at(i)->consumeFuel(timeStep, trainSpeed, EC_kwh, UsedTractiveP);
//bool fuelConsumed = out.first;
double restEC = out.second;

// if it is energy consumption and fuel was not consumed from the locomotive, consume it by the tenders
if (restEC > 0.0) {
//consume it equally from the tenders with similar fuel type
bool fuelConsumedFromTender = this->consumeTendersEnergy(timeStep,
std::pair<bool, double> fuelConsumedFromTender = this->consumeTendersEnergy(timeStep,
trainSpeed,
restEC,
this->ActiveLocos.at(i)->powerType);
// TODO: Take care of reducing notch number when there is no enough power >> later releases
// make sure all energy is consumed from active locomotives
while(fuelConsumedFromTender.first && fuelConsumedFromTender.second > 0.0) {
fuelConsumedFromTender = this->consumeTendersEnergy(timeStep,
trainSpeed,
fuelConsumedFromTender.second,
this->ActiveLocos.at(i)->powerType);
}
// When not all the energy is consumed by the tender
// reduce the current notch to a lower position,
// since there is no power source can provide all required energy
if (!fuelConsumedFromTender && restEC != EC_kwh) {
if (!fuelConsumedFromTender.first && fuelConsumedFromTender.second > 0.0) {
this->ActiveLocos.at(i)->reducePower();
}
if (!fuelConsumedFromTender && restEC == EC_kwh) {
if (!fuelConsumedFromTender.first && restEC == EC_kwh) {
this->ActiveLocos.at(i)->isLocOn = false;
}
}
Expand All @@ -946,32 +955,37 @@ bool Train::consumeEnergy(double& timeStep, double trainSpeed, Vector<double>& u
return (offLocos != this->locomotives.size());
}

bool Train::consumeTendersEnergy(double timeStep, double trainSpeed,
std::pair<bool, double> Train::consumeTendersEnergy(double timeStep, double trainSpeed,
double EC_kwh, TrainTypes::PowerType powerType,
double dieselConversionFactor,
double hydrogenConversionFactor,
double dieselDensity) {

bool consumed = false;
// bool consumed = false;
double notConsumed = 0.0;
int count = this->ActiveCarsTypes[TrainTypes::powerToCarMap.at(powerType)].size();
double ECD = 0.0;
if (count > 0) { ECD = EC_kwh / count; }
else { return consumed; }
else { return std::make_pair(false, EC_kwh); }


for (auto& car : this->ActiveCarsTypes[TrainTypes::powerToCarMap.at(powerType)]) {
// if the tender/battery still has energy to draw from, consume it
if (car->getBatteryCurrentCharge() > 0 || car->getTankCurrentCapacity() > 0) {
car->consumeFuel(timeStep, trainSpeed, ECD, 0.0 ,dieselConversionFactor,
hydrogenConversionFactor, dieselDensity);
consumed = true;
std::pair<bool, double> out = car->consumeFuel(timeStep, trainSpeed,
ECD, 0.0 ,dieselConversionFactor,
hydrogenConversionFactor, dieselDensity);
notConsumed += out.second;
// consumed = true;
}
// remove the car from the active list
else {
this->ActiveCarsTypes[TrainTypes::powerToCarMap.at(powerType)].removeValue(car);
notConsumed += ECD;
}
}
return consumed;
return std::make_pair( !(EC_kwh == notConsumed), notConsumed);
// return consumed;
}

bool Train::rechargeCarsBatteries(double timeStep, double EC_kwh, std::shared_ptr<Locomotive> &loco) {
Expand All @@ -985,8 +999,9 @@ bool Train::rechargeCarsBatteries(double timeStep, double EC_kwh, std::shared_pt
}
// refill all cars by that shared portion
for (auto& car : this->carsTypes[TrainTypes::CarType::batteryTender]) {
if (! car->refillBattery(timeStep, ECD)) {
loco->rechargeCatenary(ECD);
double restEC = car->refillBattery(timeStep, ECD);
if ( restEC > 0.0) {
loco->rechargeCatenary(restEC);
consumed = true;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/TrainDefintion/Train.h
Original file line number Diff line number Diff line change
Expand Up @@ -824,7 +824,7 @@ class Train {
*
* @returns True if it succeeds, false if it fails.
*/
bool consumeTendersEnergy(double timeStep, double trainSpeed, double EC_kwh, TrainTypes::PowerType powerType,
std::pair<bool, double> consumeTendersEnergy(double timeStep, double trainSpeed, double EC_kwh, TrainTypes::PowerType powerType,
double dieselConversionFactor = EC::DefaultDieselConversionFactor,
double hydrogenConversionFactor = EC::DefaultHydrogenConversionFactor,
double dieselDensity = EC::DefaultDieselDensity);
Expand Down
19 changes: 10 additions & 9 deletions src/TrainDefintion/TrainComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,24 +83,21 @@ std::pair<bool, double> TrainComponent::consumeElectricity(double timeStep, doub
// update stats
this->energyConsumed = EC_kwh;
this->cumEnergyConsumed += this->energyConsumed;
return out;
}
// if it consume part of the electricity
else if (out.first && out.second > 0.0) {
// update stats
this->energyConsumed = EC_kwh - out.second;
this->cumEnergyConsumed += this->energyConsumed;
return out;
}
else {
return out;
}

return out;
} // end if
else {
// update stats
this->energyConsumed = EC_kwh;
this->cumEnergyConsumed += this->energyConsumed;
this->hostLink->catenaryCumConsumedEnergy += EC_kwh;
this->hostLink->catenaryCumConsumedEnergy += EC_kwh;
// return true as all energy required is consumed from the catenary
return std::make_pair(true, 0.0);
} // end else
Expand All @@ -127,21 +124,25 @@ std::pair<bool, double> TrainComponent::consumeFuelHydrogen(double EC_kwh, doubl
return std::make_pair(false, EC_kwh); // return the tender is empty now and cannot provide any more fuel
}

bool TrainComponent::refillBattery(double timeStep, double EC_kwh) {
double TrainComponent::refillBattery(double timeStep, double EC_kwh) {
double ER = std::abs(EC_kwh); // because the passed EC_kwh is negative when it is recharge value
// get how much regenerated energy and pushed to the battery,
// 0.0 means no energy was pushed to the battery
this->energyRegenerated = this->rechargeBattery(timeStep, ER);
this->energyRegenerated = this->rechargeBatteryByRegeneratedEnergy(timeStep, ER);
this->cumEnergyRegenerated += this->energyRegenerated;
// if the battery is full, it will return 0.0 recharged to the battery.
// if the battery recharge all/part of the energy, it will return any other valye
return (this->energyRegenerated != 0.0);
return (ER - this->energyRegenerated);
}

bool TrainComponent::rechargeCatenary(double EC_kwh) {
// if the link the vehicle is on has catenary, recharge it
if (this->hostLink->hasCatenary){
// trasfer regenerated energy to the catenary
this->hostLink->catenaryCumRegeneratedEnergy += std::abs(EC_kwh);
// add the total amount of regenerated energy to the train total Regenerated Energy
this->energyRegenerated = std::abs(EC_kwh);
this->cumEnergyRegenerated += this->energyRegenerated;
return true;
} // end if
return false;
Expand Down
2 changes: 1 addition & 1 deletion src/TrainDefintion/TrainComponent.h
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ class TrainComponent : public Battery, public Tank{
* @param EC_kwh
* @return
*/
virtual bool refillBattery(double timeStep, double EC_kwh);
virtual double refillBattery(double timeStep, double EC_kwh);

/**
* @brief Rechage catenary and grid system if they are available
Expand Down
2 changes: 1 addition & 1 deletion src/TrainDefintion/TrainsList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@

// if the file has trains continue, else stop and throw error
if (lines.size() == 0) {
std::cerr << "Trains file is empty!" << std::endl;
std::cerr << "Trains file " << fileName << " is empty!" << std::endl;
exit(static_cast<int>(Error::emptyTrainsFile));
}

Expand Down
24 changes: 24 additions & 0 deletions src/util/Vector.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <vector>
#include <algorithm>
#include <numeric>
#include <sstream>

/**
* A vector.
Expand Down Expand Up @@ -192,6 +193,29 @@ class Vector : public std::vector<T> {
}
return false;
}

/**
* Convert this object into a string representation
*
* @author Ahmed Aredah
* @date 2/28/2023
*
* @returns A std::string that represents this object.
*/
std::string toString() const {
std::stringstream ss = std::stringstream("");
bool first = true;
ss << "[ ";
for (const auto& elem : *this) {
if (!first) {
ss << ", ";
}
ss << elem;
first = false;
}
ss << "]";
return ss.str();
}
};

#endif // VECTOR_H

0 comments on commit fb5e6b4

Please sign in to comment.