Global.cc 7.01 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
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
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
106
107
108
109
110
111
112
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
192
193
194
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
224
225
226
227
228
229
230
231
232
233
234
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
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
345
346
347
348
#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;
  ::std::ostream* Msg::out=NULL;
  ::std::ostream* Msg::error=NULL;
  int Global::dimOfWorld = 0;
  int Msg::msgInfo = 10;
  bool Msg::msgWait = true;

  Element *Global::referenceElement[4] = 
    { NULL, NEW Line(NULL), NEW Triangle(NULL), NEW Tetrahedron(NULL) };

  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)
  {
    FUNCNAME("Msg::open_file");
    ::std::ofstream *fp;

 
    if (filename  &&  (fp = new ::std::ofstream(filename, type)))
      {
	if (out  &&  *out != ::std::cout  &&  *out != ::std::cerr)
	  {
	    dynamic_cast< ::std::ofstream*>(out)->close();
	    delete out;
	  }

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

  void Msg::change_error_out(::std::ofstream *fp)
  {
    FUNCNAME("Msg::change_error_out");
    if (fp)
      {
	if (error  &&  *error != ::std::cout  &&  *error != ::std::cerr)
	  {
	    dynamic_cast< ::std::ofstream*>(error)->close();
	    delete error;
	  }
    
	error = fp;
      }
    else
      {
	ERROR("file pointer is pointer to nil;\n");
	ERROR("use previous stream for errors furthermore\n");
      }

    return;
  }

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

    if (filename  &&  (fp = new ::std::ofstream(filename, type)))
      {
	if (error  &&  *error != ::std::cout  &&  *error != ::std::cerr)
	  {
	    dynamic_cast< ::std::ofstream*>(error)->close();
	    delete error;
	  }

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

  const char *generate_filename(const char * path, const char * fn, int ntime)
  {
    static char  name[256];
    char         *cp;
  
    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);
      }
    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)
  {
    if (!out) out = &::std::cout;

    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;

    if (!error) error = &::std::cerr;

    if (funcName &&  oldFuncName != funcName) {
      (*error)<<funcName<< ": ";
    }
    else if (!funcName) {
      if (line-old_line > 5) (*error)<< "*unknown function*";
    }
    if (oldFuncName != funcName) {
      (*error)<<"ERROR in "<<file<<", line "<<line<<"\n";
      oldFuncName = funcName;
    } else if (line - old_line > 5)
      (*error)<< "ERROR in "<<file<<", line "<<line<<"\n";

    old_line = line;
  }

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

    if (!error) error = &::std::cerr;

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

    exit(1);
  }

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


    if (!error) error = &::std::cerr;

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

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

    if (funcName  &&  oldFuncName != funcName) {
      (*out)<<funcName<<": ";
    }
    else if (!funcName){
      (*out)<< "*unknown function*";
    }
    if (oldFuncName != funcName) {
      (*out)<<"WARNING in "<<file<<", line "<<line<<"\n";
      oldFuncName = funcName;
    } else if (line - old_line > 5)
      (*out)<<"WARNING in "<<file<<", line "<<line<<"\n";

    old_line = line;
  }

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

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

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

    return;
  }


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

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

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

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

    // get dimension
    TEST_EXIT(Parameters::initialized())("Parameters not initialized!\n");
    Parameters::getGlobalParameter(0,"dimension of world","%d",&d);
    TEST_EXIT(d>0)("can not initialize dimension\n");
    TEST_EXIT((d==1)||(d==2)||(d==3))("Invalid world dimension %d!\n",d);

    // set dimension
    dimOfWorld = d;

    // set msgWait
    Parameters::getGlobalParameter(0,"WAIT","%d",&d);  
    Msg::setMsgWait(!(d==0));
  };

  int Global::getGeo(GeoIndex p, int dim) 
  {
    FUNCNAME("Global::getGeo");
    initTest();
    TEST_EXIT((p>=MINPART)&&(p<=MAXPART))
      ("Calling for invalid geometry value %d\n",p);
    TEST_EXIT((dim>=0)&&(dim<4))("invalid dim: %d\n", dim);

    if(p == WORLD) return dimOfWorld;

    if(dim == 0) {
      switch(p) {
      case PARTS:
	return 1;
      case VERTEX:
	return 1;
      case EDGE:
	return 0;
      case FACE:
	return 0;
      default:
	ERROR_EXIT("dim = 0\n");
      }
    }

    return referenceElement[dim]->getGeo(p);
  }


  int fac(int i)
  {
    if(i<=1) return 1;
    else return i*fac(i-1);
  }

  ::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;
  }
}