Skip to content

Commit

Permalink
Timing now includes the calculation of residal contribution to chemic…
Browse files Browse the repository at this point in the history
…al potential (SLOW!)
  • Loading branch information
ianhbell committed Sep 15, 2021
1 parent 19b8ad0 commit daf0075
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 61 deletions.
40 changes: 20 additions & 20 deletions mycp/dev/mixtures/mixture_binary_pairs.json
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@
"CAS2": "74-98-6",
"F": 1.0,
"Name1": "Methane",
"Name2": "Propane",
"Name2": "n-Propane",
"betaT": 0.989680305,
"betaV": 1.00482707,
"function": "Methane-Propane",
Expand Down Expand Up @@ -613,7 +613,7 @@
"CAS2": "74-98-6",
"F": 0,
"Name1": "Nitrogen",
"Name2": "Propane",
"Name2": "n-Propane",
"betaT": 1.002677329,
"betaV": 0.974424681,
"gammaT": 1.201264026,
Expand Down Expand Up @@ -781,7 +781,7 @@
"CAS2": "74-98-6",
"F": 0,
"Name1": "CarbonDioxide",
"Name2": "Propane",
"Name2": "n-Propane",
"betaT": 1.033620538,
"betaV": 0.996898004,
"gammaT": 0.908772477,
Expand Down Expand Up @@ -937,7 +937,7 @@
"CAS2": "74-98-6",
"F": 0.13042476515,
"Name1": "Ethane",
"Name2": "Propane",
"Name2": "n-Propane",
"betaT": 0.996199694,
"betaV": 0.997607277,
"function": "GeneralizedAlkane",
Expand Down Expand Up @@ -1143,7 +1143,7 @@
"CAS1": "74-98-6",
"CAS2": "106-97-8",
"F": 0.0312572600489,
"Name1": "Propane",
"Name1": "n-Propane",
"Name2": "n-Butane",
"betaT": 1.000310289,
"betaV": 0.999795868,
Expand All @@ -1156,7 +1156,7 @@
"CAS1": "74-98-6",
"CAS2": "75-28-5",
"F": -0.0551609771024,
"Name1": "Propane",
"Name1": "n-Propane",
"Name2": "IsoButane",
"betaT": 0.998012298,
"betaV": 0.999243146,
Expand All @@ -1169,7 +1169,7 @@
"CAS1": "74-98-6",
"CAS2": "109-66-0",
"F": 0,
"Name1": "Propane",
"Name1": "n-Propane",
"Name2": "n-Pentane",
"betaT": 0.996484021,
"betaV": 1.044919431,
Expand All @@ -1181,7 +1181,7 @@
"CAS1": "74-98-6",
"CAS2": "78-78-4",
"F": 0,
"Name1": "Propane",
"Name1": "n-Propane",
"Name2": "Isopentane",
"betaT": 0.994364425,
"betaV": 1.040459289,
Expand All @@ -1193,7 +1193,7 @@
"CAS1": "74-98-6",
"CAS2": "110-54-3",
"F": 0,
"Name1": "Propane",
"Name1": "n-Propane",
"Name2": "n-Hexane",
"betaT": 1.0,
"betaV": 1.0,
Expand All @@ -1205,7 +1205,7 @@
"CAS1": "74-98-6",
"CAS2": "142-82-5",
"F": 0,
"Name1": "Propane",
"Name1": "n-Propane",
"Name2": "n-Heptane",
"betaT": 1.0,
"betaV": 1.0,
Expand All @@ -1217,7 +1217,7 @@
"CAS1": "74-98-6",
"CAS2": "111-65-9",
"F": 0,
"Name1": "Propane",
"Name1": "n-Propane",
"Name2": "n-Octane",
"betaT": 1.0,
"betaV": 1.0,
Expand All @@ -1229,7 +1229,7 @@
"CAS1": "74-98-6",
"CAS2": "111-84-2",
"F": 0,
"Name1": "Propane",
"Name1": "n-Propane",
"Name2": "n-Nonane",
"betaT": 1.0,
"betaV": 1.0,
Expand All @@ -1241,7 +1241,7 @@
"CAS1": "74-98-6",
"CAS2": "124-18-5",
"F": 0,
"Name1": "Propane",
"Name1": "n-Propane",
"Name2": "n-Decane",
"betaT": 0.985331233,
"betaV": 0.984104227,
Expand All @@ -1253,7 +1253,7 @@
"CAS1": "74-98-6",
"CAS2": "1333-74-0",
"F": 0,
"Name1": "Propane",
"Name1": "n-Propane",
"Name2": "Hydrogen",
"betaT": 1.0,
"betaV": 1.0,
Expand All @@ -1265,7 +1265,7 @@
"CAS1": "74-98-6",
"CAS2": "7782-44-7",
"F": 0,
"Name1": "Propane",
"Name1": "n-Propane",
"Name2": "Oxygen",
"betaT": 1.0,
"betaV": 1.0,
Expand All @@ -1277,7 +1277,7 @@
"CAS1": "74-98-6",
"CAS2": "630-08-0",
"F": 0,
"Name1": "Propane",
"Name1": "n-Propane",
"Name2": "CarbonMonoxide",
"betaT": 1.0,
"betaV": 1.0,
Expand All @@ -1289,7 +1289,7 @@
"CAS1": "74-98-6",
"CAS2": "7732-18-5",
"F": 0,
"Name1": "Propane",
"Name1": "n-Propane",
"Name2": "Water",
"betaT": 1.0,
"betaV": 1.0,
Expand All @@ -1301,7 +1301,7 @@
"CAS1": "74-98-6",
"CAS2": "7783-06-4",
"F": 0,
"Name1": "Propane",
"Name1": "n-Propane",
"Name2": "HydrogenSulfide",
"betaT": 0.992573556,
"betaV": 0.936811219,
Expand All @@ -1313,7 +1313,7 @@
"CAS1": "74-98-6",
"CAS2": "7440-59-7",
"F": 0,
"Name1": "Propane",
"Name1": "n-Propane",
"Name2": "Helium",
"betaT": 1.0,
"betaV": 1.0,
Expand All @@ -1325,7 +1325,7 @@
"CAS1": "74-98-6",
"CAS2": "7440-37-1",
"F": 0,
"Name1": "Propane",
"Name1": "n-Propane",
"Name2": "Argon",
"betaT": 1.0,
"betaV": 1.0,
Expand Down
141 changes: 100 additions & 41 deletions src/time_REFPROP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,37 @@ struct OneTiming {
double value, sec_per_call;
};

constexpr int repeatmax = 100;
enum class obtainablethings { PHIX, CHEMPOT };

template<typename Taus, typename Deltas>
auto some_REFPROP(int itau, int idelta, Taus &taus, Deltas &deltas) {
auto some_REFPROP(obtainablethings thing, int itau, int idelta, Taus& taus, Deltas& deltas) {
std::vector<OneTiming> o;
double z[20] = { 1.0 };
for (auto repeat = 0; repeat < 100; ++repeat) {
std::valarray<double> ps = 0.0 * taus;
double Arterm = -10000;
auto tic = std::chrono::high_resolution_clock::now();
for (auto i = 0; i < taus.size(); ++i) {
PHIXdll(itau, idelta, taus[i], deltas[i], z, Arterm); ps[i] = Arterm;

if (thing == obtainablethings::PHIX) {
double z[20] = { 1.0 };
for (auto repeat = 0; repeat < repeatmax; ++repeat) {
std::valarray<double> ps = 0.0 * taus;
double Arterm = -10000;
auto tic = std::chrono::high_resolution_clock::now();
for (auto i = 0; i < taus.size(); ++i) {
PHIXdll(itau, idelta, taus[i], deltas[i], z, Arterm); ps[i] = Arterm;
}
auto toc = std::chrono::high_resolution_clock::now();
double elap_us = std::chrono::duration<double>(toc - tic).count() / taus.size() * 1e6;
double val = std::accumulate(std::begin(ps), std::end(ps), 0.0) / ps.size();
OneTiming result = { val, elap_us };
o.emplace_back(result);
}
auto toc = std::chrono::high_resolution_clock::now();
double elap_us = std::chrono::duration<double>(toc - tic).count() / taus.size() * 1e6;
double val = std::accumulate(std::begin(ps), std::end(ps), 0.0) / ps.size();
OneTiming result = { val, elap_us };
o.emplace_back(result);
}
else {

}
return o;
}

template<int itau, int idelta, typename Taus, typename Deltas, typename TT, typename RHO, typename Model>
auto some_teqp(const Taus& taus, const Deltas& deltas, const Model &model, const TT &Ts, const RHO &rhos) {
auto some_teqp(obtainablethings thing, const Taus& taus, const Deltas& deltas, const Model &model, const TT &Ts, const RHO &rhos) {
std::vector<OneTiming> out;

// And the same example with teqp
Expand All @@ -49,33 +58,37 @@ auto some_teqp(const Taus& taus, const Deltas& deltas, const Model &model, const

using tdx = TDXDerivatives<Model, double, decltype(c)>;

for (auto counter = 0; counter < 100; ++counter)
{
double o = 0.0;
auto tic = std::chrono::high_resolution_clock::now();
for (auto j = 0; j < N; ++j) {
if constexpr (itau == 0 && idelta == 0) {
o += tdx::get_Ar00(model, Ts[j], rhos[j], c);
}
else if constexpr (itau == 0 && idelta == 1) {
o += tdx::get_Ar01(model, Ts[j], rhos[j], c);
//o += tdx::get_Ar0n<1>(model, Ts[j], rhos[j], c)[1];
}
else if constexpr (itau == 0 && idelta > 1) {
o += tdx::get_Ar0n<idelta>(model, Ts[j], rhos[j], c)[idelta];
if (thing == obtainablethings::PHIX) {
for (auto repeat = 0; repeat < repeatmax; ++repeat)
{
double o = 0.0;
auto tic = std::chrono::high_resolution_clock::now();
for (auto j = 0; j < N; ++j) {
if constexpr (itau == 0 && idelta == 0) {
o += tdx::get_Ar00(model, Ts[j], rhos[j], c);
}
else if constexpr (itau == 0 && idelta == 1) {
o += tdx::get_Ar01(model, Ts[j], rhos[j], c);
//o += tdx::get_Ar0n<1>(model, Ts[j], rhos[j], c)[1];
}
else if constexpr (itau == 0 && idelta > 1) {
o += tdx::get_Ar0n<idelta>(model, Ts[j], rhos[j], c)[idelta];
}
}
auto toc = std::chrono::high_resolution_clock::now();
double elap_us = std::chrono::duration<double>(toc - tic).count() / taus.size() * 1e6;
double val = o / N;
OneTiming result = { val, elap_us };
out.emplace_back(result);
}
auto toc = std::chrono::high_resolution_clock::now();
double elap_us = std::chrono::duration<double>(toc - tic).count() / taus.size() * 1e6;
double val = o / N;
OneTiming result = { val, elap_us };
out.emplace_back(result);
}
else {
}
return out;
}

template<int itau, int idelta, typename Taus, typename Deltas, typename TT, typename RHO, typename Model>
auto one_deriv(Taus& taus, Deltas& deltas, const Model& model, TT& Ts, RHO& rhos) {
auto one_deriv(obtainablethings thing, Taus& taus, Deltas& deltas, const Model& model, TT& Ts, RHO& rhos) {

auto check_values = [](auto res) {
Eigen::ArrayXd vals(res.size());
Expand All @@ -86,8 +99,8 @@ auto one_deriv(Taus& taus, Deltas& deltas, const Model& model, TT& Ts, RHO& rhos
return vals.mean();
};

auto timingREFPROP = some_REFPROP(itau, idelta, taus, deltas);
auto timingteqp = some_teqp<itau, idelta>(taus, deltas, model, Ts, rhos);
auto timingREFPROP = some_REFPROP(thing, itau, idelta, taus, deltas);
auto timingteqp = some_teqp<itau, idelta>(thing, taus, deltas, model, Ts, rhos);

std::cout << "Values:" << check_values(timingREFPROP) << ", " << check_values(timingteqp) << std::endl;

Expand All @@ -108,8 +121,9 @@ int main()
std::string err;
bool loaded_REFPROP = load_REFPROP(err, path, DLL_name);
printf("Loaded refprop: %s @ address %zu\n", loaded_REFPROP ? "true" : "false", REFPROP_address());
if (!loaded_REFPROP){return EXIT_FAILURE; }
if (!loaded_REFPROP) { return EXIT_FAILURE; }
SETPATHdll(const_cast<char*>(path.c_str()), 400);

int ierr = 0, nc = 1;
char herr[255], hfld[10000] = "PROPANE", hhmx[255] = "HMX.BNC", href[4] = "DEF";
SETUPdll(nc, hfld, hhmx, href, ierr, herr, 10000, 255, 3, 255);
Expand All @@ -120,6 +134,49 @@ int main()
std::cout << kFlag << std::endl;
}

{
auto model = build_multifluid_model({ "Methane","Ethane","n-Propane","n-Butane"}, "../mycp", "../mycp/dev/mixtures/mixture_binary_pairs.json");
auto tic = std::chrono::high_resolution_clock::now();
using id = IsochoricDerivatives<decltype(model), double>;
double T = 300;
int N = 1000;
auto rhovec = (Eigen::ArrayXd(4) << 0.1, 0.2, 0.3, 0.4).finished();
for (auto j = 0; j < N; ++j) {
auto val = id::build_d2PsirdTdrhoi_autodiff(model, T, rhovec);
}
auto toc = std::chrono::high_resolution_clock::now();
double elap_us = std::chrono::duration<double>(toc - tic).count()/N*1e6;
std::cout << elap_us << " us/call for temperature derivative of residual part of chemical potential" << std::endl;
}
{
auto model = build_multifluid_model({ "Methane","Ethane","n-Propane","n-Butane" }, "../mycp", "../mycp/dev/mixtures/mixture_binary_pairs.json");
auto tic = std::chrono::high_resolution_clock::now();
using id = IsochoricDerivatives<decltype(model), double>;
double T = 300;
int N = 1000;
auto rhovec = (Eigen::ArrayXd(4) << 0.1, 0.2, 0.3, 0.4).finished();
for (auto j = 0; j < N; ++j) {
auto val = id::build_Psir_gradient_autodiff(model, T, rhovec);
}
auto toc = std::chrono::high_resolution_clock::now();
double elap_us = std::chrono::duration<double>(toc - tic).count() / N * 1e6;
std::cout << elap_us << " us/call for residual part of chemical potential" << std::endl;
}
{
auto model = build_multifluid_model({ "Methane" }, "../mycp", "../mycp/dev/mixtures/mixture_binary_pairs.json");
auto tic = std::chrono::high_resolution_clock::now();
using id = IsochoricDerivatives<decltype(model), double>;
double T = 300;
int N = 1000;
auto rhovec = (Eigen::ArrayXd(1) << 1.0).finished();
for (auto j = 0; j < N; ++j) {
auto val = id::build_Psir_gradient_autodiff(model, T, rhovec);
}
auto toc = std::chrono::high_resolution_clock::now();
double elap_us = std::chrono::duration<double>(toc - tic).count() / N * 1e6;
std::cout << elap_us << " us/call for residual part of chemical potential" << std::endl;
}

if (ierr != 0) printf("This ierr: %d herr: %s\n", ierr, herr);
{
auto model = build_multifluid_model({ "n-Propane" }, "../mycp", "../mycp/dev/mixtures/mixture_binary_pairs.json");
Expand All @@ -141,10 +198,12 @@ int main()

auto Ts = Tc / taus;
auto rhos = deltas * rhoc;
one_deriv<0, 0>(taus, deltas, model, Ts, rhos);
one_deriv<0, 1>(taus, deltas, model, Ts, rhos);
one_deriv<0, 2>(taus, deltas, model, Ts, rhos);
one_deriv<0, 3>(taus, deltas, model, Ts, rhos);

obtainablethings thing = obtainablethings::PHIX;
one_deriv<0, 0>(thing, taus, deltas, model, Ts, rhos);
one_deriv<0, 1>(thing, taus, deltas, model, Ts, rhos);
one_deriv<0, 2>(thing, taus, deltas, model, Ts, rhos);
one_deriv<0, 3>(thing, taus, deltas, model, Ts, rhos);
}
return EXIT_SUCCESS;
}

0 comments on commit daf0075

Please sign in to comment.