ENGN 38

Solution

Lab Programming Assignment #10

 
 
 
// Student Number 13
// Wendy Kaufmyn
// ENGN 38
// Lab #10
 
/*
This program performs calculations for a 4 stroke IC engine parameters using pressure & volume data for one complete cycle of the engine.
It will determine the maximum pressure, minimum pressure, average pressure, compression ratio and the power of the engine.
Additionally it  plots the pressure versus time in a bar plot with the pressure on the horizontal axis and time on the vertical axis.
It also plots the pressure versus time in a bar plot with the time on the horizontal axis and pressure on the vertical axis.
*/
 
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <cmath>
using namespace std;
 
class enginePerformance
{
public:
 
      void retrieveData();               
     
      void setMaxPressure();       
      void setMinPressure();
      void setAveragePressure();
      void setCompressionRatio();
      void setEnginePower();
 
      void outputReport();         
 
 
private:
      ifstream fin;
      ofstream fout;
      string   dataTitle;
      string   engineID;
      string   date;
        int      dataCount;
      double   RPM,
               *pressure,
               *volume,
               compressionRatio,
               maxPressure,
               minPressure,
               averagePressure,
               power;
    
      void barPlot();
      void barPlotTransposed();
};
 
int main()
{
      enginePerformance myEngine;
    
      myEngine.retrieveData();
 
      myEngine.setMaxPressure();
      myEngine.setMinPressure();
      myEngine.setAveragePressure();
      myEngine.setCompressionRatio();
      myEngine.setEnginePower();
 
      myEngine.outputReport();
    
      return 0;
}
 
void enginePerformance::retrieveData()
{
      string garbage;
        char inputFileName[200];
 
      cout << "\n\nEnter the name (and path if necessary) of your input file. ";
      cin  >> inputFileName;
 
      fin.open(inputFileName);
      if (fin.fail())
      {    
            cout << "\nYour input file would not open. Sorry. Goodbye.\n";
            exit (1);
      }
       
      getline(fin, dataTitle);
        getline(fin, engineID);
      getline(fin, date);
      fin >> RPM;
        getline(fin, garbage);
      getline(fin, garbage);
 
        dataCount=0;
        while ( !fin.eof())
        {
              getline(fin, garbage);
              dataCount++;
        }
        dataCount--;
 
        fin.close();
        fin.clear();
        fin.open(inputFileName);
      if (fin.fail())
      {    
            cout << "\nYour input file would not open the second time. Sorry. Goodbye.\n";
            exit (1);
      }
 
        pressure = new double [dataCount];
        volume = new double [dataCount];
 
        for (int i = 1; i<=5; i++)
              getline(fin, garbage);
       
        for (int i=0; i<dataCount; i++)
        {
              fin >> pressure[i];
              fin >>   volume[i];
        }
 
        fin.close();
}
 
 
void enginePerformance::setMaxPressure()
{
      for (int i=0; i < dataCount; i++)
      {
            if (i == 0)
                  maxPressure = pressure[i];
 
            if (pressure[i] > maxPressure)
                  maxPressure = pressure[i];
      }
}
 
void enginePerformance::setMinPressure()
{
      for (int i=0; i < dataCount; i++)
      {
            if (i == 0)
                  minPressure = pressure[i];
            if (pressure[i] < minPressure)
                  minPressure = pressure[i];
      }
}
 
void enginePerformance::setAveragePressure()
{
      double sum(0);
      for (int i=0; i < dataCount; i++)
      {    
            sum += pressure[i];   
            averagePressure = sum/static_cast<double>(dataCount);
      }
}
 
void enginePerformance::setCompressionRatio()
{
      double maxVol, minVol;
      for (int j=0; j < dataCount; j++)
      {
            if (j == 0)
            {     
                  maxVol = volume[j];
                  minVol = volume[j];
            }
            if (volume[j] > maxVol)
                  maxVol = volume[j];
 
            if (volume[j] < minVol)
                  minVol = volume[j];
      }
      compressionRatio = maxVol / minVol;
}
 
void enginePerformance::setEnginePower()
{
      double   work = 0.0,
               delArea;
      for (int i=0; i < dataCount-1; i++)
      {      
            delArea = 0.5 * (pressure[i] + pressure[i+1]) * (volume[i+1] - volume[i]);  
            work += delArea;
      }
      // work as calculated here will be in units of in-lbs
      // It needs to be converted into Nm:
      // ( in-lb) * (0.0254 m/in) * (4.45 N/lb)=> 0.11303 Nm / in-lb
      // power in watts = work in Nm /time interval in seconds
      // The time interval is for one cycle
      // time for one cycle = 2 rev/cycle * 60 sec/RPM rev = (120/RPM) seconds for one cycle
      // So power = (work * 0.11303) / (120.0/RPM) = work / 1061.665*RPM
      power = work /(1061.665*RPM);
}
 
void enginePerformance::outputReport()
{
      char outputFileName[200];
      cout << "\nEnter the file name (and path if necessary) of your output file. ";
      cin    >> outputFileName;
 
      fout.open(outputFileName);
      if (fout.fail())
      {    
            cout << "\nYour output file had a problem. Sorry. Goodbye. \n";
            exit(1);
      }
 
      fout.setf(ios::fixed);
      fout.setf(ios::showpoint);
      fout.precision(1);
 
      fout << "\n\n" << dataTitle << " Report";
      fout << "\n\nEngine ID: " << engineID;
      fout << "\n\nAnalysis Date: " << date;
      fout << "\n\nEngine Preformance: Speed " << setw(8) << RPM << " RPM";
 
      fout << "\n\n Compression Ratio " << setw(6) << compressionRatio << ":1";
      fout << "\n\n Pressures ";
      fout << "\n Maximum " << setw(8) << maxPressure << " (psi)";
      fout << "\n Minimum " << setw(8) << minPressure << " (psi)";
      fout << "\n Average " << setw(8) << averagePressure << " (psi)";
      fout << "\n\n Power " << setw(8) << power << " (watts)";      
      fout << endl;
 
      barPlot();
      barPlotTransposed();  // This was not assigned
       
        fout.close();
}
 
 
void enginePerformance::barPlot()
{
      double pScale;
      int numAsterisks;
 
      cout << "\nHow many units of pressure do you "
            << "want one asterisk to represent for Bar Plot? ";
      cin >> pScale;
 
      fout.setf(ios::fixed);
      fout.setf(ios::showpoint);
      fout.precision(1);
 
      fout << "\n\nBAR PLOT OF CYLINDER PRESSURE -VS- TIME\n";
      fout << "\npressure is on horizontal axis - units are psi";
      fout << "\ntime is on vertical axis - units are msec\n\n ";
 
      fout << setw(3) << " ";
      for (int i=0; i<maxPressure/pScale; i++)
      {
            if (i%10 < 1)
                  fout << setw(5) << setiosflags(ios::left) << (i/10) * 10* pScale
                  << setw(5) << " ";
      }
 
      fout << setiosflags(ios::right);
      fout << endl;
      fout << setw(5) << " ";
      for (int i=0; i<maxPressure/pScale; i++)
      {
            if (i%10 < 1)
                  fout << "+";
            else
                  fout << "-";
      }
 
      fout << endl;
 
      double deltaT = 1000*(2*60)/(RPM*(dataCount-1));
      // time between pressure points in millisecs
    
      for (int i=0; i < dataCount; i++)
      {      
            numAsterisks = static_cast<int>((pressure[i]/pScale + 0.5));
            fout << setw(5) << static_cast<double>(i) * 1000*(2*60)/(RPM*(dataCount-1)) << "|";
            //this will put the time in msec on the vertical axis before each bar of asterisks
            for (int j=0; j<numAsterisks; j++)
                  fout << "*";
            fout << endl;
      }
}
 
void enginePerformance::barPlotTransposed()  // This was not assigned
{
      int yScale,
            *graphablePressure,
            maxGraphPressure,
            graphingArraySize;
      char  *graphingArray;
 
      cout << "How many units do you want one asterisk to represent on Transposed Bar Plot? ";
      cin  >> yScale;
      cout << endl << endl;
 
      graphablePressure = new int[dataCount];
      for (int i = 0; i<dataCount; i++)
            graphablePressure[i] = static_cast<int>(pressure[i]/yScale + 0.5);
            // This creates and puts values into an array of integers
            // that represents the pressures to be graphed,
            // scaled by desired scaled factor and rounded to nearest int
 
      maxGraphPressure = static_cast<int>(maxPressure/yScale + 0.5);
            // This is the maximum pressure
            // scaled by desired scaled factor and rounded to nearest int
 
      graphingArraySize = dataCount * maxGraphPressure;
      graphingArray = new char[graphingArraySize];
      for (int i = 0; i<maxGraphPressure ; i++)
      {       
            for(int j = 0; j<dataCount; j++)
            {       
                  if (graphablePressure[j] < maxGraphPressure - i)
                        graphingArray[i*dataCount+j] = ' ';
                  else
                        if (graphablePressure[j] >= maxGraphPressure -i)
                              graphingArray[i*dataCount+j] = '*';
            }
      }
      // This creates and puts values into an array of characters grouped by lines
      // Each group has dataCount elements
      // 2dim array would have been better
 
      fout << "\n\n\n\nTRANSPOSED BAR PLOT OF CYLINDER PRESSURE -VS- TIME";
      fout << "\n\n pressure (psi)\n";
      for(int i=0; i< maxGraphPressure; i++) //for line by line
      {
            int yScale2, printVariable;
            yScale2 = (maxGraphPressure - i) * yScale;
            // This will be a calibration number for the vertical axis
            printVariable = yScale2 % 10;
            // This will determine if the calibration is a factor of 10
 
            if (printVariable < 1)
                  fout << setw(5) << ((maxGraphPressure - i) * yScale) << " + ";
            //maxGraphPressure - lineNumber = pressure value on y axis
            else
                  fout << setw(5) << " " << " | ";
            for (int j = i*dataCount; j < (i+1)*dataCount; j++)
                  fout << graphingArray[j];
            fout << endl;
      } // This prints a blank or an asterisk for the first group of elements.
 
      fout << setw(5) << "0" << " + ";
      for (int i=0; i<dataCount; i++)
      {
            if ((i%3) <1)
                  fout << "+";
            else
                  fout << "-";
      }
    
      fout << " time (* 1/450 seconds)";
      fout << endl << setw(8) << " ";
      for (int i=0; i<dataCount; i++)
      {
            if ((i%3) <1)
            {
                  if ((i/30) >= 1)
                        fout << (i/30);
                  else
                        fout << " ";
            }
            else
                  fout << " ";
      }
 
      fout << endl << setw(8) << " ";
      for (int i=0; i<dataCount; i++)
      {
            if ((i%3) <1)
            {
                  if ((i/30) <= 1)
                        fout << ((i/3)%10);
                  else
                        if ((i/30) > 1)
                              fout << ((i/3)%10);
                        else
                              fout << " ";
            }
            else
                  fout << " ";
      }
 
      fout << endl << endl;
}
 
 

OUTPUT FILE LOOKS LIKE THIS:

Engine Analysis Report

Engine ID: Megatech Mark III Model TE1   Serial Number 155

Analysis Date: January 10, 2010

Engine Preformance: Speed   1800.0 RPM

 Compression Ratio    4.0:1

 Pressures
 Maximum    122.0 (psi)
 Minimum     12.0 (psi)
 Average     28.3 (psi)

 Power      0.0 (watts)


BAR PLOT OF CYLINDER PRESSURE -VS- TIME

pressure is on horizontal axis - units are psi
time is on vertical axis - units are msec

    0.0       40.0      80.0      120.0    
     +---------+---------+---------+
  0.0|************
  1.5|****************
  3.0|**********************
  4.4|*******************************
  5.9|*************************
  7.4|****************
  8.9|***********
 10.4|*********
 11.9|*******
 13.3|******
 14.8|******
 16.3|****
 17.8|***
 19.3|***
 20.7|***
 22.2|***
 23.7|****
 25.2|****
 26.7|****
 28.1|****
 29.6|****
 31.1|****
 32.6|****
 34.1|****
 35.6|****
 37.0|****
 38.5|****
 40.0|***
 41.5|***
 43.0|***
 44.4|***
 45.9|***
 47.4|***
 48.9|***
 50.4|****
 51.9|****
 53.3|****
 54.8|****
 56.3|*****
 57.8|******
 59.3|******
 60.7|********
 62.2|*********
 63.7|***********
 65.2|************
 66.7|************




TRANSPOSED BAR PLOT OF CYLINDER PRESSURE -VS- TIME

 pressure (psi)
         |    *                                         
120 +    *                                         
         |    *                                         
         |    *                                         
         |    *                                         
         |    *                                        
100 +    **                                        
         |    **                                        
         |    **                                        
         |   ***                                        
         |   ***                                        
  80 +   ***                                        
         |   ***                                        
         |   ***                                        
         |   ***                                        
         |  *****                                       
  60 +  *****                                       
         |  *****                                       
         |  *****                                       
         | ******                                      **
         | *******                                    ***
   40 + *******                                    ***
         | ********                                  ****
         | ********                                 *****
         | *********                                *****
         | ***********                            *******
    20 + ***********                           ********
         | ************    ***********       ************
         | **********************************************
         | **********************************************
         | **********************************************
        0 + +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ time (* 1/450 seconds)
                                                 1  1  1  1  1  1
         0  1  2  3  4  5  6  7  8  9  0  1  2  3  4  5