FE-AGA8-2017-1-C


FE-AGA8-2017-1-C (Revised: February 07, 2021) is the C source code implementation of AGA-8 DETAIL Equation of State in accordance with:

  • AGA Report No. 8, Part 1: Thermodynamic Properties of Natural Gas and Related Gases
    DETAIL and GROSS Equations of State

    Third Edition, April 2017 (A revision of AGA Report No. 8, 2nd edition, 1994)
FE-AGA8-2017-1-C performs the calculation of thermodynamic and volumetric properties of natural gas. FE-AGA8-2017-1-C implements AGA-8 DETAIL Equation of State. In order to speed-up the calculations, the aga8 routine (aga8part1.c) has been split into two parts: OFF-LINE and ON-LINE calculation. The ON-LINE part performs calculations dependent on gas composition, pressure and temperature. The OFF-LINE part performs calculations that depend only on the gas composition and can be performed separately. At the beginning of each calculation cycle, the current gas composition is compared with the gas composition from the preceeding cycle. If the gas composition is the same as in the previous calculation cycle, only the ON-LINE part of the calculation is performed, otherwise both the OFF-LINE and ON-LINE part of the calculation are performed. Note that unnecessary OFF-LINE calculations may increase the total execution time considerably. This is especially important when considering real-time applications such as embedded systems. The gas composition, absolute pressure and temperature must satisfy the expanded range of application (see Table below).
The software is written in C-language and embeds all the restrictions implied by AGA Report No. 8, Part 1. The code is fully re-entrant and ANSI C compatible. The following is a brief description of a function (method), input/output parameters, ranges of applications, errors and an example of a main program in a c-source code.

Description of function, input/output parameters, ranges of application and errors

Function
long aga8part1(struct aga8p1 *);
Struct aga8p1{}
Input parameters
Data typeParameter name Parameter descriptionUnit of measure
doubleaga8p1.x[0] Mole fraction of Methanemol
doubleaga8p1.x[1] Mole fraction of Nitrogenmol
doubleaga8p1.x[2] Mole fraction of Carbon dioxidemol
doubleaga8p1.x[3] Mole fraction of Ethanemol
doubleaga8p1.x[4] Mole fraction of Propanemol
doubleaga8p1.x[5] Mole fraction of i-Butanemol
doubleaga8p1.x[6] Mole fraction of n-Butanemol
doubleaga8p1.x[7] Mole fraction of i-Pentanemol
doubleaga8p1.x[8] Mole fraction of n-Pentanemol
doubleaga8p1.x[9] Mole fraction of n-Hexanemol
doubleaga8p1.x[10] Mole fraction of n-Heptanemol
doubleaga8p1.x[11] Mole fraction of n-Octanemol
doubleaga8p1.x[12] Mole fraction of n-Nonanemol
doubleaga8p1.x[13] Mole fraction of n-Decanemol
doubleaga8p1.x[14] Mole fraction of Hydrogenmol
doubleaga8p1.x[15] Mole fraction of Oxygenmol
doubleaga8p1.x[16] Mole fraction of Carbon monoxidemol
doubleaga8p1.x[17] Mole fraction of Watermol
doubleaga8p1.x[18] Mole fraction of Hydrogen sulfidemol
doubleaga8p1.x[19] Mole fraction of Heliummol
doubleaga8p1.x[20] Mole fraction of Argonmol
doubleaga8p1.p Absolute pressureMPa
doubleaga8p1.T TemperatureK
Output results
doubleaga8p1.res[23] Molar massg/mol
doubleaga8p1.res[24] Molar densitymol/dm3
doubleaga8p1.res[25] Densitykg/m3
doubleaga8p1.res[26] Compressibility factor-
doubleaga8p1.res[27] Relative density-
doubleaga8p1.res[28] Reduced density-
doubleaga8p1.res[29] Gross heating valueMJ/m3
doubleaga8p1.res[30] Molar Helmholtz energyJ/mol
doubleaga8p1.res[31] Molar ideal gas Helmholtz energyJ/mol
doubleaga8p1.res[32] Molar residual Helmholtz energyJ/mol
doubleaga8p1.res[33] Molar entropyJ/(mol⋅K)
doubleaga8p1.res[34] Molar energyJ/mol
doubleaga8p1.res[35] Molar enthalpyJ/mol
doubleaga8p1.res[36] Molar Gibbs energyJ/mol
doubleaga8p1.res[37] Molar isochoric heat capacityJ/(mol⋅K)
doubleaga8p1.res[38] Molar isobaric heat capacityJ/(mol⋅K)
doubleaga8p1.res[39] Speed of soundm/s
doubleaga8p1.res[40] Joule-Thomson coefficientK/MPa
doubleaga8p1.res[41] Isentropic exponent-
doubleaga8p1.res[42] Specific entropykJ/(kg⋅K)
doubleaga8p1.res[43] Specific energykJ/kg
doubleaga8p1.res[44] Specific enthalpykJ/kg
doubleaga8p1.res[45] Specific isochoric heat capacitykJ/(kg⋅K)
doubleaga8p1.res[46] Specific isobaric heat capacitykJ/(kg⋅K)
longaga8p1.es Error status-
Ranges of application
Parameter Expanded range Range A Range B Range C
Temperature in K 143.15 to 453.15 269.15 to 453.15 269.15 to 453.15 265.15 to 453.15
Pressure (absolute) in MPa 10-9 to 280 10-9 to 10.3 10-9 to 2.1 10-9 to 21
Gross heating value in MJ/m3 0 to 66 23.5 to 44.7 25.3 to 56 35.8 to 40.6
Relative density 0.07 to 1.52 0.554 to 0.91 0.47 to 0.91 0.554 to 0.64
Mole fractions ranges
Methane 0 to 1.0 0.6 to 1.0 0.6 to 1.0 0.6 to 1.0
Nitrogen 0 to 1.0 0 to 0.5 0 to 0.5 0 to 0.03
Carbon dioxide 0 to 1.0 0 to 0.3 0 to 0.8 0 to 0.03
Ethane 0 to 1.0 0 to 0.1 0 to 0.25 0 to 0.04
Propane 0 to 0.12 0 to 0.04 0 to 0.06 0 to 0.02
i-Butane - 0 to 0.004 0 to 0.015 0 to 0.001
n-Butane - 0 to 0.006 0 to 0.06 0 to 0.004
Total butanes 0 to 0.08 - - -
i-Pentane - 0 to 0.003 0 to 0.02 0 to 0.001
n-Pentane - 0 to 0.003 0 to 0.02 0 to 0.001
Total pentanes 0 to 0.05 0 to 0.003 0 to 0.02 -
n-Hexane - 0 to 0.0012 0 to 0.002 0 to 0.0003
n-Heptane - 0 to 0.0004 0 to 0.002 0 to 0.0001
n-Octane - 0 to 0.0003 0 to 0.002 0 to 0.00003
n-Nonane - 0 to 0.0003 0 to 0.002 0 to 0.00003
n-Decane - 0 to 0.0003 0 to 0.002 0 to 0.00003
Total hexanes plus 0 to 0.05 0 to 0.0015 - -
Total heptanes plus - 0 to 0.0004 - -
Hydrogen 0 to 1.0 0 to 0.05 0 to 1.0 0 to 0.01
Oxygen 0 to 0.21 0 to 0.002 0 to 0.01 0 to 0.002
Carbon monoxide 0 to 0.12 0 to 0.01 0 to 0.1 0 to 0.01
Water 0 to 0.02 0 to 0.0005 0 to 0.014 0 to 0.00005
Hydrogen sulfide 0 to 1.0 0 to 0.001 0 to 0.04 0 to 0.001
Helium 0 to 0.06 0 to 0.004 0 to 0.05 0 to 0.004
Argon 0 to 0.04 0 to 0.002 0 to 0.03 0 to 0.002
In each calculation all input parameters and intermediate results are first checked to determine whether their values are within the limits defined by the expanded range of application or not.
  • If the parameters satisfy the expanded range they are further checked to see if they comply with at least one of the ranges A, B or C (See Table 1 in AGA Report No. 8, Part 1) and if the result is positive again then the calculation ends with no errors and no warnings and the results of the calculation are valid with an uncertainty in density generally less than 0.1 %.
  • If the parameters satisfy the expanded range and do not comply with any of the ranges A, B or C (See Table 1 in AGA Report No. 8, Part 1), the results of the calculation are still valid but the warning bit is set in error status to warn the user that an uncertainty in density is not guaranteed to be within 0.1 %.
  • If any of the parameters do not satisfy the expanded range, the calculation terminates with no results and with the corresponding error status flags (bits) set.
ERROR STATUS
long aga8p1.es Description of Error/Warning flags
Bit 1 =1 (00001H) Error01: There exists at least one mole fraction <0 or >1
Bit 2 =1 (00002H) Error02: Total sum of mole fractions <xSumMin (0.999999) or >xSumMax (1.000001)
Bit 3 =1 (00004H) Error03: Mole fraction of Propane >xPropaneMax (0.12)
Bit 4 =1 (00008H) Error04: Mole fraction of butanes >xButanesMax (0.08)
Bit 5 =1 (00010H) Error05: Mole fraction of pentanes >xPentanesMax (0.05)
Bit 6 =1 (00020H) Error06: Mole fraction of hexanes plus>xHexanesPlusMax (0.05)
Bit 7 =1 (00040H) Error07: Mole fraction of Oxygen >xOxygenMax (0.21)
Bit 8 =1 (00080H) Error08: Mole fraction of Carbon monoxide >xCarbonMonoxideMax (0.12)
Bit 9 =1 (00100H) Error09: Mole fraction of Water >xWaterMax (0.02)
Bit 10 =1 (00200H) Error10: Mole fraction of Helium >xHeliumMax (0.06)
Bit 11 =1 (00400H) Error11: Mole fraction of Argon >xArgonMax (0.04)
Bit 12 =1 (00800H) Error12: Absolute pressure in MPa <Pmin (10-9) or >Pmax (280)
Bit 13 =1 (01000H) Error13: Temperature in K <Tmin (143.15) or >Tmax (453.15)
Bit 14 =1 (02000H) Error14: Relative density <RdMin (0.07) or >RdMax (1.52)
Bit 15 =1 (04000H) Error15: Volumetric gross heating value <VghvMin (0) or >VghvMax (66)
Bit 16 =1 (08000) Error16: No convergence
Bit 17 =1 (10000H) Warning: None of ranges A, B or C (AGA8-Table 1) has been satisfied
Bit 18,... All higher bits are unused.

Header file: "aga8part1.h"

The following is a header file:

#include "stdio.h" #include "math.h" /* declaration of the 'aga8' struct */ struct aga8p1 { /* INPUT PARAMETERS */ double xi[21];//Molar fractions xi[0 to 20] // MOLE FRACTIONS OF: // x[0] Methane // x[1] Nitrogen // x[2] Carbon Dioxide // x[3] Ethane // x[4] Propane // x[5] i-Butane // x[6] n-Butane // x[7] i-Pentane // x[8] n-Pentane // x[9] n-Hexane // x[10] n-Heptane // x[11] n-Octane // x[12] n-Nonane // x[13] n-Decane // x[14] Hydrogen // x[15] Oxygen // x[16] Carbon monoxide // x[17] Water // x[18] Hydrogen sulfide // x[19] Helium // x[20] Argon double P; //Absolute pressure [MPa] double T; //Temperature [K] /* OUTPUT PARAMETERS */ double res[24]; // Space to store all output parameters (results) // res[0] - molar mass // res[1] - molar density [kg-mole/m3] // res[2] - density [kg/m3] // res[3] - compressibility factor // res[4] - relative density // res[5] - reduced density of a gas mixture // res[6] - gross heating value on a volumetric basis 25/0/1.01325 [MJ/m^3] // res[7] - molar Helmholtz energy // res[8] - molar ideal gas Helmholtz energy // res[9] - molar residual Helmholtz energy // res[10] - molar entropy // res[11] - molar energy // res[12] - molar enthalpy // res[13] - molar Gibbs energy // res[14] - molar isochoric heat capacity // res[15] - molar isobaric heat capacity // res[16] - speed of sound // res[17] - Joule-Thomson coefficient // res[18] - isentropic exponent // res[19] - specific entropy // res[20] - specific energy // res[21] - specific enthalpy // res[22] - specific isochoric heat capacity // res[23] - specific isobaric heat capacity long int es; //Error status /* INTERMEDIATE RESULTS */ short i, j, n; double xxi[21];// Space to backup mole fractions for test of the changings //in the succeeding cycle short offlineDone; //offlineDone=1 indicates that OFF-LINE calculations have //already been performed double Gij, Eij, Bnijx, B, Bx[18], U5, U, G, Q, F, K5, rhor, K3, dr, Z, M; //Bx stands for B* double Cx[58], zet[58]; // zet[n] stands for (Cn*)*pow(T,-un[n]), Eq. 4-36; //used also in Eqs. 4.1 and 4.3. double d1, d2, dk, d0, p1, p2, temp, z0, rden, ghv; double ksi04[21], ksi05[21], ksi06[21], ksi07[21], psi[58], omega[58]; double a, ao, ar, Bt, Btt, aot, aott, art, artt, ard, ardd, ardt, dpd, dpT; double s, ss, u, us, h, hs, g, cv, cvs, cp, cps, w, JTc, kapa; double Par[28]; double xiCO2max; long int errABC, errA, errB, errC; }; /* declaration of the function 'aga8part1' */ void aga8part1(struct aga8p1 *aga0);


Example of a main program in C code calling the function "aga8part1()"

The following example illustrates the call to aga8x92dc() function from a main program. The program prompts for input parameters and then transfers the parameters to the struct. After the execution the program prints both the input parameters and the results on the screen.

/* **************************************** */ /* THE BEGINNING OF THE MAIN PROGRAM C-CODE */ /* **************************************** */ #include "stdio.h" #include "conio.h" #include "aga8part1.h" void main(void) { struct aga8p1 aga, *agap; /* 'agap' is a pointer to the structure 'aga' */ unsigned short int i, j, igas; char ch; // AGA8/2017-Part1, Table B.1 (Titles of Natural Gases) char *gastitle[6] = { "'User defined gas composition'", "'Gulf coast'", "'Amarillo'", "'Ekofisk'", "'High N2'", "'High CO2-N2'" }; // AGA8/2017-Part1, Table B.1 (Compositions of Natural Gas in Mole Fractions) double yy[5][21] = { //Gulf coast 0.965222, 0.002595, 0.005956, 0.018186, 0.004596, 0.000977, 0.001007, 0.000473, 0.000324, 0.000664, 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., //Amarillo 0.906724, 0.031284, 0.004676, 0.045279, 0.00828, 0.001037, 0.001563, 0.000321, 0.000443, 0.000393, 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., //Ekofisk 0.859063, 0.010068, 0.014954, 0.084919, 0.023015, 0.003486, 0.003506, 0.000509, 0.00048, 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., //High N2 0.81441, 0.13465, 0.00985, 0.033, 0.00605, 0.001, 0.00104, 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., //High CO2-N2 0.81212, 0.05702, 0.07585, 0.04303, 0.00895, 0.00151, 0.00152, 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., }; char *text[23] = { "Methane.........", "Nitrogen........", "Carbon Dioxide..", "Ethane..........", "Propane.........", "i-Butane........", "n-Butane........", "i-Pentane.......", "n-Pentane.......", "n-Hexane........", "n-Heptane.......", "n-Octane........", "n-Nonane........", "n-Decane........", "Hydrogen........", "Oxygen..........", "Carbon Monoxide.", "Water...........", "Hydrogen Sulfide", "Helium..........", "Argon...........", "Pressure [MPa]..", "Temperature [K]." }; char *textres[24] = { "Molar mass [g/mol]..........................", "Molar density [mol/dm^3]....................", "Density [kg/m^3]............................", "Compressibility factor......................", "Relative density............................", "Reduced density.............................", "Volumetric Gross heating value [MJ/m^3].....", "Molar Helmholtz energy [J/mol]..............", "Molar ideal gas Helmholtz energy [J/mol]....", "Molar residual Helmholtz energy [J/mol].....", "Molar entropy [J/(mol*K)]...................", "Molar energy [J/mol]........................", "Molar enthalpy [J/mol]......................", "Molar Gibbs energy [J/mol]..................", "Molar isochoric heat capacity [J/(mol*K)]...", "Molar isobaric heat capacity [J/(mol*K)]....", "Speed of sound [m/s]........................", "Joule-Thomson coefficient [K/MPa]...........", "Isentropic exponent.........................", "Specific entropy [kJ/(kg*K)]................", "Specific energy [kJ/kg].....................", "Specific enthalpy [kJ/kg]...................", "Specific isochoric heat capacity [kJ/(kg*K)]", "Specific isobaric heat capacity [kJ/(kg*K)]" }; for (j=0; ; ) { printf("\nINPUT PARAMETERS\n"); printf("%s", "\nSelect the complete input parameter setup '0',"); printf("%s", "\nor select the one of five AGA8/2017-Part1, Table B.1,"); printf("%s", "\ncompositions of natural gas in mole fractions\n"); for (i=0; i<=5; i++) { printf("%d%s%s%s", i, "-", gastitle[i], "\n"); } printf("%s", "by typing the number between 0 and 5: "); scanf("%d", &igas); if (igas > 0 && igas <= 5) { /* Copy molar fractions */ for (i=0; i<=20; i++) { aga.xi[i] = yy[igas-1][i]; } } else { /* Input molar fractions */ printf("Mole fraction of:\n"); for (i=0; i<=20; i++) { printf("%s = ", text[i]); scanf("%lf", &(aga.xi[i])); } } printf("%s = ", text[21]); scanf("%lf", &(aga.P)); /* Enter pressure */ printf("%s = ", text[22]); scanf("%lf", &(aga.T)); /* Enter temperature */ agap = &aga; /* Put the address of 'aga' structure into 'agap' */ aga8part1(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%s", "Selected gas = ", gastitle[igas]); /* Print gas selection */ printf("\nMole fractions:"); /* Print mole fractions */ for (i=0; i<21; ++i) { /* Fractions of components 1-21 */ printf("\n%s = %f", text[i], aga.xi[i]); } 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 */ for (i=0; i<24; ++i) { /* Print results of calculations */ printf("\n%s = %f",textres[i], aga.res[i]); /* print i-th result */ } printf("\nPress X to exit or anything else to continue: "); ch = getch(); if (( ch == 'X') || (ch == 'x')) { break; } printf("\n"); } } /* ********************************** */ /* THE END OF THE MAIN PROGRAM C-CODE */ /* ********************************** */


Calculation example

The following example shows the input parameters and the results printed on the screen after successful completion of the calculations of volumetric and thermodynamic properties of natural gas "Ekofiks" (see Table B.1 of AGA Report No. 8, Part 1) obtained at absolute pressure 6 MPa and temperature 333.15 K (60 °C).

INPUT PARAMETERS Select the complete input parameter setup '0', or select the one of five AGA8/2017-Part1, Table B.1, compositions of natural gas in mole fractions 0-'User defined gas composition' 1-'Gulf coast' 2-'Amarillo' 3-'Ekofisk' 4-'High N2' 5-'High CO2-N2' by typing the number between 0 and 5: 3 Pressure [MPa].. = 6 Temperature [K]. = 333.15 I N P U T P A R A M E T E R S: Selected gas = 'Ekofisk' Mole fractions: Methane......... = 0.859063 Nitrogen........ = 0.010068 Carbon Dioxide.. = 0.014954 Ethane.......... = 0.084919 Propane......... = 0.023015 i-Butane........ = 0.003486 n-Butane........ = 0.003506 i-Pentane....... = 0.000509 n-Pentane....... = 0.000480 n-Hexane........ = 0.000000 n-Heptane....... = 0.000000 n-Octane........ = 0.000000 n-Nonane........ = 0.000000 n-Decane........ = 0.000000 Hydrogen........ = 0.000000 Oxygen.......... = 0.000000 Carbon Monoxide. = 0.000000 Water........... = 0.000000 Hydrogen Sulfide = 0.000000 Helium.......... = 0.000000 Argon........... = 0.000000 Pressure [MPa].. = 6.000000 Temperature [K]. = 333.150000 R E S U L T S: Error(Hex) = 0 Molar mass [g/mol].......................... = 18.768272 Molar density [mol/dm^3].................... = 2.374298 Density [kg/m^3]............................ = 44.561464 Compressibility factor...................... = 0.912304 Relative density............................ = 0.649919 Reduced density............................. = 0.251504 Volumetric Gross heating value [MJ/m^3]..... = 44.385832 Molar Helmholtz energy [J/mol].............. = 6831.954083 Molar ideal gas Helmholtz energy [J/mol].... = 7097.020201 Molar residual Helmholtz energy [J/mol]..... = -265.066118 Molar entropy [J/(mol*K)]................... = -27.009531 Molar energy [J/mol]........................ = -2166.271106 Molar enthalpy [J/mol]...................... = 360.792058 Molar Gibbs energy [J/mol].................. = 9359.017247 Molar isochoric heat capacity [J/(mol*K)]... = 33.126345 Molar isobaric heat capacity [J/(mol*K)].... = 46.961635 Speed of sound [m/s]........................ = 419.406183 Joule-Thomson coefficient [K/MPa]........... = 3.648969 Isentropic exponent......................... = 1.306405 Specific entropy [kJ/(kg*K)]................ = -1.439106 Specific energy [kJ/kg]..................... = -115.421978 Specific enthalpy [kJ/kg]................... = 19.223509 Specific isochoric heat capacity [kJ/(kg*K)] = 1.765018 Specific isobaric heat capacity [kJ/(kg*K)] = 2.502182 Press X to exit or anything else to continue:

FELIB © 2002. All rights reserved.
This site was last updated July 23, 2021.