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_alternativeOptionsValues(),
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()
74 if (alternativeOptionsValues) m_alternativeOptionsValues = *alternativeOptionsValues;
75 if (m_env.optionsInputFileName() ==
"") {
80 m_optionsObj->scanOptionsValues();
83 UQ_FATAL_TEST_MACRO(paramRv.imageSet().vectorSpace().dimLocal() != qoiFunction.domainSet().vectorSpace().dimLocal(),
85 "StatisticalForwardProblem<P_V,P_M>::constructor()",
86 "'paramRv' and 'qoiFunction' are related to vector spaces of different dimensions");
88 UQ_FATAL_TEST_MACRO(qoiFunction.imageSet().vectorSpace().dimLocal() != qoiRv.imageSet().vectorSpace().dimLocal(),
90 "StatisticalForwardProblem<P_V,P_M>::constructor()",
91 "'qoiFunction' and 'qoiRv' are related to vector spaces of different dimensions");
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
101 template <
class P_V,
class P_M,
class Q_V,
class Q_M>
104 if (m_solutionPdf )
delete m_solutionPdf;
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;
111 if (m_subSolutionCdf )
delete m_subSolutionCdf;
112 if (m_subCdfValues )
delete m_subCdfValues;
113 if (m_subCdfGrids )
delete m_subCdfGrids;
115 if (m_subSolutionMdf )
delete m_subSolutionMdf;
117 #ifdef UQ_ALSO_COMPUTE_MDFS_WITHOUT_KDE
118 if (m_subMdfValues )
delete m_subMdfValues;
119 if (m_subMdfGrids )
delete m_subMdfGrids;
121 if (m_solutionRealizer )
delete m_solutionRealizer;
123 if (m_mcSeqGenerator )
delete m_mcSeqGenerator;
131 m_paramChain->clear();
135 if (m_optionsObj )
delete m_optionsObj;
139 template <
class P_V,
class P_M,
class Q_V,
class Q_M>
143 return m_optionsObj->m_ov.m_computeSolution;
146 template <
class P_V,
class P_M,
class Q_V,
class Q_M>
151 m_env.fullComm().Barrier();
152 m_env.fullComm().syncPrintDebugMsg(
"Entering StatisticalForwardProblem<P_V,P_M>::solveWithMonteCarlo()",1,3000000);
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"
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"
168 if (m_solutionPdf )
delete m_solutionPdf;
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;
175 if (m_subSolutionCdf )
delete m_subSolutionCdf;
176 if (m_subCdfValues )
delete m_subCdfValues;
177 if (m_subCdfGrids )
delete m_subCdfGrids;
179 if (m_subSolutionMdf )
delete m_subSolutionMdf;
181 #ifdef UQ_ALSO_COMPUTE_MDFS_WITHOUT_KDE
182 if (m_subMdfValues )
delete m_subMdfValues;
183 if (m_subMdfGrids )
delete m_subMdfGrids;
185 if (m_solutionRealizer )
delete m_solutionRealizer;
187 if (m_mcSeqGenerator )
delete m_mcSeqGenerator;
195 m_paramChain->clear();
199 Q_V numEvaluationPointsVec(m_qoiRv.imageSet().vectorSpace().zeroVector());
200 numEvaluationPointsVec.cwSet(250.);
206 alternativeOptionsValues,
210 m_mcSeqGenerator->generateSequence(*m_paramChain,
214 m_qoiRv.setRealizer(*m_solutionRealizer);
217 #ifdef UQ_ALSO_COMPUTE_MDFS_WITHOUT_KDE
220 m_qoiChain->subUniformlySampledMdf(numEvaluationPointsVec,
227 m_qoiRv.setMdf(*m_subSolutionMdf);
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;
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;
249 m_qoiRv.setSubCdf(*m_subSolutionCdf);
252 if (m_env.numSubEnvironments() == 1) {
253 m_qoiRv.setUnifiedCdf(*m_subSolutionCdf);
260 *m_unifiedCdfValues);
264 *m_unifiedCdfValues);
265 m_qoiRv.setUnifiedCdf(*m_unifiedSolutionCdf);
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"
279 pqCovarianceMatrix =
new P_M(m_env,
280 m_paramRv.imageSet().vectorSpace().map(),
281 m_qoiRv.imageSet().vectorSpace().dimGlobal());
282 pqCorrelationMatrix =
new P_M(m_env,
283 m_paramRv.imageSet().vectorSpace().map(),
284 m_qoiRv.imageSet().vectorSpace().dimGlobal());
287 std::min(m_paramRv.realizer().subPeriod(),m_qoiRv.realizer().subPeriod()),
289 *pqCorrelationMatrix);
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
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
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
317 if (m_env.openOutputFile(m_optionsObj->m_ov.m_dataOutputFileName,
319 m_optionsObj->m_ov.m_dataOutputAllowedSet,
322 #ifdef UQ_ALSO_COMPUTE_MDFS_WITHOUT_KDE
323 m_qoiRv.mdf().print(*filePtrSet.
ofsVar);
325 #ifdef QUESO_COMPUTES_EXTRA_POST_PROCESSING_STATISTICS
326 *filePtrSet.
ofsVar << m_qoiRv.subCdf();
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();
343 "StatisticalForwardProblem<P_V,P_M,Q_V,Q_M>::solveWithMonteCarlo()",
344 "unified cdf writing, parallel vectors not supported yet");
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
358 if (m_env.subDisplayFile()) {
359 *m_env.subDisplayFile() << std::endl;
362 if (pqCovarianceMatrix )
delete pqCovarianceMatrix;
363 if (pqCorrelationMatrix)
delete pqCorrelationMatrix;
365 m_env.fullComm().syncPrintDebugMsg(
"Leaving StatisticalForwardProblem<P_V,P_M>::solveWithMonteCarlo()",1,3000000);
366 m_env.fullComm().Barrier();
371 template <
class P_V,
class P_M,
class Q_V,
class Q_M>
379 #ifdef QUESO_COMPUTES_EXTRA_POST_PROCESSING_STATISTICS
380 template <
class P_V,
class P_M,
class Q_V,
class Q_M>
384 if (m_env.numSubEnvironments() == 1) {
385 return m_qoiRv.subCdf();
388 if (m_env.inter0Rank() < 0) {
389 return m_qoiRv.subCdf();
396 return m_qoiRv.unifiedCdf();
400 template <
class P_V,
class P_M,
class Q_V,
class Q_M>
401 const BaseVectorSequence<Q_V,Q_M>&
409 (std::string)(
"StatisticalForwardProblem<V,M,V,M>::getParamChain()"),
410 "m_paramChain is NULL");
412 return *m_paramChain;
416 template <
class P_V,
class P_M,
class Q_V,
class Q_M>
A templated class that implements a Monte Carlo generator of samples.
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.
#define UQ_FILE_EXTENSION_FOR_MATLAB_FORMAT
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.
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.
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).
~StatisticalForwardProblem()
Destructor.
#define UQ_FATAL_TEST_MACRO(test, givenRank, where, what)
const BaseVectorSequence< Q_V, Q_M > & getParamChain() const
Returns the parameter chain; access to private attribute m_paramChain.