]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Add test to check that wrapper resets time state of wrapped class
authorJean-Paul Pelteret <jppelteret@gmail.com>
Wed, 24 Oct 2018 12:19:04 +0000 (14:19 +0200)
committerDenis Davydov <davydden@gmail.com>
Fri, 26 Oct 2018 22:01:20 +0000 (00:01 +0200)
tests/base/functions_incremental_02.cc [new file with mode: 0644]
tests/base/functions_incremental_02.output [new file with mode: 0644]

diff --git a/tests/base/functions_incremental_02.cc b/tests/base/functions_incremental_02.cc
new file mode 100644 (file)
index 0000000..1ac22f6
--- /dev/null
@@ -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 <deal.II/base/incremental_function.h>
+
+#include "../tests.h"
+
+// homogeneous linear in time function
+template <int dim>
+class MyFunc : public Function<dim>
+{
+public:
+  MyFunc(const double a, const double b)
+    : Function<dim>(dim)
+    , a(a)
+    , b(b)
+  {}
+
+  virtual double
+  value(const Point<dim> /*p*/, const unsigned int component = 0) const
+  {
+    (void)component;
+    return a * this->get_time() + b;
+  }
+
+  virtual void
+  vector_value(const Point<dim> & /*p*/, Vector<double> &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 <int dim>
+void
+check()
+{
+  Vector<double> v1(dim);
+  Vector<double> v2(dim);
+  Vector<double> v3(dim);
+
+  const Point<dim> point;
+
+  MyFunc<dim>                         func(0.5, 10);
+  MyFunc<dim>                         func2(0.5, 10);
+  Functions::IncrementalFunction<dim> inc(func2);
+
+  // Original time state:
+  func2.set_time(7.0);
+  deallog << "Wrapped func time (before): " << func2.get_time() << std::endl;
+
+  std::vector<std::pair<double, double>> 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 (file)
index 0000000..9e58555
--- /dev/null
@@ -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

In the beginning the Universe was created. This has made a lot of people very angry and has been widely regarded as a bad move.

Douglas Adams


Typeset in Trocchi and Trocchi Bold Sans Serif.