queso-0.57.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
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-2017 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 }
113 
114 // Destructor
115 template <class V, class M>
117 {
118  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 5)) {
119  *m_env.subDisplayFile() << "Entering VectorSpace<V,M>::destructor()"
120  << std::endl;
121  }
122 
123  if (m_zeroVector != NULL) delete m_zeroVector;
124  if (m_componentsNamesVec != NULL) delete m_componentsNamesVec;
125  if (m_componentsNamesArray != NULL) delete m_componentsNamesArray;
126  if (m_map != NULL) delete m_map;
127 
128  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 5)) {
129  *m_env.subDisplayFile() << "Leaving VectorSpace<V,M>::destructor()"
130  << std::endl;
131  }
132 }
133 
134 // Attribute methods
135 template <class V, class M>
137 {
138  return m_env;
139 }
140 
141 template <class V, class M>
143 {
144  queso_require_msg(m_map, "m_map is still NULL");
145  return *m_map;
146 }
147 
148 template <class V, class M>
150 {
151  return m_map->Comm().NumProc();
152 }
153 
154 template <class V, class M>
155 unsigned int VectorSpace<V,M>::dimLocal() const
156 {
157  return m_dimLocal;
158 }
159 
160 template <class V, class M>
161 unsigned int VectorSpace<V,M>::dimGlobal() const
162 {
163  return m_dimGlobal;
164 }
165 
166 template <class V, class M>
168 {
169  return m_map->MinMyGID();
170 }
171 
172 
173 template<class V, class M>
175 {
176  queso_require_msg(m_zeroVector, "m_zeroVector is still NULL");
177  return *m_zeroVector;
178 }
179 
180 template <class V, class M>
181 V* VectorSpace<V,M>::newVector(const V& v) const
182 {
183  if (v.sizeGlobal() != m_dimGlobal) return NULL;
184  if (v.sizeLocal () != m_dimLocal ) return NULL;
185 
186  return new V(v);
187 }
188 
189 template <class V, class M>
191 {
192  if (v.sizeGlobal() != m_dimGlobal) return NULL;
193  if (v.sizeLocal () != m_dimLocal ) return NULL;
194 
195  return new M(v);
196 }
197 
198 template <class V, class M>
199 M* VectorSpace<V,M>::newProposalMatrix(const V* varVec, const V* auxVec) const
200 {
201  V tmpVec(*m_zeroVector);
202  for (unsigned int i = 0; i < m_dimLocal; ++i) {
203  double variance = INFINITY;
204  if (varVec) variance = (*varVec)[i];
205  if (m_env.subDisplayFile()) {
206  *m_env.subDisplayFile() << "In VectorSpace<V,M>::newProposalMatrix()"
207  << ": i = " << i
208  << ", variance = " << variance
209  << std::endl;
210  }
211  if ((variance == INFINITY) ||
212  (variance == NAN )) {
213  if (auxVec) {
214  tmpVec[i] = std::pow( fabs((*auxVec)[i])*0.05,2. );
215  if ((tmpVec[i] == 0. ) ||
216  (tmpVec[i] == INFINITY) ||
217  (tmpVec[i] == NAN )) {
218  tmpVec[i] = 1.;
219  }
220  }
221  else {
222  tmpVec[i] = 1.;
223  }
224  }
225  else if (variance == 0.) {
226  tmpVec[i] = 1.;
227  }
228  else {
229  tmpVec[i] = variance;
230  }
231  }
232 
233  return newDiagMatrix(tmpVec);
234 }
235 
236 template <class V, class M>
238 {
239  return *this;
240 }
241 
242 template <class V, class M>
243 bool VectorSpace<V,M>::contains(const V& /* vec */) const
244 {
245  return true;
246 }
247 
248 template <class V, class M>
249 void VectorSpace<V,M>::centroid(V& vec) const
250 {
251  queso_assert_equal_to (m_dimLocal, vec.sizeLocal());
252 
253  for (unsigned int i = 0; i < m_dimLocal; ++i) {
254  vec[i] = INFINITY;
255  }
256 }
257 
258 template <class V, class M>
259 void VectorSpace<V,M>::moments(M& mat) const
260 {
261  queso_assert_equal_to (m_dimLocal, mat.numCols());
262 
263  mat.zeroLower();
264  mat.zeroUpper();
265  for (unsigned int i = 0; i < m_dimLocal; ++i) {
266  mat(i,i) = INFINITY;
267  }
268 }
269 
270 template <class V, class M>
272 {
273  return m_componentsNamesArray;
274 }
275 
276 template <class V, class M>
278  unsigned int localComponentId) const
279 {
280  if (m_componentsNamesArray == NULL) return m_emptyComponentName;
281 
282  queso_require_less_equal_msg(localComponentId, m_dimLocal, "localComponentId is too big");
283 
284 //return (*(const_cast<DistArray<std::string>*>(m_componentsNamesArray)))(localComponentId,0);
285  return (*m_componentsNamesArray)(localComponentId,0);
286 }
287 
288 template<class V, class M>
290  bool printHorizontally) const
291 {
292  if (printHorizontally) {
293  for (unsigned int i = 0; i < this->dimLocal(); ++i) {
294  os << "'" << this->localComponentName(i) << "'"
295  << " ";
296  }
297  }
298  else {
299  for (unsigned int i = 0; i < this->dimLocal(); ++i) {
300  os << "'" << this->localComponentName(i) << "'"
301  << std::endl;
302  }
303  }
304 
305  return;
306 }
307 
308 template <class V, class M>
309 void VectorSpace<V,M>::print(std::ostream& os) const
310 {
311  os << "In VectorSpace<V,M>::print()"
312  << ": nothing to be printed" << std::endl;
313  return;
314 }
315 
316 } // End namespace QUESO
317 
319 #ifdef QUESO_HAS_TRILINOS
321 #endif
void printComponentsNames(std::ostream &os, bool printHorizontally) const
Prints the local component names.
Definition: VectorSpace.C:289
void print(std::ostream &os) const
Prints only a message.
Definition: VectorSpace.C:309
const V & zeroVector() const
Returns a vector filled with zeros.
Definition: VectorSpace.C:174
const DistArray< std::string > * componentsNamesArray() const
Access to private attribute m_componentsNamesArray, which is an instance of DistArray.
Definition: VectorSpace.C:271
unsigned int dimGlobal() const
Definition: VectorSpace.C:161
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:199
A class for partitioning vectors and matrices.
Definition: Map.h:49
A class representing a vector space.
Definition: VectorSet.h:49
This (virtual) class sets up the environment underlying the use of the QUESO library by an executable...
Definition: Environment.h:198
const Map * m_map
Map.
Definition: VectorSpace.h:153
const std::string & localComponentName(unsigned int localComponentId) const
Returns the local component names.
Definition: VectorSpace.C:277
unsigned int numOfProcsForStorage() const
Returns total number of processes.
Definition: VectorSpace.C:149
const BaseEnvironment & env() const
Environment.
Definition: VectorSpace.C:136
A templated class for handling sets.
Definition: VectorSet.h:52
bool contains(const V &vec) const
Whether vector contains vector vec.
Definition: VectorSpace.C:243
unsigned int m_dimLocal
Local dimension (number of elements owned by the calling processor.).
Definition: VectorSpace.h:156
const BaseEnvironment & m_env
Definition: VectorSet.h:104
V * newVector() const
Creates an empty vector of size given by Map&amp; map. See template specialization.
~VectorSpace()
Destructor.
Definition: VectorSpace.C:116
DistArray< std::string > * m_componentsNamesArray
Array 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:85
unsigned int globalIdOfFirstComponent() const
Definition: VectorSpace.C:167
unsigned int dimLocal() const
Definition: VectorSpace.C:155
int GlobalLength() const
Returns the global length of the array.
Definition: DistArray.C:107
int MyLength() const
Returns the length of the locally owned array.
Definition: DistArray.C:118
VectorSpace()
Default constructor.
MonteCarloSGOptions::MonteCarloSGOptions(const BaseEnvironment &env, const char *prefix, const McOptionsValues &alternativeOptionsValues queso_require_equal_to_msg)(m_env.optionsInputFileName(), std::string(""), std::string("this constructor is incompatible with the existence of an options input file"))
void centroid(V &vec) const
The (INFINITY/nonexistent) centroid of the space.
Definition: VectorSpace.C:249
const Map & map() const
Map.
Definition: VectorSpace.C:142
std::string m_prefix
Definition: VectorSet.h:105
V * m_zeroVector
A vector of all elements equal to zero.
Definition: VectorSpace.h:168
unsigned int displayVerbosity() const
Definition: Environment.C:450
const VectorSpace< V, M > & vectorSpace() const
Accessor method to this. Vector space to which this vector set belongs to.
Definition: VectorSpace.C:237
void moments(M &vec) const
The (INFINITY/nonexistent) matrix of second moments of the space.
Definition: VectorSpace.C:259
unsigned int m_dimGlobal
Global dimension.
Definition: VectorSpace.h:150
M * newDiagMatrix(const V &v) const
Creates a diagonal matrix with the elements and size of vector v.
Definition: VectorSpace.C:190
std::ofstream * subDisplayFile() const
Access function for m_subDisplayFile (displays file on stream).
Definition: Environment.C:320

Generated on Tue Jun 5 2018 19:48:54 for queso-0.57.1 by  doxygen 1.8.5