Skip to content
Snippets Groups Projects

Improve tabulation for components and other functions

Merged Timo Koch requested to merge feature/improve-tabulated-component into master
@@ -28,87 +28,123 @@
#include <config.h>
#include <dune/common/float_cmp.hh>
#include <dumux/material/components/air.hh>
#include <dumux/material/components/benzene.hh>
#include <dumux/material/components/brine.hh>
#include <dumux/material/components/calcite.hh>
#include <dumux/material/components/calciumion.hh>
#include <dumux/material/components/cao.hh>
#include <dumux/material/components/cao2h2.hh>
#include <dumux/material/components/carbonateion.hh>
#include <dumux/material/components/ch4.hh>
#include <dumux/material/components/co2.hh>
#include <dumux/material/components/granite.hh>
#include <dumux/material/components/h2.hh>
#include <dumux/material/components/h2o.hh>
#include <dumux/material/components/heavyoil.hh>
#include <dumux/material/components/mesitylene.hh>
#include <dumux/material/components/n2.hh>
#include <dumux/material/components/nacl.hh>
#include <dumux/material/components/o2.hh>
#include <dumux/material/components/simpleh2o.hh>
#include <dumux/material/components/trichloroethene.hh>
#include <dumux/material/components/xylene.hh>
#include <dumux/material/components/tabulatedcomponent.hh>
#include <dumux/material/components/componenttraits.hh>
bool success;
template <class Scalar>
void isSame(const char *str, Scalar v, Scalar vRef, Scalar tol=1e-3)
template<class Scalar>
void checkEquality(const std::string& name, Scalar tab, Scalar real, Scalar eps)
{
using std::abs;
if (abs( (v - vRef)/vRef ) > tol) {
std::cout << "error for \"" << str << "\": " << (v - vRef)/vRef*100 << "% difference (tolerance: " << tol*100 << "%)\n";
success = false;
//exit(1);
}
if (!Dune::FloatCmp::eq(tab, real, eps))
DUNE_THROW(Dune::InvalidStateException, "Tabulated and computed value for "
<< name << " differs by more than " << eps*100 << "% "
<< "(tabulated: " << tab << ", " << "actual value: " << real << ")");
}
int main()
int main(int argc, char *argv[])
{
using namespace Dumux;
using Scalar = double;
using IapwsH2O = Dumux::Components::H2O<Scalar>;
using TabulatedH2O = Dumux::Components::TabulatedComponent<IapwsH2O>;
Scalar tempMin = 274.15;
Scalar tempMax = 622.15;
int nTemp = static_cast<int>(tempMax - tempMin) * 6/8;
Scalar pMin = 10.00;
Scalar pMax = IapwsH2O::vaporPressure(tempMax*1.1);
int nPress = 200;
std::cout << "Creating tabulation with " << nTemp*nPress << " entries per quantity\n";
TabulatedH2O::init(tempMin, tempMax, nTemp,
pMin, pMax, nPress);
std::cout << "Checking tabulation\n";
success = true;
int m = nTemp*3;
int n = nPress*3;
for (int i = 0; i < m; ++i) {
Scalar T = tempMin + (tempMax - tempMin)*Scalar(i)/m;
using std::max;
if (i % max(1, m/1000) == 0) {
std::cout << Scalar(i)/m*100 << "% done \r";
std::cout.flush();
}
isSame("vaporPressure",
TabulatedH2O::vaporPressure(T),
IapwsH2O::vaporPressure(T),
1e-3);
for (int j = 0; j < n; ++j) {
Scalar p = pMin + (pMax - pMin)*Scalar(j)/n;
if (p < IapwsH2O::vaporPressure(T) * 1.001) {
Scalar tol = 1e-3;
if (p > IapwsH2O::vaporPressure(T))
tol = 1e-2;
Scalar rho = IapwsH2O::gasDensity(T,p);
//isSame("Iapws::gasPressure", IapwsH2O::gasPressure(T,rho), p, 1e-6);
//isSame("gasPressure", TabulatedH2O::gasPressure(T,rho), p, 2e-2);
isSame("gasEnthalpy", TabulatedH2O::gasEnthalpy(T,p), IapwsH2O::gasEnthalpy(T,p), tol);
isSame("gasInternalEnergy", TabulatedH2O::gasInternalEnergy(T,p), IapwsH2O::gasInternalEnergy(T,p), tol);
isSame("gasDensity", TabulatedH2O::gasDensity(T,p), rho, tol);
isSame("gasViscosity", TabulatedH2O::gasViscosity(T,p), IapwsH2O::gasViscosity(T,p), tol);
}
// test IapwsH2O in detail
{
using IapwsH2O = Components::H2O<Scalar>;
using TabulatedH2O = Components::TabulatedComponent<IapwsH2O>;
// tabulation min/max
const Scalar tempMin = 274.15;
const Scalar tempMax = 622.15;
const int nTemp = 800;
const Scalar pMin = 1e4;
const Scalar pMax = IapwsH2O::vaporPressure(tempMax*1.1);
const int nPress = 200;
std::cout << "Creating tabulation with " << nTemp*nPress << " entries per quantity\n";
TabulatedH2O::init(tempMin, tempMax, nTemp, pMin, pMax, nPress);
constexpr Scalar eps = 1e-1;
std::cout << "Checking tabulation\n";
const int m = nTemp*3;
const int n = nPress*3;
for (int i = 0; i < m; ++i)
{
const Scalar T = tempMin + (tempMax - tempMin)*Scalar(i)/Scalar(m-1);
checkEquality("vaporPressure", TabulatedH2O::vaporPressure(T), IapwsH2O::vaporPressure(T), eps);
for (int j = 0; j < n; ++j)
{
const Scalar p = pMin + (pMax - pMin)*Scalar(j)/Scalar(n-1);
if (p < IapwsH2O::vaporPressure(T) * 1.001) {
Scalar rho = IapwsH2O::gasDensity(T,p);
checkEquality("Iapws::gasPressure", IapwsH2O::gasPressure(T,rho), p, eps);
checkEquality("gasPressure", TabulatedH2O::gasPressure(T,rho), p, eps);
checkEquality("gasEnthalpy", TabulatedH2O::gasEnthalpy(T,p), IapwsH2O::gasEnthalpy(T,p), eps);
checkEquality("gasInternalEnergy", TabulatedH2O::gasInternalEnergy(T,p), IapwsH2O::gasInternalEnergy(T,p), eps);
checkEquality("gasDensity", TabulatedH2O::gasDensity(T,p), rho, eps);
checkEquality("gasViscosity", TabulatedH2O::gasViscosity(T,p), IapwsH2O::gasViscosity(T,p), eps);
}
if (p > IapwsH2O::vaporPressure(T) / 1.001) {
Scalar rho = IapwsH2O::liquidDensity(T,p);
checkEquality("Iapws::liquidPressure", IapwsH2O::liquidPressure(T,rho), p, eps);
checkEquality("liquidPressure", TabulatedH2O::liquidPressure(T,rho), p, eps);
checkEquality("liquidEnthalpy", TabulatedH2O::liquidEnthalpy(T,p), IapwsH2O::liquidEnthalpy(T,p), eps);
checkEquality("liquidInternalEnergy", TabulatedH2O::liquidInternalEnergy(T,p), IapwsH2O::liquidInternalEnergy(T,p), eps);
checkEquality("liquidDensity", TabulatedH2O::liquidDensity(T,p), rho, eps);
checkEquality("liquidViscosity", TabulatedH2O::liquidViscosity(T,p), IapwsH2O::liquidViscosity(T,p), eps);
}
if (p > IapwsH2O::vaporPressure(T) / 1.001) {
Scalar tol = 1e-3;
if (p < IapwsH2O::vaporPressure(T))
tol = 1e-2;
Scalar rho = IapwsH2O::liquidDensity(T,p);
//isSame("Iapws::liquidPressure", IapwsH2O::liquidPressure(T,rho), p, 1e-6);
//isSame("liquidPressure", TabulatedH2O::liquidPressure(T,rho), p, 2e-2);
isSame("liquidEnthalpy", TabulatedH2O::liquidEnthalpy(T,p), IapwsH2O::liquidEnthalpy(T,p), tol);
isSame("liquidInternalEnergy", TabulatedH2O::liquidInternalEnergy(T,p), IapwsH2O::liquidInternalEnergy(T,p), tol);
isSame("liquidDensity", TabulatedH2O::liquidDensity(T,p), rho, tol);
isSame("liquidViscosity", TabulatedH2O::liquidViscosity(T,p), IapwsH2O::liquidViscosity(T,p), tol);
}
}
//std::cerr << "\n";
}
if (success)
std::cout << "\nsuccess\n";
// test if other components can be tabulated
{
Components::TabulatedComponent<Components::Air<Scalar>, false>::init(273, 275, 3, 1e5, 1e6, 3);
Components::TabulatedComponent<Components::Benzene<Scalar>, false>::init(273, 275, 3, 1e5, 1e6, 3);
Components::TabulatedComponent<Components::Calcite<Scalar>, false>::init(273, 275, 3, 1e5, 1e6, 3);
Components::TabulatedComponent<Components::CalciumIon<Scalar>, false>::init(273, 275, 3, 1e5, 1e6, 3);
Components::TabulatedComponent<Components::CaO<Scalar>, false>::init(273, 275, 3, 1e5, 1e6, 3);
Components::TabulatedComponent<Components::CaO2H2<Scalar>, false>::init(273, 275, 3, 1e5, 1e6, 3);
Components::TabulatedComponent<Components::CarbonateIon<Scalar>, false>::init(273, 275, 3, 1e5, 1e6, 3);
Components::TabulatedComponent<Components::CH4<Scalar>, false>::init(273, 275, 3, 1e5, 1e6, 3);
Components::TabulatedComponent<Components::Granite<Scalar>, false>::init(273, 275, 3, 1e5, 1e6, 3);
Components::TabulatedComponent<Components::H2O<Scalar>>::init(273, 275, 3, 1e5, 1e6, 3);
Components::TabulatedComponent<Components::HeavyOil<Scalar>>::init(273, 275, 3, 1e5, 1e6, 3);
Components::TabulatedComponent<Components::Mesitylene<Scalar>>::init(273, 275, 3, 1e5, 1e6, 3);
Components::TabulatedComponent<Components::N2<Scalar>>::init(273, 275, 3, 1e5, 1e6, 3);
Components::TabulatedComponent<Components::NaCl<Scalar>, false>::init(273, 275, 3, 1e5, 1e6, 3);
Components::TabulatedComponent<Components::O2<Scalar>>::init(273, 275, 3, 1e5, 1e6, 3);
Components::TabulatedComponent<Components::SimpleH2O<Scalar>>::init(273, 275, 3, 1e5, 1e6, 3);
Components::TabulatedComponent<Components::Trichloroethene<Scalar>>::init(273, 275, 3, 1e5, 1e6, 3);
Components::TabulatedComponent<Components::Xylene<Scalar>>::init(273, 275, 3, 1e5, 1e6, 3);
}
return 0;
}
Loading