Global.h 15.3 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// ============================================================================
// ==                                                                        ==
// == AMDiS - Adaptive multidimensional simulations                          ==
// ==                                                                        ==
// ============================================================================
// ==                                                                        ==
// ==  crystal growth group                                                  ==
// ==                                                                        ==
// ==  Stiftung caesar                                                       ==
// ==  Ludwig-Erhard-Allee 2                                                 ==
// ==  53175 Bonn                                                            ==
// ==  germany                                                               ==
// ==                                                                        ==
// ============================================================================
// ==                                                                        ==
// ==  http://www.caesar.de/cg/AMDiS                                         ==
// ==                                                                        ==
// ============================================================================

/** \file Global.h */

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

/** \defgroup Common Common
 */

#ifndef AMDIS_GLOBAL_H
#define AMDIS_GLOBAL_H

#if (__GNUC__) && (__GNUC__ > 2) 
33
#define OPENMODE std::ios::openmode
34
#else
35
#define OPENMODE std::ios::open_mode
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#endif

/** \brief current AMDiS version */
#define AMDIS_VERSION  "AMDiS: Version 0.100"

// ============================================================================
// ===== includes =============================================================
// ============================================================================

#include <string>
#include <vector>
#include <fstream>
#include <math.h>
#include <iostream>
#include <stdio.h>
#include <functional>
#include <float.h>

namespace AMDiS {
  // ============================================================================
  // ===== forward declarations =================================================
  // ============================================================================

  class Mesh;
  class Element;

  extern const char *funcName;

  // ============================================================================
  // ===== definitions ==========================================================
  // ============================================================================

  /** \brief used by matrix vector multiplication */
  typedef enum { NoTranspose,
		 Transpose,
		 ConjugateTranspose } MatrixTranspose;

  /** \brief speciefies the norm used by Estimator. */
  typedef enum { NO_NORM = 0, H1_NORM = 1, L2_NORM = 2 } Norm;


  /** \brief datatype for degrees of freedom */
  typedef signed int DegreeOfFreedom;


  /** \brief
   * returns the GeoIndex of d for dimension dim.
   */
#define INDEX_OF_DIM(d, dim) (static_cast<GeoIndex>((d == dim) ? CENTER : d + 1))

  /** \brief
   * returns the dimension of GeoIndex ind for dimension dim
   */
#define DIM_OF_INDEX(ind, dim) ((static_cast<int>(ind) == 0) ? dim : static_cast<int>(ind) - 1)


  /** \brief
   * Returns boundary->getBound() if boundary is not NULL. Returns INTERIOR
   * otherwise.
   */
  //#define GET_BOUND(boundary) ((boundary) ? (boundary)->getBound() : INTERIOR)

  /** \brief
   * Calculates factorial of i
   */
  int fac(int i);

  /** \brief
   * Calculates the logaritmic error progression
   */
106
#define EOC(e,eo) log(eo/std::max(e,1.0e-15))/M_LN2
107
108

  /** \brief
109
   * Content comparision of two pointers. Used e.g. for std::find_if
110
111
   */
  template<typename T>
112
  struct comparePtrContents : public std::binary_function<T*, T*, bool>
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
  {
    /** \brief
     * Overrides binary_function::operator()
     */
    bool operator()(T* a, T* b) const {
      return (*a == *b);
    };
  };

  // ===== some simple template functions ====================================== 

  //template<typename T> T max(T a,T b ) {return  ((a) > (b) ? (a) : (b));}
  template<typename T,typename S> inline T max(T a,S b )
  {
    return  ((a) > (b) ? (a) : (b));
  }

  template<typename T> inline T min(T a,T b) 
  {
    return  ((a) < (b)) ? (a) : (b);
  }

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

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

  // ===== 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

  // ============================================================================
  // ===== class Msg ============================================================
  // ============================================================================

  /** \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:
    /** \brief
     * Prints a formated message to the message stream
     */
    static void print(const char *format, ...);

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

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

192
193
194
195
196
    /** \brief
     *
     */
    static void catch_error_exit(const char *format, ...) {};

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
    /** \brief
     * Prints an error message with funcname, file, and line to the error stream
     */
    static void print_error_funcname(const char *funcname,
				     const char *file, 
				     int line);

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

    /** \brief
     * Prints a warning with funcname, file, and line to the message stream
     */
    static void print_warn_funcname(const char *funcname,
				    const char *file, 
				    int line);

    /** \brief
     * Prints the funcname to the message stream
     */
    static void print_funcname(const char *funcname);

    /** \brief
     * Changes the message stream
     */
224
    static void change_out(std::ostream*);
225
226
227
228
229
230
231
232
233

    /** \brief
     * Creates a filestream and sets the message stream to this filestream
     */
    static void open_file(const char *filename, OPENMODE);

    /** \brief
     * Changes the error stream 
     */
234
    static void change_error_out(std::ofstream *fp);
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275

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

    /** \brief
     * Generates a filename from path, file and ntime
     */
    static const char *generate_filename(const char *path, 
					 const char *file, 
					 int         ntime);

    /** \brief
     * Sets \ref msgInfo
     */
    static void setMsgInfo(int info) { msgInfo = info; };

    /** \brief
     * Returns \ref msgInfo
     */
    static int  getMsgInfo() { return msgInfo; };

    /** \brief
     * Sets \ref msgWait
     */
    static void setMsgWait(bool wait) { msgWait = wait; };

    /** \brief
     * Returns \ref msgWait
     */
    static bool getMsgWait() { return msgWait; };

    /** \brief
     * Waits for enter if w is true
     */
    static void wait(bool w);

    /** \brief
     * Returns \ref out
     */
276
    static std::ostream *getOutStream() { return out; };
277
278
279
280

    /** \brief
     * Returns \ref error
     */
281
    static std::ostream *getErrorStream() { return error; };
282
283
284
285
286

  protected:
    /** \brief
     * Message stram
     */
287
    static std::ostream *out;
288
289
290
291

    /** \brief
     * Error stream
     */
292
    static std::ostream *error;
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344

    /** \brief
     * Remember funcName to avoid multiple output of funcName within the same
     * function call
     */
    static const char *oldFuncName;

    /** \brief
     * Global info level
     */
    static int msgInfo;

    /** \brief
     * Spezifies whether to wait when WAIT is called
     */
    static bool msgWait;
  };

  // ============================================================================
  // ===== message macros =======================================================
  // ============================================================================

  /** \brief 
   * Should be the first call in every functions. It defines the current function
   * name nn for message output via MSG, WARNING, ...
   */

#define FUNCNAME(nn) const char *funcName; funcName = nn;

  /** \brief 
   * prints an error message 
   */
#define ERROR Msg::print_error_funcname(funcName,__FILE__, __LINE__),	\
    Msg::print_error

  /** \brief 
   * prints an error message and exits 
   */
#define ERROR_EXIT Msg::print_error_funcname(funcName,__FILE__, __LINE__), \
    Msg::print_error_exit

  /** \brief 
   * prints a warning
   */
#define WARNING Msg::print_warn_funcname(funcName,__FILE__, __LINE__),	\
    Msg::print_warn

  /** \brief 
   * if test is false, an error message is printed
   */
#define TEST(test) if ((test));else ERROR

345

346
347
348
  /** \brief 
   * if test is false, an error message is printed and the program exits
   */
Thomas Witkowski's avatar
Thomas Witkowski committed
349
#define TEST_EXIT(test) if ((test));else ERROR_EXIT
350
351
352
353
354
355
356
357
358

  /** \brief
   * In debug mode, it corresponds to ERROR_EXIT, otherwise it is noop.
   */
#if (DEBUG == 0) 
  #define TEST_EXIT_DBG(test) if (false) Msg::catch_error_exit
#else
#define TEST_EXIT_DBG(test) if ((test));else ERROR_EXIT
#endif
359
360
361
362
363
364
365
366
367
368

  /** \brief 
   * prints a message
   */
#define MSG Msg::print_funcname(funcName), Msg::print

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

  /** \brief 
   * prints a message, if min(Msg::msgInfo, info) >= noinfo
   */
#define PRINT_INFO(info,noinfo)						\
375
  if (Msg::getMsgInfo()&&(std::min(Msg::getMsgInfo(),(info))>=(noinfo))) Msg::print
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
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
432
433
434
435
436
437
438
439
440
441
442
443
444
445


  /** \brief 
   * 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.
   */
#define WAIT Msg::wait(Msg::getMsgWait())

  /** \brief
   * produces the message 'wait for <enter> ...' and will continue after pressing
   * the return or enter key.
   */
#define WAIT_REALLY Msg::wait(true)

#include <time.h>
#define TIME_USED(f,s) ((double)((s)-(f))/(double)CLOCKS_PER_SEC)

  /** \brief
   * internal used indices to represent the different geometrical objects.
   * Used as parameter for getGeo() and as template parameter for FixVec. 
   */
  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

  // ============================================================================
  // ===== class Global =========================================================
  // ============================================================================

  /** \ingroup Common
   * \brief
   * Static class wich holds global information about the world and all types of
   * elements.
   */
  class Global
  {
  public:
    /** \brief
     * returns a pointer to \ref referenceElement [dim]. With this pointer you
     * can get information about the element via Element's getGeo method.
     */
    static const Element *getReferenceElement(int dim) {
446
      FUNCNAME("Global::getReferenceElement()");
447
      TEST_EXIT((dim > 0) && (dim < 4))("invalid dim: %d\n", dim);
448
449
450
451
452
453
454
      return referenceElement[dim];
    };

    /** \brief
     * returns geometrical information. Currently this is only dimOfWorld.
     */
    static inline int getGeo(GeoIndex p) {
455
456
457
458
      if (WORLD == p) 
	return dimOfWorld; 

      ERROR_EXIT("Illegal request for geometry data: part=%d!\n", p);
459
460
461
462
463
464
465
      return 0;
    };

    /** \brief
     * returns geometrical information about elements of the dimension dim.
     * getGeo(VERTEX, 3) returns 4 because a Tetrahedron has 4 vertices.
     */
466
467
468
469
470
471
472
473
474
475
    static inline int getGeo(GeoIndex p, int dim) {
      TEST_EXIT_DBG((p >= MINPART) && (p <= MAXPART))
	("Calling for invalid geometry value %d\n",p);
      TEST_EXIT_DBG((dim >= 0) && (dim < 4))
	("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];
    }
476
477
478
479
480
481

    /** \brief
     * inits the Global class with the help of Parameters.
     */
    static void init();

Thomas Witkowski's avatar
Thomas Witkowski committed
482
  private:
483
    /** \brief
Thomas Witkowski's avatar
Thomas Witkowski committed
484
485
     * Global is a pure static class. So the constructor is private to avoid
     * instantiation.
486
     */
Thomas Witkowski's avatar
Thomas Witkowski committed
487
    Global();
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502

  private:
    /** \brief
     * dimension of the simulated world
     */
    static int dimOfWorld;

    /** \brief
     * 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.
     */
    static Element *referenceElement[4];
503

Thomas Witkowski's avatar
Thomas Witkowski committed
504
505
506
    /** \brief
     * Stores the precalculated results that should be returned by Global::getGeo.
     */
507
    static std::vector< std::vector< int > > geoIndexTable;
508
509
510
511
512
513
  };

#define COMMA ,

  const int RescheduleErrorCode = 23;

514
  std::string memSizeStr(int size);
515
516
517
518
}

#endif // AMDIS_GLOBAL_H