aga8x92dc - Instructions for use

Revision: Feb 26, 2010

 

E-mail: info@felib.com

 

The aga8x92dc is the C source code implementation of:

AGA Report No. 8, Compressibility Factor of Natural Gas and Related Hydrocarbon Gases (AGA,1992).

The aga8x92dc  function implements the AGA8_92DC equation in extended operating ranges (see Table 3.1). The code has been split into two parts (OFF-LINE and ON-LINE) and optimized to speed-up the calculations (see Section 4 for more details).

The software is written in a C-language and embeds all the restrictions implied by AGA-8/1992. The code is fully re-entrant and ANSI C compatible. The following is a brief description of a header, the corresponding input/output parameters and an example of a main program in a c-source code.

 

 

1. Header file: aga8x92dc.h

 

The following is the header of the function:

 

#include "stdio.h"

#include "math.h"

 

/* declaration of the 'aga8' struct */

struct aga8

{

        /* INPUT PARAMETERS */

        double t;      /* Temperature [K] */

        double p;      /* Pressure in [MPa] */

        double x[21];                 /* Natural gas molar fractions */

        // MOLE FRACTIONS OF:

        // x[20]       Argon

        // x[19]       Helium

        // x[18]       n-Decane

        // x[17]       n-Nonane

        // x[16]       n-Octane

        // x[15]       n-Heptane

        // x[14]       n-Hexane

        // x[13]       n-Pentane

        // x[12]       i-Pentane

        // x[11]       n-Butane

        // x[10]       i-Butane

        // x[9] Oxygen

        // x[8] Carbon Monoxide

        // x[7] Hydrogen

        // x[6] Hydrogen

        // x[5] Water

        // x[4] Propane

        // x[3] Ethane

        // x[2] Carbon Dioxide

        // x[1] Nitrogen

        // x[0] Methane

        /* OUTPUT PARAMETERS */

        unsigned long int es;  /* Error status */

        double scv;                   /* Superior calorific value on a volumetric basis [MJ/m^3] */

        /* at combustion temperature 298.15 K, */

        /* metering temperature 273.15K, and */

        /* combustion and metering pressure 0.101325 MPa */

        double rd;                            /* Relative density at 273.15 K and 0.101325 MPa */

        double zf;                            /* Compression (compressibility factor) */

        double d;                             /* Density [kg/m3] */

        double md;                            /* Molar density [kg-mole/m3] */

        double mm;                            /* Molar mass */

        //

        //      Temporary parameters

    double Gij, Eij, Bnijx, B, U5, U, G, Q, F, K5, dr, Z, Cnx[58];

    double d1, d2, dk, R, p1, p2, temp, z0, rden, sc;

    int i, j, n;

        /* Intermediate results in off-line calculations */

        double K3, M;

    unsigned long int es_off;

        double BB[18];

    double Cnx0[58];

        /* Temporary registers */

        double tmp[10];

        double RT, SumCnx;

    /* Initial gas composition (false) */

        double xx[21];

};

 

/* declaration of the function 'aga8x92dc' */

void aga8x92dc(struct aga8 *);

 

 

2. Input and output parameters

 

The following Table shows the description of input and output parameters:

 

Table 2.1

Input parameters

Data type

Parameter

Parameter description

Physical unit

double

aga8.x[0]

molar fraction of Methane

dimensionless

double

aga8.x[1]

molar fraction of Nitrogen

"

double

aga8.x[2]

molar fraction of Carbon dioxide

"

double

aga8.x[3]

molar fraction of Ethane

"

double

aga8.x[4]

molar fraction of Propane

"

double

aga8.x[5]

molar fraction of Water

"

double

aga8.x[6]

molar fraction of Hydrogen sulfide

"

double

aga8.x[7]

molar fraction of Hydrogen

"

double

aga8.x[8]

molar fraction of Carbon monoxide

"

double

aga8.x[9]

molar fraction of Oxygen

"

double

aga8.x[10]

molar fraction of iso-Butane

"

double

aga8.x[11]

molar fraction of n-Butane

"

double

aga8.x[12]

molar fraction of i-Pentane

"

double

aga8.x[13]

molar fraction of n-Pentane

"

double

aga8.x[14]

molar fraction of n-Hexane

"

double

aga8.x[15]

molar fraction of n-Heptane

"

double

aga8.x[16]

molar fraction of n-Octane

"

double

aga8.x[17]

molar fraction of n-Nonane

"

double

aga8.x[18]

molar fraction of n-Decane

"

double

aga8.x[19]

molar fraction of Helium

"

double

aga8.x[20]

molar fraction of Argon

"

double

aga8.p

operating pressure

[MPa]

double

aga8.t

operating temperature

[K]

Output parameters

double

aga8.mm

gas mixture molar mass

dimensionless

double

aga8.md

gas mixture molar density

[kg×mol/m3]

double

aga8.density

gas mixture density

[kg/m3]

double

aga8.zf

gas mixture compression factor

dimensionless

double

aga8.rd

gas mixture relative density

"

double

aga8.scv

gas mixture superior calorific value

[MJ/m3]

unsigned long int

aga8.es

error status

dimensionless


3. Parameter ranges

 

AGA-8 wider operating ranges are implemented into the aga8x92dc routine. Table 3.1 shows the wider ranges of application for compression factor calculation using AGA-8/1992 equation of state.

 

Table 3.1

 

Ranges of application for compression factor calculation using AGA-8/1992 equation of state

Parameter

Extended range

Absolute pressure [MPa]

 10-8 to 275.8 MPa

Temperature [K]

140 to 678  K

Relative density

0.07 to 1.52

Superior calorific value [MJ/m3]

0 to 67.066

Mole percent of:

 

Methane [%]

0 to 100

Nitrogen [%] 

0 to 100

Carbon dioxide [%]

0 to 100

Ethane [%]

0 to 100

Propane [%]

0 to  12

Water [%]

0 to  10

Hydrogen Sulfide [%]

0 to 100

Hydrogen [%]

0 to 100

Carbon Monoxide [%]

0 to   3

Oxigen [%]

0 to   21

i-Butane + n-Butane [%]

0 to   6

i-Pentane + n-Pentane [%]

0 to   4

n‑Hexane + n‑Heptane + n‑Octane + n‑Nonane + n‑Octane [%]

0 to  10

Helium [%]

0 to   3

Argon [%]

0 to   1

 

 

 


 

4. Error status

 

After completion of the routine the error status - aga8.es (unsigned long integer) will be cleared if there were no errors during the calculation, or the corresponding bits will be set if certain errors were encountered. The description of the error status (aga8.es) bits is given in Table 4.1.

 

Table 4.1

 

Bit number

Hexadecimal address

Description

Bit 0

=1 (000001H)

If there exist at least one molar fraction < 0 or >1, else =0.

Bit 1

=1 (000002H)

If the total sum of molar fractions <0.98 or >1.02, else =0.

Bit 2

=1 (000004H)

If the total sum of molar fractions is <0.999999 or >1.000001, else =0. This bit denotes only a warning that the fractions have been normalized to give the total sum of molar fractions equal to 1. If only bit 2 was set during the calculation the results are valid.

Bit 3

=1 (000008H)

If mole fraction of Methane >1.0, else =0.

Bit 4

=1 (000010H)

If mole fraction of Nitrogen >1.0, else =0.

Bit 5

=1 (000020H)

If mole fraction of Carbon Dioxide >1.0, else =0.

Bit 6

=1 (000040H)

If mole fraction of Ethane >1.0, else =0.

Bit 7

=1 (000080H)

If mole fraction of Propane >0.12, else =0.

Bit 8

=1 (000100H)

If mole fraction of Water >0.10, else =0.

Bit 9

=1 (000200H)

If mole fraction of Hydrogen sulfide > 1.0, else =0.

Bit 10

=1 (000400H)

If mole fraction of Hydrogen >1.0, else =0.

Bit 11

=1 (000800H)

If mole fraction of Carbon monoxide > 0.03, else =0.

Bit 12

=1 (001000H)

If mole fraction of Oxygen >0.21, else =0.

Bit 13

=1 (002000H)

If mole fraction of Butanes (n-Butane + i-Butane) >0.06, else =0.

Bit 14

=1 (004000H)

If mole fraction of Pentanes (n-pentane + i-Pentane) >0.04, else =0.

Bit 15

=1 (008000H)

If mole fraction of Hexanes, Heptanes, Octanes plus higher Hydrocarbons (n-Hexane, n-Heptane, n-Octane + n-Nonane, n-Decane) >0.10, else =0.

Bit 16

=1 (010000H)

If mole fraction of Helium >0.03, else =0.

Bit 17

=1 (020000H)

If mole fraction of Argon >0.01, else =0.

Bit 18

=1 (040000H)

If pressure <0.01 Pa or >275.8 MPa, else =0.

Bit 19

=1 (080000H)

If temperature <140 K or >678 K, else =0.

Bit 20

=1 (100000H)

If relative density <0.07 or >1.52, else =0.

Bit 21

=1 (200000H)

If superior calorific value <0 MJ/m3 or >67.066 MJ/m3, else =0.

Bit 22

=x (400000H)

Unused

Bit 23

=x (800000H)

Unused

Bit 24

=1 (1000000H)

If OFF-LINE calculation failed, else=0.

Bit 25,...

=x (2000000H)

All higher bits are unused

 

 

The error status (aga8.es) represents all the errors encountered in the calculation. If bits 0,...,24 are all cleared, or if only bit 2 was set during the calculation, the returned output results are valid, otherwise the results are invalid.

To speed-up the calculations the aga8x92dc routine has been split into two parts: OFF-LINE and ON-LINE calculation. The OFF-LINE part contains the calculations independent on pressure and temperature and dependent on the gas composition only. At the beginning the procedure always compares the previous and the current gas composition supplied. If the gas composition changes the function saves the new composition and attempts to complete both OFF-LINE and ON-LINE calculations. If there were no changes in the gas composition the function automatically switches to ON-LINE mode and performs the ON-LINE calculation only. The function switches to ON-LINE mode only if the bit 0x1000000 of the error status (aga8.es) is cleared i.e. when the OFF-LINE calculation ended successfully. Note that OFF-LINE calculation may increase the total execution time significantly. The gas composition must satisfy the wider AGA8/1992 gas composition specification (See Table 3.1). The error status (aga8.es) is the indication of the accomplishment of the calculation. If the bit 0x1000000 of the error status (aga8.es) is set to 1, the OFF_LINE calculation did not end successfully. If the bit is cleared the calculation ended successfully. Always test the error status (aga8.es) to check whether the calculation ended successfully or not. Note that OFF_LINE calculation will not end successfully only if the input data are outside the specification.

 

 

5. Example of a main program calling the function aga8x92dc

 

The following example illustrates the call to aga8x92dc routine from a main program. The program defines a struct for AGA8/1992 input/output data. The program prompts for the selection of the natural gas mixture (Gas1 to Gas 6) given in Anex C of ISO-12213-2:2003(E) or for the manual setup of input parameters (see Table 6.1 below). The program then transfers the input parameters to the corresponding struct. After the execution the program prints both the input parameters and the results on the screen. The calculated results can be compared easily with the examples given in in Annex C of ISO 12213-2.

 

/* **************************************** */

/* THE BEGINNING OF THE MAIN PROGRAM C-CODE */

/* **************************************** */

 

#include "stdio.h"

#include "conio.h"

#include "aga8x92dc.h"

 

void main(void)

{

         struct aga8 aga, *agap; /* 'agap' is a pointer to a structure 'aga' */

         unsigned short int i, j, igas;

         char ch;

        

         /* ISO-12213-2 examples */

         /* Array of gas mixture molar fractions */

         //       double twodim[2][3] = { 11, 12, 13, 21, 22, 23 };

    double yy[6][21] = {

                

                 0.965, 0.003, 0.006, 0.018, 0.0045, 0.0, 0.0,

                          0.0, 0.0, 0.0, 0.001, 0.001, 0.0005, 0.0003,

                          0.0007, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,

                         

                          0.907, 0.031, 0.005, 0.045, 0.0084, 0.0, 0.0,

                          0.0, 0.0, 0.0, 0.001, 0.0015, 0.0003, 0.0004,

                          0.0004, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,

                         

                          0.859, 0.01, 0.015, 0.085, 0.023, 0.0, 0.0,

                          0.0, 0.0, 0.0, 0.0035, 0.0035, 0.0005, 0.0005,

                          0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,

                         

                          0.735, 0.1, 0.016, 0.033, 0.0074, 0.0, 0.0,

                          0.095, 0.01, 0.0, 0.0012, 0.0012, 0.0004, 0.0004,

                          0.0002, 0.0001, 0.0001, 0.0, 0.0, 0.0, 0.0,

                         

                          0.812, 0.057, 0.076, 0.043, 0.009, 0.0, 0.0,

                          0.0, 0.0, 0.0, 0.0015, 0.0015, 0.0, 0.0,

                          0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,

                         

                          0.826, 0.117, 0.011, 0.035, 0.0075, 0.0, 0.0,

                          0.0, 0.0, 0.0, 0.0012, 0.0012, 0.0004, 0.0004,

                          0.0002, 0.0001, 0.0, 0.0, 0.0, 0.0, 0.0

         };

        

         char *text[29] = {

                 "Methane         ", "Nitrogen        ", "Carbon Dioxide  ",

                          "Ethane          ", "Propane         ", "Water           ",

                          "Hydrogen Sulfide", "Hydrogen        ", "Carbon Monoxide ",

                          "Oxygen          ", "iso-Butane      ", "n-Butane        ",

                          "i-Pentane       ", "n-Pentane       ", "n-Hexane        ",

                          "n-Heptane       ", "n-Octane        ", "n-Nonane        ",

                          "n-Decane        ", "Helium          ", "Argon           ",

                          "Pressure [MPa]  ", "Temperature [K] ",

                          "Molar mass                ",

                          "Molar density [kg-mole/m3]",

                          "Density [kg/m3]           ",

                          "Compression               ",

                          "Relative density          ",

                          "Superior calorific value  "

         };

         for (j=0; ; ) {

                 printf("\nINPUT PARAMETERS\n");

                 printf("%s", "Select input parameter setup '0' or ISO12213-2 example '1-6'.");

                 printf("%s", "\nType number between 0 and 6: ");

                 scanf("%d", &igas);

                 if (igas > 0 && igas <= 6) {

                          igas -= 1;

                          /* Copy molar fractions */

                          for (i=0; i<=20; i++) {

                                  aga.x[i] = yy[igas][i];

                          }

                 }

                 else {

                          /* Input molar fractions */

                          printf("Mole fraction of:\n");

                          for (i=0; i<=20; i++) {

                                  printf("%s = ", text[i]);

                                  scanf("%lf", &(aga.x[i]));

                          }

                 }

                 printf("%s = ", text[21]);        /* Enter pressure */

                 scanf("%lf", &(aga.p));

                 printf("%s = ", text[22]);        /* Enter temperature */

                 scanf("%lf", &(aga.t));

                

                 agap = &aga; /* Put the address of 'aga' structure into 'agap' */

                 aga8x92dc(agap); /* Call function by passing the pointer to structure */

                

                 /* Print input parameters and results */

                 printf("\nI N P U T   P A R A M E T E R S:");

                 printf("\n%s%d", "Selected gas = Gas", igas+1); /* Print gas selection */

                 printf("\nMole fractions:"); /* Print mole fractions */

                 for (i=0; i<10; ++i) { /* Fractions of components 1-20 */

                          printf("\n%s = %f, %s = %f",text[2*i], aga.x[2*i], text[2*i+1], aga.x[2*i+1]);

                 }

                 printf("\n%s = %f",text[20], aga.x[20]); /* Fraction of component 21 */

                 printf("\n%s = %f", text[21], aga.p); /* Print pressure */

                 printf("\n%s = %f", text[22], aga.t); /* Print temperature */

                

                 printf("\n\nR E S U L T S");

                 printf("\nError(Hex) = %x", aga.es); /* errr status */

                 /* Print results */

                 printf("\n%s = %f",text[23], aga.mm); /* print molar mass */

                 printf("\n%s = %f",text[24], aga.md); /* print molar density */

                 printf("\n%s = %f",text[25], aga.d); /* print density */

                 printf("\n%s = %f",text[26], aga.zf); /* compression */

                 printf("\n%s = %f",text[27], aga.rd); /* relative density */

                 printf("\n%s = %f",text[28], aga.scv); /* superior calorific value */

                

                 printf("\nPress X to exit or anything else to continue: ");

                 ch = getch();

                 if (( ch == 'X') || (ch == 'x'))

                          break;

         }

}

 

/* ********************************** */

/* THE END OF THE MAIN PROGRAM C-CODE */

/* ********************************** */

 
6. Examples for the verification of the program

 

The following table contains the example calculations that are retyped from Annex C of ISO-12213-2. Refer to Annex C in order to avoid possible typing errors.

 

Table 6.1

 

 

Molar fractions of the gas mixture components

Component

Gas 1

Gas 2

Gas 3

Gas 4

Gas 5

Gas 6

Methane

0.965

0.907

0.859

0.735

0.812

0.826

Nitrogen

0.003

0.031

0.01

0.1

0.057

0.117

Carbon dioxide

0.006

0.005

0.015

0.016

0.076

0.011

Ethane

0.018

0.045

0.085

0.033

0.043

0.035

Propane

0.0045

0.0084

0.023

0.0074

0.009

0.0075

Water

0.0

0.0

0.0

0.0

0.0

0.0

Hydrogen sulfide

0.0

0.0

0.0

0.0

0.0

0.0

Hydrogen

0.0

0.0

0.0

0.095

0.0

0.0

Carbon monoxide

0.0

0.0

0.0

0.01

0.0

0.0

Oxygen

0.0

0.0

0.0

0.0

0.0

0.0

iso-Butane

0.001

0.001

0.0035

0.0012

0.0015

0.0012

n-Butane

0.001

0.0015

0.0035

0.0012

0.0015

0.0012

i-Pentane

0.0005

0.0003

0.0005

0.0004

0.0

0.0004

n-Pentane

0.0003

0.0004

0.0005

0.0004

0.0

0.0004

n-Hexane

0.0007

0.0004

0.0

0.0002

0.0

0.0002

n-Heptane

0.0

0.0

0.0

0.0001

0.0

0.0001

n-Octane

0.0

0.0

0.0

0.0001

0.0

0.0

n-Nonane

0.0

0.0

0.0

0.0

0.0

0.0

n-Decane

0.0

0.0

0.0

0.0

0.0

0.0

Helium

0.0

0.0

0.0

0.0

0.0

0.0

Argon

0.0

0.0

0.0

0.0

0.0

0.0

Conditions

 

Results Z-values

p

 [MPa]

T

[K]

6

270

0.84053

0.83348

0.79380

0.88550

0.82609

0.85380

6

280

0.86199

0.85596

0.82206

0.90144

0.84969

0.87370

6

290

0.88006

0.87484

0.84544

0.91501

0.86944

0.89052

6

310

0.90867

0.90466

0.88183

0.93674

0.90052

0.91723

6

330

0.93011

0.92696

0.90868

0.95318

0.92368

0.93730

12

270

0.72133

0.71044

0.64145

0.81024

0.69540

0.75074

12

280

0.76025

0.75066

0.68971

0.83782

0.73780

0.78586

12

290

0.79317

0.78475

0.73123

0.86137

0.77369

0.81569

12

310

0.84515

0.83863

0.79698

0.89913

0.83022

0.86311

12

330

0.88383

0.87870

0.84553

0.92766

0.87211

0.89862