FE-ISO20765-1-C


FE-ISO20765-1-C (Revised: February 19, 2010) 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:2005(E). The following is a brief description of a function (method), input/output parameters and header file with an example of a main program in a c-source code.

Description of function, struct, parameters, ranges of applications and errors

Function
Function declaration void iso20765x1(struct iso20765 *);
Struct iso20765{fpnp xx[42]; unsigned long int es;}
INPUT parameters
fpnp1) xx[0] Mole fraction of — Methane
fpnp xx[1] Mole fraction of — Nitrogen
fpnp xx[2] Mole fraction of — Carbon Dioxide
fpnp xx[3] Mole fraction of — Ethane
fpnp xx[4] Mole fraction of — Propane
fpnp xx[5] Mole fraction of — Water
fpnp xx[6] Mole fraction of — Hydrogen-sulfide
fpnp xx[7] Mole fraction of — Hydrogen
fpnp xx[8] Mole fraction of — Carbon-monoxide
fpnp xx[9] Mole fraction of — Oxygen
fpnp xx[10] Mole fraction of — Isobutane
fpnp xx[11] Mole fraction of — n-Butane
fpnp xx[12] Mole fraction of — Isopentane
fpnp xx[13] Mole fraction of — n-Pentane
fpnp xx[14] Mole fraction of — n-Hexane
fpnp xx[15] Mole fraction of — n-Heptane
fpnp xx[16] Mole fraction of — n-Octane
fpnp xx[17] Mole fraction of — n-Nonane
fpnp xx[18] Mole fraction of — n-Decane
fpnp xx[19] Mole fraction of — Helium
fpnp xx[20] Mole fraction of — Argon
fpnp xx[21] Pressure (absolute) in MPa
fpnp xx[22] Temperature in K
RESULTS
fpnp xx[23] Natural gas mixture average molar mass [kg/kmol]
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 [MJ/m3] 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] Molar internal energy [kJ/kmol]
fpnp xx[30] Specific internal energy [kJ/kg]
fpnp xx[31] Molar Enthalpy [kJ/kmol]
fpnp xx[32] Specific enthalpy [kJ/kg]
fpnp xx[33] Molar entropy [kJ/(kmol⋅K)]
fpnp xx[34] Specific entropy [kJ/(kg⋅K)]
fpnp xx[35] Molar isochoric heat capacity [kJ/(kmol⋅K)]
fpnp xx[36] Specific isochoric heat capacity [kJ/(kg⋅K)]
fpnp xx[37] Molar isobaric heat capacity [kJ/(kmol⋅K)]
fpnp xx[38] Specific isobaric heat capacity [kJ/(kg⋅K)]
fpnp xx[39] Joule-Thomson coefficient [K/MPa]
fpnp xx[40] Isentropic exponent []
fpnp xx[41] Speed of sound [m/s]
long es 2) Error status after completion of the calculation method
Ranges of application implemented in FE-ISO20765-1-C
Parameter Range
Pressure (absolute) in MPa 10-7 to 35
Temperature in K 250 to 350
Relative density 0.55 to 0.90
Superior calorific value in MJ/m3 20 to 48/td>
Mole fractions ranges
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 higher hydrocarbons 0 to 0.0005
Helium 0 to 0.005
Argon 0 to 0.0002
1)
"fpnp" stands for floating point number precision. It can be defined as single or double precision floating point number. See header file below.
2) long es "long es" - Error status after completion of the calculation
Error/Warning bits of "long es" Description of Error/Warning
Bit 0 =1 (000001H) Error: There exist at least one molar fraction < 0 or > 1
Bit 1 =1 (000002H) Error: The total sum of molar fractions <0.98 or >1.02
Bit 2 =1 (000004H) Warning: Total sum of molar fractions is <0.999999 or >1.000001.
If set this bit only warns that the fractions have been normalized to give the total sum of molar fractions exactly equal to 1. If only bit 2 was set during the calculation the results are valid.
Bit 3 =1 (000008H) Error: Mole fraction of Methane out of range.
Bit 4 =1 (000010H) Error: Mole fraction of Nitrogen out of range.
Bit 5 =1 (000020H) Error: Mole fraction of Carbon Dioxide out of range.
Bit 6 =1 (000040H) Error: Mole fraction of Ethane out of range.
Bit 7 =1 (000080H) Error: Mole fraction of Propane out of range.
Bit 8 =1 (000100H) Error: Mole fraction of Water out of range.
Bit 9 =1 (000200H) Error: Mole fraction of Hydrogen sulfide out of range.
Bit 10 =1 (000400H) Error: Mole fraction of Hydrogen out of range.
Bit 11 =1 (000800H) Error: Mole fraction of Carbon monoxide out of range.
Bit 12 =1 (001000H) Error: Mole fraction of Oxygen out of range.
Bit 13 =1 (002000H) Error: Mole fraction of Butanes) out of range.
Bit 14 =1 (004000H) Error: Mole fraction of Pentanes out of range.
Bit 15 =1 (008000H) Error: Mole fraction of Hexanes out of range.
Bit 16 =1 (010000H) Error: Mole fraction of Heptanes out of range.
Bit 17 =1 (020000H) Error: Mole fraction of Octanes plus higher hydrocarbons out of range.
Bit 18 =1 (040000H) Error: Mole fraction of Helium out of range.
Bit 19 =1 (080000H) Error: Mole fraction of Argon out of range.
Bit 20 =1 (100000H) Error: Pressure (absolute) out of range.
Bit 21 =1 (200000H) Error: Temperature out of range.
Bit 22 =1 (400000H) Error: Relative density out of range.
Bit 23 =1 (800000H) Error: Superior calorific value out of range.
All higher order bits are unused
Error status "long es" represents the errors encountered in the calculation. The results are valid if bits 0,...,23 are all cleared, or if only bit 2 was set during the calculation, otherwise the results are invalid, i.e. cleared.

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


Example of a main program "main.c" in C code calling the function "iso20765x1()"

The following example illustrates the call to iso20765x1 function from a main program. The main 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.

/* ******************* */ /* 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.004500, 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.007400, 0.000100, 0.000100, 0.095000, 0.010000, 0.000100, 0.000800, 0.000800, 0.000400, 0.000400, 0.000200, 0.000100, 0.000100, 0.000100, 0.000100, 0.000200, 0.000100, 0.812000, 0.057000, 0.076000, 0.043000, 0.009000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.001500, 0.001500, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 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.000000 }; 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; } } /* ****************************** */ /* END OF THE MAIN PROGRAM C-CODE */ /* ****************************** */