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  _sys(*getCheckedPointerParam<SystemBase *>("_sys"))
69 {
70 }
71 
72 void
74 {
75  mooseAssert(!Threads::in_threads, "paramError is not safe in threaded mode");
76 
77  // Check execute_on of the postprocessor
78  if (_pressure_pin_type == "average" &&
79  !_fe_problem.getUserObjectBase(getParam<PostprocessorName>("pressure_average"))
82  paramError("pressure_average",
83  "Pressure average postprocessor must include the pin execute_on flags");
84 }
85 
86 void
88 {
89  // Get the value of the pin
90  Real pin_value = 0;
91  if (_pressure_pin_type == "point-value")
92  {
93  Real point_value = _sys.system().point_value(_p.number(), _pressure_pin_point, false);
94 
99  if (MooseUtils::absoluteFuzzyEqual(point_value, 0.0))
100  {
101  auto pl = _mesh.sub_point_locator();
102  pl->enable_out_of_mesh_mode();
103 
104  auto * elem = (*pl)(_pressure_pin_point);
105  auto elem_id = elem ? elem->id() : DofObject::invalid_id;
106  gatherMin(elem_id);
107 
108  if (elem_id == DofObject::invalid_id)
109  mooseError("No element to gather point pressure from located at ", _pressure_pin_point);
110  // Default at construction
111  pl->disable_out_of_mesh_mode();
112  }
113 
114  pin_value = _p0 - point_value;
115  }
116  else
117  pin_value = _p0 - *_current_pressure_average;
118 
119  // Offset the entire pressure vector by the value of the pin
121  std::set<dof_id_type> local_dofs;
122  _sys.system().local_dof_indices(_p.number(), local_dofs);
123  for (const auto dof : local_dofs)
124  sln.add(dof, pin_value);
125  sln.close();
126  _sys.system().update();
127 }
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:73
const PostprocessorValue & _p0
Value of the pressure pin.
Definition: NSPressurePin.h:44
std::unique_ptr< PointLocatorBase > sub_point_locator() 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
void paramError(const std::string &param, Args... args) const
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 removeAvailableFlags(const ExecFlagType &flag, Args... flags)
void mooseError(Args &&... args) const
const MooseEnum _pressure_pin_type
Pressure pin type.
Definition: NSPressurePin.h:47
virtual void execute() override
Definition: NSPressurePin.C:87
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)
SystemBase & _sys
The nonlinear system.
Definition: NSPressurePin.h:57
MeshBase & _mesh
LibMesh mesh class for the current simulation mesh.
Definition: NSPressurePin.h:38