iso20765x1.c - Instructions for use

Revision: Feb 19, 2010

 

E-mail: ivan.maric@felib.com

 

iso20765x1.c is the C source code implementation of the International standard:

ISO 20765-1:2005(E): Natural gas - Calculation of thermodynamic properties

Part 1: Gas phase properties for transmission and distribution applications

 

The software is written in a C-language and embeds all the restrictions implied by ISO-20765-1.

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.

 

 

Header file: iso20765x1.h

 

#include "stdio.h"

#include "math.h"

 

/* IN CASE OF SINGLE FLOATING POINT NUMBER PRECISION */

/* #define fpnp float */

/* IN CASE OF Double FLOATING POINT NUMBER PRECISION */

/* #define fpnp double */

#define fpnp double

 

/* declaration of the structure */

struct iso20765

{

        /* INPUT/OUTPUT PARAMETERS SEE BELOW*/

        fpnp xx[42];           /* Parameters in floating point format */

        unsigned long int es;  /* Error status */

};

 

/* declaration of the function iso20765x1.c */

void iso20765x1(struct iso20765 *);   /* input/output parameters */

 

 

Input/output parameters

 

// The input/output parameters are defined by struct iso20765 - see header file

// iso20765x1.h

// Input parameters (the ranges of application are given below):

// fpnp xx[0]  - molar fraction of Methane

// fpnp xx[1]  - molar fraction of Nitrogen

// fpnp xx[2]  - molar fraction of Carbon Dioxide

// fpnp xx[3]  - molar fraction of Ethane

// fpnp xx[4]  - molar fraction of Propane

// fpnp xx[5]  - molar fraction of Water

// fpnp xx[6]  - molar fraction of Hydrogen sylfide

// fpnp xx[7]  - molar fraction of Hydrogen

// fpnp xx[8]  - molar fraction of Carbon monoxide

// fpnp xx[9]  - molar fraction of Oxygen

// fpnp xx[10] - molar fraction of iso-Butane

// fpnp xx[11] - molar fraction of n-Butane

// fpnp xx[12] - molar fraction of iso-Pentane

// fpnp xx[13] - molar fraction of n-Pentane

// fpnp xx[14] - molar fraction of n-Hexane

// fpnp xx[15] - molar fraction of n-Heptane

// fpnp xx[16] - molar fraction of n-Octane

// fpnp xx[17] - molar fraction of n-Nonane

// fpnp xx[18] - molar fraction of n-Decane

// fpnp xx[19] - molar fraction of Helium

// fpnp xx[20] - molar fraction of Argon

// fpnp xx[21] - Pressure in [MPa]

// fpnp xx[22] - Temperature [K]

// The function 'iso20765x1' calculates results and sets the error status

// The results:

// fpnp xx[23] - Natural gas mixture average molar mass (mass per mole)

// fpnp xx[24] - Natural gas molar density [kg-mole/m3]

// fpnp xx[25] - Natural gas density [kg/m3]

// fpnp xx[26] - Natural gas compression (compressibility factor)

// fpnp xx[27] - Natural gas density to air density ratio at

//                            T2=273.15[K], p2=0.101325[MPa]

// fpnp xx[28] - Superior calorific value on a volumetric basis at

//               reference conditions for:

//               Combustion: T1=298.15[K], p1=0.101325[MPa]

//               Metering:   T2=273.15[K], p2=0.101325[MPa]

// fpnp xx[29] - MolarInternalEnergy()

// fpnp xx[30] - SpecificInternalEnergy()

// fpnp xx[31] - MolarEnthalpy()

// fpnp xx[32] - SpecificEnthalpy()

// fpnp xx[33] - Molar entropy()

// fpnp xx[34] - Specific entropy()

// fpnp xx[35] - MolarIsochoricHeatCapacity()

// fpnp xx[36] - SpecificIsochoricHeatCapacity()

// fpnp xx[37] - MolarIsobaricHeatCapacity()

// fpnp xx[38] - SpecificIsobaricHeatCapacity()

// fpnp xx[39] - JouleThomsonCoefficient()

// fpnp xx[40] - IsentropicExponent()

// fpnp xx[41] - SpeedOfSound()

// long es     - Error status after calculation (see below)

 

// Before calling the function 'iso20765x1)' the input parameters

// should be set to valid values. Upon completion of the routine the results will be

// valid if no errors were encountered (see detailed description below).

 

// Ranges of application for pipeline quality natural gas

 

//                                          Range

//    absolute pressure [MPa]          10**-7 to 30

//    temperature [K]                     250 to 350

//    relative density []                0.55 to 0.90

//    superior calorific value [MJ/m^3]    20 to 48

 

// Ranges for mole fractions of natural gas components:

 

//    methane [%]                       0.7 to  1.00

//    nitrogen [%]                        0 to  0.20

//    carbon dioxide [%]                  0 to  0.20

//    ethane [%]                          0 to  0.10

//    propane [%]                         0 to  0.035

//    water [%]                           0 to  0.00015

//    hydrogen sulfide [%]               0 to  0.0002

//    hydrogen [%]                        0 to  0.10

//    carbon monoxide [%]                 0 to  0.03

//    oxigen [%]                          0 to  0.0002

//    butanes [%]                         0 to  0.015

//    pentanes [%]                        0 to  0.005

//    hexanes [%]                         0 to  0.001

//    heptanes [%]                        0 to  0.0005

//    octanes plus [%]                    0 to  0.0005

//    helium [%]                          0 to  0.005

//    argon [%]                           0 to  0.0002

 

// The function 'iso20765x1' sets 'long es' representing the

// error status in the following way:

 

// Bit 0   =1 (000001H) if there exist at least one molar fraction < 0, otherwise =0.

// Bit 1   =1 (000002H) if the total sum of molar fractions <0.98 or >1.02, otherwise =0.

// Bit 2   =1 (000004H) if the total sum of molar fractions is <0.999999 or >1.000001, otherwise =0.

//            This bit denotes only the 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 <0.7 or >1.0, otherwise =0.

// Bit 4   =1 (000010H) if mole fraction of Nitrogen >0.2, otherwise =0.

// Bit 5   =1 (000020H) if mole fraction of Carbon Dioxide >0.2, otherwise =0.

// Bit 6   =1 (000040H) if mole fraction of Ethane >0.1, otherwise =0.

// Bit 7   =1 (000080H) if mole fraction of Propane >0.035, otherwise =0.

// Bit 8   =1 (000100H) if mole fraction of Water >0.00015, otherwise =0.

// Bit 9   =1 (000200H) if mole fraction of Hydrogen sulfide > 0.0002, otherwise =0.

// Bit 10  =1 (000400H) if mole fraction of Hydrogen >0.10, otherwise =0.

// Bit 11  =1 (000800H) if mole fraction of Carbon monoxide > 0.03, otherwise =0.

// Bit 12  =1 (001000H) if mole fraction of Oxygen >0.0002, otherwise =0.

// Bit 13  =1 (002000H) if mole fraction of Butanes (n-Butane + i-Butane) >0.015, otherwise =0.

// Bit 14  =1 (004000H) if mole fraction of Pentanes (n-pentane + i-Pentane) >0.005, otherwise =0.

// Bit 15  =1 (008000H) if mole fraction of n-Hexane >0.001, otherwise  =0.

// Bit 16  =1 (010000H) if mole fraction of n-Heptane >0.0005, otherwise  =0.

// Bit 17  =1 (020000H) if mole fraction of octanes plus higher hydrocarbons (n-Octane +

//            n-Nonane, n-Decane) >0.0005, otherwise =0.

// Bit 18  =1 (040000H) if mole fraction of Helium >0.005, otherwise =0.

// Bit 19  =1 (080000H) if mole fraction of Argon >0.0002, otherwise =0.

// Bit 20  =1 (100000H) if pressure <0.1 Pa or >30 MPa, otherwise =0.

// Bit 21  =1 (200000H) if temperature <250 K or >350 K, otherwise =0.

// Bit 22  =1 (400000H) if relative density <0.55 or >0.90, otherwise =0.

// Bit 23  =1 (800000H) if superior calorific value <20 MJ/m^3 or >48 MJ/m^3, otherwise =0.

// All other bits are unused

 

// The error status (es) is a long number representing all the errors encountered

// in the calculation.

// If bits 0,...,23 are all cleared, or if only bit 2 was set during the calculation,

// the results are valid, otherwise the results are invalid, i.e. cleared.

 

 

Example of a main program in C code calling the iso20765x1  function

 

The following example illustrates the call to iso20765x1  function from a main program. The program defines a struct for ISO-20765-1 input/output data. The program prompts for the selection of the natural gas mixture (Gas1 to Gas 6) given in Anex G of ISO-20765-1:2005(E) or for the manual setup of input parameters. 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 Tables 2-7 in Annex G of ISO 20765-1.

 

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

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

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

 

#include "iso20765x1.h"

 

void main(void)

{

        struct iso20765 iso, *isox; /* 'isox' is a pointer to a structure 'iso' */

        unsigned short int j, ii, ix;

        char ch;

        char *text[43] = {

               "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] ",

               "Mole average                               ",

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

               "Density [kg/m3]                            ",

               "Compression factor                         ",

               "Relative density                           ",

               "Superior calorific value kJ/(kg.K)         ",

               "Molar internal energy kJ/kmol              ",

               "Specific internal energy kJ/kg             ",

               "Molar enthalpy kJ/kmol                     ",

               "Specific enthalpy kJ/kg                    ",

               "Molar entropy kJ/(kmol.K)                  ",

               "Specific entropy kJ/(kg.K)                 ",

               "Molar isochoric heat capacity kJ/(kmol.K)  ",

               "Specific isochoric heat capacity kJ/(kg.K) ",

               "Molar isobaric heat capacity kJ/(kmol.K)   ",

               "Specific isobaric heat capacity kJ/(kg.K)  ",

               "Joule-Thomson coefficient K/MPa            ",

               "Isentropic exponent                        ",

               "Speed of sound m/s                         ",

               "Error status                               "

        };

        /* ISO-20765-1, Annex G, Table G.1: example calculations Gas1,..., Gas6 */

        /* Molar fractions of natural gas components: */

        /* Methane, Nitrogen, Carbon diox., Ethane, Propane, Water, Hydrogen s., */

        /* Hydrogen, Carbon m., Oxygen, iso-Butane, n-Butane, iso-Pentane, n-Pentane, */

        /* n-Hexane, n-Heptane, n-Octane, n-Nonane, n-Decane, Helium, Argon */

        fpnp  mf[6][21] = {

               0.965000, 0.003000, 0.006000, 0.018000, 0.00450, 0.000000, 0.000000,

               0.000000, 0.000000, 0.000000, 0.001000, 0.001000, 0.000500, 0.000300,

               0.000700, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000,

 

               0.907000, 0.031000, 0.005000, 0.045000, 0.008400, 0.000100, 0.000100,

               0.000000, 0.000000, 0.000100, 0.001000, 0.001500, 0.000300, 0.000400,

               0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000100,

 

               0.859284, 0.009617, 0.015021, 0.084563, 0.023022, 0.000000, 0.000000,

               0.000000, 0.000000, 0.000000, 0.000000, 0.006985, 0.000000, 0.001218,

               0.000228, 0.000057, 0.000005, 0.000000, 0.000000, 0.000000, 0.000000,

 

               0.735000, 0.100000, 0.016000, 0.033000, 0.0074000, 0.000100, 0.000100,

               0.095000, 0.010000, 0.000100, 0.000800, 0.0008000, 0.000400, 0.000400,

               0.000200, 0.000100, 0.000100, 0.000100, 0.0001000, 0.000200, 0.000100,

 

               0.812000, 0.057000, 0.0760000, 0.0430000, 0.0090000, 0.000000, 0.000000,

               0.000000, 0.000000, 0.0000000, 0.0015000, 0.0015000, 0.000000, 0.000000,

               0.000000, 0.000000, 0.0000000, 0.0000000, 0.0000000, 0.000000, 0.000000,

 

               0.825198, 0.117266, 0.011093, 0.034611, 0.007645, 0.000000, 0.000000,

               0.000000, 0.000000, 0.000000, 0.000000, 0.002539, 0.000000, 0.000746,

               0.000225, 0.000110, 0.000029, 0.000000, 0.000000, 0.000538, 0.0000000

        };            

        for (j=0; ; ) {

               ii=0;

               printf("\n\nSelect input parameter setup '0' or ISO-20765-1 gas mixture '1-6'.\n Type number between 0 and 6: ");

               scanf("%d", &(ii));

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

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

                              iso.xx[ix]=mf[ii-1][ix];

                       }

                       printf("\nGas %d:\n", ii);

                       for (ix=21; ix<=22; ix++) {

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

                              scanf("%lf", &(iso.xx[ix]));

                       }

               }

               else {

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

                       for (ix=0; ix<=22; ix++) {

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

                              scanf("%lf", &(iso.xx[ix]));

                       }

               }

               isox = &iso; /* Put the address of 'iso' structure into 'isox' */

               iso20765x1(isox); /* Call function by passing the pointer to structure */

 

               /* Print input parameters */

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

 

               for (ix=0; ix<=22; ix++) {

                       printf("\n%s = %lf",text[ix], iso.xx[ix]);

               }

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

               printf("\n%s = %xH",text[42], iso.es);

               for (ix=23; ix<=41; ix++) {

                       printf("\n%s = %lf",text[ix], iso.xx[ix]);

               }

               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 */

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