https://mooseframework.inl.gov
FXIntegralBaseUserObject.h
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 #pragma once
11 
12 #include "AuxiliarySystem.h"
13 #include "MooseError.h"
14 #include "MooseMesh.h"
15 #include "MooseVariable.h"
16 #include "UserObject.h"
17 
18 #include "libmesh/quadrature.h"
19 
20 #include "FunctionSeries.h"
22 
31 template <class IntegralBaseVariableUserObject>
32 class FXIntegralBaseUserObject : public IntegralBaseVariableUserObject,
34 {
35 public:
36  FXIntegralBaseUserObject(const InputParameters & parameters);
37 
39 
43  const FunctionSeries & getFunctionSeries() const;
44 
45  // Override from <IntegralBaseVariableUserObject>
46  virtual Real getValue() const final;
47 
48  // Overrides from UserObject
49  virtual void finalize() final;
50  virtual void initialize() final;
51  virtual Real spatialValue(const Point & location) const final;
52  virtual void threadJoin(const UserObject & sibling) final;
53 
54 protected:
55  // Policy-based design requires us to specify which inherited members we are using
56  using IntegralBaseVariableUserObject::_communicator;
57  using IntegralBaseVariableUserObject::_console;
58  using IntegralBaseVariableUserObject::_coord;
59  using IntegralBaseVariableUserObject::_integral_value;
60  using IntegralBaseVariableUserObject::_JxW;
61  using IntegralBaseVariableUserObject::_q_point;
62  using IntegralBaseVariableUserObject::_qp;
63  using IntegralBaseVariableUserObject::_variable;
64  using IntegralBaseVariableUserObject::computeIntegral;
65  using IntegralBaseVariableUserObject::computeQpIntegral;
66  using IntegralBaseVariableUserObject::getFunction;
68 
69  // Override from <IntegralBaseVariableUserObject>
70  virtual Real computeIntegral() final;
71 
75  virtual Point getCentroid() const = 0;
76 
80  virtual Real getVolume() const = 0;
81 
83  std::vector<std::vector<Real>> _coefficient_history;
84 
86  std::vector<Real> _coefficient_partials;
87 
90 
92  const bool _keep_history;
93 
95  const bool _print_state;
96 
99 
102 };
103 
104 template <class IntegralBaseVariableUserObject>
107 {
110 
111  params.addClassDescription(
112  "This UserObject interacts with a MooseApp through functional expansions.");
113 
114  params.addRequiredParam<FunctionName>("function",
115  "The name of the FunctionSeries \"Function\" object with "
116  "which to generate this functional expansion.");
117 
118  params.addParam<bool>(
119  "keep_history", false, "Keep the expansion coefficients from previous solves");
120 
121  params.addParam<bool>("print_state", false, "Print the state of the zeroth instance each solve");
122 
123  return params;
124 }
125 
126 template <class IntegralBaseVariableUserObject>
128  const InputParameters & parameters)
129  : IntegralBaseVariableUserObject(parameters),
130  MutableCoefficientsInterface(this, parameters),
131  _function_series(FunctionSeries::checkAndConvertFunction(
132  getFunction("function"), UserObject::getParam<std::string>("_moose_base"), name())),
133  _keep_history(UserObject::getParam<bool>("keep_history")),
134  _print_state(UserObject::getParam<bool>("print_state")),
135  _standardized_function_volume(_function_series.getStandardizedFunctionVolume())
136 {
137  // Size the coefficient arrays
141 
142  if (!_keep_history)
143  _coefficient_history.resize(0);
144 }
145 
146 template <class IntegralBaseVariableUserObject>
147 Real
149 {
150  Real sum = 0.0;
151  const Point centroid = getCentroid();
152 
153  // Check to see if this element/side is within the valid boundaries
154  if (!_function_series.isInPhysicalBounds(centroid))
155  return 0.0;
156 
157  // Loop over the quadrature points
158  for (_qp = 0; _qp < _q_point.size(); ++_qp)
159  {
160  // Get the functional terms for a vectorized approach
161  _function_series.setLocation(_q_point[_qp]);
162  const std::vector<Real> & term_evaluations = _function_series.getGeneration();
163 
164  // Evaluate the functional expansion coefficients at each quadrature point
165  const Real local_contribution = computeQpIntegral();
166  const Real common_evaluation = local_contribution * _JxW[_qp] * _coord[_qp];
167  for (std::size_t c = 0; c < _coefficient_partials.size(); ++c)
168  _coefficient_partials[c] += term_evaluations[c] * common_evaluation;
169 
170  sum += local_contribution;
171  }
172 
173  _volume += getVolume();
174 
175  return sum;
176 }
177 
178 template <class IntegralBaseVariableUserObject>
179 void
181 {
182  // Sum the coefficient arrays over all processes
183  _communicator.sum(_coefficient_partials);
184  _communicator.sum(_volume);
185 
186  // Normalize the volume of the functional expansion to the FX standard space
187  const Real volume_normalization = _standardized_function_volume / _volume;
188  for (auto & partial : _coefficient_partials)
189  partial *= volume_normalization;
190 
191  // We now have the completely evaluated coefficients
192  _coefficients = _coefficient_partials;
193 
194  // The average value is the same as the zeroth coefficient
195  _integral_value = _coefficient_partials[0];
196 
197  if (_keep_history)
198  _coefficient_history.push_back(_coefficients);
199 
200  if (_print_state)
201  {
202  _function_series.setCoefficients(_coefficients);
203  _console << COLOR_YELLOW << _function_series << COLOR_DEFAULT << std::endl;
204  }
205 }
206 
207 template <class IntegralBaseVariableUserObject>
208 const FunctionSeries &
210 {
211  return _function_series;
212 }
213 
214 template <class IntegralBaseVariableUserObject>
215 Real
217 {
218  return _integral_value;
219 }
220 
221 template <class IntegralBaseVariableUserObject>
222 void
224 {
225  IntegralBaseVariableUserObject::initialize();
226 
227  // Clear the partial sums
228  for (auto & partial : _coefficient_partials)
229  partial = 0;
230 
231  _volume = 0;
232 }
233 
234 template <class IntegralBaseVariableUserObject>
235 void
237 {
240 
241  for (std::size_t c = 0; c < _coefficient_partials.size(); ++c)
242  _coefficient_partials[c] += sibling._coefficient_partials[c];
243 
244  _volume += sibling._volume;
245 }
246 
247 template <class IntegralBaseVariableUserObject>
248 Real
250 {
251  _function_series.setLocation(location);
252 
253  return _function_series.expand(_coefficients);
254 }
virtual Real getVolume() const =0
Get the volume of the evaluated unit.
const bool _keep_history
Keep the expansion coefficients after each solve.
void addParam(const std::string &name, const std::initializer_list< typename T::value_type > &value, const std::string &doc_string)
FXIntegralBaseUserObject(const InputParameters &parameters)
FunctionSeries & _function_series
Reference to the underlying function series.
virtual Real computeIntegral() final
virtual Point getCentroid() const =0
Get the centroid of the evaluated unit.
const FunctionSeries & getFunctionSeries() const
Return a reference to the underlying function series.
void addRequiredParam(const std::string &name, const std::string &doc_string)
virtual void threadJoin(const UserObject &sibling) final
This class uses implementations of CompositeSeriesBasisInterface to generate a function based on conv...
static InputParameters validParams()
InputParameters validParams()
const std::string name
Definition: Setup.h:20
const bool _print_state
Flag to prints the state of the zeroth instance in finalize()
const Real _standardized_function_volume
Volume of the standardized functional space of integration.
std::vector< Real > & _coefficients
The coefficient array.
std::size_t getNumberOfTerms() const
Returns the number of terms (coefficients) in the underlying function series.
virtual Real getValue() const final
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
std::vector< std::size_t > & _characteristics
An array of integer characteristics that can be used to check compatibility.
virtual Real spatialValue(const Point &location) const final
void addClassDescription(const std::string &doc_string)
This class is designed to provide a uniform interface for any class that uses an array of coefficient...
const std::vector< std::size_t > & getOrders() const
Returns a vector of the functional orders in the underlying functional series.
Real _volume
Moose volume of evaluation.
std::vector< std::vector< Real > > _coefficient_history
History of the expansion coefficients for each solve.
This class interacts with a MooseApp through functional expansions.
std::vector< Real > _coefficient_partials
Current coefficient partial sums.