LoginSignup
0
0

More than 1 year has passed since last update.

UbuntuにCanteraをインストールして,C++のコードを実行するまで

Last updated at Posted at 2021-12-03

Canteraといつ反応解析のソフトウェアのソースコードを変更したいと思い,以前に書いた記事ではWindows環境下でソースコードからインストールする過程を示した.
しかしながらCanteraではC++で記述されたクラスをCythonを経由することでPythonのインターフェースで使用するという,そこそこ複雑な構造になっていて,デバッグなどの昨日だったりエディタのハイライトが対応できないという事から,コードを改変するときに結構不便になってしまう.
そこでこの記事では,C++のソースコードを実行及び改変可能な環境を構築することを目指し,WSLを使用して構築したUbuntuに対してCanteraをインストールしてC++のコードを実行する手順を示しておく.

環境

  • ホストOS: Windows 10
  • 実行環境: Ubuntu 20.04 LTS (WSL2)

インストール

公式のUbuntuへのインストールに関するページを参考に実行する.

以下のコマンドを実行してCanteraのインストールを行う.

sudo apt install software-properties-common
sudo apt-add-repository ppa:cantera-team/cantera
sudo apt install cantera-python3 cantera-dev

必要なパッケージのインストールを行う.

sudo apt install libfmt-dev libboost-all-dev g++

サンプルコードの実行

公式のページを参考に実行する.ただし,実行用のコマンドは変更している.

combustor.cpp
#include "cantera/thermo.h"
#include <iostream>

using namespace Cantera;

// The actual code is put into a function that
// can be called from the main program.
void simple_demo()
{
    // Create a new phase
    std::unique_ptr<ThermoPhase> gas(newPhase("h2o2.yaml", "ohmech"));

    // Set its state by specifying T (500 K) P (2 atm) and the mole
    // fractions. Note that the mole fractions do not need to sum to
    // 1.0 - they will be normalized internally. Also, the values for
    // any unspecified species will be set to zero.
    gas->setState_TPX(500.0, 2.0*OneAtm, "H2O:1.0, H2:8.0, AR:1.0");

    // Print a summary report of the state of the gas
    std::cout << gas->report() << std::endl;
}

// the main program just calls function simple_demo within
// a 'try' block, and catches CanteraError exceptions that
// might be thrown
int main()
{
    try {
        simple_demo();
    } catch (CanteraError& err) {
        std::cout << err.what() << std::endl;
    }
}

実行コマンドは以下
-llapack-lyaml-cppのフラッグを付け加えている.

g++ combustor.cpp -o combustor -pthread -O3 -std=c++0x -lcantera -lsundials_cvodes -lsundials_ida -lsundials_nvecserial -llapack -lyaml-cpp
./combustor

無事に実行できると以下の結果が出力される.

  ohmech:

       temperature   500 K
          pressure   2.0265e+05 Pa
           density   0.36118 kg/m^3
  mean mol. weight   7.4093 kg/kmol
   phase of matter   gas

                          1 kg             1 kmol     
                     ---------------   ---------------
          enthalpy       -2.4772e+06       -1.8354e+07  J
   internal energy       -3.0382e+06       -2.2511e+07  J
           entropy             20699        1.5337e+05  J/K
    Gibbs function       -1.2827e+07       -9.5038e+07  J
 heat capacity c_p            3919.1             29038  J/K
 heat capacity c_v              2797             20724  J/K

                      mass frac. Y      mole frac. X     chem. pot. / RT
                     ---------------   ---------------   ---------------
                H2           0.21767               0.8           -15.644
                 H                 0                 0  
                 O                 0                 0  
                O2                 0                 0  
                OH                 0                 0  
               H2O           0.24314               0.1           -82.953
               HO2                 0                 0  
              H2O2                 0                 0  
                AR           0.53919               0.1           -20.503
                N2                 0                 0
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0