C++

The C++ API is provided by head files and a library file named libeosH2ONaCl.a for macOS and linux, libeosH2ONaCl.lib for windows system, respectively. The head files and library file are contained in the app installer (in the include and lib folder), see also App install section.

Before starting to use c++ api of swEOS, the CMake and c++ compiler have to be installed.

How to start ?

Assuming the library file and head files has been downloaded and saved in ~/Download/swEOS (please use your own path) path, it means that the lib folder and include folder are in ~/Download/swEOS path.

Step 1. Create a source code folder and check directory to this folder.

Step 2. Create c++ source code, e.g. main.cpp, see Listing 5. Include the head file(line 1) and instantiate a object (line 5) of cH2ONaCl class, then all the properties and member functions can be accessed through object eos, e.g. density (line 9).

Listing 5 Source code of C++ api for calculating density of H2ONaCl.
 1#include "H2ONaCl.H"
 2#include <iostream>
 3int main()
 4{
 5    H2ONaCl::cH2ONaCl eos;
 6    double p=200E5;   //Pa
 7    double T=400+273.15;   //K
 8    double X=0.2;    //wt.% NaCl
 9    double rho=eos.rho_pTX(p,T,X); //kg/m3
10    std::cout<<" Pressure(bar): "<<p/1E5<<"\n"
11             <<" Temperature(deg.C): "<<T-273.15<<"\n"
12             <<" Salinity (wt.% NaCl): "<<X<<std::endl;
13    std::cout<<" Density(kg/m3): "<<rho<<std::endl;
14}

Step 3. Create CMakeLists.txt, see Listing 6.

Listing 6 CMake file
 1# 0. CMake Minimum version
 2cmake_minimum_required(VERSION 3.3...3.12 FATAL_ERROR)
 3
 4project(test_swEOS LANGUAGES CXX)
 5set(CMAKE_CXX_STANDARD 11)
 6set(CMAKE_CXX_STANDARD_REQUIRED ON)
 7
 8# 1. set path of SWEOS library, which can be downloaded from https://github.com/zguoch/saltwatereos/releases according to your OS
 9set(SWEOS_DIR "../../" CACHE FILEPATH "Main path of SWEOS library")
10if(EXISTS ${SWEOS_DIR}/include AND  EXISTS ${SWEOS_DIR}/lib/libeosH2ONaCl.a)
11  message(STATUS "SWEOS head files found: " ${SWEOS_DIR}/include)  
12  message(STATUS "SWEOS libraries found: " ${SWEOS_DIR}/lib/libeosH2ONaCl.a )   
13else()
14  message(FATAL_ERROR "Please specify path of SWEOS library which contains lib and include paths\n cmake -DSWEOS_DIR=path_of_SWEOS .. ")
15endif()
16
17aux_source_directory(. SRC_MAIN)
18include_directories(${SWEOS_DIR}/include)
19link_directories(${SWEOS_DIR}/lib)
20
21add_executable(${PROJECT_NAME} main.cpp)
22target_link_libraries(${PROJECT_NAME} eosH2ONaCl)

Step 4. Configure and generate project using CMake command line tool or GUI app.

Please remember set cmake cache variable of SWEOS_DIR to specify the swEOS library path which contains lib and include folders.

mkdir build
cd build
cmake cmake -DSWEOS_DIR=~/Download/swEOS ..

Note

If SWEOS_DIR is not set or set incorrectly, you will get the following error information. Then you just make sure set a correct path for SWEOS_DIR to fix the problem.

CMake Error at CMakeLists.txt:14 (message):
Please specify path of SWEOS library which contains lib and include paths

    cmake -DSWEOS_DIR=path_of_SWEOS ..

-- Configuring incomplete, errors occurred!

Step 5. Compile and build the program.

In macOS or Linux system, just run make to compile and build the program. While, in Windows system and using Visual Studio 2017 Community, you will get a .sln file. Now, every VS user should know how to do!

Step 6. Run the program.

The executable program name is test_swEOS, which is configured in the CMakeLists file (see line 4 of Listing 6). Run this program in the terminal, you will get the output like this,

./test_swEOS

Pressure(bar): 200
Temperature(deg.C): 400
Salinity (wt.% NaCl): 0.2
Density(kg/m3): 188.056

More features

All right, now you must know how to use c++ api of swEOS package in your own program, see Cookbooks for usage of more functions.

pip install scipyFoam