void computeGravityAndTraveledDistance(const QUESO::FullEnvironment& env) { struct timeval timevalNow; gettimeofday(&timevalNow, NULL); if (env.fullRank() == 0) { std::cout << "\nBeginning run of 'Gravity + Projectile motion' example at " << ctime(&timevalNow.tv_sec) << "\n my fullRank = " << env.fullRank() << "\n my subEnvironmentId = " << env.subId() << "\n my subRank = " << env.subRank() << "\n my interRank = " << env.inter0Rank() << std::endl << std::endl; } // Just examples of possible calls if ((env.subDisplayFile() ) && (env.displayVerbosity() >= 2)) { *env.subDisplayFile() << "Beginning run of 'Gravity + Projectile motion' example at " << ctime(&timevalNow.tv_sec) << std::endl; } env.fullComm().Barrier(); env.subComm().Barrier(); // Just an example of a possible call //================================================================ // Statistical inverse problem (SIP): find posterior PDF for 'g' //================================================================ gettimeofday(&timevalNow, NULL); if (env.fullRank() == 0) { std::cout << "Beginning 'SIP -> Gravity estimation' at " << ctime(&timevalNow.tv_sec) << std::endl; } //------------------------------------------------------ // SIP Step 1 of 6: Instantiate the parameter space //------------------------------------------------------ QUESO::VectorSpace<QUESO::GslVector,QUESO::GslMatrix> 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<QUESO::GslVector,QUESO::GslMatrix> paramDomain("param_", paramSpace, paramMinValues, paramMaxValues); //------------------------------------------------------ // SIP Step 3 of 6: Instantiate the likelihood function // object to be used by QUESO. //------------------------------------------------------ likelihoodRoutine_Data likelihoodRoutine_Data(env); QUESO::GenericScalarFunction<QUESO::GslVector,QUESO::GslMatrix> likelihoodFunctionObj("like_", paramDomain, likelihoodRoutine, (void *) &likelihoodRoutine_Data, true); // the routine computes [ln(function)] //------------------------------------------------------ // SIP Step 4 of 6: Define the prior RV //------------------------------------------------------ #ifdef PRIOR_IS_GAUSSIAN QUESO::GslVector meanVector( paramSpace.zeroVector() ); meanVector[0] = 9; QUESO::GslMatrix covMatrix = QUESO::GslMatrix(paramSpace.zeroVector()); covMatrix(0,0) = 1.; // Create a Gaussian prior RV QUESO::GaussianVectorRV<QUESO::GslVector,QUESO::GslMatrix> priorRv("prior_",paramDomain,meanVector,covMatrix); #else // Create an uniform prior RV QUESO::UniformVectorRV<QUESO::GslVector,QUESO::GslMatrix> priorRv("prior_",paramDomain); #endif //------------------------------------------------------ // SIP Step 5 of 6: Instantiate the inverse problem //------------------------------------------------------ QUESO::GenericVectorRV<QUESO::GslVector,QUESO::GslMatrix> postRv("post_", // Extra prefix before the default "rv_" prefix paramSpace); QUESO::StatisticalInverseProblem<QUESO::GslVector,QUESO::GslMatrix> ip("", // No extra prefix before the default "ip_" prefix NULL, priorRv, likelihoodFunctionObj, postRv); //------------------------------------------------------ // SIP Step 6 of 6: Solve the inverse problem, that is, // set the 'pdf' and the 'realizer' of the posterior RV //------------------------------------------------------ std::cout << "Solving the SIP with Metropolis Hastings" << std::endl << std::endl; QUESO::GslVector paramInitials(paramSpace.zeroVector()); priorRv.realizer().realization(paramInitials); QUESO::GslMatrix proposalCovMatrix(paramSpace.zeroVector()); proposalCovMatrix(0,0) = std::pow( fabs(paramInitials[0])/20. , 2. ); 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. //================================================================ gettimeofday(&timevalNow, NULL); std::cout << "Beginning 'SFP -> Projectile motion' at " << ctime(&timevalNow.tv_sec) << std::endl; //------------------------------------------------------ // 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<QUESO::GslVector,QUESO::GslMatrix> 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 function object // to be used by QUESO. //------------------------------------------------------ qoiRoutine_Data qoiRoutine_Data; qoiRoutine_Data.m_angle = M_PI/4.0; //45 degrees (radians) qoiRoutine_Data.m_initialVelocity= 5.; //initial speed (m/s) qoiRoutine_Data.m_initialHeight = 0.; //initial height (m) QUESO::GenericVectorFunction<QUESO::GslVector,QUESO::GslMatrix,QUESO::GslVector,QUESO::GslMatrix> qoiFunctionObj("qoi_", paramDomain, qoiSpace, qoiRoutine, (void *) &qoiRoutine_Data); //------------------------------------------------------ // 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<QUESO::GslVector,QUESO::GslMatrix> qoiRv("qoi_", qoiSpace); QUESO::StatisticalForwardProblem<QUESO::GslVector,QUESO::GslMatrix,QUESO::GslVector,QUESO::GslMatrix> fp("", NULL, postRv, qoiFunctionObj, qoiRv); //------------------------------------------------------ // SFP Step 6 of 6: Solve the forward problem //------------------------------------------------------ std::cout << "Solving the SFP with Monte Carlo" << std::endl << std::endl; fp.solveWithMonteCarlo(NULL); //------------------------------------------------------ gettimeofday(&timevalNow, NULL); if ((env.subDisplayFile() ) && (env.displayVerbosity() >= 2)) { *env.subDisplayFile() << "Ending run of 'Gravity + Projectile motion' example at " << ctime(&timevalNow.tv_sec) << std::endl; } if (env.fullRank() == 0) { std::cout << "Ending run of 'Gravity + Projectile motion' example at " << ctime(&timevalNow.tv_sec) << std::endl; } return; }
//------------------------------------------------------ //------------------------------------------------------ //------------------------------------------------------ void debug_hyst(const QUESO::FullEnvironment& env) { unsigned int numFloors = 4; unsigned int numTimeSteps = 401; std::vector<double> accel(numTimeSteps,0.); FILE *inp; inp = fopen("an.txt","r"); unsigned int numObservations = 0; double tmpA; while (fscanf(inp,"%lf",&tmpA) != EOF) { UQ_FATAL_TEST_MACRO((numObservations >= accel.size()), env.fullRank(), "debug_hyst()", "input file has too many lines"); accel[numObservations] = tmpA; numObservations++; } UQ_FATAL_TEST_MACRO((numObservations != accel.size()), env.fullRank(), "debug_hyst()", "input file has a smaller number of observations than expected"); QUESO::VectorSpace<> floorSpace(env, "floor_", numFloors, NULL); QUESO::GslVector kVec(floorSpace.zeroVector()); kVec[0] = 2.20e+7; kVec[1] = 2.00e+7; kVec[2] = 1.70e+7; kVec[3] = 1.45e+7; QUESO::GslVector rVec(floorSpace.zeroVector()); rVec[0] = 0.1; rVec[1] = 0.1; rVec[2] = 0.1; rVec[3] = 0.1; QUESO::GslVector uVec(floorSpace.zeroVector()); uVec[0] = 0.008; uVec[1] = 0.008; uVec[2] = 0.007; uVec[3] = 0.007; double rho = 7.959e-1 ;//0.1976; double gamma = 2.500e-3 ; //0.0038; std::vector<double> t(numTimeSteps,0.); QUESO::SequenceOfVectors<> u (floorSpace,numTimeSteps,""); // absolute displacement QUESO::SequenceOfVectors<> ud (floorSpace,numTimeSteps,""); // velocity QUESO::SequenceOfVectors<> udd (floorSpace,numTimeSteps,""); // acceleration QUESO::SequenceOfVectors<> resfor(floorSpace,numTimeSteps,""); // restoring force QUESO::SequenceOfVectors<> ru (floorSpace,numTimeSteps,""); // relative displacement u.setPositionValues (0,floorSpace.zeroVector()); ud.setPositionValues (0,floorSpace.zeroVector()); udd.setPositionValues (0,floorSpace.zeroVector()); resfor.setPositionValues(0,floorSpace.zeroVector()); ru.setPositionValues (0,floorSpace.zeroVector()); QUESO::GslVector massVec(floorSpace.zeroVector()); massVec.cwSet(2.0e+4); hystereticModel(env, massVec, kVec, rVec, uVec, rho, gamma, accel, t, // output u, ud, udd, resfor, ru); std::set<unsigned int> auxSet; auxSet.insert(0); // Writing some data to the file 'outputData/cpp_output.m' std::ofstream myFile; myFile.open ("outputData/cpp_output.m"); // Write 't_cpp' myFile << "t_cpp = zeros(" << 1 << "," << numTimeSteps << ");\n" << "t_cpp = ["; for (unsigned int j = 0; j < numTimeSteps; ++j) { myFile << t[j] << " "; } myFile << "];" << std::endl; // Write 'a_cpp' myFile << "a_cpp = zeros(" << 1 << "," << numTimeSteps << ");\n" << "a_cpp = ["; for (unsigned int j = 0; j < numTimeSteps; ++j) { myFile << accel[j] << " "; } myFile << "];" << std::endl; QUESO::GslVector auxVec(floorSpace.zeroVector()); // Write 'u_cpp' myFile << "u_cpp = zeros(" << numFloors << "," << numTimeSteps << ");\n" << "u_cpp = ["; for (unsigned int i = 0; i < numFloors; ++i) { for (unsigned int j = 0; j < numTimeSteps; ++j) { u.getPositionValues(j,auxVec); myFile << auxVec[i] << " "; } myFile << std::endl; } myFile << "];" << std::endl; // Write 'ud_cpp' myFile << "ud_cpp = zeros(" << numFloors << "," << numTimeSteps << ");\n" << "ud_cpp = ["; for (unsigned int i = 0; i < numFloors; ++i) { for (unsigned int j = 0; j < numTimeSteps; ++j) { ud.getPositionValues(j,auxVec); myFile << auxVec[i] << " "; } myFile << std::endl; } myFile << "];" << std::endl; // Write 'udd_cpp' myFile << "udd_cpp = zeros(" << numFloors << "," << numTimeSteps << ");\n" << "udd_cpp = ["; for (unsigned int i = 0; i < numFloors; ++i) { for (unsigned int j = 0; j < numTimeSteps; ++j) { udd.getPositionValues(j,auxVec); myFile << auxVec[i] << " "; } myFile << std::endl; } myFile << "];" << std::endl; // Write 'resfor_cpp' myFile << "resfor_cpp = zeros(" << numFloors << "," << numTimeSteps << ");\n" << "resfor_cpp = ["; for (unsigned int i = 0; i < numFloors; ++i) { for (unsigned int j = 0; j < numTimeSteps; ++j) { resfor.getPositionValues(j,auxVec); myFile << auxVec[i] << " "; } myFile << std::endl; } myFile << "];" << std::endl; // Write 'ru_cpp' myFile << "ru_cpp = zeros(" << numFloors << "," << numTimeSteps << ");\n" << "ru_cpp = ["; for (unsigned int i = 0; i < numFloors; ++i) { for (unsigned int j = 0; j < numTimeSteps; ++j) { ru.getPositionValues(j,auxVec); myFile << auxVec[i] << " "; } myFile << std::endl; } myFile << "];" << std::endl; myFile.close(); return; }