https://mooseframework.inl.gov
NSPressurePin.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 "NSPressurePin.h"
11 #include "SubProblem.h"
12 #include "SystemBase.h"
13 #include "NS.h"
14 
15 #include "libmesh/mesh_base.h"
16 #include "libmesh/elem_range.h"
17 #include "libmesh/numeric_vector.h"
18 #include "libmesh/mesh_tools.h"
19 
20 using namespace libMesh;
21 
22 registerMooseObject("NavierStokesApp", NSPressurePin);
23 registerMooseObjectRenamed("NavierStokesApp", NSFVPressurePin, "01/19/2025 00:00", NSPressurePin);
24 
27 {
28  auto params = GeneralUserObject::validParams();
30 
31  // Not much flexibility there, applying the pin at the wrong time prevents convergence
32  ExecFlagEnum & exec_enum = params.set<ExecFlagEnum>("execute_on", true);
33  // all bad choices
35  exec_enum = {EXEC_TIMESTEP_END};
36 
37  // Avoid uninitialized residual objects
38  params.suppressParameter<bool>("force_preic");
39 
40  params.addParam<NonlinearVariableName>("variable", NS::pressure, "Pressure variable");
41  params.addParam<PostprocessorName>("phi0", "0", "Pressure pin value");
42  MooseEnum pin_types("point-value average");
43  params.addRequiredParam<MooseEnum>("pin_type", pin_types, "How to pin the pressure");
44  params.addParam<Point>(
45  "point",
46  "The XYZ coordinates of a point inside an element where the pinned value shall be enforced.");
47  params.addParam<PostprocessorName>(
48  "pressure_average", "A postprocessor that computes the average of the pressure variable");
49 
50  params.addClassDescription("Pins the pressure after a solve");
51  params.registerBase("Corrector");
52 
53  return params;
54 }
55 
57  : GeneralUserObject(params),
58  BlockRestrictable(this),
60  _mesh(UserObject::_subproblem.mesh().getMesh()),
61  _p(UserObject::_subproblem.getVariable(0, getParam<NonlinearVariableName>("variable"))),
62  _p0(getPostprocessorValue("phi0")),
63  _pressure_pin_type(getParam<MooseEnum>("pin_type")),
64  _pressure_pin_point(_pressure_pin_type == "point-value" ? getParam<Point>("point")
65  : Point(0, 0, 0)),
66  _current_pressure_average(
67  _pressure_pin_type == "average" ? &getPostprocessorValue("pressure_average") : nullptr)
68 {
69 }
70 
71 void
73 {
74  mooseAssert(!Threads::in_threads, "paramError is not safe in threaded mode");
75 
76  // Check execute_on of the postprocessor
77  if (_pressure_pin_type == "average" &&
78  !_fe_problem.getUserObjectBase(getParam<PostprocessorName>("pressure_average"))
81  paramError("pressure_average",
82  "Pressure average postprocessor must include the pin execute_on flags");
83 }
84 
85 void
87 {
88  // Get the value of the pin
89  Real pin_value = 0;
90  if (_pressure_pin_type == "point-value")
91  {
92  Real point_value = _sys.system().point_value(_p.number(), _pressure_pin_point, false);
93 
98  if (MooseUtils::absoluteFuzzyEqual(point_value, 0.0))
99  {
100  auto pl = _mesh.sub_point_locator();
101  pl->enable_out_of_mesh_mode();
102 
103  auto * elem = (*pl)(_pressure_pin_point);
104  auto elem_id = elem ? elem->id() : DofObject::invalid_id;
105  gatherMin(elem_id);
106 
107  if (elem_id == DofObject::invalid_id)
108  mooseError("No element to gather point pressure from located at ", _pressure_pin_point);
109  // Default at construction
110  pl->disable_out_of_mesh_mode();
111  }
112 
113  pin_value = _p0 - point_value;
114  }
115  else
116  pin_value = _p0 - *_current_pressure_average;
117 
118  // Offset the entire pressure vector by the value of the pin
120  std::set<dof_id_type> local_dofs;
121  _sys.system().local_dof_indices(_p.number(), local_dofs);
122  for (const auto dof : local_dofs)
123  sln.add(dof, pin_value);
124  sln.close();
125  _sys.system().update();
126 }
void local_dof_indices(const unsigned int var, std::set< dof_id_type > &var_indices) const
bool absoluteFuzzyEqual(const T &var1, const T2 &var2, const T3 &tol=libMesh::TOLERANCE *libMesh::TOLERANCE)
virtual void initialSetup() override
Definition: NSPressurePin.C:72
const PostprocessorValue & _p0
Value of the pressure pin.
Definition: NSPressurePin.h:44
std::unique_ptr< PointLocatorBase > sub_point_locator() const
void paramError(const std::string &param, Args... args) const
T & getMesh(MooseMesh &mesh)
function to cast mesh
Definition: SCM.h:35
static InputParameters validParams()
NumericVector< Number > & solution()
unsigned int number() const
const ExecFlagType EXEC_NONE
virtual libMesh::System & system()=0
MeshBase & mesh
const PostprocessorValue *const _current_pressure_average
If using average pressure pin, provides the average pressure value.
Definition: NSPressurePin.h:53
Number point_value(unsigned int var, const Point &p, const bool insist_on_success=true, const NumericVector< Number > *sol=nullptr) const
void gatherMin(T &value)
const ExecFlagType EXEC_TIMESTEP_END
NSPressurePin(const InputParameters &params)
Definition: NSPressurePin.C:56
The following methods are specializations for using the Parallel::packed_range_* routines for a vecto...
static InputParameters validParams()
const ExecFlagType EXEC_TIMESTEP_BEGIN
registerMooseObjectRenamed("NavierStokesApp", NSFVPressurePin, "01/19/2025 00:00", NSPressurePin)
const ExecFlagEnum & getExecuteOnEnum() const
SystemBase & _sys
bool isValueSet(const std::string &value) const
static InputParameters validParams()
Definition: NSPressurePin.C:26
const ExecFlagType EXEC_LINEAR
const Point _pressure_pin_point
If using point-value pressure pin, the point at which to apply the pin.
Definition: NSPressurePin.h:50
virtual void close()=0
virtual void update()
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
FEProblemBase & _fe_problem
This user-object corrects the pressure.
Definition: NSPressurePin.h:23
static const std::string pressure
Definition: NS.h:56
void mooseError(Args &&... args) const
void removeAvailableFlags(const ExecFlagType &flag, Args... flags)
const MooseEnum _pressure_pin_type
Pressure pin type.
Definition: NSPressurePin.h:47
virtual void execute() override
Definition: NSPressurePin.C:86
const MooseVariableFieldBase & _p
The thread 0 copy of the pressure variable.
Definition: NSPressurePin.h:41
const UserObject & getUserObjectBase(const std::string &name, const THREAD_ID tid=0) const
virtual void add(const numeric_index_type i, const T value)=0
registerMooseObject("NavierStokesApp", NSPressurePin)
MeshBase & _mesh
LibMesh mesh class for the current simulation mesh.
Definition: NSPressurePin.h:38