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