SystemVector.h 11.4 KB
Newer Older
1
2
3
4
// ============================================================================
// ==                                                                        ==
// == AMDiS - Adaptive multidimensional simulations                          ==
// ==                                                                        ==
5
// ==  http://www.amdis-fem.org                                              ==
6
7
// ==                                                                        ==
// ============================================================================
8
9
10
11
12
13
14
15
16
17
18
19
//
// 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.


20
21
22
23
24
25
26
27
28
29

/** \file SystemVector.h */

#ifndef AMDIS_SYSTEMVECTOR_H
#define AMDIS_SYSTEMVECTOR_H

#include "MatrixVector.h"
#include "DOFVector.h"
#include "CreatorInterface.h"
#include "Serializable.h"
Thomas Witkowski's avatar
Thomas Witkowski committed
30
#include "Serializer.h"
31
32
33

namespace AMDiS {

34
  /// A system vector is a vector of dof vectors used for vector valued problems.
35
36
37
  class SystemVector : public Serializable
  {
  public:
38
    /// Constructor.
Thomas Witkowski's avatar
Thomas Witkowski committed
39
    SystemVector(std::string name_,
40
		 std::vector<FiniteElemSpace*> feSpace_, 
41
42
43
44
45
46
47
		 int size)
      : name(name_),
	feSpace(feSpace_),
	vectors(size)
      
    {
      vectors.set(NULL);
48
    }
49

50
    /// Copy Constructor.
51
52
53
54
55
56
    SystemVector(const SystemVector& rhs)
      : name(rhs.name),
	feSpace(rhs.feSpace),
	vectors(rhs.vectors.getSize())
      
    {
57
      for (int i = 0; i < vectors.getSize(); i++)
Thomas Witkowski's avatar
Thomas Witkowski committed
58
	vectors[i] = new DOFVector<double>(*rhs.vectors[i]);
59
60
    }

61
62
    ~SystemVector() 
    {
63
      for (int i = 0; i < vectors.getSize(); i++)
Thomas Witkowski's avatar
Thomas Witkowski committed
64
	delete vectors[i];
65
    }
66

67
68
    void createNewDOFVectors(std::string name)
    {     
69
      for (int i = 0; i < vectors.getSize(); i++)
Thomas Witkowski's avatar
Thomas Witkowski committed
70
	vectors[i] = new DOFVector<double>(feSpace[i], "tmp");
71
    }
72

73
74
75
    /// Sets \ref vectors[index] = vec.
    inline void setDOFVector(int index, DOFVector<double> *vec) 
    {
76
      TEST_EXIT_DBG(index < vectors.getSize())("invalid index\n");
77
      vectors[index] = vec;
78
    }
79

80
81
82
    /// Returns \ref vectors[index].
    inline DOFVector<double> *getDOFVector(int index) 
    {
83
      TEST_EXIT_DBG(index < vectors.getSize())("invalid index\n");
84
      return vectors[index];
85
    }
86

87
88
89
    /// Returns \ref vectors[index].
    inline const DOFVector<double> *getDOFVector(int index) const 
    {
90
      TEST_EXIT_DBG(index < vectors.getSize())("invalid index\n");
91
      return vectors[index];
92
    }
93

94
95
96
    /// Returns sum of used vector sizes.
    inline int getUsedSize() const 
    {
97
98
      int totalSize = 0;
      int size = vectors.getSize();
99
      for (int i = 0; i < size; i++)
100
101
	totalSize += vectors[i]->getUsedSize();
      return totalSize;
102
    }
103

104
105
106
    /// Returns number of contained vectors.
    inline int getNumVectors() const 
    { 
107
      return vectors.getSize(); 
108
    }
109

110
111
    inline int getSize() const 
    {
112
      return vectors.getSize();
113
    }
114

115
    /// Returns the fe space for a given component.
116
    inline FiniteElemSpace *getFeSpace(int i) const 
117
    { 
118
      return feSpace[i]; 
119
    }
120

121
    /// Returns the fe spaces for all components.
122
    inline std::vector<FiniteElemSpace*> getFeSpaces() const 
123
    {
124
125
126
      return feSpace;
    }

127
128
129
130
131
132
    /** \brief
     * Here the system vector is interpreted as one large vector. The given
     * is used as a global index which indicates a local vector number and
     * a local index on this vector. The operator returns this local vector
     * at the local index.
     */
133
134
    inline double& operator[](int index) 
    {
135
136
137
      int localIndex = index;
      int vectorIndex = 0;

138
      while (localIndex >= vectors[vectorIndex]->getUsedSize()) {
139
140
141
142
	localIndex -= vectors[vectorIndex++]->getUsedSize();
      }

      return (*(vectors[vectorIndex]))[localIndex];
143
    }
144

145
146
147
    /// For const access.
    inline double operator[](int index) const 
    {
148
149
150
      int localIndex = index;
      int vectorIndex = 0;

151
      while (localIndex >= vectors[vectorIndex]->getUsedSize()) {
152
153
154
155
	localIndex -= vectors[vectorIndex++]->getUsedSize();
      }

      return (*(vectors[vectorIndex]))[localIndex];
156
    }
157

158
159
160
    /// Sets all entries in all vectors to value.
    inline void set(double value) 
    {
161
      int size = vectors.getSize();
162
      for (int i = 0; i < size; i++)
163
	vectors[i]->set(value);
164
    }
165

166
167
168
    inline void setCoarsenOperation(CoarsenOperation op) 
    { 
      for (int i = 0; i < static_cast<int>(vectors.getSize()); i++)
169
170
171
	vectors[i]->setCoarsenOperation(op); 
    }

172
173
174
    /// Sets all entries in all vectors to value.
    inline SystemVector& operator=(double value) 
    {
175
      int size = vectors.getSize();
176
      for (int i = 0; i < size; i++)
177
178
	(*(vectors[i])) = value;
      return *this;
179
    }
180

181
182
183
    /// Assignement operator.
    inline SystemVector& operator=(const SystemVector& rhs) 
    {
184
      TEST_EXIT_DBG(rhs.vectors.getSize() == vectors.getSize())("invalied sizes\n");
185
      int size = vectors.getSize();
186
      for (int i = 0; i < size; i++)
187
188
	(*(vectors[i])) = (*(rhs.getDOFVector(i)));
      return *this;
189
    }
190

191
192
    void serialize(std::ostream &out) 
    {
193
      int size = vectors.getSize();
Thomas Witkowski's avatar
Thomas Witkowski committed
194
      SerUtil::serialize(out, size);
195
      for (int i = 0; i < size; i++)
196
	vectors[i]->serialize(out);
197
    }
198

199
200
    void deserialize(std::istream &in) 
    {
201
      int size, oldSize = vectors.getSize();
Thomas Witkowski's avatar
Thomas Witkowski committed
202
      SerUtil::deserialize(in, size);
203
      vectors.resize(size);
204
      for (int i = oldSize; i < size; i++)
Thomas Witkowski's avatar
Thomas Witkowski committed
205
	vectors[i] = new DOFVector<double>(feSpace[i], "");
206
      for (int i = 0; i < size; i++)
207
	vectors[i]->deserialize(in);
208
    }
209

210
211
    void copy(const SystemVector& rhs) 
    {
212
      int size = vectors.getSize();
213
      TEST_EXIT_DBG(size == rhs.getNumVectors())("invalid sizes\n");
214
      for (int i = 0; i < size; i++)
215
	vectors[i]->copy(*(const_cast<SystemVector&>(rhs).getDOFVector(i)));
216
    }
217

218
219
    void interpol(std::vector<AbstractFunction<double, WorldVector<double> >*> *f) 
    {
220
      int size = vectors.getSize();
221
      for (int i = 0; i < size; i++)
222
	vectors[i]->interpol((*f)[i]);
223
224
    }

225
226
227
    void interpol(SystemVector *v, double factor) 
    {
      for (int i = 0; i < v->getSize(); i++)
228
229
	vectors[i]->interpol(v->getDOFVector(i), factor);
    }
230

231
232
    void print() 
    {
233
      int size = vectors.getSize();
234
      for (int i = 0; i < size; i++)
235
	vectors[i]->print();
236
    }
237

238
239
    int calcMemoryUsage() 
    {
240
      int result = 0;
241
      for (int i = 0; i < static_cast<int>(vectors.getSize()); i++)
242
	result += vectors[i]->calcMemoryUsage();
243
244
      result += sizeof(SystemVector);

245
246
      return result;
    }
247

248
      
249
250

  protected:
251
    /// Name of the system vector
252
    std::string name;
253

254
    /// Finite element space.
255
    std::vector<FiniteElemSpace*> feSpace;
256

257
    /// Local dof vectors.
258
259
260
    Vector<DOFVector<double>*> vectors;
  };

261

262
263
264
  /// multiplication with scalar
  inline const SystemVector& operator*=(SystemVector& x, double d) 
  {
265
    int size = x.getNumVectors();
266
    for (int i = 0; i < size; i++)
267
268
      *(x.getDOFVector(i)) *= d;
    return x;
269
  }
270

271
272
273
274
  /// scalar product
  inline double operator*(SystemVector& x, SystemVector& y) 
  {
    TEST_EXIT_DBG(x.getNumVectors() == y.getNumVectors())("invalid size\n");
275
    double result = 0.0;
276
    int size = x.getNumVectors();
277
    for (int i = 0; i < size; i++)
278
279
      result += (*x.getDOFVector(i)) * (*y.getDOFVector(i));
    return result;
280
  }
281

282
283
  /// addition of two system vectors
  inline const SystemVector& operator+=(SystemVector& x, const SystemVector& y) 
284
  {
285
    TEST_EXIT_DBG(x.getNumVectors() == y.getNumVectors())("invalid size\n");
286
    int size = x.getNumVectors();
287
    for (int i = 0; i < size; i++)
288
289
      (*(x.getDOFVector(i))) += (*(y.getDOFVector(i)));
    return x;
290
  }
291

292
293
  /// subtraction of two system vectors.
  inline const SystemVector& operator-=(SystemVector& x, SystemVector& y) 
294
  {
295
    TEST_EXIT_DBG(x.getNumVectors() == y.getNumVectors())("invalid size\n");
296
    int size = x.getNumVectors();
297
    for (int i = 0; i < size; i++)
298
299
      (*(x.getDOFVector(i))) -= (*(y.getDOFVector(i)));
    return x;
300
  }
301

302
303
304
  /// multiplication with a scalar
  inline SystemVector operator*(SystemVector& x, double d) 
  {
305
    SystemVector result = x;
306
    int size = x.getNumVectors();
307
    for (int i = 0; i < size; i++)
308
309
      (*(result.getDOFVector(i))) *= d;
    return result;
310
  }
311

312
313
314
  /// multiplication with a scalar
  inline SystemVector operator*(double d, SystemVector& x) 
  {
315
    SystemVector result = x;
316
    int size = x.getNumVectors();
317
    for (int i = 0; i < size; i++)
318
319
      (*(result.getDOFVector(i))) *= d;
    return result;
320
  }
321

322
323
  /// addition of two system vectors
  inline SystemVector operator+(const SystemVector& x, const SystemVector& y)
324
  {
325
    TEST_EXIT_DBG(x.getNumVectors() == y.getNumVectors())("invalid size\n");
326
    SystemVector result = x;
327
    int size = x.getNumVectors();
328
    for (int i = 0; i < size; i++)
329
330
      (*(result.getDOFVector(i))) += (*(y.getDOFVector(i)));
    return result;
331
  }
332

333
334
335
  /// Calls SystemVector::set(). Used for solving.
  inline void set(SystemVector& x, double value) 
  {
336
    x.set(value);
337
  } 
338

339
340
341
  /// Calls SystemVector::set(). Used for solving.
  inline void setValue(SystemVector& x, double value) 
  {
342
    x.set(value);
343
  }
344

345
346
347
  /// Norm of system vector.
  inline double norm(SystemVector* x) 
  {
348
    double result = 0.0;
349
    int size = x->getNumVectors();
350
    for (int i = 0; i < size; i++)
351
352
      result += x->getDOFVector(i)->squareNrm2();
    return sqrt(result);
353
  }
354

355
356
357
  /// L2 norm of system vector.
  inline double L2Norm(SystemVector* x) 
  {
358
    double result = 0.0;
359
    int size = x->getNumVectors();
360
    for (int i = 0; i < size; i++)
361
362
      result += x->getDOFVector(i)->L2NormSquare();
    return sqrt(result);
363
  }
364

365
366
367
  /// H1 norm of system vector.
  inline double H1Norm(SystemVector* x) 
  {
368
    double result = 0.0;
369
    int size = x->getNumVectors();
370
    for (int i = 0; i < size; i++)
371
372
      result += x->getDOFVector(i)->H1NormSquare();
    return sqrt(result);
373
  }
374
375
376
377
378
379

  inline void mv(Matrix<DOFMatrix*> &matrix,
		 const SystemVector &x,
		 SystemVector       &result,
		 bool               add = false)
  {
Naumann, Andreas's avatar
Naumann, Andreas committed
380
381
382
    FUNCNAME("mv()");
    TEST_EXIT(false)("This function is not supported any more.\n");
#if 0
383
    int size = x.getNumVectors();
384
    int i;
385

386
387
388
    TEST_EXIT_DBG(size == result.getNumVectors())("incompatible sizes\n");
    TEST_EXIT_DBG(size == matrix.getNumRows())("incompatible sizes\n");
    TEST_EXIT_DBG(size == matrix.getNumCols())("incompatible sizes\n");
389
390

    for (i = 0; i < size; i++) {
Thomas Witkowski's avatar
Thomas Witkowski committed
391
392
393
      if (!add) 
	result.getDOFVector(i)->set(0.0);

394
395
      for (int j = 0; j < size; j++)
	if (matrix[i][j])
396
397
398
399
400
401
	  mv<double>(NoTranspose, 
		     *(matrix[i][j]), 
		     *(x.getDOFVector(j)), 
		     *(result.getDOFVector(i)),
		     true);
    }
Naumann, Andreas's avatar
Naumann, Andreas committed
402
#endif
403
  }
404

405
  /// y = a*x + y;
406
407
  inline void axpy(double a, SystemVector& x, SystemVector& y)
  {
408
409
    TEST_EXIT_DBG(x.getNumVectors() == y.getNumVectors())
          ("invalid size\n");
410

Thomas Witkowski's avatar
Thomas Witkowski committed
411
    int size = x.getNumVectors();
412
    int i;
Thomas Witkowski's avatar
Thomas Witkowski committed
413

414
    for (i = 0; i < size; i++)
415
      axpy(a, *(x.getDOFVector(i)), *(y.getDOFVector(i)));
416
  }
417

418
  /// y = x + a*y
419
420
  inline void xpay(double a, SystemVector& x, SystemVector& y)
  {
421
422
    TEST_EXIT_DBG(x.getNumVectors() == y.getNumVectors())
          ("invalid size\n");
Thomas Witkowski's avatar
Thomas Witkowski committed
423
    int size = x.getNumVectors();
424

425
    for (int i = 0; i < size; i++)
426
427
428
      xpay(a, *(x.getDOFVector(i)), *(y.getDOFVector(i)));
  }

429
430
431
  /// Returns SystemVector::getUsedSize().
  inline int size(SystemVector* vec) 
  {
432
    return vec->getUsedSize();
433
  }
434

435
436
  inline void print(SystemVector* vec) 
  {
437
    vec->print();
438
  }
439
440
441
442

}

#endif // AMDIS_SYSTEMVECTOR_H