Skip to content

Commit

Permalink
Add nearest point average userobject. Refs idaholab#18536
Browse files Browse the repository at this point in the history
  • Loading branch information
aprilnovak committed Aug 6, 2021
1 parent cbe2ad2 commit f49fefb
Show file tree
Hide file tree
Showing 7 changed files with 255 additions and 0 deletions.
17 changes: 17 additions & 0 deletions framework/doc/content/source/userobject/NearestPointAverage.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# NearestPointAverage

The domain is virtually divided into a number of subdomains according to
the nearest points provided by the users. For each provided point, a virtual
subdomain is created around that point and consists of all regions of space
that are closer to that point than any of the other provided points. Then,
the variable average is computed over each individual subdomain separately.

!syntax description /UserObjects/NearestPointAverage

!syntax parameters /UserObjects/NearestPointAverage

!syntax inputs /UserObjects/NearestPointAverage

!syntax children /UserObjects/NearestPointAverage

!bibtex bibliography
43 changes: 43 additions & 0 deletions framework/include/userobject/NearestPointAverage.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//* This file is part of the MOOSE framework
//* https://www.mooseframework.org
//*
//* All rights reserved, see COPYRIGHT for full restrictions
//* https://github.com/idaholab/moose/blob/master/COPYRIGHT
//*
//* Licensed under LGPL 2.1, please see LICENSE for details
//* https://www.gnu.org/licenses/lgpl-2.1.html

#pragma once

// MOOSE includes
#include "NearestPointBase.h"
#include "ElementAverageValue.h"
#include "ElementVariableVectorPostprocessor.h"

// Forward Declarations
class NearestPointAverage;

/**
* Given a list of points this object computes the average of a variable
* over the points closest to each provided point.
*/
class NearestPointAverage
: public NearestPointBase<ElementAverageValue, ElementVariableVectorPostprocessor>
{
public:
static InputParameters validParams();

NearestPointAverage(const InputParameters & parameters);

virtual Real spatialValue(const Point & point) const override;

Real userObjectValue(unsigned int i) const;

unsigned int nearestPointIndex(const Point & point) const;

virtual void finalize() override;

protected:
/// Postprocessor values for each point
VectorPostprocessorValue & _np_post_processor_values;
};
97 changes: 97 additions & 0 deletions framework/src/userobject/NearestPointAverage.C
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
//* This file is part of the MOOSE framework
//* https://www.mooseframework.org
//*
//* All rights reserved, see COPYRIGHT for full restrictions
//* https://github.com/idaholab/moose/blob/master/COPYRIGHT
//*
//* Licensed under LGPL 2.1, please see LICENSE for details
//* https://www.gnu.org/licenses/lgpl-2.1.html

// MOOSE includes
#include "NearestPointAverage.h"

registerMooseObject("MooseApp", NearestPointAverage);

InputParameters
NearestPointAverage::validParams()
{
InputParameters params =
nearestPointBaseValidParams<ElementAverageValue, ElementVariableVectorPostprocessor>();

params.addClassDescription(
"Compute element variable averages for nearest-point based subdomains");

return params;
}

NearestPointAverage::NearestPointAverage(const InputParameters & parameters)
: NearestPointBase<ElementAverageValue, ElementVariableVectorPostprocessor>(parameters),
_np_post_processor_values(declareVector("np_post_processor_values"))
{
_np_post_processor_values.resize(_user_objects.size());
}

Real
NearestPointAverage::spatialValue(const Point & point) const
{
unsigned int i = nearestPointIndex(point);

if (i >= _np_post_processor_values.size())
mooseError("The vector length of vector post processor is ",
_np_post_processor_values.size(),
" but nearestPointIndex() return ",
i);

return _np_post_processor_values[i];
}

Real
NearestPointAverage::userObjectValue(unsigned int i) const
{
if (i >= _np_post_processor_values.size())
mooseError("The vector length of vector post processor is ",
_np_post_processor_values.size(),
" but you pass in ",
i);

return _np_post_processor_values[i];
}

void
NearestPointAverage::finalize()
{
if (_user_objects.size() != _np_post_processor_values.size())
mooseError("The vector length of the vector postprocessor ",
_np_post_processor_values.size(),
" is different from the number of user objects ",
_user_objects.size());

unsigned int i = 0;
for (auto & user_object : _user_objects)
{
user_object->finalize();
_np_post_processor_values[i++] = user_object->getValue();
}
}

unsigned int
NearestPointAverage::nearestPointIndex(const Point & p) const
{
unsigned int closest = 0;
Real closest_distance = std::numeric_limits<Real>::max();

for (auto it : Moose::enumerate(_points))
{
const auto & current_point = it.value();

Real current_distance = (p - current_point).norm();

if (current_distance < closest_distance)
{
closest_distance = current_distance;
closest = it.index();
}
}

return closest;
}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
[Mesh]
type = GeneratedMesh
dim = 3
nx = 8
ny = 8
nz = 8
[]

[Variables]
[u]
[]
[]

[AuxVariables]
[v]
[]
[np_average]
order = CONSTANT
family = MONOMIAL
[]
[]

[ICs]
[v]
type = FunctionIC
variable = v
function = v
[]
[]

[Functions]
[v]
type = ParsedFunction
value = x+y-sin(z)
[]
[]

[Kernels]
[diff]
type = Diffusion
variable = u
[]
[]

[AuxKernels]
[np_average]
type = SpatialUserObjectAux
variable = np_average
execute_on = timestep_end
user_object = npa
[]
[]

[BCs]
[left]
type = DirichletBC
variable = u
boundary = left
value = 0
[]
[]

[UserObjects]
[npa]
type = NearestPointAverage
points_file = points.txt
variable = v
[]
[]

[Executioner]
type = Steady
petsc_options_iname = '-pc_type -pc_hypre_type'
petsc_options_value = 'hypre boomeramg'
[]

[Outputs]
exodus = true
hide = 'u'
[]
8 changes: 8 additions & 0 deletions test/tests/userobjects/nearest_point_average/points.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
0.25 0.25 0.25
0.75 0.25 0.25
0.25 0.75 0.25
0.75 0.75 0.25
0.25 0.25 0.75
0.75 0.25 0.75
0.25 0.75 0.75
0.75 0.75 0.75
10 changes: 10 additions & 0 deletions test/tests/userobjects/nearest_point_average/tests
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[Tests]
[./test]
type = Exodiff
input = nearest_point_average.i
exodiff = nearest_point_average_out.e
requirement = 'The system shall compute averages computed from the closest values for a list of points'
design = 'NearestPointAverage.md'
issues = '#18536'
[../]
[]

0 comments on commit f49fefb

Please sign in to comment.