queso-0.51.1
VectorSpace.C
Go to the documentation of this file.
1 //-----------------------------------------------------------------------bl-
2 //--------------------------------------------------------------------------
3 //
4 // QUESO - a library to support the Quantification of Uncertainty
5 // for Estimation, Simulation and Optimization
6 //
7 // Copyright (C) 2008,2009,2010,2011,2012,2013 The PECOS Development Team
8 //
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the Version 2.1 GNU Lesser General
11 // Public License as published by the Free Software Foundation.
12 //
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // Lesser General Public License for more details.
17 //
18 // You should have received a copy of the GNU Lesser General Public
19 // License along with this library; if not, write to the Free Software
20 // Foundation, Inc. 51 Franklin Street, Fifth Floor,
21 // Boston, MA 02110-1301 USA
22 //
23 //-----------------------------------------------------------------------el-
24 
25 #include <queso/VectorSpace.h>
26 #include <queso/GslVector.h>
27 #include <queso/GslMatrix.h>
28 #include <queso/TeuchosVector.h>
29 #include <queso/TeuchosMatrix.h>
30 #include <queso/DistArray.h>
31 #include <queso/Map.h>
32 #include <cmath>
33 
34 namespace QUESO {
35 
36 // Default constructor
37 template <class V, class M>
39  : VectorSet<V,M>()
40 {
42  m_env.worldRank(),
43  "VectorSpace<V,M>::constructor(), default",
44  "should not be used by user");
45 }
46 
47 // Shaped constructor
48 template <class V, class M>
49 VectorSpace<V,M>::VectorSpace(const BaseEnvironment& env, const char* prefix,
50  unsigned int dimGlobalValue,
51  const std::vector<std::string>* componentsNamesVec)
52  : VectorSet<V,M> (env,((std::string)(prefix) + "space_").c_str(),INFINITY),
53  m_dimGlobal(dimGlobalValue),
54  m_map(newMap()),
55  m_dimLocal(m_map->NumMyElements()),
56  m_componentsNamesArray(NULL),
57  m_componentsNamesVec(NULL),
58  m_emptyComponentName(""),
59  m_zeroVector(new V(m_env,*m_map))
60 {
61  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 5)) {
62  *m_env.subDisplayFile() << "Entering VectorSpace<V,M>::constructor(1)"
63  << ", with m_prefix = " << m_prefix
64  << "\n m_zeroVector->sizeGlobal() = " << m_zeroVector->sizeGlobal()
65  << "\n m_dimGlobal = " << m_dimGlobal
66  << "\n m_zeroVector->sizeLocal() = " << m_zeroVector->sizeLocal()
67  << "\n m_dimLocal = " << m_dimLocal
68  << "\n m_map->NumGlobalElements() = " << m_map->NumGlobalElements()
69  << "\n componentsNamesVec = " << componentsNamesVec
70  << std::endl;
71  }
72 
73  if (m_zeroVector->sizeGlobal() != m_dimGlobal) {
74  std::cerr << "In VectorSpace<V,M>::constructor(1)"
75  << ", with m_prefix = " << m_prefix
76  << ": m_zeroVector->sizeGlobal() = " << m_zeroVector->sizeGlobal()
77  << ", m_dimGlobal = " << m_dimGlobal
78  << std::endl;
79  }
80  UQ_FATAL_TEST_MACRO((m_zeroVector->sizeGlobal() != m_dimGlobal),
81  m_env.worldRank(),
82  "VectorSpace<V,M>::constructor(1)",
83  "global size of 'm_zeroVector' is not equal to m_dimGlobal");
84 
85  if (m_zeroVector->sizeLocal() != m_dimLocal) {
86  std::cerr << "In VectorSpace<V,M>::constructor(1)"
87  << ", with m_prefix = " << m_prefix
88  << ": m_zeroVector->sizeLocal() = " << m_zeroVector->sizeLocal()
89  << ", m_dimLocal = " << m_dimLocal
90  << std::endl;
91  }
93  m_env.worldRank(),
94  "VectorSpace<V,M>::constructor(1)",
95  "local size of 'm_zeroVector' is not equal to m_dimLocal");
96 
97  if (componentsNamesVec != NULL) {
98  UQ_FATAL_TEST_MACRO((componentsNamesVec->size() != (size_t) m_dimGlobal),
99  m_env.worldRank(),
100  "VectorSpace<V,M>::constructor(1)",
101  "global size of 'componentsNames' is not equal to m_dimGlobal");
102 
104  unsigned int myFirstId = this->globalIdOfFirstComponent();
105  for (unsigned int i = 0; i < m_dimLocal; ++i) {
106  (*m_componentsNamesArray)(i,0) = (*componentsNamesVec)[myFirstId+i];
107  }
108 
109  UQ_FATAL_TEST_MACRO((m_componentsNamesArray->GlobalLength() != (int) m_dimGlobal),
110  m_env.worldRank(),
111  "VectorSpace<V,M>::constructor(1)",
112  "global size of 'm_componentsNamesArray' is not equal to m_dimGlobal");
113  UQ_FATAL_TEST_MACRO((m_componentsNamesArray->MyLength() != (int) m_dimLocal),
114  m_env.worldRank(),
115  "VectorSpace<V,M>::constructor(1)",
116  "local size of 'm_componentsNamesArray' is not equal to m_dimLocal");
117  }
118 
119  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 5)) {
120  *m_env.subDisplayFile() << "Leaving VectorSpace<V,M>::constructor(1)"
121  << ", with m_prefix = " << m_prefix
122  << std::endl;
123  }
124 }
125 
126 // Copy constructor
127 template <class V, class M>
129  : VectorSet<V,M>(aux.env(),((std::string)(aux.m_prefix)).c_str(),INFINITY),
130  m_dimGlobal(aux.m_dimGlobal),
131  m_map(newMap()),
132  m_dimLocal(m_map->NumMyElements()),
133  m_componentsNamesArray(NULL),
134  m_componentsNamesVec(NULL),
135  m_emptyComponentName(""),
136  m_zeroVector(new V(m_env,*m_map))
137 {
138  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 5)) {
139  *m_env.subDisplayFile() << "Entering VectorSpace<V,M>::constructor(2)"
140  << ": aux.m_componentsNamesArray = " << aux.m_componentsNamesArray
141  << ", aux.m_componentsNamesVec = " << aux.m_componentsNamesVec
142  << std::endl;
143  }
144 
145  if (aux.m_componentsNamesArray != NULL) {
147  }
148 
149  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 5)) {
150  *m_env.subDisplayFile() << "Leaving VectorSpace<V,M>::constructor(2)"
151  << std::endl;
152  }
153 }
154 
155 // Destructor
156 template <class V, class M>
158 {
159  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 5)) {
160  *m_env.subDisplayFile() << "Entering VectorSpace<V,M>::destructor()"
161  << std::endl;
162  }
163 
164  if (m_zeroVector != NULL) delete m_zeroVector;
165  if (m_componentsNamesVec != NULL) delete m_componentsNamesVec;
166  if (m_componentsNamesArray != NULL) delete m_componentsNamesArray;
167  if (m_map != NULL) delete m_map;
168 
169  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 5)) {
170  *m_env.subDisplayFile() << "Leaving VectorSpace<V,M>::destructor()"
171  << std::endl;
172  }
173 }
174 
175 // Attribute methods
176 template <class V, class M>
178 {
179  return m_env;
180 }
181 
182 template <class V, class M>
184 {
185  UQ_FATAL_TEST_MACRO(m_map == NULL,
186  m_env.worldRank(),
187  "VectorSpace<V,M>::map()",
188  "m_map is still NULL");
189  return *m_map;
190 }
191 
192 template <class V, class M>
194 {
195  return m_map->Comm().NumProc();
196 }
197 
198 template <class V, class M>
199 unsigned int VectorSpace<V,M>::dimLocal() const
200 {
201  return m_dimLocal;
202 }
203 
204 template <class V, class M>
205 unsigned int VectorSpace<V,M>::dimGlobal() const
206 {
207  return m_dimGlobal;
208 }
209 
210 template <class V, class M>
212 {
213  return m_map->MinMyGID();
214 }
215 
216 
217 template<class V, class M>
219 {
220  UQ_FATAL_TEST_MACRO(m_zeroVector == NULL,
221  m_env.worldRank(),
222  "VectorSpace<V,M>::zeroVector()",
223  "m_zeroVector is still NULL");
224  return *m_zeroVector;
225 }
226 
227 template <class V, class M>
228 V* VectorSpace<V,M>::newVector(const V& v) const
229 {
230  if (v.sizeGlobal() != m_dimGlobal) return NULL;
231  if (v.sizeLocal () != m_dimLocal ) return NULL;
232 
233  return new V(v);
234 }
235 
236 template <class V, class M>
238 {
239  if (v.sizeGlobal() != m_dimGlobal) return NULL;
240  if (v.sizeLocal () != m_dimLocal ) return NULL;
241 
242  return new M(v);
243 }
244 
245 template <class V, class M>
246 M* VectorSpace<V,M>::newProposalMatrix(const V* varVec, const V* auxVec) const
247 {
248  V tmpVec(*m_zeroVector);
249  for (unsigned int i = 0; i < m_dimLocal; ++i) {
250  double variance = INFINITY;
251  if (varVec) variance = (*varVec)[i];
252  if (m_env.subDisplayFile()) {
253  *m_env.subDisplayFile() << "In VectorSpace<V,M>::newProposalMatrix()"
254  << ": i = " << i
255  << ", variance = " << variance
256  << std::endl;
257  }
258  if ((variance == INFINITY) ||
259  (variance == NAN )) {
260  if (auxVec) {
261  tmpVec[i] = std::pow( fabs((*auxVec)[i])*0.05,2. );
262  if ((tmpVec[i] == 0. ) ||
263  (tmpVec[i] == INFINITY) ||
264  (tmpVec[i] == NAN )) {
265  tmpVec[i] = 1.;
266  }
267  }
268  else {
269  tmpVec[i] = 1.;
270  }
271  }
272  else if (variance == 0.) {
273  tmpVec[i] = 1.;
274  }
275  else {
276  tmpVec[i] = variance;
277  }
278  }
279 
280  return newDiagMatrix(tmpVec);
281 }
282 
283 template <class V, class M>
285 {
286  return *this;
287 }
288 
289 template <class V, class M>
290 bool VectorSpace<V,M>::contains(const V& vec) const
291 {
292  if (vec[0]) {}; // just to remove compiler warning
293  return true;
294 }
295 
296 template <class V, class M>
298 {
299  return m_componentsNamesArray;
300 }
301 
302 template <class V, class M>
304  unsigned int localComponentId) const
305 {
306  if (m_componentsNamesArray == NULL) return m_emptyComponentName;
307 
308  UQ_FATAL_TEST_MACRO(localComponentId > m_dimLocal,
309  m_env.worldRank(),
310  "VectorSpace<V,M>::localComponentName()",
311  "localComponentId is too big");
312 
313 //return (*(const_cast<DistArray<std::string>*>(m_componentsNamesArray)))(localComponentId,0);
314  return (*m_componentsNamesArray)(localComponentId,0);
315 }
316 
317 template<class V, class M>
319  bool printHorizontally) const
320 {
321  if (printHorizontally) {
322  for (unsigned int i = 0; i < this->dimLocal(); ++i) {
323  os << "'" << this->localComponentName(i) << "'"
324  << " ";
325  }
326  }
327  else {
328  for (unsigned int i = 0; i < this->dimLocal(); ++i) {
329  os << "'" << this->localComponentName(i) << "'"
330  << std::endl;
331  }
332  }
333 
334  return;
335 }
336 
337 template <class V, class M>
338 void VectorSpace<V,M>::print(std::ostream& os) const
339 {
340  os << "In VectorSpace<V,M>::print()"
341  << ": nothing to be printed" << std::endl;
342  return;
343 }
344 
345 } // End namespace QUESO
346 
348 #ifdef QUESO_HAS_TRILINOS
350 #endif
unsigned int dimLocal() const
Definition: VectorSpace.C:199
DistArray< std::string > * m_componentsNamesArray
Array of strings of the type DistArray to store the names of the components.
Definition: VectorSpace.h:156
const DistArray< std::string > * componentsNamesArray() const
Access to private attribute m_componentsNamesArray, which is an instance of DistArray.
Definition: VectorSpace.C:297
A class for partitioning vectors and matrices.
Definition: Map.h:49
A templated class for handling sets.
Definition: VectorSet.h:49
unsigned int globalIdOfFirstComponent() const
Definition: VectorSpace.C:211
const BaseEnvironment & env() const
Environment.
Definition: VectorSpace.C:177
int worldRank() const
Returns the process world rank.
Definition: Environment.C:235
std::ofstream * subDisplayFile() const
Access function for m_subDisplayFile (displays file on stream).
Definition: Environment.C:305
void printComponentsNames(std::ostream &os, bool printHorizontally) const
Prints the local component names.
Definition: VectorSpace.C:318
This (virtual) class sets up the environment underlying the use of the QUESO library by an executable...
Definition: Environment.h:187
unsigned int m_dimGlobal
Global dimension.
Definition: VectorSpace.h:147
void print(std::ostream &os) const
Prints only a message.
Definition: VectorSpace.C:338
const VectorSpace< V, M > & vectorSpace() const
Accessor method to this. Vector space to which this vector set belongs to.
Definition: VectorSpace.C:284
V * m_zeroVector
A vector of all elements equal to zero.
Definition: VectorSpace.h:165
const BaseEnvironment & m_env
Definition: VectorSet.h:99
const std::string & localComponentName(unsigned int localComponentId) const
Returns the local component names.
Definition: VectorSpace.C:303
unsigned int dimGlobal() const
Definition: VectorSpace.C:205
const Map & map() const
Map.
Definition: VectorSpace.C:183
V * newVector() const
Creates an empty vector of size given by Map&amp; map. See template specialization.
M * newProposalMatrix(const V *varVec, const V *auxVec) const
Creates a diagonal matrix conditionally to values from vector varVec, guaranteeing that its values ar...
Definition: VectorSpace.C:246
VectorSpace()
Default constructor.
Definition: VectorSpace.C:38
std::string m_prefix
Definition: VectorSet.h:100
A class representing a vector space.
Definition: VectorSet.h:46
M * newDiagMatrix(const V &v) const
Creates a diagonal matrix with the elements and size of vector v.
Definition: VectorSpace.C:237
DistArray< std::string > * m_componentsNamesVec
Vector of strings of the type DistArray to store the names of the components.
Definition: VectorSpace.h:159
int NumGlobalElements() const
Returns the total number of elements across all processors.
Definition: Map.C:96
unsigned int m_dimLocal
Local dimension (number of elements owned by the calling processor.).
Definition: VectorSpace.h:153
const V & zeroVector() const
Returns a vector filled with zeros.
Definition: VectorSpace.C:218
bool contains(const V &vec) const
Whether vector contains vector vec.
Definition: VectorSpace.C:290
unsigned int displayVerbosity() const
Definition: Environment.C:436
unsigned int numOfProcsForStorage() const
Returns total number of processes.
Definition: VectorSpace.C:193
#define UQ_FATAL_TEST_MACRO(test, givenRank, where, what)
Definition: Defines.h:223
const Map * m_map
Map.
Definition: VectorSpace.h:150
~VectorSpace()
Destructor.
Definition: VectorSpace.C:157

Generated on Thu Apr 23 2015 19:26:15 for queso-0.51.1 by  doxygen 1.8.5