Skip to content

Commit

Permalink
Merge pull request #25349 from GiudGiud/PR_function_pos
Browse files Browse the repository at this point in the history
Positions from functors
  • Loading branch information
GiudGiud authored Aug 31, 2023
2 parents 5079283 + 878d821 commit 321fa9d
Show file tree
Hide file tree
Showing 6 changed files with 247 additions and 0 deletions.
19 changes: 19 additions & 0 deletions framework/doc/content/source/positions/FunctorPositions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# FunctorPositions

!syntax description /Positions/FunctorPositions

See the [Functor](syntax/Functors/index.md) for the list of available functors.
The functors are evaluated at the origin of the mesh at the current time.

## Example File Syntax

In this example, the `FunctorPositions` is obtaining the positions from three time-dependent functions.
The functions are evaluated at the origin of the mesh at the current time.

!listing tests/positions/functor_positions.i block=Positions

!syntax parameters /Positions/FunctorPositions

!syntax inputs /Positions/FunctorPositions

!syntax children /Positions/FunctorPositions
31 changes: 31 additions & 0 deletions framework/include/positions/FunctorPositions.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//* 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 "Positions.h"
#include "FunctorInterface.h"

/**
* Positions from groups of three functors
*/
class FunctorPositions : public Positions, public NonADFunctorInterface
{
public:
static InputParameters validParams();
FunctorPositions(const InputParameters & parameters);
virtual ~FunctorPositions() = default;

void initialize() override;

private:
/// Vector of pointers to the functors for each coordinate (inner-ordering is coordinates)
std::vector<const Moose::Functor<Real> *> _pos_functors;
};
76 changes: 76 additions & 0 deletions framework/src/positions/FunctorPositions.C
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
//* 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

#include "FunctorPositions.h"

registerMooseObject("MooseApp", FunctorPositions);

InputParameters
FunctorPositions::validParams()
{
InputParameters params = Positions::validParams();
params += NonADFunctorInterface::validParams();

params.addRequiredParam<std::vector<MooseFunctorName>>(
"positions_functors", "Functors providing the XYZ coordinates of the positions");
// Use user-provided ordering
params.set<bool>("auto_sort") = false;
// All functors defined on all processes for now
params.set<bool>("auto_broadcast") = false;
// Keep as up-to-date as possible given the generality of functors
params.set<ExecFlagEnum>("execute_on") = {EXEC_LINEAR, EXEC_TIMESTEP_BEGIN};

params.addClassDescription(
"Import positions from one or more reporters, for example other Positions");
return params;
}

FunctorPositions::FunctorPositions(const InputParameters & parameters)
: Positions(parameters), NonADFunctorInterface(this)

{
const auto & functor_names = getParam<std::vector<MooseFunctorName>>("positions_functors");

// Check input sizes
if (functor_names.size() % 3 != 0)
paramError("position_functors",
"The list of functors must be divisible by three, the number of coordinates");

for (const auto & name : functor_names)
_pos_functors.push_back(&getFunctor<Real>(name));

// Obtain the positions by evaluating the functors
initialize();
// Sort if needed (user-specified)
finalize();
}

void
FunctorPositions::initialize()
{
clearPositions();
const auto n_positions = _pos_functors.size() / 3;
_positions.resize(n_positions);

// Use the mesh center as a global argument for now
_fe_problem.mesh().errorIfDistributedMesh(type());
// Locate the origin on the mesh
const Point p(0, 0, 0);
auto pl = _fe_problem.mesh().getMesh().sub_point_locator();
auto * const elem = (*pl)(p);
if (!elem)
mooseError("Origin point not in local mesh, cannot evaluate the functor there");
const Moose::ElemPointArg elem_origin = {elem, p, false};
const auto t = determineState();

for (auto i : make_range(n_positions))
_positions[i] = {(*_pos_functors[3 * i])(elem_origin, t),
(*_pos_functors[3 * i + 1])(elem_origin, t),
(*_pos_functors[3 * i + 2])(elem_origin, t)};
}
45 changes: 45 additions & 0 deletions test/tests/positions/functor_positions.i
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
[Mesh]
[cmg]
type = CartesianMeshGenerator
dx = 1
dim = 1
[]
[]

[Positions]
[functors]
type = FunctorPositions
positions_functors = '0.1 0 0.3
f1 f2 f1'
[]
[]

[Functions]
[f1]
type = PiecewiseConstant
x = '0 0.5 1'
y = '1 2 3'
[]
[f2]
type = PiecewiseLinear
x = '0 0.5 1'
y = '1 2 3'
[]
[]

[Problem]
solve = false
[]

[Executioner]
type = Transient
# Test recover
num_steps = 2
[]

[Outputs]
[out]
type = JSON
execute_system_information_on = none
[]
[]
68 changes: 68 additions & 0 deletions test/tests/positions/gold/functor_positions_out.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
{
"reporters": {
"functors": {
"type": "FunctorPositions",
"values": {
"positions_1d": {
"type": "std::vector<libMesh::Point>"
}
}
}
},
"time_steps": [
{
"functors": {
"positions_1d": [
{
"x": 0.1,
"y": 0.0,
"z": 0.3
},
{
"x": 1.0,
"y": 1.0,
"z": 1.0
}
]
},
"time": 0.0,
"time_step": 0
},
{
"functors": {
"positions_1d": [
{
"x": 0.1,
"y": 0.0,
"z": 0.3
},
{
"x": 2.0,
"y": 3.0,
"z": 2.0
}
]
},
"time": 1.0,
"time_step": 1
},
{
"functors": {
"positions_1d": [
{
"x": 0.1,
"y": 0.0,
"z": 0.3
},
{
"x": 3.0,
"y": 3.0,
"z": 3.0
}
]
},
"time": 2.0,
"time_step": 2
}
]
}
8 changes: 8 additions & 0 deletions test/tests/positions/tests
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@
jsondiff = 'reporter_positions_out.json'
detail = 'a reporter with data in the expected vector of points format,'
[]
[functors]
type = 'JSONDiff'
input = 'functor_positions.i'
jsondiff = 'functor_positions_out.json'
detail = 'triplets on functors that are evaluated to obtain positions,'
# Needs to be able to find the element containing the origin
mesh_mode = replicated
[]
[multiapps]
type = 'JSONDiff'
input = 'multiapp_positions.i'
Expand Down

0 comments on commit 321fa9d

Please sign in to comment.