queso-0.51.1
ScalarGaussianRandomField.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/ScalarGaussianRandomField.h>
26 #include <queso/GaussianVectorRV.h>
27 
28 namespace QUESO {
29 
30 // Default constructor -----------------------------
31 template <class V, class M>
33  const char* prefix,
34  const VectorSet<V,M>& indexSet,
35  const BaseScalarFunction<V,M>& meanFunction,
36  const BaseScalarCovarianceFunction<V,M>& covarianceFunction)
37  :
38  m_env (indexSet.env()),
39  m_prefix ((std::string)(prefix)+"grf_"),
40  m_indexSet (indexSet),
41  m_meanFunction (meanFunction),
42  m_covarianceFunction (covarianceFunction),
43  m_savedRvImageSpace (NULL),
44  m_savedRvLawExpVector(NULL),
45  m_savedRvLawCovMatrix(NULL),
46  m_savedRv (NULL)
47 {
48  m_savedPositions.clear();
49 }
50 // Destructor ---------------------------------------
51 template <class V, class M>
53 {
54 }
55 // Math methods -------------------------------------
56 template <class V, class M>
57 const VectorSet<V,M>&
59 {
60  return m_indexSet;
61 }
62 // --------------------------------------------------
63 template <class V, class M>
66 {
67  return m_meanFunction;
68 }
69 // --------------------------------------------------
70 template <class V, class M>
73 {
74  return m_covarianceFunction;
75 }
76 // --------------------------------------------------
77 template <class V, class M>
78 void
79 ScalarGaussianRandomField<V,M>::sampleFunction(const std::vector<V*>& fieldPositions, V& sampleValues)
80 {
81  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 99)) {
82  *m_env.subDisplayFile() << "Entering ScalarGaussianRandomField<V,M>::sampleFunction()"
83  << std::endl;
84  }
85 
86  UQ_FATAL_TEST_MACRO(fieldPositions.size() != sampleValues.sizeLocal(),
87  m_env.fullRank(),
88  "ScalarGaussianRandomField<V,M>::sampleFunction()",
89  "invalid input data");
90 
91  if ((m_savedPositions.size() == 0 ) &&
92  (m_savedRvImageSpace == NULL) &&
93  (m_savedRvLawExpVector == NULL) &&
94  (m_savedRvLawCovMatrix == NULL) &&
95  (m_savedRv == NULL)) {
96  // Ok
97  }
98  else if ((m_savedPositions.size() != 0 ) &&
99  (m_savedRvImageSpace != NULL) &&
100  (m_savedRvLawExpVector != NULL) &&
101  (m_savedRvLawCovMatrix != NULL) &&
102  (m_savedRv != NULL)) {
103  // Ok
104  }
105  else {
106  UQ_FATAL_TEST_MACRO(true,
107  m_env.fullRank(),
108  "ScalarGaussianRandomField<V,M>::sampleFunction()",
109  "invalid combination of pointer values");
110  }
111 
112  unsigned int numberOfPositions = fieldPositions.size();
113  bool instantiate = true;
114  if (m_savedPositions.size() == numberOfPositions) {
115  bool allPositionsAreEqual = true;
116  for (unsigned int i = 0; i < numberOfPositions; ++i) {
117  UQ_FATAL_TEST_MACRO(m_savedPositions[i] == NULL,
118  m_env.fullRank(),
119  "ScalarGaussianRandomField<V,M>::sampleFunction()",
120  "m_savedPositions[i] should not be NULL");
121  if ((m_savedPositions[i]->sizeLocal() == fieldPositions[i]->sizeLocal()) &&
122  (*(m_savedPositions[i]) == *(fieldPositions[i]) )) {
123  // Ok
124  }
125  else {
126  allPositionsAreEqual = false;
127  break;
128  }
129  } // for i
130  instantiate = !allPositionsAreEqual;
131  }
132 
133  if (instantiate) {
134  delete m_savedRv;
135  delete m_savedRvLawCovMatrix;
136  delete m_savedRvLawExpVector;
137  delete m_savedRvImageSpace;
138  for (unsigned int i = 0; i < m_savedPositions.size(); ++i) {
139  delete m_savedPositions[i];
140  }
141  m_savedPositions.clear();
142 
143  // Set m_savedPositions
144  m_savedPositions.resize(numberOfPositions,NULL);
145  for (unsigned int i = 0; i < m_savedPositions.size(); ++i) {
146  m_savedPositions[i] = new V(*(fieldPositions[i]));
147  }
148 
149  // Set m_savedRvImageSpace
150  m_savedRvImageSpace = new VectorSpace<V,M>(m_env, "grf_", numberOfPositions, NULL);
151 
152  // Set m_savedRvLawExpVector
153  m_savedRvLawExpVector = new V(m_savedRvImageSpace->zeroVector());
154  for (unsigned int i = 0; i < numberOfPositions; ++i) {
155  (*m_savedRvLawExpVector)[i] = m_meanFunction.actualValue(*(fieldPositions[i]),NULL,NULL,NULL,NULL);
156  }
157 
158  // Set m_savedRvLawCovMatrix
159  m_savedRvLawCovMatrix = new M(m_savedRvImageSpace->zeroVector());
160  for (unsigned int i = 0; i < numberOfPositions; ++i) {
161  for (unsigned int j = 0; j < numberOfPositions; ++j) {
162  (*m_savedRvLawCovMatrix)(i,j) = m_covarianceFunction.value(*(fieldPositions[i]),*(fieldPositions[j]));
163  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 3)) {
164  *m_env.subDisplayFile() << "In ScalarGaussianRandomField<V,M>::sampleFunction()"
165  << ": i = " << i
166  << ", j = " << j
167  << ", *(fieldPositions[i]) = " << *(fieldPositions[i])
168  << ", *(fieldPositions[j]) = " << *(fieldPositions[j])
169  << ", (*m_savedRvLawCovMatrix)(i,j) = " << (*m_savedRvLawCovMatrix)(i,j)
170  << std::endl;
171  }
172  }
173  }
174 
175  // Set m_savedRv
176  m_savedRv = new GaussianVectorRV<V,M>("grf_",
177  *m_savedRvImageSpace,
178  *m_savedRvLawExpVector,
179  *m_savedRvLawCovMatrix);
180 
181  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 3)) {
182  *m_env.subDisplayFile() << "In ScalarGaussianRandomField<V,M>::sampleFunction()"
183  << ": just instantiated Gaussian RV"
184  << "\n *m_savedRvLawExpVector = " << *m_savedRvLawExpVector
185  << "\n *m_savedRvLawCovMatrix = " << *m_savedRvLawCovMatrix
186  << std::endl;
187  for (unsigned int i = 0; i < numberOfPositions; ++i) {
188  *m_env.subDisplayFile() << " *(m_savedPositions[" << i
189  << "]) = " << *(m_savedPositions[i])
190  << std::endl;
191  }
192  }
193  } // if (instantiate)
194 
195  // Generate sample function
196  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 99)) {
197  *m_env.subDisplayFile() << "In ScalarGaussianRandomField<V,M>::sampleFunction()"
198  << ": about to realize sample values"
199  << std::endl;
200  }
201  m_savedRv->realizer().realization(sampleValues);
202  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 99)) {
203  *m_env.subDisplayFile() << "In ScalarGaussianRandomField<V,M>::sampleFunction()"
204  << ": just realized sample values"
205  << std::endl;
206  }
207 
208  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 99)) {
209  *m_env.subDisplayFile() << "Leaving ScalarGaussianRandomField<V,M>::sampleFunction()"
210  << std::endl;
211  }
212 
213  return;
214 }
215 
216 } // End namespace QUESO
A templated (base) class to accommodate scalar covariance functions (of random variables).
ScalarGaussianRandomField(const char *prefix, const VectorSet< V, M > &indexSet, const BaseScalarFunction< V, M > &meanFunction, const BaseScalarCovarianceFunction< V, M > &covarianceFunction)
Constructor.
A templated class for handling sets.
Definition: VectorSet.h:49
const BaseScalarCovarianceFunction< V, M > & covarianceFunction() const
Covariance function; access to protected attribute m_covarianceFunction.
A templated (base) class for handling scalar functions.
Definition: GslOptimizer.h:48
A class representing a vector space.
Definition: VectorSet.h:46
const VectorSet< V, M > & indexSet() const
Index set; access to protected attribute m_indexSet.
#define UQ_FATAL_TEST_MACRO(test, givenRank, where, what)
Definition: Defines.h:223
void sampleFunction(const std::vector< V * > &fieldPositions, V &sampleValues)
Function that samples from a Gaussian PDF.
std::vector< V * > m_savedPositions
Saved positions.
A class representing a Gaussian vector RV.
const BaseScalarFunction< V, M > & meanFunction() const
Mean function; access to protected attribute m_meanFunction.

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