parameter_vector.C
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2018 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
23 
24 namespace libMesh
25 {
26 
27 ParameterVector::ParameterVector(const std::vector<Number *> &params)
28  : _is_shallow_copy(false)
29 {
30  _params.reserve(params.size());
31 
32  for (std::size_t i=0; i != params.size(); ++i)
33  _params.push_back(new ParameterPointer<Number>(params[i]));
34 }
35 
36 
37 
39 {
40  const std::size_t Np = this->_params.size();
41  target.clear();
42  target._params.resize(Np);
43  target._my_data.resize(Np);
44  for (std::size_t i=0; i != Np; ++i)
45  {
46  target._params[i] =
47  new ParameterPointer<Number>(&target._my_data[i]);
48  target._my_data[i] = *(*this)[i];
49  }
50 }
51 
52 
53 
55 {
56  target._my_data.clear();
57  target._params = this->_params;
58  target._is_shallow_copy = true;
59 }
60 
61 
62 
64 {
65  const std::size_t Np = this->_params.size();
66  libmesh_assert_equal_to (target._params.size(), Np);
67 
68  for (std::size_t i=0; i != Np; ++i)
69  *target[i] = *(*this)[i];
70 }
71 
72 
73 
74 void ParameterVector::resize(std::size_t s)
75 {
76  libmesh_assert(!_is_shallow_copy);
77 
78  const std::size_t old_size = this->_params.size();
79 
80  // If we're shrinking the vector, we don't want to leak memory.
81  // Note that we're using < in these for loops, not !=
82  // We don't know a priori if we're shrinking or growing
83  for (std::size_t i=s; i < old_size; ++i)
84  delete _params[i];
85 
86  this->_params.resize(s);
87 
88  for (std::size_t i=old_size; i < s; ++i)
89  this->_params[i] =
90  new ParameterPointer<Number>(nullptr);
91 }
92 
93 
94 
95 void ParameterVector::deep_resize(std::size_t s)
96 {
97  libmesh_assert(!_is_shallow_copy);
98 
99  this->_params.resize(s);
100  this->_my_data.resize(s);
101  for (std::size_t i=0; i != s; ++i)
102  this->_params[i] =
103  new ParameterPointer<Number>(&this->_my_data[i]);
104 }
105 
106 
107 
109 {
110  const std::size_t Np = this->_params.size();
111  for (std::size_t i=0; i != Np; ++i)
112  *(*this)[i] *= a;
113  return *this;
114 }
115 
116 
117 
119 {
120  const std::size_t Np = this->_params.size();
121  libmesh_assert_equal_to (a._params.size(), Np);
122  for (std::size_t i=0; i != Np; ++i)
123  *(*this)[i] += *a[i];
124  return *this;
125 }
126 
127 
128 } // namespace libMesh
void resize(std::size_t s)
void deep_copy(ParameterVector &target) const
void deep_resize(std::size_t s)
Specifies parameters for parameter sensitivity calculations.
Stores/modifies a user-provided pointer to a parameter.
std::vector< ParameterAccessor< Number > * > _params
ParameterVector & operator*=(const Number a)
std::vector< Number > _my_data
ParameterVector & operator+=(const ParameterVector &a)
void value_copy(ParameterVector &target) const
void shallow_copy(ParameterVector &target) const