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  MooseEnum elem_to_node_projection_weighting("volume identity", "volume");
35  params.addParam<MooseEnum>("elem_to_node_projection_weighting",
36  elem_to_node_projection_weighting,
37  "How to weight individual element contributions when projecting to a "
38  "nodal degree of freedom");
39 
40  // We need some ghosting for all elemental to nodal projections
42  "GhostAllPointNeighbors",
44  [](const InputParameters & obj_params, InputParameters & rm_params)
45  { rm_params.set<bool>("use_displaced_mesh") = obj_params.get<bool>("use_displaced_mesh"); });
46  return params;
47 }
48 
50  : AuxKernel(parameters),
51  _v(coupledValue("v")),
52  _source_variable(*getFieldVar("v", 0)),
53  _source_sys(_c_fe_problem.getSystem(coupledName("v"))),
54  _use_block_restriction_for_source(getParam<bool>("use_block_restriction_for_source")),
55  _elem_to_node_projection_weighting(getParam<MooseEnum>("elem_to_node_projection_weighting")
57 {
58  // Output some messages to user
59  if (_source_variable.order() > _var.order())
60  mooseInfo("Projection lowers order, please expect a loss of accuracy");
61 }
62 
63 Real
65 {
67  return _v[_qp];
68  // projecting continuous variable onto a nodal one
69  // AND projecting from low order -> nodal higher order
72  return _source_sys.point_value(
74  // Handle discontinuous elemental variable projection into a nodal variable
75  else
76  {
77  // Custom projection rule : use nodal values, if discontinuous the one coming from each element,
78  // weighted by element volumes
79  // First, find all the elements that this node is part of
80  const auto & elem_ids = libmesh_map_find(_mesh.nodeToElemMap(), _current_node->id());
81 
82  // Get the neighbor element centroid values & element volumes
83  Real sum_weighted_values = 0;
84  Real sum_weights = 0;
85  _elem_dims.clear();
86  for (const auto id : elem_ids)
87  {
88  const auto * const elem = _mesh.elemPtr(id);
89  const auto block_id = elem->subdomain_id();
90  if (_source_variable.hasBlocks(block_id) &&
92  {
94  _elem_dims.insert(elem->dim());
95  const auto elem_weight =
97  sum_weighted_values +=
99  sum_weights += elem_weight;
100  }
101  }
102  if (sum_weights == 0)
103  mooseError("Did not find a valid source variable value for node: ", *_current_node);
105  mooseError("We should not use multiple element dimensions when computing the volume weighted "
106  "projection as the units do not make sense");
107  return sum_weighted_values / sum_weights;
108  }
109 }
110 
111 const Elem *
113 {
114  for (const auto & elem_id : _mesh.nodeToElemMap().find(_current_node->id())->second)
115  {
116  const auto & elem = _mesh.elemPtr(elem_id);
117  const auto block_id = elem->subdomain_id();
118  if (_source_variable.hasBlocks(block_id) &&
120  return elem;
121  }
122  mooseError("Source variable is not defined everywhere the target variable is");
123 }
Projects from one variable to another.
Definition: ProjectionAux.h:22
void mooseInfo(Args &&... args) const
Definition: MooseBase.h:344
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:64
const VariableValue & _v
The variable to project from.
Definition: ProjectionAux.h:39
bool _use_block_restriction_for_source
Whether to use the auxkernel block restriction to limit the values for the source variables...
Definition: ProjectionAux.h:49
virtual Elem * elemPtr(const dof_id_type i)
Definition: MooseMesh.C:3240
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.
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:138
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...
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.
MooseMesh & _mesh
Mesh this kernel is active on.
DISCONTINUOUS
SIDE_DISCONTINUOUS
const ElemToNodeProjectionWeighting _elem_to_node_projection_weighting
How to weight element to node projections.
Definition: ProjectionAux.h:52
ProjectionAux(const InputParameters &parameters)
Definition: ProjectionAux.C:49
const libMesh::System & _source_sys
The system owning the source variable.
Definition: ProjectionAux.h:46
This is a "smart" enum class intended to replace many of the shortcomings in the C++ enum type It sho...
Definition: MooseEnum.h:54
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, hides base _var.
Definition: AuxKernel.h:108
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:43
void mooseError(Args &&... args) const
Emits an error prefixed with object name and type and optionally a file path to the top-level block p...
Definition: MooseBase.h:281
unsigned int _qp
Quadrature point index.
Definition: AuxKernel.h:150
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.
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:43
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:1201
std::unordered_set< unsigned short > _elem_dims
Set for holding element dimensions when mapping from multiple element values to a node...
Definition: ProjectionAux.h:62