25 #include <queso/SimulationModel.h>
26 #include <queso/GslVector.h>
27 #include <queso/GslMatrix.h>
31 template<
class S_V,
class S_M,
class P_V,
class P_M,
class Q_V,
class Q_M>
37 m_env (simulStorage.env()),
38 m_optionsObj (alternativeOptionsValues),
39 m_paper_p_x (simulStorage.scenarioSpace().dimLocal()),
40 m_paper_p_t (simulStorage.parameterSpace().dimLocal()),
41 m_paper_m (simulStorage.numSimulations()),
42 m_paper_n_eta (simulStorage.outputSpace().dimLocal()),
43 m_p_x_space (m_env,
"p_x_", m_paper_p_x, NULL),
44 m_xSeq_original (m_p_x_space, m_paper_m,
"simul_xSeq_original"),
45 m_xSeq_original_mins (m_p_x_space.zeroVector()),
46 m_xSeq_original_maxs (m_p_x_space.zeroVector()),
47 m_xSeq_original_ranges (m_p_x_space.zeroVector()),
48 m_xSeq_standard (m_p_x_space, m_paper_m,
"simul_xSeq_standard"),
49 m_xSeq_standard_mins (m_p_x_space.zeroVector()),
50 m_xSeq_standard_maxs (m_p_x_space.zeroVector()),
51 m_xSeq_standard_ranges (m_p_x_space.zeroVector()),
52 m_xs_asterisks_standard (simulStorage.xs_asterisks_original().size(),(const S_V*) NULL),
53 m_p_t_space (m_env,
"p_t_", m_paper_p_t, NULL),
54 m_tSeq_original (m_p_t_space, m_paper_m,
"simul_tSeq_original"),
55 m_tSeq_mins (m_p_t_space.zeroVector()),
56 m_tSeq_maxs (m_p_t_space.zeroVector()),
57 m_tSeq_ranges (m_p_t_space.zeroVector()),
58 m_tSeq_standard (m_p_t_space, m_paper_m,
"simul_tSeq_standard"),
59 m_ts_asterisks_standard (simulStorage.ts_asterisks_original().size(),(const P_V*) NULL),
60 m_n_eta_space (m_env,
"n_eta_", m_paper_n_eta, NULL),
61 m_etaSeq_original (m_n_eta_space, m_paper_m,
"simul_etaSeq_original"),
62 m_etaSeq_original_mean (m_n_eta_space.zeroVector()),
63 m_etaSeq_original_std (m_n_eta_space.zeroVector()),
64 #ifdef _GPMSA_CODE_TREATS_SIMULATION_VECTORS_IN_CHUNKS
65 m_etaSeq_chunkMeans (simulStorage.chunkSizes().size(),0.),
66 m_etaSeq_chunkStds (simulStorage.chunkSizes().size(),0.),
68 m_etaSeq_allMean (0.),
71 m_etaSeq_transformed (m_n_eta_space, m_paper_m,
"simul_etaSeq_transformed"),
72 m_etaSeq_transformed_mean (m_n_eta_space.zeroVector()),
73 m_etaSeq_transformed_std (m_n_eta_space.zeroVector()),
74 m_eta_space (m_env,
"m_eta_simul_model", m_paper_m*m_paper_n_eta, NULL),
75 m_etaVec_transformed (m_eta_space.zeroVector()),
76 m_etaMat_transformed (m_env, m_n_eta_space.map(), m_paper_m),
77 m_m_space (m_env,
"m_", m_paper_m, NULL),
78 m_m_unitVec (m_env, m_m_space.map(), 1.),
79 m_m_Imat (m_m_unitVec),
83 m_kvec_is (m_paper_p_eta, (Q_V*) NULL),
84 m_Kmat_is (m_paper_p_eta, (Q_M*) NULL),
88 *
m_env.
subDisplayFile() <<
"Entering SimulationModel<S_V,S_M,P_V,P_M,Q_V,Q_M>::constructor()"
89 <<
": prefix = " << prefix
90 <<
", alternativeOptionsValues = " << alternativeOptionsValues
126 for (
unsigned int i = 0; i <
m_paper_m; ++i) {
133 for (
unsigned int i = 0; i <
m_paper_m; ++i) {
149 <<
": finished setting 'm_xSeq_original'"
150 <<
", computing mins of 'm_xSeq_original'"
151 <<
", computing ranges of 'm_xSeq_original'"
152 <<
", computing 'm_xSeq_standard'"
156 for (
unsigned int i = 0; i <
m_paper_m; ++i) {
163 for (
unsigned int i = 0; i <
m_paper_m; ++i) {
171 for (
unsigned int i = 0; i <
m_paper_m; ++i) {
186 <<
": finished forming 'm_xs_asterisks_standard'"
194 for (
unsigned int i = 0; i <
m_paper_m; ++i) {
201 for (
unsigned int i = 0; i <
m_paper_m; ++i) {
217 <<
": finished setting 'm_tSeq_original'"
218 <<
", computing mins of 'm_tSeq_original'"
219 <<
", computing ranges of 'm_tSeq_original'"
220 <<
", computing 'm_tSeq_standard'"
224 for (
unsigned int i = 0; i <
m_paper_m; ++i) {
231 for (
unsigned int i = 0; i <
m_paper_m; ++i) {
239 for (
unsigned int i = 0; i <
m_paper_m; ++i) {
254 <<
": finished forming 'm_ts_asterisks_standard'"
262 for (
unsigned int i = 0; i <
m_paper_m; ++i) {
268 for (
unsigned int i = 0; i <
m_paper_m; ++i) {
277 #ifdef _GPMSA_CODE_TREATS_SIMULATION_VECTORS_IN_CHUNKS
278 unsigned int cumulativeSize = 0;
279 for (
unsigned int chunkId = 0; chunkId < m_etaSeq_chunkStds.size(); ++chunkId) {
280 m_etaSeq_chunkMeans[chunkId] = 0.;
281 for (
unsigned int i = 0; i <
m_paper_m; ++i) {
283 for (
unsigned int j = cumulativeSize; j < (cumulativeSize + simulStorage.chunkSizes()[chunkId]); ++j) {
284 m_etaSeq_chunkMeans[chunkId] += tmpEtaVec[j];
287 m_etaSeq_chunkMeans[chunkId] /= ((double) (m_paper_m * simulStorage.chunkSizes()[chunkId]));
288 cumulativeSize += simulStorage.chunkSizes()[chunkId];
293 for (
unsigned int chunkId = 0; chunkId < m_etaSeq_chunkStds.size(); ++chunkId) {
294 m_etaSeq_chunkStds[chunkId] = 0.;
295 for (
unsigned int i = 0; i <
m_paper_m; ++i) {
297 for (
unsigned int j = cumulativeSize; j < (cumulativeSize + simulStorage.chunkSizes()[chunkId]); ++j) {
298 double diff = tmpEtaVec[j] - m_etaSeq_chunkMeans[chunkId];
299 m_etaSeq_chunkStds[chunkId] += diff * diff;
302 m_etaSeq_chunkStds[chunkId] = sqrt( m_etaSeq_chunkStds[chunkId] / ((
double) (m_paper_m * simulStorage.chunkSizes()[chunkId] - 1)) );
303 cumulativeSize += simulStorage.chunkSizes()[chunkId];
307 for (
unsigned int i = 0; i <
m_paper_m; ++i) {
310 for (
unsigned int chunkId = 0; chunkId < m_etaSeq_chunkStds.size(); ++chunkId) {
311 for (
unsigned int j = cumulativeSize; j < (cumulativeSize + simulStorage.chunkSizes()[chunkId]); ++j) {
312 tmpEtaVec[j] /= m_etaSeq_chunkStds[chunkId];
314 cumulativeSize += simulStorage.chunkSizes()[chunkId];
321 for (
unsigned int i = 0; i <
m_paper_m; ++i) {
330 for (
unsigned int i = 0; i <
m_paper_m; ++i) {
339 for (
unsigned int i = 0; i <
m_paper_m; ++i) {
348 <<
": finished setting 'm_etaSeq_original'"
349 <<
", computing means of 'm_etaSeq_original'"
350 <<
", computing sample stds of 'm_etaSeq_original'"
351 #ifdef _GPMSA_CODE_TREATS_SIMULATION_VECTORS_IN_CHUNKS
352 <<
", computing 'm_etaSeq_chunkMeans'"
353 <<
", computing 'm_etaSeq_chunkStds'"
358 <<
", computing 'm_etaSeq_transformed'"
362 #ifdef _GPMSA_CODE_TREATS_SIMULATION_VECTORS_IN_CHUNKS
365 <<
": 'm_etaSeq_chunkMeans' =";
366 for (
unsigned int chunkId = 0; chunkId < m_etaSeq_chunkStds.size(); ++chunkId) {
370 <<
": 'm_etaSeq_chunkStds' =";
371 for (
unsigned int chunkId = 0; chunkId < m_etaSeq_chunkStds.size(); ++chunkId) {
381 <<
": before forming 'm_etaVec_transformed' and 'm_etaMat_transformed"
383 <<
"\n m_paper_m = " << m_paper_m
391 unsigned int cumulativeEtaVecPosition = 0;
392 for (
unsigned int i = 0; i <
m_paper_m; ++i) {
395 cumulativeEtaVecPosition += tmpEtaVec.sizeLocal();
402 for (
unsigned int i = 0; i <
m_paper_m; ++i) {
409 for (
unsigned int i = 0; i <
m_paper_m; ++i) {
423 <<
": finished forming 'm_etaVec_transformed' and 'm_etaMat_transformed'"
447 <<
"\n iRC from m_etaMat_transformed.svd(1) = " << iRC
454 <<
": finished performing svd(1) on 'm_etaMat_transformed'"
455 <<
"\n svdU_mat =\n" << svdU_mat
456 <<
"\n svdS_vec =\n" << svdS_vec
457 <<
"\n svdVt_mat =\n" << svdVt_mat
481 if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 2)) {
482 *m_env.subDisplayFile() <<
"In SimulationModel<S_V,S_M,P_V,P_M,Q_V,Q_M>::constructor()"
484 <<
", m_Kmat_eta just created (situation 1; not yet populated)"
485 <<
", numRowsLocal = " << m_Kmat_eta->numRowsLocal()
486 <<
", numCols = " << m_Kmat_eta->numCols()
496 svdU_mat.getColumn(i, vecU_tmp);
497 matU_tmp.setColumn(i, vecU_tmp);
502 matS_tmp(i,i) = svdS_vec[i];
505 (*m_Kmat_eta) = matU_tmp * matS_tmp;
507 if (m_env.identifyingString() ==
"CASLexample") {
509 if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 3)) {
510 *m_env.subDisplayFile() <<
"IMPORTANT In SimulationModel<S_V,S_M,P_V,P_M,Q_V,Q_M>::constructor()"
511 <<
": multiplying first and third columns of 'm_Kmat_eta' by '-1' in order to match the results of Matlab GPMSA"
514 for (
unsigned int i = 0; i < m_Kmat_eta->numRowsLocal(); ++i) {
515 (*m_Kmat_eta)(i,0) *= -1.;
517 for (
unsigned int i = 0; i < m_Kmat_eta->numRowsLocal(); ++i) {
518 (*m_Kmat_eta)(i,2) *= -1.;
529 Q_M etaMat_transfored_transpose(svdU_mat);
531 int iRC = etaMat_transfored_transpose.svd(svdU_mat, svdS_vec, svdVt_mat);
535 <<
"\n iRC from m_etaMat_transformed.svd(2) = " << iRC
542 <<
": finished performing svd(2) on 'm_etaMat_transformed'"
543 <<
"\n svdU_mat =\n" << svdU_mat
544 <<
"\n svdS_vec =\n" << svdS_vec
545 <<
"\n svdVt_mat =\n" << svdVt_mat
569 if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 2)) {
570 *m_env.subDisplayFile() <<
"In SimulationModel<S_V,S_M,P_V,P_M,Q_V,Q_M>::constructor()"
572 <<
", m_Kmat_eta just created (situation 2; not yet populated)"
573 <<
", numRowsLocal = " << m_Kmat_eta->numRowsLocal()
574 <<
", numCols = " << m_Kmat_eta->numCols()
581 Q_M svdV_mat(svdVt_mat.transpose());
585 svdV_mat.getColumn(i, vecV_tmp);
586 matV_tmp.setColumn(i, vecV_tmp);
591 matS_tmp(i,i) = svdS_vec[i];
594 (*m_Kmat_eta) = matV_tmp * matS_tmp;
596 if (m_env.identifyingString() ==
"towerExampleToMatchGPMSA") {
598 if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 3)) {
599 *m_env.subDisplayFile() <<
"IMPORTANT In SimulationModel<S_V,S_M,P_V,P_M,Q_V,Q_M>::constructor()"
600 <<
": multiplying first column of 'm_Kmat_eta' by '-1' in order to match the results of Matlab GPMSA"
603 for (
unsigned int i = 0; i < m_Kmat_eta->numRowsLocal(); ++i) {
604 (*m_Kmat_eta)(i,0) *= -1.;
608 (*m_Kmat_eta) /= sqrt(m_paper_m);
613 <<
": finished forming 'm_Kmat_eta'"
614 <<
"\n (key-debug) *m_Kmat_eta =\n" << *
m_Kmat_eta
618 std::set<unsigned int> tmpSet;
634 <<
", m_Kmat just created (not yet populated)"
635 <<
", numRowsLocal = " <<
m_Kmat->numRowsLocal()
636 <<
", numCols = " <<
m_Kmat->numCols()
650 <<
": finished computing 'm_kvec_is'"
662 <<
", before 'm_Kmat_is[i]->fillWithTensorProduct()"
663 <<
", m_Kmat_is[" << i <<
"]->numRowsLocal() = " <<
m_Kmat_is[i]->numRowsLocal()
664 <<
", m_Kmat_is[" << i <<
"]->numCols() = " <<
m_Kmat_is[i]->numCols()
665 <<
", m_m_Imat.numRowsLocal() = " <<
m_m_Imat.numRowsLocal()
666 <<
", m_m_Imat.numCols() = " <<
m_m_Imat.numCols()
667 <<
", m_kvec_is[" << i <<
"]->sizeLocal() = " <<
m_kvec_is[i]->sizeLocal()
675 <<
": finished computing 'm_Kmat_is'"
684 <<
": before 'm_Kmat->fillWithBlocksDiagonal()"
685 <<
"\n m_Kmat->numRowsLocal() = " <<
m_Kmat->numRowsLocal()
686 <<
"\n m_Kmat->numCols() = " <<
m_Kmat->numCols()
692 *
m_env.
subDisplayFile() <<
"Leaving SimulationModel<S_V,S_M,P_V,P_M,Q_V,Q_M>::constructor()"
698 template<
class S_V,
class S_M,
class P_V,
class P_M,
class Q_V,
class Q_M>
701 if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 2)) {
702 *m_env.subDisplayFile() <<
"Entering SimulationModel<S_V,S_M,P_V,P_M,Q_V,Q_M>::destructor()..."
707 for (
unsigned int i = 0; i < m_paper_p_eta; ++i) {
712 delete m_p_eta_space;
713 for (
unsigned int i = 0; i < m_paper_m; ++i) {
714 delete m_xs_asterisks_standard[i];
715 delete m_ts_asterisks_standard[i];
717 if (m_optionsObj)
delete m_optionsObj;
719 if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 2)) {
720 *m_env.subDisplayFile() <<
"Leaving SimulationModel<S_V,S_M,P_V,P_M,Q_V,Q_M>::destructor()"
725 template<
class S_V,
class S_M,
class P_V,
class P_M,
class Q_V,
class Q_M>
729 unsigned int result = 0;
730 if (m_optionsObj->m_cdfThresholdForPEta <= 0.) {
731 result = m_optionsObj->m_p_eta;
733 else if (m_optionsObj->m_cdfThresholdForPEta >= 1.) {
734 for (
unsigned int i = 0; i < svdS_vec.sizeLocal(); ++i) {
735 if ((svdS_vec[i]/svdS_vec[0]) > m_optionsObj->m_zeroRelativeSingularValue) {
741 Q_V auxVec(svdS_vec);
743 for (
unsigned int i = 0; i < auxVec.sizeLocal(); ++i) {
744 double auxTerm = auxVec[i];
745 auxVec[i] = auxTerm * auxTerm;
748 for (
unsigned int i = 0; i < auxVec.sizeLocal(); ++i) {
751 Q_V auxCumSum(m_m_space.zeroVector());
752 auxCumSum[0] = auxVec[0];
753 for (
unsigned int i = 1; i < auxVec.sizeLocal(); ++i) {
754 auxCumSum[i] = auxCumSum[i-1] + auxVec[i];
756 for (
unsigned int i = 0; i < auxVec.sizeLocal(); ++i) {
757 if (auxCumSum[i] < m_optionsObj->m_cdfThresholdForPEta) {
767 template<
class S_V,
class S_M,
class P_V,
class P_M,
class Q_V,
class Q_M>
771 return m_paper_p_eta;
774 template<
class S_V,
class S_M,
class P_V,
class P_M,
class Q_V,
class Q_M>
775 const std::vector<const S_V* >&
778 return m_xs_asterisks_standard;
781 template<
class S_V,
class S_M,
class P_V,
class P_M,
class Q_V,
class Q_M>
785 return m_xSeq_original_mins;
788 template<
class S_V,
class S_M,
class P_V,
class P_M,
class Q_V,
class Q_M>
792 return m_xSeq_original_ranges;
795 template<
class S_V,
class S_M,
class P_V,
class P_M,
class Q_V,
class Q_M>
796 const std::vector<const P_V* >&
799 return m_ts_asterisks_standard;
802 template<
class S_V,
class S_M,
class P_V,
class P_M,
class Q_V,
class Q_M>
806 return m_etaSeq_original_mean;
809 #ifdef _GPMSA_CODE_TREATS_SIMULATION_VECTORS_IN_CHUNKS
810 template<
class S_V,
class S_M,
class P_V,
class P_M,
class Q_V,
class Q_M>
816 return m_etaSeq_chunkStds[chunkId];
819 template<
class S_V,
class S_M,
class P_V,
class P_M,
class Q_V,
class Q_M>
823 return m_etaSeq_allStd;
827 template<
class S_V,
class S_M,
class P_V,
class P_M,
class Q_V,
class Q_M>
838 return m_etaVec_transformed;
840 template<
class S_V,
class S_M,
class P_V,
class P_M,
class Q_V,
class Q_M>
844 return *(m_kvec_is[basisId]);
847 template<
class S_V,
class S_M,
class P_V,
class P_M,
class Q_V,
class Q_M>
854 template<
class S_V,
class S_M,
class P_V,
class P_M,
class Q_V,
class Q_M>
861 template<
class S_V,
class S_M,
class P_V,
class P_M,
class Q_V,
class Q_M>
868 return *m_simulationModelOptions;
871 template<
class S_V,
class S_M,
class P_V,
class P_M,
class Q_V,
class Q_M>
unsigned int displayVerbosity() const
unsigned int dimLocal() const
const S_V & xSeq_original_ranges() const
unsigned int computePEta(const Q_V &svdS_vec)
SequenceOfVectors< Q_V, Q_M > m_etaSeq_original
const SimulationModelOptions & optionsObj() const
VectorSpace< Q_V, Q_M > m_m_space
const Q_M & Kmat_eta() const
SequenceOfVectors< S_V, S_M > m_xSeq_standard
const Map & map() const
Map.
VectorSpace< S_V, S_M > m_p_x_space
void print(std::ostream &os) const
const std::vector< const S_V * > & xs_asterisks_original() const
const Q_V & etaSeq_original_mean() const
std::set< unsigned int > m_dataOutputAllowedSet
SequenceOfVectors< Q_V, Q_M > m_etaSeq_transformed
const BaseEnvironment & m_env
#define queso_require_less_equal_msg(expr1, expr2, msg)
std::vector< const S_V * > m_xs_asterisks_standard
VectorSpace< Q_V, Q_M > m_eta_space
unsigned int numBasis() const
const V & subMaxPlain() const
Finds the maximum value of the sub-sequence.
const V & zeroVector() const
Returns a vector filled with zeros.
Q_V m_etaSeq_transformed_std
void subSampleStd(unsigned int initialPos, unsigned int numPos, const V &meanVec, V &stdVec) const
Finds the sample standard deviation of the sub-sequence, considering numPos positions starting at pos...
#define queso_require_equal_to_msg(expr1, expr2, msg)
const Q_V & basisVec(unsigned int basisId) const
Q_V m_etaSeq_transformed_mean
double etaSeq_allStd() const
std::string optionsInputFileName() const
Access to the attribute m_optionsInputFileName, which stores the name of the input file passed by the...
void setPositionValues(unsigned int posId, const V &vec)
Set the values in vec at position posId of the sequence.
VectorSpace< Q_V, Q_M > m_n_eta_space
std::ofstream * subDisplayFile() const
Access function for m_subDisplayFile (displays file on stream).
double m_cdfThresholdForPEta
const V & subMinPlain() const
Finds the minimum value of the sub-sequence.
const std::vector< const P_V * > & ts_asterisks_original() const
unsigned int subId() const
Access function to the number of each sub-environment Id: m_subId.
unsigned int m_paper_p_eta
const std::vector< const P_V * > & ts_asterisks_standard() const
unsigned int subSequenceSize() const
Size of the sub-sequence of vectors.
std::vector< Q_M * > m_Kmat_is
VectorSpace< Q_V, Q_M > * m_p_eta_space
const SmOptionsValues * m_optionsObj
std::vector< Q_V * > m_kvec_is
const Q_V & etaVec_transformed(const std::string &debugString) const
#define queso_deprecated()
const std::vector< const S_V * > & xs_asterisks_standard() const
void getPositionValues(unsigned int posId, V &vec) const
Gets the values of the sequence at position posId and stores them at vec.
S_V m_xSeq_original_ranges
Q_V m_etaSeq_original_std
SequenceOfVectors< P_V, P_M > m_tSeq_standard
unsigned int m_paper_n_eta
SimulationModel(const char *prefix, const SmOptionsValues *alternativeOptionsValues, const SimulationStorage< S_V, S_M, P_V, P_M, Q_V, Q_M > &simulStorage)
SimulationModelOptions * m_simulationModelOptions
VectorSpace< P_V, P_M > m_p_t_space
const Q_V & outputVec_original(unsigned int simulationId) const
const S_V & xSeq_original_mins() const
Q_V m_etaSeq_original_mean
double m_zeroRelativeSingularValue
SequenceOfVectors< S_V, S_M > m_xSeq_original
const V & subMeanPlain() const
Finds the mean value of the sub-sequence.
std::vector< const P_V * > m_ts_asterisks_standard
S_V m_xSeq_standard_ranges
SequenceOfVectors< P_V, P_M > m_tSeq_original