25 #include <queso/StatisticalForwardProblem.h>
26 #include <queso/SequentialVectorRealizer.h>
27 #include <queso/GslVector.h>
28 #include <queso/GslMatrix.h>
33 template <
class P_V,
class P_M,
class Q_V,
class Q_M>
41 m_env (paramRv.env()),
43 m_qoiFunction (qoiFunction),
47 m_mcSeqGenerator (NULL),
48 m_solutionRealizer (NULL),
49 #ifdef UQ_ALSO_COMPUTE_MDFS_WITHOUT_KDE
51 m_subMdfValues (NULL),
53 #ifdef QUESO_COMPUTES_EXTRA_POST_PROCESSING_STATISTICS
54 m_subSolutionMdf (NULL),
56 m_subCdfValues (NULL),
57 m_subSolutionCdf (NULL),
58 m_unifiedCdfGrids (NULL),
59 m_unifiedCdfValues (NULL),
60 m_unifiedSolutionCdf (NULL),
63 m_optionsObj (alternativeOptionsValues),
64 m_userDidNotProvideOptions(
false)
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()
75 if (m_optionsObj == NULL) {
80 m_optionsObj = tempOptions;
83 m_userDidNotProvideOptions =
true;
86 if (m_optionsObj->m_help !=
"") {
87 if (m_env.subDisplayFile()) {
88 *m_env.subDisplayFile() << (*m_optionsObj) << std::endl;
92 queso_require_equal_to_msg(paramRv.imageSet().vectorSpace().dimLocal(), qoiFunction.domainSet().vectorSpace().dimLocal(),
"'paramRv' and 'qoiFunction' are related to vector spaces of different dimensions");
94 queso_require_equal_to_msg(qoiFunction.imageSet().vectorSpace().dimLocal(), qoiRv.imageSet().vectorSpace().dimLocal(),
"'qoiFunction' and 'qoiRv' are related to vector spaces of different dimensions");
96 if (m_env.subDisplayFile()) {
97 *m_env.
subDisplayFile() <<
"Leaving StatisticalForwardProblem<P_V,P_M,Q_V,Q_M>::constructor()"
98 <<
": prefix = " << m_optionsObj->m_prefix
104 template <
class P_V,
class P_M,
class Q_V,
class Q_M>
107 if (m_solutionPdf )
delete m_solutionPdf;
109 #ifdef QUESO_COMPUTES_EXTRA_POST_PROCESSING_STATISTICS
110 if (m_unifiedSolutionCdf)
delete m_unifiedSolutionCdf;
111 if (m_unifiedCdfValues )
delete m_unifiedCdfValues;
112 if (m_unifiedCdfGrids )
delete m_unifiedCdfGrids;
114 if (m_subSolutionCdf )
delete m_subSolutionCdf;
115 if (m_subCdfValues )
delete m_subCdfValues;
116 if (m_subCdfGrids )
delete m_subCdfGrids;
118 if (m_subSolutionMdf )
delete m_subSolutionMdf;
120 #ifdef UQ_ALSO_COMPUTE_MDFS_WITHOUT_KDE
121 if (m_subMdfValues )
delete m_subMdfValues;
122 if (m_subMdfGrids )
delete m_subMdfGrids;
124 if (m_solutionRealizer )
delete m_solutionRealizer;
126 if (m_mcSeqGenerator )
delete m_mcSeqGenerator;
134 m_paramChain->clear();
138 if (m_optionsObj )
delete m_optionsObj;
142 template <
class P_V,
class P_M,
class Q_V,
class Q_M>
146 return m_optionsObj->m_computeSolution;
149 template <
class P_V,
class P_M,
class Q_V,
class Q_M>
154 m_env.fullComm().Barrier();
155 m_env.fullComm().syncPrintDebugMsg(
"Entering StatisticalForwardProblem<P_V,P_M>::solveWithMonteCarlo()",1,3000000);
157 if (m_optionsObj->m_computeSolution ==
false) {
158 if ((m_env.subDisplayFile())) {
159 *m_env.subDisplayFile() <<
"In StatisticalForwardProblem<P_V,P_M,Q_V,Q_M>::solveWithMonteCarlo()"
160 <<
": avoiding solution, as requested by user"
165 if ((m_env.subDisplayFile())) {
166 *m_env.subDisplayFile() <<
"In StatisticalForwardProblem<P_V,P_M,Q_V,Q_M>::solveWithMonteCarlo()"
167 <<
": computing solution, as requested by user"
171 if (m_solutionPdf )
delete m_solutionPdf;
173 #ifdef QUESO_COMPUTES_EXTRA_POST_PROCESSING_STATISTICS
174 if (m_unifiedSolutionCdf)
delete m_unifiedSolutionCdf;
175 if (m_unifiedCdfValues )
delete m_unifiedCdfValues;
176 if (m_unifiedCdfGrids )
delete m_unifiedCdfGrids;
178 if (m_subSolutionCdf )
delete m_subSolutionCdf;
179 if (m_subCdfValues )
delete m_subCdfValues;
180 if (m_subCdfGrids )
delete m_subCdfGrids;
182 if (m_subSolutionMdf )
delete m_subSolutionMdf;
184 #ifdef UQ_ALSO_COMPUTE_MDFS_WITHOUT_KDE
185 if (m_subMdfValues )
delete m_subMdfValues;
186 if (m_subMdfGrids )
delete m_subMdfGrids;
188 if (m_solutionRealizer )
delete m_solutionRealizer;
190 if (m_mcSeqGenerator )
delete m_mcSeqGenerator;
198 m_paramChain->clear();
202 Q_V numEvaluationPointsVec(m_qoiRv.imageSet().vectorSpace().zeroVector());
203 numEvaluationPointsVec.cwSet(250.);
209 alternativeOptionsValues,
213 m_mcSeqGenerator->generateSequence(*m_paramChain,
217 m_qoiRv.setRealizer(*m_solutionRealizer);
220 #ifdef UQ_ALSO_COMPUTE_MDFS_WITHOUT_KDE
230 m_qoiRv.setMdf(*m_subSolutionMdf);
234 #ifdef QUESO_COMPUTES_EXTRA_POST_PROCESSING_STATISTICS
235 std::string subCoreName_qoiCdf(m_optionsObj->m_prefix+
"QoiCdf_");
236 std::string uniCoreName_qoiCdf(m_optionsObj->m_prefix+
"unifQoiCdf_");
237 if (m_env.numSubEnvironments() == 1) subCoreName_qoiCdf = uniCoreName_qoiCdf;
239 std::string subCoreName_solutionCdf(m_optionsObj->m_prefix+
"Qoi");
240 std::string uniCoreName_solutionCdf(m_optionsObj->m_prefix+
"unifQoi");
241 if (m_env.numSubEnvironments() == 1) subCoreName_solutionCdf = uniCoreName_solutionCdf;
252 m_qoiRv.setSubCdf(*m_subSolutionCdf);
255 if (m_env.numSubEnvironments() == 1) {
256 m_qoiRv.setUnifiedCdf(*m_subSolutionCdf);
263 *m_unifiedCdfValues);
267 *m_unifiedCdfValues);
268 m_qoiRv.setUnifiedCdf(*m_unifiedSolutionCdf);
273 P_M* pqCovarianceMatrix = NULL;
274 P_M* pqCorrelationMatrix = NULL;
275 if (m_optionsObj->m_computeCovariances || m_optionsObj->m_computeCorrelations) {
276 if (m_env.subDisplayFile()) {
277 *m_env.subDisplayFile() <<
"In StatisticalForwardProblem<P_V,P_M,Q_V,Q_M>::solveWithMonteCarlo()"
278 <<
", prefix = " << m_optionsObj->
m_prefix
279 <<
": instantiating cov and corr matrices"
284 if (m_env.subRank() == 0) {
285 pqCovarianceMatrix =
new P_M(m_env,
286 m_paramRv.imageSet().vectorSpace().map(),
287 m_qoiRv.imageSet().vectorSpace().dimGlobal());
288 pqCorrelationMatrix =
new P_M(m_env,
289 m_paramRv.imageSet().vectorSpace().map(),
290 m_qoiRv.imageSet().vectorSpace().dimGlobal());
293 std::min(m_paramRv.realizer().subPeriod(),m_qoiRv.realizer().subPeriod()),
295 *pqCorrelationMatrix);
300 if (m_env.subDisplayFile()) {
301 if (pqCovarianceMatrix ) {
302 *m_env.subDisplayFile() <<
"In StatisticalForwardProblem<P_V,P_M,Q_V,Q_M>::solveWithMonteCarlo()"
303 <<
", prefix = " << m_optionsObj->m_prefix
304 <<
": contents of covariance matrix are\n" << *pqCovarianceMatrix
307 if (pqCorrelationMatrix) {
308 *m_env.subDisplayFile() <<
"In StatisticalForwardProblem<P_V,P_M,Q_V,Q_M>::solveWithMonteCarlo()"
309 <<
", prefix = " << m_optionsObj->m_prefix
310 <<
": contents of correlation matrix are\n" << *pqCorrelationMatrix
316 if (m_env.subDisplayFile()) {
317 *m_env.subDisplayFile() <<
"In StatisticalForwardProblem<P_V,P_M,Q_V,Q_M>::solveWithMonteCarlo()"
318 <<
", prefix = " << m_optionsObj->m_prefix
319 <<
": checking necessity of opening data output file '" << m_optionsObj->m_dataOutputFileName
324 if (m_env.openOutputFile(m_optionsObj->m_dataOutputFileName,
325 UQ_FILE_EXTENSION_FOR_MATLAB_FORMAT,
326 m_optionsObj->m_dataOutputAllowedSet,
329 #ifdef UQ_ALSO_COMPUTE_MDFS_WITHOUT_KDE
330 m_qoiRv.mdf().print(*filePtrSet.
ofsVar);
332 #ifdef QUESO_COMPUTES_EXTRA_POST_PROCESSING_STATISTICS
333 *filePtrSet.
ofsVar << m_qoiRv.subCdf();
340 #ifdef QUESO_COMPUTES_EXTRA_POST_PROCESSING_STATISTICS
341 if (m_env.numSubEnvironments() > 1) {
342 if (m_qoiRv.imageSet().vectorSpace().numOfProcsForStorage() == 1) {
343 if (m_env.inter0Rank() == 0) {
344 *filePtrSet.
ofsVar << m_qoiRv.unifiedCdf();
348 queso_error_msg(
"unified cdf writing, parallel vectors not supported yet");
353 m_env.closeFile(filePtrSet,UQ_FILE_EXTENSION_FOR_MATLAB_FORMAT);
354 if (m_env.subDisplayFile()) {
355 *m_env.subDisplayFile() <<
"In StatisticalForwardProblem<P_V,P_M,Q_V,Q_M>::solveWithMonteCarlo()"
356 <<
", prefix = " << m_optionsObj->m_prefix
357 <<
": closed data output file '" << m_optionsObj->m_dataOutputFileName
362 if (m_env.subDisplayFile()) {
363 *m_env.subDisplayFile() << std::endl;
366 if (pqCovarianceMatrix )
delete pqCovarianceMatrix;
367 if (pqCorrelationMatrix)
delete pqCorrelationMatrix;
369 m_env.fullComm().syncPrintDebugMsg(
"Leaving StatisticalForwardProblem<P_V,P_M>::solveWithMonteCarlo()",1,3000000);
370 m_env.fullComm().Barrier();
375 template <
class P_V,
class P_M,
class Q_V,
class Q_M>
383 #ifdef QUESO_COMPUTES_EXTRA_POST_PROCESSING_STATISTICS
384 template <
class P_V,
class P_M,
class Q_V,
class Q_M>
388 if (m_env.numSubEnvironments() == 1) {
389 return m_qoiRv.subCdf();
392 if (m_env.inter0Rank() < 0) {
393 return m_qoiRv.subCdf();
400 return m_qoiRv.unifiedCdf();
404 template <
class P_V,
class P_M,
class Q_V,
class Q_M>
411 queso_require_msg(m_paramChain,
"m_paramChain is NULL");
413 return *m_paramChain;
417 template <
class P_V,
class P_M,
class Q_V,
class Q_M>
const GenericVectorRV< Q_V, Q_M > & qoiRv() const
Returns the QoI RV; access to private attribute m_qoiRv.
A class for handling sampled vector CDFs.
This class provides options for a Statistical Forward Problem if no input file is available...
void unifiedUniformlySampledCdf(const V &numEvaluationPointsVec, ArrayOfOneDGrids< V, M > &unifiedCdfGrids, ArrayOfOneDTables< V, M > &unifiedCdfValues) const
Uniformly samples from the CDF from the sub-sequence.
This class provides options for the Monte Carlo sequence generator if no input file is available...
void subUniformlySampledMdf(const V &numEvaluationPointsVec, ArrayOfOneDGrids< V, M > &mdfGrids, ArrayOfOneDTables< V, M > &mdfValues) const
A templated class that implements a Monte Carlo generator of samples.
This templated class represents a Statistical Forward Problem.
Class for handling vector samples (sequence of vectors).
bool computeSolutionFlag() const
Whether or not compute the solution.
void solveWithMonteCarlo(const McOptionsValues *alternativeOptionsValues)
Solves the problem through Monte Carlo algorithm.
const BaseVectorCdf< Q_V, Q_M > & qoiRv_unifiedCdf() const
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"))
void subUniformlySampledCdf(const V &numEvaluationPointsVec, ArrayOfOneDGrids< V, M > &cdfGrids, ArrayOfOneDTables< V, M > &cdfValues) const
Uniformly samples from the CDF from the sub-sequence.
~StatisticalForwardProblem()
Destructor.
std::ofstream * ofsVar
Provides a stream interface to write data to files.
const BaseVectorSequence< Q_V, Q_M > & getParamChain() const
Returns the parameter chain; access to private attribute m_paramChain.
A class for handling sampled vector MDFs.
Struct for handling data input and output from files.
StatisticalForwardProblem(const char *prefix, const SfpOptionsValues *alternativeOptionsValues, const BaseVectorRV< P_V, P_M > ¶mRv, const BaseVectorFunction< P_V, P_M, Q_V, Q_M > &qoiFunction, GenericVectorRV< Q_V, Q_M > &qoiRv)
Constructor.
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)
A class for handling sequential draws (sampling) from probability density distributions.
void print(std::ostream &os) const
TODO: Prints the sequence.
std::ofstream * subDisplayFile() const
Access function for m_subDisplayFile (displays file on stream).