TDraw1D::TDraw1D(func_ptr_t func, double _x_min, double _x_max, void* _params, unsigned int samples, bool is_log) : x_of_P(NULL), r(NULL), params(_params) { assert(samples > 1); x_min = _x_min; x_max = _x_max; double dx = (x_max - x_min) / (double)(samples - 1); // Construct an interpolator for P(x) double fill = -1.; TMultiLinearInterp<double> P_of_x(&x_min, &x_max, &samples, 1, fill); double x; double P = 0.; for(unsigned int i=0; i<samples; i++) { x = x_min + (double)i * dx; P_of_x.set(&x, P); if(i < samples - 1) { if(is_log) { P += dx * exp(func(x, params)); } else { P += dx * func(x, params); } } } double P_norm = P; // Invert the interpolator for get x(P) double P_min = 0.; double P_max = 1.; double dP = 1. / (double)(samples - 1); x_of_P = new TMultiLinearInterp<double>(&P_min, &P_max, &samples, 1, fill); unsigned int k_last = 0; double P_tmp, dPdx; for(unsigned int i=0; i<samples; i++) { P = (double)i * dP; for(unsigned int k=k_last+1; k<samples; k++) { x = x_min + (double)k * dx; P_tmp = P_of_x(&x) / P_norm; if(P_tmp >= P) { dPdx = (P_tmp - (double)(i-1)*dP) / dx; x = x_min + (double)(k-1) * dx + dP / dPdx; k_last = k - 1; break; } } x_of_P->set(&P, x); } P_tmp = 1.; x_of_P->set(&P_tmp, x_max); seed_gsl_rng(&r); }
// Find a point in space with high density. void TChain::find_center(double* const center, gsl_matrix *const cov, gsl_matrix *const inv_cov, double* det_cov, double dmax, unsigned int iterations) const { // Check that the matrices are the correct size /*assert(cov->size1 == N); assert(cov->size2 == N); assert(inv_cov->size1 == N); assert(inv_cov->size2 == N);*/ // Choose random point in chain as starting point gsl_rng *r; seed_gsl_rng(&r); long unsigned int index_tmp = gsl_rng_uniform_int(r, length); const double *x_tmp = get_element(index_tmp); for(unsigned int i=0; i<N; i++) { center[i] = x_tmp[i]; } // Iterate double *sum = new double[N]; double weight; for(unsigned int i=0; i<iterations; i++) { // Set mean of nearby points as center weight = 0.; for(unsigned int n=0; n<N; n++) { sum[n] = 0.; } for(unsigned int k=0; k<length; k++) { x_tmp = get_element(k); if(metric_dist2(inv_cov, x_tmp, center, N) < dmax*dmax) { for(unsigned int n=0; n<N; n++) { sum[n] += w[k] * x_tmp[n]; } weight += w[k]; } } for(unsigned int n=0; n<N; n++) { center[n] = sum[n] / (double)weight; } dmax *= 0.9; } //for(unsigned int n=0; n<N; n++) { std::cout << " " << center[n]; } //std::cout << std::endl; gsl_rng_free(r); delete[] sum; }
TGaussianMixture::TGaussianMixture(unsigned int _ndim, unsigned int _nclusters) : ndim(_ndim), nclusters(_nclusters) { w = new double[nclusters]; mu = new double[ndim*nclusters]; cov = new gsl_matrix*[nclusters]; inv_cov = new gsl_matrix*[nclusters]; sqrt_cov = new gsl_matrix*[nclusters]; for(unsigned int k=0; k<nclusters; k++) { cov[k] = gsl_matrix_alloc(ndim, ndim); inv_cov[k] = gsl_matrix_alloc(ndim, ndim); sqrt_cov[k] = gsl_matrix_alloc(ndim, ndim); } det_cov = new double[nclusters]; LU = gsl_matrix_alloc(ndim, ndim); p = gsl_permutation_alloc(ndim); esv = gsl_eigen_symmv_alloc(ndim); eival = gsl_vector_alloc(ndim); eivec = gsl_matrix_alloc(ndim, ndim); sqrt_eival = gsl_matrix_alloc(ndim, ndim); seed_gsl_rng(&r); }
TChainWriteBuffer::TChainWriteBuffer(unsigned int nDim, unsigned int nSamples, unsigned int nReserved) : buf(NULL), nDim_(nDim+1), nSamples_(nSamples), nReserved_(0), length_(0), samplePos(nSamples, 0) { reserve(nReserved); seed_gsl_rng(&r); }
bool TChain::save(std::string fname, std::string group_name, size_t index, std::string dim_name, int compression, int subsample, bool converged, float lnZ) const { if((compression<0) || (compression > 9)) { std::cerr << "! Invalid gzip compression level: " << compression << std::endl; return false; } H5::Exception::dontPrint(); H5::H5File *file = H5Utils::openFile(fname); if(file == NULL) { return false; } /* try { file->unlink(group_name); } catch(...) { // pass } */ H5::Group *group = H5Utils::openGroup(file, group_name); if(group == NULL) { delete file; return false; } /* * Attributes */ // Datatype H5::CompType att_type(sizeof(TChainAttribute)); hid_t tid = H5Tcopy(H5T_C_S1); H5Tset_size(tid, H5T_VARIABLE); att_type.insertMember("dim_name", HOFFSET(TChainAttribute, dim_name), tid); //att_type.insertMember("total_weight", HOFFSET(TChainAttribute, total_weight), H5::PredType::NATIVE_FLOAT); //att_type.insertMember("ndim", HOFFSET(TChainAttribute, ndim), H5::PredType::NATIVE_UINT64); //att_type.insertMember("length", HOFFSET(TChainAttribute, length), H5::PredType::NATIVE_UINT64); // Dataspace int att_rank = 1; hsize_t att_dim = 1; H5::DataSpace att_space(att_rank, &att_dim); // Dataset //H5::Attribute att = group->createAttribute("parameter names", att_type, att_space); TChainAttribute att_data; att_data.dim_name = new char[dim_name.size()+1]; std::strcpy(att_data.dim_name, dim_name.c_str()); //att_data.total_weight = total_weight; //att_data.ndim = N; //att_data.length = length; //att.write(att_type, &att_data); delete[] att_data.dim_name; //int att_rank = 1; //hsize_t att_dim = 1; H5::DataType conv_dtype = H5::PredType::NATIVE_UCHAR; H5::DataSpace conv_dspace(att_rank, &att_dim); //H5::Attribute conv_att = H5Utils::openAttribute(group, "converged", conv_dtype, conv_dspace); //conv_att.write(conv_dtype, &converged); H5::DataType lnZ_dtype = H5::PredType::NATIVE_FLOAT; H5::DataSpace lnZ_dspace(att_rank, &att_dim); //H5::Attribute lnZ_att = H5Utils::openAttribute(group, "ln Z", lnZ_dtype, lnZ_dspace); //lnZ_att.write(lnZ_dtype, &lnZ); // Creation property list to be used for all three datasets H5::DSetCreatPropList plist; //plist.setDeflate(compression); // gzip compression level float fillvalue = 0; plist.setFillValue(H5::PredType::NATIVE_FLOAT, &fillvalue); H5D_layout_t layout = H5D_COMPACT; plist.setLayout(layout); /* * Choose subsample of points in chain */ size_t *el_idx = NULL; size_t *subsample_idx = NULL; if(subsample > 0) { size_t tot_weight_tmp = (size_t)ceil(total_weight); el_idx = new size_t[tot_weight_tmp]; size_t unrolled_idx = 0; size_t chain_idx = 0; std::vector<double>::const_iterator it, it_end; it_end = w.end(); for(it = w.begin(); it != it_end; ++it, chain_idx++) { for(size_t n = unrolled_idx; n < unrolled_idx + (size_t)(*it); n++) { el_idx[n] = chain_idx; } unrolled_idx += (size_t)(*it); } assert(chain_idx == length); gsl_rng *r; seed_gsl_rng(&r); subsample_idx = new size_t[tot_weight_tmp]; for(size_t i=0; i<subsample; i++) { subsample_idx[i] = el_idx[gsl_rng_uniform_int(r, tot_weight_tmp)]; } } /* * Coordinates */ // Dataspace hsize_t dim; if(subsample > 0) { dim = subsample; } else { dim = length; } // Chunking (required for compression) int rank = 2; hsize_t coord_dim[2] = {dim, N}; //if(dim < chunk) { //plist.setChunk(rank, &(coord_dim[0])); //} else { // plist.setChunk(rank, &chunk); //} H5::DataSpace x_dspace(rank, &(coord_dim[0])); // Dataset //std::stringstream x_dset_path; //x_dset_path << group_name << "/chain/coords"; std::stringstream coordname; coordname << "coords " << index; H5::DataSet* x_dataset = new H5::DataSet(group->createDataSet(coordname.str(), H5::PredType::NATIVE_FLOAT, x_dspace, plist)); // Write float *buf = new float[N*dim]; if(subsample > 0) { size_t tmp_idx; for(size_t i=0; i<subsample; i++) { tmp_idx = subsample_idx[i]; for(size_t k=0; k<N; k++) { buf[N*i+k] = x[N*tmp_idx+k]; } } } else { for(size_t i=0; i<dim; i++) { buf[i] = x[i]; } } x_dataset->write(buf, H5::PredType::NATIVE_FLOAT); /* * Weights */ // Dataspace if(subsample <= 0) { dim = w.size(); rank = 1; H5::DataSpace w_dspace(rank, &dim); // Dataset //std::stringstream w_dset_path; //w_dset_path << group_name << "/chain/weights"; H5::DataSet* w_dataset = new H5::DataSet(group->createDataSet("weights", H5::PredType::NATIVE_FLOAT, w_dspace, plist)); // Write if(subsample > 0) { for(size_t i=0; i<subsample; i++) { buf[i] = 1.; } } else { assert(w.size() < x.size()); for(size_t i=0; i<w.size(); i++) { buf[i] = w[i]; } } w_dataset->write(buf, H5::PredType::NATIVE_FLOAT); delete w_dataset; } /* * Probability densities */ // Dataspace rank = 1; H5::DataSpace L_dspace(rank, &dim); // Dataset //std::stringstream L_dset_path; //L_dset_path << group_name << "/chain/probs"; std::stringstream lnpname; lnpname << "ln_p " << index; H5::DataSet* L_dataset = new H5::DataSet(group->createDataSet(lnpname.str(), H5::PredType::NATIVE_FLOAT, L_dspace, plist)); // Write if(subsample > 0) { for(size_t i=0; i<subsample; i++) { buf[i] = L[subsample_idx[i]]; } } else { assert(L.size() < x.size()); for(size_t i=0; i<L.size(); i++) { buf[i] = L[i]; } } L_dataset->write(buf, H5::PredType::NATIVE_FLOAT); if(subsample > 0) { delete[] el_idx; delete[] subsample_idx; } delete[] buf; delete x_dataset; delete L_dataset; delete group; delete file; return true; }
// Find a point in space with high density. void TChain::find_center(double* const center, gsl_matrix *const cov, gsl_matrix *const inv_cov, double* det_cov, double dmax, unsigned int iterations) const { // Check that the matrices are the correct size /*assert(cov->size1 == N); assert(cov->size2 == N); assert(inv_cov->size1 == N); assert(inv_cov->size2 == N);*/ // Choose random point in chain as starting point gsl_rng *r; seed_gsl_rng(&r); long unsigned int index_tmp = gsl_rng_uniform_int(r, length); const double *x_tmp = get_element(index_tmp); for(unsigned int i=0; i<N; i++) { center[i] = x_tmp[i]; } //std::cout << "center #0:"; //for(unsigned int n=0; n<N; n++) { std::cout << " " << center[n]; } //std::cout << std::endl; /*double *E_k = new double[N]; double *E_ij = new double[N*N]; for(unsigned int n1=0; n1<N; n1++) { E_k[n1] = 0.; for(unsigned int n2=0; n2<N; n2++) { E_ij[n1 + N*n2] = 0.; } }*/ // Iterate double *sum = new double[N]; double weight; for(unsigned int i=0; i<iterations; i++) { // Set mean of nearby points as center weight = 0.; for(unsigned int n=0; n<N; n++) { sum[n] = 0.; } for(unsigned int k=0; k<length; k++) { x_tmp = get_element(k); if(metric_dist2(inv_cov, x_tmp, center, N) < dmax*dmax) { for(unsigned int n=0; n<N; n++) { sum[n] += w[k] * x_tmp[n]; } weight += w[k]; // Calculate the covariance /*if(i == iterations - 1) { for(unsigned int n1=0; n1<N; n1++) { E_k[n1] += w[k] * x_tmp[n1]; for(unsigned int n2=0; n2<N; n2++) { E_ij[n1 + N*n2] += w[k] * x_tmp[n1] * x_tmp[n2]; } } }*/ } } //std::cout << "center #" << i+1 << ":"; for(unsigned int n=0; n<N; n++) { center[n] = sum[n] / (double)weight; }//std::cout << " " << center[n]; } //std::cout << " (" << weight << ")" << std::endl; dmax *= 0.9; } for(unsigned int n=0; n<N; n++) { std::cout << " " << center[n]; } std::cout << std::endl; // Calculate the covariance matrix of the enclosed points /*double tmp; for(unsigned int i=0; i<N; i++) { for(unsigned int j=i; j<N; j++) { tmp = (E_ij[i + N*j] - E_k[i]*E_k[j]/(double)weight) / (double)weight; gsl_matrix_set(cov, i, j, tmp); if(i != j) { gsl_matrix_set(cov, j, i, tmp); } } }*/ // Get the inverse of the covariance /*int s; gsl_permutation* p = gsl_permutation_alloc(N); gsl_matrix* LU = gsl_matrix_alloc(N, N); gsl_matrix_memcpy(LU, cov); gsl_linalg_LU_decomp(LU, p, &s); gsl_linalg_LU_invert(LU, p, inv_cov); // Get the determinant of the covariance *det_cov = gsl_linalg_LU_det(LU, s); // Cleanup gsl_matrix_free(LU); gsl_permutation_free(p); delete[] E_k; delete[] E_ij;*/ gsl_rng_free(r); delete[] sum; }
void sample_model_synth(TGalacticLOSModel &galactic_model, TSyntheticStellarModel &stellar_model, TExtinctionModel &extinction_model, TStellarData &stellar_data) { unsigned int N_DM = 20; double DM_min = 5.; double DM_max = 20.; TMCMCParams params(&galactic_model, &stellar_model, NULL, &extinction_model, &stellar_data, N_DM, DM_min, DM_max); TMCMCParams params_tmp(&galactic_model, &stellar_model, NULL, &extinction_model, &stellar_data, N_DM, DM_min, DM_max); // Random number generator gsl_rng *r; seed_gsl_rng(&r); // Vector describing position in probability space size_t length = 1 + params.N_DM + 4*params.N_stars; // x = {RV, Delta_EBV_1, ..., Delta_EBV_M, Theta_1, ..., Theta_N}, where Theta = {DM, logMass, logtau, FeH}. double *x = new double[length]; // Random starting point for reddening profile x[0] = 3.1;// + gsl_ran_gaussian_ziggurat(r, 0.2); // RV for(size_t i=0; i<params.N_DM; i++) { x[i+1] = params.data->EBV / (double)N_DM * gsl_ran_chisq(r, 1.); } // Delta_EBV // Random starting point for each star TSED sed_tmp(true); for(size_t i = 1 + params.N_DM; i < 1 + params.N_DM + 4*params.N_stars; i += 4) { x[i] = 5. + 13.*gsl_rng_uniform(r); double logMass, logtau, FeH, tau; bool in_lib = false; while(!in_lib) { logMass = gsl_ran_gaussian_ziggurat(r, 0.5); tau = -1.; while(tau <= 0.) { tau = 1.e9 * (5. + gsl_ran_gaussian_ziggurat(r, 2.)); } logtau = log10(tau); FeH = -1.0 + gsl_ran_gaussian_ziggurat(r, 1.); in_lib = stellar_model.get_sed(logMass, logtau, FeH, sed_tmp); } x[i+1] = logMass; x[i+2] = logtau; x[i+3] = FeH; } params.update_EBV_interp(x); double *lnp_star = new double[params.N_stars]; double lnp_los = logP_los_synth(x, length, params, lnp_star); std::cerr << "# ln p(x_0) = " << lnp_los << std::endl; double *x_tmp = new double[length]; double Theta_tmp[4]; double sigma_Theta[4] = {0.1, 0.1, 0.1, 0.1}; double sigma_RV = 0.05; double sigma_lnEBV = 0.1; double lnp_tmp; double *lnp_star_tmp = new double[params.N_stars]; double p; unsigned int N_steps = 1000000; TChain chain(length, N_steps); TStats EBV_stats(N_DM); // In each step unsigned int N_star = 0; unsigned int N_accept_star = 0; unsigned int N_los = 0; unsigned int N_accept_los = 0; bool accept; bool burn_in = true; for(unsigned int i=0; i<N_steps; i++) { if(i == N_steps/2) { sigma_Theta[0] = 0.05; sigma_Theta[1] = 0.05; sigma_Theta[2] = 0.05; sigma_Theta[3] = 0.05; sigma_RV = 0.005; sigma_lnEBV = 0.05; burn_in = false; } // Step each star for(unsigned int n=0; n<params.N_stars; n++) { if(!burn_in) { N_star++; } rand_gaussian_vector(&Theta_tmp[0], &x[1+N_DM+4*n], &sigma_Theta[0], 4, r); lnp_tmp = logP_single_star_synth(&Theta_tmp[0], params.get_EBV(Theta_tmp[_DM]), x[0], galactic_model, stellar_model, extinction_model, stellar_data.star[n]); accept = false; if(lnp_tmp > lnp_star[n]) { accept = true; } else { p = gsl_rng_uniform(r); if((p > 0.) && (log(p) < lnp_tmp - lnp_star[n])) { accept = true; } } if(accept) { if(!burn_in) { N_accept_star++; } for(size_t k=0; k<4; k++) { x[1+N_DM+4*n+k] = Theta_tmp[k]; } lnp_los += lnp_tmp - lnp_star[n]; lnp_star[n] = lnp_tmp; } } // Step reddening profile if(!burn_in) { N_los++; } for(size_t k=0; k<length; k++) { x_tmp[k] = x[k]; } //if(!burn_in) { x_tmp[0] += gsl_ran_gaussian_ziggurat(r, sigma_RV); } for(unsigned int m=0; m<params.N_DM; m++) { x_tmp[1+m] += gsl_ran_gaussian_ziggurat(r, sigma_lnEBV); } params_tmp.update_EBV_interp(x_tmp); lnp_tmp = logP_los_synth(x_tmp, length, params_tmp, lnp_star_tmp); //if(isinf(lnp_tmp)) { // lnp_tmp = logP_los(x, length, params_tmp, lnp_star_tmp); //} //std::cerr << "# ln p(y) = " << lnp_tmp << std::endl; accept = false; if(lnp_tmp > lnp_los) { accept = true; } else if(log(gsl_rng_uniform(r)) < lnp_tmp - lnp_los) { accept = true; } if(accept) { if(!burn_in) { N_accept_los++; } for(size_t k=0; k<1+N_DM; k++) { x[k] = x_tmp[k]; } for(size_t k=0; k<params.N_stars; k++) { lnp_star[k] = lnp_star_tmp[k]; } lnp_los = lnp_tmp; params.update_EBV_interp(x); //std::cerr << "# ln p(x) = " << lnp_los << std::endl; } if(!burn_in) { chain.add_point(x, lnp_los, 1.); x_tmp[0] = exp(x[1]); for(size_t k=1; k<N_DM; k++) { x_tmp[k] = x_tmp[k-1] + exp(x[k]); } EBV_stats(x_tmp, 1); } } std::cerr << "# ln p(x) = " << lnp_los << std::endl; std::cout.precision(4); std::cerr << std::endl; std::cerr << "# % acceptance: " << 100. * (double)N_accept_star / (double)N_star << " (stars)" << std::endl; std::cerr << " " << 100. * (double)N_accept_los / (double)N_los << " (extinction)" << std::endl; std::cerr << "# R_V = " << x[0] << std::endl << std::endl; std::cerr << "# DM E(B-V)" << std::endl; std::cerr << "# =============" << std::endl; for(double DM=5.; DM<20.; DM+=1.) { std::cerr << "# " << DM << " " << params.get_EBV(DM) << std::endl; } std::cerr << std::endl; EBV_stats.print(); std::cerr << std::endl; delete[] x; delete[] x_tmp; delete[] lnp_star; delete[] lnp_star_tmp; }
void draw_from_emp_model(size_t nstars, double RV, TGalacticLOSModel& gal_model, TStellarModel& stellar_model, TStellarData& stellar_data, TExtinctionModel& ext_model, double (&mag_limit)[5]) { unsigned int samples = 1000; void* gal_model_ptr = static_cast<void*>(&gal_model); void* stellar_model_ptr = static_cast<void*>(&stellar_model); double DM_min = 0.; double DM_max = 25.; TDraw1D draw_DM(&log_dNdmu_draw, DM_min, DM_max, gal_model_ptr, samples, true); double FeH_min = -2.5; double FeH_max = 1.; TDraw1D draw_FeH_disk(&disk_FeH_draw, FeH_min, FeH_max, gal_model_ptr, samples, false); TDraw1D draw_FeH_halo(&halo_FeH_draw, FeH_min, FeH_max, gal_model_ptr, samples, false); double Mr_min = -1.; double Mr_max = mag_limit[1]; TDraw1D draw_Mr(&Mr_draw, Mr_min, Mr_max, stellar_model_ptr, samples, true); stellar_data.clear(); gal_model.get_lb(stellar_data.l, stellar_data.b); gsl_rng *r; seed_gsl_rng(&r); double EBV, DM, Mr, FeH; double f_halo; bool halo, in_lib, observed; TSED sed; double mag[NBANDS]; double err[NBANDS]; std::cout << "# Component E(B-V) DM Mr [Fe/H] g r i z y " << std::endl; std::cout << "=============================================================================================================" << std::endl; std::cout.flags(std::ios::left); std::cout.precision(3); for(size_t i=0; i<nstars; i++) { observed = false; while(!observed) { // Draw DM DM = draw_DM(); // Draw E(B-V) //EBV = gsl_ran_chisq(r, 1.); EBV = 0.; //if(DM > 5.) { EBV += 0.05; } if(DM > 10.) { EBV += 2.5; } // Draw stellar type f_halo = gal_model.f_halo(DM); halo = (gsl_rng_uniform(r) < f_halo); in_lib = false; while(!in_lib) { if(halo) { FeH = draw_FeH_halo(); } else { FeH = draw_FeH_disk(); } Mr = draw_Mr(); in_lib = stellar_model.get_sed(Mr, FeH, sed); } // Generate magnitudes observed = true; unsigned int N_nonobs = 0; double p_det; for(size_t k=0; k<NBANDS; k++) { mag[k] = sed.absmag[k] + DM + EBV * ext_model.get_A(RV, k); err[k] = 0.02 + 0.3*exp(mag[k]-mag_limit[k]); if(err[k] > 1.) { err[k] = 1.; } mag[k] += gsl_ran_gaussian_ziggurat(r, err[k]); // Require detection in g band and 3 other bands p_det = 0.5 - 0.5 * erf((mag[k] - mag_limit[k] + 0.5) / 0.25); if(gsl_rng_uniform(r) > p_det) { mag[k] = 0.; err[k] = 1.e10; N_nonobs++; if((k == 0) || N_nonobs > 1) { observed = false; break; } } } } std::cout << std::setw(9) << i+1 << " "; std::cout << (halo ? "halo" : "disk") << " "; std::cout << std::setw(9) << EBV << " "; std::cout << std::setw(9) << DM << " "; std::cout << std::setw(9) << Mr << " "; std::cout << std::setw(9) << FeH << " "; for(size_t k=0; k<NBANDS; k++) { std::cout << std::setw(9) << mag[k] << " "; } std::cout << std::endl; TStellarData::TMagnitudes mag_tmp(mag, err); mag_tmp.obj_id = i; mag_tmp.l = stellar_data.l; mag_tmp.b = stellar_data.b; stellar_data.star.push_back(mag_tmp); } std::cout << std::endl; gsl_rng_free(r); /*std::vector<bool> filled; DM_of_P.get_filled(filled); for(std::vector<bool>::iterator it = filled.begin(); it != filled.end(); ++it) { std::cout << *it << std::endl; } */ }
void draw_from_synth_model(size_t nstars, double RV, TGalacticLOSModel& gal_model, TSyntheticStellarModel& stellar_model, TStellarData& stellar_data, TExtinctionModel& ext_model, double (&mag_limit)[5]) { unsigned int samples = 1000; void* gal_model_ptr = static_cast<void*>(&gal_model); double DM_min = 0.; double DM_max = 25.; TDraw1D draw_DM(&log_dNdmu_draw, DM_min, DM_max, gal_model_ptr, samples, true); double logMass_min = -0.9; double logMass_max = 1.1; TDraw1D draw_logMass_disk(&disk_IMF_draw, logMass_min, logMass_max, gal_model_ptr, samples, false); TDraw1D draw_logMass_halo(&halo_IMF_draw, logMass_min, logMass_max, gal_model_ptr, samples, false); double tau_min = 1.e6; double tau_max = 13.e9; TDraw1D draw_tau_disk(&disk_SFR_draw, tau_min, tau_max, gal_model_ptr, samples, false); TDraw1D draw_tau_halo(&halo_SFR_draw, tau_min, tau_max, gal_model_ptr, samples, false); double FeH_min = -2.5; double FeH_max = 1.; TDraw1D draw_FeH_disk(&disk_FeH_draw, FeH_min, FeH_max, gal_model_ptr, samples, false); TDraw1D draw_FeH_halo(&halo_FeH_draw, FeH_min, FeH_max, gal_model_ptr, samples, false); stellar_data.clear(); gal_model.get_lb(stellar_data.l, stellar_data.b); gsl_rng *r; seed_gsl_rng(&r); double EBV, DM, logtau, logMass, FeH; double f_halo; bool halo, in_lib, observed; TSED sed; double mag[NBANDS]; double err[NBANDS]; std::cout << "Component E(B-V) DM log(Mass) log(tau) [Fe/H] g r i z y " << std::endl; std::cout << "=============================================================================================================" << std::endl; std::cout.flags(std::ios::left); std::cout.precision(3); for(size_t i=0; i<nstars; i++) { observed = false; while(!observed) { // Draw E(B-V) EBV = gsl_ran_chisq(r, 1.); // Draw DM DM = draw_DM(); // Draw stellar type f_halo = gal_model.f_halo(DM); halo = (gsl_rng_uniform(r) < f_halo); in_lib = false; while(!in_lib) { if(halo) { logMass = draw_logMass_halo(); logtau = log10(draw_tau_halo()); FeH = draw_FeH_halo(); } else { logMass = draw_logMass_disk(); logtau = log10(draw_tau_disk()); FeH = draw_FeH_disk(); } in_lib = stellar_model.get_sed(logMass, logtau, FeH, sed); } // Generate magnitudes observed = true; unsigned int N_nonobs = 0; for(size_t k=0; k<NBANDS; k++) { mag[k] = sed.absmag[k] + DM + EBV * ext_model.get_A(RV, k); err[k] = 0.02 + 0.1*exp(mag[i]-mag_limit[i]-1.5); mag[k] += gsl_ran_gaussian_ziggurat(r, err[k]); // Require detection in g band and 3 other bands if(mag[k] > mag_limit[k]) { N_nonobs++; if((k == 0) || N_nonobs > 1) { observed = false; break; } } } } std::cout << (halo ? "halo" : "disk") << " "; std::cout << std::setw(9) << EBV << " "; std::cout << std::setw(9) << DM << " "; std::cout << std::setw(9) << logMass << " "; std::cout << std::setw(9) << logtau << " "; std::cout << std::setw(9) << FeH << " "; for(size_t k=0; k<NBANDS; k++) { std::cout << std::setw(9) << mag[k] << " "; } std::cout << std::endl; TStellarData::TMagnitudes mag_tmp(mag, err); mag_tmp.obj_id = i; mag_tmp.l = stellar_data.l; mag_tmp.b = stellar_data.b; stellar_data.star.push_back(mag_tmp); } std::cout << std::endl; gsl_rng_free(r); /*std::vector<bool> filled; DM_of_P.get_filled(filled); for(std::vector<bool>::iterator it = filled.begin(); it != filled.end(); ++it) { std::cout << *it << std::endl; } */ }