Line data Source code
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 <iostream> 11 : #include <iomanip> 12 : 13 : #include "MooseObject.h" 14 : 15 : #include "MutableCoefficientsInterface.h" 16 : 17 : InputParameters 18 624 : MutableCoefficientsInterface::validParams() 19 : { 20 624 : InputParameters params = emptyInputParameters(); 21 : 22 624 : params.addClassDescription("This interface universalizes the communication standards for " 23 : "array-based coefficient transfers."); 24 : 25 1248 : params.addParam<bool>("print_when_set", false, "Print the array of coefficients when set"); 26 : 27 624 : return params; 28 0 : } 29 : 30 368 : MutableCoefficientsInterface::MutableCoefficientsInterface(const MooseObject * moose_object, 31 368 : const InputParameters & parameters) 32 : : Restartable(moose_object->getMooseApp(), 33 736 : moose_object->name() + "_coefs", 34 : "MutableCoefficientsInterface", 35 368 : moose_object->parameters().get<THREAD_ID>("_tid")), 36 368 : _characteristics(declareRestartableData<std::vector<std::size_t>>("characteristics")), 37 736 : _coefficients(declareRestartableData<std::vector<Real>>("coefficients")), 38 368 : _enforce_size(false), 39 368 : _print_coefficients(parameters.get<bool>("print_when_set")), 40 1472 : _console(moose_object->_console) 41 : { 42 368 : } 43 : 44 : Real 45 7944 : MutableCoefficientsInterface::operator[](std::size_t index) const 46 : { 47 7944 : return _coefficients[index]; 48 : } 49 : 50 : const std::vector<std::size_t> & 51 0 : MutableCoefficientsInterface::getCharacteristics() const 52 : { 53 0 : return _characteristics; 54 : } 55 : 56 : const std::vector<Real> & 57 0 : MutableCoefficientsInterface::getCoefficients() const 58 : { 59 0 : return _coefficients; 60 : } 61 : 62 : std::vector<Real> & 63 0 : MutableCoefficientsInterface::getCoefficients() 64 : { 65 0 : return _coefficients; 66 : } 67 : 68 : std::string 69 0 : MutableCoefficientsInterface::getCoefficientsTable() const 70 : { 71 : std::stringbuf string; 72 0 : std::ostream table(&string); 73 : 74 0 : table << *this; 75 : 76 0 : return string.str(); 77 0 : } 78 : 79 : std::size_t 80 11916 : MutableCoefficientsInterface::getSize() const 81 : { 82 11916 : return _coefficients.size(); 83 : } 84 : 85 : bool 86 19750 : MutableCoefficientsInterface::isCompatibleWith(const MutableCoefficientsInterface & other) const 87 : { 88 : // Check the coefficient sizes if requested 89 19750 : if ((_enforce_size && other._enforce_size) && getSize() != other.getSize()) 90 : return false; 91 : 92 : // Check the size of the characteristics array 93 19750 : if (_characteristics.size() != other._characteristics.size()) 94 : return false; 95 : 96 : // Check the values of the characteristics array 97 39498 : for (std::size_t i = 0; i < _characteristics.size(); ++i) 98 19750 : if (_characteristics[i] != other._characteristics[i]) 99 : return false; 100 : 101 : return true; 102 : } 103 : 104 : bool 105 0 : MutableCoefficientsInterface::isSizeEnforced() const 106 : { 107 0 : return _enforce_size; 108 : } 109 : 110 : void 111 384 : MutableCoefficientsInterface::enforceSize(bool enforce) 112 : { 113 384 : _enforce_size = enforce; 114 384 : } 115 : 116 : void 117 9874 : MutableCoefficientsInterface::importCoefficients(const MutableCoefficientsInterface & other) 118 : { 119 9874 : if (!isCompatibleWith(other)) 120 0 : mooseError("Cannot import coefficients from incompatible MutableCoefficientsInterface"); 121 : 122 9874 : _coefficients = other._coefficients; 123 : 124 9874 : if (_print_coefficients) 125 1046 : _console << *this; 126 : 127 9874 : coefficientsChanged(); 128 9874 : } 129 : 130 : void 131 192 : MutableCoefficientsInterface::resize(std::size_t size, Real fill, bool fill_out_to_size) 132 : { 133 192 : if (size != _coefficients.size()) 134 : { 135 192 : if (_enforce_size && 136 0 : (size > _coefficients.size() || (size < _coefficients.size() && !fill_out_to_size))) 137 0 : mooseError("Cannot resize coefficient array with size enforcement enabled."); 138 : 139 192 : _coefficients.resize(size, fill); 140 : 141 192 : if (_print_coefficients) 142 10 : _console << *this; 143 : 144 192 : coefficientsChanged(); 145 : } 146 192 : } 147 : 148 : void 149 192 : MutableCoefficientsInterface::setCharacteristics( 150 : const std::vector<std::size_t> & new_characteristics) 151 : { 152 192 : _characteristics = new_characteristics; 153 192 : } 154 : 155 : void 156 930 : MutableCoefficientsInterface::setCoefficients(const std::vector<Real> & new_coefficients) 157 : { 158 930 : if (_enforce_size && new_coefficients.size() != _coefficients.size()) 159 0 : mooseError("Cannon assigned a coefficient array with differing size when size enforcement is " 160 : "enabled."); 161 : 162 930 : _coefficients = new_coefficients; 163 : 164 930 : if (_print_coefficients) 165 930 : _console << *this; 166 : 167 930 : coefficientsChanged(); 168 930 : } 169 : 170 : void 171 0 : MutableCoefficientsInterface::setCoefficients(std::vector<Real> && dropin_coefficients) 172 : { 173 0 : if (_enforce_size && dropin_coefficients.size() != _coefficients.size()) 174 0 : mooseError("Cannon assigned a coefficient array with differing size when size enforcement is " 175 : "enabled."); 176 : 177 0 : _coefficients = dropin_coefficients; 178 : 179 0 : if (_print_coefficients) 180 0 : _console << *this; 181 : 182 0 : coefficientsChanged(); 183 0 : } 184 : 185 : std::ostream & 186 1986 : operator<<(std::ostream & stream, const MutableCoefficientsInterface & me) 187 : { 188 1986 : const MooseObject * myself_again = dynamic_cast<const MooseObject *>(&me); 189 : stream << "\n\n" 190 1986 : << "MutableCoefficientsInterface: " << (myself_again ? myself_again->name() : "Unknown") 191 : << "\n" 192 5958 : << " Number of Coefficients: " << me.getSize() << "\n"; 193 : 194 9930 : for (std::size_t i = 0; i < me.getSize(); ++i) 195 23832 : stream << std::setw(4) << i << ": " << std::setw(12) << me[i] << ((i % 6 == 5) ? "\n" : " "); 196 : 197 1986 : stream << "\n\n" << std::flush; 198 : 199 1986 : return stream; 200 : }