libMesh
parameter_vector.C
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2019 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_pointer.h"
22 #include "libmesh/parameter_vector.h"
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 (auto p : params)
33  _params.push_back(new ParameterPointer<Number>(p));
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 {
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 {
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
libMesh::Number
Real Number
Definition: libmesh_common.h:195
libMesh::ParameterVector::operator+=
ParameterVector & operator+=(const ParameterVector &a)
Addition operator.
Definition: parameter_vector.C:118
libMesh::ParameterVector::ParameterVector
ParameterVector()
Default constructor: "no parameters".
Definition: parameter_vector.h:50
libMesh
The libMesh namespace provides an interface to certain functionality in the library.
Definition: factoryfunction.C:55
libMesh::ParameterVector::_my_data
std::vector< Number > _my_data
Parameters which I own; e.g.
Definition: parameter_vector.h:147
libMesh::ParameterVector::value_copy
void value_copy(ParameterVector &target) const
Value copy method: the target, which should already have as many parameters as I do,...
Definition: parameter_vector.C:63
libMesh::ParameterVector::deep_resize
void deep_resize(std::size_t s)
Sets the number of parameters to be used.
Definition: parameter_vector.C:95
libMesh::libmesh_assert
libmesh_assert(ctx)
libMesh::ParameterVector::_params
std::vector< ParameterAccessor< Number > * > _params
Pointers to parameters which may exist elsewhere.
Definition: parameter_vector.h:142
libMesh::ParameterVector::resize
void resize(std::size_t s)
Sets the number of parameters to be used.
Definition: parameter_vector.C:74
libMesh::ParameterVector
Data structure for specifying which Parameters should be independent variables in a parameter sensiti...
Definition: parameter_vector.h:44
libMesh::ParameterVector::shallow_copy
void shallow_copy(ParameterVector &target) const
Shallow copy constructor: the target will now point to all the parameter values I'm pointing to.
Definition: parameter_vector.C:54
libMesh::ParameterVector::clear
void clear()
Resets to "no parameters".
Definition: parameter_vector.h:170
libMesh::ParameterVector::deep_copy
void deep_copy(ParameterVector &target) const
Deep copy constructor: the target will now own new copies of all the parameter values I'm pointing to...
Definition: parameter_vector.C:38
libMesh::ParameterVector::_is_shallow_copy
bool _is_shallow_copy
Am I a shallow copy? If so then I shouldn't be deleting my ParameterAccessors.
Definition: parameter_vector.h:153
libMesh::ParameterVector::operator*=
ParameterVector & operator*=(const Number a)
Multiplication operator; acts individually on each parameter.
Definition: parameter_vector.C:108
libMesh::ParameterPointer
Accessor object allowing reading and modification of the independent variables in a parameter sensiti...
Definition: parameter_pointer.h:45