Flow123d  jenkins-Flow123d-windows32-release-multijob-51
time_marks.cc
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.cc 1334 2011-09-19 13:58:59Z jan.brezina $
21  * $Revision: 1334 $
22  * $LastChangedBy: jan.brezina $
23  * $LastChangedDate: 2011-09-19 15:58:59 +0200 (Po, 19 zář 2011) $
24  *
25  * @file
26  * @brief
27  *
28  * @author Jan Brezina
29  */
30 
31 #include "system/system.hh"
32 #include "time_marks.hh"
33 #include "time_governor.hh"
34 #include <algorithm>
35 #include <limits>
36 
37 // ------------------------------------------------------
38 // implementation of members of class TimeMark
39 // ------------------------------------------------------
40 
41 ostream& operator<<(ostream& stream, const TimeMark &mark)
42 {
43  //return ( stream << mark.time()<<": 0o" << oct << mark.mark_type() << dec ); //octal output
44  return ( stream << mark.time()<<": 0x" << hex << mark.mark_type() << dec );
45 }
46 
49 
50 
51 // ------------------------------------------------------
52 // implementation of members of class TimeMarks
53 // ------------------------------------------------------
54 
56 : next_mark_type_(0x1)
57 {
58  // add predefined base mark types
62 
63  // insert start and end stoppers
64  marks_.push_back(TimeMark(-INFINITY, TimeMark::every_type));
65  marks_.push_back(TimeMark(+INFINITY, TimeMark::every_type));
66 }
67 
68 
70  ASSERT(next_mark_type_ != 0, "Can not allocate new mark type. The limit is 32 mark types.\n");
71  TimeMark::Type current_type = next_mark_type_;
72 
73  next_mark_type_ <<= 1;
74  return current_type;
75 }
76 
77 void TimeMarks::add(const TimeMark &mark) {
78  // find first mark with time greater or equal to the new mark
79  vector<TimeMark>::iterator first_ge = std::lower_bound(marks_.begin(), marks_.end(), mark);
80 
81  // check equivalence with found mark
82  if (fabs(first_ge->time() - mark.time()) < 2*numeric_limits<double>::epsilon()) {
83  //if "equal" does bitwise OR with the mark type at the first_ge iterator position
84  first_ge->add_to_type(mark.mark_type());
85  return;
86  }
87  // possibly check equivalence with previous mark
88  if (first_ge != marks_.begin()) {
89  vector<TimeMark>::iterator previous = first_ge;
90  --previous;
91  if (fabs(previous->time() - mark.time()) < 2*numeric_limits<double>::epsilon()) {
92  previous->add_to_type(mark.mark_type());
93  return;
94  }
95  }
96 
97  marks_.insert(first_ge, mark);
98 }
99 
100 void TimeMarks::add_time_marks(double time, double dt, double end_time, TimeMark::Type type) {
101  ASSERT(end_time != TimeGovernor::inf_time, "Can not add time marks on infinite interval.\n");
102  ASSERT(dt > numeric_limits<double>::epsilon(), "TimeMark's step less then machine precision.\n");
103  for (double t = time; t <= end_time*1.001; t += dt) {
104  auto mark = TimeMark(t, type);
105  add(mark);
106  }
107 }
108 
109 bool TimeMarks::is_current(const TimeGovernor &tg, const TimeMark::Type &mask) const
110 {
111  if (tg.t() == TimeGovernor::inf_time) return tg.is_end();
112  const TimeMark &tm = *last(tg, mask);
113  return tg.lt(tm.time() + tg.dt()); // last_t + dt < mark_t + dt
114 }
115 
117 {
118  vector<TimeMark>::const_iterator first_ge = std::lower_bound(marks_.begin(), marks_.end(), TimeMark(tg.t(),mask));
119  while ( ! tg.lt(first_ge->time()) || ! first_ge->match_mask(mask) ) {
120  ++first_ge;
121  }
122  return TimeMarksIterator(marks_, first_ge, mask);
123 }
124 
126 {
127  vector<TimeMark>::const_iterator first_ge = std::lower_bound(marks_.begin(), marks_.end(), TimeMark(tg.t()+0.01*tg.dt(),mask));
128  while ( ! tg.ge(first_ge->time()) || ! first_ge->match_mask(mask) ) {
129  --first_ge;
130  }
131  // cout << "TimeMark::last(): " << *first_ge << endl;
132  return TimeMarksIterator(marks_, first_ge, mask);
133 }
134 
135 
136 
138 {
139  auto it = TimeMarksIterator(marks_, --marks_.end(), mask); // +INF time mark
140  --it;
141  return it;
142 }
143 
144 
145 
147 {
148  return TimeMarksIterator(marks_, marks_.begin(), mask);
149 }
150 
151 
152 
154 {
155  return TimeMarksIterator(marks_, --marks_.end(), mask);
156 }
157 
158 
159 
160 ostream& operator<<(ostream& stream, const TimeMarks &marks)
161 {
162  stream << "time marks:" << endl;
163  for(vector<TimeMark>::const_iterator it = marks.marks_.begin(); it != marks.marks_.end(); ++it)
164  stream << *it << endl;
165  return stream;
166 }
void add_time_marks(double time, double dt, double end_time, TimeMark::Type type)
Definition: time_marks.cc:100
Type mark_type() const
Getter for mark type.
Definition: time_marks.hh:79
unsigned long int Type
Definition: time_marks.hh:59
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
bool is_end() const
Returns true if the actual time is greater than or equal to the end time.
ostream & operator<<(ostream &stream, const TimeMark &mark)
Definition: time_marks.cc:41
bool ge(double other_time) const
TimeMark::Type type_input_
Predefined type for change of boundary condition.
Definition: time_marks.hh:272
double t() const
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
Basic time management class.
static const Type every_type
Mask that matches every type of TimeMark.
Definition: time_marks.hh:62
void add(const TimeMark &mark)
Definition: time_marks.cc:77
#define ASSERT(...)
Definition: global_defs.h:121
bool is_current(const TimeGovernor &tg, const TimeMark::Type &mask) const
Definition: time_marks.cc:109
bool lt(double other_time) const
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
const double epsilon
Definition: mathfce.h:6
vector< TimeMark > marks_
TimeMarks list sorted according to the their time.
Definition: time_marks.hh:265
double dt() const
double time() const
Getter for the time of the TimeMark.
Definition: time_marks.hh:84
Class used for marking specified times at which some events occur.
Definition: time_marks.hh:44
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
static const double inf_time
Infinity time used for steady case.
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