From: Olivier Gaboriault Date: Mon, 24 Feb 2025 20:39:47 +0000 (-0500) Subject: Added input parameter with the Assert X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=508fe74cc7097058bfbac2952243ff8ee827f024;p=dealii.git Added input parameter with the Assert --- diff --git a/include/deal.II/base/parsed_function.h b/include/deal.II/base/parsed_function.h index a8b2372636..e643566183 100644 --- a/include/deal.II/base/parsed_function.h +++ b/include/deal.II/base/parsed_function.h @@ -91,13 +91,14 @@ namespace Functions ParsedFunction(const unsigned int n_components = 1, const double h = 1e-8); /** - * Declare parameters needed by this class. The additional parameter @p + * Declare parameters needed by this class. The parameter @p * n_components is used to generate the right code according to the number * of components of the function that will parse this ParameterHandler. If * the number of components which is parsed does not match the number of * components of this object, an assertion is thrown and the program is - * aborted. The default behavior for this class is to declare the - * following entries: + * aborted. The additional parameter @p expr is used to declare the default + * expression used by the function. The default behavior for this class is + * to declare the following entries: * * @code * @@ -109,7 +110,8 @@ namespace Functions */ static void declare_parameters(ParameterHandler &prm, - const unsigned int n_components = 1); + const unsigned int n_components = 1, + const std::string &input_expr = ""); /** * Parse parameters needed by this class. If the number of components diff --git a/source/base/parsed_function.cc b/source/base/parsed_function.cc index 69a1f0c1de..6dc9daf29f 100644 --- a/source/base/parsed_function.cc +++ b/source/base/parsed_function.cc @@ -32,7 +32,8 @@ namespace Functions template void ParsedFunction::declare_parameters(ParameterHandler &prm, - const unsigned int n_components) + const unsigned int n_components, + const std::string &input_expr) { Assert(n_components > 0, ExcZero()); @@ -69,9 +70,21 @@ namespace Functions "variable names in your function expression."); // The expression of the function - std::string expr = "0"; - for (unsigned int i = 1; i < n_components; ++i) - expr += "; 0"; + // If the string is an empty string, 0 is set for each components. + std::string expr = input_expr; + if (expr == "") + { + expr = "0"; + for (unsigned int i = 1; i < n_components; ++i) + expr += "; 0"; + } + else + { + // If the user specified an input expr, the number of component + // specified need to match n_components. + AssertDimension((std::count(expr.begin(), expr.end(), ';') + 1) , n_components); + } + prm.declare_entry( "Function expression", diff --git a/tests/base/function_parser_13.cc b/tests/base/function_parser_13.cc new file mode 100644 index 0000000000..0b052c4741 --- /dev/null +++ b/tests/base/function_parser_13.cc @@ -0,0 +1,90 @@ +// ------------------------------------------------------------------------ +// +// SPDX-License-Identifier: LGPL-2.1-or-later +// Copyright (C) 2010 - 2023 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. +// +// ------------------------------------------------------------------------ + + +// This program tests the functionality of imposing a default expression +// when declaring a ParsedFunction. + +#include +#include + +#include "../tests.h" +#include "deal.II/matrix_free/matrix_free.h" + + +void +test() { + // set up problem: + ParameterHandler prm; + Functions::ParsedFunction<3> fct_1(1); + Functions::ParsedFunction<3> fct_2(2); + Functions::ParsedFunction<3> fct_3(3); + + std::string expression_1 = "1000"; + std::string expression_3 = "10;10;10"; + + // Declare + prm.enter_subsection("Function 1"); + fct_1.declare_parameters(prm, 1, expression_1); + prm.leave_subsection(); + + prm.enter_subsection("Function 2"); + fct_2.declare_parameters(prm, 2); + prm.leave_subsection(); + + prm.enter_subsection("Function 3"); + fct_3.declare_parameters(prm, 3, expression_3); + prm.leave_subsection(); + + + // Parse + prm.enter_subsection("Function 1"); + fct_1.parse_parameters(prm); + prm.leave_subsection(); + + prm.enter_subsection("Function 2"); + fct_2.parse_parameters(prm); + prm.leave_subsection(); + + prm.enter_subsection("Function 3"); + fct_3.parse_parameters(prm); + prm.leave_subsection(); + + + // Dummy point + const Point<3> p; + + const double result_1 = fct_1.value(p); + Vector result_2(2); + Vector result_3(3); + + fct_2.vector_value(p,result_2); + fct_3.vector_value(p,result_3); + + deallog << "Function 1 '" << expression_1 << "' is " + << result_1 << std::endl; + + deallog << "Function 2 '' is " + << result_2 << std::endl; + + deallog << "Function 3 '" << expression_3 << "' is " + << result_3 << std::endl; +} + +int +main() { + initlog(); + + test(); +} diff --git a/tests/base/function_parser_13.with_muparser=true.output b/tests/base/function_parser_13.with_muparser=true.output new file mode 100644 index 0000000000..5f5ab62522 --- /dev/null +++ b/tests/base/function_parser_13.with_muparser=true.output @@ -0,0 +1,4 @@ + +DEAL::Function 1 '1000' is 1000.00 +DEAL::Function 2 '' is 0.00000 0.00000 +DEAL::Function 3 '10;10;10' is 10.0000 10.0000 10.0000