libMesh
diff_context.C
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2025 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"
22 #include "libmesh/unsteady_solver.h"
23 
24 // C++ includes
25 #include <memory>
26 
27 namespace libMesh
28 {
29 
30 
31 
33  bool allocate_local_matrices) :
34  time(sys.time),
35  system_time(sys.time),
36  elem_solution_derivative(1.),
37  elem_solution_rate_derivative(1.),
38  elem_solution_accel_derivative(1.),
39  fixed_solution_derivative(0.),
40  _have_local_matrices(allocate_local_matrices),
41  _dof_indices_var(sys.n_vars()),
42  _deltat(nullptr),
43  _system(sys),
44  _is_adjoint(false)
45 {
46  // Finally initialize solution/residual/jacobian data structures
47  unsigned int nv = sys.n_vars();
48 
49  _elem_subsolutions.reserve(nv);
50  _elem_subresiduals.reserve(nv);
51  _elem_subsolution_rates.reserve(nv);
52  _elem_subsolution_accels.reserve(nv);
53 
54  if (sys.use_fixed_solution)
55  _elem_fixed_subsolutions.reserve(nv);
56 
57  if (allocate_local_matrices)
58  _elem_subjacobians.resize(nv);
59 
60  // If the user resizes sys.qoi, it will invalidate us
61  std::size_t n_qoi = sys.n_qois();
62  _elem_qoi.resize(n_qoi);
63  _elem_qoi_derivative.resize(n_qoi);
64  _elem_qoi_subderivatives.resize(n_qoi);
65  for (std::size_t q=0; q != n_qoi; ++q)
66  _elem_qoi_subderivatives[q].reserve(nv);
67 
68  for (unsigned int i=0; i != nv; ++i)
69  {
70  _elem_subsolutions.emplace_back(_elem_solution);
71  _elem_subresiduals.emplace_back(_elem_residual);
72  for (std::size_t q=0; q != n_qoi; ++q)
74  if (allocate_local_matrices)
75  _elem_subjacobians[i].reserve(nv);
76 
77  // Only make space for these if we're using DiffSystem
78  // This is assuming *only* DiffSystem is using elem_solution_rate/accel
79  const DifferentiableSystem * diff_system = dynamic_cast<const DifferentiableSystem *>(&sys);
80  if (diff_system)
81  {
82  // Now, we only need these if the solver is unsteady
83  if (!diff_system->get_time_solver().is_steady())
84  {
86 
87  // We only need accel space if the TimeSolver is second order
88  const UnsteadySolver & time_solver = cast_ref<const UnsteadySolver &>(diff_system->get_time_solver());
89 
90  if (time_solver.time_order() >= 2 || !diff_system->get_second_order_vars().empty())
92  }
93  }
94 
95  if (sys.use_fixed_solution)
97 
98  if (allocate_local_matrices)
99  for (unsigned int j=0; j != nv; ++j)
100  _elem_subjacobians[i].emplace_back(_elem_jacobian);
101  }
102 }
103 
104 
105 
106 DiffContext::~DiffContext () = default;
107 
108 
110 {
111  // We may actually want to be able to set this pointer to nullptr, 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.emplace_back(_localized_vectors[&localized_vector].first);
137 }
138 
139 
141 {
142  return _localized_vectors[&localized_vector].first;
143 }
144 
145 
147 {
148  auto localized_vectors_it = _localized_vectors.find(&localized_vector);
149  libmesh_assert(localized_vectors_it != _localized_vectors.end());
150  return localized_vectors_it->second.first;
151 }
152 
153 
155 {
156  return _localized_vectors[&localized_vector].second[var];
157 }
158 
159 
160 const DenseSubVector<Number> & DiffContext::get_localized_subvector (const NumericVector<Number> & localized_vector, unsigned int var) const
161 {
162  auto localized_vectors_it = _localized_vectors.find(&localized_vector);
163  libmesh_assert(localized_vectors_it != _localized_vectors.end());
164  return localized_vectors_it->second.second[var];
165 }
166 
167 } // namespace libMesh
DenseVector< Number > _elem_solution
Element by element components of nonlinear_solution as adjusted by a time_solver. ...
Definition: diff_context.h:582
std::vector< Number > _elem_qoi
Element quantity of interest contributions.
Definition: diff_context.h:621
std::vector< DenseSubVector< Number > > _elem_fixed_subsolutions
Definition: diff_context.h:605
std::vector< DenseSubVector< Number > > _elem_subsolution_accels
Definition: diff_context.h:597
DenseVector< Number > _elem_fixed_solution
Element by element components of nonlinear_solution at a fixed point in a timestep, for optional use by e.g.
Definition: diff_context.h:604
unsigned int n_qois() const
Number of currently active quantities of interest.
Definition: system.h:2621
std::vector< DenseVector< Number > > _elem_qoi_derivative
Element quantity of interest derivative contributions.
Definition: diff_context.h:626
DenseMatrix< Number > _elem_jacobian
Element jacobian: derivatives of elem_residual with respect to elem_solution.
Definition: diff_context.h:616
Real * _deltat
Defaults to nullptr, can optionally be used to point to a timestep value in the System-derived class ...
Definition: diff_context.h:655
std::vector< std::vector< DenseSubVector< Number > > > _elem_qoi_subderivatives
Definition: diff_context.h:627
virtual bool is_steady() const =0
Is this effectively a steady-state solver?
The libMesh namespace provides an interface to certain functionality in the library.
Defines a dense subvector for use in finite element computations.
virtual ~DiffContext()
Destructor.
std::vector< DenseSubVector< Number > > _elem_subsolution_rates
Definition: diff_context.h:590
This class provides a specific system class.
Definition: diff_system.h:54
void add_localized_vector(NumericVector< Number > &localized_vector, const System &sys)
Adds a vector to the map of localized vectors.
Definition: diff_context.C:125
virtual unsigned int time_order() const =0
const std::set< unsigned int > & get_second_order_vars() const
Definition: diff_physics.h:519
unsigned int n_vars
bool use_fixed_solution
A boolean to be set to true by systems using elem_fixed_solution, for optional use by e...
Definition: system.h:1563
Manages consistently variables, degrees of freedom, and coefficient vectors.
Definition: system.h:96
libmesh_assert(ctx)
std::map< const NumericVector< Number > *, std::pair< DenseVector< Number >, std::vector< DenseSubVector< Number > > > > _localized_vectors
Contains pointers to vectors the user has asked to be localized, keyed with pairs of element localize...
Definition: diff_context.h:571
DenseSubVector< Number > & get_localized_subvector(const NumericVector< Number > &localized_vector, unsigned int var)
Return a reference to DenseSubVector localization of localized_vector at variable var contained in th...
Definition: diff_context.C:154
std::vector< DenseSubVector< Number > > _elem_subsolutions
Definition: diff_context.h:583
This is a generic class that defines a solver to handle time integration of DifferentiableSystems.
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
std::vector< std::vector< DenseSubMatrix< Number > > > _elem_subjacobians
Definition: diff_context.h:634
DenseVector< Number > _elem_residual
Element residual vector.
Definition: diff_context.h:610
DenseVector< Number > & get_localized_vector(const NumericVector< Number > &localized_vector)
Return a reference to DenseVector localization of localized_vector contained in the _localized_vector...
Definition: diff_context.C:140
void set_deltat_pointer(Real *dt)
Points the _deltat member of this class at a timestep value stored in the creating System...
Definition: diff_context.C:109
DiffContext(const System &, bool allocate_local_matrices=true)
Constructor.
Definition: diff_context.C:32
DenseVector< Number > _elem_solution_accel
Element by element components of du/dt as adjusted by a time_solver.
Definition: diff_context.h:596
unsigned int n_vars() const
Definition: system.h:2430
std::vector< DenseSubVector< Number > > _elem_subresiduals
Element residual subvectors and (if _have_local_matrices) Jacobian submatrices.
Definition: diff_context.h:633
TimeSolver & get_time_solver()
Definition: diff_system.h:456
DenseVector< Number > _elem_solution_rate
Element by element components of du/dt as adjusted by a time_solver.
Definition: diff_context.h:589