Line data Source code
1 : // The libMesh Finite Element Library. 2 : // Copyright (C) 2002-2025 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner 3 : 4 : // This library is free software; you can redistribute it and/or 5 : // modify it under the terms of the GNU Lesser General Public 6 : // License as published by the Free Software Foundation; either 7 : // version 2.1 of the License, or (at your option) any later version. 8 : 9 : // This library is distributed in the hope that it will be useful, 10 : // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 : // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 : // Lesser General Public License for more details. 13 : 14 : // You should have received a copy of the GNU Lesser General Public 15 : // License along with this library; if not, write to the Free Software 16 : // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 17 : 18 : 19 : 20 : #ifndef LIBMESH_PARAMETER_VECTOR_H 21 : #define LIBMESH_PARAMETER_VECTOR_H 22 : 23 : 24 : // Local Includes 25 : #include "libmesh/libmesh_common.h" 26 : #include "libmesh/parameter_accessor.h" 27 : 28 : // C++ Includes 29 : #include <vector> 30 : #include <memory> 31 : 32 : namespace libMesh 33 : { 34 : 35 : 36 : /** 37 : * Data structure for specifying which Parameters should be 38 : * independent variables in a parameter sensitivity calculation. 39 : * 40 : * \author Roy Stogner 41 : * \date 2009 42 : * \brief Specifies parameters for parameter sensitivity calculations. 43 : */ 44 : class ParameterVector 45 : { 46 : public: 47 : /** 48 : * Default constructor: "no parameters" 49 : */ 50 0 : ParameterVector() = default; 51 : 52 : /** 53 : * Constructor-from-vector-of-Number*: each points to a parameter 54 : */ 55 : explicit 56 : ParameterVector(const std::vector<Number *> & params); 57 : 58 : /** 59 : * Destructor - deletes ParameterAccessor objects 60 : */ 61 0 : ~ParameterVector() = default; 62 : 63 : /** 64 : * Deep copy constructor: the \p target will now own new copies of 65 : * all the parameter values I'm pointing to 66 : */ 67 : void deep_copy(ParameterVector & target) const; 68 : 69 : /** 70 : * Shallow copy constructor: the \p target will now point to all the 71 : * parameter values I'm pointing to 72 : */ 73 : void shallow_copy(ParameterVector & target) const; 74 : 75 : /** 76 : * Value copy method: the \p target, which should already have as 77 : * many parameters as I do, will now have those parameters set to my 78 : * values. 79 : */ 80 : void value_copy(ParameterVector & target) const; 81 : 82 : /** 83 : * Resets to "no parameters" 84 : */ 85 : void clear(); 86 : 87 : /** 88 : * \returns The number of parameters to be used 89 : */ 90 982 : std::size_t size() const { return _params.size(); } 91 : 92 : /** 93 : * Sets the number of parameters to be used. If the new size is 94 : * larger than the old, empty ParameterPointer accessors fill the 95 : * new entries. 96 : */ 97 : void resize(std::size_t s); 98 : 99 : /** 100 : * Adds an additional parameter accessor to the end of the vector. 101 : * 102 : * We will free this accessor when we are finished with it; we 103 : * request that it be passed to us as a std::unique_ptr to reflect that 104 : * fact in the API. 105 : */ 106 : void push_back(std::unique_ptr<ParameterAccessor<Number>> new_accessor); 107 : 108 : /** 109 : * Sets the number of parameters to be used. This method is for 110 : * resizing a ParameterVector that owns its own parameter values 111 : */ 112 : void deep_resize(std::size_t s); 113 : 114 : /** 115 : * \returns A smart-pointer to a parameter value 116 : */ 117 : const ParameterAccessor<Number> & operator[](std::size_t i) const; 118 : 119 : /** 120 : * \returns A reference to a smart-pointer to a parameter value, 121 : * suitable for repointing it to a different address. 122 : * This method is deprecated and may not work with more 123 : * sophisticated ParameterAccessor subclasses. 124 : */ 125 : ParameterAccessor<Number> & operator[](std::size_t i); 126 : 127 : /** 128 : * Multiplication operator; acts individually on each parameter. 129 : */ 130 : ParameterVector & operator *= (const Number a); 131 : 132 : /** 133 : * Addition operator. The parameter vector to be added in must 134 : * have the same number of values. 135 : */ 136 : ParameterVector & operator += (const ParameterVector & a); 137 : 138 : private: 139 : /** 140 : * Pointers to parameters which may exist elsewhere 141 : */ 142 : std::vector<std::unique_ptr<ParameterAccessor<Number>>> _params; 143 : 144 : /** 145 : * Parameters which I own; e.g. as the result of a deep copy 146 : */ 147 : std::vector<Number> _my_data; 148 : 149 : #ifndef NDEBUG 150 : /** 151 : * Am I a shallow copy? If so then resizing me would be a bug. 152 : */ 153 : bool _is_shallow_copy = false; 154 : #endif 155 : }; 156 : 157 : 158 : 159 : // ------------------------------------------------------------ 160 : // ParameterVector inline methods 161 : 162 : 163 : inline 164 : void 165 0 : ParameterVector::clear() 166 : { 167 0 : _params.clear(); 168 0 : _my_data.clear(); 169 0 : } 170 : 171 : 172 : 173 : inline 174 : void ParameterVector::push_back(std::unique_ptr<ParameterAccessor<Number>> new_accessor) 175 : { 176 : // Can't append stuff we are responsible for if we're already a shallow copy. 177 : libmesh_assert(!_is_shallow_copy); 178 : libmesh_assert(new_accessor.get()); 179 : _params.push_back(std::move(new_accessor)); 180 : } 181 : 182 : 183 : 184 : inline 185 0 : const ParameterAccessor<Number> & ParameterVector::operator[] (std::size_t i) const 186 : { 187 0 : libmesh_assert_greater (_params.size(), i); 188 : 189 0 : return *_params[i]; 190 : } 191 : 192 : 193 : 194 : inline 195 160 : ParameterAccessor<Number> & ParameterVector::operator[] (std::size_t i) 196 : { 197 160 : libmesh_assert_greater (_params.size(), i); 198 : 199 1856 : return *_params[i]; 200 : } 201 : 202 : } // namespace libMesh 203 : 204 : #endif // LIBMESH_PARAMETER_VECTOR_H