int ImplicitGradient::computeGradient(double g) { // Compute gradients if this is a path-INdependent analysis // (This command only has effect if it IS path-independent.) //if (theSensAlgo != 0 && !(theSensAlgo->shouldComputeAtEachStep()) ) { if (theSensAlgo != 0){ theSensAlgo->computeSensitivities(); opserr<<" Implicit function SensAlgo is not zero"<<endln; } //} // Initialize gradient vector grad_g->Zero(); // get limit-state function from reliability domain int lsf = theReliabilityDomain->getTagOfActiveLimitStateFunction(); LimitStateFunction *theLimitStateFunction = theReliabilityDomain->getLimitStateFunctionPtr(lsf); const char *lsfExpression = theLimitStateFunction->getExpression(); // get parameters created in the domain int nparam = theOpenSeesDomain->getNumParameters(); Vector partials(nparam); // first check for dg/dimplicit partials for (int i = 0; i < nparam; i++) { // get parameter tag Parameter *theParam = theOpenSeesDomain->getParameterFromIndex(i); int tag = theParam->getTag(); if (theParam->isImplicit()) { // check for analytic gradient first on dg/dimplicit const char *gradExpression = theLimitStateFunction->getGradientExpression(tag); if (gradExpression != 0) { theFunctionEvaluator->setExpression(gradExpression); if (theFunctionEvaluator->setVariables() < 0) { opserr << "ERROR ImplicitGradient -- error setting variables in namespace" << endln; return -1; } partials(i) = theFunctionEvaluator->evaluateExpression(); // Reset limit state function in evaluator -- subsequent calls could receive gradient expression theFunctionEvaluator->setExpression(lsfExpression); } // if no analytic gradient automatically do finite differences to get dg/dimplicit else { // use parameter defined perturbation after updating implicit parameter theParam->update(0.0); double h = theParam->getPerturbation(); double original = theParam->getValue(); theParam->setValue(original+h); // set perturbed values in the variable namespace if (theFunctionEvaluator->setVariables() < 0) { opserr << "ERROR ImplicitGradient -- error setting variables in namespace" << endln; return -1; } // run analysis //if (theFunctionEvaluator->runAnalysis() < 0) { // opserr << "ERROR ImplicitGradient -- error running analysis" << endln; // return -1; //} // evaluate LSF and obtain result theFunctionEvaluator->setExpression(lsfExpression); // Add gradient contribution double g_perturbed = theFunctionEvaluator->evaluateExpression(); partials(i) = (g_perturbed-g)/h; // return values to previous state theParam->update(0.0); //opserr << "g_pert " << g_perturbed << ", g0 = " << g << endln; } } } //opserr << partials; // now loop through to create gradient vector // Mackie 7/31/2011: big consideration here is that you CANNOT have an explicit parameter appear in the // same LSF as an implicit parameter. For example, if there are two parameters: theta1 is modulus E and // theta2 is nodal displacement, then g(theta) = theta1 + theta2 // is not a viable LSF. Note that the implicit computation would need to consider // dg/dtheta1 + dg/du * du/dtheta1 // If you insist on solving this, use FiniteDifferenceGradient for (int i = 0; i < nparam; i++) { // get parameter tag Parameter *theParam = theOpenSeesDomain->getParameterFromIndex(i); int tag = theParam->getTag(); double result = 0; for (int j = 0; j < nparam; j++) { Parameter *theImplicit = theOpenSeesDomain->getParameterFromIndex(j); if (theImplicit->isImplicit()) { result = partials(j) * theImplicit->getSensitivity(i); (*grad_g)(i) += result; } } } return 0; }
double variance( const Sample& smp, bool with_pendant_length ) { // Init. double variance = 0.0; // Create PqueryPlain objects for every placement and copy all interesting data into it. // This way, we won't have to do all the pointer dereferencing during the actual calculations, // and furthermore, the data is close in memory. This gives a tremendous speedup! std::vector<PqueryPlain> vd_pqueries = plain_queries( smp ); // Also, calculate a matrix containing the pairwise distance between all nodes. this way, we // do not need to search a path between placements every time. auto node_distances = node_branch_length_distance_matrix(smp.tree()); #ifdef GENESIS_PTHREADS // Prepare storage for thread data. int num_threads = utils::Options::get().number_of_threads(); std::vector<double> partials(num_threads, 0.0); std::vector<std::thread> threads; // Start all threads. for (int i = 0; i < num_threads; ++i) { threads.emplace_back( &variance_thread_, i, num_threads, &vd_pqueries, &node_distances, &partials[i], with_pendant_length ); // threads[i] = new std::thread (); } // Wait for all threads to finish, collect their results. for (int i = 0; i < num_threads; ++i) { threads[i].join(); variance += partials[i]; } #else // Do a pairwise calculation on all placements. // int progress = 0; for (const PqueryPlain& pqry_a : vd_pqueries) { // LOG_PROG(++progress, vd_pqueries.size()) << "of Variance() finished."; variance += variance_partial_(pqry_a, vd_pqueries, node_distances, with_pendant_length); } #endif // Calculate normalizing factor. Should be the same value as given by placement_mass(), // however this calculation is probably faster, as we already have the plain values at hand. double mass = 0.0; for (const auto& pqry : vd_pqueries) { for (const auto& place : pqry.placements) { mass += place.like_weight_ratio * pqry.multiplicity; } } // As we conditionally compile the above, we add dummies here to avoid compiler warnings // about unused functions: (void) variance_thread_; (void) variance_partial_; // Return the normalized value. return ((variance / mass) / mass); }
int main(int argc,char *argv[]) { int exit_status; dataptr dz = NULL; char **cmdline; int cmdlinecnt; aplptr ap; int is_launched = FALSE; if(argc==2 && (strcmp(argv[1],"--version") == 0)) { fprintf(stdout,"%s\n",cdp_version); fflush(stdout); return 0; } /* CHECK FOR SOUNDLOOM */ if((sloom = sound_loom_in_use(&argc,&argv)) > 1) { sloom = 0; sloombatch = 1; } if(sflinit("cdp")){ sfperror("cdp: initialisation\n"); return(FAILED); } /* SET UP THE PRINCIPLE DATASTRUCTURE */ if((exit_status = establish_datastructure(&dz))<0) { // CDP LIB print_messages_and_close_sndfiles(exit_status,is_launched,dz); return(FAILED); } if(!sloom) { if(argc == 1) { usage1(); return(FAILED); } else if(argc == 2) { usage2(argv[1]); return(FAILED); } if((exit_status = make_initial_cmdline_check(&argc,&argv))<0) { // CDP LIB print_messages_and_close_sndfiles(exit_status,is_launched,dz); return(FAILED); } cmdline = argv; cmdlinecnt = argc; if((get_the_process_no(argv[0],dz))<0) return(FAILED); cmdline++; cmdlinecnt--; dz->maxmode = 4; if((get_the_mode_no(cmdline[0],dz))<0) return(FAILED); cmdline++; cmdlinecnt--; // setup_particular_application = if((exit_status = setup_getpartials_application(dz))<0) { print_messages_and_close_sndfiles(exit_status,is_launched,dz); return(FAILED); } if((exit_status = count_and_allocate_for_infiles(cmdlinecnt,cmdline,dz))<0) { // CDP LIB print_messages_and_close_sndfiles(exit_status,is_launched,dz); return(FAILED); } } else { //parse_TK_data() = if((exit_status = parse_sloom_data(argc,argv,&cmdline,&cmdlinecnt,dz))<0) { exit_status = print_messages_and_close_sndfiles(exit_status,is_launched,dz); return(exit_status); } } ap = dz->application; // parse_infile_and_hone_type() = if((exit_status = parse_infile_and_check_type(cmdline,dz))<0) { exit_status = print_messages_and_close_sndfiles(exit_status,is_launched,dz); return(FAILED); } // setup_param_ranges_and_defaults() = if((exit_status = setup_getpartials_param_ranges_and_defaults(dz))<0) { exit_status = print_messages_and_close_sndfiles(exit_status,is_launched,dz); return(FAILED); } // open_first_infile CDP LIB if((exit_status = open_first_infile(cmdline[0],dz))<0) { print_messages_and_close_sndfiles(exit_status,is_launched,dz); return(FAILED); } cmdlinecnt--; cmdline++; // handle_extra_infiles() : redundant // handle_outfile() = if((exit_status = handle_the_outfile(&cmdlinecnt,&cmdline,is_launched,dz))<0) { print_messages_and_close_sndfiles(exit_status,is_launched,dz); return(FAILED); } // handle_formants() redundant // handle_formant_quiksearch() redundant // handle_special_data() redundant if((exit_status = read_parameters_and_flags(&cmdline,&cmdlinecnt,dz))<0) { // CDP LIB print_messages_and_close_sndfiles(exit_status,is_launched,dz); return(FAILED); } //check_param_validity_and_consistency() redundant if((exit_status = check_getpartials_param_validity_and_consistency(dz))<0) { // CDP LIB print_messages_and_close_sndfiles(exit_status,is_launched,dz); return(FAILED); } is_launched = TRUE; //allocate_large_buffers() ... replaced by CDP LIB switch(dz->process) { case(PARTIALS_HARM): dz->extra_bufcnt = 0; dz->bptrcnt = 1; break; } if((exit_status = establish_spec_bufptrs_and_extra_buffers(dz))<0) { print_messages_and_close_sndfiles(exit_status,is_launched,dz); return(FAILED); } switch(dz->process) { case(PARTIALS_HARM): exit_status = allocate_single_buffer(dz); } if(exit_status < 0) { print_messages_and_close_sndfiles(exit_status,is_launched,dz); return(FAILED); } //param_preprocess() redundant //spec_process_file = if((exit_status = partials(dz))<0) { print_messages_and_close_sndfiles(exit_status,is_launched,dz); return(FAILED); } if((exit_status = complete_output(dz))<0) { // CDP LIB print_messages_and_close_sndfiles(exit_status,is_launched,dz); return(FAILED); } exit_status = print_messages_and_close_sndfiles(FINISHED,is_launched,dz); // CDP LIB free(dz); return(SUCCEEDED); }