From 9ec35906a5be06cf0d66da363b4c5951ed130848 Mon Sep 17 00:00:00 2001 From: Jean-Paul Pelteret Date: Wed, 24 Oct 2018 14:19:04 +0200 Subject: [PATCH] Add test to check that wrapper resets time state of wrapped class --- tests/base/functions_incremental_02.cc | 109 +++++++++++++++++++++ tests/base/functions_incremental_02.output | 16 +++ 2 files changed, 125 insertions(+) create mode 100644 tests/base/functions_incremental_02.cc create mode 100644 tests/base/functions_incremental_02.output diff --git a/tests/base/functions_incremental_02.cc b/tests/base/functions_incremental_02.cc new file mode 100644 index 0000000000..1ac22f6e0c --- /dev/null +++ b/tests/base/functions_incremental_02.cc @@ -0,0 +1,109 @@ +// --------------------------------------------------------------------- +// +// Copyright (C) 2016 - 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. +// +// --------------------------------------------------------------------- + + +// Check that IncrementalFunction resets the wrapped function time +// to its original state. + +#include + +#include "../tests.h" + +// homogeneous linear in time function +template +class MyFunc : public Function +{ +public: + MyFunc(const double a, const double b) + : Function(dim) + , a(a) + , b(b) + {} + + virtual double + value(const Point /*p*/, const unsigned int component = 0) const + { + (void)component; + return a * this->get_time() + b; + } + + virtual void + vector_value(const Point & /*p*/, Vector &values) const override + { + for (unsigned int i = 0; i < dim; ++i) + values[i] = a * this->get_time() + b; + } + +private: + const double a; + const double b; +}; + +template +void +check() +{ + Vector v1(dim); + Vector v2(dim); + Vector v3(dim); + + const Point point; + + MyFunc func(0.5, 10); + MyFunc func2(0.5, 10); + Functions::IncrementalFunction inc(func2); + + // Original time state: + func2.set_time(7.0); + deallog << "Wrapped func time (before): " << func2.get_time() << std::endl; + + std::vector> time_delta = {{1.0, 0.}, {1.0, 0.2}}; + + for (auto &el : time_delta) + { + deallog << "time = " << el.first << " delta = " << el.second << std::endl; + func.set_time(el.first); + func.vector_value(point, v1); + v1.print(deallog); + deallog << "-" << std::endl; + + func.set_time(el.first - el.second); + func.vector_value(point, v2); + v2.print(deallog); + deallog << "=" << std::endl; + + inc.set_time(el.first); + inc.set_decrement(el.second); + inc.vector_value(point, v3); + v3.print(deallog); + + v1.add(-1, v2); + v1.add(-1, v3); + AssertThrow(v1.l2_norm() == 0., ExcInternalError()); + } + + // Final time state (should be the same as the initial state: + deallog << "Wrapped func time (after): " << func2.get_time() << std::endl; + + deallog << "OK" << std::endl; +} + +int +main() +{ + initlog(); + + check<2>(); +} diff --git a/tests/base/functions_incremental_02.output b/tests/base/functions_incremental_02.output new file mode 100644 index 0000000000..9e585557fb --- /dev/null +++ b/tests/base/functions_incremental_02.output @@ -0,0 +1,16 @@ + +DEAL::Wrapped func time (before): 7.00000 +DEAL::time = 1.00000 delta = 0.00000 +DEAL::10.5000 10.5000 +DEAL::- +DEAL::10.5000 10.5000 +DEAL::= +DEAL::0.00000 0.00000 +DEAL::time = 1.00000 delta = 0.200000 +DEAL::10.5000 10.5000 +DEAL::- +DEAL::10.4000 10.4000 +DEAL::= +DEAL::0.100000 0.100000 +DEAL::Wrapped func time (after): 7.00000 +DEAL::OK -- 2.39.5