Line data Source code
1 : /*
2 : * Copyright (c) 2011-2015: G-CSC, Goethe University Frankfurt
3 : * Author: Andreas Vogel
4 : *
5 : * This file is part of UG4.
6 : *
7 : * UG4 is free software: you can redistribute it and/or modify it under the
8 : * terms of the GNU Lesser General Public License version 3 (as published by the
9 : * Free Software Foundation) with the following additional attribution
10 : * requirements (according to LGPL/GPL v3 §7):
11 : *
12 : * (1) The following notice must be displayed in the Appropriate Legal Notices
13 : * of covered and combined works: "Based on UG4 (www.ug4.org/license)".
14 : *
15 : * (2) The following notice must be displayed at a prominent place in the
16 : * terminal output of covered works: "Based on UG4 (www.ug4.org/license)".
17 : *
18 : * (3) The following bibliography is recommended for citation and must be
19 : * preserved in all covered files:
20 : * "Reiter, S., Vogel, A., Heppner, I., Rupp, M., and Wittum, G. A massively
21 : * parallel geometric multigrid solver on hierarchically distributed grids.
22 : * Computing and visualization in science 16, 4 (2013), 151-164"
23 : * "Vogel, A., Reiter, S., Rupp, M., Nägel, A., and Wittum, G. UG4 -- a novel
24 : * flexible software system for simulating pde based models on high performance
25 : * computers. Computing and visualization in science 16, 4 (2013), 165-179"
26 : *
27 : * This program is distributed in the hope that it will be useful,
28 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
29 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 : * GNU Lesser General Public License for more details.
31 : */
32 :
33 : #ifndef __H__UG__LIB_DISC__TIME_DISC__PREVIOUS_SOLUTION__
34 : #define __H__UG__LIB_DISC__TIME_DISC__PREVIOUS_SOLUTION__
35 :
36 : // extern libraries
37 : #include <deque>
38 :
39 : // other ug libraries
40 : #include "common/common.h"
41 :
42 : #include "lib_disc/common/local_algebra.h"
43 :
44 : namespace ug{
45 :
46 : /// \ingroup lib_disc_time_assemble
47 : /// @{
48 :
49 : /// time series of solutions and corresponding time point
50 : /**
51 : * This class holds solutions and corresponding points in time. It is
52 : * intended to group previous computed solutions for a time stepping scheme, such
53 : * that previous steps can be passed to a time stepping scheme at once. Internally,
54 : * this object is basically a deque of old solutions, and adding a newly
55 : * computed solution lets the object pop the oldest stored solution.
56 : */
57 : template <typename TVector>
58 0 : class VectorTimeSeries
59 : {
60 : public:
61 : /// vector type of solutions
62 : typedef TVector vector_type;
63 :
64 : public:
65 0 : virtual ~VectorTimeSeries() {}
66 :
67 : //! clones the object (deep-copy) including values
68 0 : SmartPtr<VectorTimeSeries<vector_type> > clone() const
69 : {
70 : SmartPtr<VectorTimeSeries<TVector> > cloneTimeSol
71 0 : = SmartPtr<VectorTimeSeries<TVector> >(new VectorTimeSeries<TVector>);
72 :
73 0 : for(int i = m_vTimeSol.size()-1; i >= 0 ; --i)
74 0 : cloneTimeSol->push(solution(i)->clone(), time(i));
75 :
76 0 : return cloneTimeSol;
77 : }
78 :
79 : /// clears the content of the member m_vTimeSol
80 : void clear() {m_vTimeSol.clear();}
81 :
82 : /// returns number of time steps handled
83 0 : size_t size() const {return m_vTimeSol.size();}
84 :
85 : /// returns point in time for solution
86 0 : number time(size_t i) const {return m_vTimeSol.at(i).time();}
87 :
88 : /// returns solution
89 0 : SmartPtr<vector_type> solution(size_t i) {return m_vTimeSol.at(i).solution();}
90 :
91 : /// returns solution
92 0 : ConstSmartPtr<vector_type> solution(size_t i) const {return m_vTimeSol.at(i).solution();}
93 :
94 : /// returns oldest solution
95 0 : SmartPtr<vector_type> oldest() {return m_vTimeSol.back().solution();}
96 :
97 : /// const access to oldest solution
98 : ConstSmartPtr<vector_type> oldest() const {return m_vTimeSol.back().solution();}
99 :
100 : /// time associated with oldest solution
101 : number oldest_time() const {return m_vTimeSol.back().time();}
102 :
103 : /// returns latest solution
104 0 : SmartPtr<vector_type> latest() {return m_vTimeSol.front().solution();}
105 :
106 : /// const access to latest solution
107 : ConstSmartPtr<vector_type> latest() const {return m_vTimeSol.front().solution();}
108 :
109 : /// time associated with latest solution
110 : number latest_time() const {return m_vTimeSol.front().time();}
111 :
112 : /// adds new time point, not discarding the oldest
113 0 : void push(SmartPtr<vector_type> vec, number time) {m_vTimeSol.push_front(TimeSol(vec, time));}
114 :
115 : /// adds new time point, oldest solution is discarded and returned
116 0 : SmartPtr<vector_type> push_discard_oldest(SmartPtr<vector_type> vec, number time)
117 : {
118 : SmartPtr<vector_type> discardVec = m_vTimeSol.back().solution();
119 0 : remove_oldest(); push(vec, time);
120 0 : return discardVec;
121 : }
122 :
123 : /// removes latest time point
124 0 : void remove_latest() {m_vTimeSol.pop_front();}
125 :
126 : /// removes oldest time point
127 0 : void remove_oldest() {m_vTimeSol.pop_back();}
128 :
129 : protected:
130 : /// grouping of solution and time point
131 0 : class TimeSol
132 : {
133 : public:
134 : TimeSol() : vec(NULL), t(0.0) {}
135 :
136 0 : TimeSol(SmartPtr<vector_type> vec_, number t_)
137 0 : : vec(vec_), t(t_) {}
138 :
139 : /// access solution
140 : SmartPtr<vector_type> solution() {return vec;}
141 :
142 : /// const access solution
143 : ConstSmartPtr<vector_type> solution() const {return vec;}
144 :
145 : /// access time
146 : number& time() {return t;}
147 :
148 : /// const access time
149 : const number& time() const {return t;}
150 :
151 : protected:
152 : // solution vector at time point
153 : SmartPtr<vector_type> vec;
154 :
155 : // point in time
156 : number t;
157 : };
158 :
159 : protected:
160 :
161 : // deque of previous solutions
162 : std::deque<TimeSol> m_vTimeSol;
163 : };
164 :
165 : /// time series of local vectors
166 0 : class LocalVectorTimeSeries
167 : {
168 : public:
169 : /// constructor
170 : LocalVectorTimeSeries() {}
171 :
172 : /// returns number of time points
173 : size_t size() const {return m_vLocalVector.size();}
174 :
175 : /// returns time point i
176 0 : number time(size_t i) const {return m_vTime.at(i);}
177 :
178 : /// returns time points
179 0 : const std::vector<number>& times() const {return m_vTime;}
180 :
181 : /// returns the local vector for the i'th time point
182 : const LocalVector& solution(size_t i) const {return m_vLocalVector.at(i);}
183 :
184 : /// returns the local vector for the i'th time point
185 : LocalVector& solution(size_t i) {return m_vLocalVector.at(i);}
186 :
187 : /// access dofs by map
188 0 : void access_by_map(const FunctionIndexMapping& funcMap)
189 : {
190 0 : for(size_t t=0; t < size(); ++t)
191 : solution(t).access_by_map(funcMap);
192 0 : }
193 :
194 : /// extract local values from global vectors
195 : template <typename TVector>
196 0 : void read_values(ConstSmartPtr<VectorTimeSeries<TVector> > vecTimeSeries, LocalIndices& ind)
197 : {
198 0 : m_vLocalVector.resize(vecTimeSeries->size());
199 0 : for(size_t i = 0; i < m_vLocalVector.size(); ++i)
200 : {
201 0 : m_vLocalVector[i].resize(ind);
202 0 : GetLocalVector(m_vLocalVector[i], *vecTimeSeries->solution(i));
203 : }
204 0 : }
205 :
206 : /// extract time points
207 : template <typename TVector>
208 0 : void read_times(ConstSmartPtr<VectorTimeSeries<TVector> > vecTimeSeries)
209 : {
210 0 : m_vTime.resize(vecTimeSeries->size());
211 0 : for(size_t i = 0; i < m_vTime.size(); ++i)
212 0 : m_vTime[i] = vecTimeSeries->time(i);
213 0 : }
214 :
215 : protected:
216 : /// time series
217 : std::vector<number> m_vTime;
218 :
219 : /// vector of local vectors (one for each time point)
220 : std::vector<LocalVector> m_vLocalVector;
221 : };
222 :
223 : /// @}
224 :
225 : } // end namespace ug
226 :
227 : #endif /* __H__UG__LIB_DISC__TIME_DISC__PREVIOUS_SOLUTION__ */
|