From 348d50c6f3e08f40ccadeb4b9879ad1a87844693 Mon Sep 17 00:00:00 2001 From: kanschat Date: Thu, 2 Aug 2007 15:12:17 +0000 Subject: [PATCH] Test for FunctionDerivative git-svn-id: https://svn.dealii.org/trunk@14882 0785d39b-7218-0410-832d-ea1e28bc413d --- tests/base/Makefile | 1 + tests/base/function_derivative.cc | 167 +++++++++++++++++++++ tests/base/function_derivative/cmp/generic | 6 + 3 files changed, 174 insertions(+) create mode 100644 tests/base/function_derivative.cc create mode 100644 tests/base/function_derivative/cmp/generic diff --git a/tests/base/Makefile b/tests/base/Makefile index f06a031d06..a9ed4112e9 100644 --- a/tests/base/Makefile +++ b/tests/base/Makefile @@ -47,6 +47,7 @@ tests_x = geometry_info_* \ data_out_base_povray \ data_out_base_tecplot \ data_out_base_vtk \ + function_* \ functions_* \ function_parser \ utilities_* \ diff --git a/tests/base/function_derivative.cc b/tests/base/function_derivative.cc new file mode 100644 index 0000000000..3bf25649ce --- /dev/null +++ b/tests/base/function_derivative.cc @@ -0,0 +1,167 @@ +//----------------------------------------------------------------------------- +// $Id$ +// Version: $Name$ +// +// Copyright (C) 2007 by the deal.II authors +// +// This file is subject to QPL and may not be distributed +// without copyright and license information. Please refer +// to the file deal.II/doc/license.html for the text and +// further information on this license. +// +//----------------------------------------------------------------------------- + +// Check automatic differentiation + +#include "../tests.h" +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include + + +template +void +check_derivative_order(const std::vector >& gradients, + FunctionDerivative& df, + const Quadrature& quadrature, + const unsigned int direction, + const double order) +{ + std::vector derivatives(quadrature.n_quadrature_points); + std::vector differences(quadrature.n_quadrature_points); + + // Compute derivatives with one + // step size and store errors + df.set_h(1.e-2); + df.value_list(quadrature.get_points(), derivatives); + for (unsigned int i=0;i 1.2 * expected || reduction < .8 * expected) + deallog << "Derivative error " << direction + << ' ' << order + << ' ' << i + << " " << reduction << std::endl; + } +} + + +template +void +check_hessian_order(const std::vector& values, + FunctionDerivative& df, + const Quadrature& quadrature, + const Point& k, + const unsigned int direction, + const double order) +{ + std::vector > derivatives(quadrature.n_quadrature_points); + std::vector > differences(quadrature.n_quadrature_points); + + const double h = (order < 3) ? 1.e-2 : 1.e-1 ; + // Compute derivatives with one + // step size and store errors + df.set_h(h); + df.gradient_list(quadrature.get_points(), derivatives); + for (unsigned int i=0;i 1.2 * expected || reduction < .8 * expected) + deallog << "Hessian error " << direction << ' ' << d + << ' ' << order + << ' ' << i + << " " << reduction + << " " << expected + << " " << differences[i][d] + << std::endl; + } +} + + +template +void +check_sine(unsigned int nquad) +{ + QGauss quadrature(nquad); + + Point wave_vector; + for (unsigned int d=0;d f(wave_vector); + + std::vector values(quadrature.n_quadrature_points); + std::vector > gradients(quadrature.n_quadrature_points); + + f.value_list(quadrature.get_points(), values); + f.gradient_list(quadrature.get_points(), gradients); + + // Check derivatives in all directions + for (unsigned int d=0;d dir; + dir(d) = 1.; + deallog.push("Euler"); + FunctionDerivative df (f, dir, 1.e-4); + check_derivative_order(gradients, df, quadrature, d, 2); + check_hessian_order(values, df, quadrature, wave_vector, d, 2); + deallog.pop(); + deallog.push("UpwindEuler"); + df.set_formula(FunctionDerivative::UpwindEuler); + check_derivative_order(gradients, df, quadrature, d, 1); + check_hessian_order(values, df, quadrature, wave_vector, d, 1); + deallog.pop(); + deallog.push("FourthOrder"); + df.set_formula(FunctionDerivative::FourthOrder); + check_derivative_order(gradients, df, quadrature, d, 4); + check_hessian_order(values, df, quadrature, wave_vector, d, 4); + deallog.pop(); + } +} + + +int main() +{ + std::ofstream logfile("function_derivative/output"); + deallog.attach(logfile); + deallog.depth_console(0); + deallog.threshold_double(1.e-10); + + check_sine<2>(3); + check_sine<3>(3); +} + + diff --git a/tests/base/function_derivative/cmp/generic b/tests/base/function_derivative/cmp/generic new file mode 100644 index 0000000000..8005591d03 --- /dev/null +++ b/tests/base/function_derivative/cmp/generic @@ -0,0 +1,6 @@ + +DEAL::Direction 0 +DEAL::Direction 1 +DEAL::Direction 0 +DEAL::Direction 1 +DEAL::Direction 2 -- 2.39.5