diff --git a/dumux/material/components/tabulatedcomponent.hh b/dumux/material/components/tabulatedcomponent.hh index dbf4b99b4..b5ef866d6 100644 --- a/dumux/material/components/tabulatedcomponent.hh +++ b/dumux/material/components/tabulatedcomponent.hh @@ -100,7 +100,7 @@ public: assert(std::numeric_limits::has_quiet_NaN); Scalar NaN = std::numeric_limits::quiet_NaN(); - // fill the arrays + // fill the temperature-pressure arrays for (unsigned iT = 0; iT < nTemp_; ++ iT) { Scalar temperature = iT * (tempMax_ - tempMin_)/(nTemp_ - 1) + tempMin_; @@ -126,26 +126,6 @@ public: catch (NumericalProblem) { gasViscosity_[i] = NaN; }; }; - // calculate the minimum and maximum values for the gas - // densities - minGasDensity__[iT] = RawComponent::gasDensity(temperature, pgMin); - maxGasDensity__[iT] = RawComponent::gasDensity(temperature, pgMax); - - // fill the temperature, density gas arrays - for (unsigned iRho = 0; iRho < nDensity_; ++ iRho) { - Scalar density = - iRho * (maxGasDensity__[iT] - minGasDensity__[iT]) - / - (nDensity_ - 1) - + - minGasDensity__[iT]; - - unsigned i = iT + iRho*nTemp_; - - try { gasPressure_[i] = RawComponent::gasPressure(temperature, density); } - catch (NumericalProblem) { gasPressure_[i] = NaN; }; - }; - Scalar plMin = minLiquidPressure_(iT); Scalar plMax = maxLiquidPressure_(iT); for (unsigned iP = 0; iP < nPress_; ++ iP) { @@ -162,18 +142,47 @@ public: try { liquidViscosity_[i] = RawComponent::liquidViscosity(temperature, pressure); } catch (NumericalProblem) { liquidViscosity_[i] = NaN; }; } + } + + // fill the temperature-density arrays + for (unsigned iT = 0; iT < nTemp_; ++ iT) { + Scalar temperature = iT * (tempMax_ - tempMin_)/(nTemp_ - 1) + tempMin_; + + // calculate the minimum and maximum values for the gas + // densities + minGasDensity__[iT] = RawComponent::gasDensity(temperature, minGasPressure_(iT)); + if (iT < nTemp_ - 1) + maxGasDensity__[iT] = RawComponent::gasDensity(temperature, maxGasPressure_(iT + 1)); + else + maxGasDensity__[iT] = RawComponent::gasDensity(temperature, maxGasPressure_(iT)); + + // fill the temperature, density gas arrays + for (unsigned iRho = 0; iRho < nDensity_; ++ iRho) { + Scalar density = + Scalar(iRho)/(nDensity_ - 1) * + (maxGasDensity__[iT] - minGasDensity__[iT]) + + + minGasDensity__[iT]; + + unsigned i = iT + iRho*nTemp_; + + try { gasPressure_[i] = RawComponent::gasPressure(temperature, density); } + catch (NumericalProblem) { gasPressure_[i] = NaN; }; + }; // calculate the minimum and maximum values for the liquid // densities - minLiquidDensity__[iT] = RawComponent::liquidDensity(temperature, plMin); - maxLiquidDensity__[iT] = RawComponent::liquidDensity(temperature, plMax); + minLiquidDensity__[iT] = RawComponent::liquidDensity(temperature, minLiquidPressure_(iT)); + if (iT < nTemp_ - 1) + maxLiquidDensity__[iT] = RawComponent::liquidDensity(temperature, maxLiquidPressure_(iT + 1)); + else + maxLiquidDensity__[iT] = RawComponent::liquidDensity(temperature, maxLiquidPressure_(iT)); // fill the temperature, density liquid arrays for (unsigned iRho = 0; iRho < nDensity_; ++ iRho) { Scalar density = - iRho * (maxLiquidDensity__[iT] - minLiquidDensity__[iT]) - / - (nDensity_ - 1) + Scalar(iRho)/(nDensity_ - 1) * + (maxLiquidDensity__[iT] - minLiquidDensity__[iT]) + minLiquidDensity__[iT]; @@ -306,7 +315,7 @@ public: */ static Scalar gasPressure(Scalar temperature, Scalar density) { - Scalar result = interpolateGasTRho_(liquidPressure_, + Scalar result = interpolateGasTRho_(gasPressure_, temperature, density); if (std::isnan(result)) { @@ -454,14 +463,14 @@ private: return std::numeric_limits::quiet_NaN(); } - unsigned iT = std::max(0, std::min(nTemp_ - 2, (int) alphaT)); + unsigned iT = std::max(0, std::min(nTemp_ - 2, (long long) alphaT)); alphaT -= iT; Scalar alphaP1 = pressLiquidIdx_(p, iT); Scalar alphaP2 = pressLiquidIdx_(p, iT + 1); - unsigned iP1 = std::max(0, std::min(nPress_ - 2, (int) alphaP1)); - unsigned iP2 = std::max(0, std::min(nPress_ - 2, (int) alphaP2)); + unsigned iP1 = std::max(0, std::min(nPress_ - 2, (long long) alphaP1)); + unsigned iP2 = std::max(0, std::min(nPress_ - 2, (long long) alphaP2)); alphaP1 -= iP1; alphaP2 -= iP2; @@ -482,13 +491,13 @@ private: return std::numeric_limits::quiet_NaN(); } - unsigned iT = std::max(0, std::min(nTemp_ - 2, (int) alphaT)); + unsigned iT = std::max(0, std::min(nTemp_ - 2, (long long) alphaT)); alphaT -= iT; Scalar alphaP1 = pressGasIdx_(p, iT); Scalar alphaP2 = pressGasIdx_(p, iT + 1); - unsigned iP1 = std::max(0, std::min(nPress_ - 2, (int) alphaP1)); - unsigned iP2 = std::max(0, std::min(nPress_ - 2, (int) alphaP2)); + unsigned iP1 = std::max(0, std::min(nPress_ - 2, (long long) alphaP1)); + unsigned iP2 = std::max(0, std::min(nPress_ - 2, (long long) alphaP2)); alphaP1 -= iP1; alphaP2 -= iP2; @@ -504,20 +513,14 @@ private: static Scalar interpolateGasTRho_(const Scalar *values, Scalar T, Scalar rho) { Scalar alphaT = tempIdx_(T); - if (alphaT < 0 || alphaT >= nTemp_ - 1) { - // std::cerr << __LINE__ << " T: " << T << "\n"; - return std::numeric_limits::quiet_NaN(); - } - - unsigned iT = (unsigned) alphaT; + unsigned iT = std::max(0, std::min(nTemp_ - 2, (long long) alphaT)); alphaT -= iT; Scalar alphaP1 = densityGasIdx_(rho, iT); Scalar alphaP2 = densityGasIdx_(rho, iT + 1); - - unsigned iP1 = std::min(nDensity_ - 2, (unsigned) alphaP1); + unsigned iP1 = std::max(0, std::min(nDensity_ - 2, (long long) alphaP1)); + unsigned iP2 = std::max(0, std::min(nDensity_ - 2, (long long) alphaP2)); alphaP1 -= iP1; - unsigned iP2 = std::min(nDensity_ - 2, (unsigned) alphaP2); alphaP2 -= iP2; return @@ -532,20 +535,14 @@ private: static Scalar interpolateLiquidTRho_(const Scalar *values, Scalar T, Scalar rho) { Scalar alphaT = tempIdx_(T); - if (alphaT < 0 || alphaT >= nTemp_ - 1) { - // std::cerr << __LINE__ << " T: " << T << "\n"; - return std::numeric_limits::quiet_NaN(); - } - - unsigned iT = (unsigned) alphaT; + unsigned iT = std::max(0, std::min(nTemp_ - 2, (long long) alphaT)); alphaT -= iT; Scalar alphaP1 = densityLiquidIdx_(rho, iT); Scalar alphaP2 = densityLiquidIdx_(rho, iT + 1); - - unsigned iP1 = std::min(nDensity_ - 2, (unsigned) alphaP1); + unsigned iP1 = std::max(0, std::min(nDensity_ - 2, (long long) alphaP1)); + unsigned iP2 = std::max(0, std::min(nDensity_ - 2, (long long) alphaP2)); alphaP1 -= iP1; - unsigned iP2 = std::min(nDensity_ - 2, (unsigned) alphaP2); alphaP2 -= iP2; return @@ -583,7 +580,7 @@ private: { Scalar densityMin = minLiquidDensity_(tempIdx); Scalar densityMax = maxLiquidDensity_(tempIdx); - return (nDensity_ - 1)*(density - densityMin)/(densityMax - densityMin); + return (nDensity_ - 1) * (density - densityMin)/(densityMax - densityMin); } // returns the index of an entry in a density field @@ -591,7 +588,7 @@ private: { Scalar densityMin = minGasDensity_(tempIdx); Scalar densityMax = maxGasDensity_(tempIdx); - return (nDensity_ - 1)*(density - densityMin)/(densityMax - densityMin); + return (nDensity_ - 1) * (density - densityMin)/(densityMax - densityMin); } // returns the minimum tabulized liquid pressure at a given diff --git a/test/material/tabulation/test_tabulation.cc b/test/material/tabulation/test_tabulation.cc index 4ad0efe4d..56ba0b785 100644 --- a/test/material/tabulation/test_tabulation.cc +++ b/test/material/tabulation/test_tabulation.cc @@ -27,12 +27,15 @@ #include #include +bool success; + template -void isSame(Scalar v, Scalar vRef, Scalar tol=5e-4) +void isSame(const char *str, Scalar v, Scalar vRef, Scalar tol=5e-4) { if (std::abs( (v - vRef)/vRef ) > tol) { - std::cout << "\nerror: " << (v - vRef)/vRef << "\n"; - exit(1); + std::cout << "error for \"" << str << "\": " << (v - vRef)/vRef*100 << "% difference\n"; + success = false; + //exit(1); } } @@ -48,14 +51,14 @@ int main() Scalar pMin = 10.00; Scalar pMax = IapwsH2O::vaporPressure(tempMax*1.1); - int nPress = 600; + 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) { @@ -66,7 +69,8 @@ int main() std::cout.flush(); } - isSame(TabulatedH2O::vaporPressure(T), + isSame("vaporPressure", + TabulatedH2O::vaporPressure(T), IapwsH2O::vaporPressure(T), 1e-3); for (int j = 0; j < n; ++j) { @@ -75,23 +79,34 @@ int main() Scalar tol = 5e-4; if (p > IapwsH2O::vaporPressure(T)) tol = 5e-2; - isSame(TabulatedH2O::gasEnthalpy(T,p), IapwsH2O::gasEnthalpy(T,p), tol); - isSame(TabulatedH2O::gasInternalEnergy(T,p), IapwsH2O::gasInternalEnergy(T,p), tol); - isSame(TabulatedH2O::gasDensity(T,p), IapwsH2O::gasDensity(T,p), tol); - isSame(TabulatedH2O::gasViscosity(T,p), IapwsH2O::gasViscosity(T,p), tol); + Scalar rho = IapwsH2O::gasDensity(T,p); + //std::cerr << T << " " << p << " " << IapwsH2O::gasPressure(T,rho) << " " << TabulatedH2O::gasPressure(T,rho) << "\n"; + 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); } if (p > IapwsH2O::vaporPressure(T) / 1.03) { Scalar tol = 5e-4; if (p < IapwsH2O::vaporPressure(T)) tol = 5e-2; - isSame(TabulatedH2O::liquidEnthalpy(T,p), IapwsH2O::liquidEnthalpy(T,p), tol); - isSame(TabulatedH2O::liquidInternalEnergy(T,p), IapwsH2O::liquidInternalEnergy(T,p), tol); - isSame(TabulatedH2O::liquidDensity(T,p), IapwsH2O::liquidDensity(T,p), tol); - isSame(TabulatedH2O::liquidViscosity(T,p), IapwsH2O::liquidViscosity(T,p), tol); + Scalar rho = IapwsH2O::liquidDensity(T,p); + //std::cerr << T << " " << p << " " << IapwsH2O::liquidPressure(T,rho) << " " << TabulatedH2O::liquidPressure(T,rho) << "\n"; + isSame("Iapws::gasPressure", 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"; } - std::cout << "\nsuccess\n"; + + if (success) + std::cout << "\nsuccess\n"; return 0; }