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 1208 : MutableCoefficientsInterface::validParams() 19 : { 20 1208 : InputParameters params = emptyInputParameters(); 21 : 22 1208 : params.addClassDescription("This interface universalizes the communication standards for " 23 : "array-based coefficient transfers."); 24 : 25 2416 : params.addParam<bool>("print_when_set", false, "Print the array of coefficients when set"); 26 : 27 1208 : return params; 28 0 : } 29 : 30 712 : MutableCoefficientsInterface::MutableCoefficientsInterface(const MooseObject * moose_object, 31 712 : const InputParameters & parameters) 32 : : Restartable(moose_object->getMooseApp(), 33 1424 : moose_object->name() + "_coefs", 34 : "MutableCoefficientsInterface", 35 712 : moose_object->parameters().get<THREAD_ID>("_tid")), 36 712 : _characteristics(declareRestartableData<std::vector<std::size_t>>("characteristics")), 37 1424 : _coefficients(declareRestartableData<std::vector<Real>>("coefficients")), 38 712 : _enforce_size(false), 39 712 : _print_coefficients(parameters.get<bool>("print_when_set")), 40 2848 : _console(moose_object->_console) 41 : { 42 712 : } 43 : 44 : Real 45 14488 : MutableCoefficientsInterface::operator[](std::size_t index) const 46 : { 47 14488 : 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 21732 : MutableCoefficientsInterface::getSize() const 81 : { 82 21732 : return _coefficients.size(); 83 : } 84 : 85 : bool 86 36854 : MutableCoefficientsInterface::isCompatibleWith(const MutableCoefficientsInterface & other) const 87 : { 88 : // Check the coefficient sizes if requested 89 36854 : if ((_enforce_size && other._enforce_size) && getSize() != other.getSize()) 90 : return false; 91 : 92 : // Check the size of the characteristics array 93 36854 : if (_characteristics.size() != other._characteristics.size()) 94 : return false; 95 : 96 : // Check the values of the characteristics array 97 73706 : for (std::size_t i = 0; i < _characteristics.size(); ++i) 98 36854 : 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 760 : MutableCoefficientsInterface::enforceSize(bool enforce) 112 : { 113 760 : _enforce_size = enforce; 114 760 : } 115 : 116 : void 117 18426 : MutableCoefficientsInterface::importCoefficients(const MutableCoefficientsInterface & other) 118 : { 119 18426 : if (!isCompatibleWith(other)) 120 0 : mooseError("Cannot import coefficients from incompatible MutableCoefficientsInterface"); 121 : 122 18426 : _coefficients = other._coefficients; 123 : 124 18426 : if (_print_coefficients) 125 1974 : _console << *this; 126 : 127 18426 : coefficientsChanged(); 128 18426 : } 129 : 130 : void 131 380 : MutableCoefficientsInterface::resize(std::size_t size, Real fill, bool fill_out_to_size) 132 : { 133 380 : if (size != _coefficients.size()) 134 : { 135 380 : 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 380 : _coefficients.resize(size, fill); 140 : 141 380 : if (_print_coefficients) 142 22 : _console << *this; 143 : 144 380 : coefficientsChanged(); 145 : } 146 380 : } 147 : 148 : void 149 380 : MutableCoefficientsInterface::setCharacteristics( 150 : const std::vector<std::size_t> & new_characteristics) 151 : { 152 380 : _characteristics = new_characteristics; 153 380 : } 154 : 155 : void 156 1626 : MutableCoefficientsInterface::setCoefficients(const std::vector<Real> & new_coefficients) 157 : { 158 1626 : 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 1626 : _coefficients = new_coefficients; 163 : 164 1626 : if (_print_coefficients) 165 1626 : _console << *this; 166 : 167 1626 : coefficientsChanged(); 168 1626 : } 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 3622 : operator<<(std::ostream & stream, const MutableCoefficientsInterface & me) 187 : { 188 3622 : const MooseObject * myself_again = dynamic_cast<const MooseObject *>(&me); 189 : stream << "\n\n" 190 3622 : << "MutableCoefficientsInterface: " << (myself_again ? myself_again->name() : "Unknown") 191 : << "\n" 192 10866 : << " Number of Coefficients: " << me.getSize() << "\n"; 193 : 194 18110 : for (std::size_t i = 0; i < me.getSize(); ++i) 195 43464 : stream << std::setw(4) << i << ": " << std::setw(12) << me[i] << ((i % 6 == 5) ? "\n" : " "); 196 : 197 3622 : stream << "\n\n" << std::flush; 198 : 199 3622 : return stream; 200 : }