libMesh
parameter_vector.C
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2024 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 // Local Includes
21 #include "libmesh/parameter_vector.h"
22 
23 #include "libmesh/int_range.h"
24 #include "libmesh/parameter_pointer.h"
25 
26 namespace libMesh
27 {
28 
29 ParameterVector::ParameterVector(const std::vector<Number *> &params)
30 #ifndef NDEBUG
31  : _is_shallow_copy(false)
32 #endif
33 {
34  _params.reserve(params.size());
35 
36  for (auto p : params)
37  _params.push_back(std::make_unique<ParameterPointer<Number>>(p));
38 }
39 
40 
41 
43 {
44  const std::size_t Np = this->_params.size();
45  target.clear();
46  target._params.resize(Np);
47  target._my_data.resize(Np);
48 #ifndef NDEBUG
49  target._is_shallow_copy = false;
50 #endif
51  for (std::size_t i=0; i != Np; ++i)
52  {
53  target._params[i] =
54  std::make_unique<ParameterPointer<Number>>
55  (&target._my_data[i]);
56  target._my_data[i] = *(*this)[i];
57  }
58 }
59 
60 
61 
62 void ParameterVector::shallow_copy(ParameterVector & target) const
63 {
64  target._my_data.clear();
65  target._params.resize(this->_params.size());
66  for (auto i : index_range(this->_params))
67  target._params[i] = this->_params[i]->clone();
68 #ifndef NDEBUG
69  target._is_shallow_copy = true;
70 #endif
71 }
72 
73 
74 
75 void ParameterVector::value_copy(ParameterVector & target) const
76 {
77  const std::size_t Np = this->_params.size();
78  libmesh_assert_equal_to (target._params.size(), Np);
79 
80  for (std::size_t i=0; i != Np; ++i)
81  *target[i] = *(*this)[i];
82 }
83 
84 
85 
86 void ParameterVector::resize(std::size_t s)
87 {
89 
90  this->_params.resize(s);
91 
92  // We used to make nullptr ParameterPointers here, but that was just
93  // to keep our destructor happy; users couldn't reseat them so
94  // shouldn't have code depending on them
95 }
96 
97 
98 
99 void ParameterVector::deep_resize(std::size_t s)
100 {
102 
103  this->_params.resize(s);
104  this->_my_data.resize(s);
105  for (std::size_t i=0; i != s; ++i)
106  this->_params[i] =
107  std::make_unique<ParameterPointer<Number>>(&this->_my_data[i]);
108 }
109 
110 
111 
113 {
114  const std::size_t Np = this->_params.size();
115  for (std::size_t i=0; i != Np; ++i)
116  *(*this)[i] *= a;
117  return *this;
118 }
119 
120 
121 
123 {
124  const std::size_t Np = this->_params.size();
125  libmesh_assert_equal_to (a._params.size(), Np);
126  for (std::size_t i=0; i != Np; ++i)
127  *(*this)[i] += *a[i];
128  return *this;
129 }
130 
131 
132 } // namespace libMesh
void resize(std::size_t s)
Sets the number of parameters to be used.
void deep_copy(ParameterVector &target) const
Deep copy constructor: the target will now own new copies of all the parameter values I&#39;m pointing to...
void deep_resize(std::size_t s)
Sets the number of parameters to be used.
Data structure for specifying which Parameters should be independent variables in a parameter sensiti...
Accessor object allowing reading and modification of the independent variables in a parameter sensiti...
The libMesh namespace provides an interface to certain functionality in the library.
ParameterVector & operator*=(const Number a)
Multiplication operator; acts individually on each parameter.
bool _is_shallow_copy
Am I a shallow copy? If so then resizing me would be a bug.
libmesh_assert(ctx)
void clear()
Resets to "no parameters".
ParameterVector()=default
Default constructor: "no parameters".
std::vector< std::unique_ptr< ParameterAccessor< Number > > > _params
Pointers to parameters which may exist elsewhere.
std::vector< Number > _my_data
Parameters which I own; e.g.
ParameterVector & operator+=(const ParameterVector &a)
Addition operator.
auto index_range(const T &sizable)
Helper function that returns an IntRange<std::size_t> representing all the indices of the passed-in v...
Definition: int_range.h:111
void value_copy(ParameterVector &target) const
Value copy method: the target, which should already have as many parameters as I do, will now have those parameters set to my values.
void shallow_copy(ParameterVector &target) const
Shallow copy constructor: the target will now point to all the parameter values I&#39;m pointing to...