Flow123d  last_with_con_2.0.0-4-g42e6930
time_marks.cc
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.cc
15  * @brief
16  * @author Jan Brezina
17  */
18 
19 #include <algorithm>
20 #include <limits>
21 #include "system/system.hh"
22 #include "system/global_defs.h"
23 #include "time_governor.hh"
24 #include "time_marks.hh"
25 #include <boost/functional/hash.hpp>
26 
27 // ------------------------------------------------------
28 // implementation of members of class TimeMark
29 // ------------------------------------------------------
30 
31 ostream& operator<<(ostream& stream, const TimeMark &mark)
32 {
33  //return ( stream << mark.time()<<": 0o" << oct << mark.mark_type() << dec ); //octal output
34  return ( stream << mark.time()<<": 0x" << hex << mark.mark_type() << dec );
35 }
36 
39 
40 
41 // ------------------------------------------------------
42 // implementation of members of class TimeMarks
43 // ------------------------------------------------------
44 
46 {
47  this->reinit();
48 }
49 
50 
51 
53 {
54  marks_.clear();
55  next_mark_type_ = 0x1;
56 
57  // 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 
69  OLD_ASSERT(next_mark_type_ != 0, "Can not allocate new mark type. The limit is 32 mark types.\n");
70  TimeMark::Type current_type = next_mark_type_;
71 
72  next_mark_type_ <<= 1;
73  return current_type;
74 }
75 
77  // find first mark with time greater or equal to the new mark
78  vector<TimeMark>::iterator first_ge = std::lower_bound(marks_.begin(), marks_.end(), mark);
79 
80  // check equivalence with found mark
81  if (fabs(first_ge->time() - mark.time()) < TimeGovernor::time_step_precision) {
82  //if times are "equal" do bitwise OR with the mark type at the first_ge iterator position
83  first_ge->add_to_type(mark.mark_type());
84  return *first_ge;
85  }
86  // possibly check equivalence with previous mark
87  if (first_ge != marks_.begin()) {
88  vector<TimeMark>::iterator previous = first_ge;
89  --previous;
90  if (fabs(previous->time() - mark.time()) < TimeGovernor::time_step_precision) {
91  previous->add_to_type(mark.mark_type());
92  return *previous;
93  }
94  }
95 
96  marks_.insert(first_ge, mark);
97  return mark;
98 }
99 
100 void TimeMarks::add_time_marks(double time, double dt, double end_time, TimeMark::Type type) {
101  OLD_ASSERT(end_time != TimeGovernor::inf_time, "Can not add time marks on infinite interval.\n");
102  OLD_ASSERT(dt > numeric_limits<double>::epsilon(), "TimeMark's step less then machine precision.\n");
103 
104  unsigned int n_steps=((end_time-time)/dt + TimeGovernor::time_step_precision);
105  for (unsigned int i = 0; i<=n_steps;i++) {
106  auto mark = TimeMark(time+i*dt, type);
107  add(mark);
108  }
109 }
110 
111 
113  for(auto &mark : marks_)
114  if (mark.match_mask(filter_type)) mark.add_to_type(add_type);
115 
116 }
117 
118 /*
119 bool TimeMarks::is_current(const TimeStep &time_step, const TimeMark::Type &mask) const
120 {
121  return ( current(time_step, mask) != this->end(mask) );
122 
123  if (tg.t() == TimeGovernor::inf_time) return tg.is_end();
124  const TimeMark &tm = *last(tg, mask);
125 
126  return tg.step().lt(tm.time() + tg.dt()); // last_t + dt < mark_t + dt
127 
128 }*/
129 
130 
131 TimeMarks::iterator TimeMarks::current(const TimeStep &time_step, const TimeMark::Type &mask) const
132 {
133  //if (time_step.end() == TimeGovernor::inf_time) return tg.is_end();
134  auto it = last(time_step, mask);
135  if ( time_step.contains(it->time()) ) return it;
136  else return this->end(mask);
137 }
138 
140 {
141  // first time mark which does not compare less then then actual tg time
143  std::lower_bound(marks_.begin(), marks_.end(), TimeMark(tg.t(),mask));
144  while ( ! tg.step().lt(first_ge->time()) || ! first_ge->match_mask(mask) ) {
145  ++first_ge;
146  }
147  return TimeMarksIterator(marks_, first_ge, mask);
148 }
149 
150 TimeMarks::iterator TimeMarks::last(const TimeStep &time_step, const TimeMark::Type &mask) const
151 {
152  // first time mark which does compare strictly greater then actual tg time
154  std::lower_bound(marks_.begin(), marks_.end(), TimeMark(time_step.end()+0.01*time_step.length(),mask));
155  while ( ! time_step.ge(first_ge->time()) || ! first_ge->match_mask(mask) ) {
156  --first_ge;
157  }
158  return TimeMarksIterator(marks_, first_ge, mask);
159 }
160 
162 {
163  return last(tg.step(), mask);
164 }
165 
166 
168 {
169  auto it = TimeMarksIterator(marks_, --marks_.end(), mask); // +INF time mark
170  --it;
171  return it;
172 }
173 
174 
175 
177 {
178  return TimeMarksIterator(marks_, marks_.begin(), mask);
179 }
180 
181 
182 
184 {
185  return TimeMarksIterator(marks_, --marks_.end(), mask);
186 }
187 
188 
189 
190 ostream& operator<<(ostream& stream, const TimeMarks &marks)
191 {
192  stream << "time marks:" << endl;
193  for(vector<TimeMark>::const_iterator it = marks.marks_.begin(); it != marks.marks_.end(); ++it)
194  stream << *it << endl;
195  return stream;
196 }
197 
198 
199 std::size_t TimeMarkHash::operator()(TimeMark const& mark) const
200 {
201  std::size_t seed = 0;
202  boost::hash_combine(seed, mark.time());
203  boost::hash_combine(seed, mark.mark_type());
204  return seed;
205 }
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:71
unsigned long int Type
Definition: time_marks.hh:51
TimeMarks::iterator next(const TimeGovernor &tg, const TimeMark::Type &mask) const
Definition: time_marks.cc:139
Iterator over TimeMark objects in TimeMarks object (database of TimeMark objects).
Definition: time_marks.hh:302
TimeMarks::iterator begin(TimeMark::Type mask=TimeMark::every_type) const
Iterator for the begin mimics container-like of TimeMarks.
Definition: time_marks.cc:176
bool lt(double other_time) const
IntFormatSpec< int, TypeSpec<'x'> > hex(int value)
bool contains(double other_time) const
ostream & operator<<(ostream &stream, const TimeMark &mark)
Definition: time_marks.cc:31
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
void reinit()
Definition: time_marks.cc:52
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:56
Basic time management class.
TimeMarks::iterator last(const TimeStep &time_step, const TimeMark::Type &mask) const
Definition: time_marks.cc:150
static const Type every_type
Mark Type with all bits set.
Definition: time_marks.hh:54
#define OLD_ASSERT(...)
Definition: global_defs.h:131
Global macros to enhance readability and debugging, general constants.
std::vector< TimeMark > marks_
TimeMarks list sorted according to the their time.
Definition: time_marks.hh:282
bool ge(double other_time) const
static const double time_step_precision
friend std::ostream & operator<<(std::ostream &stream, const TimeMarks &marks)
Friend output operator.
TimeMark::Type new_mark_type()
Definition: time_marks.cc:68
double length() const
std::size_t operator()(TimeMark const &mark) const
Definition: time_marks.cc:199
double end() const
This class is a collection of time marks to manage various events occurring during simulation time...
Definition: time_marks.hh:163
TimeMark add(const TimeMark &mark)
Definition: time_marks.cc:76
TimeMark::Type next_mark_type_
MarkType that will be used at next new_time_mark() call.
Definition: time_marks.hh:279
const double epsilon
Definition: mathfce.h:23
void add_to_type_all(TimeMark::Type filter_type, TimeMark::Type add_type)
Definition: time_marks.cc:112
double time() const
Getter for the time of the TimeMark.
Definition: time_marks.hh:76
TimeMarks::iterator current(const TimeStep &time_step, const TimeMark::Type &mask) const
Definition: time_marks.cc:131
Class used for marking specified times at which some events occur.
Definition: time_marks.hh:36
TimeMarks::iterator end(TimeMark::Type mask=TimeMark::every_type) const
Iterator for the end mimics container-like of TimeMarks.
Definition: time_marks.cc:183
TimeMark::Type type_fixed_time_
Predefined type for fixed time.
Definition: time_marks.hh:285
static const double inf_time
Infinity time used for steady case.
Representation of one time step..
TimeMark::Type type_output_
Predefined type for output.
Definition: time_marks.hh:287