Global.cc 7.44 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
//
// 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.


13
14
15
16
#include <stdarg.h>
#include <stdio.h>
#include <sstream>

17
18
19
20
21
22
23
24
25
26
27
#include "Global.h"
#include "Parameters.h"
#include "Element.h"
#include "Line.h"
#include "Triangle.h"
#include "Tetrahedron.h"

namespace AMDiS {

  const char *funcName = NULL;
  const char *Msg::oldFuncName = NULL;
28
29
  std::ostream* Msg::out = NULL;
  std::ostream* Msg::error = NULL;
30
  int Global::dimOfWorld = 0;
31
  std::vector< std::vector< int > > Global::geoIndexTable;
32
33
34
35
  int Msg::msgInfo = 10;
  bool Msg::msgWait = true;

  Element *Global::referenceElement[4] = 
36
    { NULL, 
Thomas Witkowski's avatar
Thomas Witkowski committed
37
38
39
      new Line(NULL), 
      new Triangle(NULL), 
      new Tetrahedron(NULL) 
Thomas Witkowski's avatar
Thomas Witkowski committed
40
    };
41
42
43
44
45
46
47
48
49
50
51
52

  void Msg::wait(bool w)
  {
    FUNCNAME("Msg::wait()");

    if (w) {
      char line[10];
      MSG("wait for <enter> ...");
      fgets(line, 9, stdin);
    }
  }

53
  void Msg::change_out(std::ostream  *fp)
54
55
56
57
  {
    FUNCNAME("Msg::change_out()");
  
    if (fp) {
58
59
      if (out && *out != std::cout && *out != std::cerr) {
	dynamic_cast< std::ofstream*>(out)->close();
60
61
62
63
64
65
66
	delete out;
      }

      out = fp;
    } else {
      ERROR("file pointer is pointer to nil;\n");
      ERROR("use previous stream for errors furthermore\n");
67
    } 
68
69
  }

70
  void Msg::change_error_out(std::ofstream *fp)
71
  {
72
73
74
    FUNCNAME("Msg::change_error_out()");

    if (fp) {
75
76
      if (error  &&  *error != std::cout  &&  *error != std::cerr) {
	dynamic_cast< std::ofstream*>(error)->close();
77
	delete error;
78
      }
79
80
81
82
83
84
      
      error = fp;
    } else {
      ERROR("file pointer is pointer to nil;\n");
      ERROR("use previous stream for errors furthermore\n");
    }
85
86
87
88
  }

  void Msg::open_error_file(const char *filename, OPENMODE type)
  {
89
    FUNCNAME("Msg::open_error_file()");
90
    std::ofstream *fp;
91

92
93
94
    if (filename  &&  (fp = new std::ofstream(filename, type))) {
      if (error  &&  *error != std::cout  &&  *error != std::cerr) {
	dynamic_cast< std::ofstream*>(error)->close();
95
	delete error;
96
      }
97
98
99
100
101
102
103
104
105

      error = fp;
    } else {
      if (filename)
	ERROR("can not open %s;\n", filename);
      else
	ERROR("no filename specified;\n");
      ERROR("use previous stream for errors furthermore\n");
    }
106
107
108
109
  }

  void Msg::print_funcname(const char *funcName)
  {
110
    if (!out) 
111
      out = &std::cout;
112
113

    if (funcName &&  oldFuncName != funcName) {
114
      PRINT_LINE((*out), funcName << ":" << std::endl);
115
    } else if (!funcName) {
116
      PRINT_LINE((*out), "*unknown function*" << std::endl);
117
    }
118
119
    PRINT_LINE((*out), "               ");

120
121
122
    oldFuncName = funcName;
  }

123
  void Msg::print_error_funcname(const char *funcName, const char *file, int line)
124
125
126
  {
    static int old_line = -1;

127
    if (!error) 
128
      error = &std::cerr;
129

130
131
132
133
    std::stringstream oss;

    if (funcName && oldFuncName != funcName)
      oss << funcName << ": ";
134
    else if (!funcName) {
135
      if (line-old_line > 5) 
136
	oss << "*unknown function*";
137
138
    }
    if (oldFuncName != funcName) {
139
      oss << "ERROR in " << file << ", line " << line << std::endl;;
140
141
      oldFuncName = funcName;
    } else if (line - old_line > 5)
142
      oss << "ERROR in " << file << ", line " << line << "\n" << std::endl;
143

144
    PRINT_LINE((*error), oss.str());
145
146
147
148
149
    old_line = line;
  }

  void Msg::print_error_exit(const char *format, ...)
  {
150
151
    va_list arg;
    char buff[255];
152

153
    if (!error) 
154
      error = &std::cerr;
155
156
157

    va_start(arg, format);
    vsprintf(buff, format, arg);
158
    PRINT_LINE((*error), buff);
159
160
161
162
163
164
165
    va_end(arg);

    exit(1);
  }

  void Msg::print_error(const char *format, ...)
  {
166
167
    va_list arg;
    char buff[255];
168
169


170
    if (!error) 
171
      error = &std::cerr;
172
173
174

    va_start(arg, format);
    vsprintf(buff, format, arg);
175
    PRINT_LINE((*error), buff);
176
177
178
179
180
181
182
    va_end(arg);
  }

  void Msg::print_warn_funcname(const char *funcName,
				const char *file, 
				int line)
  {
183
    static int old_line = -1;
184

185
    if (!out) 
186
      out = &std::cout;
187

188
189
    std::stringstream oss;

190
    if (funcName  &&  oldFuncName != funcName) {
191
192
193
      oss << funcName << ": ";
    } else if (!funcName) {
      oss << "*unknown function*";
194
    }
195

196
    if (oldFuncName != funcName) {
197
      oss << "WARNING in " << file << ", line " << line << std::endl;
198
      oldFuncName = funcName;
199
200
201
202
    } else if (line - old_line > 5) {
      oss << "WARNING in " << file << ", line " << line << std::endl;
    }

203
204
205
    if (oss.str() != "") 
      PRINT_LINE((*out), oss.str());
    
206
207
208
209
210
    old_line = line;
  }

  void Msg::print_warn(const char *format, ...)
  {
211
212
    va_list arg;
    char buff[255];
213

214
    if (!out) 
215
      out = &std::cout;
216
217
218

    va_start(arg, format);
    vsprintf(buff, format, arg);
219
    PRINT_LINE((*out), buff);
220
221
222
223
224
225
    va_end(arg);
  }


  void Msg::print(const char *format, ...)
  {
226
227
    va_list arg;
    char buff[255];
228
229

    if (!out) 
230
      out = &std::cout;
231
232
233

    va_start(arg, format);
    vsprintf(buff, format, arg);
234
    (*out) << buff;
235
236
237
238
239
240
241
242
243
    va_end(arg);
  }

  void Global::init()
  {
    int d = -1;

    // get dimension
    TEST_EXIT(Parameters::initialized())("Parameters not initialized!\n");
244
245
    Parameters::getGlobalParameter(0, "dimension of world","%d",&d);
    TEST_EXIT(d > 0)("Cannot initialize dimension!\n");
246
    TEST_EXIT((d == 1) || (d == 2) || (d == 3))("Invalid world dimension %d!\n",d);
247
248
249
250

    // set dimension
    dimOfWorld = d;

251
252
253
254
255
    // prepare geoIndex-Table
    int geoTableSize = abs(static_cast<int>(MINPART)) + MAXPART + 1;
    geoIndexTable.resize(4);
    for (int i = 0; i < 4; i++) {
      geoIndexTable[i].resize(geoTableSize);
256
257
      for (int j = 0; j < geoTableSize; j++)
	geoIndexTable[i][j] = 0;      
258
259
    }

260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
    geoIndexTable[0][PARTS - MINPART] = 1;
    geoIndexTable[0][VERTEX - MINPART] = 1;
    geoIndexTable[0][EDGE - MINPART] = 0;
    geoIndexTable[0][FACE - MINPART] = 0;
    geoIndexTable[0][WORLD - MINPART] = dimOfWorld;

    for (int i = 1; i < 4; i++) {
      geoIndexTable[i][CENTER - MINPART] = referenceElement[i]->getGeo(CENTER);
      geoIndexTable[i][VERTEX - MINPART] = referenceElement[i]->getGeo(VERTEX);
      geoIndexTable[i][EDGE - MINPART] = referenceElement[i]->getGeo(EDGE);
      geoIndexTable[i][FACE - MINPART] = referenceElement[i]->getGeo(FACE);
      geoIndexTable[i][DIMEN - MINPART] = referenceElement[i]->getGeo(DIMEN);
      geoIndexTable[i][PARTS - MINPART] = referenceElement[i]->getGeo(PARTS);
      geoIndexTable[i][NEIGH - MINPART] = referenceElement[i]->getGeo(NEIGH);
      geoIndexTable[i][WORLD - MINPART] = dimOfWorld;
      geoIndexTable[i][BOUNDARY - MINPART] = referenceElement[i]->getGeo(BOUNDARY);
      geoIndexTable[i][PROJECTION - MINPART] = referenceElement[i]->getGeo(PROJECTION);
    }
278

279
280
281
    // set msgWait
    Parameters::getGlobalParameter(0, "WAIT", "%d", &d);  
    Msg::setMsgWait(!(d == 0));
282
283
284
285
  }

  void Global::clear()
  {
Thomas Witkowski's avatar
Thomas Witkowski committed
286
287
288
    delete referenceElement[1];
    delete referenceElement[2];
    delete referenceElement[3];
289
  }
290
291
292

  int fac(int i)
  {
293
294
295
296
    if (i <= 1) 
      return 1;
    else 
      return i * fac(i - 1);
297
298
  }

299
300
301
302
303
304
  void waitSec(int seconds)
  {
    clock_t endwait = clock () + seconds * CLOCKS_PER_SEC;
    while (clock() < endwait) {}
  }

305
  std::string memSizeStr(int size)
306
  {
307
    std::string result;
308
309
310
311
312
313
314
315
316
317
318
319
320
//     if (size > 1024) {
//       if (size > 1024 * 1024) {
// 	result << size / (1024 * 1024) << " MByte";
//       } else {
// 	result << size / 1024 << " KByte";
//       }
//     } else {
//       result << size << " Byte";
//     }

    return result;
  }
}