MFEM  v3.4
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 {
21  for (int i = 0; i < numBlocks; ++i)
22  {
24  blockOffsets[i+1]-blockOffsets[i]);
25  }
26 }
27 
29  Vector(),
30  numBlocks(0),
31  blockOffsets(NULL),
32  blocks(NULL)
33 {
34 
35 }
36 
37 //! Standard constructor
39  Vector(bOffsets.Last()),
40  numBlocks(bOffsets.Size()-1),
41  blockOffsets(bOffsets.GetData())
42 {
43  blocks = new Vector[numBlocks];
44  SetBlocks();
45 }
46 
47 //! Copy constructor
49  Vector(v),
50  numBlocks(v.numBlocks),
51  blockOffsets(v.blockOffsets)
52 {
53  blocks = new Vector[numBlocks];
54  SetBlocks();
55 }
56 
57 //! View constructor
58 BlockVector::BlockVector(double *data, const Array<int> & bOffsets):
59  Vector(data, bOffsets.Last()),
60  numBlocks(bOffsets.Size()-1),
61  blockOffsets(bOffsets.GetData())
62 {
63  blocks = new Vector[numBlocks];
64  SetBlocks();
65 }
66 
67 void BlockVector::Update(double *data, const Array<int> & bOffsets)
68 {
69  NewDataAndSize(data, bOffsets.Last());
70  blockOffsets = bOffsets.GetData();
71  if (numBlocks != bOffsets.Size()-1)
72  {
73  delete [] blocks;
74  numBlocks = bOffsets.Size()-1;
75  blocks = new Vector[numBlocks];
76  }
77  SetBlocks();
78 }
79 
80 void BlockVector::Update(const Array<int> &bOffsets)
81 {
82  if (OwnsData())
83  {
84  // check if 'bOffsets' are the same as 'blockOffsets'
85  if (bOffsets.Size() == numBlocks+1)
86  {
87  if (bOffsets.GetData() == blockOffsets || numBlocks == 0) { return; }
88  for (int i = 0; true; i++)
89  {
90  if (blockOffsets[i] != bOffsets[i]) { break; }
91  if (i == numBlocks) { return; }
92  }
93  }
94  }
95  else
96  {
97  Destroy();
98  }
99  SetSize(bOffsets.Last());
100  blockOffsets = bOffsets.GetData();
101  if (numBlocks != bOffsets.Size()-1)
102  {
103  delete [] blocks;
104  numBlocks = bOffsets.Size()-1;
105  blocks = new Vector[numBlocks];
106  }
107  SetBlocks();
108 }
109 
111 {
112  if (numBlocks!=original.numBlocks)
113  {
114  mfem_error("Number of Blocks don't match in BlockVector::operator=");
115  }
116 
117  for (int i(0); i <= numBlocks; ++i)
118  if (blockOffsets[i]!=original.blockOffsets[i])
119  {
120  mfem_error("Size of Blocks don't match in BlockVector::operator=");
121  }
122 
123  Vector::operator=(original.GetData());
124 
125  return *this;
126 }
127 
129 {
130  Vector::operator=(val);
131  return *this;
132 }
133 
134 //! Destructor
136 {
137  delete [] blocks;
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:133
void NewDataAndSize(double *d, int s)
Set the Vector data and size, deleting the old data, if owned.
Definition: vector.hpp:108
void GetBlockView(int i, Vector &blockView)
Get the i-th vector in the block.
void SetSize(int s)
Resize the vector to size s.
Definition: vector.hpp:328
BlockVector()
empty constructor
Definition: blockvector.cpp:28
const int * blockOffsets
Offset for each block start. (length numBlocks+1)
Definition: blockvector.hpp:39
T * GetData()
Returns the data.
Definition: array.hpp:115
void Update(double *data, const Array< int > &bOffsets)
Update method.
Definition: blockvector.cpp:67
double * GetData() const
Return a pointer to the beginning of the Vector data.
Definition: vector.hpp:129
Vector & operator=(const double *v)
Copy Size() entries from v.
Definition: vector.cpp:116
bool OwnsData() const
Definition: vector.hpp:141
void mfem_error(const char *msg)
Function called when an error is encountered. Used by the macros MFEM_ABORT, MFEM_ASSERT, MFEM_VERIFY.
Definition: error.cpp:146
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.
void Destroy()
Destroy a vector.
Definition: vector.hpp:347
T & Last()
Return the last element in the array.
Definition: array.hpp:647
Vector data type.
Definition: vector.hpp:48
double * data
Definition: vector.hpp:53
Vector * blocks
array of Vector objects used to extract blocks without allocating memory.
Definition: blockvector.hpp:41