By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,820 Members | 1,189 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,820 IT Pros & Developers. It's quick & easy.

Gas Z-factor code problems

chimmy48
P: 5
Hey there people, i'm having some serious problems in my assignment. (C++ programming)
I have to calculate the gas z-factor using the Hall-Yarborough Method, and I can't seem to know how to start. It would be deeply appreciated, if someone could help. Thanks!!
Mar 21 '07 #1
Share this Question
Share on Google+
10 Replies


RedSon
Expert 5K+
P: 5,000
Here this gives you some information about the math involved.

http://www.peteng.com/jmm/zfc02.html
Mar 21 '07 #2

chimmy48
P: 5
Here this gives you some information about the math involved.

http://www.peteng.com/jmm/zfc02.html

Thanks for the info but, know about the formula involved. It's just that i don't know how to write the code. Please help me!!
Mar 23 '07 #3

RedSon
Expert 5K+
P: 5,000
Why dont you start by writing down the steps involved in the formula. What are the inputs and outputs at each stage? What do you have to do to the numbers at each stage of the process to come up with the right answer.
Mar 23 '07 #4

chimmy48
P: 5
Why dont you start by writing down the steps involved in the formula. What are the inputs and outputs at each stage? What do you have to do to the numbers at each stage of the process to come up with the right answer.
Okay. Well, i know that in order to find Z, i will first have to find Ppr, t and y. In order to do this, i propose we use functions. But according to the y formula (from the website given above) we have to apply the Newton-Raphson method. Now to me, this is very difficult to do. I know how to write a typical code for the N-R method but this one is crazy. Maybe, i'm overseeing things to much.

To calculate t, i take as input the pseudo-reduced temperature (since t is the reciprocal of this temperature) and return t. Actually, it is also calculated by Tpc/T. Therefore, INPUT: T and Tpc. Return t. But, i believe in this case we have to call the Tpc function, because we have not yet calculated Tpc.

To calculate Ppr, it is the pressure (P) the user inputs divided by the
pseudo-critical pressure (Ppc). Return ppr

You see, i have to calculate Ppc (pseudo-critical pressure) by summing the product of the critical pressure (Pc) of each component and their mole fraction.

This is the same for calculating Tpc (pseudo-critical temperature) which is the sum of the product of the critical temp (Tc) of each component and their mole fraction. So i guess, we ask the user for these critical values and their respective mole fraction. This is not too hard. But still, could u see what u could do?

The major problem lies within the Newton-Raphson Method to find y from the given formula.

Does this help?
Mar 24 '07 #5

Roonie
P: 99
i dont know, does it?

i think the point is to use some algebra to break down the equations into more manageably codable chunks . . . dont try to code the whole solution at once, code small sections of it first, one term at a time, and then cut and paste them all together.

you wont be able to read what you have in the end, but the compiler will. you will know youve got it right, because you are using basic algebraic operations to get the final product. (specifically - substitution) you will know youve got it right also after running some test data.
Mar 24 '07 #6

chimmy48
P: 5
Well, i started to work on it piece by piece rather than the whole thing one time. So, thanks anyway for your help RedSon and Roonie. I'll see what i can do. Any problems that I encounter i'll post it. Again, thanks!!!!
Mar 25 '07 #7

Roonie
P: 99
or, i guess thats a bad way to leave things for the next programmer . . . probably youd want to do the substitutions with functions and give them useful names. ideas the same though: let the algebra shape your solution.

come again whenever.
Mar 25 '07 #8

chimmy48
P: 5
I've made some progress, but i'm not too sure the functions are accurate. Please double check it.The only thing I have to do now is to calulate y via the Newton-Raphson method. If it's possible, cud u help me with it. Here' s the CODE: Please respond soon, thank you.
Expand|Select|Wrap|Line Numbers
  1. #include <cstdlib>
  2. #include <iostream>
  3. #include <cmath>
  4. //List prototypes
  5.  
  6. double calculate Pr (double P);
  7. double calculate t (double T);
  8. double calculate y (double Pr, double T);
  9. double calculate Ppc (int n, double Pc);
  10. double calculate Tpc (int n, double Tc);
  11. double calculate Tpc-corr (double Tpc, double E);
  12. double calculate Ppc-corr (double Tpc-corr, double Ppc,double Tpc, double E);
  13. double calculate Ppr (double Ppc-corr, double P);
  14.  
  15. using namespace std;
  16. int main (){
  17.     double Z, t, y, Ppr;
  18.  
  19.        y = calculate y (Pr, T);
  20.        Ppr =  calculate Ppr(Ppc-corr,P);
  21.        t = calculate t (T); 
  22.  
  23.        Z = 0.06125 * Ppr * t * exp (-1.2(pow(1-t),2))/ y;
  24.  
  25.  
  26.            cout<<"The Gas Z-Factor is: "<<Z<<endl;
  27.  
  28.  
  29.  
  30.  
  31.     system("PAUSE");
  32.     return EXIT_SUCCESS;
  33. }
  34. double calculate Ppc (int n, double Pc){
  35.        double sum;
  36.        int count;
  37.  
  38.        count = 0;
  39.        sum = 0;
  40.  
  41.  
  42.       do {
  43.               cout<<"Enter the mole fraction of the component:";
  44.               cin>>n;
  45.               cout<<"Enter the critical pressure for that component: ";
  46.               cin>>Pc;
  47.  
  48.              sum = n* Pc;
  49.  
  50.              count++;
  51.              sum = sum + count;
  52.  
  53.       } while (n!=0);
  54.  
  55.       return sum;
  56.  
  57. double calculate Tpc (int n, double Tc){
  58.        double sum;
  59.        int count;
  60.  
  61.        count = 0;
  62.        sum = 0;
  63.  
  64.  
  65.       do {
  66.               cout<<"Enter the mole fraction of the component:";
  67.               cin>>n;
  68.               cout<<"Enter the critical temperature for that component: ";
  69.               cin>>Tc;
  70.  
  71.               sum = n* Tc;
  72.  
  73.              count++;
  74.              sum = sum + count;
  75.  
  76.       } while (n!=0);
  77.  
  78.       return sum;
  79.  
  80. double calculate Pr (double P){
  81.             double Ppc; 
  82.        cout<<"Enter P: ";
  83.        cin>>P;
  84.        Ppc = calculate Ppc (n, Pc);
  85.        Pr = P/ Ppc;
  86.  
  87.        return Pr;
  88.  
  89. double calculate t (double T){
  90.                  double Tpc;
  91.        cout<<"Enter T: ";
  92.        cin>>T;
  93.        Tpc = calculate Tpc (n, Tc);
  94.        t = Tpc/T;
  95.  
  96.        return t;
  97.  
  98. double calculate Tpc-corr (double Tpc, double E);
  99.                  double n1, n2, Tpc-corr;
  100.                  cout<<"Enter mole fractions for carbon dioxide and 
  101.                      << "hydrogen sulphide respectively: ";
  102.                  cin>>n1>>n2;
  103.  
  104.                  E = 66.667* ((pow(n1+n2),0.9) - (pow(n1+n2),1.6)) + 
  105.                      8.333((pow(n2),0.5)- pow(n2),4));
  106.                  Tpc = calculate Tpc (n, Tc);
  107.  
  108.                  Tpc-corr = Tpc - E;
  109.  
  110.                  return Tpc-corr;
  111.  
  112. double calculate Ppc-corr (double Tpc-corr, double Ppc,double Tpc, double E){
  113.                           double n2;
  114.                           double Ppc-corr;
  115.        Tpc-corr = calculate Tpc-corr (Tpc, E);
  116.  
  117.        Ppc =  calculate Ppc (n, Pc);
  118.  
  119.        Ppc-corr = (Ppc*Tpc-corr)/(Tpc+n2*(1-n2)*E);
  120.  
  121.  
  122.               return Ppc-corr;
  123.  
  124. double calculate Ppr (double Ppc-corr, double P){
  125.                  double Ppr;
  126.          Ppc-corr = calculate Ppc-corr (Tpc-corr, Ppc, Tpc, E);
  127.  
  128.          Ppr = P/Ppc-corr;
  129.  
  130.          return Ppr;
  131.  
Mar 27 '07 #9

RedSon
Expert 5K+
P: 5,000
I've made some progress, but i'm not too sure the functions are accurate. Please double check it.The only thing I have to do now is to calulate y via the Newton-Raphson method. If it's possible, cud u help me with it.
I'm not a petroleum engineer so I have no idea if the functions and hence the arithmetic in your application is correct. Does your program calculate the correct answer the same as you would manually? That is one way to check that the functions are right.

Your code looks fine, does it compile? I expect that when you copied and pasted your code some under scores went missing but that should be no problem.
Mar 27 '07 #10

RedSon
Expert 5K+
P: 5,000
I also recommend changing the names of your variables away from the names used in the formula to names that are more readable.
Mar 27 '07 #11

Post your reply

Sign in to post your reply or Sign up for a free account.