queso-0.51.1
ArrayOfOneDTables.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/ArrayOfOneDTables.h>
26 #include <queso/GslVector.h>
27 #include <queso/GslMatrix.h>
28 
29 namespace QUESO {
30 
31 // Default constructor -------------------------------------------------
32 template <class V, class M>
34  const char* prefix,
35  const VectorSpace<V,M>& rowSpace)
36  :
37  m_env (rowSpace.env()),
38  m_prefix ((std::string)(prefix)+""),
39  m_rowSpace (rowSpace ),
40  m_oneDTables(m_rowSpace.map(),1)
41 {
42  for (unsigned int i = 0; i < (unsigned int) m_oneDTables.MyLength(); ++i) {
43  m_oneDTables(i,0) = NULL;
44  }
45 }
46 
47 // Destructor ----------------------------------------------------------
48 template <class V, class M>
50 {
51  for (unsigned int i = 0; i < (unsigned int) m_oneDTables.MyLength(); ++i) {
52  if (m_oneDTables(i,0)) delete m_oneDTables(i,0);
53  }
54 }
55 
56 // Math methods --------------------------------------------------------
57 template <class V, class M>
58 void
59 ArrayOfOneDTables<V,M>::setOneDTable(unsigned int rowId, const std::vector<double>& values)
60 {
61  UQ_FATAL_TEST_MACRO(rowId >= (unsigned int) m_oneDTables.MyLength(),
62  m_env.worldRank(),
63  "ArrayOfOneDTables<T>::setOneDTable()",
64  "rowId is out of range");
65 
66  if (m_oneDTables(rowId,0) == NULL) m_oneDTables(rowId,0) = new std::vector<double>(0);
67  else m_oneDTables(rowId,0)->clear();
68 
69  std::vector<double>& vec = *(m_oneDTables(rowId,0));
70  vec.resize(values.size(),0.);
71  for (unsigned int j = 0; j < values.size(); ++j) {
72  vec[j] = values[j];
73  }
74 
75  return;
76 }
77 
78 template <class V, class M>
79 const std::vector<double>&
80 ArrayOfOneDTables<V,M>::oneDTable(unsigned int rowId) const
81 {
82  UQ_FATAL_TEST_MACRO(rowId >= (unsigned int) m_oneDTables.MyLength(),
83  m_env.worldRank(),
84  "ArrayOfOneDTables<T>::oneDTable()",
85  "rowId is out of range");
86 
87  ArrayOfOneDTables<V,M>* tmp = const_cast<ArrayOfOneDTables<V,M>*>(this);
88 
89  UQ_FATAL_TEST_MACRO(tmp->m_oneDTables(rowId,0) == NULL,
90  m_env.worldRank(),
91  "ArrayOfOneDTables<T>::oneDTable()",
92  "requested row is still NULL");
93 
94  return *(tmp->m_oneDTables(rowId,0));
95 }
96 
97 // I/O methods----------------------------------------------------------
98 template <class V, class M>
99 void
100 ArrayOfOneDTables<V,M>::print(std::ostream& os) const
101 {
102  ArrayOfOneDTables<V,M>* tmp = const_cast<ArrayOfOneDTables<V,M>*>(this);
103  for (unsigned int i = 0; i < (unsigned int) m_oneDTables.MyLength(); ++i) {
104  const std::vector<double>& tmpVec = *(tmp->m_oneDTables(i,0));
105  os << m_prefix << i << "_values_sub" << m_env.subIdString() << " = zeros(" << tmpVec.size()
106  << "," << 1
107  << ");"
108  << std::endl;
109  os << m_prefix << i << "_values_sub" << m_env.subIdString() << " = [";
110  for (unsigned int j = 0; j < tmpVec.size(); ++j) {
111  os << tmpVec[j] << " ";
112  }
113  os << "];"
114  << std::endl;
115  }
116 
117  return;
118 }
119 
120 template <class V, class M>
121 std::ostream& operator<< (std::ostream& os, const ArrayOfOneDTables<V,M>& obj)
122 {
123  obj.print(os);
124  return os;
125 }
126 
127 } // End namespace QUESO
128 
DistArray< std::vector< double > * > m_oneDTables
const std::vector< double > & oneDTable(unsigned int rowId) const
Returns the array located at position rowId of the one-dimensional table.
int MyLength() const
Returns the length of the locally owned array.
Definition: DistArray.C:147
ArrayOfOneDTables(const char *prefix, const VectorSpace< V, M > &rowSpace)
Default constructor.
~ArrayOfOneDTables()
Destructor.
void setOneDTable(unsigned int rowId, const std::vector< double > &values)
Sets the one-dimensional table.
Class to accommodate arrays of one-dimensional tables.
A class representing a vector space.
Definition: VectorSet.h:46
#define UQ_FATAL_TEST_MACRO(test, givenRank, where, what)
Definition: Defines.h:223
void print(std::ostream &os) const
Prints the values in this array of tables.

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