Global.h 14.2 KB
Newer Older
1
2
3
4
// ============================================================================
// ==                                                                        ==
// == AMDiS - Adaptive multidimensional simulations                          ==
// ==                                                                        ==
5
// ==  http://www.amdis-fem.org                                              ==
6
7
// ==                                                                        ==
// ============================================================================
8
9
10
11
12
13
14
15
16
17
18
19
//
// Software License for AMDiS
//
// Copyright (c) 2010 Dresden University of Technology 
// All rights reserved.
// Authors: Simon Vey, Thomas Witkowski et al.
//
// This file is part of AMDiS
//
// See also license.opensource.txt in the distribution.


20
21
22
23
24
25
26
27
28
29
30
31
32
33

/** \file Global.h */

/** \mainpage AMDiS
 * @{ <img src="vis.png"> @}
 */

/** \defgroup Common Common
 */

#ifndef AMDIS_GLOBAL_H
#define AMDIS_GLOBAL_H

#if (__GNUC__) && (__GNUC__ > 2) 
34
#define OPENMODE std::ios::openmode
35
#else
36
#define OPENMODE std::ios::open_mode
37
38
39
#endif

/** \brief current AMDiS version */
40
41
42
#ifndef AMDIS_VERSION
#define AMDIS_VERSION  "AMDiS: Version 0.9.1"
#endif
43
44
45

#include <string>
#include <vector>
46
#include <set>
47
48
49
50
51
52
#include <fstream>
#include <math.h>
#include <iostream>
#include <stdio.h>
#include <functional>
#include <float.h>
53
#include <time.h>
54

55
56
57
58
59
60
#ifdef _WIN32
#include <io.h>
#else
#include <unistd.h>
#endif

61
#if HAVE_PARALLEL_DOMAIN_AMDIS
62
#include <mpi.h>
63
64
#endif

65
#include <boost/algorithm/string.hpp>
Praetorius, Simon's avatar
Praetorius, Simon committed
66
#include <boost/algorithm/string/trim.hpp>
67
#include "boost/tuple/tuple.hpp"
68
#include "AMDiS_fwd.h"
Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
69
#include "OpenMP.h"
70

71
namespace AMDiS {
72

73
74
  using namespace std;

75
76
  extern const char *funcName;

77
  const int amdisRevisionNumber = 1700;
78

79
  /// Used by matrix vector multiplication
80
81
82
83
  typedef enum { NoTranspose,
		 Transpose,
		 ConjugateTranspose } MatrixTranspose;

84
  /// Speciefies the norm used by Estimator.
85
86
87
  typedef enum { NO_NORM = 0, 
		 H1_NORM = 1, 
		 L2_NORM = 2 } Norm;
88

89
  /// Datatype for degrees of freedom 
90
91
  typedef signed int DegreeOfFreedom;

92
  /// Defines type for a vector of DOF pointers.
93
  typedef vector<const DegreeOfFreedom*> DofContainer;
94

95
96
  typedef std::set<const DegreeOfFreedom*> DofContainerSet;

97
  /// Defines a type for global edge identification via its DOFs.
98
  typedef pair<DegreeOfFreedom, DegreeOfFreedom> DofEdge;
99
100
101

  /// Defines a tzpe for global face identiication via its DOFs.
  typedef boost::tuple<DegreeOfFreedom, DegreeOfFreedom, DegreeOfFreedom> DofFace;
102

103

104
  /// Returns the GeoIndex of d for dimension dim.
105
106
#define INDEX_OF_DIM(d, dim) (static_cast<GeoIndex>((d == dim) ? CENTER : d + 1))

107
  /// Returns the dimension of GeoIndex ind for dimension dim
108
109
#define DIM_OF_INDEX(ind, dim) ((static_cast<int>(ind) == 0) ? dim : static_cast<int>(ind) - 1)

Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
110
111
112
113

#if SUPPRESS_OUTPUT
#define PRINT_LINE(stream, line)
#else
114
#if HAVE_PARALLEL_DOMAIN_AMDIS
115
#define PRINT_LINE(stream, line) stream << "[" << MPI::COMM_WORLD.Get_rank() << "] " << line
116
#else
117
#define PRINT_LINE(stream, line) stream << line
Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
118
#endif
119
120
#endif

121
  /// Calculates factorial of i
122
123
  int fac(int i);

124
125
  void waitSec(int seconds);

126
  void processMemUsage(double& vm_usage, double& resident_set, bool inMegaByte = true);
127

128
  /// Content comparision of two pointers. Used e.g. for find_if
129
  template<typename T>
130
  struct comparePtrContents : public binary_function<T*, T*, bool>
131
  {
132
    /// Overrides binary_function::operator()
133
134
    bool operator()(T* a, T* b) const 
    {
135
      return (*a == *b);
136
    }
137
138
  };

Praetorius, Simon's avatar
Praetorius, Simon committed
139
140
141
  /// check for file existence
  inline bool file_exists(const std::string filename)
  {
142
143
144
#ifdef _WIN32
    return _access(filename.c_str(), 0) == 0;
#else
Praetorius, Simon's avatar
Praetorius, Simon committed
145
    return access(filename.c_str(), F_OK) == 0;
146
#endif
Praetorius, Simon's avatar
Praetorius, Simon committed
147
148
149
150
151
152
153
154
155
156
  };

  /// trim std::string
  inline std::string trim(const std::string& oldStr)
  {
    std::string swap(oldStr);
    boost::algorithm::trim(swap);
    return swap;
  };

157
158
159
160
161
162
163
164
165
166
167
168
  // ===== some simple template functions ====================================== 

  template<typename T> inline T abs(T a) 
  {
    return  ((a) >= 0 ? (a) : -(a));
  }

  template<typename T> inline T sqr(T a) 
  {
    return  ((a)*(a));
  }

169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
  template<typename T> inline void nullify(T &a)
  {
    a = 0.0;
  }

  template<typename T> inline void nullify(std::vector<T> &a)
  {
    typename std::vector<T>::iterator it;
    for (it = a.begin(); it != a.end(); it++)
      nullify(*it);
  }

  template<typename T> inline void nullify(mtl::dense_vector<T> &a)
  {
    T null;
    nullify(null);
    a = null;
  }

  template<typename T> inline void nullify(WorldVector<T> &a)
  {
    T null;
    nullify(null);
    a = null;
  }

195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
  // ===== some predefined values ===============================================
  const double m_e = 		2.7182818284590452354;
  const double m_log2e = 		1.4426950408889634074;
  const double m_log10e = 	0.43429448190325182765;
  const double m_ln2 = 		0.69314718055994530942;
  const double m_ln10 = 		2.30258509299404568402;
  const double m_pi = 		3.14159265358979323846;
  const double m_pi_2 = 		1.57079632679489661923;
  const double m_pi_4 = 		0.78539816339744830962;
  const double m_1_pi = 		0.31830988618379067154;
  const double m_2_pi = 		0.63661977236758134308;
  const double m_2_sqrtpi = 	1.12837916709551257390;
  const double m_sqrt2 = 		1.41421356237309504880;
  const double m_sqrt1_2 = 	0.70710678118654752440;

  // ===== tolerance for floating point comparison ==============================
#define DBL_TOL DBL_EPSILON
#define FLT_TOL FLT_EPSILON


  /** \brief
   * Manages the output of messages, warnings, errors, ...
   * Used by the macros FUNCNAME, ERROR, ERROR_EXIT, WARNING, TEST, MSG, INFO,
   * PRINT_INFO, WAIT, WAIT_REALLY.
   * Don't use this class directly but only via these macros!
   */
  class Msg
  {
  public:
224
    /// Prints a formated message to the message stream
225
226
    static void print(const char *format, ...);

227
    /// Prints a formated message to the error stream
228
229
    static void print_error(const char *format, ...);

230
    /// Prints a formated message to the error stream and exits 
231
232
    static void print_error_exit(const char *format, ...);

233
234
    ///
    static void catch_error_exit(const char *format, ...) {}
235

236
    /// Prints an error message with funcname, file, and line to the error stream
237
238
239
240
    static void print_error_funcname(const char *funcname,
				     const char *file, 
				     int line);

241
    /// Prints a warning to the message stream
242
243
    static void print_warn(const char *format, ...);

244
    /// Prints a warning with funcname, file, and line to the message stream
245
246
247
248
    static void print_warn_funcname(const char *funcname,
				    const char *file, 
				    int line);

249
    /// Prints the funcname to the message stream
250
251
    static void print_funcname(const char *funcname);

252
    /// Changes the message stream
253
    static void change_out(ostream*);
254

255
    /// Changes the error stream 
256
    static void change_error_out(ofstream *fp);
257

258
    /// Creates a filestream and sets the error stream to this filestream
259
260
    static void open_error_file(const char *filename, OPENMODE);

261
    /// Sets \ref msgInfo
262
263
264
265
    static void setMsgInfo(int info) 
    { 
      msgInfo = info; 
    }
266

267
    /// Returns \ref msgInfo
268
269
270
271
    static int  getMsgInfo() 
    { 
      return msgInfo; 
    }
272

273
    /// Sets \ref msgWait
274
275
276
277
    static void setMsgWait(bool wait) 
    { 
      msgWait = wait; 
    }
278

279
    /// Returns \ref msgWait
280
281
282
283
    static bool getMsgWait() 
    { 
      return msgWait; 
    }
284

285
    /// Waits for enter if w is true
286
287
    static void wait(bool w);

288
    /// Returns \ref out
289
    static ostream *getOutStream() 
290
291
292
    { 
      return out; 
    }
293

294
    /// Returns \ref error
295
    static ostream *getErrorStream() 
296
297
298
    { 
      return error; 
    }
299

300
301
302
303
304
305
306
307
  public:
#if HAVE_PARALLEL_DOMAIN_AMDIS
    /// In parallel computations, when this variable is true, only the 0 rank will
    /// print messages to the output stream. Error messages and warnings are always
    /// printed from all ranks.
    static bool outputMainRank;
#endif

308
  protected:
309
    /// Message stram
310
    static ostream *out;
311

312
    /// Error stream
313
    static ostream *error;
314

Thomas Witkowski's avatar
bla  
Thomas Witkowski committed
315
316
    /// Remember funcName to avoid multiple output of funcName within the same
    /// function call
Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
317
    static ThreadPrivate<const char*> oldFuncName;
318

319
    /// Global info level
320
321
    static int msgInfo;

322
    /// Spezifies whether to wait when WAIT is called
323
324
325
    static bool msgWait;
  };

Thomas Witkowski's avatar
bla  
Thomas Witkowski committed
326
327
328
  // ===========================================================================
  // ===== message macros ======================================================
  // ===========================================================================
329

Thomas Witkowski's avatar
bla  
Thomas Witkowski committed
330
331
  /// Should be the first call in every functions. It defines the current 
  /// function name nn for message output via MSG, WARNING, ...
332
333
#define FUNCNAME(nn) const char *funcName; funcName = nn;

334
  /// prints an error message 
335
336
337
#define ERROR Msg::print_error_funcname(funcName,__FILE__, __LINE__),	\
    Msg::print_error

338
  /// prints an error message and exits 
339
340
341
#define ERROR_EXIT Msg::print_error_funcname(funcName,__FILE__, __LINE__), \
    Msg::print_error_exit

342
  /// prints a warning
343
344
345
#define WARNING Msg::print_warn_funcname(funcName,__FILE__, __LINE__),	\
    Msg::print_warn

346
  /// if test is false, an error message is printed
347
348
#define TEST(test) if ((test));else ERROR

349
  /// if test is false, an error message is printed and the program exits
Thomas Witkowski's avatar
Thomas Witkowski committed
350
#define TEST_EXIT(test) if ((test));else ERROR_EXIT
351

352
  /// In debug mode, it corresponds to ERROR_EXIT, otherwise it is noop.
353
354
#if (DEBUG == 0) 
  #define TEST_EXIT_DBG(test) if (false) Msg::catch_error_exit
355
  #define DBG_VAR(var)
356
357
#else
#define TEST_EXIT_DBG(test) if ((test));else ERROR_EXIT
358
  #define DBG_VAR(var) var
359
#endif
360

361
  /// prints a message
362
363
#define MSG Msg::print_funcname(funcName), Msg::print

364
365
366
367
368
369
#if (DEBUG == 0) 
  #define MSG_DBG
#else
  #define MSG_DBG Msg::print_funcname(funcName), Msg::print
#endif

370
  /// prints a message, if min(Msg::msgInfo, info) >= noinfo
371
#define INFO(info,noinfo)						\
372
  if (Msg::getMsgInfo() && (std::min(Msg::getMsgInfo(), (info)) >= (noinfo))) MSG
373

374
  /// prints a message, if min(Msg::msgInfo, info) >= noinfo
375
#define PRINT_INFO(info,noinfo)						\
376
  if (Msg::getMsgInfo() && (std::min(Msg::getMsgInfo(), (info)) >= (noinfo))) Msg::print
377
378


Thomas Witkowski's avatar
bla  
Thomas Witkowski committed
379
380
381
  /// If the value of Msg::wait is not zero the macro will produce the message 
  /// 'wait for <enter> ...' and will continue after pressing the return or enter
  /// key. Otherwise the program continues without a message.
382
383
#define WAIT Msg::wait(Msg::getMsgWait())

Thomas Witkowski's avatar
bla  
Thomas Witkowski committed
384
385
  /// produces the message 'wait for <enter> ...' and will continue after
  /// pressing the return or enter key.
386
387
388
389
#define WAIT_REALLY Msg::wait(true)

#define TIME_USED(f,s) ((double)((s)-(f))/(double)CLOCKS_PER_SEC)

Thomas Witkowski's avatar
bla  
Thomas Witkowski committed
390
391
  /// internal used indices to represent the different geometrical objects.
  /// Used as parameter for getGeo() and as template parameter for FixVec. 
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
  typedef enum
    {
      CENTER   = 0, /**< in 1d the center is at the edge, in 2d at the face, in 3d 
		     * at the interior of an element. So a Line has no edge but
		     * only a center, a Triangle has no face but only a center.
		     */
      VERTEX   = 1, /**< index for element vertices.
		     * number of vertices is equal to number of parts and 
		     * neighbours.
		     */
      EDGE     = 2, /**< index for element edges */
      FACE     = 3, /**< index for element faces */
      DIMEN    =-1, /**< index for problem dimension */
      PARTS    =-2, /**< index for parts of an element (vertices in 1d, edges in 2d
		     * , faces in 3d). Number of element parts is equal to number
		     * of vertices and neighbours. 
		     */
      NEIGH    =-3, /**< index for neighbours of an element.
		     * Number of element neighbours is equal to number of 
		     * vertices and parts.
		     */
      WORLD    =-4, /**< index for world dimension */
      BOUNDARY =-5, /**< index for boundary nodes of an element. This could be
		     * vertices, edges or faces.
		     */
      PROJECTION=-6 /**< index for element and boundary projections */
    } GeoIndex;

#define MAXPART FACE
#define MINPART PROJECTION


  /** \ingroup Common
   * \brief
   * Static class wich holds global information about the world and all types of
   * elements.
   */
  class Global
  {
  public:
Thomas Witkowski's avatar
bla  
Thomas Witkowski committed
432
433
    /// returns a pointer to \ref referenceElement [dim]. With this pointer you
    /// can get information about the element via Element's getGeo method.
434
435
    static const Element *getReferenceElement(int dim) 
    {
436
      FUNCNAME("Global::getReferenceElement()");
437
      TEST_EXIT(dim > 0 && dim < 4)("invalid dim: %d\n", dim);
438
      return referenceElement[dim];
439
    }
440

441
    /// returns geometrical information. Currently this is only dimOfWorld.
442
443
    static inline int getGeo(GeoIndex p) 
    {
444
445
446
      if (WORLD == p) 
	return dimOfWorld; 

447
      ERROR_EXIT("Illegal request for geometry data: part = %d!\n", p);
448
      return 0;
449
    }
450

Thomas Witkowski's avatar
bla  
Thomas Witkowski committed
451
452
    /// Returns geometrical information about elements of the dimension dim.
    /// getGeo(VERTEX, 3) returns 4 because a Tetrahedron has 4 vertices.
453
454
    static inline int getGeo(GeoIndex p, int dim) 
    {
455
      TEST_EXIT_DBG(p >= MINPART && p <= MAXPART)
456
	("Calling for invalid geometry value %d\n",p);
457
      TEST_EXIT_DBG(dim >= 0 && dim < 4)
458
459
460
461
462
463
	("invalid dim: %d\n", dim);
      TEST_EXIT_DBG((dim != 0) || (p == PARTS || p == VERTEX || p == EDGE || p == FACE))
	("dim = 0\n");

      return geoIndexTable[dim][p - MINPART];
    }
464

465
    /// Inits the Global class with the help of Parameters.
466
467
    static void init();

468
469
    static void clear();

Thomas Witkowski's avatar
Thomas Witkowski committed
470
  private:
Thomas Witkowski's avatar
bla  
Thomas Witkowski committed
471
472
    /// Global is a pure static class. So the constructor is private to avoid
    /// instantiation.
Thomas Witkowski's avatar
Thomas Witkowski committed
473
    Global();
474
475

  private:
476
    /// Dimension of the simulated world
477
478
    static int dimOfWorld;

Thomas Witkowski's avatar
bla  
Thomas Witkowski committed
479
480
481
482
483
    /// contains a pointer to a Line, a Triangle, and a Tetrahedron.
    /// This allows the access to information of the concrete elements via
    /// the dimension index.
    /// referenceElement[3]->getGeo(VERTEX) gives the number of vertices of a
    /// Tetrahedron wich is 4 => no switch statement is necessary.
484
    static Element *referenceElement[4];
485

486
    /// Stores the precalculated results that should be returned by Global::getGeo.
487
    static vector<vector<int> > geoIndexTable;
488
489
490
491
492
493
  };

#define COMMA ,

  const int RescheduleErrorCode = 23;

494
495
496
497
498
499
500
501
502
  /**
   * \ingroup Assembler
   * \brief
   * Specifies the type of a FirstOrderTerm 
   */
  enum FirstOrderType {
    GRD_PSI,
    GRD_PHI
  };
503

504
505
506
507
}

#endif // AMDIS_GLOBAL_H