From 6d451ee189a3be43cb1040139c4f885ccac38442 Mon Sep 17 00:00:00 2001 From: Tobias Leibner <tobias.leibner@googlemail.com> Date: Wed, 29 Apr 2015 14:33:40 +0200 Subject: [PATCH] [functions.expression] remove ctors and build_gradient taking FieldMatrix --- dune/stuff/functions/expression.hh | 66 ++++++++----------------- dune/stuff/test/functions_expression.cc | 1 - 2 files changed, 20 insertions(+), 47 deletions(-) diff --git a/dune/stuff/functions/expression.hh b/dune/stuff/functions/expression.hh index 50778841f..3a007c28a 100644 --- a/dune/stuff/functions/expression.hh +++ b/dune/stuff/functions/expression.hh @@ -209,22 +209,30 @@ public: // get correct config const Common::Configuration cfg = config.has_sub(sub_name) ? config.sub(sub_name) : config; const Common::Configuration default_cfg = default_config(); + // get gradient + std::vector<std::vector<std::string>> gradient_as_vectors; + if (cfg.has_key("gradient")) { + // get gradient as FieldMatrix + typedef typename Dune::FieldMatrix<std::string, dimRange, dimDomain> JacobianMatrixType; + const JacobianMatrixType gradient_as_matrix = cfg.get<JacobianMatrixType>("gradient"); + // convert FieldMatrix to std::vector< std::vector < std::string > > + assert(gradient_as_matrix.rows >= dimRange); + assert(gradient_as_matrix.cols >= dimDomain); + for (size_t rr = 0; rr < dimRange; ++rr) { + std::vector<std::string> gradient_expression; + for (size_t cc = 0; cc < dimDomain; ++cc) + gradient_expression.emplace_back(gradient_as_matrix[rr][cc]); + gradient_as_vectors.emplace_back(gradient_expression); + } + } // create - return cfg.has_key("gradient") ? Common::make_unique<ThisType>( - cfg.get("variable", default_cfg.get<std::string>("variable")), - cfg.get("expression", default_cfg.get<std::vector<std::string>>("expression")), - cfg.get("order", default_cfg.get<size_t>("order")), - cfg.get("name", default_cfg.get<std::string>("name")), - cfg.get<Dune::FieldMatrix<std::string, dimRange, dimDomain>>("gradient")) - : Common::make_unique<ThisType>( - cfg.get("variable", default_cfg.get<std::string>("variable")), + return Common::make_unique<ThisType>(cfg.get("variable", default_cfg.get<std::string>("variable")), cfg.get("expression", default_cfg.get<std::vector<std::string>>("expression")), cfg.get("order", default_cfg.get<size_t>("order")), cfg.get("name", default_cfg.get<std::string>("name")), - Dune::FieldMatrix<std::string, 0, 0>()); + gradient_as_vectors); } // ... create(...) - // constructors taking a std::vector< std::vector< std::string > > for the jacobian Expression(const std::string variable, const std::string expression, const size_t ord = default_config().get<size_t>("order"), const std::string nm = static_id(), const std::vector<std::vector<std::string>> gradient_expressions = std::vector<std::vector<std::string>>()) @@ -245,27 +253,6 @@ public: build_gradients(variable, gradient_expressions); } - // constructors taking a FieldMatrix for the jacobian - template <int rows, int cols> - Expression(const std::string variable, const std::string expression, const size_t ord, const std::string nm, - const Dune::FieldMatrix<std::string, rows, cols> jacobian) - : function_(new MathExpressionFunctionType(variable, expression)) - , order_(ord) - , name_(nm) - { - build_gradients(variable, jacobian); - } - - template <int rows, int cols> - Expression(const std::string variable, const std::vector<std::string> expressions, const size_t ord, - const std::string nm, const Dune::FieldMatrix<std::string, rows, cols> jacobian) - : function_(new MathExpressionFunctionType(variable, expressions)) - , order_(ord) - , name_(nm) - { - build_gradients(variable, jacobian); - } - virtual std::string name() const override { return name_; @@ -313,7 +300,7 @@ public: << "You can disable this check by defining DUNE_STUFF_FUNCTIONS_EXPRESSION_DISABLE_CHECKS\n"); #endif // DUNE_STUFF_FUNCTIONS_EXPRESSION_DISABLE_CHECKS #endif // NDEBUG - } + } // ... evaluate(...) virtual void jacobian(const DomainType& xx, JacobianRangeType& ret) const override { @@ -324,6 +311,7 @@ public: gradients_[ii]->evaluate(xx, ret[ii]); } } // ... jacobian(...) + private: void build_gradients(const std::string variable, const std::vector<std::vector<std::string>>& gradient_expressions) { @@ -336,20 +324,6 @@ private: } } // ... build_gradients(...) - template <int rows, int cols> - void build_gradients(const std::string variable, Dune::FieldMatrix<std::string, rows, cols> jacobian) - { - assert(jacobian.rows == 0 || jacobian.rows >= dimRange); - assert(jacobian.cols == 0 || jacobian.cols >= dimDomain); - if (jacobian.rows > 0 && jacobian.cols > 0) - for (size_t rr = 0; rr < dimRange; ++rr) { - std::vector<std::string> gradient_expression; - for (size_t cc = 0; cc < dimDomain; ++cc) - gradient_expression.emplace_back(jacobian[rr][cc]); - gradients_.emplace_back(new MathExpressionGradientType(variable, gradient_expression)); - } - } // ... build_gradients(...) - std::shared_ptr<const MathExpressionFunctionType> function_; size_t order_; std::string name_; diff --git a/dune/stuff/test/functions_expression.cc b/dune/stuff/test/functions_expression.cc index 6109c9757..f73ba629e 100644 --- a/dune/stuff/test/functions_expression.cc +++ b/dune/stuff/test/functions_expression.cc @@ -37,7 +37,6 @@ config["expression"] = "[2*x[0] 2*x[1] 2*x[2]]"; \ config["gradient"] = "[2 0 0; 0 2 0; 0 0 2]"; \ const std::unique_ptr<const LocalizableFunctionType> function2(LocalizableFunctionType::create(config)); \ - const LocalizableFunctionType function3("x", {"x[0]", "x[0]", "x[0]"}); \ } \ }; // TEST_STRUCT_GENERATOR -- GitLab