Global.cc 7.03 KB
Newer Older
1
2
3
4
#include <stdarg.h>
#include <stdio.h>
#include <sstream>

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"

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

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

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

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

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

58
  void Msg::change_error_out(std::ofstream *fp)
59
  {
60
61
62
    FUNCNAME("Msg::change_error_out()");

    if (fp) {
63
64
      if (error  &&  *error != std::cout  &&  *error != std::cerr) {
	dynamic_cast< std::ofstream*>(error)->close();
65
	delete error;
66
      }
67
68
69
70
71
72
      
      error = fp;
    } else {
      ERROR("file pointer is pointer to nil;\n");
      ERROR("use previous stream for errors furthermore\n");
    }
73
74
75
76
  }

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

80
81
82
    if (filename  &&  (fp = new std::ofstream(filename, type))) {
      if (error  &&  *error != std::cout  &&  *error != std::cerr) {
	dynamic_cast< std::ofstream*>(error)->close();
83
	delete error;
84
      }
85
86
87
88
89
90
91
92
93

      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");
    }
94
95
96
97
  }

  void Msg::print_funcname(const char *funcName)
  {
98
    if (!out) 
99
      out = &std::cout;
100
101

    if (funcName &&  oldFuncName != funcName) {
102
      PRINT_LINE((*out), funcName << ":" << std::endl);
103
    } else if (!funcName) {
104
      PRINT_LINE((*out), "*unknown function*" << std::endl);
105
    }
106
107
    PRINT_LINE((*out), "               ");

108
109
110
    oldFuncName = funcName;
  }

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

115
    if (!error) 
116
      error = &std::cerr;
117

118
119
120
121
    std::stringstream oss;

    if (funcName && oldFuncName != funcName)
      oss << funcName << ": ";
122
    else if (!funcName) {
123
      if (line-old_line > 5) 
124
	oss << "*unknown function*";
125
126
    }
    if (oldFuncName != funcName) {
127
      oss << "ERROR in " << file << ", line " << line << std::endl;;
128
129
      oldFuncName = funcName;
    } else if (line - old_line > 5)
130
      oss << "ERROR in " << file << ", line " << line << "\n" << std::endl;
131

132
    PRINT_LINE((*error), oss.str());
133
134
135
136
137
    old_line = line;
  }

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

141
    if (!error) 
142
      error = &std::cerr;
143
144
145

    va_start(arg, format);
    vsprintf(buff, format, arg);
146
    PRINT_LINE((*error), buff);
147
148
149
150
151
152
153
    va_end(arg);

    exit(1);
  }

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


158
    if (!error) 
159
      error = &std::cerr;
160
161
162

    va_start(arg, format);
    vsprintf(buff, format, arg);
163
    PRINT_LINE((*error), buff);
164
165
166
167
168
169
170
    va_end(arg);
  }

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

173
    if (!out) 
174
      out = &std::cout;
175

176
177
    std::stringstream oss;

178
    if (funcName  &&  oldFuncName != funcName) {
179
180
181
      oss << funcName << ": ";
    } else if (!funcName) {
      oss << "*unknown function*";
182
    }
183

184
    if (oldFuncName != funcName) {
185
      oss << "WARNING in " << file << ", line " << line << std::endl;
186
      oldFuncName = funcName;
187
188
189
190
191
    } else if (line - old_line > 5) {
      oss << "WARNING in " << file << ", line " << line << std::endl;
    }

    PRINT_LINE((*out), oss.str());
192
193
194
195
196
197

    old_line = line;
  }

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

201
    if (!out) 
202
      out = &std::cout;
203
204
205

    va_start(arg, format);
    vsprintf(buff, format, arg);
206
    PRINT_LINE((*out), buff);
207
208
209
210
211
212
    va_end(arg);
  }


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

    if (!out) 
217
      out = &std::cout;
218
219
220

    va_start(arg, format);
    vsprintf(buff, format, arg);
221
    (*out) << buff;
222
223
224
225
226
227
228
229
230
    va_end(arg);
  }

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

    // get dimension
    TEST_EXIT(Parameters::initialized())("Parameters not initialized!\n");
231
232
    Parameters::getGlobalParameter(0, "dimension of world","%d",&d);
    TEST_EXIT(d > 0)("Cannot initialize dimension!\n");
233
    TEST_EXIT((d == 1) || (d == 2) || (d == 3))("Invalid world dimension %d!\n",d);
234
235
236
237

    // set dimension
    dimOfWorld = d;

238
239
240
241
242
    // 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);
243
244
      for (int j = 0; j < geoTableSize; j++)
	geoIndexTable[i][j] = 0;      
245
246
    }

247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
    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);
    }
265

266
267
268
    // set msgWait
    Parameters::getGlobalParameter(0, "WAIT", "%d", &d);  
    Msg::setMsgWait(!(d == 0));
269
270
271
272
  }

  void Global::clear()
  {
Thomas Witkowski's avatar
Thomas Witkowski committed
273
274
275
    delete referenceElement[1];
    delete referenceElement[2];
    delete referenceElement[3];
276
  }
277
278
279

  int fac(int i)
  {
280
281
282
283
    if (i <= 1) 
      return 1;
    else 
      return i * fac(i - 1);
284
285
  }

286
  std::string memSizeStr(int size)
287
  {
288
    std::string result;
289
290
291
292
293
294
295
296
297
298
299
300
301
//     if (size > 1024) {
//       if (size > 1024 * 1024) {
// 	result << size / (1024 * 1024) << " MByte";
//       } else {
// 	result << size / 1024 << " KByte";
//       }
//     } else {
//       result << size << " Byte";
//     }

    return result;
  }
}