queso-0.53.0
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-2015 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 // Shaped constructor
37 template <class V, class M>
38 VectorSpace<V,M>::VectorSpace(const BaseEnvironment& env, const char* prefix,
39  unsigned int dimGlobalValue,
40  const std::vector<std::string>* componentsNamesVec)
41  : VectorSet<V,M> (env,((std::string)(prefix) + "space_").c_str(),INFINITY),
42  m_dimGlobal(dimGlobalValue),
43  m_map(newMap()),
44  m_dimLocal(m_map->NumMyElements()),
45  m_componentsNamesArray(NULL),
46  m_componentsNamesVec(NULL),
47  m_emptyComponentName(""),
48  m_zeroVector(new V(m_env,*m_map))
49 {
50  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 5)) {
51  *m_env.subDisplayFile() << "Entering VectorSpace<V,M>::constructor(1)"
52  << ", with m_prefix = " << m_prefix
53  << "\n m_zeroVector->sizeGlobal() = " << m_zeroVector->sizeGlobal()
54  << "\n m_dimGlobal = " << m_dimGlobal
55  << "\n m_zeroVector->sizeLocal() = " << m_zeroVector->sizeLocal()
56  << "\n m_dimLocal = " << m_dimLocal
57  << "\n m_map->NumGlobalElements() = " << m_map->NumGlobalElements()
58  << "\n componentsNamesVec = " << componentsNamesVec
59  << std::endl;
60  }
61 
62  if (m_zeroVector->sizeGlobal() != m_dimGlobal) {
63  std::cerr << "In VectorSpace<V,M>::constructor(1)"
64  << ", with m_prefix = " << m_prefix
65  << ": m_zeroVector->sizeGlobal() = " << m_zeroVector->sizeGlobal()
66  << ", m_dimGlobal = " << m_dimGlobal
67  << std::endl;
68  }
69  queso_require_equal_to_msg(m_zeroVector->sizeGlobal(), m_dimGlobal, "global size of 'm_zeroVector' is not equal to m_dimGlobal");
70 
71  if (m_zeroVector->sizeLocal() != m_dimLocal) {
72  std::cerr << "In VectorSpace<V,M>::constructor(1)"
73  << ", with m_prefix = " << m_prefix
74  << ": m_zeroVector->sizeLocal() = " << m_zeroVector->sizeLocal()
75  << ", m_dimLocal = " << m_dimLocal
76  << std::endl;
77  }
78  queso_require_equal_to_msg(m_zeroVector->sizeLocal(), m_dimLocal, "local size of 'm_zeroVector' is not equal to m_dimLocal");
79 
80  if (componentsNamesVec != NULL) {
81  queso_require_equal_to_msg(componentsNamesVec->size(), (size_t) m_dimGlobal, "global size of 'componentsNames' is not equal to m_dimGlobal");
82 
84  unsigned int myFirstId = this->globalIdOfFirstComponent();
85  for (unsigned int i = 0; i < m_dimLocal; ++i) {
86  (*m_componentsNamesArray)(i,0) = (*componentsNamesVec)[myFirstId+i];
87  }
88 
89  queso_require_equal_to_msg(m_componentsNamesArray->GlobalLength(), (int) m_dimGlobal, "global size of 'm_componentsNamesArray' is not equal to m_dimGlobal");
90  queso_require_equal_to_msg(m_componentsNamesArray->MyLength(), (int) m_dimLocal, "local size of 'm_componentsNamesArray' is not equal to m_dimLocal");
91  }
92 
93  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 5)) {
94  *m_env.subDisplayFile() << "Leaving VectorSpace<V,M>::constructor(1)"
95  << ", with m_prefix = " << m_prefix
96  << std::endl;
97  }
98 }
99 
100 // Copy constructor
101 template <class V, class M>
103  : VectorSet<V,M>(aux.env(),((std::string)(aux.m_prefix)).c_str(),INFINITY),
104  m_dimGlobal(aux.m_dimGlobal),
105  m_map(newMap()),
106  m_dimLocal(m_map->NumMyElements()),
107  m_componentsNamesArray(NULL),
108  m_componentsNamesVec(NULL),
109  m_emptyComponentName(""),
110  m_zeroVector(new V(m_env,*m_map))
111 {
112  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 5)) {
113  *m_env.subDisplayFile() << "Entering VectorSpace<V,M>::constructor(2)"
114  << ": aux.m_componentsNamesArray = " << aux.m_componentsNamesArray
115  << ", aux.m_componentsNamesVec = " << aux.m_componentsNamesVec
116  << std::endl;
117  }
118 
119  if (aux.m_componentsNamesArray != NULL) {
121  }
122 
123  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 5)) {
124  *m_env.subDisplayFile() << "Leaving VectorSpace<V,M>::constructor(2)"
125  << std::endl;
126  }
127 }
128 
129 // Destructor
130 template <class V, class M>
132 {
133  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 5)) {
134  *m_env.subDisplayFile() << "Entering VectorSpace<V,M>::destructor()"
135  << std::endl;
136  }
137 
138  if (m_zeroVector != NULL) delete m_zeroVector;
139  if (m_componentsNamesVec != NULL) delete m_componentsNamesVec;
140  if (m_componentsNamesArray != NULL) delete m_componentsNamesArray;
141  if (m_map != NULL) delete m_map;
142 
143  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 5)) {
144  *m_env.subDisplayFile() << "Leaving VectorSpace<V,M>::destructor()"
145  << std::endl;
146  }
147 }
148 
149 // Attribute methods
150 template <class V, class M>
152 {
153  return m_env;
154 }
155 
156 template <class V, class M>
158 {
159  queso_require_msg(m_map, "m_map is still NULL");
160  return *m_map;
161 }
162 
163 template <class V, class M>
165 {
166  return m_map->Comm().NumProc();
167 }
168 
169 template <class V, class M>
170 unsigned int VectorSpace<V,M>::dimLocal() const
171 {
172  return m_dimLocal;
173 }
174 
175 template <class V, class M>
176 unsigned int VectorSpace<V,M>::dimGlobal() const
177 {
178  return m_dimGlobal;
179 }
180 
181 template <class V, class M>
183 {
184  return m_map->MinMyGID();
185 }
186 
187 
188 template<class V, class M>
190 {
191  queso_require_msg(m_zeroVector, "m_zeroVector is still NULL");
192  return *m_zeroVector;
193 }
194 
195 template <class V, class M>
196 V* VectorSpace<V,M>::newVector(const V& v) const
197 {
198  if (v.sizeGlobal() != m_dimGlobal) return NULL;
199  if (v.sizeLocal () != m_dimLocal ) return NULL;
200 
201  return new V(v);
202 }
203 
204 template <class V, class M>
206 {
207  if (v.sizeGlobal() != m_dimGlobal) return NULL;
208  if (v.sizeLocal () != m_dimLocal ) return NULL;
209 
210  return new M(v);
211 }
212 
213 template <class V, class M>
214 M* VectorSpace<V,M>::newProposalMatrix(const V* varVec, const V* auxVec) const
215 {
216  V tmpVec(*m_zeroVector);
217  for (unsigned int i = 0; i < m_dimLocal; ++i) {
218  double variance = INFINITY;
219  if (varVec) variance = (*varVec)[i];
220  if (m_env.subDisplayFile()) {
221  *m_env.subDisplayFile() << "In VectorSpace<V,M>::newProposalMatrix()"
222  << ": i = " << i
223  << ", variance = " << variance
224  << std::endl;
225  }
226  if ((variance == INFINITY) ||
227  (variance == NAN )) {
228  if (auxVec) {
229  tmpVec[i] = std::pow( fabs((*auxVec)[i])*0.05,2. );
230  if ((tmpVec[i] == 0. ) ||
231  (tmpVec[i] == INFINITY) ||
232  (tmpVec[i] == NAN )) {
233  tmpVec[i] = 1.;
234  }
235  }
236  else {
237  tmpVec[i] = 1.;
238  }
239  }
240  else if (variance == 0.) {
241  tmpVec[i] = 1.;
242  }
243  else {
244  tmpVec[i] = variance;
245  }
246  }
247 
248  return newDiagMatrix(tmpVec);
249 }
250 
251 template <class V, class M>
253 {
254  return *this;
255 }
256 
257 template <class V, class M>
258 bool VectorSpace<V,M>::contains(const V& vec) const
259 {
260  if (vec[0]) {}; // just to remove compiler warning
261  return true;
262 }
263 
264 template <class V, class M>
266 {
267  return m_componentsNamesArray;
268 }
269 
270 template <class V, class M>
272  unsigned int localComponentId) const
273 {
274  if (m_componentsNamesArray == NULL) return m_emptyComponentName;
275 
276  queso_require_less_equal_msg(localComponentId, m_dimLocal, "localComponentId is too big");
277 
278 //return (*(const_cast<DistArray<std::string>*>(m_componentsNamesArray)))(localComponentId,0);
279  return (*m_componentsNamesArray)(localComponentId,0);
280 }
281 
282 template<class V, class M>
284  bool printHorizontally) const
285 {
286  if (printHorizontally) {
287  for (unsigned int i = 0; i < this->dimLocal(); ++i) {
288  os << "'" << this->localComponentName(i) << "'"
289  << " ";
290  }
291  }
292  else {
293  for (unsigned int i = 0; i < this->dimLocal(); ++i) {
294  os << "'" << this->localComponentName(i) << "'"
295  << std::endl;
296  }
297  }
298 
299  return;
300 }
301 
302 template <class V, class M>
303 void VectorSpace<V,M>::print(std::ostream& os) const
304 {
305  os << "In VectorSpace<V,M>::print()"
306  << ": nothing to be printed" << std::endl;
307  return;
308 }
309 
310 } // End namespace QUESO
311 
313 #ifdef QUESO_HAS_TRILINOS
315 #endif
unsigned int displayVerbosity() const
Definition: Environment.C:396
unsigned int dimLocal() const
Definition: VectorSpace.C:170
int GlobalLength() const
Returns the global length of the array.
Definition: DistArray.C:126
void printComponentsNames(std::ostream &os, bool printHorizontally) const
Prints the local component names.
Definition: VectorSpace.C:283
A templated class for handling sets.
Definition: VectorSet.h:52
const VectorSpace< V, M > & vectorSpace() const
Accessor method to this. Vector space to which this vector set belongs to.
Definition: VectorSpace.C:252
void print(std::ostream &os) const
Prints only a message.
Definition: VectorSpace.C:303
const Map & map() const
Map.
Definition: VectorSpace.C:157
unsigned int m_dimGlobal
Global dimension.
Definition: VectorSpace.h:147
VectorSpace()
Default constructor.
#define queso_require_less_equal_msg(expr1, expr2, msg)
Definition: asserts.h:89
DistArray< std::string > * m_componentsNamesVec
Vector of strings of the type DistArray to store the names of the components.
Definition: VectorSpace.h:159
int MyLength() const
Returns the length of the locally owned array.
Definition: DistArray.C:137
const Map * m_map
Map.
Definition: VectorSpace.h:150
const V & zeroVector() const
Returns a vector filled with zeros.
Definition: VectorSpace.C:189
unsigned int dimGlobal() const
Definition: VectorSpace.C:176
#define queso_require_equal_to_msg(expr1, expr2, msg)
Definition: asserts.h:85
V * newVector() const
Creates an empty vector of size given by Map&amp; map. See template specialization.
unsigned int globalIdOfFirstComponent() const
Definition: VectorSpace.C:182
const std::string & localComponentName(unsigned int localComponentId) const
Returns the local component names.
Definition: VectorSpace.C:271
V * m_zeroVector
A vector of all elements equal to zero.
Definition: VectorSpace.h:165
#define queso_require_msg(asserted, msg)
Definition: asserts.h:69
const BaseEnvironment & env() const
Environment.
Definition: VectorSpace.C:151
std::ofstream * subDisplayFile() const
Access function for m_subDisplayFile (displays file on stream).
Definition: Environment.C:274
M * newDiagMatrix(const V &v) const
Creates a diagonal matrix with the elements and size of vector v.
Definition: VectorSpace.C:205
This (virtual) class sets up the environment underlying the use of the QUESO library by an executable...
Definition: Environment.h:193
const DistArray< std::string > * componentsNamesArray() const
Access to private attribute m_componentsNamesArray, which is an instance of DistArray.
Definition: VectorSpace.C:265
unsigned int m_dimLocal
Local dimension (number of elements owned by the calling processor.).
Definition: VectorSpace.h:153
~VectorSpace()
Destructor.
Definition: VectorSpace.C:131
bool contains(const V &vec) const
Whether vector contains vector vec.
Definition: VectorSpace.C:258
std::string m_prefix
Definition: VectorSet.h:99
A class for partitioning vectors and matrices.
Definition: Map.h:49
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:214
A class representing a vector space.
Definition: VectorSet.h:49
unsigned int numOfProcsForStorage() const
Returns total number of processes.
Definition: VectorSpace.C:164
int NumGlobalElements() const
Returns the total number of elements across all processors.
Definition: Map.C:85
DistArray< std::string > * m_componentsNamesArray
Array of strings of the type DistArray to store the names of the components.
Definition: VectorSpace.h:156
const BaseEnvironment & m_env
Definition: VectorSet.h:98

Generated on Thu Jun 11 2015 13:52:31 for queso-0.53.0 by  doxygen 1.8.5