queso-0.57.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
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-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/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  queso_require_equal_to_msg(fieldPositions.size(), sampleValues.sizeLocal(), "invalid input data");
87 
88  if ((m_savedPositions.size() == 0 ) &&
89  (m_savedRvImageSpace == NULL) &&
90  (m_savedRvLawExpVector == NULL) &&
91  (m_savedRvLawCovMatrix == NULL) &&
92  (m_savedRv == NULL)) {
93  // Ok
94  }
95  else if ((m_savedPositions.size() != 0 ) &&
96  (m_savedRvImageSpace != NULL) &&
97  (m_savedRvLawExpVector != NULL) &&
98  (m_savedRvLawCovMatrix != NULL) &&
99  (m_savedRv != NULL)) {
100  // Ok
101  }
102  else {
103  queso_error_msg("invalid combination of pointer values");
104  }
105 
106  unsigned int numberOfPositions = fieldPositions.size();
107  bool instantiate = true;
108  if (m_savedPositions.size() == numberOfPositions) {
109  bool allPositionsAreEqual = true;
110  for (unsigned int i = 0; i < numberOfPositions; ++i) {
111  queso_require_msg(m_savedPositions[i], "m_savedPositions[i] should not be NULL");
112  if ((m_savedPositions[i]->sizeLocal() == fieldPositions[i]->sizeLocal()) &&
113  (*(m_savedPositions[i]) == *(fieldPositions[i]) )) {
114  // Ok
115  }
116  else {
117  allPositionsAreEqual = false;
118  break;
119  }
120  } // for i
121  instantiate = !allPositionsAreEqual;
122  }
123 
124  if (instantiate) {
125  delete m_savedRv;
126  delete m_savedRvLawCovMatrix;
127  delete m_savedRvLawExpVector;
128  delete m_savedRvImageSpace;
129  for (unsigned int i = 0; i < m_savedPositions.size(); ++i) {
130  delete m_savedPositions[i];
131  }
132  m_savedPositions.clear();
133 
134  // Set m_savedPositions
135  m_savedPositions.resize(numberOfPositions,NULL);
136  for (unsigned int i = 0; i < m_savedPositions.size(); ++i) {
137  m_savedPositions[i] = new V(*(fieldPositions[i]));
138  }
139 
140  // Set m_savedRvImageSpace
141  m_savedRvImageSpace = new VectorSpace<V,M>(m_env, "grf_", numberOfPositions, NULL);
142 
143  // Set m_savedRvLawExpVector
144  m_savedRvLawExpVector = new V(m_savedRvImageSpace->zeroVector());
145  for (unsigned int i = 0; i < numberOfPositions; ++i) {
146  (*m_savedRvLawExpVector)[i] = m_meanFunction.actualValue(*(fieldPositions[i]),NULL,NULL,NULL,NULL);
147  }
148 
149  // Set m_savedRvLawCovMatrix
150  m_savedRvLawCovMatrix = new M(m_savedRvImageSpace->zeroVector());
151  for (unsigned int i = 0; i < numberOfPositions; ++i) {
152  for (unsigned int j = 0; j < numberOfPositions; ++j) {
153  (*m_savedRvLawCovMatrix)(i,j) = m_covarianceFunction.value(*(fieldPositions[i]),*(fieldPositions[j]));
154  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 3)) {
155  *m_env.subDisplayFile() << "In ScalarGaussianRandomField<V,M>::sampleFunction()"
156  << ": i = " << i
157  << ", j = " << j
158  << ", *(fieldPositions[i]) = " << *(fieldPositions[i])
159  << ", *(fieldPositions[j]) = " << *(fieldPositions[j])
160  << ", (*m_savedRvLawCovMatrix)(i,j) = " << (*m_savedRvLawCovMatrix)(i,j)
161  << std::endl;
162  }
163  }
164  }
165 
166  // Set m_savedRv
167  m_savedRv = new GaussianVectorRV<V,M>("grf_",
168  *m_savedRvImageSpace,
169  *m_savedRvLawExpVector,
170  *m_savedRvLawCovMatrix);
171 
172  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 3)) {
173  *m_env.subDisplayFile() << "In ScalarGaussianRandomField<V,M>::sampleFunction()"
174  << ": just instantiated Gaussian RV"
175  << "\n *m_savedRvLawExpVector = " << *m_savedRvLawExpVector
176  << "\n *m_savedRvLawCovMatrix = " << *m_savedRvLawCovMatrix
177  << std::endl;
178  for (unsigned int i = 0; i < numberOfPositions; ++i) {
179  *m_env.subDisplayFile() << " *(m_savedPositions[" << i
180  << "]) = " << *(m_savedPositions[i])
181  << std::endl;
182  }
183  }
184  } // if (instantiate)
185 
186  // Generate sample function
187  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 99)) {
188  *m_env.subDisplayFile() << "In ScalarGaussianRandomField<V,M>::sampleFunction()"
189  << ": about to realize sample values"
190  << std::endl;
191  }
192  m_savedRv->realizer().realization(sampleValues);
193  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 99)) {
194  *m_env.subDisplayFile() << "In ScalarGaussianRandomField<V,M>::sampleFunction()"
195  << ": just realized sample values"
196  << std::endl;
197  }
198 
199  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 99)) {
200  *m_env.subDisplayFile() << "Leaving ScalarGaussianRandomField<V,M>::sampleFunction()"
201  << std::endl;
202  }
203 
204  return;
205 }
206 
207 } // End namespace QUESO
A templated (base) class for handling scalar functions.
A class representing a vector space.
Definition: VectorSet.h:49
ScalarGaussianRandomField(const char *prefix, const VectorSet< V, M > &indexSet, const BaseScalarFunction< V, M > &meanFunction, const BaseScalarCovarianceFunction< V, M > &covarianceFunction)
Constructor.
A class representing a Gaussian vector RV.
A templated class for handling sets.
Definition: VectorSet.h:52
A templated (base) class to accommodate scalar covariance functions (of random variables).
const BaseScalarCovarianceFunction< V, M > & covarianceFunction() const
Covariance function; access to protected attribute m_covarianceFunction.
const BaseScalarFunction< V, M > & meanFunction() const
Mean function; access to protected attribute m_meanFunction.
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"))
std::vector< V * > m_savedPositions
Saved positions.
const VectorSet< V, M > & indexSet() const
Index set; access to protected attribute m_indexSet.
void sampleFunction(const std::vector< V * > &fieldPositions, V &sampleValues)
Function that samples from a Gaussian PDF.

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