Hi,

I am trying to write an engine simulation program in C++ - please not that I have never done any programming.

I have attached what I have done so far which is a program that calculates the cylinder volume from a crank angle (theta). What I need now to do is use those iterations to find the pressure at each crank angle - the function is in the form of:
P(theta + dtheta) = P(theta) * (V(theta)/V(theta + dtheta))
ie the current pressure is found using the pressure from the previous iteration, the current volume and the previous volume.

How do I write program to use both the current and previous values for volume? Im sure it can be done, I just can't figure out how to do it.

Any help would be appreciated!

Attachments
#include <iostream.h>
#include <math.h>

//***** All functions are declared here:
double calVolume(double, double);

//***** All constants are defined here ******

double	RADIAN = 3.141592654/180;
double CR = 7.8; 		     //	CR is the compression ratio
double Vc = 0.02;            // Vc is the clearance volume in m^2
double Gamma = 1.4;          // Gamma is Cv/Cp

double Test;

main()
{
	double l,a;
	l=0.4; a=0.1;            //  l is the connecting rod length in m, a is the crank radius in m
	double V,R,Theta,P,V2;     
	R = l/a;       
    Theta = 0 * RADIAN;     // T is the crank angle in radians
    P = 1;                  //P is the pressure in bar - initally atmospheric
		while (Theta < 721*RADIAN)
	{	// First bracket
		V = calVolume(Theta, R);
		
// ******************************Help here!
		P = P*(V); // NEED HERE: P(Theta + d Theta) = P(Theta)*(V(Theta)/V(Theta + d Teta)) How do I use the previous iteration with the current one?
		
		cout << "Theta = " << Theta/RADIAN << " ; V = " << V <<  " ; P = " << P <<endl;
		Theta = Theta + 10 * RADIAN;
	}	// First bracket finished
	cout << "I am out" <<endl;
	cin >> Test;
	return 0;
}

double calVolume(double Theta, double R)
{
    double V;
    V = Vc*(1+(0.5*(CR-1)*(R+1-cos(Theta)-sqrt((pow(R,2)-pow(sin(Theta),2))))));
    return V;
}

It's 3 am here so I can't offer much assistance for the simple fact that I am having a hard time keeping my eyes open. But here is some food for thought until you get better replies: [thread]1734[/thread]

@cscgal
go to bed earlier!


You say you have never done any programming. This is not really that easy of a thing to do. Start out simple!

Yeah tell me about it!

Unfortunately I don't have any choice. I'm doing my final year project on engine simulations and I was under the naive impression that I would have a program to use! So now I have to write my own. :lol:

@cscgal
go to bed earlier!

Yea you should.Leave lights on at night if you are using the comp

...
How do I write program to use both the current and previous values for volume? Im sure it can be done, I just can't figure out how to do it.
...

If you only need one previous value, there is a simple answer to your question: Use two volume variables.

It looks like you already have two, i.e. V and V2, but you're only using one of them. I'd rename V2 something more descriptive, like OldV or something, and make it so V has a value before you start the loop. The only addition to the loop would be to add a line at the start that says 'OldV = V;'--then you can do whatever you want with V in the loop body, since you've already stored the previous value. Once you've calculated a new value for V, you can use both the current and previous values as much as you want. OldV will only ever hold the most recent value of V.

Hope that helps.
--sg

This article has been dead for over six months. Start a new discussion instead.