Reference documentation for deal.II version 8.1.0
event.h
1 // ---------------------------------------------------------------------
2 // @f$Id: event.h 30036 2013-07-18 16:55:32Z maier @f$
3 //
4 // Copyright (C) 2010 - 2013 by the deal.II authors
5 //
6 // This file is part of the deal.II library.
7 //
8 // The deal.II library is free software; you can use it, redistribute
9 // it, and/or modify it under the terms of the GNU Lesser General
10 // Public License as published by the Free Software Foundation; either
11 // version 2.1 of the License, or (at your option) any later version.
12 // The full text of the license can be found in the file LICENSE at
13 // the top level of the deal.II distribution.
14 //
15 // ---------------------------------------------------------------------
16 
17 
18 #ifndef __deal2__event_h
19 #define __deal2__event_h
20 
21 #include <deal.II/base/config.h>
22 
23 #include <vector>
24 #include <string>
25 #include <iostream>
26 
28 
29 namespace Algorithms
30 {
49  class Event
50  {
51  public:
60  static Event assign (const char *name);
61 
68 // static Event find(const std::string& name);
69 
74  Event ();
75 
79  void clear();
80 
84  void all();
85 
89  Event &operator += (const Event &event);
90 
94  Event &operator -= (const Event &event);
95 
101  bool test (const Event &event) const;
102 
107  bool any () const;
108 
112  template <class OS>
113  void print (OS &os) const;
114 
118  template <class OS>
119  static void print_assigned (OS &os);
120 
121  private:
129  bool all_true;
130 
134  std::vector<bool> flags;
135 
139 //TODO: This static field must be guarded by a mutex to be thread-safe!
140  static std::vector<std::string> names;
141  };
142 
146  namespace Events
147  {
153  extern const Event initial;
154 
160  extern const Event bad_derivative;
161 
166  extern const Event new_time;
167 
171  extern const Event new_timestep_size;
172  }
173 
174 
175 //----------------------------------------------------------------------//
176 
177 
178  inline
179  bool
180  Event::any () const
181  {
182  if (all_true) return true;
183  for (std::vector<bool>::const_iterator i=flags.begin();
184  i != flags.end(); ++i)
185  if (*i) return true;
186  return false;
187  }
188 
189 
190  inline
191  bool
192  Event::test (const Event &event) const
193  {
194 
195  // First, test all_true in this
196  if (all_true) return true;
197 
198  const unsigned int n = flags.size();
199  const unsigned int m = event.flags.size();
200  const unsigned int n_min = std::min(n, m);
201 
202  // Now, if all_true set in the
203  // other, then all must be true
204  // in this
205  if (event.all_true)
206  {
207  // Non existing flags are
208  // always assumed false
209  if (m > n)
210  return false;
211 
212  // Test all flags separately
213  // and return false if one is
214  // not set
215  for (std::vector<bool>::const_iterator i=flags.begin();
216  i != flags.end(); ++i)
217  if (!*i) return false;
218  // All flags are set
219  return true;
220  }
221 
222  // Finally, compare each flag
223  // separately
224  for (unsigned int i=0; i<n_min; ++i)
225  if (event.flags[i] && !flags[i])
226  return false;
227  for (unsigned int i=n_min; i<m; ++i)
228  if (event.flags[i])
229  return false;
230  return true;
231  }
232 
233 
234 
235  inline
237  {
238  all_true |= event.all_true;
239  if (all_true) return *this;
240 
241  if (flags.size() < event.flags.size())
242  flags.resize(event.flags.size());
243  for (unsigned int i=0; i<event.flags.size(); ++i)
244  flags[i] = flags[i] || event.flags[i];
245 
246  return *this;
247  }
248 
249 
250  inline
252  {
253  if (!event.any()) return *this;
254 
255  all_true = false;
256  if (event.all_true)
257  {
258  for (std::vector<bool>::iterator i=flags.begin();
259  i != flags.end(); ++i)
260  *i = false;
261  return *this;
262  }
263 
264  if (flags.size() < event.flags.size())
265  flags.resize(event.flags.size());
266  for (unsigned int i=0; i<event.flags.size(); ++i)
267  if (event.flags[i]) flags[i] = false;
268 
269  return *this;
270  }
271 
272 
273  template <class OS>
274  inline
275  void
276  Event::print (OS &os) const
277  {
278  if (all_true)
279  os << " ALL";
280 
281  for (unsigned int i=0; i<flags.size(); ++i)
282  if (flags[i])
283  os << ' ' << names[i];
284  }
285 
286 
287  template <class OS>
288  inline
289  void
291  {
292  for (unsigned int i=0; i<names.size(); ++i)
293  os << i << '\t' << names[i] << std::endl;
294  }
295 
296 
303  template <class OS>
304  OS &operator << (OS &o, const Event &e)
305  {
306  e.print(o);
307  return o;
308  }
309 }
310 
311 DEAL_II_NAMESPACE_CLOSE
312 
313 #endif
static Event assign(const char *name)
static void print_assigned(OS &os)
Definition: event.h:290
Event & operator-=(const Event &event)
Definition: event.h:251
std::vector< bool > flags
Definition: event.h:134
void print(OS &os) const
Definition: event.h:276
const Event bad_derivative
Event & operator+=(const Event &event)
Definition: event.h:236
const Event new_timestep_size
const Event initial
std::ostream & operator<<(std::ostream &os, const Vector< number > &v)
Definition: vector.h:1546
static std::vector< std::string > names
Definition: event.h:140
bool any() const
Definition: event.h:180
const Event new_time
bool test(const Event &event) const
Definition: event.h:192