From f1c47270f836a878d753d26f8fea360d4a3e13db Mon Sep 17 00:00:00 2001 From: Christoph Lehmann <christoph.lehmann@ufz.de> Date: Thu, 7 Jul 2016 12:22:06 +0200 Subject: [PATCH] [PL] class that creates a global vector from the results of named function calls --- ProcessLib/GlobalVectorFromNamedFunction.cpp | 63 ++++++++++++++++++++ ProcessLib/GlobalVectorFromNamedFunction.h | 42 +++++++++++++ 2 files changed, 105 insertions(+) create mode 100644 ProcessLib/GlobalVectorFromNamedFunction.cpp create mode 100644 ProcessLib/GlobalVectorFromNamedFunction.h diff --git a/ProcessLib/GlobalVectorFromNamedFunction.cpp b/ProcessLib/GlobalVectorFromNamedFunction.cpp new file mode 100644 index 00000000000..aabdb153f8f --- /dev/null +++ b/ProcessLib/GlobalVectorFromNamedFunction.cpp @@ -0,0 +1,63 @@ +/** + * \copyright + * Copyright (c) 2012-2016, OpenGeoSys Community (http://www.opengeosys.org) + * Distributed under a Modified BSD License. + * See accompanying file LICENSE.txt or + * http://www.opengeosys.org/project/license + * + */ + +#include "GlobalVectorFromNamedFunction.h" +#include "MathLib/LinAlg/MatrixVectorTraits.h" +#include "ProcessLib/Utils/VectorUtil.h" + +namespace ProcessLib +{ +GlobalVectorFromNamedFunction::GlobalVectorFromNamedFunction( + NumLib::SpecialFunctionCaller&& function_caller, + MeshLib::Mesh const& mesh, + NumLib::LocalToGlobalIndexMap const& dof_table_single, + SecondaryVariableContext& context) + : _function_caller(std::move(function_caller)) + , _mesh(mesh) + , _dof_table_single(dof_table_single) + , _context(context) +{ + assert(dof_table_single.getNumberOfComponents() == 1); +} + +GlobalVector const& GlobalVectorFromNamedFunction::call( + GlobalVector const& x, + NumLib::LocalToGlobalIndexMap const& dof_table, + std::unique_ptr<GlobalVector>& result_cache) +{ + result_cache = MathLib::MatrixVectorTraits<GlobalVector>::newInstance( + {_dof_table_single.dofSizeWithoutGhosts(), + _dof_table_single.dofSizeWithoutGhosts(), + &_dof_table_single.getGhostIndices(), nullptr}); + + GlobalIndexType nnodes = _mesh.getNumberOfNodes(); + + auto const n_args = _function_caller.getNumberOfUnboundArguments(); + assert(dof_table.getNumberOfComponents() == n_args); + std::vector<double> args(n_args); + + for (GlobalIndexType node_id = 0; node_id < nnodes; ++node_id) { + // TODO maybe fill args via callback mechanism or remove this class entirely. + // Caution: The order of args will be the same as the order of the + // components in the global vector! + for (std::size_t i = 0; i < n_args; ++i) { + args[i] = getNodalValue(x, _mesh, dof_table, node_id, i); + } + + _context.setIndex(node_id); + auto const result = _function_caller.call(args); + + // TODO Problems with PETSc? (local vs. global index) + result_cache->set(node_id, result); + } + + return *result_cache; +} + +} // namespace ProcessLib diff --git a/ProcessLib/GlobalVectorFromNamedFunction.h b/ProcessLib/GlobalVectorFromNamedFunction.h new file mode 100644 index 00000000000..45cfe08e283 --- /dev/null +++ b/ProcessLib/GlobalVectorFromNamedFunction.h @@ -0,0 +1,42 @@ +/** + * \copyright + * Copyright (c) 2012-2016, OpenGeoSys Community (http://www.opengeosys.org) + * Distributed under a Modified BSD License. + * See accompanying file LICENSE.txt or + * http://www.opengeosys.org/project/license + * + */ + +#ifndef PROCESSLIB_GLOBALVECTORFROMNAMEDFUNCTION_H +#define PROCESSLIB_GLOBALVECTORFROMNAMEDFUNCTION_H + +#include "MeshLib/Mesh.h" +#include "NumLib/DOF/LocalToGlobalIndexMap.h" +#include "NumLib/NumericsConfig.h" +#include "NumLib/NamedFunctionCaller.h" +#include "SecondaryVariableContext.h" + +namespace ProcessLib +{ +class GlobalVectorFromNamedFunction +{ +public: + GlobalVectorFromNamedFunction( + NumLib::SpecialFunctionCaller&& function_caller, + MeshLib::Mesh const& mesh, + NumLib::LocalToGlobalIndexMap const& dof_table_single, + SecondaryVariableContext& context); + + GlobalVector const& call(GlobalVector const& x, + NumLib::LocalToGlobalIndexMap const& dof_table, + std::unique_ptr<GlobalVector>& result_cache); + +private: + NumLib::SpecialFunctionCaller _function_caller; + MeshLib::Mesh const& _mesh; + NumLib::LocalToGlobalIndexMap const& _dof_table_single; + SecondaryVariableContext& _context; +}; +} // namespace ProcessLib + +#endif // PROCESSLIB_GLOBALVECTORFROMNAMEDFUNCTION_H -- GitLab