void ScatterQoI<PHAL::AlbanyTraits::Jacobian, Traits>:: evaluateFields(typename Traits::EvalData workset) { const int neq = workset.wsElNodeEqID[0][0].size(); const int nunk = neq*numNodes; Teuchos::RCP<Tpetra_Vector> dqdx = workset.qoi; for (int cell=0; cell < workset.numCells; ++cell) { Teuchos::ArrayRCP<Teuchos::ArrayRCP<int> > const& nodeID = workset.wsElNodeEqID[cell]; int lunk=0; for (int node=0; node < numNodes; ++node) { for (int eq=0; eq < neq; ++eq) { const LO row = nodeID[node][eq]; dqdx->sumIntoLocalValue(row, qoi(cell).fastAccessDx(lunk)); lunk++; } } } }
void infer_slope(const QUESO::FullEnvironment & env) { // Statistical Inverse Problem: Compute posterior pdf for slope 'm' and y-intercept c // Step 1: Instantiate the parameter space QUESO::VectorSpace<> paramSpace(env, "param_", 2, NULL); // 2 since we have a 2D problem // Step 2: Parameter domain QUESO::GslVector paramMinValues(paramSpace.zeroVector()); QUESO::GslVector paramMaxValues(paramSpace.zeroVector()); paramMinValues[0] = 2.; paramMaxValues[0] = 5.; paramMinValues[1] = 3.; paramMaxValues[1] = 7.; QUESO::BoxSubset<> paramDomain("param_", paramSpace, paramMinValues, paramMaxValues); // Step 3: Instantiate likelihood Likelihood<> lhood("like_", paramDomain); // Step 4: Define the prior RV QUESO::UniformVectorRV<> priorRv("prior_", paramDomain); // Step 5: Instantiate the inverse problem QUESO::GenericVectorRV<> postRv("post_", paramSpace); QUESO::StatisticalInverseProblem<> ip("", NULL, priorRv, lhood, postRv); // Step 6: Solve the inverse problem // Randomly sample for the initial state? QUESO::GslVector paramInitials(paramSpace.zeroVector()); priorRv.realizer().realization(paramInitials); // Initialize the Cov matrix: QUESO::GslMatrix proposalCovMatrix(paramSpace.zeroVector()); proposalCovMatrix(0,0) = std::pow(std::abs(paramInitials[0]) / 20.0, 2.0); proposalCovMatrix(1,1) = std::pow(std::abs(paramInitials[1]) / 20.0, 2.0); ip.solveWithBayesMetropolisHastings(NULL, paramInitials, &proposalCovMatrix); // Using the posterior pdfs for m and c, compute 'y' at a given 'x' // Step 1: Instantiate the qoi space QUESO::VectorSpace<> qoiSpace(env, "qoi_", 1, NULL); // Step 2: Instantiate the parameter domain // Not necessary here because the posterior from SIP is used as the RV for SFP // Step 3: Instantiate the qoi object to be used by QUESO Qoi<> qoi("qoi_", paramDomain, qoiSpace); // Step 4: Define the input RV // Not required because we use the posterior as RV // Step 5: Instantiate the forward problem QUESO::GenericVectorRV<> qoiRv("qoi_", qoiSpace); QUESO::StatisticalForwardProblem<> fp("", NULL, postRv, qoi, qoiRv); // Step 6: Solve the forward problem std::cout << "Solving the SFP with Monte Carlo" << std::endl << std::endl; fp.solveWithMonteCarlo(NULL); system("mv outputData/sfp_lineSlope_qoi_seq.txt outputData/sfp_lineSlope_qoi_seq_post.txt"); // SENSITIVITY ANALYSIS // For m Qoi_m<> qoi_m("qoi_", paramDomain, qoiSpace); // Step 4: Define the input RV // Not required because we use the prior as RV for sensitivity analysis // Step 5: Instantiate the forward problem QUESO::StatisticalForwardProblem<> fp_m("", NULL, priorRv, qoi_m, qoiRv); // Step 6: Solve the forward problem fp_m.solveWithMonteCarlo(NULL); system("mv outputData/sfp_lineSlope_qoi_seq.txt outputData/sense_m.txt"); // For c Qoi_c<> qoi_c("qoi_", paramDomain, qoiSpace); // Step 4: Define the input RV // Not required because we use the prior as RV for sensitivity analysis // Step 5: Instantiate the forward problem QUESO::StatisticalForwardProblem<> fp_c("", NULL, priorRv, qoi_c, qoiRv); // Step 6: Solve the forward problem fp_c.solveWithMonteCarlo(NULL); system("mv outputData/sfp_lineSlope_qoi_seq.txt outputData/sense_c.txt"); // For both, m and c Qoi_mc<> qoi_mc("qoi_", paramDomain, qoiSpace); // Step 4: Define the input RV // Not required because we use the prior as RV for sensitivity analysis // Step 5: Instantiate the forward problem QUESO::StatisticalForwardProblem<> fp_mc("", NULL, priorRv, qoi_mc, qoiRv); // Step 6: Solve the forward problem fp_mc.solveWithMonteCarlo(NULL); system("mv outputData/sfp_lineSlope_qoi_seq.txt outputData/sense_mc.txt"); }
int main(int argc, char* argv[]) { #ifndef QUESO_HAS_MPI // Skip this test if we're not in parallel return 77; #else MPI_Init(&argc, &argv); std::string inputFileName = argv[1]; const char * test_srcdir = std::getenv("srcdir"); if (test_srcdir) inputFileName = test_srcdir + ('/' + inputFileName); // Initialize QUESO environment QUESO::FullEnvironment env(MPI_COMM_WORLD, inputFileName, "", NULL); //================================================================ // Statistical inverse problem (SIP): find posterior PDF for 'g' //================================================================ //------------------------------------------------------ // SIP Step 1 of 6: Instantiate the parameter space //------------------------------------------------------ QUESO::VectorSpace<> paramSpace(env, "param_", 1, NULL); //------------------------------------------------------ // SIP Step 2 of 6: Instantiate the parameter domain //------------------------------------------------------ QUESO::GslVector paramMinValues(paramSpace.zeroVector()); QUESO::GslVector paramMaxValues(paramSpace.zeroVector()); paramMinValues[0] = 8.; paramMaxValues[0] = 11.; QUESO::BoxSubset<> paramDomain("param_", paramSpace, paramMinValues, paramMaxValues); //------------------------------------------------------ // SIP Step 3 of 6: Instantiate the likelihood function // object to be used by QUESO. //------------------------------------------------------ Likelihood<> lhood("like_", paramDomain); //------------------------------------------------------ // SIP Step 4 of 6: Define the prior RV //------------------------------------------------------ QUESO::UniformVectorRV<> priorRv("prior_", paramDomain); //------------------------------------------------------ // SIP Step 5 of 6: Instantiate the inverse problem //------------------------------------------------------ // Extra prefix before the default "rv_" prefix QUESO::GenericVectorRV<> postRv("post_", paramSpace); // No extra prefix before the default "ip_" prefix QUESO::StatisticalInverseProblem<> ip("", NULL, priorRv, lhood, postRv); //------------------------------------------------------ // SIP Step 6 of 6: Solve the inverse problem, that is, // set the 'pdf' and the 'realizer' of the posterior RV //------------------------------------------------------ QUESO::GslVector paramInitials(paramSpace.zeroVector()); priorRv.realizer().realization(paramInitials); QUESO::GslMatrix proposalCovMatrix(paramSpace.zeroVector()); proposalCovMatrix(0,0) = std::pow(std::abs(paramInitials[0]) / 20.0, 2.0); ip.solveWithBayesMetropolisHastings(NULL, paramInitials, &proposalCovMatrix); //================================================================ // Statistical forward problem (SFP): find the max distance // traveled by an object in projectile motion; input pdf for 'g' // is the solution of the SIP above. //================================================================ //------------------------------------------------------ // SFP Step 1 of 6: Instantiate the parameter *and* qoi spaces. // SFP input RV = FIP posterior RV, so SFP parameter space // has been already defined. //------------------------------------------------------ QUESO::VectorSpace<> qoiSpace(env, "qoi_", 1, NULL); //------------------------------------------------------ // SFP Step 2 of 6: Instantiate the parameter domain //------------------------------------------------------ // Not necessary because input RV of the SFP = output RV of SIP. // Thus, the parameter domain has been already defined. //------------------------------------------------------ // SFP Step 3 of 6: Instantiate the qoi object // to be used by QUESO. //------------------------------------------------------ Qoi<> qoi("qoi_", paramDomain, qoiSpace); //------------------------------------------------------ // SFP Step 4 of 6: Define the input RV //------------------------------------------------------ // Not necessary because input RV of SFP = output RV of SIP // (postRv). //------------------------------------------------------ // SFP Step 5 of 6: Instantiate the forward problem //------------------------------------------------------ QUESO::GenericVectorRV<> qoiRv("qoi_", qoiSpace); QUESO::StatisticalForwardProblem<> fp("", NULL, postRv, qoi, qoiRv); //------------------------------------------------------ // SFP Step 6 of 6: Solve the forward problem //------------------------------------------------------ fp.solveWithMonteCarlo(NULL); MPI_Finalize(); return 0; #endif // QUESO_HAS_MPI }