Skip to main content

aga8/
lib.rs

1#![warn(missing_docs)]
2
3/*!
4Provides methods to calculate thermodynamic properties inlcuding compressibility factors and densities of natural gases.
5It includes the AGA8 DETAIL and the GERG2008 equations of state described in AGA Report No. 8, Part 1, Third Edition, April 2017.
6
7This crate is a Rust port of NIST's [AGA8 code](https://github.com/usnistgov/AGA8).
8
9# Quick Start
10To use the AGA8 DETAIL and GERG equiations of state you typically create a struct with `new()`.
11Then you set the gas composition `x`, the pressure `p` and the temperature `t`.
12Lastly you call the `density()` and `properties()` functions to calculate the molar density and the rest of the properies.
13
14All of the calculation results are public fields in the struct that was created with `new()`.
15
16```
17use aga8::detail::Detail;
18use aga8::composition::Composition;
19
20let mut aga8_test: Detail = Detail::new();
21
22// Set the gas composition in mol fraction
23// The sum of all the components must be 1.0
24let comp = Composition {
25    methane: 0.778_24,
26    nitrogen: 0.02,
27    carbon_dioxide: 0.06,
28    ethane: 0.08,
29    propane: 0.03,
30    isobutane: 0.001_5,
31    n_butane: 0.003,
32    isopentane: 0.000_5,
33    n_pentane: 0.001_65,
34    hexane: 0.002_15,
35    heptane: 0.000_88,
36    octane: 0.000_24,
37    nonane: 0.000_15,
38    decane: 0.000_09,
39    hydrogen: 0.004,
40    oxygen: 0.005,
41    carbon_monoxide: 0.002,
42    water: 0.000_1,
43    hydrogen_sulfide: 0.002_5,
44    helium: 0.007,
45    argon: 0.001,
46};
47aga8_test.set_composition(&comp);
48// Set pressure in kPa
49aga8_test.p = 50_000.0;
50// Set temperature in K
51aga8_test.t = 400.0;
52// Run density to calculate the density in mol/l
53aga8_test.density();
54// Run properties to calculate all of the
55// output properties
56aga8_test.properties();
57
58// Molar density
59assert!((12.807 - aga8_test.d).abs() < 1.0e-3);
60// Compressibility factor
61assert!((1.173 - aga8_test.z).abs() < 1.0e-3);
62```
63
64# Crate features
65* **extern** - Builds external ffi functions. These functions can be used by other programming languages.
66*/
67
68pub mod composition;
69pub mod detail;
70pub mod gerg2008;
71mod gerg2008const;
72
73/// Error conditions for density calculation
74#[repr(C)]
75#[derive(Debug, PartialEq, Eq)]
76pub enum DensityError {
77    /// Calculation was successful
78    Ok,
79    /// Calculation failed to iterate
80    IterationFail,
81    /// Pressure is too low
82    PressureTooLow,
83}
84
85#[cfg(feature = "extern")]
86pub mod ffi;