Global.cc 8 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include "Global.h"
#include "Parameters.h"
#include "Element.h"
#include "Line.h"
#include "Triangle.h"
#include "Tetrahedron.h"


#include <stdarg.h>
#include <stdio.h>

namespace AMDiS {

  const char *funcName = NULL;
  const char *Msg::oldFuncName = NULL;
16
17
  std::ostream* Msg::out = NULL;
  std::ostream* Msg::error = NULL;
18
  int Global::dimOfWorld = 0;
19
  std::vector< std::vector< int > > Global::geoIndexTable;
20
21
22
23
  int Msg::msgInfo = 10;
  bool Msg::msgWait = true;

  Element *Global::referenceElement[4] = 
24
    { NULL, 
Thomas Witkowski's avatar
Thomas Witkowski committed
25
26
27
      new Line(NULL), 
      new Triangle(NULL), 
      new Tetrahedron(NULL) 
Thomas Witkowski's avatar
Thomas Witkowski committed
28
    };
29
30
31
32
33
34
35
36
37
38
39
40
41

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

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

42
  void Msg::change_out(std::ostream  *fp)
43
44
45
46
  {
    FUNCNAME("Msg::change_out()");
  
    if (fp) {
47
48
      if (out && *out != std::cout && *out != std::cerr) {
	dynamic_cast< std::ofstream*>(out)->close();
49
50
51
52
53
54
55
56
57
58
59
60
61
62
	delete out;
      }

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

  void Msg::open_file(const char *filename, OPENMODE type)
  {
63
    FUNCNAME("Msg::open_file()");
64
    std::ofstream *fp;
65
66

 
67
68
69
    if (filename  &&  (fp = new std::ofstream(filename, type))) {
      if (out && *out != std::cout && *out != std::cerr) {
	dynamic_cast< std::ofstream*>(out)->close();
70
	delete out;
71
      }
72
73
74
75
76
77
78
79
80
81
      
      out = fp;
    } else {
      if (filename)
	ERROR("can not open %s;\n", filename);
      else
	ERROR("no filename specified;\n");
    
      ERROR("use previous stream for messages furthermore\n");
    }
82
83
84
    return;
  }

85
  void Msg::change_error_out(std::ofstream *fp)
86
  {
87
88
89
    FUNCNAME("Msg::change_error_out()");

    if (fp) {
90
91
      if (error  &&  *error != std::cout  &&  *error != std::cerr) {
	dynamic_cast< std::ofstream*>(error)->close();
92
	delete error;
93
      }
94
95
96
97
98
99
      
      error = fp;
    } else {
      ERROR("file pointer is pointer to nil;\n");
      ERROR("use previous stream for errors furthermore\n");
    }
100
101
102
103
104
105

    return;
  }

  void Msg::open_error_file(const char *filename, OPENMODE type)
  {
106
    FUNCNAME("Msg::open_error_file()");
107
    std::ofstream *fp;
108

109
110
111
    if (filename  &&  (fp = new std::ofstream(filename, type))) {
      if (error  &&  *error != std::cout  &&  *error != std::cerr) {
	dynamic_cast< std::ofstream*>(error)->close();
112
	delete error;
113
      }
114
115
116
117
118
119
120
121
122
123

      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");
    }

124
125
126
127
128
    return;
  }

  const char *generate_filename(const char * path, const char * fn, int ntime)
  {
129
130
    static char name[256];
    char *cp;
131
  
132
133
134
135
136
137
138
139
140
141
142
143
    if (path == NULL || path[0] == '\0') {
      sprintf(name, "./%s", fn);
    } else {
      const char* ccp = path;
      while (*ccp)
	ccp++;
      ccp--;
      if (*ccp == '/')
	sprintf(name, "%s%s", path, fn);
      else
	sprintf(name, "%s/%s", path, fn);
    }
144
145
146
147
148
149
150
151
152
153
154
    cp = name;
    while (*cp)
      cp++;
    sprintf(cp, "%d", 100000 + ntime);
    if (ntime < 100000)  *cp = '0';

    return(const_cast<const char *>(name));
  }

  void Msg::print_funcname(const char *funcName)
  {
155
    if (!out) 
156
      out = &std::cout;
157
158

    if (funcName &&  oldFuncName != funcName) {
159
      (*out)<< funcName << ":" << std::endl;
160
    } else if (!funcName) {
161
      (*out)<<"*unknown function*" << std::endl;
162
163
164
165
166
167
168
169
170
171
172
    }
    (*out) << "               ";
    oldFuncName = funcName;
  }

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

173
    if (!error) 
174
      error = &std::cerr;
175
176
177
178
179

    if (funcName &&  oldFuncName != funcName) {
      (*error)<<funcName<< ": ";
    }
    else if (!funcName) {
180
181
      if (line-old_line > 5) 
	(*error) << "*unknown function*";
182
183
    }
    if (oldFuncName != funcName) {
184
      (*error) << "ERROR in " << file << ", line " << line << "\n";
185
186
      oldFuncName = funcName;
    } else if (line - old_line > 5)
187
      (*error) << "ERROR in " << file << ", line " << line << "\n";
188
189
190
191
192
193

    old_line = line;
  }

  void Msg::print_error_exit(const char *format, ...)
  {
194
195
    va_list arg;
    char buff[255];
196

197
    if (!error) 
198
      error = &std::cerr;
199
200
201
202
203
204
205
206
207
208
209

    va_start(arg, format);
    vsprintf(buff, format, arg);
    (*error)<<buff;
    va_end(arg);

    exit(1);
  }

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


214
    if (!error) 
215
      error = &std::cerr;
216
217
218
219
220
221
222
223
224
225
226
227
228

    va_start(arg, format);
    vsprintf(buff, format, arg);
    (*error)<<buff;
    va_end(arg);

    return;
  }

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

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

    if (funcName  &&  oldFuncName != funcName) {
235
      (*out) << funcName << ": ";
236
237
    }
    else if (!funcName){
238
      (*out) << "*unknown function*";
239
240
    }
    if (oldFuncName != funcName) {
241
      (*out) << "WARNING in " << file << ", line " << line << "\n";
242
243
      oldFuncName = funcName;
    } else if (line - old_line > 5)
244
      (*out) << "WARNING in " << file << ", line " << line << "\n";
245
246
247
248
249
250

    old_line = line;
  }

  void Msg::print_warn(const char *format, ...)
  {
251
252
    va_list arg;
    char buff[255];
253

254
    if (!out) 
255
      out = &std::cout;
256
257
258
259
260
261
262
263
264
265
266
267

    va_start(arg, format);
    vsprintf(buff, format, arg);
    (*out)<<buff;
    va_end(arg);

    return;
  }


  void Msg::print(const char *format, ...)
  {
268
269
    va_list arg;
    char buff[255];
270
271

    if (!out) 
272
      out = &std::cout;
273
274
275

    va_start(arg, format);
    vsprintf(buff, format, arg);
276
    (*out) << buff;
277
278
279
280
281
282
283
284
285
    va_end(arg);
  }

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

    // get dimension
    TEST_EXIT(Parameters::initialized())("Parameters not initialized!\n");
286
287
    Parameters::getGlobalParameter(0, "dimension of world","%d",&d);
    TEST_EXIT(d > 0)("Cannot initialize dimension!\n");
288
    TEST_EXIT((d == 1) || (d == 2) || (d == 3))("Invalid world dimension %d!\n",d);
289
290
291
292

    // set dimension
    dimOfWorld = d;

293
294
295
296
297
298
299
    // 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);
      for (int j = 0; j < geoTableSize; j++) {
	geoIndexTable[i][j] = 0;
300
301
302
      }
    }

303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
    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);
    }
321

322
323
324
    // set msgWait
    Parameters::getGlobalParameter(0, "WAIT", "%d", &d);  
    Msg::setMsgWait(!(d == 0));
325
326
327
328
  }

  void Global::clear()
  {
Thomas Witkowski's avatar
Thomas Witkowski committed
329
330
331
    delete referenceElement[1];
    delete referenceElement[2];
    delete referenceElement[3];
332
  }
333
334
335

  int fac(int i)
  {
336
337
338
339
    if (i <= 1) 
      return 1;
    else 
      return i * fac(i - 1);
340
341
  }

342
  std::string memSizeStr(int size)
343
  {
344
    std::string result;
345
346
347
348
349
350
351
352
353
354
355
356
357
//     if (size > 1024) {
//       if (size > 1024 * 1024) {
// 	result << size / (1024 * 1024) << " MByte";
//       } else {
// 	result << size / 1024 << " KByte";
//       }
//     } else {
//       result << size << " Byte";
//     }

    return result;
  }
}