Skip to content

Commit

Permalink
Merge pull request #29126 from GiudGiud/PR_nodal_sampling
Browse files Browse the repository at this point in the history
Add more checks to samplers
  • Loading branch information
GiudGiud authored Nov 28, 2024
2 parents efd9ea4 + 030459f commit 3283cf7
Show file tree
Hide file tree
Showing 15 changed files with 164 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@

This `VectorPostprocessor` is similar to [NodalValueSampler](NodalValueSampler.md),
but is used for sampling elemental variables instead of nodal variables. The
coordinate used for each sampling point is the centroid of the associated
coordinate used for each sampling point is the centroid (vertex-average approximation) of the associated
element.

!alert note title=Vector names / CSV output column names
`ElementValueSampler` declares a vector for each spatial coordinate, (`x`, `y`, `z`), of the centroid of the element
along with its ID as well as a vector named after each variable sampled, containing the variable values.

!alert note title=General sampling
The `ElementValueSampler` samples elemental variables at the element centroids. For more flexible sampling,
use the [PositionsFunctorValueSampler.md].

!syntax parameters /VectorPostprocessors/ElementValueSampler

!syntax inputs /VectorPostprocessors/ElementValueSampler
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ If the line value sampler is used with a discontinuous variable on the edge/face
the distance along the sampled line in a vector called `id`,
and a vector named after each value sampled, containing the variable values at each point.

!alert note title=General sampling
The `LineValueSampler` samples variables on the specified line. For more flexible sampling,
use the [PositionsFunctorValueSampler.md].

!syntax parameters /VectorPostprocessors/LineValueSampler

!syntax inputs /VectorPostprocessors/LineValueSampler
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ in the domain, selection of blocks, or selection of boundaries.
the IDs of the nodes in a vector named `id`,
and a vector named after each variable sampled, containing the variable values at each point.

!alert note title=General sampling
The `NodalValueSampler` samples nodal variables at the mesh nodes. For more flexible sampling,
use the [PositionsFunctorValueSampler.md].

!syntax parameters /VectorPostprocessors/NodalValueSampler

!syntax inputs /VectorPostprocessors/NodalValueSampler
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ share the same names as the column headers.

- the x, y and z coordinates of the requested sampled points

!alert note title=General sampling
The `PointValueSampler` samples variables at the specified points. For more flexible sampling,
use the [PositionsFunctorValueSampler.md].

## Example input syntax

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ names.

- the X, Y, Z coordinates of the quadrature points on the side

!alert note title=General sampling
The `SideValueSampler` samples variables on the specified boundary on element side quadrature points. For more flexible sampling,
use the [PositionsFunctorValueSampler.md].

## Example input syntax

In this example, variable `u` and `v` are the solutions of two boundary value diffusion problems. Their value along the `top` boundary and along the `center` internal sidesets are reported using two `SideValueSampler`. The rows in the CSV output are sorted according the `x` coordinate along the boundary for the former, and the element `id` for the latter.
Expand Down
9 changes: 9 additions & 0 deletions framework/include/vectorpostprocessors/SamplerBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,15 @@ class SamplerBase
*/
void setupVariables(const std::vector<std::string> & variable_names);

/**
* Checks whether the passed variable pointer corresponds to a regular single-valued field
* variable
* @param var_param_name name of the variable parameter in which the variables were passed
* @param var_ptr pointer to the field variable
*/
void checkForStandardFieldVariableType(const MooseVariableFieldBase * const var_ptr,
const std::string & var_param_name = "variable") const;

/**
* Call this with the value of every variable at each point you want to sample at.
* @param p The point where you took the sample
Expand Down
17 changes: 11 additions & 6 deletions framework/src/vectorpostprocessors/ElementValueSampler.C
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ ElementValueSampler::validParams()
{
InputParameters params = ElementVariableVectorPostprocessor::validParams();

params.addClassDescription("Samples values of elemental variable(s).");
params.addClassDescription("Samples values of variables on elements.");

params += SamplerBase::validParams();

Expand All @@ -32,12 +32,17 @@ ElementValueSampler::validParams()
ElementValueSampler::ElementValueSampler(const InputParameters & parameters)
: ElementVariableVectorPostprocessor(parameters), SamplerBase(parameters, this, _communicator)
{
// ensure that variables are elemental, i.e., not scalar and and not nodal
// ensure that variables are 'elemental'
for (unsigned int i = 0; i < _coupled_moose_vars.size(); i++)
if (_coupled_moose_vars[i]->feType().family == SCALAR || _coupled_moose_vars[i]->isNodal())
paramError(
"variable", "The variable '", _coupled_moose_vars[i]->name(), "' is not elemental.");

{
if (_coupled_moose_vars[i]->isNodal())
paramError("variable",
"The variable '",
_coupled_moose_vars[i]->name(),
"' is a nodal variable. Nodal variables can be sampled using a "
"'NodalValueSampler'.");
SamplerBase::checkForStandardFieldVariableType(_coupled_moose_vars[i]);
}
std::vector<std::string> var_names(_coupled_moose_vars.size());
_values.resize(_coupled_moose_vars.size());

Expand Down
7 changes: 5 additions & 2 deletions framework/src/vectorpostprocessors/NodalValueSampler.C
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,13 @@ NodalValueSampler::validParams()
NodalValueSampler::NodalValueSampler(const InputParameters & parameters)
: NodalVariableVectorPostprocessor(parameters), SamplerBase(parameters, this, _communicator)
{
// ensure that variables are nodal, i.e., not scalar and and not elemental
// ensure that variables are 'nodal' (they have DoFs at nodes)
for (unsigned int i = 0; i < _coupled_moose_vars.size(); i++)
if (_coupled_moose_vars[i]->feType().family == SCALAR || !_coupled_moose_vars[i]->isNodal())
{
if (!_coupled_moose_vars[i]->isNodal())
paramError("variable", "The variable '", _coupled_moose_vars[i]->name(), "' is not nodal.");
SamplerBase::checkForStandardFieldVariableType(_coupled_moose_vars[i]);
}

std::vector<std::string> var_names(_coupled_moose_vars.size());
_values.resize(_coupled_moose_vars.size());
Expand Down
3 changes: 3 additions & 0 deletions framework/src/vectorpostprocessors/PointVariableSamplerBase.C
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@ PointVariableSamplerBase::PointVariableSamplerBase(const InputParameters & param
std::vector<std::string> var_names(_coupled_moose_vars.size());

for (unsigned int i = 0; i < _coupled_moose_vars.size(); i++)
{
var_names[i] = _coupled_moose_vars[i]->name();
SamplerBase::checkForStandardFieldVariableType(_coupled_moose_vars[i]);
}

// Initialize the data structures in SamplerBase
SamplerBase::setupVariables(var_names);
Expand Down
29 changes: 29 additions & 0 deletions framework/src/vectorpostprocessors/SamplerBase.C
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
#include "MooseEnum.h"
#include "MooseError.h"
#include "VectorPostprocessor.h"
#include "MooseVariableFieldBase.h"
#include "MooseBaseParameterInterface.h"

#include "libmesh/point.h"

Expand Down Expand Up @@ -87,6 +89,33 @@ SamplerBase::initialize()
_values.begin(), _values.end(), [](VectorPostprocessorValue * vec) { vec->clear(); });
}

void
SamplerBase::checkForStandardFieldVariableType(const MooseVariableFieldBase * const var_ptr,
const std::string & var_param_name) const
{
// A pointer to a MooseVariableFieldBase should never be SCALAR
mooseAssert(var_ptr->feType().family != SCALAR,
"Scalar variable '" + var_ptr->name() + "' cannot be sampled.");
mooseAssert(dynamic_cast<const MooseObject *>(_vpp), "Should have succeeded");
if (var_ptr->isVector())
dynamic_cast<const MooseObject *>(_vpp)->paramError(
var_param_name,
"The variable '",
var_ptr->name(),
"' is a vector variable. Sampling those is not currently supported in the "
"framework. It may be supported using a dedicated object in your application. Use "
"'VectorVariableComponentAux' auxkernel to copy those values into a regular field "
"variable");
if (var_ptr->isArray())
dynamic_cast<const MooseObject *>(_vpp)->paramError(
var_param_name,
"The variable '",
var_ptr->name(),
"' is an array variable. Sampling those is not currently supported in the "
"framework. It may be supported using a dedicated object in your application. Use "
"'ArrayVariableComponent' auxkernel to copy those values into a regular field variable");
}

void
SamplerBase::finalize()
{
Expand Down
5 changes: 4 additions & 1 deletion framework/src/vectorpostprocessors/SideValueSampler.C
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,12 @@ SideValueSampler::SideValueSampler(const InputParameters & parameters)
_values.resize(_coupled_moose_vars.size());

for (unsigned int i = 0; i < _coupled_moose_vars.size(); i++)
{
var_names[i] = _coupled_moose_vars[i]->name();
SamplerBase::checkForStandardFieldVariableType(_coupled_moose_vars[i]);
}

// Initialize the datastructions in SamplerBase
// Initialize the data structures in SamplerBase
SamplerBase::setupVariables(var_names);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,47 +9,47 @@
[]

[Functions]
[./u_fn]
[u_fn]
type = ParsedFunction
expression = '2 * x + 3 * y'
[../]
[./v_fn]
[]
[v_fn]
type = ParsedFunction
expression = 'x + y'
[../]
[]
[]

[AuxVariables]
[./u]
[u]
family = MONOMIAL
order = CONSTANT
[../]
[./v]
[]
[v]
family = MONOMIAL
order = CONSTANT
[../]
[]
[]

[ICs]
[./u_ic]
[u_ic]
type = FunctionIC
variable = u
function = u_fn
[../]
[./v_ic]
[]
[v_ic]
type = FunctionIC
variable = v
function = v_fn
[../]
[]
[]

[VectorPostprocessors]
[./element_value_sampler]
[element_value_sampler]
type = ElementValueSampler
variable = 'u v'
sort_by = id
execute_on = 'initial'
[../]
[]
[]

[Problem]
Expand Down
26 changes: 22 additions & 4 deletions test/tests/vectorpostprocessors/element_value_sampler/tests
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,32 @@

detail = 'for finite volume variables mixed with finite element variables,'
[]
[lagrange]
[]
[exceptions]
requirement = 'The system shall throw an error if the variables specified for elemental sampling are'
[nodal]
type = 'RunException'
input = 'element_value_sampler.i'
cli_args = 'AuxVariables/u/family=LAGRANGE AuxVariables/u/order=FIRST'
expect_err = "The variable 'u' is not elemental"
expect_err = "The variable 'u' is a nodal variable. Nodal variables can be sampled using a 'NodalValueSampler'"
mesh_mode = REPLICATED

detail = 'but not for nodal variables.'
detail = 'nodal variables,'
[]
[vector]
type = 'RunException'
input = 'element_value_sampler.i'
cli_args = 'AuxVariables/u/family=MONOMIAL_VEC ICs/active='
expect_err = "The variable 'u' is a vector variable. Sampling those is not currently supported"
mesh_mode = REPLICATED
detail = 'vector variables,'
[]
[array]
type = 'RunException'
input = 'element_value_sampler.i'
cli_args = 'AuxVariables/u/components=8 ICs/active='
expect_err = "The variable 'u' is an array variable. Sampling those is not currently supported"
mesh_mode = REPLICATED
detail = 'and array variables.'
[]
[]
[]
Original file line number Diff line number Diff line change
Expand Up @@ -6,57 +6,57 @@
[]

[Variables]
[./u]
[../]
[./v]
[../]
[u]
[]
[v]
[]
[]

[Kernels]
[./diff]
[diff]
type = Diffusion
variable = u
[../]
[./diff_v]
[]
[diff_v]
type = Diffusion
variable = v
[../]
[]
[]

[BCs]
[./left]
[left]
type = DirichletBC
variable = u
boundary = left
value = 0
[../]
[./right]
[]
[right]
type = DirichletBC
variable = u
boundary = right
value = 1
[../]
[./left_v]
[]
[left_v]
type = DirichletBC
variable = v
boundary = left
value = 1
[../]
[./right_v]
[]
[right_v]
type = DirichletBC
variable = v
boundary = right
value = 0
[../]
[]
[]

[VectorPostprocessors]
[./nodal_sample]
[nodal_sample]
type = NodalValueSampler
variable = 'u v'
boundary = top
sort_by = x
[../]
[]
[]

[Executioner]
Expand Down
Loading

0 comments on commit 3283cf7

Please sign in to comment.