Skip to content

Commit

Permalink
[unittest] Add ctreactor tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ischoegl committed Mar 2, 2023
1 parent 3c9a4e5 commit 6d632db
Show file tree
Hide file tree
Showing 2 changed files with 138 additions and 142 deletions.
160 changes: 63 additions & 97 deletions test/clib/clib_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,11 @@
#include <gmock/gmock.h>

#include "cantera/core.h"
#include "cantera/extensions/Cabinet.h"
#include "cantera/clib/ct.h"

using namespace Cantera;
using ::testing::HasSubstr;

typedef SharedCabinet<ThermoPhase> ThermoCabinet;
typedef SharedCabinet<Kinetics> KineticsCabinet;
typedef SharedCabinet<Transport> TransportCabinet;
typedef SharedCabinet<Solution> SolutionCabinet;

template<> ThermoCabinet* ThermoCabinet::s_storage;
template<> KineticsCabinet* KineticsCabinet::s_storage;
template<> TransportCabinet* TransportCabinet::s_storage;
template<> SolutionCabinet* SolutionCabinet::s_storage;

string reportError()
{
int buflen = 0;
Expand All @@ -32,69 +21,44 @@ string reportError()

TEST(ct, cabinet_size)
{
ThermoCabinet::reset();
ASSERT_EQ(ThermoCabinet::size(), 0);
thermo_reset();
ASSERT_EQ(thermo_size(), 0);

int ref = thermo_newFromFile("h2o2.yaml", "ohmech");
ASSERT_EQ(ref, 0);
ASSERT_EQ(ThermoCabinet::size(), 1);
ASSERT_EQ(thermo_size(), 1);

int ref2 = thermo_newFromFile("h2o2.yaml", "ohmech");
ASSERT_EQ(ref2, 1);
ASSERT_EQ(ThermoCabinet::size(), 2);
ASSERT_EQ(thermo_size(), 2);

solution_del(ref);
ASSERT_EQ(ThermoCabinet::size(), 2);

ThermoCabinet::clear();
ASSERT_EQ(ThermoCabinet::size(), 2);

ThermoCabinet::reset();
ASSERT_EQ(ThermoCabinet::size(), 0);
}

TEST(ct, cabinet_consistency)
{
ThermoCabinet::reset();

thermo_newFromFile("h2o2.yaml", "ohmech");
int ref = thermo_newFromFile("h2o2.yaml", "ohmech");
ASSERT_EQ(ref, 1);

auto ptr = ThermoCabinet::at(ref);
auto& item = ThermoCabinet::item(ref);
ASSERT_EQ(ptr.get(), &item);
ASSERT_EQ(thermo_size(), 2);

ASSERT_EQ(ThermoCabinet::index(item), ref);
ASSERT_EQ(ThermoCabinet::index(ThermoCabinet::item(0)), 0);
thermo_reset();
ASSERT_EQ(thermo_size(), 0);
}

TEST(ct, cabinet_exceptions)
{
SolutionCabinet::reset();
int ref = solution_newFromFile("h2o2.yaml", "ohmech", "");
solution_name(999, 0, 0);

string err = reportError();
EXPECT_THAT(err, HasSubstr("Invalid index 999."));
EXPECT_THAT(err, HasSubstr("Index 999 out of range."));

solution_del(999);
err = reportError();
EXPECT_THAT(err, HasSubstr("delete a non-existing object."));

solution_del(ref);
ASSERT_THROW(SolutionCabinet::item(ref), CanteraError);
}

TEST(ct, new_solution)
{
SolutionCabinet::reset();

string name = "ohmech";
int ref = solution_newFromFile("h2o2.yaml", name.c_str(), "");

int buflen = solution_name(ref, 0, 0) + 1; // include \0
ASSERT_EQ(buflen, name.size() + 1);
ASSERT_EQ(buflen, int(name.size() + 1));

char* buf = new char[buflen];
solution_name(ref, buflen, buf);
Expand All @@ -105,37 +69,38 @@ TEST(ct, new_solution)

TEST(ct, solution_objects)
{
ThermoCabinet::reset();
KineticsCabinet::reset();
TransportCabinet::reset();
SolutionCabinet::reset();
thermo_reset();
kin_reset();
trans_reset();

thermo_newFromFile("h2o2.yaml", "ohmech");
thermo_newFromFile("gri30.yaml", "gri30");

int ref = solution_newFromFile("h2o2.yaml", "ohmech", "none");
ASSERT_EQ(ThermoCabinet::size(), 2);
ASSERT_EQ(KineticsCabinet::size(), 1);
ASSERT_EQ(TransportCabinet::size(), 1);
auto sol = SolutionCabinet::at(ref);
ASSERT_EQ(sol->transport()->transportModel(), "None");
int ref = solution_newFromFile("gri30.yaml", "gri30", "none");
ASSERT_EQ(thermo_size(), 2);
ASSERT_EQ(kin_size(), 1);
ASSERT_EQ(trans_size(), 1);

int ref2 = solution_newFromFile("h2o2.yaml", "ohmech", "Mix");
ASSERT_EQ(ThermoCabinet::size(), 3);
ASSERT_EQ(KineticsCabinet::size(), 2);
ASSERT_EQ(TransportCabinet::size(), 2);
ASSERT_EQ(thermo_size(), 3);
ASSERT_EQ(kin_size(), 2);
ASSERT_EQ(trans_size(), 2);

auto sol2 = SolutionCabinet::at(ref2);
int thermo = solution_thermo(ref2);
ASSERT_EQ(sol2->thermo().get(), &ThermoCabinet::item(thermo));
ASSERT_EQ(thermo_nSpecies(thermo), 10);

int kin = solution_kinetics(ref2);
ASSERT_EQ(sol2->kinetics().get(), &KineticsCabinet::item(kin));
int tran = solution_transport(ref2);
ASSERT_EQ(sol2->transport().get(), &TransportCabinet::item(tran));
ASSERT_EQ(kin_nReactions(kin), 29);

solution_del(ref2);
ASSERT_THROW(ThermoCabinet::item(thermo), CanteraError);
ASSERT_THROW(KineticsCabinet::item(kin), CanteraError);
ASSERT_THROW(TransportCabinet::item(tran), CanteraError);
size_t nsp = thermo_nSpecies(thermo);
ASSERT_EQ(nsp, npos);
string err = reportError();
EXPECT_THAT(err, HasSubstr("has been deleted."));

size_t nr = thermo_nSpecies(thermo);
ASSERT_EQ(nr, npos);
err = reportError();
EXPECT_THAT(err, HasSubstr("has been deleted."));
}

TEST(ct, thermo)
Expand All @@ -146,13 +111,9 @@ TEST(ct, thermo)
size_t nsp = thermo_nSpecies(thermo);
ASSERT_EQ(nsp, 53);

auto& phase = ThermoCabinet::item(thermo);

ret = thermo_setTemperature(thermo, 500);
ASSERT_NEAR(phase.temperature(), 500, 1e-2);
ASSERT_EQ(ret, 0);
ret = thermo_setPressure(thermo, 5 * 101325);
ASSERT_NEAR(phase.pressure(), 5 * 101325, 1e-2);
ASSERT_EQ(ret, 0);
ret = thermo_setMoleFractionsByName(thermo, "CH4:1.0, O2:2.0, N2:7.52");
ASSERT_EQ(ret, 0);
Expand All @@ -162,35 +123,37 @@ TEST(ct, thermo)
double T = thermo_temperature(thermo);
ASSERT_GT(T, 2200);
ASSERT_LT(T, 2300);

// ret = thermo_print(thermo, 1, 0);
// ASSERT_EQ(ret, 0);
}

TEST(ct, kinetics)
{
int thermo = thermo_newFromFile("gri30.yaml", "gri30");
thermo_equilibrate(thermo, "HP", 0, 1e-9, 50000, 1000, 0);
double T = thermo_temperature(thermo);

int kin = kin_newFromFile("gri30.yaml", "gri30", thermo, 0, 0, 0, 0);
int kin = kin_newFromFile("gri30.yaml", "gri30", thermo, -1, -1, -1, -1);
ASSERT_GE(kin, 0);

size_t nr = kin_nReactions(kin);
ASSERT_EQ(nr, 325);

int ret = thermo_setTemperature(thermo, T - 200);
ASSERT_EQ(ret, 0);
thermo_equilibrate(thermo, "HP", 0, 1e-9, 50000, 1000, 0);
double T = thermo_temperature(thermo);
thermo_setTemperature(thermo, T - 200);

auto sol = newSolution("gri30.yaml", "gri30", "none");
auto phase = sol->thermo();
auto kinetics = sol->kinetics();

phase->equilibrate("HP");
ASSERT_NEAR(T, phase->temperature(), 1e-2);
phase->setTemperature(T - 200);

// char buf [1000];
// double ropf[325];
// printf("\n Reaction Forward ROP\n");
// kin_getFwdRatesOfProgress(kin, 325, ropf);
// int n; // declare this here for C89 compatibility
// for (n = 0; n < nr; n++) {
// kin_getReactionString(kin, n, 1000, buf);
// printf("%35s %8.6e\n", buf, ropf[n]);
// }
vector<double> c_ropf(nr);
kin_getFwdRatesOfProgress(kin, 325, c_ropf.data());
vector<double> cpp_ropf(nr);
kinetics->getFwdRatesOfProgress(cpp_ropf.data());

for (size_t n = 0; n < nr; n++) {
ASSERT_NEAR(cpp_ropf[n], c_ropf[n], 1e-6);
}
}

TEST(ct, transport)
Expand All @@ -199,16 +162,19 @@ TEST(ct, transport)
int tran = trans_newDefault(thermo, 0);
ASSERT_GE(tran, 0);

double dkm[53];
int ret = trans_getMixDiffCoeffs(tran, 53, dkm);
size_t nsp = thermo_nSpecies(thermo);
vector<double> c_dkm(nsp);
int ret = trans_getMixDiffCoeffs(tran, 53, c_dkm.data());
ASSERT_EQ(ret, 0);

// // printf("\n Species Mix diff coeff\n");
// int k; // declare this here for C89 compatibility
// for (k = 0; k < nsp; k++) {
// thermo_getSpeciesName(thermo, k, 1000, buf);
// printf("%10s %8.6e\n", buf, dkm[k]);
// }
vector<double> cpp_dkm(nsp);
auto sol = newSolution("gri30.yaml", "gri30");
auto transport = sol->transport();
transport->getMixDiffCoeffs(cpp_dkm.data());

for (size_t n = 0; n < nsp; n++) {
ASSERT_NEAR(cpp_dkm[n], c_dkm[n], 1e-6);
}
}


Expand Down
Loading

0 comments on commit 6d632db

Please sign in to comment.