libMesh
parameter_vector.C
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2017 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 (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 unsigned int Np = cast_int<unsigned int>
41  (this->_params.size());
42  target.clear();
43  target._params.resize(Np);
44  target._my_data.resize(Np);
45  for (unsigned int i=0; i != Np; ++i)
46  {
47  target._params[i] =
48  new ParameterPointer<Number>(&target._my_data[i]);
49  target._my_data[i] = *(*this)[i];
50  }
51 }
52 
53 
54 
56 {
57  target._my_data.clear();
58  target._params = this->_params;
59  target._is_shallow_copy = true;
60 }
61 
62 
63 
65 {
66  const unsigned int Np = cast_int<unsigned int>
67  (this->_params.size());
68  libmesh_assert_equal_to (target._params.size(), Np);
69 
70  for (unsigned int i=0; i != Np; ++i)
71  *target[i] = *(*this)[i];
72 }
73 
74 
75 
76 void ParameterVector::resize(unsigned int s)
77 {
79 
80  const std::size_t old_size = this->_params.size();
81 
82  // If we're shrinking the vector, we don't want to leak memory.
83  // Note that we're using < in these for loops, not !=
84  // We don't know a priori if we're shrinking or growing
85  for (unsigned int i=s; i < old_size; ++i)
86  delete _params[i];
87 
88  this->_params.resize(s);
89 
90  for (unsigned int i=old_size; i < s; ++i)
91  this->_params[i] =
93 }
94 
95 
96 
97 void ParameterVector::deep_resize(unsigned int s)
98 {
100 
101  this->_params.resize(s);
102  this->_my_data.resize(s);
103  for (unsigned int i=0; i != s; ++i)
104  this->_params[i] =
105  new ParameterPointer<Number>(&this->_my_data[i]);
106 }
107 
108 
109 
111 {
112  const unsigned int Np = cast_int<unsigned int>
113  (this->_params.size());
114  for (unsigned int i=0; i != Np; ++i)
115  *(*this)[i] *= a;
116  return *this;
117 }
118 
119 
120 
122 {
123  const unsigned int Np = cast_int<unsigned int>
124  (this->_params.size());
125  libmesh_assert_equal_to (a._params.size(), Np);
126  for (unsigned int i=0; i != Np; ++i)
127  *(*this)[i] += *a[i];
128  return *this;
129 }
130 
131 
132 } // namespace libMesh
void resize(unsigned int 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...
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.
std::vector< ParameterAccessor< Number > * > _params
Pointers to parameters which may exist elsewhere.
const class libmesh_nullptr_t libmesh_nullptr
The libMesh namespace provides an interface to certain functionality in the library.
ParameterVector & operator*=(const Number a)
Multiplication operator; acts individually on each parameter.
libmesh_assert(j)
bool _is_shallow_copy
Am I a shallow copy? If so then I shouldn&#39;t be deleting my ParameterAccessors.
void deep_resize(unsigned int s)
Sets the number of parameters to be used.
void shallow_copy(ParameterVector &target) const
Shallow copy constructor: the target will now point to all the parameter values I&#39;m pointing to...
void clear()
Resets to "no parameters".
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...
ParameterVector()
Default constructor: "no parameters".
std::vector< Number > _my_data
Parameters which I own; e.g.
ParameterVector & operator+=(const ParameterVector &a)
Addition operator.