deal.II version GIT relicensing-2169-gec1b43f35b 2024-11-22 07:10:00+00:00
\(\newcommand{\dealvcentcolon}{\mathrel{\mathop{:}}}\) \(\newcommand{\dealcoloneq}{\dealvcentcolon\mathrel{\mkern-1.2mu}=}\) \(\newcommand{\jump}[1]{\left[\!\left[ #1 \right]\!\right]}\) \(\newcommand{\average}[1]{\left\{\!\left\{ #1 \right\}\!\right\}}\)
Loading...
Searching...
No Matches
event.h
Go to the documentation of this file.
1// ------------------------------------------------------------------------
2//
3// SPDX-License-Identifier: LGPL-2.1-or-later
4// Copyright (C) 2010 - 2019 by the deal.II authors
5//
6// This file is part of the deal.II library.
7//
8// Part of the source code is dual licensed under Apache-2.0 WITH
9// LLVM-exception OR LGPL-2.1-or-later. Detailed license information
10// governing the source code and code contributions can be found in
11// LICENSE.md and CONTRIBUTING.md at the top level directory of deal.II.
12//
13// ------------------------------------------------------------------------
14
15
16#ifndef dealii_event_h
17#define dealii_event_h
18
19#include <deal.II/base/config.h>
20
21#include <algorithm>
22#include <iostream>
23#include <string>
24#include <vector>
25
27
28namespace Algorithms
29{
48 class Event
49 {
50 public:
56 static Event
57 assign(const std::string &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:
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
177 {
178 if (all_true)
179 return true;
180 return std::find(flags.begin(), flags.end(), true) != flags.end();
181 }
182
183
184 inline bool
185 Event::test(const Event &event) const
186 {
187 // First, test all_true in this
188 if (all_true)
189 return true;
190
191 const unsigned int n = flags.size();
192 const unsigned int m = event.flags.size();
193 const unsigned int n_min = (n < m) ? n : m;
194
195 // Now, if all_true set in the
196 // other, then all must be true
197 // in this
198 if (event.all_true)
199 {
200 // Non existing flags are
201 // always assumed false
202 if (m > n)
203 return false;
204
205 // Test all flags separately
206 // and return false if one is
207 // not set
208 return std::find(flags.begin(), flags.end(), false) == flags.end();
209 }
210
211 // Finally, compare each flag
212 // separately
213 for (unsigned int i = 0; i < n_min; ++i)
214 if (event.flags[i] && !flags[i])
215 return false;
216 for (unsigned int i = n_min; i < m; ++i)
217 if (event.flags[i])
218 return false;
219 return true;
220 }
221
222
223
224 inline Event &
226 {
227 all_true |= event.all_true;
228 if (all_true)
229 return *this;
230
231 if (flags.size() < event.flags.size())
232 flags.resize(event.flags.size());
233 for (unsigned int i = 0; i < event.flags.size(); ++i)
234 flags[i] = flags[i] || event.flags[i];
235
236 return *this;
237 }
238
239
240 inline Event &
242 {
243 if (!event.any())
244 return *this;
245
246 all_true = false;
247 if (event.all_true)
248 {
249 std::fill(flags.begin(), flags.end(), false);
250 return *this;
251 }
252
253 if (flags.size() < event.flags.size())
254 flags.resize(event.flags.size());
255 for (unsigned int i = 0; i < event.flags.size(); ++i)
256 if (event.flags[i])
257 flags[i] = false;
258
259 return *this;
260 }
261
262
263 template <class OS>
264 inline void
265 Event::print(OS &os) const
266 {
267 if (all_true)
268 os << " ALL";
269
270 for (unsigned int i = 0; i < flags.size(); ++i)
271 if (flags[i])
272 os << ' ' << names[i];
273 }
274
275
276 template <class OS>
277 inline 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 &
292 operator<<(OS &o, const Event &e)
293 {
294 e.print(o);
295 return o;
296 }
297} // namespace Algorithms
298
300
301#endif
bool test(const Event &event) const
Definition event.h:185
bool any() const
Definition event.h:176
static std::vector< std::string > names
Definition event.h:137
void print(OS &os) const
Definition event.h:265
Event & operator+=(const Event &event)
Definition event.h:225
std::vector< bool > flags
Definition event.h:131
Event & operator-=(const Event &event)
Definition event.h:241
static Event assign(const std::string &name)
Definition event.cc:27
static void print_assigned(OS &os)
Definition event.h:278
#define DEAL_II_NAMESPACE_OPEN
Definition config.h:498
#define DEAL_II_NAMESPACE_CLOSE
Definition config.h:499
const Event initial
Definition event.cc:64
const Event new_time
Definition event.cc:67
const Event new_timestep_size
Definition event.cc:68
const Event remesh
Definition event.cc:65
const Event bad_derivative
Definition event.cc:66
OutputOperator< VectorType > & operator<<(OutputOperator< VectorType > &out, unsigned int step)
Definition operator.h:164