-
Notifications
You must be signed in to change notification settings - Fork 239
Make use of named functions to compute secondary variables #1315
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
6e871f2
[PL] restructured secondary variables
chleh 6ef7ee2
[PL] adapted output to changed secondary vars
chleh baa7ac1
[PL] removed unneeded arguments
chleh e53c5af
[MaL] added header file
chleh 04592b0
[NL] added include
chleh 5406c12
[App] changed diffed fields
chleh b741a65
[PL] GWFlow adapted to named functions
chleh 1ee1bd6
[PL] added util to get a value from a vector for a specific DOF
chleh a292b51
[PL] added util to parse secondary variables
chleh 1362a01
[PL] class that can cache a secondary variable
chleh f1c4727
[PL] class that creates a global vector from the results of named fun…
chleh d0b842a
[PL] "global" context to evaluate a secondary variable using named fu…
chleh 1d9ec99
[PL] process owns named functions
chleh 78210b8
[PL] TES uses named functions
chleh 5dca603
[PL] changed includes
chleh a5df30c
[PL] fixed explicit ctor error
chleh 9c7220d
]PL] fixes after rebase
chleh f2acbb4
[PL] added docu
chleh c9be60c
[PL] added docu
chleh 6380a7f
[PL] added docu
chleh 0ed9b82
[PL] added docu
chleh 98d7b06
[PL] fixed compilation error
chleh d63cf5f
[PL] code review changes.
chleh 861c74e
[Data] changed 2ndary var configuration
chleh f16efdb
[PL] updated std::set usage
chleh fe3466a
[PL] added docu
chleh 5ff1414
[PL] added final
chleh 5932e84
[Data] changed some attributes
chleh c6bbe42
[PL] made variable names match the docu
chleh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/** | ||
* \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 "CachedSecondaryVariable.h" | ||
#include "BaseLib/Functional.h" | ||
#include "MathLib/LinAlg/LinAlg.h" | ||
|
||
namespace ProcessLib | ||
{ | ||
std::vector<NumLib::NamedFunction> CachedSecondaryVariable::getNamedFunctions() | ||
const | ||
{ | ||
return {{_internal_variable_name, | ||
std::vector<std::string>{}, | ||
BaseLib::easyBind(&CachedSecondaryVariable::getValue, this)}}; | ||
} | ||
|
||
double CachedSecondaryVariable::getValue() const | ||
{ | ||
if (_needs_recomputation) | ||
evalFieldNoArgs(); | ||
return _cached_nodal_values.get(_context.index); | ||
} | ||
|
||
SecondaryVariableFunctions CachedSecondaryVariable::getExtrapolator() | ||
{ | ||
// TODO copied from makeExtrapolator() | ||
auto const eval_residuals = [this]( | ||
GlobalVector const& /*x*/, | ||
NumLib::LocalToGlobalIndexMap const& /*dof_table*/, | ||
std::unique_ptr<GlobalVector> & /*result_cache*/ | ||
) -> GlobalVector const& { | ||
_extrapolator.calculateResiduals(*_extrapolatables); | ||
return _extrapolator.getElementResiduals(); | ||
}; | ||
return {BaseLib::easyBind(&CachedSecondaryVariable::evalField, this), | ||
eval_residuals}; | ||
} | ||
|
||
GlobalVector const& CachedSecondaryVariable::evalField( | ||
GlobalVector const& /*x*/, | ||
NumLib::LocalToGlobalIndexMap const& /*dof_table*/, | ||
std::unique_ptr<GlobalVector>& /*result_cache*/ | ||
) const | ||
{ | ||
return evalFieldNoArgs(); | ||
} | ||
|
||
GlobalVector const& CachedSecondaryVariable::evalFieldNoArgs() const | ||
{ | ||
if (!_needs_recomputation) { | ||
DBUG("%s does not need to be recomputed. Returning cached values", | ||
_internal_variable_name.c_str()); | ||
return _cached_nodal_values; | ||
} | ||
DBUG("Recomputing %s.", _internal_variable_name.c_str()); | ||
_extrapolator.extrapolate(*_extrapolatables); | ||
auto const& nodal_values = _extrapolator.getNodalValues(); | ||
MathLib::LinAlg::copy(nodal_values, _cached_nodal_values); | ||
_needs_recomputation = false; | ||
return nodal_values; | ||
} | ||
|
||
} // namespace ProcessLib |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/** | ||
* \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_CACHEDSECONDARYVARIABLE_H | ||
#define PROCESSLIB_CACHEDSECONDARYVARIABLE_H | ||
|
||
#include "NumLib/Extrapolation/ExtrapolatableElementCollection.h" | ||
#include "NumLib/NamedFunctionProvider.h" | ||
#include "NumLib/NumericsConfig.h" | ||
#include "SecondaryVariable.h" | ||
#include "SecondaryVariableContext.h" | ||
|
||
namespace ProcessLib | ||
{ | ||
/*! Secondary variable which is extrapolated from integration points to mesh | ||
* nodes; the resulting extrapolated values are cached for subsequent use. | ||
*/ | ||
class CachedSecondaryVariable final : public NumLi 1E0A b::NamedFunctionProvider | ||
{ | ||
public: | ||
/*! Constructs a new instance. | ||
* | ||
* \param internal_variable_name the variable's name | ||
* \param extrapolator extrapolates integration point values to nodal | ||
* values. | ||
* \param local_assemblers provide the integration point values | ||
* \param integration_point_values_method extracts the integration point | ||
* values from the \c local_assemblers | ||
* \param context needed s.t. this class can act as a NamedFunction | ||
*/ | ||
template <typename LocalAssemblerCollection, | ||
typename IntegrationPointValuesMethod> | ||
CachedSecondaryVariable( | ||
std::string const& internal_variable_name, | ||
NumLib::Extrapolator& extrapolator, | ||
LocalAssemblerCollection const& local_assemblers, | ||
IntegrationPointValuesMethod integration_point_values_method, | ||
SecondaryVariableContext const& context) | ||
: _extrapolator(extrapolator) | ||
, _extrapolatables(new NumLib::ExtrapolatableLocalAssemblerCollection< | ||
LocalAssemblerCollection>{ | ||
local_assemblers, integration_point_values_method}) | ||
, _context(context) | ||
, _internal_variable_name(internal_variable_name) | ||
{ | ||
} | ||
|
||
CachedSecondaryVariable(CachedSecondaryVariable const&) = delete; | ||
CachedSecondaryVariable(CachedSecondaryVariable&&) = delete; | ||
|
||
std::vector<NumLib::NamedFunction> getNamedFunctions() const override; | ||
|
||
//! Returns extrapolation functions that compute the secondary variable. | ||
SecondaryVariableFunctions getExtrapolator(); | ||
|
||
//! Set that recomputation is necessary. | ||
void expire() { _needs_recomputation = true; } | ||
|
||
private: | ||
//! Provides the value at the current index of the _context. | ||
double getValue() const; | ||
|
||
//! Computes the secondary Variable. | ||
GlobalVector const& evalField( | ||
GlobalVector const& /*x*/, | ||
NumLib::LocalToGlobalIndexMap const& /*dof_table*/, | ||
std::unique_ptr<GlobalVector>& /*result_cache*/ | ||
) const; | ||
|
||
//! Computes the secondary Variable. | ||
GlobalVector const& evalFieldNoArgs() const; | ||
|
||
//! Cache for the computed values. | ||
mutable GlobalVector _cached_nodal_values; | ||
mutable bool _needs_recomputation = true; | ||
|
||
NumLib::Extrapolator& _extrapolator; | ||
std::unique_ptr<NumLib::ExtrapolatableElementCollection> _extrapolatables; | ||
SecondaryVariableContext const& _context; | ||
std::string const _internal_variable_name; | ||
}; | ||
|
||
} // namespace ProcessLib | ||
|
||
#endif // PROCESSLIB_CACHEDSECONDARYVARIABLE_H |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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::SpecificFunctionCaller&& 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) | ||
{ | ||
result = 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.index = node_id; | ||
auto const value = _function_caller.call(args); | ||
|
||
// TODO Problems with PETSc? (local vs. global index) | ||
result->set(node_id, value); | ||
} | ||
|
||
return *result; | ||
} | ||
|
||
} // namespace ProcessLib |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/** | ||
* \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 | ||
{ | ||
//! Computes a global vector from a NamedFunction (which can only compute double | ||
//! values). | ||
class GlobalVectorFromNamedFunction final | ||
{ | ||
public: | ||
/*! Constructs a new instance. | ||
* | ||
* \param function_caller will provide the individual entries of the | ||
* GlobalVector to be computed. | ||
* \param mesh to which the \c dof_table_single is associated | ||
* \param dof_table_single used for constructing the GlobalVector | ||
* \param context used by the \c function_caller to access "auxiliary | ||
* unbound arguments". | ||
*/ | ||
GlobalVectorFromNamedFunction( | ||
NumLib::SpecificFunctionCaller&& 10000 amp; function_caller, | ||
MeshLib::Mesh const& mesh, | ||
NumLib::LocalToGlobalIndexMap const& dof_table_single, | ||
SecondaryVariableContext& context); | ||
|
||
//! Computes the GlobalVector. | ||
//! | ||
//! The signature of this method matches | ||
//! SecondaryVariableFunctions::Function, i.e., this method can be used to | ||
//! compute a secondary variable. | ||
GlobalVector const& call(GlobalVector const& x, | ||
NumLib::LocalToGlobalIndexMap const& dof_table, | ||
std::unique_ptr<GlobalVector>& result); | ||
|
||
private: | ||
NumLib::SpecificFunctionCaller _function_caller; | ||
MeshLib::Mesh const& _mesh; | ||
NumLib::LocalToGlobalIndexMap const& _dof_table_single; | ||
SecondaryVariableContext& _context; | ||
}; | ||
} // namespace ProcessLib | ||
|
||
#endif // PROCESSLIB_GLOBALVECTORFROMNAMEDFUNCTION_H |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i -> component_id
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd like to keep it as it is. The relation between number of arguments and number of components should be clear from the comment and the assertion.