diff_context.C
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2014 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 #include "libmesh/diff_context.h"
20 #include "libmesh/diff_system.h"
21 
22 namespace libMesh
23 {
24 
25 
26 
28  time(sys.time),
29  system_time(sys.time),
30  elem_solution_derivative(1.),
31  fixed_solution_derivative(0.),
32  dof_indices_var(sys.n_vars()),
33  _deltat(NULL),
34  _system(sys),
35  _is_adjoint(false)
36 {
37  // Finally initialize solution/residual/jacobian data structures
38  unsigned int nv = sys.n_vars();
39 
40  elem_subsolutions.reserve(nv);
41  elem_subresiduals.reserve(nv);
42  elem_subjacobians.resize(nv);
43  if (sys.use_fixed_solution)
44  elem_fixed_subsolutions.reserve(nv);
45 
46  // If the user resizes sys.qoi, it will invalidate us
47  std::size_t n_qoi = sys.qoi.size();
48  elem_qoi.resize(n_qoi);
49  elem_qoi_derivative.resize(n_qoi);
50  elem_qoi_subderivatives.resize(n_qoi);
51  for (std::size_t q=0; q != n_qoi; ++q)
52  elem_qoi_subderivatives[q].reserve(nv);
53 
54  for (unsigned int i=0; i != nv; ++i)
55  {
58  for (std::size_t q=0; q != n_qoi; ++q)
60  elem_subjacobians[i].reserve(nv);
61 
62  if (sys.use_fixed_solution)
63  elem_fixed_subsolutions.push_back
65 
66  for (unsigned int j=0; j != nv; ++j)
67  {
68  elem_subjacobians[i].push_back
70  }
71  }
72 }
73 
74 
75 
77 {
78  for (std::size_t i=0; i != elem_subsolutions.size(); ++i)
79  {
80  delete elem_subsolutions[i];
81  delete elem_subresiduals[i];
82  for (std::size_t q=0; q != elem_qoi_subderivatives.size(); ++q)
83  delete elem_qoi_subderivatives[q][i];
84  if (!elem_fixed_subsolutions.empty())
85  delete elem_fixed_subsolutions[i];
86 
87  for (std::size_t j=0; j != elem_subjacobians[i].size(); ++j)
88  delete elem_subjacobians[i][j];
89  }
90 
91  // We also need to delete all the DenseSubVectors from the localized_vectors map
92  // localized_vectors iterators
93  std::map<const NumericVector<Number>*, std::pair<DenseVector<Number>, std::vector<DenseSubVector<Number>*> > >::iterator localized_vectors_it = localized_vectors.begin();
94  std::map<const NumericVector<Number>*, std::pair<DenseVector<Number>, std::vector<DenseSubVector<Number>*> > >::iterator localized_vectors_end = localized_vectors.end();
95 
96  // Loop over every localized_vector
97  for(; localized_vectors_it != localized_vectors_end; ++localized_vectors_it)
98  {
99  // Grab the DenseSubVector to be deleted
100  std::vector<DenseSubVector<Number>* >& localized_vector_dsv = localized_vectors_it->second.second;
101 
102  // Loop over that vector and delete each entry
103  for(std::size_t i=0; i != localized_vector_dsv.size(); ++i)
104  delete localized_vector_dsv[i];
105  }
106 }
107 
108 
110 {
111  // We may actually want to be able to set this pointer to NULL, so
112  // don't report an error for that.
113  _deltat = dt;
114 }
115 
116 
118 {
120 
121  return *_deltat;
122 }
123 
124 
125 void DiffContext::add_localized_vector (NumericVector<Number> & _localized_vector, const System & _sys)
126 {
127  // Make an empty pair keyed with a reference to this _localized_vector
128  localized_vectors[&_localized_vector] = std::make_pair(DenseVector<Number>(), std::vector<DenseSubVector<Number>*>());
129 
130  unsigned int nv = _sys.n_vars();
131 
132  localized_vectors[&_localized_vector].second.reserve(nv);
133 
134  // Fill the DenseSubVector with nv copies of DenseVector
135  for(unsigned int i=0; i != nv; ++i)
136  localized_vectors[&_localized_vector].second.push_back(new DenseSubVector<Number>(localized_vectors[&_localized_vector].first));
137 }
138 
139 
141 {
142  return localized_vectors[&_localized_vector].first;
143 }
144 
145 
147 {
148  std::map<const NumericVector<Number>*, std::pair<DenseVector<Number>, std::vector<DenseSubVector<Number>*> > >::const_iterator
149  localized_vectors_it = localized_vectors.find(&_localized_vector);
150  libmesh_assert(localized_vectors_it != localized_vectors.end());
151  return localized_vectors_it->second.first;
152 }
153 
154 
156 {
157  return *localized_vectors[&_localized_vector].second[_var];
158 }
159 
160 
161 const DenseSubVector<Number>& DiffContext::get_localized_subvector (const NumericVector<Number> & _localized_vector, unsigned int _var) const
162 {
163  std::map<const NumericVector<Number>*, std::pair<DenseVector<Number>, std::vector<DenseSubVector<Number>*> > >::const_iterator
164  localized_vectors_it = localized_vectors.find(&_localized_vector);
165  libmesh_assert(localized_vectors_it != localized_vectors.end());
166  return *localized_vectors_it->second.second[_var];
167 }
168 
169 } // namespace libMesh

Site Created By: libMesh Developers
Last modified: February 07 2014 16:57:04 UTC

Hosted By:
SourceForge.net Logo