libMesh
Loading...
Searching...
No Matches
Public Types | Public Member Functions | Protected Types | Protected Member Functions | Protected Attributes | Private Attributes | List of all members
libMesh::MemorySolutionHistory Class Reference

Subclass of Solution History that stores the solutions and other important vectors in memory. More...

#include <memory_solution_history.h>

Inheritance diagram for libMesh::MemorySolutionHistory:
[legend]

Public Types

typedef std::map< std::string, std::unique_ptr< NumericVector< Number > > > map_type
 

Public Member Functions

 MemorySolutionHistory (DifferentiableSystem &system_)
 Constructor, reference to system to be passed by user, set the stored_sols iterator to some initial value.
 
 ~MemorySolutionHistory ()
 Destructor.
 
virtual void store (bool is_adjoint_solve, Real time) override
 Virtual function store which we will be overriding to store timesteps.
 
virtual void retrieve (bool is_adjoint_solve, Real time) override
 Virtual function retrieve which we will be overriding to retrieve timesteps.
 
virtual std::unique_ptr< SolutionHistoryclone () const override
 Definition of the clone function needed for the setter function.
 
void erase (Real time)
 Erase stored_data entry at time.
 
void set_overwrite_previously_stored (bool val)
 Turn on overwrite_previously_stored to overwrite any already-saved data encountered during subsequent store() calls.
 

Protected Types

typedef map_type::iterator stored_data_iterator
 

Protected Member Functions

void find_stored_entry (Real time, bool storing=false)
 

Protected Attributes

bool overwrite_previously_stored
 
map_type stored_data
 
stored_data_iterator stored_datum
 

Private Attributes

DifferentiableSystem_system
 

Detailed Description

Subclass of Solution History that stores the solutions and other important vectors in memory.

Author
Vikram Garg
Date
2012

Stores past solutions in memory.

Definition at line 43 of file memory_solution_history.h.

Member Typedef Documentation

◆ map_type

typedef std::map<std::string, std::unique_ptr<NumericVector<Number> > > libMesh::MemorySolutionHistory::map_type

Definition at line 69 of file memory_solution_history.h.

◆ stored_data_iterator

typedef map_type::iterator libMesh::SolutionHistory::stored_data_iterator
protectedinherited

Definition at line 95 of file solution_history.h.

Constructor & Destructor Documentation

◆ MemorySolutionHistory()

libMesh::MemorySolutionHistory::MemorySolutionHistory ( DifferentiableSystem system_)
inline

Constructor, reference to system to be passed by user, set the stored_sols iterator to some initial value.

Definition at line 51 of file memory_solution_history.h.

51 : SolutionHistory(), _system(system_)
52 { libmesh_experimental(); }

◆ ~MemorySolutionHistory()

libMesh::MemorySolutionHistory::~MemorySolutionHistory ( )

Destructor.

Definition at line 30 of file memory_solution_history.C.

31{
32}

Member Function Documentation

◆ clone()

virtual std::unique_ptr< SolutionHistory > libMesh::MemorySolutionHistory::clone ( ) const
inlineoverridevirtual

Definition of the clone function needed for the setter function.

Implements libMesh::SolutionHistory.

Definition at line 74 of file memory_solution_history.h.

75 {
76 return std::make_unique<MemorySolutionHistory>(_system);
77 }

References _system.

◆ erase()

void libMesh::SolutionHistory::erase ( Real  time)
inherited

Erase stored_data entry at time.

Definition at line 95 of file solution_history.C.

96 {
97 // We cant erase the stored_datum iterator which is used in other places
98 // So save its current value for the future
99 stored_data_iterator stored_datum_last = stored_datum;
100 //std::map<Real, unsigned int>::iterator timeTotimestamp_iterator_last = timeTotimestamp_iterator;
101
102 // This will map the stored_datum iterator to the current time
103 this->find_stored_entry(time, false);
104
105 // map::erase behaviour is undefined if the iterator is pointing
106 // to a non-existent element.
108
109 // We want to keep using the stored_datum iterator, so we have to create
110 // a new one to erase the concerned entry
111 stored_data_iterator stored_datum_copy = stored_datum;
112
113 // If we're asking to erase the entry at stored_datum, then move stored_datum somewhere safer first
114 if(stored_datum == stored_datum_last)
115 stored_datum--;
116
117 stored_data.erase(stored_datum_copy);
118 }
void find_stored_entry(Real time, bool storing=false)
map_type::iterator stored_data_iterator
stored_data_iterator stored_datum
libmesh_assert(ctx)

References libMesh::SolutionHistory::find_stored_entry(), libMesh::libmesh_assert(), libMesh::SolutionHistory::stored_data, and libMesh::SolutionHistory::stored_datum.

◆ find_stored_entry()

void libMesh::SolutionHistory::find_stored_entry ( Real  time,
bool  storing = false 
)
protectedinherited

Definition at line 27 of file solution_history.C.

28 {
29 if (stored_data.begin() == stored_data.end())
30 return;
31
32 // We will use the map::lower_bound operation to find the key which
33 // is the least upper bound among all existing keys for time.
34 // (key before map::lower_bound) < time < map::lower_bound, one of these
35 // should be within TOLERANCE of time (unless we are creating a new map entry)
36 // If the lower bound iterator points to:
37 // begin -> we are looking for the solution at the initial time
38 // end -> we are creating a new entry
39 // anything else, we are looking for an existing entry
40 stored_data_iterator lower_bound_it = stored_data.lower_bound(time);
41
42 // For the key right before the lower bound
43 stored_data_iterator lower_bound_it_decremented;
44
45 // If we are at end, we could be creating a new entry (depends on the storing bool), return
46 // Otherwise, get a decremented iterator for the sandwich test
47 if(lower_bound_it == stored_data.end())
48 {
49 // If we are storing and lower_bound_it points to stored_data.end(), we assume
50 // that this is a brand new entry in the map. We leave stored_datum unchanged.
51 if(storing)
52 {
53 return;
54 }
55 else
56 {
57 // We are trying to retrieve and none of the keys was an upper bound.
58 // We could have a situation in which the time is greatest key + FPE.
59 // So we can check the key before the end and see if it matches time, else we have an error.
60 lower_bound_it = std::prev(lower_bound_it);
61 }
62 }
63 else if(lower_bound_it == stored_data.begin()) // At the beginning, so we cant go back any further
64 {
65 stored_datum = stored_data.begin();
66 return;
67 }
68 else // A decremented iterator, to perform the sandwich test for the key closest to time
69 {
70 lower_bound_it_decremented = std::prev(lower_bound_it);
71 }
72
73 // Set the stored sols iterator as per the key which is within TOLERANCE of time
74 if(std::abs(lower_bound_it->first - time) < TOLERANCE)
75 {
76 stored_datum = lower_bound_it;
77 }
78 else if(std::abs(lower_bound_it_decremented->first - time) < TOLERANCE)
79 {
80 stored_datum = lower_bound_it_decremented;
81 }
82 else // Neither of the two candidate keys matched our time
83 {
84 if(storing) // If we are storing, this is fine, we need to create a new entry, so just return
85 {
86 return;
87 }
88 else // If we are not storing, then we expected to find something but didn't, so we have a problem
89 {
90 libmesh_error_msg("Failed to set stored solutions iterator to a valid value.");
91 }
92 }
93 }
static constexpr Real TOLERANCE

References libMesh::SolutionHistory::stored_data, libMesh::SolutionHistory::stored_datum, and libMesh::TOLERANCE.

Referenced by libMesh::SolutionHistory::erase(), libMesh::FileSolutionHistory::retrieve(), retrieve(), libMesh::FileSolutionHistory::store(), and store().

◆ retrieve()

void libMesh::MemorySolutionHistory::retrieve ( bool  is_adjoint_solve,
Real  time 
)
overridevirtual

Virtual function retrieve which we will be overriding to retrieve timesteps.

Implements libMesh::SolutionHistory.

Definition at line 87 of file memory_solution_history.C.

88{
89 this->find_stored_entry(time, false);
90
91 // If we are solving the adjoint, the timestep we need to move to the past step
92 // is the one taken at that step to get to the current time.
93 // At the initial time, be ready for the primal time march again.
94 if(is_adjoint_solve)
95 {
96 if( stored_datum != stored_data.begin() )
97 {
98 stored_data_iterator stored_datum_past = stored_datum;
99 stored_datum_past--;
100
101 _system.deltat = (stored_datum_past->second)->get_deltat_at();
102 }
103 else
104 {
105 _system.deltat = (stored_datum->second)->get_deltat_at();
106 }
107 }
108 else
109 {
110 if( stored_datum != std::prev(stored_data.end()) )
111 _system.deltat = (stored_datum->second)->get_deltat_at();
112 else
113 {
114 stored_data_iterator stored_datum_past = stored_datum;
115 stored_datum_past--;
116
117 _system.deltat = (stored_datum_past->second)->get_deltat_at();
118 }
119
120 }
121
122 // Get the time at which we are recovering the solution vectors
123 Real recovery_time = stored_datum->first;
124
125 // Print out what time we are recovering vectors at
126 // libMesh::out << "Recovering solution vectors at time: " <<
127 // recovery_time << std::endl;
128
129 // Do we not have a solution for this time? Then
130 // there's nothing to do.
131 if (stored_datum == stored_data.end() ||
132 std::abs(recovery_time - time) > TOLERANCE)
133 {
134 //libMesh::out << "No more solutions to recover ! We are at time t = " <<
135 // _system.time << std::endl;
136 return;
137 }
138
139 (stored_datum->second)->retrieve_primal_solution();
140
141 // We need to call update to put system in a consistent state
142 // with the solution that was read in
143 _system.update();
144}
Real deltat
For time-dependent problems, this is the amount delta t to advance the solution in time.
virtual void update()
Update the local values to reflect the solution on neighboring processors.
Definition system.C:498
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real

References _system, libMesh::DifferentiableSystem::deltat, libMesh::SolutionHistory::find_stored_entry(), libMesh::Real, libMesh::SolutionHistory::stored_data, libMesh::SolutionHistory::stored_datum, libMesh::TOLERANCE, and libMesh::System::update().

◆ set_overwrite_previously_stored()

void libMesh::SolutionHistory::set_overwrite_previously_stored ( bool  val)
inlineinherited

Turn on overwrite_previously_stored to overwrite any already-saved data encountered during subsequent store() calls.

Definition at line 80 of file solution_history.h.

References libMesh::SolutionHistory::overwrite_previously_stored.

◆ store()

void libMesh::MemorySolutionHistory::store ( bool  is_adjoint_solve,
Real  time 
)
overridevirtual

Virtual function store which we will be overriding to store timesteps.

Implements libMesh::SolutionHistory.

Definition at line 36 of file memory_solution_history.C.

37{
38 this->find_stored_entry(time, true);
39
40 // In an empty history we create the first entry
41 if (stored_data.begin() == stored_data.end())
42 {
43 stored_data[time] = std::make_unique<MemoryHistoryData>(_system);
44 stored_datum = stored_data.begin();
45 }
46
47 // If we're past the end we can create a new entry
48 if (time - stored_datum->first > TOLERANCE )
49 {
50#ifndef NDEBUG
53#endif
54 stored_data[time] = std::make_unique<MemoryHistoryData>(_system);
57 }
58
59 // If we're before the beginning we can create a new entry
60 else if (stored_datum->first - time > TOLERANCE)
61 {
63 stored_data[time] = std::make_unique<MemoryHistoryData>(_system);
64 stored_datum = stored_data.begin();
65 }
66
67 // We don't support inserting entries elsewhere
68 libmesh_assert(std::abs(stored_datum->first - time) < TOLERANCE);
69
70 // First we handle the case of the initial data, this is the only case in which
71 // stored_data will have size one
72 if(stored_data.size() == 1)
73 {
74 // The initial data should only be stored once.
75 (stored_datum->second)->store_initial_solution();
76 }
77 else if((stored_datum->second)->get_previously_stored() == false) // If we are not at the initial time, we are either creating a new entry or overwriting an existing one
78 {
79 (stored_datum->second)->store_primal_solution(stored_datum);
80 }
81 else // We are overwriting an existing history data
82 {
83 (stored_datum->second)->rewrite_stored_solution();
84 }
85}

References _system, libMesh::SolutionHistory::find_stored_entry(), libMesh::libmesh_assert(), libMesh::SolutionHistory::stored_data, libMesh::SolutionHistory::stored_datum, and libMesh::TOLERANCE.

Member Data Documentation

◆ _system

DifferentiableSystem& libMesh::MemorySolutionHistory::_system
private

Definition at line 82 of file memory_solution_history.h.

Referenced by clone(), retrieve(), and store().

◆ overwrite_previously_stored

bool libMesh::SolutionHistory::overwrite_previously_stored
protectedinherited

◆ stored_data

map_type libMesh::SolutionHistory::stored_data
protectedinherited

◆ stored_datum

stored_data_iterator libMesh::SolutionHistory::stored_datum
protectedinherited

The documentation for this class was generated from the following files: