| 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 |
|
| 26 |
DEAL_II_NAMESPACE_OPEN |
| 27 |
|
| 28 |
namespace Algorithms |
| 29 |
{ |
| 30 |
/** |
| 31 |
* Objects of this kind are used to notify interior applications of changes |
| 32 |
* provoked by an outer loop. They are handed to the application through |
| 33 |
* Operator::notify() and it is up to the actual application how to handle |
| 34 |
* them. |
| 35 |
* |
| 36 |
* Event is organized as an extensible binary enumerator. Every class can |
| 37 |
* add its own events using assign(). A typical code example is |
| 38 |
* |
| 39 |
* @code |
| 40 |
* class A |
| 41 |
* { |
| 42 |
* static Event event; |
| 43 |
* }; |
| 44 |
* |
| 45 |
* Event A::event = Event::assign("Event for A"); |
| 46 |
* @endcode |
| 47 |
*/ |
| 48 |
class Event |
| 49 |
{ |
| 50 |
public: |
| 51 |
/** |
| 52 |
* This function registers a new event type and assigns a unique |
| 53 |
* identifier to it. The result of this function should be stored for |
| 54 |
* later use. |
| 55 |
*/ |
| 56 |
static Event |
| 57 |
assign(const std::string &name); |
| 58 |
|
| 59 |
/** |
| 60 |
* If you forgot to store the result of assign, here is how to retrieve it |
| 61 |
* knowing the name. |
| 62 |
*/ |
| 63 |
// static Event find(const std::string& name); |
| 64 |
|
| 65 |
/** |
| 66 |
* Constructor, generating a clear Event. |
| 67 |
*/ |
| 68 |
Event(); |
| 69 |
|
| 70 |
/** |
| 71 |
* Clear all flags |
| 72 |
*/ |
| 73 |
void |
| 74 |
clear(); |
| 75 |
|
| 76 |
/** |
| 77 |
* Set all flags |
| 78 |
*/ |
| 79 |
void |
| 80 |
all(); |
| 81 |
|
| 82 |
/** |
| 83 |
* Add the flags of the other event |
| 84 |
*/ |
| 85 |
Event & |
| 86 |
operator+=(const Event &event); |
| 87 |
|
| 88 |
/** |
| 89 |
* Clear the flags of the other event |
| 90 |
*/ |
| 91 |
Event & |
| 92 |
operator-=(const Event &event); |
| 93 |
|
| 94 |
/** |
| 95 |
* Test whether all the flags set in the other Event are also set in this |
| 96 |
* one. |
| 97 |
*/ |
| 98 |
bool |
| 99 |
test(const Event &event) const; |
| 100 |
|
| 101 |
/** |
| 102 |
* Return <tt>true</tt> if any event is set. |
| 103 |
*/ |
| 104 |
bool |
| 105 |
any() const; |
| 106 |
|
| 107 |
/** |
| 108 |
* List the flags to a stream. |
| 109 |
*/ |
| 110 |
template <class OS> |
| 111 |
void |
| 112 |
print(OS &os) const; |
| 113 |
|
| 114 |
/** |
| 115 |
* List all assigned events. |
| 116 |
*/ |
| 117 |
template <class OS> |
| 118 |
static void |
| 119 |
print_assigned(OS &os); |
| 120 |
|
| 121 |
private: |
| 122 |
/** |
| 123 |
* Sometimes, actions have to be taken by all means. Therefore, if this |
| 124 |
* value is true, test() always returns true. |
| 125 |
*/ |
| 126 |
bool all_true; |
| 127 |
|
| 128 |
/** |
| 129 |
* The actual list of events |
| 130 |
*/ |
| 131 |
std::vector<bool> flags; |
| 132 |
|
| 133 |
/** |
| 134 |
* The names of registered events |
| 135 |
*/ |
| 136 |
// TODO: This static field must be guarded by a mutex to be thread-safe! |
| 137 |
static std::vector<std::string> names; |
| 138 |
}; |
| 139 |
|
| 140 |
/** |
| 141 |
* Events used by library operators |
| 142 |
*/ |
| 143 |
namespace Events |
| 144 |
{ |
| 145 |
/** |
| 146 |
* The program has just started and everything should be new. |
| 147 |
*/ |
| 148 |
extern const Event initial; |
| 149 |
|
| 150 |
/** |
| 151 |
* The mesh has changed. |
| 152 |
*/ |
| 153 |
extern const Event remesh; |
| 154 |
|
| 155 |
/** |
| 156 |
* The current derivative leads to slow convergence of Newton's method. |
| 157 |
*/ |
| 158 |
extern const Event bad_derivative; |
| 159 |
|
| 160 |
/** |
| 161 |
* The time stepping scheme starts a new time step. |
| 162 |
*/ |
| 163 |
extern const Event new_time; |
| 164 |
|
| 165 |
/** |
| 166 |
* The time stepping scheme has changed the time step size. |
| 167 |
*/ |
| 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 |
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 & |
| 225 |
Event::operator+=(const Event &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 & |
| 241 |
Event::operator-=(const Event &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 |
| 278 |
Event::print_assigned(OS &os) |
| 279 |
{ |
| 280 |
for (unsigned int i = 0; i < names.size(); ++i) |
| 281 |
os << i << '\t' << names[i] << std::endl; |
| 282 |
} |
| 283 |
|
| 284 |
|
| 285 |
/** |
| 286 |
* Output shift operator for events. Calls Event::print(). |
| 287 |
* |
| 288 |
* @relatesalso Event |
| 289 |
*/ |
| 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 |
|
| 299 |
DEAL_II_NAMESPACE_CLOSE |
| 300 |
|
| 301 |
#endif |