Examples

Fortran

program example_in_f
    use stdlib_kinds, only: dp, int32
    use iapws
    implicit none
    integer(int32) :: i, ngas
    real(dp) :: T(1), kh_res(1), kd_res(1)
    real(dp) :: Ts(7), ps(7)
    character(len=2) :: gas = "O2"
    integer(int32) :: heavywater = 0
    type(gas_type), pointer :: gases_list(:)
    character(len=:), pointer :: gases_str

    print *, '########################## IAPWS VERSION ##########################'
    print *, "version ", get_version()  

    print *, '########################## IAPWS R2-83 ##########################'
    print "(a, f10.3, a)", "Tc in h2o=", Tc_H2O, " k"
    print "(a, f10.3, a)", "pc in h2o=", pc_H2O, " mpa"
    print "(a, f10.3, a)", "rhoc in h2o=", rhoc_H2O, " kg/m3"

    print "(a, f10.3, a)", "Tc in D2O=", Tc_D2O, " k"
    print "(a, f10.3, a)", "pc in D2O=", pc_D2O, " mpa"
    print "(a, f10.3, a)", "rhoc in D2O=", rhoc_D2O, " kg/m3"
    print *, ''

    print *, '########################## IAPWS G7-04 ##########################'
    ! Compute kh and kd in H2O
    T(1) = 25.0_dp + 273.15_dp
    call kh(T, gas, heavywater, kh_res)
    print "(A10, 1X, A10, 1X, A2, F10.1, A, 4X, A3, SP, F10.4)", "Gas=", gas, "T=", T, "K", "kh=", kh_res

    call kd(T, gas, heavywater, kd_res)
    print "(A10, 1X, A10, 1X, A2, F10.1, A, 4X, A3, SP, F15.4)", "Gas=", gas, "T=", T, "K", "kh=", kd_res

    ! Get and print available gases
    heavywater = 0
    ngas = ngases(heavywater)
    gases_list => null()
    gases_list => gases(heavywater)
    gases_str => gases2(heavywater)
    print *, "Gases in H2O: ", ngas
    print *, gases_str
    do i=1, ngas
        print *, gases_list(i)%gas
    enddo

    heavywater = 1
    ngas = ngases(heavywater)
    gases_list => null()
    gases_list => gases(heavywater)
    gases_str => gases2(heavywater)
    print *, "Gases in D2O: ", ngas
    print *, gases_str
    do i=1, ngas
        print *, gases_list(i)%gas
    enddo

    print *, '########################## IAPWS R7-97 ##########################'
    ! Compute ps from Ts.
    Ts(:) = [-1.0_dp, 25.0_dp, 100.0_dp, 200.0_dp, 300.0_dp, 360.0_dp, 374.0_dp]
    Ts(:) = Ts(:) + 273.15_dp
    call psat(Ts, ps)

    do i=1, size(Ts)
        print "(SP, F23.3, A3, 4X, F23.3, A3)", Ts(i), "K", ps(i), "MPa"
    end do 

    ! Compute Ts from ps
    call Tsat(ps, Ts)
    do i=1, size(Ts)
        print "(SP, F23.3, A3, 4X, F23.3, A3)", Ts(i), "K", ps(i), "MPa"
    end do 

end program

C

#include <string.h>
#include <stdio.h>
#include "iapws.h"

int main(void){

    double T = 25.0 + 273.15; /* in C*/
    char *gas = "O2";
    double kh, kd;
    char **gases_list;
    char *gases_str;
    int ngas;
    int i;
    int heavywater = 0;

    printf("%s\n", "########################## IAPWS VERSION ##########################");
    printf("version %s\n", iapws_get_version());

    printf("%s\n", "########################## IAPWS R2-83 ##########################");
    printf("%s %10.3f %s\n", "Tc in H2O", iapws_r283_Tc_H2O, "K");
    printf("%s %10.3f %s\n", "pc in H2O", iapws_r283_pc_H2O, "MPa");
    printf("%s %10.3f %s\n", "rhoc in H2O", iapws_r283_rhoc_H2O, "kg/m3");

    printf("%s %10.3f %s\n", "Tc in D2O", iapws_r283_Tc_D2O, "K");
    printf("%s %10.3f %s\n", "pc in D2O", iapws_r283_pc_D2O, "MPa");
    printf("%s %10.3f %s\n", "rhoc in D2O", iapws_r283_rhoc_D2O, "kg/m3");

    printf("\n");


    printf("%s\n", "########################## IAPWS G7-04 ##########################");
    /* Compute kh and kd in H2O*/
    iapws_g704_kh(&T, gas, heavywater, &kh, strlen(gas), 1);
    printf("Gas=%s\tT=%fK\tkh=%+10.4f\n", gas, T, kh);

    iapws_g704_kd(&T, gas, heavywater, &kd, strlen(gas), 1);
    printf("Gas=%s\tT=%fK\tkd=%+15.4f\n", gas, T, kd);

    /* Get and print the available gases */
    ngas = iapws_g704_ngases(heavywater);
    gases_list = iapws_g704_gases(heavywater);
    gases_str = iapws_g704_gases2(heavywater);
    printf("Gases in H2O: %d\n", ngas);
    printf("%s\n", gases_str);
    for(i=0; i<ngas; i++){
        printf("%s\n", gases_list[i]);
    }

    heavywater = 1;
    ngas = iapws_g704_ngases(heavywater);
    gases_list = iapws_g704_gases(heavywater);
    gases_str = iapws_g704_gases2(heavywater);
    printf("Gases in D2O: %d\n", ngas);
    printf("%s\n", gases_str);
    for(i=0; i<ngas; i++){
        printf("%s\n", gases_list[i]);
    }

    printf("%s\n", "########################## IAPWS R7-97 ##########################");
    double Ts[7] =  {-1.0, 25.0, 100.0, 200.0, 300.0, 360.0, 374.0};
    double ps[7] = {1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0};
    for(i=0; i<7; i++){
        Ts[i] = Ts[i] + 273.15;
    }
    iapws_r797_psat(7, Ts, ps);

    for(i=0; i<7; i++){
        printf("%+23.3f %s %+23.3f %s\n", Ts[i], "K", ps[i], "MPa");
    }

    iapws_r797_Tsat(7, ps, Ts);
    for(i=0; i<7; i++){
        printf("%+23.3f %s %+23.3f %s\n", Ts[i], "K", ps[i], "MPa");
    }


    return 0;
}

Python

r"""Example in python"""
import sys
sys.path.insert(0, "../py/src/")
import array
import numpy as np
import matplotlib.pyplot as plt
import pyiapws

print("########################## IAPWS VERSION ##########################")
print(pyiapws.__version__)

print("########################## IAPWS R2-83 ##########################")
print("Tc in H2O", pyiapws.r283.Tc_H2O, "K")
print("pc in H2O", pyiapws.r283.pc_H2O, "MPa")
print("rhoc in H2O", pyiapws.r283.rhoc_H2O, "kg/m3")

print("Tc in D2O", pyiapws.r283.Tc_D2O, "K")
print("pc in D2O", pyiapws.r283.pc_D2O, "MPa")
print("rhoc in D2O", pyiapws.r283.rhoc_D2O, "kg/m3")

print("")

print("########################## IAPWS G7-04 ##########################")
gas  = "O2"
T = array.array("d", (25.0+273.15,))

# Compute kh and kd in H2O
heavywater = False
k = pyiapws.g704.kh(T, "O2", heavywater)
print(f"Gas={gas}\tT={T[0]}K\tkh={k[0]:+10.4f}\n")

k = pyiapws.g704.kd(T, "O2", heavywater)
print(f"Gas={gas}\tT={T[0]}K\tkd={k[0]:+10.4f}\n")

# Get and print the available gases
heavywater = False
gases_list = pyiapws.g704.gases(heavywater)
gases_str = pyiapws.g704.gases2(heavywater)
ngas = pyiapws.g704.ngases(heavywater)
print(f"Gases in H2O: {ngas:}")
print(gases_str)
for gas in gases_list:
    print(gas)

heavywater = True
gases_list = pyiapws.g704.gases(heavywater)
gases_str = pyiapws.g704.gases2(heavywater)
ngas = pyiapws.g704.ngases(heavywater)
print(f"Gases in D2O: {ngas:}")
print(gases_str)
for gas in gases_list:
    print(gas)

style = {"marker":".", "ls":"", "ms":2}
T_KELVIN = 273.15
T = np.linspace(0.0, 360.0, 1000) + 273.15

solvent = {True: "D2O", False: "H2O"}

print("Generating plot for kh")
kname = "kh"
for HEAVYWATER in (False, True):
    print(solvent[HEAVYWATER])
    fig = plt.figure()
    ax = fig.add_subplot()
    ax.grid(visible=True, ls=':')
    ax.set_xlabel("T /°K")
    ax.set_ylabel("ln (kh/1GPa)")
    gases = pyiapws.g704.gases(HEAVYWATER)
    for gas in gases:
        k = pyiapws.g704.kh(T, gas, HEAVYWATER) / 1000.0
        ln_k = np.log(k)
        ax.plot(T, ln_k, label=gas, **style)
    ax.legend(ncol=3)
    fig.savefig(f"../media/g704-{kname:s}_{solvent[HEAVYWATER]}.png", dpi=100, format="png")

print("Generating plot for kd")
kname = "kd"
for HEAVYWATER in (False, True):
    print(solvent[HEAVYWATER])
    fig = plt.figure()
    ax = fig.add_subplot()
    ax.grid(visible=True, ls=':')
    ax.set_xlabel("T /°K")
    ax.set_ylabel("ln kd")
    gases = pyiapws.g704.gases(HEAVYWATER)
    for gas in gases:
        k = pyiapws.g704.kd(T, gas, HEAVYWATER)
        ln_k = np.log(k)
        ax.plot(T, ln_k, label=gas, **style)
    ax.legend(ncol=3)
    fig.savefig(f"../media/g704-{kname:s}_{solvent[HEAVYWATER]}.png", dpi=100, format="png")



print("########################## IAPWS R7-97 ##########################")
Ts = np.asarray([-1.0, 25.0, 100.0, 200.0, 300.0, 360.0, 374.0])
Ts = Ts + 273.15


ps = pyiapws.r797.psat(Ts)
for i in range(Ts.size): 
    print(f"{Ts[i]:23.3f} K {ps[i]:23.3f} MPa.")

Ts = pyiapws.r797.Tsat(ps)
for i in range(Ts.size): 
    print(f"{Ts[i]:23.3f} K {ps[i]:23.3f} MPa.")

fig = plt.figure()
ax = fig.add_subplot()
ax.grid(visible=True, ls=':')
ax.set_xlabel("Ts /K")
ax.set_ylabel("ps /MPa")
Ts = np.linspace(0.0, 370.0, 500)
Ts = Ts + 273.15

ps = pyiapws.r797.psat(Ts)
ax.plot(Ts, ps, "r-", label="ps(Ts)")

Ts = pyiapws.r797.Tsat(ps)
ax.plot(Ts, ps, "b--", label="Ts(ps)")

ax.legend()
fig.savefig(f"../media/r797-r4.png", dpi=100, format="png")


plt.show()