From: Wolfgang Bangerth Date: Wed, 29 May 2019 02:40:08 +0000 (-0600) Subject: Add a test. X-Git-Tag: v9.2.0-rc1~1443^2~1 X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=3047ac6f324417193472b1a49aa64a73f9cbfa92;p=dealii.git Add a test. --- diff --git a/tests/base/functions_11_gradient.cc b/tests/base/functions_11_gradient.cc new file mode 100644 index 0000000000..476030415f --- /dev/null +++ b/tests/base/functions_11_gradient.cc @@ -0,0 +1,128 @@ +// --------------------------------------------------------------------- +// +// Copyright (C) 2013 - 2018 by the deal.II authors +// +// This file is part of the deal.II library. +// +// The deal.II library is free software; you can use it, redistribute +// it, and/or modify it under the terms of the GNU Lesser General +// Public License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// The full text of the license can be found in the file LICENSE.md at +// the top level directory of deal.II. +// +// --------------------------------------------------------------------- + + +// Test InterpolatedUniformGridData::gradient + +#include + +#include "../tests.h" + +// now interpolate the function x*y*z onto points. note that this function is +// (bi/tri)linear and so we can later know what the correct value is that the +// function should provide +Table<1, double> +fill(const std::array, 1> &coordinates) +{ + Table<1, double> data(coordinates[0].size()); + for (unsigned int i = 0; i < coordinates[0].size(); ++i) + data[i] = coordinates[0][i]; + return data; +} + +Table<2, double> +fill(const std::array, 2> &coordinates) +{ + Table<2, double> data(coordinates[0].size(), coordinates[1].size()); + for (unsigned int i = 0; i < coordinates[0].size(); ++i) + for (unsigned int j = 0; j < coordinates[1].size(); ++j) + data[i][j] = coordinates[0][i] * coordinates[1][j]; + return data; +} + +Table<3, double> +fill(const std::array, 3> &coordinates) +{ + Table<3, double> data(coordinates[0].size(), + coordinates[1].size(), + coordinates[2].size()); + for (unsigned int i = 0; i < coordinates[0].size(); ++i) + for (unsigned int j = 0; j < coordinates[1].size(); ++j) + for (unsigned int k = 0; k < coordinates[2].size(); ++k) + data[i][j][k] = + coordinates[0][i] * coordinates[1][j] * coordinates[2][k]; + return data; +} + + +template +void +check() +{ + // have coordinate arrays that span an interval starting at d+1 + // d+5 nonuniform intervals + std::array, dim> intervals; + std::array n_subintervals; + for (unsigned int d = 0; d < dim; ++d) + { + intervals[d] = std::make_pair(d + 2., 2 * d + 5.); + n_subintervals[d] = d + 1 + d * d; + } + + std::array, dim> coordinates; + for (unsigned int d = 0; d < dim; ++d) + { + const double x = intervals[d].first; + const double dx = + (intervals[d].second - intervals[d].first) / n_subintervals[d]; + + for (unsigned int i = 0; i < n_subintervals[d] + 1; ++i) + coordinates[d].push_back(x + dx * i); + } + + const Table data = fill(coordinates); + + Functions::InterpolatedUniformGridData f(intervals, + n_subintervals, + data); + + // now choose a number of randomly chosen points inside the box and + // verify that the functions returned are correct + for (unsigned int i = 0; i < 10; ++i) + { + Point p; + for (unsigned int d = 0; d < dim; ++d) + p[d] = + coordinates[d][0] + (random_value()) * + (coordinates[d].back() - coordinates[d][0]); + + // The function is x*y*z, so we can compute the gradient pretty easily + Tensor<1, dim> exact_gradient; + for (unsigned int d = 0; d < dim; ++d) + { + exact_gradient[d] = 1; + for (unsigned int e = 0; e < dim; ++e) + if (e != d) + exact_gradient[d] *= p[e]; + } + + AssertThrow((exact_gradient - f.gradient(p)).norm() < 1e-12, + ExcInternalError()); + } + + deallog << "OK" << std::endl; +} + + + +int +main() +{ + initlog(); + + check<1>(); + check<2>(); + check<3>(); +} diff --git a/tests/base/functions_11_gradient.output b/tests/base/functions_11_gradient.output new file mode 100644 index 0000000000..fb71de2867 --- /dev/null +++ b/tests/base/functions_11_gradient.output @@ -0,0 +1,4 @@ + +DEAL::OK +DEAL::OK +DEAL::OK