Flow123d  jenkins-Flow123d-windows-release-multijob-285
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 
32 
33 #include <algorithm>
34 #include <limits>
35 #include "system/system.hh"
36 #include "system/global_defs.h"
37 #include "time_governor.hh"
38 #include "time_marks.hh"
39 
40 // ------------------------------------------------------
41 // implementation of members of class TimeMark
42 // ------------------------------------------------------
43 
44 ostream& operator<<(ostream& stream, const TimeMark &mark)
45 {
46  //return ( stream << mark.time()<<": 0o" << oct << mark.mark_type() << dec ); //octal output
47  return ( stream << mark.time()<<": 0x" << hex << mark.mark_type() << dec );
48 }
49 
52 
53 
54 // ------------------------------------------------------
55 // implementation of members of class TimeMarks
56 // ------------------------------------------------------
57 
59 {
60  this->reinit();
61 }
62 
63 
64 
66 {
67  marks_.clear();
68  next_mark_type_ = 0x1;
69 
70  // add predefined base mark types
74 
75  // insert start and end stoppers
76  marks_.push_back(TimeMark(-INFINITY, TimeMark::every_type));
77  marks_.push_back(TimeMark(+INFINITY, TimeMark::every_type));
78 }
79 
81  ASSERT(next_mark_type_ != 0, "Can not allocate new mark type. The limit is 32 mark types.\n");
82  TimeMark::Type current_type = next_mark_type_;
83 
84  next_mark_type_ <<= 1;
85  return current_type;
86 }
87 
88 void TimeMarks::add(const TimeMark &mark) {
89  // find first mark with time greater or equal to the new mark
90  vector<TimeMark>::iterator first_ge = std::lower_bound(marks_.begin(), marks_.end(), mark);
91 
92  // check equivalence with found mark
93  if (fabs(first_ge->time() - mark.time()) < TimeGovernor::time_step_precision) {
94  //if "equal" does bitwise OR with the mark type at the first_ge iterator position
95  first_ge->add_to_type(mark.mark_type());
96  return;
97  }
98  // possibly check equivalence with previous mark
99  if (first_ge != marks_.begin()) {
100  vector<TimeMark>::iterator previous = first_ge;
101  --previous;
102  if (fabs(previous->time() - mark.time()) < TimeGovernor::time_step_precision) {
103  previous->add_to_type(mark.mark_type());
104  return;
105  }
106  }
107 
108  marks_.insert(first_ge, mark);
109 }
110 
111 void TimeMarks::add_time_marks(double time, double dt, double end_time, TimeMark::Type type) {
112  ASSERT(end_time != TimeGovernor::inf_time, "Can not add time marks on infinite interval.\n");
113  ASSERT(dt > numeric_limits<double>::epsilon(), "TimeMark's step less then machine precision.\n");
114 
115  unsigned int n_steps=((end_time-time)/dt + TimeGovernor::time_step_precision);
116  for (unsigned int i = 0; i<=n_steps;i++) {
117  auto mark = TimeMark(time+i*dt, type);
118  add(mark);
119  }
120 }
121 
122 bool TimeMarks::is_current(const TimeGovernor &tg, const TimeMark::Type &mask) const
123 {
124  if (tg.t() == TimeGovernor::inf_time) return tg.is_end();
125  const TimeMark &tm = *last(tg, mask);
126 
127  return tg.step().lt(tm.time() + tg.dt()); // last_t + dt < mark_t + dt
128 }
129 
131 {
132  // first time mark which does not compare less then then actual tg time
134  std::lower_bound(marks_.begin(), marks_.end(), TimeMark(tg.t(),mask));
135  while ( ! tg.step().lt(first_ge->time()) || ! first_ge->match_mask(mask) ) {
136  ++first_ge;
137  }
138  return TimeMarksIterator(marks_, first_ge, mask);
139 }
140 
142 {
143  // first time mark which does compare strictly greater then actual tg time
145  std::lower_bound(marks_.begin(), marks_.end(), TimeMark(tg.t()+0.01*tg.dt(),mask));
146  while ( ! tg.step().ge(first_ge->time()) || ! first_ge->match_mask(mask) ) {
147  --first_ge;
148  }
149  return TimeMarksIterator(marks_, first_ge, mask);
150 }
151 
152 
153 
155 {
156  auto it = TimeMarksIterator(marks_, --marks_.end(), mask); // +INF time mark
157  --it;
158  return it;
159 }
160 
161 
162 
164 {
165  return TimeMarksIterator(marks_, marks_.begin(), mask);
166 }
167 
168 
169 
171 {
172  return TimeMarksIterator(marks_, --marks_.end(), mask);
173 }
174 
175 
176 
177 ostream& operator<<(ostream& stream, const TimeMarks &marks)
178 {
179  stream << "time marks:" << endl;
180  for(vector<TimeMark>::const_iterator it = marks.marks_.begin(); it != marks.marks_.end(); ++it)
181  stream << *it << endl;
182  return stream;
183 }
void add_time_marks(double time, double dt, double end_time, TimeMark::Type type)
Definition: time_marks.cc:111
Type mark_type() const
Getter for mark type.
Definition: time_marks.hh:84
unsigned long int Type
Definition: time_marks.hh:64
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
bool lt(double other_time) const
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:44
TimeMark::Type type_input_
Predefined type for change of boundary condition.
Definition: time_marks.hh:283
void reinit()
Definition: time_marks.cc:65
const TimeStep & step(int index=-1) const
double t() const
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
Basic time management class.
static const Type every_type
Mark Type with all bits set.
Definition: time_marks.hh:67
void add(const TimeMark &mark)
Definition: time_marks.cc:88
Global macros to enhance readability and debugging, general constants.
#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
bool ge(double other_time) const
static const double time_step_precision
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
TimeMark::Type next_mark_type_
MarkType that will be used at next new_time_mark() call.
Definition: time_marks.hh:273
const double epsilon
Definition: mathfce.h:6
double dt() const
double time() const
Getter for the time of the TimeMark.
Definition: time_marks.hh:89
Class used for marking specified times at which some events occur.
Definition: time_marks.hh:49
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
static const double inf_time
Infinity time used for steady case.
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