double likelihoodRoutine( const QUESO::GslVector& paramValues, const QUESO::GslVector* paramDirection, const void* functionDataPtr, QUESO::GslVector* gradVector, QUESO::GslMatrix* hessianMatrix, QUESO::GslVector* hessianEffect) { likelihoodCounter++; if (paramDirection || functionDataPtr || gradVector || hessianMatrix || hessianEffect) {}; // just to remove compiler warning double returnValue = 0.; double x = paramValues[0]; double mean1 = 10.; double sigma1 = 1.; double y1 = (x-mean1)*(x-mean1)/(2.*sigma1*sigma1); double z1 = (1./sigma1/sqrt(2*M_PI))*exp(-y1); double mean2 = 100.; double sigma2 = 5.; double y2 = (x-mean2)*(x-mean2)/(2.*sigma2*sigma2); double z2 = (1./sigma2/sqrt(2*M_PI))*exp(-y2); double resultValue = -2*log((z1+2.*z2)/3.); if (resultValue == INFINITY) { //std::cerr << "WARNING In likelihoodRoutine" // << ", fullRank " << paramValues.env().fullRank() // << ", subEnvironment " << paramValues.env().subId() // << ", subRank " << paramValues.env().subRank() // << ", inter0Rank " << paramValues.env().inter0Rank() // << ": x = " << x // << ", z1 = " << z1 // << ", z2 = " << z2 // << ", resultValue = " << resultValue // << std::endl; resultValue = 1040.; } returnValue = -.5*resultValue; if (paramValues.env().exceptionalCircumstance()) { if ((paramValues.env().subDisplayFile() ) && (paramValues.env().displayVerbosity() > 0)) { // detailed output debug *paramValues.env().subDisplayFile() << "Leaving likelihood function" << ": paramValues = " << paramValues << ", returnValue = " << returnValue << std::endl; } } return returnValue; }
void qoiRoutine( const QUESO::GslVector& paramValues, const QUESO::GslVector* paramDirection, const void* functionDataPtr, QUESO::GslVector& qoiValues, QUESO::DistArray<QUESO::GslVector*>* gradVectors, QUESO::DistArray<QUESO::GslMatrix*>* hessianMatrices, QUESO::DistArray<QUESO::GslVector*>* hessianEffects) { // Logic just to avoid warnings from INTEL compiler const QUESO::GslVector* aux1 = paramDirection; if (aux1) {}; QUESO::DistArray<QUESO::GslVector*>* aux2 = gradVectors; if (aux2) {}; aux2 = hessianEffects; QUESO::DistArray<QUESO::GslMatrix*>* aux3 = hessianMatrices; if (aux3) {}; // Just checking: the user, at the application level, expects // vector 'paramValues' to have size 2 and // vector 'qoiValues' to have size 1. UQ_FATAL_TEST_MACRO(paramValues.sizeGlobal() != 2, QUESO::UQ_UNAVAILABLE_RANK, "qoiRoutine()", "paramValues vector does not have size 2"); UQ_FATAL_TEST_MACRO(qoiValues.sizeGlobal() != 1, QUESO::UQ_UNAVAILABLE_RANK, "qoiRoutine()", "qoiValues vector does not have size 1"); // Actual code // // This code exemplifies multiple Monte Carlo solvers, each calling this qoi routine. // In this simple example, only node 0 in each sub-environment does the job even though // there might be more than one node per sub-environment. // In a more realistic situation, if the user is asking for multiple nodes per sub- // environment, then the model code in the qoi routine might really demand more than one // node. Here we use 'env.subRank()' only. A realistic application might want to use // either 'env.subComm()' or 'env.subComm().Comm()'. const QUESO::BaseEnvironment& env = paramValues.env(); if (env.subRank() == 0) { double coef1 = ((qoiRoutine_DataType *) functionDataPtr)->coef1; double coef2 = ((qoiRoutine_DataType *) functionDataPtr)->coef2; qoiValues[0] = (coef1*paramValues[0] + coef2*paramValues[1]); } else { qoiValues[0] = 0.; } return; }
void qoiRoutine( const QUESO::GslVector& paramValues, const QUESO::GslVector* paramDirection, const void* functionDataPtr, QUESO::GslVector& qoiValues, QUESO::DistArray<QUESO::GslVector*>* gradVectors, QUESO::DistArray<QUESO::GslMatrix*>* hessianMatrices, QUESO::DistArray<QUESO::GslVector*>* hessianEffects) { //logic to avoid warnings from intel compiler const QUESO::GslVector* aux1 = paramDirection; if (aux1) {}; QUESO::DistArray<QUESO::GslVector*>* aux2 = gradVectors; if (aux2) {}; aux2 = hessianEffects; QUESO::DistArray<QUESO::GslMatrix*>* aux3 = hessianMatrices; if (aux3) {}; //checking size of paramValues and qoiValues //both should be size 1 UQ_FATAL_TEST_MACRO(paramValues.sizeGlobal() != 1, QUESO::UQ_UNAVAILABLE_RANK, "qoiRoutine()", "paramValues vector does not have size 1"); UQ_FATAL_TEST_MACRO(qoiValues.sizeGlobal() != 1, QUESO::UQ_UNAVAILABLE_RANK, "qoiRoutine()", "qoiValues vector does not have size 1"); //compute qoi const QUESO::BaseEnvironment& env = paramValues.env(); if(env.subRank() == 0) { double coef1 = ((qoiRoutine_DataType *) functionDataPtr)->coef1; qoiValues[0] = coef1*paramValues[0]; } else { qoiValues[0] = 0.; } return; }
// The actual (user defined) qoi routine void qoiRoutine(const QUESO::GslVector& paramValues, const QUESO::GslVector* paramDirection, const void* functionDataPtr, QUESO::GslVector& qoiValues, QUESO::DistArray<QUESO::GslVector*>* gradVectors, QUESO::DistArray<QUESO::GslMatrix*>* hessianMatrices, QUESO::DistArray<QUESO::GslVector*>* hessianEffects) { if (paramDirection && functionDataPtr && gradVectors && hessianMatrices && hessianEffects) { // Just to eliminate INTEL compiler warnings } double A = paramValues[0]; double E = paramValues[1]; double beta = ((qoiRoutine_Data *) functionDataPtr)->m_beta; double criticalMass = ((qoiRoutine_Data *) functionDataPtr)->m_criticalMass; double criticalTime = ((qoiRoutine_Data *) functionDataPtr)->m_criticalTime; double params[]={A,E,beta}; // integration const gsl_odeiv_step_type *T = gsl_odeiv_step_rkf45; //rkf45; //gear1; gsl_odeiv_step *s = gsl_odeiv_step_alloc(T,1); gsl_odeiv_control *c = gsl_odeiv_control_y_new(1e-6,0.0); gsl_odeiv_evolve *e = gsl_odeiv_evolve_alloc(1); gsl_odeiv_system sys = {func, NULL, 1, (void *)params}; double temperature = 0.1; double h = 1e-3; double Mass[1]; Mass[0]=1.; double temperature_old = 0.; double M_old[1]; M_old[0]=1.; double crossingTemperature = 0.; //unsigned int loopSize = 0; while ((temperature < criticalTime*beta) && (Mass[0] > criticalMass )) { int status = gsl_odeiv_evolve_apply(e, c, s, &sys, &temperature, criticalTime*beta, &h, Mass); UQ_FATAL_TEST_MACRO((status != GSL_SUCCESS), paramValues.env().fullRank(), "qoiRoutine()", "gsl_odeiv_evolve_apply() failed"); //printf("t = %6.1lf, mass = %10.4lf\n",t,Mass[0]); //loopSize++; if (Mass[0] <= criticalMass) { crossingTemperature = temperature_old + (temperature - temperature_old) * (M_old[0]-criticalMass)/(M_old[0]-Mass[0]); } temperature_old=temperature; M_old[0]=Mass[0]; } if (criticalMass > 0.) qoiValues[0] = crossingTemperature/beta; // QoI = time to achieve critical mass if (criticalTime > 0.) qoiValues[0] = Mass[0]; // QoI = mass fraction remaining at critical time //printf("loopSize = %d\n",loopSize); if ((paramValues.env().displayVerbosity() >= 3) && (paramValues.env().fullRank() == 0)) { printf("In qoiRoutine(), A = %g, E = %g, beta = %.3lf, criticalTime = %.3lf, criticalMass = %.3lf: qoi = %lf.\n",A,E,beta,criticalTime,criticalMass,qoiValues[0]); } gsl_odeiv_evolve_free (e); gsl_odeiv_control_free(c); gsl_odeiv_step_free (s); return; }
double Likelihood<V, M>::lnValue(const QUESO::GslVector & paramValues) const { double resultValue = 0.; m_env.subComm().Barrier(); //env.syncPrintDebugMsg("Entering likelihoodRoutine()",1,env.fullComm()); // Compute likelihood for scenario 1 double betaTest = m_beta1; if (betaTest) { double A = paramValues[0]; double E = paramValues[1]; double beta = m_beta1; double variance = m_variance1; const std::vector<double>& Te = m_Te1; const std::vector<double>& Me = m_Me1; std::vector<double> Mt(Me.size(),0.); double params[]={A,E,beta}; // integration const gsl_odeiv_step_type *T = gsl_odeiv_step_rkf45; //rkf45; //gear1; gsl_odeiv_step *s = gsl_odeiv_step_alloc(T,1); gsl_odeiv_control *c = gsl_odeiv_control_y_new(1e-6,0.0); gsl_odeiv_evolve *e = gsl_odeiv_evolve_alloc(1); gsl_odeiv_system sys = {func, NULL, 1, (void *)params}; double t = 0.1, t_final = 1900.; double h = 1e-3; double Mass[1]; Mass[0]=1.; unsigned int i = 0; double t_old = 0.; double M_old[1]; M_old[0]=1.; double misfit=0.; //unsigned int loopSize = 0; while ((t < t_final) && (i < Me.size())) { int status = gsl_odeiv_evolve_apply(e, c, s, &sys, &t, t_final, &h, Mass); UQ_FATAL_TEST_MACRO((status != GSL_SUCCESS), paramValues.env().fullRank(), "likelihoodRoutine()", "gsl_odeiv_evolve_apply() failed"); //printf("t = %6.1lf, mass = %10.4lf\n",t,Mass[0]); //loopSize++; while ( (i < Me.size()) && (t_old <= Te[i]) && (Te[i] <= t) ) { Mt[i] = (Te[i]-t_old)*(Mass[0]-M_old[0])/(t-t_old) + M_old[0]; misfit += (Me[i]-Mt[i])*(Me[i]-Mt[i]); //printf("%i %lf %lf %lf %lf\n",i,Te[i],Me[i],Mt[i],misfit); i++; } t_old=t; M_old[0]=Mass[0]; } resultValue += misfit/variance; //printf("loopSize = %d\n",loopSize); if ((paramValues.env().displayVerbosity() >= 10) && (paramValues.env().fullRank() == 0)) { printf("In likelihoodRoutine(), A = %g, E = %g, beta = %.3lf: misfit = %lf, likelihood = %lf.\n",A,E,beta,misfit,resultValue); } gsl_odeiv_evolve_free (e); gsl_odeiv_control_free(c); gsl_odeiv_step_free (s); } // Compute likelihood for scenario 2 betaTest = m_beta2; if (betaTest > 0.) { double A = paramValues[0]; double E = paramValues[1]; double beta = m_beta2; double variance = m_variance2; const std::vector<double>& Te = m_Te2; const std::vector<double>& Me = m_Me2; std::vector<double> Mt(Me.size(),0.); double params[]={A,E,beta}; // integration const gsl_odeiv_step_type *T = gsl_odeiv_step_rkf45; //rkf45; //gear1; gsl_odeiv_step *s = gsl_odeiv_step_alloc(T,1); gsl_odeiv_control *c = gsl_odeiv_control_y_new(1e-6,0.0); gsl_odeiv_evolve *e = gsl_odeiv_evolve_alloc(1); gsl_odeiv_system sys = {func, NULL, 1, (void *)params}; double t = 0.1, t_final = 1900.; double h = 1e-3; double Mass[1]; Mass[0]=1.; unsigned int i = 0; double t_old = 0.; double M_old[1]; M_old[0]=1.; double misfit=0.; //unsigned int loopSize = 0; while ((t < t_final) && (i < Me.size())) { int status = gsl_odeiv_evolve_apply(e, c, s, &sys, &t, t_final, &h, Mass); UQ_FATAL_TEST_MACRO((status != GSL_SUCCESS), paramValues.env().fullRank(), "likelihoodRoutine()", "gsl_odeiv_evolve_apply() failed"); //printf("t = %6.1lf, mass = %10.4lf\n",t,Mass[0]); //loopSize++; while ( (i < Me.size()) && (t_old <= Te[i]) && (Te[i] <= t) ) { Mt[i] = (Te[i]-t_old)*(Mass[0]-M_old[0])/(t-t_old) + M_old[0]; misfit += (Me[i]-Mt[i])*(Me[i]-Mt[i]); //printf("%i %lf %lf %lf %lf\n",i,Te[i],Me[i],Mt[i],misfit); i++; } t_old=t; M_old[0]=Mass[0]; } resultValue += misfit/variance; //printf("loopSize = %d\n",loopSize); if ((paramValues.env().displayVerbosity() >= 10) && (paramValues.env().fullRank() == 0)) { printf("In likelihoodRoutine(), A = %g, E = %g, beta = %.3lf: misfit = %lf, likelihood = %lf.\n",A,E,beta,misfit,resultValue); } gsl_odeiv_evolve_free (e); gsl_odeiv_control_free(c); gsl_odeiv_step_free (s); } // Compute likelihood for scenario 3 betaTest = m_beta3; if (betaTest > 0.) { double A = paramValues[0]; double E = paramValues[1]; double beta = m_beta3; double variance = m_variance3; const std::vector<double>& Te = m_Te3; const std::vector<double>& Me = m_Me3; std::vector<double> Mt(Me.size(),0.); double params[]={A,E,beta}; // integration const gsl_odeiv_step_type *T = gsl_odeiv_step_rkf45; //rkf45; //gear1; gsl_odeiv_step *s = gsl_odeiv_step_alloc(T,1); gsl_odeiv_control *c = gsl_odeiv_control_y_new(1e-6,0.0); gsl_odeiv_evolve *e = gsl_odeiv_evolve_alloc(1); gsl_odeiv_system sys = {func, NULL, 1, (void *)params}; double t = 0.1, t_final = 1900.; double h = 1e-3; double Mass[1]; Mass[0]=1.; unsigned int i = 0; double t_old = 0.; double M_old[1]; M_old[0]=1.; double misfit=0.; //unsigned int loopSize = 0; while ((t < t_final) && (i < Me.size())) { int status = gsl_odeiv_evolve_apply(e, c, s, &sys, &t, t_final, &h, Mass); UQ_FATAL_TEST_MACRO((status != GSL_SUCCESS), paramValues.env().fullRank(), "likelihoodRoutine()", "gsl_odeiv_evolve_apply() failed"); //printf("t = %6.1lf, mass = %10.4lf\n",t,Mass[0]); //loopSize++; while ( (i < Me.size()) && (t_old <= Te[i]) && (Te[i] <= t) ) { Mt[i] = (Te[i]-t_old)*(Mass[0]-M_old[0])/(t-t_old) + M_old[0]; misfit += (Me[i]-Mt[i])*(Me[i]-Mt[i]); //printf("%i %lf %lf %lf %lf\n",i,Te[i],Me[i],Mt[i],misfit); i++; } t_old=t; M_old[0]=Mass[0]; } resultValue += misfit/variance; //printf("loopSize = %d\n",loopSize); if ((paramValues.env().displayVerbosity() >= 10) && (paramValues.env().fullRank() == 0)) { printf("In likelihoodRoutine(), A = %g, E = %g, beta = %.3lf: misfit = %lf, likelihood = %lf.\n",A,E,beta,misfit,resultValue); } gsl_odeiv_evolve_free (e); gsl_odeiv_control_free(c); gsl_odeiv_step_free (s); } m_env.subComm().Barrier(); //env.syncPrintDebugMsg("Leaving likelihoodRoutine()",1,env.fullComm()); return -.5*resultValue; }
//******************************************************** // The 'unified comparison stage' of the driving routine "uqAppl()" //******************************************************** void uqAppl_UnifiedComparisonStage(QUESO::ValidationCycle<QUESO::GslVector,QUESO::GslMatrix,QUESO::GslVector,QUESO::GslMatrix>& cycle) { if (cycle.calFP().computeSolutionFlag() && cycle.valFP().computeSolutionFlag()) { #ifdef QUESO_COMPUTES_EXTRA_POST_PROCESSING_STATISTICS QUESO::GslVector cdfDistancesVec(cycle.calFP().qoiRv().imageSet().vectorSpace().zeroVector()); QUESO::GslVector epsilonVec (cycle.calFP().qoiRv().imageSet().vectorSpace().zeroVector()); // Epsilon = 0.02 epsilonVec.cwSet(0.02); horizontalDistances(cycle.calFP().qoiRv_unifiedCdf(), cycle.valFP().qoiRv_unifiedCdf(), epsilonVec, cdfDistancesVec); if (cycle.env().fullRank() == 0) { std::cout << "For epsilonVec = " << epsilonVec << ", unifiedCdfDistancesVec = " << cdfDistancesVec << std::endl; } // Test independence of 'distance' w.r.t. order of cdfs horizontalDistances(cycle.valFP().qoiRv_unifiedCdf(), cycle.calFP().qoiRv_unifiedCdf(), epsilonVec, cdfDistancesVec); if (cycle.env().fullRank() == 0) { std::cout << "For epsilonVec = " << epsilonVec << ", unifiedCdfDistancesVec (switched order of cdfs) = " << cdfDistancesVec << std::endl; } // Epsilon = 0.04 epsilonVec.cwSet(0.04); horizontalDistances(cycle.calFP().qoiRv_unifiedCdf(), cycle.valFP().qoiRv_unifiedCdf(), epsilonVec, cdfDistancesVec); if (cycle.env().fullRank() == 0) { std::cout << "For epsilonVec = " << epsilonVec << ", unifiedCdfDistancesVec = " << cdfDistancesVec << std::endl; } // Epsilon = 0.06 epsilonVec.cwSet(0.06); horizontalDistances(cycle.calFP().qoiRv_unifiedCdf(), cycle.valFP().qoiRv_unifiedCdf(), epsilonVec, cdfDistancesVec); if (cycle.env().fullRank() == 0) { std::cout << "For epsilonVec = " << epsilonVec << ", unifiedCdfDistancesVec = " << cdfDistancesVec << std::endl; } // Epsilon = 0.08 epsilonVec.cwSet(0.08); horizontalDistances(cycle.calFP().qoiRv_unifiedCdf(), cycle.valFP().qoiRv_unifiedCdf(), epsilonVec, cdfDistancesVec); if (cycle.env().fullRank() == 0) { std::cout << "For epsilonVec = " << epsilonVec << ", unifiedCdfDistancesVec = " << cdfDistancesVec << std::endl; } // Epsilon = 0.10 epsilonVec.cwSet(0.10); horizontalDistances(cycle.calFP().qoiRv_unifiedCdf(), cycle.valFP().qoiRv_unifiedCdf(), epsilonVec, cdfDistancesVec); if (cycle.env().fullRank() == 0) { std::cout << "For epsilonVec = " << epsilonVec << ", unifiedCdfDistancesVec = " << cdfDistancesVec << std::endl; } #endif } return; }