Reference documentation for deal.II version 9.1.0-pre
event.h
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 2010 - 2018 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.md at
12 // the top level directory of deal.II.
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 <iostream>
23 #include <string>
24 #include <vector>
25 
26 DEAL_II_NAMESPACE_OPEN
27 
28 namespace Algorithms
29 {
48  class Event
49  {
50  public:
56  static Event
57  assign(const char *name);
58 
63  // static Event find(const std::string& name);
64 
68  Event();
69 
73  void
74  clear();
75 
79  void
80  all();
81 
85  Event &
86  operator+=(const Event &event);
87 
91  Event &
92  operator-=(const Event &event);
93 
98  bool
99  test(const Event &event) const;
100 
104  bool
105  any() const;
106 
110  template <class OS>
111  void
112  print(OS &os) const;
113 
117  template <class OS>
118  static void
119  print_assigned(OS &os);
120 
121  private:
126  bool all_true;
127 
131  std::vector<bool> flags;
132 
136  // TODO: This static field must be guarded by a mutex to be thread-safe!
137  static std::vector<std::string> names;
138  };
139 
143  namespace Events
144  {
148  extern const Event initial;
149 
153  extern const Event remesh;
154 
158  extern const Event bad_derivative;
159 
163  extern const Event new_time;
164 
168  extern const Event new_timestep_size;
169  } // namespace Events
170 
171 
172  //----------------------------------------------------------------------//
173 
174 
175  inline bool
176  Event::any() const
177  {
178  if (all_true)
179  return true;
180  for (std::vector<bool>::const_iterator i = flags.begin(); i != flags.end();
181  ++i)
182  if (*i)
183  return true;
184  return false;
185  }
186 
187 
188  inline bool
189  Event::test(const Event &event) const
190  {
191  // First, test all_true in this
192  if (all_true)
193  return true;
194 
195  const unsigned int n = flags.size();
196  const unsigned int m = event.flags.size();
197  const unsigned int n_min = (n < m) ? n : m;
198 
199  // Now, if all_true set in the
200  // other, then all must be true
201  // in this
202  if (event.all_true)
203  {
204  // Non existing flags are
205  // always assumed false
206  if (m > n)
207  return false;
208 
209  // Test all flags separately
210  // and return false if one is
211  // not set
212  for (std::vector<bool>::const_iterator i = flags.begin();
213  i != flags.end();
214  ++i)
215  if (!*i)
216  return false;
217  // All flags are set
218  return true;
219  }
220 
221  // Finally, compare each flag
222  // separately
223  for (unsigned int i = 0; i < n_min; ++i)
224  if (event.flags[i] && !flags[i])
225  return false;
226  for (unsigned int i = n_min; i < m; ++i)
227  if (event.flags[i])
228  return false;
229  return true;
230  }
231 
232 
233 
234  inline Event &
235  Event::operator+=(const Event &event)
236  {
237  all_true |= event.all_true;
238  if (all_true)
239  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 Event &
251  Event::operator-=(const Event &event)
252  {
253  if (!event.any())
254  return *this;
255 
256  all_true = false;
257  if (event.all_true)
258  {
259  for (std::vector<bool>::iterator i = flags.begin(); i != flags.end();
260  ++i)
261  *i = false;
262  return *this;
263  }
264 
265  if (flags.size() < event.flags.size())
266  flags.resize(event.flags.size());
267  for (unsigned int i = 0; i < event.flags.size(); ++i)
268  if (event.flags[i])
269  flags[i] = false;
270 
271  return *this;
272  }
273 
274 
275  template <class OS>
276  inline void
277  Event::print(OS &os) const
278  {
279  if (all_true)
280  os << " ALL";
281 
282  for (unsigned int i = 0; i < flags.size(); ++i)
283  if (flags[i])
284  os << ' ' << names[i];
285  }
286 
287 
288  template <class OS>
289  inline void
291  {
292  for (unsigned int i = 0; i < names.size(); ++i)
293  os << i << '\t' << names[i] << std::endl;
294  }
295 
296 
302  template <class OS>
303  OS &
304  operator<<(OS &o, const Event &e)
305  {
306  e.print(o);
307  return o;
308  }
309 } // namespace Algorithms
310 
311 DEAL_II_NAMESPACE_CLOSE
312 
313 #endif
static void print_assigned(OS &os)
Definition: event.h:290
Event & operator-=(const Event &event)
Definition: event.h:251
const Event remesh
Definition: event.cc:66
std::vector< bool > flags
Definition: event.h:131
void print(OS &os) const
Definition: event.h:277
const Event bad_derivative
Definition: event.cc:67
Event & operator+=(const Event &event)
Definition: event.h:235
static Event assign(const char *name)
Definition: event.cc:28
const Event new_timestep_size
Definition: event.cc:69
void all()
Definition: event.cc:58
OS & operator<<(OS &o, const Event &e)
Definition: event.h:304
const Event initial
Definition: event.cc:65
void clear()
Definition: event.cc:50
bool any() const
Definition: event.h:176
const Event new_time
Definition: event.cc:68
static std::vector< std::string > names
Definition: event.h:137
bool test(const Event &event) const
Definition: event.h:189