From 8037219c1e0ae721c6a8df6ab1c085c25c8250f8 Mon Sep 17 00:00:00 2001 From: Peter Munch Date: Thu, 26 Sep 2024 18:09:29 +0200 Subject: [PATCH] ScalarFunctionFromFunctionObject for time-dependent functions --- doc/news/changes/minor/20240926Munch | 4 ++ include/deal.II/base/function.h | 12 +++- include/deal.II/base/function.templates.h | 19 +++++- tests/base/functions_15.cc | 79 +++++++++++++++++++++++ tests/base/functions_15.output | 7 ++ 5 files changed, 118 insertions(+), 3 deletions(-) create mode 100644 doc/news/changes/minor/20240926Munch create mode 100644 tests/base/functions_15.cc create mode 100644 tests/base/functions_15.output diff --git a/doc/news/changes/minor/20240926Munch b/doc/news/changes/minor/20240926Munch new file mode 100644 index 0000000000..5f243adb77 --- /dev/null +++ b/doc/news/changes/minor/20240926Munch @@ -0,0 +1,4 @@ +Improved: The class ScalarFunctionFromFunctionObject can now +also handle time-dependent functions. +
+(Peter Munch, 2024/09/26) diff --git a/include/deal.II/base/function.h b/include/deal.II/base/function.h index 36cd270d6e..cb5b913238 100644 --- a/include/deal.II/base/function.h +++ b/include/deal.II/base/function.h @@ -810,6 +810,15 @@ public: explicit ScalarFunctionFromFunctionObject( const std::function &)> &function_object); + /** + * Given a function object that takes a time and a Point and returns a + * RangeNumberType value, convert this into an object that matches the + * Function interface. + */ + explicit ScalarFunctionFromFunctionObject( + const std::function &)> + &function_object_t); + /** * Return the value of the function at the given point. Returns the value * the function given to the constructor produces for this point. @@ -822,7 +831,8 @@ private: * The function object which we call when this class's value() or * value_list() functions are called. */ - const std::function &)> function_object; + const std::function &)> + function_object; }; diff --git a/include/deal.II/base/function.templates.h b/include/deal.II/base/function.templates.h index 7405c291ad..1103012fca 100644 --- a/include/deal.II/base/function.templates.h +++ b/include/deal.II/base/function.templates.h @@ -691,7 +691,21 @@ ComponentSelectFunction::memory_consumption() const template ScalarFunctionFromFunctionObject:: ScalarFunctionFromFunctionObject( - const std::function &)> &function_object) + const std::function &)> &fu) + : ScalarFunctionFromFunctionObject( + [fu](const double t, const Point &x) { + (void)t; // we got a function object that only takes 'x', so ignore 't' + return fu(x); + }) +{} + + + +template +ScalarFunctionFromFunctionObject:: + ScalarFunctionFromFunctionObject( + const std::function &)> + &function_object) : Function(1) , function_object(function_object) {} @@ -707,7 +721,8 @@ ScalarFunctionFromFunctionObject::value( (void)component; Assert(component == 0, ExcMessage("This object represents only scalar functions")); - return function_object(p); + + return function_object(this->get_time(), p); } diff --git a/tests/base/functions_15.cc b/tests/base/functions_15.cc new file mode 100644 index 0000000000..b7bf939a5c --- /dev/null +++ b/tests/base/functions_15.cc @@ -0,0 +1,79 @@ +// ------------------------------------------------------------------------ +// +// SPDX-License-Identifier: LGPL-2.1-or-later +// Copyright (C) 2024 by the deal.II authors +// +// This file is part of the deal.II library. +// +// Part of the source code is dual licensed under Apache-2.0 WITH +// LLVM-exception OR LGPL-2.1-or-later. Detailed license information +// governing the source code and code contributions can be found in +// LICENSE.md and CONTRIBUTING.md at the top level directory of deal.II. +// +// ------------------------------------------------------------------------ + + +// Test ScalarFunctionFromFunctionObject with functions with and without +// time dependency. + +#include + +#include "../tests.h" + + +template +void +check1() +{ + ScalarFunctionFromFunctionObject object( + [](const auto &p) { return p.norm(); }); + + for (unsigned int i = 0; i < 10; ++i) + { + Point p; + for (unsigned int d = 0; d < dim; ++d) + p[d] = i + d; + + AssertThrow(object.value(p) == p.norm(), ExcInternalError()); + } + + deallog << "OK" << std::endl; +} + + +template +void +check2() +{ + ScalarFunctionFromFunctionObject object( + [](const auto t, const auto &p) { return p.norm() * t; }); + + for (unsigned int i = 0; i < 10; ++i) + { + object.set_time(i); + + Point p; + for (unsigned int d = 0; d < dim; ++d) + p[d] = i + d; + + AssertThrow(object.value(p) == (p.norm() * static_cast(i)), + ExcInternalError()); + } + + deallog << "OK" << std::endl; +} + + +int +main() +{ + initlog(); + + check1<1>(); + check1<2>(); + check1<3>(); + + check2<1>(); + check2<2>(); + check2<3>(); +} diff --git a/tests/base/functions_15.output b/tests/base/functions_15.output new file mode 100644 index 0000000000..0aa61ff573 --- /dev/null +++ b/tests/base/functions_15.output @@ -0,0 +1,7 @@ + +DEAL::OK +DEAL::OK +DEAL::OK +DEAL::OK +DEAL::OK +DEAL::OK -- 2.39.5