SystemVector.h 10.8 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
30
31
32

/** \file SystemVector.h */

#ifndef AMDIS_SYSTEMVECTOR_H
#define AMDIS_SYSTEMVECTOR_H

#include "MatrixVector.h"
#include "DOFVector.h"
#include "CreatorInterface.h"
#include "Serializable.h"

namespace AMDiS {

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

46
    /// Copy Constructor.
47
    SystemVector(const SystemVector& rhs)
48
49
      : name(rhs.getName()),
	feSpace(rhs.getFeSpaces()),
Thomas Witkowski's avatar
Thomas Witkowski committed
50
	vectors(rhs.getNumVectors())
51
    {
52
      for (unsigned int i = 0; i < vectors.size(); i++)
53
	vectors[i] = new DOFVector<double>(*rhs.getDOFVector(i));
54
55
    }

56
57
    ~SystemVector() 
    {
58
      for (unsigned int i = 0; i < vectors.size(); i++)
Thomas Witkowski's avatar
Thomas Witkowski committed
59
	delete vectors[i];
60
    }
61

62
63
64
    /// Sets \ref vectors[index] = vec.
    inline void setDOFVector(int index, DOFVector<double> *vec) 
    {
65
66
      TEST_EXIT_DBG(index < static_cast<int>(vectors.size()))
	("Invalid index %d!\n", index);
67
      vectors[index] = vec;
68
    }
69

70
71
72
    /// Returns \ref vectors[index].
    inline DOFVector<double> *getDOFVector(int index) 
    {
73
74
      TEST_EXIT_DBG(index < static_cast<int>(vectors.size()))
	("Invalid index %d!\n", index);
75
      return vectors[index];
76
    }
77

78
79
80
    /// Returns \ref vectors[index].
    inline const DOFVector<double> *getDOFVector(int index) const 
    {
81
82
      TEST_EXIT_DBG(index < static_cast<int>(vectors.size()))
	("Invalid index %d!\n", index);
83
      return vectors[index];
84
    }
85

86
87
88
89
90
    inline std::string getName() const
    {
      return name;
    }

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

100
101
102
    /// Returns number of contained vectors.
    inline int getNumVectors() const 
    { 
103
      return static_cast<int>(vectors.size());
104
    }
105

106
107
    inline int getSize() const 
    {
108
      return static_cast<int>(vectors.size());
109
    }
110

111
    /// Returns the fe space for a given component.
112
    inline const FiniteElemSpace *getFeSpace(int i) const 
113
    { 
114
      return feSpace[i]; 
115
    }
116

117
    /// Returns the fe spaces for all components.
118
    inline std::vector<const FiniteElemSpace*> getFeSpaces() const 
119
    {
120
121
122
      return feSpace;
    }

123
124
125
126
    /// 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.
127
128
    inline double& operator[](int index) 
    {
129
130
131
      int localIndex = index;
      int vectorIndex = 0;

132
      while (localIndex >= vectors[vectorIndex]->getUsedSize()) {
133
134
135
136
	localIndex -= vectors[vectorIndex++]->getUsedSize();
      }

      return (*(vectors[vectorIndex]))[localIndex];
137
    }
138

139
140
141
    /// For const access.
    inline double operator[](int index) const 
    {
142
143
144
      int localIndex = index;
      int vectorIndex = 0;

145
      while (localIndex >= vectors[vectorIndex]->getUsedSize()) {
146
147
148
149
	localIndex -= vectors[vectorIndex++]->getUsedSize();
      }

      return (*(vectors[vectorIndex]))[localIndex];
150
    }
151

152
153
154
    /// Sets all entries in all vectors to value.
    inline void set(double value) 
    {
155
      for (unsigned int i = 0; i < vectors.size(); i++)
156
	vectors[i]->set(value);
157
    }
158

159
160
    inline void setCoarsenOperation(CoarsenOperation op) 
    { 
161
      for (int i = 0; i < static_cast<int>(vectors.size()); i++)
162
163
164
	vectors[i]->setCoarsenOperation(op); 
    }

165
166
167
    /// Sets all entries in all vectors to value.
    inline SystemVector& operator=(double value) 
    {
168
      for (int i = 0; i < static_cast<int>(vectors.size()); i++)
169
170
	(*(vectors[i])) = value;
      return *this;
171
    }
172

173
174
175
    /// Assignement operator.
    inline SystemVector& operator=(const SystemVector& rhs) 
    {
176
177
178
      TEST_EXIT_DBG(rhs.vectors.size() == vectors.size())("Invalied sizes!\n");

      for (int i = 0; i < static_cast<int>(vectors.size()); i++)
179
	(*(vectors[i])) = (*(rhs.getDOFVector(i)));
180

181
      return *this;
182
    }
183

184
    void serialize(std::ostream &out);
185

186
    void deserialize(std::istream &in);
187

188
189
    void copy(const SystemVector& rhs) 
    {
190
191
      int size = static_cast<int>(vectors.size());
      TEST_EXIT_DBG(size == rhs.getNumVectors())("Invalid sizes!\n");
192
      for (int i = 0; i < size; i++)
193
	vectors[i]->copy(*(const_cast<SystemVector&>(rhs).getDOFVector(i)));
194
    }
195

196
197
    void interpol(std::vector<AbstractFunction<double, WorldVector<double> >*> *f) 
    {
198
      for (unsigned int i = 0; i < vectors.size(); i++)
199
	vectors[i]->interpol((*f)[i]);
200
201
    }

202
203
204
    void interpol(SystemVector *v, double factor) 
    {
      for (int i = 0; i < v->getSize(); i++)
205
206
	vectors[i]->interpol(v->getDOFVector(i), factor);
    }
207

208
209
    void print() 
    {
210
      for (unsigned int i = 0; i < vectors.size(); i++)
211
	vectors[i]->print();
212
    }
213

214
215
    int calcMemoryUsage() 
    {
216
      int result = 0;
217
      for (unsigned int i = 0; i < vectors.size(); i++)
218
	result += vectors[i]->calcMemoryUsage();
219
220
      result += sizeof(SystemVector);

221
222
      return result;
    }
223

224
      
225
226

  protected:
227
    /// Name of the system vector
228
    std::string name;
229

230
    /// Finite element space.
231
    std::vector<const FiniteElemSpace*> feSpace;
232

233
    /// Local dof vectors.
234
    std::vector<DOFVector<double>*> vectors;
235
236
  };

237

238
239
240
  /// multiplication with scalar
  inline const SystemVector& operator*=(SystemVector& x, double d) 
  {
241
    int size = x.getNumVectors();
242
    for (int i = 0; i < size; i++)
243
244
      *(x.getDOFVector(i)) *= d;
    return x;
245
  }
246

247
248
249
250
  /// scalar product
  inline double operator*(SystemVector& x, SystemVector& y) 
  {
    TEST_EXIT_DBG(x.getNumVectors() == y.getNumVectors())("invalid size\n");
251
    double result = 0.0;
252
    int size = x.getNumVectors();
253
    for (int i = 0; i < size; i++)
254
255
      result += (*x.getDOFVector(i)) * (*y.getDOFVector(i));
    return result;
256
  }
257

258
259
  /// addition of two system vectors
  inline const SystemVector& operator+=(SystemVector& x, const SystemVector& y) 
260
  {
261
    TEST_EXIT_DBG(x.getNumVectors() == y.getNumVectors())("invalid size\n");
262
    int size = x.getNumVectors();
263
    for (int i = 0; i < size; i++)
264
265
      (*(x.getDOFVector(i))) += (*(y.getDOFVector(i)));
    return x;
266
  }
267

268
269
  /// subtraction of two system vectors.
  inline const SystemVector& operator-=(SystemVector& x, SystemVector& y) 
270
  {
271
    TEST_EXIT_DBG(x.getNumVectors() == y.getNumVectors())("invalid size\n");
272
    int size = x.getNumVectors();
273
    for (int i = 0; i < size; i++)
274
275
      (*(x.getDOFVector(i))) -= (*(y.getDOFVector(i)));
    return x;
276
  }
277

278
279
280
  /// multiplication with a scalar
  inline SystemVector operator*(SystemVector& x, double d) 
  {
281
    SystemVector result = x;
282
    int size = x.getNumVectors();
283
    for (int i = 0; i < size; i++)
284
285
      (*(result.getDOFVector(i))) *= d;
    return result;
286
  }
287

288
289
290
  /// multiplication with a scalar
  inline SystemVector operator*(double d, SystemVector& x) 
  {
291
    SystemVector result = x;
292
    int size = x.getNumVectors();
293
    for (int i = 0; i < size; i++)
294
295
      (*(result.getDOFVector(i))) *= d;
    return result;
296
  }
297

298
299
  /// addition of two system vectors
  inline SystemVector operator+(const SystemVector& x, const SystemVector& y)
300
  {
301
    TEST_EXIT_DBG(x.getNumVectors() == y.getNumVectors())("invalid size\n");
302
    SystemVector result = x;
303
    int size = x.getNumVectors();
304
    for (int i = 0; i < size; i++)
305
306
      (*(result.getDOFVector(i))) += (*(y.getDOFVector(i)));
    return result;
307
  }
308

309
310
311
  /// Calls SystemVector::set(). Used for solving.
  inline void set(SystemVector& x, double value) 
  {
312
    x.set(value);
313
  } 
314

315
316
317
  /// Calls SystemVector::set(). Used for solving.
  inline void setValue(SystemVector& x, double value) 
  {
318
    x.set(value);
319
  }
320

321
322
323
  /// Norm of system vector.
  inline double norm(SystemVector* x) 
  {
324
    double result = 0.0;
325
    int size = x->getNumVectors();
326
    for (int i = 0; i < size; i++)
327
328
      result += x->getDOFVector(i)->squareNrm2();
    return sqrt(result);
329
  }
330

331
332
333
  /// L2 norm of system vector.
  inline double L2Norm(SystemVector* x) 
  {
334
    double result = 0.0;
335
    int size = x->getNumVectors();
336
    for (int i = 0; i < size; i++)
337
338
      result += x->getDOFVector(i)->L2NormSquare();
    return sqrt(result);
339
  }
340

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

  inline void mv(Matrix<DOFMatrix*> &matrix,
		 const SystemVector &x,
		 SystemVector       &result,
		 bool               add = false)
  {
Naumann, Andreas's avatar
Naumann, Andreas committed
356
    FUNCNAME("mv()");
357

358
    int size = x.getNumVectors();
359
    int i;
360

361
362
363
    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");
364
365

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

369
370
      for (int j = 0; j < size; j++)
	if (matrix[i][j])
371
372
373
374
375
376
	  mv<double>(NoTranspose, 
		     *(matrix[i][j]), 
		     *(x.getDOFVector(j)), 
		     *(result.getDOFVector(i)),
		     true);
    }
377
  }
378

379
  /// y = a*x + y;
380
381
  inline void axpy(double a, SystemVector& x, SystemVector& y)
  {
382
383
    TEST_EXIT_DBG(x.getNumVectors() == y.getNumVectors())
          ("invalid size\n");
384

Thomas Witkowski's avatar
Thomas Witkowski committed
385
    int size = x.getNumVectors();
386
    int i;
Thomas Witkowski's avatar
Thomas Witkowski committed
387

388
    for (i = 0; i < size; i++)
389
      axpy(a, *(x.getDOFVector(i)), *(y.getDOFVector(i)));
390
  }
391

392
  /// y = x + a*y
393
394
  inline void xpay(double a, SystemVector& x, SystemVector& y)
  {
395
396
    TEST_EXIT_DBG(x.getNumVectors() == y.getNumVectors())
          ("invalid size\n");
Thomas Witkowski's avatar
Thomas Witkowski committed
397
    int size = x.getNumVectors();
398

399
    for (int i = 0; i < size; i++)
400
401
402
      xpay(a, *(x.getDOFVector(i)), *(y.getDOFVector(i)));
  }

403
404
405
  /// Returns SystemVector::getUsedSize().
  inline int size(SystemVector* vec) 
  {
406
    return vec->getUsedSize();
407
  }
408

409
410
  inline void print(SystemVector* vec) 
  {
411
    vec->print();
412
  }
413
414
415
416

}

#endif // AMDIS_SYSTEMVECTOR_H