Reference documentation for deal.II version 8.5.1
event.h
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 2010 - 2015 by the deal.II authors
4 //
5 // This file is part of the deal.II library.
6 //
7 // The deal.II library is free software; you can use it, redistribute
8 // it, and/or modify it under the terms of the GNU Lesser General
9 // Public License as published by the Free Software Foundation; either
10 // version 2.1 of the License, or (at your option) any later version.
11 // The full text of the license can be found in the file LICENSE at
12 // the top level of the deal.II distribution.
13 //
14 // ---------------------------------------------------------------------
15 
16 
17 #ifndef dealii__event_h
18 #define dealii__event_h
19 
20 #include <deal.II/base/config.h>
21 
22 #include <vector>
23 #include <string>
24 #include <iostream>
25 
26 DEAL_II_NAMESPACE_OPEN
27 
28 namespace Algorithms
29 {
48  class Event
49  {
50  public:
56  static Event assign (const char *name);
57 
62 // static Event find(const std::string& name);
63 
67  Event ();
68 
72  void clear();
73 
77  void all();
78 
82  Event &operator += (const Event &event);
83 
87  Event &operator -= (const Event &event);
88 
93  bool test (const Event &event) const;
94 
98  bool any () const;
99 
103  template <class OS>
104  void print (OS &os) const;
105 
109  template <class OS>
110  static void print_assigned (OS &os);
111 
112  private:
117  bool all_true;
118 
122  std::vector<bool> flags;
123 
127 //TODO: This static field must be guarded by a mutex to be thread-safe!
128  static std::vector<std::string> names;
129  };
130 
134  namespace Events
135  {
139  extern const Event initial;
140 
144  extern const Event remesh;
145 
149  extern const Event bad_derivative;
150 
154  extern const Event new_time;
155 
159  extern const Event new_timestep_size;
160  }
161 
162 
163 //----------------------------------------------------------------------//
164 
165 
166  inline
167  bool
168  Event::any () const
169  {
170  if (all_true) return true;
171  for (std::vector<bool>::const_iterator i=flags.begin();
172  i != flags.end(); ++i)
173  if (*i) return true;
174  return false;
175  }
176 
177 
178  inline
179  bool
180  Event::test (const Event &event) const
181  {
182 
183  // First, test all_true in this
184  if (all_true) return true;
185 
186  const unsigned int n = flags.size();
187  const unsigned int m = event.flags.size();
188  const unsigned int n_min = (n<m)?n:m;
189 
190  // Now, if all_true set in the
191  // other, then all must be true
192  // in this
193  if (event.all_true)
194  {
195  // Non existing flags are
196  // always assumed false
197  if (m > n)
198  return false;
199 
200  // Test all flags separately
201  // and return false if one is
202  // not set
203  for (std::vector<bool>::const_iterator i=flags.begin();
204  i != flags.end(); ++i)
205  if (!*i) return false;
206  // All flags are set
207  return true;
208  }
209 
210  // Finally, compare each flag
211  // separately
212  for (unsigned int i=0; i<n_min; ++i)
213  if (event.flags[i] && !flags[i])
214  return false;
215  for (unsigned int i=n_min; i<m; ++i)
216  if (event.flags[i])
217  return false;
218  return true;
219  }
220 
221 
222 
223  inline
225  {
226  all_true |= event.all_true;
227  if (all_true) return *this;
228 
229  if (flags.size() < event.flags.size())
230  flags.resize(event.flags.size());
231  for (unsigned int i=0; i<event.flags.size(); ++i)
232  flags[i] = flags[i] || event.flags[i];
233 
234  return *this;
235  }
236 
237 
238  inline
240  {
241  if (!event.any()) return *this;
242 
243  all_true = false;
244  if (event.all_true)
245  {
246  for (std::vector<bool>::iterator i=flags.begin();
247  i != flags.end(); ++i)
248  *i = false;
249  return *this;
250  }
251 
252  if (flags.size() < event.flags.size())
253  flags.resize(event.flags.size());
254  for (unsigned int i=0; i<event.flags.size(); ++i)
255  if (event.flags[i]) flags[i] = false;
256 
257  return *this;
258  }
259 
260 
261  template <class OS>
262  inline
263  void
264  Event::print (OS &os) const
265  {
266  if (all_true)
267  os << " ALL";
268 
269  for (unsigned int i=0; i<flags.size(); ++i)
270  if (flags[i])
271  os << ' ' << names[i];
272  }
273 
274 
275  template <class OS>
276  inline
277  void
279  {
280  for (unsigned int i=0; i<names.size(); ++i)
281  os << i << '\t' << names[i] << std::endl;
282  }
283 
284 
290  template <class OS>
291  OS &operator << (OS &o, const Event &e)
292  {
293  e.print(o);
294  return o;
295  }
296 }
297 
298 DEAL_II_NAMESPACE_CLOSE
299 
300 #endif
static void print_assigned(OS &os)
Definition: event.h:278
Event & operator-=(const Event &event)
Definition: event.h:239
const Event remesh
Definition: event.cc:67
bool test(const Event &event) const
Definition: event.h:180
std::vector< bool > flags
Definition: event.h:122
void print(OS &os) const
Definition: event.h:264
const Event bad_derivative
Definition: event.cc:68
Event & operator+=(const Event &event)
Definition: event.h:224
bool any() const
Definition: event.h:168
static Event assign(const char *name)
Definition: event.cc:28
const Event new_timestep_size
Definition: event.cc:70
void all()
Definition: event.cc:59
const Event initial
Definition: event.cc:66
void clear()
Definition: event.cc:51
const Event new_time
Definition: event.cc:69
static std::vector< std::string > names
Definition: event.h:128
StreamType & operator<<(StreamType &s, UpdateFlags u)