https://mooseframework.inl.gov
ProjectionAux.C
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://mooseframework.inl.gov
3 //*
4 //* All rights reserved, see COPYRIGHT for full restrictions
5 //* https://github.com/idaholab/moose/blob/master/COPYRIGHT
6 //*
7 //* Licensed under LGPL 2.1, please see LICENSE for details
8 //* https://www.gnu.org/licenses/lgpl-2.1.html
9 
10 #include "ProjectionAux.h"
11 #include "SystemBase.h"
12 #include "libmesh/system.h"
13 
14 registerMooseObjectRenamed("MooseApp", SelfAux, "01/30/2024 24:00", ProjectionAux);
16 
19 {
21  params.addClassDescription(
22  "Returns the specified variable as an auxiliary variable with a projection of the source "
23  "variable. If they are the same type, this amounts to a simple copy.");
24  params.addRequiredCoupledVar("v", "Variable to take the value of.");
25 
26  params.addParam<bool>("use_block_restriction_for_source",
27  false,
28  "Whether to use the auxkernel block restriction to also restrict the "
29  "locations selected for source variable values");
30 
31  // Technically possible to project from nodal to elemental and back
32  params.set<bool>("_allow_nodal_to_elemental_coupling") = true;
33 
34  // We need some ghosting for all elemental to nodal projections
35  params.addParam<unsigned short>("ghost_layers", 1, "The number of layers of elements to ghost.");
36  params.addRelationshipManager("ElementPointNeighborLayers",
38  [](const InputParameters & obj_params, InputParameters & rm_params)
39  {
40  rm_params.set<unsigned short>("layers") =
41  obj_params.get<unsigned short>("ghost_layers");
42  rm_params.set<bool>("use_displaced_mesh") =
43  obj_params.get<bool>("use_displaced_mesh");
44  });
45  return params;
46 }
47 
49  : AuxKernel(parameters),
50  _v(coupledValue("v")),
51  _source_variable(*getFieldVar("v", 0)),
52  _source_sys(_c_fe_problem.getSystem(coupledName("v"))),
53  _use_block_restriction_for_source(getParam<bool>("use_block_restriction_for_source"))
54 {
55  // Output some messages to user
56  if (_source_variable.order() > _var.order())
57  mooseInfo("Projection lowers order, please expect a loss of accuracy");
58  // Check the dimension of the block restriction
59  for (const auto & sub_id : blockIDs())
60  if (_mesh.isLowerD(sub_id))
61  paramError("block",
62  "ProjectionAux's block restriction must not include lower dimensional blocks");
63 }
64 
65 Real
67 {
69  return _v[_qp];
70  // projecting continuous elemental variable onto a nodal one
71  // AND nodal low order -> nodal higher order
74  {
75  return _source_sys.point_value(
77  }
78  // Handle discontinuous elemental variable projection into a nodal variable
79  else
80  {
81  // Custom projection rule : use nodal values, if discontinuous the one coming from each element,
82  // weighted by element volumes
83  // First, find all the elements that this node is part of
84  auto elem_ids = _mesh.nodeToElemMap().find(_current_node->id());
85  mooseAssert(elem_ids != _mesh.nodeToElemMap().end(),
86  "Should have found an element around node " + std::to_string(_current_node->id()));
87 
88  // Get the neighbor element centroid values & element volumes
89  Real sum_weighted_values = 0;
90  Real sum_volumes = 0;
91  for (auto & id : elem_ids->second)
92  {
93  const auto & elem = _mesh.elemPtr(id);
94  const auto block_id = elem->subdomain_id();
95  // Only use higher D elements
96  // We allow full-dimensional elements in a higher dimension mesh
97  if (_source_variable.hasBlocks(block_id) && (!_mesh.isLowerD(block_id)) &&
99  {
100  const auto elem_volume = elem->volume();
101  sum_weighted_values +=
103  sum_volumes += elem_volume;
104  }
105  }
106  if (sum_volumes == 0)
107  mooseError("Did not find a valid source variable value for node: ", *_current_node);
108  return sum_weighted_values / sum_volumes;
109  }
110 }
111 
112 const Elem *
114 {
115  for (const auto & elem_id : _mesh.nodeToElemMap().find(_current_node->id())->second)
116  {
117  const auto & elem = _mesh.elemPtr(elem_id);
118  const auto block_id = elem->subdomain_id();
119  if (_source_variable.hasBlocks(block_id) &&
120  (!_mesh.isLowerD(block_id) && elem->dim() == _mesh.dimension()) &&
122  return elem;
123  }
124  mooseError("Source variable is not defined everywhere the target variable is");
125 }
Projects from one variable to another.
Definition: ProjectionAux.h:22
virtual bool isNodal() const
Is this variable nodal.
registerMooseObject("MooseApp", ProjectionAux)
virtual Real computeValue() override
Compute and return the value of the aux variable.
Definition: ProjectionAux.C:66
const VariableValue & _v
The variable to project from.
Definition: ProjectionAux.h:33
bool _use_block_restriction_for_source
Whether to use the auxkernel block restriction to limit the values for the source variables...
Definition: ProjectionAux.h:43
virtual Elem * elemPtr(const dof_id_type i)
Definition: MooseMesh.C:3108
MooseMesh & _mesh
Mesh this kernel is active on.
Definition: AuxKernel.h:188
unsigned int number() const
Get variable number coming from libMesh.
std::vector< std::pair< R1, R2 > > get(const std::string &param1, const std::string &param2) const
Combine two vector parameters into a single vector of pairs.
void mooseInfo(Args &&... args) const
virtual libMesh::FEContinuity getContinuity() const
Return the continuity of this variable.
const Node *const & _current_node
Current node (valid only for nodal kernels)
Definition: AuxKernel.h:214
T & set(const std::string &name, bool quiet_mode=false)
Returns a writable reference to the named parameters.
Number point_value(unsigned int var, const Point &p, const bool insist_on_success=true, const NumericVector< Number > *sol=nullptr) const
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
virtual const std::set< SubdomainID > & blockIDs() const
Return the block subdomain ids for this object Note, if this is not block restricted, this function returns all mesh subdomain ids.
void addRelationshipManager(const std::string &name, Moose::RelationshipManagerType rm_type, Moose::RelationshipManagerInputParameterCallback input_parameter_callback=nullptr)
Tells MOOSE about a RelationshipManager that this object needs.
DISCONTINUOUS
bool isLowerD(const SubdomainID subdomain_id) const
Definition: MooseMesh.h:2197
SIDE_DISCONTINUOUS
virtual unsigned int dimension() const
Returns MeshBase::mesh_dimension(), (not MeshBase::spatial_dimension()!) of the underlying libMesh me...
Definition: MooseMesh.C:2923
ProjectionAux(const InputParameters &parameters)
Definition: ProjectionAux.C:48
const libMesh::System & _source_sys
The system owning the source variable.
Definition: ProjectionAux.h:40
void paramError(const std::string &param, Args... args) const
Emits an error prefixed with the file and line number of the given param (from the input file) along ...
const Elem * elemOnNodeVariableIsDefinedOn() const
For a node, finds an element we can use to evaluate the (continuous) source variable.
void addRequiredCoupledVar(const std::string &name, const std::string &doc_string)
This method adds a coupled variable name pair.
static InputParameters validParams()
Definition: ProjectionAux.C:18
libMesh::Order order() const
Get the order of this variable Note: Order enum can be implicitly converted to unsigned int...
registerMooseObjectRenamed("MooseApp", SelfAux, "01/30/2024 24:00", ProjectionAux)
MooseVariableField< Real > & _var
This is a regular kernel so we cast to a regular MooseVariable.
Definition: AuxKernel.h:174
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
const MooseVariableFieldBase & _source_variable
A reference to the variable to project from We must use a field variable to support finite volume var...
Definition: ProjectionAux.h:37
void mooseError(Args &&... args) const
Emits an error prefixed with object name and type.
unsigned int _qp
Quadrature point index.
Definition: AuxKernel.h:230
void addClassDescription(const std::string &doc_string)
This method adds a description of the class that will be displayed in the input file syntax dump...
void addParam(const std::string &name, const S &value, const std::string &doc_string)
These methods add an optional parameter and a documentation string to the InputParameters object...
static InputParameters validParams()
Definition: AuxKernel.C:27
virtual const OutputTools< Real >::VariableSecond & second()
The second derivative of the variable this object is operating on.
Base class for creating new auxiliary kernels and auxiliary boundary conditions.
Definition: AuxKernel.h:36
bool hasBlocks(const SubdomainName &name) const
Test if the supplied block name is valid for this object.
bool isNodal() const
Nodal or elemental kernel?
Definition: AuxKernel.h:86
const std::map< dof_id_type, std::vector< dof_id_type > > & nodeToElemMap()
If not already created, creates a map from every node to all elements to which they are connected...
Definition: MooseMesh.C:1175