MFEM  v3.3
Finite element discretization library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
blockvector.cpp
Go to the documentation of this file.
1 // Copyright (c) 2010, Lawrence Livermore National Security, LLC. Produced at
2 // the Lawrence Livermore National Laboratory. LLNL-CODE-443211. All Rights
3 // reserved. See file COPYRIGHT for details.
4 //
5 // This file is part of the MFEM library. For more information and source code
6 // availability see http://mfem.org.
7 //
8 // MFEM is free software; you can redistribute it and/or modify it under the
9 // terms of the GNU Lesser General Public License (as published by the Free
10 // Software Foundation) version 2.1 dated February 1999.
11 
12 #include "../general/array.hpp"
13 #include "vector.hpp"
14 #include "blockvector.hpp"
15 
16 namespace mfem
17 {
18 
20  Vector(),
21  numBlocks(0),
22  blockOffsets(NULL),
23  tmp_block(0)
24 {
25 
26 }
27 
28 //! Standard constructor
30  Vector(bOffsets.Last()),
31  numBlocks(bOffsets.Size()-1),
32  blockOffsets(bOffsets.GetData()),
33  tmp_block(numBlocks)
34 {
35  for (int i = 0; i < numBlocks; ++i)
36  {
37  tmp_block[i] = new Vector(data+blockOffsets[i],
38  blockOffsets[i+1]-blockOffsets[i]);
39  }
40 }
41 
42 //! Copy constructor
44  Vector(v),
45  numBlocks(v.numBlocks),
46  blockOffsets(v.blockOffsets),
47  tmp_block(numBlocks)
48 {
49  for (int i = 0; i < numBlocks; ++i)
50  {
51  tmp_block[i] = new Vector(data+blockOffsets[i],
52  blockOffsets[i+1]-blockOffsets[i]);
53  }
54 }
55 
56 //! View constructor
57 BlockVector::BlockVector(double *data, const Array<int> & bOffsets):
58  Vector(data, bOffsets.Last()),
59  numBlocks(bOffsets.Size()-1),
60  blockOffsets(bOffsets.GetData()),
61  tmp_block(numBlocks)
62 {
63  for (int i = 0; i < numBlocks; ++i)
64  {
65  tmp_block[i] = new Vector(data+blockOffsets[i],
66  blockOffsets[i+1]-blockOffsets[i]);
67  }
68 }
69 
70 void BlockVector::Update(double *data, const Array<int> & bOffsets)
71 {
72  NewDataAndSize(data, bOffsets.Last());
73  blockOffsets = bOffsets.GetData();
74  numBlocks = bOffsets.Size()-1;
75 
76  int oldNumBlocks = tmp_block.Size();
77  for (int i = numBlocks; i < oldNumBlocks; ++i)
78  {
79  delete tmp_block[i];
80  }
81 
82  tmp_block.SetSize(numBlocks);
83  for (int i = oldNumBlocks; i < numBlocks; ++i)
84  {
85  tmp_block[i] = new Vector(data+blockOffsets[i],
86  blockOffsets[i+1]-blockOffsets[i]);
87  }
88 }
89 
91 {
92  if (numBlocks!=original.numBlocks)
93  {
94  mfem_error("Number of Blocks don't match in BlockVector::operator=");
95  }
96 
97  for (int i(0); i <= numBlocks; ++i)
98  if (blockOffsets[i]!=original.blockOffsets[i])
99  {
100  mfem_error("Size of Blocks don't match in BlockVector::operator=");
101  }
102 
103  for (int i = 0; i < original.size; i++)
104  {
105  data[i] = original.data[i];
106  }
107 
108  return *this;
109 }
110 
112 {
113  Vector::operator=(val);
114  return *this;
115 }
116 
117 //! Destructor
119 {
120  for (int i = 0; i < tmp_block.Size(); ++i)
121  {
122  delete tmp_block[i];
123  }
124 }
125 
127 {
128  tmp_block[i]->NewDataAndSize(data+blockOffsets[i],
129  blockOffsets[i+1]-blockOffsets[i]);
130  return *(tmp_block[i]);
131 }
132 
133 const Vector & BlockVector::GetBlock(int i) const
134 {
135  tmp_block[i]->NewDataAndSize(data+blockOffsets[i],
136  blockOffsets[i+1]-blockOffsets[i]);
137  return *(tmp_block[i]);
138 }
139 
140 void BlockVector::GetBlockView(int i, Vector & blockView)
141 {
142  blockView.NewDataAndSize(data+blockOffsets[i],
143  blockOffsets[i+1]-blockOffsets[i]);
144 }
145 
146 }
~BlockVector()
Destructor.
int Size() const
Logical size of the array.
Definition: array.hpp:109
Vector()
Default constructor for Vector. Sets size = 0 and data = NULL.
Definition: vector.hpp:46
void NewDataAndSize(double *d, int s)
Set the Vector data and size, deleting the old data, if owned.
Definition: vector.hpp:94
void GetBlockView(int i, Vector &blockView)
Get the i-th vector in the block.
BlockVector()
empty constructor
Definition: blockvector.cpp:19
const int * blockOffsets
Offset for each block start. (length numBlocks+1)
Definition: blockvector.hpp:39
T * GetData()
Returns the data.
Definition: array.hpp:91
void Update(double *data, const Array< int > &bOffsets)
Update method.
Definition: blockvector.cpp:70
Vector & operator=(const double *v)
Definition: vector.cpp:117
Array< Vector * > tmp_block
array of Vector objects used to extract blocks without allocating memory.
Definition: blockvector.hpp:41
int numBlocks
Number of blocks in the blockVector.
Definition: blockvector.hpp:34
BlockVector & operator=(const BlockVector &original)
Assignment operator. this and original must have the same block structure.
Definition: blockvector.cpp:90
void mfem_error(const char *msg)
Definition: error.cpp:106
T & Last()
Return the last element in the array.
Definition: array.hpp:429
Vector data type.
Definition: vector.hpp:36
double * data
Definition: vector.hpp:41
Vector & GetBlock(int i)
Get the i-th vector in the block.