Skip to content

Commit

Permalink
Use displaced system for meshfunctions when using displaced source me…
Browse files Browse the repository at this point in the history
…shes, refs idaholab#3372 idaholab#1868

Add an extrapolation value and default it to 0
  • Loading branch information
GiudGiud committed Feb 7, 2023
1 parent 59f1695 commit 809a229
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 24 deletions.
37 changes: 28 additions & 9 deletions framework/include/transfers/MultiAppGeneralFieldTransfer.h
Expand Up @@ -39,7 +39,7 @@ class MultiAppGeneralFieldTransfer : public MultiAppConservativeTransfer
MultiAppGeneralFieldTransfer(const InputParameters & parameters);

virtual void initialSetup() override;

virtual void getAppInfo() override;
virtual void execute() override;

/// Get the source variable name, with the suffix for array/vector variables
Expand Down Expand Up @@ -249,6 +249,9 @@ class MultiAppGeneralFieldTransfer : public MultiAppConservativeTransfer
/// Error out when some points can not be located
bool _error_on_miss;

/// Value to use when no received data is valid for a target location
const Real _default_extrapolation_value;

/// How much we should relax bounding boxes
Real _bbox_factor;

Expand Down Expand Up @@ -463,7 +466,7 @@ class RecordRequests
std::vector<Point> & points_requested() { return _points_requested; }

private:
// Vector of points requested
/// Vector of points requested
std::vector<Point> _points_requested;

RecordRequests * _primary = nullptr;
Expand Down Expand Up @@ -509,13 +512,16 @@ class CachedData
* @param cache a map/cache to search for points in
* @param backup a function that can be queried for a point value when the cache doesnt have it
*/
CachedData(const Cache & cache, const FunctionBase<Output> & backup)
: _cache(cache), _backup(backup.clone())
CachedData(const Cache & cache, const FunctionBase<Output> & backup, Real default_value)
: _cache(cache), _backup(backup.clone()), _default_value(default_value)
{
}

/// Copy constructor
CachedData(const CachedData & primary) : _cache(primary._cache), _backup(primary._backup->clone())
CachedData(const CachedData & primary)
: _cache(primary._cache),
_backup(primary._backup->clone()),
_default_value(primary._default_value)
{
}

Expand All @@ -531,7 +537,12 @@ class CachedData
{
auto it = _cache.find(n);
if (it == _cache.end())
return (*_backup)(n);
{
if (_default_value != GeneralFieldTransfer::BetterOutOfMeshValue)
return _default_value;
else
return (*_backup)(n);
}
else
return it->second;
}
Expand All @@ -545,7 +556,12 @@ class CachedData
{
auto it = _cache.find(n);
if (it == _cache.end())
return (*_backup)(n);
{
if (_default_value != GeneralFieldTransfer::BetterOutOfMeshValue)
return _default_value;
else
return (*_backup)(n);
}
else
return it->second;
}
Expand Down Expand Up @@ -578,11 +594,14 @@ class CachedData
}

private:
// Data to return for cached points
/// Data to return for cached points
const Cache & _cache;

// Function to evaluate for uncached points
/// Function to evaluate for uncached points
std::unique_ptr<FunctionBase<Output>> _backup;

/// Default value when no point is found
const Real _default_value;
};

}
2 changes: 1 addition & 1 deletion framework/include/transfers/MultiAppTransfer.h
Expand Up @@ -118,7 +118,7 @@ class MultiAppTransfer : public Transfer
* This method will fill information into the convenience member variables
* (_to_problems, _from_meshes, etc.)
*/
void getAppInfo();
virtual void getAppInfo();

protected:
/**
Expand Down
Expand Up @@ -35,6 +35,8 @@ MultiAppGeneralFieldNearestNodeTransfer::validParams()

// Nearest node is historically more an extrapolation transfer
params.set<bool>("from_app_must_contain_point") = false;
params.set<Real>("extrapolation_constant") = GeneralFieldTransfer::BetterOutOfMeshValue;
params.suppressParameter<Real>("extrapolation_constant");
// We dont keep track of both point distance to app and to nearest node
params.set<bool>("use_nearest_app") = false;
params.suppressParameter<bool>("use_nearest_app");
Expand Down
45 changes: 32 additions & 13 deletions framework/src/transfers/MultiAppGeneralFieldTransfer.C
Expand Up @@ -45,6 +45,10 @@ MultiAppGeneralFieldTransfer::validParams()
"fixed_bounding_box_size >= 0",
"Override source app bounding box size(s) for searches. App bounding boxes will still be "
"centered on the same coordinates. Only non-zero components passed will override.");
params.addParam<Real>(
"extrapolation_constant",
0,
"Constant to use when no source app can provide a valid value for a target location.");

// Block restrictions
params.addParam<std::vector<SubdomainName>>(
Expand Down Expand Up @@ -92,7 +96,7 @@ MultiAppGeneralFieldTransfer::validParams()
"(using the `location`) sub-app and query that app for the value to transfer.");
params.addParam<bool>(
"from_app_must_contain_point",
true,
false,
"Wether on not the origin mesh must contain the point to evaluate data at. If false, this "
"allows for interpolation between origin app meshes. Origin app bounding boxes are still "
"considered so you may want to increase them with 'fixed_bounding_box_size'");
Expand Down Expand Up @@ -122,6 +126,7 @@ MultiAppGeneralFieldTransfer::MultiAppGeneralFieldTransfer(const InputParameters
_greedy_search(getParam<bool>("greedy_search")),
_search_value_conflicts(getParam<bool>("search_value_conflicts")),
_error_on_miss(getParam<bool>("error_on_miss")),
_default_extrapolation_value(getParam<Real>("extrapolation_constant")),
_bbox_factor(getParam<Real>("bbox_factor")),
_fixed_bbox_size(isParamValid("fixed_bounding_box_size")
? getParam<std::vector<Real>>("fixed_bounding_box_size")
Expand Down Expand Up @@ -192,16 +197,6 @@ MultiAppGeneralFieldTransfer::initialSetup()
}
}

// Create the point locators to locate evaluation points in the origin mesh(es)
_from_point_locators.resize(_from_problems.size());
for (unsigned int i_from = 0; i_from < _from_problems.size(); ++i_from)
{
const auto & from_moose_mesh = _from_problems[i_from]->mesh(_displaced_source_mesh);
_from_point_locators[i_from] =
PointLocatorBase::build(TREE_LOCAL_ELEMENTS, from_moose_mesh.getMesh());
_from_point_locators[i_from]->enable_out_of_mesh_mode();
}

// Check if components are set correctly if using an array variable
for (unsigned int i_from = 0; i_from < _from_problems.size(); ++i_from)
{
Expand Down Expand Up @@ -246,6 +241,22 @@ MultiAppGeneralFieldTransfer::initialSetup()
}
}

void
MultiAppGeneralFieldTransfer::getAppInfo()
{
MultiAppFieldTransfer::getAppInfo();

// Create the point locators to locate evaluation points in the origin mesh(es)
_from_point_locators.resize(_from_problems.size());
for (unsigned int i_from = 0; i_from < _from_problems.size(); ++i_from)
{
const auto & from_moose_mesh = _from_problems[i_from]->mesh(_displaced_source_mesh);
_from_point_locators[i_from] =
PointLocatorBase::build(TREE_LOCAL_ELEMENTS, from_moose_mesh.getMesh());
_from_point_locators[i_from]->enable_out_of_mesh_mode();
}
}

void
MultiAppGeneralFieldTransfer::execute()
{
Expand Down Expand Up @@ -854,7 +865,8 @@ MultiAppGeneralFieldTransfer::setSolutionVectorValues(
MeshFunction to_func(es, *to_sys->current_local_solution, to_sys->get_dof_map(), var_num);
to_func.init();

GeneralFieldTransfer::CachedData<Number> f(interp_caches[problem_id], to_func);
GeneralFieldTransfer::CachedData<Number> f(
interp_caches[problem_id], to_func, _default_extrapolation_value);
libMesh::VectorSetAction<Number> setter(*to_sys->solution);
const std::vector<unsigned int> varvec(1, var_num);

Expand Down Expand Up @@ -903,9 +915,16 @@ MultiAppGeneralFieldTransfer::setSolutionVectorValues(
"Element ", dof_object_id, " for app ", problem_id, " could not be located ");
}

// We should not put garbage into solution vector
// We should not put garbage into our solution vector
// but it can be that we want to set it to a different value than what was already there
// for example: the source app has been displaced and was sending an indicator of its
// position
if (GeneralFieldTransfer::isBetterOutOfMeshValue(val))
{
if (!GeneralFieldTransfer::isBetterOutOfMeshValue(_default_extrapolation_value))
to_sys->solution->set(dof, _default_extrapolation_value);
continue;
}

to_sys->solution->set(dof, val);
}
Expand Down
2 changes: 1 addition & 1 deletion framework/src/transfers/MultiAppShapeEvaluationTransfer.C
Expand Up @@ -160,7 +160,7 @@ MultiAppShapeEvaluationTransfer::transferVariable(unsigned int i)

points.clear();
point_ids.clear();
// grap sample points
// grab sample points
// for constant shape function, we take the element centroid
if (is_constant)
{
Expand Down

0 comments on commit 809a229

Please sign in to comment.