Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

#include <iostream>

#include <fstream>
#include <vector>
#include <cmath>
#include <sstream>

using namespace std;

int main() {
vector<double> times;
vector<double> velocities;
vector<double> enginepowers;

// Constants
const double gravity = 9.81;
const double air_density = 1.225;

// User Input
double mass, frontal_area, rolling_resistance_coefficient, drivetrain_efficiency, drag_coefficient;
double velocity;

cout << "Enter vehicle mass (in kg): ";


cin >> mass;
cout << "Enter frontal area (in m^2): ";
cin >> frontal_area;
cout << "Enter rolling resistance coefficient: ";
cin >> rolling_resistance_coefficient;
cout << "Enter drivetrain efficiency: ";
cin >> drivetrain_efficiency;
cout << "Enter drag coefficient: ";
cin >> drag_coefficient;

ifstream file("UDDS.txt");

if (!file.is_open()) {
cerr << "Failed to open UDDS.txt." << endl;
return 1;
}

string line;
while (getline(file, line)) {
stringstream ss(line);
double time, vel;
char comma;
ss >> time >> comma >> vel;
times.push_back(time);
velocities.push_back(vel);
}

file.close();

ofstream csvFile("output.csv");

csvFile << "Time (s), Velocity (km/h), Engine Power (kW)\n";

for (size_t i = 0; i < times.size(); ++i) {


double velocity_mps = velocities[i] * 1000 / 3600; // Convert km/h to m/s
double power_at_wheels = 0.5 * drag_coefficient * frontal_area * air_density * pow(velocity_mps, 3) +
mass * gravity * rolling_resistance_coefficient * velocity_mps;
double power_at_engine = (power_at_wheels/1000) / drivetrain_efficiency;

csvFile << times[i] << "," << velocities[i] << "," << power_at_engine << "\n";
enginepowers.push_back(power_at_engine);
}

csvFile.close();

cout << "Data written to output.csv" << endl;

return 0;
}

You might also like