From: Wolfgang Bangerth Date: Mon, 19 Apr 2021 21:16:40 +0000 (-0600) Subject: Add a test. X-Git-Tag: v9.3.0-rc1~178^2 X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=refs%2Fpull%2F12094%2Fhead;p=dealii.git Add a test. --- diff --git a/tests/base/functions_11_move.cc b/tests/base/functions_11_move.cc new file mode 100644 index 0000000000..8c174c7070 --- /dev/null +++ b/tests/base/functions_11_move.cc @@ -0,0 +1,149 @@ +// --------------------------------------------------------------------- +// +// 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 +// +// Like the _11 test, but using the move constructor that moves the +// data from the initializing tables into the +// InterpolatedTensorProductGridData object. + +#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); + } + + Table data = fill(coordinates); + + Functions::InterpolatedUniformGridData f(std::move(intervals), + std::move(n_subintervals), + std::move(data)); + + // Verify tha the data has really been moved + for (unsigned int d = 0; d < dim; ++d) + Assert(data.size()[d] == 0, ExcInternalError()); + + // 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]); + + double exact_value = 1; + for (unsigned int d = 0; d < dim; ++d) + exact_value *= p[d]; + + AssertThrow(std::fabs(exact_value - f.value(p)) < 1e-12, + ExcInternalError()); + } + + // now also verify that it computes values outside the box correctly, as + // documented + double value_at_bottom_left = 1; + for (unsigned int d = 0; d < dim; ++d) + value_at_bottom_left *= coordinates[d][0]; + + AssertThrow(std::fabs(f.value(Point()) - value_at_bottom_left) < 1e-12, + ExcInternalError()); + + Point top_right; + double value_at_top_right = 1; + for (unsigned int d = 0; d < dim; ++d) + { + top_right[d] = 1000; + value_at_top_right *= coordinates[d].back(); + } + AssertThrow(std::fabs(f.value(top_right) - value_at_top_right) < 1e-12, + ExcInternalError()); + + deallog << "OK" << std::endl; +} + + + +int +main() +{ + initlog(); + + check<1>(); + check<2>(); + check<3>(); +} diff --git a/tests/base/functions_11_move.output b/tests/base/functions_11_move.output new file mode 100644 index 0000000000..fb71de2867 --- /dev/null +++ b/tests/base/functions_11_move.output @@ -0,0 +1,4 @@ + +DEAL::OK +DEAL::OK +DEAL::OK