queso-0.51.1
VectorGaussianRandomField.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/VectorGaussianRandomField.h>
26 
27 namespace QUESO {
28 
29 // Default constructor -----------------------------
30 template <class P_V, class P_M, class Q_V, class Q_M>
32  const char* prefix,
33  const VectorSet<P_V,P_M>& indexSet,
34  const VectorSet<Q_V,Q_M>& imageSetPerIndex,
35  const BaseVectorFunction<P_V,P_M,Q_V,Q_M>& meanFunction,
36  const BaseMatrixCovarianceFunction<P_V,P_M,Q_V,Q_M>& covarianceFunction)
37  :
38  m_env (indexSet.env()),
39  m_prefix ((std::string)(prefix)+"grf_"),
40  m_indexSet (indexSet),
41  m_imageSetPerIndex (imageSetPerIndex),
42  m_meanFunction (meanFunction),
43  m_covarianceFunction (covarianceFunction),
44  m_savedRvImageSpace (NULL),
45  m_savedRvLawExpVector(NULL),
46  m_savedRvLawCovMatrix(NULL),
47  m_savedRv (NULL)
48 {
49  m_savedPositions.clear();
50 }
51 // Destructor ---------------------------------------
52 template <class P_V, class P_M, class Q_V, class Q_M>
54 {
55 }
56 // Math methods -------------------------------------
57 template <class P_V, class P_M, class Q_V, class Q_M>
58 const VectorSet<P_V,P_M>&
60 {
61  return m_indexSet;
62 }
63 // --------------------------------------------------
64 template <class P_V, class P_M, class Q_V, class Q_M>
67 {
68  return m_meanFunction;
69 }
70 // --------------------------------------------------
71 template <class P_V, class P_M, class Q_V, class Q_M>
74 {
75  return m_covarianceFunction;
76 }
77 // --------------------------------------------------
78 template <class P_V, class P_M, class Q_V, class Q_M>
79 void
80 VectorGaussianRandomField<P_V,P_M,Q_V,Q_M>::sampleFunction(const std::vector<P_V*>& fieldPositions, Q_V& sampleValues)
81 {
82  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 3)) {
83  *m_env.subDisplayFile() << "Entering VectorGaussianRandomField<P_V,P_M,Q_V,Q_M>::sampleFunction()"
84  << std::endl;
85  }
86 
87  UQ_FATAL_TEST_MACRO(( sampleValues.sizeLocal() % fieldPositions.size() ) != 0,
88  m_env.fullRank(),
89  "VectorGaussianRandomField<P_V,P_M,Q_V,Q_M>::sampleFunction()",
90  "input data is not multiple of each other");
91 
92  unsigned int numberOfImageValuesPerIndex = sampleValues.sizeLocal()/fieldPositions.size();
93 
94  UQ_FATAL_TEST_MACRO(numberOfImageValuesPerIndex != m_imageSetPerIndex.vectorSpace().dimLocal(),
95  m_env.fullRank(),
96  "VectorGaussianRandomField<P_V,P_M,Q_V,Q_M>::sampleFunction()",
97  "invalid input data dimension");
98 
99  if ((m_savedPositions.size() == 0 ) &&
100  (m_savedRvImageSpace == NULL) &&
101  (m_savedRvLawExpVector == NULL) &&
102  (m_savedRvLawCovMatrix == NULL) &&
103  (m_savedRv == NULL)) {
104  // Ok
105  }
106  else if ((m_savedPositions.size() != 0 ) &&
107  (m_savedRvImageSpace != NULL) &&
108  (m_savedRvLawExpVector != NULL) &&
109  (m_savedRvLawCovMatrix != NULL) &&
110  (m_savedRv != NULL)) {
111  // Ok
112  }
113  else {
114  UQ_FATAL_TEST_MACRO(true,
115  m_env.fullRank(),
116  "VectorGaussianRandomField<P_V,P_M,Q_V,Q_M>::sampleFunction()",
117  "invalid combination of pointer values");
118  }
119 
120  unsigned int numberOfPositions = fieldPositions.size();
121  bool instantiate = true;
122  if (m_savedPositions.size() == numberOfPositions) {
123  bool allPositionsAreEqual = true;
124  for (unsigned int i = 0; i < numberOfPositions; ++i) {
125  UQ_FATAL_TEST_MACRO(m_savedPositions[i] == NULL,
126  m_env.fullRank(),
127  "VectorGaussianRandomField<P_V,P_M,Q_V,Q_M>::sampleFunction()",
128  "m_savedPositions[i] should not be NULL");
129  if ((m_savedPositions[i]->sizeLocal() == fieldPositions[i]->sizeLocal()) &&
130  (*(m_savedPositions[i]) == *(fieldPositions[i]) )) {
131  // Ok
132  }
133  else {
134  allPositionsAreEqual = false;
135  break;
136  }
137  } // for i
138  instantiate = !allPositionsAreEqual;
139  }
140 
141  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 3)) {
142  *m_env.subDisplayFile() << "In VectorGaussianRandomField<P_V,P_M,Q_V,Q_M>::sampleFunction()"
143  << ": numberOfPositions = " << numberOfPositions
144  << ", instantiate = " << instantiate
145  << std::endl;
146  }
147 
148  if (instantiate) {
149  delete m_savedRv;
150  delete m_savedRvLawCovMatrix;
151  delete m_savedRvLawExpVector;
152  delete m_savedRvImageSpace;
153  for (unsigned int i = 0; i < m_savedPositions.size(); ++i) {
154  delete m_savedPositions[i];
155  }
156  m_savedPositions.clear();
157 
158  // Set m_savedPositions
159  m_savedPositions.resize(numberOfPositions,NULL);
160  for (unsigned int i = 0; i < m_savedPositions.size(); ++i) {
161  m_savedPositions[i] = new P_V(*(fieldPositions[i]));
162  }
163 
164  // Set m_savedRvImageSpace
165  m_savedRvImageSpace = new VectorSpace<Q_V,Q_M>(m_env, "grf_", numberOfPositions*numberOfImageValuesPerIndex, NULL);
166 
167  // Set m_savedRvLawExpVector
168  Q_V tmpVec(m_imageSetPerIndex.vectorSpace().zeroVector());
169  m_savedRvLawExpVector = new Q_V(m_savedRvImageSpace->zeroVector());
170  for (unsigned int i = 0; i < numberOfPositions; ++i) {
171  m_meanFunction.compute(*(fieldPositions[i]),NULL,tmpVec,NULL,NULL,NULL);
172  for (unsigned int j = 0; j < numberOfImageValuesPerIndex; ++j) {
173  (*m_savedRvLawExpVector)[i*numberOfImageValuesPerIndex + j] = tmpVec[j];
174  }
175  }
176 
177  // Set m_savedRvLawCovMatrix
178  Q_M tmpMat(m_imageSetPerIndex.vectorSpace().zeroVector());
179  m_savedRvLawCovMatrix = new Q_M(m_savedRvImageSpace->zeroVector());
180  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 3)) {
181  *m_env.subDisplayFile() << "In VectorGaussianRandomField<P_V,P_M,Q_V,Q_M>::sampleFunction()"
182  << ": m_savedRvLawCovMatrix order = " << m_savedRvLawCovMatrix->numCols()
183  << ", numberOfPositions = " << numberOfPositions
184  << ", tmpMat order = " << tmpMat.numCols()
185  << ", numberOfImageValuesPerIndex = " << numberOfImageValuesPerIndex
186  << std::endl;
187  }
188  for (unsigned int i = 0; i < numberOfPositions; ++i) {
189  for (unsigned int j = 0; j < numberOfPositions; ++j) {
190  m_covarianceFunction.covMatrix(*(fieldPositions[i]),*(fieldPositions[j]),tmpMat);
191 #if 1
192  Q_M testMat(tmpMat);
193  if (testMat.chol() != 0) {
194  *m_env.subDisplayFile() << "In VectorGaussianRandomField<P_V,P_M,Q_V,Q_M>::sampleFunction()"
195  << ": i = " << i
196  << ", j = " << j
197  << ", *(fieldPositions[i]) = " << *(fieldPositions[i])
198  << ", *(fieldPositions[j]) = " << *(fieldPositions[j])
199  << ", tmpMat = " << tmpMat
200  << ", testMat = " << testMat
201  << ", tmpMat is not positive definite"
202  << std::endl;
203  UQ_FATAL_TEST_MACRO(true,
204  m_env.fullRank(),
205  "VectorGaussianRandomField<P_V,P_M,Q_V,Q_M>::sampleFunction()",
206  "tmpMat is not positive definite");
207  }
208 #endif
209  for (unsigned int k1 = 0; k1 < numberOfImageValuesPerIndex; ++k1) {
210  for (unsigned int k2 = 0; k2 < numberOfImageValuesPerIndex; ++k2) {
211  unsigned int tmpI = i*numberOfImageValuesPerIndex + k1;
212  unsigned int tmpJ = j*numberOfImageValuesPerIndex + k2;
213  (*m_savedRvLawCovMatrix)(tmpI,tmpJ) = tmpMat(k1,k2);
214  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 3)) {
215  *m_env.subDisplayFile() << "In VectorGaussianRandomField<P_V,P_M,Q_V,Q_M>::sampleFunction()"
216  << ": i = " << i
217  << ", j = " << j
218  << ", k1 = " << k1
219  << ", k2 = " << k2
220  << ", tmpI = " << tmpI
221  << ", tmpJ = " << tmpJ
222  << ", *(fieldPositions[i]) = " << *(fieldPositions[i])
223  << ", *(fieldPositions[j]) = " << *(fieldPositions[j])
224  << ", (*m_savedRvLawCovMatrix)(tmpI,tmpJ) = " << (*m_savedRvLawCovMatrix)(tmpI,tmpJ)
225  << std::endl;
226  }
227  }
228  }
229  }
230  }
231 
232  // Set m_savedRv
233  m_savedRv = new GaussianVectorRV<Q_V,Q_M>("grf_",
234  *m_savedRvImageSpace,
235  *m_savedRvLawExpVector,
236  *m_savedRvLawCovMatrix);
237 
238  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 3)) {
239  *m_env.subDisplayFile() << "In VectorGaussianRandomField<P_V,P_M,Q_V,Q_M>::sampleFunction()"
240  << ": just instantiated Gaussian RV"
241  << "\n *m_savedRvLawExpVector = " << *m_savedRvLawExpVector
242  << "\n *m_savedRvLawCovMatrix = " << *m_savedRvLawCovMatrix
243  << std::endl;
244  for (unsigned int i = 0; i < numberOfPositions; ++i) {
245  *m_env.subDisplayFile() << " *(m_savedPositions[" << i
246  << "]) = " << *(m_savedPositions[i])
247  << std::endl;
248  }
249  }
250  } // if (instantiate)
251 
252  // Generate sample function
253  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 3)) {
254  *m_env.subDisplayFile() << "In VectorGaussianRandomField<P_V,P_M,Q_V,Q_M>::sampleFunction()"
255  << ": about to realize sample values"
256  << std::endl;
257  }
258  m_savedRv->realizer().realization(sampleValues);
259  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 3)) {
260  *m_env.subDisplayFile() << "In VectorGaussianRandomField<P_V,P_M,Q_V,Q_M>::sampleFunction()"
261  << ": just realized sample values"
262  << std::endl;
263  }
264 
265  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 3)) {
266  *m_env.subDisplayFile() << "Leaving VectorGaussianRandomField<P_V,P_M,Q_V,Q_M>::sampleFunction()"
267  << std::endl;
268  }
269 
270  return;
271 }
272 
273 } // End namespace QUESO
VectorGaussianRandomField(const char *prefix, const VectorSet< P_V, P_M > &indexSet, const VectorSet< Q_V, Q_M > &imageSetPerIndex, const BaseVectorFunction< P_V, P_M, Q_V, Q_M > &meanFunction, const BaseMatrixCovarianceFunction< P_V, P_M, Q_V, Q_M > &covarianceFunction)
Constructor.
const BaseMatrixCovarianceFunction< P_V, P_M, Q_V, Q_M > & covarianceFunction() const
Covariance function; access to protected attribute m_covarianceFunction.
std::vector< P_V * > m_savedPositions
Saved positions.
const VectorSet< P_V, P_M > & indexSet() const
Index set; access to protected attribute m_indexSet.
A templated (base) class to accommodate covariance matrix of (random) vector functions.
#define UQ_FATAL_TEST_MACRO(test, givenRank, where, what)
Definition: Defines.h:223
const BaseVectorFunction< P_V, P_M, Q_V, Q_M > & meanFunction() const
Mean function; access to protected attribute m_meanFunction.
void sampleFunction(const std::vector< P_V * > &fieldPositions, Q_V &sampleValues)
Function that samples from a Gaussian PDF.
A templated (base) class for handling vector functions.

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