www.mooseframework.org
RichardsMultiphaseProblem.C
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://www.mooseframework.org
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 
11 
12 // MOOSE includes
13 #include "MooseMesh.h"
14 #include "MooseVariable.h"
15 #include "NonlinearSystem.h"
16 
18 
19 template <>
20 InputParameters
22 {
23  InputParameters params = validParams<FEProblemBase>();
24  params.addRequiredParam<NonlinearVariableName>(
25  "bounded_var", "Variable whose value will be constrained to be greater than lower_var");
26  params.addRequiredParam<NonlinearVariableName>(
27  "lower_var",
28  "Variable that acts as a lower bound to bounded_var. It will not be "
29  "constrained during the solution procedure");
30  return params;
31 }
32 
34  : FEProblem(params),
35  // in the following have to get the names of the variables, and then find their numbers in
36  // initialSetup,
37  // as their numbers won't be defined at the moment of instantiation of this class
38  _bounded_var_name(params.get<NonlinearVariableName>("bounded_var")),
39  _lower_var_name(params.get<NonlinearVariableName>("lower_var")),
40  _bounded_var_num(0),
41  _lower_var_num(0)
42 {
43 }
44 
46 
47 void
49 {
50  // the first argument to getVariable is threadID - i hope the following always works
51  unsigned int tid = 0;
52 
53  // We are going to do more specific checks below, hence allowing
54  // more specific error messages to be printed in case something goes
55  // wrong. Therefore we just pass VAR_ANY here.
56  MooseVariableFEBase & bounded = getVariable(
57  tid, _bounded_var_name, Moose::VarKindType::VAR_ANY, Moose::VarFieldType::VAR_FIELD_STANDARD);
58  MooseVariableFEBase & lower = getVariable(
59  tid, _lower_var_name, Moose::VarKindType::VAR_ANY, Moose::VarFieldType::VAR_FIELD_STANDARD);
60 
61  // some checks
62  if (!bounded.isNodal() || !lower.isNodal())
63  mooseError("Both the bounded and lower variables must be nodal variables in "
64  "RichardsMultiphaseProblem");
65  if (bounded.feType().family != lower.feType().family)
66  mooseError("Both the bounded and lower variables must belong to the same element family, eg "
67  "LAGRANGE, in RichardsMultiphaseProblem");
68  if (bounded.feType().order != lower.feType().order)
69  mooseError("Both the bounded and lower variables must have the same order, eg FIRST, in "
70  "RichardsMultiphaseProblem");
71 
72  // extract the required info
73  _bounded_var_num = bounded.number();
74  _lower_var_num = lower.number();
75 
76  FEProblemBase::initialSetup();
77 }
78 
79 bool
81 {
82  return true;
83 }
84 
85 bool
86 RichardsMultiphaseProblem::updateSolution(NumericVector<Number> & vec_solution,
87  NumericVector<Number> & ghosted_solution)
88 {
89  bool updatedSolution =
90  false; // this gets set to true if we needed to enforce the bound at any node
91 
92  unsigned int sys_num = getNonlinearSystemBase().number();
93 
94  // For parallel procs i believe that i have to use local_nodes_begin, rather than just nodes_begin
95  // _mesh comes from SystemBase (_mesh = getNonlinearSystemBase().subproblem().mesh(), and
96  // subproblem is this object)
97  for (const auto & node : _mesh.getMesh().local_node_ptr_range())
98  {
99  // dofs[0] is the dof number of the bounded variable at this node
100  // dofs[1] is the dof number of the lower variable at this node
101  std::vector<dof_id_type> dofs(2);
102  dofs[0] = node->dof_number(sys_num, _bounded_var_num, 0);
103  dofs[1] = node->dof_number(sys_num, _lower_var_num, 0);
104 
105  // soln[0] is the value of the bounded variable at this node
106  // soln[1] is the value of the lower variable at this node
107  std::vector<Number> soln(2);
108  vec_solution.get(dofs, soln);
109 
110  // do the bounding
111  if (soln[0] < soln[1])
112  {
113  vec_solution.set(dofs[0], soln[1]); // set the bounded variable equal to the lower value
114  updatedSolution = true;
115  }
116  }
117 
118  // The above vec_solution.set calls potentially added "set" commands to a queue
119  // The following actions the queue (doing MPI commands if necessary), so
120  // vec_solution will actually be modified by this following command
121  vec_solution.close();
122 
123  // if any proc updated the solution, all procs will know about it
124  _communicator.max(updatedSolution);
125 
126  if (updatedSolution)
127  {
128  ghosted_solution = vec_solution;
129  ghosted_solution.close();
130  }
131 
132  return updatedSolution;
133 }
RichardsMultiphaseProblem.h
RichardsMultiphaseProblem::initialSetup
virtual void initialSetup()
extracts the moose variable numbers associated with bounded_var and lower_var
Definition: RichardsMultiphaseProblem.C:48
RichardsMultiphaseProblem::updateSolution
virtual bool updateSolution(NumericVector< Number > &vec_solution, NumericVector< Number > &ghosted_solution)
Does the bounding by modifying vec_solution, and then ghosted_solution.
Definition: RichardsMultiphaseProblem.C:86
RichardsMultiphaseProblem::_bounded_var_name
NonlinearVariableName _bounded_var_name
name of the bounded variable (this is the variable that gets altered to respect bounded_var > lower_v...
Definition: RichardsMultiphaseProblem.h:49
RichardsMultiphaseProblem
Allows a constraint u>=v to be enforced during the nonlinear iteration process.
Definition: RichardsMultiphaseProblem.h:24
validParams< RichardsMultiphaseProblem >
InputParameters validParams< RichardsMultiphaseProblem >()
Definition: RichardsMultiphaseProblem.C:21
RichardsMultiphaseProblem::_lower_var_num
unsigned int _lower_var_num
internal moose variable number associated with _lower_var
Definition: RichardsMultiphaseProblem.h:58
RichardsMultiphaseProblem::_lower_var_name
NonlinearVariableName _lower_var_name
name of the variable that acts as the lower bound to bounded_var
Definition: RichardsMultiphaseProblem.h:52
RichardsMultiphaseProblem::~RichardsMultiphaseProblem
virtual ~RichardsMultiphaseProblem()
Definition: RichardsMultiphaseProblem.C:45
registerMooseObject
registerMooseObject("RichardsApp", RichardsMultiphaseProblem)
RichardsMultiphaseProblem::_bounded_var_num
unsigned int _bounded_var_num
internal moose variable number associated with _bounded_var
Definition: RichardsMultiphaseProblem.h:55
RichardsMultiphaseProblem::shouldUpdateSolution
virtual bool shouldUpdateSolution()
returns true, indicating that updateSolution should be run
Definition: RichardsMultiphaseProblem.C:80
RichardsMultiphaseProblem::RichardsMultiphaseProblem
RichardsMultiphaseProblem(const InputParameters &params)
Definition: RichardsMultiphaseProblem.C:33