Global.cc 7.94 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
25
26
27
    { NULL, 
      NEW Line(NULL), 
      NEW Triangle(NULL), 
      NEW Tetrahedron(NULL) };
28
29
30
31
32
33
34
35
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

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

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

  void Msg::change_out(::std::ostream  *fp)
  {
    FUNCNAME("Msg::change_out()");
  
    if (fp) {
      if (out &&  *out != ::std::cout  &&  *out != ::std::cerr) {
	dynamic_cast< ::std::ofstream*>(out)->close();
	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)
  {
62
    FUNCNAME("Msg::open_file()");
63
64
65
    ::std::ofstream *fp;

 
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();
	delete out;
70
      }
71
72
73
74
75
76
77
78
79
80
      
      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");
    }
81
82
83
84
85
    return;
  }

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

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

    return;
  }

  void Msg::open_error_file(const char *filename, OPENMODE type)
  {
105
    FUNCNAME("Msg::open_error_file()");
106
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();
	delete error;
112
      }
113
114
115
116
117
118
119
120
121
122

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

123
124
125
126
127
    return;
  }

  const char *generate_filename(const char * path, const char * fn, int ntime)
  {
128
129
    static char name[256];
    char *cp;
130
  
131
132
133
134
135
136
137
138
139
140
141
142
    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);
    }
143
144
145
146
147
148
149
150
151
152
153
    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)
  {
154
155
    if (!out) 
      out = &::std::cout;
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171

    if (funcName &&  oldFuncName != funcName) {
      (*out)<< funcName << ":" << ::std::endl;
    } else if (!funcName) {
      (*out)<<"*unknown function*" << ::std::endl;
    }
    (*out) << "               ";
    oldFuncName = funcName;
  }

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

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

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

    old_line = line;
  }

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

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

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

    exit(1);
  }

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


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

    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)
  {
228
    static int old_line = -1;
229

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

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

    old_line = line;
  }

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

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

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

    return;
  }


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

    if (!out) 
      out = &::std::cout;

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

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

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

    // set dimension
    dimOfWorld = d;

292
293
294
295
296
297
298
    // 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;
299
300
301
      }
    }

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

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

  int fac(int i)
  {
328
329
330
331
    if (i <= 1) 
      return 1;
    else 
      return i * fac(i - 1);
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
  }

  ::std::string memSizeStr(int size)
  {
    ::std::string result;
//     if (size > 1024) {
//       if (size > 1024 * 1024) {
// 	result << size / (1024 * 1024) << " MByte";
//       } else {
// 	result << size / 1024 << " KByte";
//       }
//     } else {
//       result << size << " Byte";
//     }

    return result;
  }
}