int main(void) { /* Local scalars */ char compz, compz_i; lapack_int n, n_i; lapack_int ldz, ldz_i; lapack_int ldz_r; lapack_int info, info_i; lapack_int i; int failed; /* Local arrays */ float *d = NULL, *d_i = NULL; float *e = NULL, *e_i = NULL; lapack_complex_float *z = NULL, *z_i = NULL; float *work = NULL, *work_i = NULL; float *d_save = NULL; float *e_save = NULL; lapack_complex_float *z_save = NULL; lapack_complex_float *z_r = NULL; /* Iniitialize the scalar parameters */ init_scalars_csteqr( &compz, &n, &ldz ); ldz_r = n+2; compz_i = compz; n_i = n; ldz_i = ldz; /* Allocate memory for the LAPACK routine arrays */ d = (float *)LAPACKE_malloc( n * sizeof(float) ); e = (float *)LAPACKE_malloc( (n-1) * sizeof(float) ); z = (lapack_complex_float *) LAPACKE_malloc( ldz*n * sizeof(lapack_complex_float) ); work = (float *)LAPACKE_malloc( ((MAX(1,2*n-2))) * sizeof(float) ); /* Allocate memory for the C interface function arrays */ d_i = (float *)LAPACKE_malloc( n * sizeof(float) ); e_i = (float *)LAPACKE_malloc( (n-1) * sizeof(float) ); z_i = (lapack_complex_float *) LAPACKE_malloc( ldz*n * sizeof(lapack_complex_float) ); work_i = (float *)LAPACKE_malloc( ((MAX(1,2*n-2))) * sizeof(float) ); /* Allocate memory for the backup arrays */ d_save = (float *)LAPACKE_malloc( n * sizeof(float) ); e_save = (float *)LAPACKE_malloc( (n-1) * sizeof(float) ); z_save = (lapack_complex_float *) LAPACKE_malloc( ldz*n * sizeof(lapack_complex_float) ); /* Allocate memory for the row-major arrays */ z_r = (lapack_complex_float *) LAPACKE_malloc( n*(n+2) * sizeof(lapack_complex_float) ); /* Initialize input arrays */ init_d( n, d ); init_e( (n-1), e ); init_z( ldz*n, z ); init_work( (MAX(1,2*n-2)), work ); /* Backup the ouptut arrays */ for( i = 0; i < n; i++ ) { d_save[i] = d[i]; } for( i = 0; i < (n-1); i++ ) { e_save[i] = e[i]; } for( i = 0; i < ldz*n; i++ ) { z_save[i] = z[i]; } /* Call the LAPACK routine */ csteqr_( &compz, &n, d, e, z, &ldz, work, &info ); /* Initialize input data, call the column-major middle-level * interface to LAPACK routine and check the results */ for( i = 0; i < n; i++ ) { d_i[i] = d_save[i]; } for( i = 0; i < (n-1); i++ ) { e_i[i] = e_save[i]; } for( i = 0; i < ldz*n; i++ ) { z_i[i] = z_save[i]; } for( i = 0; i < (MAX(1,2*n-2)); i++ ) { work_i[i] = work[i]; } info_i = LAPACKE_csteqr_work( LAPACK_COL_MAJOR, compz_i, n_i, d_i, e_i, z_i, ldz_i, work_i ); failed = compare_csteqr( d, d_i, e, e_i, z, z_i, info, info_i, compz, ldz, n ); if( failed == 0 ) { printf( "PASSED: column-major middle-level interface to csteqr\n" ); } else { printf( "FAILED: column-major middle-level interface to csteqr\n" ); } /* Initialize input data, call the column-major high-level * interface to LAPACK routine and check the results */ for( i = 0; i < n; i++ ) { d_i[i] = d_save[i]; } for( i = 0; i < (n-1); i++ ) { e_i[i] = e_save[i]; } for( i = 0; i < ldz*n; i++ ) { z_i[i] = z_save[i]; } for( i = 0; i < (MAX(1,2*n-2)); i++ ) { work_i[i] = work[i]; } info_i = LAPACKE_csteqr( LAPACK_COL_MAJOR, compz_i, n_i, d_i, e_i, z_i, ldz_i ); failed = compare_csteqr( d, d_i, e, e_i, z, z_i, info, info_i, compz, ldz, n ); if( failed == 0 ) { printf( "PASSED: column-major high-level interface to csteqr\n" ); } else { printf( "FAILED: column-major high-level interface to csteqr\n" ); } /* Initialize input data, call the row-major middle-level * interface to LAPACK routine and check the results */ for( i = 0; i < n; i++ ) { d_i[i] = d_save[i]; } for( i = 0; i < (n-1); i++ ) { e_i[i] = e_save[i]; } for( i = 0; i < ldz*n; i++ ) { z_i[i] = z_save[i]; } for( i = 0; i < (MAX(1,2*n-2)); i++ ) { work_i[i] = work[i]; } if( LAPACKE_lsame( compz, 'i' ) || LAPACKE_lsame( compz, 'v' ) ) { LAPACKE_cge_trans( LAPACK_COL_MAJOR, n, n, z_i, ldz, z_r, n+2 ); } info_i = LAPACKE_csteqr_work( LAPACK_ROW_MAJOR, compz_i, n_i, d_i, e_i, z_r, ldz_r, work_i ); if( LAPACKE_lsame( compz, 'i' ) || LAPACKE_lsame( compz, 'v' ) ) { LAPACKE_cge_trans( LAPACK_ROW_MAJOR, n, n, z_r, n+2, z_i, ldz ); } failed = compare_csteqr( d, d_i, e, e_i, z, z_i, info, info_i, compz, ldz, n ); if( failed == 0 ) { printf( "PASSED: row-major middle-level interface to csteqr\n" ); } else { printf( "FAILED: row-major middle-level interface to csteqr\n" ); } /* Initialize input data, call the row-major high-level * interface to LAPACK routine and check the results */ for( i = 0; i < n; i++ ) { d_i[i] = d_save[i]; } for( i = 0; i < (n-1); i++ ) { e_i[i] = e_save[i]; } for( i = 0; i < ldz*n; i++ ) { z_i[i] = z_save[i]; } for( i = 0; i < (MAX(1,2*n-2)); i++ ) { work_i[i] = work[i]; } /* Init row_major arrays */ if( LAPACKE_lsame( compz, 'i' ) || LAPACKE_lsame( compz, 'v' ) ) { LAPACKE_cge_trans( LAPACK_COL_MAJOR, n, n, z_i, ldz, z_r, n+2 ); } info_i = LAPACKE_csteqr( LAPACK_ROW_MAJOR, compz_i, n_i, d_i, e_i, z_r, ldz_r ); if( LAPACKE_lsame( compz, 'i' ) || LAPACKE_lsame( compz, 'v' ) ) { LAPACKE_cge_trans( LAPACK_ROW_MAJOR, n, n, z_r, n+2, z_i, ldz ); } failed = compare_csteqr( d, d_i, e, e_i, z, z_i, info, info_i, compz, ldz, n ); if( failed == 0 ) { printf( "PASSED: row-major high-level interface to csteqr\n" ); } else { printf( "FAILED: row-major high-level interface to csteqr\n" ); } /* Release memory */ if( d != NULL ) { LAPACKE_free( d ); } if( d_i != NULL ) { LAPACKE_free( d_i ); } if( d_save != NULL ) { LAPACKE_free( d_save ); } if( e != NULL ) { LAPACKE_free( e ); } if( e_i != NULL ) { LAPACKE_free( e_i ); } if( e_save != NULL ) { LAPACKE_free( e_save ); } if( z != NULL ) { LAPACKE_free( z ); } if( z_i != NULL ) { LAPACKE_free( z_i ); } if( z_r != NULL ) { LAPACKE_free( z_r ); } if( z_save != NULL ) { LAPACKE_free( z_save ); } if( work != NULL ) { LAPACKE_free( work ); } if( work_i != NULL ) { LAPACKE_free( work_i ); } return 0; }
int main(void) { /* Local scalars */ char job, job_i; char compz, compz_i; lapack_int n, n_i; lapack_int ilo, ilo_i; lapack_int ihi, ihi_i; lapack_int ldh, ldh_i; lapack_int ldh_r; lapack_int ldz, ldz_i; lapack_int ldz_r; lapack_int lwork, lwork_i; lapack_int info, info_i; lapack_int i; int failed; /* Local arrays */ lapack_complex_float *h = NULL, *h_i = NULL; lapack_complex_float *w = NULL, *w_i = NULL; lapack_complex_float *z = NULL, *z_i = NULL; lapack_complex_float *work = NULL, *work_i = NULL; lapack_complex_float *h_save = NULL; lapack_complex_float *w_save = NULL; lapack_complex_float *z_save = NULL; lapack_complex_float *h_r = NULL; lapack_complex_float *z_r = NULL; /* Iniitialize the scalar parameters */ init_scalars_chseqr( &job, &compz, &n, &ilo, &ihi, &ldh, &ldz, &lwork ); ldh_r = n+2; ldz_r = n+2; job_i = job; compz_i = compz; n_i = n; ilo_i = ilo; ihi_i = ihi; ldh_i = ldh; ldz_i = ldz; lwork_i = lwork; /* Allocate memory for the LAPACK routine arrays */ h = (lapack_complex_float *) LAPACKE_malloc( ldh*n * sizeof(lapack_complex_float) ); w = (lapack_complex_float *) LAPACKE_malloc( n * sizeof(lapack_complex_float) ); z = (lapack_complex_float *) LAPACKE_malloc( ldz*n * sizeof(lapack_complex_float) ); work = (lapack_complex_float *) LAPACKE_malloc( lwork * sizeof(lapack_complex_float) ); /* Allocate memory for the C interface function arrays */ h_i = (lapack_complex_float *) LAPACKE_malloc( ldh*n * sizeof(lapack_complex_float) ); w_i = (lapack_complex_float *) LAPACKE_malloc( n * sizeof(lapack_complex_float) ); z_i = (lapack_complex_float *) LAPACKE_malloc( ldz*n * sizeof(lapack_complex_float) ); work_i = (lapack_complex_float *) LAPACKE_malloc( lwork * sizeof(lapack_complex_float) ); /* Allocate memory for the backup arrays */ h_save = (lapack_complex_float *) LAPACKE_malloc( ldh*n * sizeof(lapack_complex_float) ); w_save = (lapack_complex_float *) LAPACKE_malloc( n * sizeof(lapack_complex_float) ); z_save = (lapack_complex_float *) LAPACKE_malloc( ldz*n * sizeof(lapack_complex_float) ); /* Allocate memory for the row-major arrays */ h_r = (lapack_complex_float *) LAPACKE_malloc( n*(n+2) * sizeof(lapack_complex_float) ); z_r = (lapack_complex_float *) LAPACKE_malloc( n*(n+2) * sizeof(lapack_complex_float) ); /* Initialize input arrays */ init_h( ldh*n, h ); init_w( n, w ); init_z( ldz*n, z ); init_work( lwork, work ); /* Backup the ouptut arrays */ for( i = 0; i < ldh*n; i++ ) { h_save[i] = h[i]; } for( i = 0; i < n; i++ ) { w_save[i] = w[i]; } for( i = 0; i < ldz*n; i++ ) { z_save[i] = z[i]; } /* Call the LAPACK routine */ chseqr_( &job, &compz, &n, &ilo, &ihi, h, &ldh, w, z, &ldz, work, &lwork, &info ); /* Initialize input data, call the column-major middle-level * interface to LAPACK routine and check the results */ for( i = 0; i < ldh*n; i++ ) { h_i[i] = h_save[i]; } for( i = 0; i < n; i++ ) { w_i[i] = w_save[i]; } for( i = 0; i < ldz*n; i++ ) { z_i[i] = z_save[i]; } for( i = 0; i < lwork; i++ ) { work_i[i] = work[i]; } info_i = LAPACKE_chseqr_work( LAPACK_COL_MAJOR, job_i, compz_i, n_i, ilo_i, ihi_i, h_i, ldh_i, w_i, z_i, ldz_i, work_i, lwork_i ); failed = compare_chseqr( h, h_i, w, w_i, z, z_i, info, info_i, compz, ldh, ldz, n ); if( failed == 0 ) { printf( "PASSED: column-major middle-level interface to chseqr\n" ); } else { printf( "FAILED: column-major middle-level interface to chseqr\n" ); } /* Initialize input data, call the column-major high-level * interface to LAPACK routine and check the results */ for( i = 0; i < ldh*n; i++ ) { h_i[i] = h_save[i]; } for( i = 0; i < n; i++ ) { w_i[i] = w_save[i]; } for( i = 0; i < ldz*n; i++ ) { z_i[i] = z_save[i]; } for( i = 0; i < lwork; i++ ) { work_i[i] = work[i]; } info_i = LAPACKE_chseqr( LAPACK_COL_MAJOR, job_i, compz_i, n_i, ilo_i, ihi_i, h_i, ldh_i, w_i, z_i, ldz_i ); failed = compare_chseqr( h, h_i, w, w_i, z, z_i, info, info_i, compz, ldh, ldz, n ); if( failed == 0 ) { printf( "PASSED: column-major high-level interface to chseqr\n" ); } else { printf( "FAILED: column-major high-level interface to chseqr\n" ); } /* Initialize input data, call the row-major middle-level * interface to LAPACK routine and check the results */ for( i = 0; i < ldh*n; i++ ) { h_i[i] = h_save[i]; } for( i = 0; i < n; i++ ) { w_i[i] = w_save[i]; } for( i = 0; i < ldz*n; i++ ) { z_i[i] = z_save[i]; } for( i = 0; i < lwork; i++ ) { work_i[i] = work[i]; } LAPACKE_cge_trans( LAPACK_COL_MAJOR, n, n, h_i, ldh, h_r, n+2 ); if( LAPACKE_lsame( compz, 'i' ) || LAPACKE_lsame( compz, 'v' ) ) { LAPACKE_cge_trans( LAPACK_COL_MAJOR, n, n, z_i, ldz, z_r, n+2 ); } info_i = LAPACKE_chseqr_work( LAPACK_ROW_MAJOR, job_i, compz_i, n_i, ilo_i, ihi_i, h_r, ldh_r, w_i, z_r, ldz_r, work_i, lwork_i ); LAPACKE_cge_trans( LAPACK_ROW_MAJOR, n, n, h_r, n+2, h_i, ldh ); if( LAPACKE_lsame( compz, 'i' ) || LAPACKE_lsame( compz, 'v' ) ) { LAPACKE_cge_trans( LAPACK_ROW_MAJOR, n, n, z_r, n+2, z_i, ldz ); } failed = compare_chseqr( h, h_i, w, w_i, z, z_i, info, info_i, compz, ldh, ldz, n ); if( failed == 0 ) { printf( "PASSED: row-major middle-level interface to chseqr\n" ); } else { printf( "FAILED: row-major middle-level interface to chseqr\n" ); } /* Initialize input data, call the row-major high-level * interface to LAPACK routine and check the results */ for( i = 0; i < ldh*n; i++ ) { h_i[i] = h_save[i]; } for( i = 0; i < n; i++ ) { w_i[i] = w_save[i]; } for( i = 0; i < ldz*n; i++ ) { z_i[i] = z_save[i]; } for( i = 0; i < lwork; i++ ) { work_i[i] = work[i]; } /* Init row_major arrays */ LAPACKE_cge_trans( LAPACK_COL_MAJOR, n, n, h_i, ldh, h_r, n+2 ); if( LAPACKE_lsame( compz, 'i' ) || LAPACKE_lsame( compz, 'v' ) ) { LAPACKE_cge_trans( LAPACK_COL_MAJOR, n, n, z_i, ldz, z_r, n+2 ); } info_i = LAPACKE_chseqr( LAPACK_ROW_MAJOR, job_i, compz_i, n_i, ilo_i, ihi_i, h_r, ldh_r, w_i, z_r, ldz_r ); LAPACKE_cge_trans( LAPACK_ROW_MAJOR, n, n, h_r, n+2, h_i, ldh ); if( LAPACKE_lsame( compz, 'i' ) || LAPACKE_lsame( compz, 'v' ) ) { LAPACKE_cge_trans( LAPACK_ROW_MAJOR, n, n, z_r, n+2, z_i, ldz ); } failed = compare_chseqr( h, h_i, w, w_i, z, z_i, info, info_i, compz, ldh, ldz, n ); if( failed == 0 ) { printf( "PASSED: row-major high-level interface to chseqr\n" ); } else { printf( "FAILED: row-major high-level interface to chseqr\n" ); } /* Release memory */ if( h != NULL ) { LAPACKE_free( h ); } if( h_i != NULL ) { LAPACKE_free( h_i ); } if( h_r != NULL ) { LAPACKE_free( h_r ); } if( h_save != NULL ) { LAPACKE_free( h_save ); } if( w != NULL ) { LAPACKE_free( w ); } if( w_i != NULL ) { LAPACKE_free( w_i ); } if( w_save != NULL ) { LAPACKE_free( w_save ); } if( z != NULL ) { LAPACKE_free( z ); } if( z_i != NULL ) { LAPACKE_free( z_i ); } if( z_r != NULL ) { LAPACKE_free( z_r ); } if( z_save != NULL ) { LAPACKE_free( z_save ); } if( work != NULL ) { LAPACKE_free( work ); } if( work_i != NULL ) { LAPACKE_free( work_i ); } return 0; }
void TopicSearch::run_hybrid_random_walk_simulated_annealing( vec iter_temperature, double random_walk_prob, double percent_random_walk){ size_t accepted_Z_instances; bool valid_burn_in_period; Timer timer = Timer(); timer.restart_time(); init_z(); mat multinomial_prob = init_topical_Multinomial_probabilities(); if (this->verbose_ >= 1) cout << "Multinomial probabilities: " << endl << multinomial_prob; if (this->burn_in_period_ > 0 && this->burn_in_period_ < this->max_iterations_){ valid_burn_in_period = true; accepted_Z_instances = ceil((this->max_iterations_ - this->burn_in_period_) / this->spacing); } else { valid_burn_in_period = false; accepted_Z_instances = ceil(this->max_iterations_ / this->spacing); } for (size_t d = 0; d < this->num_documents_; d++){ // START For each document size_t num_words = this->document_lengths_[d]; umat accepted_Z = zeros<umat>(num_words, accepted_Z_instances); uvec proposed_Z = zeros<uvec>(num_words); uvec current_Z = zeros<uvec>(num_words); size_t acceptance_count = 0; size_t count = 0; size_t random_walk_count = ceil((percent_random_walk / 100) * num_words); size_t num_random_walks = 0; uvec sampled_z; uvec sampled_z2; vector <size_t> word_indices = this->document_word_indices_[d]; for (size_t n = 0; n < num_words; n++) current_Z(n) = this->initial_z_(word_indices[n]); long double ppZ = calc_ln_partition_probality(word_indices, current_Z); for (size_t iter = 0; iter < this->max_iterations_; iter++){ // START TOPIC SEARCH if (this->sample_uniform() <= random_walk_prob){ // do random walk from the previous state num_random_walks++; proposed_Z = current_Z; for (size_t s = 0; s < random_walk_count; s++){ size_t idx = sample_uniform_int(num_words); // selects a word at random while(1){ size_t topic = sample_uniform_int(this->num_topics_); if (topic != current_Z(idx)){ proposed_Z(idx) = topic; break; } } } } else { // do sample from the topic specific Multinomial for (size_t i = 0; i < num_words; i++) proposed_Z(i) = sample_multinomial(multinomial_prob.col(current_Z(i))); } long double ppZ_prime = calc_ln_partition_probality(word_indices, proposed_Z); long double tpZ_prime = calc_lnTP_hybrid_randomwalk( num_words, proposed_Z, current_Z, multinomial_prob, random_walk_prob, random_walk_count); long double tpZ = calc_lnTP_hybrid_randomwalk( num_words, current_Z, proposed_Z, multinomial_prob, random_walk_prob, random_walk_count); long double p_ratio = ppZ_prime - ppZ; long double q_ratio = tpZ_prime - tpZ; double acceptance_probability = min(1.0L, pow(exp(p_ratio + q_ratio), (1 / iter_temperature(iter)))); // MH acceptance probability if (this->sample_uniform() <= acceptance_probability){ ppZ = ppZ_prime; // To avoid re-calculation current_Z = proposed_Z; acceptance_count++; // if (this->verbose_ >= 1){ // // cout << "doc " << d + 1 << " iter " << iter + 1; // cout << " accepted"; // cout << " [a.p. = " << pow(exp(p_ratio + q_ratio), (1 / iter_temperature(iter))) // // << " t.ratio = " << exp(q_ratio) << " p.ratio = " << exp(p_ratio) // << " ln P(z') = " << ppZ_prime << " ln P(z) = " << ppZ // << " ln T(z',z) = " << tpZ_prime << " ln T(z,z') = " << tpZ // << " a.count = " << acceptance_count << " ]" << endl; // } } if ((iter % this->spacing == 0) && (!valid_burn_in_period || (valid_burn_in_period && (this->burn_in_period_ < iter)))) { accepted_Z.col(count) = current_Z; count++; } } // END TOPIC SEARCH // // Saves the results to the class variables // sampled_z = find_mode(accepted_Z); // sampled_z2 = accepted_Z.col(count - 1); //// for (size_t n = 0; n < num_words; n++){ //// this->sampled_z_(word_indices[n]) = sampled_z(n); //// this->beta_counts_(this->sampled_z_(word_indices[n]), this->word_ids_(word_indices[n])) += 1; //// this->beta_counts_last_(sampled_z2(n), this->word_ids_(word_indices[n])) += 1; //// } // // // Calculates theta counts // this->theta_counts_.col(d) = calc_topic_counts(sampled_z, this->num_topics_); // this->theta_counts_last_.col(d) = calc_topic_counts(sampled_z2, this->num_topics_); // // // Resets all used data structures // current_Z.reset(); // proposed_Z.reset(); // accepted_Z.reset(); // sampled_z.reset(); // sampled_z2.reset(); // Saves the results to the class variable sampled_z = find_mode(accepted_Z); sampled_z2 = accepted_Z.col(count - 1); for (size_t n = 0; n < num_words; n++){ this->sampled_z_(word_indices[n]) = sampled_z(n); this->beta_counts_(this->sampled_z_(word_indices[n]), this->word_ids_(word_indices[n])) += 1; this->beta_counts_last_(sampled_z2(n), this->word_ids_(word_indices[n])) += 1; } // Calculates theta counts this->theta_counts_.col(d) = calc_topic_counts(sampled_z, this->num_topics_); this->theta_counts_last_.col(d) = calc_topic_counts(sampled_z2, this->num_topics_); // Resets all used data structures current_Z.reset(); proposed_Z.reset(); accepted_Z.reset(); sampled_z.reset(); sampled_z2.reset(); if (this->verbose_ >= 1) cout << "doc " << d + 1 << " accepted # " << acceptance_count << " random walks # " << num_random_walks << endl; } // END For each document if (this->verbose_ >= 1){ cout << endl << "Total execution time: " << timer.get_time() << "s" << endl; cout << "Model perplexity: " << calc_corpus_perplexity() << endl; // using beta_counts_ and theta_counts_ cout << "Log partition probability: " << calc_ln_corpus_partition_probality() << endl; } }
void TopicSearch::run_hybrid_random_walk_simulated_annealing_uniform2( vec iter_temperature, double random_walk_prob, double percent_random_walk){ size_t accepted_Z_instances; bool valid_burn_in_period; Timer timer = Timer(); timer.restart_time(); init_z(); if (this->burn_in_period_ > 0 && this->burn_in_period_ < this->max_iterations_){ valid_burn_in_period = true; accepted_Z_instances = ceil((this->max_iterations_ - this->burn_in_period_) / this->spacing); } else { valid_burn_in_period = false; accepted_Z_instances = ceil(this->max_iterations_ / this->spacing); } for (size_t d = 0; d < this->num_documents_; d++){ // START For each document size_t num_words = this->document_lengths_[d]; umat accepted_Z = zeros<umat>(num_words, accepted_Z_instances); vec accepted_Z_pp = zeros<vec>(accepted_Z_instances); uvec proposed_Z = zeros<uvec>(num_words); uvec current_Z = zeros<uvec>(num_words); size_t acceptance_count = 0; size_t count = 0; size_t num_random_walks = 0; size_t num_random_walks2 = 0; uvec sampled_z; uvec sampled_z2; vector <size_t> word_indices = this->document_word_indices_[d]; long double ppZ; long double ppZ_prime; long double tpZ_prime; long double tpZ; long double p_ratio; long double q_ratio; double acceptance_probability; double multi_jump_prob = percent_random_walk / 100.0; for (size_t n = 0; n < num_words; n++) current_Z(n) = this->initial_z_(word_indices[n]); ppZ = calc_ln_partition_probality(word_indices, current_Z); for (size_t iter = 0; iter < this->max_iterations_; iter++){ // START TOPIC SEARCH if (this->sample_uniform() <= random_walk_prob){ // do random walk from the previous state num_random_walks++; proposed_Z = current_Z; for (size_t i = 0; i < num_words; i++){ if (this->sample_uniform() <= multi_jump_prob){ /// for each word with some probability multinomial jump proposed_Z(i) = sample_multinomial(this->init_beta_sample_.col(this->word_ids_(word_indices[i]))); num_random_walks2++; } } } else { // do sample from a uniform for (size_t i = 0; i < num_words; i++) proposed_Z(i) = sample_uniform_int(this->num_topics_); } ppZ_prime = calc_ln_partition_probality(word_indices, proposed_Z); p_ratio = ppZ_prime - ppZ; tpZ_prime = calc_lnTP_hybrid_multi_randomwalk(num_words, word_indices, current_Z, random_walk_prob, multi_jump_prob); tpZ = calc_lnTP_hybrid_multi_randomwalk(num_words, word_indices, proposed_Z, random_walk_prob, multi_jump_prob); q_ratio = tpZ_prime - tpZ; assert(iter_temperature(iter) > 0); // acceptance_probability = min(1.0L, pow(exp(p_ratio), (1 / iter_temperature(iter)))); // MH acceptance probability acceptance_probability = min(1.0L, pow(exp(p_ratio + q_ratio), (1.0 / iter_temperature(iter)))); // MH acceptance probability if (this->sample_uniform() <= acceptance_probability){ current_Z = proposed_Z; ppZ = ppZ_prime; acceptance_count++; // if (this->verbose_ >= 1){ // cout << "doc " << d + 1 << " iter " << iter + 1; // cout << " accepted"; // cout << " [a.p. = " << pow(exp(p_ratio), (1 / iter_temperature(iter))) // << " ln P(z') = " << ppZ_prime << " ln P(z) = " << ppZ // << " a.count = " << acceptance_count << " ]" << endl; // } } if (((iter + 1) % this->spacing == 0) && (!valid_burn_in_period || (valid_burn_in_period && (this->burn_in_period_ < iter)))) { accepted_Z.col(count) = current_Z; accepted_Z_pp(count) = ppZ_prime; count++; } } // END TOPIC SEARCH // Saves the results to the class variable sampled_z = find_mode(accepted_Z); sampled_z2 = accepted_Z.col(count - 1); for (size_t n = 0; n < num_words; n++){ this->sampled_z_(word_indices[n]) = sampled_z(n); this->beta_counts_(this->sampled_z_(word_indices[n]), this->word_ids_(word_indices[n])) += 1; this->beta_counts_last_(sampled_z2(n), this->word_ids_(word_indices[n])) += 1; } // Calculates theta counts this->theta_counts_.col(d) = calc_topic_counts(sampled_z, this->num_topics_); this->theta_counts_last_.col(d) = calc_topic_counts(sampled_z2, this->num_topics_); // Resets all used data structures current_Z.reset(); proposed_Z.reset(); accepted_Z.reset(); sampled_z.reset(); sampled_z2.reset(); // if (this->verbose_ >= 1) cout << "doc " << d + 1 << " accepted # " << acceptance_count << " random walks # " << num_random_walks << " actual random walks # " << num_random_walks2 << endl; } // END For each document // if (this->verbose_ >= 1){ cout << endl << "Total execution time: " << timer.get_time() << "s" << endl; cout << "Model perplexity: " << calc_corpus_perplexity() << endl; // using beta_counts_ and theta_counts_ cout << "Log partition probability: " << calc_ln_corpus_partition_probality() << endl; // } }
virtual void init(const Opm::parameter::ParameterGroup& param, const Grid& grid, const Fluid& fluid, typename Grid::Vector gravity, State& simstate) { typedef typename Fluid::CompVec CompVec; typedef typename Fluid::PhaseVec PhaseVec; if (param.getDefault("heterogenous_initial_mix", false)) { CompVec init_oil(0.0); init_oil[Fluid::Oil] = 1.0; CompVec init_water(0.0); init_water[Fluid::Water] = 1.0; simstate.cell_z_.resize(grid.numCells()); std::fill(simstate.cell_z_.begin(), simstate.cell_z_.begin() + simstate.cell_z_.size()/2, init_oil); std::fill(simstate.cell_z_.begin() + simstate.cell_z_.size()/2, simstate.cell_z_.end(), init_water); OPM_MESSAGE("******* Assuming zero capillary pressures *******"); PhaseVec init_p(100.0*Opm::unit::barsa); simstate.cell_pressure_.resize(grid.numCells(), init_p); // if (gravity.two_norm() != 0.0) { // double ref_gravpot = grid.cellCentroid(0)*gravity; // double rho = init_z*fluid_.surfaceDensities(); // Assuming incompressible, and constant initial z. // for (int cell = 1; cell < grid.numCells(); ++cell) { // double press = rho*(grid.cellCentroid(cell)*gravity - ref_gravpot) + simstate.cell_pressure_[0][0]; // simstate.cell_pressure_[cell] = PhaseVec(press); // } // } } else if (param.getDefault("unstable_initial_mix", false)) { CompVec init_oil(0.0); init_oil[Fluid::Oil] = 1.0; init_oil[Fluid::Gas] = 0.0; CompVec init_water(0.0); init_water[Fluid::Water] = 1.0; CompVec init_gas(0.0); init_gas[Fluid::Gas] = 150.0; simstate.cell_z_.resize(grid.numCells()); std::fill(simstate.cell_z_.begin(), simstate.cell_z_.begin() + simstate.cell_z_.size()/3, init_water); std::fill(simstate.cell_z_.begin() + simstate.cell_z_.size()/3, simstate.cell_z_.begin() + 2*(simstate.cell_z_.size()/3), init_oil); std::fill(simstate.cell_z_.begin() + 2*(simstate.cell_z_.size()/3), simstate.cell_z_.end(), init_gas); OPM_MESSAGE("******* Assuming zero capillary pressures *******"); PhaseVec init_p(100.0*Opm::unit::barsa); simstate.cell_pressure_.resize(grid.numCells(), init_p); if (gravity.two_norm() != 0.0) { typename Fluid::FluidState state = fluid.computeState(simstate.cell_pressure_[0], simstate.cell_z_[0]); simstate.cell_z_[0] *= 1.0/state.total_phase_volume_density_; for (int cell = 1; cell < grid.numCells(); ++cell) { double fluid_vol_dens; int cnt =0; do { double rho = 0.5*((simstate.cell_z_[cell]+simstate.cell_z_[cell-1])*fluid.surfaceDensities()); double press = rho*((grid.cellCentroid(cell) - grid.cellCentroid(cell-1))*gravity) + simstate.cell_pressure_[cell-1][0]; simstate.cell_pressure_[cell] = PhaseVec(press); state = fluid.computeState(simstate.cell_pressure_[cell], simstate.cell_z_[cell]); fluid_vol_dens = state.total_phase_volume_density_; simstate.cell_z_[cell] *= 1.0/fluid_vol_dens; ++cnt; } while (std::fabs((fluid_vol_dens-1.0)) > 1.0e-8 && cnt < 10); } } else { std::cout << "---- Exit - BlackoilSimulator.hpp: No gravity, no fun ... ----" << std::endl; exit(-1); } } else if (param.getDefault("CO2-injection", false)) { CompVec init_water(0.0); // Initially water filled (use Oil-component for water in order // to utilise blackoil mechanisms for brine-co2 interaction) init_water[Fluid::Oil] = 1.0; simstate.cell_z_.resize(grid.numCells()); std::fill(simstate.cell_z_.begin(),simstate.cell_z_.end(),init_water); double datum_pressure_barsa = param.getDefault<double>("datum_pressure", 200.0); double datum_pressure = Opm::unit::convert::from(datum_pressure_barsa, Opm::unit::barsa); PhaseVec init_p(datum_pressure); simstate.cell_pressure_.resize(grid.numCells(), init_p); // Simple initial condition based on "incompressibility"-assumption double zMin = grid.cellCentroid(0)[2]; for (int cell = 1; cell < grid.numCells(); ++cell) { if (grid.cellCentroid(cell)[2] < zMin) zMin = grid.cellCentroid(cell)[2]; } typename Fluid::FluidState state = fluid.computeState(init_p, init_water); simstate.cell_z_[0] *= 1.0/state.total_phase_volume_density_; double density = (init_water*fluid.surfaceDensities())/state.total_phase_volume_density_; for (int cell = 0; cell < grid.numCells(); ++cell) { double pressure(datum_pressure + (grid.cellCentroid(cell)[2] - zMin)*gravity[2]*density); simstate.cell_pressure_[cell] = PhaseVec(pressure); state = fluid.computeState(simstate.cell_pressure_[cell], simstate.cell_z_[cell]); simstate.cell_z_[cell] *= 1.0/state.total_phase_volume_density_; } } else { CompVec init_z(0.0); double initial_mixture_gas = param.getDefault("initial_mixture_gas", 0.0); double initial_mixture_oil = param.getDefault("initial_mixture_oil", 1.0); double initial_mixture_water = param.getDefault("initial_mixture_water", 0.0); init_z[Fluid::Water] = initial_mixture_water; init_z[Fluid::Gas] = initial_mixture_gas; init_z[Fluid::Oil] = initial_mixture_oil; simstate.cell_z_.resize(grid.numCells(), init_z); OPM_MESSAGE("******* Assuming zero capillary pressures *******"); PhaseVec init_p(param.getDefault("initial_pressure", 100.0*Opm::unit::barsa)); simstate.cell_pressure_.resize(grid.numCells(), init_p); if (gravity.two_norm() != 0.0) { double ref_gravpot = grid.cellCentroid(0)*gravity; double rho = init_z*fluid.surfaceDensities(); // Assuming incompressible, and constant initial z. for (int cell = 1; cell < grid.numCells(); ++cell) { double press = rho*(grid.cellCentroid(cell)*gravity - ref_gravpot) + simstate.cell_pressure_[0][0]; simstate.cell_pressure_[cell] = PhaseVec(press); } } } }