libMesh
memory_solution_history.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 // Local includes
19 #include "libmesh/memory_solution_history.h"
20 
21 #include <cmath>
22 
23 namespace libMesh
24 {
25 
27 {
28 }
29 
30 // This function finds, if it can, the entry where we're supposed to
31 // be storing data
33 {
34  if (stored_solutions.begin() == stored_solutions.end())
35  return;
36 
38 
39  if (std::abs(stored_sols->first - _system.time) < TOLERANCE)
40  return;
41 
42  // If we're not at the front, check the previous entry
43  if (stored_sols != stored_solutions.begin())
44  {
46  if (std::abs((--test_it)->first - _system.time) < TOLERANCE)
47  {
48  --stored_sols;
49  return;
50  }
51  }
52 
53  // If we're not at the end, check the subsequent entry
55  if ((++test_it) != stored_solutions.end())
56  {
57  if (std::abs(test_it->first - _system.time) < TOLERANCE)
58  {
59  ++stored_sols;
60  return;
61  }
62  }
63 }
64 
65 // This functions saves all the 'projection-worthy' system vectors for
66 // future use
68 {
69  this->find_stored_entry();
70 
71  // In an empty history we create the first entry
72  if (stored_solutions.begin() == stored_solutions.end())
73  {
74  stored_solutions.push_back(std::make_pair(_system.time, map_type()));
75  stored_sols = stored_solutions.begin();
76  }
77 
78  // If we're past the end we can create a new entry
79  if (_system.time - stored_sols->first > TOLERANCE )
80  {
81 #ifndef NDEBUG
82  ++stored_sols;
84 #endif
85  stored_solutions.push_back(std::make_pair(_system.time, map_type()));
87  --stored_sols;
88  }
89 
90  // If we're before the beginning we can create a new entry
91  else if (stored_sols->first - _system.time > TOLERANCE)
92  {
94  stored_solutions.push_front(std::make_pair(_system.time, map_type()));
95  stored_sols = stored_solutions.begin();
96  }
97 
98  // We don't support inserting entries elsewhere
100 
101  // Map of stored vectors for this solution step
102  std::map<std::string, std::unique_ptr<NumericVector<Number>>> & saved_vectors = stored_sols->second;
103 
104  // Loop over all the system vectors
106  vec_end = _system.vectors_end();
107  vec != vec_end; ++vec)
108  {
109  // The name of this vector
110  const std::string & vec_name = vec->first;
111 
112  // If we haven't seen this vector before or if we have and
113  // want to overwrite it
114  if ((overwrite_previously_stored || !saved_vectors.count(vec_name)) &&
115  // and if we think it's worth preserving
116  _system.vector_preservation(vec_name))
117  {
118  // Then we save it.
119  saved_vectors[vec_name] = vec->second->clone();
120  }
121  }
122 
123  // Of course, we will usually save the actual solution
124  std::string _solution("_solution");
125  if ((overwrite_previously_stored || !saved_vectors.count(_solution)) &&
126  // and if we think it's worth preserving
128  saved_vectors[_solution] = _system.solution->clone();
129 }
130 
132 {
133  this->find_stored_entry();
134 
135  // Get the time at which we are recovering the solution vectors
136  Real recovery_time = stored_sols->first;
137 
138  // Print out what time we are recovering vectors at
139  // libMesh::out << "Recovering solution vectors at time: " <<
140  // recovery_time << std::endl;
141 
142  // Do we not have a solution for this time? Then
143  // there's nothing to do.
144  if (stored_sols == stored_solutions.end() ||
145  std::abs(recovery_time - _system.time) > TOLERANCE)
146  {
147  //libMesh::out << "No more solutions to recover ! We are at time t = " <<
148  // _system.time << std::endl;
149  return;
150  }
151 
152  // Get the saved vectors at this timestep
153  map_type & saved_vectors = stored_sols->second;
154 
155  map_type::iterator vec = saved_vectors.begin();
156  map_type::iterator vec_end = saved_vectors.end();
157 
158  // Loop over all the saved vectors
159  for (; vec != vec_end; ++vec)
160  {
161  // The name of this vector
162  const std::string & vec_name = vec->first;
163 
164  // Get the vec_name entry in the saved vectors map and set the
165  // current system vec[vec_name] entry to it
166  if (vec_name != "_solution")
167  _system.get_vector(vec_name) = *(vec->second);
168  }
169 
170  // Of course, we will *always* have to get the actual solution
171  std::string _solution("_solution");
172  *(_system.solution) = *(saved_vectors[_solution]);
173 }
174 
175 }
libMesh::System::vectors_iterator
std::map< std::string, NumericVector< Number > * >::iterator vectors_iterator
Vector iterator typedefs.
Definition: system.h:756
libMesh::MemorySolutionHistory::retrieve
virtual void retrieve() override
Virtual function retrieve which we will be overriding to retrieve timesteps.
Definition: memory_solution_history.C:131
libMesh::MemorySolutionHistory::_system
System & _system
Definition: memory_solution_history.h:97
libMesh::MemorySolutionHistory::store
virtual void store() override
Virtual function store which we will be overriding to store timesteps.
Definition: memory_solution_history.C:67
libMesh
The libMesh namespace provides an interface to certain functionality in the library.
Definition: factoryfunction.C:55
libMesh::SolutionHistory::overwrite_previously_stored
bool overwrite_previously_stored
Definition: solution_history.h:77
libMesh::TOLERANCE
static const Real TOLERANCE
Definition: libmesh_common.h:128
libMesh::MemorySolutionHistory::stored_solutions_iterator
list_type::iterator stored_solutions_iterator
Definition: memory_solution_history.h:74
libMesh::MemorySolutionHistory::stored_solutions
list_type stored_solutions
Definition: memory_solution_history.h:88
libMesh::System::project_solution_on_reinit
bool & project_solution_on_reinit(void)
Tells the System whether or not to project the solution vector onto new grids when the system is rein...
Definition: system.h:802
libMesh::libmesh_assert
libmesh_assert(ctx)
libMesh::MemorySolutionHistory::find_stored_entry
void find_stored_entry()
Definition: memory_solution_history.C:32
libMesh::MemorySolutionHistory::stored_sols
stored_solutions_iterator stored_sols
Definition: memory_solution_history.h:91
std::abs
MetaPhysicL::DualNumber< T, D > abs(const MetaPhysicL::DualNumber< T, D > &in)
libMesh::System::vectors_end
vectors_iterator vectors_end()
End of vectors container.
Definition: system.h:2307
libMesh::System::vectors_begin
vectors_iterator vectors_begin()
Beginning of vectors container.
Definition: system.h:2295
libMesh::MemorySolutionHistory::~MemorySolutionHistory
~MemorySolutionHistory()
Destructor.
Definition: memory_solution_history.C:26
libMesh::MemorySolutionHistory::map_type
std::map< std::string, std::unique_ptr< NumericVector< Number > > > map_type
Typedef for Stored Solutions iterator, a list of pairs of the current system time,...
Definition: memory_solution_history.h:72
libMesh::System::time
Real time
For time-dependent problems, this is the time t at the beginning of the current timestep.
Definition: system.h:1561
libMesh::System::solution
std::unique_ptr< NumericVector< Number > > solution
Data structure to hold solution values.
Definition: system.h:1539
libMesh::Real
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
Definition: libmesh_common.h:121
libMesh::System::vector_preservation
bool vector_preservation(const std::string &vec_name) const
Definition: system.C:863
libMesh::System::get_vector
const NumericVector< Number > & get_vector(const std::string &vec_name) const
Definition: system.C:774