diff_context.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 #include "libmesh/diff_context.h"
20 #include "libmesh/diff_system.h"
21 #include "libmesh/diff_system.h"
23 #include "libmesh/auto_ptr.h" // libmesh_make_unique
24 
25 namespace libMesh
26 {
27 
28 
29 
31  time(sys.time),
32  system_time(sys.time),
33  elem_solution_derivative(1.),
34  elem_solution_rate_derivative(1.),
35  elem_solution_accel_derivative(1.),
36  fixed_solution_derivative(0.),
37  _dof_indices_var(sys.n_vars()),
38  _deltat(nullptr),
39  _system(sys),
40  _is_adjoint(false)
41 {
42  // Finally initialize solution/residual/jacobian data structures
43  unsigned int nv = sys.n_vars();
44 
45  _elem_subsolutions.reserve(nv);
46  _elem_subresiduals.reserve(nv);
47  _elem_subjacobians.resize(nv);
48  _elem_subsolution_rates.reserve(nv);
49  _elem_subsolution_accels.reserve(nv);
50  if (sys.use_fixed_solution)
51  _elem_fixed_subsolutions.reserve(nv);
52 
53  // If the user resizes sys.qoi, it will invalidate us
54 
55  std::size_t n_qoi = sys.qoi.size();
56  _elem_qoi.resize(n_qoi);
57  _elem_qoi_derivative.resize(n_qoi);
58  _elem_qoi_subderivatives.resize(n_qoi);
59  for (std::size_t q=0; q != n_qoi; ++q)
60  _elem_qoi_subderivatives[q].reserve(nv);
61 
62  for (unsigned int i=0; i != nv; ++i)
63  {
64  _elem_subsolutions.emplace_back(libmesh_make_unique<DenseSubVector<Number>>(_elem_solution));
65  _elem_subresiduals.emplace_back(libmesh_make_unique<DenseSubVector<Number>>(_elem_residual));
66  for (std::size_t q=0; q != n_qoi; ++q)
67  _elem_qoi_subderivatives[q].emplace_back(libmesh_make_unique<DenseSubVector<Number>>(_elem_qoi_derivative[q]));
68  _elem_subjacobians[i].reserve(nv);
69 
70  // Only make space for these if we're using DiffSystem
71  // This is assuming *only* DiffSystem is using elem_solution_rate/accel
72  const DifferentiableSystem * diff_system = dynamic_cast<const DifferentiableSystem *>(&sys);
73  if (diff_system)
74  {
75  // Now, we only need these if the solver is unsteady
76  if (!diff_system->get_time_solver().is_steady())
77  {
78  _elem_subsolution_rates.emplace_back(libmesh_make_unique<DenseSubVector<Number>>(_elem_solution_rate));
79 
80  // We only need accel space if the TimeSolver is second order
81  const UnsteadySolver & time_solver = cast_ref<const UnsteadySolver &>(diff_system->get_time_solver());
82 
83  if (time_solver.time_order() >= 2 || !diff_system->get_second_order_vars().empty())
84  _elem_subsolution_accels.emplace_back(libmesh_make_unique<DenseSubVector<Number>>(_elem_solution_accel));
85  }
86  }
87 
88  if (sys.use_fixed_solution)
89  _elem_fixed_subsolutions.emplace_back(libmesh_make_unique<DenseSubVector<Number>>(_elem_fixed_solution));
90 
91  for (unsigned int j=0; j != nv; ++j)
92  _elem_subjacobians[i].emplace_back(libmesh_make_unique<DenseSubMatrix<Number>>(_elem_jacobian));
93  }
94 }
95 
96 
97 
99 {
100 }
101 
102 
104 {
105  // We may actually want to be able to set this pointer to nullptr, so
106  // don't report an error for that.
107  _deltat = dt;
108 }
109 
110 
112 {
113  libmesh_assert(_deltat);
114 
115  return *_deltat;
116 }
117 
118 
119 void DiffContext::add_localized_vector (NumericVector<Number> & localized_vector, const System & sys)
120 {
121  // Make an empty pair keyed with a reference to this _localized_vector
122  _localized_vectors[&localized_vector] = std::make_pair(DenseVector<Number>(), std::vector<std::unique_ptr<DenseSubVector<Number>>>());
123 
124  unsigned int nv = sys.n_vars();
125 
126  _localized_vectors[&localized_vector].second.reserve(nv);
127 
128  // Fill the DenseSubVector with nv copies of DenseVector
129  for (unsigned int i=0; i != nv; ++i)
130  _localized_vectors[&localized_vector].second.emplace_back(libmesh_make_unique<DenseSubVector<Number>>(_localized_vectors[&localized_vector].first));
131 }
132 
133 
135 {
136  return _localized_vectors[&localized_vector].first;
137 }
138 
139 
141 {
142  auto localized_vectors_it = _localized_vectors.find(&localized_vector);
143  libmesh_assert(localized_vectors_it != _localized_vectors.end());
144  return localized_vectors_it->second.first;
145 }
146 
147 
149 {
150  return *_localized_vectors[&localized_vector].second[var];
151 }
152 
153 
154 const DenseSubVector<Number> & DiffContext::get_localized_subvector (const NumericVector<Number> & localized_vector, unsigned int var) const
155 {
156  auto localized_vectors_it = _localized_vectors.find(&localized_vector);
157  libmesh_assert(localized_vectors_it != _localized_vectors.end());
158  return *localized_vectors_it->second.second[var];
159 }
160 
161 } // namespace libMesh
DenseVector< Number > _elem_solution
Definition: diff_context.h:581
std::vector< Number > _elem_qoi
Definition: diff_context.h:620
std::vector< std::unique_ptr< DenseSubVector< Number > > > _elem_subsolution_rates
Definition: diff_context.h:589
DenseVector< Number > _elem_fixed_solution
Definition: diff_context.h:603
std::vector< std::unique_ptr< DenseSubVector< Number > > > _elem_subresiduals
Definition: diff_context.h:631
std::vector< std::unique_ptr< DenseSubVector< Number > > > _elem_subsolution_accels
Definition: diff_context.h:596
std::vector< DenseVector< Number > > _elem_qoi_derivative
Definition: diff_context.h:625
DenseMatrix< Number > _elem_jacobian
Definition: diff_context.h:615
std::vector< std::vector< std::unique_ptr< DenseSubVector< Number > > > > _elem_qoi_subderivatives
Definition: diff_context.h:626
virtual bool is_steady() const =0
const unsigned int n_vars
Definition: tecplot_io.C:69
DiffContext(const System &)
Definition: diff_context.C:30
virtual ~DiffContext()
Definition: diff_context.C:98
void add_localized_vector(NumericVector< Number > &localized_vector, const System &sys)
Definition: diff_context.C:119
virtual unsigned int time_order() const =0
const std::set< unsigned int > & get_second_order_vars() const
Definition: diff_physics.h:530
std::map< const NumericVector< Number > *, std::pair< DenseVector< Number >, std::vector< std::unique_ptr< DenseSubVector< Number > > > > > _localized_vectors
Definition: diff_context.h:575
std::vector< Number > qoi
Definition: system.h:1558
bool use_fixed_solution
Definition: system.h:1493
Manages consistently variables, degrees of freedom, and coefficient vectors.
Definition: system.h:92
std::vector< std::unique_ptr< DenseSubVector< Number > > > _elem_subsolutions
Definition: diff_context.h:582
std::vector< std::vector< std::unique_ptr< DenseSubMatrix< Number > > > > _elem_subjacobians
Definition: diff_context.h:632
DenseSubVector< Number > & get_localized_subvector(const NumericVector< Number > &localized_vector, unsigned int var)
Definition: diff_context.C:148
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
DenseVector< Number > _elem_residual
Definition: diff_context.h:609
DenseVector< Number > & get_localized_vector(const NumericVector< Number > &localized_vector)
Definition: diff_context.C:134
void set_deltat_pointer(Real *dt)
Definition: diff_context.C:103
DenseVector< Number > _elem_solution_accel
Definition: diff_context.h:595
unsigned int n_vars() const
Definition: system.h:2105
std::vector< std::unique_ptr< DenseSubVector< Number > > > _elem_fixed_subsolutions
Definition: diff_context.h:604
TimeSolver & get_time_solver()
Definition: diff_system.h:415
DenseVector< Number > _elem_solution_rate
Definition: diff_context.h:588