Flow123d  jenkins-Flow123d-windows-release-multijob-285
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 <ostream>
37 #include <vector>
38 
39 #include "system/system.hh"
40 #include "system/global_defs.h"
41 
42 
43 /**
44  * @brief Class used for marking specified times at which some events occur.
45  *
46  * This class represents one record in the TimeMarks simple database.
47  * Class members can not be modified after the item is created.
48  */
49 class TimeMark {
50 public:
51 
52  /**
53  * MarkType is a bitmap where each bit represents one base type such as (Output, Input, BC change, Fixed...)
54  * This allow more complex queries through bitwise operations. Also one TimeMark can be shared by more events.
55  * In the context of TimeMarks the Type can be either fixed or vague. If a TimeGovernor is connected to the TimeMarks object
56  * the TimeMarks with fixed Type are used to match exactly their times. Base Types should be created/obtained from TimeMarks class
57  * through the TimeMarks::new_mark_type method.
58  * There are three Types predefined in TimeMarks constructor:
59  * - type_fixed_time (hex 0x01)
60  * - type_output (hex 0x02)
61  * - type_bc_change (hex 0x04)
62  * @see TimeMarks
63  */
64  typedef unsigned long int Type;
65 
66  /// Mark Type with all bits set.
67  static const Type every_type;
68  /// Mark Type with all bits unset.
69  static const Type none_type;
70 
71  /**
72  * Constructor for a TimeMarks::Mark
73  * @param time time of the mark
74  * @param type type of the mark
75  *
76  * In order to create a fixed TimeMark (at time=0.1) with base TimeMark::Type my_type, use the TimeMarks class:
77  * TimeMark( 0.1, timemarks.type_fixed_time() | my_type)
78  */
79  TimeMark(double time, Type type) :
80  time_(time), mark_type_(type) {}
81 
82 
83  /// Getter for mark type.
84  inline Type mark_type() const {
85  return mark_type_;
86  }
87 
88  /// Getter for the time of the TimeMark.
89  inline double time() const {
90  return time_;
91  }
92 
93  /**
94  * Returns true if TimeMark's type has 1 on all positions where mask has 1.
95  * @param mask {Select bits that should be 1 for matching mark types.
96  */
97 
98  inline bool match_mask(const TimeMark::Type &mask) const {
99  return ( mask & (~mark_type_) ) == 0;
100  }
101 
102  /// Add more bits that a mark satisfies.
103  /// @param type type that should be modified
104  inline void add_to_type(const TimeMark::Type &type) {
105  mark_type_ |= type;
106  }
107 
108  /// Comparison of time marks according to their time.
109  /// @param another is another Timemark which should be compared.
110  bool operator<(const TimeMark& another) const
111  { return time_ < another.time(); }
112 
113 
114 private:
115  /// The marked time.
116  double time_;
117  /// The type of the TimeMark.
119 };
120 
121 /**
122  * Output to stream operator for TimeMark class.
123  */
124 std::ostream& operator<<(std::ostream& stream, const TimeMark &marks);
125 
126 
127 
128 
129 /***************************************************************************************/
130 
131 
132 
133 
134 
135 /***************************************************************************************/
136 class TimeGovernor;
137 class TimeMarksIterator;
138 
139 /**
140  * @brief This class is a collection of time marks to manage various events occurring during simulation time.
141  *
142  * <b> TimeMark and their types </b>
143  *
144  * One TimeMark consists of time and type (TimeMark::Type), see the constructor TimeMark::TimeMark.
145  * 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
146  * equation and so on. Base types can be combined by bitwise or (operator|).
147  *
148  * Special types are predefined in TimeMarks class. These are returned by their getters:
149  * - type_fixed_time() - marks of this type are considered as fixed times by a TimeGovernor which is connected to particular TimeMarks object.
150  * - type_output() - this type marks times at which solution should be computed and written to output.
151  * - type_bc_change() - this type marks times at which BC is is changed and model has to be updated.
152  *
153  * <b> TimeMarks collection </b>
154  *
155  * TimeMarks collect marks of various types and provides methods for iterating over stored marks. You can selectively access only marks matching given
156  * type mask. See TimeMark::match_mask.
157  *
158  * You can add one new mark through method add or add evenly spaced marks of same type by TimeMarks::add_time_marks.
159  *
160  * You can allocate new TimeMark::Type in the context of one TimeMarks object by TimeMarks::new_mark_type.
161  *
162  * 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
163  * 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.
164  *
165  * In most cases there will be only one TimeMarks object for the whole solved problem and used by TimeGovernors of individual equations. However
166  * this is not necessary.
167  *
168  * @see TimeMark
169  */
170 class TimeMarks {
171 
172 public:
173  /// Iterator class for iteration over time marks of particular type. This is always const_iterator.
175 
176  /**
177  * Default constructor.
178  */
179  TimeMarks();
180 
181  /**
182  * Reset state after construction (through default constructor).
183  * Useful for unit tests.
184  */
185  void reinit();
186 
187  /**
188  * Add a new base mark within the context of the particular TimeMarks instance.
189  * User should keep the returned value (MarkType is basically a bitmap) for further queries and
190  * TimeMark insertions. ATTENTION: You can not use the TimeMark::Type with other TimeMarks instance!
191  * Types are added as they are prepared in next_mark_type_.
192  * Next mark type is updated by (left) bit shifting operator.
193  */
195 
196  /// Predefined base TimeMark type that is taken into account by the TimeGovernor.
197  /// Is defined by constructor as 0x01.
199  { return type_fixed_time_;}
200 
201  /// Predefined base TimeMark type for output times.
202  /// Is defined by constructor as 0x02.
204  { return type_output_;}
205 
206  /// Predefined base TimeMark type for times when the boundary condition is changed.
207  /// Is defined by constructor as 0x04.
209  { return type_input_;}
210 
211 
212  /**
213  * Basic method for inserting TimeMarks.
214  * @param mark Reference to TimeMark object.
215  */
216  void add(const TimeMark &mark);
217 
218  /**
219  * Method for creating and inserting equally spaced TimeMarks.
220  * @param time Time of the first TimeMark.
221  * @param dt Lenght of interval between equally spaced TimeMarks.
222  * @param end_time No marks after the end_time.
223  * @param type Type of inserted TimeMarks or their combinations.
224  *
225  * 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.
226  * TODO: O(n+m) implementation
227  */
228  void add_time_marks(double time, double dt, double end_time, TimeMark::Type type);
229 
230  /**
231  * Find the last time mark matching given mask, and returns true if it is in the time interval of
232  * current time step.
233  */
234  bool is_current(const TimeGovernor &tg, const TimeMark::Type &mask) const;
235 
236  /**
237  * Return the first TimeMark with time strictly greater then tg.time() that match the mask.
238  * The time governor tg is used also for time comparisons.
239  *
240  * @param tg the time governor
241  * @param mask mask of marks to iterate on
242  *
243  * TODO: have also method which accepts double (time) instead of the whole TimeGovernor.
244  * and compare without safety.
245  */
246  TimeMarks::iterator next(const TimeGovernor &tg, const TimeMark::Type &mask) const;
247 
248  /**
249  * Return the last TimeMark with time less or equal to tg.time() that match the mask.
250  * The time governor tg is used also for time comparisons.
251  * @param tg the time governor
252  * @param mask mask of marks to iterate on
253  */
254  TimeMarks::iterator last(const TimeGovernor &tg, const TimeMark::Type &mask) const;
255 
256  /**
257  * Returns iterator to the last time mark matching given @p mask.
258  */
259  TimeMarks::iterator last(const TimeMark::Type &mask) const;
260 
261  /// Iterator for the begin mimics container-like of TimeMarks
263 
264  /// Iterator for the end mimics container-like of TimeMarks
266 
267  /// Friend output operator.
268  friend std::ostream& operator<<(std::ostream& stream, const TimeMarks &marks);
269 
270 private:
271 
272  /// MarkType that will be used at next new_time_mark() call.
274 
275  /// TimeMarks list sorted according to the their time.
277 
278  /// Predefined type for fixed time.
280  /// Predefined type for output.
282  /// Predefined type for change of boundary condition.
284 };
285 
286 
287 
288 /**
289  * @brief Iterator over TimeMark objects in TimeMarks object (database of TimeMark objects).
290  *
291  * Iterator over the TimeMarks of particular mask. This is always const iterator, i.e. it points to const TimeMark.
292  * While iterating over TimeMarks with different types, all non matching types are skipped.
293  */
295 public:
296  /**Constructor. It is used in TimeMarks class which has the vector of TimeMark objects.
297  * @param marks is vector of TimeMark objects.
298  * @param it is iterator over the vector of TimeMark objects.
299  * @param mask is the type of marks over which we iterate.
300  */
302  : marks_(marks), it_(it), mask_(mask) {}
303 
305  {ASSERT(&marks_ == &it.marks_, "Can not assign TimeMarks::iterator of different container.\n");
306  it_=it.it_;
307  mask_=it.mask_;
308  return *this;
309  }
310 
311  /// Prefix increment. Skip non matching marks.
313  {
314  while ( it_ != marks_.end() ) {
315  ++it_;
316  if (it_->match_mask(mask_)) break;
317  }
318  return (*this);
319  }
320 
321  /// Prefix decrement. Skip non matching marks.
323  {
324  while ( it_ != marks_.begin() ) {
325  --it_;
326  if (it_->match_mask(mask_)) break;
327  }
328  return (*this);
329  }
330 
331  /// * dereference operator
332  inline const TimeMark & operator *() const
333  {
334  ASSERT(it_!= marks_.end(), "Out of marks vector.\n");
335  return *it_;
336  }
337 
338  /// -> dereference operator
339  inline const TimeMark * operator ->() const
340  {
341  ASSERT(it_!= marks_.end(), "Out of marks vector.\n");
342  return &(*(it_));
343  }
344 
345  inline bool operator ==(const TimeMarksIterator &other) const
346  {return it_ == other.it_; }
347 
348  inline bool operator !=(const TimeMarksIterator &other) const
349  {return it_ != other.it_; }
350 
351  /// Returns mask.
353  { return mask_; }
354 
355 private:
356  /// Reference to the vector of TimeMark objects.
358  /// Iterator over the vector of TimeMark objects.
360  /// Mask type.
362 };
363 
364 #endif /* TIME_MARKS_HH_ */
void add_time_marks(double time, double dt, double end_time, TimeMark::Type type)
Definition: time_marks.cc:111
TimeMark::Type type_fixed_time()
Definition: time_marks.hh:198
Type mark_type() const
Getter for mark type.
Definition: time_marks.hh:84
unsigned long int Type
Definition: time_marks.hh:64
bool operator<(const TimeMark &another) const
Definition: time_marks.hh:110
TimeMarks::iterator next(const TimeGovernor &tg, const TimeMark::Type &mask) const
Definition: time_marks.cc:130
Iterator over TimeMark objects in TimeMarks object (database of TimeMark objects).
Definition: time_marks.hh:294
TimeMarks::iterator begin(TimeMark::Type mask=TimeMark::every_type) const
Iterator for the begin mimics container-like of TimeMarks.
Definition: time_marks.cc:163
TimeMarksIterator & operator=(const TimeMarksIterator &it)
Definition: time_marks.hh:304
Type mark_type_
The type of the TimeMark.
Definition: time_marks.hh:118
bool operator==(const TimeMarksIterator &other) const
Definition: time_marks.hh:345
TimeMark::Type type_output()
Definition: time_marks.hh:203
TimeMark(double time, Type type)
Definition: time_marks.hh:79
TimeMark::Type type_input_
Predefined type for change of boundary condition.
Definition: time_marks.hh:283
void reinit()
Definition: time_marks.cc:65
Basic time management functionality for unsteady (and steady) solvers (class Equation).
static const Type none_type
Mark Type with all bits unset.
Definition: time_marks.hh:69
bool operator!=(const TimeMarksIterator &other) const
Definition: time_marks.hh:348
static const Type every_type
Mark Type with all bits set.
Definition: time_marks.hh:67
TimeMarksIterator & operator--()
Prefix decrement. Skip non matching marks.
Definition: time_marks.hh:322
TimeMark::Type mask_
Mask type.
Definition: time_marks.hh:361
double time_
The marked time.
Definition: time_marks.hh:116
void add(const TimeMark &mark)
Definition: time_marks.cc:88
Global macros to enhance readability and debugging, general constants.
TimeMarksIterator & operator++()
Prefix increment. Skip non matching marks.
Definition: time_marks.hh:312
#define ASSERT(...)
Definition: global_defs.h:121
bool is_current(const TimeGovernor &tg, const TimeMark::Type &mask) const
Definition: time_marks.cc:122
std::vector< TimeMark > marks_
TimeMarks list sorted according to the their time.
Definition: time_marks.hh:276
friend std::ostream & operator<<(std::ostream &stream, const TimeMarks &marks)
Friend output operator.
TimeMarksIterator iterator
Iterator class for iteration over time marks of particular type. This is always const_iterator.
Definition: time_marks.hh:174
TimeMark::Type new_mark_type()
Definition: time_marks.cc:80
This class is a collection of time marks to manage various events occurring during simulation time...
Definition: time_marks.hh:170
std::ostream & operator<<(std::ostream &stream, const TimeMark &marks)
Definition: time_marks.cc:44
TimeMark::Type next_mark_type_
MarkType that will be used at next new_time_mark() call.
Definition: time_marks.hh:273
bool match_mask(const TimeMark::Type &mask) const
Definition: time_marks.hh:98
TimeMark::Type mask()
Returns mask.
Definition: time_marks.hh:352
TimeMark::Type type_input()
Definition: time_marks.hh:208
TimeMarksIterator(const std::vector< TimeMark > &marks, const std::vector< TimeMark >::const_iterator &it, const TimeMark::Type &mask)
Definition: time_marks.hh:301
void add_to_type(const TimeMark::Type &type)
Definition: time_marks.hh:104
double time() const
Getter for the time of the TimeMark.
Definition: time_marks.hh:89
const TimeMark * operator->() const
-> dereference operator
Definition: time_marks.hh:339
std::vector< TimeMark >::const_iterator it_
Iterator over the vector of TimeMark objects.
Definition: time_marks.hh:359
Class used for marking specified times at which some events occur.
Definition: time_marks.hh:49
const TimeMark & operator*() const
Definition: time_marks.hh:332
TimeMarks::iterator end(TimeMark::Type mask=TimeMark::every_type) const
Iterator for the end mimics container-like of TimeMarks.
Definition: time_marks.cc:170
TimeMark::Type type_fixed_time_
Predefined type for fixed time.
Definition: time_marks.hh:279
const std::vector< TimeMark > & marks_
Reference to the vector of TimeMark objects.
Definition: time_marks.hh:357
TimeMark::Type type_output_
Predefined type for output.
Definition: time_marks.hh:281
TimeMarks::iterator last(const TimeGovernor &tg, const TimeMark::Type &mask) const
Definition: time_marks.cc:141