queso-0.51.1
StatisticalForwardProblem.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/StatisticalForwardProblem.h>
26 #include <queso/SequentialVectorRealizer.h>
27 #include <queso/GslVector.h>
28 #include <queso/GslMatrix.h>
29 
30 namespace QUESO {
31 
32 // Default constructor -----------------------------
33 template <class P_V,class P_M,class Q_V,class Q_M>
34 StatisticalForwardProblem<P_V,P_M,Q_V,Q_M>::StatisticalForwardProblem( const char* prefix, const SfpOptionsValues* alternativeOptionsValues, // dakota const BaseVectorRV <P_V,P_M>& paramRv, const BaseVectorFunction<P_V,P_M,Q_V,Q_M>& qoiFunction, GenericVectorRV <Q_V,Q_M>& qoiRv)
40  :
41  m_env (paramRv.env()),
42  m_paramRv (paramRv),
43  m_qoiFunction (qoiFunction),
44  m_qoiRv (qoiRv),
45  m_paramChain (NULL),
46  m_qoiChain (NULL),
47  m_mcSeqGenerator (NULL),
48  m_solutionRealizer (NULL),
49 #ifdef UQ_ALSO_COMPUTE_MDFS_WITHOUT_KDE
50  m_subMdfGrids (NULL),
51  m_subMdfValues (NULL),
52 #endif
53 #ifdef QUESO_COMPUTES_EXTRA_POST_PROCESSING_STATISTICS
54  m_subSolutionMdf (NULL),
55  m_subCdfGrids (NULL),
56  m_subCdfValues (NULL),
57  m_subSolutionCdf (NULL),
58  m_unifiedCdfGrids (NULL),
59  m_unifiedCdfValues (NULL),
60  m_unifiedSolutionCdf (NULL),
61 #endif
62  m_solutionPdf (NULL),
63  m_alternativeOptionsValues(),
64  m_optionsObj (NULL)
65 {
66  if (m_env.subDisplayFile()) {
67  *m_env.subDisplayFile() << "Entering StatisticalForwardProblem<P_V,P_M,Q_V,Q_M>::constructor()"
68  << ": prefix = " << prefix
69  << ", alternativeOptionsValues = " << alternativeOptionsValues
70  << ", m_env.optionsInputFileName() = " << m_env.optionsInputFileName()
71  << std::endl;
72  }
73 
74  if (alternativeOptionsValues) m_alternativeOptionsValues = *alternativeOptionsValues;
75  if (m_env.optionsInputFileName() == "") {
76  m_optionsObj = new StatisticalForwardProblemOptions(m_env,prefix,m_alternativeOptionsValues);
77  }
78  else {
79  m_optionsObj = new StatisticalForwardProblemOptions(m_env,prefix);
80  m_optionsObj->scanOptionsValues();
81  }
82 
83  UQ_FATAL_TEST_MACRO(paramRv.imageSet().vectorSpace().dimLocal() != qoiFunction.domainSet().vectorSpace().dimLocal(),
84  m_env.worldRank(),
85  "StatisticalForwardProblem<P_V,P_M>::constructor()",
86  "'paramRv' and 'qoiFunction' are related to vector spaces of different dimensions");
87 
88  UQ_FATAL_TEST_MACRO(qoiFunction.imageSet().vectorSpace().dimLocal() != qoiRv.imageSet().vectorSpace().dimLocal(),
89  m_env.worldRank(),
90  "StatisticalForwardProblem<P_V,P_M>::constructor()",
91  "'qoiFunction' and 'qoiRv' are related to vector spaces of different dimensions");
92 
93  if (m_env.subDisplayFile()) {
94  *m_env.subDisplayFile() << "Leaving StatisticalForwardProblem<P_V,P_M,Q_V,Q_M>::constructor()"
95  << ": prefix = " << m_optionsObj->m_prefix
96  << std::endl;
97  }
98 }
99 
100 // Destructor --------------------------------------
101 template <class P_V,class P_M,class Q_V,class Q_M>
103 {
104  if (m_solutionPdf ) delete m_solutionPdf;
105 
106 #ifdef QUESO_COMPUTES_EXTRA_POST_PROCESSING_STATISTICS
107  if (m_unifiedSolutionCdf) delete m_unifiedSolutionCdf;
108  if (m_unifiedCdfValues ) delete m_unifiedCdfValues;
109  if (m_unifiedCdfGrids ) delete m_unifiedCdfGrids;
110 
111  if (m_subSolutionCdf ) delete m_subSolutionCdf;
112  if (m_subCdfValues ) delete m_subCdfValues;
113  if (m_subCdfGrids ) delete m_subCdfGrids;
114 
115  if (m_subSolutionMdf ) delete m_subSolutionMdf;
116 #endif
117 #ifdef UQ_ALSO_COMPUTE_MDFS_WITHOUT_KDE
118  if (m_subMdfValues ) delete m_subMdfValues;
119  if (m_subMdfGrids ) delete m_subMdfGrids;
120 #endif
121  if (m_solutionRealizer ) delete m_solutionRealizer;
122 
123  if (m_mcSeqGenerator ) delete m_mcSeqGenerator;
124 
125  if (m_qoiChain) {
126  m_qoiChain->clear();
127  delete m_qoiChain;
128  }
129 
130  if (m_paramChain) {
131  m_paramChain->clear();
132  delete m_paramChain;
133  }
134 
135  if (m_optionsObj ) delete m_optionsObj;
136 }
137 
138 // Statistical methods -----------------------------
139 template <class P_V,class P_M, class Q_V, class Q_M>
140 bool
142 {
143  return m_optionsObj->m_ov.m_computeSolution;
144 }
145 //--------------------------------------------------
146 template <class P_V,class P_M,class Q_V,class Q_M>
147 void
149  const McOptionsValues* alternativeOptionsValues)
150 {
151  m_env.fullComm().Barrier();
152  m_env.fullComm().syncPrintDebugMsg("Entering StatisticalForwardProblem<P_V,P_M>::solveWithMonteCarlo()",1,3000000);
153 
154  if (m_optionsObj->m_ov.m_computeSolution == false) {
155  if ((m_env.subDisplayFile())) {
156  *m_env.subDisplayFile() << "In StatisticalForwardProblem<P_V,P_M,Q_V,Q_M>::solveWithMonteCarlo()"
157  << ": avoiding solution, as requested by user"
158  << std::endl;
159  }
160  return;
161  }
162  if ((m_env.subDisplayFile())) {
163  *m_env.subDisplayFile() << "In StatisticalForwardProblem<P_V,P_M,Q_V,Q_M>::solveWithMonteCarlo()"
164  << ": computing solution, as requested by user"
165  << std::endl;
166  }
167 
168  if (m_solutionPdf ) delete m_solutionPdf;
169 
170 #ifdef QUESO_COMPUTES_EXTRA_POST_PROCESSING_STATISTICS
171  if (m_unifiedSolutionCdf) delete m_unifiedSolutionCdf;
172  if (m_unifiedCdfValues ) delete m_unifiedCdfValues;
173  if (m_unifiedCdfGrids ) delete m_unifiedCdfGrids;
174 
175  if (m_subSolutionCdf ) delete m_subSolutionCdf;
176  if (m_subCdfValues ) delete m_subCdfValues;
177  if (m_subCdfGrids ) delete m_subCdfGrids;
178 
179  if (m_subSolutionMdf ) delete m_subSolutionMdf;
180 #endif
181 #ifdef UQ_ALSO_COMPUTE_MDFS_WITHOUT_KDE
182  if (m_subMdfValues ) delete m_subMdfValues;
183  if (m_subMdfGrids ) delete m_subMdfGrids;
184 #endif
185  if (m_solutionRealizer ) delete m_solutionRealizer;
186 
187  if (m_mcSeqGenerator ) delete m_mcSeqGenerator;
188 
189  if (m_qoiChain) {
190  m_qoiChain->clear();
191  delete m_qoiChain;
192  }
193 
194  if (m_paramChain) {
195  m_paramChain->clear();
196  delete m_paramChain;
197  }
198 
199  Q_V numEvaluationPointsVec(m_qoiRv.imageSet().vectorSpace().zeroVector());
200  numEvaluationPointsVec.cwSet(250.);
201 
202  // Compute output realizer: Monte Carlo approach
203  m_paramChain = new SequenceOfVectors<P_V,P_M>(m_paramRv.imageSet().vectorSpace(),0,m_optionsObj->m_prefix+"paramChain");
204  m_qoiChain = new SequenceOfVectors<Q_V,Q_M>(m_qoiRv.imageSet().vectorSpace(), 0,m_optionsObj->m_prefix+"qoiChain" );
205  m_mcSeqGenerator = new MonteCarloSG<P_V,P_M,Q_V,Q_M>(m_optionsObj->m_prefix.c_str(),
206  alternativeOptionsValues,
207  m_paramRv,
208  m_qoiFunction);
209  //m_qoiRv);
210  m_mcSeqGenerator->generateSequence(*m_paramChain,
211  *m_qoiChain);
212  m_solutionRealizer = new SequentialVectorRealizer<Q_V,Q_M>((m_optionsObj->m_prefix+"Qoi").c_str(),
213  *m_qoiChain);
214  m_qoiRv.setRealizer(*m_solutionRealizer);
215 
216  // Compute output mdf: uniform sampling approach
217 #ifdef UQ_ALSO_COMPUTE_MDFS_WITHOUT_KDE
218  m_subMdfGrids = new ArrayOfOneDGrids <Q_V,Q_M>((m_optionsObj->m_prefix+"QoiMdf_").c_str(),m_qoiRv.imageSet().vectorSpace());
219  m_subMdfValues = new ArrayOfOneDTables<Q_V,Q_M>((m_optionsObj->m_prefix+"QoiMdf_").c_str(),m_qoiRv.imageSet().vectorSpace());
220  m_qoiChain->subUniformlySampledMdf(numEvaluationPointsVec, // input
221  *m_subMdfGrids, // output
222  *m_subMdfValues); // output
223 
224  m_subSolutionMdf = new SampledVectorMdf<Q_V,Q_M>((m_optionsObj->m_prefix+"Qoi").c_str(),
225  *m_subMdfGrids,
226  *m_subMdfValues);
227  m_qoiRv.setMdf(*m_subSolutionMdf);
228 #endif
229 
230  // Compute output cdf: uniform sampling approach
231 #ifdef QUESO_COMPUTES_EXTRA_POST_PROCESSING_STATISTICS
232  std::string subCoreName_qoiCdf(m_optionsObj->m_prefix+ "QoiCdf_");
233  std::string uniCoreName_qoiCdf(m_optionsObj->m_prefix+"unifQoiCdf_");
234  if (m_env.numSubEnvironments() == 1) subCoreName_qoiCdf = uniCoreName_qoiCdf;
235 
236  std::string subCoreName_solutionCdf(m_optionsObj->m_prefix+ "Qoi");
237  std::string uniCoreName_solutionCdf(m_optionsObj->m_prefix+"unifQoi");
238  if (m_env.numSubEnvironments() == 1) subCoreName_solutionCdf = uniCoreName_solutionCdf;
239 
240  m_subCdfGrids = new ArrayOfOneDGrids <Q_V,Q_M>(subCoreName_qoiCdf.c_str(),m_qoiRv.imageSet().vectorSpace());
241  m_subCdfValues = new ArrayOfOneDTables<Q_V,Q_M>(subCoreName_qoiCdf.c_str(),m_qoiRv.imageSet().vectorSpace());
242  m_qoiChain->subUniformlySampledCdf(numEvaluationPointsVec, // input
243  *m_subCdfGrids, // output
244  *m_subCdfValues); // output
245 
246  m_subSolutionCdf = new SampledVectorCdf<Q_V,Q_M>(subCoreName_solutionCdf.c_str(),
247  *m_subCdfGrids,
248  *m_subCdfValues);
249  m_qoiRv.setSubCdf(*m_subSolutionCdf);
250 
251  // Compute unified cdf if necessary
252  if (m_env.numSubEnvironments() == 1) {
253  m_qoiRv.setUnifiedCdf(*m_subSolutionCdf);
254  }
255  else {
256  m_unifiedCdfGrids = new ArrayOfOneDGrids <Q_V,Q_M>(uniCoreName_qoiCdf.c_str(),m_qoiRv.imageSet().vectorSpace());
257  m_unifiedCdfValues = new ArrayOfOneDTables<Q_V,Q_M>(uniCoreName_qoiCdf.c_str(),m_qoiRv.imageSet().vectorSpace());
258  m_qoiChain->unifiedUniformlySampledCdf(numEvaluationPointsVec, // input
259  *m_unifiedCdfGrids, // output
260  *m_unifiedCdfValues); // output
261 
262  m_unifiedSolutionCdf = new SampledVectorCdf<Q_V,Q_M>(uniCoreName_solutionCdf.c_str(),
263  *m_unifiedCdfGrids,
264  *m_unifiedCdfValues);
265  m_qoiRv.setUnifiedCdf(*m_unifiedSolutionCdf);
266  }
267 #endif
268  // Compute (just unified one) covariance matrix, if requested
269  // Compute (just unified one) correlation matrix, if requested
270  P_M* pqCovarianceMatrix = NULL;
271  P_M* pqCorrelationMatrix = NULL;
272  if (m_optionsObj->m_ov.m_computeCovariances || m_optionsObj->m_ov.m_computeCorrelations) {
273  if (m_env.subDisplayFile()) {
274  *m_env.subDisplayFile() << "In StatisticalForwardProblem<P_V,P_M,Q_V,Q_M>::solveWithMonteCarlo()"
275  << ", prefix = " << m_optionsObj->m_prefix
276  << ": instantiating cov and corr matrices"
277  << std::endl;
278  }
279  pqCovarianceMatrix = new P_M(m_env,
280  m_paramRv.imageSet().vectorSpace().map(), // number of rows
281  m_qoiRv.imageSet().vectorSpace().dimGlobal()); // number of cols
282  pqCorrelationMatrix = new P_M(m_env,
283  m_paramRv.imageSet().vectorSpace().map(), // number of rows
284  m_qoiRv.imageSet().vectorSpace().dimGlobal()); // number of cols
286  *m_qoiChain,
287  std::min(m_paramRv.realizer().subPeriod(),m_qoiRv.realizer().subPeriod()), // FIX ME: might be INFINITY
288  *pqCovarianceMatrix,
289  *pqCorrelationMatrix);
290  }
291 
292  // Write data out
293  if (m_env.subDisplayFile()) {
294  if (pqCovarianceMatrix ) {
295  *m_env.subDisplayFile() << "In StatisticalForwardProblem<P_V,P_M,Q_V,Q_M>::solveWithMonteCarlo()"
296  << ", prefix = " << m_optionsObj->m_prefix
297  << ": contents of covariance matrix are\n" << *pqCovarianceMatrix
298  << std::endl;
299  }
300  if (pqCorrelationMatrix) {
301  *m_env.subDisplayFile() << "In StatisticalForwardProblem<P_V,P_M,Q_V,Q_M>::solveWithMonteCarlo()"
302  << ", prefix = " << m_optionsObj->m_prefix
303  << ": contents of correlation matrix are\n" << *pqCorrelationMatrix
304  << std::endl;
305  }
306  }
307 
308  // Open data output file
309  if (m_env.subDisplayFile()) {
310  *m_env.subDisplayFile() << "In StatisticalForwardProblem<P_V,P_M,Q_V,Q_M>::solveWithMonteCarlo()"
311  << ", prefix = " << m_optionsObj->m_prefix
312  << ": checking necessity of opening data output file '" << m_optionsObj->m_ov.m_dataOutputFileName
313  << "'"
314  << std::endl;
315  }
316  FilePtrSetStruct filePtrSet;
317  if (m_env.openOutputFile(m_optionsObj->m_ov.m_dataOutputFileName,
318  UQ_FILE_EXTENSION_FOR_MATLAB_FORMAT, // Yes, always ".m"
319  m_optionsObj->m_ov.m_dataOutputAllowedSet,
320  false,
321  filePtrSet)) {
322 #ifdef UQ_ALSO_COMPUTE_MDFS_WITHOUT_KDE
323  m_qoiRv.mdf().print(*filePtrSet.ofsVar);
324 #endif
325 #ifdef QUESO_COMPUTES_EXTRA_POST_PROCESSING_STATISTICS
326  *filePtrSet.ofsVar << m_qoiRv.subCdf();
327 #endif
328 
329  //if (pqCovarianceMatrix ) *filePtrSet.ofsVar << *pqCovarianceMatrix; // FIX ME: output matrix in matlab format
330  //if (pqCorrelationMatrix) *filePtrSet.ofsVar << *pqCorrelationMatrix; // FIX ME: output matrix in matlab format
331 
332  // Write unified cdf if necessary
333 #ifdef QUESO_COMPUTES_EXTRA_POST_PROCESSING_STATISTICS
334  if (m_env.numSubEnvironments() > 1) {
335  if (m_qoiRv.imageSet().vectorSpace().numOfProcsForStorage() == 1) {
336  if (m_env.inter0Rank() == 0) {
337  *filePtrSet.ofsVar << m_qoiRv.unifiedCdf(); //*m_unifiedSolutionCdf;
338  }
339  }
340  else {
341  UQ_FATAL_TEST_MACRO(true,
342  m_env.worldRank(),
343  "StatisticalForwardProblem<P_V,P_M,Q_V,Q_M>::solveWithMonteCarlo()",
344  "unified cdf writing, parallel vectors not supported yet");
345  }
346  }
347 #endif
348  // Close data output file
349  m_env.closeFile(filePtrSet,UQ_FILE_EXTENSION_FOR_MATLAB_FORMAT);
350  if (m_env.subDisplayFile()) {
351  *m_env.subDisplayFile() << "In StatisticalForwardProblem<P_V,P_M,Q_V,Q_M>::solveWithMonteCarlo()"
352  << ", prefix = " << m_optionsObj->m_prefix
353  << ": closed data output file '" << m_optionsObj->m_ov.m_dataOutputFileName
354  << "'"
355  << std::endl;
356  }
357  }
358  if (m_env.subDisplayFile()) {
359  *m_env.subDisplayFile() << std::endl;
360  }
361 
362  if (pqCovarianceMatrix ) delete pqCovarianceMatrix;
363  if (pqCorrelationMatrix) delete pqCorrelationMatrix;
364 
365  m_env.fullComm().syncPrintDebugMsg("Leaving StatisticalForwardProblem<P_V,P_M>::solveWithMonteCarlo()",1,3000000);
366  m_env.fullComm().Barrier();
367 
368  return;
369 }
370 //--------------------------------------------------
371 template <class P_V,class P_M,class Q_V,class Q_M>
374 {
375  return m_qoiRv;
376 }
377 //--------------------------------------------------
378 
379 #ifdef QUESO_COMPUTES_EXTRA_POST_PROCESSING_STATISTICS
380 template <class P_V,class P_M,class Q_V,class Q_M>
383 {
384  if (m_env.numSubEnvironments() == 1) {
385  return m_qoiRv.subCdf();
386  }
387 
388  if (m_env.inter0Rank() < 0) {
389  return m_qoiRv.subCdf();
390  }
391 
392  //UQ_FATAL_TEST_MACRO(m_unifiedSolutionCdf == NULL,
393  // m_env.worldRank(),
394  // "StatisticalForwardProblem<P_V,P_M,Q_V,Q_M>::qoiRv_unifiedCdf()",
395  // "variable is NULL");
396  return m_qoiRv.unifiedCdf(); //*m_unifiedSolutionCdf;
397 }
398 #endif
399 //--------------------------------------------------
400 template <class P_V,class P_M,class Q_V,class Q_M>
401  const BaseVectorSequence<Q_V,Q_M>&
403 {
404 
405  // Make sure this runs after the forward propagation
406  // only then we obtain the actual realizations of the parameters
407  UQ_FATAL_TEST_MACRO(m_paramChain == NULL,
408  m_env.worldRank(),
409  (std::string)("StatisticalForwardProblem<V,M,V,M>::getParamChain()"),
410  "m_paramChain is NULL");
411 
412  return *m_paramChain;
413 
414 }
415 // I/O methods--------------------------------------
416 template <class P_V,class P_M,class Q_V,class Q_M>
417 void
419 {
420  return;
421 }
422 
423 } // End namespace QUESO
424 
A templated class that implements a Monte Carlo generator of samples.
Definition: MonteCarloSG.h:49
const GenericVectorRV< Q_V, Q_M > & qoiRv() const
Returns the QoI RV; access to private attribute m_qoiRv.
Class to accommodate arrays of one-dimensional grid.
void subUniformlySampledCdf(const V &numEvaluationPointsVec, ArrayOfOneDGrids< V, M > &cdfGrids, ArrayOfOneDTables< V, M > &cdfValues) const
Uniformly samples from the CDF from the sub-sequence.
A class for handling sampled vector MDFs.
std::ofstream * ofsVar
Provides a stream interface to write data to files.
Definition: Environment.h:75
std::string m_prefix
Definition: VectorCdf.h:102
#define UQ_FILE_EXTENSION_FOR_MATLAB_FORMAT
Definition: Defines.h:89
StatisticalForwardProblem(const char *prefix, const SfpOptionsValues *alternativeOptionsValues, const BaseVectorRV< P_V, P_M > &paramRv, const BaseVectorFunction< P_V, P_M, Q_V, Q_M > &qoiFunction, GenericVectorRV< Q_V, Q_M > &qoiRv)
Constructor.
This class provides options for a Statistical Forward Problem if no input file is available...
void print(std::ostream &os) const
TODO: Prints the sequence.
This class reads option values for a Statistical Forward Problem from an input file.
void ComputeCovCorrMatricesBetweenVectorSequences(const BaseVectorSequence< P_V, P_M > &subPSeq, const BaseVectorSequence< Q_V, Q_M > &subQSeq, unsigned int subNumSamples, P_M &pqCovMatrix, P_M &pqCorrMatrix)
void unifiedUniformlySampledCdf(const V &numEvaluationPointsVec, ArrayOfOneDGrids< V, M > &unifiedCdfGrids, ArrayOfOneDTables< V, M > &unifiedCdfValues) const
Uniformly samples from the CDF from the sub-sequence.
A class for handling sampled vector CDFs.
Struct for handling data input and output from files.
Definition: Environment.h:66
A class for handling sequential draws (sampling) from probability density distributions.
bool computeSolutionFlag() const
Whether or not compute the solution.
This templated class represents a Statistical Forward Problem.
void solveWithMonteCarlo(const McOptionsValues *alternativeOptionsValues)
Solves the problem through Monte Carlo algorithm.
This class provides options for the Monte Carlo sequence generator if no input file is available...
Class to accommodate arrays of one-dimensional tables.
Class for handling vector samples (sequence of vectors).
#define UQ_FATAL_TEST_MACRO(test, givenRank, where, what)
Definition: Defines.h:223
const BaseVectorSequence< Q_V, Q_M > & getParamChain() const
Returns the parameter chain; access to private attribute m_paramChain.

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