Flow123d  jenkins-Flow123d-windows32-release-multijob-51
time_marks.hh
Go to the documentation of this file.
1 /*!
2  *
3  * Copyright (C) 2007 Technical University of Liberec. All rights reserved.
4  *
5  * Please make a following refer to Flow123d on your project site if you use the program for any purpose,
6  * especially for academic research:
7  * Flow123d, Research Centre: Advanced Remedial Technologies, Technical University of Liberec, Czech Republic
8  *
9  * This program is free software; you can redistribute it and/or modify it under the terms
10  * of the GNU General Public License version 3 as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
13  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14  * See the GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along with this program; if not,
17  * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 021110-1307, USA.
18  *
19  *
20  * $Id: time_marks.hh 1974 2012-11-14 13:46:11Z pavel.exner $
21  * $Revision: 1974 $
22  * $LastChangedBy: pavel.exner $
23  * $LastChangedDate: 2012-11-14 14:46:11 +0100 (St, 14 lis 2012) $
24  *
25  * @file
26  * @brief
27  *
28  * @author Jan Brezina
29  * Created on: Jun 15, 2011
30  *
31  */
32 
33 #ifndef TIME_MARKS_HH_
34 #define TIME_MARKS_HH_
35 
36 #include "system/global_defs.h"
37 
38 /**
39  * @brief Class used for marking specified times at which some events occur.
40  *
41  * This class represents one record in the TimeMarks simple database.
42  * Class members can not be modified after the item is created.
43  */
44 class TimeMark {
45 public:
46 
47  /**
48  * MarkType is a bitmap where each bit represents one base type such as (Output, Input, BC change, Fixed...)
49  * This allow more complex queries through bitwise operations. Also one TimeMark can be shared by more events.
50  * In the context of TimeMarks the Type can be either fixed or vague. If a TimeGovernor is connected to the TimeMarks object
51  * the TimeMarks with fixed Type are used to match exactly their times. Base Types should be created/obtained from TimeMarks class
52  * through the TimeMarks::new_mark_type method.
53  * There are three Types predefined in TimeMarks constructor:
54  * - type_fixed_time (hex 0x01)
55  * - type_output (hex 0x02)
56  * - type_bc_change (hex 0x04)
57  * @see TimeMarks
58  */
59  typedef unsigned long int Type;
60 
61  /// Mask that matches every type of TimeMark.
62  static const Type every_type;
63  /// Mask that matches no type of TimeMark.
64  static const Type none_type;
65 
66  /**
67  * Constructor for a TimeMarks::Mark
68  * @param time time of the mark
69  * @param type type of the mark
70  *
71  * In order to create a fixed TimeMark (at time=0.1) with base TimeMark::Type my_type, use the TimeMarks class:
72  * TimeMark( 0.1, timemarks.type_fixed_time() | my_type)
73  */
74  TimeMark(double time, Type type) :
75  time_(time), mark_type_(type) {}
76 
77 
78  /// Getter for mark type.
79  inline Type mark_type() const {
80  return mark_type_;
81  }
82 
83  /// Getter for the time of the TimeMark.
84  inline double time() const {
85  return time_;
86  }
87 
88  /**
89  * Returns true if TimeMark's type has 1 on all positions where mask has 1.
90  * @param mask {Select bits that should be 1 for matching mark types.
91  */
92 
93  inline bool match_mask(const TimeMark::Type &mask) const {
94  return ( mask & (~mark_type_) ) == 0;
95  }
96 
97  /// Add more bits that a mark satisfies.
98  /// @param type type that should be modified
99  inline void add_to_type(const TimeMark::Type &type) {
100  mark_type_ |= type;
101  }
102 
103  /// Comparison of time marks according to their time.
104  /// @param another is another Timemark which should be compared.
105  bool operator<(const TimeMark& another) const
106  { return time_ < another.time(); }
107 
108 
109 private:
110  /// The marked time.
111  double time_;
112  /// The type of the TimeMark.
114 };
115 
116 /**
117  * Output to stream operator for TimeMark class.
118  */
119 ostream& operator<<(ostream& stream, const TimeMark &marks);
120 
121 
122 
123 
124 /***************************************************************************************/
125 
126 
127 
128 
129 
130 /***************************************************************************************/
131 class TimeGovernor;
132 class TimeMarksIterator;
133 
134 /**
135  * @brief This class is a collection of time marks to manage various events occurring during simulation time.
136  *
137  * <b> TimeMark and their types </b>
138  *
139  * One TimeMark consists of time and type (TimeMark::Type), see the constructor TimeMark::TimeMark.
140  * The type of mark is bitmap where individual bits corresponds to some base event types like changing a BC, output solution, coupling time with another
141  * equation and so on. Base types can be combined by bitwise or (operator|).
142  *
143  * Special types are predefined in TimeMarks class. These are returned by their getters:
144  * - type_fixed_time() - marks of this type are considered as fixed times by a TimeGovernor which is connected to particular TimeMarks object.
145  * - type_output() - this type marks times at which solution should be computed and written to output.
146  * - type_bc_change() - this type marks times at which BC is is changed and model has to be updated.
147  *
148  * <b> TimeMarks collection </b>
149  *
150  * TimeMarks collect marks of various types and provides methods for iterating over stored marks. You can selectively access only marks matching given
151  * type mask. See TimeMark::match_mask.
152  *
153  * You can add one new mark through method add or add evenly spaced marks of same type by TimeMarks::add_time_marks.
154  *
155  * You can allocate new TimeMark::Type in the context of one TimeMarks object by TimeMarks::new_mark_type.
156  *
157  * For a given TimeGovernor (not necessarily connected one) you can ask about existence of mark in current time interval (TimeMarks::is_current) and see TimeMarks
158  * close to the current time (TimeMarks::next and TimeMarks::last). The current time interval is left-open and right-closed: (t,t+dt]. Repeatedly used TimeMarks::next always returns the same TimeMark if the time of the TimeGovernor is not changed.
159  *
160  * In most cases there will be only one TimeMarks object for the whole solved problem and used by TimeGovernors of individual equations. However
161  * this is not necessary.
162  *
163  * @see TimeMark
164  */
165 class TimeMarks {
166 
167 public:
168  /// Iterator class for iteration over time marks of particular type. This is always const_iterator.
170 
171  /**
172  * Default constructor.
173  */
174  TimeMarks();
175 
176  /**
177  * Add a new base mark within the context of the particular TimeMarks instance.
178  * User should keep the returned value (MarkType is basically a bitmap) for further queries and
179  * TimeMark insertions. ATTENTION: You can not use the TimeMark::Type with other TimeMarks instance!
180  * Types are added as they are prepared in next_mark_type_.
181  * Next mark type is updated by (left) bit shifting operator.
182  */
184 
185  /// Predefined base TimeMark type that is taken into account by the TimeGovernor.
186  /// Is defined by constructor as 0x01.
188  { return type_fixed_time_;}
189 
190  /// Predefined base TimeMark type for output times.
191  /// Is defined by constructor as 0x02.
193  { return type_output_;}
194 
195  /// Predefined base TimeMark type for times when the boundary condition is changed.
196  /// Is defined by constructor as 0x04.
198  { return type_input_;}
199 
200 
201  /**
202  * Basic method for inserting TimeMarks.
203  * @param mark Reference to TimeMark object.
204  */
205  void add(const TimeMark &mark);
206 
207  /**
208  * Method for creating and inserting equally spaced TimeMarks.
209  * @param time Time of the first TimeMark.
210  * @param dt Lenght of interval between equally spaced TimeMarks.
211  * @param end_time No marks after the end_time.
212  * @param type Type of inserted TimeMarks or their combinations.
213  *
214  * Current lazy implementation have complexity O(m*n) where m is number of inserted time marks and n number of time marks in the array.
215  * TODO: O(n+m) implementation
216  */
217  void add_time_marks(double time, double dt, double end_time, TimeMark::Type type);
218 
219  /**
220  * Find the last time mark matching given mask, and returns true if it is in the time interval of
221  * current time step.
222  */
223  bool is_current(const TimeGovernor &tg, const TimeMark::Type &mask) const;
224 
225  /**
226  * Return the first TimeMark with time strictly greater then tg.time() that match the mask.
227  * The time governor tg is used also for time comparisons.
228  *
229  * @param tg the time governor
230  * @param mask mask of marks to iterate on
231  *
232  * TODO: have also method which accepts double (time) instead of the whole TimeGovernor.
233  * and compare without safety.
234  */
235  TimeMarks::iterator next(const TimeGovernor &tg, const TimeMark::Type &mask) const;
236 
237  /**
238  * Return the last TimeMark with time less or equal to tg.time() that match the mask.
239  * The time governor tg is used also for time comparisons.
240  * @param tg the time governor
241  * @param mask mask of marks to iterate on
242  */
243  TimeMarks::iterator last(const TimeGovernor &tg, const TimeMark::Type &mask) const;
244 
245  /**
246  * Returns iterator to the last time mark matching given @p mask.
247  */
248  TimeMarks::iterator last(const TimeMark::Type &mask) const;
249 
250  /// Iterator for the begin mimics container-like of TimeMarks
252 
253  /// Iterator for the end mimics container-like of TimeMarks
255 
256  /// Friend output operator.
257  friend ostream& operator<<(ostream& stream, const TimeMarks &marks);
258 
259 private:
260 
261  /// MarkType that will be used at next new_time_mark() call.
263 
264  /// TimeMarks list sorted according to the their time.
265  vector<TimeMark> marks_;
266 
267  /// Predefined type for fixed time.
269  /// Predefined type for output.
271  /// Predefined type for change of boundary condition.
273 };
274 
275 
276 
277 /**
278  * @brief Iterator over TimeMark objects in TimeMarks object (database of TimeMark objects).
279  *
280  * Iterator over the TimeMarks of particular mask. This is always const iterator, i.e. it points to const TimeMark.
281  * While iterating over TimeMarks with different types, all non matching types are skipped.
282  */
284 public:
285  /**Constructor. It is used in TimeMarks class which has the vector of TimeMark objects.
286  * @param marks is vector of TimeMark objects.
287  * @param it is iterator over the vector of TimeMark objects.
288  * @param mask is the type of marks over which we iterate.
289  */
290  TimeMarksIterator(const vector<TimeMark> &marks,const vector<TimeMark>::const_iterator &it, const TimeMark::Type &mask)
291  : marks_(marks), it_(it), mask_(mask) {}
292 
294  {ASSERT(&marks_ == &it.marks_, "Can not assign TimeMarks::iterator of different container.\n");
295  it_=it.it_;
296  mask_=it.mask_;
297  return *this;
298  }
299 
300  /// Prefix increment. Skip non matching marks.
302  {
303  while ( it_ != marks_.end() ) {
304  ++it_;
305  if (it_->match_mask(mask_)) break;
306  }
307  return (*this);
308  }
309 
310  /// Prefix decrement. Skip non matching marks.
312  {
313  while ( it_ != marks_.begin() ) {
314  --it_;
315  if (it_->match_mask(mask_)) break;
316  }
317  return (*this);
318  }
319 
320  /// * dereference operator
321  inline const TimeMark & operator *() const
322  {
323  ASSERT(it_!= marks_.end(), "Out of marks vector.\n");
324  return *it_;
325  }
326 
327  /// -> dereference operator
328  inline const TimeMark * operator ->() const
329  {
330  ASSERT(it_!= marks_.end(), "Out of marks vector.\n");
331  return &(*(it_));
332  }
333 
334  inline bool operator ==(const TimeMarksIterator &other) const
335  {return it_ == other.it_; }
336 
337  inline bool operator !=(const TimeMarksIterator &other) const
338  {return it_ != other.it_; }
339 
340  /// Returns mask.
342  { return mask_; }
343 
344 private:
345  /// Reference to the vector of TimeMark objects.
346  const vector<TimeMark> &marks_;
347  /// Iterator over the vector of TimeMark objects.
348  vector<TimeMark>::const_iterator it_;
349  /// Mask type.
351 };
352 
353 #endif /* TIME_MARKS_HH_ */
void add_time_marks(double time, double dt, double end_time, TimeMark::Type type)
Definition: time_marks.cc:100
TimeMark::Type type_fixed_time()
Definition: time_marks.hh:187
Type mark_type() const
Getter for mark type.
Definition: time_marks.hh:79
unsigned long int Type
Definition: time_marks.hh:59
bool operator<(const TimeMark &another) const
Definition: time_marks.hh:105
TimeMarks::iterator next(const TimeGovernor &tg, const TimeMark::Type &mask) const
Definition: time_marks.cc:116
Iterator over TimeMark objects in TimeMarks object (database of TimeMark objects).
Definition: time_marks.hh:283
TimeMarks::iterator begin(TimeMark::Type mask=TimeMark::every_type) const
Iterator for the begin mimics container-like of TimeMarks.
Definition: time_marks.cc:146
TimeMarksIterator & operator=(const TimeMarksIterator &it)
Definition: time_marks.hh:293
Type mark_type_
The type of the TimeMark.
Definition: time_marks.hh:113
bool operator==(const TimeMarksIterator &other) const
Definition: time_marks.hh:334
TimeMark::Type type_output()
Definition: time_marks.hh:192
TimeMark(double time, Type type)
Definition: time_marks.hh:74
TimeMarksIterator(const vector< TimeMark > &marks, const vector< TimeMark >::const_iterator &it, const TimeMark::Type &mask)
Definition: time_marks.hh:290
TimeMark::Type type_input_
Predefined type for change of boundary condition.
Definition: time_marks.hh:272
Basic time management functionality for unsteady (and steady) solvers (class Equation).
static const Type none_type
Mask that matches no type of TimeMark.
Definition: time_marks.hh:64
bool operator!=(const TimeMarksIterator &other) const
Definition: time_marks.hh:337
static const Type every_type
Mask that matches every type of TimeMark.
Definition: time_marks.hh:62
TimeMarksIterator & operator--()
Prefix decrement. Skip non matching marks.
Definition: time_marks.hh:311
TimeMark::Type mask_
Mask type.
Definition: time_marks.hh:350
double time_
The marked time.
Definition: time_marks.hh:111
void add(const TimeMark &mark)
Definition: time_marks.cc:77
Global macros to enhance readability and debugging, general constants.
TimeMarksIterator & operator++()
Prefix increment. Skip non matching marks.
Definition: time_marks.hh:301
#define ASSERT(...)
Definition: global_defs.h:121
bool is_current(const TimeGovernor &tg, const TimeMark::Type &mask) const
Definition: time_marks.cc:109
TimeMarksIterator iterator
Iterator class for iteration over time marks of particular type. This is always const_iterator.
Definition: time_marks.hh:169
TimeMark::Type new_mark_type()
Definition: time_marks.cc:69
This class is a collection of time marks to manage various events occurring during simulation time...
Definition: time_marks.hh:165
TimeMark::Type next_mark_type_
MarkType that will be used at next new_time_mark() call.
Definition: time_marks.hh:262
vector< TimeMark >::const_iterator it_
Iterator over the vector of TimeMark objects.
Definition: time_marks.hh:348
bool match_mask(const TimeMark::Type &mask) const
Definition: time_marks.hh:93
TimeMark::Type mask()
Returns mask.
Definition: time_marks.hh:341
const vector< TimeMark > & marks_
Reference to the vector of TimeMark objects.
Definition: time_marks.hh:346
vector< TimeMark > marks_
TimeMarks list sorted according to the their time.
Definition: time_marks.hh:265
TimeMark::Type type_input()
Definition: time_marks.hh:197
void add_to_type(const TimeMark::Type &type)
Definition: time_marks.hh:99
double time() const
Getter for the time of the TimeMark.
Definition: time_marks.hh:84
const TimeMark * operator->() const
-> dereference operator
Definition: time_marks.hh:328
ostream & operator<<(ostream &stream, const TimeMark &marks)
Definition: time_marks.cc:41
Class used for marking specified times at which some events occur.
Definition: time_marks.hh:44
const TimeMark & operator*() const
Definition: time_marks.hh:321
TimeMarks::iterator end(TimeMark::Type mask=TimeMark::every_type) const
Iterator for the end mimics container-like of TimeMarks.
Definition: time_marks.cc:153
TimeMark::Type type_fixed_time_
Predefined type for fixed time.
Definition: time_marks.hh:268
friend ostream & operator<<(ostream &stream, const TimeMarks &marks)
Friend output operator.
Definition: time_marks.cc:160
TimeMark::Type type_output_
Predefined type for output.
Definition: time_marks.hh:270
TimeMarks::iterator last(const TimeGovernor &tg, const TimeMark::Type &mask) const
Definition: time_marks.cc:125