www.mooseframework.org
MutableCoefficientsInterface.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 
10 #include <iostream>
11 #include <iomanip>
12 
13 #include "MooseObject.h"
14 
16 
17 template <>
18 InputParameters
20 {
21  InputParameters params = emptyInputParameters();
22 
23  params.addClassDescription("This interface universalizes the communication standards for "
24  "array-based coefficient transfers.");
25 
26  params.addParam<bool>("print_when_set", false, "Print the array of coefficients when set");
27 
28  return params;
29 }
30 
32  const InputParameters & parameters)
33  : Restartable(moose_object->getMooseApp(),
34  moose_object->name() + "_coefs",
35  "MutableCoefficientsInterface",
36  moose_object->parameters().get<THREAD_ID>("_tid")),
37  _characteristics(declareRestartableData<std::vector<std::size_t>>("characteristics")),
38  _coefficients(declareRestartableData<std::vector<Real>>("coefficients")),
39  _enforce_size(false),
40  _print_coefficients(parameters.get<bool>("print_when_set")),
41  _console(moose_object->_console)
42 {
43 }
44 
45 Real MutableCoefficientsInterface::operator[](std::size_t index) const
46 {
47  return _coefficients[index];
48 }
49 
50 const std::vector<std::size_t> &
52 {
53  return _characteristics;
54 }
55 
56 const std::vector<Real> &
58 {
59  return _coefficients;
60 }
61 
62 std::vector<Real> &
64 {
65  return _coefficients;
66 }
67 
68 std::string
70 {
71  std::stringbuf string;
72  std::ostream table(&string);
73 
74  table << *this;
75 
76  return string.str();
77 }
78 
79 std::size_t
81 {
82  return _coefficients.size();
83 }
84 
85 bool
87 {
88  // Check the coefficient sizes if requested
89  if ((_enforce_size && other._enforce_size) && getSize() != other.getSize())
90  return false;
91 
92  // Check the size of the characteristics array
93  if (_characteristics.size() != other._characteristics.size())
94  return false;
95 
96  // Check the values of the characteristics array
97  for (std::size_t i = 0; i < _characteristics.size(); ++i)
98  if (_characteristics[i] != other._characteristics[i])
99  return false;
100 
101  return true;
102 }
103 
104 bool
106 {
107  return _enforce_size;
108 }
109 
110 void
112 {
113  _enforce_size = enforce;
114 }
115 
116 void
118 {
119  if (!isCompatibleWith(other))
120  mooseError("Cannot import coefficients from incompatible MutableCoefficientsInterface");
121 
123 
125  _console << *this;
126 
128 }
129 
130 void
131 MutableCoefficientsInterface::resize(std::size_t size, Real fill, bool fill_out_to_size)
132 {
133  if (size != _coefficients.size())
134  {
135  if (_enforce_size &&
136  (size > _coefficients.size() || (size < _coefficients.size() && !fill_out_to_size)))
137  mooseError("Cannot resize coefficient array with size enforcement enabled.");
138 
139  _coefficients.resize(size, fill);
140 
142  _console << *this;
143 
145  }
146 }
147 
148 void
150  const std::vector<std::size_t> & new_characteristics)
151 {
152  _characteristics = new_characteristics;
153 }
154 
155 void
156 MutableCoefficientsInterface::setCoefficients(const std::vector<Real> & new_coefficients)
157 {
158  if (_enforce_size && new_coefficients.size() != _coefficients.size())
159  mooseError("Cannon assigned a coefficient array with differing size when size enforcement is "
160  "enabled.");
161 
162  _coefficients = new_coefficients;
163 
165  _console << *this;
166 
168 }
169 
170 void
171 MutableCoefficientsInterface::setCoefficients(std::vector<Real> && dropin_coefficients)
172 {
173  if (_enforce_size && dropin_coefficients.size() != _coefficients.size())
174  mooseError("Cannon assigned a coefficient array with differing size when size enforcement is "
175  "enabled.");
176 
177  _coefficients = dropin_coefficients;
178 
180  _console << *this;
181 
183 }
184 
185 std::ostream &
186 operator<<(std::ostream & stream, const MutableCoefficientsInterface & me)
187 {
188  const MooseObject * myself_again = dynamic_cast<const MooseObject *>(&me);
189  stream << "\n\n"
190  << "MutableCoefficientsInterface: " << (myself_again ? myself_again->name() : "Unknown")
191  << "\n"
192  << " Number of Coefficients: " << me.getSize() << "\n";
193 
194  for (std::size_t i = 0; i < me.getSize(); ++i)
195  stream << std::setw(4) << i << ": " << std::setw(12) << me[i] << ((i % 6 == 5) ? "\n" : " ");
196 
197  stream << "\n\n";
198 
199  return stream;
200 }
MutableCoefficientsInterface::getCharacteristics
const std::vector< std::size_t > & getCharacteristics() const
Get a reference to the characteristics array.
Definition: MutableCoefficientsInterface.C:51
MutableCoefficientsInterface::_print_coefficients
const bool _print_coefficients
Boolean to flag if the coefficients should be printed when set.
Definition: MutableCoefficientsInterface.h:119
MutableCoefficientsInterface::setCharacteristics
void setCharacteristics(const std::vector< std::size_t > &new_characteristics)
Sets the characteristics array.
Definition: MutableCoefficientsInterface.C:149
operator<<
std::ostream & operator<<(std::ostream &stream, const MutableCoefficientsInterface &me)
Definition: MutableCoefficientsInterface.C:186
MutableCoefficientsInterface::resize
void resize(std::size_t size, Real fill=0.0, bool fill_out_to_size=true)
Resize the array, using the value for fill if the new size is larger.
Definition: MutableCoefficientsInterface.C:131
MutableCoefficientsInterface::_console
const ConsoleStream & _console
MooseObject instance of this to provide access to _console
Definition: MutableCoefficientsInterface.h:123
MutableCoefficientsInterface::importCoefficients
void importCoefficients(const MutableCoefficientsInterface &other)
Import the coefficients from another instance.
Definition: MutableCoefficientsInterface.C:117
validParams< MutableCoefficientsInterface >
InputParameters validParams< MutableCoefficientsInterface >()
Definition: MutableCoefficientsInterface.C:19
MutableCoefficientsInterface::getCoefficients
const std::vector< Real > & getCoefficients() const
Get a read-only reference to the vector of coefficients.
Definition: MutableCoefficientsInterface.C:57
MutableCoefficientsInterface::_coefficients
std::vector< Real > & _coefficients
The coefficient array.
Definition: MutableCoefficientsInterface.h:113
MutableCoefficientsInterface::_characteristics
std::vector< std::size_t > & _characteristics
An array of integer characteristics that can be used to check compatibility.
Definition: MutableCoefficientsInterface.h:107
MutableCoefficientsInterface::getCoefficientsTable
std::string getCoefficientsTable() const
Get a formatted string of the coefficients.
Definition: MutableCoefficientsInterface.C:69
MutableCoefficientsInterface::getSize
std::size_t getSize() const
Get the size, aka number of coefficients.
Definition: MutableCoefficientsInterface.C:80
name
const std::string name
Definition: Setup.h:21
MutableCoefficientsInterface::enforceSize
void enforceSize(bool enforce)
Toggle whether the size of the coefficient array can be changed.
Definition: MutableCoefficientsInterface.C:111
MutableCoefficientsInterface.h
MutableCoefficientsInterface::coefficientsChanged
virtual void coefficientsChanged()
Called when the coefficients have been changed.
Definition: MutableCoefficientsInterface.h:107
MutableCoefficientsInterface::operator[]
Real operator[](std::size_t index) const
Get the value of the coefficient at the corresponding index.
Definition: MutableCoefficientsInterface.C:45
MutableCoefficientsInterface::setCoefficients
void setCoefficients(const std::vector< Real > &new_coefficients)
Set the coefficients using a copy operation.
Definition: MutableCoefficientsInterface.C:156
MutableCoefficientsInterface::MutableCoefficientsInterface
MutableCoefficientsInterface(const MooseObject *moose_object, const InputParameters &parameters)
Definition: MutableCoefficientsInterface.C:31
MutableCoefficientsInterface
This class is designed to provide a uniform interface for any class that uses an array of coefficient...
Definition: MutableCoefficientsInterface.h:30
MutableCoefficientsInterface::isCompatibleWith
bool isCompatibleWith(const MutableCoefficientsInterface &other) const
Checks to see if another instance is compatible.
Definition: MutableCoefficientsInterface.C:86
MutableCoefficientsInterface::isSizeEnforced
bool isSizeEnforced() const
Returns true if the size of the coefficient array is fixed and enforced.
Definition: MutableCoefficientsInterface.C:105
MutableCoefficientsInterface::_enforce_size
bool _enforce_size
Boolean that locks or allows resizing of the coefficient array.
Definition: MutableCoefficientsInterface.h:116