Skip to content
Snippets Groups Projects
main.cpp 2.86 KiB
Newer Older
  • Learn to ignore specific revisions
  • Ian Bell's avatar
    Ian Bell committed
    #include <iostream>
    #include <algorithm>
    #include <numeric>
    #include <vector>
    #include <valarray>
    #include <functional>
    #include <complex>
    #include <chrono>
    #include <optional>
    
    #include "teqp/core.hpp"
    
    void test_vdW() {
    
    Ian Bell's avatar
    Ian Bell committed
        volatile double T = 298.15;
        auto rho = 3.0;
        auto R = get_R_gas<double>();
    
    Ian Bell's avatar
    Ian Bell committed
    
    
    Ian Bell's avatar
    Ian Bell committed
        double Omega_b = 1.0 / 8, Omega_a = 27.0 / 64;
        double Tcrit = 150.687, pcrit = 4863000.0; // Argon
        double b = Omega_b * R * Tcrit / pcrit;
        double ba = Omega_b / Omega_a / Tcrit / R;
        double a = b / ba;
    
    Ian Bell's avatar
    Ian Bell committed
    
    
    Ian Bell's avatar
    Ian Bell committed
        auto vdW = vdWEOS1(a, b);
    
    Ian Bell's avatar
    Ian Bell committed
    
    
    Ian Bell's avatar
    Ian Bell committed
        auto t2 = std::chrono::steady_clock::now();
        volatile auto pp = vdW.p(T, 1 / rho);
        auto t3 = std::chrono::steady_clock::now();
        std::cout << std::chrono::duration<double>(t3 - t2).count() << " from p(T,v)" << std::endl;
    
    Ian Bell's avatar
    Ian Bell committed
    
    
    Ian Bell's avatar
    Ian Bell committed
        const std::valarray<double> rhovec = { rho, 0.0 };
    
    Ian Bell's avatar
    Ian Bell committed
    
    
    Ian Bell's avatar
    Ian Bell committed
        auto t21 = std::chrono::steady_clock::now();
        
        auto Psir = vdW.Psir(T, rhovec);
        auto dPsirdrho0 = rhovec[0] * deriv2([&vdW](const auto& T, const auto& rhovec) { return vdW.Psir(T, rhovec); }, T, rhovec);
        auto dPsirdrho1 = rhovec[1] * deriv3([&vdW](const auto& T, const auto& rhovec) { return vdW.Psir(T, rhovec); }, T, rhovec);
        auto pfromderiv = rho * R * T - Psir + dPsirdrho0 + dPsirdrho1;
    
    Ian Bell's avatar
    Ian Bell committed
    
    
    Ian Bell's avatar
    Ian Bell committed
        auto t31 = std::chrono::steady_clock::now();
        std::cout << std::chrono::duration<double>(t31 - t21).count() << " from isochoric" << std::endl;
        std::cout << pfromderiv / pp - 1 << std::endl;
    
    Ian Bell's avatar
    Ian Bell committed
    }
    
    void test_vdwMix() {
    
    Ian Bell's avatar
    Ian Bell committed
        // Argon + Xenon
        std::valarray<double> Tc_K = { 150.687, 289.733 };
        std::valarray<double> pc_Pa = { 4863000.0, 5842000.0 };
        vdWEOS<double> vdW(Tc_K, pc_Pa);
    
    Ian Bell's avatar
    Ian Bell committed
    
    
    Ian Bell's avatar
    Ian Bell committed
        double T = 298.15;
        auto rho = 3.0;
        auto R = get_R_gas<double>();
        auto rhotot = rho;
    
    Ian Bell's avatar
    Ian Bell committed
    
    
    Ian Bell's avatar
    Ian Bell committed
        const std::valarray<double> rhovec = { rho/2, rho/2 };
    
    Ian Bell's avatar
    Ian Bell committed
    
    
    Ian Bell's avatar
    Ian Bell committed
        auto t2 = std::chrono::steady_clock::now();
    
    Ian Bell's avatar
    Ian Bell committed
    
    
    Ian Bell's avatar
    Ian Bell committed
        auto Psir = vdW.Psir(T, rhovec);
        auto dPsirdrho0 = rhovec[0] * deriv2([&vdW, rhotot](const auto& T, const auto& rhovec) { return vdW.Psir(T, rhovec); }, T, rhovec);
        auto dPsirdrho1 = rhovec[1] * deriv3([&vdW, rhotot](const auto& T, const auto& rhovec) { return vdW.Psir(T, rhovec); }, T, rhovec);
        auto pfromderiv = rho*R*T - Psir + dPsirdrho0 + dPsirdrho1;
        {
            auto term0 = rhovec[0] * deriv2([&vdW, rhotot](const auto& T, const auto& rhovec) { return vdW.alphar(T, rhovec); }, T, rhovec);
            auto term1 = rhovec[1] * deriv3([&vdW, rhotot](const auto& T, const auto& rhovec) { return vdW.alphar(T, rhovec); }, T, rhovec);
            auto pr = (term0 + term1)*rhotot*R*T;
            auto pfromderiv2 = rho*R*T + pr;
            auto err2 = pfromderiv / pfromderiv2 - 1;
            int err = 0;
        }
    
    Ian Bell's avatar
    Ian Bell committed
    
    
    Ian Bell's avatar
    Ian Bell committed
        auto t3 = std::chrono::steady_clock::now();
        std::cout << std::chrono::duration<double>(t3 - t2).count() << " from isochoric (mix) " << std::endl;
    
    Ian Bell's avatar
    Ian Bell committed
    }
    
    int main(){
    
    Ian Bell's avatar
    Ian Bell committed
        test_vdW();
        test_vdwMix();
        return EXIT_SUCCESS;
    
    Ian Bell's avatar
    Ian Bell committed
    }