void test2DFunction2(AdFn* adFn, ClassicFn* classicFn, Scalar x, Scalar yMin, Scalar yMax) { typedef Opm::LocalAd::Evaluation<Scalar, numVars> Eval; int n = 100*1000; for (int i = 0; i < n; ++ i) { Scalar y = Scalar(i)/(n - 1)*(yMax - yMin) + yMin; const auto& xEval = Eval::createConstant(x); const auto& yEval = Eval::createVariable(y, 1); const Eval& zEval = adFn(xEval, yEval); Scalar eps = std::numeric_limits<Scalar>::epsilon()*1e7; eps = std::max(eps, eps*std::abs(y)); Scalar z = classicFn(x, y); Scalar zStar1 = classicFn(x, y - eps); Scalar zStar2 = classicFn(x, y + eps); Scalar zPrime = (zStar2 - zStar1)/(2*eps); if (std::abs(z - zEval.value)/std::abs(z + zEval.value) > std::numeric_limits<Scalar>::epsilon()*1e2) throw std::logic_error("oops: value"); Scalar deltaAbs = std::abs(zPrime - zEval.derivatives[1]); Scalar deltaRel = std::abs(deltaAbs/zPrime); if (deltaAbs > 1000*eps && deltaRel > 1000*eps) throw std::logic_error("oops: derivative @"+std::to_string((long double) x)+": " + std::to_string((long double) zPrime) + " vs " + std::to_string((long double) zEval.derivatives[1]) + " delta: " + std::to_string((long double) std::abs(zPrime - zEval.derivatives[1]))); } }
void test2DFunction2(AdFn* adFn, ClassicFn* classicFn, Scalar x, Scalar yMin, Scalar yMax) { typedef Opm::LocalAd::Evaluation<Scalar, VariablesDescriptor, VariablesDescriptor::size> Eval; int n = 100*1000; for (int i = 0; i < n; ++ i) { Scalar y = Scalar(i)/(n - 1)*(yMax - yMin) + yMin; const auto& xEval = Eval::createConstant(x); const auto& yEval = Eval::createVariable(y, 1); const Eval& zEval = adFn(xEval, yEval); const Scalar eps = 1e-10; Scalar z = classicFn(x, y); Scalar zStar1 = classicFn(x, y - eps); Scalar zStar2 = classicFn(x, y + eps); Scalar zPrime = (zStar2 - zStar1)/(2*eps); if (z != zEval.value) throw std::logic_error("oops: value"); Scalar deltaAbs = std::abs(zPrime - zEval.derivatives[1]); Scalar deltaRel = std::abs(deltaAbs/zPrime); if (deltaAbs > 1e-3 && deltaRel > 1e-3) throw std::logic_error("oops: derivative @"+std::to_string((long double) x)+": " + std::to_string((long double) zPrime) + " vs " + std::to_string((long double) zEval.derivatives[1]) + " delta: " + std::to_string((long double) std::abs(zPrime - zEval.derivatives[1]))); } }
void test1DFunction(AdFn* adFn, ClassicFn* classicFn, Scalar xMin = 1e-6, Scalar xMax = 1000) { typedef Opm::LocalAd::Evaluation<Scalar, numVars> Eval; int n = 100*1000; for (int i = 0; i < n; ++ i) { Scalar x = Scalar(i)/(n - 1)*(xMax - xMin) + xMin; const auto& xEval = Eval::createVariable(x, 0); const Eval& yEval = adFn(xEval); Scalar eps = std::numeric_limits<Scalar>::epsilon()*1e7; eps = std::max(eps, eps*std::abs(x)); Scalar y = classicFn(x); Scalar yStar1 = classicFn(x - eps); Scalar yStar2 = classicFn(x + eps); Scalar yPrime = (yStar2 - yStar1)/(2*eps); if (std::abs(y-yEval.value) > 5e-14) throw std::logic_error("oops: value"); Scalar deltaAbs = std::abs(yPrime - yEval.derivatives[0]); Scalar deltaRel = std::abs(deltaAbs/yPrime); if (deltaAbs > 1000*eps && deltaRel > 1000*eps) throw std::logic_error("oops: derivative @"+std::to_string((long double) x)+": " + std::to_string((long double) yPrime) + " vs " + std::to_string((long double) yEval.derivatives[0]) + " delta: " + std::to_string((long double) std::abs(yPrime - yEval.derivatives[0]))); } }
void test1DFunction(AdFn* adFn, ClassicFn* classicFn, Scalar xMin = 1e-6, Scalar xMax = 1000) { typedef Opm::LocalAd::Evaluation<Scalar, VariablesDescriptor, VariablesDescriptor::size> Eval; int n = 100*1000; for (int i = 0; i < n; ++ i) { Scalar x = Scalar(i)/(n - 1)*(xMax - xMin) + xMin; const auto& xEval = Eval::createVariable(x, 0); const Eval& yEval = adFn(xEval); const Scalar eps = 1e-10; Scalar y = classicFn(x); Scalar yStar1 = classicFn(x - eps); Scalar yStar2 = classicFn(x + eps); Scalar yPrime = (yStar2 - yStar1)/(2*eps); if (y != yEval.value) throw std::logic_error("oops: value"); Scalar deltaAbs = std::abs(yPrime - yEval.derivatives[0]); Scalar deltaRel = std::abs(deltaAbs/yPrime); if (deltaAbs > 1e-3 && deltaRel > 1e-3) throw std::logic_error("oops: derivative @"+std::to_string((long double) x)+": " + std::to_string((long double) yPrime) + " vs " + std::to_string((long double) yEval.derivatives[0]) + " delta: " + std::to_string((long double) std::abs(yPrime - yEval.derivatives[0]))); } }