Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion framework/include/vectorpostprocessors/SamplerBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ class SamplerBase
std::vector<std::string> _variable_names;

/// What to sort by
const unsigned int _sort_by;
const std::string _sort_by;

/// x coordinate of the points
VectorPostprocessorValue & _x;
Expand Down
2 changes: 1 addition & 1 deletion framework/src/vectorpostprocessors/LineValueSampler.C
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ LineValueSampler::getValue(const Point & p) const
"only one variable can be provided as input to LineValueSampler.");

// Check if vectors are sorted by id
if (_sort_by != 3)
if (_sort_by != "id")
mooseError("LineValueSampler: When calling getValue() on LineValueSampler, "
"`sort_by` should be set to `id`.");

Expand Down
32 changes: 28 additions & 4 deletions framework/src/vectorpostprocessors/SamplerBase.C
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ SamplerBase::validParams()
{
InputParameters params = emptyInputParameters();

MooseEnum sort_options("x y z id");
params.addRequiredParam<MooseEnum>("sort_by", sort_options, "What to sort the samples by");
params.addRequiredParam<std::string>(
"sort_by",
"What to sort the samples by. Options are 'x y z id' and the name of any of the sampled "
"quantities (= name of the vector created by the vectorpostprocessor).");

// The value from this VPP is naturally already on every processor
// TODO: Make this not the case! See #11415
Expand All @@ -45,7 +47,7 @@ SamplerBase::SamplerBase(const InputParameters & parameters,
parameters.getCheckedPointerParam<FEProblemBase *>("_fe_problem_base")
->getMooseApp()
.getExecutioner())),
_sort_by(parameters.get<MooseEnum>("sort_by")),
_sort_by(parameters.get<std::string>("sort_by")),
_x(vpp->declareVector("x")),
_y(vpp->declareVector("y")),
_z(vpp->declareVector("z")),
Expand Down Expand Up @@ -173,9 +175,31 @@ SamplerBase::finalize()
for (auto vec_ptr : vec_ptrs)
_comm.allgather(*vec_ptr, /* identical buffer lengths = */ false);

// Find the index of the column to sort by
unsigned int sort_by_i = 0;
if (_sort_by == "x")
sort_by_i = 0;
else if (_sort_by == "y")
sort_by_i = 1;
else if (_sort_by == "z")
sort_by_i = 2;
else if (_sort_by == "id")
sort_by_i = 3;
else
{
// Find in 'variable_names'
const auto & it = std::find(_variable_names.begin(), _variable_names.end(), _sort_by);
if (it != _variable_names.end())
sort_by_i = it - _variable_names.begin();
else
mooseError("Sorting index '" + _sort_by +
"' was not found in x/y/z/id or sampled variable names: " +
Moose::stringify(_variable_names));
}

// Now create an index vector by using an indirect sort
std::vector<std::size_t> sorted_indices;
Moose::indirectSort(vec_ptrs[_sort_by]->begin(), vec_ptrs[_sort_by]->end(), sorted_indices);
Moose::indirectSort(vec_ptrs[sort_by_i]->begin(), vec_ptrs[sort_by_i]->end(), sorted_indices);

/**
* We now have one sorted vector. The remaining vectors need to be sorted according to that
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
id,u,v,x,y,z
0,1.25,0.5,0.25,0.25,0
2,2.75,1,0.25,0.75,0
1,2.25,1,0.75,0.25,0
3,3.75,1.5,0.75,0.75,0
14 changes: 13 additions & 1 deletion test/tests/vectorpostprocessors/element_value_sampler/tests
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
[Tests]
issues = '#11594 #31240'
design = 'ElementValueSampler.md'

[element_value_sampler]
issues = '#11594 #31240'
requirement = 'The system shall support sampling of a field variable at the centroid of every element in the domain'
[monomial]
type = 'CSVDiff'
Expand Down Expand Up @@ -31,7 +31,19 @@
max_threads = 1 # see libmesh issue #3808 (due to the linear fv variable here)
[]
[]
[sorting]
issues = '#31913'
requirement = "The system shall be able to sort sampled output"
[by_variable]
type = CSVDiff
input = 'element_value_sampler.i'
cli_args = "Outputs/file_base=sort_var VectorPostprocessors/element_value_sampler/sort_by='u'"
csvdiff = 'sort_var_element_value_sampler_0000.csv'
detail = "using the variables as a sorting index,"
[]
[]
[exceptions]
issues = '#11594 #31240'
requirement = 'The system shall throw an error if the variables specified for elemental sampling are'
[nodal]
type = 'RunException'
Expand Down