From c925a3ae5095396178fbe058784e24329166cd99 Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Tue, 20 Sep 2016 19:05:17 +0200 Subject: [PATCH 001/227] Replaced the exponential smoothing for simulation particles with a cubic B-spline smoothing. Implemented the time delay for simulation particles. --- TreeCode/qTreeNB.cpp | 1 + TreeCode/quadTree.cpp | 47 ++++++++++++++++++++++---------------- include/quadTree.h | 53 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 82 insertions(+), 19 deletions(-) diff --git a/TreeCode/qTreeNB.cpp b/TreeCode/qTreeNB.cpp index 57e7623b..6cd353d2 100644 --- a/TreeCode/qTreeNB.cpp +++ b/TreeCode/qTreeNB.cpp @@ -47,6 +47,7 @@ QTreeNB::QTreeNB(PosType **xp,IndexType *particles,IndexType nparticles Nbranches = 1; current = top; + } /// Free treeNB. Does not free the particle positions, masses or sizes diff --git a/TreeCode/quadTree.cpp b/TreeCode/quadTree.cpp index e014a94a..7c783e69 100644 --- a/TreeCode/quadTree.cpp +++ b/TreeCode/quadTree.cpp @@ -44,6 +44,7 @@ TreeQuad::TreeQuad( CalcMoments(); + phiintconst = (120*log(2.) - 631.)/840 + 19./70; return; } /** \brief Constructor meant for halos with internal structure parameters. This is a protected constructor because @@ -78,6 +79,7 @@ MultiMass(true),MultiRadius(true),masses(NULL),sizes(NULL) CalcMoments(); + phiintconst = (120*log(2.) - 631.)/840 + 19./70; return; } @@ -686,8 +688,11 @@ void TreeQuad::walkTree_iter( PosType size = sizes[tmp_index*MultiRadius]; // intersecting, subtract the point particle - if(rcm2 < 9*size*size) + if(rcm2 < 4*size*size) { + + b_spline_profile(xcm,sqrt(rcm2),masses[MultiMass*tmp_index],size,alpha,kappa,gamma,phi); + /* prefac = masses[MultiMass*tmp_index]/rcm2/pi; arg1 = rcm2/(size*size); @@ -695,18 +700,18 @@ void TreeQuad::walkTree_iter( alpha[0] += tmp*xcm[0]; alpha[1] += tmp*xcm[1]; - { - *kappa += kappa_h(arg1,size)*prefac; - - tmp = (gamma_h(arg1,size) + 2.0)*prefac/rcm2; + + *kappa += kappa_h(arg1,size)*prefac; - gamma[0] += 0.5*(xcm[0]*xcm[0]-xcm[1]*xcm[1])*tmp; - gamma[1] += xcm[0]*xcm[1]*tmp; + tmp = (gamma_h(arg1,size) + 2.0)*prefac/rcm2; + gamma[0] += 0.5*(xcm[0]*xcm[0]-xcm[1]*xcm[1])*tmp; + gamma[1] += xcm[0]*xcm[1]*tmp; + + */ // TODO: makes sure the normalization of phi_h agrees with this - *phi += (phi_h(arg1,size) + 0.5*log(rcm2))*prefac*rcm2; - - } + //*phi += (phi_h(arg1,size) + 0.5*log(rcm2))*prefac*rcm2; + } } } @@ -921,13 +926,16 @@ void TreeQuad::walkTree_recur(QBranchNB *branch,PosType const *ray,PosType *alph }else{ // case of no halos just particles and no class derived from TreeQuad if(rcm2 < 1e-20) rcm2 = 1e-20; - //rcm = sqrt(rcm2); PosType size = sizes[tmp_index*MultiRadius]; // intersecting, subtract the point particle - if(rcm2 < 9*size*size) + if(rcm2 < 4*size*size) { + + b_spline_profile(xcm,sqrt(rcm2),masses[MultiMass*tmp_index],size,alpha,kappa,gamma,phi); + + /* prefac = masses[MultiMass*tmp_index]/rcm2/pi; arg1 = rcm2/(size*size); arg2 = size; @@ -936,17 +944,18 @@ void TreeQuad::walkTree_recur(QBranchNB *branch,PosType const *ray,PosType *alph alpha[0] += tmp*xcm[0]; alpha[1] += tmp*xcm[1]; - { - *kappa += kappa_h(arg1,arg2)*prefac; + + *kappa += kappa_h(arg1,arg2)*prefac; - tmp = (gamma_h(arg1,arg2) + 2.0)*prefac/rcm2; + tmp = (gamma_h(arg1,arg2) + 2.0)*prefac/rcm2; - gamma[0] += 0.5*(xcm[0]*xcm[0]-xcm[1]*xcm[1])*tmp; - gamma[1] += xcm[0]*xcm[1]*tmp; + gamma[0] += 0.5*(xcm[0]*xcm[0]-xcm[1]*xcm[1])*tmp; + gamma[1] += xcm[0]*xcm[1]*tmp; // TODO: makes sure the normalization of phi_h agrees with this - *phi += (phi_h(arg1,arg2) + 0.5*log(rcm2))*prefac*rcm2; - } + *phi += (phi_h(arg1,arg2) + 0.5*log(rcm2))*prefac*rcm2; + + */ } } } diff --git a/include/quadTree.h b/include/quadTree.h index 10c83df0..d40afaa1 100644 --- a/include/quadTree.h +++ b/include/quadTree.h @@ -260,6 +260,57 @@ class TreeQuad { return 0; } + + /* cubic B-spline kernal for particle profile + + The lensing qunatities are added to and a point mass is subtacted + */ + void b_spline_profile( + PosType *xcm + ,PosType r // distance from center in Mpc + ,PosType Mass + ,PosType size // size scale in Mpc + ,PosType *alpha + ,KappaType *kappa + ,KappaType *gamma + ,KappaType *phi + ) const { + + PosType q = r/size; + PosType M,sigma; + if(q > 2){ + sigma = 0; + M = 1; + }else{ + PosType q2=q*q,q3=q2*q,q4=q2*q2,q5=q4*q; + + sigma = (8 - 12*q + 6*q2 - q3)/4; + if(q > 1){ + sigma *= 10/size/size/7/pi; + M = (-1 + 20*q2*(1 - q + 3*q2/8 - q3/20) )/7; + *phi += Mass*(-1232. + 1200*q2 - 800.*q3 + 225.*q4 - 24*q5 + 120*log(2./q) )/840/pi; + }else{ + sigma = 10*( sigma - 1 + 3*q - 3*q2 + q3)/size/size/7/pi; + M = 10*q2*(1 - 3*q/4 + 3*q3/10)/7; + + *phi += Mass*( phiintconst + 10*(q2/2 - 3*q4/4 + 3*q5/50)/7 + )/pi; + } + } + + PosType alpha_r,gt; // deflection * Sig_crit / Mass + alpha_r = (M-1)/pi/r; + gt = alpha_r/r - sigma; + + alpha[0] -= Mass*alpha_r*xcm[0]/r; + alpha[1] -= Mass*alpha_r*xcm[1]/r; + gamma[0] -= gt*Mass*(xcm[0]*xcm[0]-xcm[1]*xcm[1])/r/r; + gamma[1] -= gt*Mass*2*xcm[0]*xcm[1]/r/r; + *kappa += Mass*sigma; + *phi -= Mass*log(r)/pi; + } + + QTreeNBHndl rotate_simulation(PosType **xp,IndexType Nparticles,IndexType *particles ,PosType **coord,PosType theta,float *rsph,float *mass ,bool MultiRadius,bool MultiMass); @@ -272,6 +323,8 @@ class TreeQuad { void walkTree_iter(QTreeNB::iterator &treeit, PosType const *ray,PosType *alpha,KappaType *kappa,KappaType *gamma ,KappaType *phi) const; + PosType phiintconst; + }; #endif /* QUAD_TREE_H_ */ From 38590ac1240658f6887a4f1c308295bf703accf6 Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Sun, 25 Sep 2016 11:30:47 +0200 Subject: [PATCH 002/227] Added a constructor for SourceUniform that doesn't require a parameter file. --- AnalyticNSIE/source.cpp | 11 +++++++++++ include/source.h | 6 +++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/AnalyticNSIE/source.cpp b/AnalyticNSIE/source.cpp index a45ca892..4ec4fb2a 100644 --- a/AnalyticNSIE/source.cpp +++ b/AnalyticNSIE/source.cpp @@ -19,6 +19,17 @@ SourceUniform::SourceUniform(InputParams& params) : Source(){ assignParams(params); } +SourceUniform::SourceUniform(PosType *position,PosType z,PosType radius_in_radians): + Source() +{ + source_r = radius_in_radians; + source_x[0] = position[0]; + source_x[1] = position[1]; + setSBlimit_magarcsec(100.); + zsource = z; +} + + SourceGaussian::SourceGaussian(InputParams& params) : Source(){ assignParams(params); } diff --git a/include/source.h b/include/source.h index 36d6d9b4..9682e38e 100644 --- a/include/source.h +++ b/include/source.h @@ -225,7 +225,11 @@ class SourceShapelets: public Source{ /// A uniform surface brightness circular source. class SourceUniform : public Source{ public: - SourceUniform(InputParams& params); + SourceUniform(InputParams& params); + SourceUniform(PosType *position /// postion on the sky in radians + ,PosType z /// redshift of source + ,PosType radius_in_radians /// radius of source in radians + ); ~SourceUniform(); PosType SurfaceBrightness(PosType *y); From ff82c498f38ef0c3b578e0a07198c7fbe8b8c6f6 Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Sun, 25 Sep 2016 11:56:57 +0200 Subject: [PATCH 003/227] Cleaned some things up and made it easier to re-implement exponential smoothing if desired. --- TreeCode/quadTree.cpp | 41 ------------------------------- include/quadTree.h | 56 ++++++++++++++++++++++++++++++++++--------- 2 files changed, 45 insertions(+), 52 deletions(-) diff --git a/TreeCode/quadTree.cpp b/TreeCode/quadTree.cpp index 7c783e69..0e6f7319 100644 --- a/TreeCode/quadTree.cpp +++ b/TreeCode/quadTree.cpp @@ -692,26 +692,6 @@ void TreeQuad::walkTree_iter( { b_spline_profile(xcm,sqrt(rcm2),masses[MultiMass*tmp_index],size,alpha,kappa,gamma,phi); - /* - prefac = masses[MultiMass*tmp_index]/rcm2/pi; - arg1 = rcm2/(size*size); - - tmp = (alpha_h(arg1,size) + 1.0)*prefac; - alpha[0] += tmp*xcm[0]; - alpha[1] += tmp*xcm[1]; - - - *kappa += kappa_h(arg1,size)*prefac; - - tmp = (gamma_h(arg1,size) + 2.0)*prefac/rcm2; - - gamma[0] += 0.5*(xcm[0]*xcm[0]-xcm[1]*xcm[1])*tmp; - gamma[1] += xcm[0]*xcm[1]*tmp; - - */ - // TODO: makes sure the normalization of phi_h agrees with this - //*phi += (phi_h(arg1,size) + 0.5*log(rcm2))*prefac*rcm2; - } } } @@ -935,27 +915,6 @@ void TreeQuad::walkTree_recur(QBranchNB *branch,PosType const *ray,PosType *alph b_spline_profile(xcm,sqrt(rcm2),masses[MultiMass*tmp_index],size,alpha,kappa,gamma,phi); - /* - prefac = masses[MultiMass*tmp_index]/rcm2/pi; - arg1 = rcm2/(size*size); - arg2 = size; - - tmp = (alpha_h(arg1,arg2) + 1.0)*prefac; - alpha[0] += tmp*xcm[0]; - alpha[1] += tmp*xcm[1]; - - - *kappa += kappa_h(arg1,arg2)*prefac; - - tmp = (gamma_h(arg1,arg2) + 2.0)*prefac/rcm2; - - gamma[0] += 0.5*(xcm[0]*xcm[0]-xcm[1]*xcm[1])*tmp; - gamma[1] += xcm[0]*xcm[1]*tmp; - - // TODO: makes sure the normalization of phi_h agrees with this - *phi += (phi_h(arg1,arg2) + 0.5*log(rcm2))*prefac*rcm2; - - */ } } } diff --git a/include/quadTree.h b/include/quadTree.h index d40afaa1..a4a3cc2c 100644 --- a/include/quadTree.h +++ b/include/quadTree.h @@ -261,18 +261,18 @@ class TreeQuad { } - /* cubic B-spline kernal for particle profile + /* cubic B-spline kernel for particle profile - The lensing qunatities are added to and a point mass is subtacted + The lensing quantities are added to and a point mass is subtracted */ - void b_spline_profile( - PosType *xcm - ,PosType r // distance from center in Mpc - ,PosType Mass - ,PosType size // size scale in Mpc - ,PosType *alpha - ,KappaType *kappa - ,KappaType *gamma + inline void b_spline_profile( + PosType *xcm // vector in Mpc connecting ray to center of particle + ,PosType r // distance from center in Mpc + ,PosType Mass // mass in solar masses + ,PosType size // size scale in Mpc + ,PosType *alpha // deflection angle times Sigma_crit + ,KappaType *kappa // surface density + ,KappaType *gamma // shear times Sigma_crit ,KappaType *phi ) const { @@ -310,7 +310,41 @@ class TreeQuad { *phi -= Mass*log(r)/pi; } - + /* Exponential kernel for particle profile + + The lensing quantities are added to and a point mass is subtracted + */ + inline void exponential_profile( + PosType *xcm + ,PosType rcm2 // distance from center in Mpc + ,PosType Mass + ,PosType size // size scale in Mpc + ,PosType *alpha + ,KappaType *kappa + ,KappaType *gamma + ,KappaType *phi + ) const { + + + PosType prefac = Mass/rcm2/pi; + PosType arg1 = rcm2/(size*size); + + PosType tmp = (alpha_h(arg1,size) + 1.0)*prefac; + alpha[0] += tmp*xcm[0]; + alpha[1] += tmp*xcm[1]; + + + *kappa += kappa_h(arg1,size)*prefac; + + tmp = (gamma_h(arg1,size) + 2.0)*prefac/rcm2; + + gamma[0] += 0.5*(xcm[0]*xcm[0]-xcm[1]*xcm[1])*tmp; + gamma[1] += xcm[0]*xcm[1]*tmp; + + // TODO: makes sure the normalization of phi_h agrees with this + //*phi += (phi_h(arg1,size) + 0.5*log(rcm2))*prefac*rcm2; + } + QTreeNBHndl rotate_simulation(PosType **xp,IndexType Nparticles,IndexType *particles ,PosType **coord,PosType theta,float *rsph,float *mass ,bool MultiRadius,bool MultiMass); From 9b7f65fda27187be4887ce6be5fc143033f0b186 Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Mon, 26 Sep 2016 18:49:03 +0200 Subject: [PATCH 004/227] Some additions to PixelMap --- ImageProcessing/observation.cpp | 36 ++++++++++++++++----------- ImageProcessing/pixelize.cpp | 35 +++++++++++++++++++++++--- MultiPlane/lens.cpp | 44 +++++++++++++++++++-------------- include/image_processing.h | 34 ++++++++++++++++++++++--- include/lens.h | 11 ++++++--- 5 files changed, 116 insertions(+), 44 deletions(-) diff --git a/ImageProcessing/observation.cpp b/ImageProcessing/observation.cpp index 10d4cf2c..1ff4a243 100644 --- a/ImageProcessing/observation.cpp +++ b/ImageProcessing/observation.cpp @@ -280,21 +280,27 @@ void Observation::setPSF(std::string psf_file, float os) */ PixelMap Observation::Convert (PixelMap &map, bool psf, bool noise, long *seed, unitType unit) { - if (telescope == true && fabs(map.getResolution()-pix_size) > pix_size*1.0e-5) - { - std::cout << "The resolution of the input map is different from the one of the simulated instrument in Observation::Convert!" << std::endl; - throw std::runtime_error("The resolution of the input map is different from the one of the simulated instrument!"); - } - PixelMap outmap = PhotonToCounts(map); - if (psf == true) outmap = ApplyPSF(outmap); - if (noise == true) outmap = AddNoise(outmap,seed); - - if (unit == flux) - { - double counts_to_flux = pow(10,-0.4*mag_zeropoint); - outmap.Renormalize(counts_to_flux); - } - return outmap; + if (telescope == true && fabs(map.getResolution()-pix_size) > pix_size*1.0e-5) + { + std::cout << "The resolution of the input map is different from the one of the simulated instrument in Observation::Convert!" << std::endl; + throw std::runtime_error("The resolution of the input map is different from the one of the simulated instrument!"); + } + PixelMap outmap = PhotonToCounts(map); + if (psf == true) outmap = ApplyPSF(outmap); + if (noise == true) outmap = AddNoise(outmap,seed); + + if (unit == flux) + { + double counts_to_flux = pow(10,-0.4*mag_zeropoint); + outmap.Renormalize(counts_to_flux); + } + return outmap; +} + +/// returns factor by which code image units need to be multiplied by to get flux units +double Observation::flux_convertion_factor() +{ + return pow(10,-0.4*mag_zeropoint); } /// Converts an observed image to the units of the lensing simulation diff --git a/ImageProcessing/pixelize.cpp b/ImageProcessing/pixelize.cpp index 3185f78d..ed0bf5ff 100644 --- a/ImageProcessing/pixelize.cpp +++ b/ImageProcessing/pixelize.cpp @@ -74,11 +74,36 @@ PixelMap::PixelMap(const PixelMap& other) Nx(other.Nx), Ny(other.Ny), resolution(other.resolution), rangeX(other.rangeX), rangeY(other.rangeY) { std::copy(other.center, other.center + 2, center); - std::copy(other.map_boundary_p1, other.map_boundary_p1 + 2, map_boundary_p1); std::copy(other.map_boundary_p2, other.map_boundary_p2 + 2, map_boundary_p2); } +// move constructor +PixelMap::PixelMap(PixelMap&& other) +:Nx(0),Ny(0),map(std::move(other.map)),resolution(0), rangeX(0), rangeY(0){ + + Nx = other.Nx; + Ny = other.Ny; + resolution = other.resolution; + rangeX = other.rangeX; + rangeY = other.rangeY; + std::copy(other.center, other.center + 2, center); + std::copy(other.map_boundary_p1, other.map_boundary_p1 + 2, map_boundary_p1); + std::copy(other.map_boundary_p2, other.map_boundary_p2 + 2, map_boundary_p2); + + other.Nx = 0; + other.Ny = 0; + other.resolution = 0; + other.center[0] = 0; + other.center[1] = 0; + + other.map_boundary_p1[0] = 0; + other.map_boundary_p1[1] = 0; + other.map_boundary_p2[0] = 0; + other.map_boundary_p2[1] = 0; + +} + /// make square PixelMap PixelMap::PixelMap( const PosType* center, /// The location of the center of the map @@ -314,10 +339,11 @@ PixelMap::~PixelMap() PixelMap& PixelMap::operator=(PixelMap other) { - PixelMap::swap(*this, other); + if(this != &other) PixelMap::swap(*this, other); return *this; } + void PixelMap::swap(PixelMap &map1,PixelMap &map2) { @@ -443,11 +469,14 @@ PixelMap operator*(const PixelMap& a, PosType b) } PosType PixelMap::ave() const{ + return sum()/map.size(); +} +PosType PixelMap::sum() const{ PosType tmp=0; for(size_t i=0;i 1.0) field_galaxy_mass_fraction = 1; + sigma = 126*pow(mass*(1-field_galaxy_mass_fraction)/1.0e10,0.25); // From Tully-Fisher and Bell & de Jong 2001 + //field_halos[j]->initFromMassFunc(mass*(1-field_galaxy_mass_fraction),Rsize,rscale,field_prof_internal_slope,seed); + r200 = halo_calc->getR200(); // used for Kravtsov 2013 2013ApJ...764L..31K + r_half_stel_mass = pow(10.,(0.95*log10(0.015*r200*1000.)+0.015))/1000.; // in Mpc + } - if(field_galaxy_mass_fraction > 1.0) field_galaxy_mass_fraction = 1; - sigma = 126*pow(mass*(1-field_galaxy_mass_fraction)/1.0e10,0.25); // From Tully-Fisher and Bell & de Jong 2001 - //field_halos[j]->initFromMassFunc(mass*(1-field_galaxy_mass_fraction),Rsize,rscale,field_prof_internal_slope,seed); - r200 = halo_calc->getR200(); // used for Kravtsov 2013 2013ApJ...764L..31K - r_half_stel_mass = pow(10.,(0.95*log10(0.015*r200*1000.)+0.015))/1000.; // in Mpc - }else{ field_galaxy_mass_fraction = 0; } @@ -1814,7 +1815,7 @@ void Lens::createFieldHalos(bool verbose) * is not the FOF parent. Multiple LensHalos can have the same id because they were derived from the same simulation halo (ex. one for galaxy and one for DM halo). * */ -void Lens::readInputSimFileMillennium(bool verbose) +void Lens::readInputSimFileMillennium(bool verbose,DM_Light_Division division_mode) { std::cout << "Reading Field Halos from " << field_input_sim_file << std::endl; @@ -1920,6 +1921,8 @@ void Lens::readInputSimFileMillennium(bool verbose) if(flag_field_gal_on){ + if(division_mode == Moster){ + // from Moster et al. 2010ApJ...710..903M field_galaxy_mass_fraction = HALOCalculator::MosterStellarMassFraction(mass); @@ -1927,6 +1930,7 @@ void Lens::readInputSimFileMillennium(bool verbose) sigma = 126*pow(mass*(1-field_galaxy_mass_fraction)/1.0e10,0.25); // From Tully-Fisher and Bell & de Jong 2001 r200 = halo_calc->getR200(); // used for Kravtsov 2013 2013ApJ...764L..31K ; r_half_stel_mass = pow(10.,(0.95*log10(0.015*r200*1000.)+0.015))/1000.; // in Mpc + } }else{ field_galaxy_mass_fraction = 0; } @@ -2008,7 +2012,7 @@ void Lens::readInputSimFileMillennium(bool verbose) ++j; - if(flag_field_gal_on){ + if(flag_field_gal_on && field_galaxy_mass_fraction > 0){ float sigma = 126*pow(mass*field_galaxy_mass_fraction/1.0e10,0.25); // From Tully-Fisher and Bell & de Jong 2001 //std::cout << "Warning: All galaxies are spherical" << std::endl; @@ -2128,7 +2132,7 @@ void Lens::readInputSimFileMillennium(bool verbose) * \brief Read in information from a MultiDark Halo Catalog */ -void Lens::readInputSimFileMultiDarkHalos(bool verbose) +void Lens::readInputSimFileMultiDarkHalos(bool verbose,DM_Light_Division division_mode) { std::cout << "Reading Field Halos from " << field_input_sim_file << std::endl; PosType z,zob,xpos,ypos,zpos,vx,vy,vz,mass; @@ -2137,7 +2141,6 @@ void Lens::readInputSimFileMultiDarkHalos(bool verbose) // const PosType masslimit =2.0e12; const PosType masslimit = 0.0; - Utilities::Geometry::SphericalPoint tmp_sph_point(1,0,0); PosType rmax=0,rtmp=0,boundary_p1[2],boundary_p2[2],boundary_diagonal[2]; @@ -2270,10 +2273,13 @@ void Lens::readInputSimFileMultiDarkHalos(bool verbose) if(flag_field_gal_on){ - field_galaxy_mass_fraction = HALOCalculator::MosterStellarMassFraction(mass); + if(division_mode == Moster){ + field_galaxy_mass_fraction = HALOCalculator::MosterStellarMassFraction(mass); + + if(field_galaxy_mass_fraction > 1.0) field_galaxy_mass_fraction = 1; + sigma = 126*pow(mass*(1-field_galaxy_mass_fraction)/1.0e10,0.25); // From Tully-Fisher and Bell & de Jong 2001 + } - if(field_galaxy_mass_fraction > 1.0) field_galaxy_mass_fraction = 1; - sigma = 126*pow(mass*(1-field_galaxy_mass_fraction)/1.0e10,0.25); // From Tully-Fisher and Bell & de Jong 2001 }else{ field_galaxy_mass_fraction = 0; } @@ -2349,7 +2355,7 @@ void Lens::readInputSimFileMultiDarkHalos(bool verbose) ++j; - if(flag_field_gal_on){ + if(flag_field_gal_on && field_galaxy_mass_fraction > 0){ float sigma = 126*pow(mass*field_galaxy_mass_fraction/1.0e10,0.25); // From Tully-Fisher and Bell & de Jong 2001 //std::cout << "Warning: All galaxies are spherical" << std::endl; float fratio = (ran2(seed)+1)*0.5; //TODO: Ben change this! This is a kluge. diff --git a/include/image_processing.h b/include/image_processing.h index 3c177d8d..f2618dd9 100644 --- a/include/image_processing.h +++ b/include/image_processing.h @@ -31,7 +31,8 @@ class PixelMap public: PixelMap(const PixelMap& pmap, double res_ratio); PixelMap(); - PixelMap(const PixelMap& other); + PixelMap(const PixelMap& other); + PixelMap(PixelMap&& other); PixelMap(const PixelMap& pmap, const double* center, std::size_t Npixels); PixelMap(const double* center, std::size_t Npixels, double resolution); PixelMap(const double* center, std::size_t Nx, std::size_t Ny, double resolution); @@ -39,7 +40,7 @@ class PixelMap ~PixelMap(); PixelMap& operator=(PixelMap other); - + inline bool valid() const { return map.size(); }; inline std::size_t size() const { return map.size(); }; @@ -135,6 +136,8 @@ class PixelMap /// return average pixel value PosType ave() const; + /// return sum of all pixel values + PosType sum() const; /// Total number of pixels size_t size(){return map.size();} @@ -254,6 +257,28 @@ class PixelMap return pixel_index.size(); } + /// reflects the image about the horizontal mid-line + void flipY(){ + for(size_t i=0;i map; void AddGrid_(const PointList &list,LensingVariable val); @@ -339,8 +364,9 @@ class Observation float getBackgroundNoise(float resolution, unitType unit = counts_x_sec); std::valarray getPSF(){return map_psf;} void setPSF(std::string psf_file, float os = 1.); - PixelMap Convert (PixelMap &map, bool psf, bool noise,long *seed, unitType unit = counts_x_sec); - PixelMap Convert_back (PixelMap &map); + PixelMap Convert(PixelMap &map, bool psf, bool noise,long *seed, unitType unit = counts_x_sec); + double flux_convertion_factor(); + PixelMap Convert_back(PixelMap &map); void setExpTime(float time){exp_time = time;} private: diff --git a/include/lens.h b/include/lens.h index 80a9f75f..321a4d72 100644 --- a/include/lens.h +++ b/include/lens.h @@ -278,12 +278,17 @@ class Lens /// computes the distribution variables for field halos as specified in the parameter file /// this material was before computed in createFieldHalos void ComputeHalosDistributionVariables (); - void createFieldHalos(bool verbose); + + enum DM_Light_Division {All_DM,Moster}; + + void createFieldHalos(bool verbose,DM_Light_Division division = Moster); /// read field halo data in from a file in Millennium output format - void readInputSimFileMillennium(bool verbose); + void readInputSimFileMillennium(bool verbose,DM_Light_Division division = Moster); + /// read field halo data in from a file in MultiDarkHalos output format - void readInputSimFileMultiDarkHalos(bool verbose); + void readInputSimFileMultiDarkHalos(bool verbose,DM_Light_Division division = Moster); + /// read field halo data in from a file in Cabriel Caminha's input format void readInputSimFileObservedGalaxies(bool verbose); From 136959015151995f6ddf30180966b8a6e2a9ed44 Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Tue, 27 Sep 2016 14:14:12 +0200 Subject: [PATCH 005/227] Change to Utilities::ReadFileNames() --- ImageProcessing/pixelize.cpp | 4 +++- include/image_processing.h | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/ImageProcessing/pixelize.cpp b/ImageProcessing/pixelize.cpp index ed0bf5ff..9e4fd27f 100644 --- a/ImageProcessing/pixelize.cpp +++ b/ImageProcessing/pixelize.cpp @@ -1604,6 +1604,7 @@ void Utilities::ReadFileNames( std::string dir /// path to directory containing fits files ,const std::string filespec /// string of charactors in file name that are matched. It can be an empty string. ,std::vector & filenames /// output vector of PixelMaps + ,const std::string file_non_spec /// string of charactors in file name that file must not have. ,bool verbose){ DIR *dp = opendir( dir.c_str() ); @@ -1627,7 +1628,8 @@ void Utilities::ReadFileNames( if (S_ISDIR( filestat.st_mode )) continue; filename = dirp->d_name; - if(filename.find(filespec) != std::string::npos){ + if(filename.find(filespec) != std::string::npos && + filename.find(file_non_spec) == std::string::npos ){ if(verbose) std::cout << "adding " << filepath << std::endl; filenames.push_back(filename); } diff --git a/include/image_processing.h b/include/image_processing.h index f2618dd9..04b4d313 100644 --- a/include/image_processing.h +++ b/include/image_processing.h @@ -398,7 +398,9 @@ void _SplitFluxIntoPixels(TreeHndl ptree,Branch *leaf,double *leaf_sb); namespace Utilities{ void LoadFitsImages(std::string dir,const std::string& filespec,std::vector & images,int maxN,double resolution = -1,bool verbose = false); void LoadFitsImages(std::string dir,std::vector filespecs,std::vector file_non_specs ,std::vector & images,std::vector & names,int maxN,double resolution = -1,bool verbose = false); - void ReadFileNames(std::string dir,const std::string filespec,std::vector & filenames + void ReadFileNames(std::string dir,const std::string filespec + ,std::vector & filenames + ,const std::string file_non_spec = " " ,bool verbose = false); } From 73a50d667098289011bd7cc346222886154172b6 Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Thu, 6 Oct 2016 13:20:40 +0200 Subject: [PATCH 006/227] Modified constructors of Lens so that internal values are set correctly when there is no parameter file. --- InputParameters/InputParams.cpp | 6 +- MultiPlane/lens.cpp | 114 +++++++++++++++++++++++--------- TreeCode/quadTree.cpp | 5 +- include/InputParams.h | 8 +-- include/lens.h | 1 + 5 files changed, 95 insertions(+), 39 deletions(-) diff --git a/InputParameters/InputParams.cpp b/InputParameters/InputParams.cpp index 8e86e12d..2415b183 100644 --- a/InputParameters/InputParams.cpp +++ b/InputParameters/InputParams.cpp @@ -399,17 +399,17 @@ bool InputParams::get(std::string label, MassFuncType& value) const if(!it->second.compare("0") || !it->second.compare("PS")) { - value = PS; + value = PressSchechter; return true; } if(!it->second.compare("1") || !it->second.compare("ST")) { - value = ST; + value = ShethTormen; return true; } if(!it->second.compare("2") || !it->second.compare("PowerLaw")) { - value = PL; + value = PowerLaw; return true; } diff --git a/MultiPlane/lens.cpp b/MultiPlane/lens.cpp index d55a1bc6..ce75714a 100644 --- a/MultiPlane/lens.cpp +++ b/MultiPlane/lens.cpp @@ -45,21 +45,16 @@ Lens::Lens(long* my_seed,PosType z_source, CosmoParamSet cosmoset,bool verbose) printf("ERROR: Lens can only handle flat universes at present. Must change cosmology.\n"); exit(1); } - - read_sim_file = false; - - charge = 4*pi*Grav; - if(verbose) std::cout << "charge: " << charge << std::endl; - - // initially let source be the one inputed from parameter file - index_of_new_sourceplane = -1; - toggle_source_plane = false; - flag_switch_deflection_off = false; - flag_switch_lensing_off = false; - - //charge = cosmo.angDist(zsource)/cosmo.angDist(0.3)/cosmo.angDist(0.3,zsource); - //charge = 4*pi/cosmo.angDist(0.3); + defaultParams(z_source,verbose); + if(verbose) std::cout << "charge: " << charge << std::endl; + + // initially let source be the one inputed from parameter file + //index_of_new_sourceplane = -1; + //toggle_source_plane = false; + //flag_switch_deflection_off = false; + //flag_switch_lensing_off = false; + PosType ztmp = zsource; combinePlanes(verbose); if(zsource != ztmp) ResetSourcePlane(ztmp,false); @@ -78,19 +73,15 @@ Lens::Lens(long* my_seed,PosType z_source, const COSMOLOGY &cosmoset,bool verbos exit(1); } - read_sim_file = false; - - charge = 4*pi*Grav; + defaultParams(z_source,verbose); if(verbose) std::cout << "charge: " << charge << std::endl; - + // initially let source be the one inputed from parameter file - index_of_new_sourceplane = -1; - toggle_source_plane = false; - flag_switch_deflection_off = false; - flag_switch_lensing_off = false; + //index_of_new_sourceplane = -1; + //toggle_source_plane = false; + //flag_switch_deflection_off = false; + //flag_switch_lensing_off = false; - //charge = cosmo.angDist(zsource)/cosmo.angDist(0.3)/cosmo.angDist(0.3,zsource); - //charge = 4*pi/cosmo.angDist(0.3); PosType ztmp = zsource; combinePlanes(true); if(zsource != ztmp) ResetSourcePlane(ztmp,false); @@ -532,6 +523,66 @@ void Lens::assignParams(InputParams& params,bool verbose) if(verbose) printMultiLens(); } +// Set default values for internal variables. Used in constructor that don't take a InputParams. +void Lens::defaultParams(PosType z_source,bool verbose) +{ + + charge = 4*pi*Grav; + + read_sim_file = false; + index_of_new_sourceplane = -1; + toggle_source_plane = false; + + + // toggles for testing + flag_switch_deflection_off = false; + flag_switch_lensing_off = false; + + + flag_switch_main_halo_on = true; + main_halo_type = null_lens; + main_galaxy_halo_type = null_gal; + + read_redshift_planes = false; + flag_switch_field_off = false; + + // perameters related to simulating field halos or reading in feild halo information + + field_Nplanes_original = 0; + field_Nplanes_current = field_Nplanes_original; + field_int_prof_type = null_lens; + + flag_field_gal_on = false; + field_int_prof_gal_type = null_gal; + mass_func_PL_slope =0; + field_prof_internal_slope = 0; + field_input_sim_file = ""; + field_mass_func_type = PressSchechter; + + sim_input_flag = false; + field_min_mass = 0; + field_buffer = 0.0; + fieldofview = 0.0; + + field_input_sim_format = null_cat; + + // spherical coordinates of center of field + central_point_sphere.phi = 0.0; + central_point_sphere.theta = 0.0; + sim_angular_radius = 0.0; + + // read Pixelized map parameters + + pixel_map_on = 0; + pixel_map_input_file = ""; + pixel_map_zeropad = 0; + pixel_map_zeromean = false; + + zsource = z_source; + + if(verbose) printMultiLens(); +} + void Lens::resetFieldNplanes(std::size_t Np, bool verbose) { Utilities::delete_container(field_planes); @@ -655,13 +706,13 @@ void Lens::printMultiLens(){ std::cout << "Mass function type: "<< endl; switch(field_mass_func_type){ - case PS: + case PressSchechter: std::cout << " Press-Schechter mass function " << endl; break; - case ST: + case ShethTormen: std::cout << " Sheth-Tormen mass function " << endl; break; - case PL: + case PowerLaw: std::cout << " Power law mass function " << endl; std::cout << " slope: " << mass_func_PL_slope << endl; break; @@ -925,8 +976,8 @@ void Lens::insertSubstructures(PosType Rregion, // in radians theta = 2*pi*ran2(seed); // in radians // position in proper distance - theta_pos[0] = (rr*cos(theta) + center[0]);//*Dl; // in radians * angular Distance in PhysMpc = PhysMpc - theta_pos[1] = (rr*sin(theta) + center[1]);//*Dl; // same : PhysMpc + theta_pos[0] = (rr*cos(theta) + center[0]); // in radians * angular Distance in PhysMpc = PhysMpc + theta_pos[1] = (rr*sin(theta) + center[1]); // same : PhysMpc theta_pos[2] = 0.0; f = ran2(seed); // dimensionless and between 0 and 1 @@ -1010,6 +1061,7 @@ void Lens::insertSubstructures(PosType Rregion, // in radians // Insertion : if(verbose) std::cout << "Lens::insertSubstructures : inserting a new plane at redshift z = " << redshift << std::endl; + assert(NhalosSub == substructure.halos.size()); field_planes.push_back(new LensPlaneTree(substructure.halos.data(), NhalosSub, 0, 0)); field_plane_redshifts.push_back(redshift); field_Dl.push_back(Dl*(1+redshift)); @@ -2096,7 +2148,7 @@ void Lens::readInputSimFileMillennium(bool verbose) std::cout << " It is now " << fieldofview << " deg^2" << std::endl; if(verbose) std::cout << "Setting mass function to Sheth-Tormen." << std::endl; - field_mass_func_type = ST; // set mass function + field_mass_func_type = ShethTormen; // set mass function if(verbose) std::cout << "sorting in Lens::readInputSimFileMillennium()" << std::endl; // sort the field_halos by readshift @@ -2437,7 +2489,7 @@ void Lens::readInputSimFileMultiDarkHalos(bool verbose) } if(verbose) std::cout << "Setting mass function to Sheth-Tormen." << std::endl; - field_mass_func_type = ST; // set mass function + field_mass_func_type = ShethTormen; // set mass function if(verbose) std::cout << "sorting in Lens::readInputSimFileMultiDarkHalos()" << std::endl; // sort the field_halos by readshift diff --git a/TreeCode/quadTree.cpp b/TreeCode/quadTree.cpp index e014a94a..55c098ca 100644 --- a/TreeCode/quadTree.cpp +++ b/TreeCode/quadTree.cpp @@ -115,12 +115,15 @@ QTreeNBHndl TreeQuad::BuildQTreeNB(PosType **xp,IndexType Nparticles,IndexType * p2[1]=0.25; } - // store true dimensions of simulation PosType lengths[2] = {p2[0]-p1[0],p2[1]-p1[1]}; original_xl = lengths[0]; original_yl = lengths[1]; + if(lengths[0] != 0 || lengths[1] != 0){ + throw std::invalid_argument("particles in same place."); + } + // If region is not square, make it square. j = lengths[0] > lengths[1] ? 1 : 0; p2[j] = p1[j] + lengths[!j]; diff --git a/include/InputParams.h b/include/InputParams.h index 694df8c0..4972f508 100644 --- a/include/InputParams.h +++ b/include/InputParams.h @@ -30,9 +30,9 @@ enum MassFuncType { - PS, - ST, - PL + PressSchechter, + ShethTormen, + PowerLaw }; enum LensHaloType @@ -72,7 +72,7 @@ enum IMFtype {One,Mono,BrokenPowerLaw,Salpeter,SinglePowerLaw,Kroupa,Chabrier}; enum Band {EUC_VIS,EUC_Y,EUC_J,EUC_H,SDSS_U,SDSS_G,SDSS_R,SDSS_I,SDSS_Z,J,H,Ks,IRAC1,IRAC2,F435W,F606W,F775W,F850LP,F814W,F110W,F160W}; std::ostream &operator<<(std::ostream &os, Band const &p); -enum HaloCatFormats {MillenniumObs,MultiDarkHalos,ObservedData}; +enum HaloCatFormats {MillenniumObs,MultiDarkHalos,ObservedData,null_cat}; /// Methods to make a previously isotropic halo elliptical enum EllipMethod {Fourier,Pseudo,Schramm,Keeton}; diff --git a/include/lens.h b/include/lens.h index 80a9f75f..5e877005 100644 --- a/include/lens.h +++ b/include/lens.h @@ -251,6 +251,7 @@ class Lens void readCosmology(InputParams& params); void assignParams(InputParams& params,bool verbose = false); + void defaultParams(PosType zsource,bool verbose = true); /// turns source plane on and off bool toggle_source_plane; From 8acdad67351cef2bcb0c322b56eed9ff8270cd59 Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Fri, 7 Oct 2016 11:20:21 +0200 Subject: [PATCH 007/227] Put in some checks to make it harder for the Dist and the lens in LensHalo to get out of sync with each other. --- MultiPlane/MOKAlens.cpp | 7 +++---- MultiPlane/lens.cpp | 19 +++++++++++++------ MultiPlane/lens_halos.cpp | 17 ++++++++++++++--- include/base_analens.h | 11 ----------- include/lens_halos.h | 39 ++++++++++++++++++++++----------------- 5 files changed, 52 insertions(+), 41 deletions(-) diff --git a/MultiPlane/MOKAlens.cpp b/MultiPlane/MOKAlens.cpp index 7f86a917..b6a30055 100644 --- a/MultiPlane/MOKAlens.cpp +++ b/MultiPlane/MOKAlens.cpp @@ -92,16 +92,15 @@ LensHaloMassMap::LensHaloMassMap( , flag_MOKA_analyze(0), flag_background_field(0),maptype(pix_map),cosmo(lenscosmo),zerosize(pixel_map_zeropad),zeromean(my_zeromean) { rscale = 1.0; - Dist = lenscosmo.angDist(redshift); setMap(MassMap,massconvertion,redshift); LensHalo::setTheta(MassMap.getCenter()[0],MassMap.getCenter()[1]); - setZlens(redshift); - + setZlensDist(map->zlens,cosmo); + //setZlens(redshift); // set redshift to value from map - setZlens(map->zlens); + //setZlens(map->zlens); } /* diff --git a/MultiPlane/lens.cpp b/MultiPlane/lens.cpp index ce75714a..57556135 100644 --- a/MultiPlane/lens.cpp +++ b/MultiPlane/lens.cpp @@ -861,7 +861,9 @@ void Lens::createFieldPlanes(bool verbose) //field_halos[j]->getX(tmp); //field_halos[j]->setX(tmp[0]*field_Dl[i]/(1+field_plane_redshifts[i]) // ,tmp[1]*field_Dl[i]/(1+field_plane_redshifts[i])); - field_halos[j]->setDist(field_Dl[i]/(1+field_plane_redshifts[i])); + //field_halos[j]->setDist(field_Dl[i]/(1+field_plane_redshifts[i])); + + field_halos[j]->setZlensDist(field_plane_redshifts[i],cosmo); //halo_pos[j][0] *= field_Dl[i]/(1+field_plane_redshifts[i]); //halo_pos[j][1] *= field_Dl[i]/(1+field_plane_redshifts[i]); } @@ -1225,7 +1227,8 @@ void Lens::addMainHaloToPlane(LensHalo* halo) { // add to plane at (i-1) main_planes[i-1]->addHalo(halo); - halo->setDist(main_Dl[i-1]/(1+main_plane_redshifts[i-1])); + //halo->setDist(main_Dl[i-1]/(1+main_plane_redshifts[i-1])); + halo->setZlensDist(main_plane_redshifts[i-1],cosmo); } else if(i == main_Dl.size()) { @@ -1233,13 +1236,15 @@ void Lens::addMainHaloToPlane(LensHalo* halo) main_planes.push_back(new LensPlaneSingular(&halo, 1)); main_plane_redshifts.push_back(halo_z); main_Dl.push_back(halo_Dl); - halo->setDist(halo_Dl/(1+halo_z)); + //halo->setDist(halo_Dl/(1+halo_z)); + halo->setZlensDist(halo_z,cosmo); } else if((main_Dl[i] - halo_Dl) < MIN_PLANE_DIST) { // add to existing plane at position i main_planes[i]->addHalo(halo); - halo->setDist(main_Dl[i]/(1+main_plane_redshifts[i])); + //halo->setDist(main_Dl[i]/(1+main_plane_redshifts[i])); + halo->setZlensDist(main_plane_redshifts[i],cosmo); } else { @@ -1247,7 +1252,8 @@ void Lens::addMainHaloToPlane(LensHalo* halo) main_planes.insert(main_planes.begin() + i, new LensPlaneSingular(&halo, 1)); main_plane_redshifts.insert(main_plane_redshifts.begin() + i, halo_z); main_Dl.insert(main_Dl.begin() + i, halo_Dl); - halo->setDist(halo_Dl/(1+halo_z)); + //halo->setDist(halo_Dl/(1+halo_z)); + halo->setZlensDist(halo_z,cosmo); } } @@ -1272,7 +1278,8 @@ void Lens::addMainHaloToNearestPlane(LensHalo* halo) std::size_t i = Utilities::closest(main_Dl,halo_Dl); main_planes[i]->addHalo(halo); - halo->setDist(main_Dl[i]/(1+main_plane_redshifts[i])); + //halo->setDist(main_Dl[i]/(1+main_plane_redshifts[i])); + halo->setZlensDist(main_plane_redshifts[i],cosmo); } diff --git a/MultiPlane/lens_halos.cpp b/MultiPlane/lens_halos.cpp index a12c75af..e31a4b13 100644 --- a/MultiPlane/lens_halos.cpp +++ b/MultiPlane/lens_halos.cpp @@ -15,8 +15,19 @@ LensHalo::LensHalo(){ mass = Rsize = Rmax = xmax = posHalo[0] = posHalo[1] = 0.0; stars_implanted = false; elliptical_flag = false; - Dist = 0.0; + Dist = -1; stars_N =0.0; + zlens = 0.0; +} + +LensHalo::LensHalo(PosType z,COSMOLOGY &cosmo){ + rscale = 1.0; + mass = Rsize = Rmax = xmax = posHalo[0] = posHalo[1] = 0.0; + stars_implanted = false; + elliptical_flag = false; + Dist = cosmo.angDist(z); + stars_N =0.0; + zlens = z; } LensHalo::LensHalo(InputParams& params){ @@ -24,7 +35,7 @@ LensHalo::LensHalo(InputParams& params){ stars_implanted = false; posHalo[0] = posHalo[1] = 0.0; elliptical_flag = false; - Dist = 0.0; + Dist = -1; } void LensHalo::initFromMassFunc(float my_mass, float my_Rsize, float my_rscale, PosType my_slope, long *seed){ @@ -1645,7 +1656,7 @@ LensHaloDummy::LensHaloDummy(float my_mass,float my_Rsize,PosType my_zlens,float mass=my_mass, Rsize=my_Rsize, zlens=my_zlens, rscale=my_rscale; stars_N=my_stars_N; stars_implanted = false; - posHalo[0] = posHalo[1] = 0.0; + setTheta(0.0,0.0); } LensHaloDummy::LensHaloDummy(InputParams& params) diff --git a/include/base_analens.h b/include/base_analens.h index 96ca24a3..d65635c4 100644 --- a/include/base_analens.h +++ b/include/base_analens.h @@ -130,17 +130,6 @@ class LensHaloBaseNSIE : public LensHalo{ PosType setParam(std::size_t p, PosType value); void printCSV(std::ostream& out, bool header = false) const; - - /// set the velocity dispersion of NSIE - //void set_sigma(float my_sigma){sigma = my_sigma;} - /// set the NSIE radius - //void set_Rsize(float my_Rsize){Rsize = my_Rsize;} - /// set the axis ratio - //void set_fratio(float my_fratio){fratio = my_fratio;} - /// set the position angle - //void set_pa(float my_pa){pa = my_pa;} - /// set the core radius - //void set_rcore(float my_rcore){rcore = my_rcore;} /// computes phi for NSIE KappaType phiNSIE(PosType const *xt,PosType f,PosType bc,PosType theta); diff --git a/include/lens_halos.h b/include/lens_halos.h index 43878c85..83b439ce 100644 --- a/include/lens_halos.h +++ b/include/lens_halos.h @@ -46,6 +46,7 @@ class TreeQuad; class LensHalo{ public: LensHalo(); + LensHalo(PosType z,COSMOLOGY &cosmo); LensHalo(InputParams& params); virtual ~LensHalo(); @@ -68,8 +69,11 @@ class LensHalo{ //void setX(PosType *PosXY) { posHalo[0] = PosXY[0] ; posHalo[1] = PosXY[1] ; } /// get the position of the Halo in physical Mpc on the lens plane - void getX(PosType * MyPosHalo) const { MyPosHalo[0] = posHalo[0]*Dist ; - MyPosHalo[1] = posHalo[1]*Dist; } + void getX(PosType * MyPosHalo) const { + assert(Dist != -1); + MyPosHalo[0] = posHalo[0]*Dist; + MyPosHalo[1] = posHalo[1]*Dist; + } /// set the position of the Halo in radians void setTheta(PosType PosX, PosType PosY) { posHalo[0] = PosX ; posHalo[1] = PosY ; } @@ -79,7 +83,7 @@ class LensHalo{ void getTheta(PosType * MyPosHalo) const { MyPosHalo[0] = posHalo[0] ; MyPosHalo[1] = posHalo[1]; } /// Set the angular size distance to the halo. This should be the distance to the lens plane. - void setDist(PosType my_Dist){Dist = my_Dist;} + void setDist(COSMOLOGY &co){Dist = co.angDist(zlens);} /// return current angular size distance, ie conversion between angular and special coordinates. This may not agree with /// the getZ() value because of the projection onto the lens plane. PosType getDist() const {return Dist;} @@ -100,7 +104,12 @@ class LensHalo{ /// set scale radius (in Mpc) virtual void set_rscale(float my_rscale){rscale=my_rscale; xmax = Rsize/rscale;}; /// set redshift - void setZlens(PosType my_zlens){ zlens=my_zlens; }; + void setZlens(PosType my_zlens){zlens=my_zlens; Dist=-1;} + // ste redshift and distance + void setZlensDist(PosType my_zlens,const COSMOLOGY &cos){ + zlens=my_zlens; + Dist = cos.angDist(zlens); + } /// set slope virtual void set_slope(PosType my_slope){beta=my_slope;}; /// get slope @@ -180,16 +189,21 @@ class LensHalo{ PosType renormalization(PosType r_max); PosType mnorm; - +private: + size_t idnumber; /// Identification number of halo. It is not always used. + PosType Dist; + /// Position of the Halo in angle + PosType posHalo[2]; + protected: + PosType zlens; + // make LensHalo uncopyable void operator=(LensHalo &){}; LensHalo(LensHalo &){}; - size_t idnumber; /// Identification number of halo. It is not always used. - PosType Dist; - + PosType alpha_int(PosType x) const; PosType norm_int(PosType r_max); @@ -483,12 +497,6 @@ class LensHalo{ PosType r_eps; - PosType zlens; - - /// Position of the Halo in angle - PosType posHalo[2]; - - // These are stucts used in doing tests /*struct test_gt_func{ @@ -832,9 +840,6 @@ class LensHaloPseudoNFW: public LensHalo{ }; - - - /** \ingroup DeflectionL2 * * \brief A class for calculating the deflection, kappa and gamma caused by a collection of halos From faa78ea77a8c885b9abab4a4023e40f459d3b80a Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Fri, 7 Oct 2016 10:31:06 +0200 Subject: [PATCH 008/227] Changed Utilities::shuffle() to take a reference to the random number generator. Created Utilities::ShuffledIndex --- include/utilities_slsim.h | 52 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 51 insertions(+), 1 deletion(-) diff --git a/include/utilities_slsim.h b/include/utilities_slsim.h index 678f3add..a09c0820 100644 --- a/include/utilities_slsim.h +++ b/include/utilities_slsim.h @@ -1000,7 +1000,7 @@ namespace Utilities template void shuffle( std::vector &vec /// The vector to be shuffled - ,R ran /// a random number generator so that ran() gives a number between 0 and 1 + ,R &ran /// a random number generator so that ran() gives a number between 0 and 1 ){ T tmp; size_t ran_t; @@ -1057,6 +1057,56 @@ namespace Utilities std::sort(index.begin(), index.end(), [&v](size_t i1, size_t i2) {return v[i1] > v[i2];}); } + + /** \brief Gives a randomized sequence of numbers from 0 to N-1. + + If the sequence is exhausted then it is reshuffled and numbers will + repeat in randomized order. + **/ + template + class ShuffledIndex{ + public: + ShuffledIndex(size_t N,R &ran){ + if(N == 0) throw std::invalid_argument("N=0"); + + for(size_t i=0 ; i index; + size_t index_internal; + }; // reorders vec according to index p From b057b8777336d3524c1fa39438a07fedc2d37311 Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Tue, 11 Oct 2016 10:00:53 +0200 Subject: [PATCH 009/227] COSMOLOGY::rho_crit() -> COSMOLOGY::rho_crit_comoving() --- MultiPlane/lens.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/MultiPlane/lens.cpp b/MultiPlane/lens.cpp index a41fe1a5..5246ccc9 100644 --- a/MultiPlane/lens.cpp +++ b/MultiPlane/lens.cpp @@ -150,7 +150,7 @@ Lens::Lens(InputParams& params, long* my_seed, CosmoParamSet cosmoset, bool verb } else { if(field_buffer == 0.0){ - field_buffer = pow(3.0e14/800/pi/cosmo.rho_crit(0),1.0/3.); + field_buffer = pow(3.0e14/800/pi/cosmo.rho_crit_comoving(0),1.0/3.); std::cout << " Resetting field buffer to " << field_buffer << " Mpc." << std::endl; } // Compute the distribution variables : @@ -222,7 +222,7 @@ Lens::Lens(InputParams& params, long* my_seed, const COSMOLOGY &cosmoset, bool v } else { if(field_buffer == 0.0){ - field_buffer = pow(3.0e14/800/pi/cosmo.rho_crit(0),1.0/3.); + field_buffer = pow(3.0e14/800/pi/cosmo.rho_crit_comoving(0),1.0/3.); std::cout << " Resetting field buffer to " << field_buffer << " Mpc." << std::endl; } // Compute the distribution variables : @@ -898,7 +898,7 @@ void Lens::insertSubstructures(PosType Rregion, // in radians PosType Rsize; PosType AveMassTh; - PosType rho = density_contrast*cosmo.rho_crit(0)*cosmo.getOmega_matter()*(1+redshift)*(1+redshift)*(1+redshift); + PosType rho = density_contrast*cosmo.rho_crit_comoving(0)*cosmo.getOmega_matter()*(1+redshift)*(1+redshift)*(1+redshift); // rho in 1 * (M_sun/Mpc^3) * 1 * (1+z)^3 = M_sun / PhysMpc^3, // where Mpc \equiv comoving Mpc. @@ -1104,7 +1104,7 @@ void Lens::resetSubstructure(bool verbose){ PosType Rsize; - PosType rho = substructure.rho_tidal*cosmo.rho_crit(0)*cosmo.getOmega_matter()*(1+redshift)*(1+redshift)*(1+redshift); + PosType rho = substructure.rho_tidal*cosmo.rho_crit_comoving(0)*cosmo.getOmega_matter()*(1+redshift)*(1+redshift)*(1+redshift); Utilities::delete_container(substructure.halos); From 35aa5363b9bd4b8b42bfe9d9791934aabeb29e82 Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Wed, 12 Oct 2016 10:25:22 +0200 Subject: [PATCH 010/227] Set Dist in main lenses by default when constructing from parameter file. --- MultiPlane/lens.cpp | 3 +-- TreeCode/quadTree.cpp | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/MultiPlane/lens.cpp b/MultiPlane/lens.cpp index 57556135..e1409836 100644 --- a/MultiPlane/lens.cpp +++ b/MultiPlane/lens.cpp @@ -89,7 +89,6 @@ Lens::Lens(long* my_seed,PosType z_source, const COSMOLOGY &cosmoset,bool verbos } /** - * \ingroup Constructor * \brief allocates space for the halo trees and the inout lens, if there is any */ Lens::Lens(InputParams& params, long* my_seed, CosmoParamSet cosmoset, bool verbose) @@ -162,7 +161,6 @@ Lens::Lens(InputParams& params, long* my_seed, CosmoParamSet cosmoset, bool verb } /** - * \ingroup Constructor * \brief allocates space for the halo trees and the inout lens, if there is any */ Lens::Lens(InputParams& params, long* my_seed, const COSMOLOGY &cosmoset, bool verbose) @@ -1458,6 +1456,7 @@ void Lens::createMainHalos(InputParams& params) for(std::size_t i = 0; i < main_halos.size(); ++i){ main_halos[i]->setCosmology(cosmo); + main_halos[i]->setDist(); } } diff --git a/TreeCode/quadTree.cpp b/TreeCode/quadTree.cpp index 55c098ca..8c55bf09 100644 --- a/TreeCode/quadTree.cpp +++ b/TreeCode/quadTree.cpp @@ -120,7 +120,7 @@ QTreeNBHndl TreeQuad::BuildQTreeNB(PosType **xp,IndexType Nparticles,IndexType * original_xl = lengths[0]; original_yl = lengths[1]; - if(lengths[0] != 0 || lengths[1] != 0){ + if(lengths[0] == 0 || lengths[1] == 0){ throw std::invalid_argument("particles in same place."); } From 533e830dac9d8375be34e708e3fa97846e816478 Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Wed, 12 Oct 2016 10:48:40 +0200 Subject: [PATCH 011/227] Fixed last commit. --- MultiPlane/lens.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/MultiPlane/lens.cpp b/MultiPlane/lens.cpp index e1409836..20fcf198 100644 --- a/MultiPlane/lens.cpp +++ b/MultiPlane/lens.cpp @@ -1456,7 +1456,7 @@ void Lens::createMainHalos(InputParams& params) for(std::size_t i = 0; i < main_halos.size(); ++i){ main_halos[i]->setCosmology(cosmo); - main_halos[i]->setDist(); + main_halos[i]->setDist(cosmo); } } From 7d476776d22f87ecbf15248d113ba79eca1a1c47 Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Wed, 12 Oct 2016 14:15:36 +0200 Subject: [PATCH 012/227] Made changes to make LensHalo::Dist assigned in Lens::insertMain() and Lens::replaceMain(). Also imposed some more discipline on the LenHalo hierarchy by making zlens, mass and Rsize private variables of LensHalo and do the assignment of these variables from the parameter file through the LensHalo constructor only. --- AnalyticNSIE/base_analens.cpp | 11 +- AnalyticNSIE/elliptic.cpp | 4 +- AnalyticNSIE/randomize_lens.cpp | 10 +- AnalyticNSIE/readfiles_ana.cpp | 8 +- AnalyticNSIE/readfiles_uni.cpp | 4 +- Fitlens/fitlens.cpp | 2 +- MultiPlane/MOKAlens.cpp | 11 +- MultiPlane/lens.cpp | 5 + MultiPlane/lens_halos.cpp | 301 +++++++++++++++----------------- MultiPlane/particle_lens.cpp | 12 +- include/analytic_lens.h | 2 +- include/base_analens.h | 2 +- include/elliptic.h | 4 +- include/lens_halos.h | 47 ++--- 14 files changed, 215 insertions(+), 208 deletions(-) diff --git a/AnalyticNSIE/base_analens.cpp b/AnalyticNSIE/base_analens.cpp index 49a3be8c..2fbbeac0 100644 --- a/AnalyticNSIE/base_analens.cpp +++ b/AnalyticNSIE/base_analens.cpp @@ -159,8 +159,11 @@ void LensHaloBaseNSIE::force_halo( * force calculation. */ void LensHaloBaseNSIE::assignParams(InputParams& params){ - if(!params.get("main_mass",mass)) error_message1("main_mass",params.filename()); - if(!params.get("main_zlens",zlens)) error_message1("main_zlens",params.filename()); + double tmp; + if(!params.get("main_mass",tmp)) error_message1("main_mass",params.filename()); + LensHalo::setMass(tmp); + if(!params.get("main_zlens",tmp)) error_message1("main_zlens",params.filename()); + LensHalo::setZlens(tmp); if(!params.get("main_sigma",sigma)) error_message1("main_sigma",params.filename()); if(!params.get("main_core",rcore)) error_message1("main_core",params.filename()); if(!params.get("main_axis_ratio",fratio)) error_message1("main_axis_ratio",params.filename()); @@ -171,7 +174,7 @@ void LensHaloBaseNSIE::assignParams(InputParams& params){ } if(!params.get("main_pos_angle",pa)) error_message1("main_pos_angle",params.filename()); - Rsize = rmaxNSIE(sigma,mass,fratio,rcore); + Rsize = rmaxNSIE(sigma,get_mass(),fratio,rcore); Rmax = MAX(1.0,1.0/fratio)*Rsize; // redefine @@ -320,7 +323,7 @@ void LensHaloBaseNSIE::PrintLens(bool show_substruct,bool show_stars){ if(show_substruct){ if(substruct_implanted || sub_N > 0){ for(i=0;ikappa(x)/(ap*ap*ap*bp*p2); - PosType alpha[2]={0,0},tmp[2] = {m*(isohalo->get_Rsize()),0}; + PosType alpha[2]={0,0},tmp[2] = {m*(isohalo->getRsize()),0}; KappaType kappa=0,gamma[2]={0,0},phi; isohalo->force_halo(alpha,&kappa,gamma,&phi,tmp); @@ -34,7 +34,7 @@ PosType Elliptic::DALPHAYDM::operator()(PosType m){ double ap = m*m*a2 + lambda,bp = m*m*b2 + lambda; double p2 = x[0]*x[0]/ap/ap/ap/ap + x[1]*x[1]/bp/bp/bp/bp; // actually the inverse of equation (5) in Schramm 1990 - PosType alpha[2]={0,0},tmp[2] = {m*(isohalo->get_Rsize()),0}; + PosType alpha[2]={0,0},tmp[2] = {m*(isohalo->getRsize()),0}; KappaType kappa=0,gamma[2]={0,0},phi; isohalo->force_halo(alpha,&kappa,gamma,&phi,tmp); // here we need an elliptical kappa but in forcehalo the only elliptical kappas implemented are based on Ansatz I+II diff --git a/AnalyticNSIE/randomize_lens.cpp b/AnalyticNSIE/randomize_lens.cpp index 8f44ba8d..2a2b67ea 100644 --- a/AnalyticNSIE/randomize_lens.cpp +++ b/AnalyticNSIE/randomize_lens.cpp @@ -258,10 +258,10 @@ void LensHaloAnaNSIE::RandomizeSubstructure2(PosType rangeInRei,long *seed){ subs[k].set_slope(sub_beta); - subs[k].set_rscale(0.1*subs[k].get_Rsize()); + subs[k].set_rscale(0.1*subs[k].getRsize()); // maximum radius for a substructure of this mass - rmax = (Einstein_ro_save*rangeInRei + subs[k].get_Rsize() + rmax = (Einstein_ro_save*rangeInRei + subs[k].getRsize() + pow(2*subs[k].get_mass()*Einstein_ro_save/pi/Sigma_crit/sheartol,1./3.) ); //std::cout << "RcutSubstruct[%i] = %e\n",k,RcutSubstruct[k]); @@ -275,7 +275,7 @@ void LensHaloAnaNSIE::RandomizeSubstructure2(PosType rangeInRei,long *seed){ rav[0] += sub_x[k][0]; rav[1] += sub_x[k][1]; r2av += r*r; - area_av += pow(subs[k].get_Rsize(),2); + area_av += pow(subs[k].getRsize(),2); ++k; } } @@ -387,7 +387,7 @@ void LensHaloAnaNSIE::RandomizeSubstructure3(PosType rangeInRei,long *seed){ subs[k].set_Rsize(sub_Rsize*pow(subs[k].get_mass()/sub_Mmax,1/3.)); // maximum radius for a substructure of this mass - rmax = (Einstein_ro_save*rangeInRei + subs[k].get_Rsize() + rmax = (Einstein_ro_save*rangeInRei + subs[k].getRsize() + pow(2*subs[k].get_mass()*Einstein_ro_save/pi/Sigma_crit/sheartol,1./3.) ); //std::cout << "RcutSubstruct[%i] = %e\n",k,RcutSubstruct[k]); @@ -401,7 +401,7 @@ void LensHaloAnaNSIE::RandomizeSubstructure3(PosType rangeInRei,long *seed){ rav[0] += sub_x[k][0]; rav[1] += sub_x[k][1]; r2av += r*r; - area_av += pow(subs[k].get_Rsize(),2); + area_av += pow(subs[k].getRsize(),2); ++k; } } diff --git a/AnalyticNSIE/readfiles_ana.cpp b/AnalyticNSIE/readfiles_ana.cpp index 02a8baeb..e7a8d966 100644 --- a/AnalyticNSIE/readfiles_ana.cpp +++ b/AnalyticNSIE/readfiles_ana.cpp @@ -114,7 +114,7 @@ LensHaloFit::LensHaloFit(const COSMOLOGY& cosmo, int MyNmodes, PosType beta,PosT // assignParams(params); - zlens = zlensref ; + LensHalo::setZlens(zlensref); // zsource = zsourceref ; zsource_reference = zsourceref ; @@ -129,11 +129,11 @@ LensHaloFit::LensHaloFit(const COSMOLOGY& cosmo, int MyNmodes, PosType beta,PosT sigma = 0.0; setCosmology(cosmo); - Sigma_crit = cosmo.SigmaCrit(zlens, zsource_reference); + Sigma_crit = cosmo.SigmaCrit(getZlens(), zsource_reference); perturb_beta = beta; - Dl = cosmo.angDist(zlens); - Dls = cosmo.angDist(zlens,zsource_reference); + Dl = cosmo.angDist(getZlens()); + Dls = cosmo.angDist(getZlens(),zsource_reference); Ds = cosmo.angDist(zsource_reference); //if(verbose) PrintLens(false,false); } diff --git a/AnalyticNSIE/readfiles_uni.cpp b/AnalyticNSIE/readfiles_uni.cpp index c61ad527..d9ead682 100644 --- a/AnalyticNSIE/readfiles_uni.cpp +++ b/AnalyticNSIE/readfiles_uni.cpp @@ -173,7 +173,9 @@ void LensHalo::assignParams_stars(InputParams& params){ void LensHaloUniform::assignParams(InputParams& params){ //if(perturb_Nmodes > 0){ - if(!params.get("main_zlens",zlens)) error_message1("main_zlens",params.filename()); + PosType tmp; + if(!params.get("main_zlens",tmp)) error_message1("main_zlens",params.filename()); + LensHalo::setZlens(tmp); if(!params.get("kappa_uniform",kappa_uniform)) error_message1("kappa_uniform",params.filename()); if(!params.get("gamma_uniform_1",gamma_uniform[0])) error_message1("gamma_uniform_1",params.filename()); if(!params.get("gamma_uniform_2",gamma_uniform[1])) error_message1("gamma_uniform_2",params.filename()); diff --git a/Fitlens/fitlens.cpp b/Fitlens/fitlens.cpp index 31bd83a3..1e777297 100644 --- a/Fitlens/fitlens.cpp +++ b/Fitlens/fitlens.cpp @@ -372,7 +372,7 @@ void LensHaloFit::FindLensSimple( std::cout << "source : y[0] = " << y[0] << " , y[1] = " << y[1] << std::endl; // For convenience : - PosType zl = zlens ; + PosType zl = LensHalo::getZlens() ; PosType zs = zsource_reference ; // Converting source position to physical angle : diff --git a/MultiPlane/MOKAlens.cpp b/MultiPlane/MOKAlens.cpp index b6a30055..cd734e9f 100644 --- a/MultiPlane/MOKAlens.cpp +++ b/MultiPlane/MOKAlens.cpp @@ -186,7 +186,7 @@ LensHaloMassMap::LensHaloMassMap(InputParams& params, const COSMOLOGY& lenscosmo initMap(); // set redshift if necessary - if(zlens == -1) + if(LensHalo::getZlens() == -1) setZlens(map->zlens); } @@ -385,9 +385,12 @@ void LensHaloMassMap::checkCosmology() void LensHaloMassMap::assignParams(InputParams& params) { - if(!params.get("z_lens", zlens)) - zlens = -1; // set to -1 so that it will be set to the MOKA map value - + PosType tmp; + if(!params.get("z_lens", tmp)){ + LensHalo::setZlens(-1); // set to -1 so that it will be set to the MOKA map value + }else{ + LensHalo::setZlens(tmp); + } if(!params.get("MOKA_input_file", MOKA_input_file)) { ERROR_MESSAGE(); diff --git a/MultiPlane/lens.cpp b/MultiPlane/lens.cpp index 20fcf198..ffffbbb3 100644 --- a/MultiPlane/lens.cpp +++ b/MultiPlane/lens.cpp @@ -1486,6 +1486,7 @@ void Lens::clearMainHalos(bool verbose) void Lens::insertMainHalo(LensHalo* halo,bool addplanes,bool verbose) { halo->setCosmology(cosmo); + halo->setDist(cosmo); main_halos.push_back(halo); flag_switch_main_halo_on = true; @@ -1510,6 +1511,7 @@ void Lens::insertMainHalos(LensHalo** halos, std::size_t Nhalos,bool addplanes, for(std::size_t i = 0; i < Nhalos; ++i) { halos[i]->setCosmology(cosmo); + halos[i]->setDist(cosmo); main_halos.push_back(halos[i]); if(addplanes) addMainHaloToPlane(halos[i]); else addMainHaloToNearestPlane(halos[i]); @@ -1535,6 +1537,8 @@ void Lens::replaceMainHalos(LensHalo* halo,bool verbose) main_halos.clear(); halo->setCosmology(cosmo); + halo->setDist(cosmo); + main_halos.push_back(halo); flag_switch_main_halo_on = true; @@ -1558,6 +1562,7 @@ void Lens::replaceMainHalos(LensHalo** halos, std::size_t Nhalos,bool verbose) for(std::size_t i = 0; i < Nhalos; ++i) { halos[i]->setCosmology(cosmo); + halos[i]->setDist(cosmo); main_halos.push_back(halos[i]); } diff --git a/MultiPlane/lens_halos.cpp b/MultiPlane/lens_halos.cpp index e31a4b13..f6326166 100644 --- a/MultiPlane/lens_halos.cpp +++ b/MultiPlane/lens_halos.cpp @@ -30,15 +30,24 @@ LensHalo::LensHalo(PosType z,COSMOLOGY &cosmo){ zlens = z; } -LensHalo::LensHalo(InputParams& params){ - assignParams(params); +LensHalo::LensHalo(InputParams& params,COSMOLOGY &cosmo,bool needRsize){ + Dist = -1; + assignParams(params,needRsize); stars_implanted = false; posHalo[0] = posHalo[1] = 0.0; elliptical_flag = false; + setDist(cosmo); +} +LensHalo::LensHalo(InputParams& params,bool needRsize){ Dist = -1; + assignParams(params,needRsize); + stars_implanted = false; + posHalo[0] = posHalo[1] = 0.0; + elliptical_flag = false; } -void LensHalo::initFromMassFunc(float my_mass, float my_Rsize, float my_rscale, PosType my_slope, long *seed){ +void LensHalo::initFromMassFunc(float my_mass, float my_Rsize, float my_rscale + , PosType my_slope, long *seed){ mass = my_mass; Rsize = my_Rsize; rscale = my_rscale; @@ -48,13 +57,15 @@ void LensHalo::initFromMassFunc(float my_mass, float my_Rsize, float my_rscale, void LensHalo::error_message1(std::string parameter,std::string file){ ERROR_MESSAGE(); std::cout << "Parameter " << parameter << " is needed to construct a LensHalo. It needs to be set in parameter file " << file << "!" << std::endl; - exit(0); + throw std::runtime_error(parameter); } -void LensHalo::assignParams(InputParams& params){ - if(!params.get("mass",mass)) error_message1("mass",params.filename()); - if(!params.get("Rsize",Rsize)) error_message1("Rsize",params.filename()); - if(!params.get("z_lens",zlens)) error_message1("z_lens",params.filename()); +void LensHalo::assignParams(InputParams& params,bool needRsize){ + if(!params.get("main_mass",mass)) error_message1("main_mass",params.filename()); + if(needRsize){ + if(!params.get("main_Rsize",Rsize)) error_message1("main_Rsize",params.filename()); + } + if(!params.get("main_zlens",zlens)) error_message1("main_zlens",params.filename()); } void LensHalo::PrintStars(bool show_stars) const @@ -132,11 +143,11 @@ LensHaloNFW::LensHaloNFW() } LensHaloNFW::LensHaloNFW(float my_mass,float my_Rsize,PosType my_zlens,float my_concentration,float my_fratio,float my_pa,int my_stars_N, EllipMethod my_ellip_method){ - mass=my_mass, Rsize=my_Rsize, zlens=my_zlens; + LensHalo::setRsize(my_Rsize); fratio=my_fratio, pa=my_pa, stars_N=my_stars_N, main_ellip_method=my_ellip_method; stars_implanted = false; - rscale = Rsize/my_concentration; - xmax = Rsize/rscale; + rscale = LensHalo::getRsize()/my_concentration; + xmax = LensHalo::getRsize()/rscale; make_tables(); gmax = InterpolateFromTable(gtable, xmax); @@ -144,7 +155,7 @@ LensHaloNFW::LensHaloNFW(float my_mass,float my_Rsize,PosType my_zlens,float my_ set_slope(1); /// If the axis ratio given in the parameter file is set to 1 all ellipticizing routines are skipped. if(fratio!=1){ - Rmax = Rmax_to_Rsize_ratio*Rsize; + Rmax = Rmax_to_Rsize_ratio*LensHalo::getRsize(); //std::cout << getEllipMethod() << " method to ellipticise" << std::endl; if(getEllipMethod()==Fourier){ std::cout << "NFW constructor: slope set to " << get_slope() << std::endl; @@ -161,7 +172,7 @@ LensHaloNFW::LensHaloNFW(float my_mass,float my_Rsize,PosType my_zlens,float my_ }else{ set_flag_elliptical(false); - Rmax = Rsize; + Rmax = LensHalo::getRsize(); } } @@ -177,13 +188,13 @@ LensHaloNFW::LensHaloNFW(float my_mass,float my_Rsize,PosType my_zlens,float my_ posHalo[0] = posHalo[1] = 0.0; }*/ -LensHaloNFW::LensHaloNFW(InputParams& params) +LensHaloNFW::LensHaloNFW(InputParams& params):LensHalo(params) { assignParams(params); make_tables(); gmax = InterpolateFromTable(gtable, xmax); - mnorm = renormalization(Rsize); + mnorm = renormalization(LensHalo::getRsize()); std::cout << "mass normalization: " << mnorm << std::endl; @@ -191,7 +202,7 @@ LensHaloNFW::LensHaloNFW(InputParams& params) set_slope(1); // If the axis ratio given in the parameter file is set to 1 all ellipticizing routines are skipped. if(fratio!=1){ - Rmax = Rmax_to_Rsize_ratio*Rsize; + Rmax = Rmax_to_Rsize_ratio*LensHalo::getRsize(); //std::cout << getEllipMethod() << " method to ellipticise" << std::endl; if(getEllipMethod()==Fourier){ std::cout << "NFW constructor: slope set to " << get_slope() << std::endl; @@ -211,7 +222,7 @@ LensHaloNFW::LensHaloNFW(InputParams& params) } }else{ set_flag_elliptical(false); - Rmax = Rsize; + Rmax = LensHalo::getRsize(); } } @@ -318,18 +329,15 @@ PosType LensHaloNFW::InterpolateFromTable(PosType *table, PosType y) const{ void LensHaloNFW::assignParams(InputParams& params){ - if(!params.get("main_mass",mass)) error_message1("main_mass",params.filename()); - if(!params.get("main_Rsize",Rsize)) error_message1("main_Rsize",params.filename()); - if(!params.get("main_zlens",zlens)) error_message1("main_zlens",params.filename()); + PosType tmp; + if(!params.get("main_zlens",tmp)) error_message1("main_zlens",params.filename()); if(!params.get("main_concentration",rscale)) error_message1("main_concentration",params.filename()); if(!params.get("main_axis_ratio",fratio)){fratio=1; std::cout << "main_axis_ratio not defined in file " << params.filename() << ", hence set to 1." << std::endl;}; if(!params.get("main_pos_angle",pa)){pa=0; std::cout << "main_pos_angle not defined in file " << params.filename() << ", hence set to 0." << std::endl;}; if(!params.get("main_ellip_method",main_ellip_method)){if(fratio!=1){main_ellip_method=Pseudo;std::cout << "main_ellip_method is not defined in file " << params.filename() << ", hence set to Pseudo." << endl;};}; - - - rscale = Rsize/rscale; // was the concentration - xmax = Rsize/rscale; + rscale = LensHalo::getRsize()/rscale; // was the concentration + xmax = LensHalo::getRsize()/rscale; } LensHaloNFW::~LensHaloNFW(){ @@ -357,16 +365,16 @@ LensHaloNFW::~LensHaloNFW(){ /// Sets the profile to match the mass, Vmax and R_halfmass void LensHaloNFW::initFromFile(float my_mass, long *seed, float vmax, float r_halfmass){ - mass = my_mass; + LensHalo::setMass(my_mass); NFW_Utility nfw_util; // Find the NFW profile with the same mass, Vmax and R_halfmass - nfw_util.match_nfw(vmax,r_halfmass,mass,&rscale,&Rmax); - Rsize=Rmax; - rscale = Rsize/rscale; // Was the concentration - xmax = Rsize/rscale; + nfw_util.match_nfw(vmax,r_halfmass,LensHalo::get_mass(),&rscale,&Rmax); + LensHalo::setRsize(Rmax); + rscale = LensHalo::getRsize()/rscale; // Was the concentration + xmax = LensHalo::getRsize()/rscale; gmax = InterpolateFromTable(gtable,xmax); - // std::cout << Rmax_halo << " " << Rsize << std::endl; + // std::cout << Rmax_halo << " " << LensHalo::getRsize() << std::endl; } @@ -400,22 +408,23 @@ LensHaloPseudoNFW::LensHaloPseudoNFW( ,EllipMethod my_ellip_method /// ellipticizing method ) { - mass = my_mass; - Rsize = my_Rsize; - zlens = my_zlens; + LensHalo::setMass(my_mass); + LensHalo::setZlens(my_zlens); + LensHalo::setRsize(my_Rsize); + beta = my_beta; fratio = my_fratio; pa = my_pa; stars_N = my_stars_N; stars_implanted = false; - rscale = Rsize/my_concentration; - xmax = Rsize/rscale; + rscale = LensHalo::getRsize()/my_concentration; + xmax = LensHalo::getRsize()/rscale; make_tables(); if(fratio!=1){ - Rmax = Rmax_to_Rsize_ratio*Rsize; + Rmax = Rmax_to_Rsize_ratio*LensHalo::getRsize(); //std::cout << getEllipMethod() << " method to ellipticise" << std::endl; if(getEllipMethod()==Fourier){ std::cout << "Note: Fourier modes set to ellipticize kappa at slope main_slope+0.5, i.e. "<< get_slope()+0.5 << std::endl; @@ -429,17 +438,17 @@ LensHaloPseudoNFW::LensHaloPseudoNFW( } }else{ set_flag_elliptical(false); - Rmax = Rsize; + Rmax = LensHalo::getRsize(); } } /// The Fourier modes set to ellipticize kappa at slope main_slope+0.5, i.e. e.g. 1.5 for main_slope = 1. Note that set_slope is overridden for PseudoNFW to recalculate tables for different beta. But only fixed values of beta, i.e. 1,2 and >=3 are allowed! -LensHaloPseudoNFW::LensHaloPseudoNFW(InputParams& params) +LensHaloPseudoNFW::LensHaloPseudoNFW(InputParams& params):LensHalo(params) { assignParams(params); make_tables(); if(fratio!=1){ - Rmax = Rmax_to_Rsize_ratio*Rsize; + Rmax = Rmax_to_Rsize_ratio*LensHalo::getRsize(); //std::cout << getEllipMethod() << " method to ellipticise" << std::endl; if(getEllipMethod()==Fourier){ std::cout << "Note: Fourier modes set to ellipticize kappa at slope main_slope+0.5, i.e. "<< get_slope()+0.5 << std::endl; @@ -454,7 +463,7 @@ LensHaloPseudoNFW::LensHaloPseudoNFW(InputParams& params) } }else{ set_flag_elliptical(false); - Rmax = Rsize; + Rmax = LensHalo::getRsize(); } } @@ -515,17 +524,14 @@ void LensHaloPseudoNFW::initFromMassFunc(float my_mass, float my_Rsize, float my } void LensHaloPseudoNFW::assignParams(InputParams& params){ - if(!params.get("main_mass",mass)) error_message1("main_mass",params.filename()); - if(!params.get("main_Rsize",Rsize)) error_message1("main_Rsize",params.filename()); - if(!params.get("main_zlens",zlens)) error_message1("main_zlens",params.filename()); if(!params.get("main_concentration",rscale)) error_message1("main_concentration",params.filename()); if(!params.get("main_slope",beta)) error_message1("main_slope",params.filename()); if(!params.get("main_axis_ratio",fratio)){fratio=1; std::cout << "main_axis_ratio not defined in file " << params.filename() << ", hence set to 1." << std::endl;}; if(!params.get("main_pos_angle",pa)){pa=0; std::cout << "main_pos_angle not defined in file " << params.filename() << ", hence set to 0." << std::endl;}; if(!params.get("main_ellip_method",main_ellip_method)){if(fratio!=1){main_ellip_method=Pseudo;std::cout << "main_ellip_method is not defined in file " << params.filename() << ", hence set to Pseudo. CAUTION: Ellipticizing methods have not been tested with PseudoNFW yet!" << endl;};}; - rscale = Rsize/rscale; // was the concentration - xmax = Rsize/rscale; + rscale = LensHalo::getRsize()/rscale; // was the concentration + xmax = LensHalo::getRsize()/rscale; } LensHaloPseudoNFW::~LensHaloPseudoNFW(){ @@ -554,18 +560,22 @@ LensHaloPowerLaw::LensHaloPowerLaw( ,int my_stars_N /// number of stars, not yet implanted ,EllipMethod my_ellip_method /// ellipticizing method ){ - mass=my_mass, Rsize=my_Rsize, zlens=my_zlens; + + LensHalo::setMass(my_mass); + LensHalo::setZlens(my_zlens); + LensHalo::setRsize(my_Rsize); + beta=my_beta; fratio=my_fratio, pa=my_pa, main_ellip_method=my_ellip_method, stars_N=my_stars_N; stars_implanted = false; rscale = 1; - xmax = Rsize/rscale ; /// xmax needs to be in initialized before the mass_norm_factor for Pseudo ellip method is calculated via set_norm_factor() + xmax = LensHalo::getRsize()/rscale ; /// xmax needs to be in initialized before the mass_norm_factor for Pseudo ellip method is calculated via set_norm_factor() //mnorm = renormalization(get_Rmax()); //std::cout << "PA in PowerLawConstructor: " << pa << std::endl; if(fratio!=1){ - Rmax = Rmax_to_Rsize_ratio*Rsize; + Rmax = Rmax_to_Rsize_ratio*LensHalo::getRsize(); //std::cout << getEllipMethod() << " method to ellipticise" << std::endl; if(getEllipMethod()==Fourier){ calcModes(fratio, beta, pa, mod); @@ -581,11 +591,12 @@ LensHaloPowerLaw::LensHaloPowerLaw( } }else{ set_flag_elliptical(false); - Rmax = Rsize; + Rmax = LensHalo::getRsize(); } } -LensHaloPowerLaw::LensHaloPowerLaw(InputParams& params){ +LensHaloPowerLaw::LensHaloPowerLaw(InputParams& params):LensHalo(params) +{ assignParams(params); /// If the 2nd argument in calcModes(fratio, slope, pa, mod), the slope, is set to 1 it yields an elliptical kappa contour of given axis ratio (fratio) at the radius where the slope of the 3D density profile is -2, which is defined as the scale radius for the NFW profile. To ellipticize the potential instead of the convergence use calcModes(fratio, 2-get_slope(), pa, mod), this produces also an ellipse in the convergence map, but at the radius where the slope is 2-get_slope(). /// If the axis ratio given in the parameter file is set to 1 all ellipticizing routines are skipped. @@ -593,10 +604,10 @@ LensHaloPowerLaw::LensHaloPowerLaw(InputParams& params){ // rscale = xmax = 1.0; // Commented in order to have a correct computation of the potential term in the time delay. // Replacing it by : rscale = 1; - xmax = Rsize/rscale; + xmax = LensHalo::getRsize()/rscale; if(fratio!=1){ - Rmax = Rmax_to_Rsize_ratio*Rsize; + Rmax = Rmax_to_Rsize_ratio*LensHalo::getRsize(); //for(int islope=1;islope<20;islope++){ //beta=islope*0.1; @@ -632,7 +643,7 @@ LensHaloPowerLaw::LensHaloPowerLaw(InputParams& params){ } }else{ set_flag_elliptical(false); - Rmax = Rsize; + Rmax = LensHalo::getRsize(); } // rscale = xmax = 1.0; // mnorm = renormalization(get_Rmax()); @@ -649,9 +660,6 @@ void LensHaloPowerLaw::initFromMassFunc(float my_mass, float my_Rsize, float my_ } void LensHaloPowerLaw::assignParams(InputParams& params){ - if(!params.get("main_mass",mass)) error_message1("main_mass",params.filename()); - if(!params.get("main_Rsize",Rsize)) error_message1("main_Rsize",params.filename()); - if(!params.get("main_zlens",zlens)) error_message1("main_zlens",params.filename()); if(!params.get("main_slope",beta)) error_message1("main_slope, example 1",params.filename()); //if(beta>=2.0) error_message1("main_slope < 2",params.filename()); if(!params.get("main_axis_ratio",fratio)){fratio=1; std::cout << "main_axis_ratio not defined in file " << params.filename() << ", hence set to 1." << std::endl;}; @@ -695,8 +703,11 @@ LensHaloRealNSIE::LensHaloRealNSIE( ,float my_pa ,int my_stars_N) :LensHalo(){ - mass=my_mass, zlens=my_zlens, rscale=1.0; - + rscale=1.0; + LensHalo::setMass(my_mass); + LensHalo::setZlens(my_zlens); + + sigma=my_sigma, rcore=my_rcore; fratio=my_fratio, pa=my_pa, stars_N=my_stars_N; stars_implanted = false; @@ -707,24 +718,21 @@ LensHaloRealNSIE::LensHaloRealNSIE( if(objectCount == 1){ // make table for calculating elliptical integrale construct_ellip_tables(); } - - //Rsize = rmaxNSIE(sigma,mass,fratio,rcore); - //Rmax = MAX(1.0,1.0/fratio)*Rsize; // redefine - Rsize = rmax_calc(); + LensHalo::setRsize(rmax_calc()); //std::cout << "NSIE " << Rsize << std::endl; - Rmax = Rmax_to_Rsize_ratio*Rsize; + Rmax = Rmax_to_Rsize_ratio*LensHalo::getRsize(); if(fratio > 1.0 || fratio < 0.01) throw std::invalid_argument("invalid fratio"); if(rcore > 0.0){ - mass = MassBy1DIntegation(Rsize); + LensHalo::setMass(MassBy1DIntegation(LensHalo::getRsize())); } } -LensHaloRealNSIE::LensHaloRealNSIE(InputParams& params){ +LensHaloRealNSIE::LensHaloRealNSIE(InputParams& params):LensHalo(params,false){ sigma = 0.; - zlens = 0.; + LensHalo::setZlens(0.0); fratio = 0.; pa = 0.; rcore = 0.; @@ -739,21 +747,18 @@ LensHaloRealNSIE::LensHaloRealNSIE(InputParams& params){ } //Rsize = rmaxNSIE(sigma,mass,fratio,rcore); //Rmax = MAX(1.0,1.0/fratio)*Rsize; // redefine - Rsize = rmax_calc(); - Rmax = Rmax_to_Rsize_ratio*Rsize; + LensHalo::setRsize(rmax_calc()); + Rmax = Rmax_to_Rsize_ratio*LensHalo::getRsize(); if(fratio > 1.0 || fratio < 0.01) throw std::invalid_argument("invalid fratio"); if(rcore > 0.0){ - mass = MassBy1DIntegation(Rsize); + LensHalo::setMass(MassBy1DIntegation(LensHalo::getRsize()) ); } } void LensHaloRealNSIE::assignParams(InputParams& params){ - if(!params.get("main_mass",mass)) error_message1("main_mass",params.filename()); - if(!params.get("main_zlens",zlens)) error_message1("main_zlens",params.filename()); - if(!params.get("main_sigma",sigma)) error_message1("main_sigma",params.filename()); if(!params.get("main_core",rcore)) error_message1("main_core",params.filename()); if(!params.get("main_axis_ratio",fratio)) error_message1("main_axis_ratio",params.filename()); @@ -802,7 +807,8 @@ void LensHaloRealNSIE::construct_ellip_tables(){ PosType LensHaloRealNSIE::rmax_calc(){ if(fratio == 1.0 || rcore > 0.0) - return sqrt( pow(mass*Grav*lightspeed*lightspeed*sqrt(fratio)/pi/sigma/sigma + rcore,2) + return sqrt( pow( LensHalo::get_mass()*Grav*lightspeed*lightspeed + *sqrt(fratio)/pi/sigma/sigma + rcore,2) - rcore*rcore ); // This is because there is no easy way of finding the circular Rmax for a fixed mass when rcore != 0 @@ -814,7 +820,7 @@ PosType LensHaloRealNSIE::rmax_calc(){ double ellipticint = Utilities::InterpolateYvec(q_table,Fofq_table,fratio)*sqrt(fratio); - return mass*Grav*lightspeed*lightspeed/pi/sigma/sigma/ellipticint; + return LensHalo::get_mass()*Grav*lightspeed*lightspeed/pi/sigma/sigma/ellipticint; } /* @@ -964,16 +970,16 @@ void LensHalo::force_halo_asym( double theta; if(xcm[0] == 0.0 && xcm[1] == 0.0) theta = 0.0; else theta=atan2(xcm[1],xcm[0]); - if(rcm2 > Rsize*Rsize){ + if(rcm2 > LensHalo::getRsize()*LensHalo::getRsize()){ PosType alpha_iso[2],alpha_ellip[2]; alpha_ellip[0] = alpha_ellip[1] = 0; - if(main_ellip_method==Pseudo){alphakappagamma_asym(Rsize,theta, alpha_tmp,&kappa_tmp,gamma_tmp,&phi_tmp);} - if(main_ellip_method==Fourier){alphakappagamma1asym(Rsize,theta, alpha_tmp,&kappa_tmp,gamma_tmp,&phi_tmp);} - if(main_ellip_method==Schramm){alphakappagamma2asym(Rsize,theta, alpha_tmp,&kappa_tmp,gamma_tmp,&phi_tmp);} - if(main_ellip_method==Keeton){alphakappagamma3asym(Rsize,theta, alpha_tmp,&kappa_tmp,gamma_tmp,&phi_tmp);} + if(main_ellip_method==Pseudo){alphakappagamma_asym(LensHalo::getRsize(),theta, alpha_tmp,&kappa_tmp,gamma_tmp,&phi_tmp);} + if(main_ellip_method==Fourier){alphakappagamma1asym(LensHalo::getRsize(),theta, alpha_tmp,&kappa_tmp,gamma_tmp,&phi_tmp);} + if(main_ellip_method==Schramm){alphakappagamma2asym(LensHalo::getRsize(),theta, alpha_tmp,&kappa_tmp,gamma_tmp,&phi_tmp);} + if(main_ellip_method==Keeton){alphakappagamma3asym(LensHalo::getRsize(),theta, alpha_tmp,&kappa_tmp,gamma_tmp,&phi_tmp);} alpha_ellip[0]=alpha_tmp[0]*mass_norm_factor; alpha_ellip[1]=alpha_tmp[1]*mass_norm_factor; - double f1 = (Rmax - r)/(Rmax - Rsize),f2 = (r - Rsize)/(Rmax - Rsize); + double f1 = (Rmax - r)/(Rmax - LensHalo::getRsize()),f2 = (r - LensHalo::getRsize())/(Rmax - LensHalo::getRsize()); // PosType tmp = mass/Rmax/pi/r; PosType tmp = mass/rcm2/pi; @@ -1096,7 +1102,7 @@ void LensHalo::force_halo_asym( if(rcm2 < Rmax*Rmax){ PosType ellipR = ellipticRadiusNSIE(xcm,fratio,pa); - if(ellipR > Rsize){ + if(ellipR > LensHalo::getRsize()){ // This is the case when the ray is within the NSIE's circular region of influence but outside its elliptical truncation PosType alpha_out[2],alpha_in[2],rin,x_in[2]; @@ -1106,7 +1112,7 @@ void LensHalo::force_halo_asym( alpha_in[0] = alpha_in[1] = 0; - rin = r*Rsize/ellipR; + rin = r*LensHalo::getRsize()/ellipR; alpha_out[0] = prefac*xcm[0]/r; alpha_out[1] = prefac*xcm[1]/r; @@ -1218,9 +1224,9 @@ void LensHaloRealNSIE::force_halo( if(rcm2 < Rmax*Rmax){ //PosType ellipR = ellipticRadiusNSIE(xcm,fratio,pa); float units = pow(sigma/lightspeed,2)/Grav;///sqrt(fratio); // mass/distance(physical) - // std::cout << "rsize , rmax, mass_norm =" << Rsize << " , " << Rmax << " , " << mass_norm_factor << std::endl; - if(rcm2 > Rsize*Rsize) - //if(ellipR > Rsize*Rsize) + // std::cout << "rsize , rmax, mass_norm =" << LensHalo::getRsize() << " , " << Rmax << " , " << mass_norm_factor << std::endl; + if(rcm2 > LensHalo::getRsize()*LensHalo::getRsize()) + //if(ellipR > LensHalo::getRsize()*LensHalo::getRsize()) { // This is the case when the ray is within the NSIE's circular region of influence but outside its elliptical truncation @@ -1228,9 +1234,9 @@ void LensHaloRealNSIE::force_halo( //PosType prefac = -1.0*mass/Rmax/pi; PosType r = sqrt(rcm2); - //double Rin = sqrt(rcm2)*Rsize/ellipR; - //std::cout << Rmax << " " << Rsize << " " << Rmax/Rsize << std::endl; - double f1 = (Rmax - r)/(Rmax - Rsize),f2 = (r - Rsize)/(Rmax - Rsize); + //double Rin = sqrt(rcm2)*LensHalo::getRsize()/ellipR; + //std::cout << Rmax << " " << LensHalo::getRsize() << " " << Rmax/LensHalo::getRsize() << std::endl; + double f1 = (Rmax - r)/(Rmax - LensHalo::getRsize()),f2 = (r - LensHalo::getRsize())/(Rmax - LensHalo::getRsize()); //double f1 = (Rmax - r)/(Rmax - Rin),f2 = (r - Rin)/(Rmax - Rin); // SIE solution @@ -1248,29 +1254,18 @@ void LensHaloRealNSIE::force_halo( // point mass solution // PosType tmp = mass/rcm2/pi; - PosType tmp = mass/rcm2/pi; + PosType tmp = LensHalo::get_mass()/rcm2/pi; alpha_iso[0] = -1.0*tmp*xcm[0]; alpha_iso[1] = -1.0*tmp*xcm[1]; alpha[0] += alpha_iso[0]*f2 + alpha_ellip[0]*f1; alpha[1] += alpha_iso[1]*f2 + alpha_ellip[1]*f1; { - PosType tmp = -2.0*mass/rcm2/pi/rcm2; + PosType tmp = -2.0*LensHalo::get_mass()/rcm2/pi/rcm2; gamma[0] += 0.5*(xcm[0]*xcm[0]-xcm[1]*xcm[1])*tmp; gamma[1] += xcm[0]*xcm[1]*tmp; - /* test lines to be uncommented - PosType tmp = -2.0*prefac/rcm2; - gamma[0] += 0.5*(xcm[0]*xcm[0]-xcm[1]*xcm[1])*tmp*f2; - gamma[1] += xcm[0]*xcm[1]*tmp*f2; - - KappaType tmp_k[2]={0,0}; - *kappa += units*kappaNSIE(xcm,fratio,rcore,pa)*f1; - gammaNSIE(tmp_k,xcm,fratio,rcore,pa); - gamma[0] += units*tmp_k[0]*f1; - gamma[1] += units*tmp_k[1]*f1; - */ } }else{ @@ -1278,7 +1273,6 @@ void LensHaloRealNSIE::force_halo( xt[0]=xcm[0]; xt[1]=xcm[1]; alphaNSIE(tmp,xt,fratio,rcore,pa); - PosType tmpa[2]={tmp[0],tmp[1]}; //alpha[0] = units*tmp[0]; // minus sign removed because already included in alphaNSIE //alpha[1] = units*tmp[1]; // Why was the "+=" removed? alpha[0] += units*tmp[0];//*sqrt(fratio); @@ -1289,22 +1283,13 @@ void LensHaloRealNSIE::force_halo( gammaNSIE(tmp,xt,fratio,rcore,pa); gamma[0] += units*tmp[0]; gamma[1] += units*tmp[1]; - double theta=atan2(xcm[1],xcm[0]); - //if(theta < 0.660 && theta> 0.659){ - // std::cout << theta << " , " << units*tmp[0] << " , " << units*tmp[1] << " , " << units*kappaNSIE(xt,fratio,rcore,pa) << " , " << tmpa[0]*units << " , " << tmpa[0]*units << std::endl; - - - //} - //if (rcm2 < 1E-9){ - //std::cout << units*kappaNSIE(xt,fratio,rcore,pa) << " " << units*tmp[0] << " " << units*tmp[1] << " " << rcm2 <get_Rsize()); + PosType tmp = m*(isohalo->getRsize()); KappaType kappa=0; double xiso=tmp/isohalo->rscale; - //PosType alpha[2]={0,0},tm[2] = {m*(isohalo->get_Rsize()),0}; + //PosType alpha[2]={0,0},tm[2] = {m*(isohalo->getRsize()),0}; //KappaType kappam=0,gamma[2]={0,0},phi; kappa=isohalo->kappa_h(xiso)/pi/xiso/xiso*isohalo->mass; @@ -1888,10 +1875,10 @@ PosType LensHalo::DALPHAYDM::operator()(PosType m){ double ap = m*m*a2 + lambda,bp = m*m*b2 + lambda; double p2 = x[0]*x[0]/ap/ap/ap/ap + x[1]*x[1]/bp/bp/bp/bp; // actually the inverse of equation (5) in Schramm 1990 - PosType tmp = m*(isohalo->get_Rsize()); + PosType tmp = m*(isohalo->getRsize()); KappaType kappa=0; double xiso=tmp/isohalo->rscale; - //PosType alpha[2]={0,0},tm[2] = {m*(isohalo->get_Rsize()),0}; + //PosType alpha[2]={0,0},tm[2] = {m*(isohalo->getRsize()),0}; //KappaType kappam=0,gamma[2]={0,0},phi; kappa=isohalo->kappa_h(xiso)/pi/xiso/xiso*isohalo->mass; diff --git a/MultiPlane/particle_lens.cpp b/MultiPlane/particle_lens.cpp index c627e820..82b3782d 100644 --- a/MultiPlane/particle_lens.cpp +++ b/MultiPlane/particle_lens.cpp @@ -25,10 +25,12 @@ LensHaloParticles::LensHaloParticles( LensHalo::setCosmology(cosmo); LensHalo::set_Rsize(1.0e3); LensHalo::set_flag_elliptical(false); + stars_N = 0; stars_implanted = false; - Rsize = Rmax = 1.0e3; + Rmax = 1.0e3; + LensHalo::setRsize(Rmax); readPositionFileASCII(simulation_filename); @@ -42,9 +44,8 @@ LensHaloParticles::LensHaloParticles( // convert from comoving to physical coordinates PosType scale_factor = 1/(1+redshift); - mass = 0.0; mcenter *= 0.0; - PosType max_mass = 0.0,min_mass = HUGE_VALF; + PosType max_mass = 0.0,min_mass = HUGE_VALF,mass=0; for(size_t i=0;i max_mass) ? masses[multimass*i] : max_mass; min_mass = (masses[multimass*i] < min_mass) ? masses[multimass*i] : min_mass; } - + LensHalo::setMass(mass); + mcenter /= mass; std::cout << " Particle mass range : " << min_mass << " to " << max_mass << " ratio of : " << max_mass/min_mass << std::endl; @@ -76,7 +78,7 @@ LensHaloParticles::LensHaloParticles( if(r2 > r2max) r2max = r2; } - Rsize = sqrt(r2max); + LensHalo::setRsize( sqrt(r2max) ); } // rotate positions diff --git a/include/analytic_lens.h b/include/analytic_lens.h index e0641d12..a35e648d 100644 --- a/include/analytic_lens.h +++ b/include/analytic_lens.h @@ -152,7 +152,7 @@ class LensHaloAnaNSIE : public LensHaloBaseNSIE{ /// get the velocity dispersion virtual PosType get_sigma(){return sigma;}; /// get the NSIE radius - //PosType get_Rsize(){return Rsize;}; + //PosType getRsize(){return Rsize;}; /// get the axis ratio virtual PosType get_fratio(){return fratio;}; /// get the position angle diff --git a/include/base_analens.h b/include/base_analens.h index d65635c4..f2dde1d3 100644 --- a/include/base_analens.h +++ b/include/base_analens.h @@ -71,7 +71,7 @@ class LensHaloBaseNSIE : public LensHalo{ /// get the velocity dispersion virtual PosType get_sigma(){return sigma;}; /// get the NSIE radius - //PosType get_Rsize(){return Rsize;}; + //PosType getRsize(){return Rsize;}; /// get the axis ratio virtual PosType get_fratio(){return fratio;}; /// get the position angle diff --git a/include/elliptic.h b/include/elliptic.h index 4655a0f0..028cc7a9 100644 --- a/include/elliptic.h +++ b/include/elliptic.h @@ -30,8 +30,8 @@ class Elliptic{ ,PosType inclination /// position angle of ellipse ) : inclination(inclination),isohalo(isohalo){ - a = isohalo->get_Rsize()*sqrt(fratio); - b = isohalo->get_Rsize()/sqrt(fratio); + a = isohalo->getRsize()*sqrt(fratio); + b = isohalo->getRsize()/sqrt(fratio); }; /// The deflection angle in solar mass/Mpc diff --git a/include/lens_halos.h b/include/lens_halos.h index 83b439ce..ee62fdfd 100644 --- a/include/lens_halos.h +++ b/include/lens_halos.h @@ -47,21 +47,22 @@ class LensHalo{ public: LensHalo(); LensHalo(PosType z,COSMOLOGY &cosmo); - LensHalo(InputParams& params); + LensHalo(InputParams& params,COSMOLOGY &cosmo,bool needRsize = true); + LensHalo(InputParams& params,bool needRsize = true); virtual ~LensHalo(); /** get the Rmax which is larger than Rsize in Mpc. This is the region exterior to which the halo will be considered a point mass. Between Rsize and Rmax the deflection and shear are interpolated. */ - float get_Rmax() const { return Rmax; } + inline float get_Rmax() const { return Rmax; } /// get the Rsize which is the size of the halo in Mpc - float get_Rsize() const { return Rsize; } + inline float getRsize() const { return Rsize; } /// get the mass solar units - float get_mass() const { return mass; } + inline float get_mass() const { return mass; } /// get the scale radius in Mpc - float get_rscale() const { return rscale; } + inline float get_rscale() const { return rscale; } /// get the redshift - PosType getZlens() const { return zlens; } + inline PosType getZlens() const { return zlens; } // set the position of the Halo in physical Mpc on the lens plane //void setX(PosType PosX, PosType PosY) { posHalo[0] = PosX ; posHalo[1] = PosY ; } @@ -105,11 +106,16 @@ class LensHalo{ virtual void set_rscale(float my_rscale){rscale=my_rscale; xmax = Rsize/rscale;}; /// set redshift void setZlens(PosType my_zlens){zlens=my_zlens; Dist=-1;} + void setRsize(PosType R){Rsize = R;} + // ste redshift and distance void setZlensDist(PosType my_zlens,const COSMOLOGY &cos){ zlens=my_zlens; Dist = cos.angDist(zlens); } + void setMass(PosType m){mass = m;} + + /// set slope virtual void set_slope(PosType my_slope){beta=my_slope;}; /// get slope @@ -194,10 +200,14 @@ class LensHalo{ PosType Dist; /// Position of the Halo in angle PosType posHalo[2]; + PosType zlens; + float mass; + // This is the size of the halo beyond which it does not have the profile expected profile. + float Rsize = 0; + protected: - PosType zlens; // make LensHalo uncopyable void operator=(LensHalo &){}; @@ -210,9 +220,6 @@ class LensHalo{ void force_halo_sym(PosType *alpha,KappaType *kappa,KappaType *gamma,KappaType *phi,PosType const *xcm,bool subtract_point=false,PosType screening = 1.0); void force_halo_asym(PosType *alpha,KappaType *kappa,KappaType *gamma,KappaType *phi,PosType const *xcm,bool subtract_point=false,PosType screening = 1.0); - - // This is the size of the halo beyond which it does not have the profile expected profile. - float Rsize = 0; struct norm_func{ norm_func(LensHalo& halo, PosType my_r_max): halo(halo), r_max(my_r_max){}; @@ -253,15 +260,13 @@ class LensHalo{ /// read in parameters from a parameterfile in InputParams params - void assignParams(InputParams& params); + void assignParams(InputParams& params,bool needRsize); /// read in star parameters. This is valid for all halos and not overloaded. void assignParams_stars(InputParams& params); /// error message printout void error_message1(std::string name,std::string filename); - float mass; - // Beyond Rmax the halo will be treated as a point mass. Between Rsize and Rmax the deflection // and shear are interpolated. For circularly symmetric lenses Rmax should be equal to Rsize float Rmax; @@ -709,9 +714,9 @@ class LensHaloNFW: public LensHalo{ void initFromFile(float my_mass, long *seed, float vmax, float r_halfmass); void initFromMassFunc(float my_mass, float my_Rsize, float my_rscale, PosType my_slope, long *seed); /// set Rsize - void set_Rsize(float my_Rsize){Rsize=my_Rsize; xmax = Rsize/rscale; gmax = InterpolateFromTable(gtable,xmax);}; + void set_Rsize(float my_Rsize){LensHalo::setRsize(my_Rsize); xmax = LensHalo::getRsize()/rscale; gmax = InterpolateFromTable(gtable,xmax);}; /// set scale radius - void set_rscale(float my_rscale){rscale=my_rscale; xmax = Rsize/rscale; gmax = InterpolateFromTable(gtable,xmax);}; + void set_rscale(float my_rscale){rscale=my_rscale; xmax = LensHalo::getRsize()/rscale; gmax = InterpolateFromTable(gtable,xmax);}; protected: /// table size @@ -747,7 +752,7 @@ class LensHaloNFW: public LensHalo{ return -0.5*x*x*InterpolateFromTable(g2table,x)/gmax; } inline KappaType phi_h(PosType x) const{ - return 0.25*(InterpolateFromTable(htable,x) - InterpolateFromTable(htable,Rsize/rscale))/gmax + log(Rsize) ; + return 0.25*(InterpolateFromTable(htable,x) - InterpolateFromTable(htable,LensHalo::getRsize()/rscale))/gmax + log(LensHalo::getRsize()) ; // The constant contribution is made to match with the point mass at x = Rsize/rscale. } inline KappaType phi_int(PosType x) const{ @@ -897,7 +902,7 @@ class LensHaloPowerLaw: public LensHalo{ /// this is phi Sigma_crit pi / mass, the constants are added so that it is continous over the bourder at Rsize inline KappaType phi_h(PosType x) const { if(x==0) x=1e-6*xmax; - return ( pow(x/xmax,2-beta) - 1 )/(2-beta) + log(Rsize) ; + return ( pow(x/xmax,2-beta) - 1 )/(2-beta) + log(LensHalo::getRsize()) ; } inline KappaType phi_int(PosType x) const{ //return alpha_int(x); @@ -933,7 +938,7 @@ class LensHaloRealNSIE : public LensHalo{ /// get the velocity dispersion float get_sigma(){return sigma;}; /// get the NSIE radius - float get_Rsize(){return Rsize;}; + //float get_Rsize(){return Rsize;}; /// get the axis ratio float get_fratio(){return fratio;}; /// get the position angle @@ -944,7 +949,7 @@ class LensHaloRealNSIE : public LensHalo{ /// set the velocity dispersion void set_sigma(float my_sigma){sigma=my_sigma;}; /// set the NSIE radius - void set_Rsize(float my_Rsize){Rsize=my_Rsize;}; + //void set_Rsize(float my_Rsize){Rsize=my_Rsize;}; ///set the axis ratio void set_fratio(float my_fratio){fratio=my_fratio;}; /// set the position angle @@ -1031,7 +1036,7 @@ class LensHaloHernquist: public LensHalo{ /// set Rsize //void set_Rsize(float my_Rsize){Rsize=my_Rsize; xmax = Rsize/rscale; gmax = InterpolateFromTable(gtable,xmax);}; /// set scale radius - void set_rscale(float my_rscale){rscale=my_rscale; xmax = Rsize/rscale; gmax = InterpolateFromTable(gtable,xmax);}; + void set_rscale(float my_rscale){rscale=my_rscale; xmax = LensHalo::getRsize()/rscale; gmax = InterpolateFromTable(gtable,xmax);}; // friend struct Ialpha_func; protected: @@ -1096,7 +1101,7 @@ class LensHaloJaffe: public LensHalo{ /// set Rsize //void set_Rsize(float my_Rsize){Rsize = my_Rsize; xmax = Rsize/rscale; gmax = InterpolateFromTable(gtable,xmax);}; /// set scale radius - void set_rscale(float my_rscale){rscale=my_rscale; xmax = Rsize/rscale; gmax = InterpolateFromTable(gtable,xmax);}; + void set_rscale(float my_rscale){rscale=my_rscale; xmax = LensHalo::getRsize()/rscale; gmax = InterpolateFromTable(gtable,xmax);}; PosType ffunction(PosType x) const; PosType gfunction(PosType x) const; From 13b1dd68d2f4cee8c6aa5762ed83835d901086e4 Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Wed, 12 Oct 2016 16:15:56 +0200 Subject: [PATCH 013/227] Corrected bug where NSIE redshifts coming from the parameter file where set to zero. Cleaned up some loose ends. --- AnalyticNSIE/randomize_lens.cpp | 4 ++-- AnalyticNSIE/readfiles_uni.cpp | 5 +---- MultiPlane/lens.cpp | 1 + MultiPlane/lens_halos.cpp | 3 +-- MultiPlane/particle_lens.cpp | 1 - include/lens_halos.h | 13 +++++-------- 6 files changed, 10 insertions(+), 17 deletions(-) diff --git a/AnalyticNSIE/randomize_lens.cpp b/AnalyticNSIE/randomize_lens.cpp index 2a2b67ea..1997b0d0 100644 --- a/AnalyticNSIE/randomize_lens.cpp +++ b/AnalyticNSIE/randomize_lens.cpp @@ -253,7 +253,7 @@ void LensHaloAnaNSIE::RandomizeSubstructure2(PosType rangeInRei,long *seed){ }while(subs[k].get_mass() < sub_Mmin); // not sure why this is necessary // average density of a substructure does not scale with host - subs[k].set_Rsize(sub_Rsize*pow(scale,1./3.) + subs[k].set_RsizeRmax(sub_Rsize*pow(scale,1./3.) *pow(subs[k].get_mass()/sub_Mmax/scale,1/3.)); subs[k].set_slope(sub_beta); @@ -384,7 +384,7 @@ void LensHaloAnaNSIE::RandomizeSubstructure3(PosType rangeInRei,long *seed){ }while(subs[k].get_mass() < sub_Mmin); // not sure why this is necessary // average density of a substructure does not scale with host - subs[k].set_Rsize(sub_Rsize*pow(subs[k].get_mass()/sub_Mmax,1/3.)); + subs[k].set_RsizeRmax(sub_Rsize*pow(subs[k].get_mass()/sub_Mmax,1/3.)); // maximum radius for a substructure of this mass rmax = (Einstein_ro_save*rangeInRei + subs[k].getRsize() diff --git a/AnalyticNSIE/readfiles_uni.cpp b/AnalyticNSIE/readfiles_uni.cpp index d9ead682..38773001 100644 --- a/AnalyticNSIE/readfiles_uni.cpp +++ b/AnalyticNSIE/readfiles_uni.cpp @@ -18,7 +18,7 @@ using namespace std; */ LensHaloUniform::LensHaloUniform(InputParams& params, const COSMOLOGY& cosmo, bool verbose) -: LensHalo() +: LensHalo(params) { assignParams(params); @@ -173,9 +173,6 @@ void LensHalo::assignParams_stars(InputParams& params){ void LensHaloUniform::assignParams(InputParams& params){ //if(perturb_Nmodes > 0){ - PosType tmp; - if(!params.get("main_zlens",tmp)) error_message1("main_zlens",params.filename()); - LensHalo::setZlens(tmp); if(!params.get("kappa_uniform",kappa_uniform)) error_message1("kappa_uniform",params.filename()); if(!params.get("gamma_uniform_1",gamma_uniform[0])) error_message1("gamma_uniform_1",params.filename()); if(!params.get("gamma_uniform_2",gamma_uniform[1])) error_message1("gamma_uniform_2",params.filename()); diff --git a/MultiPlane/lens.cpp b/MultiPlane/lens.cpp index ffffbbb3..89ea5785 100644 --- a/MultiPlane/lens.cpp +++ b/MultiPlane/lens.cpp @@ -1452,6 +1452,7 @@ void Lens::createMainHalos(InputParams& params) main_halos.push_back(new LensHaloJaffe(params)); break; } + } for(std::size_t i = 0; i < main_halos.size(); ++i){ diff --git a/MultiPlane/lens_halos.cpp b/MultiPlane/lens_halos.cpp index f6326166..4509b515 100644 --- a/MultiPlane/lens_halos.cpp +++ b/MultiPlane/lens_halos.cpp @@ -62,7 +62,7 @@ void LensHalo::error_message1(std::string parameter,std::string file){ void LensHalo::assignParams(InputParams& params,bool needRsize){ if(!params.get("main_mass",mass)) error_message1("main_mass",params.filename()); - if(needRsize){ + if(needRsize){ // this might not be required for lenses like NSIE if(!params.get("main_Rsize",Rsize)) error_message1("main_Rsize",params.filename()); } if(!params.get("main_zlens",zlens)) error_message1("main_zlens",params.filename()); @@ -732,7 +732,6 @@ LensHaloRealNSIE::LensHaloRealNSIE( LensHaloRealNSIE::LensHaloRealNSIE(InputParams& params):LensHalo(params,false){ sigma = 0.; - LensHalo::setZlens(0.0); fratio = 0.; pa = 0.; rcore = 0.; diff --git a/MultiPlane/particle_lens.cpp b/MultiPlane/particle_lens.cpp index 82b3782d..2a54fdb0 100644 --- a/MultiPlane/particle_lens.cpp +++ b/MultiPlane/particle_lens.cpp @@ -23,7 +23,6 @@ LensHaloParticles::LensHaloParticles( LensHalo::setZlens(redshift); LensHalo::setCosmology(cosmo); - LensHalo::set_Rsize(1.0e3); LensHalo::set_flag_elliptical(false); stars_N = 0; diff --git a/include/lens_halos.h b/include/lens_halos.h index ee62fdfd..b7bcfee2 100644 --- a/include/lens_halos.h +++ b/include/lens_halos.h @@ -99,7 +99,7 @@ class LensHalo{ virtual void initFromMassFunc(float my_mass, float my_Rsize, float my_rscale, PosType my_slope, long *seed); /// set Rsize (in Mpc) and reset Rmax - virtual void set_Rsize(float my_Rsize){Rmax = Rmax*my_Rsize/Rsize; Rsize = my_Rsize; xmax = Rsize/rscale;}; + virtual void set_RsizeRmax(float my_Rsize){Rmax = Rmax*my_Rsize/Rsize; Rsize = my_Rsize; xmax = Rsize/rscale;}; /// set mass (in solar masses) void set_mass(float my_mass){mass=my_mass;}; /// set scale radius (in Mpc) @@ -713,10 +713,11 @@ class LensHaloNFW: public LensHalo{ void initFromFile(float my_mass, long *seed, float vmax, float r_halfmass); void initFromMassFunc(float my_mass, float my_Rsize, float my_rscale, PosType my_slope, long *seed); - /// set Rsize - void set_Rsize(float my_Rsize){LensHalo::setRsize(my_Rsize); xmax = LensHalo::getRsize()/rscale; gmax = InterpolateFromTable(gtable,xmax);}; + /// set Rsize, xmax and gmax + void set_RsizeXmax(float my_Rsize){LensHalo::setRsize(my_Rsize); xmax = LensHalo::getRsize()/rscale; gmax = InterpolateFromTable(gtable,xmax);}; /// set scale radius - void set_rscale(float my_rscale){rscale=my_rscale; xmax = LensHalo::getRsize()/rscale; gmax = InterpolateFromTable(gtable,xmax);}; + /// set rscale, xmax and gmax + void set_rscaleXmax(float my_rscale){rscale=my_rscale; xmax = LensHalo::getRsize()/rscale; gmax = InterpolateFromTable(gtable,xmax);}; protected: /// table size @@ -1033,8 +1034,6 @@ class LensHaloHernquist: public LensHalo{ */ //void initFromFile(float my_mass, long *seed, float vmax, float r_halfmass); - /// set Rsize - //void set_Rsize(float my_Rsize){Rsize=my_Rsize; xmax = Rsize/rscale; gmax = InterpolateFromTable(gtable,xmax);}; /// set scale radius void set_rscale(float my_rscale){rscale=my_rscale; xmax = LensHalo::getRsize()/rscale; gmax = InterpolateFromTable(gtable,xmax);}; // friend struct Ialpha_func; @@ -1098,8 +1097,6 @@ class LensHaloJaffe: public LensHalo{ LensHaloJaffe(InputParams& params); virtual ~LensHaloJaffe(); - /// set Rsize - //void set_Rsize(float my_Rsize){Rsize = my_Rsize; xmax = Rsize/rscale; gmax = InterpolateFromTable(gtable,xmax);}; /// set scale radius void set_rscale(float my_rscale){rscale=my_rscale; xmax = LensHalo::getRsize()/rscale; gmax = InterpolateFromTable(gtable,xmax);}; From bac49d8e23ae698dbcf34edbb76de97450497049 Mon Sep 17 00:00:00 2001 From: = Date: Thu, 20 Oct 2016 16:42:28 +0800 Subject: [PATCH 014/227] Added lens to signature of Lens::replaceMainHalos() and Lens::insertMainHalos(). --- MultiPlane/lens.cpp | 40 ++++++++++++++++++++-------------------- include/lens.h | 5 +++-- 2 files changed, 23 insertions(+), 22 deletions(-) diff --git a/MultiPlane/lens.cpp b/MultiPlane/lens.cpp index 89ea5785..8a6fd2ad 100644 --- a/MultiPlane/lens.cpp +++ b/MultiPlane/lens.cpp @@ -1484,18 +1484,19 @@ void Lens::clearMainHalos(bool verbose) * * The angular position of the halo should be preserved, but the x coordinates may change */ -void Lens::insertMainHalo(LensHalo* halo,bool addplanes,bool verbose) + +void Lens::insertMainHalo(LensHalo* halo,PosType zlens, bool addplanes,bool verbose) { - halo->setCosmology(cosmo); - halo->setDist(cosmo); - main_halos.push_back(halo); + halo->setCosmology(cosmo); + halo->setZlensDist(zlens,cosmo); + main_halos.push_back(halo); - flag_switch_main_halo_on = true; - - if(addplanes) addMainHaloToPlane(halo); + flag_switch_main_halo_on = true; + + if(addplanes) addMainHaloToPlane(halo); else addMainHaloToNearestPlane(halo); - combinePlanes(verbose); + combinePlanes(verbose); } /** @@ -1533,20 +1534,19 @@ void Lens::insertMainHalos(LensHalo** halos, std::size_t Nhalos,bool addplanes, * Note that this does not delete the halos that were there. It just removes * them from the lens. */ -void Lens::replaceMainHalos(LensHalo* halo,bool verbose) +void Lens::replaceMainHalo(LensHalo* halo,PosType zlens, bool addplanes,bool verbose) { - main_halos.clear(); - - halo->setCosmology(cosmo); - halo->setDist(cosmo); + main_halos.clear(); - main_halos.push_back(halo); - - flag_switch_main_halo_on = true; - - Utilities::delete_container(main_planes); - createMainPlanes(); - combinePlanes(verbose); + halo->setCosmology(cosmo); + halo->setZlensDist(zlens,cosmo); + main_halos.push_back(halo); + + flag_switch_main_halo_on = true; + + Utilities::delete_container(main_planes); + createMainPlanes(); + combinePlanes(verbose); } /** diff --git a/include/lens.h b/include/lens.h index 5e877005..ae5324ed 100644 --- a/include/lens.h +++ b/include/lens.h @@ -133,13 +133,14 @@ class Lens /// inserts a single main lens halo and adds it to the existing ones - void insertMainHalo(LensHalo* halo,bool addplanes,bool verbose = false); + void insertMainHalo(LensHalo* halo,PosType zlens, bool addplanes,bool verbose); /// inserts a sequence of main lens halos and adds them to the existing ones void insertMainHalos(LensHalo** halos, std::size_t Nhalos,bool addplanes,bool verbose = false); /// replaces existing main halos with a single main halo - void replaceMainHalos(LensHalo* halo,bool verbose = false); + void replaceMainHalo(LensHalo* halo,PosType zlens, bool addplanes,bool verbose); + /// replaces existing main halos with a sequence of main halos void replaceMainHalos(LensHalo** halos, std::size_t Nhalos,bool verbose = false); From 0407aea812a6e604888ed8d3f3c8179b63b4849c Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Tue, 25 Oct 2016 16:37:24 +0100 Subject: [PATCH 015/227] created a new PixelMap::writePixelMapUniform() that keeps the original size and resolution automatically. --- MultiPlane/lens.cpp | 2 ++ TreeCode_link/gridmap.cpp | 16 ++++++++++++++++ include/gridmap.h | 2 ++ 3 files changed, 20 insertions(+) diff --git a/MultiPlane/lens.cpp b/MultiPlane/lens.cpp index d55a1bc6..e9273b5e 100644 --- a/MultiPlane/lens.cpp +++ b/MultiPlane/lens.cpp @@ -2347,6 +2347,8 @@ void Lens::readInputSimFileMultiDarkHalos(bool verbose) minmass = mass; } + if(field_halos.back()->get_Rmax() > R_max) R_max = field_halos.back()->get_Rmax(); + ++j; if(flag_field_gal_on){ diff --git a/TreeCode_link/gridmap.cpp b/TreeCode_link/gridmap.cpp index 43c48cbb..1459d1fd 100644 --- a/TreeCode_link/gridmap.cpp +++ b/TreeCode_link/gridmap.cpp @@ -124,6 +124,7 @@ void GridMap::ReInitializeGrid(LensHndl lens){ ClearSurfaceBrightnesses(); } +/// Output a PixelMap of the surface brightness with same res as the GridMap PixelMap GridMap::getPixelMap(int resf){ if(resf <=0){ @@ -150,6 +151,7 @@ PixelMap GridMap::getPixelMap(int resf){ return map; } +/// surface brightness map void GridMap::getPixelMap(PixelMap &map){ int resf = (Ngrid_init-1)/(map.getNx()-1); @@ -221,7 +223,21 @@ PixelMap GridMap::writePixelMapUniform( if(getNumberOfPoints() == 0 ) return PixelMap(); PixelMap map(center, Nx, Ny,x_range/(Nx-1)); + + map.Clean(); + + writePixelMapUniform(map,lensvar); + + return map; +} +PixelMap GridMap::writePixelMapUniform( + LensingVariable lensvar /// which quantity is to be displayed +){ + size_t Nx = Ngrid_init; + size_t Ny = Ngrid_init2; + + PixelMap map( center.x, Nx, Ny,x_range/(Nx-1) ); map.Clean(); writePixelMapUniform(map,lensvar); diff --git a/include/gridmap.h b/include/gridmap.h index 2f2c641f..957d12bb 100644 --- a/include/gridmap.h +++ b/include/gridmap.h @@ -46,6 +46,8 @@ struct GridMap{ double getResolution(){return x_range/(Ngrid_init-1);} PixelMap writePixelMapUniform(const PosType center[],size_t Nx,size_t Ny,LensingVariable lensvar); + /// make pixel map of lensing quantities at the resolution of the GridMap + PixelMap writePixelMapUniform(LensingVariable lensvar); void writePixelMapUniform(PixelMap &map,LensingVariable lensvar); void writeFitsUniform(const PosType center[],size_t Nx,size_t Ny,LensingVariable lensvar,std::string filename); From f5ebbf5ca4e5f9e8490622252d53d63dc3c20527 Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Wed, 2 Nov 2016 14:58:19 +0100 Subject: [PATCH 016/227] Made the RAY class. --- include/point.h | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/include/point.h b/include/point.h index 05e8baff..129a0fbb 100644 --- a/include/point.h +++ b/include/point.h @@ -198,6 +198,25 @@ struct Point: public Point_2d{ //Point &operator=(const Point &p); }; +/** \brief A point that is automatically linked to its source point eliminating the + need for using LinkToSourcePoint(). Useful when a limited number of Points are needed and they do not need to be in continuous memory. +*/ +struct RAY: public Point{ + RAY(){ + image = &source_point; + image->image = this; + } + ~RAY(){}; + + /// postions on image plane + PosType *pos_image(){return x;}; + /// postions on source plane + PosType *pos_source(){return image->x;} + +private: + Point source_point; +}; + std::ostream &operator<<(std::ostream &os, Point const &p); /// The box representing a branch of a binary tree structure. Used specifically in TreeStruct for organizing points in the grid. @@ -365,10 +384,8 @@ struct PointList{ private: Point *top; Point *bottom; - //Point *current; unsigned long Npoints; -private: // make a point uncopyable //PointList(const PointList &p); PointList &operator=(const PointList &p); From 835cddb3613221135c74eb65e778b7e655d59f10 Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Wed, 14 Dec 2016 18:13:16 +0100 Subject: [PATCH 017/227] Implemented a Quaternian class for simple and fast rotations in 3 dimensions. --- include/geometry.h | 168 ++++++++++++++++++++++++++++++++++++++++++++- include/point.h | 6 ++ 2 files changed, 173 insertions(+), 1 deletion(-) diff --git a/include/geometry.h b/include/geometry.h index 965db754..03b90c59 100644 --- a/include/geometry.h +++ b/include/geometry.h @@ -10,6 +10,7 @@ #define __GLAMER__geometry__ #include +#include #include "standard.h" #include "point.h" // @@ -42,13 +43,178 @@ namespace Utilities { void InverseOrthographicProjection(const SphericalPoint ¢ral,PosType const x[]); }; + + class Quaternian{ + public: + Quaternian(double s,double x,double y,double z){ + v[0] = s; + v[1] = x; + v[2] = y; + v[3] = z; + } + Quaternian(const Quaternian &q){ + v[0] = q.v[0]; + v[1] = q.v[1]; + v[2] = q.v[2]; + v[3] = q.v[3]; + } + Quaternian(Point_3d p){ + v[0] = 0; + v[1] = p[0]; + v[2] = p[1]; + v[3] = p[2]; + } + Quaternian(SphericalPoint sp){ + sp.sphericalTOcartisian(v+1); + v[0] = 0; + } + + Quaternian &operator=(const Quaternian &q){ + v[0] = q.v[0]; + v[1] = q.v[1]; + v[2] = q.v[2]; + v[3] = q.v[3]; + + return *this; + } + + Quaternian operator*(const Quaternian &q) const{ + return Quaternian( + v[0]*q.v[0] - v[1]*q.v[1] - v[2]*q.v[2] - v[3]*q.v[3], + v[0]*q.v[1] + v[1]*q.v[0] + v[2]*q.v[3] - v[3]*q.v[2], + v[0]*q.v[2] - v[1]*q.v[3] + v[2]*q.v[0] + v[3]*q.v[1], + v[0]*q.v[3] + v[1]*q.v[2] - v[2]*q.v[1] + v[3]*q.v[0]); + } + + /// returns the conjugate of the quaternian + Quaternian conj() const{ + return Quaternian(v[0],-v[1],-v[2],-v[3]); + } + /** \brief returns the reciprocal of the quaternian + + This is the quaternian whose product with the original quaternian is + the real number 1. + */ + Quaternian inverse() const{ + return this->conj()/(v[0]*v[0] + v[1]*v[1] + v[2]*v[2] + v[3]*v[3]); + } + + /// the norm of the quaternian + double norm() const{ + return sqrt(v[0]*v[0] + v[1]*v[1] + v[2]*v[2] + v[3]*v[3]); + } + + /// returns the quaternian rotated around the x-axis by theta in radians + Quaternian RotateX(double theta){ + Quaternian R = q_x_rotation(theta); + return R*(*this)*R.conj(); + } + /// returns the quaternian rotated around the y-axis by theta in radians + Quaternian RotateY(double theta){ + Quaternian R = q_y_rotation(theta); + return R*(*this)*R.conj(); + } + /// returns the quaternian rotated around the z-axis by theta in radians + Quaternian RotateZ(double theta){ + Quaternian R = q_z_rotation(theta); + return R*(*this)*R.conj(); + } + /** returns the quaternian rotated first around the z-axis by phi and then + around the z-axis by theta, this is more efficient than doing two rotation + sequentially. Usefull for recentering a field of points. + */ + Quaternian RotateYZ(double theta,double phi){ + Quaternian R = q_z_rotation(theta)*q_z_rotation(phi); + return R*(*this)*R.conj(); + } + + /** returns the quaternian rotated with the rotation quaternian R. + It is assumed that R has norm = 1, ie ||R|| = 1 */ + Quaternian Rotate(const Quaternian &R) const{ + return R*(*this)*R.conj(); + } + + Quaternian operator+(const Quaternian &q) const{ + Quaternian p = q; + + p.v[0] += v[0]; + p.v[1] += v[1]; + p.v[2] += v[2]; + p.v[3] += v[3]; + + return p; + } + Quaternian operator-(const Quaternian &q) const{ + Quaternian p = *this; + + p.v[0] -= q.v[0]; + p.v[1] -= q.v[1]; + p.v[2] -= q.v[2]; + p.v[3] -= q.v[3]; + + return p; + } + + /// division by a scaler + Quaternian operator/(double s) const{ + return Quaternian(v[0]/s,v[1]/s,v[2]/s,v[3]/s); + } + /// division by a scaler + + Quaternian operator*(double s) const{ + return Quaternian(v[0]*s,v[1]*s,v[2]*s,v[3]*s); + } + + /// cross (outer) product + Quaternian cross(const Quaternian &q) const{ + return (*this*q - this->conj()*q.conj()); + } + + /// scalar product of vector part of the quaternians + double scalor(const Quaternian &q) const{ + return v[1]*q.v[1] + v[2]*q.v[2] + v[3]*q.v[3]; + } + + /// returns the vector part of the quaternian as a Point_3d + Point_3d to_point_3d() const{ + return Point_3d(v[1],v[2],v[3]); + } + + /// returns the vector part of the quaternian as a SpericalPoint + SphericalPoint to_SpericalPoint() const{ + SphericalPoint sp; + sp.cartisianTOspherical(v+1); + return sp; + } + + + /// the components of the quaternian + double v[4]; + + /// returns the rotation quaternian for a ratation around the x-axis + static Quaternian q_x_rotation(double theta){ + return Quaternian(cos(theta/2),sin(theta/2),0,0); + } + /// returns the rotation quaternian for a ratation around the y-axis + static Quaternian q_y_rotation(double theta){ + return Quaternian(cos(theta/2),0,sin(theta/2),0); + } + /// returns the rotation quaternian for a ratation around the z-axis + static Quaternian q_z_rotation(double theta){ + return Quaternian(cos(theta/2),0,0,sin(theta/2)); + } + + }; + + /// 3 dimensional distance between points PosType Seporation(const SphericalPoint &p1,const SphericalPoint &p2); /// Angular seporation between points PosType AngleSeporation(const SphericalPoint &p1,const SphericalPoint &p2); /// Determine if line segments a1a2 and b1b2 intersect. Sharing an endpoint does not count as intersecting - bool intersect(const PosType a1[],const PosType a2[],const PosType b1[],const PosType b2[]); + bool intersect(const PosType a1[],const PosType a2[] + ,const PosType b1[],const PosType b2[]); /// returns the number of times a closed curve intersects itself int intersect(const std::vector &curve); diff --git a/include/point.h b/include/point.h index 129a0fbb..fee492dc 100644 --- a/include/point.h +++ b/include/point.h @@ -409,6 +409,11 @@ struct Point_3d{ Point_3d(){ x[0]=x[1]=x[2]=0.0; } + Point_3d(PosType xx,PosType yy,PosType zz){ + x[0]=xx; + x[1]=yy; + x[2]=zz; + } ~Point_3d(){}; Point_3d(const Point_3d &p){ @@ -416,6 +421,7 @@ struct Point_3d{ x[1]=p.x[1]; x[2]=p.x[2]; } + Point_3d & operator=(const Point_3d &p){ if(this == &p) return *this; x[0]=p.x[0]; From 18ac4c9ae426426426cc87ee3e9efa98a6ea7fc2 Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Mon, 19 Dec 2016 13:09:15 +0100 Subject: [PATCH 018/227] Improvements and corrections to Quaternion class. --- TreeCode_link/geometry.cpp | 4 + include/Kist.h | 2 + include/geometry.h | 170 +++++++++++++++++++++++++------------ include/point.h | 3 +- 4 files changed, 122 insertions(+), 57 deletions(-) diff --git a/TreeCode_link/geometry.cpp b/TreeCode_link/geometry.cpp index cef46636..b20fa7a7 100644 --- a/TreeCode_link/geometry.cpp +++ b/TreeCode_link/geometry.cpp @@ -181,3 +181,7 @@ int Utilities::Geometry::incurve(PosType x[],std::vector curve){ return number == 0 ? 0 : 1; } +std::ostream &operator<<(std::ostream &os, Utilities::Geometry::SphericalPoint const &p){ + return os << p.r << " " << p.theta << " " << p.phi; +} + diff --git a/include/Kist.h b/include/Kist.h index 3db53b3e..405c2f53 100644 --- a/include/Kist.h +++ b/include/Kist.h @@ -173,6 +173,8 @@ struct Kist{ unit = unit->prev; return *this; } + + Data * operator->(){return unit->data;} iterator operator++(int){ diff --git a/include/geometry.h b/include/geometry.h index 03b90c59..c2f0bdb0 100644 --- a/include/geometry.h +++ b/include/geometry.h @@ -42,34 +42,76 @@ namespace Utilities { void OrthographicProjection(const SphericalPoint ¢ral,PosType x[]) const; void InverseOrthographicProjection(const SphericalPoint ¢ral,PosType const x[]); }; + /** \brief Quaternion class that is especially useful for rotations. + + +
+     The Quaternion can be easily transformed between classes Point_3d and Utilities::Geomotry::SphericalPoint.
+     
+     Below is an example of a rotation of a vector initially defined as a Utilities::Geometry::SphericalPoint.
+     
+     
+     #include "geometry.h"
+     {
+     using Utilities::Geometry::SphericalPoint;
+     using Utilities::Geometry::Quaternion;
+     
+     SphericalPoint sp;
+     
+     sp.phi = -pi/2;
+     sp.theta = pi/3;
+     sp.r = 1.0;
+     
+     cout << sp.r << " " << sp.theta << " " << sp.phi << endl;
+     
+     // make a rotation Quaternion that will rotate the vector to the x-axis.
+     Quaternion R = Quaternion::q_y_rotation(-sp.theta)*Quaternion::q_z_rotation(-sp.phi);
+     
+     
+     // construct a Quaternion from a SphericalPoint
+     Quaternion q(sp);
+     
+     // apply the rotation
+     q = q.Rotate(R);
+     
+     // convert to a Point_3d
+     Point_3d p = q.to_point_3d();
+     
+     cout << q.to_point_3d() << endl;
+     
+     // convert back to a SphericalPoint
+     cout << q.to_SpericalPoint() << endl;
+     }
+     
+     <\pre>
+     */
     
-    
-    class Quaternian{
+    class Quaternion{
     public:
-      Quaternian(double s,double x,double y,double z){
+      Quaternion(double s,double x,double y,double z){
         v[0] = s;
         v[1] = x;
         v[2] = y;
         v[3] = z;
       }
-      Quaternian(const Quaternian &q){
+      Quaternion(const Quaternion &q){
         v[0] = q.v[0];
         v[1] = q.v[1];
         v[2] = q.v[2];
         v[3] = q.v[3];
       }
-      Quaternian(Point_3d p){
+      Quaternion(Point_3d p){
         v[0] = 0;
         v[1] = p[0];
         v[2] = p[1];
         v[3] = p[2];
       }
-      Quaternian(SphericalPoint sp){
+      Quaternion(SphericalPoint sp){
         sp.sphericalTOcartisian(v+1);
         v[0] = 0;
       }
       
-      Quaternian &operator=(const Quaternian &q){
+      Quaternion &operator=(const Quaternion &q){
         v[0] = q.v[0];
         v[1] = q.v[1];
         v[2] = q.v[2];
@@ -78,64 +120,79 @@ namespace Utilities {
         return *this;
       }
       
-      Quaternian operator*(const Quaternian &q) const{
-        return Quaternian(
+      Quaternion operator*(const Quaternion &q) const{
+        return Quaternion(
                           v[0]*q.v[0] - v[1]*q.v[1] - v[2]*q.v[2] - v[3]*q.v[3],
                           v[0]*q.v[1] + v[1]*q.v[0] + v[2]*q.v[3] - v[3]*q.v[2],
                           v[0]*q.v[2] - v[1]*q.v[3] + v[2]*q.v[0] + v[3]*q.v[1],
                           v[0]*q.v[3] + v[1]*q.v[2] - v[2]*q.v[1] + v[3]*q.v[0]);
       }
       
-      /// returns the conjugate of the quaternian
-      Quaternian conj() const{
-        return Quaternian(v[0],-v[1],-v[2],-v[3]);
+      /// returns the conjugate of the Quaternion
+      Quaternion conj() const{
+        return Quaternion(v[0],-v[1],-v[2],-v[3]);
       }
-      /** \brief returns the reciprocal of the quaternian
+      /** \brief returns the reciprocal of the Quaternion
       
-      This is the quaternian whose product with the original quaternian is 
+      This is the Quaternion whose product with the original Quaternion is 
        the real number 1.
       */
-      Quaternian inverse() const{
+      Quaternion inverse() const{
         return this->conj()/(v[0]*v[0] + v[1]*v[1] + v[2]*v[2] + v[3]*v[3]);
       }
       
-      /// the norm of the quaternian
+      /// the norm of the Quaternion
       double norm() const{
         return sqrt(v[0]*v[0] + v[1]*v[1] + v[2]*v[2] + v[3]*v[3]);
       }
       
-      /// returns the quaternian rotated around the x-axis by theta in radians
-      Quaternian RotateX(double theta){
-        Quaternian R = q_x_rotation(theta);
+      /// returns the Quaternion rotated around the x-axis by theta in radians
+      Quaternion RotateX(double theta){
+        Quaternion R = q_x_rotation(theta);
         return R*(*this)*R.conj();
       }
-      /// returns the quaternian rotated around the y-axis by theta in radians
-      Quaternian RotateY(double theta){
-        Quaternian R = q_y_rotation(theta);
+      /// returns the Quaternion rotated around the y-axis by theta in radians
+      Quaternion RotateY(double theta){
+        Quaternion R = q_y_rotation(theta);
         return R*(*this)*R.conj();
       }
-      /// returns the quaternian rotated around the z-axis by theta in radians
-      Quaternian RotateZ(double theta){
-        Quaternian R = q_z_rotation(theta);
+      /// returns the Quaternion rotated around the z-axis by theta in radians
+      Quaternion RotateZ(double theta){
+        Quaternion R = q_z_rotation(theta);
         return R*(*this)*R.conj();
       }
-      /** returns the quaternian rotated first around the z-axis by phi and then
+      /** returns the Quaternion rotated first around the z-axis by phi and then
       around the z-axis by theta, this is more efficient than doing two rotation 
        sequentially.  Usefull for recentering a field of points.
        */
-      Quaternian RotateYZ(double theta,double phi){
-        Quaternian R = q_z_rotation(theta)*q_z_rotation(phi);
+      Quaternion RotateYZ(double theta,double phi){
+        Quaternion R = q_z_rotation(theta)*q_z_rotation(phi);
         return R*(*this)*R.conj();
       }
 
-      /** returns the quaternian rotated with the rotation quaternian R.
+      /** returns the Quaternion rotated with the rotation Quaternion R.
        It is assumed that R has norm = 1, ie ||R|| = 1 */
-      Quaternian Rotate(const Quaternian &R) const{
+      Quaternion Rotate(const Quaternion &R) const{
         return R*(*this)*R.conj();
       }
       
-      Quaternian operator+(const Quaternian &q) const{
-        Quaternian p = q;
+      
+      /// rotate a Point_3d using a rotation Quaternion
+      static Point_3d Rotate(const Point_3d &p,const Quaternion &R){
+        Quaternion q(p);
+        q.Rotate(R);
+        return q.to_point_3d();
+      }
+
+      /// rotate a SpericalPoint using a rotation Quaternion
+      static SphericalPoint Rotate(const SphericalPoint &p,const Quaternion &R){
+        Quaternion q(p);
+        q.Rotate(R);
+        return q.to_SpericalPoint();
+      }
+      
+      Quaternion operator+(const Quaternion &q) const{
+        Quaternion p = q;
         
         p.v[0] += v[0];
         p.v[1] += v[1];
@@ -144,8 +201,8 @@ namespace Utilities {
         
         return p;
       }
-      Quaternian operator-(const Quaternian &q) const{
-        Quaternian p = *this;
+      Quaternion operator-(const Quaternion &q) const{
+        Quaternion p = *this;
         
         p.v[0] -= q.v[0];
         p.v[1] -= q.v[1];
@@ -156,31 +213,30 @@ namespace Utilities {
       }
       
       /// division by a scaler
-      Quaternian operator/(double s) const{
-       return Quaternian(v[0]/s,v[1]/s,v[2]/s,v[3]/s);
+      Quaternion operator/(double s) const{
+       return Quaternion(v[0]/s,v[1]/s,v[2]/s,v[3]/s);
       }
-      /// division by a scaler
-
-      Quaternian operator*(double s) const{
-        return Quaternian(v[0]*s,v[1]*s,v[2]*s,v[3]*s);
+      /// multiply by a scaler
+      Quaternion operator*(double s) const{
+        return Quaternion(v[0]*s,v[1]*s,v[2]*s,v[3]*s);
       }
       
       /// cross (outer) product
-      Quaternian cross(const Quaternian &q) const{
-        return (*this*q - this->conj()*q.conj());
+      Point_3d cross(const Quaternion &q) const{
+        return (*this*q).to_point_3d();
       }
 
-      /// scalar product of vector part of the quaternians
-      double scalor(const Quaternian &q) const{
+      /// scalar product of vector part of the Quaternions
+      double scalor(const Quaternion &q) const{
         return v[1]*q.v[1] + v[2]*q.v[2] + v[3]*q.v[3];
       }
       
-      /// returns the vector part of the quaternian as a Point_3d
+      /// returns the vector part of the Quaternion as a Point_3d
       Point_3d to_point_3d() const{
         return Point_3d(v[1],v[2],v[3]);
       }
 
-      /// returns the vector part of the quaternian as a SpericalPoint
+      /// returns the vector part of the Quaternion as a SpericalPoint
       SphericalPoint to_SpericalPoint() const{
         SphericalPoint sp;
         sp.cartisianTOspherical(v+1);
@@ -188,20 +244,20 @@ namespace Utilities {
       }
 
       
-      /// the components of the quaternian
+      /// the components of the Quaternion
       double v[4];
 
-      /// returns the rotation quaternian for a ratation around the x-axis
-      static Quaternian q_x_rotation(double theta){
-        return Quaternian(cos(theta/2),sin(theta/2),0,0);
+      /// returns the rotation Quaternion for a ratation around the x-axis
+      static Quaternion q_x_rotation(double theta){
+        return Quaternion(cos(theta/2),sin(theta/2),0,0);
       }
-      /// returns the rotation quaternian for a ratation around the y-axis
-      static Quaternian q_y_rotation(double theta){
-        return Quaternian(cos(theta/2),0,sin(theta/2),0);
+      /// returns the rotation Quaternion for a ratation around the y-axis
+      static Quaternion q_y_rotation(double theta){
+        return Quaternion(cos(theta/2),0,-sin(theta/2),0);
       }
-      /// returns the rotation quaternian for a ratation around the z-axis
-      static Quaternian q_z_rotation(double theta){
-        return Quaternian(cos(theta/2),0,0,sin(theta/2));
+      /// returns the rotation Quaternion for a ratation around the z-axis
+      static Quaternion q_z_rotation(double theta){
+        return Quaternion(cos(theta/2),0,0,sin(theta/2));
       }
       
     };
@@ -242,4 +298,6 @@ namespace Utilities {
   }  
 }
 
+std::ostream &operator<<(std::ostream &os, Utilities::Geometry::SphericalPoint const &p);
+
 #endif /* defined(__GLAMER__geometry__) */
diff --git a/include/point.h b/include/point.h
index fee492dc..bbfcea72 100644
--- a/include/point.h
+++ b/include/point.h
@@ -286,7 +286,8 @@ struct PointList{
       current = p;
     }
     
-    Point *operator*(){return current;}
+    Point * operator*(){return current;}
+    
     PointList::iterator &operator=(PointList::iterator &p){
       if(&p == this) return *this;
       current = p.current;

From 1bfb6eaf209dfad1cd6ef7bbf89c34228dc9b8c9 Mon Sep 17 00:00:00 2001
From: RB Metcalf 
Date: Mon, 19 Dec 2016 16:36:47 +0100
Subject: [PATCH 019/227] Change to std::ostream &operator<<(std::ostream &os,
 Utilities::Geometry::SphericalPoint const &p)

---
 TreeCode_link/geometry.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/TreeCode_link/geometry.cpp b/TreeCode_link/geometry.cpp
index b20fa7a7..d0afa2d0 100644
--- a/TreeCode_link/geometry.cpp
+++ b/TreeCode_link/geometry.cpp
@@ -182,6 +182,6 @@ int Utilities::Geometry::incurve(PosType x[],std::vector curve){
 }
 
 std::ostream &operator<<(std::ostream &os, Utilities::Geometry::SphericalPoint const &p){
-  return os << p.r << " " << p.theta << " " << p.phi;
+  return os << "r: " << p.r << " theta: " << p.theta << " phi: " << p.phi;
 }
 

From bc12e2c6a03ccd21b0d003f843b60e8f1db92be5 Mon Sep 17 00:00:00 2001
From: RB Metcalf 
Date: Tue, 7 Mar 2017 15:25:51 +0100
Subject: [PATCH 020/227] Catch exception in incurve().

---
 TreeCode_link/curve_routines.cpp | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/TreeCode_link/curve_routines.cpp b/TreeCode_link/curve_routines.cpp
index d69568b0..24eadcad 100644
--- a/TreeCode_link/curve_routines.cpp
+++ b/TreeCode_link/curve_routines.cpp
@@ -1854,8 +1854,11 @@ namespace Utilities{
     return number == 0 ? 0 : 1;
   }
   int incurve(PosType x[],std::vector curve){
+
+    if(curve.size() == 0) return 0;
+
     int number = 0;
-    size_t i;
+    size_t i;    
     
     // The reason this does not return the winding number is because horizontal
     //  sections of the curve can be overcounted if they are colinear with x

From 7db501cbef8f36aa1451d10b9814fc9e752f46f1 Mon Sep 17 00:00:00 2001
From: RB Metcalf 
Date: Tue, 2 May 2017 15:25:05 +0200
Subject: [PATCH 021/227] Bug fix for PixelMap constructor when reading in fits
 file.

---
 ImageProcessing/pixelize.cpp | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/ImageProcessing/pixelize.cpp b/ImageProcessing/pixelize.cpp
index 3185f78d..0cf481fc 100644
--- a/ImageProcessing/pixelize.cpp
+++ b/ImageProcessing/pixelize.cpp
@@ -201,6 +201,8 @@ PixelMap::PixelMap(
       }
     }
     resolution = fabs(my_res)*pi/180.;
+  }else{
+    resolution = my_res;
   }
   
   rangeX = resolution*Nx;

From 21afd1e367aac03eff24d03a2b04c773a02d6497 Mon Sep 17 00:00:00 2001
From: RB Metcalf 
Date: Fri, 12 May 2017 16:01:25 +0200
Subject: [PATCH 022/227] Added PixelMap::AddGridBrightness() so that the flux
 from an entire grid can be added to a pixel map without having to find
 images.

Clean up some things related to the PointList.
---
 ImageProcessing/pixelize.cpp       |  58 ++++++++-
 TreeCode_link/KistDriver.cpp       | 116 ++++++++---------
 TreeCode_link/List/list.cpp        |  99 ++++++++-------
 TreeCode_link/Tree.cpp             |  25 ++--
 TreeCode_link/TreeDriver.cpp       |  70 +++++-----
 TreeCode_link/curve_routines.cpp   |  35 ++---
 TreeCode_link/find_crit.cpp        |  75 +++++------
 TreeCode_link/grid_maintenance.cpp | 197 +++++++++++++++--------------
 TreeCode_link/peak_refinement.cpp  |   3 +-
 TreeCode_link/tree_maintenance.cpp |   4 +-
 include/Tree.h                     |   4 +-
 include/image_processing.h         |   1 +
 include/lens.h                     |   4 +-
 include/point.h                    |  45 ++++---
 14 files changed, 411 insertions(+), 325 deletions(-)

diff --git a/ImageProcessing/pixelize.cpp b/ImageProcessing/pixelize.cpp
index 0cf481fc..bdd1c9ab 100644
--- a/ImageProcessing/pixelize.cpp
+++ b/ImageProcessing/pixelize.cpp
@@ -500,6 +500,43 @@ void PixelMap::AddImages(
   
   return;
 }
+/** \brief Add an image to the map
+ *
+ */
+void PixelMap::AddGridBrightness(Grid &grid){
+  
+  
+  
+  PointList *plist = grid.i_tree->pointlist;
+
+  if(plist->size() == 0) return;
+  
+  PointList::iterator listit= plist->Top();// = plist->begin();
+  
+  PosType sb = 1;
+  float area = 1;
+  
+  std::list  neighborlist;
+  std::list::iterator it;
+  //for(long ii=0;iibegin() ; listit != plist->end() ; ++listit ){
+    sb = (*listit)->surface_brightness;
+  
+    if (sb != 0.0 && (inMapBox((*listit)->leaf)) == true){
+      PointsWithinLeaf((*listit)->leaf,neighborlist);
+      for(it = neighborlist.begin();it != neighborlist.end();it++){
+        area = LeafPixelArea(*it,(*listit)->leaf);
+        map[*it] += sb*area;
+      }
+    }
+  }while(--listit);
+
+  for(size_t i=0; i< Nx*Ny ;++i) map[i] /= resolution*resolution;
+
+  return;
+}
 
 void PixelMap::AddImages(
                          std::vector &imageinfo   /// An array of ImageInfo-s.  There is no reason to separate images for this routine
@@ -1178,13 +1215,17 @@ void PixelMap::AddCurve(std::vector &curve,double value){
 
 
 /**
- *  \brief Fills in pixels where the image plane points in the grid are located with the value given
+ *  \brief Fills in pixels where the image plane points in the grid are located with the value given.
+ 
+ This is for lensing quantities and not surface brightness.  If you want surface brightness use PixelMap::AddGridBrightness()
  */
 void PixelMap::AddGrid(const Grid &grid,PosType value){
+  if(grid.getNumberOfPoints() == 0) return;
+
   PointList* list = grid.i_tree->pointlist;
   size_t index;
   
-  PointList::iterator list_current(list->Top());
+  PointList::iterator list_current = list->Top();
   do{
     if(inMapBox((*list_current)->x)){
       index = find_index((*list_current)->x);
@@ -1199,6 +1240,8 @@ void PixelMap::AddGrid(const Grid &grid,PosType value){
  *  The grid and PixelMap do not need to be related in any way.
  *  Using this function multiple grids can be added to the same image.
  *
+ * This is for lensing quantities and not surface brightness.  If you want surface brightness use PixelMap::AddGridBrightness()
+
  *
  *  Warning: When adding a new grid it should not overlap with any of the previously added grids.
  */
@@ -1227,7 +1270,7 @@ void PixelMap::AddGrid(const Grid &grid,LensingVariable val){
       
       lists[i].setTop((*treeit)->points);
       lists[i].setN((*treeit)->npoints);
-      list_current = lists[i].Top();
+      list_current.current = lists[i].Top();
       list_current.JumpDownList( (*treeit)->npoints -1);
       lists[i].setBottom(*list_current);
 
@@ -1253,8 +1296,10 @@ void PixelMap::AddGrid_(const PointList &list,LensingVariable val){
   double tmp,area;
   PosType tmp2[2];
   
-  PointList::iterator pl_it(list.Top());
-  for(size_t i = 0; i< list.size(); ++i){
+  PointList::iterator pl_it = list.Top();
+  do{
+  //for(PointList::iterator pl_it = list.begin() ; pl_it != list.end() ; ++pl_it){
+  //for(size_t i = 0; i< list.size(); ++i){
     
     switch (val) {
       case ALPHA:
@@ -1310,9 +1355,8 @@ void PixelMap::AddGrid_(const PointList &list,LensingVariable val){
         }
       }
     }
-    --pl_it;
     
-  }
+  }while(--pl_it);
 }
 
 
diff --git a/TreeCode_link/KistDriver.cpp b/TreeCode_link/KistDriver.cpp
index 57deaa5a..f78e9362 100644
--- a/TreeCode_link/KistDriver.cpp
+++ b/TreeCode_link/KistDriver.cpp
@@ -12,10 +12,10 @@
 bool TreeStruct::Test(){
   
   std::cout << "Running Test on tree ..." << std::endl;
-  PointList::iterator pl_current(*pointlist);
+  PointList::iterator pl_current;
   TreeStruct::iterator treeit(top);
 
-  pl_current = (*treeit)->points;
+  pl_current.current = (*treeit)->points;
   for(int k = 0; k < (*treeit)->npoints ; ++k,--pl_current){
     //assert( !std::isnan((*pl_current)->invmag) );
     assert( (*pl_current)->invmag == (*pl_current)->invmag );
@@ -25,7 +25,7 @@ bool TreeStruct::Test(){
   do{
     
     // check that every points is actually in the branch it should be in
-      pl_current = (*treeit)->points;
+      pl_current.current = (*treeit)->points;
       for(int k = 0; k < (*treeit)->npoints ; ++k,--pl_current){
         assert( inbox((*pl_current)->x,(*treeit)->boundary_p1,(*treeit)->boundary_p2) );
         //assert( !std::isnan((*pl_current)->invmag) );
@@ -276,7 +276,7 @@ void TreeStruct::_PointsWithinKist(TreeStruct::iterator ¤t,PosType *rmax,K
   int i,j,incell2=1;
   PosType radius;
   short pass;
-  PointList::iterator pointlist_current;
+  PointList::iterator pointlist_it;
   
   if((*current)->npoints == 0) return;
 
@@ -301,42 +301,42 @@ void TreeStruct::_PointsWithinKist(TreeStruct::iterator ¤t,PosType *rmax,K
     	  globs.ray[0] = globs.realray[0];
     	  globs.ray[1] = globs.realray[1];
 
-    	  if((*current)->points != NULL) pointlist_current=(*current)->points;
+    	  if((*current)->points != NULL) pointlist_it.current = (*current)->points;
 
     	  if( current.atLeaf() ){
     	   	  // if leaf calculate the distance to all the points in cell
     		  for(i=0;i<(*current)->npoints;++i){
-    			  for(j=0,radius=0.0;j<2;++j) radius+=pow((*pointlist_current)->x[j] - globs.ray[j],2);
+    			  for(j=0,radius=0.0;j<2;++j) radius+=pow((*pointlist_it)->x[j] - globs.ray[j],2);
     			  if( radius < *rmax**rmax ){
        				  if(markpoints == 1){
-       					  (*pointlist_current)->in_image = YES;
-      					  (*pointlist_current)->image->in_image = YES;
+       					  (*pointlist_it)->in_image = YES;
+      					  (*pointlist_it)->image->in_image = YES;
       				  }else if(markpoints == -1){
-      					  (*pointlist_current)->in_image=NO;
-                  (*pointlist_current)->image->in_image=NO;
-                  (*pointlist_current)->surface_brightness = (*pointlist_current)->image->surface_brightness = 0.0;
+      					  (*pointlist_it)->in_image=NO;
+                  (*pointlist_it)->image->in_image=NO;
+                  (*pointlist_it)->surface_brightness = (*pointlist_it)->image->surface_brightness = 0.0;
        				  }else if(markpoints == 0){
-         				  neighborkist->InsertAfterCurrent(*pointlist_current);
+         				  neighborkist->InsertAfterCurrent(*pointlist_it);
       				  }
-       				  if(*maxgridsize < (*pointlist_current)->gridsize) *maxgridsize = (*pointlist_current)->gridsize;
+       				  if(*maxgridsize < (*pointlist_it)->gridsize) *maxgridsize = (*pointlist_it)->gridsize;
     			  }
-            --pointlist_current;
+            --pointlist_it;
     		  }
     	  }else{ // put all of points in box into getCurrentKist(imagekist)
        		  for(i=0;i<(*current)->npoints;++i){
        			  if(markpoints == 1){
-       				  (*pointlist_current)->in_image=YES;
-       				  (*pointlist_current)->image->in_image=YES;
+       				  (*pointlist_it)->in_image=YES;
+       				  (*pointlist_it)->image->in_image=YES;
        			  }else if(markpoints == -1){
-       				  (*pointlist_current)->in_image=NO;
-       				  (*pointlist_current)->image->in_image=NO;
-                (*pointlist_current)->surface_brightness = (*pointlist_current)->image->surface_brightness = 0.0;
+       				  (*pointlist_it)->in_image=NO;
+       				  (*pointlist_it)->image->in_image=NO;
+                (*pointlist_it)->surface_brightness = (*pointlist_it)->image->surface_brightness = 0.0;
       			  }else if(markpoints == 0){
-       				  neighborkist->InsertAfterCurrent(*pointlist_current);
+       				  neighborkist->InsertAfterCurrent(*pointlist_it);
  				  }
-  				  if(*maxgridsize < (*pointlist_current)->gridsize) *maxgridsize = (*pointlist_current)->gridsize;
+  				  if(*maxgridsize < (*pointlist_it)->gridsize) *maxgridsize = (*pointlist_it)->gridsize;
 
-              --pointlist_current;
+              --pointlist_it;
        		  }
     	  }
 
@@ -373,50 +373,50 @@ void TreeStruct::_PointsWithinKist(TreeStruct::iterator ¤t,PosType *rmax,K
 	  // does radius cut into the box
 	  if( pass ){
 
-      if((*current)->points != NULL) pointlist_current = (*current)->points;
+      if((*current)->points != NULL) pointlist_it.current = (*current)->points;
 
 		  if( current.atLeaf()  ){  /* leaf case */
 
 			  for(i=0;i<(*current)->npoints;++i){
 
-				  for(j=0,radius=0.0;j<2;++j) radius+=pow((*pointlist_current)->x[j] - globs.ray[j],2);
+				  for(j=0,radius=0.0;j<2;++j) radius+=pow((*pointlist_it)->x[j] - globs.ray[j],2);
 				  if( radius < *rmax**rmax ){
 					  if(markpoints==1){
-						  (*pointlist_current)->in_image=YES;
-						  (*pointlist_current)->image->in_image=YES;
+						  (*pointlist_it)->in_image=YES;
+						  (*pointlist_it)->image->in_image=YES;
 					  }else if(markpoints==-1){
-						  (*pointlist_current)->in_image=NO;
-						  (*pointlist_current)->image->in_image=NO;
-     					  (*pointlist_current)->surface_brightness = (*pointlist_current)->image->surface_brightness = 0.0;
+						  (*pointlist_it)->in_image=NO;
+						  (*pointlist_it)->image->in_image=NO;
+     					  (*pointlist_it)->surface_brightness = (*pointlist_it)->image->surface_brightness = 0.0;
 					  }else if(markpoints==0){
-						  neighborkist->InsertAfterCurrent((*pointlist_current));
+						  neighborkist->InsertAfterCurrent((*pointlist_it));
      				  }
-      				  if(*maxgridsize < (*pointlist_current)->gridsize) *maxgridsize = (*pointlist_current)->gridsize;
+      				  if(*maxgridsize < (*pointlist_it)->gridsize) *maxgridsize = (*pointlist_it)->gridsize;
 
 				  }
-          --pointlist_current;
+          --pointlist_it;
 			  }
 		  }else if(pass==1){ // whole box is inside radius
 
-			  pointlist_current = (*current)->points;
+			  pointlist_it.current = (*current)->points;
 			  for(i=0;i<(*current)->npoints;++i){
 
-				  //assert( inbox((*pointlist_current)->x,(*current)->boundary_p1,(*current)->boundary_p2) );
-				  //assert( *rmax**rmax >= (pow((*pointlist_current)->x[0] - ray[0],2) + pow((*pointlist_current)->x[1] - ray[1],2) ));
+				  //assert( inbox((*pl_it)->x,(*current)->boundary_p1,(*current)->boundary_p2) );
+				  //assert( *rmax**rmax >= (pow((*pl_it)->x[0] - ray[0],2) + pow((*pl_it)->x[1] - ray[1],2) ));
 
 				  if(markpoints==1){
-   					  (*pointlist_current)->in_image=YES;
-  					  (*pointlist_current)->image->in_image=YES;
+   					  (*pointlist_it)->in_image=YES;
+  					  (*pointlist_it)->image->in_image=YES;
   				  }else if(markpoints==-1){
-  					  (*pointlist_current)->in_image=NO;
- 					  (*pointlist_current)->image->in_image=NO;
- 					  (*pointlist_current)->surface_brightness = (*pointlist_current)->image->surface_brightness = 0.0;
+  					  (*pointlist_it)->in_image=NO;
+ 					  (*pointlist_it)->image->in_image=NO;
+ 					  (*pointlist_it)->surface_brightness = (*pointlist_it)->image->surface_brightness = 0.0;
    				  }else if(markpoints==0){
-   					  neighborkist->InsertAfterCurrent((*pointlist_current));
+   					  neighborkist->InsertAfterCurrent((*pointlist_it));
   				  }
-  				  if(*maxgridsize < (*pointlist_current)->gridsize) *maxgridsize = (*pointlist_current)->gridsize;
+  				  if(*maxgridsize < (*pointlist_it)->gridsize) *maxgridsize = (*pointlist_it)->gridsize;
 
-          --pointlist_current;
+          --pointlist_it;
 			  }
 		  }else{
 			  if((*current)->child1 !=NULL){
@@ -449,7 +449,7 @@ void TreeStruct::PointsWithinKist_iter(const PosType* center,float rmin,float rm
 	unsigned long i;
 
   TreeStruct::iterator current(top);
-  PointList::iterator pointlist_current;
+  PointList::iterator pl_it;
   
 	neighborkist->Empty();
 
@@ -475,10 +475,10 @@ void TreeStruct::PointsWithinKist_iter(const PosType* center,float rmin,float rm
 			){
 
 				decend = false;
-				if((*current)->points != NULL) pointlist_current = (*current)->points;
+				if((*current)->points != NULL) pl_it.current = (*current)->points;
 				for(i=0;i<(*current)->npoints;++i){
-					neighborkist->InsertAfterCurrent((*pointlist_current));
-          --pointlist_current;
+					neighborkist->InsertAfterCurrent((*pl_it));
+          --pl_it;
 				}
 
 			}else if(Utilities::cutbox(center,(*current)->boundary_p1,(*current)->boundary_p2,rmax) == 0  // box is all outside outer circle
@@ -488,11 +488,11 @@ void TreeStruct::PointsWithinKist_iter(const PosType* center,float rmin,float rm
 
 			}else if(current.atLeaf()){      // box is a leaf that intersects the circle
 
-				if((*current)->points != NULL) pointlist_current = (*current)->points;
+				if((*current)->points != NULL) pl_it.current = (*current)->points;
 				for(i=0;i<(*current)->npoints;++i){
-					if(rmax*rmax >= pow((*pointlist_current)->x[0] - center[0],2) + pow((*pointlist_current)->x[1] - center[1],2) )
-						neighborkist->InsertAfterCurrent((*pointlist_current));
-          --pointlist_current;
+					if(rmax*rmax >= pow((*pl_it)->x[0] - center[0],2) + pow((*pl_it)->x[1] - center[1],2) )
+						neighborkist->InsertAfterCurrent((*pl_it));
+          --pl_it;
 				}
 			}
 
@@ -509,16 +509,16 @@ void TreeStruct::PointsWithinKist_iter(const PosType* center,float rmin,float rm
 			if(current.atLeaf()){      // box is a leaf that intersects the circle
 
 				if((*current)->points != NULL){
-					pointlist_current = (*current)->points;
+					pl_it.current = (*current)->points;
 				}
 
 				for(i=0;i<(*current)->npoints;++i){
 
-					r2 = pow((*pointlist_current)->x[0] - center[0],2) + pow((*pointlist_current)->x[1] - center[1],2);
+					r2 = pow((*pl_it)->x[0] - center[0],2) + pow((*pl_it)->x[1] - center[1],2);
 					if(rmax*rmax >= r2 && rmin*rmin <= r2){
-						neighborkist->InsertAfterCurrent((*pointlist_current));
+						neighborkist->InsertAfterCurrent((*pl_it));
 					}
-          --pointlist_current;
+          --pl_it;
 				}
 
 			}else if(BoxInCircle(center,rmax,(*current)->boundary_p1,(*current)->boundary_p2)  // box is all inside outer circle
@@ -527,12 +527,12 @@ void TreeStruct::PointsWithinKist_iter(const PosType* center,float rmin,float rm
 
 				decend = false;
 				if((*current)->points != NULL){
-					pointlist_current = (*current)->points;
+					pl_it.current = (*current)->points;
 				}
 
 				for(i=0;i<(*current)->npoints;++i){
-					neighborkist->InsertAfterCurrent((*pointlist_current));
-          --pointlist_current;
+					neighborkist->InsertAfterCurrent((*pl_it));
+          --pl_it;
 				}
 
 			}else if(Utilities::cutbox(center,(*current)->boundary_p1,(*current)->boundary_p2,rmax) == 0  // box is all outside outer circle
diff --git a/TreeCode_link/List/list.cpp b/TreeCode_link/List/list.cpp
index 8b517523..ead4bebd 100644
--- a/TreeCode_link/List/list.cpp
+++ b/TreeCode_link/List/list.cpp
@@ -22,7 +22,7 @@ void PointList::InsertAfterCurrent(iterator ¤t,PosType *x,unsigned long id
     return;
 }
 
-void PointList::InsertPointAfterCurrent(iterator ¤t,Point *point){
+void PointList::InsertPointAfterCurrent(PointList::iterator &it,Point *point){
   // leaves current unchanged
   // changes only list and links in point
   
@@ -30,18 +30,18 @@ void PointList::InsertPointAfterCurrent(iterator ¤t,Point *point){
     assert(top);
     assert(bottom);
     
-    point->prev=*current;
-    point->next=(*current)->next;
+    point->prev=*it;
+    point->next=(*it)->next;
     
-    if(*current == bottom) bottom=point;
-    else (*current)->next->prev=point;
-    (*current)->next=point;
+    if(*it == bottom) bottom=point;
+    else (*it)->next->prev=point;
+    (*it)->next=point;
   }else{  /* empty list case */
-    current=point;
+    it.current=point;
     top=point;
     bottom=point;
     point->prev = point->next = NULL;
-    current = point;
+    it.current = point;
   }
   Npoints++;
   return;
@@ -58,23 +58,23 @@ void PointList::InsertBeforeCurrent(iterator ¤t,PosType *x,unsigned long i
     return;
 }
 
-void PointList::InsertPointBeforeCurrent(iterator ¤t,Point *point){
+void PointList::InsertPointBeforeCurrent(iterator &it,Point *point){
   
   if(Npoints > 0){
     assert(top);
     assert(bottom);
     
-    point->prev = (*current)->prev;
-    point->next = *current;
-    if(*current == top) top=point;
-    else (*current)->prev->next=point;
-    (*current)->prev=point;
+    point->prev = (*it)->prev;
+    point->next = *it;
+    if(*it == top) top=point;
+    else (*it)->prev->next=point;
+    (*it)->prev=point;
   }else{  /* empty list case */
-    current=point;
+    it.current=point;
     top=point;
     bottom=point;
     point->prev = point->next = NULL;
-    current = point;
+    it.current = point;
   }
   
   Npoints++;
@@ -82,7 +82,7 @@ void PointList::InsertPointBeforeCurrent(iterator ¤t,Point *point){
   return;
 }
 
-void PointList::MoveCurrentToBottom(iterator ¤t){
+void PointList::MoveCurrentToBottom(iterator &it){
 
 	assert(top);
 	assert(bottom);
@@ -90,42 +90,42 @@ void PointList::MoveCurrentToBottom(iterator ¤t){
 	Point *point,*point_current;
 	// leaves current one above former current
 
-	point=TakeOutCurrent(current);
-	point_current=*current;
-  current = bottom;
-	InsertPointAfterCurrent(current,point);
-	current=point_current;
+	point=TakeOutCurrent(it);
+	point_current=*it;
+  it.current = bottom;
+	InsertPointAfterCurrent(it,point);
+	it.current=point_current;
 }
 
 /**
  *  takes out current point and set current to point previous */
 /* Except at top where current is set to new top */
 /* returns pointer to removed point */
-Point *PointList::TakeOutCurrent(iterator ¤t){
+Point *PointList::TakeOutCurrent(iterator &it){
 
     Point *point;
 
     if(Npoints <= 0) return NULL;
-    assert(*current);
+    assert(*it);
 
-    point = *current;
+    point = *it;
 
     if(top == bottom){  /* last point */
-      current=NULL;
+      it.current=NULL;
       top=NULL;
       bottom=NULL;
-    }else if(*current==top){
+    }else if(*it==top){
       top=top->next;
-      current = top;
+      it.current = top;
       top->prev=NULL;
-    } else if(*current == bottom){
+    } else if(*it == bottom){
       bottom = bottom->prev;
-      current = bottom;
+      it.current = bottom;
       bottom->next=NULL;
     }else{
-      (*current)->prev->next=(*current)->next;
-      (*current)->next->prev=(*current)->prev;
-      current = (*current)->prev;
+      (*it)->prev->next=(*it)->next;
+      (*it)->next->prev=(*it)->prev;
+      it.current = (*it)->prev;
     }
 
     Npoints--;
@@ -229,21 +229,22 @@ void PointList::EmptyList(){
 	Point **point;
 	unsigned long blocks=0,i=0,Nfreepoints=0;
 
-  iterator current(top);
+  iterator it;
+  it.current = top;
 
 	do{
-	  if((*current)->head > 0){ ++blocks; Nfreepoints += (*current)->head;}
-	}while(current--);
+	  if((*it)->head > 0){ ++blocks; Nfreepoints += (*it)->head;}
+	}while(it--);
 
 	assert(blocks <= Npoints);
 	assert(Nfreepoints == Npoints);
 
 	point=(Point **)malloc(blocks*sizeof(Point*));
 
-  current = top;
+  it.current = top;
 	do{
-	  if((*current)->head > 0){ point[i] = *current; ++i;}
-	}while(current--);
+	  if((*it)->head > 0){ point[i] = *it; ++i;}
+	}while(it--);
 
 	for(i=0;iNpoints);
   std::printf("%li\n",Npoints);
 
   for(i=0;iid,(*current)->x[0],(*current)->x[1]
-                                  ,(*current)->gridsize);
-    --current;
+    std::printf("%li  %li  %e %e %e\n",i,(*it)->id,(*it)->x[0],(*it)->x[1]
+                                  ,(*it)->gridsize);
+    --it;
   }
 
 }
diff --git a/TreeCode_link/Tree.cpp b/TreeCode_link/Tree.cpp
index a4ac84d9..97003e72 100644
--- a/TreeCode_link/Tree.cpp
+++ b/TreeCode_link/Tree.cpp
@@ -230,10 +230,10 @@ void TreeStruct::construct_root(
       //pointlist=NewList();
       pointlist= new PointList;
    //EmptyList(pointlist);
-      PointList::iterator pointlist_current;
+      PointList::iterator pl_it;
   for(i=0;iInsertPointAfterCurrent(pointlist_current,&xp[i]);
-    --pointlist_current;
+    pointlist->InsertPointAfterCurrent(pl_it,&xp[i]);
+    --pl_it;
   }
 
   top = new Branch(pointlist->Top(),npoints,boundary_p1,boundary_p2
@@ -450,10 +450,12 @@ void TreeStruct::insertChildToCurrent(Branch *current,Branch *branch,int child){
     }
 
     if(branch->npoints > 0){
-      PointList::iterator pointlist_current(branch->points);
+      PointList::iterator pl_it;
+      pl_it.current = branch->points;
+      
     	for(unsigned long i=0;inpoints;++i){
-    		(*pointlist_current)->leaf = branch;
-        --pointlist_current;
+    		(*pl_it)->leaf = branch;
+        --pl_it;
     	}
     }
 
@@ -523,11 +525,14 @@ void TreeStruct::printTree(TreeStruct::iterator ¤t){
   int i;
 
     printBranch(*current);
-  PointList::iterator pointlist_current((*current)->points);
+  PointList::iterator pl_it;
+  pl_it.current = ((*current)->points);
+  
+  
     for(i=0;i<(*current)->npoints;++i){
-      std::cout << (*pointlist_current)->id << " " << (*pointlist_current)->x[0] <<
-      " " << (*pointlist_current)->x[1] << std::endl;
-      --pointlist_current;
+      std::cout << (*pl_it)->id << " " << (*pl_it)->x[0] <<
+      " " << (*pl_it)->x[1] << std::endl;
+      --pl_it;
     }
     if((*current)->child1 == NULL) return;
 
diff --git a/TreeCode_link/TreeDriver.cpp b/TreeCode_link/TreeDriver.cpp
index fad7762e..6fee9349 100644
--- a/TreeCode_link/TreeDriver.cpp
+++ b/TreeCode_link/TreeDriver.cpp
@@ -126,7 +126,7 @@ void TreeStruct::_NearestNeighbor(TreeStruct::iterator ¤t,int Nneighbors,P
   int i,incell2=1;
   unsigned long index[Nneighbors+Nbucket];
   PosType dx,dy;
-  PointList::iterator pointlist_current;
+  PointList::iterator pl_it;
   
   //std::printf("**************************************\nlevel %i\n",(*current)->level);
   //for(i=0;i<(*current)->npoints;++i) std::printf("   %i\n",(*current)->points[i]);
@@ -145,11 +145,11 @@ void TreeStruct::_NearestNeighbor(TreeStruct::iterator ¤t,int Nneighbors,P
     		globs.ray[1] = globs.realray[1];
 
     		/* calculate the distance to all the points in cell */
-    		if((*current)->points != NULL ) pointlist_current = (*current)->points;
+    		if((*current)->points != NULL ) pl_it.current = (*current)->points;
     		for(i=0;i<(*current)->npoints;++i){
 
-    			dx = (*pointlist_current)->x[0] - globs.ray[0];
-    			dy = (*pointlist_current)->x[1] - globs.ray[1];
+    			dx = (*pl_it)->x[0] - globs.ray[0];
+    			dy = (*pl_it)->x[1] - globs.ray[1];
 
     			switch(*direction){
     			case 0: /* distance */
@@ -175,8 +175,8 @@ void TreeStruct::_NearestNeighbor(TreeStruct::iterator ¤t,int Nneighbors,P
 
      			index[i]=i;
     			//temp_points[i]=pointlist->current;
-          globs.tmp_point[i] = (*pointlist_current);
-          --pointlist_current;
+          globs.tmp_point[i] = (*pl_it);
+          --pl_it;
     		}
 
     		if((*current)->npoints > 0){
@@ -221,7 +221,7 @@ void TreeStruct::_NearestNeighbor(TreeStruct::iterator ¤t,int Nneighbors,P
 		  if( current.atLeaf() ){  /* leaf case */
 
 			  /* combine found neighbors with points in box and resort */
-			  if((*current)->points != NULL) pointlist_current=(*current)->points;
+			  if((*current)->points != NULL) pl_it.current=(*current)->points;
 
 			  for(i=0;inpoints+Nneighbors);++i){
 
-				  dx=(*pointlist_current)->x[0] - globs.ray[0];
-				  dy=(*pointlist_current)->x[1] - globs.ray[1];
+				  dx=(*pl_it)->x[0] - globs.ray[0];
+				  dy=(*pl_it)->x[1] - globs.ray[1];
 
 				  switch(*direction){
 				  case 0: /* distance */
@@ -257,8 +257,8 @@ void TreeStruct::_NearestNeighbor(TreeStruct::iterator ¤t,int Nneighbors,P
 				  }
 
 				  index[i]=i;
-				  globs.tmp_point[i] = *pointlist_current;
-          --pointlist_current;
+				  globs.tmp_point[i] = *pl_it;
+          --pl_it;
 			  }
 
 			  // sort the points found so far
@@ -482,7 +482,7 @@ Point *sortList(long n, PosType *arr,ListHndl list,Point *firstpointin){
   long i,j;
   PosType a;
   Point *point,*firstpoint;
-  PointList::iterator list_current;
+  PointList::iterator pl_it;
 
   if(n <= 1) return firstpointin;
 
@@ -491,21 +491,21 @@ Point *sortList(long n, PosType *arr,ListHndl list,Point *firstpointin){
   for(j=1;jTakeOutCurrent(list_current);
+    point=list->TakeOutCurrent(pl_it);
 
     i=j-1;
     while(i>-1 && arr[i] > a){
       arr[i+1]=arr[i];
       i--;
-      ++list_current;
+      ++pl_it;
     }
     arr[i+1]=a;
 
-    if( *list_current==list->Top() && i==-1) list->InsertPointBeforeCurrent(list_current,point);
-    else list->InsertPointAfterCurrent(list_current,point);
+    if( *pl_it==list->Top() && i==-1) list->InsertPointBeforeCurrent(pl_it,point);
+    else list->InsertPointAfterCurrent(pl_it,point);
 
     if(i == -1) firstpoint=point;
   }
@@ -830,23 +830,25 @@ void NeighborsOfNeighbors(ListHndl neighbors,ListHndl wholelist){
 
 	if(neighbors->size() < 1 || wholelist->size() < 1) return;
 
-  PointList::iterator wholelist_current(*wholelist);
-  PointList::iterator neighbors_current(*neighbors);
+  PointList::iterator wholelist_it;
+  wholelist_it.current = wholelist->Top();
+  PointList::iterator neighbors_it;
+  neighbors_it.current = neighbors->Top();
   
 	do{
-    wholelist_current = wholelist->Top();
+    wholelist_it.current = wholelist->Top();
 		do{
 			if(check==1){
-        ++wholelist_current;
+        ++wholelist_it;
 				check=0;
 			}
-			if(AreBoxNeighbors(*neighbors_current,*wholelist_current) ){
-				if( wholelist->Top() == *wholelist_current ) check=1;
-				point = wholelist->TakeOutCurrent(wholelist_current);
-				neighbors->InsertPointAfterCurrent(neighbors_current,point);
+			if(AreBoxNeighbors(*neighbors_it,*wholelist_it) ){
+				if( wholelist->Top() == *wholelist_it ) check=1;
+				point = wholelist->TakeOutCurrent(wholelist_it);
+				neighbors->InsertPointAfterCurrent(neighbors_it,point);
 			}
-		}while(--wholelist_current);
-	}while((--neighbors_current) && wholelist->size() > 0);
+		}while(--wholelist_it);
+	}while((--neighbors_it) && wholelist->size() > 0);
 
 	return ;
 }
@@ -1156,12 +1158,14 @@ void TransformPoints(ListHndl listout,ListHndl listin){
   unsigned long i;
 
   listout->EmptyList();
-  PointList::iterator listin_current(listin->Top());
-  PointList::iterator listout_current(listout->Top());
+  PointList::iterator listin_it;
+  listin_it.current = listin->Top();
+  PointList::iterator listout_it;
+  listout_it.current = listout->Top();
   for(i=0;isize();++i){
-    listout->InsertAfterCurrent(listout_current,(*listin_current)->image->x
-                                ,(*listin_current)->image->id,*listin_current);
-    --listin_current;
+    listout->InsertAfterCurrent(listout_it,(*listin_it)->image->x
+                                ,(*listin_it)->image->id,*listin_it);
+    --listin_it;
   }
   
 }
diff --git a/TreeCode_link/curve_routines.cpp b/TreeCode_link/curve_routines.cpp
index 24eadcad..e93212d5 100644
--- a/TreeCode_link/curve_routines.cpp
+++ b/TreeCode_link/curve_routines.cpp
@@ -1472,9 +1472,10 @@ void splitter(OldImageInfo *images,int Maximages,int *Nimages){
   
   NpointsTotal = images[0].Npoints;
   
-  PointList::iterator imagelist_current(imagelist->Bottom());
+  PointList::iterator imagelist_it;
+  imagelist_it.current = imagelist->Bottom();
   // copy image points into a list
-  for(i=0;iInsertPointAfterCurrent(imagelist_current,&(images[0].points[i]));
+  for(i=0;iInsertPointAfterCurrent(imagelist_it,&(images[0].points[i]));
   
   assert(imagelist->size() == images[0].Npoints);
   //std::printf("imagelist = %il\n",imagelist->Npoints);
@@ -1487,17 +1488,17 @@ void splitter(OldImageInfo *images,int Maximages,int *Nimages){
   point = images[0].points;
   newpointarray = NewPointArray(NpointsTotal);
   
-  imagelist_current = imagelist->Top();
+  imagelist_it.current = imagelist->Top();
   m=0;
   i=0;
   do{
-    if(i < *Nimages && images[i].points == *imagelist_current){
+    if(i < *Nimages && images[i].points == *imagelist_it){
       images[i].points=&(newpointarray[m]);
       ++i;
     }
-    PointCopyData(&(newpointarray[m]),*imagelist_current);
+    PointCopyData(&(newpointarray[m]),*imagelist_it);
     ++m;
-  }while(--imagelist_current);
+  }while(--imagelist_it);
   
   assert(m == NpointsTotal);
   
@@ -1537,14 +1538,16 @@ void splitlist(ListHndl imagelist,OldImageInfo *images,int *Nimages,int Maximage
   //std::printf("imagelist = %li\n",imagelist->Npoints);
   // divide images into disconnected curves using neighbors-of-neighbors
   
-  PointList::iterator imagelist_current(*imagelist);
-  PointList::iterator orderedlist_current;
+  PointList::iterator imagelist_it;
+  imagelist_it.current = imagelist->Top();
+  
+  PointList::iterator orderedlist_it;
   
   while(imagelist->size() > 0 && i < Maximages){
-    images[i].points = imagelist->TakeOutCurrent(imagelist_current);
-    orderedlist_current = orderedlist->Bottom();
-    orderedlist->InsertPointAfterCurrent(orderedlist_current,images[i].points);
-    --orderedlist_current;
+    images[i].points = imagelist->TakeOutCurrent(imagelist_it);
+    orderedlist_it.current = orderedlist->Bottom();
+    orderedlist->InsertPointAfterCurrent(orderedlist_it,images[i].points);
+    --orderedlist_it;
     NeighborsOfNeighbors(orderedlist,imagelist);
     images[i].Npoints = orderedlist->size() - m;
     m += images[i].Npoints;
@@ -1556,11 +1559,11 @@ void splitlist(ListHndl imagelist,OldImageInfo *images,int *Nimages,int Maximage
   if(i == Maximages && imagelist->size() > 0){
     Point *point;
     do{
-      point = imagelist->TakeOutCurrent(imagelist_current);
+      point = imagelist->TakeOutCurrent(imagelist_it);
       //MoveToBottomList(orderedlist);
-      orderedlist_current = orderedlist->Bottom();
-      orderedlist->InsertPointAfterCurrent(orderedlist_current,point);
-      --orderedlist_current;
+      orderedlist_it.current = orderedlist->Bottom();
+      orderedlist->InsertPointAfterCurrent(orderedlist_it,point);
+      --orderedlist_it;
       ++(images[Maximages-1].Npoints);
     }while(imagelist->size() > 0);
   }
diff --git a/TreeCode_link/find_crit.cpp b/TreeCode_link/find_crit.cpp
index bd26576c..f8e21411 100644
--- a/TreeCode_link/find_crit.cpp
+++ b/TreeCode_link/find_crit.cpp
@@ -57,20 +57,21 @@ void ImageFinding::find_crit(
   
   // find kist of points with 1/magnification less than invmag_min
   negimage[0].imagekist->Empty();
-  PointList::iterator i_tree_pointlist_current(grid->i_tree->pointlist->Top());
-  Point *minpoint = *i_tree_pointlist_current;
+  PointList::iterator i_tree_pointlist_it;
+  i_tree_pointlist_it.current = (grid->i_tree->pointlist->Top());
+  Point *minpoint = *i_tree_pointlist_it;
   
   for(i=0;ii_tree->pointlist->size();++i){
-    if((*i_tree_pointlist_current)->invmag < invmag_min){
-      negimage[0].imagekist->InsertAfterCurrent(*i_tree_pointlist_current);
+    if((*i_tree_pointlist_it)->invmag < invmag_min){
+      negimage[0].imagekist->InsertAfterCurrent(*i_tree_pointlist_it);
       negimage[0].imagekist->Down();
     }else{
-      (*i_tree_pointlist_current)->in_image = NO;
+      (*i_tree_pointlist_it)->in_image = NO;
     }
     
     // record point of maximum kappa
-    if((*i_tree_pointlist_current)->kappa > minpoint->kappa) minpoint = *i_tree_pointlist_current;
-    --i_tree_pointlist_current;
+    if((*i_tree_pointlist_it)->kappa > minpoint->kappa) minpoint = *i_tree_pointlist_it;
+    --i_tree_pointlist_it;
   }
   
   if(negimage[0].imagekist->Nunits() == 0){
@@ -1270,18 +1271,18 @@ CritType ImageFinding::find_pseudo(ImageInfo &pseudocurve,ImageInfo &negimage
  
  // find kist of points with 1/magnification less than invmag_min
  critcurve[0].imagekist->Empty();
- PointList::iterator i_tree_pointlist_current(grid->i_tree->pointlist->Top());
- Point *minpoint = *i_tree_pointlist_current;
+ PointList::iterator i_tree_pointlist_it(grid->i_tree->pointlist->Top());
+ Point *minpoint = *i_tree_pointlist_it;
  
  for(i=0;ii_tree->pointlist->size();++i){
- if((*i_tree_pointlist_current)->invmag < invmag_min){
- critcurve[0].imagekist->InsertAfterCurrent(*i_tree_pointlist_current);
+ if((*i_tree_pointlist_it)->invmag < invmag_min){
+ critcurve[0].imagekist->InsertAfterCurrent(*i_tree_pointlist_it);
  critcurve[0].imagekist->Down();
  }
  
  // record point of maximum kappa
- if((*i_tree_pointlist_current)->kappa > minpoint->kappa) minpoint = *i_tree_pointlist_current;
- --i_tree_pointlist_current;
+ if((*i_tree_pointlist_it)->kappa > minpoint->kappa) minpoint = *i_tree_pointlist_it;
+ --i_tree_pointlist_it;
  }
  bool maxpoint = false;
  
@@ -1585,16 +1586,17 @@ void ImageFinding::IF_routines::refine_crit_in_image(
   
   // find kist of points with negative magnification
   negimage.imagekist->Empty();
-  PointList::iterator i_tree_pointlist_current(grid->i_tree->pointlist->Top());
+  PointList::iterator i_tree_pointlist_it;
+  i_tree_pointlist_it.current = (grid->i_tree->pointlist->Top());
   for(i=0;ii_tree->pointlist->size();++i){
-    x[0] = (*i_tree_pointlist_current)->image->x[0] - x_source[0];
-    x[1] = (*i_tree_pointlist_current)->image->x[1] - x_source[1];
+    x[0] = (*i_tree_pointlist_it)->image->x[0] - x_source[0];
+    x[1] = (*i_tree_pointlist_it)->image->x[1] - x_source[1];
     
-    if( (*i_tree_pointlist_current)->invmag < 0 && r_source*r_source > (x[0]*x[0] + x[1]*x[1]) ){
-      negimage.imagekist->InsertAfterCurrent(*i_tree_pointlist_current);
+    if( (*i_tree_pointlist_it)->invmag < 0 && r_source*r_source > (x[0]*x[0] + x[1]*x[1]) ){
+      negimage.imagekist->InsertAfterCurrent(*i_tree_pointlist_it);
       negimage.imagekist->Down();
     }
-    --i_tree_pointlist_current;
+    --i_tree_pointlist_it;
   }
   
   if(negimage.imagekist->Nunits() == 0) return;
@@ -1637,8 +1639,8 @@ void ImageFinding::IF_routines::refine_crit_in_image(
     newpoint_kist.MoveToTop();
     negimage.imagekist->MoveToBottom();
     do{
-      x[0] = (*i_tree_pointlist_current)->image->x[0] - x_source[0];
-      x[1] = (*i_tree_pointlist_current)->image->x[1] - x_source[1];
+      x[0] = (*i_tree_pointlist_it)->image->x[0] - x_source[0];
+      x[1] = (*i_tree_pointlist_it)->image->x[1] - x_source[1];
       
       if(newpoint_kist.getCurrent()->image->invmag < 0 && r_source*r_source > (x[0]*x[0] + x[1]*x[1]) )
         negimage.imagekist->InsertAfterCurrent(newpoint_kist.getCurrent());
@@ -1692,19 +1694,19 @@ void ImageFinding::IF_routines::refine_crit_in_image(
  // find kist of points with negative magnification
  contour.imagekist->Empty();
  
- PointList::iterator i_tree_pointlist_current(grid->i_tree->pointlist->Top());
- Point *minpoint = *i_tree_pointlist_current;
+ PointList::iterator i_tree_pointlist_it(grid->i_tree->pointlist->Top());
+ Point *minpoint = *i_tree_pointlist_it;
  
  for(i=0;ii_tree->pointlist->size();++i){
- if ((*i_tree_pointlist_current)->kappa>isokappa){
- std::cout << (*i_tree_pointlist_current)->kappa << std::endl;
- contour.imagekist->InsertAfterCurrent(*i_tree_pointlist_current);
+ if ((*i_tree_pointlist_it)->kappa>isokappa){
+ std::cout << (*i_tree_pointlist_it)->kappa << std::endl;
+ contour.imagekist->InsertAfterCurrent(*i_tree_pointlist_it);
  contour.imagekist->Down();
  }
  
  // record point of maximum kappa
- if((*i_tree_pointlist_current)->kappa > minpoint->kappa) minpoint = *i_tree_pointlist_current;
- --i_tree_pointlist_current;
+ if((*i_tree_pointlist_it)->kappa > minpoint->kappa) minpoint = *i_tree_pointlist_it;
+ --i_tree_pointlist_it;
  }
  
  findborders4(grid->i_tree,&critcurve[ii])
@@ -1784,8 +1786,9 @@ void ImageFinding::find_contour(
   
   // find kist of points with negative magnification
   critcurve[0].imagekist->Empty();
-  PointList::iterator i_tree_pointlist_current(grid->i_tree->pointlist->Top());
-  Point *minpoint = *i_tree_pointlist_current;
+  PointList::iterator i_tree_pointlist_it;
+  i_tree_pointlist_it.current = (grid->i_tree->pointlist->Top());
+  Point *minpoint = *i_tree_pointlist_it;
   
   KappaType value,maxval=0;
   
@@ -1793,15 +1796,15 @@ void ImageFinding::find_contour(
     
     switch (contour_type) {
       case KAPPA:
-        value = (*i_tree_pointlist_current)->kappa;
+        value = (*i_tree_pointlist_it)->kappa;
         maxval = minpoint->kappa;
         break;
       case INVMAG:
-        value = (*i_tree_pointlist_current)->invmag;
+        value = (*i_tree_pointlist_it)->invmag;
         maxval = minpoint->invmag;
         break;
       case DT:
-        value = (*i_tree_pointlist_current)->dt;
+        value = (*i_tree_pointlist_it)->dt;
         maxval = minpoint->dt;
         break;
       default:
@@ -1809,13 +1812,13 @@ void ImageFinding::find_contour(
     }
     
     if(value > contour_value){
-      critcurve[0].imagekist->InsertAfterCurrent(*i_tree_pointlist_current);
+      critcurve[0].imagekist->InsertAfterCurrent(*i_tree_pointlist_it);
       critcurve[0].imagekist->Down();
     }
     
     // record point of maximum kappa
-    if(value > maxval) minpoint = *i_tree_pointlist_current;
-    --i_tree_pointlist_current;
+    if(value > maxval) minpoint = *i_tree_pointlist_it;
+    --i_tree_pointlist_it;
   }
   bool maxpoint = false;
   
diff --git a/TreeCode_link/grid_maintenance.cpp b/TreeCode_link/grid_maintenance.cpp
index c01d7335..e9f8fd80 100644
--- a/TreeCode_link/grid_maintenance.cpp
+++ b/TreeCode_link/grid_maintenance.cpp
@@ -249,10 +249,11 @@ void Grid::ReShoot(LensHndl lens){
   s_points = NewPointArray(i_tree->pointlist->size());
   
 	// build new initial grid
-  PointList::iterator i_tree_pointlist_current(i_tree->pointlist->Top());
+  PointList::iterator i_tree_pointlist_it;
+  i_tree_pointlist_it.current = i_tree->pointlist->Top();
   size_t k;
   for(i=0,k=0;ipointlist->size();++i){
-    i_points = *i_tree_pointlist_current;
+    i_points = *i_tree_pointlist_it;
     if(i_points->head > 0){
 
       // link source and image points
@@ -270,7 +271,7 @@ void Grid::ReShoot(LensHndl lens){
       }
     }
     
-    --i_tree_pointlist_current;
+    --i_tree_pointlist_it;
   }
   
   s_tree = new TreeStruct(s_points,s_points->head,1,(i_tree->getTop()->boundary_p2[0] - i_tree->getTop()->boundary_p1[0])/10 );
@@ -291,14 +292,15 @@ void Grid::ReShoot(LensHndl lens){
 PosType Grid::RefreshSurfaceBrightnesses(SourceHndl source){
 	PosType total=0,tmp;
   
-  PointList::iterator s_tree_pointlist_current(s_tree->pointlist->Top());
-	for(unsigned long i=0;ipointlist->size();++i,--s_tree_pointlist_current){
-		tmp = source->SurfaceBrightness((*s_tree_pointlist_current)->x);
-		(*s_tree_pointlist_current)->surface_brightness = (*s_tree_pointlist_current)->image->surface_brightness
+  PointList::iterator s_tree_pointlist_it;
+  s_tree_pointlist_it.current = (s_tree->pointlist->Top());
+	for(unsigned long i=0;ipointlist->size();++i,--s_tree_pointlist_it){
+		tmp = source->SurfaceBrightness((*s_tree_pointlist_it)->x);
+		(*s_tree_pointlist_it)->surface_brightness = (*s_tree_pointlist_it)->image->surface_brightness
     = tmp;
 		total += tmp;//*pow( s_tree->pointlist->current->gridsize,2);
-		assert((*s_tree_pointlist_current)->surface_brightness >= 0.0);
-		(*s_tree_pointlist_current)->in_image = (*s_tree_pointlist_current)->image->in_image
+		assert((*s_tree_pointlist_it)->surface_brightness >= 0.0);
+		(*s_tree_pointlist_it)->in_image = (*s_tree_pointlist_it)->image->in_image
     = NO;
 	}
   
@@ -310,11 +312,12 @@ PosType Grid::RefreshSurfaceBrightnesses(SourceHndl source){
 PosType Grid::ClearSurfaceBrightnesses(){
 	PosType total=0;
   
-  PointList::iterator s_tree_pointlist_current(s_tree->pointlist->Top());
-	for(unsigned long i=0;ipointlist->size();++i,--s_tree_pointlist_current){
-		(*s_tree_pointlist_current)->surface_brightness = (*s_tree_pointlist_current)->image->surface_brightness
+  PointList::iterator s_tree_pointlist_it;
+  s_tree_pointlist_it.current = (s_tree->pointlist->Top());
+	for(unsigned long i=0;ipointlist->size();++i,--s_tree_pointlist_it){
+		(*s_tree_pointlist_it)->surface_brightness = (*s_tree_pointlist_it)->image->surface_brightness
     = 0.0;
-		(*s_tree_pointlist_current)->in_image = (*s_tree_pointlist_current)->image->in_image
+		(*s_tree_pointlist_it)->in_image = (*s_tree_pointlist_it)->image->in_image
     = NO;
 	}
   
@@ -446,24 +449,24 @@ Point * Grid::RefineLeaf(LensHndl lens,Point *point){
 	// re-assign leaf of point that was to be refined
 	assert(inbox(point->x,i_tree->getTop()->boundary_p1,i_tree->getTop()->boundary_p2));
   
-  TreeStruct::iterator i_tree_current(i_tree);
-  i_tree_current = point->leaf;
-	assert(inbox(point->x,(*i_tree_current)->boundary_p1,(*i_tree_current)->boundary_p2));
+  TreeStruct::iterator i_tree_it(i_tree);
+  i_tree_it = point->leaf;
+	assert(inbox(point->x,(*i_tree_it)->boundary_p1,(*i_tree_it)->boundary_p2));
 	// This line should not be necessary!! It is repairing the leaf that has been assigned incorrectly somewhere
-	//if(!inbox(point->x,(*i_tree_current)->boundary_p1,(*i_tree_current)->boundary_p2) ) moveTop(i_tree);
-	i_tree->_FindLeaf(i_tree_current,point->x,0);
-	point->leaf = *i_tree_current;
+	//if(!inbox(point->x,(*i_tree_it)->boundary_p1,(*i_tree_it)->boundary_p2) ) moveTop(i_tree);
+	i_tree->_FindLeaf(i_tree_it,point->x,0);
+	point->leaf = *i_tree_it;
 
 	assert(inbox(point->image->x,s_tree->getTop()->boundary_p1,s_tree->getTop()->boundary_p2));
 	assert(inbox(point->image->x,s_tree->getTop()->boundary_p1,s_tree->getTop()->boundary_p2));
   
-  TreeStruct::iterator s_tree_current(s_tree);
-	s_tree_current = point->image->leaf;
-	assert(inbox(point->image->x,(*s_tree_current)->boundary_p1,(*s_tree_current)->boundary_p2));
+  TreeStruct::iterator s_tree_it(s_tree);
+	s_tree_it = point->image->leaf;
+	assert(inbox(point->image->x,(*s_tree_it)->boundary_p1,(*s_tree_it)->boundary_p2));
 	// This line should not be necessary!! It is repairing the leaf that has been assigned incorrectly somewhere
 	//if(!inbox(point->image->x,s_tree->current->boundary_p1,s_tree->current->boundary_p2) ) moveTop(s_tree);
-	s_tree->_FindLeaf(s_tree_current,point->image->x,0);
-	point->image->leaf = *s_tree_current;
+	s_tree->_FindLeaf(s_tree_it,point->image->x,0);
+	point->image->leaf = *s_tree_it;
 
 	return i_points;
 }
@@ -685,8 +688,8 @@ Point * Grid::RefineLeaves(LensHndl lens,std::vector& points){
     i_tree->AddPointsToTree(i_points,i_points->head);
     s_tree->AddPointsToTree(s_points,s_points->head);
   
-  TreeStruct::iterator i_tree_current(i_tree);
-  TreeStruct::iterator s_tree_current(s_tree);
+  TreeStruct::iterator i_tree_it(i_tree);
+  TreeStruct::iterator s_tree_it(s_tree);
 
     assert(s_points->head > 0);
 
@@ -697,10 +700,10 @@ Point * Grid::RefineLeaves(LensHndl lens,std::vector& points){
         //std::cout << ii << std::endl;
         //i_points[ii].print();
         //i_points[ii].leaf->print();
-        i_tree_current = i_points[ii].leaf;
-        i_tree->_FindLeaf(i_tree_current,i_points[ii].x,0);
-        assert((*i_tree_current)->npoints == 1);
-        i_points[ii].leaf = *i_tree_current;
+        i_tree_it = i_points[ii].leaf;
+        i_tree->_FindLeaf(i_tree_it,i_points[ii].x,0);
+        assert((*i_tree_it)->npoints == 1);
+        i_points[ii].leaf = *i_tree_it;
         assert(i_points[ii].prev != NULL || i_points[ii].next != NULL || s_points->head == 1);
         assert(i_points[ii].image->prev != NULL || i_points[ii].image->next != NULL || s_points->head == 1);
       }
@@ -709,10 +712,10 @@ Point * Grid::RefineLeaves(LensHndl lens,std::vector& points){
         //std::cout << ii << std::endl;
         //s_points[ii].print();
         //s_points[ii].leaf->print();
-        s_tree_current = s_points[ii].leaf;
-        s_tree->_FindLeaf(s_tree_current,s_points[ii].x,0);
-        assert((*s_tree_current)->npoints == 1);
-        s_points[ii].leaf = *s_tree_current;
+        s_tree_it = s_points[ii].leaf;
+        s_tree->_FindLeaf(s_tree_it,s_points[ii].x,0);
+        assert((*s_tree_it)->npoints == 1);
+        s_points[ii].leaf = *s_tree_it;
         assert(s_points[ii].prev != NULL || s_points[ii].next != NULL || s_points->head == 1);
         assert(s_points[ii].image->prev != NULL || s_points[ii].image->next != NULL || s_points->head == 1);
       }
@@ -721,27 +724,27 @@ Point * Grid::RefineLeaves(LensHndl lens,std::vector& points){
   for(ii=0;iileaf->child1 == NULL && points[ii]->leaf->child2 == NULL);
 		if(points[ii]->image->leaf->child1 != NULL || points[ii]->image->leaf->child2 != NULL){
-      s_tree_current = points[ii]->image->leaf;
-      s_tree_current.movetop();
+      s_tree_it = points[ii]->image->leaf;
+      s_tree_it.movetop();
       //assert(inbox(points[ii]->x,s_tree->getTop()->boundary_p1,s_tree->getTop()->boundary_p2));
-      assert(inbox(points[ii]->x,(*s_tree_current)->boundary_p1,(*s_tree_current)->boundary_p2));
-      s_tree->_FindLeaf(s_tree_current,points[ii]->x,0);
-      assert((*s_tree_current)->npoints == 1);
-      points[ii]->image->leaf = *s_tree_current;
+      assert(inbox(points[ii]->x,(*s_tree_it)->boundary_p1,(*s_tree_it)->boundary_p2));
+      s_tree->_FindLeaf(s_tree_it,points[ii]->x,0);
+      assert((*s_tree_it)->npoints == 1);
+      points[ii]->image->leaf = *s_tree_it;
     }
   }
 	//*********************************************************************
 
 	// This loop should not be necessary!! It is repairing the leaf that has been assigned incorrectly somewhere
-	//if(!inbox(point[ii].x,(*i_tree_current)->boundary_p1,(*i_tree_current)->boundary_p2) ) moveTop(i_tree);
+	//if(!inbox(point[ii].x,(*i_tree_it)->boundary_p1,(*i_tree_it)->boundary_p2) ) moveTop(i_tree);
 	/*for(ii=0;ii < Nleaves;++ii){
 
 		// re-assign leaf of point that was to be refined
 		assert(inbox(points[ii]->x,i_tree->getTop()->boundary_p1,i_tree->getTop()->boundary_p2));
-		(*i_tree_current) = points[ii]->leaf;
-		assert(inbox(points[ii]->x,(*i_tree_current)->boundary_p1,(*i_tree_current)->boundary_p2));
+		(*i_tree_it) = points[ii]->leaf;
+		assert(inbox(points[ii]->x,(*i_tree_it)->boundary_p1,(*i_tree_it)->boundary_p2));
 		_FindLeaf(i_tree,points[ii]->x,0);
-		points[ii]->leaf = (*i_tree_current);
+		points[ii]->leaf = (*i_tree_it);
 
 		assert(inbox(points[ii]->image->x,s_tree->getTop()->boundary_p1,s_tree->getTop()->boundary_p2));
 		assert(inbox(points[ii]->image->x,s_tree->getTop()->boundary_p1,s_tree->getTop()->boundary_p2));
@@ -763,11 +766,13 @@ void Grid::ClearAllMarks(){
 	unsigned long i;
 
   if(i_tree->pointlist->size() > 0){
-    PointList::iterator i_tree_pointlist_current(i_tree->pointlist->Top());
+    PointList::iterator i_tree_pointlist_it;
+    i_tree_pointlist_it.current = (i_tree->pointlist->Top());
+    
     for(i=0;ipointlist->size();++i){
-      (*i_tree_pointlist_current)->in_image=NO;
-      (*i_tree_pointlist_current)->image->in_image=NO;
-      --i_tree_pointlist_current;
+      (*i_tree_pointlist_it)->in_image=NO;
+      (*i_tree_pointlist_it)->image->in_image=NO;
+      --i_tree_pointlist_it;
     }
   }
 }
@@ -894,18 +899,19 @@ void Grid::test_mag_matrix(){
 	int count;
 	PosType kappa, gamma[3], invmag;
 
-  PointList::iterator i_tree_pointlist_current(i_tree->pointlist->Top());
+  PointList::iterator i_tree_pointlist_it;
+  i_tree_pointlist_it.current = (i_tree->pointlist->Top());
 	do{
 		count=0;
-		i_tree->FindAllBoxNeighborsKist(*i_tree_pointlist_current,neighbors);
+		i_tree->FindAllBoxNeighborsKist(*i_tree_pointlist_it,neighbors);
 		assert(neighbors->Nunits() >= 3);
 		neighbors->MoveToTop();
 		point2 = neighbors->getCurrent();
 		neighbors->Down();
-		while(!find_mag_matrix(aa,*i_tree_pointlist_current,point2,neighbors->getCurrent())) neighbors->Down();
+		while(!find_mag_matrix(aa,*i_tree_pointlist_it,point2,neighbors->getCurrent())) neighbors->Down();
 		//std::cout << "deflection neighbors a" << std::endl;
 		while(neighbors->Down()){
-			if(find_mag_matrix(ao,*i_tree_pointlist_current,point2,neighbors->getCurrent())){
+			if(find_mag_matrix(ao,*i_tree_pointlist_it,point2,neighbors->getCurrent())){
 				aa[0] = (count*aa[0] + ao[0])/(count+1);
 				aa[1] = (count*aa[1] + ao[1])/(count+1);
 				aa[2] = (count*aa[2] + ao[2])/(count+1);
@@ -921,13 +927,13 @@ void Grid::test_mag_matrix(){
 		gamma[2] = -0.5*(aa[2]-aa[3]);
 		invmag = (1-kappa)*(1-kappa) - gamma[0]*gamma[0] - gamma[1]*gamma[1] + gamma[2]*gamma[2];
 
-		(*i_tree_pointlist_current)->kappa =  kappa/(*i_tree_pointlist_current)->kappa - 1.0;
-		(*i_tree_pointlist_current)->gamma[0] = gamma[0]/(*i_tree_pointlist_current)->gamma[0] - 1.0;
-		(*i_tree_pointlist_current)->gamma[1] = gamma[1]/(*i_tree_pointlist_current)->gamma[1] - 1.0;
-		(*i_tree_pointlist_current)->gamma[2] = gamma[2]/(*i_tree_pointlist_current)->gamma[2] - 1.0;
-		(*i_tree_pointlist_current)->invmag = invmag/(*i_tree_pointlist_current)->invmag - 1.0;
+		(*i_tree_pointlist_it)->kappa =  kappa/(*i_tree_pointlist_it)->kappa - 1.0;
+		(*i_tree_pointlist_it)->gamma[0] = gamma[0]/(*i_tree_pointlist_it)->gamma[0] - 1.0;
+		(*i_tree_pointlist_it)->gamma[1] = gamma[1]/(*i_tree_pointlist_it)->gamma[1] - 1.0;
+		(*i_tree_pointlist_it)->gamma[2] = gamma[2]/(*i_tree_pointlist_it)->gamma[2] - 1.0;
+		(*i_tree_pointlist_it)->invmag = invmag/(*i_tree_pointlist_it)->invmag - 1.0;
 
-	}while(--i_tree_pointlist_current);
+	}while(--i_tree_pointlist_it);
 }
 
 /**
@@ -945,27 +951,27 @@ void Grid::zoom(
 		,Branch *top         /// where on the tree to start, if NULL it will start at the root
 		){
 
-  TreeStruct::iterator i_tree_current(i_tree);
+  TreeStruct::iterator i_tree_it(i_tree);
       
 	if(top==NULL){
 		if(!inbox(center,i_tree->getTop()->boundary_p1,i_tree->getTop()->boundary_p2)) return;
-		i_tree_current.movetop();
+		i_tree_it.movetop();
 	}else{
 		if(!inbox(center,top->boundary_p1,top->boundary_p2)) return;
-		i_tree_current = top;
+		i_tree_it = top;
 	}
 	Branch *tmp=NULL;
 	Point *newpoints;
 
-	i_tree->_FindLeaf(i_tree_current,center,0);
-	while((*i_tree_current)->points->gridsize > min_scale ){
-		tmp = *i_tree_current;
-		newpoints = RefineLeaf(lens,(*i_tree_current)->points);
+	i_tree->_FindLeaf(i_tree_it,center,0);
+	while((*i_tree_it)->points->gridsize > min_scale ){
+		tmp = *i_tree_it;
+		newpoints = RefineLeaf(lens,(*i_tree_it)->points);
 		if(newpoints == NULL) break;  // case where all the new points where outside the region
-		i_tree_current = tmp;
-		assert(inbox(center,(*i_tree_current)->boundary_p1,(*i_tree_current)->boundary_p2));
-		i_tree->_FindLeaf(i_tree_current,center,0);
-		assert((*i_tree_current)->npoints == 1);
+		i_tree_it = tmp;
+		assert(inbox(center,(*i_tree_it)->boundary_p1,(*i_tree_it)->boundary_p2));
+		i_tree->_FindLeaf(i_tree_it,center,0);
+		assert((*i_tree_it)->npoints == 1);
 	};
 
 	return;
@@ -1151,18 +1157,18 @@ PixelMap Grid::writePixelMapUniform(
   std::vector lists(Nblocks);
   
   bool allowDecent;
-  TreeStruct::iterator i_tree_current(i_tree);
+  TreeStruct::iterator i_tree_it(i_tree);
   int i = 0;
   do{
-    if((*i_tree_current)->level == 4){
-      lists[i].setTop( (*i_tree_current)->points );
-      lists[i].setN( (*i_tree_current)->npoints );
+    if((*i_tree_it)->level == 4){
+      lists[i].setTop( (*i_tree_it)->points );
+      lists[i].setN( (*i_tree_it)->npoints );
       ++i;
       allowDecent = false;
     }else{
       allowDecent = true;
     }
-  }while(i_tree_current.TreeWalkStep(allowDecent) && i < Nblocks);
+  }while(i_tree_it.TreeWalkStep(allowDecent) && i < Nblocks);
   
   std::vector thrs;
   
@@ -1183,23 +1189,23 @@ void Grid::writePixelMapUniform(
   map.Clean();
   int Nblocks = 16;
   std::vector lists(Nblocks);
-  TreeStruct::iterator i_tree_current(i_tree);
+  TreeStruct::iterator i_tree_it(i_tree);
 
   
   bool allowDecent;
-  i_tree_current.movetop();
+  i_tree_it.movetop();
   int i = 0;
   do{
-    if((*i_tree_current)->level == 4){
+    if((*i_tree_it)->level == 4){
       assert(i < 16);
-      lists[i].setTop( (*i_tree_current)->points );
-      lists[i].setN( (*i_tree_current)->npoints );
+      lists[i].setTop( (*i_tree_it)->points );
+      lists[i].setN( (*i_tree_it)->npoints );
       ++i;
       allowDecent = false;
     }else{
       allowDecent = true;
     }
-  }while(i_tree_current.TreeWalkStep(allowDecent) && i < Nblocks);
+  }while(i_tree_it.TreeWalkStep(allowDecent) && i < Nblocks);
   
   std::thread thr[16];
   
@@ -1215,42 +1221,43 @@ void Grid::writePixelMapUniform_(const PointList &list,PixelMap *map,LensingVari
   PosType tmp2[2];
   long index;
   
-  PointList::iterator list_current(list.Top());
+  PointList::iterator list_it;
+  list_it.current = (list.Top());
   for(size_t i = 0; i< list.size(); ++i){
     switch (val) {
       case ALPHA:
-        tmp2[0] = (*list_current)->x[0] - (*list_current)->image->x[0];
-        tmp2[1] = (*list_current)->x[1] - (*list_current)->image->x[1];
+        tmp2[0] = (*list_it)->x[0] - (*list_it)->image->x[0];
+        tmp2[1] = (*list_it)->x[1] - (*list_it)->image->x[1];
         tmp = sqrt(tmp2[0]*tmp2[0] + tmp2[1]*tmp2[1]);
         break;
       case ALPHA1:
-        tmp = ((*list_current)->x[0] - (*list_current)->image->x[0]);
+        tmp = ((*list_it)->x[0] - (*list_it)->image->x[0]);
         break;
       case ALPHA2:
-        tmp = ((*list_current)->x[1] - (*list_current)->image->x[1]);
+        tmp = ((*list_it)->x[1] - (*list_it)->image->x[1]);
         break;
       case KAPPA:
-        tmp = (*list_current)->kappa;
+        tmp = (*list_it)->kappa;
         break;
       case GAMMA:
-        tmp2[0] = (*list_current)->gamma[0];
-        tmp2[1] = (*list_current)->gamma[1];
+        tmp2[0] = (*list_it)->gamma[0];
+        tmp2[1] = (*list_it)->gamma[1];
         tmp = sqrt(tmp2[0]*tmp2[0] + tmp2[1]*tmp2[1]);
         break;
       case GAMMA1:
-        tmp = (*list_current)->gamma[0];
+        tmp = (*list_it)->gamma[0];
         break;
       case GAMMA2:
-        tmp = (*list_current)->gamma[1];
+        tmp = (*list_it)->gamma[1];
         break;
       case GAMMA3:
-        tmp = (*list_current)->gamma[2];
+        tmp = (*list_it)->gamma[2];
         break;
       case INVMAG:
-        tmp = (*list_current)->invmag;
+        tmp = (*list_it)->invmag;
         break;
       case DT:
-        tmp = (*list_current)->dt;
+        tmp = (*list_it)->dt;
         break;
       default:
         std::cerr << "PixelMap::AddGrid() does not work for the input LensingVariable" << std::endl;
@@ -1259,10 +1266,10 @@ void Grid::writePixelMapUniform_(const PointList &list,PixelMap *map,LensingVari
         // If this list is to be expanded to include ALPHA or GAMMA take care to add them as vectors
     }
     
-    index = map->find_index((*list_current)->x);
+    index = map->find_index((*list_it)->x);
     if(index != -1)(*map)[index] = tmp;
     
-    --list_current;
+    --list_it;
   }
 }
 
diff --git a/TreeCode_link/peak_refinement.cpp b/TreeCode_link/peak_refinement.cpp
index 81a63a9c..b0ac94a4 100644
--- a/TreeCode_link/peak_refinement.cpp
+++ b/TreeCode_link/peak_refinement.cpp
@@ -46,7 +46,8 @@ short find_peaks(
 
 	// Add all points to imageinfo
 	//MoveToTopList(grid->i_tree->pointlist);
-      PointList::iterator i_tree_pl_current(grid->i_tree->pointlist->Top());
+      PointList::iterator i_tree_pl_current;
+      i_tree_pl_current.current = (grid->i_tree->pointlist->Top());
 	do{
 		imageinfo[0].imagekist->InsertAfterCurrent(*i_tree_pl_current);
 	}while(--i_tree_pl_current);
diff --git a/TreeCode_link/tree_maintenance.cpp b/TreeCode_link/tree_maintenance.cpp
index f4354d73..e9e2b315 100644
--- a/TreeCode_link/tree_maintenance.cpp
+++ b/TreeCode_link/tree_maintenance.cpp
@@ -269,7 +269,7 @@ short TreeStruct::emptyTree(){
 
   assert(Nbranches == 1);
 
-  PointList::iterator pointlist_current(*pointlist);
+  PointList::iterator pointlist_current(pointlist->Top());
   for(i=0,j=0,count=0;isize();++i){
 	  if((*pointlist_current)->head){
 		  heads[j] = *pointlist_current;
@@ -365,7 +365,7 @@ void TreeStruct::_freeBranches_iter(){
 	}
 
 	// re-assign the leaf pointers in the particles to the root
-  PointList::iterator pointlist_current(*pointlist);
+  PointList::iterator pointlist_current(pointlist->Top());
 	 do{ (*pointlist_current)->leaf = top; }while(--pointlist_current);
 
     return;
diff --git a/include/Tree.h b/include/Tree.h
index c00d80fc..20eda943 100644
--- a/include/Tree.h
+++ b/include/Tree.h
@@ -105,12 +105,12 @@ struct TreeStruct{
     std::vector tmp_point;
   };
   
-  iterator begin() const{
+  TreeStruct::iterator begin() const{
     iterator it(top);
     return it;
   };
   
-  iterator end() const{
+  TreeStruct::iterator end() const{
     iterator it(top->brother);
     return it;
   };
diff --git a/include/image_processing.h b/include/image_processing.h
index 3c177d8d..3262ade2 100644
--- a/include/image_processing.h
+++ b/include/image_processing.h
@@ -54,6 +54,7 @@ class PixelMap
 
   void AddImages(ImageInfo *imageinfo,int Nimages,float rescale = 1.);
   void AddImages(std::vector &imageinfo,int Nimages,float rescale = 1.);
+  void AddGridBrightness(Grid &grid);
   void AddUniformImages(ImageInfo *imageinfo,int Nimages,double value);
   PosType AddSource(Source &source);
   /// Add a source to the pixel map by oversamples the source so that oversample^2 points within each pixel are averaged
diff --git a/include/lens.h b/include/lens.h
index ae5324ed..dfedb3f8 100644
--- a/include/lens.h
+++ b/include/lens.h
@@ -133,13 +133,13 @@ class Lens
 
 
 	/// inserts a single main lens halo and adds it to the existing ones
-  void insertMainHalo(LensHalo* halo,PosType zlens, bool addplanes,bool verbose);
+  void insertMainHalo(LensHalo* halo,PosType zlens, bool addplanes,bool verbose = false);
 
 	/// inserts a sequence of main lens halos and adds them to the existing ones
 	void insertMainHalos(LensHalo** halos, std::size_t Nhalos,bool addplanes,bool verbose = false);
 
 	/// replaces existing main halos with a single main halo
-  void replaceMainHalo(LensHalo* halo,PosType zlens, bool addplanes,bool verbose);
+  void replaceMainHalo(LensHalo* halo,PosType zlens, bool addplanes,bool verbose = false);
 
 	/// replaces existing main halos with a sequence of main halos
 	void replaceMainHalos(LensHalo** halos, std::size_t Nhalos,bool verbose = false);
diff --git a/include/point.h b/include/point.h
index bbfcea72..51dfd6c8 100644
--- a/include/point.h
+++ b/include/point.h
@@ -268,37 +268,37 @@ struct PointList{
   }
   ~PointList(){EmptyList();}
   
-  class iterator{
-  private:
+  struct iterator{
+
     Point *current;
-  public:
     
-    iterator(){
-      current = NULL;
-    }
-    iterator(PointList &list){
+    iterator():current(NULL){ }
+    
+/*    iterator(PointList &list){
       current = list.top;
     }
     iterator(iterator &it){
       current = *it;
     }
+ */
     iterator(Point *p){
       current = p;
     }
     
-    Point * operator*(){return current;}
-    
-    PointList::iterator &operator=(PointList::iterator &p){
-      if(&p == this) return *this;
-      current = p.current;
-      return *this;
-    }
-    
+ /*
     PointList::iterator &operator=(Point *point){
       current = point;
       return *this;
     }
+*/
+    Point *operator*(){return current;}
     
+    /*iterator &operator=(iterator &p){
+      if(&p == this) return *this;
+      current = p.current;
+      return *this;
+    }*/
+
     bool operator++(){
       assert(current);
       if(current->prev == NULL) return false;
@@ -357,6 +357,21 @@ struct PointList{
     return *it == bottom;
   };
   
+  /* Many changes need to be made to implement this correctly
+  PointList::iterator begin() const{
+    PointList::iterator it;
+    it.current = bottom;
+    return it;
+  }
+
+  PointList::iterator end() const{
+    PointList::iterator it;
+    it.current = top->prev;
+    return it;
+  }
+  */
+  
+  
   Point *Top() const {return top;}
   Point *Bottom() const {return bottom;}
   

From c53842df4302cd6ee21c086247680e565d728f5a Mon Sep 17 00:00:00 2001
From: RB Metcalf 
Date: Fri, 12 May 2017 16:01:25 +0200
Subject: [PATCH 023/227] Added PixelMap::AddGridBrightness() so that the flux
 from an entire grid can be added to a pixel map without having to find
 images.

Clean up some things related to the PointList.
---
 AnalyticNSIE/source.cpp            |   4 +-
 ImageProcessing/pixelize.cpp       |  58 ++++++++-
 TreeCode_link/KistDriver.cpp       | 116 ++++++++---------
 TreeCode_link/List/list.cpp        |  99 ++++++++-------
 TreeCode_link/Tree.cpp             |  25 ++--
 TreeCode_link/TreeDriver.cpp       |  70 +++++-----
 TreeCode_link/curve_routines.cpp   |  35 ++---
 TreeCode_link/find_crit.cpp        |  75 +++++------
 TreeCode_link/grid_maintenance.cpp | 197 +++++++++++++++--------------
 TreeCode_link/gridmap.cpp          |   2 -
 TreeCode_link/peak_refinement.cpp  |   3 +-
 TreeCode_link/tree_maintenance.cpp |   4 +-
 include/Tree.h                     |   4 +-
 include/image_processing.h         |   4 +-
 include/lens.h                     |   4 +-
 include/point.h                    |  45 ++++---
 16 files changed, 416 insertions(+), 329 deletions(-)

diff --git a/AnalyticNSIE/source.cpp b/AnalyticNSIE/source.cpp
index 4ec4fb2a..7b163af5 100644
--- a/AnalyticNSIE/source.cpp
+++ b/AnalyticNSIE/source.cpp
@@ -262,8 +262,10 @@ SourcePixelled::SourcePixelled(
   source_x[1] = gal_map.getCenter()[1];
   source_r =  range/sqrt(2.);
   values.resize(Npixels*Npixels);
+  
+  double convertion = 1.0/resolution/resolution*factor;
   for (int i = 0; i < Npixels*Npixels; i++)
-    values[i] = gal_map(i)/resolution/resolution*factor;
+    values[i] = gal_map(i)*convertion;
   
   calcTotalFlux();
   calcCentroid();
diff --git a/ImageProcessing/pixelize.cpp b/ImageProcessing/pixelize.cpp
index 0cf481fc..1f7b3a1c 100644
--- a/ImageProcessing/pixelize.cpp
+++ b/ImageProcessing/pixelize.cpp
@@ -500,6 +500,43 @@ void PixelMap::AddImages(
   
   return;
 }
+/** \brief Add an image to the map
+ *
+ */
+void PixelMap::AddGridBrightness(Grid &grid){
+  
+  
+  
+  PointList *plist = grid.i_tree->pointlist;
+
+  if(plist->size() == 0) return;
+  
+  PointList::iterator listit= plist->Top();// = plist->begin();
+  
+  PosType sb = 1;
+  float area = 1;
+  
+  std::list  neighborlist;
+  std::list::iterator it;
+  //for(long ii=0;iibegin() ; listit != plist->end() ; ++listit ){
+    sb = (*listit)->surface_brightness;
+  
+    if (sb != 0.0 && (inMapBox((*listit)->leaf)) == true){
+      PointsWithinLeaf((*listit)->leaf,neighborlist);
+      for(it = neighborlist.begin();it != neighborlist.end();it++){
+        area = LeafPixelArea(*it,(*listit)->leaf);
+        map[*it] += sb*area;
+      }
+    }
+  }while(--listit);
+
+  //for(size_t i=0; i< Nx*Ny ;++i) map[i] /= resolution*resolution;
+
+  return;
+}
 
 void PixelMap::AddImages(
                          std::vector &imageinfo   /// An array of ImageInfo-s.  There is no reason to separate images for this routine
@@ -1178,13 +1215,17 @@ void PixelMap::AddCurve(std::vector &curve,double value){
 
 
 /**
- *  \brief Fills in pixels where the image plane points in the grid are located with the value given
+ *  \brief Fills in pixels where the image plane points in the grid are located with the value given.
+ 
+ This is for lensing quantities and not surface brightness.  If you want surface brightness use PixelMap::AddGridBrightness()
  */
 void PixelMap::AddGrid(const Grid &grid,PosType value){
+  if(grid.getNumberOfPoints() == 0) return;
+
   PointList* list = grid.i_tree->pointlist;
   size_t index;
   
-  PointList::iterator list_current(list->Top());
+  PointList::iterator list_current = list->Top();
   do{
     if(inMapBox((*list_current)->x)){
       index = find_index((*list_current)->x);
@@ -1199,6 +1240,8 @@ void PixelMap::AddGrid(const Grid &grid,PosType value){
  *  The grid and PixelMap do not need to be related in any way.
  *  Using this function multiple grids can be added to the same image.
  *
+ * This is for lensing quantities and not surface brightness.  If you want surface brightness use PixelMap::AddGridBrightness()
+
  *
  *  Warning: When adding a new grid it should not overlap with any of the previously added grids.
  */
@@ -1227,7 +1270,7 @@ void PixelMap::AddGrid(const Grid &grid,LensingVariable val){
       
       lists[i].setTop((*treeit)->points);
       lists[i].setN((*treeit)->npoints);
-      list_current = lists[i].Top();
+      list_current.current = lists[i].Top();
       list_current.JumpDownList( (*treeit)->npoints -1);
       lists[i].setBottom(*list_current);
 
@@ -1253,8 +1296,10 @@ void PixelMap::AddGrid_(const PointList &list,LensingVariable val){
   double tmp,area;
   PosType tmp2[2];
   
-  PointList::iterator pl_it(list.Top());
-  for(size_t i = 0; i< list.size(); ++i){
+  PointList::iterator pl_it = list.Top();
+  do{
+  //for(PointList::iterator pl_it = list.begin() ; pl_it != list.end() ; ++pl_it){
+  //for(size_t i = 0; i< list.size(); ++i){
     
     switch (val) {
       case ALPHA:
@@ -1310,9 +1355,8 @@ void PixelMap::AddGrid_(const PointList &list,LensingVariable val){
         }
       }
     }
-    --pl_it;
     
-  }
+  }while(--pl_it);
 }
 
 
diff --git a/TreeCode_link/KistDriver.cpp b/TreeCode_link/KistDriver.cpp
index 57deaa5a..f78e9362 100644
--- a/TreeCode_link/KistDriver.cpp
+++ b/TreeCode_link/KistDriver.cpp
@@ -12,10 +12,10 @@
 bool TreeStruct::Test(){
   
   std::cout << "Running Test on tree ..." << std::endl;
-  PointList::iterator pl_current(*pointlist);
+  PointList::iterator pl_current;
   TreeStruct::iterator treeit(top);
 
-  pl_current = (*treeit)->points;
+  pl_current.current = (*treeit)->points;
   for(int k = 0; k < (*treeit)->npoints ; ++k,--pl_current){
     //assert( !std::isnan((*pl_current)->invmag) );
     assert( (*pl_current)->invmag == (*pl_current)->invmag );
@@ -25,7 +25,7 @@ bool TreeStruct::Test(){
   do{
     
     // check that every points is actually in the branch it should be in
-      pl_current = (*treeit)->points;
+      pl_current.current = (*treeit)->points;
       for(int k = 0; k < (*treeit)->npoints ; ++k,--pl_current){
         assert( inbox((*pl_current)->x,(*treeit)->boundary_p1,(*treeit)->boundary_p2) );
         //assert( !std::isnan((*pl_current)->invmag) );
@@ -276,7 +276,7 @@ void TreeStruct::_PointsWithinKist(TreeStruct::iterator ¤t,PosType *rmax,K
   int i,j,incell2=1;
   PosType radius;
   short pass;
-  PointList::iterator pointlist_current;
+  PointList::iterator pointlist_it;
   
   if((*current)->npoints == 0) return;
 
@@ -301,42 +301,42 @@ void TreeStruct::_PointsWithinKist(TreeStruct::iterator ¤t,PosType *rmax,K
     	  globs.ray[0] = globs.realray[0];
     	  globs.ray[1] = globs.realray[1];
 
-    	  if((*current)->points != NULL) pointlist_current=(*current)->points;
+    	  if((*current)->points != NULL) pointlist_it.current = (*current)->points;
 
     	  if( current.atLeaf() ){
     	   	  // if leaf calculate the distance to all the points in cell
     		  for(i=0;i<(*current)->npoints;++i){
-    			  for(j=0,radius=0.0;j<2;++j) radius+=pow((*pointlist_current)->x[j] - globs.ray[j],2);
+    			  for(j=0,radius=0.0;j<2;++j) radius+=pow((*pointlist_it)->x[j] - globs.ray[j],2);
     			  if( radius < *rmax**rmax ){
        				  if(markpoints == 1){
-       					  (*pointlist_current)->in_image = YES;
-      					  (*pointlist_current)->image->in_image = YES;
+       					  (*pointlist_it)->in_image = YES;
+      					  (*pointlist_it)->image->in_image = YES;
       				  }else if(markpoints == -1){
-      					  (*pointlist_current)->in_image=NO;
-                  (*pointlist_current)->image->in_image=NO;
-                  (*pointlist_current)->surface_brightness = (*pointlist_current)->image->surface_brightness = 0.0;
+      					  (*pointlist_it)->in_image=NO;
+                  (*pointlist_it)->image->in_image=NO;
+                  (*pointlist_it)->surface_brightness = (*pointlist_it)->image->surface_brightness = 0.0;
        				  }else if(markpoints == 0){
-         				  neighborkist->InsertAfterCurrent(*pointlist_current);
+         				  neighborkist->InsertAfterCurrent(*pointlist_it);
       				  }
-       				  if(*maxgridsize < (*pointlist_current)->gridsize) *maxgridsize = (*pointlist_current)->gridsize;
+       				  if(*maxgridsize < (*pointlist_it)->gridsize) *maxgridsize = (*pointlist_it)->gridsize;
     			  }
-            --pointlist_current;
+            --pointlist_it;
     		  }
     	  }else{ // put all of points in box into getCurrentKist(imagekist)
        		  for(i=0;i<(*current)->npoints;++i){
        			  if(markpoints == 1){
-       				  (*pointlist_current)->in_image=YES;
-       				  (*pointlist_current)->image->in_image=YES;
+       				  (*pointlist_it)->in_image=YES;
+       				  (*pointlist_it)->image->in_image=YES;
        			  }else if(markpoints == -1){
-       				  (*pointlist_current)->in_image=NO;
-       				  (*pointlist_current)->image->in_image=NO;
-                (*pointlist_current)->surface_brightness = (*pointlist_current)->image->surface_brightness = 0.0;
+       				  (*pointlist_it)->in_image=NO;
+       				  (*pointlist_it)->image->in_image=NO;
+                (*pointlist_it)->surface_brightness = (*pointlist_it)->image->surface_brightness = 0.0;
       			  }else if(markpoints == 0){
-       				  neighborkist->InsertAfterCurrent(*pointlist_current);
+       				  neighborkist->InsertAfterCurrent(*pointlist_it);
  				  }
-  				  if(*maxgridsize < (*pointlist_current)->gridsize) *maxgridsize = (*pointlist_current)->gridsize;
+  				  if(*maxgridsize < (*pointlist_it)->gridsize) *maxgridsize = (*pointlist_it)->gridsize;
 
-              --pointlist_current;
+              --pointlist_it;
        		  }
     	  }
 
@@ -373,50 +373,50 @@ void TreeStruct::_PointsWithinKist(TreeStruct::iterator ¤t,PosType *rmax,K
 	  // does radius cut into the box
 	  if( pass ){
 
-      if((*current)->points != NULL) pointlist_current = (*current)->points;
+      if((*current)->points != NULL) pointlist_it.current = (*current)->points;
 
 		  if( current.atLeaf()  ){  /* leaf case */
 
 			  for(i=0;i<(*current)->npoints;++i){
 
-				  for(j=0,radius=0.0;j<2;++j) radius+=pow((*pointlist_current)->x[j] - globs.ray[j],2);
+				  for(j=0,radius=0.0;j<2;++j) radius+=pow((*pointlist_it)->x[j] - globs.ray[j],2);
 				  if( radius < *rmax**rmax ){
 					  if(markpoints==1){
-						  (*pointlist_current)->in_image=YES;
-						  (*pointlist_current)->image->in_image=YES;
+						  (*pointlist_it)->in_image=YES;
+						  (*pointlist_it)->image->in_image=YES;
 					  }else if(markpoints==-1){
-						  (*pointlist_current)->in_image=NO;
-						  (*pointlist_current)->image->in_image=NO;
-     					  (*pointlist_current)->surface_brightness = (*pointlist_current)->image->surface_brightness = 0.0;
+						  (*pointlist_it)->in_image=NO;
+						  (*pointlist_it)->image->in_image=NO;
+     					  (*pointlist_it)->surface_brightness = (*pointlist_it)->image->surface_brightness = 0.0;
 					  }else if(markpoints==0){
-						  neighborkist->InsertAfterCurrent((*pointlist_current));
+						  neighborkist->InsertAfterCurrent((*pointlist_it));
      				  }
-      				  if(*maxgridsize < (*pointlist_current)->gridsize) *maxgridsize = (*pointlist_current)->gridsize;
+      				  if(*maxgridsize < (*pointlist_it)->gridsize) *maxgridsize = (*pointlist_it)->gridsize;
 
 				  }
-          --pointlist_current;
+          --pointlist_it;
 			  }
 		  }else if(pass==1){ // whole box is inside radius
 
-			  pointlist_current = (*current)->points;
+			  pointlist_it.current = (*current)->points;
 			  for(i=0;i<(*current)->npoints;++i){
 
-				  //assert( inbox((*pointlist_current)->x,(*current)->boundary_p1,(*current)->boundary_p2) );
-				  //assert( *rmax**rmax >= (pow((*pointlist_current)->x[0] - ray[0],2) + pow((*pointlist_current)->x[1] - ray[1],2) ));
+				  //assert( inbox((*pl_it)->x,(*current)->boundary_p1,(*current)->boundary_p2) );
+				  //assert( *rmax**rmax >= (pow((*pl_it)->x[0] - ray[0],2) + pow((*pl_it)->x[1] - ray[1],2) ));
 
 				  if(markpoints==1){
-   					  (*pointlist_current)->in_image=YES;
-  					  (*pointlist_current)->image->in_image=YES;
+   					  (*pointlist_it)->in_image=YES;
+  					  (*pointlist_it)->image->in_image=YES;
   				  }else if(markpoints==-1){
-  					  (*pointlist_current)->in_image=NO;
- 					  (*pointlist_current)->image->in_image=NO;
- 					  (*pointlist_current)->surface_brightness = (*pointlist_current)->image->surface_brightness = 0.0;
+  					  (*pointlist_it)->in_image=NO;
+ 					  (*pointlist_it)->image->in_image=NO;
+ 					  (*pointlist_it)->surface_brightness = (*pointlist_it)->image->surface_brightness = 0.0;
    				  }else if(markpoints==0){
-   					  neighborkist->InsertAfterCurrent((*pointlist_current));
+   					  neighborkist->InsertAfterCurrent((*pointlist_it));
   				  }
-  				  if(*maxgridsize < (*pointlist_current)->gridsize) *maxgridsize = (*pointlist_current)->gridsize;
+  				  if(*maxgridsize < (*pointlist_it)->gridsize) *maxgridsize = (*pointlist_it)->gridsize;
 
-          --pointlist_current;
+          --pointlist_it;
 			  }
 		  }else{
 			  if((*current)->child1 !=NULL){
@@ -449,7 +449,7 @@ void TreeStruct::PointsWithinKist_iter(const PosType* center,float rmin,float rm
 	unsigned long i;
 
   TreeStruct::iterator current(top);
-  PointList::iterator pointlist_current;
+  PointList::iterator pl_it;
   
 	neighborkist->Empty();
 
@@ -475,10 +475,10 @@ void TreeStruct::PointsWithinKist_iter(const PosType* center,float rmin,float rm
 			){
 
 				decend = false;
-				if((*current)->points != NULL) pointlist_current = (*current)->points;
+				if((*current)->points != NULL) pl_it.current = (*current)->points;
 				for(i=0;i<(*current)->npoints;++i){
-					neighborkist->InsertAfterCurrent((*pointlist_current));
-          --pointlist_current;
+					neighborkist->InsertAfterCurrent((*pl_it));
+          --pl_it;
 				}
 
 			}else if(Utilities::cutbox(center,(*current)->boundary_p1,(*current)->boundary_p2,rmax) == 0  // box is all outside outer circle
@@ -488,11 +488,11 @@ void TreeStruct::PointsWithinKist_iter(const PosType* center,float rmin,float rm
 
 			}else if(current.atLeaf()){      // box is a leaf that intersects the circle
 
-				if((*current)->points != NULL) pointlist_current = (*current)->points;
+				if((*current)->points != NULL) pl_it.current = (*current)->points;
 				for(i=0;i<(*current)->npoints;++i){
-					if(rmax*rmax >= pow((*pointlist_current)->x[0] - center[0],2) + pow((*pointlist_current)->x[1] - center[1],2) )
-						neighborkist->InsertAfterCurrent((*pointlist_current));
-          --pointlist_current;
+					if(rmax*rmax >= pow((*pl_it)->x[0] - center[0],2) + pow((*pl_it)->x[1] - center[1],2) )
+						neighborkist->InsertAfterCurrent((*pl_it));
+          --pl_it;
 				}
 			}
 
@@ -509,16 +509,16 @@ void TreeStruct::PointsWithinKist_iter(const PosType* center,float rmin,float rm
 			if(current.atLeaf()){      // box is a leaf that intersects the circle
 
 				if((*current)->points != NULL){
-					pointlist_current = (*current)->points;
+					pl_it.current = (*current)->points;
 				}
 
 				for(i=0;i<(*current)->npoints;++i){
 
-					r2 = pow((*pointlist_current)->x[0] - center[0],2) + pow((*pointlist_current)->x[1] - center[1],2);
+					r2 = pow((*pl_it)->x[0] - center[0],2) + pow((*pl_it)->x[1] - center[1],2);
 					if(rmax*rmax >= r2 && rmin*rmin <= r2){
-						neighborkist->InsertAfterCurrent((*pointlist_current));
+						neighborkist->InsertAfterCurrent((*pl_it));
 					}
-          --pointlist_current;
+          --pl_it;
 				}
 
 			}else if(BoxInCircle(center,rmax,(*current)->boundary_p1,(*current)->boundary_p2)  // box is all inside outer circle
@@ -527,12 +527,12 @@ void TreeStruct::PointsWithinKist_iter(const PosType* center,float rmin,float rm
 
 				decend = false;
 				if((*current)->points != NULL){
-					pointlist_current = (*current)->points;
+					pl_it.current = (*current)->points;
 				}
 
 				for(i=0;i<(*current)->npoints;++i){
-					neighborkist->InsertAfterCurrent((*pointlist_current));
-          --pointlist_current;
+					neighborkist->InsertAfterCurrent((*pl_it));
+          --pl_it;
 				}
 
 			}else if(Utilities::cutbox(center,(*current)->boundary_p1,(*current)->boundary_p2,rmax) == 0  // box is all outside outer circle
diff --git a/TreeCode_link/List/list.cpp b/TreeCode_link/List/list.cpp
index 8b517523..ead4bebd 100644
--- a/TreeCode_link/List/list.cpp
+++ b/TreeCode_link/List/list.cpp
@@ -22,7 +22,7 @@ void PointList::InsertAfterCurrent(iterator ¤t,PosType *x,unsigned long id
     return;
 }
 
-void PointList::InsertPointAfterCurrent(iterator ¤t,Point *point){
+void PointList::InsertPointAfterCurrent(PointList::iterator &it,Point *point){
   // leaves current unchanged
   // changes only list and links in point
   
@@ -30,18 +30,18 @@ void PointList::InsertPointAfterCurrent(iterator ¤t,Point *point){
     assert(top);
     assert(bottom);
     
-    point->prev=*current;
-    point->next=(*current)->next;
+    point->prev=*it;
+    point->next=(*it)->next;
     
-    if(*current == bottom) bottom=point;
-    else (*current)->next->prev=point;
-    (*current)->next=point;
+    if(*it == bottom) bottom=point;
+    else (*it)->next->prev=point;
+    (*it)->next=point;
   }else{  /* empty list case */
-    current=point;
+    it.current=point;
     top=point;
     bottom=point;
     point->prev = point->next = NULL;
-    current = point;
+    it.current = point;
   }
   Npoints++;
   return;
@@ -58,23 +58,23 @@ void PointList::InsertBeforeCurrent(iterator ¤t,PosType *x,unsigned long i
     return;
 }
 
-void PointList::InsertPointBeforeCurrent(iterator ¤t,Point *point){
+void PointList::InsertPointBeforeCurrent(iterator &it,Point *point){
   
   if(Npoints > 0){
     assert(top);
     assert(bottom);
     
-    point->prev = (*current)->prev;
-    point->next = *current;
-    if(*current == top) top=point;
-    else (*current)->prev->next=point;
-    (*current)->prev=point;
+    point->prev = (*it)->prev;
+    point->next = *it;
+    if(*it == top) top=point;
+    else (*it)->prev->next=point;
+    (*it)->prev=point;
   }else{  /* empty list case */
-    current=point;
+    it.current=point;
     top=point;
     bottom=point;
     point->prev = point->next = NULL;
-    current = point;
+    it.current = point;
   }
   
   Npoints++;
@@ -82,7 +82,7 @@ void PointList::InsertPointBeforeCurrent(iterator ¤t,Point *point){
   return;
 }
 
-void PointList::MoveCurrentToBottom(iterator ¤t){
+void PointList::MoveCurrentToBottom(iterator &it){
 
 	assert(top);
 	assert(bottom);
@@ -90,42 +90,42 @@ void PointList::MoveCurrentToBottom(iterator ¤t){
 	Point *point,*point_current;
 	// leaves current one above former current
 
-	point=TakeOutCurrent(current);
-	point_current=*current;
-  current = bottom;
-	InsertPointAfterCurrent(current,point);
-	current=point_current;
+	point=TakeOutCurrent(it);
+	point_current=*it;
+  it.current = bottom;
+	InsertPointAfterCurrent(it,point);
+	it.current=point_current;
 }
 
 /**
  *  takes out current point and set current to point previous */
 /* Except at top where current is set to new top */
 /* returns pointer to removed point */
-Point *PointList::TakeOutCurrent(iterator ¤t){
+Point *PointList::TakeOutCurrent(iterator &it){
 
     Point *point;
 
     if(Npoints <= 0) return NULL;
-    assert(*current);
+    assert(*it);
 
-    point = *current;
+    point = *it;
 
     if(top == bottom){  /* last point */
-      current=NULL;
+      it.current=NULL;
       top=NULL;
       bottom=NULL;
-    }else if(*current==top){
+    }else if(*it==top){
       top=top->next;
-      current = top;
+      it.current = top;
       top->prev=NULL;
-    } else if(*current == bottom){
+    } else if(*it == bottom){
       bottom = bottom->prev;
-      current = bottom;
+      it.current = bottom;
       bottom->next=NULL;
     }else{
-      (*current)->prev->next=(*current)->next;
-      (*current)->next->prev=(*current)->prev;
-      current = (*current)->prev;
+      (*it)->prev->next=(*it)->next;
+      (*it)->next->prev=(*it)->prev;
+      it.current = (*it)->prev;
     }
 
     Npoints--;
@@ -229,21 +229,22 @@ void PointList::EmptyList(){
 	Point **point;
 	unsigned long blocks=0,i=0,Nfreepoints=0;
 
-  iterator current(top);
+  iterator it;
+  it.current = top;
 
 	do{
-	  if((*current)->head > 0){ ++blocks; Nfreepoints += (*current)->head;}
-	}while(current--);
+	  if((*it)->head > 0){ ++blocks; Nfreepoints += (*it)->head;}
+	}while(it--);
 
 	assert(blocks <= Npoints);
 	assert(Nfreepoints == Npoints);
 
 	point=(Point **)malloc(blocks*sizeof(Point*));
 
-  current = top;
+  it.current = top;
 	do{
-	  if((*current)->head > 0){ point[i] = *current; ++i;}
-	}while(current--);
+	  if((*it)->head > 0){ point[i] = *it; ++i;}
+	}while(it--);
 
 	for(i=0;iNpoints);
   std::printf("%li\n",Npoints);
 
   for(i=0;iid,(*current)->x[0],(*current)->x[1]
-                                  ,(*current)->gridsize);
-    --current;
+    std::printf("%li  %li  %e %e %e\n",i,(*it)->id,(*it)->x[0],(*it)->x[1]
+                                  ,(*it)->gridsize);
+    --it;
   }
 
 }
diff --git a/TreeCode_link/Tree.cpp b/TreeCode_link/Tree.cpp
index a4ac84d9..97003e72 100644
--- a/TreeCode_link/Tree.cpp
+++ b/TreeCode_link/Tree.cpp
@@ -230,10 +230,10 @@ void TreeStruct::construct_root(
       //pointlist=NewList();
       pointlist= new PointList;
    //EmptyList(pointlist);
-      PointList::iterator pointlist_current;
+      PointList::iterator pl_it;
   for(i=0;iInsertPointAfterCurrent(pointlist_current,&xp[i]);
-    --pointlist_current;
+    pointlist->InsertPointAfterCurrent(pl_it,&xp[i]);
+    --pl_it;
   }
 
   top = new Branch(pointlist->Top(),npoints,boundary_p1,boundary_p2
@@ -450,10 +450,12 @@ void TreeStruct::insertChildToCurrent(Branch *current,Branch *branch,int child){
     }
 
     if(branch->npoints > 0){
-      PointList::iterator pointlist_current(branch->points);
+      PointList::iterator pl_it;
+      pl_it.current = branch->points;
+      
     	for(unsigned long i=0;inpoints;++i){
-    		(*pointlist_current)->leaf = branch;
-        --pointlist_current;
+    		(*pl_it)->leaf = branch;
+        --pl_it;
     	}
     }
 
@@ -523,11 +525,14 @@ void TreeStruct::printTree(TreeStruct::iterator ¤t){
   int i;
 
     printBranch(*current);
-  PointList::iterator pointlist_current((*current)->points);
+  PointList::iterator pl_it;
+  pl_it.current = ((*current)->points);
+  
+  
     for(i=0;i<(*current)->npoints;++i){
-      std::cout << (*pointlist_current)->id << " " << (*pointlist_current)->x[0] <<
-      " " << (*pointlist_current)->x[1] << std::endl;
-      --pointlist_current;
+      std::cout << (*pl_it)->id << " " << (*pl_it)->x[0] <<
+      " " << (*pl_it)->x[1] << std::endl;
+      --pl_it;
     }
     if((*current)->child1 == NULL) return;
 
diff --git a/TreeCode_link/TreeDriver.cpp b/TreeCode_link/TreeDriver.cpp
index fad7762e..6fee9349 100644
--- a/TreeCode_link/TreeDriver.cpp
+++ b/TreeCode_link/TreeDriver.cpp
@@ -126,7 +126,7 @@ void TreeStruct::_NearestNeighbor(TreeStruct::iterator ¤t,int Nneighbors,P
   int i,incell2=1;
   unsigned long index[Nneighbors+Nbucket];
   PosType dx,dy;
-  PointList::iterator pointlist_current;
+  PointList::iterator pl_it;
   
   //std::printf("**************************************\nlevel %i\n",(*current)->level);
   //for(i=0;i<(*current)->npoints;++i) std::printf("   %i\n",(*current)->points[i]);
@@ -145,11 +145,11 @@ void TreeStruct::_NearestNeighbor(TreeStruct::iterator ¤t,int Nneighbors,P
     		globs.ray[1] = globs.realray[1];
 
     		/* calculate the distance to all the points in cell */
-    		if((*current)->points != NULL ) pointlist_current = (*current)->points;
+    		if((*current)->points != NULL ) pl_it.current = (*current)->points;
     		for(i=0;i<(*current)->npoints;++i){
 
-    			dx = (*pointlist_current)->x[0] - globs.ray[0];
-    			dy = (*pointlist_current)->x[1] - globs.ray[1];
+    			dx = (*pl_it)->x[0] - globs.ray[0];
+    			dy = (*pl_it)->x[1] - globs.ray[1];
 
     			switch(*direction){
     			case 0: /* distance */
@@ -175,8 +175,8 @@ void TreeStruct::_NearestNeighbor(TreeStruct::iterator ¤t,int Nneighbors,P
 
      			index[i]=i;
     			//temp_points[i]=pointlist->current;
-          globs.tmp_point[i] = (*pointlist_current);
-          --pointlist_current;
+          globs.tmp_point[i] = (*pl_it);
+          --pl_it;
     		}
 
     		if((*current)->npoints > 0){
@@ -221,7 +221,7 @@ void TreeStruct::_NearestNeighbor(TreeStruct::iterator ¤t,int Nneighbors,P
 		  if( current.atLeaf() ){  /* leaf case */
 
 			  /* combine found neighbors with points in box and resort */
-			  if((*current)->points != NULL) pointlist_current=(*current)->points;
+			  if((*current)->points != NULL) pl_it.current=(*current)->points;
 
 			  for(i=0;inpoints+Nneighbors);++i){
 
-				  dx=(*pointlist_current)->x[0] - globs.ray[0];
-				  dy=(*pointlist_current)->x[1] - globs.ray[1];
+				  dx=(*pl_it)->x[0] - globs.ray[0];
+				  dy=(*pl_it)->x[1] - globs.ray[1];
 
 				  switch(*direction){
 				  case 0: /* distance */
@@ -257,8 +257,8 @@ void TreeStruct::_NearestNeighbor(TreeStruct::iterator ¤t,int Nneighbors,P
 				  }
 
 				  index[i]=i;
-				  globs.tmp_point[i] = *pointlist_current;
-          --pointlist_current;
+				  globs.tmp_point[i] = *pl_it;
+          --pl_it;
 			  }
 
 			  // sort the points found so far
@@ -482,7 +482,7 @@ Point *sortList(long n, PosType *arr,ListHndl list,Point *firstpointin){
   long i,j;
   PosType a;
   Point *point,*firstpoint;
-  PointList::iterator list_current;
+  PointList::iterator pl_it;
 
   if(n <= 1) return firstpointin;
 
@@ -491,21 +491,21 @@ Point *sortList(long n, PosType *arr,ListHndl list,Point *firstpointin){
   for(j=1;jTakeOutCurrent(list_current);
+    point=list->TakeOutCurrent(pl_it);
 
     i=j-1;
     while(i>-1 && arr[i] > a){
       arr[i+1]=arr[i];
       i--;
-      ++list_current;
+      ++pl_it;
     }
     arr[i+1]=a;
 
-    if( *list_current==list->Top() && i==-1) list->InsertPointBeforeCurrent(list_current,point);
-    else list->InsertPointAfterCurrent(list_current,point);
+    if( *pl_it==list->Top() && i==-1) list->InsertPointBeforeCurrent(pl_it,point);
+    else list->InsertPointAfterCurrent(pl_it,point);
 
     if(i == -1) firstpoint=point;
   }
@@ -830,23 +830,25 @@ void NeighborsOfNeighbors(ListHndl neighbors,ListHndl wholelist){
 
 	if(neighbors->size() < 1 || wholelist->size() < 1) return;
 
-  PointList::iterator wholelist_current(*wholelist);
-  PointList::iterator neighbors_current(*neighbors);
+  PointList::iterator wholelist_it;
+  wholelist_it.current = wholelist->Top();
+  PointList::iterator neighbors_it;
+  neighbors_it.current = neighbors->Top();
   
 	do{
-    wholelist_current = wholelist->Top();
+    wholelist_it.current = wholelist->Top();
 		do{
 			if(check==1){
-        ++wholelist_current;
+        ++wholelist_it;
 				check=0;
 			}
-			if(AreBoxNeighbors(*neighbors_current,*wholelist_current) ){
-				if( wholelist->Top() == *wholelist_current ) check=1;
-				point = wholelist->TakeOutCurrent(wholelist_current);
-				neighbors->InsertPointAfterCurrent(neighbors_current,point);
+			if(AreBoxNeighbors(*neighbors_it,*wholelist_it) ){
+				if( wholelist->Top() == *wholelist_it ) check=1;
+				point = wholelist->TakeOutCurrent(wholelist_it);
+				neighbors->InsertPointAfterCurrent(neighbors_it,point);
 			}
-		}while(--wholelist_current);
-	}while((--neighbors_current) && wholelist->size() > 0);
+		}while(--wholelist_it);
+	}while((--neighbors_it) && wholelist->size() > 0);
 
 	return ;
 }
@@ -1156,12 +1158,14 @@ void TransformPoints(ListHndl listout,ListHndl listin){
   unsigned long i;
 
   listout->EmptyList();
-  PointList::iterator listin_current(listin->Top());
-  PointList::iterator listout_current(listout->Top());
+  PointList::iterator listin_it;
+  listin_it.current = listin->Top();
+  PointList::iterator listout_it;
+  listout_it.current = listout->Top();
   for(i=0;isize();++i){
-    listout->InsertAfterCurrent(listout_current,(*listin_current)->image->x
-                                ,(*listin_current)->image->id,*listin_current);
-    --listin_current;
+    listout->InsertAfterCurrent(listout_it,(*listin_it)->image->x
+                                ,(*listin_it)->image->id,*listin_it);
+    --listin_it;
   }
   
 }
diff --git a/TreeCode_link/curve_routines.cpp b/TreeCode_link/curve_routines.cpp
index 24eadcad..e93212d5 100644
--- a/TreeCode_link/curve_routines.cpp
+++ b/TreeCode_link/curve_routines.cpp
@@ -1472,9 +1472,10 @@ void splitter(OldImageInfo *images,int Maximages,int *Nimages){
   
   NpointsTotal = images[0].Npoints;
   
-  PointList::iterator imagelist_current(imagelist->Bottom());
+  PointList::iterator imagelist_it;
+  imagelist_it.current = imagelist->Bottom();
   // copy image points into a list
-  for(i=0;iInsertPointAfterCurrent(imagelist_current,&(images[0].points[i]));
+  for(i=0;iInsertPointAfterCurrent(imagelist_it,&(images[0].points[i]));
   
   assert(imagelist->size() == images[0].Npoints);
   //std::printf("imagelist = %il\n",imagelist->Npoints);
@@ -1487,17 +1488,17 @@ void splitter(OldImageInfo *images,int Maximages,int *Nimages){
   point = images[0].points;
   newpointarray = NewPointArray(NpointsTotal);
   
-  imagelist_current = imagelist->Top();
+  imagelist_it.current = imagelist->Top();
   m=0;
   i=0;
   do{
-    if(i < *Nimages && images[i].points == *imagelist_current){
+    if(i < *Nimages && images[i].points == *imagelist_it){
       images[i].points=&(newpointarray[m]);
       ++i;
     }
-    PointCopyData(&(newpointarray[m]),*imagelist_current);
+    PointCopyData(&(newpointarray[m]),*imagelist_it);
     ++m;
-  }while(--imagelist_current);
+  }while(--imagelist_it);
   
   assert(m == NpointsTotal);
   
@@ -1537,14 +1538,16 @@ void splitlist(ListHndl imagelist,OldImageInfo *images,int *Nimages,int Maximage
   //std::printf("imagelist = %li\n",imagelist->Npoints);
   // divide images into disconnected curves using neighbors-of-neighbors
   
-  PointList::iterator imagelist_current(*imagelist);
-  PointList::iterator orderedlist_current;
+  PointList::iterator imagelist_it;
+  imagelist_it.current = imagelist->Top();
+  
+  PointList::iterator orderedlist_it;
   
   while(imagelist->size() > 0 && i < Maximages){
-    images[i].points = imagelist->TakeOutCurrent(imagelist_current);
-    orderedlist_current = orderedlist->Bottom();
-    orderedlist->InsertPointAfterCurrent(orderedlist_current,images[i].points);
-    --orderedlist_current;
+    images[i].points = imagelist->TakeOutCurrent(imagelist_it);
+    orderedlist_it.current = orderedlist->Bottom();
+    orderedlist->InsertPointAfterCurrent(orderedlist_it,images[i].points);
+    --orderedlist_it;
     NeighborsOfNeighbors(orderedlist,imagelist);
     images[i].Npoints = orderedlist->size() - m;
     m += images[i].Npoints;
@@ -1556,11 +1559,11 @@ void splitlist(ListHndl imagelist,OldImageInfo *images,int *Nimages,int Maximage
   if(i == Maximages && imagelist->size() > 0){
     Point *point;
     do{
-      point = imagelist->TakeOutCurrent(imagelist_current);
+      point = imagelist->TakeOutCurrent(imagelist_it);
       //MoveToBottomList(orderedlist);
-      orderedlist_current = orderedlist->Bottom();
-      orderedlist->InsertPointAfterCurrent(orderedlist_current,point);
-      --orderedlist_current;
+      orderedlist_it.current = orderedlist->Bottom();
+      orderedlist->InsertPointAfterCurrent(orderedlist_it,point);
+      --orderedlist_it;
       ++(images[Maximages-1].Npoints);
     }while(imagelist->size() > 0);
   }
diff --git a/TreeCode_link/find_crit.cpp b/TreeCode_link/find_crit.cpp
index bd26576c..f8e21411 100644
--- a/TreeCode_link/find_crit.cpp
+++ b/TreeCode_link/find_crit.cpp
@@ -57,20 +57,21 @@ void ImageFinding::find_crit(
   
   // find kist of points with 1/magnification less than invmag_min
   negimage[0].imagekist->Empty();
-  PointList::iterator i_tree_pointlist_current(grid->i_tree->pointlist->Top());
-  Point *minpoint = *i_tree_pointlist_current;
+  PointList::iterator i_tree_pointlist_it;
+  i_tree_pointlist_it.current = (grid->i_tree->pointlist->Top());
+  Point *minpoint = *i_tree_pointlist_it;
   
   for(i=0;ii_tree->pointlist->size();++i){
-    if((*i_tree_pointlist_current)->invmag < invmag_min){
-      negimage[0].imagekist->InsertAfterCurrent(*i_tree_pointlist_current);
+    if((*i_tree_pointlist_it)->invmag < invmag_min){
+      negimage[0].imagekist->InsertAfterCurrent(*i_tree_pointlist_it);
       negimage[0].imagekist->Down();
     }else{
-      (*i_tree_pointlist_current)->in_image = NO;
+      (*i_tree_pointlist_it)->in_image = NO;
     }
     
     // record point of maximum kappa
-    if((*i_tree_pointlist_current)->kappa > minpoint->kappa) minpoint = *i_tree_pointlist_current;
-    --i_tree_pointlist_current;
+    if((*i_tree_pointlist_it)->kappa > minpoint->kappa) minpoint = *i_tree_pointlist_it;
+    --i_tree_pointlist_it;
   }
   
   if(negimage[0].imagekist->Nunits() == 0){
@@ -1270,18 +1271,18 @@ CritType ImageFinding::find_pseudo(ImageInfo &pseudocurve,ImageInfo &negimage
  
  // find kist of points with 1/magnification less than invmag_min
  critcurve[0].imagekist->Empty();
- PointList::iterator i_tree_pointlist_current(grid->i_tree->pointlist->Top());
- Point *minpoint = *i_tree_pointlist_current;
+ PointList::iterator i_tree_pointlist_it(grid->i_tree->pointlist->Top());
+ Point *minpoint = *i_tree_pointlist_it;
  
  for(i=0;ii_tree->pointlist->size();++i){
- if((*i_tree_pointlist_current)->invmag < invmag_min){
- critcurve[0].imagekist->InsertAfterCurrent(*i_tree_pointlist_current);
+ if((*i_tree_pointlist_it)->invmag < invmag_min){
+ critcurve[0].imagekist->InsertAfterCurrent(*i_tree_pointlist_it);
  critcurve[0].imagekist->Down();
  }
  
  // record point of maximum kappa
- if((*i_tree_pointlist_current)->kappa > minpoint->kappa) minpoint = *i_tree_pointlist_current;
- --i_tree_pointlist_current;
+ if((*i_tree_pointlist_it)->kappa > minpoint->kappa) minpoint = *i_tree_pointlist_it;
+ --i_tree_pointlist_it;
  }
  bool maxpoint = false;
  
@@ -1585,16 +1586,17 @@ void ImageFinding::IF_routines::refine_crit_in_image(
   
   // find kist of points with negative magnification
   negimage.imagekist->Empty();
-  PointList::iterator i_tree_pointlist_current(grid->i_tree->pointlist->Top());
+  PointList::iterator i_tree_pointlist_it;
+  i_tree_pointlist_it.current = (grid->i_tree->pointlist->Top());
   for(i=0;ii_tree->pointlist->size();++i){
-    x[0] = (*i_tree_pointlist_current)->image->x[0] - x_source[0];
-    x[1] = (*i_tree_pointlist_current)->image->x[1] - x_source[1];
+    x[0] = (*i_tree_pointlist_it)->image->x[0] - x_source[0];
+    x[1] = (*i_tree_pointlist_it)->image->x[1] - x_source[1];
     
-    if( (*i_tree_pointlist_current)->invmag < 0 && r_source*r_source > (x[0]*x[0] + x[1]*x[1]) ){
-      negimage.imagekist->InsertAfterCurrent(*i_tree_pointlist_current);
+    if( (*i_tree_pointlist_it)->invmag < 0 && r_source*r_source > (x[0]*x[0] + x[1]*x[1]) ){
+      negimage.imagekist->InsertAfterCurrent(*i_tree_pointlist_it);
       negimage.imagekist->Down();
     }
-    --i_tree_pointlist_current;
+    --i_tree_pointlist_it;
   }
   
   if(negimage.imagekist->Nunits() == 0) return;
@@ -1637,8 +1639,8 @@ void ImageFinding::IF_routines::refine_crit_in_image(
     newpoint_kist.MoveToTop();
     negimage.imagekist->MoveToBottom();
     do{
-      x[0] = (*i_tree_pointlist_current)->image->x[0] - x_source[0];
-      x[1] = (*i_tree_pointlist_current)->image->x[1] - x_source[1];
+      x[0] = (*i_tree_pointlist_it)->image->x[0] - x_source[0];
+      x[1] = (*i_tree_pointlist_it)->image->x[1] - x_source[1];
       
       if(newpoint_kist.getCurrent()->image->invmag < 0 && r_source*r_source > (x[0]*x[0] + x[1]*x[1]) )
         negimage.imagekist->InsertAfterCurrent(newpoint_kist.getCurrent());
@@ -1692,19 +1694,19 @@ void ImageFinding::IF_routines::refine_crit_in_image(
  // find kist of points with negative magnification
  contour.imagekist->Empty();
  
- PointList::iterator i_tree_pointlist_current(grid->i_tree->pointlist->Top());
- Point *minpoint = *i_tree_pointlist_current;
+ PointList::iterator i_tree_pointlist_it(grid->i_tree->pointlist->Top());
+ Point *minpoint = *i_tree_pointlist_it;
  
  for(i=0;ii_tree->pointlist->size();++i){
- if ((*i_tree_pointlist_current)->kappa>isokappa){
- std::cout << (*i_tree_pointlist_current)->kappa << std::endl;
- contour.imagekist->InsertAfterCurrent(*i_tree_pointlist_current);
+ if ((*i_tree_pointlist_it)->kappa>isokappa){
+ std::cout << (*i_tree_pointlist_it)->kappa << std::endl;
+ contour.imagekist->InsertAfterCurrent(*i_tree_pointlist_it);
  contour.imagekist->Down();
  }
  
  // record point of maximum kappa
- if((*i_tree_pointlist_current)->kappa > minpoint->kappa) minpoint = *i_tree_pointlist_current;
- --i_tree_pointlist_current;
+ if((*i_tree_pointlist_it)->kappa > minpoint->kappa) minpoint = *i_tree_pointlist_it;
+ --i_tree_pointlist_it;
  }
  
  findborders4(grid->i_tree,&critcurve[ii])
@@ -1784,8 +1786,9 @@ void ImageFinding::find_contour(
   
   // find kist of points with negative magnification
   critcurve[0].imagekist->Empty();
-  PointList::iterator i_tree_pointlist_current(grid->i_tree->pointlist->Top());
-  Point *minpoint = *i_tree_pointlist_current;
+  PointList::iterator i_tree_pointlist_it;
+  i_tree_pointlist_it.current = (grid->i_tree->pointlist->Top());
+  Point *minpoint = *i_tree_pointlist_it;
   
   KappaType value,maxval=0;
   
@@ -1793,15 +1796,15 @@ void ImageFinding::find_contour(
     
     switch (contour_type) {
       case KAPPA:
-        value = (*i_tree_pointlist_current)->kappa;
+        value = (*i_tree_pointlist_it)->kappa;
         maxval = minpoint->kappa;
         break;
       case INVMAG:
-        value = (*i_tree_pointlist_current)->invmag;
+        value = (*i_tree_pointlist_it)->invmag;
         maxval = minpoint->invmag;
         break;
       case DT:
-        value = (*i_tree_pointlist_current)->dt;
+        value = (*i_tree_pointlist_it)->dt;
         maxval = minpoint->dt;
         break;
       default:
@@ -1809,13 +1812,13 @@ void ImageFinding::find_contour(
     }
     
     if(value > contour_value){
-      critcurve[0].imagekist->InsertAfterCurrent(*i_tree_pointlist_current);
+      critcurve[0].imagekist->InsertAfterCurrent(*i_tree_pointlist_it);
       critcurve[0].imagekist->Down();
     }
     
     // record point of maximum kappa
-    if(value > maxval) minpoint = *i_tree_pointlist_current;
-    --i_tree_pointlist_current;
+    if(value > maxval) minpoint = *i_tree_pointlist_it;
+    --i_tree_pointlist_it;
   }
   bool maxpoint = false;
   
diff --git a/TreeCode_link/grid_maintenance.cpp b/TreeCode_link/grid_maintenance.cpp
index c01d7335..e9f8fd80 100644
--- a/TreeCode_link/grid_maintenance.cpp
+++ b/TreeCode_link/grid_maintenance.cpp
@@ -249,10 +249,11 @@ void Grid::ReShoot(LensHndl lens){
   s_points = NewPointArray(i_tree->pointlist->size());
   
 	// build new initial grid
-  PointList::iterator i_tree_pointlist_current(i_tree->pointlist->Top());
+  PointList::iterator i_tree_pointlist_it;
+  i_tree_pointlist_it.current = i_tree->pointlist->Top();
   size_t k;
   for(i=0,k=0;ipointlist->size();++i){
-    i_points = *i_tree_pointlist_current;
+    i_points = *i_tree_pointlist_it;
     if(i_points->head > 0){
 
       // link source and image points
@@ -270,7 +271,7 @@ void Grid::ReShoot(LensHndl lens){
       }
     }
     
-    --i_tree_pointlist_current;
+    --i_tree_pointlist_it;
   }
   
   s_tree = new TreeStruct(s_points,s_points->head,1,(i_tree->getTop()->boundary_p2[0] - i_tree->getTop()->boundary_p1[0])/10 );
@@ -291,14 +292,15 @@ void Grid::ReShoot(LensHndl lens){
 PosType Grid::RefreshSurfaceBrightnesses(SourceHndl source){
 	PosType total=0,tmp;
   
-  PointList::iterator s_tree_pointlist_current(s_tree->pointlist->Top());
-	for(unsigned long i=0;ipointlist->size();++i,--s_tree_pointlist_current){
-		tmp = source->SurfaceBrightness((*s_tree_pointlist_current)->x);
-		(*s_tree_pointlist_current)->surface_brightness = (*s_tree_pointlist_current)->image->surface_brightness
+  PointList::iterator s_tree_pointlist_it;
+  s_tree_pointlist_it.current = (s_tree->pointlist->Top());
+	for(unsigned long i=0;ipointlist->size();++i,--s_tree_pointlist_it){
+		tmp = source->SurfaceBrightness((*s_tree_pointlist_it)->x);
+		(*s_tree_pointlist_it)->surface_brightness = (*s_tree_pointlist_it)->image->surface_brightness
     = tmp;
 		total += tmp;//*pow( s_tree->pointlist->current->gridsize,2);
-		assert((*s_tree_pointlist_current)->surface_brightness >= 0.0);
-		(*s_tree_pointlist_current)->in_image = (*s_tree_pointlist_current)->image->in_image
+		assert((*s_tree_pointlist_it)->surface_brightness >= 0.0);
+		(*s_tree_pointlist_it)->in_image = (*s_tree_pointlist_it)->image->in_image
     = NO;
 	}
   
@@ -310,11 +312,12 @@ PosType Grid::RefreshSurfaceBrightnesses(SourceHndl source){
 PosType Grid::ClearSurfaceBrightnesses(){
 	PosType total=0;
   
-  PointList::iterator s_tree_pointlist_current(s_tree->pointlist->Top());
-	for(unsigned long i=0;ipointlist->size();++i,--s_tree_pointlist_current){
-		(*s_tree_pointlist_current)->surface_brightness = (*s_tree_pointlist_current)->image->surface_brightness
+  PointList::iterator s_tree_pointlist_it;
+  s_tree_pointlist_it.current = (s_tree->pointlist->Top());
+	for(unsigned long i=0;ipointlist->size();++i,--s_tree_pointlist_it){
+		(*s_tree_pointlist_it)->surface_brightness = (*s_tree_pointlist_it)->image->surface_brightness
     = 0.0;
-		(*s_tree_pointlist_current)->in_image = (*s_tree_pointlist_current)->image->in_image
+		(*s_tree_pointlist_it)->in_image = (*s_tree_pointlist_it)->image->in_image
     = NO;
 	}
   
@@ -446,24 +449,24 @@ Point * Grid::RefineLeaf(LensHndl lens,Point *point){
 	// re-assign leaf of point that was to be refined
 	assert(inbox(point->x,i_tree->getTop()->boundary_p1,i_tree->getTop()->boundary_p2));
   
-  TreeStruct::iterator i_tree_current(i_tree);
-  i_tree_current = point->leaf;
-	assert(inbox(point->x,(*i_tree_current)->boundary_p1,(*i_tree_current)->boundary_p2));
+  TreeStruct::iterator i_tree_it(i_tree);
+  i_tree_it = point->leaf;
+	assert(inbox(point->x,(*i_tree_it)->boundary_p1,(*i_tree_it)->boundary_p2));
 	// This line should not be necessary!! It is repairing the leaf that has been assigned incorrectly somewhere
-	//if(!inbox(point->x,(*i_tree_current)->boundary_p1,(*i_tree_current)->boundary_p2) ) moveTop(i_tree);
-	i_tree->_FindLeaf(i_tree_current,point->x,0);
-	point->leaf = *i_tree_current;
+	//if(!inbox(point->x,(*i_tree_it)->boundary_p1,(*i_tree_it)->boundary_p2) ) moveTop(i_tree);
+	i_tree->_FindLeaf(i_tree_it,point->x,0);
+	point->leaf = *i_tree_it;
 
 	assert(inbox(point->image->x,s_tree->getTop()->boundary_p1,s_tree->getTop()->boundary_p2));
 	assert(inbox(point->image->x,s_tree->getTop()->boundary_p1,s_tree->getTop()->boundary_p2));
   
-  TreeStruct::iterator s_tree_current(s_tree);
-	s_tree_current = point->image->leaf;
-	assert(inbox(point->image->x,(*s_tree_current)->boundary_p1,(*s_tree_current)->boundary_p2));
+  TreeStruct::iterator s_tree_it(s_tree);
+	s_tree_it = point->image->leaf;
+	assert(inbox(point->image->x,(*s_tree_it)->boundary_p1,(*s_tree_it)->boundary_p2));
 	// This line should not be necessary!! It is repairing the leaf that has been assigned incorrectly somewhere
 	//if(!inbox(point->image->x,s_tree->current->boundary_p1,s_tree->current->boundary_p2) ) moveTop(s_tree);
-	s_tree->_FindLeaf(s_tree_current,point->image->x,0);
-	point->image->leaf = *s_tree_current;
+	s_tree->_FindLeaf(s_tree_it,point->image->x,0);
+	point->image->leaf = *s_tree_it;
 
 	return i_points;
 }
@@ -685,8 +688,8 @@ Point * Grid::RefineLeaves(LensHndl lens,std::vector& points){
     i_tree->AddPointsToTree(i_points,i_points->head);
     s_tree->AddPointsToTree(s_points,s_points->head);
   
-  TreeStruct::iterator i_tree_current(i_tree);
-  TreeStruct::iterator s_tree_current(s_tree);
+  TreeStruct::iterator i_tree_it(i_tree);
+  TreeStruct::iterator s_tree_it(s_tree);
 
     assert(s_points->head > 0);
 
@@ -697,10 +700,10 @@ Point * Grid::RefineLeaves(LensHndl lens,std::vector& points){
         //std::cout << ii << std::endl;
         //i_points[ii].print();
         //i_points[ii].leaf->print();
-        i_tree_current = i_points[ii].leaf;
-        i_tree->_FindLeaf(i_tree_current,i_points[ii].x,0);
-        assert((*i_tree_current)->npoints == 1);
-        i_points[ii].leaf = *i_tree_current;
+        i_tree_it = i_points[ii].leaf;
+        i_tree->_FindLeaf(i_tree_it,i_points[ii].x,0);
+        assert((*i_tree_it)->npoints == 1);
+        i_points[ii].leaf = *i_tree_it;
         assert(i_points[ii].prev != NULL || i_points[ii].next != NULL || s_points->head == 1);
         assert(i_points[ii].image->prev != NULL || i_points[ii].image->next != NULL || s_points->head == 1);
       }
@@ -709,10 +712,10 @@ Point * Grid::RefineLeaves(LensHndl lens,std::vector& points){
         //std::cout << ii << std::endl;
         //s_points[ii].print();
         //s_points[ii].leaf->print();
-        s_tree_current = s_points[ii].leaf;
-        s_tree->_FindLeaf(s_tree_current,s_points[ii].x,0);
-        assert((*s_tree_current)->npoints == 1);
-        s_points[ii].leaf = *s_tree_current;
+        s_tree_it = s_points[ii].leaf;
+        s_tree->_FindLeaf(s_tree_it,s_points[ii].x,0);
+        assert((*s_tree_it)->npoints == 1);
+        s_points[ii].leaf = *s_tree_it;
         assert(s_points[ii].prev != NULL || s_points[ii].next != NULL || s_points->head == 1);
         assert(s_points[ii].image->prev != NULL || s_points[ii].image->next != NULL || s_points->head == 1);
       }
@@ -721,27 +724,27 @@ Point * Grid::RefineLeaves(LensHndl lens,std::vector& points){
   for(ii=0;iileaf->child1 == NULL && points[ii]->leaf->child2 == NULL);
 		if(points[ii]->image->leaf->child1 != NULL || points[ii]->image->leaf->child2 != NULL){
-      s_tree_current = points[ii]->image->leaf;
-      s_tree_current.movetop();
+      s_tree_it = points[ii]->image->leaf;
+      s_tree_it.movetop();
       //assert(inbox(points[ii]->x,s_tree->getTop()->boundary_p1,s_tree->getTop()->boundary_p2));
-      assert(inbox(points[ii]->x,(*s_tree_current)->boundary_p1,(*s_tree_current)->boundary_p2));
-      s_tree->_FindLeaf(s_tree_current,points[ii]->x,0);
-      assert((*s_tree_current)->npoints == 1);
-      points[ii]->image->leaf = *s_tree_current;
+      assert(inbox(points[ii]->x,(*s_tree_it)->boundary_p1,(*s_tree_it)->boundary_p2));
+      s_tree->_FindLeaf(s_tree_it,points[ii]->x,0);
+      assert((*s_tree_it)->npoints == 1);
+      points[ii]->image->leaf = *s_tree_it;
     }
   }
 	//*********************************************************************
 
 	// This loop should not be necessary!! It is repairing the leaf that has been assigned incorrectly somewhere
-	//if(!inbox(point[ii].x,(*i_tree_current)->boundary_p1,(*i_tree_current)->boundary_p2) ) moveTop(i_tree);
+	//if(!inbox(point[ii].x,(*i_tree_it)->boundary_p1,(*i_tree_it)->boundary_p2) ) moveTop(i_tree);
 	/*for(ii=0;ii < Nleaves;++ii){
 
 		// re-assign leaf of point that was to be refined
 		assert(inbox(points[ii]->x,i_tree->getTop()->boundary_p1,i_tree->getTop()->boundary_p2));
-		(*i_tree_current) = points[ii]->leaf;
-		assert(inbox(points[ii]->x,(*i_tree_current)->boundary_p1,(*i_tree_current)->boundary_p2));
+		(*i_tree_it) = points[ii]->leaf;
+		assert(inbox(points[ii]->x,(*i_tree_it)->boundary_p1,(*i_tree_it)->boundary_p2));
 		_FindLeaf(i_tree,points[ii]->x,0);
-		points[ii]->leaf = (*i_tree_current);
+		points[ii]->leaf = (*i_tree_it);
 
 		assert(inbox(points[ii]->image->x,s_tree->getTop()->boundary_p1,s_tree->getTop()->boundary_p2));
 		assert(inbox(points[ii]->image->x,s_tree->getTop()->boundary_p1,s_tree->getTop()->boundary_p2));
@@ -763,11 +766,13 @@ void Grid::ClearAllMarks(){
 	unsigned long i;
 
   if(i_tree->pointlist->size() > 0){
-    PointList::iterator i_tree_pointlist_current(i_tree->pointlist->Top());
+    PointList::iterator i_tree_pointlist_it;
+    i_tree_pointlist_it.current = (i_tree->pointlist->Top());
+    
     for(i=0;ipointlist->size();++i){
-      (*i_tree_pointlist_current)->in_image=NO;
-      (*i_tree_pointlist_current)->image->in_image=NO;
-      --i_tree_pointlist_current;
+      (*i_tree_pointlist_it)->in_image=NO;
+      (*i_tree_pointlist_it)->image->in_image=NO;
+      --i_tree_pointlist_it;
     }
   }
 }
@@ -894,18 +899,19 @@ void Grid::test_mag_matrix(){
 	int count;
 	PosType kappa, gamma[3], invmag;
 
-  PointList::iterator i_tree_pointlist_current(i_tree->pointlist->Top());
+  PointList::iterator i_tree_pointlist_it;
+  i_tree_pointlist_it.current = (i_tree->pointlist->Top());
 	do{
 		count=0;
-		i_tree->FindAllBoxNeighborsKist(*i_tree_pointlist_current,neighbors);
+		i_tree->FindAllBoxNeighborsKist(*i_tree_pointlist_it,neighbors);
 		assert(neighbors->Nunits() >= 3);
 		neighbors->MoveToTop();
 		point2 = neighbors->getCurrent();
 		neighbors->Down();
-		while(!find_mag_matrix(aa,*i_tree_pointlist_current,point2,neighbors->getCurrent())) neighbors->Down();
+		while(!find_mag_matrix(aa,*i_tree_pointlist_it,point2,neighbors->getCurrent())) neighbors->Down();
 		//std::cout << "deflection neighbors a" << std::endl;
 		while(neighbors->Down()){
-			if(find_mag_matrix(ao,*i_tree_pointlist_current,point2,neighbors->getCurrent())){
+			if(find_mag_matrix(ao,*i_tree_pointlist_it,point2,neighbors->getCurrent())){
 				aa[0] = (count*aa[0] + ao[0])/(count+1);
 				aa[1] = (count*aa[1] + ao[1])/(count+1);
 				aa[2] = (count*aa[2] + ao[2])/(count+1);
@@ -921,13 +927,13 @@ void Grid::test_mag_matrix(){
 		gamma[2] = -0.5*(aa[2]-aa[3]);
 		invmag = (1-kappa)*(1-kappa) - gamma[0]*gamma[0] - gamma[1]*gamma[1] + gamma[2]*gamma[2];
 
-		(*i_tree_pointlist_current)->kappa =  kappa/(*i_tree_pointlist_current)->kappa - 1.0;
-		(*i_tree_pointlist_current)->gamma[0] = gamma[0]/(*i_tree_pointlist_current)->gamma[0] - 1.0;
-		(*i_tree_pointlist_current)->gamma[1] = gamma[1]/(*i_tree_pointlist_current)->gamma[1] - 1.0;
-		(*i_tree_pointlist_current)->gamma[2] = gamma[2]/(*i_tree_pointlist_current)->gamma[2] - 1.0;
-		(*i_tree_pointlist_current)->invmag = invmag/(*i_tree_pointlist_current)->invmag - 1.0;
+		(*i_tree_pointlist_it)->kappa =  kappa/(*i_tree_pointlist_it)->kappa - 1.0;
+		(*i_tree_pointlist_it)->gamma[0] = gamma[0]/(*i_tree_pointlist_it)->gamma[0] - 1.0;
+		(*i_tree_pointlist_it)->gamma[1] = gamma[1]/(*i_tree_pointlist_it)->gamma[1] - 1.0;
+		(*i_tree_pointlist_it)->gamma[2] = gamma[2]/(*i_tree_pointlist_it)->gamma[2] - 1.0;
+		(*i_tree_pointlist_it)->invmag = invmag/(*i_tree_pointlist_it)->invmag - 1.0;
 
-	}while(--i_tree_pointlist_current);
+	}while(--i_tree_pointlist_it);
 }
 
 /**
@@ -945,27 +951,27 @@ void Grid::zoom(
 		,Branch *top         /// where on the tree to start, if NULL it will start at the root
 		){
 
-  TreeStruct::iterator i_tree_current(i_tree);
+  TreeStruct::iterator i_tree_it(i_tree);
       
 	if(top==NULL){
 		if(!inbox(center,i_tree->getTop()->boundary_p1,i_tree->getTop()->boundary_p2)) return;
-		i_tree_current.movetop();
+		i_tree_it.movetop();
 	}else{
 		if(!inbox(center,top->boundary_p1,top->boundary_p2)) return;
-		i_tree_current = top;
+		i_tree_it = top;
 	}
 	Branch *tmp=NULL;
 	Point *newpoints;
 
-	i_tree->_FindLeaf(i_tree_current,center,0);
-	while((*i_tree_current)->points->gridsize > min_scale ){
-		tmp = *i_tree_current;
-		newpoints = RefineLeaf(lens,(*i_tree_current)->points);
+	i_tree->_FindLeaf(i_tree_it,center,0);
+	while((*i_tree_it)->points->gridsize > min_scale ){
+		tmp = *i_tree_it;
+		newpoints = RefineLeaf(lens,(*i_tree_it)->points);
 		if(newpoints == NULL) break;  // case where all the new points where outside the region
-		i_tree_current = tmp;
-		assert(inbox(center,(*i_tree_current)->boundary_p1,(*i_tree_current)->boundary_p2));
-		i_tree->_FindLeaf(i_tree_current,center,0);
-		assert((*i_tree_current)->npoints == 1);
+		i_tree_it = tmp;
+		assert(inbox(center,(*i_tree_it)->boundary_p1,(*i_tree_it)->boundary_p2));
+		i_tree->_FindLeaf(i_tree_it,center,0);
+		assert((*i_tree_it)->npoints == 1);
 	};
 
 	return;
@@ -1151,18 +1157,18 @@ PixelMap Grid::writePixelMapUniform(
   std::vector lists(Nblocks);
   
   bool allowDecent;
-  TreeStruct::iterator i_tree_current(i_tree);
+  TreeStruct::iterator i_tree_it(i_tree);
   int i = 0;
   do{
-    if((*i_tree_current)->level == 4){
-      lists[i].setTop( (*i_tree_current)->points );
-      lists[i].setN( (*i_tree_current)->npoints );
+    if((*i_tree_it)->level == 4){
+      lists[i].setTop( (*i_tree_it)->points );
+      lists[i].setN( (*i_tree_it)->npoints );
       ++i;
       allowDecent = false;
     }else{
       allowDecent = true;
     }
-  }while(i_tree_current.TreeWalkStep(allowDecent) && i < Nblocks);
+  }while(i_tree_it.TreeWalkStep(allowDecent) && i < Nblocks);
   
   std::vector thrs;
   
@@ -1183,23 +1189,23 @@ void Grid::writePixelMapUniform(
   map.Clean();
   int Nblocks = 16;
   std::vector lists(Nblocks);
-  TreeStruct::iterator i_tree_current(i_tree);
+  TreeStruct::iterator i_tree_it(i_tree);
 
   
   bool allowDecent;
-  i_tree_current.movetop();
+  i_tree_it.movetop();
   int i = 0;
   do{
-    if((*i_tree_current)->level == 4){
+    if((*i_tree_it)->level == 4){
       assert(i < 16);
-      lists[i].setTop( (*i_tree_current)->points );
-      lists[i].setN( (*i_tree_current)->npoints );
+      lists[i].setTop( (*i_tree_it)->points );
+      lists[i].setN( (*i_tree_it)->npoints );
       ++i;
       allowDecent = false;
     }else{
       allowDecent = true;
     }
-  }while(i_tree_current.TreeWalkStep(allowDecent) && i < Nblocks);
+  }while(i_tree_it.TreeWalkStep(allowDecent) && i < Nblocks);
   
   std::thread thr[16];
   
@@ -1215,42 +1221,43 @@ void Grid::writePixelMapUniform_(const PointList &list,PixelMap *map,LensingVari
   PosType tmp2[2];
   long index;
   
-  PointList::iterator list_current(list.Top());
+  PointList::iterator list_it;
+  list_it.current = (list.Top());
   for(size_t i = 0; i< list.size(); ++i){
     switch (val) {
       case ALPHA:
-        tmp2[0] = (*list_current)->x[0] - (*list_current)->image->x[0];
-        tmp2[1] = (*list_current)->x[1] - (*list_current)->image->x[1];
+        tmp2[0] = (*list_it)->x[0] - (*list_it)->image->x[0];
+        tmp2[1] = (*list_it)->x[1] - (*list_it)->image->x[1];
         tmp = sqrt(tmp2[0]*tmp2[0] + tmp2[1]*tmp2[1]);
         break;
       case ALPHA1:
-        tmp = ((*list_current)->x[0] - (*list_current)->image->x[0]);
+        tmp = ((*list_it)->x[0] - (*list_it)->image->x[0]);
         break;
       case ALPHA2:
-        tmp = ((*list_current)->x[1] - (*list_current)->image->x[1]);
+        tmp = ((*list_it)->x[1] - (*list_it)->image->x[1]);
         break;
       case KAPPA:
-        tmp = (*list_current)->kappa;
+        tmp = (*list_it)->kappa;
         break;
       case GAMMA:
-        tmp2[0] = (*list_current)->gamma[0];
-        tmp2[1] = (*list_current)->gamma[1];
+        tmp2[0] = (*list_it)->gamma[0];
+        tmp2[1] = (*list_it)->gamma[1];
         tmp = sqrt(tmp2[0]*tmp2[0] + tmp2[1]*tmp2[1]);
         break;
       case GAMMA1:
-        tmp = (*list_current)->gamma[0];
+        tmp = (*list_it)->gamma[0];
         break;
       case GAMMA2:
-        tmp = (*list_current)->gamma[1];
+        tmp = (*list_it)->gamma[1];
         break;
       case GAMMA3:
-        tmp = (*list_current)->gamma[2];
+        tmp = (*list_it)->gamma[2];
         break;
       case INVMAG:
-        tmp = (*list_current)->invmag;
+        tmp = (*list_it)->invmag;
         break;
       case DT:
-        tmp = (*list_current)->dt;
+        tmp = (*list_it)->dt;
         break;
       default:
         std::cerr << "PixelMap::AddGrid() does not work for the input LensingVariable" << std::endl;
@@ -1259,10 +1266,10 @@ void Grid::writePixelMapUniform_(const PointList &list,PixelMap *map,LensingVari
         // If this list is to be expanded to include ALPHA or GAMMA take care to add them as vectors
     }
     
-    index = map->find_index((*list_current)->x);
+    index = map->find_index((*list_it)->x);
     if(index != -1)(*map)[index] = tmp;
     
-    --list_current;
+    --list_it;
   }
 }
 
diff --git a/TreeCode_link/gridmap.cpp b/TreeCode_link/gridmap.cpp
index 1459d1fd..47d7c413 100644
--- a/TreeCode_link/gridmap.cpp
+++ b/TreeCode_link/gridmap.cpp
@@ -160,8 +160,6 @@ void GridMap::getPixelMap(PixelMap &map){
   if(resf*map.getNy() != Ngrid_init2-1+resf) throw std::invalid_argument("PixelMap does not match GripMap! Use the other GridMap::getPixelMap() to contruct a PixelMap.");
   if(map.getResolution() != x_range*resf/(Ngrid_init-1)) throw std::invalid_argument("PixelMap does not match GripMap resolution! Use the other GridMap::getPixelMap() to contruct a PixelMap.");
   
-  size_t N = Ngrid_init*Ngrid_init2;
-
   if(map.getCenter()[0] != center[0]) throw std::invalid_argument("PixelMap does not match GripMap!");
   if(map.getCenter()[1] != center[1]) throw std::invalid_argument("PixelMap does not match GripMap!");
   
diff --git a/TreeCode_link/peak_refinement.cpp b/TreeCode_link/peak_refinement.cpp
index 81a63a9c..b0ac94a4 100644
--- a/TreeCode_link/peak_refinement.cpp
+++ b/TreeCode_link/peak_refinement.cpp
@@ -46,7 +46,8 @@ short find_peaks(
 
 	// Add all points to imageinfo
 	//MoveToTopList(grid->i_tree->pointlist);
-      PointList::iterator i_tree_pl_current(grid->i_tree->pointlist->Top());
+      PointList::iterator i_tree_pl_current;
+      i_tree_pl_current.current = (grid->i_tree->pointlist->Top());
 	do{
 		imageinfo[0].imagekist->InsertAfterCurrent(*i_tree_pl_current);
 	}while(--i_tree_pl_current);
diff --git a/TreeCode_link/tree_maintenance.cpp b/TreeCode_link/tree_maintenance.cpp
index f4354d73..e9e2b315 100644
--- a/TreeCode_link/tree_maintenance.cpp
+++ b/TreeCode_link/tree_maintenance.cpp
@@ -269,7 +269,7 @@ short TreeStruct::emptyTree(){
 
   assert(Nbranches == 1);
 
-  PointList::iterator pointlist_current(*pointlist);
+  PointList::iterator pointlist_current(pointlist->Top());
   for(i=0,j=0,count=0;isize();++i){
 	  if((*pointlist_current)->head){
 		  heads[j] = *pointlist_current;
@@ -365,7 +365,7 @@ void TreeStruct::_freeBranches_iter(){
 	}
 
 	// re-assign the leaf pointers in the particles to the root
-  PointList::iterator pointlist_current(*pointlist);
+  PointList::iterator pointlist_current(pointlist->Top());
 	 do{ (*pointlist_current)->leaf = top; }while(--pointlist_current);
 
     return;
diff --git a/include/Tree.h b/include/Tree.h
index c00d80fc..20eda943 100644
--- a/include/Tree.h
+++ b/include/Tree.h
@@ -105,12 +105,12 @@ struct TreeStruct{
     std::vector tmp_point;
   };
   
-  iterator begin() const{
+  TreeStruct::iterator begin() const{
     iterator it(top);
     return it;
   };
   
-  iterator end() const{
+  TreeStruct::iterator end() const{
     iterator it(top->brother);
     return it;
   };
diff --git a/include/image_processing.h b/include/image_processing.h
index 3c177d8d..baec5566 100644
--- a/include/image_processing.h
+++ b/include/image_processing.h
@@ -47,13 +47,15 @@ class PixelMap
 	inline std::size_t getNy() const { return Ny; }
 	inline double getRangeX() const { return rangeX; }
 	inline double getRangeY() const { return rangeY; }
-	inline const double* getCenter() const{ return center; }
+  inline const double* getCenter() const{ return center; }
+  void const getCenter(Point_2d &c) const{ c[0]=center[0]; c[1]=center[1];}
 	inline double getResolution() const { return resolution; }
 	
 	void Clean();
 
   void AddImages(ImageInfo *imageinfo,int Nimages,float rescale = 1.);
   void AddImages(std::vector &imageinfo,int Nimages,float rescale = 1.);
+  void AddGridBrightness(Grid &grid);
   void AddUniformImages(ImageInfo *imageinfo,int Nimages,double value);
   PosType AddSource(Source &source);
   /// Add a source to the pixel map by oversamples the source so that oversample^2 points within each pixel are averaged
diff --git a/include/lens.h b/include/lens.h
index ae5324ed..dfedb3f8 100644
--- a/include/lens.h
+++ b/include/lens.h
@@ -133,13 +133,13 @@ class Lens
 
 
 	/// inserts a single main lens halo and adds it to the existing ones
-  void insertMainHalo(LensHalo* halo,PosType zlens, bool addplanes,bool verbose);
+  void insertMainHalo(LensHalo* halo,PosType zlens, bool addplanes,bool verbose = false);
 
 	/// inserts a sequence of main lens halos and adds them to the existing ones
 	void insertMainHalos(LensHalo** halos, std::size_t Nhalos,bool addplanes,bool verbose = false);
 
 	/// replaces existing main halos with a single main halo
-  void replaceMainHalo(LensHalo* halo,PosType zlens, bool addplanes,bool verbose);
+  void replaceMainHalo(LensHalo* halo,PosType zlens, bool addplanes,bool verbose = false);
 
 	/// replaces existing main halos with a sequence of main halos
 	void replaceMainHalos(LensHalo** halos, std::size_t Nhalos,bool verbose = false);
diff --git a/include/point.h b/include/point.h
index bbfcea72..51dfd6c8 100644
--- a/include/point.h
+++ b/include/point.h
@@ -268,37 +268,37 @@ struct PointList{
   }
   ~PointList(){EmptyList();}
   
-  class iterator{
-  private:
+  struct iterator{
+
     Point *current;
-  public:
     
-    iterator(){
-      current = NULL;
-    }
-    iterator(PointList &list){
+    iterator():current(NULL){ }
+    
+/*    iterator(PointList &list){
       current = list.top;
     }
     iterator(iterator &it){
       current = *it;
     }
+ */
     iterator(Point *p){
       current = p;
     }
     
-    Point * operator*(){return current;}
-    
-    PointList::iterator &operator=(PointList::iterator &p){
-      if(&p == this) return *this;
-      current = p.current;
-      return *this;
-    }
-    
+ /*
     PointList::iterator &operator=(Point *point){
       current = point;
       return *this;
     }
+*/
+    Point *operator*(){return current;}
     
+    /*iterator &operator=(iterator &p){
+      if(&p == this) return *this;
+      current = p.current;
+      return *this;
+    }*/
+
     bool operator++(){
       assert(current);
       if(current->prev == NULL) return false;
@@ -357,6 +357,21 @@ struct PointList{
     return *it == bottom;
   };
   
+  /* Many changes need to be made to implement this correctly
+  PointList::iterator begin() const{
+    PointList::iterator it;
+    it.current = bottom;
+    return it;
+  }
+
+  PointList::iterator end() const{
+    PointList::iterator it;
+    it.current = top->prev;
+    return it;
+  }
+  */
+  
+  
   Point *Top() const {return top;}
   Point *Bottom() const {return bottom;}
   

From 7b61faa6b414b9bbb7cc23641c2417f678a0395b Mon Sep 17 00:00:00 2001
From: RB Metcalf 
Date: Mon, 5 Jun 2017 15:43:31 +0200
Subject: [PATCH 024/227] Added LensHaloParticle::makeSIE() to make test SIEs
 out of particles.

---
 MultiPlane/particle_lens.cpp | 39 ++++++++++++++++++++++++++++++++++++
 include/particle_halo.h      | 13 ++++++++++++
 2 files changed, 52 insertions(+)

diff --git a/MultiPlane/particle_lens.cpp b/MultiPlane/particle_lens.cpp
index 2a54fdb0..1d064088 100644
--- a/MultiPlane/particle_lens.cpp
+++ b/MultiPlane/particle_lens.cpp
@@ -375,3 +375,42 @@ void LensHaloParticles::writeSizes(const std::string &filename,int Nsmooth){
     throw std::runtime_error("file writing error");
   }
 }
+
+void LensHaloParticles::makeSIE(
+                                std::string new_filename  /// file name
+                                ,PosType redshift     /// redshift of particles
+                                ,double particle_mass /// particle mass
+                                ,double total_mass  /// total mass of SIE
+                                ,double sigma       /// velocity dispersion in km/s
+                                ,double q  /// axis ratio
+                                ,Utilities::RandomNumbers_NR &ran
+                                ){
+  
+  size_t Npoints = total_mass/particle_mass;
+  PosType Rmax = (1+redshift)*total_mass*Grav*lightspeed*lightspeed/sigma/sigma/2;
+  Point_3d point;
+  double qq = sqrt(q);
+  
+  std::ofstream datafile;
+  datafile.open(new_filename);
+  
+  datafile << "# nparticles " << Npoints << std::endl;
+  datafile << "# mass " << particle_mass << std::endl;
+  // create particles
+  for(size_t i=0; i< Npoints ;++i){
+    point[0] = ran.gauss();
+    point[1] = ran.gauss();
+    point[2] = ran.gauss();
+    
+    point *= Rmax*ran()/point.length();
+    
+    point[0] *= qq;
+    point[1] /= qq;
+    
+    datafile << point[0] << " " << point[1] << " "
+    << point[2] << " " << std::endl;
+    
+  }
+  
+  datafile.close();
+}
\ No newline at end of file
diff --git a/include/particle_halo.h b/include/particle_halo.h
index a4342dbc..66c4dbf0 100644
--- a/include/particle_halo.h
+++ b/include/particle_halo.h
@@ -53,6 +53,19 @@ class LensHaloParticles : public LensHalo
   
   /// center of mass in input coordinates
   Point_3d CenterOfMass(){return mcenter;}
+  /** \brief This is a test class that makes a truncated SIE out of particles and puts it into a file in the right format for constructing a LensHaloParticles.
+   
+   This is useful for calculating the level of shot noise and finite source size.
+   */
+  static void makeSIE(
+                      std::string new_filename  /// file name to store the particles
+                      ,PosType redshift     /// redshift of particles
+                      ,double particle_mass /// particle mass
+                      ,double total_mass  /// total mass of SIE
+                      ,double sigma       /// velocity dispersion in km/s
+                      ,double q  /// axis ratio
+                      ,Utilities::RandomNumbers_NR &ran
+                      );
   
 private:
 

From aeb86e09dfc19baecfcc851fdbc0d126ac74b798 Mon Sep 17 00:00:00 2001
From: RB Metcalf 
Date: Mon, 5 Jun 2017 16:12:19 +0200
Subject: [PATCH 025/227] Added a more easy to use constructor for
 LensHaloUniform

---
 AnalyticNSIE/readfiles_uni.cpp | 19 +++++++++++++++++++
 include/uniform_lens.h         | 16 +++++++++++++++-
 2 files changed, 34 insertions(+), 1 deletion(-)

diff --git a/AnalyticNSIE/readfiles_uni.cpp b/AnalyticNSIE/readfiles_uni.cpp
index 38773001..c8654c8e 100644
--- a/AnalyticNSIE/readfiles_uni.cpp
+++ b/AnalyticNSIE/readfiles_uni.cpp
@@ -50,6 +50,25 @@ LensHaloUniform::LensHaloUniform(InputParams& params, bool verbose): LensHalo(){
   if(verbose) PrintLens(false,false);
 }
 
+LensHaloUniform::LensHaloUniform(double zlens,double zsource,double kappa,Point_2d &gamma,COSMOLOGY &cosmo): LensHalo(zlens,cosmo),kappa_uniform(kappa),zsource_reference(zsource){
+  
+  gamma_uniform[0] = gamma[0];
+  gamma_uniform[1] = gamma[1];
+  gamma_uniform[2] = 0;
+
+  stars_N = 0;
+  
+  if(std::numeric_limits::has_infinity){
+    Rmax = std::numeric_limits::infinity();
+  }else{
+    Rmax = std::numeric_limits::max();
+  }
+  perturb_Nmodes=3;
+  perturb_modes = new PosType[3];
+  
+  setCosmology(cosmo);
+}
+
 LensHaloUniform::~LensHaloUniform(){
 
 }
diff --git a/include/uniform_lens.h b/include/uniform_lens.h
index 2059405b..0cde002c 100644
--- a/include/uniform_lens.h
+++ b/include/uniform_lens.h
@@ -15,7 +15,12 @@
  *
  *
  *
- * Input Parameters:
+ *
+ *  Warning:Once this lens is fully constructed it is independent of the source redshift so
+ *  if the source redshift is change through the Lens class the input kappa and gamma values will
+ *  longer be the correct ones because of the change in the critical density.
+ *
+ * Input Parameters in parameter file:
  *
  *  ****  Uniform model parameters
  *	kappa_uniform
@@ -23,6 +28,8 @@
  *  gamma_uniform_2
  *
  *
+ *
+ *
  *  **** Stars parameters
  *	main_stars_N                 Total number of stars that will be used in the simulation.  If zero the other star parameters are not needed.
  *	main_stars_fraction                 Fraction of surface denity in stars.
@@ -50,6 +57,13 @@ class LensHaloUniform: public LensHalo{
 public:
 	LensHaloUniform(InputParams& params, const COSMOLOGY& cosmo, bool verbose = false);
 	LensHaloUniform(InputParams& params,bool verbose =false);
+  /// Direct constructor without any stars
+  LensHaloUniform(double zlens   /// lens redshift
+                  ,double zsource /// source redshift
+                  ,double kappa  /// convergence
+                  ,Point_2d &gamma /// shear
+                  ,COSMOLOGY &cosmo  /// cosmology to translate into surface density
+                  );
 	~LensHaloUniform();
 
 	void assignParams(InputParams& params);

From a563a98bcc7376b3a6664ce4c8ea5ace340f4e9b Mon Sep 17 00:00:00 2001
From: RB Metcalf 
Date: Sun, 11 Jun 2017 15:46:16 +0200
Subject: [PATCH 026/227] Converted the invention from comoving distance to
 redshift in MOKAfits.cpp to standard COMOLOGY method. Requires updating
 CosmoLib.

---
 MultiPlane/MOKAfits.cpp | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/MultiPlane/MOKAfits.cpp b/MultiPlane/MOKAfits.cpp
index ca8fd4cd..d9fb68fc 100644
--- a/MultiPlane/MOKAfits.cpp
+++ b/MultiPlane/MOKAfits.cpp
@@ -220,7 +220,7 @@ void LensHaloMassMap::readMap(){
           }
         }
         if(d2 != 0 ){
-          double dll = ( d1 + d2 )*0.5; // comoving dists
+          /*double dll = ( d1 + d2 )*0.5; // comoving dists
           
           std:: vector zi;
           int ni=2048;
@@ -235,8 +235,10 @@ void LensHaloMassMap::readMap(){
           
           // set the redshift of the plane half distance between
           // d1 and d2
-          
           map->zlens = Utilities::InterpolateYvec(dli,zi,dll);
+          */
+          
+          map->zlens = cosmo.invComovingDist(( d1 + d2 )*0.5);
           
         }else{
           

From 7076445abceb39db6cc4f244a397dee91802ce0d Mon Sep 17 00:00:00 2001
From: RB Metcalf 
Date: Thu, 6 Jul 2017 14:26:34 -0400
Subject: [PATCH 027/227] Comments

---
 include/particle_halo.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/include/particle_halo.h b/include/particle_halo.h
index 66c4dbf0..a23f0f61 100644
--- a/include/particle_halo.h
+++ b/include/particle_halo.h
@@ -55,7 +55,7 @@ class LensHaloParticles : public LensHalo
   Point_3d CenterOfMass(){return mcenter;}
   /** \brief This is a test class that makes a truncated SIE out of particles and puts it into a file in the right format for constructing a LensHaloParticles.
    
-   This is useful for calculating the level of shot noise and finite source size.
+   This is useful for calculating the level of shot noise and finite source size.  The particles are distributed in 3D according to the SIE profile with only the perpendicular coordinates (1st and 2nd) distorted into an elliptical shape. If the halo is rotated from the original orientation it will not be a oblate spheroid.
    */
   static void makeSIE(
                       std::string new_filename  /// file name to store the particles

From 3f824d1f2964b4d534d2e0257ecc34b6a3dd5ed3 Mon Sep 17 00:00:00 2001
From: Nicolas Tessore 
Date: Fri, 7 Jul 2017 11:48:43 +0100
Subject: [PATCH 028/227] support for HDF5 library

---
 CMakeLists.txt | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 2f9c62b1..0aa43a79 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -79,6 +79,11 @@ if(ENABLE_HEALPIX)
 	include_directories(${HEALPIX_INCLUDE_DIRS})
 endif()
 
+if(ENABLE_HDF5)
+	include_directories(${HDF5_INCLUDE_DIRS})
+	add_definitions(${HDF5_DEFINITIONS})
+endif()
+
 include_directories(
 	"${NR_SOURCE_DIR}/include"
 	"${CosmoLib_SOURCE_DIR}/include"

From 813e7b78f4e2a971ac70752fbdccb8b26b6e9e98 Mon Sep 17 00:00:00 2001
From: RB Metcalf 
Date: Tue, 18 Jul 2017 14:23:02 +0200
Subject: [PATCH 029/227] Fixed bug in constructor of LenshaloNFW where it
 would give the halo zero mass.

---
 MultiPlane/lens_halos.cpp | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/MultiPlane/lens_halos.cpp b/MultiPlane/lens_halos.cpp
index 4509b515..7ef30b89 100644
--- a/MultiPlane/lens_halos.cpp
+++ b/MultiPlane/lens_halos.cpp
@@ -143,7 +143,12 @@ LensHaloNFW::LensHaloNFW()
 }
 
 LensHaloNFW::LensHaloNFW(float my_mass,float my_Rsize,PosType my_zlens,float my_concentration,float my_fratio,float my_pa,int my_stars_N, EllipMethod my_ellip_method){
+
   LensHalo::setRsize(my_Rsize);
+  LensHalo::setMass(my_mass);
+  LensHalo::setZlens(my_zlens);
+
+
   fratio=my_fratio, pa=my_pa, stars_N=my_stars_N, main_ellip_method=my_ellip_method;
   stars_implanted = false;
   rscale = LensHalo::getRsize()/my_concentration;

From bfc12ad53e08313ea31328c2e7794ad6fedd586a Mon Sep 17 00:00:00 2001
From: RB Metcalf 
Date: Sat, 12 Aug 2017 17:22:51 +0200
Subject: [PATCH 030/227] Added a function Lens::GenerateFieldHalos() so that
 the light cone can be filled with a realisation of the expected halos.

---
 MultiPlane/lens.cpp     | 48 +++++++++++++++++++++++++++++++++--------
 TreeCode/qTreeNB.cpp    |  1 +
 TreeCode/quadTree.cpp   | 20 +++++++++++------
 include/analytic_lens.h |  6 ++++++
 include/base_analens.h  |  2 ++
 include/lens.h          | 35 ++++++++++++++++++++++--------
 include/lens_halos.h    |  7 +++---
 include/point.h         |  4 ++++
 8 files changed, 95 insertions(+), 28 deletions(-)

diff --git a/MultiPlane/lens.cpp b/MultiPlane/lens.cpp
index 6b9abf4a..154061c9 100644
--- a/MultiPlane/lens.cpp
+++ b/MultiPlane/lens.cpp
@@ -59,7 +59,7 @@ Lens::Lens(long* my_seed,PosType z_source, CosmoParamSet cosmoset,bool verbose)
   combinePlanes(verbose);
   if(zsource != ztmp) ResetSourcePlane(ztmp,false);
 
-  std::cout << "number of field halos :" << field_halos.size() << std::endl;
+  //std::cout << "number of field halos :" << field_halos.size() << std::endl;
 }
 
 Lens::Lens(long* my_seed,PosType z_source, const COSMOLOGY &cosmoset,bool verbose)
@@ -85,7 +85,7 @@ Lens::Lens(long* my_seed,PosType z_source, const COSMOLOGY &cosmoset,bool verbos
   PosType ztmp = zsource;
   combinePlanes(true);
   if(zsource != ztmp) ResetSourcePlane(ztmp,false);
-  std::cout << "number of field halos :" << field_halos.size() << std::endl;
+  //std::cout << "number of field halos :" << field_halos.size() << std::endl;
 }
 
 /**
@@ -856,14 +856,8 @@ void Lens::createFieldPlanes(bool verbose)
 			     
       assert( field_Dl[i] > 0 );
 			// convert to proper distance on the lens plane
-      //field_halos[j]->getX(tmp);
-      //field_halos[j]->setX(tmp[0]*field_Dl[i]/(1+field_plane_redshifts[i])
-      //                     ,tmp[1]*field_Dl[i]/(1+field_plane_redshifts[i]));
-      //field_halos[j]->setDist(field_Dl[i]/(1+field_plane_redshifts[i]));
       
       field_halos[j]->setZlensDist(field_plane_redshifts[i],cosmo);
-      //halo_pos[j][0] *= field_Dl[i]/(1+field_plane_redshifts[i]);
-			//halo_pos[j][1] *= field_Dl[i]/(1+field_plane_redshifts[i]);
 		}
 		
 		//max_r=sqrt(max_r);
@@ -1707,7 +1701,7 @@ void Lens::createFieldHalos(bool verbose)
 			maxr = pi*sqrt(fieldofview/pi)/180. + field_buffer/Ds; // fov is a circle
 			rr = maxr*sqrt(ran2(seed));
       
-      if(verbose) std::cout << "          maxr = " << maxr << std::endl;
+      //if(verbose) std::cout << "          maxr = " << maxr << std::endl;
 
 			assert(rr == rr);
       
@@ -2965,6 +2959,42 @@ void Lens::buildPlanes(InputParams& params, bool verbose)
 	combinePlanes(verbose);
 }
 
+void Lens::GenerateFieldHalos(double min_mass
+                         ,MassFuncType mass_function
+                         ,double field_of_view
+                         ,int Nplanes
+                         ,LensHaloType halo_type
+                         ,bool verbose
+                         ){
+
+  if(field_halos.size() >0 ){
+    std::cerr << "Lens:GenerateField() cannot be used in the lens already has field halos." << std::endl;
+    throw std::runtime_error("Field halos already exist.");
+  }
+  field_min_mass = min_mass;
+  fieldofview = field_of_view;
+  field_mass_func_type = mass_function;
+
+  field_buffer = 0.0;
+
+  mass_func_PL_slope = 0;
+  flag_field_gal_on = false;
+  field_int_prof_type = halo_type;
+  field_int_prof_gal_type = nsie_gal;
+
+  NhalosbinZ.resize(Nzbins);
+  Nhaloestot_Tab.resize(NZSamples);
+  ComputeHalosDistributionVariables();
+  
+  field_Nplanes_original = field_Nplanes_current = Nplanes;
+  setupFieldPlanes();
+
+  //resetFieldHalos();
+  createFieldHalos(verbose);
+  createFieldPlanes(verbose);
+  combinePlanes(verbose);
+}
+
 /**
  * \brief Changes the maximum redshift that the rays are shot to. Warning: Grids that have already been made with this Lens will not have this new source redshift. 
  *
diff --git a/TreeCode/qTreeNB.cpp b/TreeCode/qTreeNB.cpp
index 6cd353d2..afa1fbea 100644
--- a/TreeCode/qTreeNB.cpp
+++ b/TreeCode/qTreeNB.cpp
@@ -62,6 +62,7 @@ QTreeNB::~QTreeNB(){
 
 short QTreeNB::empty(){
 
+  if(Nbranches <= 1) return;
 	moveTop();
 	_freeQTree(0);
 
diff --git a/TreeCode/quadTree.cpp b/TreeCode/quadTree.cpp
index 5002df48..0f0d5aca 100644
--- a/TreeCode/quadTree.cpp
+++ b/TreeCode/quadTree.cpp
@@ -75,9 +75,11 @@ MultiMass(true),MultiRadius(true),masses(NULL),sizes(NULL)
 
 	haloON = true;  //use internal halo parameters
 
-	tree = BuildQTreeNB(xp,Npoints,index);
+  if(Npoints > 0){
+    tree = BuildQTreeNB(xp,Npoints,index);
 
-	CalcMoments();
+    CalcMoments();
+  }
 
   phiintconst = (120*log(2.) - 631.)/840 + 19./70;
 	return;
@@ -86,6 +88,7 @@ MultiMass(true),MultiRadius(true),masses(NULL),sizes(NULL)
 /// Particle positions and other data are not destroyed.
 TreeQuad::~TreeQuad()
 {
+  if(Nparticles == 0) return;
 	delete tree;
 	delete[] index;
   if(haloON) Utilities::free_PosTypeMatrix(xp,Nparticles,2);
@@ -494,12 +497,15 @@ void TreeQuad::rotate_coordinates(PosType **coord){
 
 void TreeQuad::force2D(const PosType *ray,PosType *alpha,KappaType *kappa,KappaType *gamma,KappaType *phi) const{
   
+  alpha[0]=alpha[1]=gamma[0]=gamma[1]=gamma[2]=0.0;
+  *kappa=*phi=0.0;
+  
+  if(Nparticles == 0) return;
+
   assert(tree);
   QTreeNB::iterator it(tree);
   
-  alpha[0]=alpha[1]=gamma[0]=gamma[1]=gamma[2]=0.0;
-  *kappa=*phi=0.0;
-    
+  
   if(periodic_buffer){
     PosType tmp_ray[2];
         
@@ -777,11 +783,13 @@ void TreeQuad::walkTree_iter(
 
 void TreeQuad::force2D_recur(const PosType *ray,PosType *alpha,KappaType *kappa,KappaType *gamma,KappaType *phi){
   
-  assert(tree);
   
   alpha[0]=alpha[1]=gamma[0]=gamma[1]=gamma[2]=0.0;
   *kappa=*phi=0.0;
   
+  if(Nparticles == 0) return;
+  assert(tree);
+  
   //walkTree_recur(tree->top,ray,&alpha[0],kappa,&gamma[0],phi);
   
   if(periodic_buffer){
diff --git a/include/analytic_lens.h b/include/analytic_lens.h
index a35e648d..2be3df00 100644
--- a/include/analytic_lens.h
+++ b/include/analytic_lens.h
@@ -18,6 +18,9 @@
  */
 // TODO: BEN finish this documentation for perturbation parameters.
 class LensHaloFit : public LensHaloBaseNSIE{
+  
+  void abstractfunction(){}; // pure virtual
+  
 public:
   //LensHaloAnaNSIE(InputParams& params,bool verbose = false);
   /// Creates a AnaLens which initially has no mass,  Use FindLensSimple() to give it mass
@@ -128,6 +131,9 @@ class LensHaloFit : public LensHaloBaseNSIE{
  */
 // TODO: BEN finish this documentation for perturbation parameters.
 class LensHaloAnaNSIE : public LensHaloBaseNSIE{
+  
+  void abstractfunction(){}; // pure virtual
+
 public:
   LensHaloAnaNSIE(InputParams& params,bool verbose = false);
   /// Creates a AnaLens which initially has no mass,  Use FindLensSimple() to give it mass
diff --git a/include/base_analens.h b/include/base_analens.h
index f2dde1d3..1dd935f4 100644
--- a/include/base_analens.h
+++ b/include/base_analens.h
@@ -54,6 +54,8 @@ class LensHaloBaseNSIE : public LensHalo{
 public:
 	LensHaloBaseNSIE(InputParams& params);
   LensHaloBaseNSIE();
+  
+  virtual void abstractfunction() = 0; // pure virtual
 
   virtual ~LensHaloBaseNSIE();
 
diff --git a/include/lens.h b/include/lens.h
index dfedb3f8..bbcf52e1 100644
--- a/include/lens.h
+++ b/include/lens.h
@@ -85,10 +85,10 @@ class Lens
 	bool set;
 
 	/// the total number of lens planes
-	int getNplanes(){return lensing_planes.size();}
+	int getNplanes() const {return lensing_planes.size();}
   
 	/// field of view in square degrees
-	PosType getfov(){return fieldofview;};
+	PosType getfov() const {return fieldofview;};
 	void setfov(PosType fov){fieldofview=fov;};
 
 	/// reset the number of planes, but keep the field halos and main lens
@@ -105,7 +105,7 @@ class Lens
 
   
   /// Redshift of first main lens plane
-	PosType getZlens(){
+	PosType getZlens() const{
 		if(flag_switch_main_halo_on)
 			return main_halos[0]->getZlens();
 		else{
@@ -115,7 +115,7 @@ class Lens
 		}
 	}
   /// Angular size distance (Mpc) to first main lens plane
-	PosType getAngDistLens(){
+	PosType getAngDistLens() const{
 		if(flag_switch_main_halo_on)
 			return cosmo.angDist( main_halos[0]->getZlens());
 		else{
@@ -124,6 +124,8 @@ class Lens
 			exit(1);
 		}
 	}
+  
+  Utilities::Geometry::SphericalPoint getCenter() const {return central_point_sphere;}
 
 	/// remove all main halos
 	void clearMainHalos(bool verbose=false);
@@ -198,7 +200,8 @@ class Lens
 
 	// methods used for use with implanted sources
 
-	short ResetSourcePlane(PosType z,bool nearest, unsigned long GalID=0, PosType *xx=NULL,bool verbose = false);
+  ///  reset the redshift of the source plane
+	short ResetSourcePlane(PosType z,bool nearest=false, unsigned long GalID=0, PosType *xx=NULL,bool verbose = false);
 
 	/// Revert the source redshift to the value it was when the Lens was created.
 	void RevertSourcePlane(){ toggle_source_plane = false;}
@@ -211,13 +214,13 @@ class Lens
 		}
 	}
 
-	PosType getZmax(){return plane_redshifts.back();}
+	PosType getZmax() const{return plane_redshifts.back();}
 
 	/// print the cosmological parameters
 	void PrintCosmology() { cosmo.PrintCosmology(); }
 	
 	/// returns the critical density at the main lens in Msun/ Mpc^2 for a source at zsource
-	PosType getSigmaCrit(PosType zsource) { return cosmo.SigmaCrit(getZlens(), zsource); }
+	PosType getSigmaCrit(PosType zsource) const{ return cosmo.SigmaCrit(getZlens(), zsource); }
 	
 
   /// returns a const reference to the cosmology so that constant functions can be used, but the cosmological parameters cannot be changed.
@@ -228,10 +231,22 @@ class Lens
   void TurnFieldOn() { flag_switch_field_off = false ; }
   
   /// get the field min mass :
-  PosType getFieldMinMass() { return field_min_mass ; }
+  PosType getFieldMinMass() const { return field_min_mass ; }
  
   // get the field_Off value :
-  bool getfieldOff() {return flag_switch_field_off ;}
+  bool getfieldOff() const {return flag_switch_field_off ;}
+  
+  /** \brief Add random halos to the light cone according to standard structure formation theory.  A new realization of the light-cone can be made with Lens::resetFieldHalos() after this function is called once.
+   
+   The cone is filled up until the redshift of the current zsource that is stored in the Lens class.  The field is a circular on the sky.  There is no clustering of the halos.
+   */
+  void GenerateFieldHalos(double min_mass /// minimum mass of halos
+                    ,MassFuncType mass_function /// type of mass function
+                    ,double field_of_view  /// in square degrees
+                    ,int Nplanes           /// number of lens planes
+                    ,LensHaloType halo_type = nfw_lens  /// type of halo
+                    ,bool verbose = false
+                );
   
 protected:
   /// field of view in square degrees
@@ -334,7 +349,9 @@ class Lens
   // get the adress of field_plane_redshifts
   std::vector & get_field_plane_redshifts () { return field_plane_redshifts ; }
   
+  /// Number of Field Halos
   size_t getNFieldHalos() const {return field_halos.size();}
+  /// Number of Sub Halos
   size_t getNSubHalos() const {return substructure.halos.size();}
   
 private: /* force calculation */
diff --git a/include/lens_halos.h b/include/lens_halos.h
index b7bcfee2..ce144414 100644
--- a/include/lens_halos.h
+++ b/include/lens_halos.h
@@ -208,6 +208,9 @@ class LensHalo{
 
 protected:
 
+  // Beyond Rmax the halo will be treated as a point mass.  Between Rsize and Rmax the deflection
+  // and shear are interpolated.  For circularly symmetric lenses Rmax should be equal to Rsize
+  float Rmax;
 
   // make LensHalo uncopyable
   void operator=(LensHalo &){};
@@ -267,10 +270,6 @@ class LensHalo{
   /// error message printout
   void error_message1(std::string name,std::string filename);
   
-  // Beyond Rmax the halo will be treated as a point mass.  Between Rsize and Rmax the deflection
-  // and shear are interpolated.  For circularly symmetric lenses Rmax should be equal to Rsize
-  float Rmax;
-  float Rmax_halo;
   
   /// The factor by which Rmax is larger than Rsize
   const float Rmax_to_Rsize_ratio = 1.2;
diff --git a/include/point.h b/include/point.h
index 51dfd6c8..6fd909b5 100644
--- a/include/point.h
+++ b/include/point.h
@@ -135,6 +135,8 @@ struct Point_2d{
     x[1] /= s;
   }
   
+  PosType* data(){return x;}
+  
   PosType x[2];
   PosType & operator[](size_t i){return x[i];}
 };
@@ -520,6 +522,8 @@ struct Point_3d{
     x[2] /= s;
   }
   
+  PosType* data(){return x;}
+  
   PosType x[3];
   PosType & operator[](size_t i){return x[i];}
 };

From 8315d109e13ce66cbc8c4da149d9ee03f3da180e Mon Sep 17 00:00:00 2001
From: RB Metcalf 
Date: Sun, 13 Aug 2017 10:51:42 +0200
Subject: [PATCH 031/227] Improved default constructor for LensHaloNFW so that
 it initializes everything properly.

---
 MultiPlane/lens_halos.cpp | 15 ++++++++++++++-
 include/lens_halos.h      |  2 +-
 2 files changed, 15 insertions(+), 2 deletions(-)

diff --git a/MultiPlane/lens_halos.cpp b/MultiPlane/lens_halos.cpp
index 7ef30b89..228d918b 100644
--- a/MultiPlane/lens_halos.cpp
+++ b/MultiPlane/lens_halos.cpp
@@ -134,12 +134,25 @@ PosType* LensHaloNFW::xgtable = NULL;
 PosType*** LensHaloNFW::modtable= NULL; // was used for Ansatz IV
 
 
-
 LensHaloNFW::LensHaloNFW()
 : LensHalo(), gmax(0)
 {
+  
+  LensHalo::setRsize(1.0);
+  Rmax = LensHalo::getRsize();
+
+  LensHalo::setMass(0.0);
+  LensHalo::setZlens(0);
+  
+  fratio=1;
+  pa = stars_N = 0;
+  stars_implanted = false;
+  rscale = LensHalo::getRsize()/5;
+  xmax = LensHalo::getRsize()/rscale;
+
   make_tables();
   gmax = InterpolateFromTable(gtable, xmax);
+  set_flag_elliptical(false);
 }
 
 LensHaloNFW::LensHaloNFW(float my_mass,float my_Rsize,PosType my_zlens,float my_concentration,float my_fratio,float my_pa,int my_stars_N, EllipMethod my_ellip_method){
diff --git a/include/lens_halos.h b/include/lens_halos.h
index ce144414..49427a58 100644
--- a/include/lens_halos.h
+++ b/include/lens_halos.h
@@ -669,7 +669,7 @@ class LensHalo{
  */
 class LensHaloNFW: public LensHalo{
 public:
-  /// Shell constructor that should be avoided
+  /// Shell constructor.  Sets the halo to zero mass
 	LensHaloNFW();
   LensHaloNFW(float my_mass   /// in solar masses
               ,float my_Rsize  /// in Mpc

From f714e1d9855a6d1f06481871a7c8efe2400fb456 Mon Sep 17 00:00:00 2001
From: RB Metcalf 
Date: Mon, 14 Aug 2017 12:41:48 +0200
Subject: [PATCH 032/227] Bug fix in QTreeNB::empty()

---
 TreeCode/qTreeNB.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/TreeCode/qTreeNB.cpp b/TreeCode/qTreeNB.cpp
index afa1fbea..330cdb1e 100644
--- a/TreeCode/qTreeNB.cpp
+++ b/TreeCode/qTreeNB.cpp
@@ -62,7 +62,7 @@ QTreeNB::~QTreeNB(){
 
 short QTreeNB::empty(){
 
-  if(Nbranches <= 1) return;
+  if(Nbranches <= 1) return 1;
 	moveTop();
 	_freeQTree(0);
 

From 209309eba4c4a8e483fc5740a9665e2617d1cca2 Mon Sep 17 00:00:00 2001
From: RB Metcalf 
Date: Mon, 14 Aug 2017 17:50:57 +0200
Subject: [PATCH 033/227] Added some useful utilities for reading ASCII files
 and put them in a new name space Utilities::IO

---
 ImageProcessing/pixelize.cpp |  41 ----
 MultiPlane/lens.cpp          |   4 +-
 TreeCode_link/utilities.cpp  |  72 +++++++
 include/utilities_slsim.h    | 405 +++++++++++++++++++++--------------
 4 files changed, 313 insertions(+), 209 deletions(-)

diff --git a/ImageProcessing/pixelize.cpp b/ImageProcessing/pixelize.cpp
index 3057a6b8..194a50eb 100644
--- a/ImageProcessing/pixelize.cpp
+++ b/ImageProcessing/pixelize.cpp
@@ -1612,47 +1612,6 @@ void Utilities::LoadFitsImages(
   return ;
 }
 
-/** \brief Reads the file names in a directory that contain a specific sub string.
- 
- */
-void Utilities::ReadFileNames(
-                              std::string dir              /// path to directory containing fits files
-                              ,const std::string filespec /// string of charactors in file name that are matched. It can be an empty string.
-                              ,std::vector & filenames  /// output vector of PixelMaps
-                              ,bool verbose){
-  
-  DIR *dp = opendir( dir.c_str() );
-  struct dirent *dirp;
-  struct stat filestat;
-  std::string filepath,filename;
-  
-  if (dp == NULL)
-  {
-    std::cerr << "Cannot find directory" << std::endl;
-    throw std::runtime_error("error opening directory");
-    return;
-  }
-  
-  while ((dirp = readdir( dp )) )
-  {
-    filepath = dir + "/" + dirp->d_name;
-    
-    // If the file is a directory (or is in some way invalid) we'll skip it
-    if (stat( filepath.c_str(), &filestat )) continue;
-    if (S_ISDIR( filestat.st_mode ))         continue;
-    
-    filename = dirp->d_name;
-    if(filename.find(filespec) !=  std::string::npos){
-      if(verbose) std::cout << "adding " << filepath << std::endl;
-      filenames.push_back(filename);
-    }
-  }
-  
-  closedir( dp );
-  
-  std::cout << filenames.size() << " file names." << std::endl;
-  return ;
-}
 
 /*// get the index for a position, returns -1 if out of map
  long PixelMap::find_index(PosType const x[],long &ix,long &iy){
diff --git a/MultiPlane/lens.cpp b/MultiPlane/lens.cpp
index 6b9abf4a..20f65582 100644
--- a/MultiPlane/lens.cpp
+++ b/MultiPlane/lens.cpp
@@ -2207,7 +2207,7 @@ void Lens::readInputSimFileMultiDarkHalos(bool verbose)
 	PosType rmax=0,rtmp=0,boundary_p1[2],boundary_p2[2],boundary_diagonal[2];
   
   std::vector filenames;
-  //Utilities::ReadFileNames(field_input_sim_file.c_str(),".dat",filenames);
+  //Utilities::IO::ReadFileNames(field_input_sim_file.c_str(),".dat",filenames);
   filenames.push_back(" ");
   //std::vector halo_pos_vec;
   //std::vector sph_halo_pos_vec;
@@ -2537,7 +2537,7 @@ void Lens::readInputSimFileObservedGalaxies(bool verbose)
   
   PosType rmax=0,rtmp=0,boundary_p1[2],boundary_p2[2],boundary_diagonal[2];
   
-  //Utilities::ReadFileNames(field_input_sim_file.c_str(),".dat",filenames);
+  //Utilities::IO::ReadFileNames(field_input_sim_file.c_str(),".dat",filenames);
   //filenames.push_back(" ");
   //std::vector halo_pos_vec;
   //std::vector sph_halo_pos_vec;
diff --git a/TreeCode_link/utilities.cpp b/TreeCode_link/utilities.cpp
index b9523e37..7a0553a8 100644
--- a/TreeCode_link/utilities.cpp
+++ b/TreeCode_link/utilities.cpp
@@ -646,3 +646,75 @@ unsigned long prevpower(unsigned long k){
   int GetNThreads(){return N_THREADS;}
 }
 
+void Utilities::IO::ReadFileNames(
+                   std::string dir              /// path to directory containing fits files
+                   ,const std::string filespec /// string of charactors in file name that are matched. It can be an empty string.
+                   ,std::vector & filenames  /// output vector of PixelMaps
+                   ,bool verbose){
+  
+  DIR *dp = opendir( dir.c_str() );
+  struct dirent *dirp;
+  struct stat filestat;
+  std::string filepath,filename;
+  
+  if (dp == NULL)
+  {
+    std::cerr << "Cannot find directory" << std::endl;
+    throw std::runtime_error("error opening directory");
+    return;
+  }
+  
+  while ((dirp = readdir( dp )) )
+  {
+    filepath = dir + "/" + dirp->d_name;
+    
+    // If the file is a directory (or is in some way invalid) we'll skip it
+    if (stat( filepath.c_str(), &filestat )) continue;
+    if (S_ISDIR( filestat.st_mode ))         continue;
+    
+    filename = dirp->d_name;
+    if(filename.find(filespec) !=  std::string::npos){
+      if(verbose) std::cout << "adding " << filepath << std::endl;
+      filenames.push_back(filename);
+    }
+  }
+  
+  closedir( dp );
+  
+  std::cout << filenames.size() << " file names." << std::endl;
+  return ;
+}
+
+/// Count the number of columns in a ASCII data file
+int Utilities::IO::CountColumns(std::string filename,char comment_char
+                                ,char deliniator
+){
+  
+  std::ifstream file(filename);
+  // find number of particles
+  if (!file.is_open()){
+    std::cerr << "file " << filename << " cann't be opened." << std::endl;
+    throw std::runtime_error("no file");
+  }
+  
+  std::string line;
+  // read comment lines and first data line
+  do{
+    std::getline(file,line);
+    if(!file) break;  // probably EOF
+  }while(line[0] == comment_char);
+  
+  return  NumberOfEntries(line,deliniator);
+}
+
+size_t Utilities::IO::NumberOfEntries(const std::string &string,char deliniator){
+  size_t number = 0;
+  auto it = string.begin();
+  while(it != string.end()){
+    while( it != string.end() && (*it == ' ' || *it == deliniator)) ++it;
+    if(it != string.end()) ++number;
+    while(it != string.end() && *it != deliniator) ++it;
+  }
+  return number;
+}
+
diff --git a/include/utilities_slsim.h b/include/utilities_slsim.h
index 678f3add..cd9eac88 100644
--- a/include/utilities_slsim.h
+++ b/include/utilities_slsim.h
@@ -1118,188 +1118,261 @@ namespace Utilities
 
   /// returns the compiler variable N_THREADS that is maximum number of threads to be used.
   int GetNThreads();
-  
-  /** \brief Read in data from an ASCII file with two columns
-   */
-  template 
-  void read2columnfile(
-                       std::string filename    /// input file name
-                       ,std::vector &x     /// vector that will contain the first column
-                       ,std::vector &y     /// vector that will contain the first column
-                       ,std::string delineator = " "  /// specific string the seporates columns, ex. ",", "|", etc.
-                       ,bool verbose = false
-                       
-                       ){
-    
-    x.clear();
-    y.clear();
-    
-    std::ifstream file_in(filename.c_str());
-    std::string myline;
-    std::string space = " ";
-    T1 myt1;
-    T2 myt2;
-    
-    std::string strg;
-    std::stringstream buffer;
-    
-    if(!file_in){
-      std::cout << "Can't open file " << filename << std::endl;
-      ERROR_MESSAGE();
-      throw std::runtime_error(" Cannot open file.");
-    }
-    
-    std::cout << "Reading caustic information from " << filename << std::endl;
-    size_t i=0;
-    while(file_in.peek() == '#'){
-      file_in.ignore(10000,'\n');
-      ++i;
-    }
-    std::cout << "skipped "<< i << " comment lines in " << filename << std::endl;
-    
-    size_t pos;
-    // read in data
-    while(getline(file_in,myline)){
-      
-      if(myline[0] == '#'){
-        std::cout << "skipped line " << i << std::endl;
-        continue;
+
+  namespace IO{  ///
+    /** \brief Read in data from an ASCII file with two columns
+     */
+    template 
+    void read2columnfile(
+                         std::string filename    /// input file name
+                         ,std::vector &x     /// vector that will contain the first column
+                         ,std::vector &y     /// vector that will contain the first column
+                         ,std::string delineator = " "  /// specific string the seporates columns, ex. ",", "|", etc.
+                         ,bool verbose = false
+                         
+                         ){
+      
+      x.clear();
+      y.clear();
+      
+      std::ifstream file_in(filename.c_str());
+      std::string myline;
+      std::string space = " ";
+      T1 myt1;
+      T2 myt2;
+      
+      std::string strg;
+      std::stringstream buffer;
+      
+      if(!file_in){
+        std::cout << "Can't open file " << filename << std::endl;
+        ERROR_MESSAGE();
+        throw std::runtime_error(" Cannot open file.");
       }
       
-      pos= myline.find_first_not_of(space);
-      myline.erase(0,pos);
-      
-      
-      pos = myline.find(delineator);
-      strg.assign(myline,0,pos);
-      buffer << strg;
-      buffer >> myt1;
-      if(verbose) std::cout << myt1 << " ";
-      x.push_back(myt1);
-      
-      myline.erase(0,pos+1);
-      pos= myline.find_first_not_of(space);
-      myline.erase(0,pos);
-      
-      strg.clear();
-      buffer.clear();
+      std::cout << "Reading caustic information from " << filename << std::endl;
+      size_t i=0;
+      while(file_in.peek() == '#'){
+        file_in.ignore(10000,'\n');
+        ++i;
+      }
+      std::cout << "skipped "<< i << " comment lines in " << filename << std::endl;
       
-      pos = myline.find(space);
-      strg.assign(myline,0,pos);
-      buffer << strg;
-      buffer >> myt2;
-      if(verbose)  std::cout << myt2 << std::endl;
-      y.push_back(myt2);
+      size_t pos;
+      // read in data
+      while(getline(file_in,myline)){
+        
+        if(myline[0] == '#'){
+          std::cout << "skipped line " << i << std::endl;
+          continue;
+        }
+        
+        pos= myline.find_first_not_of(space);
+        myline.erase(0,pos);
+        
+        
+        pos = myline.find(delineator);
+        strg.assign(myline,0,pos);
+        buffer << strg;
+        buffer >> myt1;
+        if(verbose) std::cout << myt1 << " ";
+        x.push_back(myt1);
+        
+        myline.erase(0,pos+1);
+        pos= myline.find_first_not_of(space);
+        myline.erase(0,pos);
+        
+        strg.clear();
+        buffer.clear();
+        
+        pos = myline.find(space);
+        strg.assign(myline,0,pos);
+        buffer << strg;
+        buffer >> myt2;
+        if(verbose)  std::cout << myt2 << std::endl;
+        y.push_back(myt2);
+        
+        strg.clear();
+        buffer.clear();
+        myline.clear();
+        
+      }
+      std::cout << "Read " << x.size() << " lines from " << filename << std::endl;
+    }
+    /** \brief Read in data from an ASCII file with three columns
+     */
+    template 
+    void read3columnfile(
+                         std::string filename    /// input file name
+                         ,std::vector &x     /// vector that will contain the first column
+                         ,std::vector &y     /// vector that will contain the first column
+                         ,std::vector &z     /// vector that will contain the first column
+                         ,std::string delineator = " "  /// specific string the seporates columns, ex. ",", "|", etc.
+                         ,bool verbose = false
+                         
+                         ){
+      
+      
+      assert(0); // Untested!!!!
+      x.clear();
+      y.clear();
+      z.clear();
+      
+      std::ifstream file_in(filename.c_str());
+      std::string myline;
+      std::string space = " ";
+      T1 myt1;
+      T2 myt2;
+      T3 myt3;
+      
+      std::string strg;
+      std::stringstream buffer;
+      
+      if(!file_in){
+        std::cout << "Can't open file " << filename << std::endl;
+        ERROR_MESSAGE();
+        throw std::runtime_error(" Cannot open file.");
+      }
       
-      strg.clear();
-      buffer.clear();
-      myline.clear();
+      std::cout << "Reading caustic information from " << filename << std::endl;
+      size_t i=0;
+      while(file_in.peek() == '#'){
+        file_in.ignore(10000,'\n');
+        ++i;
+      }
+      std::cout << "skipped "<< i << " comment lines in " << filename << std::endl;
       
+      size_t pos;
+      // read in data
+      while(getline(file_in,myline)){
+        
+        if(myline[0] == '#'){
+          std::cout << "skipped line " << i << std::endl;
+          continue;
+        }
+        
+        pos= myline.find_first_not_of(space);
+        myline.erase(0,pos);
+        
+        
+        pos = myline.find(delineator);
+        strg.assign(myline,0,pos);
+        buffer << strg;
+        buffer >> myt1;
+        if(verbose) std::cout << myt1 << " ";
+        x.push_back(myt1);
+        
+        myline.erase(0,pos+1);
+        pos= myline.find_first_not_of(space);
+        myline.erase(0,pos);
+        
+        strg.clear();
+        buffer.clear();
+        
+        // ******************
+        
+        
+        pos = myline.find(space);
+        strg.assign(myline,0,pos);
+        buffer << strg;
+        buffer >> myt2;
+        if(verbose)  std::cout << myt2 << std::endl;
+        y.push_back(myt2);
+        
+        myline.erase(0,pos+1);
+        pos= myline.find_first_not_of(space);
+        myline.erase(0,pos);
+        
+        strg.clear();
+        buffer.clear();
+        
+        // ******************
+        pos = myline.find(space);
+        strg.assign(myline,0,pos);
+        buffer << strg;
+        buffer >> myt3;
+        if(verbose)  std::cout << myt3 << std::endl;
+        y.push_back(myt3);
+        
+        strg.clear();
+        buffer.clear();
+        myline.clear();
+        
+      }
+      std::cout << "Read " << x.size() << " lines from " << filename << std::endl;
     }
-    std::cout << "Read " << x.size() << " lines from " << filename << std::endl;
-  }
-  /** \brief Read in data from an ASCII file with three columns
-   */
-  template 
-  void read3columnfile(
-                       std::string filename    /// input file name
-                       ,std::vector &x     /// vector that will contain the first column
-                       ,std::vector &y     /// vector that will contain the first column
-                       ,std::vector &z     /// vector that will contain the first column
-                       ,std::string delineator = " "  /// specific string the seporates columns, ex. ",", "|", etc.
-                       ,bool verbose = false
-                       
-                       ){
-    
-    
-    assert(0); // Untested!!!!
-    x.clear();
-    y.clear();
-    z.clear();
-    
-    std::ifstream file_in(filename.c_str());
-    std::string myline;
-    std::string space = " ";
-    T1 myt1;
-    T2 myt2;
-    T3 myt3;
-    
-    std::string strg;
-    std::stringstream buffer;
-    
-    if(!file_in){
-      std::cout << "Can't open file " << filename << std::endl;
-      ERROR_MESSAGE();
-      throw std::runtime_error(" Cannot open file.");
-    }
-    
-    std::cout << "Reading caustic information from " << filename << std::endl;
-    size_t i=0;
-    while(file_in.peek() == '#'){
-      file_in.ignore(10000,'\n');
-      ++i;
-    }
-    std::cout << "skipped "<< i << " comment lines in " << filename << std::endl;
-    
-    size_t pos;
-    // read in data
-    while(getline(file_in,myline)){
-      
-      if(myline[0] == '#'){
-        std::cout << "skipped line " << i << std::endl;
-        continue;
+    
+    size_t NumberOfEntries(const std::string &string,char deliniator);
+    
+    /// Count the number of columns in a ASCII data file.
+    int CountColumns(std::string filename  /// name of file
+                     ,char comment_char = '#'  /// comment charactor
+                     ,char deliniator = ' '    /// deliniator between columns
+    );
+    
+    /** \brief Reads the file names in a directory that contain a specific sub string.
+     
+     */
+    void ReadFileNames(
+                                  std::string dir              /// path to directory containing fits files
+                                  ,const std::string filespec /// string of charactors in file name that are matched. It can be an empty string.
+                                  ,std::vector & filenames  /// output vector of PixelMaps
+                       ,bool verbose);
+    
+    
+    /** \brief This function will read in all the numbers from a multi-column
+     ASCII data file.
+     
+     It will skip the comment lines if the are at the head of the file.  The 
+     number of columns and rows are returned.  The entry at row r and column c will be stored at data[c + column*r].
+     
+     This function is not particularly fast for large amounts of data.  If the 
+     number of roaws is large it would be best to use data.reserve() to set the capacity of data large enough that no rellocation of memory occurs.
+     */
+    template 
+    void ReadASCII(std::vector &data
+                   ,std::string filename
+                   ,int &columns
+                   ,int &rows
+                   ,char comment_char = '#'
+                   ,size_t MaxNrows = SIZE_T_MAX
+                   ,bool verbose = true){
+      
+      std::ifstream file(filename);
+      // find number of particles
+      if (!file.is_open()){
+        std::cerr << "file " << filename << " cann't be opened." << std::endl;
+        throw std::runtime_error("no file");
       }
       
-      pos= myline.find_first_not_of(space);
-      myline.erase(0,pos);
       
+      data.empty();
       
-      pos = myline.find(delineator);
-      strg.assign(myline,0,pos);
-      buffer << strg;
-      buffer >> myt1;
-      if(verbose) std::cout << myt1 << " ";
-      x.push_back(myt1);
+      std::string line;
+      columns = 0;
+      rows = 0;
+      size_t first_data_line;
       
-      myline.erase(0,pos+1);
-      pos= myline.find_first_not_of(space);
-      myline.erase(0,pos);
+      // read comment lines and first data line
+      do{
+        first_data_line = file.tellg();
+        std::getline(file,line);
+        if(!file) break;  // probably EOF
+      }while(line[0] == comment_char);
       
-      strg.clear();
-      buffer.clear();
-
-      // ******************
+      columns =  NumberOfEntries(line,' ');
       
+      file.seekg(first_data_line);   // move back to first data line
       
-      pos = myline.find(space);
-      strg.assign(myline,0,pos);
-      buffer << strg;
-      buffer >> myt2;
-      if(verbose)  std::cout << myt2 << std::endl;
-      y.push_back(myt2);
-
-      myline.erase(0,pos+1);
-      pos= myline.find_first_not_of(space);
-      myline.erase(0,pos);
-      
-      strg.clear();
-      buffer.clear();
-      
-      // ******************
-      pos = myline.find(space);
-      strg.assign(myline,0,pos);
-      buffer << strg;
-      buffer >> myt3;
-      if(verbose)  std::cout << myt3 << std::endl;
-      y.push_back(myt3);
-
-      strg.clear();
-      buffer.clear();
-      myline.clear();
+      std::copy(std::istream_iterator(file),
+                  std::istream_iterator(),
+                  std::back_inserter(data));
       
+      rows = data.size()/columns;
+      if(verbose){
+        std::cout << "Read " << rows << " rows of " << columns << " columns from file " << filename << std::endl;
+      }
     }
-    std::cout << "Read " << x.size() << " lines from " << filename << std::endl;
   }
-
 }
 #endif

From 9eac94901ed22bea8a20a40507a77dbecdc3e86e Mon Sep 17 00:00:00 2001
From: RB Metcalf 
Date: Mon, 14 Aug 2017 18:07:38 +0200
Subject: [PATCH 034/227] Put some file format checking in LensHaloParticles,

---
 MultiPlane/particle_lens.cpp | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/MultiPlane/particle_lens.cpp b/MultiPlane/particle_lens.cpp
index 1d064088..be4203d6 100644
--- a/MultiPlane/particle_lens.cpp
+++ b/MultiPlane/particle_lens.cpp
@@ -114,6 +114,15 @@ void LensHaloParticles::rotate(Point_2d theta){
  */
 void LensHaloParticles::readPositionFileASCII(const std::string &filename){
   
+  int ncoll = Utilities::IO::CountColumns(filename);
+  if(!multimass && ncoll != 3 ){
+    std::cerr << filename << " should have three columns!" << std::endl;
+  }
+  if(multimass && ncoll != 4 ){
+    std::cerr << filename << " should have four columns!" << std::endl;
+  }
+ 
+  
   std::ifstream myfile(filename);
   
   // find number of particles

From 7c11e60ff3d5035ff5a081dd14eb7b19745f0b2b Mon Sep 17 00:00:00 2001
From: RB Metcalf 
Date: Wed, 23 Aug 2017 22:08:32 +0200
Subject: [PATCH 035/227] Changed the sign of the deflection in
 LensHaloUniform.

---
 AnalyticNSIE/readfiles_uni.cpp | 35 ++++++++++++++++++----------------
 1 file changed, 19 insertions(+), 16 deletions(-)

diff --git a/AnalyticNSIE/readfiles_uni.cpp b/AnalyticNSIE/readfiles_uni.cpp
index c8654c8e..d89f8014 100644
--- a/AnalyticNSIE/readfiles_uni.cpp
+++ b/AnalyticNSIE/readfiles_uni.cpp
@@ -100,23 +100,26 @@ void LensHaloUniform::force_halo(
                                  ,PosType screening
                                  )
 {
-    PosType alpha_tmp[2];
-    KappaType gamma_tmp[3], phi_tmp;
-
-    gamma_tmp[0] = gamma_tmp[1] = gamma_tmp[2] = 0.0;
-    alpha_tmp[0] = alpha_tmp[1] = 0.0;
-    phi_tmp = 0.0;
+  PosType alpha_tmp[2];
+  KappaType gamma_tmp[3], phi_tmp;
+  
+  gamma_tmp[0] = gamma_tmp[1] = gamma_tmp[2] = 0.0;
+  alpha_tmp[0] = alpha_tmp[1] = 0.0;
+  phi_tmp = 0.0;
+  
+  *kappa += lens_expand(perturb_modes,xcm,alpha,gamma,&phi_tmp);
+  
+  alpha[0] *= -1;
+  alpha[1] *= -1;
+  
+  *phi += phi_tmp ;
+  
+  // add stars for microlensing
+  if(stars_N > 0 && stars_implanted)
+  {
+    force_stars(alpha,kappa,gamma,xcm);
+  }
   
-    *kappa += lens_expand(perturb_modes,xcm,alpha,gamma,&phi_tmp);
-    
-    *phi += phi_tmp ;
-    
-    // add stars for microlensing
-    if(stars_N > 0 && stars_implanted)
-    {
-      force_stars(alpha,kappa,gamma,xcm);
-    }
-    
 }
 
 PosType LensHaloUniform::lens_expand(PosType *mod

From b38df9c99d4e7948a2de244c20d63577a50683e6 Mon Sep 17 00:00:00 2001
From: RB Metcalf 
Date: Thu, 24 Aug 2017 10:16:31 +0200
Subject: [PATCH 036/227] Tried to fix a compiling bug that comes up on some
 systems having to do with SIZE_T_MAX

---
 include/utilities_slsim.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/include/utilities_slsim.h b/include/utilities_slsim.h
index cd9eac88..cbcf042f 100644
--- a/include/utilities_slsim.h
+++ b/include/utilities_slsim.h
@@ -1335,7 +1335,7 @@ namespace Utilities
                    ,int &columns
                    ,int &rows
                    ,char comment_char = '#'
-                   ,size_t MaxNrows = SIZE_T_MAX
+                   ,size_t MaxNrows = std::numeric_limits::max()
                    ,bool verbose = true){
       
       std::ifstream file(filename);

From 0851dffde90403dc2ff56db89a5cfd182238a8e1 Mon Sep 17 00:00:00 2001
From: RB Metcalf 
Date: Thu, 18 Jan 2018 14:10:55 +0100
Subject: [PATCH 037/227] Added PixelMap::copy_in() to copy the contents of one
 map into another that has different dimensions. Added
 PixelMap::AddGridMapBrightness() Made some functions constant.

---
 ImageProcessing/observation.cpp |  3 +-
 ImageProcessing/pixelize.cpp    | 99 ++++++++++++++++++++++++++-------
 TreeCode_link/gridmap.cpp       |  6 +-
 include/gridmap.h               |  4 +-
 include/image_processing.h      | 26 +++++++--
 include/point.h                 |  1 +
 6 files changed, 107 insertions(+), 32 deletions(-)

diff --git a/ImageProcessing/observation.cpp b/ImageProcessing/observation.cpp
index 1ff4a243..6878792e 100644
--- a/ImageProcessing/observation.cpp
+++ b/ImageProcessing/observation.cpp
@@ -458,8 +458,9 @@ float Observation::getBackgroundNoise(float resolution, unitType unit)
     if (telescope==true && fabs(resolution-pix_size) > pix_size*1.0e-5)
     {
         std::cout << "The resolution is different from the one of the simulated instrument in Observation::getBackgroundNoise!" << std::endl;
-        throw std::runtime_error("The resolution is different from the one of the simulated instrument!");
+      throw std::runtime_error("The resolution is different from the one of the simulated instrument!");
     }
+
     double Q = pow(10,0.4*(mag_zeropoint+48.6));
     double res_in_arcsec = resolution*180.*60.*60/pi;
     double back_mean = pow(10,-0.4*(48.6+back_mag))*res_in_arcsec*res_in_arcsec*Q*exp_time;
diff --git a/ImageProcessing/pixelize.cpp b/ImageProcessing/pixelize.cpp
index 3eaa2d63..f9e1eeaa 100644
--- a/ImageProcessing/pixelize.cpp
+++ b/ImageProcessing/pixelize.cpp
@@ -304,9 +304,9 @@ PixelMap::PixelMap(
   
   map.resize(Nx*Ny);
   
-  int old_Nx = pmap.Nx;
-  int old_Ny = pmap.Ny;
-  int ix, iy;
+  long old_Nx = pmap.Nx;
+  long old_Ny = pmap.Ny;
+  long ix, iy;
   PosType area;
   PosType old_p1[2];
   PosType old_p2[2];
@@ -319,8 +319,8 @@ PixelMap::PixelMap(
     old_p1[0] = std::max(0,int(ix*res_ratio));
     old_p1[1] = std::max(0,int(iy*res_ratio));
     
-    old_p2[0] = std::min(old_Nx-1,int((ix+1.)*res_ratio));
-    old_p2[1] = std::min(old_Ny-1,int((iy+1.)*res_ratio));
+    old_p2[0] = fmin(old_Nx-1,int((ix+1.)*res_ratio));
+    old_p2[1] = fmin(old_Ny-1,int((iy+1.)*res_ratio));
     
     for (int old_iy = old_p1[1]; old_iy <= old_p2[1]; ++old_iy)
     {
@@ -390,7 +390,6 @@ void PixelMap::AssignValue(std::size_t i, PosType value)
   map[i] = value;
 }
 
-/// Check whether two PixelMaps agree in their physical dimensions.
 bool PixelMap::agrees(const PixelMap& other) const
 {
   return
@@ -529,15 +528,11 @@ void PixelMap::AddImages(
   
   return;
 }
-/** \brief Add an image to the map
- *
- */
+
 void PixelMap::AddGridBrightness(Grid &grid){
   
-  
-  
   PointList *plist = grid.i_tree->pointlist;
-
+  
   if(plist->size() == 0) return;
   
   PointList::iterator listit= plist->Top();// = plist->begin();
@@ -550,9 +545,9 @@ void PixelMap::AddGridBrightness(Grid &grid){
   //for(long ii=0;iibegin() ; listit != plist->end() ; ++listit ){
+    //for(listit = plist->begin() ; listit != plist->end() ; ++listit ){
     sb = (*listit)->surface_brightness;
-  
+    
     if (sb != 0.0 && (inMapBox((*listit)->leaf)) == true){
       PointsWithinLeaf((*listit)->leaf,neighborlist);
       for(it = neighborlist.begin();it != neighborlist.end();it++){
@@ -561,7 +556,20 @@ void PixelMap::AddGridBrightness(Grid &grid){
       }
     }
   }while(--listit);
+  
+  return;
+}
 
+void PixelMap::AddGridMapBrightness(const GridMap &grid){
+  
+  try {
+    // if GridMap res is an integer multiple of PixelMap res and they are aligned this will go
+    grid.getPixelMap(*this);
+  } catch (const std::invalid_argument& ia) {
+    // dimensions and/or alignment do not match
+    PixelMap newmap = grid.getPixelMap(1);
+    copy_in(newmap);
+  }
   return;
 }
 
@@ -1708,7 +1716,7 @@ void Utilities::ReadFileNames(
  */
 
 /// get the index for a position, returns -1 if out of map
-long PixelMap::find_index(PosType const x[],long &ix,long &iy){
+long PixelMap::find_index(PosType const x[],long &ix,long &iy) const{
   
   ix = (long)((x[0] - map_boundary_p1[0])/resolution);
   iy = (long)((x[1] - map_boundary_p1[1])/resolution);
@@ -1725,7 +1733,7 @@ long PixelMap::find_index(PosType const x[],long &ix,long &iy){
   return ix + Nx*iy;
 }
 /// get the index for a position, returns -1 if out of map
-long PixelMap::find_index(PosType const x,PosType const y,long &ix,long &iy){
+long PixelMap::find_index(PosType const x,PosType const y,long &ix,long &iy) const{
   
   //ix = (long)((x - map_boundary_p1[0])/resolution + 0.5);
   //iy = (long)((y - map_boundary_p1[1])/resolution + 0.5);
@@ -1745,17 +1753,17 @@ long PixelMap::find_index(PosType const x,PosType const y,long &ix,long &iy){
   return ix + Nx*iy;
 }
 /// get the index for a position, returns -1 if out of map
-long PixelMap::find_index(PosType const x[]){
+long PixelMap::find_index(PosType const x[]) const{
   long ix,iy;
   return find_index(x,ix,iy);
 }
 /// get the index for a position, returns -1 if out of map
-long PixelMap::find_index(PosType const x,PosType const y){
+long PixelMap::find_index(PosType const x,PosType const y) const{
   long ix,iy;
   return find_index(x,y,ix,iy);
 }
 /// get the index for a position, returns -1 if out of map
-void PixelMap::find_position(PosType x[],std::size_t const index){
+void PixelMap::find_position(PosType x[],std::size_t const index) const{
   if(Nx == 1){
     x[0] = center[0];
     x[1] = center[1];
@@ -1766,7 +1774,7 @@ void PixelMap::find_position(PosType x[],std::size_t const index){
   return;
 }
 /// get the index for a position, returns -1 if out of map
-void PixelMap::find_position(PosType x[],std::size_t const ix,std::size_t const iy){
+void PixelMap::find_position(PosType x[],std::size_t const ix,std::size_t const iy) const{
   if(Nx == 1){
     x[0] = center[0];
     x[1] = center[1];
@@ -2034,6 +2042,57 @@ PosType PixelMap::AddSource(Source &source,int oversample){
   return total;
 }
 
+void PixelMap::copy_in(
+                   const PixelMap& pmap
+)
+{
+  
+  if(agrees(pmap)){  // maps are the same dimensions and position
+    for(size_t i=0;i pmap.map_boundary_p2[0] ) return;
+  if(map_boundary_p2[0] < pmap.map_boundary_p1[0] ) return;
+  if(map_boundary_p1[1] > pmap.map_boundary_p2[1] ) return;
+  if(map_boundary_p2[1] < pmap.map_boundary_p1[1] ) return;
+
+  
+  double halfpixel = res_ratio/2;
+  PosType x[2];
+  for(size_t ii=0;ii < map.size(); ++ii){
+    
+    find_position(x,ii);
+      // find range if this pixel in pmap's pixel space
+    double ix = (x[0] - pmap.map_boundary_p1[0])/pmap.resolution;
+    double iy = (x[1] - pmap.map_boundary_p1[1])/pmap.resolution;
+    
+    double xmin = fmax(-0.5,ix - halfpixel);
+    double xmax = fmin(pmap.getNx() - 0.5 ,ix + halfpixel);
+    if(xmin >= xmax) continue;
+
+    double ymin = fmax(-0.5,iy - halfpixel);
+    double ymax = fmin(pmap.getNx() - 0.5 ,iy + halfpixel);
+    if(ymin >= ymax) continue;
+    
+    long imin = (long)(xmin + 0.5);
+    long imax = (long)(xmax + 0.5);
+    long jmin = (long)(ymin + 0.5);
+    long jmax = (long)(ymax + 0.5);
+
+    for(size_t j = jmin ; j <= jmax ; ++j ){
+      double area = fmin(ymax,j+0.5) -  fmax(ymin,j-0.5);
+      for(size_t i = imin ; i <= imax ; ++i ){
+        area *= fmin(xmax,i+0.5) -  fmax(xmin,i-0.5);
+        map[ii] += pmap.map[i + Nx*j]*area/res_ratio2;
+      }
+    }
+  }
+}
+
 /*
 void PixelMap::AddSource(Source &source,int oversample){
   Point_2d s_center;
diff --git a/TreeCode_link/gridmap.cpp b/TreeCode_link/gridmap.cpp
index 47d7c413..7954a7b3 100644
--- a/TreeCode_link/gridmap.cpp
+++ b/TreeCode_link/gridmap.cpp
@@ -125,7 +125,7 @@ void GridMap::ReInitializeGrid(LensHndl lens){
 }
 
 /// Output a PixelMap of the surface brightness with same res as the GridMap
-PixelMap GridMap::getPixelMap(int resf){
+PixelMap GridMap::getPixelMap(int resf) const{
   
   if(resf <=0){
     ERROR_MESSAGE();
@@ -152,7 +152,7 @@ PixelMap GridMap::getPixelMap(int resf){
 }
 
 /// surface brightness map
-void GridMap::getPixelMap(PixelMap &map){
+void GridMap::getPixelMap(PixelMap &map) const{
   
   int resf = (Ngrid_init-1)/(map.getNx()-1);
   
@@ -167,7 +167,7 @@ void GridMap::getPixelMap(PixelMap &map){
     ERROR_MESSAGE();
     throw std::invalid_argument("resf must be > 0");
   }
-
+  
   map.Clean();
   
   int factor = resf*resf;
diff --git a/include/gridmap.h b/include/gridmap.h
index 957d12bb..c1d5626d 100644
--- a/include/gridmap.h
+++ b/include/gridmap.h
@@ -52,11 +52,11 @@ struct GridMap{
   void writeFitsUniform(const PosType center[],size_t Nx,size_t Ny,LensingVariable lensvar,std::string filename);
   
   /// returns a PixelMap with the flux in pixels at a resolution of res times the original resolution
-  PixelMap getPixelMap(int res);
+  PixelMap getPixelMap(int res) const;
   /// update a PixelMap with the flux in pixels at a resolution of res times the original resolution.
   /// The map must have precisely the right size and center to match or an exception will be thrown.
   /// Constructing the map with PixelMap getPixelMap(int res) will insure that it does.
-  void getPixelMap(PixelMap &map);
+  void getPixelMap(PixelMap &map) const;
   
   /// returns the area (radians^2) of the region with negative magnification at resolution of fixed grid
   PosType EisnsteinArea() const;
diff --git a/include/image_processing.h b/include/image_processing.h
index 773be17e..09da6315 100644
--- a/include/image_processing.h
+++ b/include/image_processing.h
@@ -18,6 +18,7 @@
 
 // forward declaration
 struct Grid;
+struct GridMap;
 
 /** \ingroup Image
  * \brief Takes image structure and pixelizes the flux into regular pixel grid which then
@@ -56,11 +57,22 @@ class PixelMap
 
   void AddImages(ImageInfo *imageinfo,int Nimages,float rescale = 1.);
   void AddImages(std::vector &imageinfo,int Nimages,float rescale = 1.);
+  /// Add an image from a the surface brightnesses of a Grid to the PixelMap
   void AddGridBrightness(Grid &grid);
+  /// Add an image from a the surface brightnesses of a GridMap to the PixelMap
+  void AddGridMapBrightness(const GridMap &grid);
   void AddUniformImages(ImageInfo *imageinfo,int Nimages,double value);
   PosType AddSource(Source &source);
   /// Add a source to the pixel map by oversamples the source so that oversample^2 points within each pixel are averaged
   PosType AddSource(Source &source,int oversample);
+  
+  /** \brief copy a PixelMap into this one.
+   
+   The size, resolutio and center of the pixel maps are not changed and do
+   not need to match.  The input pixel map is added while conserving the area integral
+   of the map within the area of overlaping pixels.
+  **/
+  void copy_in(const PixelMap& pmap);
 
   /// Adds source to map.  This version breaks pixels up into blocks and does them in seporate threads.
   template 
@@ -117,6 +129,7 @@ class PixelMap
   inline double operator()(std::size_t i) const { return map[i]; };
   inline double operator()(std::size_t i,std::size_t j) const { return map[i + Nx*j]; };
 	
+  
 	PixelMap& operator+=(const PixelMap& rhs);
 	friend PixelMap operator+(const PixelMap&, const PixelMap&);
 
@@ -131,6 +144,7 @@ class PixelMap
 	
 	std::valarray& data() { return map; }
 	
+  /// Check whether two PixelMaps agree in their physical dimensions.
 	bool agrees(const PixelMap& other) const;
 	
 	friend void swap(PixelMap&, PixelMap&);
@@ -147,19 +161,19 @@ class PixelMap
                          ,PosType threshold);
   
   /// get the index for a position, returns -1 if out of map, this version returns the 2D grid coordinates
-  long find_index(PosType const x[],long &ix,long &iy);
+  long find_index(PosType const x[],long &ix,long &iy) const;
   /// get the index for a position, returns -1 if out of map
-  long find_index(PosType const x[]);
+  long find_index(PosType const x[]) const;
   
   /// get the index for a position, returns -1 if out of map, this version returns the 2D grid coordinates
-  long find_index(PosType const x,PosType const y,long &ix,long &iy);
+  long find_index(PosType const x,PosType const y,long &ix,long &iy) const;
   /// get the index for a position, returns -1 if out of map
-  long find_index(PosType const x,PosType const y);
+  long find_index(PosType const x,PosType const y) const;
   
   /// get the index for a position, returns -1 if out of map
-  void find_position(PosType x[],std::size_t const index);
+  void find_position(PosType x[],std::size_t const index) const;
   /// get the index for a position, returns -1 if out of map
-  void find_position(PosType x[],std::size_t const ix,std::size_t const iy);
+  void find_position(PosType x[],std::size_t const ix,std::size_t const iy) const;
   
   /// interpolate to point x[]
   PosType linear_interpolate(PosType x[]);
diff --git a/include/point.h b/include/point.h
index 6fd909b5..01928652 100644
--- a/include/point.h
+++ b/include/point.h
@@ -139,6 +139,7 @@ struct Point_2d{
   
   PosType x[2];
   PosType & operator[](size_t i){return x[i];}
+  PosType operator[](size_t i) const {return x[i];}
 };
 
 std::ostream &operator<<(std::ostream &os, Point_2d const &p);

From 530e3e815602773496275897b71cf0ff6f9630b7 Mon Sep 17 00:00:00 2001
From: RB Metcalf 
Date: Sun, 21 Jan 2018 18:37:18 +0100
Subject: [PATCH 038/227] Some fixes to PixelMap::copy_in()

---
 ImageProcessing/pixelize.cpp | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)

diff --git a/ImageProcessing/pixelize.cpp b/ImageProcessing/pixelize.cpp
index f9e1eeaa..82acf706 100644
--- a/ImageProcessing/pixelize.cpp
+++ b/ImageProcessing/pixelize.cpp
@@ -2051,7 +2051,7 @@ void PixelMap::copy_in(
     for(size_t i=0;i
Date: Mon, 22 Jan 2018 19:25:10 +0100
Subject: [PATCH 039/227] Added PixelMap::recenter() Fixed PixelMap::copy_in()

---
 ImageProcessing/pixelize.cpp | 45 +++++++++++++++++++++++++-----------
 include/image_processing.h   |  5 +++-
 2 files changed, 36 insertions(+), 14 deletions(-)

diff --git a/ImageProcessing/pixelize.cpp b/ImageProcessing/pixelize.cpp
index 82acf706..c6bbfb8d 100644
--- a/ImageProcessing/pixelize.cpp
+++ b/ImageProcessing/pixelize.cpp
@@ -2063,37 +2063,56 @@ void PixelMap::copy_in(
   
   double halfpixel = res_ratio/2;
   PosType x[2];
+  size_t NNx = pmap.getNx(),NNy = pmap.getNy();
+
   for(size_t ii=0 ; ii < map.size() ; ++ii){
     
     find_position(x,ii);
       // find range if this pixel in pmap's pixel space
-    double ix = (x[0] - pmap.map_boundary_p1[0])/pmap.resolution + 0.5;
-    double iy = (x[1] - pmap.map_boundary_p1[1])/pmap.resolution + 0.5;
+    double ix = (x[0] - pmap.map_boundary_p1[0])/pmap.resolution;
+    double iy = (x[1] - pmap.map_boundary_p1[1])/pmap.resolution;
     
-    double xmin = fmax(-0.5,ix - halfpixel);
-    double xmax = fmin(pmap.getNx() - 0.5 ,ix + halfpixel);
+    double xmin = fmax(0,ix - halfpixel);
+    double xmax = fmin(NNx,ix + halfpixel);
     if(xmin >= xmax) continue;
 
-    double ymin = fmax(-0.5,iy - halfpixel);
-    double ymax = fmin(pmap.getNx() - 0.5 ,iy + halfpixel);
+    double ymin = fmax(0,iy - halfpixel);
+    double ymax = fmin(NNy,iy + halfpixel);
     if(ymin >= ymax) continue;
     
-    long imin = (long)(xmin + 0.5);
-    long imax = (long)(xmax + 0.5);
-    long jmin = (long)(ymin + 0.5);
-    long jmax = (long)(ymax + 0.5);
+    long imin = (long)(xmin);
+    long imax = (long)(xmax);
+    long jmin = (long)(ymin);
+    long jmax = (long)(ymax);
 
     for(size_t j = jmin ; j <= jmax ; ++j ){
-      double area1 = fmin(ymax,j+0.5) -  fmax(ymin,j-0.5);
+      double area1 = fmin(ymax,j+1) -  fmax(ymin,j);
       if(area1 <= 0.0) continue;
       for(size_t i = imin ; i <= imax ; ++i ){
-        double area = (fmin(xmax,i+0.5) -  fmax(xmin,i-0.5)) * area1 ;
-        map[ii] += pmap.map[i + Nx*j]*area/res_ratio2;
+        double area = (fmin(xmax,i+1) -  fmax(xmin,i)) * area1 ;
+        map[ii] += pmap.map[i + NNx*j]*area/res_ratio2;
       }
     }
   }
 }
 
+void PixelMap::recenter(PosType c[2] /// new center
+){
+  double dc[2];
+  dc[0] = c[0] - center[0];
+  dc[1] = c[1] - center[1];
+  
+  map_boundary_p1[0] = map_boundary_p1[0] + dc[0];
+  map_boundary_p1[1] = map_boundary_p1[1] + dc[1];
+  map_boundary_p2[0] = map_boundary_p2[0] + dc[0];
+  map_boundary_p2[1] = map_boundary_p2[1] + dc[1];
+  
+  center[0] = c[0];
+  center[1] = c[1];
+  
+  return;
+}
+
 /*
 void PixelMap::AddSource(Source &source,int oversample){
   Point_2d s_center;
diff --git a/include/image_processing.h b/include/image_processing.h
index 09da6315..e1ceb569 100644
--- a/include/image_processing.h
+++ b/include/image_processing.h
@@ -295,6 +295,9 @@ class PixelMap
     flipY();
   }
   
+  /// recenter the map without changing anything else
+  void recenter(PosType c[2]);
+
 private:
 	std::valarray map;
   void AddGrid_(const PointList &list,LensingVariable val);
@@ -303,7 +306,7 @@ class PixelMap
 	std::size_t Ny;
 	double resolution,rangeX,rangeY,center[2];
 	double map_boundary_p1[2],map_boundary_p2[2];
-
+  
 	double LeafPixelArea(IndexType i,Branch * branch1);
 	void PointsWithinLeaf(Branch * branch1, std::list  &neighborlist);
 	bool inMapBox(Branch * branch1) const;

From 4c7ab4a9070ef418aea7bd078c8ef6ce8ace612a Mon Sep 17 00:00:00 2001
From: RB Metcalf 
Date: Fri, 26 Jan 2018 10:32:30 +0100
Subject: [PATCH 040/227] Added some useful utility functions.
 Utilities::to_numeric<>() - templated function to convert strings to
 numerical types Utilities::IO::ReadCSVnumerical() - reads csv files of
 numeric data.

---
 TreeCode_link/utilities.cpp |  3 +-
 include/utilities_slsim.h   | 93 ++++++++++++++++++++++++++++++++++++-
 2 files changed, 93 insertions(+), 3 deletions(-)

diff --git a/TreeCode_link/utilities.cpp b/TreeCode_link/utilities.cpp
index 7a0553a8..63756c58 100644
--- a/TreeCode_link/utilities.cpp
+++ b/TreeCode_link/utilities.cpp
@@ -707,7 +707,7 @@ int Utilities::IO::CountColumns(std::string filename,char comment_char
   return  NumberOfEntries(line,deliniator);
 }
 
-size_t Utilities::IO::NumberOfEntries(const std::string &string,char deliniator){
+int Utilities::IO::NumberOfEntries(const std::string &string,char deliniator){
   size_t number = 0;
   auto it = string.begin();
   while(it != string.end()){
@@ -718,3 +718,4 @@ size_t Utilities::IO::NumberOfEntries(const std::string &string,char deliniator)
   return number;
 }
 
+
diff --git a/include/utilities_slsim.h b/include/utilities_slsim.h
index ec1ac073..dace886e 100644
--- a/include/utilities_slsim.h
+++ b/include/utilities_slsim.h
@@ -23,6 +23,29 @@
 
 namespace Utilities
 {
+  /// convert a string to a numerical value of various types
+  template
+  T to_numeric(const std::string &str) {
+    return std::stoi(str);
+  };
+  template<>
+  inline long to_numeric(const std::string &str) {
+    return std::stol(str);
+  };
+  template<>
+  inline int to_numeric(const std::string &str) {
+    return std::stoi(str);
+  };
+  template<>
+  inline float to_numeric(const std::string &str) {
+    return std::stof(str);
+  };
+  template<>
+  inline double to_numeric(const std::string &str) {
+    return std::stod(str);
+  };
+  //********************************************************
+  
   // this is not for the user
   namespace detail
   {
@@ -1352,7 +1375,7 @@ namespace Utilities
       std::cout << "Read " << x.size() << " lines from " << filename << std::endl;
     }
     
-    size_t NumberOfEntries(const std::string &string,char deliniator);
+    int NumberOfEntries(const std::string &string,char deliniator);
     
     /// Count the number of columns in a ASCII data file.
     int CountColumns(std::string filename  /// name of file
@@ -1373,7 +1396,7 @@ namespace Utilities
     /** \brief This function will read in all the numbers from a multi-column
      ASCII data file.
      
-     It will skip the comment lines if the are at the head of the file.  The 
+     It will skip the comment lines if they are at the head of the file.  The 
      number of columns and rows are returned.  The entry at row r and column c will be stored at data[c + column*r].
      
      This function is not particularly fast for large amounts of data.  If the 
@@ -1423,6 +1446,72 @@ namespace Utilities
         std::cout << "Read " << rows << " rows of " << columns << " columns from file " << filename << std::endl;
       }
     }
+  
+    /** \brief Read numerical data from a csv file with a header
+     
+     It will skip the comment lines if they are at the head of the file.  The
+     number of columns and rows are returned.  The entries will be stored at data[column][row].
+     
+     Comments must only be before the data.  There must be a line with the
+     column lines after the comments and before the data.
+     
+     This function is not particularly fast for large amounts of data.  If the
+     number of rows is large it would be best to use data.reserve() to set the capacity of data large enough that no rellocation of memory occurs.
+     */
+
+    template 
+    int ReadCSVnumerical(std::string filename   /// file name to be read
+                ,std::vector > &data  /// output data
+                ,std::vector &column_names /// list of column names
+                ,char comment_char = '#'  /// comment charactor for header
+                ,char deliniator = ','    /// deliniator between values
+                         ){
+    
+      std::ifstream file(filename);
+      // find number of particles
+      if (!file.is_open()){
+        std::cerr << "file " << filename << " cann't be opened." << std::endl;
+        throw std::runtime_error("no file");
+      }
+      std::string line;
+      // read comment lines and first data line
+      do{
+        std::getline(file,line);
+        if(!file) break;  // probably EOF
+      }while(line[0] == comment_char);
+    
+      // read the names
+      std::stringstream          lineStream(line);
+      std::string                cell;
+      column_names.empty();
+      while(std::getline(lineStream,cell, ','))
+      {
+        column_names.push_back(cell);
+      }
+      // This checks for a trailing comma with no data after it.
+      if (!lineStream && cell.empty())
+      {
+         column_names.push_back("");
+      }
+
+      int columns = NumberOfEntries(line,deliniator);
+      //int i = 0;
+      data.resize(columns);
+      for(auto &v : data ) v.empty();
+      while(std::getline(file,line)){
+        // read the names
+        std::stringstream          lineStream(line);
+        std::string                cell;
+        
+        int i=0;
+        while(std::getline(lineStream,cell, deliniator))
+        {
+          data[i].push_back(to_numeric(cell));
+          i = (i+1)%columns;
+        }
+
+      }
+    }
   }
 }
 #endif

From cd7d48f172282a1c174e086c1fd8c17db7b4673d Mon Sep 17 00:00:00 2001
From: RB Metcalf 
Date: Sat, 27 Jan 2018 18:05:35 +0100
Subject: [PATCH 041/227] Added XYcsvLookUp() for inputing a csv file and look
 up entries.

---
 TreeCode_link/utilities.cpp | 175 ++++++++++++++++++++++++++++++++++++
 include/utilities_slsim.h   | 152 ++++++++++++++++++++++++++++++-
 2 files changed, 326 insertions(+), 1 deletion(-)

diff --git a/TreeCode_link/utilities.cpp b/TreeCode_link/utilities.cpp
index 63756c58..824bf0af 100644
--- a/TreeCode_link/utilities.cpp
+++ b/TreeCode_link/utilities.cpp
@@ -718,4 +718,179 @@ int Utilities::IO::NumberOfEntries(const std::string &string,char deliniator){
   return number;
 }
 
+Utilities::XYcsvLookUp::XYcsvLookUp(
+                                    std::string datafile   /// input catalog file in csv format
+                                    ,std::string Xlabel
+                                    ,std::string Ylabel
+                                    ,int Nxbins  /// number of X bins
+                                    ,size_t MaxNumber
+                                    ,bool verbose)
+:filename(datafile)
+{
+  Utilities::IO::ReadCSVnumerical2(datafile,data,column_names,MaxNumber);
+  if(verbose){
+    for(auto name : column_names){ std::cout << name << " " ;}
+    std::cout << std::endl;
+    for(int i=0 ; i < 1 ; ++i){
+      for(auto v : data[i] ) std::cout << v << " " ;
+      std::cout << std::endl;
+    }
+    std::cout << data.size() << " rows with " << column_names.size() << " columns read." << std::endl;
+  }
+  
+  int i=0;
+  Xindex = Yindex = -1;
+  for(auto name : column_names){
+    if(name == Xlabel) Xindex = i;
+    if(name == Ylabel) Yindex = i;
+    ++i;
+  }
+  if(Xindex == -1){
+    std::cerr << filename << " needs a column named " << Xlabel << std::endl;
+    throw std::invalid_argument("No column named: " + Xlabel);
+  }
+  if(Yindex == -1){
+    std::cerr << filename << " needs a column named " << Ylabel << std::endl
+    << " They are :" << std::endl;
+    for(auto c : column_names ) std::cout << c << " ";
+    std::cout << std::endl;
+    throw std::invalid_argument("No column named: " + Ylabel);
+  }
+  
+  //************* test ********************
+  //xmin = 100;
+  //xmax = -1;
+  //for(auto d : data){
+  //  if(d[Xindex] < xmin) xmin = d[Xindex];
+  //  if(d[Xindex] > xmax) xmax = d[Xindex];
+  //}
+  //**********************************
+  
+  // sort by redshift
+  std::sort(data.begin(),data.end(), [this](std::vector &v1,std::vector &v2){return v1[Xindex] < v2[Xindex];});
+  //&v1,std::vector &v2){return v1[1] < v2[1];});
+  NinXbins = data.size()/Nxbins;
+  Xborders.resize(Nxbins);
+  Xborders[0]=0.0;
+  xmin =  data[0][Xindex];
+  xmax =  data.back()[Xindex];
+  if(verbose){
+    std::cout << "min X : "<< data[0][Xindex] << " max X : "
+    << data.back()[Xindex] << std::endl;
+    std::cout << "redshift bins " << std::endl;
+  }
+  for(int i=1 ; i &v1,std::vector &v2){return v1[Yindex] < v2[Yindex];});
+  }
+  
+  current = data.begin();
+}
+
+Utilities::XYcsvLookUp::XYcsvLookUp(
+                                    std::string datafile   /// input catalog file in csv format
+                                    ,std::string Xlabel
+                                    ,std::string Ylabel
+                                    ,std::vector Xbins
+                                    ,size_t MaxNumber
+                                    ,bool verbose)
+:Xborders(Xbins),filename(datafile)
+{
+  
+  Utilities::IO::ReadCSVnumerical2(datafile,data,column_names,MaxNumber);
+  if(verbose){
+    for(auto name : column_names){ std::cout << name << " " ;}
+    std::cout << std::endl;
+    for(int i=0 ; i < 1 ; ++i){
+      for(auto v : data[i] ) std::cout << v << " " ;
+      std::cout << std::endl;
+    }
+    std::cout << data.size() << " rows with " << column_names.size() << " columns read." << std::endl;
+  }
+  
+  int i=0;
+  Xindex = Yindex = -1;
+  for(auto name : column_names){
+    if(name == Xlabel) Xindex = i;
+    if(name == Ylabel) Yindex = i;
+    ++i;
+  }
+  if(Xindex == -1){
+    std::cerr << filename << " needs a column named " << Xlabel << std::endl;
+    throw std::invalid_argument("No column named: " + Xlabel);
+  }
+  if(Yindex == -1){
+    std::cerr << filename << " needs a column named " << Ylabel << std::endl
+    << " They are :" << std::endl;
+    for(auto c : column_names ) std::cout << c << " ";
+    std::cout << std::endl;
+    throw std::invalid_argument("No column named: " + Ylabel);
+  }
+  
+  // sort by redshift
+  std::sort(data.begin(),data.end(), [this](std::vector &v1,std::vector &v2){return v1[Xindex] < v2[Xindex];});
+
+  size_t Nxbins = Xborders.size();
+  NinXbins = data.size()/Nxbins;
+  xmin =  data[0][Xindex];
+  xmax =  data.back()[Xindex];
+  if(verbose){
+    std::cout << "min X : "<< data[0][Xindex] << " max X"
+    << data.back()[Xindex] << std::endl;
+    std::cout << "redshift bins " << std::endl;
+  }
+  
+  // set up iterators to boundaries of x bins
+  borders.resize(Nxbins + 1);
+  borders[0] = data.begin();
+  borders.back() = data.end();
+  for(int i=1 ; i < Nxbins ; ++i) borders[i] = borders[i-1] + NinXbins;
+  
+  // sort by mass within x bins
+  for(int i=0 ; i < Nxbins ; ++i){
+    std::sort(borders[i],borders[i+1], [this](std::vector &v1,std::vector &v2){return v1[Yindex] < v2[Yindex];});
+  }
+  
+  current = data.begin();
+}
+
+std::vector Utilities::XYcsvLookUp::find(double x,double y){
+  long xbin = Utilities::locate(Xborders, x);
+  current = std::upper_bound(borders[xbin],borders[xbin+1],y
+                             , [this](double y,std::vector &v1){return y < v1[Yindex];});
+  
+  return *current;
+}
+double Utilities::XYcsvLookUp::Ymin(double x){
+  long xbin = Utilities::locate(Xborders, x);
+  return (*borders[xbin])[Yindex];
+}
+double Utilities::XYcsvLookUp::Ymax(double x){
+  long xbin = Utilities::locate(Xborders, x);
+  return (*(borders[xbin+1]-1))[Yindex];
+}
+
+double Utilities::XYcsvLookUp::operator[](std::string label){
+  int i = 0;
+  for(auto name : column_names){
+    if(name == label){
+      return (*current)[i];
+    }
+    ++i;
+  }
+  for(auto c : column_names ) std::cout << c << " ";
+  std::cout << std::endl;
+  throw std::invalid_argument(label + " was not one of the columns of the galaxy data file :" + filename);
+}
 
diff --git a/include/utilities_slsim.h b/include/utilities_slsim.h
index dace886e..44908370 100644
--- a/include/utilities_slsim.h
+++ b/include/utilities_slsim.h
@@ -1460,7 +1460,7 @@ namespace Utilities
      */
 
     template 
-    int ReadCSVnumerical(std::string filename   /// file name to be read
+    int ReadCSVnumerical1(std::string filename   /// file name to be read
                 ,std::vector > &data  /// output data
                 ,std::vector &column_names /// list of column names
                 ,char comment_char = '#'  /// comment charactor for header
@@ -1512,6 +1512,156 @@ namespace Utilities
 
       }
     }
+    
+    /** \brief Read numerical data from a csv file with a header
+     
+     Same as ReadCSVnumerical1 except the order of the data storage is
+     reversed data[row][column].
+     
+     It will skip the comment lines if they are at the head of the file.  The
+     number of columns and rows are returned.
+     
+     Comments must only be before the data.  There must be a line with the
+     column lines after the comments and before the data.
+     
+     This function is not particularly fast for large amounts of data.  If the
+     number of rows is large it would be best to use data.reserve() to set the capacity of data large enough that no rellocation of memory occurs.
+     */
+    
+    template 
+    int ReadCSVnumerical2(std::string filename   /// file name to be read
+                         ,std::vector > &data  /// output data
+                         ,std::vector &column_names /// list of column names
+                          ,size_t Nmax = 1000000
+                          ,char comment_char = '#'  /// comment charactor for header
+                         ,char deliniator = ','    /// deliniator between values
+    ){
+      
+      
+      std::ifstream file(filename);
+      // find number of particles
+      if (!file.is_open()){
+        std::cerr << "file " << filename << " cann't be opened." << std::endl;
+        throw std::runtime_error("no file");
+      }
+      std::string line;
+      // read comment lines and first data line
+      do{
+        std::getline(file,line);
+        if(!file) break;  // probably EOF
+      }while(line[0] == comment_char);
+      
+      // read the names
+      std::stringstream          lineStream(line);
+      std::string                cell;
+      column_names.empty();
+      while(std::getline(lineStream,cell, ','))
+      {
+        column_names.push_back(cell);
+      }
+      // This checks for a trailing comma with no data after it.
+      if (!lineStream && cell.empty())
+      {
+        column_names.push_back("");
+      }
+      
+      int columns = NumberOfEntries(line,deliniator);
+      size_t ii = 0;
+    
+      for(auto &v : data ) v.empty();
+      while(std::getline(file,line) && ii < Nmax){
+        
+        data.emplace_back(columns);
+        
+        // read the names
+        std::stringstream          lineStream(line);
+        std::string                cell;
+        
+        int i=0;
+        while(std::getline(lineStream,cell, deliniator))
+        {
+          data.back()[i] = to_numeric(cell);
+          i = (i+1)%columns;
+        }
+        ++ii;
+      }
+    }
+
   }
+  
+  /*** \brief A class for reading and then looking up objects from a CSV catalog.
+   
+   The constructor will read in the whole catalog and sort it into Nxbins X-bins.  Each
+   X-bin is then sorted by Y.
+   
+   The find(x,y) function will set the current value to a galaxy with a x within the
+   x-bin of x and a y close to y.  The other information in the row of the csv file for
+   this entry can then be read.
+   
+   */
+  class XYcsvLookUp{
+  public:
+    XYcsvLookUp(std::string datafile   /// input catalog file in csv format
+                ,std::string Xlabel    /// label in catalog for X variable
+                ,std::string Ylabel    /// label in catalog for Y variable
+                ,int Nxbins            /// number of X bins
+                ,size_t MaxNumber = 1000000 /// maximum number of entries read
+                ,bool verbose = false);
+    XYcsvLookUp(
+                std::string datafile   /// input catalog file in csv format
+                ,std::string Xlabel    /// label in catalog for X variable
+                ,std::string Ylabel    /// label in catalog for Y variable
+                ,std::vector Xbins  /// minimum X value in each bin
+                ,size_t MaxNumber = 1000000 /// maximum number of entries read
+                ,bool verbose = false);
+    
+    /// find a galaxy with a redshift and log(halo mass) near x and logm, return a vector of its properties
+    std::vector find(double x,double y);
+    
+    /// returns the current galxay's property by label
+    double operator[](std::string label);
+    
+    /// returns the ith entry for the current galaxy
+    double operator[](int i){
+      return (*current)[i];
+    }
+    /// returns the redshift of the current galaxy
+    double getX(){
+      return (*current)[Xindex];
+    }
+    /// returns the log(mass) of the current halo
+    double getY(){
+      return (*current)[Yindex];
+    }
+    /// returns a vector of the entries for the current galaxy
+    std::vector record(){
+      return *current;
+    }
+    
+    /// returns labels of the columns from the data file
+    std::vector labels(){
+      return column_names;
+    }
+    
+    double Xmin(){return xmin;}
+    double Xmax(){return xmax;}
+    double Ymin(double x);
+    double Ymax(double x);
+
+    
+  private:
+    int Xindex;
+    int Yindex;
+    double xmax;
+    double xmin;
+    std::vector >::iterator current;
+    std::vector >::iterator> borders;
+    std::vector > data;
+    std::vector column_names;
+    std::vector Xborders;
+    size_t NinXbins;
+    std::string filename;
+  };
+
 }
 #endif

From 345c1455aa967f55834c5b38421c479ba62a4746 Mon Sep 17 00:00:00 2001
From: RB Metcalf 
Date: Sun, 28 Jan 2018 18:09:56 +0100
Subject: [PATCH 042/227] Added XYcsvLookUp() for inputing a csv file and look
 up entries.

---
 Source/overzier.cpp         |  6 +++---
 TreeCode_link/utilities.cpp |  2 +-
 include/utilities_slsim.h   | 20 ++++++++++++++++++--
 3 files changed, 22 insertions(+), 6 deletions(-)

diff --git a/Source/overzier.cpp b/Source/overzier.cpp
index 7c12a95b..645d51e6 100644
--- a/Source/overzier.cpp
+++ b/Source/overzier.cpp
@@ -80,7 +80,7 @@ SourceOverzier& SourceOverzier::operator=(const SourceOverzier &s){
   
   Source::operator=(s);
   /// bulge half light radius
-  Reff = s.Rh;
+  Reff = s.Reff;
   Rh = s.Rh;
   PA = s.PA;
   inclination = s.inclination;
@@ -190,7 +190,7 @@ PosType SourceOverzier::getTotalFlux() const{
 }
 
 void SourceOverzier::printSource(){
-	std::cout << "bulge half light radius: " << Reff << " arcs   disk scale hight: " << Rh << " arcs" << std::endl;
+	std::cout << "bulge half light radius: " << Reff*180*60*60/pi << " arcs   disk scale hight: " << Rh*180*60*60/pi << " arcs" << std::endl;
 }
 
 void SourceOverzier::assignParams(InputParams& /* params */)
@@ -470,7 +470,7 @@ PosType SourceOverzierPlus::SurfaceBrightness(PosType *y){
   Point_2d z;
   PosType sb;
 
-  if(xlength > 0){
+  if(xlength > 0 && sbDo > 0){
     z[0] = cospa*x[0] - sinpa*x[1];
     z[1] = ( sinpa*x[0] + cospa*x[1] )/cosi;
     
diff --git a/TreeCode_link/utilities.cpp b/TreeCode_link/utilities.cpp
index 824bf0af..694e36c6 100644
--- a/TreeCode_link/utilities.cpp
+++ b/TreeCode_link/utilities.cpp
@@ -1,4 +1,4 @@
-/**
+ /**
  * utilities.c
  *
  *  Created on: Sep 8, 2009
diff --git a/include/utilities_slsim.h b/include/utilities_slsim.h
index 44908370..75936e8c 100644
--- a/include/utilities_slsim.h
+++ b/include/utilities_slsim.h
@@ -1637,7 +1637,17 @@ namespace Utilities
     std::vector record(){
       return *current;
     }
-    
+    /// returns a vector of the entries for the current galaxy
+    std::vector operator*(){
+      return *current;
+    }
+
+    void operator++(){
+      if(current != data.end() ) ++current;
+    }
+    void operator--(){
+      if(current != data.begin() ) --current;
+    }
     /// returns labels of the columns from the data file
     std::vector labels(){
       return column_names;
@@ -1647,7 +1657,13 @@ namespace Utilities
     double Xmax(){return xmax;}
     double Ymin(double x);
     double Ymax(double x);
-
+    
+    void printcurrent(){
+      int i = 0;
+      for(auto label : column_names){
+        std::cout << label << " : " << (*current)[i++] << std::endl;
+      }
+    }
     
   private:
     int Xindex;

From c7ae43f29e218f68112d5006ef571afefb62f3ac Mon Sep 17 00:00:00 2001
From: RB Metcalf 
Date: Mon, 29 Jan 2018 15:44:32 +0100
Subject: [PATCH 043/227] Added GridMap::magnification()

---
 TreeCode_link/gridmap.cpp | 10 ++++++++++
 include/gridmap.h         |  3 +++
 2 files changed, 13 insertions(+)

diff --git a/TreeCode_link/gridmap.cpp b/TreeCode_link/gridmap.cpp
index 7954a7b3..a75974de 100644
--- a/TreeCode_link/gridmap.cpp
+++ b/TreeCode_link/gridmap.cpp
@@ -413,3 +413,13 @@ PosType GridMap::EisnsteinArea() const{
   return count*x_range*x_range/Ngrid_init/Ngrid_init;
 }
 
+PosType GridMap::magnification() const{
+  double mag = 0,flux = 0;
+  size_t N = Ngrid_init*Ngrid_init2;
+  for(size_t i=0;i
Date: Mon, 29 Jan 2018 18:36:40 +0100
Subject: [PATCH 044/227] Improved SourceShaplets Improved handling of bands in
 Source Oversier

added SED tag to sources.
---
 AnalyticNSIE/source.cpp          |  71 +++----
 AnalyticNSIE/sourceAnaGalaxy.cpp |  48 +++--
 Source/overzier.cpp              | 319 ++++---------------------------
 TreeCode_link/gridmap.cpp        |   2 +-
 include/InputParams.h            |   4 +-
 include/overzier_source.h        |  42 ++--
 include/source.h                 |  22 ++-
 include/sourceAnaGalaxy.h        |   2 +-
 8 files changed, 130 insertions(+), 380 deletions(-)

diff --git a/AnalyticNSIE/source.cpp b/AnalyticNSIE/source.cpp
index 7b163af5..1b82dff9 100644
--- a/AnalyticNSIE/source.cpp
+++ b/AnalyticNSIE/source.cpp
@@ -493,26 +493,17 @@ PosType Source::integrateFilterSed(std::vector wavel_fil, std::vector

::epsilon(); + else + flux = pow(10,-0.4*(mag+48.6))*inv_hplanck; + current_band = band; + return; } SourceShapelets::SourceShapelets( @@ -593,6 +584,8 @@ SourceShapelets::SourceShapelets( flux = pow(10,-0.4*(mag+48.6))*inv_hplanck; NormalizeFlux(); + + current_band = NoBand; } SourceShapelets::SourceShapelets( @@ -625,35 +618,26 @@ SourceShapelets::SourceShapelets( exit(1); } - CCfits::PHDU& h0 = fp->pHDU(); h0.readKey("BETA", source_r); source_r *= 0.03/180./60./60.*pi; + h0.readKey("SED_TYPE",sed_type); + + h0.readKey("MAG_B",mag_map[F435W]); // ACS F435W band magnitude + h0.readKey("MAG_V",mag_map[F606W]); // ACS F606W band magnitude + h0.readKey("MAG_I",mag_map[F775W]); // ACS F775W band magnitude + h0.readKey("MAG_Z",mag_map[F850LP]);// ACS F850LP band magnitude + h0.readKey("MAG_J",mag_map[F110W]); // NIC3 F110W band magnitude + h0.readKey("MAG_H",mag_map[F160W]); // NIC3 F160W band magnitude + h0.readKey("MAG_u_KIDS",mag_map[KiDS_U]); // u band obtained from SED fitting + h0.readKey("MAG_g_KIDS",mag_map[KiDS_G]); // g band obtained from SED fitting + h0.readKey("MAG_r_KIDS",mag_map[KiDS_R]); // r band obtained from SED fitting + h0.readKey("MAG_i_KIDS",mag_map[KiDS_I]); // i band obtained from SED fitting - h0.readKey("MAG_B", mags[0]); - h0.readKey("MAG_V", mags[1]); - h0.readKey("MAG_I", mags[2]); - h0.readKey("MAG_Z", mags[3]); - h0.readKey("MAG_J", mags[4]); - h0.readKey("MAG_H", mags[5]); - h0.readKey("MAG_u_KIDS", mags[6]); - h0.readKey("MAG_g_KIDS", mags[7]); - h0.readKey("MAG_r_KIDS", mags[8]); - h0.readKey("MAG_i_KIDS", mags[9]); - - for (int i = 0; i < 10; i++) - { - if (mags[i] < 0.) - fluxes[i] = std::numeric_limits::epsilon(); - else - fluxes[i] = pow(10,-0.4*(mags[i]+48.6))*inv_hplanck; - } - // by default, the magnitude is the one in the i band, // whose image has been used for shapelets decomposition - mag = mags[2]; - flux = fluxes[2]; + setActiveBand(KiDS_I); h0.readKey("REDSHIFT", zsource); h0.readKey("ID", id); @@ -665,6 +649,12 @@ SourceShapelets::SourceShapelets( exit(1); #endif + // ??? kluge + mag_map[EUC_VIS] = mag_map.at(KiDS_I); + mag_map[EUC_Y] = mag_map.at(F110W); + mag_map[EUC_J] = mag_map.at(F110W); + mag_map[EUC_H] = mag_map.at(F160W); + NormalizeFlux(); } @@ -738,7 +728,6 @@ void SourceShapelets::NormalizeFlux() } } coeff_flux *= sqrt(pi)*source_r; - } /// Default constructor. Reads in sources from the default catalog. No magnitude limit. @@ -764,7 +753,7 @@ void SourceMultiShapelets::readCatalog() if (shap_input) { SourceShapelets s(shap_file.c_str()); - s.setActiveBand(band); + //s.setActiveBand(band); if (s.getMag() > 0. && s.getMag() < mag_limit) galaxies.push_back(s); shap_input.close(); @@ -778,7 +767,7 @@ void SourceMultiShapelets::readCatalog() } } - + band = galaxies[0].getBand(); } diff --git a/AnalyticNSIE/sourceAnaGalaxy.cpp b/AnalyticNSIE/sourceAnaGalaxy.cpp index 8cc9b011..d1a94f14 100644 --- a/AnalyticNSIE/sourceAnaGalaxy.cpp +++ b/AnalyticNSIE/sourceAnaGalaxy.cpp @@ -227,6 +227,7 @@ void SourceMultiAnaGalaxy::readDataFile(Utilities::RandomNumbers_NR &ran){ addr[19] = &i1; addr[20] = &i2; + /* switch (band){ case SDSS_U: mag = SDSS_u; @@ -260,20 +261,12 @@ void SourceMultiAnaGalaxy::readDataFile(Utilities::RandomNumbers_NR &ran){ mag = Ks_band; mag_bulge = Ks_band_Bulge; break; - /*case i1: - mag = i1; - mag_bulge = i1_Bulge; - break; - case i2: - mag = i2; - mag_bulge = i2_Bulge; - break;*/ default: std::cout << "Requested band is not an available option." << std::endl; ERROR_MESSAGE(); throw std::invalid_argument("band not supported"); - } + }*/ color_cat << GalID << " " << z_app << " " << SDSS_u << " " < 0.0){ spheroid->setMag(mag_bulge); } @@ -594,15 +348,24 @@ void SourceOverzierPlus::randomize(Utilities::RandomNumbers_NR &ran){ PosType tmp = 0.1*(2*ran()-1.); - setUMag(getMag(SDSS_U) + tmp); + for(auto mag = mag_map.begin() ; mag != mag_map.end() ; ++mag){ + mag->second = mag->second + tmp; + } + + for(auto mag = bulge_mag_map.begin() ; mag != bulge_mag_map.end() ; ++mag){ + mag->second = mag->second + tmp; + } + + /* + setUMag(getMag(SDSS_U) + tmp); setGMag(getMag(SDSS_G) + tmp); setRMag(getMag(SDSS_R) + tmp); setIMag(getMag(SDSS_I) + tmp); setZMag(getMag(SDSS_Z) + tmp); setJMag(getMag(J) + tmp); setKMag(getMag(Ks) + tmp); - mag += tmp; - + */ + mag += tmp; PA = pi*ran(); inclination = 0.9*pi/2*ran(); diff --git a/TreeCode_link/gridmap.cpp b/TreeCode_link/gridmap.cpp index a75974de..70bc582b 100644 --- a/TreeCode_link/gridmap.cpp +++ b/TreeCode_link/gridmap.cpp @@ -417,7 +417,7 @@ PosType GridMap::magnification() const{ double mag = 0,flux = 0; size_t N = Ngrid_init*Ngrid_init2; for(size_t i=0;i mag_map; + std::map bulge_mag_map; + // optional position variables }; @@ -161,7 +145,7 @@ class SourceOverzierPlus : public SourceOverzier PosType getSphAxisRatio() const {return spheroid->getAxesRatio();} PosType getSphPA() const {return spheroid->getPA();} - void setBand(Band band); + void changeBand(Band band); static PosType *getx(SourceOverzierPlus &sourceo){return sourceo.getX();} /// Reset the position of the source in radians @@ -175,7 +159,9 @@ class SourceOverzierPlus : public SourceOverzier source_x[1] = my_y; spheroid->setX(my_x,my_y); } - + void setBulgeAxisRatio(PosType q){ + spheroid->setAxesRatio(q); + } /// Randomly change some of the internal paramters and angles of the source void randomize(Utilities::RandomNumbers_NR &ran); private: diff --git a/include/source.h b/include/source.h index 9682e38e..7feced3c 100644 --- a/include/source.h +++ b/include/source.h @@ -174,9 +174,10 @@ class SourceShapelets: public Source{ flux = s.flux; mag = s.mag; ang = s.ang; - for(int i=0; i<10 ; ++i) mags[i] = s.mags[i]; - for(int i=0; i<10 ; ++i) fluxes[i] = s.fluxes[i]; + mag_map = s.mag_map; coeff_flux = s.coeff_flux; + current_band = s.current_band; + sed_type = s.sed_type; } SourceShapelets & operator= (const SourceShapelets &s){ @@ -190,9 +191,10 @@ class SourceShapelets: public Source{ flux = s.flux; mag = s.mag; ang = s.ang; - for(int i=0; i<10 ; ++i) mags[i] = s.mags[i]; - for(int i=0; i<10 ; ++i) fluxes[i] = s.fluxes[i]; + mag_map = s.mag_map; coeff_flux = s.coeff_flux; + current_band = s.current_band; + sed_type = s.sed_type; return *this; } @@ -202,11 +204,15 @@ class SourceShapelets: public Source{ inline PosType getTotalFlux() const {return flux;} inline PosType getRadius() const {return source_r*10.;} inline PosType getMag() const {return mag;} - inline PosType getMag(Band band) const {return mags[band];} - inline PosType getID() const {return id;} + inline PosType getMag(Band band) const {return mag_map.at(band);} + inline Band getBand() const{return current_band;} + inline long getID() const {return id;} + inline float getSEDtype() const {return sed_type;} void setActiveBand(Band band); private: + Band current_band; + float sed_type = -1; void assignParams(InputParams& params); void Hermite(std::vector &hg,int N, PosType x); @@ -216,10 +222,8 @@ class SourceShapelets: public Source{ int id; PosType flux, mag; PosType ang; - PosType mags[10]; - PosType fluxes[10]; + std::map mag_map; PosType coeff_flux; - static Band shape_band[10]; }; /// A uniform surface brightness circular source. diff --git a/include/sourceAnaGalaxy.h b/include/sourceAnaGalaxy.h index fc8202ac..1e50ae93 100644 --- a/include/sourceAnaGalaxy.h +++ b/include/sourceAnaGalaxy.h @@ -85,7 +85,7 @@ class SourceMultiAnaGalaxy: public Source{ /// Set redshift of current source. Only changes the redshift while leaving position fixed. void setZ(PosType my_z){ galaxies[index].setZ(my_z);} void resetBand(Band my_band){ - for(size_t i=0;i Date: Wed, 31 Jan 2018 12:23:23 +0100 Subject: [PATCH 045/227] Changed some things in the Sources to make it not pass pointers to the protected angular position. --- AnalyticNSIE/sourceAnaGalaxy.cpp | 2 +- ImageProcessing/observation.cpp | 2 +- Source/overzier.cpp | 6 +++--- TreeCode_link/map_images.cpp | 6 +++--- TreeCode_link/map_imagesISOP.cpp | 2 +- TreeCode_link/utilities.cpp | 6 +++--- include/overzier_source.h | 4 ++-- include/source.h | 4 ++-- include/sourceAnaGalaxy.h | 4 ++-- include/utilities_slsim.h | 24 +++++++++++++----------- 10 files changed, 31 insertions(+), 29 deletions(-) diff --git a/AnalyticNSIE/sourceAnaGalaxy.cpp b/AnalyticNSIE/sourceAnaGalaxy.cpp index d1a94f14..4a477b11 100644 --- a/AnalyticNSIE/sourceAnaGalaxy.cpp +++ b/AnalyticNSIE/sourceAnaGalaxy.cpp @@ -177,7 +177,7 @@ void SourceMultiAnaGalaxy::readDataFile(Utilities::RandomNumbers_NR &ran){ break; for(int l=0;lgetMag(),p.getReff(),p.spheroid->getPA() ,p.spheroid->getSersicIndex(),p.spheroid->getAxesRatio() - ,p.spheroid->getZ(),p.spheroid->getX()); + ,p.spheroid->getZ(),p.spheroid->getX().x); modes = p.modes; } @@ -250,7 +250,7 @@ SourceOverzierPlus & SourceOverzierPlus::operator=(const SourceOverzierPlus &p){ spheroid = new SourceSersic(p.spheroid->getMag(),p.getReff(),p.spheroid->getPA() ,p.spheroid->getSersicIndex(),p.spheroid->getAxesRatio() - ,p.spheroid->getZ(),p.spheroid->getX()); + ,p.spheroid->getZ(),p.spheroid->getX().x); modes = p.modes; return *this; @@ -409,7 +409,7 @@ void SourceOverzierPlus::randomize(Utilities::RandomNumbers_NR &ran){ double q = 1 + (0.5-1)*ran(); delete spheroid; - spheroid = new SourceSersic(mag_bulge,Reff/arcsecTOradians,-PA + 10*(ran() - 0.5)*pi/180,index,q,zsource,getX()); + spheroid = new SourceSersic(mag_bulge,Reff/arcsecTOradians,-PA + 10*(ran() - 0.5)*pi/180,index,q,zsource,getX().x); for(PosType &mod : modes){ diff --git a/TreeCode_link/map_images.cpp b/TreeCode_link/map_images.cpp index 0aa75964..c5e29b8f 100644 --- a/TreeCode_link/map_images.cpp +++ b/TreeCode_link/map_images.cpp @@ -189,7 +189,7 @@ void ImageFinding::map_images( if(verbose) std::cout << "number of grid points before ImageFinding::find_images_kist: "<< grid->getNumberOfPoints() << std::endl; //ImageFinding::find_images_kist(lens,source->getX(),xmin,grid,Nimages,imageinfo,NimageMax,&Nimagepoints // ,0,true,0,false,true); - ImageFinding::find_images_kist(lens,source->getX(),xmin,grid,Nimages,imageinfo,&Nimagepoints + ImageFinding::find_images_kist(lens,source->getX().x,xmin,grid,Nimages,imageinfo,&Nimagepoints ,0,false,0,verbose); if(verbose) std::cout << "number of grid points after ImageFinding::find_images_kist: "<< grid->getNumberOfPoints() << std::endl; Nsources = 1; @@ -418,7 +418,7 @@ void ImageFinding::map_images( / *******************************************************/ //PointsWithinKist(grid->s_tree,source->getX(),source->source_r_out,imageinfo->imagekist,0); - grid->s_tree->PointsWithinKist_iter(source->getX(),0,source->getRadius(),imageinfo[0].imagekist); + grid->s_tree->PointsWithinKist_iter(source->getX().x,0,source->getRadius(),imageinfo[0].imagekist); // move from source plane to image plane @@ -617,7 +617,7 @@ void ImageFinding::map_images_fixedgrid( PosType x[2]; - grid->s_tree->PointsWithinKist(source->getX(), xmax, imageinfo[0].imagekist, 0); + grid->s_tree->PointsWithinKist(source->getX().x, xmax, imageinfo[0].imagekist, 0); bool move; // Assign surface brightnesses and remove points from image without flux diff --git a/TreeCode_link/map_imagesISOP.cpp b/TreeCode_link/map_imagesISOP.cpp index b2c2c9b3..da91c407 100644 --- a/TreeCode_link/map_imagesISOP.cpp +++ b/TreeCode_link/map_imagesISOP.cpp @@ -66,7 +66,7 @@ void ImageFinding::map_imagesISOP( if(verbose) std::cout << "number of grid points before ImageFinding::find_images_kist: " << grid->getNumberOfPoints() << std::endl; - ImageFinding::find_images_kist(lens,source->getX(),source->getRadius(),grid,Nimages + ImageFinding::find_images_kist(lens,source->getX().x,source->getRadius(),grid,Nimages ,imageinfo,&Nimagepoints,0.0,true,0,verbose); //assert(*Nimages == 1); diff --git a/TreeCode_link/utilities.cpp b/TreeCode_link/utilities.cpp index 694e36c6..ca47851d 100644 --- a/TreeCode_link/utilities.cpp +++ b/TreeCode_link/utilities.cpp @@ -872,16 +872,16 @@ std::vector Utilities::XYcsvLookUp::find(double x,double y){ return *current; } -double Utilities::XYcsvLookUp::Ymin(double x){ +double Utilities::XYcsvLookUp::Ymin(double x) const{ long xbin = Utilities::locate(Xborders, x); return (*borders[xbin])[Yindex]; } -double Utilities::XYcsvLookUp::Ymax(double x){ +double Utilities::XYcsvLookUp::Ymax(double x) const{ long xbin = Utilities::locate(Xborders, x); return (*(borders[xbin+1]-1))[Yindex]; } -double Utilities::XYcsvLookUp::operator[](std::string label){ +double Utilities::XYcsvLookUp::operator[](std::string label) const{ int i = 0; for(auto name : column_names){ if(name == label){ diff --git a/include/overzier_source.h b/include/overzier_source.h index 83a04d52..f1f9670b 100644 --- a/include/overzier_source.h +++ b/include/overzier_source.h @@ -88,7 +88,7 @@ class SourceOverzier : public Source */ inline PosType getMinSize(PosType f) {return std::min(1.678*Reff*fabs(cos(inclination))*pow(-log (f)/7.67,4),Rh*(-log (f)/1.67));} - static PosType *getx(SourceOverzier &sourceo){return sourceo.getX();} + static PosType *getx(SourceOverzier &sourceo){return sourceo.source_x.x;} protected: @@ -146,7 +146,7 @@ class SourceOverzierPlus : public SourceOverzier PosType getSphPA() const {return spheroid->getPA();} void changeBand(Band band); - static PosType *getx(SourceOverzierPlus &sourceo){return sourceo.getX();} + static PosType* getx(SourceOverzierPlus &sourceo){return sourceo.source_x.x;} /// Reset the position of the source in radians virtual inline void setX(PosType *xx){ diff --git a/include/source.h b/include/source.h index 7feced3c..ada64c80 100644 --- a/include/source.h +++ b/include/source.h @@ -68,7 +68,7 @@ class Source /// Reset the radius of the source in radians virtual void setRadius(PosType my_radius){source_r = my_radius;} /// position of source in radians - virtual inline PosType* getX(){return source_x.x;} + virtual inline Point_2d getX(){return source_x;} /// position of source in radians virtual inline void getX(PosType *x) const {x[0] = source_x.x[0]; x[1] = source_x.x[0];} /// position of source in radians @@ -92,7 +92,7 @@ class Source PosType integrateFilter(std::vector wavel_fil, std::vector fil); PosType integrateFilterSed(std::vector wavel_fil, std::vector fil, std::vector wavel_sed, std::vector sed); - static PosType *getx(Source &source){return source.getX();} + static PosType *getx(Source &source){return source.source_x.x;} protected: virtual void assignParams(InputParams& params) = 0; diff --git a/include/sourceAnaGalaxy.h b/include/sourceAnaGalaxy.h index 1e50ae93..229207e4 100644 --- a/include/sourceAnaGalaxy.h +++ b/include/sourceAnaGalaxy.h @@ -93,7 +93,7 @@ class SourceMultiAnaGalaxy: public Source{ /// Return angular position of current source. - PosType *getX(){return galaxies[index].getX();} + Point_2d getX(){return galaxies[index].getX();} /// Set angular position of current source. void setX(PosType my_theta[2]){galaxies[index].setX(my_theta);} void setX(PosType my_x,PosType my_y){galaxies[index].setX(my_x, my_y);} @@ -202,7 +202,7 @@ class SourceMultiShapelets: public Source{ PosType getTotalFlux() const {return pow(10,-(48.6+galaxies[index].getMag())/2.5);} /// Return angular position of current source. - PosType *getX(){return galaxies[index].getX();} + Point_2d getX(){return galaxies[index].getX();} /// Set angular position of current source. void setX(PosType my_theta[2]){galaxies[index].setX(my_theta);} void setX(PosType my_x,PosType my_y){galaxies[index].setX(my_x, my_y);} diff --git a/include/utilities_slsim.h b/include/utilities_slsim.h index 75936e8c..baa54ca1 100644 --- a/include/utilities_slsim.h +++ b/include/utilities_slsim.h @@ -1619,26 +1619,26 @@ namespace Utilities std::vector find(double x,double y); /// returns the current galxay's property by label - double operator[](std::string label); + double operator[](std::string label) const; /// returns the ith entry for the current galaxy - double operator[](int i){ + double operator[](int i) const{ return (*current)[i]; } /// returns the redshift of the current galaxy - double getX(){ + double getX() const { return (*current)[Xindex]; } /// returns the log(mass) of the current halo - double getY(){ + double getY() const { return (*current)[Yindex]; } /// returns a vector of the entries for the current galaxy - std::vector record(){ + std::vector record() const{ return *current; } /// returns a vector of the entries for the current galaxy - std::vector operator*(){ + std::vector operator*() const { return *current; } @@ -1649,14 +1649,16 @@ namespace Utilities if(current != data.begin() ) --current; } /// returns labels of the columns from the data file - std::vector labels(){ + std::vector labels() const{ return column_names; } - double Xmin(){return xmin;} - double Xmax(){return xmax;} - double Ymin(double x); - double Ymax(double x); + double Xmin() const {return xmin;} + double Xmax() const {return xmax;} + /// returns minimum Y value in x-bin + double Ymin(double x) const; + /// returns maximum Y value in x-bin + double Ymax(double x) const; void printcurrent(){ int i = 0; From d51aa799d34f6ce15c48bbf08d44c9ca4dfd8301 Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Wed, 31 Jan 2018 12:24:36 +0100 Subject: [PATCH 046/227] Changed Source::getX() to Source::getTheta() to avoid confusion. --- AnalyticNSIE/source.cpp | 24 +++++----- AnalyticNSIE/sourceAnaGalaxy.cpp | 12 ++--- ImageProcessing/pixelize.cpp | 6 +-- MultiPlane/lens.cpp | 1 - Source/overzier.cpp | 18 ++++---- Source/sersic.cpp | 2 +- TreeCode_link/gridmap.cpp | 4 +- TreeCode_link/map_images.cpp | 78 ++++++++++++++++---------------- TreeCode_link/map_imagesISOP.cpp | 4 +- include/InputParams.h | 2 +- include/image_processing.h | 2 +- include/overzier_source.h | 8 ++-- include/source.h | 10 ++-- include/sourceAnaGalaxy.h | 14 +++--- 14 files changed, 92 insertions(+), 93 deletions(-) diff --git a/AnalyticNSIE/source.cpp b/AnalyticNSIE/source.cpp index 1b82dff9..f23b4a39 100644 --- a/AnalyticNSIE/source.cpp +++ b/AnalyticNSIE/source.cpp @@ -189,23 +189,23 @@ void SourceBLR::printSource(){ } PosType SourceUniform::SurfaceBrightness(PosType *y){ - return (PosType)( (pow(y[0]-getX()[0],2) + pow(y[1]-getX()[1],2)) < source_r*source_r ); + return (PosType)( (pow(y[0]-getTheta()[0],2) + pow(y[1]-getTheta()[1],2)) < source_r*source_r ); } PosType SourceGaussian::SurfaceBrightness(PosType *y){ - return exp( -(pow(y[0]-getX()[0],2) + pow(y[1]-getX()[1],2))/source_gauss_r2 ); + return exp( -(pow(y[0]-getTheta()[0],2) + pow(y[1]-getTheta()[1],2))/source_gauss_r2 ); } // surface brightness for models of the Broad Line Region PosType SourceBLRDisk::SurfaceBrightness(PosType *y){ - PosType x[2] = {y[0]-getX()[0],y[1]-getX()[1]}; + PosType x[2] = {y[0]-getTheta()[0],y[1]-getTheta()[1]}; return blr_surface_brightness_disk(x,this); } PosType SourceBLRSph1::SurfaceBrightness(PosType *y){ - return blr_surface_brightness_spherical_circular_motions(sqrt((pow(y[0]-getX()[0],2) + pow(y[1]-getX()[1],2))),this); + return blr_surface_brightness_spherical_circular_motions(sqrt((pow(y[0]-getTheta()[0],2) + pow(y[1]-getTheta()[1],2))),this); } PosType SourceBLRSph2::SurfaceBrightness(PosType *y){ - return blr_surface_brightness_spherical_random_motions(sqrt((pow(y[0]-getX()[0],2) + pow(y[1]-getX()[1],2))),this); + return blr_surface_brightness_spherical_random_motions(sqrt((pow(y[0]-getTheta()[0],2) + pow(y[1]-getTheta()[1],2))),this); } //void in_source(PosType *y_source,ListHndl sourcelist){ @@ -519,9 +519,9 @@ SourceShapelets::SourceShapelets( zsource = my_z; mag = my_mag; if(my_center != NULL) - setX(my_center[0], my_center[1]); + setTheta(my_center[0], my_center[1]); else - setX(0, 0); + setTheta(0, 0); ang = my_ang; n1 = sqrt(my_coeff.size()); n2 = n1; @@ -545,9 +545,9 @@ SourceShapelets::SourceShapelets( zsource = my_z; mag = my_mag; if(my_center != NULL) - setX(my_center[0], my_center[1]); + setTheta(my_center[0], my_center[1]); else - setX(0, 0); + setTheta(0, 0); ang = my_ang; #ifdef ENABLE_FITS @@ -596,9 +596,9 @@ SourceShapelets::SourceShapelets( :Source() { if(my_center != NULL) - setX(my_center[0], my_center[1]); + setTheta(my_center[0], my_center[1]); else - setX(0, 0); + setTheta(0, 0); ang = my_ang; #ifdef ENABLE_FITS @@ -651,7 +651,7 @@ SourceShapelets::SourceShapelets( // ??? kluge mag_map[EUC_VIS] = mag_map.at(KiDS_I); - mag_map[EUC_Y] = mag_map.at(F110W); + //mag_map[EUC_Y] = mag_map.at(F110W); mag_map[EUC_J] = mag_map.at(F110W); mag_map[EUC_H] = mag_map.at(F160W); diff --git a/AnalyticNSIE/sourceAnaGalaxy.cpp b/AnalyticNSIE/sourceAnaGalaxy.cpp index 4a477b11..ca2cb027 100644 --- a/AnalyticNSIE/sourceAnaGalaxy.cpp +++ b/AnalyticNSIE/sourceAnaGalaxy.cpp @@ -48,7 +48,7 @@ SourceMultiAnaGalaxy::SourceMultiAnaGalaxy( std::cout << "Constructing SourceAnaGalaxy" << std::endl; - readDataFile(ran); + readDataFileMillenn(ran); index = 0; searchtree = new TreeSimpleVec(galaxies.data(),galaxies.size(),1,2,true,SourceOverzierPlus::getx); //searchtree = new TreeSimpleVec(galaxies.data(),galaxies.size(),1,3,true); @@ -60,7 +60,7 @@ SourceMultiAnaGalaxy::~SourceMultiAnaGalaxy() } /// read in galaxies from a Millennium simulation file -void SourceMultiAnaGalaxy::readDataFile(Utilities::RandomNumbers_NR &ran){ +void SourceMultiAnaGalaxy::readDataFileMillenn(Utilities::RandomNumbers_NR &ran){ //int type; //long galid,haloid; @@ -399,10 +399,10 @@ void SourceMultiAnaGalaxy::multiplier( PosType x1[2],x2[2],theta[2]; for(unsigned long i=0;i x2[0]) x2[0] = galaxies[i].getX()[0]; - if(galaxies[i].getX()[1] < x1[1]) x1[1] = galaxies[i].getX()[1]; - if(galaxies[i].getX()[1] > x2[1]) x2[1] = galaxies[i].getX()[1]; + if(galaxies[i].getTheta()[0] < x1[0]) x1[0] = galaxies[i].getTheta()[0]; + if(galaxies[i].getTheta()[0] > x2[0]) x2[0] = galaxies[i].getTheta()[0]; + if(galaxies[i].getTheta()[1] < x1[1]) x1[1] = galaxies[i].getTheta()[1]; + if(galaxies[i].getTheta()[1] > x2[1]) x2[1] = galaxies[i].getTheta()[1]; if(galaxies[i].getZ() > z && galaxies[i].getMag() < mag_cut) ++NtoAdd; } diff --git a/ImageProcessing/pixelize.cpp b/ImageProcessing/pixelize.cpp index c6bbfb8d..70c52dff 100644 --- a/ImageProcessing/pixelize.cpp +++ b/ImageProcessing/pixelize.cpp @@ -1989,7 +1989,7 @@ void MultiGridSmoother::smooth(int Nsmooth,PixelMap &map){ PosType PixelMap::AddSource(Source &source){ Point_2d s_center; - source.getX(s_center); + source.getTheta(s_center); if( s_center[0] + source.getRadius() < map_boundary_p1[0] ) return 0.0; if( s_center[0] - source.getRadius() > map_boundary_p2[0] ) return 0.0; @@ -2012,7 +2012,7 @@ PosType PixelMap::AddSource(Source &source){ PosType PixelMap::AddSource(Source &source,int oversample){ Point_2d s_center; - source.getX(s_center); + source.getTheta(s_center); if( s_center[0] + source.getRadius() < map_boundary_p1[0] ) return 0.0; if( s_center[0] - source.getRadius() > map_boundary_p2[0] ) return 0.0; @@ -2116,7 +2116,7 @@ void PixelMap::recenter(PosType c[2] /// new center /* void PixelMap::AddSource(Source &source,int oversample){ Point_2d s_center; - source.getX(s_center); + source.getTheta(s_center); if( s_center[0] + source.getRadius() < map_boundary_p1[0] ) return; if( s_center[0] - source.getRadius() > map_boundary_p2[0] ) return; diff --git a/MultiPlane/lens.cpp b/MultiPlane/lens.cpp index dcd02ab8..c000d0c7 100644 --- a/MultiPlane/lens.cpp +++ b/MultiPlane/lens.cpp @@ -1895,7 +1895,6 @@ void Lens::readInputSimFileMillennium(bool verbose,DM_Light_Division division_mo std::cout << "Can't open file " << field_input_sim_file << std::endl; ERROR_MESSAGE(); throw std::runtime_error(" Cannot open file."); - exit(1); } // skip through header information in data file diff --git a/Source/overzier.cpp b/Source/overzier.cpp index 94e4a3d4..6d1483d1 100644 --- a/Source/overzier.cpp +++ b/Source/overzier.cpp @@ -113,9 +113,9 @@ void SourceOverzier::setInternals(double my_mag,double my_mag_bulge,double my_Re // position if(my_theta != NULL) - setX(my_theta[0], my_theta[1]); + setTheta(my_theta[0], my_theta[1]); else - setX(0, 0); + setTheta(0, 0); } /// Surface brightness in erg/cm^2/sec/rad^2/Hz @@ -124,8 +124,8 @@ PosType SourceOverzier::SurfaceBrightness( ){ // position relative to center PosType x[2]; - x[0] = y[0]-getX()[0]; - x[1] = y[1]-getX()[1]; + x[0] = y[0]-getTheta()[0]; + x[1] = y[1]-getTheta()[1]; PosType R = cxx*x[0]*x[0] + cyy*x[1]*x[1] + cxy*x[0]*x[1],sb; R = sqrt(R); @@ -231,7 +231,7 @@ Narms(p.Narms),Ad(p.Ad),mctalpha(p.mctalpha),arm_alpha(p.arm_alpha) { spheroid = new SourceSersic(p.spheroid->getMag(),p.getReff(),p.spheroid->getPA() ,p.spheroid->getSersicIndex(),p.spheroid->getAxesRatio() - ,p.spheroid->getZ(),p.spheroid->getX().x); + ,p.spheroid->getZ(),p.spheroid->getTheta().x); modes = p.modes; } @@ -250,7 +250,7 @@ SourceOverzierPlus & SourceOverzierPlus::operator=(const SourceOverzierPlus &p){ spheroid = new SourceSersic(p.spheroid->getMag(),p.getReff(),p.spheroid->getPA() ,p.spheroid->getSersicIndex(),p.spheroid->getAxesRatio() - ,p.spheroid->getZ(),p.spheroid->getX().x); + ,p.spheroid->getZ(),p.spheroid->getTheta().x); modes = p.modes; return *this; @@ -260,8 +260,8 @@ SourceOverzierPlus & SourceOverzierPlus::operator=(const SourceOverzierPlus &p){ PosType SourceOverzierPlus::SurfaceBrightness(PosType *y){ // position relative to center Point_2d x; - x[0] = y[0]-getX()[0]; - x[1] = y[1]-getX()[1]; + x[0] = y[0]-getTheta()[0]; + x[1] = y[1]-getTheta()[1]; PosType xlength = x.length(); @@ -409,7 +409,7 @@ void SourceOverzierPlus::randomize(Utilities::RandomNumbers_NR &ran){ double q = 1 + (0.5-1)*ran(); delete spheroid; - spheroid = new SourceSersic(mag_bulge,Reff/arcsecTOradians,-PA + 10*(ran() - 0.5)*pi/180,index,q,zsource,getX().x); + spheroid = new SourceSersic(mag_bulge,Reff/arcsecTOradians,-PA + 10*(ran() - 0.5)*pi/180,index,q,zsource,getTheta().x); for(PosType &mod : modes){ diff --git a/Source/sersic.cpp b/Source/sersic.cpp index ac128e12..2fc5f9a9 100644 --- a/Source/sersic.cpp +++ b/Source/sersic.cpp @@ -26,7 +26,7 @@ SourceSersic::SourceSersic( setZ(my_z); if(my_theta) - setX(my_theta[0], my_theta[1]); + setTheta(my_theta[0], my_theta[1]); if(q > 1) throw std::invalid_argument("Error: q must be < 1!"); diff --git a/TreeCode_link/gridmap.cpp b/TreeCode_link/gridmap.cpp index 70bc582b..1645c97c 100644 --- a/TreeCode_link/gridmap.cpp +++ b/TreeCode_link/gridmap.cpp @@ -417,9 +417,9 @@ PosType GridMap::magnification() const{ double mag = 0,flux = 0; size_t N = Ngrid_init*Ngrid_init2; for(size_t i=0;i &imageinfo /// information on each image ,PosType xmax /// Maximum size of source on image plane. The entire source must be within this distance from - /// source->getX()[] + /// source->getTheta()[] ,PosType xmin /// The smallest scale of the source measured on the lens plane. The more accurate these /// 2 parameters are the less likely it is that an image will be missed. ,PosType initial_size /// Initial size of source for telescoping, 0 to start from the initial grid size. @@ -41,7 +41,7 @@ void ImageFinding::map_images( //,bool kappa_off /// turns off calculation of surface density, shear, magnification and time delay ,bool FindCenter /// if the center of the source is not known this can be set to true and it will attempt to /// find the center, find the size of the source and determine if there is more than one source - /// The entire source must be within xmax of source->getX() because this is the only + /// The entire source must be within xmax of source->getTheta() because this is the only /// region that will be scanned. ,bool divide_images /// if true will divide images and apply the exit criterion to them separately. ){ @@ -94,8 +94,8 @@ void ImageFinding::map_images( if(sb > 0.0){ newpoint = NewPointArray(1); - newpoint->x[0] = y[0] + source->getX()[0]; - newpoint->x[1] = y[1] + source->getX()[1]; + newpoint->x[0] = y[0] + source->getTheta()[0]; + newpoint->x[1] = y[1] + source->getTheta()[1]; newpoint->image = newpoint; newpoint->surface_brightness = sb; newpoint->gridsize = 2*xmax/(Ntmp-1); @@ -144,8 +144,8 @@ void ImageFinding::map_images( do{ rs[i] = MAX(rs[i],pow(sourceinfo[i].imagekist->getCurrent()->x[0] - sourceinfo[i].centroid[0],2) + pow(sourceinfo[i].imagekist->getCurrent()->x[1] - sourceinfo[i].centroid[1],2) ); - //xx[0] = getCurrentKist(sourceinfo[i].imagekist)->x[0] - source->getX()[0]; - //xx[1] = getCurrentKist(sourceinfo[i].imagekist)->x[1] - source->getX()[1]; + //xx[0] = getCurrentKist(sourceinfo[i].imagekist)->x[0] - source->getTheta()[0]; + //xx[1] = getCurrentKist(sourceinfo[i].imagekist)->x[1] - source->getTheta()[1]; sourceinfo[i].area += source->SurfaceBrightness(sourceinfo[i].imagekist->getCurrent()->x)*pow(2*xmax/(Ntmp-1),2); }while(sourceinfo[i].imagekist->Down()); @@ -154,8 +154,8 @@ void ImageFinding::map_images( // filling factor or holiness of source if(verbose) printf("holiness of source %e\n",sourceinfo[i].imagekist->Nunits()*pow(2*xmax/(Ntmp-1)/rs[i],2)/pi); - if(verbose) printf(" dx = %e %e rs = %e Npoints = %li\n",(source->getX()[0]-sourceinfo[i].centroid[0])/xmin - ,(source->getX()[1]-sourceinfo[i].centroid[1])/xmin + if(verbose) printf(" dx = %e %e rs = %e Npoints = %li\n",(source->getTheta()[0]-sourceinfo[i].centroid[0])/xmin + ,(source->getTheta()[1]-sourceinfo[i].centroid[1])/xmin ,rs[i],sourceinfo[i].imagekist->Nunits()); } @@ -168,8 +168,8 @@ void ImageFinding::map_images( center[0] = sourceinfo[0].centroid[0]; center[1] = sourceinfo[0].centroid[1]; }else{ - center[0] = source->getX()[0]; - center[1] = source->getX()[1]; + center[0] = source->getTheta()[0]; + center[1] = source->getTheta()[1]; } ImageFinding::find_images_kist(lens,center,rs[0],grid,Nimages,imageinfo,&Nimagepoints,initial_size,true,0,false); for(i=1;igetX()[0]; - center[1] = source->getX()[1]; + center[0] = source->getTheta()[0]; + center[1] = source->getTheta()[1]; } ImageFinding::find_images_kist(lens,center,rs[i],grid,Nimages,imageinfo,&Nimagepoints,xmax,true,0,false); } @@ -187,14 +187,14 @@ void ImageFinding::map_images( delete[] rs; }else{ if(verbose) std::cout << "number of grid points before ImageFinding::find_images_kist: "<< grid->getNumberOfPoints() << std::endl; - //ImageFinding::find_images_kist(lens,source->getX(),xmin,grid,Nimages,imageinfo,NimageMax,&Nimagepoints + //ImageFinding::find_images_kist(lens,source->getTheta(),xmin,grid,Nimages,imageinfo,NimageMax,&Nimagepoints // ,0,true,0,false,true); - ImageFinding::find_images_kist(lens,source->getX().x,xmin,grid,Nimages,imageinfo,&Nimagepoints + ImageFinding::find_images_kist(lens,source->getTheta().x,xmin,grid,Nimages,imageinfo,&Nimagepoints ,0,false,0,verbose); if(verbose) std::cout << "number of grid points after ImageFinding::find_images_kist: "<< grid->getNumberOfPoints() << std::endl; Nsources = 1; - sourceinfo[0].centroid[0] = source->getX()[0]; - sourceinfo[0].centroid[1] = source->getX()[1]; + sourceinfo[0].centroid[0] = source->getTheta()[0]; + sourceinfo[0].centroid[1] = source->getTheta()[1]; sourceinfo[0].area = source->getTotalFlux(); } @@ -261,8 +261,8 @@ void ImageFinding::map_images( }while(MoveDownKist(imageinfo[i].imagekist)); printf(" sb = %e\n",source->SurfaceBrightness(centers[i])); - centers[i][0] += source->getX()[0]; - centers[i][1] += source->getX()[1]; + centers[i][0] += source->getTheta()[0]; + centers[i][1] += source->getTheta()[1]; if(rs[i] == 0.0) rs[i] = 2*source->source_r_out/Ntmp; } @@ -277,13 +277,13 @@ void ImageFinding::map_images( freeTree(tree); }else{ - ImageFinding::find_images_kist(source->getX(),xmin,grid,Nimages + ImageFinding::find_images_kist(source->getTheta(),xmin,grid,Nimages ,imageinfo,NimageMax,&Nimagepoints,initial_size,true,0,false,true); } */ //if(oldr==0) oldr=source->source_r_out; - //if((oldy[0]==source->getX()[0])*(oldy[1]==source->getX()[1])*(oldr > source->source_r_out)) initial_size=oldr; + //if((oldy[0]==source->getTheta()[0])*(oldy[1]==source->getTheta()[1])*(oldr > source->source_r_out)) initial_size=oldr; if(source->getRadius() <= 0.0){ERROR_MESSAGE(); printf("ERROR: find_images, point source must have a resolution target\n"); exit(1);} @@ -312,8 +312,8 @@ void ImageFinding::map_images( xsMin[0] = xsMin[1] = 1.0e100; sbmax = 0; ssize = 0.0; - center[0] = source->getX()[0]; - center[1] = source->getX()[1]; + center[0] = source->getTheta()[0]; + center[1] = source->getTheta()[1]; r_source = xmin; grid->ClearAllMarks(); @@ -332,8 +332,8 @@ void ImageFinding::map_images( point = getCurrentKist(imageinfo->imagekist); //find surface brightnesses - y[0] = point->image->x[0] - source->getX()[0]; - y[1] = point->image->x[1] - source->getX()[1]; + y[0] = point->image->x[0] - source->getTheta()[0]; + y[1] = point->image->x[1] - source->getTheta()[1]; point->surface_brightness = source->SurfaceBrightness(y); point->image->surface_brightness = point->surface_brightness; @@ -398,7 +398,7 @@ void ImageFinding::map_images( assert(tmp > 0.0 || imageinfo[0].imagekist->Nunits() == 0); /*/********** test lines ********************** - PointsWithinKist_iter(grid->s_tree,source->getX(),0,source->source_r_out,imageinfo->imagekist); + PointsWithinKist_iter(grid->s_tree,source->getTheta(),0,source->source_r_out,imageinfo->imagekist); Ntmp = imageinfo->imagekist->Nunits(); for(i=0,MoveToTopKist(imageinfo->imagekist); i < Ntmp ; ++i ){ @@ -417,8 +417,8 @@ void ImageFinding::map_images( printf("number of sources %i\n",*Nimages); / *******************************************************/ - //PointsWithinKist(grid->s_tree,source->getX(),source->source_r_out,imageinfo->imagekist,0); - grid->s_tree->PointsWithinKist_iter(source->getX().x,0,source->getRadius(),imageinfo[0].imagekist); + //PointsWithinKist(grid->s_tree,source->getTheta(),source->source_r_out,imageinfo->imagekist,0); + grid->s_tree->PointsWithinKist_iter(source->getTheta().x,0,source->getRadius(),imageinfo[0].imagekist); // move from source plane to image plane @@ -527,8 +527,8 @@ void ImageFinding::map_images( //printf("i=%i Nold=%li\n",i,Nold); //printf("%li\n",imagelist->Npoints); - oldy[0] = source->getX()[0]; - oldy[1] = source->getX()[1]; + oldy[0] = source->getTheta()[0]; + oldy[1] = source->getTheta()[1]; oldr = source->getRadius(); oldNimages=*Nimages; @@ -591,7 +591,7 @@ void ImageFinding::map_images_fixedgrid( ,int *Nimages /// number of images found ,std::vector &imageinfo /// information on each image ,PosType xmax /// Maximum size of source on image plane. The entire source must be within this distance from - /// source->getX()[]. Decreasing it will make the code run faster. Making xmax much bigger than + /// source->getTheta()[]. Decreasing it will make the code run faster. Making xmax much bigger than /// the grid boundaries will check all points for surface brightness. ,bool divide_images /// if true will divide images. ,bool find_borders /// if true will find the inner and outer borders of each image @@ -617,14 +617,14 @@ void ImageFinding::map_images_fixedgrid( PosType x[2]; - grid->s_tree->PointsWithinKist(source->getX().x, xmax, imageinfo[0].imagekist, 0); + grid->s_tree->PointsWithinKist(source->getTheta().x, xmax, imageinfo[0].imagekist, 0); bool move; // Assign surface brightnesses and remove points from image without flux for(imageinfo[0].imagekist->MoveToTop();!(imageinfo[0].imagekist->OffBottom());){ - x[0] = imageinfo[0].imagekist->getCurrent()->x[0] - source->getX()[0]; - x[1] = imageinfo[0].imagekist->getCurrent()->x[1] - source->getX()[1]; + x[0] = imageinfo[0].imagekist->getCurrent()->x[0] - source->getTheta()[0]; + x[1] = imageinfo[0].imagekist->getCurrent()->x[1] - source->getTheta()[1]; imageinfo[0].imagekist->getCurrent()->surface_brightness = imageinfo[0].imagekist->getCurrent()->image->surface_brightness @@ -828,8 +828,8 @@ int ImageFinding::IF_routines::refine_grid_on_image(Lens *lens,Source *source,Gr // put point into image imageinfo[i].imagekist - //y[0] = i_points[k].image->x[0];// - source->getX()[0]; - //y[1] = i_points[k].image->x[1];// - source->getX()[1]; + //y[0] = i_points[k].image->x[0];// - source->getTheta()[0]; + //y[1] = i_points[k].image->x[1];// - source->getTheta()[1]; i_points[k].surface_brightness = source->SurfaceBrightness(i_points[k].image->x); i_points[k].image->surface_brightness = i_points[k].surface_brightness; @@ -917,8 +917,8 @@ int ImageFinding::IF_routines::refine_grid_on_image(Lens *lens,Source *source,Gr for(k=0;k < i_points->head ;++k){ // put point into image imageinfo[i].outerborder - //y[0] = i_points[k].image->x[0] - source->getX()[0]; - //y[1] = i_points[k].image->x[1] - source->getX()[1]; + //y[0] = i_points[k].image->x[0] - source->getTheta()[0]; + //y[1] = i_points[k].image->x[1] - source->getTheta()[1]; i_points[k].surface_brightness = source->SurfaceBrightness(i_points[k].image->x); i_points[k].image->surface_brightness = i_points[k].surface_brightness; @@ -1058,8 +1058,8 @@ void ImageFinding::IF_routines::check_sb_add(Source *source,ImageInfo *imageinfo // put point into image imageinfo[i].imagekist - //y[0] = i_points[k].image->x[0];// - source->getX()[0]; - //y[1] = i_points[k].image->x[1];// - source->getX()[1]; + //y[0] = i_points[k].image->x[0];// - source->getTheta()[0]; + //y[1] = i_points[k].image->x[1];// - source->getTheta()[1]; i_points[k].surface_brightness = source->SurfaceBrightness(i_points[k].image->x); i_points[k].image->surface_brightness = i_points[k].surface_brightness; diff --git a/TreeCode_link/map_imagesISOP.cpp b/TreeCode_link/map_imagesISOP.cpp index da91c407..b6548e9d 100644 --- a/TreeCode_link/map_imagesISOP.cpp +++ b/TreeCode_link/map_imagesISOP.cpp @@ -30,7 +30,7 @@ void ImageFinding::map_imagesISOP( ,int *Nimages /// number of images found ,std::vector &imageinfo /// information on each image ,PosType rmax /// Maximum size of source on souce plane. The entire source must be within this distance from - /// source->getX()[] + /// source->getTheta()[] ,PosType res_min /// requred resolution of image, typically the pixel size of the final image ,PosType initial_size /// Initial size of source for telescoping, 0 to start from the initial grid size. /// If < 0 no telescoping is used and only the already existing points are used to @@ -66,7 +66,7 @@ void ImageFinding::map_imagesISOP( if(verbose) std::cout << "number of grid points before ImageFinding::find_images_kist: " << grid->getNumberOfPoints() << std::endl; - ImageFinding::find_images_kist(lens,source->getX().x,source->getRadius(),grid,Nimages + ImageFinding::find_images_kist(lens,source->getTheta().x,source->getRadius(),grid,Nimages ,imageinfo,&Nimagepoints,0.0,true,0,verbose); //assert(*Nimages == 1); diff --git a/include/InputParams.h b/include/InputParams.h index 7e88e1d4..eacfe9df 100644 --- a/include/InputParams.h +++ b/include/InputParams.h @@ -70,7 +70,7 @@ typedef enum {nfw,powerlaw,pointmass} ClumpInternal; enum IMFtype {One,Mono,BrokenPowerLaw,Salpeter,SinglePowerLaw,Kroupa,Chabrier}; /// Photometric bands enum Band {NoBand,EUC_VIS,EUC_Y,EUC_J,EUC_H,SDSS_U,SDSS_G,SDSS_R,SDSS_I,SDSS_Z - ,KiDS_U,KiDS_G,KiDS_R,KiDS_I + ,KiDS_U,KiDS_G,KiDS_R,KiDS_I,DES_R,DES_G,DES_I,DES_Z ,J,H,Ks,IRAC1,IRAC2,F435W,F606W,F775W,F850LP,F814W,F110W,F160W}; std::ostream &operator<<(std::ostream &os, Band const &p); diff --git a/include/image_processing.h b/include/image_processing.h index e1ceb569..f6fcdc6d 100644 --- a/include/image_processing.h +++ b/include/image_processing.h @@ -78,7 +78,7 @@ class PixelMap template PosType AddSource_parallel(T &source,int oversample){ Point_2d s_center; - source.getX(s_center); + source.getTheta(s_center); if( s_center[0] + source.getRadius() < map_boundary_p1[0] ) return 0.0; if( s_center[0] - source.getRadius() > map_boundary_p2[0] ) return 0.0; diff --git a/include/overzier_source.h b/include/overzier_source.h index f1f9670b..96778e4a 100644 --- a/include/overzier_source.h +++ b/include/overzier_source.h @@ -149,15 +149,15 @@ class SourceOverzierPlus : public SourceOverzier static PosType* getx(SourceOverzierPlus &sourceo){return sourceo.source_x.x;} /// Reset the position of the source in radians - virtual inline void setX(PosType *xx){ + virtual inline void setTheta(PosType *xx){ source_x[0] = xx[0]; source_x[1] = xx[1]; - spheroid->setX(xx); + spheroid->setTheta(xx); } - virtual void setX(PosType my_x,PosType my_y){ + virtual void setTheta(PosType my_x,PosType my_y){ source_x[0] = my_x; source_x[1] = my_y; - spheroid->setX(my_x,my_y); + spheroid->setTheta(my_x,my_y); } void setBulgeAxisRatio(PosType q){ spheroid->setAxesRatio(q); diff --git a/include/source.h b/include/source.h index ada64c80..28483f84 100644 --- a/include/source.h +++ b/include/source.h @@ -68,15 +68,15 @@ class Source /// Reset the radius of the source in radians virtual void setRadius(PosType my_radius){source_r = my_radius;} /// position of source in radians - virtual inline Point_2d getX(){return source_x;} + virtual inline Point_2d getTheta(){return source_x;} /// position of source in radians - virtual inline void getX(PosType *x) const {x[0] = source_x.x[0]; x[1] = source_x.x[0];} + virtual inline void getTheta(PosType *x) const {x[0] = source_x.x[0]; x[1] = source_x.x[0];} /// position of source in radians - virtual inline void getX(Point_2d &x) const {x = source_x;} + virtual inline void getTheta(Point_2d &x) const {x = source_x;} /// Reset the position of the source in radians - virtual inline void setX(PosType *xx){source_x[0] = xx[0]; source_x[1] = xx[1];} - virtual void setX(PosType my_x,PosType my_y){source_x[0] = my_x; source_x[1] = my_y;} + virtual inline void setTheta(PosType *xx){source_x[0] = xx[0]; source_x[1] = xx[1];} + virtual void setTheta(PosType my_x,PosType my_y){source_x[0] = my_x; source_x[1] = my_y;} /// In the case of a single plane lens, the ratio of angular size distances virtual inline PosType getDlDs(){return DlDs;} diff --git a/include/sourceAnaGalaxy.h b/include/sourceAnaGalaxy.h index 229207e4..af9fb67d 100644 --- a/include/sourceAnaGalaxy.h +++ b/include/sourceAnaGalaxy.h @@ -93,10 +93,10 @@ class SourceMultiAnaGalaxy: public Source{ /// Return angular position of current source. - Point_2d getX(){return galaxies[index].getX();} + Point_2d getTheta(){return galaxies[index].getTheta();} /// Set angular position of current source. - void setX(PosType my_theta[2]){galaxies[index].setX(my_theta);} - void setX(PosType my_x,PosType my_y){galaxies[index].setX(my_x, my_y);} + void setTheta(PosType my_theta[2]){galaxies[index].setTheta(my_theta);} + void setTheta(PosType my_x,PosType my_y){galaxies[index].setTheta(my_x, my_y);} std::size_t getNumberOfGalaxies() const {return galaxies.size();} @@ -163,7 +163,7 @@ class SourceMultiAnaGalaxy: public Source{ TreeSimpleVec *searchtree; std::string input_gal_file; - void readDataFile(Utilities::RandomNumbers_NR &ran); + void readDataFileMillenn(Utilities::RandomNumbers_NR &ran); void assignParams(InputParams& params); PosType rangex[2],rangey[2]; @@ -202,10 +202,10 @@ class SourceMultiShapelets: public Source{ PosType getTotalFlux() const {return pow(10,-(48.6+galaxies[index].getMag())/2.5);} /// Return angular position of current source. - Point_2d getX(){return galaxies[index].getX();} + Point_2d getTheta(){return galaxies[index].getTheta();} /// Set angular position of current source. - void setX(PosType my_theta[2]){galaxies[index].setX(my_theta);} - void setX(PosType my_x,PosType my_y){galaxies[index].setX(my_x, my_y);} + void setTheta(PosType my_theta[2]){galaxies[index].setTheta(my_theta);} + void setTheta(PosType my_x,PosType my_y){galaxies[index].setTheta(my_x, my_y);} /// Return redshift of current source. PosType getZ() const {return galaxies[index].getZ();} From a249d25d3635b85dd8019fc41e6fb880bacdd276 Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Tue, 27 Feb 2018 13:42:15 +0100 Subject: [PATCH 047/227] Added Observation::AddNoiseFromCorr() Added PixelMap::convolve() --- ImageProcessing/observation.cpp | 17 +++++- ImageProcessing/pixelize.cpp | 45 +++++++++++++- include/image_processing.h | 102 ++++++++++++++++++-------------- 3 files changed, 114 insertions(+), 50 deletions(-) diff --git a/ImageProcessing/observation.cpp b/ImageProcessing/observation.cpp index 88a5a308..2f505ff3 100644 --- a/ImageProcessing/observation.cpp +++ b/ImageProcessing/observation.cpp @@ -470,9 +470,6 @@ float Observation::getBackgroundNoise(float resolution, unitType unit) if (unit==flux) rms *= pow(10,-0.4*mag_zeropoint); return rms; - - - } /// Applies realistic noise (read-out + Poisson) on an image @@ -516,6 +513,20 @@ PixelMap Observation::AddNoise(PixelMap &pmap,long *seed) return outmap; } +void Observation::AddNoiseFromCorr(PixelMap &input,PixelMap &output + ,PixelMap &sqrt_coor_noise + ,Utilities::RandomNumbers_NR &ran + ){ + + size_t N = output.size(); + for(size_t i = 0 ; i < N ; ++i) + output[i] = ran.gauss(); + + output.convolve(sqrt_coor_noise); + + output += input; +} + /// Translates photon flux (in 1/(s*cm^2*Hz*hplanck)) into telescope pixel counts PixelMap Observation::PhotonToCounts(PixelMap &pmap) { diff --git a/ImageProcessing/pixelize.cpp b/ImageProcessing/pixelize.cpp index 70c52dff..a8fa5969 100644 --- a/ImageProcessing/pixelize.cpp +++ b/ImageProcessing/pixelize.cpp @@ -217,7 +217,7 @@ PixelMap::PixelMap( h0.readKey("PHYSICALSIZE",ps); } catch(CCfits::HDU::NoSuchKeyword){ - std::cerr << "PixelMap input fits fiel must have header keywords:" << std::endl + std::cerr << "PixelMap input fits field must have header keywords:" << std::endl << " PHYSICALSIZE - size of map in degrees" < points,PosType size,PosType value){ + if(size < resolution*3){ + size_t index; + for(int i=0;ix)){ + //index = Utilities::IndexFromPosition(x1,Nx,range,center); + index = find_index(points[i]->x); + map[index] = value; + } + } + }else + for(int i=0;ix,0.01*rangeX,value); + +} +void PixelMap::drawPoints(std::vector points,PosType size,PosType value){ + if(size < resolution*3){ + size_t index; + for(int i=0;i points,PosType size,PosType value){ + if(size < resolution*3){ + size_t index; + for(int i=0;i points,PosType size,PosType value){ - if(size < resolution*3){ - size_t index; - for(int i=0;ix)){ - //index = Utilities::IndexFromPosition(x1,Nx,range,center); - index = find_index(points[i]->x); - map[index] = value; - } - } - }else - for(int i=0;ix,0.01*rangeX,value); - - } - void drawPoints(std::vector points,PosType size,PosType value){ - if(size < resolution*3){ - size_t index; - for(int i=0;i points,PosType size,PosType value); + + void drawPoints(std::vector points,PosType size,PosType value); + void drawCurve(std::vector points,PosType value){ for(int i=0;ix,points[i+1]->x,value); } void drawCurve(std::vector points,PosType value){ for(int i=0;i points,PosType size,PosType value){ - if(size < resolution*3){ - size_t index; - for(int i=0;i points,PosType size,PosType value); void drawCurve(std::vector points,PosType value){ for(int i=0;i tmp = Utilities::AdaptiveSmooth(data(),Nx,Ny,value); map = tmp; } @@ -297,6 +259,47 @@ class PixelMap /// recenter the map without changing anything else void recenter(PosType c[2]); + + /** \brief convolve the image with a kernel. + + It is assumed that the size of the kernel is much smaller than the image and + that the kernal has the same pixel size as the image. + **/ + void convolve(PixelMap &kernel,long center_x = 0,long center_y = 0){ + std::valarray output(Nx*Ny); + + if( center_x == 0 ){ + center_x = kernel.getNx()/2; + center_y = kernel.getNy()/2; + } + + size_t Nxk = kernel.getNx(); + size_t Nyk = kernel.getNy(); + + for(size_t k1 = 0 ; k1 < Nx ; ++k1){ + long bl1 = k1 - center_x; + + for(size_t k2 = 0 ; k2 < Ny ; ++k2){ + long bl2 = k2 - center_y; + + long k = k1 + Nx * k2; + output[k] = 0; + + for(size_t j = 0 ; j < Nyk ; ++j){ + long kk2 = bl2 + j; + if(kk2 < 0 || kk2 > Ny-1) continue; + + for(size_t i = 0; i < Nxk ; ++i){ + long kk1 = bl1 + i; + if(kk1 < 0 || kk1 > Nx-1) continue; + + output[k] += map[ kk1 + Nx * kk2 ] * kernel[i + Nxk * j]; + } + } + } + } + std::swap(map,output); +} private: std::valarray map; @@ -368,7 +371,9 @@ class Observation Observation(Telescope tel_name); Observation(float diameter, float transmission, float exp_time, int exp_num, float back_mag, float ron, float seeing = 0.); Observation(float diameter, float transmission, float exp_time, int exp_num, float back_mag, float ron, std::string psf_file, float oversample = 1.); - float getExpTime(){return exp_time;} + + + float getExpTime(){return exp_time;} int getExpNum(){return exp_num;} float getBackMag(){return back_mag;} float getDiameter(){return diameter;} @@ -386,7 +391,12 @@ class Observation PixelMap Convert(PixelMap &map, bool psf, bool noise,long *seed, unitType unit = counts_x_sec); double flux_convertion_factor(); PixelMap Convert_back(PixelMap &map); - void setExpTime(float time){exp_time = time;} + void setExpTime(float time){exp_time = time;} + + static void AddNoiseFromCorr(PixelMap &input,PixelMap &output + ,PixelMap &sqrt_coor_noise + ,Utilities::RandomNumbers_NR &ran + ); private: float diameter; // diameter of telescope (in cm) @@ -403,9 +413,11 @@ class Observation bool telescope; // was the observation created from a default telescope? PixelMap AddNoise(PixelMap &pmap,long *seed); + PixelMap PhotonToCounts(PixelMap &pmap); PixelMap ApplyPSF(PixelMap &pmap); + PixelMap noise_correlation; }; void pixelize(double *map,long Npixels,double range,double *center From e0ac4b5439dea8ddf091a25153e6a5631a8d3a42 Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Thu, 22 Mar 2018 14:06:04 +0100 Subject: [PATCH 048/227] Rewrote PixelMap::count_islands() Wrote PixelMap::duplicate() --- ImageProcessing/pixelize.cpp | 70 ++++++++++++++++++++++++++++++++---- include/image_processing.h | 17 ++++++--- 2 files changed, 76 insertions(+), 11 deletions(-) diff --git a/ImageProcessing/pixelize.cpp b/ImageProcessing/pixelize.cpp index a8fa5969..93338aa4 100644 --- a/ImageProcessing/pixelize.cpp +++ b/ImageProcessing/pixelize.cpp @@ -331,17 +331,19 @@ PixelMap::PixelMap( map[ix+Nx*iy] += area*pmap.map[old_ix+old_Nx*old_iy]; } } - } + } } -PixelMap::~PixelMap() -{ - map.resize(0); -} +//PixelMap::~PixelMap(){} +//{ +// map.resize(0); +//} PixelMap& PixelMap::operator=(PixelMap other) { - if(this != &other) PixelMap::swap(*this, other); + if(this != &other){ + PixelMap::swap(*this, other); + } return *this; } @@ -679,6 +681,47 @@ bool PixelMap::pixels_are_neighbors(size_t i,size_t j) const{ return true; } +int PixelMap::count_islands(std::vector &pixel_index) const{ + + if(pixel_index.size() == 0) return 0; + if(pixel_index.size() == 1) return 1; + + size_t *end = pixel_index.data() + pixel_index.size() + 1; + size_t *current = pixel_index.data(); + int Ngroups = 0; + size_t *group_boundary = current + 1; + + while(current != end){ + long ic = *current%Nx; + long jc = *current/Nx; + + int Neighbors = 0; + for(size_t *test = group_boundary + ; test != end && Neighbors < 8 + ; ++test,++Neighbors + ){ + long it = *test%Nx; + long jt = *test/Nx; + + if( abs(it - ic) == 1 || abs(jt - jc) == 1 ){ + // swap test for group boundary + size_t tmp = *group_boundary; + *test = tmp; + *group_boundary = *test; + ++group_boundary; + --test; + } + } + ++current; + if(current == group_boundary){ + ++group_boundary; + ++Ngroups; + } + } + return Ngroups; +} + + /* int PixelMap::count_islands(std::list &pixel_index,std::vector::iterator> &heads) const{ heads.clear(); @@ -715,7 +758,8 @@ int PixelMap::count_islands(std::list &pixel_index,std::vector &reservoir ,std::list::iterator &group) const{ @@ -2083,6 +2127,18 @@ PosType PixelMap::AddSource(Source &source,int oversample){ return total; } +void PixelMap::duplicate( + const PixelMap& pmap + ){ + + if(!agrees(pmap)){ + throw std::invalid_argument("Maps not the same"); + } + + for(size_t i=0;i @@ -228,7 +235,9 @@ class PixelMap in each group plus the end of the list so that heads[i] to heads[i+1] is a group for 0 <= i <= ngroups. The number of groups is returned which is also heads.size() - 1 */ - int count_islands(std::list &pixel_index,std::vector::iterator> &heads) const; + //int count_islands(std::list &pixel_index,std::vector::iterator> &heads) const; + int count_islands(std::vector &pixel_index) const; + /// get a list of pixels above value size_t threshold(std::list &pixel_index,PosType value){ for(size_t i=0;i Date: Sat, 24 Mar 2018 17:36:11 +0100 Subject: [PATCH 049/227] Change inclination of random disks. Made Observation mode efficient. Rewrote PixelMap::find_islands() --- AnalyticNSIE/sourceAnaGalaxy.cpp | 2 +- ImageProcessing/observation.cpp | 65 ++++++++++++++++---------------- ImageProcessing/pixelize.cpp | 5 +-- include/image_processing.h | 12 +++--- 4 files changed, 42 insertions(+), 42 deletions(-) diff --git a/AnalyticNSIE/sourceAnaGalaxy.cpp b/AnalyticNSIE/sourceAnaGalaxy.cpp index ca2cb027..27c79693 100644 --- a/AnalyticNSIE/sourceAnaGalaxy.cpp +++ b/AnalyticNSIE/sourceAnaGalaxy.cpp @@ -291,7 +291,7 @@ void SourceMultiAnaGalaxy::readDataFileMillenn(Utilities::RandomNumbers_NR &ran) theta[1] = dec*pi/180; pa = (90 - pa)*pi/180; inclination *= pi/180; - if(cos(inclination)< 0.1) inclination = acos(0.1); + if(cos(inclination)< 0.5) inclination = acos(0.5); if(j == 0){ rangex[0] = rangex[1] = theta[0]; diff --git a/ImageProcessing/observation.cpp b/ImageProcessing/observation.cpp index 2f505ff3..8b59c13f 100644 --- a/ImageProcessing/observation.cpp +++ b/ImageProcessing/observation.cpp @@ -278,23 +278,24 @@ void Observation::setPSF(std::string psf_file, float os) * \param noise Decides if noise is added * \param unit Decides units of output (if flux, output is in 10**(-0.4*mag)) */ -PixelMap Observation::Convert (PixelMap &map, bool psf, bool noise, long *seed, unitType unit) +void Observation::Convert(PixelMap &map, bool psf, bool noise, long *seed, unitType unit) { if (telescope == true && fabs(map.getResolution()-pix_size) > pix_size*1.0e-5) { std::cout << "The resolution of the input map is different from the one of the simulated instrument in Observation::Convert!" << std::endl; throw std::runtime_error("The resolution of the input map is different from the one of the simulated instrument!"); } - PixelMap outmap = PhotonToCounts(map); - if (psf == true) outmap = ApplyPSF(outmap); - if (noise == true) outmap = AddNoise(outmap,seed); + //PixelMap outmap = + PhotonToCounts(map); + if (psf == true) ApplyPSF(map); + if (noise == true) AddNoise(map,seed); if (unit == flux) { double counts_to_flux = pow(10,-0.4*mag_zeropoint); - outmap.Renormalize(counts_to_flux); + map.Renormalize(counts_to_flux); } - return outmap; + return; } /// returns factor by which code image units need to be multiplied by to get flux units @@ -304,19 +305,19 @@ double Observation::flux_convertion_factor() } /// Converts an observed image to the units of the lensing simulation -PixelMap Observation::Convert_back (PixelMap &map) +void Observation::Convert_back (PixelMap &map) { - PixelMap outmap(map); + //PixelMap outmap(map); double Q = pow(10,0.4*(mag_zeropoint+48.6))*hplanck; - outmap.Renormalize(1./Q); - return outmap; + map.Renormalize(1./Q); + return; } /** * \brief Smooths the image with a PSF map. * */ -PixelMap Observation::ApplyPSF(PixelMap &pmap) +void Observation::ApplyPSF(PixelMap &pmap) { if(pmap.getNx() != pmap.getNy()){ std::cout << "Observation::AddNoise() Doesn't work on nonsquare maps" << std::endl; @@ -327,13 +328,13 @@ PixelMap Observation::ApplyPSF(PixelMap &pmap) { if (seeing > 0.) { - PixelMap outmap(pmap); - outmap.smooth(seeing/2.355); - return outmap; + //PixelMap outmap(pmap); + pmap.smooth(seeing/2.355); + return; } else { - return pmap; + return; } } else @@ -341,7 +342,7 @@ PixelMap Observation::ApplyPSF(PixelMap &pmap) #ifdef ENABLE_FITS #ifdef ENABLE_FFTW - PixelMap outmap(pmap); + //PixelMap outmap(pmap); // calculates normalisation of psf int N_psf = map_psf.size(); @@ -355,7 +356,7 @@ PixelMap Observation::ApplyPSF(PixelMap &pmap) // creates plane for fft of map, sets properly input and output data, then performs fft fftw_plan p; - long Npix = outmap.getNx(); + long Npix = pmap.getNx(); long Npix_zeropad = Npix + side_psf; // rows and columns between first_p and last_p are copied in the zero-padded version @@ -373,7 +374,7 @@ PixelMap Observation::ApplyPSF(PixelMap &pmap) long ix = i/Npix_zeropad; long iy = i%Npix_zeropad; if (ix >= first_p && ix <= last_p && iy >= first_p && iy <= last_p) - in_zeropad[i] = outmap[(ix-side_psf/2)*Npix+(iy-side_psf/2)]; + in_zeropad[i] = pmap[(ix-side_psf/2)*Npix+(iy-side_psf/2)]; else in_zeropad[i] = 0.; } @@ -436,10 +437,10 @@ PixelMap Observation::ApplyPSF(PixelMap &pmap) if (ix >= first_p && ix <= last_p && iy >= first_p && iy <= last_p) { int ii = (ix-side_psf/2)*Npix+(iy-side_psf/2); - outmap.AssignValue(ii,out2[i]/double(Npix_zeropad*Npix_zeropad)); + pmap.AssignValue(ii,out2[i]/double(Npix_zeropad*Npix_zeropad)); } } - return outmap; + return; #else std::cout << "Please enable the preprocessor flag ENABLE_FFTW !" << std::endl; exit(1); @@ -473,27 +474,27 @@ float Observation::getBackgroundNoise(float resolution, unitType unit) } /// Applies realistic noise (read-out + Poisson) on an image -PixelMap Observation::AddNoise(PixelMap &pmap,long *seed) +void Observation::AddNoise(PixelMap &pmap,long *seed) { if(pmap.getNx() != pmap.getNy()){ std::cout << "Observation::AddNoise() Doesn't work on nonsquare maps" << std::endl; throw std::runtime_error("nonsquare"); } - PixelMap outmap(pmap); + //PixelMap outmap(pmap); double Q = pow(10,0.4*(mag_zeropoint+48.6)); - double res_in_arcsec = outmap.getResolution()*180.*60.*60/pi; + double res_in_arcsec = pmap.getResolution()*180.*60.*60/pi; double back_mean = pow(10,-0.4*(48.6+back_mag))*res_in_arcsec*res_in_arcsec*Q*exp_time; double rms, noise; double norm_map; - for (unsigned long i = 0; i < outmap.getNx()*outmap.getNy(); i++) + for (unsigned long i = 0; i < pmap.size() ; i++) { - norm_map = outmap[i]*exp_time; + norm_map = pmap[i]*exp_time; if (norm_map+back_mean > 500.) { rms = sqrt(exp_num*ron*ron+norm_map+back_mean); noise = gasdev(seed)*rms; - outmap.AssignValue(i,double(norm_map+noise)/exp_time); + pmap.AssignValue(i,double(norm_map+noise)/exp_time); } else { @@ -507,10 +508,10 @@ PixelMap Observation::AddNoise(PixelMap &pmap,long *seed) } rms = sqrt(exp_num*ron*ron); noise = gasdev(seed)*rms; - outmap.AssignValue(i,double(k-1+noise-back_mean)/exp_time); + pmap.AssignValue(i,double(k-1+noise-back_mean)/exp_time); } } - return outmap; + return; } void Observation::AddNoiseFromCorr(PixelMap &input,PixelMap &output @@ -528,11 +529,11 @@ void Observation::AddNoiseFromCorr(PixelMap &input,PixelMap &output } /// Translates photon flux (in 1/(s*cm^2*Hz*hplanck)) into telescope pixel counts -PixelMap Observation::PhotonToCounts(PixelMap &pmap) +void Observation::PhotonToCounts(PixelMap &pmap) { - PixelMap outmap(pmap); + //PixelMap outmap(pmap); double Q = pow(10,0.4*(mag_zeropoint+48.6))*hplanck; - outmap.Renormalize(Q); - return outmap; + pmap.Renormalize(Q); + return; } diff --git a/ImageProcessing/pixelize.cpp b/ImageProcessing/pixelize.cpp index 93338aa4..30291038 100644 --- a/ImageProcessing/pixelize.cpp +++ b/ImageProcessing/pixelize.cpp @@ -101,7 +101,6 @@ PixelMap::PixelMap(PixelMap&& other) other.map_boundary_p1[1] = 0; other.map_boundary_p2[0] = 0; other.map_boundary_p2[1] = 0; - } /// make square PixelMap @@ -703,11 +702,11 @@ int PixelMap::count_islands(std::vector &pixel_index) const{ long it = *test%Nx; long jt = *test/Nx; - if( abs(it - ic) == 1 || abs(jt - jc) == 1 ){ + if( abs(it - ic) <= 1 && abs(jt - jc) <= 1 ){ // swap test for group boundary size_t tmp = *group_boundary; - *test = tmp; *group_boundary = *test; + *test = tmp; ++group_boundary; --test; } diff --git a/include/image_processing.h b/include/image_processing.h index 3ced7b80..e9df8f34 100644 --- a/include/image_processing.h +++ b/include/image_processing.h @@ -397,9 +397,9 @@ class Observation float getBackgroundNoise(float resolution, unitType unit = counts_x_sec); std::valarray getPSF(){return map_psf;} void setPSF(std::string psf_file, float os = 1.); - PixelMap Convert(PixelMap &map, bool psf, bool noise,long *seed, unitType unit = counts_x_sec); + void Convert(PixelMap &map, bool psf, bool noise,long *seed, unitType unit = counts_x_sec); double flux_convertion_factor(); - PixelMap Convert_back(PixelMap &map); + void Convert_back(PixelMap &map); void setExpTime(float time){exp_time = time;} static void AddNoiseFromCorr(PixelMap &input,PixelMap &output @@ -421,12 +421,12 @@ class Observation double pix_size; // pixel size (in rad) bool telescope; // was the observation created from a default telescope? - PixelMap AddNoise(PixelMap &pmap,long *seed); + void AddNoise(PixelMap &pmap,long *seed); - PixelMap PhotonToCounts(PixelMap &pmap); - PixelMap ApplyPSF(PixelMap &pmap); + void PhotonToCounts(PixelMap &pmap); + void ApplyPSF(PixelMap &pmap); - PixelMap noise_correlation; + //PixelMap noise_correlation; }; void pixelize(double *map,long Npixels,double range,double *center From b99c9ed41a7cb8e9eaf554bbfcb3f2a719b0a946 Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Sat, 24 Mar 2018 17:38:06 +0100 Subject: [PATCH 050/227] Nothing --- ImageProcessing/pixelize.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ImageProcessing/pixelize.cpp b/ImageProcessing/pixelize.cpp index a8fa5969..6c50e160 100644 --- a/ImageProcessing/pixelize.cpp +++ b/ImageProcessing/pixelize.cpp @@ -1074,7 +1074,7 @@ void PixelMap::drawdisk( PosType x1[2],x2[2]; // To do the circle (easy) : - // ======================== + // group===================== drawcircle(r_center,radius,value); // To fill the circle : From b65ba9a313b37233dda6924c222a8fc71bcec458 Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Sun, 25 Mar 2018 17:49:12 +0200 Subject: [PATCH 051/227] Fixed bug in PixelMap::find_islands() --- ImageProcessing/pixelize.cpp | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/ImageProcessing/pixelize.cpp b/ImageProcessing/pixelize.cpp index 34f3c8a7..920321d8 100644 --- a/ImageProcessing/pixelize.cpp +++ b/ImageProcessing/pixelize.cpp @@ -685,24 +685,25 @@ int PixelMap::count_islands(std::vector &pixel_index) const{ if(pixel_index.size() == 0) return 0; if(pixel_index.size() == 1) return 1; - size_t *end = pixel_index.data() + pixel_index.size() + 1; + size_t *end = pixel_index.data() + pixel_index.size(); size_t *current = pixel_index.data(); - int Ngroups = 0; + int Ngroups = 1; size_t *group_boundary = current + 1; - while(current != end){ + while(group_boundary != end){ long ic = *current%Nx; long jc = *current/Nx; int Neighbors = 0; for(size_t *test = group_boundary - ; test != end && Neighbors < 8 - ; ++test,++Neighbors + ; test != end && Neighbors < 8 && group_boundary != end + ; ++test ){ long it = *test%Nx; long jt = *test/Nx; if( abs(it - ic) <= 1 && abs(jt - jc) <= 1 ){ + ++Neighbors; // swap test for group boundary size_t tmp = *group_boundary; *group_boundary = *test; @@ -712,7 +713,7 @@ int PixelMap::count_islands(std::vector &pixel_index) const{ } } ++current; - if(current == group_boundary){ + if(current == group_boundary && group_boundary != end ){ ++group_boundary; ++Ngroups; } From b1df3c528d685d0fdbb43a5bf921f70f5e774099 Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Tue, 17 Apr 2018 19:11:23 +0200 Subject: [PATCH 052/227] Adding Gaussian random filed generator. --- FullRange/CMakeLists.txt | 1 + FullRange/guassian.cpp | 8 ++++++++ include/CMakeLists.txt | 1 + include/gaussian.h | 13 +++++++++++++ 4 files changed, 23 insertions(+) create mode 100644 FullRange/guassian.cpp create mode 100644 include/gaussian.h diff --git a/FullRange/CMakeLists.txt b/FullRange/CMakeLists.txt index e4be1e5b..913a6a56 100644 --- a/FullRange/CMakeLists.txt +++ b/FullRange/CMakeLists.txt @@ -1,4 +1,5 @@ add_sources( implant_stars.cpp internal_rayshooter_multi.cpp + gaussian.cpp ) diff --git a/FullRange/guassian.cpp b/FullRange/guassian.cpp new file mode 100644 index 00000000..147200a9 --- /dev/null +++ b/FullRange/guassian.cpp @@ -0,0 +1,8 @@ +// +// guassian.cpp +// NR +// +// Created by Ben Metcalf on 17/04/2018. +// + +#include "guassian.hpp" diff --git a/include/CMakeLists.txt b/include/CMakeLists.txt index 847d6cbf..36ac5378 100644 --- a/include/CMakeLists.txt +++ b/include/CMakeLists.txt @@ -48,4 +48,5 @@ add_sources( particle_halo.h concave_hull.h bands_etc.h + gaussian.h ) diff --git a/include/gaussian.h b/include/gaussian.h new file mode 100644 index 00000000..f26570a6 --- /dev/null +++ b/include/gaussian.h @@ -0,0 +1,13 @@ +// +// guassian.hpp +// NR +// +// Created by Ben Metcalf on 17/04/2018. +// + +#ifndef guassian_hpp +#define guassian_hpp + +#include + +#endif /* guassian_hpp */ From 76af83d5f01c201242213484144d79bff1cec661 Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Tue, 17 Apr 2018 19:30:00 +0200 Subject: [PATCH 053/227] Added gaussian.cpp --- FullRange/gaussian.cpp | 52 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 FullRange/gaussian.cpp diff --git a/FullRange/gaussian.cpp b/FullRange/gaussian.cpp new file mode 100644 index 00000000..28c285ad --- /dev/null +++ b/FullRange/gaussian.cpp @@ -0,0 +1,52 @@ +// +// guassian.cpp +// NR +// +// Created by Ben Metcalf on 17/04/2018. +// + +#include "gaussian.h" +#include "slsimlib.h" +#include +#include + + +#ifdef ENABLE_FFTW +#include "fftw3.h" +#endif + +/** \brief Index of the discrete Fourier mode using FFTW convention + */ +double k_to_index_2d(double k){ + +} +/** \brief Discete Fourier k from 1d index using FFTW convention + + k is in units + */ +double index_to_k_2d(size_t i){ + +} + +template +void gaussianField(std::vector &im,std::vector &power + ,size_t Nx,size_t Ny,Utilities::RandomNumbers_NR &ran){ + + size_t N = Nx*Ny; + if(N != im.size()){ + std::cerr << "incompatable input in gaussianField" << std::endl; + throw std::invalid_argument(""); + } + std::vector > fftmap(Nx*(Ny/2+1)); + + for(size_t i = 0 ; i < N ; ++i){ + fftmap[i].real(sqrt(power[i])*ran.gauss()); + fftmap[i].imag(sqrt(power[i])*ran.gauss()); + } + + fftw_plan p = fftw_plan_dft_c2r_2d(Nx,Ny,im.data() + , reinterpret_cast(fftmap.data()), FFTW_ESTIMATE); + fftw_execute(p); + + fftw_destroy_plan(p); +} From dda9edc2b4535feca78ee55e92e857900b69b1b2 Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Tue, 17 Apr 2018 19:31:33 +0200 Subject: [PATCH 054/227] renamed file --- FullRange/{guassian.cpp => gaussian.cpp} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename FullRange/{guassian.cpp => gaussian.cpp} (100%) diff --git a/FullRange/guassian.cpp b/FullRange/gaussian.cpp similarity index 100% rename from FullRange/guassian.cpp rename to FullRange/gaussian.cpp From 2cba6d1d591ec12d028c56dd186c1a8bb0915005 Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Wed, 18 Apr 2018 17:27:20 +0200 Subject: [PATCH 055/227] Added class GaussianField to make 2d Gaussian random fields. --- FullRange/gaussian.cpp | 68 +++++++++++++++++++++++++++++++++++++++++- include/gaussian.h | 58 +++++++++++++++++++++++++++++++++++ 2 files changed, 125 insertions(+), 1 deletion(-) diff --git a/FullRange/gaussian.cpp b/FullRange/gaussian.cpp index 147200a9..b48ca6a7 100644 --- a/FullRange/gaussian.cpp +++ b/FullRange/gaussian.cpp @@ -5,4 +5,70 @@ // Created by Ben Metcalf on 17/04/2018. // -#include "guassian.hpp" +#include "gaussian.h" +#include "slsimlib.h" +#include +#include +#include + + + +#ifdef ENABLE_FFTW +#include "fftw3.h" +#endif + +void GaussianField::GaussianField2D(double *image + ,std::function PofK + ,double boxlrad + ,Utilities::RandomNumbers_NR &ran + ){ + + size_t i,j,ii; + double kx,ky; + double p; + + double K = 2. * M_PI / boxlrad; + + for (i=0; i < Nx; i++){ + for (j=0; j < Ny/2+1; j++){ + + ii = i*(Ny/2+1) + j; + + kx= (i(kappak.data()),image); + return; +} + +void GaussianField::GaussianField2D(std::vector &image + ,std::function PofK + ,double boxlrad + ,Utilities::RandomNumbers_NR &ran + ){ + image.resize(Nx*Ny); + GaussianField2D(image.data(),PofK,boxlrad,ran); +} + +void GaussianField::GaussianField2D(std::valarray &image + ,std::function PofK + ,double boxlrad + ,Utilities::RandomNumbers_NR &ran + ){ + image.resize(Nx*Ny); + GaussianField2D(&(image[0]),PofK,boxlrad,ran); +} diff --git a/include/gaussian.h b/include/gaussian.h index f26570a6..f07fbb98 100644 --- a/include/gaussian.h +++ b/include/gaussian.h @@ -9,5 +9,63 @@ #define guassian_hpp #include +#include +#include +#include +#include "slsimlib.h" +#include + + +#ifdef ENABLE_FFTW +#include "fftw3.h" +#endif + +class GaussianField{ +public: + GaussianField(size_t Nx,size_t Ny): + Nx(Nx),Ny(Ny) + { + kappak.resize(Nx*(Ny/2+1)); + std::vector image(Nx*Ny); + + pbackward = fftw_plan_dft_c2r_2d(Nx,Ny + ,reinterpret_cast(kappak.data()) + ,image.data(), FFTW_ESTIMATE); + } + + ~GaussianField(){ + fftw_destroy_plan(pbackward); + } + void GaussianField2D(double *image,std::function PofK, + double boxlrad,Utilities::RandomNumbers_NR &ran); + + void GaussianField2D(std::vector &image,std::function PofK, + double boxlrad,Utilities::RandomNumbers_NR &ran); + + void GaussianField2D(std::valarray &image,std::function PofK, + double boxlrad,Utilities::RandomNumbers_NR &ran); + + +private: + std::vector > kappak; + fftw_plan pbackward; + size_t Nx; + size_t Ny; +}; + +/** \brief Discete Fourier k from 1d index using FFTW convention + asd + k is in units (box length)^-1 + */ +void index_to_k_2d(size_t k /// id index + ,double &kx /// output kx in 1/Length_x units + ,double &ky /// output ky in 1/Length_y units + ,size_t Nx /// number of pixels on x-side + ,size_t Ny /// number of pixels on y-side + ); + +void GaussianField2D(double *image,std::function PofK, + double boxlrad,int Nx,int Ny,Utilities::RandomNumbers_NR &ran); + #endif /* guassian_hpp */ From 3e3631b6bd921174e64d7fa11b28ed54043d89a2 Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Wed, 18 Apr 2018 17:29:08 +0200 Subject: [PATCH 056/227] Working on noise correlations in Observation --- ImageProcessing/observation.cpp | 25 +++++++++++++++++++------ include/image_processing.h | 4 +++- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/ImageProcessing/observation.cpp b/ImageProcessing/observation.cpp index 8b59c13f..3803d596 100644 --- a/ImageProcessing/observation.cpp +++ b/ImageProcessing/observation.cpp @@ -25,7 +25,6 @@ */ Observation::Observation(Telescope tel_name) { - switch (tel_name) { case Euclid_VIS: diameter = 119.; @@ -240,7 +239,9 @@ Observation::Observation(float diameter, float transmission, float exp_time, int } /// Reads in and sets the PSF from a fits file. If the pixel size of the fits is different (smaller) than the one of the telescope, it must be specified. -void Observation::setPSF(std::string psf_file, float os) +void Observation::setPSF(std::string psf_file /// name of fits file with psf + , float os /// over sampling factor + ) { #ifdef ENABLE_FITS // std::auto_ptr fp (new CCfits::FITS (psf_file.c_str(), CCfits::Read)); @@ -267,8 +268,20 @@ void Observation::setPSF(std::string psf_file, float os) exit(1); #endif - oversample = os; + oversample = os; +} +/// Read in and set the noise correlation function. +void Observation::setNoiseCorrelation(std::string &nc_file /// name of fits file with noise correlation function in pixel units +) +{ + PixelMap noise_corr(nc_file,pix_size); + size_t N = nc_map.size(); + // take the quare root of the correlation function + for(size_t i = 0 ; i < N ; ++i) + noise_corr[i] = sqrt(noise_corr[i]); + ***** + PixelMap::swap(noise_corr,nc_map); } /** \brief Converts the input map to a realistic image @@ -486,13 +499,14 @@ void Observation::AddNoise(PixelMap &pmap,long *seed) double res_in_arcsec = pmap.getResolution()*180.*60.*60/pi; double back_mean = pow(10,-0.4*(48.6+back_mag))*res_in_arcsec*res_in_arcsec*Q*exp_time; double rms, noise; + double rms2 = sqrt(exp_num*ron*ron); double norm_map; for (unsigned long i = 0; i < pmap.size() ; i++) { norm_map = pmap[i]*exp_time; if (norm_map+back_mean > 500.) { - rms = sqrt(exp_num*ron*ron+norm_map+back_mean); + rms = sqrt(exp_num*ron*ron + norm_map + back_mean); noise = gasdev(seed)*rms; pmap.AssignValue(i,double(norm_map+noise)/exp_time); } @@ -506,8 +520,7 @@ void Observation::AddNoise(PixelMap &pmap,long *seed) k++; p *= ran2(seed); } - rms = sqrt(exp_num*ron*ron); - noise = gasdev(seed)*rms; + noise = gasdev(seed)*rms2; pmap.AssignValue(i,double(k-1+noise-back_mean)/exp_time); } } diff --git a/include/image_processing.h b/include/image_processing.h index e9df8f34..3877502e 100644 --- a/include/image_processing.h +++ b/include/image_processing.h @@ -155,7 +155,7 @@ class PixelMap bool agrees(const PixelMap& other) const; friend void swap(PixelMap&, PixelMap&); - void swap(PixelMap&, PixelMap&); + static void swap(PixelMap&, PixelMap&); /// return average pixel value PosType ave() const; @@ -397,6 +397,7 @@ class Observation float getBackgroundNoise(float resolution, unitType unit = counts_x_sec); std::valarray getPSF(){return map_psf;} void setPSF(std::string psf_file, float os = 1.); + void setNoiseCorrelation(std::string &nc_file); void Convert(PixelMap &map, bool psf, bool noise,long *seed, unitType unit = counts_x_sec); double flux_convertion_factor(); void Convert_back(PixelMap &map); @@ -417,6 +418,7 @@ class Observation float ron; // read-out noise in electrons/pixel float seeing; // full-width at half maximum of the gaussian smoothing std::valarray map_psf; // array of the point spread function + PixelMap nc_map; // noise correlation function float oversample; // psf oversampling factor double pix_size; // pixel size (in rad) bool telescope; // was the observation created from a default telescope? From 0cb3dbad31bd8c460397502af7683908a67b276b Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Mon, 23 Apr 2018 10:24:47 +0200 Subject: [PATCH 057/227] Changed the reading of halos and galaxies to count the number of lines first so that it allocates memory once. --- AnalyticNSIE/sourceAnaGalaxy.cpp | 34 ++++++++++++++++++++++++++------ InputParameters/causticdata.cpp | 9 +++++++++ MultiPlane/lens.cpp | 10 +++++++++- Source/sersic.cpp | 1 + include/sersic_source.h | 1 + 5 files changed, 48 insertions(+), 7 deletions(-) diff --git a/AnalyticNSIE/sourceAnaGalaxy.cpp b/AnalyticNSIE/sourceAnaGalaxy.cpp index 27c79693..86e122f9 100644 --- a/AnalyticNSIE/sourceAnaGalaxy.cpp +++ b/AnalyticNSIE/sourceAnaGalaxy.cpp @@ -71,8 +71,9 @@ void SourceMultiAnaGalaxy::readDataFileMillenn(Utilities::RandomNumbers_NR &ran) unsigned long i,j; unsigned long GalID,HaloID; - PosType ra,dec,z_cosm,z_app,Dlum,inclination,pa,Rh,Ref,SDSS_u,SDSS_g,SDSS_r,SDSS_i,SDSS_z - ,J_band,H_band,Ks_band,i1,i2,SDSS_u_Bulge,SDSS_g_Bulge,SDSS_r_Bulge,SDSS_i_Bulge,SDSS_z_Bulge + PosType ra,dec,z_cosm,z_app,Dlum,inclination,pa,Rh,Ref,SDSS_u,SDSS_g + ,SDSS_r,SDSS_i,SDSS_z,J_band,H_band,Ks_band,i1,i2 + ,SDSS_u_Bulge,SDSS_g_Bulge,SDSS_r_Bulge,SDSS_i_Bulge,SDSS_z_Bulge ,J_band_Bulge,H_band_Bulge,Ks_band_Bulge,i1_Bulge,i2_Bulge; std::ofstream color_cat; @@ -108,6 +109,15 @@ void SourceMultiAnaGalaxy::readDataFileMillenn(Utilities::RandomNumbers_NR &ran) throw std::runtime_error(" Cannot open file."); exit(1); } + + std::string myline; + size_t count = 0; + while(getline(file_in,myline)){ + if(myline[0] != '#') ++count; + } + file_in.clear(); + file_in.seekg(0); + galaxies.reserve(count); std::cout << "Reading from galaxy data file " << input_gal_file.c_str() << std::endl; //file_in >> Ngalaxies; @@ -161,7 +171,6 @@ void SourceMultiAnaGalaxy::readDataFileMillenn(Utilities::RandomNumbers_NR &ran) unsigned long myint; PosType myPosType; - std::string myline; std::string strg; std::string f=","; std::stringstream buffer; @@ -176,6 +185,7 @@ void SourceMultiAnaGalaxy::readDataFileMillenn(Utilities::RandomNumbers_NR &ran) if(myline[0] == '#') break; + //std::cout << "read columns" << std::endl; for(int l=0;l> c >> i2_Bulge >> c; */ - addr[11] = &SDSS_u; + /*addr[11] = &SDSS_u; addr[12] = &SDSS_g; addr[13] = &SDSS_r; addr[14] = &SDSS_i; @@ -226,6 +236,7 @@ void SourceMultiAnaGalaxy::readDataFileMillenn(Utilities::RandomNumbers_NR &ran) addr[18] = &Ks_band; addr[19] = &i1; addr[20] = &i2; + */ /* switch (band){ @@ -276,6 +287,8 @@ void SourceMultiAnaGalaxy::readDataFileMillenn(Utilities::RandomNumbers_NR &ran) << std::endl; if(mag < mag_limit){ + //std::cout << "good mag " << std::endl; + /* std::cout << galid << c << haloid << c << cx << c << cy << c << cz << c << ra << c << dec << c << z_geo << c << z_app << c << dlum << c << vlos << c << incl @@ -293,19 +306,26 @@ void SourceMultiAnaGalaxy::readDataFileMillenn(Utilities::RandomNumbers_NR &ran) inclination *= pi/180; if(cos(inclination)< 0.5) inclination = acos(0.5); + //std::cout << "did inclination" << std::endl; + + if(j == 0){ rangex[0] = rangex[1] = theta[0]; rangey[0] = rangey[1] = theta[1]; }else{ + //std::cout << "rang" << std::endl; + if(theta[0] < rangex[0]) rangex[0] = theta[0]; if(theta[0] > rangex[1]) rangex[1] = theta[0]; if(theta[1] < rangey[0]) rangey[0] = theta[1]; if(theta[1] > rangey[1]) rangey[1] = theta[1]; } + //std::cout << "adding to galaxies" << std::endl; /***************************/ - galaxies.emplace_back(mag,mag_bulge,Ref,Rh - ,pa,inclination,HaloID,z_cosm,theta,ran); + galaxies.push_back(SourceOverzierPlus(mag,mag_bulge,Ref,Rh + ,pa,inclination,HaloID,z_cosm,theta,ran)); + //std::cout << "filling last galaxy" << std::endl; galaxies.back().setMag(SDSS_U,SDSS_u); galaxies.back().setMagBulge(SDSS_U,SDSS_u_Bulge); @@ -344,10 +364,12 @@ void SourceMultiAnaGalaxy::readDataFileMillenn(Utilities::RandomNumbers_NR &ran) // << " theta = " << theta[0] << " " << theta[1] << std::endl; assert(galaxies.back().getMag(band) == galaxies.back().getMag() ); + //std::cout << " j in SourceMultiAnaGalaxy : " << j << std::endl; ++j; } } + std::cout << " closing file in SourceMultiAnaGalaxy : " << std::endl; color_cat.close(); file_in.close(); diff --git a/InputParameters/causticdata.cpp b/InputParameters/causticdata.cpp index dfd0c729..094691c0 100644 --- a/InputParameters/causticdata.cpp +++ b/InputParameters/causticdata.cpp @@ -165,6 +165,14 @@ void CausticDataStore::readfile(std::string filename,bool verbose){ throw std::runtime_error(" Cannot open file."); } + size_t count = 0; + while(getline(file_in,myline)){ + if(myline[0] != '#') ++count; + } + file_in.clear(); + file_in.seekg(0); + data.reserve(count); + if(verbose) std::cout << "Reading caustic information from " << filename << std::endl; size_t i=0; while(file_in.peek() == '#'){ @@ -173,6 +181,7 @@ void CausticDataStore::readfile(std::string filename,bool verbose){ } if(verbose) std::cout << " skipped "<< i << " comment lines in " << filename << std::endl; + size_t pos; CausticStructure tmp_data; // read in data diff --git a/MultiPlane/lens.cpp b/MultiPlane/lens.cpp index c000d0c7..09b419b9 100644 --- a/MultiPlane/lens.cpp +++ b/MultiPlane/lens.cpp @@ -1897,6 +1897,15 @@ void Lens::readInputSimFileMillennium(bool verbose,DM_Light_Division division_mo throw std::runtime_error(" Cannot open file."); } + std::string myline; + size_t count = 0; + while(getline(file_in,myline)){ + if(myline[0] != '#') ++count; + } + file_in.clear(); + file_in.seekg(0); + field_halos.reserve(count); + // skip through header information in data file i=0; while(file_in.peek() == '#'){ @@ -1930,7 +1939,6 @@ void Lens::readInputSimFileMillennium(bool verbose,DM_Light_Division division_mo unsigned long myint; PosType myPosType; - std::string myline; std::string strg; std::string f=","; std::stringstream buffer; diff --git a/Source/sersic.cpp b/Source/sersic.cpp index 2fc5f9a9..e1e6c2b3 100644 --- a/Source/sersic.cpp +++ b/Source/sersic.cpp @@ -17,6 +17,7 @@ SourceSersic::SourceSersic( ) : Source() { + //std::cout << "SourceSersic constructor" << std::endl; setReff(my_Reff); setMag(my_mag); setPA(my_PA); diff --git a/include/sersic_source.h b/include/sersic_source.h index 4e88b3c7..e3b00b93 100644 --- a/include/sersic_source.h +++ b/include/sersic_source.h @@ -22,6 +22,7 @@ class SourceSersic : public Source SourceSersic(PosType mag,PosType Reff,PosType PA,PosType my_index,PosType my_q,PosType my_z,const PosType *theta=0); ~SourceSersic(); + /// calculates radius where the surface brightness drops by a factor f with respect to the central peak inline PosType FractionRadius (PosType f) {return Reff*pow(-log (f)/bn,index);} From 638dd910f5ac03970f7723d6e3f4c96cdad9c425 Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Tue, 24 Apr 2018 10:33:13 +0200 Subject: [PATCH 058/227] LensHaloOverzierPlus - made the member spheroid a real object and not a pointer. Fixed problem with SourceSeresic when Ref was zero. Fixed bug in Oberservation when the input noise correlation was negative. --- AnalyticNSIE/base_analens.cpp | 2 + AnalyticNSIE/sourceAnaGalaxy.cpp | 83 +++----------------------------- ImageProcessing/observation.cpp | 7 +-- ImageProcessing/pixelize.cpp | 47 ++++++++++++++++++ MultiPlane/MOKAlens.cpp | 9 ++-- MultiPlane/lens_halos.cpp | 8 +++ MultiPlane/planes.cpp | 6 ++- Source/overzier.cpp | 57 ++++++++++++++++------ Source/sersic.cpp | 54 +++++++++++++++------ include/image_processing.h | 38 +-------------- include/overzier_source.h | 14 +++--- include/sersic_source.h | 11 +++-- 12 files changed, 176 insertions(+), 160 deletions(-) diff --git a/AnalyticNSIE/base_analens.cpp b/AnalyticNSIE/base_analens.cpp index 2fbbeac0..30c7dc70 100644 --- a/AnalyticNSIE/base_analens.cpp +++ b/AnalyticNSIE/base_analens.cpp @@ -148,6 +148,8 @@ void LensHaloBaseNSIE::force_halo( force_stars(alpha,kappa,gamma,xcm); } + //assert(alpha[0] == alpha[0] && alpha[1] == alpha[1]); + return ; } diff --git a/AnalyticNSIE/sourceAnaGalaxy.cpp b/AnalyticNSIE/sourceAnaGalaxy.cpp index 86e122f9..2133c374 100644 --- a/AnalyticNSIE/sourceAnaGalaxy.cpp +++ b/AnalyticNSIE/sourceAnaGalaxy.cpp @@ -130,6 +130,8 @@ void SourceMultiAnaGalaxy::readDataFileMillenn(Utilities::RandomNumbers_NR &ran) ++i; } std::cout << " skipped "<< i << " comment lines in " << input_gal_file << std::endl; + getline(file_in,myline); // skip title line + PosType theta[2] = {0.0,0.0}; @@ -147,6 +149,7 @@ void SourceMultiAnaGalaxy::readDataFileMillenn(Utilities::RandomNumbers_NR &ran) addr[8] = &pa; addr[9] = &Rh; addr[10] = &Ref; + addr[11] = &SDSS_u; addr[12] = &SDSS_g; addr[13] = &SDSS_r; @@ -185,16 +188,20 @@ void SourceMultiAnaGalaxy::readDataFileMillenn(Utilities::RandomNumbers_NR &ran) if(myline[0] == '#') break; + //std::cout << "myline " << myline << std::endl ; + //std::cout << "read columns" << std::endl; for(int l=0;l> myint; *((unsigned long *)addr[l]) = myint; } else{ + //std::cout << "myPosType " << myPosType ; buffer >> myPosType; *((PosType *)addr[l]) = myPosType; } @@ -204,81 +211,7 @@ void SourceMultiAnaGalaxy::readDataFileMillenn(Utilities::RandomNumbers_NR &ran) buffer.str(std::string()); } - // read a line of data - /*file_in >> galid >> c >> haloid >> c >> cx >> c >> cy >> c >> cz >> c >> ra >> c >> dec >> c >> z_geo >> c >> z_app - >> c >> dlum >> c >> vlos >> c >> incl - >> c >> acs435 >> c >> acs606 >> c >> acs775 >> c >> acs850 - >> c >> acs435_bulge >> c >> acs606_bulge >> c >> acs775_bulge >> c >> acs850_bulge - >> c >> type >> c >> mvir >> c >> rvir >> c >> vmax >> c >> stellarmass >> c >> bulgemass - >> c >> stellardiskradius >> c >> bulgesize - >> c >> inclination >> c >> pa >> c >> angdist >> c >> diskradius_arcsec >> c >> bulgesize_arcsec >> c; - */ - /*file_in >> GalID >> HaloID >> ra >> dec >> z_cosm >> z_app >> Dlum >> inclination - >> pa >> Rh >> Ref >> SDSS_u >> SDSS_g >> SDSS_r >> SDSS_i >> SDSS_z - >> J_band >> H_band >> Ks_band >> i1 >> i2 >> SDSS_u_Bulge >> SDSS_g_Bulge >> SDSS_r_Bulge - >> SDSS_i_Bulge >> SDSS_z_Bulge >> J_band_Bulge >> H_band_Bulge >> Ks_band_Bulge >> i1_Bulge - >> i2_Bulge ; - - file_in >> GalID >> c >> HaloID >> c >> ra >> c >> dec >> c >> z_cosm >> c >> z_app >> c >> Dlum >> c >> inclination - >> c >> pa >> c >> Rh >> c >> Ref >> c >> SDSS_u >> c >> SDSS_g >> c >> SDSS_r >> c >> SDSS_i >> c >> SDSS_z - >> c >> J_band >> c >> H_band >> c >> Ks_band >> c >> i1 >> c >> i2 >> c >> SDSS_u_Bulge >> c >> SDSS_g_Bulge >> c >> SDSS_r_Bulge - >> c >> SDSS_i_Bulge >> c >> SDSS_z_Bulge >> c >> J_band_Bulge >> c >> H_band_Bulge >> c >> Ks_band_Bulge >> c >> i1_Bulge - >> c >> i2_Bulge >> c; -*/ - - /*addr[11] = &SDSS_u; - addr[12] = &SDSS_g; - addr[13] = &SDSS_r; - addr[14] = &SDSS_i; - addr[15] = &SDSS_z; - addr[16] = &J_band; - addr[17] = &H_band; - addr[18] = &Ks_band; - addr[19] = &i1; - addr[20] = &i2; - */ - - /* - switch (band){ - case SDSS_U: - mag = SDSS_u; - mag_bulge = SDSS_u_Bulge; - break; - case SDSS_G: - mag = SDSS_g; - mag_bulge = SDSS_g_Bulge; - break; - case SDSS_R: - mag = SDSS_r; - mag_bulge = SDSS_r_Bulge; - break; - case SDSS_I: - mag = SDSS_i; - mag_bulge = SDSS_i_Bulge; - break; - case SDSS_Z: - mag = SDSS_z; - mag_bulge = SDSS_z_Bulge; - break; - case J: - mag = J_band; - mag_bulge = J_band_Bulge; - break; - case H: - mag = H_band; - mag_bulge = H_band_Bulge; - break; - case Ks: - mag = Ks_band; - mag_bulge = Ks_band_Bulge; - break; - default: - std::cout << "Requested band is not an available option." << std::endl; - ERROR_MESSAGE(); - throw std::invalid_argument("band not supported"); - - }*/ - + color_cat << GalID << " " << z_app << " " << SDSS_u << " " < L) { k++; p *= ran2(seed); } noise = gasdev(seed)*rms2; - noise_map[i] = (k-1+noise-back_mean - norm_map)/exp_time; + noise_map[i] = (k - 1 + noise - back_mean - norm_map)/exp_time; } sum += noise_map[i]; sum2 += noise_map[i]*noise_map[i]; diff --git a/ImageProcessing/pixelize.cpp b/ImageProcessing/pixelize.cpp index 50a4ce6a..fcc8d86f 100644 --- a/ImageProcessing/pixelize.cpp +++ b/ImageProcessing/pixelize.cpp @@ -2261,6 +2261,53 @@ void PixelMap::AddSource(Source &source,int oversample){ } */ +/** \brief convolve the image with a kernel. + + It is assumed that the size of the kernel is much smaller than the image and + that the kernal has the same pixel size as the image. + **/ +void PixelMap::convolve(PixelMap &kernel,long center_x,long center_y){ + std::valarray output(Nx*Ny); + + //std::cout << output.size() << " " << map.size() << std::endl; + + if( center_x == 0 ){ + center_x = kernel.getNx()/2; + center_y = kernel.getNy()/2; + } + + size_t Nxk = kernel.getNx(); + size_t Nyk = kernel.getNy(); + + //std::cout << "sum of map : " << sum() << std::endl; + //std::cout << "sum of kernel : " << kernel.sum() << std::endl; + //double total=0; + for(size_t k1 = 0 ; k1 < Nx ; ++k1){ + long bl1 = k1 - center_x; + + for(size_t k2 = 0 ; k2 < Ny ; ++k2){ + long bl2 = k2 - center_y; + + long k = k1 + Nx * k2; + output[k] = 0; + + for(size_t j = 0 ; j < Nyk ; ++j){ + long kk2 = bl2 + j; + if(kk2 < 0 || kk2 >= Ny) continue; + + for(size_t i = 0; i < Nxk ; ++i){ + long kk1 = bl1 + i; + if(kk1 < 0 || kk1 >= Nx) continue; + + output[k] += map[ kk1 + Nx * kk2 ] * kernel[i + Nxk * j]; + } + } + //total += output[k]; + } + } + + std::swap(map,output); +} diff --git a/MultiPlane/MOKAlens.cpp b/MultiPlane/MOKAlens.cpp index cd734e9f..78d7268a 100644 --- a/MultiPlane/MOKAlens.cpp +++ b/MultiPlane/MOKAlens.cpp @@ -336,6 +336,7 @@ void LensHaloMassMap::setMap( pixelarea *= pixelarea; for(size_t i=0;iconvergence[i] = massconvertion*inputmap(i)/pixelarea; } @@ -659,17 +660,17 @@ void LensHaloMassMap::force_halo(double *alpha assert(map->nx == map->ny); - size_t N = map->nx * map->ny; + //size_t N = map->nx * map->ny; map->convergence.size(); - assert(map->alpha1.size() == N); + /*assert(map->alpha1.size() == N); assert(map->alpha2.size() == N); assert(map->gamma1.size() == N); assert(map->gamma2.size() == N); assert(map->convergence.size() == N); assert(map->phi.size() == N); - +*/ alpha[0] = interp.interpolate(map->alpha1); alpha[1] = interp.interpolate(map->alpha2); gamma[0] = interp.interpolate(map->gamma1); @@ -678,6 +679,8 @@ void LensHaloMassMap::force_halo(double *alpha *kappa = interp.interpolate(map->convergence); *phi = interp.interpolate(map->phi); + //assert(alpha[0] == alpha[0] && alpha[1] == alpha[1]); + return; } diff --git a/MultiPlane/lens_halos.cpp b/MultiPlane/lens_halos.cpp index 228d918b..d4d99456 100644 --- a/MultiPlane/lens_halos.cpp +++ b/MultiPlane/lens_halos.cpp @@ -957,6 +957,8 @@ void LensHalo::force_halo_sym( force_stars(alpha,kappa,gamma,xcm); } + //(alpha[0] == alpha[0] && alpha[1] == alpha[1]); + return; } // TODO: put in some comments about the units used @@ -1096,6 +1098,8 @@ void LensHalo::force_halo_asym( force_stars(alpha,kappa,gamma,xcm); } + //assert(alpha[0] == alpha[0] && alpha[1] == alpha[1]); + return; } @@ -1344,6 +1348,8 @@ void LensHaloRealNSIE::force_halo( force_stars(alpha,kappa,gamma,xcm); } + //assert(alpha[0] == alpha[0] && alpha[1] == alpha[1]); + return; } /**/ @@ -1720,6 +1726,8 @@ void LensHaloDummy::force_halo(PosType *alpha force_stars(alpha,kappa,gamma,xcm); } + //assert(alpha[0] == alpha[0] && alpha[1] == alpha[1]); + } void LensHaloDummy::assignParams(InputParams& params) diff --git a/MultiPlane/planes.cpp b/MultiPlane/planes.cpp index 23b8169e..6339d7c5 100644 --- a/MultiPlane/planes.cpp +++ b/MultiPlane/planes.cpp @@ -83,10 +83,10 @@ void LensPlaneSingular::force(PosType *alpha KappaType phi_tmp; alpha[0] = alpha[1] = 0.0; - x_tmp[0] = x_tmp[1] = 0.0; + x_tmp[0] = x_tmp[1] = 0.0; *kappa = 0.0; gamma[0] = gamma[1] = gamma[2] = 0.0; - *phi = 0.0; + *phi = 0.0; // Loop over the different halos present in a given lens plane. for(std::size_t i = 0, n = halos.size(); i < n; ++i) @@ -113,6 +113,8 @@ void LensPlaneSingular::force(PosType *alpha gamma[1] += gamma_tmp[1]; gamma[2] += gamma_tmp[2]; *phi += phi_tmp; + + //(alpha[0] == alpha[0] && alpha[1] == alpha[1]); } } diff --git a/Source/overzier.cpp b/Source/overzier.cpp index 67a53503..fdfb42b4 100644 --- a/Source/overzier.cpp +++ b/Source/overzier.cpp @@ -27,7 +27,9 @@ SourceOverzier::SourceOverzier( ,double my_z /// optional redshift ,const double *my_theta /// optional angular position on the sky ){ - setInternals(my_mag,my_mag_bulge,my_Reff,my_Rh,my_PA,my_inclination,my_id,my_z,my_theta); + + //std::cout << "SourceOverzier constructor" << std::endl; + setInternals(my_mag,my_mag_bulge,my_Reff,my_Rh,my_PA,my_inclination,my_id,my_z,my_theta); } SourceOverzier::~SourceOverzier() @@ -191,7 +193,8 @@ void SourceOverzier::renormalize(){ SourceOverzierPlus::SourceOverzierPlus(PosType my_mag,PosType my_mag_bulge,PosType my_Reff,PosType my_Rh,PosType my_PA,PosType inclination,unsigned long my_id,PosType my_z,const PosType *theta,Utilities::RandomNumbers_NR &ran): SourceOverzier(my_mag,my_mag_bulge,my_Reff,my_Rh,my_PA,inclination,my_id,my_z,theta) { - + //std::cout << "SourceOverzierPlus constructor" << std::endl; + float minA = 0.01,maxA = 0.2; // minimum and maximum amplitude of arms Narms = (ran() > 0.2) ? 2 : 4; // number of arms arm_alpha = (21 + 10*(ran()-0.5)*2)*degreesTOradians; // arm pitch angle @@ -204,10 +207,12 @@ SourceOverzier(my_mag,my_mag_bulge,my_Reff,my_Rh,my_PA,inclination,my_id,my_z,th //double index = 4*pow(MAX(getBtoT(),0.03),0.4)*pow(10,0.2*(ran()-0.5)); double index = ran() + 3.5 ; - double q = 1 + (0.5-1)*ran(); + spheroid.setSersicIndex(index); + + //spheroid = new SourceSersic(my_mag_bulge,my_Reff,-my_PA + 10*(ran() - 0.5)*pi/180,index,q,my_z,theta); - spheroid = new SourceSersic(my_mag_bulge,my_Reff,-my_PA + 10*(ran() - 0.5)*pi/180,index,q,my_z,theta); + spheroid.ReSet(my_mag_bulge,my_Reff,-my_PA + 10*(ran() - 0.5)*pi/180,index,q,my_z,theta); cospa = cos(PA); sinpa = sin(PA); @@ -221,7 +226,8 @@ SourceOverzier(my_mag,my_mag_bulge,my_Reff,my_Rh,my_PA,inclination,my_id,my_z,th } SourceOverzierPlus::~SourceOverzierPlus(){ - delete spheroid; + //std::cout << "SourceOverzierPlus destructor" << std::endl; + //spheroid; } SourceOverzierPlus::SourceOverzierPlus(const SourceOverzierPlus &p): @@ -229,9 +235,18 @@ SourceOverzier(p), Narms(p.Narms),Ad(p.Ad),mctalpha(p.mctalpha),arm_alpha(p.arm_alpha) ,disk_phase(p.disk_phase),cospa(p.cospa),sinpa(p.sinpa),cosi(p.cosi) { - spheroid = new SourceSersic(p.spheroid->getMag(),p.getReff(),p.spheroid->getPA() - ,p.spheroid->getSersicIndex(),p.spheroid->getAxesRatio() - ,p.spheroid->getZ(),p.spheroid->getTheta().x); + //delete spheroid; + /*spheroid = new SourceSersic(p.spheroid->getMag(),p.getReff() + ,p.spheroid->getPA() + ,p.spheroid->getSersicIndex() + ,p.spheroid->getAxesRatio() + ,p.spheroid->getZ() + ,p.spheroid->getTheta().x); + */ + + spheroid = p.spheroid; + + //*spheroid = *(p.spheroid); modes = p.modes; } @@ -248,9 +263,16 @@ SourceOverzierPlus & SourceOverzierPlus::operator=(const SourceOverzierPlus &p){ sinpa=p.sinpa; cosi=p.cosi; - spheroid = new SourceSersic(p.spheroid->getMag(),p.getReff(),p.spheroid->getPA() - ,p.spheroid->getSersicIndex(),p.spheroid->getAxesRatio() - ,p.spheroid->getZ(),p.spheroid->getTheta().x); + /*delete spheroid; + spheroid = new SourceSersic(p.spheroid->getMag(),p.getReff() + ,p.spheroid->getPA() + ,p.spheroid->getSersicIndex() + ,p.spheroid->getAxesRatio() + ,p.spheroid->getZ() + ,p.spheroid->getTheta().x); + */ + spheroid = p.spheroid; + modes = p.modes; return *this; @@ -266,7 +288,7 @@ PosType SourceOverzierPlus::SurfaceBrightness(PosType *y){ PosType xlength = x.length(); Point_2d z; - PosType sb; + PosType sb = 0; if(xlength > 0 && sbDo > 0){ z[0] = cospa*x[0] - sinpa*x[1]; @@ -288,6 +310,8 @@ PosType SourceOverzierPlus::SurfaceBrightness(PosType *y){ sb = sbDo; } + assert(sb >= 0.0); + sb *= pow(10,-0.4*48.6)*inv_hplanck; //std::cout << "disk sb " << sb << std::endl; @@ -309,7 +333,7 @@ PosType SourceOverzierPlus::SurfaceBrightness(PosType *y){ } } // spheroid contribution - sb += spheroid->SurfaceBrightness(y)*perturb; + sb += spheroid.SurfaceBrightness(y)*perturb; // !!! @@ -334,7 +358,7 @@ void SourceOverzierPlus::changeBand(Band band){ //mag_bulge = bulge_mag_map.at(band); //SourceOverzier::renormalize(); if(Reff > 0.0){ - spheroid->setMag(mag_bulge); + spheroid.setMag(mag_bulge); } } @@ -408,8 +432,11 @@ void SourceOverzierPlus::randomize(Utilities::RandomNumbers_NR &ran){ double q = 1 + (0.5-1)*ran(); - delete spheroid; + /*delete spheroid; spheroid = new SourceSersic(mag_bulge,Reff/arcsecTOradians,-PA + 10*(ran() - 0.5)*pi/180,index,q,zsource,getTheta().x); + */ + + spheroid.ReSet(mag_bulge,Reff/arcsecTOradians,-PA + 10*(ran() - 0.5)*pi/180,index,q,zsource,getTheta().x); for(PosType &mod : modes){ diff --git a/Source/sersic.cpp b/Source/sersic.cpp index e1e6c2b3..dd060d1a 100644 --- a/Source/sersic.cpp +++ b/Source/sersic.cpp @@ -6,6 +6,13 @@ */ #include "slsimlib.h" +SourceSersic::SourceSersic() +: Source() +{ + /// set to values that hopefully will cause an error if it is used + ReSet(1.0e10,1,0,0,-1,-1,0); +} + SourceSersic::SourceSersic( double my_mag /// Total magnitude ,double my_Reff /// Bulge half light radius (arcs) @@ -17,25 +24,40 @@ SourceSersic::SourceSersic( ) : Source() { - //std::cout << "SourceSersic constructor" << std::endl; - setReff(my_Reff); - setMag(my_mag); - setPA(my_PA); - setSersicIndex(my_index); - setAxesRatio(my_q); - - setZ(my_z); - - if(my_theta) - setTheta(my_theta[0], my_theta[1]); - - if(q > 1) - throw std::invalid_argument("Error: q must be < 1!"); - + assert(my_Reff > 0); + ReSet(my_mag,my_Reff,my_PA,my_index,my_q,my_z,my_theta); +} + +/// Reset all the parameters +void SourceSersic::ReSet( + double my_mag /// Total magnitude + ,double my_Reff /// Bulge half light radius (arcs) + ,double my_PA /// Position angle (radians) + ,double my_index /// Sersic index + ,double my_q /// axes ratio + ,double my_z /// redshift + ,const double *my_theta /// optional angular position on the sky +){ +//std::cout << "SourceSersic constructor" << std::endl; + //assert(my_Reff > 0); + setReff(my_Reff); + setMag(my_mag); + setPA(my_PA); + setSersicIndex(my_index); + setAxesRatio(my_q); + + setZ(my_z); + + if(my_theta) + setTheta(my_theta[0], my_theta[1]); + + if(q > 1) + throw std::invalid_argument("Error: q must be < 1!"); } SourceSersic::~SourceSersic() { + } PosType SourceSersic::SurfaceBrightness( @@ -43,6 +65,7 @@ PosType SourceSersic::SurfaceBrightness( ) { + if(Reff <= 0.0) return 0.0; PosType x_new[2]; x_new[0] = (x[0]-source_x[0])*cosPA+(x[1]-source_x[1])*sinPA; x_new[1] = (x[0]-source_x[0])*sinPA-(x[1]-source_x[1])*cosPA; @@ -52,6 +75,7 @@ PosType SourceSersic::SurfaceBrightness( PosType sb = flux * I_n * I_q * I_r *inv_hplanck * exp(-bn*pow(r/Reff,1./index)); if (sb < sb_limit) return 0.; + assert(sb >= 0.0); return sb; } diff --git a/include/image_processing.h b/include/image_processing.h index d73a56e9..6986d198 100644 --- a/include/image_processing.h +++ b/include/image_processing.h @@ -274,42 +274,8 @@ class PixelMap It is assumed that the size of the kernel is much smaller than the image and that the kernal has the same pixel size as the image. **/ - void convolve(PixelMap &kernel,long center_x = 0,long center_y = 0){ - std::valarray output(Nx*Ny); - - if( center_x == 0 ){ - center_x = kernel.getNx()/2; - center_y = kernel.getNy()/2; - } - - size_t Nxk = kernel.getNx(); - size_t Nyk = kernel.getNy(); - - for(size_t k1 = 0 ; k1 < Nx ; ++k1){ - long bl1 = k1 - center_x; - - for(size_t k2 = 0 ; k2 < Ny ; ++k2){ - long bl2 = k2 - center_y; - - long k = k1 + Nx * k2; - output[k] = 0; - - for(size_t j = 0 ; j < Nyk ; ++j){ - long kk2 = bl2 + j; - if(kk2 < 0 || kk2 > Ny-1) continue; - - for(size_t i = 0; i < Nxk ; ++i){ - long kk1 = bl1 + i; - if(kk1 < 0 || kk1 > Nx-1) continue; - - output[k] += map[ kk1 + Nx * kk2 ] * kernel[i + Nxk * j]; - } - } - } - } - std::swap(map,output); -} - + void convolve(PixelMap &kernel,long center_x = 0,long center_y = 0); + private: std::valarray map; void AddGrid_(const PointList &list,LensingVariable val); diff --git a/include/overzier_source.h b/include/overzier_source.h index 96778e4a..c7452361 100644 --- a/include/overzier_source.h +++ b/include/overzier_source.h @@ -141,9 +141,9 @@ class SourceOverzierPlus : public SourceOverzier int getNarms() const {return Narms;} PosType getArmAmplitude() const {return Ad;} PosType getArmAlpha() const {return arm_alpha;} - PosType getSphIndex() const {return spheroid->getSersicIndex();} - PosType getSphAxisRatio() const {return spheroid->getAxesRatio();} - PosType getSphPA() const {return spheroid->getPA();} + PosType getSphIndex() const {return spheroid.getSersicIndex();} + PosType getSphAxisRatio() const {return spheroid.getAxesRatio();} + PosType getSphPA() const {return spheroid.getPA();} void changeBand(Band band); static PosType* getx(SourceOverzierPlus &sourceo){return sourceo.source_x.x;} @@ -152,22 +152,22 @@ class SourceOverzierPlus : public SourceOverzier virtual inline void setTheta(PosType *xx){ source_x[0] = xx[0]; source_x[1] = xx[1]; - spheroid->setTheta(xx); + spheroid.setTheta(xx); } virtual void setTheta(PosType my_x,PosType my_y){ source_x[0] = my_x; source_x[1] = my_y; - spheroid->setTheta(my_x,my_y); + spheroid.setTheta(my_x,my_y); } void setBulgeAxisRatio(PosType q){ - spheroid->setAxesRatio(q); + spheroid.setAxesRatio(q); } /// Randomly change some of the internal paramters and angles of the source void randomize(Utilities::RandomNumbers_NR &ran); private: int Narms; PosType Ad,mctalpha,arm_alpha; - SourceSersic *spheroid; + SourceSersic spheroid; std::vector modes; PosType disk_phase; PosType cospa,sinpa,cosi; diff --git a/include/sersic_source.h b/include/sersic_source.h index e3b00b93..03c0de34 100644 --- a/include/sersic_source.h +++ b/include/sersic_source.h @@ -19,18 +19,21 @@ class SourceSersic : public Source { public: - SourceSersic(PosType mag,PosType Reff,PosType PA,PosType my_index,PosType my_q,PosType my_z,const PosType *theta=0); + /// sets values to invalid values + SourceSersic(); + SourceSersic(PosType mag,PosType Reff,PosType PA,PosType my_index,PosType my_q,PosType my_z,const PosType *theta=0); ~SourceSersic(); - + void ReSet(PosType mag,PosType Reff,PosType PA,PosType my_index,PosType my_q,PosType my_z,const PosType *theta=0); + /// calculates radius where the surface brightness drops by a factor f with respect to the central peak inline PosType FractionRadius (PosType f) {return Reff*pow(-log (f)/bn,index);} inline PosType getSersicIndex() const { return index; } inline PosType getAxesRatio() const { return q; } inline PosType getReff() const { return Reff*180*60*60/pi; } - inline PosType getMag() { return mag; } - inline PosType getPA() { return PA; } + inline PosType getMag() const { return mag; } + inline PosType getPA() const { return PA; } inline void setSersicIndex(PosType x) { From 3a4768c588eafbc2313dcacc716051be128b4aa5 Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Fri, 27 Apr 2018 10:03:01 +0200 Subject: [PATCH 059/227] Modified Observation for case where external noise power spectrum. --- ImageProcessing/observation.cpp | 168 +++++++++++++++++++++++++++++--- MultiPlane/MOKAfits.cpp | 4 +- MultiPlane/MOKAlens.cpp | 2 +- Source/overzier.cpp | 4 +- include/image_processing.h | 17 +++- 5 files changed, 173 insertions(+), 22 deletions(-) diff --git a/ImageProcessing/observation.cpp b/ImageProcessing/observation.cpp index 388a43b9..8bf4915e 100644 --- a/ImageProcessing/observation.cpp +++ b/ImageProcessing/observation.cpp @@ -23,7 +23,8 @@ /** * \brief Creates an observation setup that mimics a known instrument * */ -Observation::Observation(Telescope tel_name) +Observation::Observation(Telescope tel_name,size_t Npix_x,size_t Npix_y): +Npix_x(Npix_x),Npix_y(Npix_y) { @@ -193,8 +194,8 @@ Observation::Observation(Telescope tel_name) * \param ron Read-out noise in electrons/pixel * \param seeing FWHM in arcsecs of the image */ -Observation::Observation(float diameter, float transmission, float exp_time, int exp_num, float back_mag, float ron, float seeing): - diameter(diameter), transmission(transmission), exp_time(exp_time), exp_num(exp_num), back_mag(back_mag), ron(ron), seeing(seeing) +Observation::Observation(float diameter, float transmission, float exp_time, int exp_num, float back_mag, float ron, size_t Npix_x,size_t Npix_y,float seeing): +diameter(diameter), transmission(transmission), exp_time(exp_time), exp_num(exp_num), back_mag(back_mag), ron(ron), seeing(seeing),Npix_x(Npix_x),Npix_y(Npix_y) { mag_zeropoint = 2.5*log10(diameter*diameter*transmission*pi/4./hplanck) - 48.6; telescope = false; @@ -211,8 +212,8 @@ Observation::Observation(float diameter, float transmission, float exp_time, int * \param psf_file Input PSF image * \param oversample Oversampling rate of the PSF image */ -Observation::Observation(float diameter, float transmission, float exp_time, int exp_num, float back_mag, float ron, std::string psf_file, float oversample): - diameter(diameter), transmission(transmission), exp_time(exp_time), exp_num(exp_num), back_mag(back_mag), ron(ron), oversample(oversample) +Observation::Observation(float diameter, float transmission, float exp_time, int exp_num, float back_mag, float ron, std::string psf_file,size_t Npix_x,size_t Npix_y, float oversample): +diameter(diameter), transmission(transmission), exp_time(exp_time), exp_num(exp_num), back_mag(back_mag), ron(ron), oversample(oversample),Npix_x(Npix_x),Npix_y(Npix_y) { mag_zeropoint = 2.5*log10(diameter*diameter*transmission*pi/4./hplanck) - 48.6; @@ -290,7 +291,7 @@ void Observation::setNoiseCorrelation(std::string nc_file /// name of fits file for(size_t i = 0 ; i < N ; ++i){ corr_max = MAX(corr_max,noise_corr[i]); sum += noise_corr[i]; - noise_corr[i] = sqrt(fabs(noise_corr[i])); + //noise_corr[i] = sqrt(fabs(noise_corr[i])); } sum = sqrt(sum); for(size_t i = 0 ; i < N ; ++i) noise_corr[i] /= sum; @@ -298,7 +299,62 @@ void Observation::setNoiseCorrelation(std::string nc_file /// name of fits file std::cout << "zero lag noise correlation is : " << corr_max << " size : " << noise_corr.size() << std::endl; - PixelMap::swap(noise_corr,nc_map); + //PixelMap::swap(noise_corr,nc_map); + + + // now find the power spectrum of the noise + + // calculates normalisation of ncorr + int N_ncorr = noise_corr.size(); + side_ncorr = sqrt(N_ncorr); + double map_norm = 0.; + for (int i = 0; i < N_ncorr; i++) + { + map_norm += noise_corr[i]; + } + fftw_plan p_ncorr; + + // arrange ncorr data for fft, creates plane, then performs fft + // ncorr data are moved into the four corners of ncorr_big_zeropad + + assert(Npix_x == Npix_y); + size_t Npix = Npix_x; + size_t ncorr_big_zeropad_Npixels = Npix + side_ncorr; + size_t ncorr_big_zeropad_Npixels2 = ncorr_big_zeropad_Npixels*ncorr_big_zeropad_Npixels; + + std::vector ncorr_big_zeropad(ncorr_big_zeropad_Npixels2); + std::vector > out_ncorr(ncorr_big_zeropad_Npixels*(ncorr_big_zeropad_Npixels/2+1)); + + sqrt_noise_power.resize(ncorr_big_zeropad_Npixels*(ncorr_big_zeropad_Npixels/2+1)); + + p_ncorr = fftw_plan_dft_r2c_2d(ncorr_big_zeropad_Npixels,ncorr_big_zeropad_Npixels,ncorr_big_zeropad.data(), reinterpret_cast(out_ncorr.data()), FFTW_ESTIMATE); + long ix, iy; + for (int i = 0; i < ncorr_big_zeropad_Npixels*ncorr_big_zeropad_Npixels; i++) + { + ix = i/ncorr_big_zeropad_Npixels; + iy = i%ncorr_big_zeropad_Npixels; + if(ix=ncorr_big_zeropad_Npixels-side_ncorr/2) + ncorr_big_zeropad[i] = noise_corr[(ix+side_ncorr/2)*side_ncorr+(iy-(ncorr_big_zeropad_Npixels-side_ncorr/2))]/map_norm; + else if(ix>=ncorr_big_zeropad_Npixels-side_ncorr/2 && iy=ncorr_big_zeropad_Npixels-side_ncorr/2 && iy>=ncorr_big_zeropad_Npixels-side_ncorr/2) + ncorr_big_zeropad[i] = noise_corr[(ix-(ncorr_big_zeropad_Npixels-side_ncorr/2))*side_ncorr+(iy-(ncorr_big_zeropad_Npixels-side_ncorr/2))]/map_norm; + else + ncorr_big_zeropad[i] = 0.; + } + fftw_execute(p_ncorr); + + fftw_destroy_plan(p_ncorr); + + // normalized so that later the variance will be conserved + sum = 0.0; + for(size_t i = 0 ; i < sqrt_noise_power.size() ; ++i) + sum += out_ncorr[i].real(); + sum /= sqrt_noise_power.size(); + for(size_t i = 0 ; i < sqrt_noise_power.size() ; ++i) + sqrt_noise_power[i] = sqrt( out_ncorr[i].real()/sum ); } /** \brief Converts the input map to a realistic image @@ -475,6 +531,89 @@ void Observation::ApplyPSF(PixelMap &pmap) #endif } } + +/** * \brief Smooths the image with a PSF map. + * + */ +void Observation::CorrelateNoise(PixelMap &pmap) +{ + if(pmap.getNx() != pmap.getNy()){ + std::cout << "Observation::AddNoise() Doesn't work on nonsquare maps" << std::endl; + throw std::runtime_error("nonsquare"); + } + if(pmap.getNx() != Npix_x){ + std::cout << "Observation::AddNoise() Map must have the same dimensions as Observation was constructed with." << std::endl; + throw std::runtime_error("nonsquare"); + } + +#ifdef ENABLE_FFTW + + + // creates plane for fft of map, sets properly input and output data, then performs fft + assert(Npix_x == Npix_y); + size_t Npix = Npix_x; + size_t ncorr_big_zeropad_Npixels = Npix + side_ncorr; + long Npix_zeropad = Npix + side_ncorr; + + size_t fftsize = sqrt_noise_power.size(); + + // rows and columns between first_p and last_p are copied in the zero-padded version + long first_p = side_ncorr/2; + long last_p = first_p + (Npix-1); + std::vector > out(fftsize); + + // add zero-padding + std::vector in_zeropad(Npix_zeropad*Npix_zeropad); + for (int i = 0; i < Npix_zeropad*Npix_zeropad; i++) + { + long ix = i/Npix_zeropad; + long iy = i%Npix_zeropad; + if (ix >= first_p && ix <= last_p && iy >= first_p && iy <= last_p) + in_zeropad[i] = pmap[(ix-side_ncorr/2)*Npix+(iy-side_ncorr/2)]; + else + in_zeropad[i] = 0.; + } + + fftw_plan p = fftw_plan_dft_r2c_2d(Npix_zeropad,Npix_zeropad,in_zeropad.data() + , reinterpret_cast(out.data()), FFTW_ESTIMATE); + fftw_execute(p); + + // creates plane for perform backward fft after convolution, sets output data + fftw_plan p2; + //double* out2 = new double[Npix_zeropad*Npix_zeropad]; + std::vector out2(Npix_zeropad*Npix_zeropad); + p2 = fftw_plan_dft_c2r_2d(Npix_zeropad,Npix_zeropad,reinterpret_cast(out.data()), out2.data(), FFTW_ESTIMATE); + + + // performs convolution in Fourier space , and transforms back to real space + for (unsigned long i = 0; i < fftsize ; i++) + { + size_t ix = i/(Npix_zeropad/2+1); + size_t iy = i%(Npix_zeropad/2+1); + if (ix>Npix_zeropad/2) + out[i] *= sqrt_noise_power[(ncorr_big_zeropad_Npixels-(Npix_zeropad-ix))*(ncorr_big_zeropad_Npixels/2+1)+iy]; + else + out[i] *= sqrt_noise_power[ix*(ncorr_big_zeropad_Npixels/2+1)+iy]; + } + fftw_execute(p2); + + // translates array of data in (normalised) counts map + for (unsigned long i = 0; i < Npix_zeropad*Npix_zeropad; i++) + { + size_t ix = i/Npix_zeropad; + size_t iy = i%Npix_zeropad; + if (ix >= first_p && ix <= last_p && iy >= first_p && iy <= last_p) + { + int ii = (ix-side_ncorr/2)*Npix+(iy-side_ncorr/2); + pmap[ii] = out2[i]/(Npix_zeropad*Npix_zeropad); + } + } + return; +#else + std::cout << "Please enable the preprocessor flag ENABLE_FFTW !" << std::endl; + exit(1); +#endif +} /// Outputs rms of noise counts due to background and instrument /// in the unit decided by the user float Observation::getBackgroundNoise(float resolution, unitType unit) @@ -513,7 +652,7 @@ void Observation::AddNoise(PixelMap &pmap,long *seed) double norm_map; PixelMap noise_map(pmap); - double sum=0,sum2=0; + //double sum=0,sum2=0; size_t N = pmap.size(); for (unsigned long i = 0; i < N ; i++) { @@ -537,15 +676,16 @@ void Observation::AddNoise(PixelMap &pmap,long *seed) noise = gasdev(seed)*rms2; noise_map[i] = (k - 1 + noise - back_mean - norm_map)/exp_time; } - sum += noise_map[i]; - sum2 += noise_map[i]*noise_map[i]; + //sum += noise_map[i]; + //sum2 += noise_map[i]*noise_map[i]; } - sum /= N; - std::cout << "Noise Variance is : " << (sum2 - N*sum*sum)/(N-1) << std::endl; + //sum /= N; + //sum2 = (sum2 - N*sum*sum)/(N-1); + //std::cout << "Noise Variance in flux units is : " << sum2 << std::endl; + + CorrelateNoise(noise_map); - if(nc_map.size() > 0) noise_map.convolve(nc_map); /// correlate the pixels - pmap += noise_map; return; diff --git a/MultiPlane/MOKAfits.cpp b/MultiPlane/MOKAfits.cpp index f8a604d0..b4d89377 100644 --- a/MultiPlane/MOKAfits.cpp +++ b/MultiPlane/MOKAfits.cpp @@ -117,7 +117,7 @@ void LensHaloMassMap::readMap(){ int nhdu = h0->axis(2); if(nhdu==1){ - std:: cout << " preProcessing Map MOKA fits file has only KAPPA map" << std:: endl; + //std:: cout << " preProcessing Map MOKA fits file has only KAPPA map" << std:: endl; PreProcessFFTWMap(); } else{ @@ -378,7 +378,7 @@ void LensHaloMassMap::readMap(){ // create alpha and gamma arrays by FFT // valid only to force the map to be square map->nx = map->ny = npixels; #ifdef ENABLE_FFTW - std:: cout << " preProcessing Map " << std:: endl; + //std:: cout << " preProcessing Map " << std:: endl; // reducing the size of the maps // carlo test begin /* diff --git a/MultiPlane/MOKAlens.cpp b/MultiPlane/MOKAlens.cpp index 78d7268a..be5ecdc1 100644 --- a/MultiPlane/MOKAlens.cpp +++ b/MultiPlane/MOKAlens.cpp @@ -358,7 +358,7 @@ void LensHaloMassMap::setMap( // create alpha and gamma arrays by FFT // valid only to force the map to be square map->nx = map->ny = npixels; #ifdef ENABLE_FFTW - std:: cout << " preProcessing Map " << std:: endl; + //std:: cout << " preProcessing Map " << std:: endl; PreProcessFFTWMap(); #else std::cout << "Please enable the preprocessor flag ENABLE_FFTW !" << std::endl; diff --git a/Source/overzier.cpp b/Source/overzier.cpp index fdfb42b4..99f11e1a 100644 --- a/Source/overzier.cpp +++ b/Source/overzier.cpp @@ -392,7 +392,9 @@ void SourceOverzierPlus::randomize(Utilities::RandomNumbers_NR &ran){ mag += tmp; PA = pi*ran(); - inclination = 0.9*pi/2*ran(); + inclination = 1.0*pi/2*ran(); + if(cos(inclination)< 0.25) inclination = acos(0.25); + cospa = cos(PA); sinpa = sin(PA); diff --git a/include/image_processing.h b/include/image_processing.h index 6986d198..6a86727c 100644 --- a/include/image_processing.h +++ b/include/image_processing.h @@ -343,9 +343,10 @@ typedef enum {counts_x_sec, flux} unitType; class Observation { public: - Observation(Telescope tel_name); - Observation(float diameter, float transmission, float exp_time, int exp_num, float back_mag, float ron, float seeing = 0.); - Observation(float diameter, float transmission, float exp_time, int exp_num, float back_mag, float ron, std::string psf_file, float oversample = 1.); + Observation(Telescope tel_name,size_t Npix_x,size_t Npix_y); + Observation(float diameter, float transmission, float exp_time, int exp_num, float back_mag, float ron + ,size_t Npix_x,size_t Npix_y,float seeing = 0.); + Observation(float diameter, float transmission, float exp_time, int exp_num, float back_mag, float ron ,std::string psf_file,size_t Npix_x,size_t Npix_y, float oversample = 1.); float getExpTime(){return exp_time;} @@ -369,7 +370,15 @@ class Observation void Convert_back(PixelMap &map); void setExpTime(float time){exp_time = time;} + size_t getNx(){ return Npix_x;} + size_t getNy(){ return Npix_y;} + private: + + size_t Npix_x,Npix_y; + std::vector sqrt_noise_power; // stores sqrt root of power noise spectrum + size_t side_ncorr; // pixels on a side of input noise correlation function + float diameter; // diameter of telescope (in cm) float transmission; // total transmission of the instrument float mag_zeropoint; // magnitude of a source that produces one count/sec in the image @@ -379,7 +388,6 @@ class Observation float ron; // read-out noise in electrons/pixel float seeing; // full-width at half maximum of the gaussian smoothing std::valarray map_psf; // array of the point spread function - PixelMap nc_map; // noise correlation function float oversample; // psf oversampling factor double pix_size; // pixel size (in rad) bool telescope; // was the observation created from a default telescope? @@ -388,6 +396,7 @@ class Observation void PhotonToCounts(PixelMap &pmap); void ApplyPSF(PixelMap &pmap); + void CorrelateNoise(PixelMap &pmap); //PixelMap noise_correlation; }; From fba0d0f65ec57789a1992d832361daf0d38395e3 Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Mon, 11 Jun 2018 15:44:28 +0200 Subject: [PATCH 060/227] Fixed fmin() and fmax() missuse. --- ImageProcessing/pixelize.cpp | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/ImageProcessing/pixelize.cpp b/ImageProcessing/pixelize.cpp index fcc8d86f..0bad469f 100644 --- a/ImageProcessing/pixelize.cpp +++ b/ImageProcessing/pixelize.cpp @@ -319,8 +319,8 @@ PixelMap::PixelMap( old_p1[0] = std::max(0,int(ix*res_ratio)); old_p1[1] = std::max(0,int(iy*res_ratio)); - old_p2[0] = fmin(old_Nx-1,int((ix+1.)*res_ratio)); - old_p2[1] = fmin(old_Ny-1,int((iy+1.)*res_ratio)); + old_p2[0] = MIN(old_Nx-1,int((ix+1.)*res_ratio)); + old_p2[1] = MIN(old_Ny-1,int((iy+1.)*res_ratio)); for (int old_iy = old_p1[1]; old_iy <= old_p2[1]; ++old_iy) { @@ -2171,12 +2171,12 @@ void PixelMap::copy_in( double ix = (x[0] - pmap.map_boundary_p1[0])/pmap.resolution; double iy = (x[1] - pmap.map_boundary_p1[1])/pmap.resolution; - double xmin = fmax(0,ix - halfpixel); - double xmax = fmin(NNx,ix + halfpixel); + double xmin = MAX(0,ix - halfpixel); + double xmax = MIN(NNx,ix + halfpixel); if(xmin >= xmax) continue; - double ymin = fmax(0,iy - halfpixel); - double ymax = fmin(NNy,iy + halfpixel); + double ymin = MAX(0,iy - halfpixel); + double ymax = MIN(NNy,iy + halfpixel); if(ymin >= ymax) continue; long imin = (long)(xmin); @@ -2185,10 +2185,10 @@ void PixelMap::copy_in( long jmax = (long)(ymax); for(size_t j = jmin ; j <= jmax ; ++j ){ - double area1 = fmin(ymax,j+1) - fmax(ymin,j); + double area1 = MIN(ymax,j+1) - MIN(ymin,j); if(area1 <= 0.0) continue; for(size_t i = imin ; i <= imax ; ++i ){ - double area = (fmin(xmax,i+1) - fmax(xmin,i)) * area1 ; + double area = (MIN(xmax,i+1) - MIN(xmin,i)) * area1 ; map[ii] += pmap.map[i + NNx*j]*area/res_ratio2; } } From 09cebfe5b34d093694852fc676a7cef85c305194 Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Mon, 11 Jun 2018 15:50:33 +0200 Subject: [PATCH 061/227] Fixed last commit. --- ImageProcessing/pixelize.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ImageProcessing/pixelize.cpp b/ImageProcessing/pixelize.cpp index 0bad469f..f029e61b 100644 --- a/ImageProcessing/pixelize.cpp +++ b/ImageProcessing/pixelize.cpp @@ -2185,10 +2185,10 @@ void PixelMap::copy_in( long jmax = (long)(ymax); for(size_t j = jmin ; j <= jmax ; ++j ){ - double area1 = MIN(ymax,j+1) - MIN(ymin,j); + double area1 = MIN(ymax,j+1) - MAX(ymin,j); if(area1 <= 0.0) continue; for(size_t i = imin ; i <= imax ; ++i ){ - double area = (MIN(xmax,i+1) - MIN(xmin,i)) * area1 ; + double area = (MIN(xmax,i+1) - MAX(xmin,i)) * area1 ; map[ii] += pmap.map[i + NNx*j]*area/res_ratio2; } } From edde79dc1c0f022846525161579eba6b6946cbea Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Thu, 21 Jun 2018 18:31:20 +0200 Subject: [PATCH 062/227] No functional change. --- ImageProcessing/observation.cpp | 6 ------ include/image_processing.h | 4 +++- 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/ImageProcessing/observation.cpp b/ImageProcessing/observation.cpp index 8bf4915e..73b4edb5 100644 --- a/ImageProcessing/observation.cpp +++ b/ImageProcessing/observation.cpp @@ -384,12 +384,6 @@ void Observation::Convert(PixelMap &map, bool psf, bool noise, long *seed, unitT return; } -/// returns factor by which code image units need to be multiplied by to get flux units -double Observation::flux_convertion_factor() -{ - return pow(10,-0.4*mag_zeropoint); -} - /// Converts an observed image to the units of the lensing simulation void Observation::Convert_back (PixelMap &map) { diff --git a/include/image_processing.h b/include/image_processing.h index 6a86727c..d4fbbdcd 100644 --- a/include/image_processing.h +++ b/include/image_processing.h @@ -366,7 +366,9 @@ class Observation void setPSF(std::string psf_file, float os = 1.); void setNoiseCorrelation(std::string nc_file); void Convert(PixelMap &map, bool psf, bool noise,long *seed, unitType unit = counts_x_sec); - double flux_convertion_factor(); + /// returns factor by which code image units need to be multiplied by to get flux units + double flux_convertion_factor(){ return pow(10,-0.4*mag_zeropoint); } + void Convert_back(PixelMap &map); void setExpTime(float time){exp_time = time;} From 0a04f1bc8e7a0ba9d21e89db3332d7a2e7437ea4 Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Wed, 18 Jul 2018 15:47:21 +0200 Subject: [PATCH 063/227] Added a counter to RandomNumbers_NR --- TreeCode_link/utilities.cpp | 4 ++-- include/utilities_slsim.h | 2 ++ 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/TreeCode_link/utilities.cpp b/TreeCode_link/utilities.cpp index ca47851d..d1fcf3ac 100644 --- a/TreeCode_link/utilities.cpp +++ b/TreeCode_link/utilities.cpp @@ -363,7 +363,7 @@ unsigned long prevpower(unsigned long k){ #endif RandomNumbers_NR::RandomNumbers_NR(long seed): - IM1(2147483399),IM2(2147483399),IA1(40014),IA2(40692),IQ1(53668), + calls(0),IM1(2147483399),IM2(2147483399),IA1(40014),IA2(40692),IQ1(53668), IQ2(52774),IR1(12211),IR2(3791),EPS(1.2e-7),idum2(123456789),iy(0), count(true) { @@ -388,11 +388,11 @@ unsigned long prevpower(unsigned long k){ if (j < 32) iv[j] = idum; } iy=iv[0]; - } /// return a uniform random number between 0 and 1 PosType RandomNumbers_NR::operator()(void){ + ++calls; return ran2(); } diff --git a/include/utilities_slsim.h b/include/utilities_slsim.h index baa54ca1..42e393ad 100644 --- a/include/utilities_slsim.h +++ b/include/utilities_slsim.h @@ -977,6 +977,7 @@ namespace Utilities PosType operator()(void); /// generates a Gaussian distributed number with unit variance by polar Box-Muller transform PosType gauss(){ + ++calls; if(count){ do{ u = 2*ran2() - 1; @@ -993,6 +994,7 @@ namespace Utilities } }; + size_t calls = 0; /// total number of calls private: long idum; PosType ran2(void); From 8065586175893d439bc9c95dd1e5cb111acd31d8 Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Mon, 27 Aug 2018 18:47:40 +0200 Subject: [PATCH 064/227] Small change to Utilities::readASCI() --- ImageProcessing/pixelize.cpp | 2 +- include/utilities_slsim.h | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/ImageProcessing/pixelize.cpp b/ImageProcessing/pixelize.cpp index 3eaa2d63..bc69eac2 100644 --- a/ImageProcessing/pixelize.cpp +++ b/ImageProcessing/pixelize.cpp @@ -1641,7 +1641,7 @@ void Utilities::LoadFitsImages( return ; } -/** \brief Reads the file names in a directory that contain a specific sub string. +/*** \brief Reads the file names in a directory that contain a specific sub string. */ void Utilities::ReadFileNames( diff --git a/include/utilities_slsim.h b/include/utilities_slsim.h index ec1ac073..010f9c3a 100644 --- a/include/utilities_slsim.h +++ b/include/utilities_slsim.h @@ -1178,6 +1178,7 @@ namespace Utilities ,std::vector &x /// vector that will contain the first column ,std::vector &y /// vector that will contain the first column ,std::string delineator = " " /// specific string the seporates columns, ex. ",", "|", etc. + ,int skiplines = 0 ,bool verbose = false ){ @@ -1202,6 +1203,10 @@ namespace Utilities std::cout << "Reading caustic information from " << filename << std::endl; size_t i=0; + while(i < skiplines){ + getline(file_in,myline); + ++i; + } while(file_in.peek() == '#'){ file_in.ignore(10000,'\n'); ++i; @@ -1385,6 +1390,7 @@ namespace Utilities ,int &columns ,int &rows ,char comment_char = '#' + ,int skiplines = 0 ,size_t MaxNrows = std::numeric_limits::max() ,bool verbose = true){ @@ -1403,6 +1409,14 @@ namespace Utilities rows = 0; size_t first_data_line; + // skip over first lines + int i=0; + while(i < skiplines){ + std::getline(file,line); + if(!file) break; // probably EOF + ++i; + } + // read comment lines and first data line do{ first_data_line = file.tellg(); From 49b2145aa135110168ce7845721b555c7d8567f1 Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Wed, 5 Sep 2018 11:51:55 +0200 Subject: [PATCH 065/227] Changed pi constant to PI --- AnalyticNSIE/base_analens.cpp | 88 +++++++++++++------------- AnalyticNSIE/elliptic.cpp | 6 +- AnalyticNSIE/jaffe_lens.cpp | 10 +-- AnalyticNSIE/nfw_lens.cpp | 6 +- AnalyticNSIE/nsie.cpp | 2 +- AnalyticNSIE/powerlaw.cpp | 8 +-- AnalyticNSIE/randomize_lens.cpp | 30 ++++----- AnalyticNSIE/readfiles_uni.cpp | 2 +- AnalyticNSIE/source.cpp | 18 +++--- AnalyticNSIE/sourceAnaGalaxy.cpp | 12 ++-- BLR/blr_surface_brightness.cpp | 2 +- BLR/blr_surface_brightness2.0.cpp | 4 +- Fitlens/fitlens.cpp | 28 ++++----- FullRange/implant_stars.cpp | 6 +- ImageProcessing/observation.cpp | 38 ++++++------ ImageProcessing/pixelize.cpp | 29 ++++----- InputParameters/causticdata.cpp | 2 +- MultiPlane/MOKAlens.cpp | 8 +-- MultiPlane/lens.cpp | 96 ++++++++++++++--------------- MultiPlane/lens_halos.cpp | 78 +++++++++++------------ Source/overzier.cpp | 18 +++--- TreeCode/quadTree.cpp | 34 +++++----- TreeCode_link/curve_routines.cpp | 22 +++---- TreeCode_link/geometry.cpp | 2 +- TreeCode_link/image_finder_kist.cpp | 24 ++++---- TreeCode_link/map_images.cpp | 2 +- TreeCode_link/utilities.cpp | 2 +- include/Tree.h | 2 +- include/TreeNB.h | 4 +- include/lens_halos.h | 32 +++++----- include/overzier_source.h | 10 +-- include/point.h | 4 +- include/quadTree.h | 14 ++--- include/sersic_source.h | 6 +- include/source.h | 8 +-- include/sourceAnaGalaxy.h | 2 +- include/standard.h | 4 +- 37 files changed, 332 insertions(+), 331 deletions(-) diff --git a/AnalyticNSIE/base_analens.cpp b/AnalyticNSIE/base_analens.cpp index 30c7dc70..d7c5e265 100644 --- a/AnalyticNSIE/base_analens.cpp +++ b/AnalyticNSIE/base_analens.cpp @@ -41,7 +41,7 @@ void LensHaloBaseNSIE::force_halo( if(sigma > 0.0){ PosType xt[2]={0,0}; float units = pow(sigma/lightspeed,2)/Grav ; /// sqrt(fratio); // in mass / PhysMpc - units *= 2. * Rsize / pi ; // units now in mass /// Multiplying by 2*Rsize/pi to match with Power Law + units *= 2. * Rsize / PI ; // units now in mass /// Multiplying by 2*Rsize/PI to match with Power Law xt[0]=xcm[0]; // in PhysMpc xt[1]=xcm[1]; @@ -94,7 +94,7 @@ void LensHaloBaseNSIE::force_halo( // Printing quantities for control : // std::cout << " xt in force_halo : @@@ " << xt[0]/Dls << " " << xt[1]/Dls << " @@@" << std::endl ; - // std::cout << "alpha in force_halo : " << alpha_tmp[0] * (4*pi*Grav) << " " << alpha_tmp[1]* (4*pi*Grav) << std::endl ; + // std::cout << "alpha in force_halo : " << alpha_tmp[0] * (4*PI*Grav) << " " << alpha_tmp[1]* (4*PI*Grav) << std::endl ; // std::cout << "xt - alpha in force_halo : !!! " << xt[0] - alpha_tmp[0] << " " << xt[1] - alpha_tmp[1] << " !!!" << std::endl ; // Changing the sign because there is a change of sign in LensPlaneSingular::force : @@ -242,12 +242,12 @@ void LensHaloAnaNSIE::setCosmology(const COSMOLOGY& cosmo) Dl = cosmo.angDist(0,zlens); Ds = cosmo.angDist(0,zsource_reference); Dls = cosmo.angDist(zlens,zsource_reference); - MpcToAsec = 60*60*180 / pi / Dl; + MpcToAsec = 60*60*180 / PI / Dl; // in Mpc - Einstein_ro=4*pi*pow(sigma/lightspeed,2)*Dl + Einstein_ro=4*PI*pow(sigma/lightspeed,2)*Dl *Dls/Ds; // find critical density - Sigma_crit=Ds/Dls/Dl/4/pi/Grav; + Sigma_crit=Ds/Dls/Dl/4/PI/Grav; to = (1+zlens)*Ds/Dls/Dl/8.39428142e-10; } void LensHaloFit::setCosmology(const COSMOLOGY& cosmo) @@ -256,10 +256,10 @@ void LensHaloFit::setCosmology(const COSMOLOGY& cosmo) Dl = cosmo.angDist(0,zlens); Ds = cosmo.angDist(0,zsource_reference); Dls = cosmo.angDist(zlens,zsource_reference); - MpcToAsec = 60*60*180 / pi / Dl; + MpcToAsec = 60*60*180 / PI / Dl; // find critical density - Sigma_crit=Ds/Dls/Dl/4/pi/Grav; + Sigma_crit=Ds/Dls/Dl/4/PI/Grav; to = (1+zlens)*Ds/Dls/Dl/8.39428142e-10; } @@ -374,7 +374,7 @@ PosType LensHaloBaseNSIE::getParam(std::size_t p) const case 1: return fratio; case 2: - return pa/pi; + return pa/PI; default: throw std::invalid_argument("bad parameter index for getParam()"); } @@ -395,7 +395,7 @@ PosType LensHaloBaseNSIE::setParam(std::size_t p, PosType val) case 1: return (fratio = (float)between(val, 1e-10, 1.)); case 2: - return (pa = (float)between(pi*val, -pi/2, pi/2)); + return (pa = (float)between(PI*val, -PI/2, PI/2)); default: throw std::invalid_argument("bad parameter index for setParam()"); } @@ -578,7 +578,7 @@ void LensHalo::gradial2(PosType r,PosType mu, PosType sigma, PosType g[]){ /// Calculates fourier-coefficients for power law halo double LensHalo::fourier_coeff(double n, double q, double beta){ struct fourier_func f(n,q,beta); - return Utilities::nintegrate(f,0.0,2*pi,1.0e-6); + return Utilities::nintegrate(f,0.0,2*PI,1.0e-6); } /// Calculates potential (phi_int) from alpha_h. If flag is_alphah_a_table is True it takes and integrates directly the gfunction instead of alpha_h. The gfunction is used for the InterpolationTable used in alpha_h. Setting the flag to False speeds up the calculation of phi_h. @@ -589,7 +589,7 @@ PosType LensHalo::alpha_int(PosType x) const{ PosType LensHalo::norm_int(PosType r_max){ struct norm_func g(*this,r_max); - return Utilities::nintegrate(g,0.0,2.*pi,1.0e-8); + return Utilities::nintegrate(g,0.0,2.*PI,1.0e-8); } //PosType LensHalo::norm_intt(PosType theta){ @@ -605,11 +605,11 @@ void LensHalo::calcModes(double q, double beta, double rottheta, PosType my_mod[ } // fill in modes with their values for an elliptical lens if(q != 1.0){ - my_mod[0] = fourier_coeff(0, q, beta)/pi/2.; + my_mod[0] = fourier_coeff(0, q, beta)/PI/2.; for(i=4;i0.1 ){ @@ -715,7 +715,7 @@ PosType LensHalo::alpha_ell(PosType x,PosType theta){ // used only for calculati felliptical(r,fratio,theta,f,g); - PosType alpha_isoG = mass*alpha_h(f[0]/rscale)/f[0]/pi; + PosType alpha_isoG = mass*alpha_h(f[0]/rscale)/f[0]/PI; alpha_r = alpha_isoG * g[1]; // with damping alpha_theta = alpha_isoG * f[1] / r; // with damping @@ -726,7 +726,7 @@ PosType LensHalo::alpha_ell(PosType x,PosType theta){ // used only for calculati alpha[0] = (alpha_r*cos(theta) - alpha_theta*sin(theta)); alpha[1] = alpha_r*sin(theta) + alpha_theta*cos(theta); - double kappa_isoG = mass*kappa_h(f[0]/rscale)/f[0]/f[0]/pi; + double kappa_isoG = mass*kappa_h(f[0]/rscale)/f[0]/f[0]/PI; double phitwo = ( 2*kappa_isoG + alpha_isoG/f[0] ); @@ -759,9 +759,9 @@ void LensHalo::alphakappagamma_asym( //Utilities::rotation(x,xt,theta); felliptical(x,fratio,theta+pa,f,g); - PosType alpha_isoG = mass*alpha_h(f[0])/f[0]/pi; - PosType kappa_isoG = mass*kappa_h(f[0])/f[0]/f[0]/pi; - PosType phi_isoG = mass*phi_int(f[0])/pi; + PosType alpha_isoG = mass*alpha_h(f[0])/f[0]/PI; + PosType kappa_isoG = mass*kappa_h(f[0])/f[0]/f[0]/PI; + PosType phi_isoG = mass*phi_int(f[0])/PI; alpha_r = alpha_isoG * g[1]; // with damping alpha_theta = alpha_isoG * f[1]/x; // with damping @@ -773,7 +773,7 @@ void LensHalo::alphakappagamma_asym( /* To check for the correctness of alpha_isoG being the first derivatice of phi PosType h=1e-4; - double phione=mass/pi*(phi_int(G+h)-phi_int(G-h))/2/h; + double phione=mass/PI*(phi_int(G+h)-phi_int(G-h))/2/h; std::cout << "I: " << alpha_isoG << " " << phione << " " <(funcX,0,MIN(mo,1.0),1.0e-1)/pi; + alpha[0] = -8*a*b*xtmp[0]*IDAXDM(lambda,a2,b2,xtmp,Rsize,mo)/PI; + //alpha[0] = -8*a*b*xtmp[0]*Utilities::nintegrate(funcX,0,MIN(mo,1.0),1.0e-1)/PI; //DALPHAYDM funcY(lambda,a2,b2,xtmp,this); - alpha[1] = -8*a*b*xtmp[1]*IDAYDM(lambda,a2,b2,xtmp,Rsize,mo)/pi; - //alpha[1] = -8*a*b*xtmp[1]*Utilities::nintegrate(funcY,0,MIN(mo,1.0),1.0e-1)/pi; + alpha[1] = -8*a*b*xtmp[1]*IDAYDM(lambda,a2,b2,xtmp,Rsize,mo)/PI; + //alpha[1] = -8*a*b*xtmp[1]*Utilities::nintegrate(funcY,0,MIN(mo,1.0),1.0e-1)/PI; double temp = alpha[0]; //std::cout << c << " " << s << std::endl; alpha[0] = temp*c + alpha[1]*s; alpha[1] = -temp*s + alpha[1]*c; double xi=sqrt(xm[0]*xm[0]+xm[1]*xm[1]/fratio/fratio); - *kappa = mass*kappa_h(xi)/pi/xi/xi; + *kappa = mass*kappa_h(xi)/PI/xi/xi; gamma[0] = 0.0; gamma[1] = 0.0; @@ -932,7 +932,7 @@ void LensHalo::alphakappagamma3asym( // Keeton's 2001 adaption of Schramm 1990 alpha[1] = -temp*s + alpha[1]*c; double xi=sqrt(xtmp[0]*xtmp[0]+xtmp[1]*xtmp[1]/fratio/fratio); - *kappa = mass*kappa_h(xi)/pi/xi/xi; + *kappa = mass*kappa_h(xi)/PI/xi/xi; double gt = 0.5*(2.0*fratio*xtmp[0]*xtmp[0]*SCHRAMMKN(0,xtmp,Rsize)+fratio*j0*2.75 - 2.0*fratio*xtmp[1]*xtmp[1]*SCHRAMMKN(2,xtmp,Rsize)-fratio*j1*2.25); double g45 = -2.0*fratio*xtmp[0]*xtmp[1]*SCHRAMMKN(1,xtmp,Rsize); gamma[0] = (cos(2*theta)*gt - sin(2*theta)*g45); @@ -1023,7 +1023,7 @@ void LensHalo::felliptical(double r, double q, double theta, double f[], double PosType LensHalo::renormalization(PosType r_max){ // Calculates renormalization factor in the constructor of PowerLaw and NFW only (for now) double fac=1; - return norm_int(fac*r_max)*fac*r_max/2.0/pi/(-1.0*alpha_h(fac*r_max)/r_max/fac); + return norm_int(fac*r_max)*fac*r_max/2.0/PI/(-1.0*alpha_h(fac*r_max)/r_max/fac); } PosType LensHalo::kappa_asym(PosType r, PosType theta){ /// radius in Mpc (Not x = r/rscale) @@ -1047,7 +1047,7 @@ PosType LensHalo::kappa_asym(PosType r, PosType theta){ /// radius in Mpc (Not x /* Phi(G(r)) ansatz (IV in notes) with r-dependent modes a_n(r) [requires renormalization?] PosType LensHalo::renormalization(PosType r_max){ // Calculates renormalization factor in the constructor of PowerLaw and NFW only (for now) double fac=1; - return norm_int(fac*r_max)*fac*r_max/2.0/pi/(-1.0*alpha_h(fac*r_max)/r_max/fac); + return norm_int(fac*r_max)*fac*r_max/2.0/PI/(-1.0*alpha_h(fac*r_max)/r_max/fac); } PosType LensHalo::kappa_asym(PosType r, PosType theta){ /// radius in Mpc (Not x = r/rscale) @@ -1055,8 +1055,8 @@ PosType LensHalo::kappa_asym(PosType r, PosType theta){ /// radius in Mpc (Not x double f[3],g[3]; //double kappa; felliptical(r,fratio,theta,f,g); - double alpha_isoG = mass*alpha_h(f[0]/rscale)/f[0]/pi; - double kappa_isoG = mass*kappa_h(f[0]/rscale)/f[0]/f[0]/pi; + double alpha_isoG = mass*alpha_h(f[0]/rscale)/f[0]/PI; + double kappa_isoG = mass*kappa_h(f[0]/rscale)/f[0]/f[0]/PI; // double b=dbfunction(G); // std::cout<< b << std::endl; @@ -1075,7 +1075,7 @@ PosType LensHalo::kappa_asym(PosType r, PosType theta){ /// radius in Mpc (Not x //double beta=get_slope(); // needed for PowerLawHalos - //double phitwo = mass*(1-beta)*pow(f[0]/Rsize,-beta)/pi/Rsize/Rsize; // works for PowerLawHalos + //double phitwo = mass*(1-beta)*pow(f[0]/Rsize,-beta)/PI/Rsize/Rsize; // works for PowerLawHalos //phitwo=dgfunctiondx(G); // used for a check of values, leave it here for now. diff --git a/AnalyticNSIE/elliptic.cpp b/AnalyticNSIE/elliptic.cpp index a343a3a7..9343aa5d 100644 --- a/AnalyticNSIE/elliptic.cpp +++ b/AnalyticNSIE/elliptic.cpp @@ -67,12 +67,12 @@ void Elliptic::alpha(PosType x[],PosType alpha[]){ std::cout << "mo = " << mo << "a2 = " << a2 << "b2 = " << b2 << "x1 " << xtmp[0] << "x2 " << xtmp[1] << std::endl; Elliptic::DALPHAXDM funcX(lambda,a2,b2,xtmp,isohalo); - //alpha[0] = -8*a*b*xtmp[0]*Utilities::nintegrate(funcX,0,MIN(mo,1.0),1.0e-6)/pi; - alpha[0] = -8*a*b*xtmp[0]*Utilities::nintegrate(funcX,0,MIN(mo,1.0),1.0e-6)/pi; + //alpha[0] = -8*a*b*xtmp[0]*Utilities::nintegrate(funcX,0,MIN(mo,1.0),1.0e-6)/PI; + alpha[0] = -8*a*b*xtmp[0]*Utilities::nintegrate(funcX,0,MIN(mo,1.0),1.0e-6)/PI; Elliptic::DALPHAYDM funcY(lambda,a2,b2,xtmp,isohalo); - alpha[1] = -8*a*b*xtmp[1]*Utilities::nintegrate(funcY,0,MIN(mo,1.0),1.0e-6)/pi; + alpha[1] = -8*a*b*xtmp[1]*Utilities::nintegrate(funcY,0,MIN(mo,1.0),1.0e-6)/PI; //std::cout << "alpha = " << alpha[0] << " " << alpha[1] << std::endl; diff --git a/AnalyticNSIE/jaffe_lens.cpp b/AnalyticNSIE/jaffe_lens.cpp index 5374a83a..d511cc86 100644 --- a/AnalyticNSIE/jaffe_lens.cpp +++ b/AnalyticNSIE/jaffe_lens.cpp @@ -7,7 +7,7 @@ PosType LensHaloJaffe::gfunction(PosType x) const{ PosType ans; - ans=pi; + ans=PI; if(x==0) x=1e-5; if(x==1.0) return (ans-2.)*x; if(x<1.0){ ans -= 2.*x*acosh(1./x)/sqrt(1.-x*x) ; return ans*x;} @@ -18,7 +18,7 @@ PosType LensHaloJaffe::gfunction(PosType x) const{ PosType LensHaloJaffe::ffunction(PosType x) const{ PosType ans; if(x==0) x=1e-5; - ans=pi/x; + ans=PI/x; if(x==1.0){ return ans-(2.+2./3.);} if(x>1.0){ ans += 2./(1.-x*x)*(1.-(2.-x*x)*acos(1./x)/sqrt(x*x-1.)); return ans;} if(x<1.0){ ans += 2./(1.-x*x)*(1.-(2.-x*x)*acosh(1./x)/sqrt(1.-x*x)); return ans;} @@ -29,7 +29,7 @@ PosType LensHaloJaffe::ffunction(PosType x) const{ PosType LensHaloJaffe::g2function(PosType x) const{ PosType ans; if(x==0) x=1e-5; - ans=pi/x; + ans=PI/x; if(x==1.0){ ans -=4./3. ; return ans*x/3.;} if(x>1.0){ ans += 2./x/x*(-1.0*(2.*x*x*acos(1./x))/sqrt(x*x-1.)+(sqrt(1.-1./x)*sqrt(1.+1./x)*x*(log(x-1.0)+log(1.+x)))/sqrt(x*x-1.)-log(x*x-1.))-(2./(1.-x*x)*(1.-(2.-x*x)*acos(1./x)/sqrt(x*x-1.))); return ans*x/3.;} if(x<1.0){ ans += 2./x/x*(-(2.*x*x*acosh(1./x))/sqrt(1.-x*x)+(sqrt(-1.+1./x)*sqrt(1.+1./x)*x*(log(1.-x)+log(1.+x)))/sqrt(1.-x*x)-log(1.-x*x))-(2./(1.-x*x)*(1.-(2.-x*x)*acosh(1./x)/sqrt(1.-x*x))); return ans*x/3.;} @@ -50,8 +50,8 @@ PosType LensHaloJaffe::bfunction(PosType fx){ if(x==0) x=1e-5; if(x==1){return -2.1231*fac;} // (x/ffunction(x))*(2.+2./15.) PosType aux=sqrt(1.-x*x); - if(x<1){ans=x*((-(pi/x/x)+(2.*(((2.-x*x))/(sqrt(-1+1./x)*sqrt(1+1./x) *x*x* aux)+(2*x*acosh(1./x))/aux-(x*(2.-x*x)*acosh(1./x))/pow(aux,3)))/(1.-x*x)+(4.*x*(1.-((2.-x*x)*acosh(1./x))/aux))/(1.-x*x)/(1.-x*x))/(pi/x+(2.*(1.-((2.-x*x)*acosh(1./x))/aux))/(1.-x*x))); return fac*ans;} - if(x>1){ans=(x*(-1.0*(pi/x/x)+(2.*(-(((2.-x*x))/(sqrt(1-1./x/x)*x*x*sqrt(-1.+x*x)))+(x*(2.-x*x)*acos(1./x))/(pow(-1.+x*x,3./2.))+(2.*x*acos(1./x))/sqrt(-1.+x*x)))/(1.-x*x)+(4.*x*(1.-((2.-x*x)*acos(1./x))/sqrt(-1.+x*x)))/(1.-x*x)/(1.-x*x)))/(pi/x+(2.*(1.-((2.-x*x)*acos(1./x))/sqrt(-1.+x*x)))/(1.-x*x)); return fac*ans;} + if(x<1){ans=x*((-(PI/x/x)+(2.*(((2.-x*x))/(sqrt(-1+1./x)*sqrt(1+1./x) *x*x* aux)+(2*x*acosh(1./x))/aux-(x*(2.-x*x)*acosh(1./x))/pow(aux,3)))/(1.-x*x)+(4.*x*(1.-((2.-x*x)*acosh(1./x))/aux))/(1.-x*x)/(1.-x*x))/(PI/x+(2.*(1.-((2.-x*x)*acosh(1./x))/aux))/(1.-x*x))); return fac*ans;} + if(x>1){ans=(x*(-1.0*(PI/x/x)+(2.*(-(((2.-x*x))/(sqrt(1-1./x/x)*x*x*sqrt(-1.+x*x)))+(x*(2.-x*x)*acos(1./x))/(pow(-1.+x*x,3./2.))+(2.*x*acos(1./x))/sqrt(-1.+x*x)))/(1.-x*x)+(4.*x*(1.-((2.-x*x)*acos(1./x))/sqrt(-1.+x*x)))/(1.-x*x)/(1.-x*x)))/(PI/x+(2.*(1.-((2.-x*x)*acos(1./x))/sqrt(-1.+x*x)))/(1.-x*x)); return fac*ans;} //if(x>1.0){ ans =(x/ffunction(x))*((4.-6.*x*x+2.*pow(x,4)+2.*aux/x*pow(x,5)*acos(1./x))/(aux*x*(1.-x*x)*pow(aux,3))+(4.*x*(1.+((-2.+x*x)*acos(1/x))/aux))/(1.-x*x)/(1.-x*x)) ; return fac*ans;} // if(x<1.0){ ans = (x/ffunction(x))*(((4.-6.*x*x+2.*pow(x,4)-2.*sqrt(-1+1./x)*pow(x,5)*sqrt((1.+x)/x)*acosh(1./x))/(sqrt(-1+1./x)*x*x*sqrt((1.+x)/x)*sqrt(1.-x*x))+4.*x*(1.+((-2.+x*x)*acosh(1./x))/sqrt(1. -x*x)))/(1. -x*x)/(1. -x*x)) // ; return fac*ans;} diff --git a/AnalyticNSIE/nfw_lens.cpp b/AnalyticNSIE/nfw_lens.cpp index c009dd7d..e6f97ff9 100644 --- a/AnalyticNSIE/nfw_lens.cpp +++ b/AnalyticNSIE/nfw_lens.cpp @@ -15,7 +15,7 @@ void LensHaloNFW::alphaNFW(PosType *alpha,PosType *x,PosType Rtrunc,PosType mass alpha[0]=alpha[1]=0.0; return ; } - b=mass/pow(r,2)/pi/Sigma_crit; + b=mass/pow(r,2)/PI/Sigma_crit; if(r < Rtrunc){ PosType y; @@ -50,7 +50,7 @@ KappaType LensHaloNFW::kappaNFW(PosType *x,PosType Rtrunc,PosType mass,PosType r y = r/r_scale; b*= ffunction(y); - return b*mass/(2*pi*pow(r_scale,2)*Sigma_crit); + return b*mass/(2*PI*pow(r_scale,2)*Sigma_crit); } /// Shear for and NFW halo. this might have a flaw in it @@ -64,7 +64,7 @@ void LensHaloNFW::gammaNFW(KappaType *gamma,PosType *x,PosType Rtrunc,PosType ma return ; } - gt=mass/pi/Sigma_crit/pow(r,2); + gt=mass/PI/Sigma_crit/pow(r,2); if(rR) return 0.0; if(r < 1.0-20) r=1.0e-20; - return (beta+2)*mass*pow(r/R,beta)/(2*pi*pow(R,2)*Sigma_crit); + return (beta+2)*mass*pow(r/R,beta)/(2*PI*pow(R,2)*Sigma_crit); } /// void gammaPowLaw(KappaType *gamma,PosType *x,PosType R,PosType mass,PosType beta @@ -48,7 +48,7 @@ void gammaPowLaw(KappaType *gamma,PosType *x,PosType R,PosType mass,PosType beta gamma[0]=gamma[1]=0.0; return ; } - gt=mass/pi/Sigma_crit/pow(r,2); + gt=mass/PI/Sigma_crit/pow(r,2); if(r 0){ for(n=3;n<6;++n) AlignedRandomlyDistortLens(seed - ,pa+3*pi*gasdev(seed)/180,n); + ,pa+3*PI*gasdev(seed)/180,n); } delete[] axisTable; @@ -110,7 +110,7 @@ void LensHaloAnaNSIE::RandomlyDistortLens(long *seed, int Nmodes){ // lognormal shear and kappa distribution perturb_modes[0]=0.015*pow(10,gasdev(seed)*perturb_rms[0]); tmp=0.015*pow(10,gasdev(seed)*perturb_rms[1]); - theta=2*pi*ran2(seed); + theta=2*PI*ran2(seed); perturb_modes[1] = tmp*cos(theta); perturb_modes[2] = tmp*sin(theta); @@ -202,12 +202,12 @@ void LensHaloAnaNSIE::RandomizeSubstructure2(PosType rangeInRei,long *seed){ if(Einstein_ro > 0.0){ Rm = Einstein_ro*rangeInRei + sub_Rsize*pow(scale,1./3.) - + pow(2*sub_Mmax*scale*Einstein_ro/pi/Sigma_crit/sheartol,1./3.); + + pow(2*sub_Mmax*scale*Einstein_ro/PI/Sigma_crit/sheartol,1./3.); Einstein_ro_save = Einstein_ro; } if(!(substruct_implanted) && ndensity > 0){ - NsubMax=(unsigned long)(ndensity*pi*Rm*Rm + 5*sqrt(ndensity*pi*Rm*Rm) ); + NsubMax=(unsigned long)(ndensity*PI*Rm*Rm + 5*sqrt(ndensity*PI*Rm*Rm) ); if(NsubMax > 0){ sub_x=Utilities::PosTypeMatrix(NsubMax,2); switch(main_sub_type){ @@ -229,9 +229,9 @@ void LensHaloAnaNSIE::RandomizeSubstructure2(PosType rangeInRei,long *seed){ substruct_implanted=true; } //std::cout << "Rm/re = %e\n",Rm/Einstein_ro); - //for(i=0;i<12;++i) std::cout << "%f %f\n",poidev(ndensity*pi*Rm*Rm,seed),ndensity*pi*Rm*Rm); + //for(i=0;i<12;++i) std::cout << "%f %f\n",poidev(ndensity*PI*Rm*Rm,seed),ndensity*PI*Rm*Rm); - unsigned int Nsub=(int)(poidev(ndensity*pi*Rm*Rm,seed)); + unsigned int Nsub=(int)(poidev(ndensity*PI*Rm*Rm,seed)); Nsub = (NsubMax > Nsub) ? Nsub : NsubMax ; //std::cout << "scale = %e\n",scale); @@ -262,12 +262,12 @@ void LensHaloAnaNSIE::RandomizeSubstructure2(PosType rangeInRei,long *seed){ // maximum radius for a substructure of this mass rmax = (Einstein_ro_save*rangeInRei + subs[k].getRsize() - + pow(2*subs[k].get_mass()*Einstein_ro_save/pi/Sigma_crit/sheartol,1./3.) ); + + pow(2*subs[k].get_mass()*Einstein_ro_save/PI/Sigma_crit/sheartol,1./3.) ); //std::cout << "RcutSubstruct[%i] = %e\n",k,RcutSubstruct[k]); //std::cout << "%e %e %e Rm=%e\n",r/rmax,r,rmax,Rm); if( r < rmax){ - theta=2*pi*ran2(seed); + theta=2*PI*ran2(seed); sub_x[k][0]=r*cos(theta); sub_x[k][1]=r*sin(theta); assert(k 0.0); if(!substruct_implanted){ - NsubMax=(unsigned long)(sub_Ndensity*pi*Rm*Rm*(1+5/sqrt(sub_Ndensity*pi*Rm*Rm)) ); + NsubMax=(unsigned long)(sub_Ndensity*PI*Rm*Rm*(1+5/sqrt(sub_Ndensity*PI*Rm*Rm)) ); sub_x=Utilities::PosTypeMatrix(NsubMax,2); switch(main_sub_type){ case pointmass: @@ -359,9 +359,9 @@ void LensHaloAnaNSIE::RandomizeSubstructure3(PosType rangeInRei,long *seed){ sub_substructures = new IndexType[NsubMax]; } //std::cout << "Rm/re = %e\n",Rm/Einstein_ro); - //for(i=0;i<12;++i) std::cout << "%f %f\n",poidev(ndensity*pi*Rm*Rm,seed),ndensity*pi*Rm*Rm); + //for(i=0;i<12;++i) std::cout << "%f %f\n",poidev(ndensity*PI*Rm*Rm,seed),ndensity*PI*Rm*Rm); - unsigned int Nsub=(int)(poidev(sub_Ndensity*pi*Rm*Rm,seed)); + unsigned int Nsub=(int)(poidev(sub_Ndensity*PI*Rm*Rm,seed)); assert(Nsub < NsubMax); @@ -388,12 +388,12 @@ void LensHaloAnaNSIE::RandomizeSubstructure3(PosType rangeInRei,long *seed){ // maximum radius for a substructure of this mass rmax = (Einstein_ro_save*rangeInRei + subs[k].getRsize() - + pow(2*subs[k].get_mass()*Einstein_ro_save/pi/Sigma_crit/sheartol,1./3.) ); + + pow(2*subs[k].get_mass()*Einstein_ro_save/PI/Sigma_crit/sheartol,1./3.) ); //std::cout << "RcutSubstruct[%i] = %e\n",k,RcutSubstruct[k]); //std::cout << "%e %e %e Rm=%e\n",r/rmax,r,rmax,Rm); if( r < rmax){ - theta=2*pi*ran2(seed); + theta=2*PI*ran2(seed); sub_x[k][0]=r*cos(theta); sub_x[k][1]=r*sin(theta); assert(kpHDU(); h0.readKey("BETA", source_r); - source_r *= 0.03/180./60./60.*pi; + source_r *= 0.03/180./60./60.*PI; h0.readKey("DIM", n1); h0.readKey("ID", id); n2 = n1; @@ -621,7 +621,7 @@ SourceShapelets::SourceShapelets( CCfits::PHDU& h0 = fp->pHDU(); h0.readKey("BETA", source_r); - source_r *= 0.03/180./60./60.*pi; + source_r *= 0.03/180./60./60.*PI; h0.readKey("SED_TYPE",sed_type); h0.readKey("MAG_B",mag_map[F435W]); // ACS F435W band magnitude @@ -676,7 +676,7 @@ PosType SourceShapelets::SurfaceBrightness(PosType *y) for (int i = 0; i < n1; i++,coei *= 2,coej=1 ) { - tmp = factrl(i)*pi; + tmp = factrl(i)*PI; for (int j = 0; j < n2; j++,coej *= 2 ) { @@ -727,7 +727,7 @@ void SourceShapelets::NormalizeFlux() coeff_flux += pow(2,0.5*(2-i-j))*sqrt(factrl(i))/factrl(i/2.)*sqrt(factrl(j))/factrl(j/2.)*coeff[j*n1+i]; } } - coeff_flux *= sqrt(pi)*source_r; + coeff_flux *= sqrt(PI)*source_r; } /// Default constructor. Reads in sources from the default catalog. No magnitude limit. @@ -781,7 +781,7 @@ void SourceMultiShapelets::assignParams(InputParams& params){ if(!params.get("source_sb_limit",sb_limit)) setSBlimit_magarcsec(30.); else - sb_limit = pow(10,-0.4*(48.6+sb_limit))*pow(180*60*60/pi,2)/hplanck; + sb_limit = pow(10,-0.4*(48.6+sb_limit))*pow(180*60*60/PI,2)/hplanck; if(!params.get("shapelets_folder",shapelets_folder)){ std::cout << "ERROR: shapelets_folder not found in parameter file " << params.filename() << std::endl; diff --git a/AnalyticNSIE/sourceAnaGalaxy.cpp b/AnalyticNSIE/sourceAnaGalaxy.cpp index 2133c374..2896b0c2 100644 --- a/AnalyticNSIE/sourceAnaGalaxy.cpp +++ b/AnalyticNSIE/sourceAnaGalaxy.cpp @@ -233,10 +233,10 @@ void SourceMultiAnaGalaxy::readDataFileMillenn(Utilities::RandomNumbers_NR &ran) */ // converting from Millennium conventions - theta[0] = -ra*pi/180; - theta[1] = dec*pi/180; - pa = (90 - pa)*pi/180; - inclination *= pi/180; + theta[0] = -ra*PI/180; + theta[1] = dec*PI/180; + pa = (90 - pa)*PI/180; + inclination *= PI/180; if(cos(inclination)< 0.5) inclination = acos(0.5); //std::cout << "did inclination" << std::endl; @@ -333,7 +333,7 @@ void SourceMultiAnaGalaxy::assignParams(InputParams& params){ if(!params.get("source_sb_limit",sb_limit)) setSBlimit_magarcsec(30.); else - sb_limit = pow(10,-0.4*(48.6+sb_limit))*pow(180*60*60/pi,2)/hplanck; + sb_limit = pow(10,-0.4*(48.6+sb_limit))*pow(180*60*60/PI,2)/hplanck; /*{ std::cout << "ERROR: Must assign source_sb_limit in parameter file " << params.filename() << std::endl; exit(1);*/ @@ -371,7 +371,7 @@ void SourceMultiAnaGalaxy::multiplier( theta[1] = x1[1] + (x2[1] - x1[1])*ran(); galaxies.push_back(SourceOverzierPlus(galaxies[i].getMag(),galaxies[i].getMagBulge() - ,galaxies[i].getReff(),galaxies[i].getRh(),ran()*pi,ran()*2*pi + ,galaxies[i].getReff(),galaxies[i].getRh(),ran()*PI,ran()*2*PI ,Nold+NtoAdd,galaxies[i].getZ(),theta,ran)); ++NtoAdd; diff --git a/BLR/blr_surface_brightness.cpp b/BLR/blr_surface_brightness.cpp index d3cb7fe2..37814616 100644 --- a/BLR/blr_surface_brightness.cpp +++ b/BLR/blr_surface_brightness.cpp @@ -105,7 +105,7 @@ double blr_surface_brightness_disk_old(double x[],SourceBLR *source){ float vr = sqrt(4.7788e-20*source->source_BHmass/R); float v_shift_yprime; // in units of the speed of light - if(source->source_opening_angle < 0.9999*pi/2){ + if(source->source_opening_angle < 0.9999*PI/2){ //double v_shift_xprime,v_shift_yprime,v_shift_zprime; //v_shift_xprime = - vr * yy_prime/sqrt(xx_prime*xx_prime + yy_prime*yy_prime); diff --git a/BLR/blr_surface_brightness2.0.cpp b/BLR/blr_surface_brightness2.0.cpp index 37027bf2..d98ce1df 100644 --- a/BLR/blr_surface_brightness2.0.cpp +++ b/BLR/blr_surface_brightness2.0.cpp @@ -78,7 +78,7 @@ double blr_surface_brightness_spherical_circular_motions(double x,SourceBLR *sou if ( fabs(source->source_nu*(1+source->getZ()) - source->source_nuo) > nu_m ) return 0.0; - eta = source->source_nuo *(1+source->getZ())/sqrt( 1. - pow( (source->source_nu*(1+source->getZ()) - source->source_nuo)/nu_m ,2) )/nu_m/pi; + eta = source->source_nuo *(1+source->getZ())/sqrt( 1. - pow( (source->source_nu*(1+source->getZ()) - source->source_nuo)/nu_m ,2) )/nu_m/PI; //printf("tau = %e eta =%e r=%e xi=%e\n",tau,eta,r,pow(r/source->source_r_in,source->source_gamma)); return pow(r/source->source_r_in,source->source_gamma)*eta*r/tau; @@ -143,7 +143,7 @@ double blr_surface_brightness_disk(double x[],SourceBLR *source){ float v_Kep = sqrt(4.7788e-20*source->source_BHmass/r); float v_shift_yprime; // in units of the speed of light - // if(source->source_opening_angle < 0.9999*pi/2){ + // if(source->source_opening_angle < 0.9999*PI/2){ // BEN -- I'VE REMOVED THIS CONDITION SINCE I THINK THE LAST FEW LINES (THE ALTERNATIVE) MIGHT BE REDUNDANT NOW v_shift_yprime = v_Kep * xx_prime/r; diff --git a/Fitlens/fitlens.cpp b/Fitlens/fitlens.cpp index 1e777297..90456e2a 100644 --- a/Fitlens/fitlens.cpp +++ b/Fitlens/fitlens.cpp @@ -213,8 +213,8 @@ bool LensHaloFit::SafeFindLensSimple( alphaTMP[1] *= -1. ; // Applying the extra factors of RayShooter : - alphaTMP[0] *= -4*pi*Grav ; - alphaTMP[1] *= -4*pi*Grav ; + alphaTMP[0] *= -4*PI*Grav ; + alphaTMP[1] *= -4*PI*Grav ; alphaTMP[0] *= Dls / Ds ; alphaTMP[1] *= Dls / Ds ; @@ -408,7 +408,7 @@ void LensHaloFit::FindLensSimple( assert(Dl*(1+zl) + Dls*(1+zs) - Ds*(1+zs) == 0.); // std::cout << "Dl (1+zl) + Dls (1+zs) = " << Dl*(1+zl) + Dls*(1+zs) << " , Ds (1+zs) = " << Ds*(1+zs) << std::endl ; - for(i=0;i3) modoT[4] =4*( (1+q*q)*K-2*q*q*E )/(1-q*q)/pi/(1-4); + if(NmodT>3) modoT[4] =4*( (1+q*q)*K-2*q*q*E )/(1-q*q)/PI/(1-4); if(NmodT>7) modoT[8] =4*( (3*q*q+1)*(q*q+3)*K-8*q*q*(1+q*q)*E ) - /( 3*pi*pow(1-q*q,2) )/(1-16); + /( 3*PI*pow(1-q*q,2) )/(1-16); if(NmodT>11) modoT[12] =4*( (1+q*q)*(15+98*q*q+15*q*q*q*q)*K-2*q*q*(23+82*q*q+23*q*q*q*q)*E ) - /( 15*pi*pow(1-q*q,3) )/(1-36); + /( 15*PI*pow(1-q*q,3) )/(1-36); if(NmodT>15) modoT[16]=4*( -32*q*q*(1+q*q)*(11+74*q*q+11*q*q*q*q)*E +(105+1436*q*q+3062*q*q*q*q+1436*pow(q,6)+105*pow(q,8))*K ) - /(105*pi*pow(1-q*q,4))/(1-64); + /(105*PI*pow(1-q*q,4))/(1-64); } // rotate model @@ -720,13 +720,13 @@ double minEllip(double *par){ oldsm=sm; for(i=1;i<=NmodT+2*NsourcesT+1;++i) modTT[i]=modT[i]; } - //std::printf("q=%e theta=%e gamma=%e %e\n",q,theta*180/pi,modT[1],modT[2]); + //std::printf("q=%e theta=%e gamma=%e %e\n",q,theta*180/PI,modT[1],modT[2]); //std::printf("%e %e %e %e %e %e\n",modoT[3],modoT[4],modoT[5],modoT[6],modoT[7],modoT[8]); //std::printf("%e %e %e %e %e %e\n\n",modT[3],modT[4],modT[5],modT[6],modT[7],modT[8]); // keep orientation within range - if(theta > pi/2) sm *= exp( (theta - pi/2)/1.0e-5 ); - if(theta < -pi/2 ) sm *= exp( -(theta - pi/2)/1.0e-5 ); + if(theta > PI/2) sm *= exp( (theta - PI/2)/1.0e-5 ); + if(theta < -PI/2 ) sm *= exp( -(theta - PI/2)/1.0e-5 ); // keep center of lens within sigG if(Nmin == 4 || Nmin == 5){ r=pow(x_center[0]-x_centerT[0],2) + pow(x_center[1]-x_centerT[1],2); @@ -1165,7 +1165,7 @@ double LensHaloAnaNSIE::find_axis(double *mod,int Nmod){ NmodT=Nmod; for(i=1;i<=NmodT;++i) modT[i] = mod[i]; - theta = zriddrD(minaxis,0,pi/4.,1.0e-9); + theta = zriddrD(minaxis,0,PI/4.,1.0e-9); /* calc 2nd deriv */ for(i=4,ans=0.0;i<=5;i+=2){ @@ -1175,7 +1175,7 @@ double LensHaloAnaNSIE::find_axis(double *mod,int Nmod){ - 2*k*k*(modT[i]*modT[i] - modT[i+1]*modT[i+1])*cos(2*k*theta); } - if(ans < 0) theta += pi/4; /* make answer a minimum */ + if(ans < 0) theta += PI/4; /* make answer a minimum */ return theta; } diff --git a/FullRange/implant_stars.cpp b/FullRange/implant_stars.cpp index 99bf2ae7..5662cca2 100644 --- a/FullRange/implant_stars.cpp +++ b/FullRange/implant_stars.cpp @@ -100,7 +100,7 @@ void LensHalo::implant_stars( KappaType phi; force_halo(alpha,&Sigma,gamma,&phi,xcm,false); star_Sigma[j] = star_fstars*Sigma; - star_region[j] = 1.0/sqrt(pi*star_Sigma[j]/(mean_mstar[j]*(float)NstarsPerImage)); + star_region[j] = 1.0/sqrt(PI*star_Sigma[j]/(mean_mstar[j]*(float)NstarsPerImage)); star_xdisk[j][0] = centers[j][0]; star_xdisk[j][1] = centers[j][1]; @@ -125,7 +125,7 @@ void LensHalo::implant_stars( for(i=0;i void swap(std::valarray& x, std::valarray& y) { @@ -34,7 +34,8 @@ void swap(std::valarray& x, std::valarray& y) y.resize(z.size()); y = z; } -#endif +//#endif +*/ void swap(PixelMap& x, PixelMap& y) { @@ -224,7 +225,7 @@ PixelMap::PixelMap( my_res = ps/Nx; } } - resolution = fabs(my_res)*pi/180.; + resolution = fabs(my_res)*PI/180.; }else{ resolution = my_res; } @@ -885,13 +886,13 @@ void PixelMap::printFITS(std::string filename, bool verbose) const phout.addKey("CTYPE2", "DEC--TAN", "the coordinate type for the second axis"); phout.addKey("CUNIT1", "deg ", "the coordinate unit for the first axis"); phout.addKey("CUNIT2", "deg ", "the coordinate unit for the second axis"); - phout.addKey("CDELT1", -180*resolution/pi, "partial of first axis coordinate w.r.t. x"); - phout.addKey("CDELT2", 180*resolution/pi, "partial of second axis coordinate w.r.t. y"); + phout.addKey("CDELT1", -180*resolution/PI, "partial of first axis coordinate w.r.t. x"); + phout.addKey("CDELT2", 180*resolution/PI, "partial of second axis coordinate w.r.t. y"); phout.addKey("CROTA2", 0.0, ""); - phout.addKey("CD1_1", -180*resolution/pi, "partial of first axis coordinate w.r.t. x"); + phout.addKey("CD1_1", -180*resolution/PI, "partial of first axis coordinate w.r.t. x"); phout.addKey("CD1_2", 0.0, "partial of first axis coordinate w.r.t. y"); phout.addKey("CD2_1", 0.0, "partial of second axis coordinate w.r.t. x"); - phout.addKey("CD2_2", 180*resolution/pi, "partial of second axis coordinate w.r.t. y"); + phout.addKey("CD2_2", 180*resolution/PI, "partial of second axis coordinate w.r.t. y"); phout.addKey("Nx", Nx, ""); phout.addKey("Ny", Ny, ""); @@ -948,13 +949,13 @@ void PixelMap::printFITS(std::string filename,std::vectorzlens = zlens; map->center[0] = map->center[1] = 0.0; - map->boxlrad = map->boxlarcsec*pi/180/3600.; + map->boxlrad = map->boxlarcsec*PI/180/3600.; PreProcessFFTWMap(); @@ -240,7 +240,7 @@ void LensHaloMassMap::initMap() } map->center[0] = map->center[1] = 0.0; - map->boxlrad = map->boxlarcsec*pi/180/3600.; + map->boxlrad = map->boxlarcsec*PI/180/3600.; if(maptype == moka){ @@ -249,14 +249,14 @@ void LensHaloMassMap::initMap() /// converts to the code units std::cout << "converting the units of the MOKA map" << std::endl; - double fac = map->DS/map->DLS/map->Dlens*map->h/(4*pi*Grav); + double fac = map->DS/map->DLS/map->Dlens*map->h/(4*PI*Grav); map->convergence *= fac; map->gamma1 *= fac; map->gamma2 *= fac; map->phi *= fac; - fac = 1/(4*pi*Grav); + fac = 1/(4*PI*Grav); map->alpha1 *= fac; map->alpha2 *= fac; diff --git a/MultiPlane/lens.cpp b/MultiPlane/lens.cpp index 09b419b9..b501b8e5 100644 --- a/MultiPlane/lens.cpp +++ b/MultiPlane/lens.cpp @@ -110,7 +110,7 @@ Lens::Lens(InputParams& params, long* my_seed, CosmoParamSet cosmoset, bool verb read_sim_file = false; - charge = 4*pi*Grav; + charge = 4*PI*Grav; if(verbose) std::cout << "charge: " << charge << std::endl; // initially let source be the one inputed from parameter file @@ -141,7 +141,7 @@ Lens::Lens(InputParams& params, long* my_seed, CosmoParamSet cosmoset, bool verb } else { if(field_buffer == 0.0){ - field_buffer = pow(3.0e14/800/pi/cosmo.rho_crit_comoving(0),1.0/3.); + field_buffer = pow(3.0e14/800/PI/cosmo.rho_crit_comoving(0),1.0/3.); std::cout << " Resetting field buffer to " << field_buffer << " Mpc." << std::endl; } // Compute the distribution variables : @@ -182,7 +182,7 @@ Lens::Lens(InputParams& params, long* my_seed, const COSMOLOGY &cosmoset, bool v read_sim_file = false; - charge = 4*pi*Grav; + charge = 4*PI*Grav; if(verbose) std::cout << "charge: " << charge << std::endl; // initially let source be the one inputed from parameter file @@ -213,7 +213,7 @@ Lens::Lens(InputParams& params, long* my_seed, const COSMOLOGY &cosmoset, bool v } else { if(field_buffer == 0.0){ - field_buffer = pow(3.0e14/800/pi/cosmo.rho_crit_comoving(0),1.0/3.); + field_buffer = pow(3.0e14/800/PI/cosmo.rho_crit_comoving(0),1.0/3.); std::cout << " Resetting field buffer to " << field_buffer << " Mpc." << std::endl; } // Compute the distribution variables : @@ -525,7 +525,7 @@ void Lens::assignParams(InputParams& params,bool verbose) void Lens::defaultParams(PosType z_source,bool verbose) { - charge = 4*pi*Grav; + charge = 4*PI*Grav; read_sim_file = false; index_of_new_sourceplane = -1; @@ -841,7 +841,7 @@ void Lens::createFieldPlanes(bool verbose) PosType sigma_back; if(sigma_back_Tab.size() < field_Nplanes_original){ sigma_back = - cosmo.haloMassInBufferedCone(field_min_mass,z1,z2,fieldofview*pow(pi/180,2),field_buffer,field_mass_func_type,mass_func_PL_slope)/(pi*pow(sqrt(fieldofview/pi)*pi*field_Dl[i]/180/(1+field_plane_redshifts[i]) + field_buffer,2)); + cosmo.haloMassInBufferedCone(field_min_mass,z1,z2,fieldofview*pow(PI/180,2),field_buffer,field_mass_func_type,mass_func_PL_slope)/(PI*pow(sqrt(fieldofview/PI)*PI*field_Dl[i]/180/(1+field_plane_redshifts[i]) + field_buffer,2)); sigma_back_Tab.push_back(sigma_back); }else{ sigma_back = sigma_back_Tab[i]; @@ -862,7 +862,7 @@ void Lens::createFieldPlanes(bool verbose) //max_r=sqrt(max_r); - sb /= (pi*pow(sqrt(fieldofview/pi)*pi*field_Dl[i]/180/(1+field_plane_redshifts[i]) + field_buffer,2)); + sb /= (PI*pow(sqrt(fieldofview/PI)*PI*field_Dl[i]/180/(1+field_plane_redshifts[i]) + field_buffer,2)); assert(sb == sb); @@ -922,7 +922,7 @@ void Lens::insertSubstructures(PosType Rregion, // in radians if(alpha == -1) throw std::invalid_argument("alpha must not be -1 in Lens::createOneFieldPlane"); // non-projected number of halos : - PosType aveNhalos = NumberDensity * pi*Rregion*Rregion; // in Number/radians^2 * radians^2 = dimensionless + PosType aveNhalos = NumberDensity * PI*Rregion*Rregion; // in Number/radians^2 * radians^2 = dimensionless if(verbose) std::cout << "Lens::insertSubstructures : Average number of Substructures : " << aveNhalos << std::endl; // So numberDensity refers to the number density in 3D. @@ -967,7 +967,7 @@ void Lens::insertSubstructures(PosType Rregion, // in radians rr = Rregion*sqrt(ran2(seed)); // in radians theta_pos = new PosType[3]; - theta = 2*pi*ran2(seed); // in radians + theta = 2*PI*ran2(seed); // in radians // position in proper distance theta_pos[0] = (rr*cos(theta) + center[0]); // in radians * angular Distance in PhysMpc = PhysMpc @@ -988,9 +988,9 @@ void Lens::insertSubstructures(PosType Rregion, // in radians mass_max = MAX(mass,mass_max); // in Msun // Rsize from tidal truncation - Rsize = pow(mass/rho/4/pi,1.0/3.); // in [Msun / (Msun / PhysMpc^3)]^(1/3) = PhysMpc + Rsize = pow(mass/rho/4/PI,1.0/3.); // in [Msun / (Msun / PhysMpc^3)]^(1/3) = PhysMpc - // keeping track of the highest substructure rmax : + // keePIng track of the highest substructure rmax : rmax_max = MAX(Rsize,rmax_max); // in PhysMpc // Adding the randomly-generated halo into the substructure : @@ -1010,7 +1010,7 @@ void Lens::insertSubstructures(PosType Rregion, // in radians << rmax_max << " PhysMpc, Number of substructure halos = " << substructure.halos.size() << std::endl ; std::cout << "Lens::insertSubstructures : SumMassSub = " << SumMassSub << " Msun, Theoretical total averaged mass = " << AveMassTh << " Msun." << std::endl ; std::cout << "Lens::insertSubstructures : Sigma_crit = " << cosmo.SigmaCrit(redshift, zsource) << " Msun/PhysMpc^2." << std::endl ; - std::cout << "Lens::insertSubstructures : 4 pi G = " << 4*pi*Grav << " Mpc/Msun." << std::endl ; + std::cout << "Lens::insertSubstructures : 4 pi G = " << 4*PI*Grav << " Mpc/Msun." << std::endl ; } // Test : @@ -1132,7 +1132,7 @@ void Lens::resetSubstructure(bool verbose){ PosType redshift = field_plane_redshifts[fplane_index]; PosType Dlsub = Dl[fplane_index]/(1+redshift); - PosType aveNhalos = substructure.Ndensity*substructure.Rregion*substructure.Rregion*pi; + PosType aveNhalos = substructure.Ndensity*substructure.Rregion*substructure.Rregion*PI; if(verbose) std::cout << "Lens::resetSubstructures : Average number of Substructures : " << aveNhalos << std::endl; @@ -1167,7 +1167,7 @@ void Lens::resetSubstructure(bool verbose){ rr = substructure.Rregion*sqrt(ran2(seed)); theta_pos = new PosType[3]; - theta = 2*pi*ran2(seed); + theta = 2*PI*ran2(seed); // position in proper distance theta_pos[0] = (rr*cos(theta) + substructure.center[0]);//*Dlsub; @@ -1182,7 +1182,7 @@ void Lens::resetSubstructure(bool verbose){ mass_max = MAX(mass,mass_max); // Rsize from tidal truncation - Rsize = pow(mass/rho/4/pi,1.0/3.); + Rsize = pow(mass/rho/4/PI,1.0/3.); rmax_max = MAX(Rsize,rmax_max); @@ -1579,7 +1579,7 @@ void Lens::ComputeHalosDistributionVariables () { const PosType MaxLogm=16.; - aveNhalosField = cosmo.haloNumberInBufferedCone(field_min_mass,0,zsource,fieldofview*pow(pi/180,2),field_buffer,field_mass_func_type,mass_func_PL_slope); + aveNhalosField = cosmo.haloNumberInBufferedCone(field_min_mass,0,zsource,fieldofview*pow(PI/180,2),field_buffer,field_mass_func_type,mass_func_PL_slope); Utilities::fill_linear(zbins,Nzbins,0.0,zsource); // construct redshift distribution table @@ -1587,7 +1587,7 @@ void Lens::ComputeHalosDistributionVariables () zbins[0] = 0; for(int k=1;kinitFromMassFunc(mass*field_galaxy_mass_fraction,Rsize,rscale,field_prof_internal_slope,seed); @@ -1971,10 +1971,10 @@ void Lens::readInputSimFileMillennium(bool verbose,DM_Light_Division division_mo if(np == 0) continue; //PosType Ds = cosmo->angDist(0,z); - //theta[0] = -ra*pi/180.; - //theta[1] = dec*pi/180.; - tmp_sph_point.theta *= pi/180; - tmp_sph_point.phi *= -pi/180; + //theta[0] = -ra*PI/180.; + //theta[1] = dec*PI/180.; + tmp_sph_point.theta *= PI/180; + tmp_sph_point.phi *= -PI/180; rtmp = Utilities::Geometry::AngleSeporation(central_point_sphere,tmp_sph_point); if(sim_angular_radius > 0 && rtmp > sim_angular_radius) continue; @@ -2088,7 +2088,7 @@ void Lens::readInputSimFileMillennium(bool verbose,DM_Light_Division division_mo //field_galaxy_mass_fraction *= 2; float fratio = (ran2(seed)+1)*0.5; //TODO: Ben change this! This is a kluge. - float pa = 2*pi*ran2(seed); //TODO: This is a kluge. + float pa = 2*PI*ran2(seed); //TODO: This is a kluge. switch(field_int_prof_gal_type){ case null_gal: @@ -2102,9 +2102,9 @@ void Lens::readInputSimFileMillennium(bool verbose,DM_Light_Division division_mo case pl_gal: assert(field_int_prof_gal_slope>0); - field_halos.push_back(new LensHaloPowerLaw(mass*field_galaxy_mass_fraction,rmaxNSIE(sigma, mass*field_galaxy_mass_fraction, fratio, 0.0),z,field_int_prof_gal_slope,fratio,pa+pi/2.,0,Fourier)); - //field_halos.push_back(new LensHaloPowerLaw(mass*field_galaxy_mass_fraction,r_half_stel_mass/1.6*2.5,z,field_int_prof_gal_slope,0.99,pa+pi/2.,0,Fourier)); // explanation for r_half_stel_mass/1.34: relation between r_half_stel_mass and effective radius according to Kravtsev 2013 used! - //field_halos.push_back(new LensHaloPowerLaw(mass*field_galaxy_mass_fraction,mass*Grav*lightspeed*lightspeed*sqrt(fratio)/pi/sigma/sigma,z,field_int_prof_gal_slope,fratio,pa,0,Fourier)); + field_halos.push_back(new LensHaloPowerLaw(mass*field_galaxy_mass_fraction,rmaxNSIE(sigma, mass*field_galaxy_mass_fraction, fratio, 0.0),z,field_int_prof_gal_slope,fratio,pa+PI/2.,0,Fourier)); + //field_halos.push_back(new LensHaloPowerLaw(mass*field_galaxy_mass_fraction,r_half_stel_mass/1.6*2.5,z,field_int_prof_gal_slope,0.99,pa+PI/2.,0,Fourier)); // explanation for r_half_stel_mass/1.34: relation between r_half_stel_mass and effective radius according to Kravtsev 2013 used! + //field_halos.push_back(new LensHaloPowerLaw(mass*field_galaxy_mass_fraction,mass*Grav*lightspeed*lightspeed*sqrt(fratio)/PI/sigma/sigma,z,field_int_prof_gal_slope,fratio,pa,0,Fourier)); //std::cout << "PL "< masslimit && z <= zsource){ - tmp_sph_point.theta *= pi/180; - tmp_sph_point.phi *= -pi/180; + tmp_sph_point.theta *= PI/180; + tmp_sph_point.phi *= -PI/180; rtmp = Utilities::Geometry::AngleSeporation(central_point_sphere,tmp_sph_point); @@ -2330,8 +2330,8 @@ void Lens::readInputSimFileMultiDarkHalos(bool verbose,DM_Light_Division divisio boundary_diagonal[0] = boundary_diagonal[1] = theta[0]+theta[1]; } - //theta[0] = -ra*pi/180.; - //theta[1] = dec*pi/180.; + //theta[0] = -ra*PI/180.; + //theta[1] = dec*PI/180.; center[0] += tmp_sph_point.theta; center[1] += tmp_sph_point.phi; @@ -2428,7 +2428,7 @@ void Lens::readInputSimFileMultiDarkHalos(bool verbose,DM_Light_Division divisio float sigma = 126*pow(mass*field_galaxy_mass_fraction/1.0e10,0.25); // From Tully-Fisher and Bell & de Jong 2001 //std::cout << "Warning: All galaxies are spherical" << std::endl; float fratio = (ran2(seed)+1)*0.5; //TODO: Ben change this! This is a kluge. - float pa = 2*pi*ran2(seed); //TODO: This is a kluge. + float pa = 2*PI*ran2(seed); //TODO: This is a kluge. switch(field_int_prof_gal_type){ case null_gal: @@ -2495,19 +2495,19 @@ void Lens::readInputSimFileMultiDarkHalos(bool verbose,DM_Light_Division divisio /*if(diagonal1 < diagonal2*0.9){ // circular region rmax = diagonal1/2; - fieldofview = pi*rmax*rmax*pow(180/pi,2); // Resets field of view to range of input galaxies + fieldofview = PI*rmax*rmax*pow(180/PI,2); // Resets field of view to range of input galaxies inv_ang_screening_scale = 0.0; }else{ - fieldofview = (boundary_p2[0] - boundary_p1[0])*(boundary_p2[1] - boundary_p1[1])*pow(180/pi,2); + fieldofview = (boundary_p2[0] - boundary_p1[0])*(boundary_p2[1] - boundary_p1[1])*pow(180/PI,2); rmax = diagonal2/2; inv_ang_screening_scale = 5.0/(MIN(boundary_p2[0] - boundary_p1[0],boundary_p2[1] - boundary_p1[1])); }*/ if(sim_angular_radius == 0.0){ - fieldofview = (boundary_p2[0] - boundary_p1[0])*(boundary_p2[1] - boundary_p1[1])*pow(180/pi,2); + fieldofview = (boundary_p2[0] - boundary_p1[0])*(boundary_p2[1] - boundary_p1[1])*pow(180/PI,2); inv_ang_screening_scale = 5.0/(MIN(boundary_p2[0] - boundary_p1[0],boundary_p2[1] - boundary_p1[1])); }else{ - fieldofview = pi*rmax*rmax*pow(180/pi,2); + fieldofview = PI*rmax*rmax*pow(180/PI,2); inv_ang_screening_scale = 0.0; } @@ -2625,8 +2625,8 @@ void Lens::readInputSimFileObservedGalaxies(bool verbose) for(int l=0;l(funcer,0.0,pi/2,1.0e-6)*2/pi; + Fofq_table[0] = Utilities::nintegrate(funcer,0.0,PI/2,1.0e-6)*2/PI; for(int i=1 ; i < N-1 ; ++i){ q_table[i] = i*1.0/(N-1) + 0.; NormFuncer funcer(q_table[i]); - Fofq_table[i] = Utilities::nintegrate(funcer,0.0,pi/2,1.0e-6)*2/pi; + Fofq_table[i] = Utilities::nintegrate(funcer,0.0,PI/2,1.0e-6)*2/PI; } q_table.back() = 1.0; Fofq_table.back() = 1.0; @@ -825,7 +825,7 @@ PosType LensHaloRealNSIE::rmax_calc(){ if(fratio == 1.0 || rcore > 0.0) return sqrt( pow( LensHalo::get_mass()*Grav*lightspeed*lightspeed - *sqrt(fratio)/pi/sigma/sigma + rcore,2) + *sqrt(fratio)/PI/sigma/sigma + rcore,2) - rcore*rcore ); // This is because there is no easy way of finding the circular Rmax for a fixed mass when rcore != 0 @@ -833,11 +833,11 @@ PosType LensHaloRealNSIE::rmax_calc(){ // asymmetric case //NormFuncer funcer(fratio); - //double ellipticint = Utilities::nintegrate(funcer,0.0,pi/2,1.0e-6)*2/pi; + //double ellipticint = Utilities::nintegrate(funcer,0.0,PI/2,1.0e-6)*2/PI; double ellipticint = Utilities::InterpolateYvec(q_table,Fofq_table,fratio)*sqrt(fratio); - return LensHalo::get_mass()*Grav*lightspeed*lightspeed/pi/sigma/sigma/ellipticint; + return LensHalo::get_mass()*Grav*lightspeed*lightspeed/PI/sigma/sigma/ellipticint; } /* @@ -912,7 +912,7 @@ void LensHalo::force_halo_sym( /// intersecting, subtract the point particle if(rcm2 < Rmax*Rmax) { - PosType prefac = mass/rcm2/pi; + PosType prefac = mass/rcm2/PI; PosType x = sqrt(rcm2)/rscale; // PosType xmax = Rmax/rscale; PosType tmp = (alpha_h(x) + 1.0*subtract_point)*prefac; @@ -927,13 +927,13 @@ void LensHalo::force_halo_sym( /*if (rcm2 < 1E-6){ std::cout << kappa_h(x)*prefac << " " << 0.5*(xcm[0]*xcm[0]-xcm[1]*xcm[1])*gamma_h(x)*prefac/rcm2 << " " << xcm[0]*xcm[1]*gamma_h(x)*prefac/rcm2 << " " < 0.659){ //assert(mass_norm_factor==1); @@ -1055,7 +1055,7 @@ void LensHalo::force_halo_asym( if(subtract_point){ //std::cout << "DO WE EVEN GET HERE??" << std::endl; - PosType tmp = screening*mass/pi/rcm2; // *mass_norm_factor + PosType tmp = screening*mass/PI/rcm2; // *mass_norm_factor alpha[0] += tmp*xcm[0]; alpha[1] += tmp*xcm[1]; @@ -1063,7 +1063,7 @@ void LensHalo::force_halo_asym( gamma[0] += 0.5*(xcm[0]*xcm[0]-xcm[1]*xcm[1])*tmp; gamma[1] += xcm[0]*xcm[1]*tmp; - *phi += 0.5 * log(rcm2) * mass / pi ; // *mass_norm_factor + *phi += 0.5 * log(rcm2) * mass / PI ; // *mass_norm_factor } } @@ -1071,7 +1071,7 @@ void LensHalo::force_halo_asym( { if (subtract_point == false) { - PosType prefac = mass/rcm2/pi; + PosType prefac = mass/rcm2/PI; alpha[0] += -1.0 * prefac * xcm[0]; alpha[1] += -1.0 * prefac * xcm[1]; @@ -1088,7 +1088,7 @@ void LensHalo::force_halo_asym( gamma[0] += 0.5*(xcm[0]*xcm[0]-xcm[1]*xcm[1])*tmp; gamma[1] += xcm[0]*xcm[1]*tmp; - *phi += 0.5 * log(rcm2) * mass / pi ; + *phi += 0.5 * log(rcm2) * mass / PI ; } } @@ -1127,7 +1127,7 @@ void LensHalo::force_halo_asym( // This is the case when the ray is within the NSIE's circular region of influence but outside its elliptical truncation PosType alpha_out[2],alpha_in[2],rin,x_in[2]; - PosType prefac = -1.0*mass/Rmax/pi; + PosType prefac = -1.0*mass/Rmax/PI; PosType r = sqrt(rcm2); float units = pow(sigma/lightspeed,2)/Grav/sqrt(fratio); // mass/distance(physical) @@ -1190,7 +1190,7 @@ void LensHalo::force_halo_asym( { if (subtract_point == false) { - PosType prefac = mass/rcm2/pi; + PosType prefac = mass/rcm2/PI; alpha[0] += -1.0*prefac*xcm[0]; alpha[1] += -1.0*prefac*xcm[1]; @@ -1206,7 +1206,7 @@ void LensHalo::force_halo_asym( if(subtract_point){ - PosType fac = mass/rcm2/pi; + PosType fac = mass/rcm2/PI; alpha[0] += fac*xcm[0]; alpha[1] += fac*xcm[1]; @@ -1252,7 +1252,7 @@ void LensHaloRealNSIE::force_halo( // This is the case when the ray is within the NSIE's circular region of influence but outside its elliptical truncation PosType alpha_iso[2],alpha_ellip[2]; - //PosType prefac = -1.0*mass/Rmax/pi; + //PosType prefac = -1.0*mass/Rmax/PI; PosType r = sqrt(rcm2); //double Rin = sqrt(rcm2)*LensHalo::getRsize()/ellipR; @@ -1274,15 +1274,15 @@ void LensHaloRealNSIE::force_halo( // // point mass solution - // PosType tmp = mass/rcm2/pi; - PosType tmp = LensHalo::get_mass()/rcm2/pi; + // PosType tmp = mass/rcm2/PI; + PosType tmp = LensHalo::get_mass()/rcm2/PI; alpha_iso[0] = -1.0*tmp*xcm[0]; alpha_iso[1] = -1.0*tmp*xcm[1]; alpha[0] += alpha_iso[0]*f2 + alpha_ellip[0]*f1; alpha[1] += alpha_iso[1]*f2 + alpha_ellip[1]*f1; { - PosType tmp = -2.0*LensHalo::get_mass()/rcm2/pi/rcm2; + PosType tmp = -2.0*LensHalo::get_mass()/rcm2/PI/rcm2; gamma[0] += 0.5*(xcm[0]*xcm[0]-xcm[1]*xcm[1])*tmp; gamma[1] += xcm[0]*xcm[1]*tmp; @@ -1310,7 +1310,7 @@ void LensHaloRealNSIE::force_halo( if(subtract_point) { - PosType fac = screening*LensHalo::get_mass()/rcm2/pi; + PosType fac = screening*LensHalo::get_mass()/rcm2/PI; alpha[0] += fac*xcm[0]; alpha[1] += fac*xcm[1]; @@ -1328,7 +1328,7 @@ void LensHaloRealNSIE::force_halo( // outside of the halo if (subtract_point == false) { - PosType prefac = LensHalo::get_mass()/rcm2/pi; + PosType prefac = LensHalo::get_mass()/rcm2/PI; alpha[0] += -1.0*prefac*xcm[0]; alpha[1] += -1.0*prefac*xcm[1]; @@ -1699,7 +1699,7 @@ void LensHaloDummy::force_halo(PosType *alpha ) { PosType rcm2 = xcm[0]*xcm[0] + xcm[1]*xcm[1]; - PosType prefac = LensHalo::get_mass()/rcm2/pi; + PosType prefac = LensHalo::get_mass()/rcm2/PI; PosType tmp = subtract_point*prefac; alpha[0] += tmp*xcm[0]; alpha[1] += tmp*xcm[1]; @@ -1775,19 +1775,19 @@ PosType LensHalo::MassBy2DIntegation(PosType R){ PosType LensHalo::MassBy1DIntegation(PosType R){ LensHalo::DMDTHETA dmdtheta(R,this); - return R*Utilities::nintegrate(dmdtheta, 0, 2*pi, 1.0e-6)/2; + return R*Utilities::nintegrate(dmdtheta, 0, 2*PI, 1.0e-6)/2; } /// calculates the average gamma_t for LensHalo::test() double LensHalo::test_average_gt(PosType R){ struct test_gt_func f(R,this); - return Utilities::nintegrate(f,0.0,2.*pi,1.0e-3); + return Utilities::nintegrate(f,0.0,2.*PI,1.0e-3); } -// returns x 2pi on a ring at R +// returns x 2PI on a ring at R double LensHalo::test_average_kappa(PosType R){ struct test_kappa_func f(R,this); - return Utilities::nintegrate(f,0.0,2.*pi,1.0e-3); + return Utilities::nintegrate(f,0.0,2.*PI,1.0e-3); } /// Three tests: 1st - Mass via 1D integration vs mass via 2D integration. 2nd: gamma_t=alpha/r - kappa(R) which can be used for spherical distributions. Deviations are expected for axis ratios <1. For the latter case we use the next test. 3rd: The average along a circular aperture of gamma_t should be equal to minus the average along a circular aperture over kappa. Note that also alpha/r - kappa is checked for consistency with kappa(. For axis ratios < 1 the factor between the two is expected to be of order O(10%). @@ -1836,9 +1836,9 @@ bool LensHalo::test(){ //integrate over t PosType average_gt, average_kappa; - average_gt=test_average_gt(r)/2/pi; - average_kappa=test_average_kappa(r)/2/pi; - m1 = MassBy1DIntegation(r)/pi/r/r; + average_gt=test_average_gt(r)/2/PI; + average_kappa=test_average_kappa(r)/2/PI; + m1 = MassBy1DIntegation(r)/PI/r/r; std::cout << r/Rmax << " " << r/Rsize << " " << -1.0*average_gt << " " << m1-average_kappa << " " << m1 << " " << average_kappa << " " << -1.0*(m1-average_kappa)/average_gt << std::endl; @@ -1886,7 +1886,7 @@ PosType LensHalo::DALPHAXDM::operator()(PosType m){ //PosType alpha[2]={0,0},tm[2] = {m*(isohalo->getRsize()),0}; //KappaType kappam=0,gamma[2]={0,0},phi; - kappa=isohalo->kappa_h(xiso)/pi/xiso/xiso*isohalo->mass; + kappa=isohalo->kappa_h(xiso)/PI/xiso/xiso*isohalo->mass; //isohalo->force_halo_sym(alpha,&kappam,gamma,&phi,tm); //std::cout << "kappa: " << kappa << " " << kappam << " " << kappa/kappam << std::endl; @@ -1906,7 +1906,7 @@ PosType LensHalo::DALPHAYDM::operator()(PosType m){ //PosType alpha[2]={0,0},tm[2] = {m*(isohalo->getRsize()),0}; //KappaType kappam=0,gamma[2]={0,0},phi; - kappa=isohalo->kappa_h(xiso)/pi/xiso/xiso*isohalo->mass; + kappa=isohalo->kappa_h(xiso)/PI/xiso/xiso*isohalo->mass; //isohalo->force_halo_sym(alpha,&kappam,gamma,&phi,tm); //std::cout << "kappa: " << kappa << " " << kappam << " " << kappa/kappam << std::endl; assert(kappa >= 0.0); diff --git a/Source/overzier.cpp b/Source/overzier.cpp index 99f11e1a..feb64811 100644 --- a/Source/overzier.cpp +++ b/Source/overzier.cpp @@ -150,7 +150,7 @@ PosType SourceOverzier::getTotalFlux() const{ } void SourceOverzier::printSource(){ - std::cout << "bulge half light radius: " << Reff*180*60*60/pi << " arcs disk scale hight: " << Rh*180*60*60/pi << " arcs" << std::endl; + std::cout << "bulge half light radius: " << Reff*180*60*60/PI << " arcs disk scale hight: " << Rh*180*60*60/PI << " arcs" << std::endl; } void SourceOverzier::assignParams(InputParams& /* params */) @@ -199,7 +199,7 @@ SourceOverzier(my_mag,my_mag_bulge,my_Reff,my_Rh,my_PA,inclination,my_id,my_z,th Narms = (ran() > 0.2) ? 2 : 4; // number of arms arm_alpha = (21 + 10*(ran()-0.5)*2)*degreesTOradians; // arm pitch angle mctalpha = Narms/tan(arm_alpha); - disk_phase = pi*ran(); // add phase of arms + disk_phase = PI*ran(); // add phase of arms Ad = minA + (maxA-minA)*ran(); // extra cersic component @@ -210,9 +210,9 @@ SourceOverzier(my_mag,my_mag_bulge,my_Reff,my_Rh,my_PA,inclination,my_id,my_z,th double q = 1 + (0.5-1)*ran(); spheroid.setSersicIndex(index); - //spheroid = new SourceSersic(my_mag_bulge,my_Reff,-my_PA + 10*(ran() - 0.5)*pi/180,index,q,my_z,theta); + //spheroid = new SourceSersic(my_mag_bulge,my_Reff,-my_PA + 10*(ran() - 0.5)*PI/180,index,q,my_z,theta); - spheroid.ReSet(my_mag_bulge,my_Reff,-my_PA + 10*(ran() - 0.5)*pi/180,index,q,my_z,theta); + spheroid.ReSet(my_mag_bulge,my_Reff,-my_PA + 10*(ran() - 0.5)*PI/180,index,q,my_z,theta); cospa = cos(PA); sinpa = sin(PA); @@ -391,8 +391,8 @@ void SourceOverzierPlus::randomize(Utilities::RandomNumbers_NR &ran){ */ mag += tmp; - PA = pi*ran(); - inclination = 1.0*pi/2*ran(); + PA = PI*ran(); + inclination = 1.0*PI/2*ran(); if(cos(inclination)< 0.25) inclination = acos(0.25); @@ -422,7 +422,7 @@ void SourceOverzierPlus::randomize(Utilities::RandomNumbers_NR &ran){ Narms = (ran() > 0.2) ? 2 : 4; // number of arms arm_alpha = (21 + 10*(ran()-0.5)*2)*degreesTOradians; // arm pitch angle mctalpha = Narms/tan(arm_alpha); - disk_phase = pi*ran(); // add phase of arms + disk_phase = PI*ran(); // add phase of arms Ad = minA + (maxA-minA)*ran(); // spheroid @@ -435,10 +435,10 @@ void SourceOverzierPlus::randomize(Utilities::RandomNumbers_NR &ran){ double q = 1 + (0.5-1)*ran(); /*delete spheroid; - spheroid = new SourceSersic(mag_bulge,Reff/arcsecTOradians,-PA + 10*(ran() - 0.5)*pi/180,index,q,zsource,getTheta().x); + spheroid = new SourceSersic(mag_bulge,Reff/arcsecTOradians,-PA + 10*(ran() - 0.5)*PI/180,index,q,zsource,getTheta().x); */ - spheroid.ReSet(mag_bulge,Reff/arcsecTOradians,-PA + 10*(ran() - 0.5)*pi/180,index,q,zsource,getTheta().x); + spheroid.ReSet(mag_bulge,Reff/arcsecTOradians,-PA + 10*(ran() - 0.5)*PI/180,index,q,zsource,getTheta().x); for(PosType &mod : modes){ diff --git a/TreeCode/quadTree.cpp b/TreeCode/quadTree.cpp index 0f0d5aca..0a585890 100644 --- a/TreeCode/quadTree.cpp +++ b/TreeCode/quadTree.cpp @@ -648,7 +648,7 @@ void TreeQuad::walkTree_iter( if(haloON){ prefac = halos[tmp_index]->get_mass(); } else{ prefac = masses[tmp_index]; } - prefac /= rcm2*pi/screening; + prefac /= rcm2*PI/screening; tmp = -1.0*prefac; @@ -689,7 +689,7 @@ void TreeQuad::walkTree_iter( if(rcm2 < 1e-20) rcm2 = 1e-20; //rcm = sqrt(rcm2); - //prefac = masses[MultiMass*tmp_index]/rcm2/pi; + //prefac = masses[MultiMass*tmp_index]/rcm2/PI; //arg1 = rcm2/(sizes[tmp_index*MultiRadius]*sizes[tmp_index*MultiRadius]); //arg2 = sizes[tmp_index*MultiRadius]; //tmp = sizes[tmp_index*MultiRadius]; @@ -713,29 +713,29 @@ void TreeQuad::walkTree_iter( allowDescent=false; double screening = exp(-rcm2cell*inv_screening_scale2); - double tmp = -1.0*(*treeit)->mass/rcm2cell/pi*screening; + double tmp = -1.0*(*treeit)->mass/rcm2cell/PI*screening; alpha[0] += tmp*xcm[0]; alpha[1] += tmp*xcm[1]; { // taken out to speed up - tmp=-2.0*(*treeit)->mass/pi/rcm2cell/rcm2cell*screening; + tmp=-2.0*(*treeit)->mass/PI/rcm2cell/rcm2cell*screening; gamma[0] += 0.5*(xcm[0]*xcm[0]-xcm[1]*xcm[1])*tmp; gamma[1] += xcm[0]*xcm[1]*tmp; - *phi += 0.5*(*treeit)->mass*log( rcm2cell )/pi*screening; - *phi -= 0.5*( (*treeit)->quad[0]*xcm[0]*xcm[0] + (*treeit)->quad[1]*xcm[1]*xcm[1] + 2*(*treeit)->quad[2]*xcm[0]*xcm[1] )/(pi*rcm2cell*rcm2cell)*screening; + *phi += 0.5*(*treeit)->mass*log( rcm2cell )/PI*screening; + *phi -= 0.5*( (*treeit)->quad[0]*xcm[0]*xcm[0] + (*treeit)->quad[1]*xcm[1]*xcm[1] + 2*(*treeit)->quad[2]*xcm[0]*xcm[1] )/(PI*rcm2cell*rcm2cell)*screening; } // quadrapole contribution // the kappa and gamma are not calculated to this order alpha[0] -= ((*treeit)->quad[0]*xcm[0] + (*treeit)->quad[2]*xcm[1]) - /(rcm2cell*rcm2cell)/pi*screening; + /(rcm2cell*rcm2cell)/PI*screening; alpha[1] -= ((*treeit)->quad[1]*xcm[1] + (*treeit)->quad[2]*xcm[0]) - /(rcm2cell*rcm2cell)/pi*screening; + /(rcm2cell*rcm2cell)/PI*screening; tmp = 4*((*treeit)->quad[0]*xcm[0]*xcm[0] + (*treeit)->quad[1]*xcm[1]*xcm[1] - + 2*(*treeit)->quad[2]*xcm[0]*xcm[1])/(rcm2cell*rcm2cell*rcm2cell)/pi*screening; + + 2*(*treeit)->quad[2]*xcm[0]*xcm[1])/(rcm2cell*rcm2cell*rcm2cell)/PI*screening; alpha[0] += tmp*xcm[0]; alpha[1] += tmp*xcm[1]; @@ -879,7 +879,7 @@ void TreeQuad::walkTree_recur(QBranchNB *branch,PosType const *ray,PosType *alph if(haloON ) { prefac = halos[tmp_index]->get_mass(); } else{ prefac = masses[tmp_index]; } - prefac /= rcm2*pi/screening; + prefac /= rcm2*PI/screening; alpha[0] += -1.0*prefac*xcm[0]; @@ -945,29 +945,29 @@ void TreeQuad::walkTree_recur(QBranchNB *branch,PosType const *ray,PosType *alph { // use whole cell double screening = exp(-rcm2cell*inv_screening_scale2); - double tmp = -1.0*branch->mass/rcm2cell/pi*screening; + double tmp = -1.0*branch->mass/rcm2cell/PI*screening; alpha[0] += tmp*xcm[0]; alpha[1] += tmp*xcm[1]; { // taken out to speed up - tmp=-2.0*branch->mass/pi/rcm2cell/rcm2cell*screening; + tmp=-2.0*branch->mass/PI/rcm2cell/rcm2cell*screening; gamma[0] += 0.5*(xcm[0]*xcm[0]-xcm[1]*xcm[1])*tmp; gamma[1] += xcm[0]*xcm[1]*tmp; - *phi += 0.5*tree->current->mass*log( rcm2cell )/pi*screening; - *phi -= 0.5*( tree->current->quad[0]*xcm[0]*xcm[0] + tree->current->quad[1]*xcm[1]*xcm[1] + 2*tree->current->quad[2]*xcm[0]*xcm[1] )/(pi*rcm2cell*rcm2cell)*screening; + *phi += 0.5*tree->current->mass*log( rcm2cell )/PI*screening; + *phi -= 0.5*( tree->current->quad[0]*xcm[0]*xcm[0] + tree->current->quad[1]*xcm[1]*xcm[1] + 2*tree->current->quad[2]*xcm[0]*xcm[1] )/(PI*rcm2cell*rcm2cell)*screening; } // quadrapole contribution // the kappa and gamma are not calculated to this order alpha[0] -= (branch->quad[0]*xcm[0] + branch->quad[2]*xcm[1]) - /(rcm2cell*rcm2cell)/pi*screening; + /(rcm2cell*rcm2cell)/PI*screening; alpha[1] -= (branch->quad[1]*xcm[1] + branch->quad[2]*xcm[0]) - /(rcm2cell*rcm2cell)/pi*screening; + /(rcm2cell*rcm2cell)/PI*screening; tmp = 4*(branch->quad[0]*xcm[0]*xcm[0] + branch->quad[1]*xcm[1]*xcm[1] - + 2*branch->quad[2]*xcm[0]*xcm[1])/(rcm2cell*rcm2cell*rcm2cell)/pi*screening; + + 2*branch->quad[2]*xcm[0]*xcm[1])/(rcm2cell*rcm2cell*rcm2cell)/PI*screening; alpha[0] += tmp*xcm[0]; alpha[1] += tmp*xcm[1]; diff --git a/TreeCode_link/curve_routines.cpp b/TreeCode_link/curve_routines.cpp index e93212d5..781d38d5 100644 --- a/TreeCode_link/curve_routines.cpp +++ b/TreeCode_link/curve_routines.cpp @@ -1913,8 +1913,8 @@ namespace Utilities{ for(ii=0;iiMoveToTop(),l=0;lNunits();l++,critical[ii].imagekist->Down()){ - file_crit << 180/pi*3600.*critical[ii].imagekist->getCurrent()->image->x[0] << " "; - file_crit << 180/pi*3600.*critical[ii].imagekist->getCurrent()->image->x[1] << std::endl; + file_crit << 180/PI*3600.*critical[ii].imagekist->getCurrent()->image->x[0] << " "; + file_crit << 180/PI*3600.*critical[ii].imagekist->getCurrent()->image->x[1] << std::endl; } } @@ -1934,8 +1934,8 @@ namespace Utilities{ for(ii=0;iiMoveToTop(),l=0;lNunits();l++,critical[ii].imagekist->Down()){ - file_crit << 180/pi*3600.*critical[ii].imagekist->getCurrent()->x[0] << " "; - file_crit << 180/pi*3600.*critical[ii].imagekist->getCurrent()->x[1] << std::endl; + file_crit << 180/PI*3600.*critical[ii].imagekist->getCurrent()->x[0] << " "; + file_crit << 180/PI*3600.*critical[ii].imagekist->getCurrent()->x[1] << std::endl; } } file_crit.close(); @@ -1953,8 +1953,8 @@ namespace Utilities{ } for(critical[ind_caustic].imagekist->MoveToTop(),l=0;lNunits();l++,critical[ind_caustic].imagekist->Down()){ - file_crit << 180/pi*3600.*critical[ind_caustic].imagekist->getCurrent()->image->x[0] << " "; - file_crit << 180/pi*3600.*critical[ind_caustic].imagekist->getCurrent()->image->x[1] << std::endl; + file_crit << 180/PI*3600.*critical[ind_caustic].imagekist->getCurrent()->image->x[0] << " "; + file_crit << 180/PI*3600.*critical[ind_caustic].imagekist->getCurrent()->image->x[1] << std::endl; } file_crit.close(); @@ -1972,8 +1972,8 @@ namespace Utilities{ } for(critical[ind_caustic].imagekist->MoveToTop(),l=0;lNunits();l++,critical[ind_caustic].imagekist->Down()){ - file_crit << 180/pi*3600.*critical[ind_caustic].imagekist->getCurrent()->x[0] << " "; - file_crit << 180/pi*3600.*critical[ind_caustic].imagekist->getCurrent()->x[1] << std::endl; + file_crit << 180/PI*3600.*critical[ind_caustic].imagekist->getCurrent()->x[0] << " "; + file_crit << 180/PI*3600.*critical[ind_caustic].imagekist->getCurrent()->x[1] << std::endl; } file_crit.close(); @@ -2439,7 +2439,7 @@ std::vector Utilities::concave_hull(std::vector &P,int k,bool intersects = true; if(test){ - std::cout << "smin/pi = " << smin/pi << std::endl; + std::cout << "smin/pi = " << smin/PI << std::endl; std::cout << "intersecting line segments ii = " << ii << std::endl; std::cout << hull.back()->x[0] << " " << hull.back()->x[1] << " -- " << Res[imin]->x[0] << " " << Res[imin]->x[1] << std::endl; @@ -2581,13 +2581,13 @@ void Utilities::contour_ellipse(std::vector &P, Point_2d center, unsig if (mostdistant_contour_pnt imageinfo[k].area/pi/r_source/r_source ) imageinfo[k].ShouldNotRefine = true; + if( 5.0e-3 > imageinfo[k].area/PI/r_source/r_source ) imageinfo[k].ShouldNotRefine = true; else imageinfo[k].ShouldNotRefine = false; } */ @@ -745,7 +745,7 @@ void ImageFinding::find_images_microlens( */ NuniformMags = 0; for(int k=0; k < *Nimages; ++k){ - if( 1.0e-4 > imageinfo[k].area/pi/r_source/r_source ) imageinfo[k].ShouldNotRefine = true; + if( 1.0e-4 > imageinfo[k].area/PI/r_source/r_source ) imageinfo[k].ShouldNotRefine = true; if(*Nimages > 10 && (imageinfo[k].imagekist->Nunits() > 10 && imageinfo[k].constant(INVMAG,1.0e-4)) ){ imageinfo[k].ShouldNotRefine = true; ++NuniformMags; @@ -773,8 +773,8 @@ void ImageFinding::find_images_microlens( printf(" refound images after refinement\n Nimagepoints=%li Nimages = %i\n" ,*Nimagepoints,*Nimages); for(int k=0; k < *Nimages; ++k){ - std::cout << " " << imageinfo[k].area << " " << imageinfo[k].area_error << " " << imageinfo[k].area/pi/rtemp/rtemp - << " " << imageinfo[k].area/pi/r_source/r_source << " " << imageinfo[k].getNimagePoints() << std::endl; + std::cout << " " << imageinfo[k].area << " " << imageinfo[k].area_error << " " << imageinfo[k].area/PI/rtemp/rtemp + << " " << imageinfo[k].area/PI/r_source/r_source << " " << imageinfo[k].getNimagePoints() << std::endl; } } @@ -832,7 +832,7 @@ void ImageFinding::find_images_microlens( assert(*Nimages > 10); for(int i=0;i<*Nimages;++i){ - imageinfo[i].area = fabs(pi*r_source*r_source/imageinfo[i].imagekist->getCurrent()->invmag); + imageinfo[i].area = fabs(PI*r_source*r_source/imageinfo[i].imagekist->getCurrent()->invmag); imageinfo[i].imagekist->Empty(); imageinfo[i].ShouldNotRefine = false; @@ -1074,7 +1074,7 @@ void ImageFinding::find_images_microlens( } if(imageinfo[i].constant(INVMAG,1.0e-3)) - imageinfo[i].area = fabs(pi*r_source*r_source/imageinfo[i].imagekist->getCurrent()->invmag); + imageinfo[i].area = fabs(PI*r_source*r_source/imageinfo[i].imagekist->getCurrent()->invmag); // redefine error so that it is based on the smallest grid cell on the border of the image if(imageinfo[i].outerborder->Nunits() > 0 ) imageinfo[i].area_error = imageinfo[i].gridrange[2]/imageinfo[i].area; @@ -1229,7 +1229,7 @@ void ImageFinding::find_images_microlens_exper( NuniformMags = 0; for(int k=0; k < *Nimages; ++k){ - if( 1.0e-4 > imageinfo[k].area/pi/r_source/r_source ) imageinfo[k].ShouldNotRefine = true; + if( 1.0e-4 > imageinfo[k].area/PI/r_source/r_source ) imageinfo[k].ShouldNotRefine = true; if( (*Nimages > 10) * (imageinfo[k].imagekist->Nunits() > 20) * imageinfo[k].constant(INVMAG,1.0e-4) ){ if(!(imageinfo[k].ShouldNotRefine)){ @@ -1275,8 +1275,8 @@ void ImageFinding::find_images_microlens_exper( printf(" refound images after refinement\n Nimagepoints=%li Nimages = %i\n" ,*Nimagepoints,*Nimages); for(int k=0; k < *Nimages; ++k){ - std::cout << " " << imageinfo[k].area << " " << imageinfo[k].area_error << " " << imageinfo[k].area/pi/rtemp/rtemp - << " " << imageinfo[k].area/pi/r_source/r_source << " " << imageinfo[k].getNimagePoints() << std::endl; + std::cout << " " << imageinfo[k].area << " " << imageinfo[k].area_error << " " << imageinfo[k].area/PI/rtemp/rtemp + << " " << imageinfo[k].area/PI/r_source/r_source << " " << imageinfo[k].getNimagePoints() << std::endl; } } @@ -1316,7 +1316,7 @@ void ImageFinding::find_images_microlens_exper( assert(*Nimages > 10); for(int i=0;i<*Nimages;++i){ - imageinfo[i].area = fabs(pi*r_source*r_source/imageinfo[i].imagekist->getCurrent()->invmag); + imageinfo[i].area = fabs(PI*r_source*r_source/imageinfo[i].imagekist->getCurrent()->invmag); imageinfo[i].imagekist->Empty(); imageinfo[i].ShouldNotRefine = false; @@ -1558,7 +1558,7 @@ void ImageFinding::find_images_microlens_exper( } if(imageinfo[i].constant(INVMAG,1.0e-3)) - imageinfo[i].area = fabs(pi*r_source*r_source/imageinfo[i].imagekist->getCurrent()->invmag); + imageinfo[i].area = fabs(PI*r_source*r_source/imageinfo[i].imagekist->getCurrent()->invmag); // redefine error so that it is based on the smallest grid cell on the border of the image if(imageinfo[i].outerborder->Nunits() > 0 ) imageinfo[i].area_error = imageinfo[i].gridrange[2]/imageinfo[i].area; diff --git a/TreeCode_link/map_images.cpp b/TreeCode_link/map_images.cpp index 2f7b7c96..37aa76c5 100644 --- a/TreeCode_link/map_images.cpp +++ b/TreeCode_link/map_images.cpp @@ -153,7 +153,7 @@ void ImageFinding::map_images( rs[i] = (rs[i] == 0) ? xmin : sqrt(rs[i]); // filling factor or holiness of source - if(verbose) printf("holiness of source %e\n",sourceinfo[i].imagekist->Nunits()*pow(2*xmax/(Ntmp-1)/rs[i],2)/pi); + if(verbose) printf("holiness of source %e\n",sourceinfo[i].imagekist->Nunits()*pow(2*xmax/(Ntmp-1)/rs[i],2)/PI); if(verbose) printf(" dx = %e %e rs = %e Npoints = %li\n",(source->getTheta()[0]-sourceinfo[i].centroid[0])/xmin ,(source->getTheta()[1]-sourceinfo[i].centroid[1])/xmin ,rs[i],sourceinfo[i].imagekist->Nunits()); diff --git a/TreeCode_link/utilities.cpp b/TreeCode_link/utilities.cpp index d1fcf3ac..57ca9573 100644 --- a/TreeCode_link/utilities.cpp +++ b/TreeCode_link/utilities.cpp @@ -74,7 +74,7 @@ void log_polar_grid(Point *i_points,PosType rmax,PosType rmin,PosType *center,lo static long id=0; for(i=0;i #include -#ifndef pi -#define pi 3.141593 +#ifndef PI +#define PI 3.141593 #endif #ifndef treeNBdim diff --git a/include/lens_halos.h b/include/lens_halos.h index 49427a58..b929b2f6 100644 --- a/include/lens_halos.h +++ b/include/lens_halos.h @@ -381,7 +381,7 @@ class LensHalo{ PosType xisq=sqrt((x[0]*x[0]+x[1]*x[1]/(1-u*(1-halo->fratio*halo->fratio)))); if(xisq*xisq < 1e-20) xisq = 1.0e-10; - KappaType kappa=halo->kappa_h(xisq)/pi/xisq/xisq; + KappaType kappa=halo->kappa_h(xisq)/PI/xisq/xisq; //std::cout << "Schramm: n=" << n << " " << u << " " << xisq << " " << halo->fratio << " " << rmx << " " << halo->Rmax << " " << halo->rscale << std::endl; return kappa*halo->mass/pow((1.-(1.-halo->fratio*halo->fratio)*u),n+0.5); } @@ -402,7 +402,7 @@ class LensHalo{ PosType km1=halo->kappa_h(xisq-h)/((xisq-h)*(xisq-h)); PosType kp2=halo->kappa_h(xisq+2*h)/((xisq+2*h)*(xisq+2*h)); PosType km2=halo->kappa_h(xisq-2*h)/((xisq-2*h)*(xisq-2*h)); - PosType dkdxi=(8*kp1-8*km1-kp2+km2)/12/h/pi; + PosType dkdxi=(8*kp1-8*km1-kp2+km2)/12/h/PI; @@ -423,7 +423,7 @@ class LensHalo{ PosType xisq=sqrt((x[0]*x[0]+x[1]*x[1]/(1-u*(1-halo->fratio*halo->fratio)))); if(xisq*xisq < 1e-20) xisq = 1.0e-10; - PosType alpha=halo->alpha_h(xisq)/pi/xisq; + PosType alpha=halo->alpha_h(xisq)/PI/xisq; //std::cout << "Schramm: n=" << n << " " << u << " " << xisq << " " << halo->fratio << " " << rmx << " " << halo->Rmax << " " << halo->rscale << std::endl; return xisq*alpha*halo->mass/pow((1.-(1.-halo->fratio*halo->fratio)*u),0.5); } @@ -441,7 +441,7 @@ class LensHalo{ PosType tmp = m*rmx; if(tmp*tmp < 1e-20) tmp = 1.0e-10; PosType xiso=tmp/halo->rscale; - KappaType kappa=halo->kappa_h(xiso)/pi/xiso/xiso; + KappaType kappa=halo->kappa_h(xiso)/PI/xiso/xiso; PosType alpha[2]={0,0},tm[2] = {m*rmx,0}; KappaType kappam=0,gamma[2]={0,0},phi; @@ -472,7 +472,7 @@ class LensHalo{ PosType tmp = m*rmx; if(tmp*tmp < 1e-20) tmp = 1.0e-10; PosType xiso=tmp/halo->rscale; - KappaType kappa=halo->kappa_h(xiso)/pi/xiso/xiso; + KappaType kappa=halo->kappa_h(xiso)/PI/xiso/xiso; PosType alpha[2]={0,0},tm[2] = {m*rmx,0}; KappaType kappam=0,gamma[2]={0,0},phi; @@ -539,7 +539,7 @@ class LensHalo{ PosType r; PosType a[2] = {0,0},x[2] = {0,0}; KappaType k = 0,g[3] = {0,0,0} ,p=0; - double operator ()(PosType t) {x[0]=r*cos(t); x[1]=r*sin(t); halo.force_halo(a,&k,g,&p,x);return 2*pi*k*r*r; } + double operator ()(PosType t) {x[0]=r*cos(t); x[1]=r*sin(t); halo.force_halo(a,&k,g,&p,x);return 2*PI*k*r*r; } }; */ @@ -608,7 +608,7 @@ class LensHalo{ //std::cout << " R = " << exp(logR) << std::endl; if(exp(2*logR) == 0.0) return 0.0; - return Utilities::nintegrate(dmdrdtheta,0,2*pi,1.0e-7) + return Utilities::nintegrate(dmdrdtheta,0,2*PI,1.0e-7) *exp(2*logR); }else{ PosType alpha[2] = {0,0},x[2] = {0,0}; @@ -618,7 +618,7 @@ class LensHalo{ x[1] = 0; halo->force_halo(alpha,&kappa,gamma,&phi,x); - return 2*pi*kappa*exp(2*logR); + return 2*PI*kappa*exp(2*logR); } } protected: @@ -738,7 +738,7 @@ class LensHaloNFW: public LensHalo{ void assignParams(InputParams& params); // Override internal structure of halos - /// r |alpha(r = x*rscale)| pi Sigma_crit / Mass + /// r |alpha(r = x*rscale)| PI Sigma_crit / Mass inline PosType alpha_h(PosType x) const { //return -1.0*InterpolateFromTable(gtable,x)/InterpolateFromTable(gtable,xmax); return -1.0*InterpolateFromTable(gtable,x)/gmax; @@ -821,7 +821,7 @@ class LensHaloPseudoNFW: public LensHalo{ PosType beta; // Override internal structure of halos - /// r |alpha(r)| pi Sigma_crit / Mass + /// r |alpha(r)| PI Sigma_crit / Mass inline PosType alpha_h(PosType x) const { return -1.0*InterpolateFromTable(x)/InterpolateFromTable(xmax); } @@ -879,14 +879,14 @@ class LensHaloPowerLaw: public LensHalo{ // PosType beta; // Override internal structure of halos - /// r |alpha(r)| pi Sigma_crit / Mass + /// r |alpha(r)| PI Sigma_crit / Mass inline PosType alpha_h( PosType x /// r/rscale ) const{ if(x==0) x=1e-6*xmax; return -1.0*pow(x/xmax,-beta+2); } - /// this is kappa Sigma_crit pi (r/rscale)^2 / mass + /// this is kappa Sigma_crit PI (r/rscale)^2 / mass inline KappaType kappa_h( PosType x /// r/rscale ) const { @@ -894,12 +894,12 @@ class LensHaloPowerLaw: public LensHalo{ double tmp = x/xmax; return 0.5*(-beta+2)*pow(tmp,2-beta); } - /// this is |gamma| Sigma_crit pi (r/rscale)^2 / mass + /// this is |gamma| Sigma_crit PI (r/rscale)^2 / mass inline KappaType gamma_h(PosType x) const { if(x==0) x=1e-6*xmax; return -beta*pow(x/xmax,-beta+2); } - /// this is phi Sigma_crit pi / mass, the constants are added so that it is continous over the bourder at Rsize + /// this is phi Sigma_crit PI / mass, the constants are added so that it is continous over the bourder at Rsize inline KappaType phi_h(PosType x) const { if(x==0) x=1e-6*xmax; return ( pow(x/xmax,2-beta) - 1 )/(2-beta) + log(LensHalo::getRsize()) ; @@ -1056,7 +1056,7 @@ class LensHaloHernquist: public LensHalo{ void assignParams(InputParams& params); // Override internal structure of halos - /// r |alpha(r)| pi Sigma_crit / Mass + /// r |alpha(r)| PI Sigma_crit / Mass inline PosType alpha_h(PosType x) const { return -0.25*InterpolateFromTable(gtable,x)/gmax; } @@ -1127,7 +1127,7 @@ class LensHaloJaffe: public LensHalo{ void assignParams(InputParams& params); // Override internal structure of halos - /// r |alpha(r)| pi Sigma_crit / Mass + /// r |alpha(r)| PI Sigma_crit / Mass inline PosType alpha_h(PosType x) const{ return -0.25*InterpolateFromTable(gtable,x)/gmax; } diff --git a/include/overzier_source.h b/include/overzier_source.h index c7452361..34b0dd6d 100644 --- a/include/overzier_source.h +++ b/include/overzier_source.h @@ -10,9 +10,9 @@ #include "source.h" #include "sersic_source.h" -// define pi here if not done via include -#ifndef pi -#define pi 3.141592653589793238462643383279502884 +// define PI here if not done via include +#ifndef PI +#define PI 3.141592653589793238462643383279502884 #endif /** @@ -69,9 +69,9 @@ class SourceOverzier : public Source void setMagBulge(Band band,PosType my_mag); /// bulge half light radius in radians - PosType getReff() const { return Reff/(pi/180/60/60); } + PosType getReff() const { return Reff/(PI/180/60/60); } /// disk scale height in radians - PosType getRh() const { return Rh/(pi/180/60/60); } + PosType getRh() const { return Rh/(PI/180/60/60); } /// the bulge to total flux ratio PosType getBtoT() const { return pow(10,(-mag_bulge + mag)/2.5); } diff --git a/include/point.h b/include/point.h index 01928652..7f29f870 100644 --- a/include/point.h +++ b/include/point.h @@ -16,8 +16,8 @@ #include #include "Kist.h" -#ifndef pi -#define pi 3.141593 +#ifndef PI +#define PI 3.141593 #endif #ifndef error_message diff --git a/include/quadTree.h b/include/quadTree.h index a4a3cc2c..067aaeae 100644 --- a/include/quadTree.h +++ b/include/quadTree.h @@ -286,20 +286,20 @@ class TreeQuad { sigma = (8 - 12*q + 6*q2 - q3)/4; if(q > 1){ - sigma *= 10/size/size/7/pi; + sigma *= 10/size/size/7/PI; M = (-1 + 20*q2*(1 - q + 3*q2/8 - q3/20) )/7; - *phi += Mass*(-1232. + 1200*q2 - 800.*q3 + 225.*q4 - 24*q5 + 120*log(2./q) )/840/pi; + *phi += Mass*(-1232. + 1200*q2 - 800.*q3 + 225.*q4 - 24*q5 + 120*log(2./q) )/840/PI; }else{ - sigma = 10*( sigma - 1 + 3*q - 3*q2 + q3)/size/size/7/pi; + sigma = 10*( sigma - 1 + 3*q - 3*q2 + q3)/size/size/7/PI; M = 10*q2*(1 - 3*q/4 + 3*q3/10)/7; *phi += Mass*( phiintconst + 10*(q2/2 - 3*q4/4 + 3*q5/50)/7 - )/pi; + )/PI; } } PosType alpha_r,gt; // deflection * Sig_crit / Mass - alpha_r = (M-1)/pi/r; + alpha_r = (M-1)/PI/r; gt = alpha_r/r - sigma; alpha[0] -= Mass*alpha_r*xcm[0]/r; @@ -307,7 +307,7 @@ class TreeQuad { gamma[0] -= gt*Mass*(xcm[0]*xcm[0]-xcm[1]*xcm[1])/r/r; gamma[1] -= gt*Mass*2*xcm[0]*xcm[1]/r/r; *kappa += Mass*sigma; - *phi -= Mass*log(r)/pi; + *phi -= Mass*log(r)/PI; } /* Exponential kernel for particle profile @@ -326,7 +326,7 @@ class TreeQuad { ) const { - PosType prefac = Mass/rcm2/pi; + PosType prefac = Mass/rcm2/PI; PosType arg1 = rcm2/(size*size); PosType tmp = (alpha_h(arg1,size) + 1.0)*prefac; diff --git a/include/sersic_source.h b/include/sersic_source.h index 03c0de34..02688ada 100644 --- a/include/sersic_source.h +++ b/include/sersic_source.h @@ -31,7 +31,7 @@ class SourceSersic : public Source inline PosType getSersicIndex() const { return index; } inline PosType getAxesRatio() const { return q; } - inline PosType getReff() const { return Reff*180*60*60/pi; } + inline PosType getReff() const { return Reff*180*60*60/PI; } inline PosType getMag() const { return mag; } inline PosType getPA() const { return PA; } @@ -51,8 +51,8 @@ class SourceSersic : public Source inline void setReff(PosType x) { - Reff = x*pi/180/60/60; - I_r = 1./2./pi/Reff/Reff; + Reff = x*PI/180/60/60; + I_r = 1./2./PI/Reff/Reff; updateRadius(); } diff --git a/include/source.h b/include/source.h index 28483f84..af1edb8c 100644 --- a/include/source.h +++ b/include/source.h @@ -57,7 +57,7 @@ class Source /// Gets sb_limit in erg/cm^2/sec/rad^2/Hz PosType getSBlimit(){return sb_limit;} /// Gets sb_limit in mag/arcsec^2 - PosType getSBlimit_magarcsec(){return -2.5*log10(sb_limit*hplanck/pow((180*60*60/pi),2))-48.6;} + PosType getSBlimit_magarcsec(){return -2.5*log10(sb_limit*hplanck/pow((180*60*60/PI),2))-48.6;} // accessor functions that will sometimes be over ridden in class derivatives /// Redshift of source @@ -86,7 +86,7 @@ class Source /// Sets sb_limit in erg/cm^2/sec/rad^2/Hz void setSBlimit(float limit) {sb_limit = limit;} /// Sets sb_limit in mag/arcsec^2 - void setSBlimit_magarcsec(float limit) {sb_limit = pow(10,-0.4*(48.6+limit))*pow(180*60*60/pi,2)/hplanck;} + void setSBlimit_magarcsec(float limit) {sb_limit = pow(10,-0.4*(48.6+limit))*pow(180*60*60/PI,2)/hplanck;} PosType changeFilter(std::string filter_in, std::string filter_out, std::string sed); PosType integrateFilter(std::vector wavel_fil, std::vector fil); @@ -239,7 +239,7 @@ class SourceUniform : public Source{ PosType SurfaceBrightness(PosType *y); void assignParams(InputParams& params); void printSource(); - PosType getTotalFlux() const {return pi*source_r*source_r;} + PosType getTotalFlux() const {return PI*source_r*source_r;} }; /// A source with a Gaussian surface brightness profile @@ -254,7 +254,7 @@ class SourceGaussian : public Source{ PosType SurfaceBrightness(PosType *y); void assignParams(InputParams& params); void printSource(); - PosType getTotalFlux() const {return 2*pi*source_gauss_r2;/*std::cout << "No total flux in SourceGaussian yet" << std::endl; exit(1);*/} + PosType getTotalFlux() const {return 2*PI*source_gauss_r2;/*std::cout << "No total flux in SourceGaussian yet" << std::endl; exit(1);*/} }; /// Base class for all sources representing the Broad Line Region (BLR) of a AGN/QSO diff --git a/include/sourceAnaGalaxy.h b/include/sourceAnaGalaxy.h index af9fb67d..883011f9 100644 --- a/include/sourceAnaGalaxy.h +++ b/include/sourceAnaGalaxy.h @@ -105,7 +105,7 @@ class SourceMultiAnaGalaxy: public Source{ void sortInMag(Band tmp_band); void sortInID(); /// returns field-of-view in deg^2 assuming region is square - PosType getFOV(){return (rangex[1]-rangex[0])*(rangey[1]-rangey[0])*180*180/pi/pi;} + PosType getFOV(){return (rangex[1]-rangex[0])*(rangey[1]-rangey[0])*180*180/PI/PI;} /** \brief Finds the closest source to the position theta[] on the sky in Cartesian distance. * Returns the index of that source and its distance from theta[]. diff --git a/include/standard.h b/include/standard.h index 6fa5611b..9621deb9 100644 --- a/include/standard.h +++ b/include/standard.h @@ -33,8 +33,8 @@ #include #include -#ifndef pi -#define pi M_PI +#ifndef PI +#define PI M_PI #endif #ifndef Grav From 554860ed2465bee8c4f695cb996434e7a4fdc301 Mon Sep 17 00:00:00 2001 From: Nicolas Tessore Date: Fri, 7 Sep 2018 23:30:44 +0100 Subject: [PATCH 066/227] fix const reference methods --- include/utilities_slsim.h | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/include/utilities_slsim.h b/include/utilities_slsim.h index 010f9c3a..2d3bead5 100644 --- a/include/utilities_slsim.h +++ b/include/utilities_slsim.h @@ -190,7 +190,9 @@ namespace Utilities public: typedef ValueT value_type; typedef ValueT* pointer; + typedef const ValueT* const_pointer; typedef ValueT& reference; + typedef const ValueT& const_reference; typedef typename base_iterator::difference_type difference_type; typedef typename base_iterator::iterator_category iterator_category; @@ -201,10 +203,10 @@ namespace Utilities iterator& operator=(const iterator& rhs) { it = rhs.it; return *this; } reference operator*() { return (reference)(**it); } - const reference operator*() const { return (const reference)(**it); } + const_reference operator*() const { return (const_reference)(**it); } pointer operator->() { return (pointer)(*it); } - const pointer operator->() const { return (const pointer)(*it); } + const_pointer operator->() const { return (const_pointer)(*it); } iterator& operator++() { ++it; return *this; } iterator operator++(int) { iterator tmp(*this); ++it; return tmp; } @@ -218,7 +220,7 @@ namespace Utilities iterator& operator-=(difference_type n) { it -= n; return *this; } reference operator[](difference_type n) { return (reference)*it[n]; } - const reference operator[](difference_type n) const { return (const reference)*it[n]; } + const_reference operator[](difference_type n) const { return (const_reference)*it[n]; } friend iterator operator+(const iterator& i, difference_type n) { return iterator(i.it + n); } friend iterator operator+(difference_type n, const iterator& i) { return iterator(i.it + n); } From 086071547a7e43f77a5e0ec105241b273b7663ac Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Tue, 23 Oct 2018 20:23:48 +0200 Subject: [PATCH 067/227] Added ability to retrieve original seed from Utilities::RandomNumbers_NR --- TreeCode_link/utilities.cpp | 2 +- include/utilities_slsim.h | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/TreeCode_link/utilities.cpp b/TreeCode_link/utilities.cpp index 57ca9573..e0ef6a56 100644 --- a/TreeCode_link/utilities.cpp +++ b/TreeCode_link/utilities.cpp @@ -365,7 +365,7 @@ unsigned long prevpower(unsigned long k){ RandomNumbers_NR::RandomNumbers_NR(long seed): calls(0),IM1(2147483399),IM2(2147483399),IA1(40014),IA2(40692),IQ1(53668), IQ2(52774),IR1(12211),IR2(3791),EPS(1.2e-7),idum2(123456789),iy(0), - count(true) + count(true),firstseed(seed) { AM = (1.0/IM1); diff --git a/include/utilities_slsim.h b/include/utilities_slsim.h index 42e393ad..3dfc6833 100644 --- a/include/utilities_slsim.h +++ b/include/utilities_slsim.h @@ -995,6 +995,8 @@ namespace Utilities }; size_t calls = 0; /// total number of calls + + long getseed(){return firstseed;} private: long idum; PosType ran2(void); @@ -1018,6 +1020,7 @@ namespace Utilities long iv[32]; bool count; PosType u,v,s; + long firstseed; }; From 61dbb0d26062fe43323b02dda9ad43211fbd11a6 Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Wed, 24 Oct 2018 11:13:51 +0200 Subject: [PATCH 068/227] Fixed bug when fits is not enabled. --- ImageProcessing/observation.cpp | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/ImageProcessing/observation.cpp b/ImageProcessing/observation.cpp index c7983a89..b9a93a2a 100644 --- a/ImageProcessing/observation.cpp +++ b/ImageProcessing/observation.cpp @@ -5,6 +5,7 @@ * Author: F. Bellagamba */ +#include #include "slsimlib.h" #ifdef ENABLE_FITS @@ -425,7 +426,7 @@ void Observation::ApplyPSF(PixelMap &pmap) } else { -#ifdef ENABLE_FITS +//#ifdef ENABLE_FITS #ifdef ENABLE_FFTW // calculates normalisation of psf @@ -525,10 +526,10 @@ void Observation::ApplyPSF(PixelMap &pmap) exit(1); #endif -#else - std::cout << "Please enable the preprocessor flag ENABLE_FITS !" << std::endl; - exit(1); -#endif +//#else +// std::cout << "Please enable the preprocessor flag ENABLE_FITS !" << std::endl; +// exit(1); +//#endif } } From 9bb7e6768557779d90873111dfb6e048954689e1 Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Wed, 7 Nov 2018 17:40:21 +0100 Subject: [PATCH 069/227] Added Gadget reading function. --- CMakeLists.txt | 2 +- Miscellaneous/CMakeLists.txt | 3 + Miscellaneous/gadgetio.cpp | 357 +++++++++++++++++++++++++++++++++++ MultiPlane/particle_lens.cpp | 3 +- include/CMakeLists.txt | 1 + include/gadgetio.hpp | 50 +++++ 6 files changed, 413 insertions(+), 3 deletions(-) create mode 100644 Miscellaneous/CMakeLists.txt create mode 100644 Miscellaneous/gadgetio.cpp create mode 100644 include/gadgetio.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 0aa43a79..5375ff33 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -57,7 +57,7 @@ add_subdirectory(MultiPlane) add_subdirectory(Source) add_subdirectory(TreeCode) add_subdirectory(TreeCode_link) - +add_subdirectory(Miscellaneous) #### # dependencies diff --git a/Miscellaneous/CMakeLists.txt b/Miscellaneous/CMakeLists.txt new file mode 100644 index 00000000..aac65108 --- /dev/null +++ b/Miscellaneous/CMakeLists.txt @@ -0,0 +1,3 @@ +add_sources( + gadgetio.cpp +) diff --git a/Miscellaneous/gadgetio.cpp b/Miscellaneous/gadgetio.cpp new file mode 100644 index 00000000..7d4675a4 --- /dev/null +++ b/Miscellaneous/gadgetio.cpp @@ -0,0 +1,357 @@ +// +// gadgetio.cpp +// NR +// +// Created by bmetcalf on 07.11.18. +// + +#include "gadgetio.hpp" +#include +#include +#include + + +void load_snapshot_gadget2(char *fname + ,int nfiles + ,PosType **xp + ,std::vector &masses + ,std::vector &sizes + ,bool &multimass + ) +{ + + FILE *fd; + char buf[200]; + int i, k, dummy, ntot_withmasses; + int n, pc, pc_new, pc_sph; + gadget_header header1; + gadget_particle_data p; + int Ngas = 0; + int NumPart = 0; + int id; + + std::vector types; + + + for(i = 0, pc = 1; i < nfiles; i++, pc = pc_new) + { + if(nfiles > 1) + sprintf(buf, "%s.%d", fname, i); + else + sprintf(buf, "%s", fname); + + if(!(fd = fopen(buf, "r"))) + { + printf("can't open file `%s`\n", buf); + throw std::invalid_argument("no file"); + } + + printf("reading `%s' ...\n", buf); + fflush(stdout); + + fread(&dummy, sizeof(dummy), 1, fd); + fread(&header1, sizeof(header1), 1, fd); + fread(&dummy, sizeof(dummy), 1, fd); + + if(nfiles == 1) + { + for(k = 0, NumPart = 0, ntot_withmasses = 0; k < 6; k++) + NumPart += header1.npart[k]; + Ngas = header1.npart[0]; + } + else + { + for(k = 0, NumPart = 0, ntot_withmasses = 0; k < 6; k++) + NumPart += header1.npartTotal[k]; + Ngas = header1.npartTotal[0]; + } + + for(k = 0, ntot_withmasses = 0; k < 6; k++) + { + if(header1.mass[k] == 0) + ntot_withmasses += header1.npart[k]; + } + + if(i == 0){ + // allocate memory + try{ + //p.resize(NumPart); + //ids.resize(NumPart); + xp = Utilities::PosTypeMatrix(NumPart,3); + masses.resize(NumPart); + sizes.resize(NumPart,0); + types.resize(NumPart); + }catch (const std::bad_alloc& ba){ + std::cerr <<"ERROR: "; + std::cerr << ba.what() << std::endl; + } + } + + float pos[3]; + float v_dummy[3]; + + fread(&dummy, sizeof(dummy), 1, fd); // skip + for(k = 0, pc_new = pc; k < 6; k++) + { + for(n = 0; n < header1.npart[k]; n++) + { + fread(pos, sizeof(float), 3, fd); + xp[pc_new][0] = pos[0]; + xp[pc_new][1] = pos[0]; + xp[pc_new][2] = pos[0]; + pc_new++; + } + } + fread(&dummy, sizeof(dummy), 1, fd); // skip + + fread(&dummy, sizeof(dummy), 1, fd); // skip + for(k = 0, pc_new = pc; k < 6; k++) + { + for(n = 0; n < header1.npart[k]; n++) + { + fread(v_dummy, sizeof(float), 3, fd); + pc_new++; + } + } + fread(&dummy, sizeof(dummy), 1, fd); // skip + + + fread(&dummy, sizeof(dummy), 1, fd); // skip + for(k = 0, pc_new = pc; k < 6; k++) + { + for(n = 0; n < header1.npart[k]; n++) + { + fread(&id, sizeof(int), 1, fd); + pc_new++; + } + } + fread(&dummy, sizeof(dummy), 1, fd); // skip + + + if(ntot_withmasses > 0) + fread(&dummy, sizeof(dummy), 1, fd); // skip + for(k = 0, pc_new = pc; k < 6; k++) + { + for(n = 0; n < header1.npart[k]; n++) + { + types[pc_new] = k; + + if(header1.mass[k] == 0) + fread(&masses[pc_new], sizeof(float), 1, fd); + else + masses[pc_new] = header1.mass[k]; + pc_new++; + } + } + + if(ntot_withmasses > 0) + fread(&dummy, sizeof(dummy), 1, fd); // skip + + + float temp,rho,ne; + + if(header1.npart[0] > 0) + { + fread(&dummy, sizeof(dummy), 1, fd); // skip + for(n = 0, pc_sph = pc; n < header1.npart[0]; n++) + { + fread(&temp, sizeof(float), 1, fd); + pc_sph++; + } + fread(&dummy, sizeof(dummy), 1, fd); // skip + + fread(&dummy, sizeof(dummy), 1, fd); // skip + for(n = 0, pc_sph = pc; n < header1.npart[0]; n++) + { + fread(&rho, sizeof(float), 1, fd); + pc_sph++; + } + fread(&dummy, sizeof(dummy), 1, fd); // skip + + if(header1.flag_cooling) + { + fread(&dummy, sizeof(dummy), 1, fd); // skip + for(n = 0, pc_sph = pc; n < header1.npart[0]; n++) + { + fread(&ne, sizeof(float), 1, fd); + pc_sph++; + } + fread(&dummy, sizeof(dummy), 1, fd); // skip + } + else + for(n = 0, pc_sph = pc; n < header1.npart[0]; n++) + { + ne = 1.0; + pc_sph++; + } + } + + fclose(fd); + } +} + + +/* this routine loads particle data from Gadget's default + * binary file format. (A snapshot may be distributed + * into multiple files. + */ +void load_snapshot_gadget2(char *fname, int nfiles, std::vector &p + ,std::vector ids,gadget_header &header1 + ,int &NumPart,int &Ngas + ) +{ + FILE *fd; + char buf[200]; + int i, k, dummy, ntot_withmasses; + int n, pc, pc_new, pc_sph; + + + for(i = 0, pc = 1; i < nfiles; i++, pc = pc_new) + { + if(nfiles > 1) + sprintf(buf, "%s.%d", fname, i); + else + sprintf(buf, "%s", fname); + + if(!(fd = fopen(buf, "r"))) + { + printf("can't open file `%s`\n", buf); + throw std::invalid_argument("no file"); + } + + printf("reading `%s' ...\n", buf); + fflush(stdout); + + fread(&dummy, sizeof(dummy), 1, fd); + fread(&header1, sizeof(header1), 1, fd); + fread(&dummy, sizeof(dummy), 1, fd); + + if(nfiles == 1) + { + for(k = 0, NumPart = 0, ntot_withmasses = 0; k < 6; k++) + NumPart += header1.npart[k]; + Ngas = header1.npart[0]; + } + else + { + for(k = 0, NumPart = 0, ntot_withmasses = 0; k < 6; k++) + NumPart += header1.npartTotal[k]; + Ngas = header1.npartTotal[0]; + } + + for(k = 0, ntot_withmasses = 0; k < 6; k++) + { + if(header1.mass[k] == 0) + ntot_withmasses += header1.npart[k]; + } + + if(i == 0){ + // allocate memory + try{ + p.resize(NumPart); + ids.resize(NumPart); + }catch (const std::bad_alloc& ba){ + std::cerr <<"ERROR: "; + std::cerr << ba.what() << std::endl; + } + } + + + gadget_particle_data *P = p.data() - 1; + int *Id = ids.data() - 1; + + fread(&dummy, sizeof(dummy), 1, fd); // skip + for(k = 0, pc_new = pc; k < 6; k++) + { + for(n = 0; n < header1.npart[k]; n++) + { + fread(&P[pc_new].Pos[0], sizeof(float), 3, fd); + pc_new++; + } + } + fread(&dummy, sizeof(dummy), 1, fd); // skip + + fread(&dummy, sizeof(dummy), 1, fd); // skip + for(k = 0, pc_new = pc; k < 6; k++) + { + for(n = 0; n < header1.npart[k]; n++) + { + fread(&P[pc_new].Vel[0], sizeof(float), 3, fd); + pc_new++; + } + } + fread(&dummy, sizeof(dummy), 1, fd); // skip + + + fread(&dummy, sizeof(dummy), 1, fd); // skip + for(k = 0, pc_new = pc; k < 6; k++) + { + for(n = 0; n < header1.npart[k]; n++) + { + fread(&Id[pc_new], sizeof(int), 1, fd); + pc_new++; + } + } + fread(&dummy, sizeof(dummy), 1, fd); // skip + + + if(ntot_withmasses > 0) + fread(&dummy, sizeof(dummy), 1, fd); // skip + for(k = 0, pc_new = pc; k < 6; k++) + { + for(n = 0; n < header1.npart[k]; n++) + { + P[pc_new].Type = k; + + if(header1.mass[k] == 0) + fread(&P[pc_new].Mass, sizeof(float), 1, fd); + else + P[pc_new].Mass = header1.mass[k]; + pc_new++; + } + } + + if(ntot_withmasses > 0) + fread(&dummy, sizeof(dummy), 1, fd); // skip + + + if(header1.npart[0] > 0) + { + fread(&dummy, sizeof(dummy), 1, fd); // skip + for(n = 0, pc_sph = pc; n < header1.npart[0]; n++) + { + fread(&P[pc_sph].U, sizeof(float), 1, fd); + pc_sph++; + } + fread(&dummy, sizeof(dummy), 1, fd); // skip + + fread(&dummy, sizeof(dummy), 1, fd); // skip + for(n = 0, pc_sph = pc; n < header1.npart[0]; n++) + { + fread(&P[pc_sph].Rho, sizeof(float), 1, fd); + pc_sph++; + } + fread(&dummy, sizeof(dummy), 1, fd); // skip + + if(header1.flag_cooling) + { + fread(&dummy, sizeof(dummy), 1, fd); // skip + for(n = 0, pc_sph = pc; n < header1.npart[0]; n++) + { + fread(&P[pc_sph].Ne, sizeof(float), 1, fd); + pc_sph++; + } + fread(&dummy, sizeof(dummy), 1, fd); // skip + } + else + for(n = 0, pc_sph = pc; n < header1.npart[0]; n++) + { + P[pc_sph].Ne = 1.0; + pc_sph++; + } + } + + fclose(fd); + } +} + diff --git a/MultiPlane/particle_lens.cpp b/MultiPlane/particle_lens.cpp index be4203d6..9774193b 100644 --- a/MultiPlane/particle_lens.cpp +++ b/MultiPlane/particle_lens.cpp @@ -122,7 +122,6 @@ void LensHaloParticles::readPositionFileASCII(const std::string &filename){ std::cerr << filename << " should have four columns!" << std::endl; } - std::ifstream myfile(filename); // find number of particles @@ -422,4 +421,4 @@ void LensHaloParticles::makeSIE( } datafile.close(); -} \ No newline at end of file +} diff --git a/include/CMakeLists.txt b/include/CMakeLists.txt index 36ac5378..ba339e68 100644 --- a/include/CMakeLists.txt +++ b/include/CMakeLists.txt @@ -49,4 +49,5 @@ add_sources( concave_hull.h bands_etc.h gaussian.h + gadgetio.hpp ) diff --git a/include/gadgetio.hpp b/include/gadgetio.hpp new file mode 100644 index 00000000..85fc0373 --- /dev/null +++ b/include/gadgetio.hpp @@ -0,0 +1,50 @@ +// +// gadgetio.hpp +// NR +// +// Created by bmetcalf on 07.11.18. +// + +#ifndef gadgetio_hpp +#define gadgetio_hpp + +#include + +struct gadget_header +{ + int npart[6]; + double mass[6]; + double time; + double redshift; + int flag_sfr; + int flag_feedback; + int npartTotal[6]; + int flag_cooling; + int num_files; + double BoxSize; + double Omega0; + double OmegaLambda; + double HubbleParam; + char fill[256 - 6 * 4 - 6 * 8 - 2 * 8 - 2 * 4 - 6 * 4 - 2 * 4 - 4 * 8]; /* fills to 256 Bytes */ +}; + +struct gadget_particle_data +{ + float Pos[3]; + float Vel[3]; + float Mass; + int Type; + + float Rho, U, Temp, Ne; +}; + +void load_snapshot_gadget2(char *fname /// base name of snapshot + ,int nfiles /// number of files in snapshot + ,std::vector &p /// particle information + ,std::vector IDs /// list of ID numbers in order + ,gadget_header &header1 /// header information from file + ,int &NumPart /// total number of particles + ,int &Ngas /// number of gas particles + ); + +#endif /* gadgetio_hpp */ From af8607aabac60abd46503d6771904b244042be06 Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Sat, 10 Nov 2018 21:07:01 +0100 Subject: [PATCH 070/227] Much work on making a better TreeQuadParticle and TreeQuadHalo. Several classes where made into templates to improve flexibility. --- AnalyticNSIE/base_analens.cpp | 3 +- AnalyticNSIE/readfiles_uni.cpp | 2 +- FullRange/implant_stars.cpp | 28 +- Miscellaneous/CMakeLists.txt | 3 +- Miscellaneous/gadgetio.cpp | 50 +- MultiPlane/lens_halos.cpp | 2 +- MultiPlane/particle_lens.cpp | 176 ++-- MultiPlane/planes.cpp | 6 +- TreeCode/CMakeLists.txt | 1 - TreeCode/qTreeNB.cpp | 377 --------- TreeCode/quadTree.cpp | 1063 +++++++++++------------ TreeCode/simpleTree.cpp | 103 ++- TreeCode_link/utilities.cpp | 2 +- include/CMakeLists.txt | 4 +- include/TreeNB.h | 33 - include/base_analens.h | 3 +- include/gadgetio.hpp | 5 + include/grid_maintenance.h | 1 + include/lens.h | 2 - include/lens_halos.h | 30 +- include/particle_halo.h | 94 ++- include/planes.h | 3 +- include/qTreeNB.h | 545 ++++++++++++ include/quadTree.h | 1437 ++++++++++++++++++++++++++------ include/simpleTree.h | 11 +- include/uniform_lens.h | 2 +- include/utilities_slsim.h | 4 +- 27 files changed, 2546 insertions(+), 1444 deletions(-) delete mode 100644 TreeCode/qTreeNB.cpp delete mode 100644 include/TreeNB.h create mode 100644 include/qTreeNB.h diff --git a/AnalyticNSIE/base_analens.cpp b/AnalyticNSIE/base_analens.cpp index d7c5e265..5b23bb62 100644 --- a/AnalyticNSIE/base_analens.cpp +++ b/AnalyticNSIE/base_analens.cpp @@ -436,9 +436,8 @@ LensHaloBaseNSIE::~LensHaloBaseNSIE(){ } if(stars_N > 0 && stars_implanted){ // std::cout << "deleting stars" << endl; - delete[] star_masses; + //delete[] star_masses; delete[] stars; - Utilities::free_PosTypeMatrix(stars_xp,stars_N,3); delete[] star_region; delete[] star_Sigma; Utilities::free_PosTypeMatrix(star_xdisk,star_Nregions,2); diff --git a/AnalyticNSIE/readfiles_uni.cpp b/AnalyticNSIE/readfiles_uni.cpp index 0770196c..ba271d6a 100644 --- a/AnalyticNSIE/readfiles_uni.cpp +++ b/AnalyticNSIE/readfiles_uni.cpp @@ -210,7 +210,7 @@ void LensHaloUniform::assignParams(InputParams& params){ /** \ingroup ImageFinding * \brief Prints the parameters of the analytic lens to stdout */ -void LensHaloUniform::PrintLens(bool show_substruct,bool show_stars) const +void LensHaloUniform::PrintLens(bool show_substruct,bool show_stars) { // uni lens parameters only cout << endl << "**Host lens model**" << endl; diff --git a/FullRange/implant_stars.cpp b/FullRange/implant_stars.cpp index 5662cca2..be57ea04 100644 --- a/FullRange/implant_stars.cpp +++ b/FullRange/implant_stars.cpp @@ -8,6 +8,9 @@ #include "slsimlib.h" using namespace std; + + + /** \ingroup ChangeLens * \brief Implant or randomize stars into the lens around the images. * @@ -35,9 +38,9 @@ void LensHalo::implant_stars( if(star_fstars > 1.0){ std::printf("fstars > 1.0\n"); exit(0); } if(!(stars_implanted) ){ - star_masses = new float[stars_N]; + //star_masses = new float[stars_N]; stars = new unsigned long[stars_N]; - stars_xp = Utilities::PosTypeMatrix(stars_N,3); + stars_xp.resize(stars_N); star_theta_force = 1.0e-1; assert(Nregions > 0); star_Nregions = Nregions; @@ -66,7 +69,15 @@ void LensHalo::implant_stars( //params.get("main_sub_mass_max",sub_Mmax) NstarsPerImage = stars_N/star_Nregions; - star_masses=stellar_mass_function(type, stars_N, seed, main_stars_min_mass, main_stars_max_mass, bend_mstar,lo_mass_slope,hi_mass_slope); + stars_xp.resize(stars_N); + { + std::vector star_masses=stellar_mass_function(type, stars_N, seed, main_stars_min_mass, main_stars_max_mass, bend_mstar,lo_mass_slope,hi_mass_slope); + + for(int i=0; i(stars_xp.data(),stars_N ,false,false,0,4,star_theta_force); // visit every branch to find center of mass and cutoff scale */ @@ -194,9 +205,7 @@ void LensHalo::remove_stars(){ if(stars_implanted){ delete star_tree; - delete star_masses; delete stars; - delete stars_xp; delete star_region; delete star_Sigma; Utilities::free_PosTypeMatrix(star_xdisk,star_Nregions,2); @@ -257,14 +266,15 @@ void LensHalo::substract_stars_disks(PosType const *ray,PosType *alpha * 2 - broken power law, requires lower mass end slope (powerlo), high mass slope (powerhi), bending point (bendmass) * 3 - further IMF models may follow */ -float* LensHalo::stellar_mass_function(IMFtype type, unsigned long Nstars, long *seed +std::vector LensHalo::stellar_mass_function(IMFtype type, unsigned long Nstars, long *seed ,PosType minmass, PosType maxmass, PosType bendmass, PosType powerlo, PosType powerhi){ //if(!(stars_implanted)) return; unsigned long i; PosType powerp1,powerp2,powerp3,powerlp1,bendmass1,bendmass2,shiftmax,shiftmin,n0,n1,n2,n3,rndnr,tmp0,tmp1,tmp2; - float *stellar_masses = new float[Nstars]; + std::vector stellar_masses(Nstars); + if(type==One){ for(i = 0; i < Nstars; i++){ stellar_masses[i]=1.0; diff --git a/Miscellaneous/CMakeLists.txt b/Miscellaneous/CMakeLists.txt index aac65108..22b22795 100644 --- a/Miscellaneous/CMakeLists.txt +++ b/Miscellaneous/CMakeLists.txt @@ -1,3 +1,4 @@ add_sources( - gadgetio.cpp + gadget.cp + gensim.cp ) diff --git a/Miscellaneous/gadgetio.cpp b/Miscellaneous/gadgetio.cpp index 7d4675a4..f44506bf 100644 --- a/Miscellaneous/gadgetio.cpp +++ b/Miscellaneous/gadgetio.cpp @@ -5,18 +5,19 @@ // Created by bmetcalf on 07.11.18. // -#include "gadgetio.hpp" #include #include #include +#include "utilities.h" +//#include "utilities_slsim.h" +#include "particle_halo.h" +#include "gadgetio.hpp" void load_snapshot_gadget2(char *fname ,int nfiles - ,PosType **xp - ,std::vector &masses - ,std::vector &sizes - ,bool &multimass + ,std::vector > &xp + ,std::vector &ntypes ) { @@ -25,14 +26,12 @@ void load_snapshot_gadget2(char *fname int i, k, dummy, ntot_withmasses; int n, pc, pc_new, pc_sph; gadget_header header1; - gadget_particle_data p; int Ngas = 0; int NumPart = 0; int id; std::vector types; - for(i = 0, pc = 1; i < nfiles; i++, pc = pc_new) { if(nfiles > 1) @@ -77,10 +76,11 @@ void load_snapshot_gadget2(char *fname try{ //p.resize(NumPart); //ids.resize(NumPart); - xp = Utilities::PosTypeMatrix(NumPart,3); - masses.resize(NumPart); - sizes.resize(NumPart,0); - types.resize(NumPart); + //xp = Utilities::PosTypeMatrix(NumPart,3); + xp.resize(NumPart); + //masses.resize(NumPart); + //sizes.resize(NumPart,0); + //types.resize(NumPart); }catch (const std::bad_alloc& ba){ std::cerr <<"ERROR: "; std::cerr << ba.what() << std::endl; @@ -97,8 +97,8 @@ void load_snapshot_gadget2(char *fname { fread(pos, sizeof(float), 3, fd); xp[pc_new][0] = pos[0]; - xp[pc_new][1] = pos[0]; - xp[pc_new][2] = pos[0]; + xp[pc_new][1] = pos[1]; + xp[pc_new][2] = pos[2]; pc_new++; } } @@ -134,12 +134,12 @@ void load_snapshot_gadget2(char *fname { for(n = 0; n < header1.npart[k]; n++) { - types[pc_new] = k; + xp[pc_new].type = k; if(header1.mass[k] == 0) - fread(&masses[pc_new], sizeof(float), 1, fd); + fread(&xp[pc_new].mass, sizeof(float), 1, fd); else - masses[pc_new] = header1.mass[k]; + xp[pc_new].mass = header1.mass[k]; pc_new++; } } @@ -188,6 +188,24 @@ void load_snapshot_gadget2(char *fname fclose(fd); } + + // sort by type + std::sort(xp.begin(),xp.end(),[](ParticleData &p1,ParticleData &p2){return p1.type < p2.type;}); + + ntypes.resize(0); + if(nfiles == 1) + { + for(k = 0 ; k < 6; k++){ + if(header1.npart[k] > 0) ntypes.push_back(header1.npart[k]); + } + } + else + { + for(k = 0 ; k < 6; k++){ + if(header1.npartTotal[k] > 0) ntypes.push_back(header1.npartTotal[k]); + } + } + } diff --git a/MultiPlane/lens_halos.cpp b/MultiPlane/lens_halos.cpp index af009a3a..5ab52652 100644 --- a/MultiPlane/lens_halos.cpp +++ b/MultiPlane/lens_halos.cpp @@ -68,7 +68,7 @@ void LensHalo::assignParams(InputParams& params,bool needRsize){ if(!params.get("main_zlens",zlens)) error_message1("main_zlens",params.filename()); } -void LensHalo::PrintStars(bool show_stars) const +void LensHalo::PrintStars(bool show_stars) { std::cout << std::endl << "Nstars "<0){ diff --git a/MultiPlane/particle_lens.cpp b/MultiPlane/particle_lens.cpp index 9774193b..7a149ee2 100644 --- a/MultiPlane/particle_lens.cpp +++ b/MultiPlane/particle_lens.cpp @@ -9,16 +9,18 @@ using namespace CCfits; #endif -LensHaloParticles::LensHaloParticles( - const std::string& simulation_filename - ,PosType redshift /// redshift of origin - ,int Nsmooth /// number of neighbours for adaptive smoothing - ,const COSMOLOGY& cosmo /// cosmology - ,Point_2d theta_rotate /// rotation of particles around the origin - ,bool recenter - ,bool my_multimass /// Set to true is particles have different sizes - ,PosType MinPSize - ):min_size(MinPSize),multimass(my_multimass),simfile(simulation_filename) +template +LensHaloParticles::LensHaloParticles(const std::string& simulation_filename + ,SimFileFormats format + ,PosType redshift + ,int Nsmooth + ,const COSMOLOGY& cosmo + ,Point_2d theta_rotate + ,bool recenter + ,bool my_multimass + ,PosType MinPSize + ) +:min_size(MinPSize),multimass(my_multimass),simfile(simulation_filename) { LensHalo::setZlens(redshift); @@ -31,14 +33,25 @@ LensHaloParticles::LensHaloParticles( Rmax = 1.0e3; LensHalo::setRsize(Rmax); - readPositionFileASCII(simulation_filename); + switch (format) { + case ascii: + readPositionFileASCII(simulation_filename,multimass,xxp); + break; + default: + std::cerr << "LensHaloParticles does not accept ." << std::endl; + throw std::invalid_argument("bad format"); + } sizefile = simfile + "." + std::to_string(Nsmooth) + "sizes"; if(!readSizesFile(sizefile,Nsmooth,min_size)){ // calculate sizes - sizes.resize(Npoints); - calculate_smoothing(Nsmooth); - for(size_t i=0; i max_mass) ? masses[multimass*i] : max_mass; - min_mass = (masses[multimass*i] < min_mass) ? masses[multimass*i] : min_mass; + mass += xxp[multimass*i].mass(); + + max_mass = (xxp[multimass*i].mass() > max_mass) ? xxp[multimass*i].mass() : max_mass; + min_mass = (xxp[multimass*i].mass() < min_mass) ? xxp[multimass*i].mass() : min_mass; } LensHalo::setMass(mass); @@ -69,11 +82,11 @@ LensHaloParticles::LensHaloParticles( if(recenter){ PosType r2,r2max=0; for(size_t i=0;i r2max) r2max = r2; } @@ -83,15 +96,17 @@ LensHaloParticles::LensHaloParticles( // rotate positions rotate_particles(theta_rotate[0],theta_rotate[1]); - qtree = new TreeQuad(xp,masses.data(),sizes.data(),Npoints,multimass,true,0,20); + qtree = new TreeQuadParticles >(xxp.data(),xxp.size(),-1,-1,0,20); } -LensHaloParticles::~LensHaloParticles(){ +template +LensHaloParticles::~LensHaloParticles(){ delete qtree; - Utilities::free_PosTypeMatrix(xp,Npoints,3); } -void LensHaloParticles::force_halo(double *alpha,KappaType *kappa,KappaType *gamma,KappaType *phi,double const *xcm +template +void LensHaloParticles::force_halo(double *alpha,KappaType *kappa,KappaType *gamma,KappaType *phi + ,double const *xcm ,bool subtract_point,PosType screening){ qtree->force2D_recur(xcm,alpha,kappa,gamma,phi); @@ -99,10 +114,11 @@ void LensHaloParticles::force_halo(double *alpha,KappaType *kappa,KappaType *gam alpha[1] *= -1; } -void LensHaloParticles::rotate(Point_2d theta){ +template +void LensHaloParticles::rotate(Point_2d theta){ rotate_particles(theta[0],theta[1]); delete qtree; - qtree =new TreeQuad(xp,masses.data(),sizes.data(),Npoints,multimass,true,0,20); + qtree =new TreeQuadParticles >(xxp.data(),Npoints,multimass,true,0,20); } /** \brief Reads number of particle and particle positons into Npoint and xp from a ASCII file. @@ -112,7 +128,11 @@ void LensHaloParticles::rotate(Point_2d theta){ * * Coordinates of particles are in physical Mpc units. */ -void LensHaloParticles::readPositionFileASCII(const std::string &filename){ +template +void LensHaloParticles::readPositionFileASCII(const std::string &filename + ,bool multimass + ,std::vector &xxp + ){ int ncoll = Utilities::IO::CountColumns(filename); if(!multimass && ncoll != 3 ){ @@ -124,6 +144,8 @@ void LensHaloParticles::readPositionFileASCII(const std::string &filename){ std::ifstream myfile(filename); + size_t Npoints = 0; + // find number of particles if (myfile.is_open()){ @@ -159,9 +181,10 @@ void LensHaloParticles::readPositionFileASCII(const std::string &filename){ throw std::runtime_error("file reading error"); } - xp = Utilities::PosTypeMatrix(Npoints,3); - if(multimass) masses.resize(Npoints); - else masses.push_back(tmp_mass); + xxp.resize(Npoints); + //xp = Utilities::PosTypeMatrix(Npoints,3); + //if(multimass) masses.resize(Npoints); + //else masses.push_back(tmp_mass); size_t row = 0; @@ -171,10 +194,10 @@ void LensHaloParticles::readPositionFileASCII(const std::string &filename){ if(str[0] == '#') continue; //for comments std::stringstream ss(str); - ss >> xp[row][0]; - if(!(ss >> xp[row][1])) std::cerr << "3 columns are expected in line " << row + ss >> xxp[row][0]; + if(!(ss >> xxp[row][1])) std::cerr << "3 columns are expected in line " << row << " of " << filename << std::endl; - if(!(ss >> xp[row][2])) std::cerr << "3 columns are expected in line " << row + if(!(ss >> xxp[row][2])) std::cerr << "3 columns are expected in line " << row << " of " << filename << std::endl; row++; @@ -184,12 +207,12 @@ void LensHaloParticles::readPositionFileASCII(const std::string &filename){ if(str[0] == '#') continue; //for comments std::stringstream ss(str); - ss >> xp[row][0]; - if(!(ss >> xp[row][1])) std::cerr << "4 columns are expected in line " << row + ss >> xxp[row][0]; + if(!(ss >> xxp[row][1])) std::cerr << "4 columns are expected in line " << row << " of " << filename << std::endl; - if(!(ss >> xp[row][2])) std::cerr << "4 columns are expected in line " << row + if(!(ss >> xxp[row][2])) std::cerr << "4 columns are expected in line " << row << " of " << filename << std::endl; - if(!(ss >> masses[row])) std::cerr << "4 columns are expected in line " << row + if(!(ss >> xxp[row].Mass)) std::cerr << "4 columns are expected in line " << row << " of " << filename << std::endl; row++; @@ -210,7 +233,8 @@ void LensHaloParticles::readPositionFileASCII(const std::string &filename){ } -bool LensHaloParticles::readSizesFile(const std::string &filename,int Nsmooth +template +bool LensHaloParticles::readSizesFile(const std::string &filename,int Nsmooth ,PosType min_size){ std::ifstream myfile(filename); @@ -253,8 +277,6 @@ bool LensHaloParticles::readSizesFile(const std::string &filename,int Nsmooth throw std::runtime_error("file reading error"); } - sizes.resize(Npoints); - size_t row = 0; std::cout << "reading in particle sizes from " << filename << "..." << std::endl; @@ -264,10 +286,10 @@ bool LensHaloParticles::readSizesFile(const std::string &filename,int Nsmooth if(str[0] == '#') continue; //for comments std::stringstream ss(str); - ss >> sizes[row]; - if(min_size > sizes[row] ) sizes[row] = min_size; - min = min < sizes[row] ? min : sizes[row]; - max = max > sizes[row] ? max : sizes[row]; + ss >> xxp[row].Size; + if(min_size > xxp[row].size() ) xxp[row].Size = min_size; + min = min < xxp[row].size() ? min : xxp[row].size(); + max = max > xxp[row].size() ? max : xxp[row].size(); row++; } @@ -286,7 +308,8 @@ bool LensHaloParticles::readSizesFile(const std::string &filename,int Nsmooth return true; } -void LensHaloParticles::rotate_particles(PosType theta_x,PosType theta_y){ +template +void LensHaloParticles::rotate_particles(PosType theta_x,PosType theta_y){ if(theta_x == 0.0 && theta_y == 0.0) return; @@ -306,26 +329,31 @@ void LensHaloParticles::rotate_particles(PosType theta_x,PosType theta_y){ for(size_t i=0;i +void LensHaloParticles::calculate_smoothing(int Nsmooth,std::vector &xxp + ){ + std::cout << "Calculating smoothing of particles ..." << std::endl << Nsmooth << " neighbors. If there are a lot of particles this could take a while." << std::endl; + size_t Npoints = xxp.size(); + // make 3d tree of particle postions - TreeSimple tree3d(xp,Npoints,10,3,true); + TreeSimple tree3d(xxp,Npoints,10,3,true); // find distance to nth neighbour for every particle if(Npoints < 1000){ IndexType neighbors[Nsmooth]; for(size_t i=0;i +void LensHaloParticles::smooth_(TreeSimple *tree3d,PType *xp,size_t N,int Nsmooth){ IndexType neighbors[Nsmooth]; for(size_t i=0;iNearestNeighbors(xp[i],Nsmooth,sizesp + i,neighbors); + //tree3d->NearestNeighbors(&xp[i][0],Nsmooth,sizesp + i,neighbors); + tree3d->NearestNeighbors(&xp[i][0],Nsmooth,&(xp[i].Size),neighbors); } } -void LensHaloParticles::writeSizes(const std::string &filename,int Nsmooth){ +template +void LensHaloParticles::writeSizes(const std::string &filename,int Nsmooth + ,const std::vector &xxp +){ std::ofstream myfile(filename); @@ -366,10 +398,11 @@ void LensHaloParticles::writeSizes(const std::string &filename,int Nsmooth){ std::cout << "Writing particle size information to file " << filename << " ...." << std::endl; - myfile << "# nparticles " << Npoints << std::endl; + myfile << "# nparticles " << xxp.size() << std::endl; myfile << "# nsmooth " << Nsmooth << std::endl; + size_t Npoints = xxp.size(); for(size_t i=0;i +void LensHaloParticles::makeSIE( std::string new_filename /// file name ,PosType redshift /// redshift of particles ,double particle_mass /// particle mass diff --git a/MultiPlane/planes.cpp b/MultiPlane/planes.cpp index 6339d7c5..0451784e 100644 --- a/MultiPlane/planes.cpp +++ b/MultiPlane/planes.cpp @@ -6,6 +6,7 @@ */ #include "planes.h" +#include "quadTree.h" #include @@ -13,10 +14,11 @@ LensPlaneTree::LensPlaneTree(LensHaloHndl *my_halos,IndexType Nhalos ,PosType my_sigma_background,PosType inv_screening_scale) : LensPlane(), halos(my_halos, my_halos + Nhalos) { - if(inv_screening_scale != 0) halo_tree = new TreeQuad(my_halos,Nhalos,my_sigma_background,5,0.1,true,inv_screening_scale); - else halo_tree = new TreeQuad(my_halos,Nhalos,my_sigma_background); + if(inv_screening_scale != 0) halo_tree = new TreeQuadHalos(my_halos,Nhalos,my_sigma_background,5,0.1,true,inv_screening_scale); + else halo_tree = new TreeQuadHalos(my_halos,Nhalos,my_sigma_background); } + LensPlaneTree::~LensPlaneTree(){ delete halo_tree; } diff --git a/TreeCode/CMakeLists.txt b/TreeCode/CMakeLists.txt index 18f6f7a7..bd678406 100644 --- a/TreeCode/CMakeLists.txt +++ b/TreeCode/CMakeLists.txt @@ -1,5 +1,4 @@ add_sources( - qTreeNB.cpp quadTree.cpp simpleTree.cpp ) diff --git a/TreeCode/qTreeNB.cpp b/TreeCode/qTreeNB.cpp deleted file mode 100644 index 330cdb1e..00000000 --- a/TreeCode/qTreeNB.cpp +++ /dev/null @@ -1,377 +0,0 @@ -/* - * qTreeNB.cpp - * - * Created on: Jun 6, 2012 - * Author: bmetcalf - */ - -#include "slsimlib.h" - -QBranchNB::QBranchNB(){ - static unsigned long n=0; - child0 = NULL; - child1 = NULL; - child2 = NULL; - child3 = NULL; - prev = NULL; - brother = NULL; - particles = NULL; - big_particles = NULL; - nparticles = 0; - number = n; - ++n; -} -QBranchNB::~QBranchNB(){}; - - -/************************************************************************ - * This constructor just creates a root branch. To construct a full tree - * an external function must be used. For example, TreeQuad uses QTreeNB - ************************************************************************/ -QTreeNB::QTreeNB(PosType **xp,IndexType *particles,IndexType nparticles - ,PosType boundary_p1[],PosType boundary_p2[]): xp(xp) { - - top = new QBranchNB(); - - top->boundary_p1[0] = boundary_p1[0]; - top->boundary_p1[1] = boundary_p1[1]; - top->boundary_p2[0] = boundary_p2[0]; - top->boundary_p2[1] = boundary_p2[1]; - - top->nparticles = nparticles; - top->level = 0; - top->particles = particles; - - top->center[0] = (boundary_p1[0] + boundary_p2[0])/2; - top->center[1] = (boundary_p1[1] + boundary_p2[1])/2; - - Nbranches = 1; - current = top; - -} - -/// Free treeNB. Does not free the particle positions, masses or sizes -QTreeNB::~QTreeNB(){ -// void TreeQuad::freeQTreeNB(QTreeNBHndl tree){ - - empty(); - delete top; - - return; -} - -short QTreeNB::empty(){ - - if(Nbranches <= 1) return 1; - moveTop(); - _freeQTree(0); - - assert(Nbranches == 1); - - return 1; -} - -void QTreeNB::_freeQTree(short child){ - QBranchNB *branch; - - assert(current); - if(current->particles != current->big_particles - && current->big_particles != NULL) delete[] current->big_particles; - - if(current->child0 != NULL){ - moveToChild(0); - _freeQTree(0); - } - - if(current->child1 != NULL){ - moveToChild(1); - _freeQTree(1); - } - - if(current->child2 != NULL){ - moveToChild(2); - _freeQTree(2); - } - - if(current->child3 != NULL){ - moveToChild(3); - _freeQTree(3); - } - - if( atLeaf(current) ){ - - if(atTop()) return; - - branch = current; - moveUp(); - delete branch; - - /*printf("*** removing branch %i number of branches %i\n",branch->number - ,Nbranches-1);*/ - - if(child==0) current->child0 = NULL; - if(child==1) current->child1 = NULL; - if(child==2) current->child2 = NULL; - if(child==3) current->child3 = NULL; - - --Nbranches; - - return; - } - - return; -} - -/************************************************************************ - * isEmpty - * Returns "true" if the QTreeNB is empty and "false" otherwise. Exported. - ************************************************************************/ -const bool QTreeNB::isEmpty(){ - - return(Nbranches == 0); -} - -/************************************************************************ - * atTop - * Returns "true" if current is the same as top and "false" otherwise. - * Exported. - * Pre: !isEmptyNB(tree) - ************************************************************************/ -const bool QTreeNB::atTop(){ - - if( isEmpty() ){ - ERROR_MESSAGE(); - std::cout << "QTreeNB Error: calling atTop() on empty tree" << std::endl; - exit(1); - } - return(current == top); -} - -/************************************************************************ - * offEndNB - * Returns "true" if current is off end and "false" otherwise. Exported. - ************************************************************************/ -const bool QTreeNB::offEnd(){ - - return(current == NULL); -} - -/************************************************************************ - * getCurrentNB - * Returns the particles of current. Exported. - * Pre: !offEnd() - ************************************************************************/ -void QTreeNB::getCurrent(IndexType *particles,IndexType *nparticles){ - - if( offEnd() ){ - ERROR_MESSAGE(); - std::cout << "QTreeNB Error: calling getCurrent() when current is off end" << std::endl; - exit(1); - } - - *nparticles = current->nparticles; - particles = current->particles; - - return; -} - -/************************************************************************ - * getNbranches - * Returns the NbranchNBes of tree. Exported. - ************************************************************************/ -const unsigned long QTreeNB::getNbranches(){ - - return(Nbranches); -} - -/***** Manipulation procedures *****/ - -/************************************************************************ - * moveTop - * Moves current to the front of tree. Exported. - * Pre: !isEmpty(tree) - ************************************************************************/ -void QTreeNB::moveTop(){ - //std::cout << tree << std::endl; - //std::cout << tree->current << std::endl; - //std::cout << tree->top << std::endl; - - if( isEmpty() ){ - ERROR_MESSAGE(); - std::cout << "QTreeNB Error: calling moveTop() on empty tree" << std::endl; - exit(1); - } - - current = top; - - return; -} - -/************************************************************************ - * movePrev - * Moves current to the branchNB before it in tree. This can move current - * off end. Exported. - * Pre: !offEndNB(tree) - ************************************************************************/ -void QTreeNB::moveUp(){ - - if( offEnd() ){ - ERROR_MESSAGE(); - std::cout << "QTreeNB Error: call to moveUp() when current is off end" << std::endl; - exit(1); - } - if( current == top ){ - ERROR_MESSAGE(); - std::cout << "QTreeNB Error: call to moveUp() tried to move off the top" << std::endl; - exit(1); - } - - current = current->prev; /* can move off end */ - return; -} - -/************************************************************************ - * moveToChild - * Moves current to child branchNB after it in tree. This can move current off - * end. Exported. - * Pre: !offEnd(tree) - ************************************************************************/ -void QTreeNB::moveToChild(int child){ - - if( offEnd() ){ - ERROR_MESSAGE(); std::cout << "QTreeNB Error: calling moveChildren() when current is off end" << std::endl; - exit(1); - } - if(child==0){ - if( current->child0 == NULL ){ - ERROR_MESSAGE(); std::cout << "QTreeNB Error: moveToChild() typing to move to child1 when it doesn't exist" << std::endl; - exit(1); - } - current = current->child0; - } - if(child==1){ - if( current->child1 == NULL ){ - ERROR_MESSAGE(); std::cout << "QTreeNB Error: moveToChild() typing to move to child1 when it doesn't exist" << std::endl; - exit(1); - } - current = current->child1; - } - if(child==2){ - if( current->child2 == NULL ){ - ERROR_MESSAGE(); std::cout << "QTreeNB Error: moveToChild() typing to move to child2 when it doesn't exist" << std::endl; - exit(1); - } - current = current->child2; - } - if(child==3){ - if( current->child3 == NULL ){ - ERROR_MESSAGE(); std::cout << "QTreeNB Error: moveToChild() typing to move to child2 when it doesn't exist" << std::endl; - exit(1); - } - current = current->child3; - } - return; -} - - -void QTreeNB::attachChildrenToCurrent(QBranchNB *branch0,QBranchNB *branch1 - ,QBranchNB *branch2,QBranchNB *branch3){ - - Nbranches += 4; - current->child0 = branch0; - current->child1 = branch1; - current->child2 = branch2; - current->child3 = branch3; - - int level = current->level+1; - branch0->level = level; - branch1->level = level; - branch2->level = level; - branch3->level = level; - - // work out brothers for children - branch0->brother = branch1; - branch1->brother = branch2; - branch2->brother = branch3; - branch3->brother = current->brother; - - branch0->prev = current; - branch1->prev = current; - branch2->prev = current; - branch3->prev = current; - - return; -} - -// step for walking tree by iteration instead of recursion -bool QTreeNB::WalkStep(bool allowDescent){ - if(allowDescent && current->child0 != NULL){ - moveToChild(0); - return true; - } - - if(current->brother != NULL){ - current=current->brother; - return true; - } - return false; -} - -bool QTreeNB::iterator::up(){ - if( current == top ){ - return false; - } - current = current->prev; /* can move off end */ - return true; -} - -/// Move to child -bool QTreeNB::iterator::down(short child){ - - if(child==0){ - if( current->child0 == NULL ){ - ERROR_MESSAGE(); std::cout << "QTreeNB Error: moveToChild() typing to move to child1 when it doesn't exist" << std::endl; - return false; - } - current = current->child0; - return true; - } - if(child==1){ - if( current->child1 == NULL ){ - ERROR_MESSAGE(); std::cout << "QTreeNB Error: moveToChild() typing to move to child1 when it doesn't exist" << std::endl; - return false; - } - current = current->child1; - return true; - } - if(child==2){ - if( current->child2 == NULL ){ - ERROR_MESSAGE(); std::cout << "QTreeNB Error: moveToChild() typing to move to child2 when it doesn't exist" << std::endl; - return false; - } - current = current->child2; - return true; - } - if(child==3){ - if( current->child3 == NULL ){ - ERROR_MESSAGE(); std::cout << "QTreeNB Error: moveToChild() typing to move to child2 when it doesn't exist" << std::endl; - return false; - } - current = current->child3; - return true; - } - return true; -} - -bool QTreeNB::iterator::TreeWalkStep(bool allowDescent){ - if(allowDescent && current->child0 != NULL){ - down(0); - return true; - } - - if(current->brother != NULL){ - current=current->brother; - return true; - } - return false; -} diff --git a/TreeCode/quadTree.cpp b/TreeCode/quadTree.cpp index 0a585890..140ce529 100644 --- a/TreeCode/quadTree.cpp +++ b/TreeCode/quadTree.cpp @@ -12,99 +12,63 @@ #include "slsimlib.h" #include "Tree.h" -/** \brief Constructor meant for point particles, simulation particles - */ -TreeQuad::TreeQuad( - PosType **xpt - ,float *my_masses - ,float *my_sizes - ,IndexType Npoints - ,bool Multimass - ,bool Multisize - ,PosType my_sigma_background /// background kappa that is subtracted - ,int bucket - ,PosType theta_force - ,bool my_periodic_buffer /// if true a periodic buffer will be imposed in the force calulation. See documentation on TreeQuad::force2D() for details. See note for TreeQuad::force2D_recur(). - ,PosType my_inv_screening_scale /// the inverse of the square of the sreening length. See note for TreeQuad::force2D_recur(). - ): - xp(xpt),MultiMass(Multimass), MultiRadius(Multisize), masses(my_masses) - ,sizes(my_sizes),Nparticles(Npoints),sigma_background(my_sigma_background) - ,Nbucket(bucket),force_theta(theta_force),periodic_buffer(my_periodic_buffer) - ,inv_screening_scale2(my_inv_screening_scale*my_inv_screening_scale) -{ - index = new IndexType[Npoints]; - IndexType ii; - - for(ii=0;iigetX(xp[ii]); - - haloON = true; //use internal halo parameters - + if(Npoints > 0){ tree = BuildQTreeNB(xp,Npoints,index); - + CalcMoments(); } - + phiintconst = (120*log(2.) - 631.)/840 + 19./70; - return; + return; } /// Particle positions and other data are not destroyed. -TreeQuad::~TreeQuad() +TreeQuadHalos::~TreeQuadHalos() { if(Nparticles == 0) return; - delete tree; - delete[] index; - if(haloON) Utilities::free_PosTypeMatrix(xp,Nparticles,2); - return; + delete tree; + delete[] index; + Utilities::free_PosTypeMatrix(xp,Nparticles,2); + return; } -QTreeNBHndl TreeQuad::BuildQTreeNB(PosType **xp,IndexType Nparticles,IndexType *particles){ +QTreeNB * TreeQuadHalos::BuildQTreeNB(PosType **xp,IndexType Nparticles,IndexType *particles){ IndexType i; short j; PosType p1[2],p2[2]; - + for(j=0;j<2;++j){ p1[j]=xp[0][j]; p2[j]=xp[0][j]; } - + // Find borders that enclose all particles for(i=0;i p2[j] ) p2[j]=xp[i][j]; } } - + if(Nparticles == 1){ - p1[0]=-0.25; - p1[1]=-0.25; - p2[0]=0.25; - p2[1]=0.25; + p1[0]=-0.25; + p1[1]=-0.25; + p2[0]=0.25; + p2[1]=0.25; } // store true dimensions of simulation PosType lengths[2] = {p2[0]-p1[0],p2[1]-p1[1]}; original_xl = lengths[0]; original_yl = lengths[1]; - + if(lengths[0] == 0 || lengths[1] == 0){ throw std::invalid_argument("particles in same place."); } @@ -132,335 +96,308 @@ QTreeNBHndl TreeQuad::BuildQTreeNB(PosType **xp,IndexType Nparticles,IndexType * // If region is not square, make it square. j = lengths[0] > lengths[1] ? 1 : 0; p2[j] = p1[j] + lengths[!j]; - + /* Initialize tree root */ - tree = new QTreeNB(xp,particles,Nparticles,p1,p2); - + tree = new QTreeNB(xp,particles,Nparticles,p1,p2); + /* build the tree */ _BuildQTreeNB(Nparticles,particles); - + /* visit every branch to find center of mass and cutoff scale */ tree->moveTop(); - + return tree; } /// returns an index for which of the four quadrangles of the branch the point x[] is in -inline short TreeQuad::WhichQuad(PosType *x,QBranchNB &branch){ - return (x[0] < branch.center[0]) + 2*(x[1] < branch.center[1]); +inline short TreeQuadHalos::WhichQuad(PosType *x,QBranchNB &branch){ + return (x[0] < branch.center[0]) + 2*(x[1] < branch.center[1]); } /// tree must be created and first branch must be set before start -void TreeQuad::_BuildQTreeNB(IndexType nparticles,IndexType *particles){ - - QBranchNB *cbranch = tree->current; /* pointer to current branch */ - IndexType i,j,cut,cut2,jt; - - cbranch->center[0] = (cbranch->boundary_p1[0] + cbranch->boundary_p2[0])/2; - cbranch->center[1] = (cbranch->boundary_p1[1] + cbranch->boundary_p2[1])/2; - cbranch->quad[0]=cbranch->quad[1]=cbranch->quad[2]=0; - cbranch->mass = 0.0; - - - // leaf case - if(cbranch->nparticles <= Nbucket){ - PosType r; - cbranch->Nbig_particles = 0; - for(i=0;inparticles;++i){ - jt = particles[i]*MultiRadius; - if(haloON){ - r = halos[jt]->get_Rmax(); - }else{ - r = sizes[jt]; - } - //r = haloON ? halos[particles[i]*MultiRadius].Rmax : sizes[particles[i]*MultiRadius]; - if(r < (cbranch->boundary_p2[0]-cbranch->boundary_p1[0])) ++cbranch->Nbig_particles; - } - if(cbranch->Nbig_particles){ - cbranch->big_particles = new IndexType[cbranch->Nbig_particles]; - for(i=0,j=0;inparticles;++i){ - jt = particles[i]*MultiRadius; - if(haloON){ - r = halos[jt]->get_Rmax(); - }else{ - r = sizes[jt]; - } - //r = haloON ? halos[particles[i]*MultiRadius].Rmax : sizes[particles[i]*MultiRadius]; - if(r < (cbranch->boundary_p2[0]-cbranch->boundary_p1[0])) cbranch->big_particles[j++] = particles[i]; - } - } - else{ - cbranch->big_particles = NULL; - } - - return; - } - - // find particles too big to be in children - - PosType *x = new PosType[cbranch->nparticles]; - - cbranch->Nbig_particles=0; - - if(MultiRadius){ - // store the particles that are too large to be in a child at the end of the list of particles in cbranch - for(i=0;inparticles;++i){ - if(haloON){ - x[i] = halos[particles[i]]->get_Rmax(); - }else{ - x[i] = sizes[particles[i]]; - } - //x[i] = haloON ? halos[particles[i]].Rmax : sizes[particles[i]]; - } - PosType maxsize = (cbranch->boundary_p2[0]-cbranch->boundary_p1[0])/2; - - // sort particles in size - //quicksort(particles,x,cbranch->nparticles); - Utilities::quickPartition(maxsize,&cut,particles,x,cbranch->nparticles); - - if(cut < cbranch->nparticles){ - if(tree->atTop()){ - cbranch->Nbig_particles = cut2 = cbranch->nparticles-cut; - }else{ - Utilities::quickPartition(2*maxsize,&cut2,&particles[cut],&x[cut],cbranch->nparticles-cut); - cbranch->Nbig_particles = cut2; - } - - cbranch->big_particles = new IndexType[cbranch->Nbig_particles]; - for(i=cut;i<(cut+cut2);++i) cbranch->big_particles[i-cut] = particles[i]; - } - - }else{ - if(haloON){ - x[0] = halos[0]->get_Rmax(); - }else{ - x[0] = sizes[0]; - } - //x[0] = haloON ? halos[0].Rmax : sizes[0]; - if(x[0] > (cbranch->boundary_p2[0]-cbranch->boundary_p1[0])/2 - && x[0] < (cbranch->boundary_p2[0]-cbranch->boundary_p1[0])){ - cbranch->Nbig_particles = cbranch->nparticles; - cbranch->big_particles = cbranch->particles; - } - } - - assert( cbranch->nparticles >= cbranch->Nbig_particles ); - IndexType NpInChildren = cbranch->nparticles;// - cbranch->Nbig_particles; - assert(NpInChildren >= 0); - - if(NpInChildren == 0){ - delete[] x; - return; - } - - IndexType cutx,cuty; - PosType xcut,ycut; - - QBranchNB *child0 = new QBranchNB(); - QBranchNB *child1 = new QBranchNB(); - QBranchNB *child2 = new QBranchNB(); - QBranchNB *child3 = new QBranchNB(); - - tree->attachChildrenToCurrent(child0,child1,child2,child3); - - // initialize boundaries of children - - child0->boundary_p1[0]=cbranch->center[0]; - child0->boundary_p1[1]=cbranch->center[1]; - child0->boundary_p2[0]=cbranch->boundary_p2[0]; - child0->boundary_p2[1]=cbranch->boundary_p2[1]; - - child1->boundary_p1[0]=cbranch->boundary_p1[0]; - child1->boundary_p1[1]=cbranch->center[1]; - child1->boundary_p2[0]=cbranch->center[0]; - child1->boundary_p2[1]=cbranch->boundary_p2[1]; - - child2->boundary_p1[0]=cbranch->center[0]; - child2->boundary_p1[1]=cbranch->boundary_p1[1]; - child2->boundary_p2[0]=cbranch->boundary_p2[0]; - child2->boundary_p2[1]=cbranch->center[1]; - - child3->boundary_p1[0]=cbranch->boundary_p1[0]; - child3->boundary_p1[1]=cbranch->boundary_p1[1]; - child3->boundary_p2[0]=cbranch->center[0]; - child3->boundary_p2[1]=cbranch->center[1]; - - - // **** makes sure force does not require nbucket at leaf - - xcut = cbranch->center[0]; - ycut = cbranch->center[1]; - - // divide along y direction - for(i=0;ixp[particles[i]][1]; - Utilities::quickPartition(ycut,&cuty,particles,x,NpInChildren); - - if(cuty > 0){ - // divide first group in the x direction - for(i=0;ixp[particles[i]][0]; - Utilities::quickPartition(xcut,&cutx,particles,x,cuty); - - child3->nparticles=cutx; - assert(child3->nparticles >= 0); - if(child3->nparticles > 0) - child3->particles = particles; - else child3->particles = NULL; - - child2->nparticles = cuty - cutx; - assert(child2->nparticles >= 0); - if(child2->nparticles > 0) - child2->particles = &particles[cutx]; - else child2->particles = NULL; - - }else{ - child3->nparticles = 0; - child3->particles = NULL; - - child2->nparticles = 0; - child2->particles = NULL; - } - - if(cuty < NpInChildren){ - // divide second group in the x direction - for(i=cuty;ixp[particles[i]][0]; - Utilities::quickPartition(xcut,&cutx,&particles[cuty],x,NpInChildren-cuty); - - child1->nparticles=cutx; - assert(child1->nparticles >= 0); - if(child1->nparticles > 0) - child1->particles = &particles[cuty]; - else child1->particles = NULL; - - child0->nparticles=NpInChildren - cuty - cutx; - assert(child0->nparticles >= 0); - if(child0->nparticles > 0) - child0->particles = &particles[cuty + cutx]; - else child0->particles = NULL; - - }else{ - child1->nparticles = 0; - child1->particles = NULL; - - child0->nparticles = 0; - child0->particles = NULL; - } - - delete[] x; - - tree->moveToChild(0); - _BuildQTreeNB(child0->nparticles,child0->particles); - tree->moveUp(); - - tree->moveToChild(1); - _BuildQTreeNB(child1->nparticles,child1->particles); - tree->moveUp(); - - tree->moveToChild(2); - _BuildQTreeNB(child2->nparticles,child2->particles); - tree->moveUp(); - - tree->moveToChild(3); - _BuildQTreeNB(child3->nparticles,child3->particles); - tree->moveUp(); - - return; +void TreeQuadHalos::_BuildQTreeNB(IndexType nparticles,IndexType *particles){ + + QBranchNB *cbranch = tree->current; /* pointer to current branch */ + IndexType i,j,cut,cut2,jt; + + cbranch->center[0] = (cbranch->boundary_p1[0] + cbranch->boundary_p2[0])/2; + cbranch->center[1] = (cbranch->boundary_p1[1] + cbranch->boundary_p2[1])/2; + cbranch->quad[0]=cbranch->quad[1]=cbranch->quad[2]=0; + cbranch->mass = 0.0; + + + // leaf case + if(cbranch->nparticles <= Nbucket){ + PosType r; + cbranch->Nbig_particles = 0; + for(i=0;inparticles;++i){ + jt = particles[i]*MultiRadius; + r = halos[jt]->get_Rmax(); + //r = haloON ? halos[particles[i]*MultiRadius].Rmax : sizes[particles[i]*MultiRadius]; + if(r < (cbranch->boundary_p2[0]-cbranch->boundary_p1[0])) ++cbranch->Nbig_particles; + } + if(cbranch->Nbig_particles){ + cbranch->big_particles = new IndexType[cbranch->Nbig_particles]; + for(i=0,j=0;inparticles;++i){ + jt = particles[i]*MultiRadius; + r = halos[jt]->get_Rmax(); + //r = haloON ? halos[particles[i]*MultiRadius].Rmax : sizes[particles[i]*MultiRadius]; + if(r < (cbranch->boundary_p2[0]-cbranch->boundary_p1[0])) cbranch->big_particles[j++] = particles[i]; + } + } + else{ + cbranch->big_particles = NULL; + } + + return; + } + + // find particles too big to be in children + + PosType *x = new PosType[cbranch->nparticles]; + + cbranch->Nbig_particles=0; + + if(MultiRadius){ + // store the particles that are too large to be in a child at the end of the list of particles in cbranch + for(i=0;inparticles;++i){ + x[i] = halos[particles[i]]->get_Rmax(); + //x[i] = haloON ? halos[particles[i]].Rmax : sizes[particles[i]]; + } + PosType maxsize = (cbranch->boundary_p2[0]-cbranch->boundary_p1[0])/2; + + // sort particles in size + //quicksort(particles,x,cbranch->nparticles); + Utilities::quickPartition(maxsize,&cut,particles,x,cbranch->nparticles); + + if(cut < cbranch->nparticles){ + if(tree->atTop()){ + cbranch->Nbig_particles = cut2 = cbranch->nparticles-cut; + }else{ + Utilities::quickPartition(2*maxsize,&cut2,&particles[cut],&x[cut],cbranch->nparticles-cut); + cbranch->Nbig_particles = cut2; + } + + cbranch->big_particles = new IndexType[cbranch->Nbig_particles]; + for(i=cut;i<(cut+cut2);++i) cbranch->big_particles[i-cut] = particles[i]; + } + + }else{ + x[0] = halos[0]->get_Rmax(); + //x[0] = haloON ? halos[0].Rmax : sizes[0]; + if(x[0] > (cbranch->boundary_p2[0]-cbranch->boundary_p1[0])/2 + && x[0] < (cbranch->boundary_p2[0]-cbranch->boundary_p1[0])){ + cbranch->Nbig_particles = cbranch->nparticles; + cbranch->big_particles = cbranch->particles; + } + } + + assert( cbranch->nparticles >= cbranch->Nbig_particles ); + IndexType NpInChildren = cbranch->nparticles;// - cbranch->Nbig_particles; + assert(NpInChildren >= 0); + + if(NpInChildren == 0){ + delete[] x; + return; + } + + IndexType cutx,cuty; + PosType xcut,ycut; + + QBranchNB *child0 = new QBranchNB(); + QBranchNB *child1 = new QBranchNB(); + QBranchNB *child2 = new QBranchNB(); + QBranchNB *child3 = new QBranchNB(); + + tree->attachChildrenToCurrent(child0,child1,child2,child3); + + // initialize boundaries of children + + child0->boundary_p1[0]=cbranch->center[0]; + child0->boundary_p1[1]=cbranch->center[1]; + child0->boundary_p2[0]=cbranch->boundary_p2[0]; + child0->boundary_p2[1]=cbranch->boundary_p2[1]; + + child1->boundary_p1[0]=cbranch->boundary_p1[0]; + child1->boundary_p1[1]=cbranch->center[1]; + child1->boundary_p2[0]=cbranch->center[0]; + child1->boundary_p2[1]=cbranch->boundary_p2[1]; + + child2->boundary_p1[0]=cbranch->center[0]; + child2->boundary_p1[1]=cbranch->boundary_p1[1]; + child2->boundary_p2[0]=cbranch->boundary_p2[0]; + child2->boundary_p2[1]=cbranch->center[1]; + + child3->boundary_p1[0]=cbranch->boundary_p1[0]; + child3->boundary_p1[1]=cbranch->boundary_p1[1]; + child3->boundary_p2[0]=cbranch->center[0]; + child3->boundary_p2[1]=cbranch->center[1]; + + + // **** makes sure force does not require nbucket at leaf + + xcut = cbranch->center[0]; + ycut = cbranch->center[1]; + + // divide along y direction + for(i=0;ixxp[particles[i]][1]; + Utilities::quickPartition(ycut,&cuty,particles,x,NpInChildren); + + if(cuty > 0){ + // divide first group in the x direction + for(i=0;ixxp[particles[i]][0]; + Utilities::quickPartition(xcut,&cutx,particles,x,cuty); + + child3->nparticles=cutx; + assert(child3->nparticles >= 0); + if(child3->nparticles > 0) + child3->particles = particles; + else child3->particles = NULL; + + child2->nparticles = cuty - cutx; + assert(child2->nparticles >= 0); + if(child2->nparticles > 0) + child2->particles = &particles[cutx]; + else child2->particles = NULL; + + }else{ + child3->nparticles = 0; + child3->particles = NULL; + + child2->nparticles = 0; + child2->particles = NULL; + } + + if(cuty < NpInChildren){ + // divide second group in the x direction + for(i=cuty;ixxp[particles[i]][0]; + Utilities::quickPartition(xcut,&cutx,&particles[cuty],x,NpInChildren-cuty); + + child1->nparticles=cutx; + assert(child1->nparticles >= 0); + if(child1->nparticles > 0) + child1->particles = &particles[cuty]; + else child1->particles = NULL; + + child0->nparticles=NpInChildren - cuty - cutx; + assert(child0->nparticles >= 0); + if(child0->nparticles > 0) + child0->particles = &particles[cuty + cutx]; + else child0->particles = NULL; + + }else{ + child1->nparticles = 0; + child1->particles = NULL; + + child0->nparticles = 0; + child0->particles = NULL; + } + + delete[] x; + + tree->moveToChild(0); + _BuildQTreeNB(child0->nparticles,child0->particles); + tree->moveUp(); + + tree->moveToChild(1); + _BuildQTreeNB(child1->nparticles,child1->particles); + tree->moveUp(); + + tree->moveToChild(2); + _BuildQTreeNB(child2->nparticles,child2->particles); + tree->moveUp(); + + tree->moveToChild(3); + _BuildQTreeNB(child3->nparticles,child3->particles); + tree->moveUp(); + + return; } // calculates moments of the mass and the cutoff scale for each box -void TreeQuad::CalcMoments(){ - - //*** make compatable - IndexType i; - PosType rcom,xcm[2],xcut; - QBranchNB *cbranch; - PosType tmp; - - tree->moveTop(); - do{ - cbranch=tree->current; /* pointer to current branch */ - - cbranch->rmax = sqrt( pow(cbranch->boundary_p2[0]-cbranch->boundary_p1[0],2) - + pow(cbranch->boundary_p2[1]-cbranch->boundary_p1[1],2) ); - - // calculate mass - for(i=0,cbranch->mass=0;inparticles;++i) - if(haloON ){ - cbranch->mass += halos[cbranch->particles[i]]->get_mass(); - }else{ - cbranch->mass += masses[cbranch->particles[i]*MultiMass]; - } - //cbranch->mass += haloON ? halos[cbranch->particles[i]*MultiMass].mass : masses[cbranch->particles[i]*MultiMass]; - - // calculate center of mass - cbranch->center[0]=cbranch->center[1]=0; - for(i=0;inparticles;++i){ - if(haloON ){ - tmp = halos[cbranch->particles[i]]->get_mass(); - }else{ - tmp = masses[cbranch->particles[i]*MultiMass]; - } - //tmp = haloON ? halos[cbranch->particles[i]*MultiMass].mass : masses[cbranch->particles[i]*MultiMass]; - cbranch->center[0] += tmp*tree->xp[cbranch->particles[i]][0]/cbranch->mass; - cbranch->center[1] += tmp*tree->xp[cbranch->particles[i]][1]/cbranch->mass; - } - ////////////////////////////////////////////// - // calculate quadropole moment of branch - ////////////////////////////////////////////// - cbranch->quad[0]=cbranch->quad[1]=cbranch->quad[2]=0; - for(i=0;inparticles;++i){ - xcm[0]=tree->xp[cbranch->particles[i]][0]-cbranch->center[0]; - xcm[1]=tree->xp[cbranch->particles[i]][1]-cbranch->center[1]; - xcut = pow(xcm[0],2) + pow(xcm[1],2); - if(haloON ){ - tmp = halos[cbranch->particles[i]]->get_mass(); - }else{ - tmp = masses[cbranch->particles[i]*MultiMass]; - } - //tmp = haloON ? halos[cbranch->particles[i]*MultiMass].mass : masses[cbranch->particles[i]*MultiMass]; - - cbranch->quad[0] += (xcut-2*xcm[0]*xcm[0])*tmp; - cbranch->quad[1] += (xcut-2*xcm[1]*xcm[1])*tmp; - cbranch->quad[2] += -2*xcm[0]*xcm[1]*tmp; - } - - // largest distance from center of mass of cell - for(i=0,rcom=0.0;i<2;++i) rcom += ( pow(cbranch->center[i]-cbranch->boundary_p1[i],2) > pow(cbranch->center[i]-cbranch->boundary_p2[i],2) ) ? - pow(cbranch->center[i]-cbranch->boundary_p1[i],2) : pow(cbranch->center[i]-cbranch->boundary_p2[i],2); - - rcom=sqrt(rcom); - - if(force_theta > 0.0) cbranch->rcrit_angle = 1.15470*rcom/(force_theta); - else cbranch->rcrit_angle=1.0e100; - - /*if(MultiRadius){ - - for(i=0,cbranch->maxrsph=0.0;inparticles;++i){ - tmp = haloON ? halos[cbranch->particles[i]].Rmax : sizes[cbranch->particles[i]]; - if(cbranch->maxrsph <= tmp ){ - cbranch->maxrsph = tmp; - } - } - - cbranch->rcrit_part = rcom + 2*cbranch->maxrsph; - - }else{ - // single size case - cbranch->maxrsph = haloON ? halos[0].Rmax : sizes[0]; - cbranch->rcrit_part = rcom + 2*cbranch->maxrsph; - }*/ - //cbranch->rcrit_angle += cbranch->rcrit_part; - - }while(tree->WalkStep(true)); - - return; +void TreeQuadHalos::CalcMoments(){ + + //*** make compatable + IndexType i; + PosType rcom,xcm[2],xcut; + QBranchNB *cbranch; + PosType tmp; + + tree->moveTop(); + do{ + cbranch=tree->current; /* pointer to current branch */ + + cbranch->rmax = sqrt( pow(cbranch->boundary_p2[0]-cbranch->boundary_p1[0],2) + + pow(cbranch->boundary_p2[1]-cbranch->boundary_p1[1],2) ); + + // calculate mass + for(i=0,cbranch->mass=0;inparticles;++i) + cbranch->mass += halos[cbranch->particles[i]]->get_mass(); + //cbranch->mass += haloON ? halos[cbranch->particles[i]*MultiMass].mass : masses[cbranch->particles[i]*MultiMass]; + + // calculate center of mass + cbranch->center[0]=cbranch->center[1]=0; + for(i=0;inparticles;++i){ + tmp = halos[cbranch->particles[i]]->get_mass(); + //tmp = haloON ? halos[cbranch->particles[i]*MultiMass].mass : masses[cbranch->particles[i]*MultiMass]; + cbranch->center[0] += tmp*tree->xxp[cbranch->particles[i]][0]/cbranch->mass; + cbranch->center[1] += tmp*tree->xxp[cbranch->particles[i]][1]/cbranch->mass; + } + ////////////////////////////////////////////// + // calculate quadropole moment of branch + ////////////////////////////////////////////// + cbranch->quad[0]=cbranch->quad[1]=cbranch->quad[2]=0; + for(i=0;inparticles;++i){ + xcm[0]=tree->xxp[cbranch->particles[i]][0]-cbranch->center[0]; + xcm[1]=tree->xxp[cbranch->particles[i]][1]-cbranch->center[1]; + xcut = pow(xcm[0],2) + pow(xcm[1],2); + + tmp = halos[cbranch->particles[i]]->get_mass(); + //tmp = haloON ? halos[cbranch->particles[i]*MultiMass].mass : masses[cbranch->particles[i]*MultiMass]; + + cbranch->quad[0] += (xcut-2*xcm[0]*xcm[0])*tmp; + cbranch->quad[1] += (xcut-2*xcm[1]*xcm[1])*tmp; + cbranch->quad[2] += -2*xcm[0]*xcm[1]*tmp; + } + + // largest distance from center of mass of cell + for(i=0,rcom=0.0;i<2;++i) rcom += ( pow(cbranch->center[i]-cbranch->boundary_p1[i],2) > pow(cbranch->center[i]-cbranch->boundary_p2[i],2) ) ? + pow(cbranch->center[i]-cbranch->boundary_p1[i],2) : pow(cbranch->center[i]-cbranch->boundary_p2[i],2); + + rcom=sqrt(rcom); + + if(force_theta > 0.0) cbranch->rcrit_angle = 1.15470*rcom/(force_theta); + else cbranch->rcrit_angle=1.0e100; + + /*if(MultiRadius){ + + for(i=0,cbranch->maxrsph=0.0;inparticles;++i){ + tmp = haloON ? halos[cbranch->particles[i]].Rmax : sizes[cbranch->particles[i]]; + if(cbranch->maxrsph <= tmp ){ + cbranch->maxrsph = tmp; + } + } + + cbranch->rcrit_part = rcom + 2*cbranch->maxrsph; + + }else{ + // single size case + cbranch->maxrsph = haloON ? halos[0].Rmax : sizes[0]; + cbranch->rcrit_part = rcom + 2*cbranch->maxrsph; + }*/ + //cbranch->rcrit_angle += cbranch->rcrit_part; + + }while(tree->WalkStep(true)); + + return; } /// simple rotates the coordinates in the xp array -void TreeQuad::rotate_coordinates(PosType **coord){ - IndexType i; - short j; - PosType tmp[3]; - +void TreeQuadHalos::rotate_coordinates(PosType **coord){ + IndexType i; + short j; + PosType tmp[3]; + /* rotate particle positions */ for(i=0;itop->nparticles;++i){ for(j=0;j<3;++j) tmp[j]=0.0; @@ -471,7 +408,7 @@ void TreeQuad::rotate_coordinates(PosType **coord){ } for(j=0;j<3;++j) xp[i][j]=tmp[j]; } - + return; } @@ -495,20 +432,21 @@ void TreeQuad::rotate_coordinates(PosType **coord){ * screens the large scale geometry of the simulation on the sky. This is useful when the region is rectangular instead of circular. * */ -void TreeQuad::force2D(const PosType *ray,PosType *alpha,KappaType *kappa,KappaType *gamma,KappaType *phi) const{ +void TreeQuadHalos::force2D(const PosType *ray,PosType *alpha,KappaType *kappa,KappaType *gamma,KappaType *phi) const{ alpha[0]=alpha[1]=gamma[0]=gamma[1]=gamma[2]=0.0; *kappa=*phi=0.0; if(Nparticles == 0) return; - + assert(tree); - QTreeNB::iterator it(tree); + //QTreeNB::iterator it(tree); + QBiterator it(tree); if(periodic_buffer){ PosType tmp_ray[2]; - + for(int i = -1;i<2;++i){ for(int j = -1;j<2;++j){ tmp_ray[0] = ray[0] + i*original_xl; @@ -532,8 +470,10 @@ void TreeQuad::force2D(const PosType *ray,PosType *alpha,KappaType *kappa,KappaT /** \brief Returns the halos that are within rmax of ray[] */ -void TreeQuad::neighbors(PosType ray[],PosType rmax,std::list &neighbors) const{ - QTreeNB::iterator it(tree); +void TreeQuadHalos::neighbors(PosType ray[],PosType rmax,std::list &neighbors) const{ + //QTreeNB::iterator it(tree); + QBiterator it(tree); + neighbors.clear(); PosType r2 = rmax*rmax; @@ -550,8 +490,8 @@ void TreeQuad::neighbors(PosType ray[],PosType rmax,std::list &neighb decend = false; }else if((*it)->child0 == NULL){ // at leaf for(int i=0;i<(*it)->nparticles;++i){ - if( (tree->xp[(*it)->particles[i]][0] - ray[0])*(tree->xp[(*it)->particles[i]][0] - ray[0]) - + (tree->xp[(*it)->particles[i]][1] - ray[1])*(tree->xp[(*it)->particles[i]][1] - ray[1]) < r2) neighbors.push_back((*it)->particles[i]); + if( (tree->xxp[(*it)->particles[i]][0] - ray[0])*(tree->xxp[(*it)->particles[i]][0] - ray[0]) + + (tree->xxp[(*it)->particles[i]][1] - ray[1])*(tree->xxp[(*it)->particles[i]][1] - ray[1]) < r2) neighbors.push_back((*it)->particles[i]); } decend = false; } @@ -560,29 +500,29 @@ void TreeQuad::neighbors(PosType ray[],PosType rmax,std::list &neighb } /** \brief Returns the halos that are within rmax of ray[] */ -void TreeQuad::neighbors(PosType ray[],PosType rmax,std::vector &neighbors) const{ +void TreeQuadHalos::neighbors(PosType ray[],PosType rmax,std::vector &neighbors) const{ neighbors.clear(); if(halos == NULL){ - std::cerr << "TreeQuad::neighbors - The are no halos in this tree use other version of this function" << std::endl; + std::cerr << "TreeQuadHalos::neighbors - The are no halos in this tree use other version of this function" << std::endl; throw std::runtime_error("no halos"); return; } std::list neighbor_indexes; - TreeQuad::neighbors(ray,rmax,neighbor_indexes); + TreeQuadHalos::neighbors(ray,rmax,neighbor_indexes); neighbors.resize(neighbor_indexes.size()); std::list::iterator it = neighbor_indexes.begin(); for(size_t i=0;i &treeit, const PosType *ray ,PosType *alpha ,KappaType *kappa @@ -602,116 +542,95 @@ void TreeQuad::walkTree_iter( treeit.movetop(); //tree->moveTop(); do{ - ++count; - allowDescent=false; + ++count; + allowDescent=false; /* - if(inv_screening_scale2 != 0) notscreen = BoxIntersectCircle(ray,3*sqrt(1.0/inv_screening_scale2) - ,(*treeit)->boundary_p1 - ,(*treeit)->boundary_p2); - else notscreen = true; -*/ - //if(tree->current->nparticles > 0) + if(inv_screening_scale2 != 0) notscreen = BoxIntersectCircle(ray,3*sqrt(1.0/inv_screening_scale2) + ,(*treeit)->boundary_p1 + ,(*treeit)->boundary_p2); + else notscreen = true; + */ + //if(tree->current->nparticles > 0) if((*treeit)->nparticles > 0) { - xcm[0]=(*treeit)->center[0]-ray[0]; - xcm[1]=(*treeit)->center[1]-ray[1]; + xcm[0]=(*treeit)->center[0]-ray[0]; + xcm[1]=(*treeit)->center[1]-ray[1]; - rcm2cell = xcm[0]*xcm[0] + xcm[1]*xcm[1]; + rcm2cell = xcm[0]*xcm[0] + xcm[1]*xcm[1]; - boxsize2 = ((*treeit)->boundary_p2[0]-(*treeit)->boundary_p1[0])*((*treeit)->boundary_p2[0]-(*treeit)->boundary_p1[0]); + boxsize2 = ((*treeit)->boundary_p2[0]-(*treeit)->boundary_p1[0])*((*treeit)->boundary_p2[0]-(*treeit)->boundary_p1[0]); - if( rcm2cell < ((*treeit)->rcrit_angle)*((*treeit)->rcrit_angle) || rcm2cell < 5.83*boxsize2) + if( rcm2cell < ((*treeit)->rcrit_angle)*((*treeit)->rcrit_angle) || rcm2cell < 5.83*boxsize2) { - // includes rcrit_particle constraint - allowDescent=true; + // includes rcrit_particle constraint + allowDescent=true; - // Treat all particles in a leaf as a point particle - if(treeit.atLeaf()) + // Treat all particles in a leaf as a point particle + if(treeit.atLeaf()) { - for(i = 0 ; i < (*treeit)->nparticles ; ++i) + for(i = 0 ; i < (*treeit)->nparticles ; ++i) { - xcm[0] = tree->xp[(*treeit)->particles[i]][0] - ray[0]; - xcm[1] = tree->xp[(*treeit)->particles[i]][1] - ray[1]; + xcm[0] = tree->xxp[(*treeit)->particles[i]][0] - ray[0]; + xcm[1] = tree->xxp[(*treeit)->particles[i]][1] - ray[1]; - - rcm2 = xcm[0]*xcm[0] + xcm[1]*xcm[1]; + + rcm2 = xcm[0]*xcm[0] + xcm[1]*xcm[1]; double screening = exp(-rcm2*inv_screening_scale2); - if(rcm2 < 1e-20) rcm2 = 1e-20; + if(rcm2 < 1e-20) rcm2 = 1e-20; - tmp_index = MultiMass*(*treeit)->particles[i]; + tmp_index = MultiMass*(*treeit)->particles[i]; - if(haloON){ prefac = halos[tmp_index]->get_mass(); } - else{ prefac = masses[tmp_index]; } - prefac /= rcm2*PI/screening; + prefac = halos[tmp_index]->get_mass(); + prefac /= rcm2*PI/screening; - tmp = -1.0*prefac; + tmp = -1.0*prefac; + + alpha[0] += tmp*xcm[0]; + alpha[1] += tmp*xcm[1]; - alpha[0] += tmp*xcm[0]; - alpha[1] += tmp*xcm[1]; - tmp = -2.0*prefac/rcm2; - + gamma[0] += 0.5*(xcm[0]*xcm[0]-xcm[1]*xcm[1])*tmp; gamma[1] += xcm[0]*xcm[1]*tmp; - + *phi += prefac*rcm2*0.5*log(rcm2); - - } // end of for - } // end of if(tree->atLeaf()) + + } // end of for + } // end of if(tree->atLeaf()) - // Find the particles that intersect with ray and add them individually. - if(rcm2cell < 5.83*boxsize2) + // Find the particles that intersect with ray and add them individually. + if(rcm2cell < 5.83*boxsize2) { - for(i = 0 ; i < (*treeit)->Nbig_particles ; ++i) + for(i = 0 ; i < (*treeit)->Nbig_particles ; ++i) { - tmp_index = (*treeit)->big_particles[i]; + tmp_index = (*treeit)->big_particles[i]; - xcm[0] = tree->xp[tmp_index][0] - ray[0]; - xcm[1] = tree->xp[tmp_index][1] - ray[1]; + xcm[0] = tree->xxp[tmp_index][0] - ray[0]; + xcm[1] = tree->xxp[tmp_index][1] - ray[1]; rcm2 = xcm[0]*xcm[0] + xcm[1]*xcm[1]; - if(haloON){ - double screening = exp(-rcm2*inv_screening_scale2); - - halos[tmp_index]->force_halo(alpha,kappa,gamma,phi,xcm,true,screening); - }else{ // case of no halos just particles and no class derived from TreeQuad - - if(rcm2 < 1e-20) rcm2 = 1e-20; - //rcm = sqrt(rcm2); - - //prefac = masses[MultiMass*tmp_index]/rcm2/PI; - //arg1 = rcm2/(sizes[tmp_index*MultiRadius]*sizes[tmp_index*MultiRadius]); - //arg2 = sizes[tmp_index*MultiRadius]; - //tmp = sizes[tmp_index*MultiRadius]; - - PosType size = sizes[tmp_index*MultiRadius]; - - // intersecting, subtract the point particle - if(rcm2 < 4*size*size) - { - - b_spline_profile(xcm,sqrt(rcm2),masses[MultiMass*tmp_index],size,alpha,kappa,gamma,phi); - } - } - } - } + + double screening = exp(-rcm2*inv_screening_scale2); + halos[tmp_index]->force_halo(alpha,kappa,gamma,phi,xcm,true,screening); + } + } - } + } else { // use whole cell - allowDescent=false; - + allowDescent=false; + double screening = exp(-rcm2cell*inv_screening_scale2); double tmp = -1.0*(*treeit)->mass/rcm2cell/PI*screening; @@ -739,9 +658,9 @@ void TreeQuad::walkTree_iter( alpha[0] += tmp*xcm[0]; alpha[1] += tmp*xcm[1]; - - } - } + + } + } }while(treeit.TreeWalkStep(allowDescent)); @@ -750,7 +669,7 @@ void TreeQuad::walkTree_iter( alpha[0] -= ray[0]*sigma_background*(inv_screening_scale2 == 0.0); alpha[1] -= ray[1]*sigma_background*(inv_screening_scale2 == 0.0); { // taken out to speed up - *kappa -= sigma_background; + *kappa -= sigma_background; // *phi -= sigma_background * sigma_background ; } @@ -760,7 +679,7 @@ void TreeQuad::walkTree_iter( /** \brief Force2D_recur calculates the defection, convergence and shear using * the plane-lens approximation. * - * This function should do the same work as TreeQuad::force2D() except it is + * This function should do the same work as TreeQuadHalos::force2D() except it is * done recursively instead of iteratively. This is done to enable multi-threading * of the force calculation. * @@ -781,7 +700,7 @@ void TreeQuad::walkTree_iter( * screens the large scale geometry of the simulation on the sky. This is useful when the region is rectangular instead of circular. * */ -void TreeQuad::force2D_recur(const PosType *ray,PosType *alpha,KappaType *kappa,KappaType *gamma,KappaType *phi){ +void TreeQuadHalos::force2D_recur(const PosType *ray,PosType *alpha,KappaType *kappa,KappaType *gamma,KappaType *phi){ alpha[0]=alpha[1]=gamma[0]=gamma[1]=gamma[2]=0.0; @@ -797,24 +716,24 @@ void TreeQuad::force2D_recur(const PosType *ray,PosType *alpha,KappaType *kappa, // for points outside of primary zone shift to primary zone //if(inbox(ray,tree->top->boundary_p1,tree->top->boundary_p2)){ - tmp_c[0] = ray[0]; - tmp_c[1] = ray[1]; + tmp_c[0] = ray[0]; + tmp_c[1] = ray[1]; /*}else{ - int di[2]; - PosType center[2] = { (tree->top->boundary_p1[0] + tree->top->boundary_p2[0])/2 , - (tree->top->boundary_p1[1] + tree->top->boundary_p2[1])/2 }; - - di[0] = (int)( 2*(ray[0] - center[0])/lx ); - di[1] = (int)( 2*(ray[1] - center[1])/ly ); - - di[0] = (di[0] > 0) ? (di[0] + 1)/2 : (di[0] - 1)/2; - di[1] = (di[1] > 0) ? (di[1] + 1)/2 : (di[1] - 1)/2; - - tmp_c[0] = ray[0] - lx * di[0]; - tmp_c[1] = ray[1] - ly * di[1]; - - assert(inbox(tmp_c,tree->top->boundary_p1,tree->top->boundary_p2)); - }*/ + int di[2]; + PosType center[2] = { (tree->top->boundary_p1[0] + tree->top->boundary_p2[0])/2 , + (tree->top->boundary_p1[1] + tree->top->boundary_p2[1])/2 }; + + di[0] = (int)( 2*(ray[0] - center[0])/lx ); + di[1] = (int)( 2*(ray[1] - center[1])/ly ); + + di[0] = (di[0] > 0) ? (di[0] + 1)/2 : (di[0] - 1)/2; + di[1] = (di[1] > 0) ? (di[1] + 1)/2 : (di[1] - 1)/2; + + tmp_c[0] = ray[0] - lx * di[0]; + tmp_c[1] = ray[1] - ly * di[1]; + + assert(inbox(tmp_c,tree->top->boundary_p1,tree->top->boundary_p2)); + }*/ for(int i = -1;i<2;++i){ for(int j = -1;j<2;++j){ @@ -833,28 +752,28 @@ void TreeQuad::force2D_recur(const PosType *ray,PosType *alpha,KappaType *kappa, alpha[1] -= ray[1]*sigma_background*(inv_screening_scale2 == 0); *kappa -= sigma_background; - + return; } -void TreeQuad::walkTree_recur(QBranchNB *branch,PosType const *ray,PosType *alpha,KappaType *kappa,KappaType *gamma, KappaType *phi){ +void TreeQuadHalos::walkTree_recur(QBranchNB *branch,PosType const *ray,PosType *alpha,KappaType *kappa,KappaType *gamma, KappaType *phi){ - PosType xcm[2],rcm2cell,rcm2,tmp,boxsize2; - IndexType i; - std::size_t tmp_index; - PosType arg1, arg2, prefac; + PosType xcm[2],rcm2cell,rcm2,tmp,boxsize2; + IndexType i; + std::size_t tmp_index; + PosType arg1, arg2, prefac; /*bool notscreen = true; - - if(inv_screening_scale2 != 0) notscreen = BoxIntersectCircle(ray,3*sqrt(1.0/inv_screening_scale2), branch->boundary_p1, branch->boundary_p2); - */ - if(branch->nparticles > 0) + + if(inv_screening_scale2 != 0) notscreen = BoxIntersectCircle(ray,3*sqrt(1.0/inv_screening_scale2), branch->boundary_p1, branch->boundary_p2); + */ + if(branch->nparticles > 0) { - xcm[0]=branch->center[0]-ray[0]; - xcm[1]=branch->center[1]-ray[1]; + xcm[0]=branch->center[0]-ray[0]; + xcm[1]=branch->center[1]-ray[1]; - rcm2cell = xcm[0]*xcm[0] + xcm[1]*xcm[1]; + rcm2cell = xcm[0]*xcm[0] + xcm[1]*xcm[1]; boxsize2 = (branch->boundary_p2[0]-branch->boundary_p1[0])*(branch->boundary_p2[0]-branch->boundary_p1[0]); @@ -867,8 +786,8 @@ void TreeQuad::walkTree_recur(QBranchNB *branch,PosType const *ray,PosType *alph for(i = 0 ; i < branch->nparticles ; ++i){ - xcm[0] = tree->xp[branch->particles[i]][0] - ray[0]; - xcm[1] = tree->xp[branch->particles[i]][1] - ray[1]; + xcm[0] = tree->xxp[branch->particles[i]][0] - ray[0]; + xcm[1] = tree->xxp[branch->particles[i]][1] - ray[1]; rcm2 = xcm[0]*xcm[0] + xcm[1]*xcm[1]; double screening = exp(-rcm2*inv_screening_scale2); @@ -877,8 +796,7 @@ void TreeQuad::walkTree_recur(QBranchNB *branch,PosType const *ray,PosType *alph tmp_index = MultiMass*branch->particles[i]; - if(haloON ) { prefac = halos[tmp_index]->get_mass(); } - else{ prefac = masses[tmp_index]; } + prefac = halos[tmp_index]->get_mass(); prefac /= rcm2*PI/screening; @@ -897,121 +815,106 @@ void TreeQuad::walkTree_recur(QBranchNB *branch,PosType const *ray,PosType *alph } } - // Find the particles that intersect with ray and add them individually. - if(rcm2cell < 5.83*boxsize2) + // Find the particles that intersect with ray and add them individually. + if(rcm2cell < 5.83*boxsize2) { - for(i = 0 ; i < branch->Nbig_particles ; ++i) + for(i = 0 ; i < branch->Nbig_particles ; ++i) { - tmp_index = branch->big_particles[i]; + tmp_index = branch->big_particles[i]; - xcm[0] = tree->xp[tmp_index][0] - ray[0]; - xcm[1] = tree->xp[tmp_index][1] - ray[1]; + xcm[0] = tree->xxp[tmp_index][0] - ray[0]; + xcm[1] = tree->xxp[tmp_index][1] - ray[1]; rcm2 = xcm[0]*xcm[0] + xcm[1]*xcm[1]; - - ///////////////////////////////////////// - if(haloON){ - double screening = exp(-rcm2*inv_screening_scale2); - halos[tmp_index]->force_halo(alpha,kappa,gamma,phi,xcm,true,screening); - }else{ // case of no halos just particles and no class derived from TreeQuad - - if(rcm2 < 1e-20) rcm2 = 1e-20; - - PosType size = sizes[tmp_index*MultiRadius]; - - // intersecting, subtract the point particle - if(rcm2 < 4*size*size) - { - - b_spline_profile(xcm,sqrt(rcm2),masses[MultiMass*tmp_index],size,alpha,kappa,gamma,phi); - - } - } - } - } + + ///////////////////////////////////////// + double screening = exp(-rcm2*inv_screening_scale2); + halos[tmp_index]->force_halo(alpha,kappa,gamma,phi,xcm,true,screening); + } + } - if(branch->child0 != NULL) - walkTree_recur(branch->child0,&ray[0],&alpha[0],kappa,&gamma[0],&phi[0]); - if(branch->child1 != NULL) - walkTree_recur(branch->child1,&ray[0],&alpha[0],kappa,&gamma[0],&phi[0]); - if(branch->child2 != NULL) - walkTree_recur(branch->child2,&ray[0],&alpha[0],kappa,&gamma[0],&phi[0]); - if(branch->child3 != NULL) - walkTree_recur(branch->child3,&ray[0],&alpha[0],kappa,&gamma[0],&phi[0]); + if(branch->child0 != NULL) + walkTree_recur(branch->child0,&ray[0],&alpha[0],kappa,&gamma[0],&phi[0]); + if(branch->child1 != NULL) + walkTree_recur(branch->child1,&ray[0],&alpha[0],kappa,&gamma[0],&phi[0]); + if(branch->child2 != NULL) + walkTree_recur(branch->child2,&ray[0],&alpha[0],kappa,&gamma[0],&phi[0]); + if(branch->child3 != NULL) + walkTree_recur(branch->child3,&ray[0],&alpha[0],kappa,&gamma[0],&phi[0]); - } + } else { // use whole cell double screening = exp(-rcm2cell*inv_screening_scale2); - double tmp = -1.0*branch->mass/rcm2cell/PI*screening; + double tmp = -1.0*branch->mass/rcm2cell/PI*screening; - alpha[0] += tmp*xcm[0]; - alpha[1] += tmp*xcm[1]; + alpha[0] += tmp*xcm[0]; + alpha[1] += tmp*xcm[1]; - { // taken out to speed up - tmp=-2.0*branch->mass/PI/rcm2cell/rcm2cell*screening; - gamma[0] += 0.5*(xcm[0]*xcm[0]-xcm[1]*xcm[1])*tmp; - gamma[1] += xcm[0]*xcm[1]*tmp; + { // taken out to speed up + tmp=-2.0*branch->mass/PI/rcm2cell/rcm2cell*screening; + gamma[0] += 0.5*(xcm[0]*xcm[0]-xcm[1]*xcm[1])*tmp; + gamma[1] += xcm[0]*xcm[1]*tmp; *phi += 0.5*tree->current->mass*log( rcm2cell )/PI*screening; *phi -= 0.5*( tree->current->quad[0]*xcm[0]*xcm[0] + tree->current->quad[1]*xcm[1]*xcm[1] + 2*tree->current->quad[2]*xcm[0]*xcm[1] )/(PI*rcm2cell*rcm2cell)*screening; - } + } - // quadrapole contribution - // the kappa and gamma are not calculated to this order - alpha[0] -= (branch->quad[0]*xcm[0] + branch->quad[2]*xcm[1]) + // quadrapole contribution + // the kappa and gamma are not calculated to this order + alpha[0] -= (branch->quad[0]*xcm[0] + branch->quad[2]*xcm[1]) /(rcm2cell*rcm2cell)/PI*screening; - alpha[1] -= (branch->quad[1]*xcm[1] + branch->quad[2]*xcm[0]) + alpha[1] -= (branch->quad[1]*xcm[1] + branch->quad[2]*xcm[0]) /(rcm2cell*rcm2cell)/PI*screening; - tmp = 4*(branch->quad[0]*xcm[0]*xcm[0] + branch->quad[1]*xcm[1]*xcm[1] + tmp = 4*(branch->quad[0]*xcm[0]*xcm[0] + branch->quad[1]*xcm[1]*xcm[1] + 2*branch->quad[2]*xcm[0]*xcm[1])/(rcm2cell*rcm2cell*rcm2cell)/PI*screening; - alpha[0] += tmp*xcm[0]; - alpha[1] += tmp*xcm[1]; + alpha[0] += tmp*xcm[0]; + alpha[1] += tmp*xcm[1]; - return; - } + return; + } - } - return; + } + return; } /** This is a diagnostic routine that prints the position of every point in a * given branch of the tree. */ -void TreeQuad::printParticlesInBranch(unsigned long number){ - unsigned long i; - - tree->moveTop(); - do{ - if(tree->current->number == number){ - std::cout << tree->current->nparticles << std::endl; - for(i=0;icurrent->nparticles;++i){ - std::cout << xp[tree->current->particles[i]][0] << " " << xp[tree->current->particles[i]][1] << std::endl; - } - return; - } - }while(tree->WalkStep(true)); - - return; +void TreeQuadHalos::printParticlesInBranch(unsigned long number){ + unsigned long i; + + tree->moveTop(); + do{ + if(tree->current->number == number){ + std::cout << tree->current->nparticles << std::endl; + for(i=0;icurrent->nparticles;++i){ + std::cout << xp[tree->current->particles[i]][0] << " " << xp[tree->current->particles[i]][1] << std::endl; + } + return; + } + }while(tree->WalkStep(true)); + + return; } /** * Prints to stdout the borders of each branch in the tree below level. * If level < 0 or not specified the whole tree will be printed. */ -void TreeQuad::printBranchs(int level){ - - bool decend = true; - tree->moveTop(); - do{ - std::cout << tree->current->boundary_p1[0] << " " << tree->current->boundary_p1[1] << " " - << tree->current->boundary_p2[0] << " " << tree->current->boundary_p2[1] << std::endl; - if(tree->current->level == level) decend = false; - else decend = true; - }while(tree->WalkStep(decend)); - - return; +void TreeQuadHalos::printBranchs(int level){ + + bool decend = true; + tree->moveTop(); + do{ + std::cout << tree->current->boundary_p1[0] << " " << tree->current->boundary_p1[1] << " " + << tree->current->boundary_p2[0] << " " << tree->current->boundary_p2[1] << std::endl; + if(tree->current->level == level) decend = false; + else decend = true; + }while(tree->WalkStep(decend)); + + return; } diff --git a/TreeCode/simpleTree.cpp b/TreeCode/simpleTree.cpp index ee6b2efe..892e1b88 100644 --- a/TreeCode/simpleTree.cpp +++ b/TreeCode/simpleTree.cpp @@ -21,7 +21,8 @@ BranchNB::~BranchNB(){ delete center; } -TreeSimple::TreeSimple(PosType **xpt,IndexType Npoints,int bucket,int Ndimensions,bool median){ +template +TreeSimple::TreeSimple(PType *xpt,IndexType Npoints,int bucket,int Ndimensions,bool median){ index = new IndexType[Npoints]; IndexType ii; @@ -30,23 +31,25 @@ TreeSimple::TreeSimple(PosType **xpt,IndexType Npoints,int bucket,int Ndimension median_cut = median; Nparticles = Npoints; - xp = xpt; + xxp = xpt; for(ii=0;ii +TreeSimple::~TreeSimple() { freeTreeNB(tree); delete[] index; return; } -void TreeSimple::PointsWithinEllipse( +template +void TreeSimple::PointsWithinEllipse( PosType *ray /// center of ellipse ,float rmax /// major axis ,float rmin /// minor axis @@ -67,14 +70,16 @@ void TreeSimple::PointsWithinEllipse( sn = sin(posangle); // go through points within the circle and reject points outside the ellipse for( std::list::iterator it = neighborlist.begin();it != neighborlist.end();){ - x = xp[*it][0]*cs - xp[*it][1]*sn; - y = xp[*it][0]*sn + xp[*it][1]*cs; + x = xxp[*it][0]*cs - xxp[*it][1]*sn; + y = xxp[*it][0]*sn + xxp[*it][1]*cs; if( ( pow(x/rmax,2) + pow(y/rmin,2) ) > 1) it = neighborlist.erase(it); else ++it; } return; } -void TreeSimple::PointsWithinCircle( + +template +void TreeSimple::PointsWithinCircle( PosType *ray /// center of circle ,float rmax /// radius of circle ,std::list &neighborlist /// output neighbor list, will be emptied if it contains anything on entry @@ -110,7 +115,8 @@ void TreeSimple::PointsWithinCircle( } /** * Used in PointsWithinKist() to walk tree.*/ -void TreeSimple::_PointsWithin(PosType *ray,float *rmax,std::list &neighborlist){ +template +void TreeSimple::_PointsWithin(PosType *ray,float *rmax,std::list &neighborlist){ int j,incell2=1; unsigned long i; @@ -137,7 +143,7 @@ void TreeSimple::_PointsWithin(PosType *ray,float *rmax,std::list current->nparticles;++i){ - for(j=0,radius=0.0;j<2;++j) radius+=pow(xp[tree->current->particles[i]][j]-ray[j],2); + for(j=0,radius=0.0;j<2;++j) radius+=pow(xxp[tree->current->particles[i]][j]-ray[j],2); if( radius < *rmax**rmax ){ neighborlist.push_back(tree->current->particles[i]); //cout << "add point to list" << neighborlist.size() << endl; @@ -189,7 +195,7 @@ void TreeSimple::_PointsWithin(PosType *ray,float *rmax,std::list current->nparticles;++i){ - for(j=0,radius=0.0;j<2;++j) radius+=pow(xp[tree->current->particles[i]][j]-ray[j],2); + for(j=0,radius=0.0;j<2;++j) radius+=pow(xxp[tree->current->particles[i]][j]-ray[j],2); if( radius < *rmax**rmax ){ neighborlist.push_back(tree->current->particles[i]); //InsertAfterCurrentKist(neighborlist,tree->current->particles[i]); @@ -228,7 +234,8 @@ void TreeSimple::_PointsWithin(PosType *ray,float *rmax,std::list +void TreeSimple::NearestNeighbors( PosType *ray /// position ,int Nneighbors /// number of neighbors to be found ,float *radius /// distance furthest neighbor found from ray[] @@ -277,8 +284,9 @@ void TreeSimple::NearestNeighbors( return; } -void TreeSimple::_NearestNeighbors(PosType *ray,PosType *rlray,int Nneighbors,unsigned long *neighbors - ,PosType *rneighbors +template +void TreeSimple::_NearestNeighbors(PosType *ray,PosType *rlray,int Nneighbors + ,unsigned long *neighbors,PosType *rneighbors ,TreeSimple::iterator &it,int ¬found) const { int incellNB2=1; @@ -377,8 +385,8 @@ void TreeSimple::_NearestNeighbors(PosType *ray,PosType *rlray,int Nneighbors,un return; } - -void TreeSimple::_NearestNeighbors(PosType *ray,int Nneighbors,unsigned long *neighbors,PosType *rneighbors){ +template +void TreeSimple::_NearestNeighbors(PosType *ray,int Nneighbors,unsigned long *neighbors,PosType *rneighbors){ int incellNB2=1; IndexType i; @@ -471,9 +479,8 @@ void TreeSimple::_NearestNeighbors(PosType *ray,int Nneighbors,unsigned long *ne return; } - - -TreeNBHndl TreeSimple::BuildTreeNB(PosType **xp,IndexType Nparticles,IndexType *particles,int Ndims +template +TreeNBHndl TreeSimple::BuildTreeNB(PType *xp,IndexType Nparticles,IndexType *particles,int Ndims ,PosType theta){ TreeNBHndl tree; IndexType i; @@ -512,7 +519,8 @@ TreeNBHndl TreeSimple::BuildTreeNB(PosType **xp,IndexType Nparticles,IndexType * // tree must be created and first branch must be set before start -void TreeSimple::_BuildTreeNB(TreeNBHndl tree,IndexType nparticles,IndexType *particles){ +template +void TreeSimple::_BuildTreeNB(TreeNBHndl tree,IndexType nparticles,IndexType *particles){ IndexType i,cut,dimension; short j; BranchNB *cbranch,branch1(Ndim),branch2(Ndim); @@ -622,7 +630,8 @@ void TreeSimple::_BuildTreeNB(TreeNBHndl tree,IndexType nparticles,IndexType *pa * Returns pointer to new BranchNB struct. Initializes children pointers to NULL, * and sets data field to input. Private. ************************************************************************/ -BranchNB *TreeSimple::NewBranchNB(IndexType *particles,IndexType nparticles +template +BranchNB *TreeSimple::NewBranchNB(IndexType *particles,IndexType nparticles ,PosType boundary_p1[],PosType boundary_p2[] ,PosType center[],int level,unsigned long branchNBnumber){ @@ -658,7 +667,8 @@ BranchNB *TreeSimple::NewBranchNB(IndexType *particles,IndexType nparticles * FreeBranchNB * Frees memory pointed to by branchNB. Private. ************************************************************************/ -void TreeSimple::FreeBranchNB(BranchNB *branchNB){ +template +void TreeSimple::FreeBranchNB(BranchNB *branchNB){ assert( branchNB != NULL); delete branchNB; @@ -671,7 +681,8 @@ void TreeSimple::FreeBranchNB(BranchNB *branchNB){ * Returns pointer to new TreeNB struct. Initializes top, last, and * current pointers to NULL. Sets NbranchNBes field to 0. Exported. ************************************************************************/ -TreeNBHndl TreeSimple::NewTreeNB(IndexType *particles,IndexType nparticles +template +TreeNBHndl TreeSimple::NewTreeNB(IndexType *particles,IndexType nparticles ,PosType boundary_p1[],PosType boundary_p2[], PosType center[],short Ndimensions){ @@ -696,7 +707,8 @@ TreeNBHndl TreeSimple::NewTreeNB(IndexType *particles,IndexType nparticles return(tree); } -void TreeSimple::freeTreeNB(TreeNBHndl tree){ +template +void TreeSimple::freeTreeNB(TreeNBHndl tree){ /* free treeNB * does not free the particle positions, masses or rsph */ @@ -710,7 +722,8 @@ void TreeSimple::freeTreeNB(TreeNBHndl tree){ return; } -short TreeSimple::emptyTreeNB(TreeNBHndl tree){ +template +short TreeSimple::emptyTreeNB(TreeNBHndl tree){ moveTopNB(tree); _freeTreeNB(tree,0); @@ -720,7 +733,8 @@ short TreeSimple::emptyTreeNB(TreeNBHndl tree){ return 1; } -void TreeSimple::_freeTreeNB(TreeNBHndl tree,short child){ +template +void TreeSimple::_freeTreeNB(TreeNBHndl tree,short child){ BranchNB *branch; assert( tree ); @@ -762,7 +776,8 @@ void TreeSimple::_freeTreeNB(TreeNBHndl tree,short child){ * isEmptyNB * Returns "true" if the TreeNB is empty and "false" otherwise. Exported. ************************************************************************/ -bool TreeSimple::isEmptyNB(TreeNBHndl tree){ +template +bool TreeSimple::isEmptyNB(TreeNBHndl tree){ assert(tree != NULL); return(tree->Nbranches == 0); @@ -774,7 +789,8 @@ bool TreeSimple::isEmptyNB(TreeNBHndl tree){ * Exported. * Pre: !isEmptyNB(tree) ************************************************************************/ -bool TreeSimple::atTopNB(TreeNBHndl tree){ +template +bool TreeSimple::atTopNB(TreeNBHndl tree){ assert(tree != NULL); if( isEmptyNB(tree) ){ @@ -791,7 +807,8 @@ bool TreeSimple::atTopNB(TreeNBHndl tree){ * Exported. * Pre: !isEmptyNB(tree) ************************************************************************/ -bool TreeSimple::noChildNB(TreeNBHndl tree){ +template +bool TreeSimple::noChildNB(TreeNBHndl tree){ assert(tree != NULL); if( isEmptyNB(tree) ){ @@ -808,7 +825,8 @@ bool TreeSimple::noChildNB(TreeNBHndl tree){ * offEndNB * Returns "true" if current is off end and "false" otherwise. Exported. ************************************************************************/ -bool TreeSimple::offEndNB(TreeNBHndl tree){ +template +bool TreeSimple::offEndNB(TreeNBHndl tree){ assert(tree != NULL); return(tree->current == NULL); @@ -819,7 +837,8 @@ bool TreeSimple::offEndNB(TreeNBHndl tree){ * Returns the particuls of current. Exported. * Pre: !offEndNB(tree) ************************************************************************/ -void TreeSimple::getCurrentNB(TreeNBHndl tree,IndexType *particles,IndexType *nparticles){ +template +void TreeSimple::getCurrentNB(TreeNBHndl tree,IndexType *particles,IndexType *nparticles){ assert(tree != NULL); if( offEndNB(tree) ){ @@ -838,7 +857,8 @@ void TreeSimple::getCurrentNB(TreeNBHndl tree,IndexType *particles,IndexType *np * getNbranchesNB * Returns the NbranchNBes of tree. Exported. ************************************************************************/ -unsigned long TreeSimple::getNbranchesNB(TreeNBHndl tree){ +template +unsigned long TreeSimple::getNbranchesNB(TreeNBHndl tree){ assert(tree != NULL); return(tree->Nbranches); @@ -851,7 +871,8 @@ unsigned long TreeSimple::getNbranchesNB(TreeNBHndl tree){ * Moves current to the front of tree. Exported. * Pre: !isEmptyNB(tree) ************************************************************************/ -void TreeSimple::moveTopNB(TreeNBHndl tree){ +template +void TreeSimple::moveTopNB(TreeNBHndl tree){ //std::cout << tree << std::endl; //std::cout << tree->current << std::endl; //std::cout << tree->top << std::endl; @@ -875,7 +896,8 @@ void TreeSimple::moveTopNB(TreeNBHndl tree){ * off end. Exported. * Pre: !offEndNB(tree) ************************************************************************/ -void TreeSimple::moveUpNB(TreeNBHndl tree){ +template +void TreeSimple::moveUpNB(TreeNBHndl tree){ assert(tree != NULL); if( offEndNB(tree) ){ @@ -897,7 +919,8 @@ void TreeSimple::moveUpNB(TreeNBHndl tree){ * end. Exported. * Pre: !offEndNB(tree) ************************************************************************/ -void TreeSimple::moveToChildNB(TreeNBHndl tree,int child){ +template +void TreeSimple::moveToChildNB(TreeNBHndl tree,int child){ assert(tree != NULL); if( offEndNB(tree) ){ @@ -928,7 +951,8 @@ void TreeSimple::moveToChildNB(TreeNBHndl tree,int child){ * data field of the new BranchNB to input. Exported. * Pre: !offEndNB(tree) ************************************************************************/ -void TreeSimple::insertChildToCurrentNB(TreeNBHndl tree, IndexType *particles,IndexType nparticles +template +void TreeSimple::insertChildToCurrentNB(TreeNBHndl tree, IndexType *particles,IndexType nparticles ,PosType boundary_p1[],PosType boundary_p2[] ,PosType center[],int child){ @@ -972,8 +996,8 @@ void TreeSimple::insertChildToCurrentNB(TreeNBHndl tree, IndexType *particles,In } /* same as above but takes a branchNB structure */ - -void TreeSimple::attachChildToCurrentNB(TreeNBHndl tree,BranchNB &data,int child){ +template +void TreeSimple::attachChildToCurrentNB(TreeNBHndl tree,BranchNB &data,int child){ insertChildToCurrentNB(tree,data.particles,data.nparticles ,data.boundary_p1,data.boundary_p2,data.center,child); @@ -981,7 +1005,8 @@ void TreeSimple::attachChildToCurrentNB(TreeNBHndl tree,BranchNB &data,int child } // step for walking tree by iteration instead of recursion -bool TreeSimple::TreeNBWalkStep(TreeNBHndl tree,bool allowDescent){ +template +bool TreeSimple::TreeNBWalkStep(TreeNBHndl tree,bool allowDescent){ if(allowDescent && tree->current->child1 != NULL){ moveToChildNB(tree,1); return true; diff --git a/TreeCode_link/utilities.cpp b/TreeCode_link/utilities.cpp index e0ef6a56..58f35794 100644 --- a/TreeCode_link/utilities.cpp +++ b/TreeCode_link/utilities.cpp @@ -183,7 +183,7 @@ long IndexFromPosition(PosType x,long Npixels,PosType range,PosType center){ * Out of bounds points return 0. map is a i dimensional array representing a 2 dimensional map. * Don't use init. * After it is used once, later calls can use TwoDInterpolator(PosType *map) for the same point - * in the same coordinate system to save time in calculating the idndexes. + * in the same coordinate system to save time in calculating the indexes. */ PosType TwoDInterpolator( PosType *x diff --git a/include/CMakeLists.txt b/include/CMakeLists.txt index ba339e68..9fb82ad4 100644 --- a/include/CMakeLists.txt +++ b/include/CMakeLists.txt @@ -39,7 +39,7 @@ add_sources( Tree.h #TreeKist.h tree_maintenance.h - TreeNB.h + qTreeNB.h uniform_lens.h utilities_slsim.h geometry.h @@ -50,4 +50,6 @@ add_sources( bands_etc.h gaussian.h gadgetio.hpp + gadget.hh + gensim.hh ) diff --git a/include/TreeNB.h b/include/TreeNB.h deleted file mode 100644 index dae72aa7..00000000 --- a/include/TreeNB.h +++ /dev/null @@ -1,33 +0,0 @@ - /* - * Code Name: treeNB.h - * Programmer: Ben Metcalf - * Discription: - * Comments: - */ - -#ifndef treeNBtypes_declare -#define treeNBtypes_declare - -#include -#include - -#ifndef PI -#define PI 3.141593 -#endif - -#ifndef treeNBdim -#define treeNBdim 2 // dimension of boxes in tree -#endif - -/** type for particle positions and boundaries etc **/ - -#ifndef IndexType_declare -#define IndexType_declare -typedef unsigned long IndexType; -#endif - -namespace Utilities{ - //PosType **PosTypeMatrix(long nrl, long nrh, long ncl, long nch); - //void free_PosTypeMatrix(PosType **m, long nrl, long nrh, long ncl, long nch); -} -#endif diff --git a/include/base_analens.h b/include/base_analens.h index 1dd935f4..3b16dd73 100644 --- a/include/base_analens.h +++ b/include/base_analens.h @@ -11,6 +11,7 @@ #include "quadTree.h" #include "source.h" #include "InputParams.h" +#include "lens_halos.h" /** * \brief An "analytic" model to represent a lens on a single plane. @@ -98,7 +99,7 @@ class LensHaloBaseNSIE : public LensHalo{ PosType sub_Mmin; PosType sub_theta_force; LensHalo *subs; - TreeQuad *sub_tree; + TreeQuadHalos *sub_tree; IndexType *sub_substructures; ClumpInternal main_sub_type; diff --git a/include/gadgetio.hpp b/include/gadgetio.hpp index 85fc0373..c4ac29fa 100644 --- a/include/gadgetio.hpp +++ b/include/gadgetio.hpp @@ -47,4 +47,9 @@ void load_snapshot_gadget2(char *fname /// base name of snapshot ,int &Ngas /// number of gas particles ); +void load_snapshot_gadget2(char *fname + ,int nfiles + ,std::vector > &xp + ,std::vector &ntypes + ); #endif /* gadgetio_hpp */ diff --git a/include/grid_maintenance.h b/include/grid_maintenance.h index a2838c7b..f4cc154a 100644 --- a/include/grid_maintenance.h +++ b/include/grid_maintenance.h @@ -11,6 +11,7 @@ #include "lens.h" #include "point.h" #include "Tree.h" +#include "source.h" #include #include diff --git a/include/lens.h b/include/lens.h index 42e205c3..ff8a485d 100644 --- a/include/lens.h +++ b/include/lens.h @@ -1,8 +1,6 @@ /* * multiplane.h * - * Created on: Jan 12, 2012 - * Author: mpetkova */ #ifndef MULTIPLANE_H_ diff --git a/include/lens_halos.h b/include/lens_halos.h index b929b2f6..a0a75351 100644 --- a/include/lens_halos.h +++ b/include/lens_halos.h @@ -8,14 +8,11 @@ #ifndef LENS_HALOS_H_ #define LENS_HALOS_H_ -#include "standard.h" +//#include "standard.h" #include "InputParams.h" -#include "source.h" -#include "point.h" - -//#include "quadTree.h" - -class TreeQuad; +//#include "source.h" +//#include "point.h" +#include "quadTree.h" /** * \brief A base class for all types of lensing halos. @@ -58,8 +55,8 @@ class LensHalo{ /// get the Rsize which is the size of the halo in Mpc inline float getRsize() const { return Rsize; } /// get the mass solar units - inline float get_mass() const { return mass; } - /// get the scale radius in Mpc + inline float get_mass() const { return mass; } + /// get the scale radius in Mpc inline float get_rscale() const { return rscale; } /// get the redshift inline PosType getZlens() const { return zlens; } @@ -76,6 +73,9 @@ class LensHalo{ MyPosHalo[1] = posHalo[1]*Dist; } + /// returns position of the Halo in physical Mpc on the lens plane + PosType operator[](int i) const{return posHalo[0]*Dist;} + /// set the position of the Halo in radians void setTheta(PosType PosX, PosType PosY) { posHalo[0] = PosX ; posHalo[1] = PosY ; } /// set the position of the Halo in radians @@ -149,7 +149,7 @@ class LensHalo{ /// Fraction of surface density in stars PosType getFstars() const {return star_fstars;} /// The mass of the stars if they are all the same mass - PosType getStarMass() const {if(stars_implanted)return star_masses[0]; else return 0.0;} + PosType getStarMass() const {if(stars_implanted)return stars_xp[0].mass(); else return 0.0;} /// the method used to ellipticize a halo if fratio!=1 and halo is not NSIE EllipMethod getEllipMethod() const {return main_ellip_method;} @@ -169,7 +169,7 @@ class LensHalo{ virtual void printCSV(std::ostream&, bool header = false) const; /// Prints star parameters; if show_stars is true, prints data for single stars - void PrintStars(bool show_stars) const; + void PrintStars(bool show_stars); PosType MassBy2DIntegation(PosType R); PosType MassBy1DIntegation(PosType R); @@ -245,12 +245,12 @@ class LensHalo{ }; IndexType *stars; - PosType **stars_xp; - TreeQuad *star_tree; + std::vector stars_xp; + TreeQuadParticles *star_tree; int stars_N; PosType star_massscale; /// star masses relative to star_massscles - float *star_masses; + //float *star_masses; PosType star_fstars; PosType star_theta_force; int star_Nregions; @@ -258,7 +258,7 @@ class LensHalo{ PosType beta; void substract_stars_disks(PosType const *ray,PosType *alpha ,KappaType *kappa,KappaType *gamma); - float* stellar_mass_function(IMFtype type, unsigned long Nstars, long *seed, PosType minmass=0.0, PosType maxmass=0.0 + std::vector stellar_mass_function(IMFtype type, unsigned long Nstars, long *seed, PosType minmass=0.0, PosType maxmass=0.0 ,PosType bendmass=0.0, PosType powerlo=0.0, PosType powerhi=0.0); diff --git a/include/particle_halo.h b/include/particle_halo.h index a23f0f61..c4193a76 100644 --- a/include/particle_halo.h +++ b/include/particle_halo.h @@ -5,11 +5,45 @@ // Created by bmetcalf on 16/06/15. // // -#include "quadTree.h" #ifndef GLAMER_particle_halo_h #define GLAMER_particle_halo_h +#include "geometry.h" +#include "quadTree.h" +#include "simpleTree.h" + +/// Atomic data class for simulation particles with individual sizes and masses +template +struct ParticleData{ + T &operator[](int i){return x[i];} + T *operator*(){return x;} + T x[3]; + float Mass; + float Size; + int type; + + float size(){return Size;} + float mass(){return Mass;} +}; + +/// Atomic data class for simulation particles of the same size and mass +struct ParticleDataSimple{ + float &operator[](int i){return x[i];} + float *operator*(){return x;} + float x[3]; + + static float Mass; + static float Size; + + float size(){return ParticleDataSimple::Size;} + float mass(){return ParticleDataSimple::Mass;} +}; +float ParticleDataSimple::Size = 0; +float ParticleDataSimple::Mass = 0; + +enum SimFileFormats {ascii,gadget2}; + /** * \brief A class that represents the lensing by a collection of simulation particles. @@ -26,20 +60,23 @@ More input formats will be added in the future. */ + +template class LensHaloParticles : public LensHalo { public: - LensHaloParticles( - const std::string& simulation_filename /// name of file containing particles - ,PosType redshift /// redshift of particles - ,int Nsmooth /// number of neighbours for the smoothing - ,const COSMOLOGY& lenscosmo /// cosmology - ,Point_2d theta_rotate /// rotation of simulation, x-axis ratation angle and then y-axis rotation angle - ,bool recenter = false /// re-center the coordinates to the center of mass - ,bool multimass = false /** allows the particles to have different masses - , the masses for each particles must be provided as a 4th column in the particle data file */ - ,PosType MinPSize = 0.0 /// Minimum smoothing size of particles - ); + + + LensHaloParticles(const std::string& simulation_filename /// name of data files + ,SimFileFormats format /// format of data file + ,PosType redshift /// redshift of origin + ,int Nsmooth /// number of neighbours for adaptive smoothing + ,const COSMOLOGY& cosmo /// cosmology + ,Point_2d theta_rotate /// rotation of particles around the origin + ,bool recenter /// center on center of mass + ,bool my_multimass /// set to true is particles have different sizes + ,PosType MinPSize /// minimum particle size + ); ~LensHaloParticles(); @@ -53,6 +90,7 @@ class LensHaloParticles : public LensHalo /// center of mass in input coordinates Point_3d CenterOfMass(){return mcenter;} + /** \brief This is a test class that makes a truncated SIE out of particles and puts it into a file in the right format for constructing a LensHaloParticles. This is useful for calculating the level of shot noise and finite source size. The particles are distributed in 3D according to the SIE profile with only the perpendicular coordinates (1st and 2nd) distorted into an elliptical shape. If the halo is rotated from the original orientation it will not be a oblate spheroid. @@ -67,23 +105,37 @@ class LensHaloParticles : public LensHalo ,Utilities::RandomNumbers_NR &ran ); + static void calculate_smoothing(int Nsmooth + ,std::vector &xxp + ); + + static void readPositionFileASCII(const std::string &filename + ,bool multimass + ,std::vector &xxp + ); + + static void writeSizes(const std::string &filename + ,int Nsmooth + ,const std::vector &xxp + ); + private: + Point_3d mcenter; void rotate_particles(PosType theta_x,PosType theta_y); - void calculate_smoothing(int Nsmooth); - void smooth_(TreeSimple *tree3d,PosType **xp,float *sizes,size_t N,int Nsmooth); - - void readPositionFileASCII(const std::string& filename); + static void smooth_(TreeSimple *tree3d,PType *xp,size_t N,int Nsmooth); + bool readSizesFile(const std::string& filename,int Nsmooth,PosType min_size); - void writeSizes(const std::string& filename,int Nsmooth); void assignParams(InputParams& params); - PosType **xp; - std::vector masses; - std::vector sizes; + std::vector xxp; + //PosType **xp; + //std::vector masses; + //std::vector sizes; + PosType min_size; bool multimass; @@ -94,7 +146,7 @@ class LensHaloParticles : public LensHalo std::string simfile; std::string sizefile; - TreeQuad * qtree; + TreeQuadParticles * qtree; }; diff --git a/include/planes.h b/include/planes.h index 0e8ed31e..2db344a0 100644 --- a/include/planes.h +++ b/include/planes.h @@ -9,6 +9,7 @@ #define PLANES_H_ #include "quadTree.h" +#include "lens_halos.h" /// Base class representing a plane in redshift onto which lenses are placed. class LensPlane{ @@ -48,7 +49,7 @@ class LensPlaneTree : public LensPlane{ private: std::vector halos; - TreeQuad* halo_tree; + TreeQuadHalos * halo_tree; }; /** \brief A LensPlane with a list of LensHalo's in it. diff --git a/include/qTreeNB.h b/include/qTreeNB.h new file mode 100644 index 00000000..845a82ed --- /dev/null +++ b/include/qTreeNB.h @@ -0,0 +1,545 @@ +// +// qTreeNB.h +// GLAMER +// +// Created by Ben Metcalf on 11/11/2018. +// + +#ifndef qTreeNB_h +#define qTreeNB_h + +#ifndef PI +#define PI 3.141593 +#endif + +#ifndef treeNBdim +#define treeNBdim 2 // dimension of boxes in tree +#endif + +/** type for particle positions and boundaries etc **/ + +#ifndef IndexType_declare +#define IndexType_declare +typedef unsigned long IndexType; +#endif + +/** \brief Box representing a branch in a tree. It has four children. Used in QTreeNB which is used in TreeQuad. + */ +struct QBranchNB{ + QBranchNB(){ + static unsigned long n=0; + child0 = NULL; + child1 = NULL; + child2 = NULL; + child3 = NULL; + prev = NULL; + brother = NULL; + particles = NULL; + big_particles = NULL; + nparticles = 0; + number = n; + ++n; + }; + + ~QBranchNB(){}; + + /// array of particles in QBranchNB + IndexType *particles; + IndexType *big_particles; + IndexType nparticles; + /// the number of particles that aren't in children + IndexType Nbig_particles; + /// Size of largest particle in branch + PosType maxrsph; + /// center of mass + PosType center[2]; + PosType mass; + /// level in tree + int level; + unsigned long number; + /// bottom, left, back corner of box + PosType boundary_p1[2]; + /// top, right, front corner of box + PosType boundary_p2[2]; + QBranchNB *child0; + QBranchNB *child1; + QBranchNB *child2; + QBranchNB *child3; + + /// father of branch + QBranchNB *prev; + /// Either child2 of father is branch is child1 and child2 exists or the brother of the father. + /// Used for iterative tree walk. + QBranchNB *brother; + + /* projected quantities */ + /// quadropole moment of branch + PosType quad[3]; + /// largest dimension of box + PosType rmax; + /// the critical distance below which a branch is opened in the + PosType rcrit_angle; + /* force calculation */ + PosType rcrit_part; + //PosType cm[2]; /* projected center of mass */ + +}; + +/** \brief + * QTreeNB: Tree structure used for force calculation with particles (i.e. stars, Nbody or halos). + * + * The tree also contains pointers to the list of positions, sizes and masses of the particles. + * Also flags for the number of dimensions the tree is defined in (2 or 3), and if multiple + * masses and sizes should be used. + */ +template +struct QTreeNB{ + QTreeNB(PType *xp,IndexType *particles,IndexType nparticles + ,PosType boundary_p1[],PosType boundary_p2[]); + ~QTreeNB(); + + const bool isEmpty(); + const bool atTop(); + const bool noChild(); + const bool offEnd(); + const unsigned long getNbranches(); + const bool atLeaf(QBranchNB *branch){ + return (branch->child0 == NULL)*(branch->child1 == NULL) + *(branch->child2 == NULL)*(branch->child3 == NULL); + } + + + void getCurrent(IndexType *particles,IndexType *nparticles); + void moveTop(); + void moveUp(); + void moveToChild(int child); + bool WalkStep(bool allowDescent); + + short empty(); + void attachChildrenToCurrent(QBranchNB *branch0,QBranchNB *branch1 + ,QBranchNB *branch2,QBranchNB *branch3); + + QBranchNB *top; + QBranchNB *current; + /// Array of particle positions + PType *xxp; + +private: + /// number of branches in tree + unsigned long Nbranches; + void _freeQTree(short child); +}; + +/************************************************************************ + * This constructor just creates a root branch. To construct a full tree + * an external function must be used. For example, TreeQuad uses QTreeNB + ************************************************************************/ +template +QTreeNB::QTreeNB(PType *xp,IndexType *particles,IndexType nparticles + ,PosType boundary_p1[],PosType boundary_p2[]): xxp(xp) { + + top = new QBranchNB(); + + top->boundary_p1[0] = boundary_p1[0]; + top->boundary_p1[1] = boundary_p1[1]; + top->boundary_p2[0] = boundary_p2[0]; + top->boundary_p2[1] = boundary_p2[1]; + + top->nparticles = nparticles; + top->level = 0; + top->particles = particles; + + top->center[0] = (boundary_p1[0] + boundary_p2[0])/2; + top->center[1] = (boundary_p1[1] + boundary_p2[1])/2; + + Nbranches = 1; + current = top; + +} + + +/** + * \brief A iterator class fore TreeStruct that allows for movement through the tree without changing + * anything in the tree itself. + * + * This class should be able to preform all of the constant movements within the tree without causing + * any change to the tree. + */ +template +class QBiterator{ + +private: + QBranchNB *current; + QBranchNB *top; + +public: + /// Sets the top or root to the top of "tree". + QBiterator(QTreeNB * tree){current = top = tree->top;} + /// Sets the root to the input branch so that this will be a subtree in branch is not the real root. + QBiterator(QBranchNB *branch){current = top = branch;} + + /// Returns a pointer to the current Branch. + QBranchNB *operator*(){return current;} + + void movetop(){current = top;} + + /// Same as up() + bool operator++(){ return up();} + + /// Same as up() + bool operator++(int){ return up();} + + bool up(); + /// Move to child + bool down(short child); + const bool atLeaf(){ + return (current->child0 == NULL)*(current->child1 == NULL) + *(current->child2 == NULL)*(current->child3 == NULL); + } + bool TreeWalkStep(bool allowDescent); +}; + + + +/// Free treeNB. Does not free the particle positions, masses or sizes +template +QTreeNB::~QTreeNB(){ + // void TreeQuad::freeQTreeNB(QTreeNBHndl tree){ + + empty(); + delete top; + + return; +} + +template +short QTreeNB::empty(){ + + if(Nbranches <= 1) return 1; + moveTop(); + _freeQTree(0); + + assert(Nbranches == 1); + + return 1; +} + +template +void QTreeNB::_freeQTree(short child){ + QBranchNB *branch; + + assert(current); + if(current->particles != current->big_particles + && current->big_particles != NULL) delete[] current->big_particles; + + if(current->child0 != NULL){ + moveToChild(0); + _freeQTree(0); + } + + if(current->child1 != NULL){ + moveToChild(1); + _freeQTree(1); + } + + if(current->child2 != NULL){ + moveToChild(2); + _freeQTree(2); + } + + if(current->child3 != NULL){ + moveToChild(3); + _freeQTree(3); + } + + if( atLeaf(current) ){ + + if(atTop()) return; + + branch = current; + moveUp(); + delete branch; + + /*printf("*** removing branch %i number of branches %i\n",branch->number + ,Nbranches-1);*/ + + if(child==0) current->child0 = NULL; + if(child==1) current->child1 = NULL; + if(child==2) current->child2 = NULL; + if(child==3) current->child3 = NULL; + + --Nbranches; + + return; + } + + return; +} + +/************************************************************************ + * isEmpty + * Returns "true" if the QTreeNB is empty and "false" otherwise. Exported. + ************************************************************************/ +template +const bool QTreeNB::isEmpty(){ + + return(Nbranches == 0); +} + +/************************************************************************ + * atTop + * Returns "true" if current is the same as top and "false" otherwise. + * Exported. + * Pre: !isEmptyNB(tree) + ************************************************************************/ +template +const bool QTreeNB::atTop(){ + + if( isEmpty() ){ + ERROR_MESSAGE(); + std::cout << "QTreeNB Error: calling atTop() on empty tree" << std::endl; + exit(1); + } + return(current == top); +} + +/************************************************************************ + * offEndNB + * Returns "true" if current is off end and "false" otherwise. Exported. + ************************************************************************/ +template +const bool QTreeNB::offEnd(){ + + return(current == NULL); +} + +/************************************************************************ + * getCurrentNB + * Returns the particles of current. Exported. + * Pre: !offEnd() + ************************************************************************/ +template +void QTreeNB::getCurrent(IndexType *particles,IndexType *nparticles){ + + if( offEnd() ){ + ERROR_MESSAGE(); + std::cout << "QTreeNB Error: calling getCurrent() when current is off end" << std::endl; + exit(1); + } + + *nparticles = current->nparticles; + particles = current->particles; + + return; +} + +/************************************************************************ + * getNbranches + * Returns the NbranchNBes of tree. Exported. + ************************************************************************/ +template +const unsigned long QTreeNB::getNbranches(){ + + return(Nbranches); +} + +/***** Manipulation procedures *****/ + +/************************************************************************ + * moveTop + * Moves current to the front of tree. Exported. + * Pre: !isEmpty(tree) + ************************************************************************/ +template +void QTreeNB::moveTop(){ + //std::cout << tree << std::endl; + //std::cout << tree->current << std::endl; + //std::cout << tree->top << std::endl; + + if( isEmpty() ){ + ERROR_MESSAGE(); + std::cout << "QTreeNB Error: calling moveTop() on empty tree" << std::endl; + exit(1); + } + + current = top; + + return; +} + +/************************************************************************ + * movePrev + * Moves current to the branchNB before it in tree. This can move current + * off end. Exported. + * Pre: !offEndNB(tree) + ************************************************************************/ +template +void QTreeNB::moveUp(){ + + if( offEnd() ){ + ERROR_MESSAGE(); + std::cout << "QTreeNB Error: call to moveUp() when current is off end" << std::endl; + exit(1); + } + if( current == top ){ + ERROR_MESSAGE(); + std::cout << "QTreeNB Error: call to moveUp() tried to move off the top" << std::endl; + exit(1); + } + + current = current->prev; /* can move off end */ + return; +} + +/************************************************************************ + * moveToChild + * Moves current to child branchNB after it in tree. This can move current off + * end. Exported. + * Pre: !offEnd(tree) + ************************************************************************/ +template +void QTreeNB::moveToChild(int child){ + + if( offEnd() ){ + ERROR_MESSAGE(); std::cout << "QTreeNB Error: calling moveChildren() when current is off end" << std::endl; + exit(1); + } + if(child==0){ + if( current->child0 == NULL ){ + ERROR_MESSAGE(); std::cout << "QTreeNB Error: moveToChild() typing to move to child1 when it doesn't exist" << std::endl; + exit(1); + } + current = current->child0; + } + if(child==1){ + if( current->child1 == NULL ){ + ERROR_MESSAGE(); std::cout << "QTreeNB Error: moveToChild() typing to move to child1 when it doesn't exist" << std::endl; + exit(1); + } + current = current->child1; + } + if(child==2){ + if( current->child2 == NULL ){ + ERROR_MESSAGE(); std::cout << "QTreeNB Error: moveToChild() typing to move to child2 when it doesn't exist" << std::endl; + exit(1); + } + current = current->child2; + } + if(child==3){ + if( current->child3 == NULL ){ + ERROR_MESSAGE(); std::cout << "QTreeNB Error: moveToChild() typing to move to child2 when it doesn't exist" << std::endl; + exit(1); + } + current = current->child3; + } + return; +} + +template +void QTreeNB::attachChildrenToCurrent(QBranchNB *branch0,QBranchNB *branch1 + ,QBranchNB *branch2,QBranchNB *branch3){ + + Nbranches += 4; + current->child0 = branch0; + current->child1 = branch1; + current->child2 = branch2; + current->child3 = branch3; + + int level = current->level+1; + branch0->level = level; + branch1->level = level; + branch2->level = level; + branch3->level = level; + + // work out brothers for children + branch0->brother = branch1; + branch1->brother = branch2; + branch2->brother = branch3; + branch3->brother = current->brother; + + branch0->prev = current; + branch1->prev = current; + branch2->prev = current; + branch3->prev = current; + + return; +} + +// step for walking tree by iteration instead of recursion +template +bool QTreeNB::WalkStep(bool allowDescent){ + if(allowDescent && current->child0 != NULL){ + moveToChild(0); + return true; + } + + if(current->brother != NULL){ + current=current->brother; + return true; + } + return false; +} + +template +bool QBiterator::up(){ + if( current == top ){ + return false; + } + current = current->prev; /* can move off end */ + return true; +} + +/// Move to child +template +bool QBiterator::down(short child){ + + if(child==0){ + if( current->child0 == NULL ){ + ERROR_MESSAGE(); std::cout << "QTreeNB Error: moveToChild() typing to move to child1 when it doesn't exist" << std::endl; + return false; + } + current = current->child0; + return true; + } + if(child==1){ + if( current->child1 == NULL ){ + ERROR_MESSAGE(); std::cout << "QTreeNB Error: moveToChild() typing to move to child1 when it doesn't exist" << std::endl; + return false; + } + current = current->child1; + return true; + } + if(child==2){ + if( current->child2 == NULL ){ + ERROR_MESSAGE(); std::cout << "QTreeNB Error: moveToChild() typing to move to child2 when it doesn't exist" << std::endl; + return false; + } + current = current->child2; + return true; + } + if(child==3){ + if( current->child3 == NULL ){ + ERROR_MESSAGE(); std::cout << "QTreeNB Error: moveToChild() typing to move to child2 when it doesn't exist" << std::endl; + return false; + } + current = current->child3; + return true; + } + return true; +} + +template +bool QBiterator::TreeWalkStep(bool allowDescent){ + if(allowDescent && current->child0 != NULL){ + down(0); + return true; + } + + if(current->brother != NULL){ + current=current->brother; + return true; + } + return false; +} + +#endif /* qTreeNB_h */ diff --git a/include/quadTree.h b/include/quadTree.h index 067aaeae..3c16c70e 100644 --- a/include/quadTree.h +++ b/include/quadTree.h @@ -4,153 +4,43 @@ * Created on: Oct 14, 2011 * Author: bmetcalf */ +//********************************************************************************************************** +/* + * quadTree.cpp + * + * Created on: Sep 4, 2012 + * Author: mpetkova + */ +/* + * Programmer: R Ben Metcalf + */ #ifndef QUAD_TREE_H_ #define QUAD_TREE_H_ -#include "simpleTree.h" +//#include "utilities_slsim.h" +//#include "utilities.h" +#include "Tree.h" +#include "qTreeNB.h" +//#include "lens_halos.h" //short const treeNBdim = 2; -/** \brief Box representing a branch in a tree. It has four children. Used in QTreeNB which is used in TreeQuad. - */ -struct QBranchNB{ - QBranchNB(); - ~QBranchNB(); - - /// array of particles in QBranchNB - IndexType *particles; - IndexType *big_particles; - IndexType nparticles; - /// the number of particles that aren't in children - IndexType Nbig_particles; - /// Size of largest particle in branch - PosType maxrsph; - /// center of mass - PosType center[2]; - PosType mass; - /// level in tree - int level; - unsigned long number; - /// bottom, left, back corner of box - PosType boundary_p1[2]; - /// top, right, front corner of box - PosType boundary_p2[2]; - QBranchNB *child0; - QBranchNB *child1; - QBranchNB *child2; - QBranchNB *child3; - - /// father of branch - QBranchNB *prev; - /// Either child2 of father is branch is child1 and child2 exists or the brother of the father. - /// Used for iterative tree walk. - QBranchNB *brother; - - /* projected quantities */ - /// quadropole moment of branch - PosType quad[3]; - /// largest dimension of box - PosType rmax; - /// the critical distance below which a branch is opened in the - PosType rcrit_angle; - /* force calculation */ - PosType rcrit_part; - //PosType cm[2]; /* projected center of mass */ - -}; - -/** \brief - * QTreeNB: Tree structure used for force calculation with particles (i.e. stars, Nbody or halos). - * - * The tree also contains pointers to the list of positions, sizes and masses of the particles. - * Also flags for the number of dimensions the tree is defined in (2 or 3), and if multiple - * masses and sizes should be used. - */ -struct QTreeNB{ - QTreeNB(PosType **xp,IndexType *particles,IndexType nparticles - ,PosType boundary_p1[],PosType boundary_p2[]); - ~QTreeNB(); - - const bool isEmpty(); - const bool atTop(); - const bool noChild(); - const bool offEnd(); - const unsigned long getNbranches(); - const bool atLeaf(QBranchNB *branch){ - return (branch->child0 == NULL)*(branch->child1 == NULL) - *(branch->child2 == NULL)*(branch->child3 == NULL); - } - - - void getCurrent(IndexType *particles,IndexType *nparticles); - void moveTop(); - void moveUp(); - void moveToChild(int child); - bool WalkStep(bool allowDescent); - - short empty(); - void attachChildrenToCurrent(QBranchNB *branch0,QBranchNB *branch1 - ,QBranchNB *branch2,QBranchNB *branch3); - - QBranchNB *top; - QBranchNB *current; - /// Array of particle positions - PosType **xp; - - /** - * \brief A iterator class fore TreeStruct that allows for movement through the tree without changing - * anything in the tree itself. - * - * This class should be able to preform all of the constant movements within the tree without causing - * any change to the tree. - */ - class iterator{ - - private: - QBranchNB *current; - QBranchNB *top; - - public: - /// Sets the top or root to the top of "tree". - iterator(QTreeNB * tree){current = top = tree->top;} - /// Sets the root to the input branch so that this will be a subtree in branch is not the real root. - iterator(QBranchNB *branch){current = top = branch;} - - /// Returns a pointer to the current Branch. - QBranchNB *operator*(){return current;} - - void movetop(){current = top;} - - /// Same as up() - bool operator++(){ return up();} - - /// Same as up() - bool operator++(int){ return up();} - - bool up(); - /// Move to child - bool down(short child); - const bool atLeaf(){ - return (current->child0 == NULL)*(current->child1 == NULL) - *(current->child2 == NULL)*(current->child3 == NULL); - } - bool TreeWalkStep(bool allowDescent); - }; - -private: - /// number of branches in tree - unsigned long Nbranches; - void _freeQTree(short child); +/// Atomic data class for stars with different masses +struct StarData{ + PosType &operator[](int i){return x[i];} + PosType *operator*(){return x;} + PosType x[3]; + float Mass; + + float mass() const {return Mass;} + static float size() {return 0;} }; -typedef struct QTreeNB * QTreeNBHndl; -typedef int QTreeNBElement; - /** - * \brief TreeQuad is a class for calculating the deflection, kappa and gamma by tree method. + * \brief TreeQuadParticles is a class for calculating the deflection, kappa and gamma by tree method. * - * TreeQuad is evolved from TreeSimple and TreeForce. It splits each cell into four equal area + * TreeQuadParticles is evolved from TreeSimple and TreeForce. It splits each cell into four equal area * subcells instead of being a binary tree like TreeSimple. When the "particles" are given sizes * the tree is built in such a way the large particles are stored in branches that are no smaller * than their size. In this way particles are stored on all levels of the tree and not just in the @@ -161,120 +51,111 @@ typedef int QTreeNBElement; * */ -class TreeQuad { +template +class TreeQuadParticles { public: - TreeQuad( - PosType **xpt - ,float *my_masses - ,float *my_sizes - ,IndexType Npoints - ,bool Multimass - ,bool Multisize - ,PosType my_sigma_background = 0 - ,int bucket = 5 - ,PosType theta_force = 0.1 + TreeQuadParticles( + PType *xpt + ,IndexType Npoints + ,float mass_fixed = -1 + ,float size_fixed = -1 + ,PosType my_sigma_background = 0 + ,int bucket = 5 + ,PosType theta_force = 0.1 ,bool my_periodic_buffer = false ,PosType my_inv_screening_scale = 0 - ); - TreeQuad( - LensHaloHndl *my_halos - ,IndexType Npoints - ,PosType my_sigma_background = 0 - ,int bucket = 5 - ,PosType theta_force = 0.1 - ,bool my_periodic_buffer = false - ,PosType my_inv_screening_scale = 0 - ); - virtual ~TreeQuad(); - - virtual void force2D(PosType const *ray,PosType *alpha,KappaType *kappa,KappaType *gamma + ); + + ~TreeQuadParticles(); + + void force2D(PosType const *ray,PosType *alpha,KappaType *kappa,KappaType *gamma ,KappaType *phi) const; - virtual void force2D_recur(const PosType *ray,PosType *alpha,KappaType *kappa + void force2D_recur(const PosType *ray,PosType *alpha,KappaType *kappa ,KappaType *gamma,KappaType *phi); /// find all points within rmax of ray in 2D void neighbors(PosType ray[],PosType rmax,std::list &neighbors) const; - void neighbors(PosType ray[],PosType rmax,std::vector &neighbors) const; + //void neighbors(PosType ray[],PosType rmax,std::vector &neighbors) const; + + void printParticlesInBranch(unsigned long number); + + void printBranchs(int level = -1); - virtual void printParticlesInBranch(unsigned long number); - - virtual void printBranchs(int level = -1); - protected: + + PType *xxp; + bool MultiMass; + double mass_fixed = 0.0; + bool MultiRadius; + double size_fixed = 0.0; - PosType **xp; - bool MultiMass; - bool MultiRadius; - float *masses; - float *sizes; - - IndexType Nparticles; - PosType sigma_background; - int Nbucket; - - PosType force_theta; - - QTreeNBHndl tree; - IndexType *index; - - bool haloON; - LensHaloHndl *halos; - - PosType realray[2]; - int incell,incell2; + IndexType Nparticles; + PosType sigma_background; + int Nbucket; + + PosType force_theta; + + QTreeNB * tree; + IndexType *index; + + //bool haloON; + //LensHaloHndl *halos; + + PosType realray[2]; + int incell,incell2; /// if true there is one layer of peridic buffering bool periodic_buffer; PosType inv_screening_scale2; - PosType original_xl; // x-axis size of simulation used for peridic buffering. Requrement that it top branch be square my make it differ from the size of top branch. + PosType original_xl; // x-axis size of simulation used for peridic buffering. Requrement that it top branch be square my make it differ from the size of top branch. PosType original_yl; // x-axis size of simulation used for peridic buffering. - QTreeNBHndl BuildQTreeNB(PosType **xp,IndexType Nparticles,IndexType *particles); - void _BuildQTreeNB(IndexType nparticles,IndexType *particles); - - inline short WhichQuad(PosType *x,QBranchNB &branch); - - //inline bool atLeaf(); - inline bool inbox(const PosType *ray,const PosType *p1,const PosType *p2){ - return (ray[0]>=p1[0])*(ray[0]<=p2[0])*(ray[1]>=p1[1])*(ray[1]<=p2[1]); - } - //int cutbox(PosType *ray,PosType *p1,PosType *p2,float rmax); - - void CalcMoments(); - void rotate_coordinates(PosType **coord); - - // Internal profiles for a Gaussian particle - virtual inline PosType alpha_h(PosType r2s2,PosType sigma) const{ - return (sigma > 0.0 ) ? ( exp(-0.5*r2s2) - 1.0 ) : -1.0; - } - virtual inline PosType kappa_h(PosType r2s2,PosType sigma) const{ - return 0.5*r2s2*exp(-0.5*r2s2); - } - virtual inline PosType gamma_h(PosType r2s2,PosType sigma) const{ - return (sigma > 0.0 ) ? (-2.0 + (2.0 + r2s2)*exp(-0.5*r2s2) ) : -2.0; - } - virtual inline PosType phi_h(PosType r2s2,PosType sigma) const{ - //ERROR_MESSAGE(); // not yet written - //exit(1); - return 0; - } - + QTreeNB * BuildQTreeNB(PType *xp,IndexType Nparticles,IndexType *particles); + void _BuildQTreeNB(IndexType nparticles,IndexType *particles); + + inline short WhichQuad(PosType *x,QBranchNB &branch); + + //inline bool atLeaf(); + inline bool inbox(const PosType *ray,const PosType *p1,const PosType *p2){ + return (ray[0]>=p1[0])*(ray[0]<=p2[0])*(ray[1]>=p1[1])*(ray[1]<=p2[1]); + } + //int cutbox(PosType *ray,PosType *p1,PosType *p2,float rmax); + + void CalcMoments(); + void rotate_coordinates(PosType **coord); + + // Internal profiles for a Gaussian particle + inline PosType alpha_h(PosType r2s2,PosType sigma) const{ + return (sigma > 0.0 ) ? ( exp(-0.5*r2s2) - 1.0 ) : -1.0; + } + inline PosType kappa_h(PosType r2s2,PosType sigma) const{ + return 0.5*r2s2*exp(-0.5*r2s2); + } + inline PosType gamma_h(PosType r2s2,PosType sigma) const{ + return (sigma > 0.0 ) ? (-2.0 + (2.0 + r2s2)*exp(-0.5*r2s2) ) : -2.0; + } + inline PosType phi_h(PosType r2s2,PosType sigma) const{ + //ERROR_MESSAGE(); // not yet written + //exit(1); + return 0; + } + /* cubic B-spline kernel for particle profile The lensing quantities are added to and a point mass is subtracted - */ + */ inline void b_spline_profile( - PosType *xcm // vector in Mpc connecting ray to center of particle - ,PosType r // distance from center in Mpc - ,PosType Mass // mass in solar masses - ,PosType size // size scale in Mpc - ,PosType *alpha // deflection angle times Sigma_crit - ,KappaType *kappa // surface density - ,KappaType *gamma // shear times Sigma_crit - ,KappaType *phi - ) const { + PosType *xcm // vector in Mpc connecting ray to center of particle + ,PosType r // distance from center in Mpc + ,PosType Mass // mass in solar masses + ,PosType size // size scale in Mpc + ,PosType *alpha // deflection angle times Sigma_crit + ,KappaType *kappa // surface density + ,KappaType *gamma // shear times Sigma_crit + ,KappaType *phi + ) const { PosType q = r/size; PosType M,sigma; @@ -297,7 +178,7 @@ class TreeQuad { )/PI; } } - + PosType alpha_r,gt; // deflection * Sig_crit / Mass alpha_r = (M-1)/PI/r; gt = alpha_r/r - sigma; @@ -315,50 +196,1084 @@ class TreeQuad { The lensing quantities are added to and a point mass is subtracted */ inline void exponential_profile( - PosType *xcm - ,PosType rcm2 // distance from center in Mpc - ,PosType Mass - ,PosType size // size scale in Mpc - ,PosType *alpha - ,KappaType *kappa - ,KappaType *gamma - ,KappaType *phi - ) const { - - - PosType prefac = Mass/rcm2/PI; - PosType arg1 = rcm2/(size*size); + PosType *xcm + ,PosType rcm2 // distance from center in Mpc + ,PosType Mass + ,PosType size // size scale in Mpc + ,PosType *alpha + ,KappaType *kappa + ,KappaType *gamma + ,KappaType *phi + ) const { + + + PosType prefac = Mass/rcm2/PI; + PosType arg1 = rcm2/(size*size); + + PosType tmp = (alpha_h(arg1,size) + 1.0)*prefac; + alpha[0] += tmp*xcm[0]; + alpha[1] += tmp*xcm[1]; + + + *kappa += kappa_h(arg1,size)*prefac; + + tmp = (gamma_h(arg1,size) + 2.0)*prefac/rcm2; + + gamma[0] += 0.5*(xcm[0]*xcm[0]-xcm[1]*xcm[1])*tmp; + gamma[1] += xcm[0]*xcm[1]*tmp; + + // TODO: makes sure the normalization of phi_h agrees with this + //*phi += (phi_h(arg1,size) + 0.5*log(rcm2))*prefac*rcm2; + } + + //QTreeNB * rotate_simulation(PType *xp,IndexType Nparticles,IndexType *particles + // ,PosType **coord,PosType theta,float *rsph,float *mass + // ,bool MultiRadius,bool MultiMass); + //QTreeNB * rotate_project(PType *xp,IndexType Nparticles,IndexType *particles + // ,PosType **coord,PosType theta,float *rsph,float *mass + // ,bool MultiRadius,bool MultiMass); + void cuttoffscale(QTreeNB * tree,PosType *theta); + + void walkTree_recur(QBranchNB *branch,PosType const *ray,PosType *alpha,KappaType *kappa,KappaType *gamma,KappaType *phi); + + void walkTree_iter(QBiterator &treeit, PosType const *ray,PosType *alpha,KappaType *kappa + ,KappaType *gamma,KappaType *phi) const; + + PosType phiintconst; +}; + + + +/** \brief Constructor meant for point particles, simulation particles + */ +template +TreeQuadParticles::TreeQuadParticles( + PType *xpt + ,IndexType Npoints + ,float mass_fixed + ,float size_fixed + ,PosType my_sigma_background /// background kappa that is subtracted + ,int bucket + ,PosType theta_force + ,bool my_periodic_buffer /// if true a periodic buffer will be imposed in the force calulation. See documentation on TreeQuadParticles::force2D() for details. See note for TreeQuadParticles::force2D_recur(). + ,PosType my_inv_screening_scale /// the inverse of the square of the sreening length. See note for TreeQuadParticles::force2D_recur(). +): +xxp(xpt) +,Nparticles(Npoints),sigma_background(my_sigma_background) +,Nbucket(bucket),force_theta(theta_force),periodic_buffer(my_periodic_buffer) +,inv_screening_scale2(my_inv_screening_scale*my_inv_screening_scale) +{ + index = new IndexType[Npoints]; + IndexType ii; + if(mass_fixed <= 0 ) MultiMass = true; else MultiMass = false; + if(size_fixed <= 0 ) MultiRadius = true; else MultiRadius = false; + + for(ii=0;ii 0){ + tree = BuildQTreeNB(xxp,Npoints,index); + CalcMoments(); + } + + phiintconst = (120*log(2.) - 631.)/840 + 19./70; + + return; +} + +/// Particle positions and other data are not destroyed. +template +TreeQuadParticles::~TreeQuadParticles() +{ + if(Nparticles == 0) return; + delete tree; + delete[] index; + return; +} + +template +QTreeNB * TreeQuadParticles::BuildQTreeNB(PType *xxp,IndexType Nparticles,IndexType *particles){ + IndexType i; + short j; + PosType p1[2],p2[2]; + + for(j=0;j<2;++j){ + p1[j]=xxp[0][j]; + p2[j]=xxp[0][j]; + } + + // Find borders that enclose all particles + for(i=0;i p2[j] ) p2[j]=xxp[i][j]; + } + } + + if(Nparticles == 1){ + p1[0]=-0.25; + p1[1]=-0.25; + p2[0]=0.25; + p2[1]=0.25; + } + + // store true dimensions of simulation + PosType lengths[2] = {p2[0]-p1[0],p2[1]-p1[1]}; + original_xl = lengths[0]; + original_yl = lengths[1]; + + if(lengths[0] == 0 || lengths[1] == 0){ + throw std::invalid_argument("particles in same place."); + } + + // If region is not square, make it square. + j = lengths[0] > lengths[1] ? 1 : 0; + p2[j] = p1[j] + lengths[!j]; + + /* Initialize tree root */ + tree = new QTreeNB(xxp,particles,Nparticles,p1,p2); + + /* build the tree */ + _BuildQTreeNB(Nparticles,particles); + + /* visit every branch to find center of mass and cutoff scale */ + tree->moveTop(); + + return tree; +} + +/// returns an index for which of the four quadrangles of the branch the point x[] is in +template +inline short TreeQuadParticles::WhichQuad(PosType *x,QBranchNB &branch){ + return (x[0] < branch.center[0]) + 2*(x[1] < branch.center[1]); +} + +/// tree must be created and first branch must be set before start +template +void TreeQuadParticles::_BuildQTreeNB(IndexType nparticles,IndexType *particles){ + + QBranchNB *cbranch = tree->current; // pointer to current branch + IndexType i,j,cut,cut2; + + cbranch->center[0] = (cbranch->boundary_p1[0] + cbranch->boundary_p2[0])/2; + cbranch->center[1] = (cbranch->boundary_p1[1] + cbranch->boundary_p2[1])/2; + cbranch->quad[0] = cbranch->quad[1]=cbranch->quad[2]=0; + cbranch->mass = 0.0; + + + // leaf case + if(cbranch->nparticles <= Nbucket){ + PosType r; + cbranch->Nbig_particles = 0; + for(i=0;inparticles;++i){ + r = xxp[particles[i]*MultiRadius].size(); + if(r < (cbranch->boundary_p2[0]-cbranch->boundary_p1[0])) ++cbranch->Nbig_particles; + } + if(cbranch->Nbig_particles){ + cbranch->big_particles = new IndexType[cbranch->Nbig_particles]; + for(i=0,j=0;inparticles;++i){ + r = xxp[particles[i]*MultiRadius].size(); + if(r < (cbranch->boundary_p2[0]-cbranch->boundary_p1[0])) cbranch->big_particles[j++] = particles[i]; + } + } + else{ + cbranch->big_particles = NULL; + } + + return; + } + + // find particles too big to be in children + + PosType *x = new PosType[cbranch->nparticles]; + + cbranch->Nbig_particles=0; + + if(MultiRadius){ + // store the particles that are too large to be in a child at the end of the list of particles in cbranch + for(i=0;inparticles;++i){ + x[i] = xxp[particles[i]].size(); + } + PosType maxsize = (cbranch->boundary_p2[0]-cbranch->boundary_p1[0])/2; + + // sort particles in size + //quicksort(particles,x,cbranch->nparticles); + Utilities::quickPartition(maxsize,&cut,particles,x,cbranch->nparticles); + + if(cut < cbranch->nparticles){ + if(tree->atTop()){ + cbranch->Nbig_particles = cut2 = cbranch->nparticles-cut; + }else{ + Utilities::quickPartition(2*maxsize,&cut2,&particles[cut],&x[cut],cbranch->nparticles-cut); + cbranch->Nbig_particles = cut2; + } + + cbranch->big_particles = new IndexType[cbranch->Nbig_particles]; + for(i=cut;i<(cut+cut2);++i) cbranch->big_particles[i-cut] = particles[i]; + } + + }else{ + //x[0] = xxp[0].Size(); + x[0] = size_fixed; + if(x[0] > (cbranch->boundary_p2[0]-cbranch->boundary_p1[0])/2 + && x[0] < (cbranch->boundary_p2[0]-cbranch->boundary_p1[0])){ + cbranch->Nbig_particles = cbranch->nparticles; + cbranch->big_particles = cbranch->particles; + } + } + + assert( cbranch->nparticles >= cbranch->Nbig_particles ); + IndexType NpInChildren = cbranch->nparticles;// - cbranch->Nbig_particles; + assert(NpInChildren >= 0); + + if(NpInChildren == 0){ + delete[] x; + return; + } + + IndexType cutx,cuty; + PosType xcut,ycut; + + QBranchNB *child0 = new QBranchNB(); + QBranchNB *child1 = new QBranchNB(); + QBranchNB *child2 = new QBranchNB(); + QBranchNB *child3 = new QBranchNB(); + + tree->attachChildrenToCurrent(child0,child1,child2,child3); + + // initialize boundaries of children + + child0->boundary_p1[0]=cbranch->center[0]; + child0->boundary_p1[1]=cbranch->center[1]; + child0->boundary_p2[0]=cbranch->boundary_p2[0]; + child0->boundary_p2[1]=cbranch->boundary_p2[1]; + + child1->boundary_p1[0]=cbranch->boundary_p1[0]; + child1->boundary_p1[1]=cbranch->center[1]; + child1->boundary_p2[0]=cbranch->center[0]; + child1->boundary_p2[1]=cbranch->boundary_p2[1]; + + child2->boundary_p1[0]=cbranch->center[0]; + child2->boundary_p1[1]=cbranch->boundary_p1[1]; + child2->boundary_p2[0]=cbranch->boundary_p2[0]; + child2->boundary_p2[1]=cbranch->center[1]; + + child3->boundary_p1[0]=cbranch->boundary_p1[0]; + child3->boundary_p1[1]=cbranch->boundary_p1[1]; + child3->boundary_p2[0]=cbranch->center[0]; + child3->boundary_p2[1]=cbranch->center[1]; + + + // **** makes sure force does not require nbucket at leaf + + xcut = cbranch->center[0]; + ycut = cbranch->center[1]; + + // divide along y direction + for(i=0;ixxp[particles[i]][1]; + Utilities::quickPartition(ycut,&cuty,particles,x,NpInChildren); + + if(cuty > 0){ + // divide first group in the x direction + for(i=0;ixxp[particles[i]][0]; + Utilities::quickPartition(xcut,&cutx,particles,x,cuty); + + child3->nparticles=cutx; + assert(child3->nparticles >= 0); + if(child3->nparticles > 0) + child3->particles = particles; + else child3->particles = NULL; + + child2->nparticles = cuty - cutx; + assert(child2->nparticles >= 0); + if(child2->nparticles > 0) + child2->particles = &particles[cutx]; + else child2->particles = NULL; + + }else{ + child3->nparticles = 0; + child3->particles = NULL; + + child2->nparticles = 0; + child2->particles = NULL; + } + + if(cuty < NpInChildren){ + // divide second group in the x direction + for(i=cuty;ixxp[particles[i]][0]; + Utilities::quickPartition(xcut,&cutx,&particles[cuty],x,NpInChildren-cuty); + + child1->nparticles=cutx; + assert(child1->nparticles >= 0); + if(child1->nparticles > 0) + child1->particles = &particles[cuty]; + else child1->particles = NULL; + + child0->nparticles=NpInChildren - cuty - cutx; + assert(child0->nparticles >= 0); + if(child0->nparticles > 0) + child0->particles = &particles[cuty + cutx]; + else child0->particles = NULL; + + }else{ + child1->nparticles = 0; + child1->particles = NULL; + + child0->nparticles = 0; + child0->particles = NULL; + } + + delete[] x; + + tree->moveToChild(0); + _BuildQTreeNB(child0->nparticles,child0->particles); + tree->moveUp(); + + tree->moveToChild(1); + _BuildQTreeNB(child1->nparticles,child1->particles); + tree->moveUp(); + + tree->moveToChild(2); + _BuildQTreeNB(child2->nparticles,child2->particles); + tree->moveUp(); + + tree->moveToChild(3); + _BuildQTreeNB(child3->nparticles,child3->particles); + tree->moveUp(); + + return; +} + +// calculates moments of the mass and the cutoff scale for each box +template +void TreeQuadParticles::CalcMoments(){ + + //*** make compatable + IndexType i; + PosType rcom,xcm[2],xcut; + QBranchNB *cbranch; + PosType tmp; + + tree->moveTop(); + do{ + cbranch=tree->current; /* pointer to current branch */ + + cbranch->rmax = sqrt( pow(cbranch->boundary_p2[0]-cbranch->boundary_p1[0],2) + + pow(cbranch->boundary_p2[1]-cbranch->boundary_p1[1],2) ); + + // calculate mass + for(i=0,cbranch->mass=0;inparticles;++i) + //if(haloON ){ + // cbranch->mass += halos[cbranch->particles[i]]->get_mass(); + //}else{ + // cbranch->mass += masses[cbranch->particles[i]*MultiMass]; + //} + cbranch->mass += xxp[cbranch->particles[i]*MultiMass].mass(); + + // calculate center of mass + cbranch->center[0]=cbranch->center[1]=0; + for(i=0;inparticles;++i){ + //if(haloON ){ + // tmp = halos[cbranch->particles[i]]->get_mass(); + //}else{ + // tmp = masses[cbranch->particles[i]*MultiMass]; + //} + tmp = xxp[cbranch->particles[i]*MultiMass].mass(); + + //tmp = haloON ? halos[cbranch->particles[i]*MultiMass].mass : masses[cbranch->particles[i]*MultiMass]; + cbranch->center[0] += tmp*tree->xxp[cbranch->particles[i]][0]/cbranch->mass; + cbranch->center[1] += tmp*tree->xxp[cbranch->particles[i]][1]/cbranch->mass; + } + ////////////////////////////////////////////// + // calculate quadropole moment of branch + ////////////////////////////////////////////// + cbranch->quad[0]=cbranch->quad[1]=cbranch->quad[2]=0; + for(i=0;inparticles;++i){ + xcm[0]=tree->xxp[cbranch->particles[i]][0]-cbranch->center[0]; + xcm[1]=tree->xxp[cbranch->particles[i]][1]-cbranch->center[1]; + xcut = pow(xcm[0],2) + pow(xcm[1],2); + //if(haloON ){ + // tmp = halos[cbranch->particles[i]]->get_mass(); + //}else{ + // tmp = masses[cbranch->particles[i]*MultiMass]; + //} + tmp = xxp[cbranch->particles[i]*MultiMass].mass(); + + cbranch->quad[0] += (xcut-2*xcm[0]*xcm[0])*tmp; + cbranch->quad[1] += (xcut-2*xcm[1]*xcm[1])*tmp; + cbranch->quad[2] += -2*xcm[0]*xcm[1]*tmp; + } + + // largest distance from center of mass of cell + for(i=0,rcom=0.0;i<2;++i) rcom += ( pow(cbranch->center[i]-cbranch->boundary_p1[i],2) > pow(cbranch->center[i]-cbranch->boundary_p2[i],2) ) ? + pow(cbranch->center[i]-cbranch->boundary_p1[i],2) : pow(cbranch->center[i]-cbranch->boundary_p2[i],2); + + rcom=sqrt(rcom); + + if(force_theta > 0.0) cbranch->rcrit_angle = 1.15470*rcom/(force_theta); + else cbranch->rcrit_angle=1.0e100; + + /*if(MultiRadius){ - PosType tmp = (alpha_h(arg1,size) + 1.0)*prefac; - alpha[0] += tmp*xcm[0]; - alpha[1] += tmp*xcm[1]; + for(i=0,cbranch->maxrsph=0.0;inparticles;++i){ + tmp = haloON ? halos[cbranch->particles[i]].Rmax : sizes[cbranch->particles[i]]; + if(cbranch->maxrsph <= tmp ){ + cbranch->maxrsph = tmp; + } + } + cbranch->rcrit_part = rcom + 2*cbranch->maxrsph; - *kappa += kappa_h(arg1,size)*prefac; + }else{ + // single size case + cbranch->maxrsph = haloON ? halos[0].Rmax : sizes[0]; + cbranch->rcrit_part = rcom + 2*cbranch->maxrsph; + }*/ + //cbranch->rcrit_angle += cbranch->rcrit_part; + + }while(tree->WalkStep(true)); + + return; +} + +/// simple rotates the coordinates in the xp array +template +void TreeQuadParticles::rotate_coordinates(PosType **coord){ + IndexType i; + short j; + PosType tmp[3]; + + /* rotate particle positions */ + for(i=0;itop->nparticles;++i){ + for(j=0;j<3;++j) tmp[j]=0.0; + for(j=0;j<3;++j){ + tmp[0]+=coord[0][j]*xxp[i][j]; + tmp[1]+=coord[1][j]*xxp[i][j]; + tmp[2]+=coord[2][j]*xxp[i][j]; + } + for(j=0;j<3;++j) xxp[i][j]=tmp[j]; + } + return; +} + +/** \brief Force2D calculates the defection, convergence and shear using + * the plane-lens approximation. The tree is walked iteratively. + * + * The output alpha[] is in units of mass_scale/Mpc, ie it needs to be + * divided by Sigma_crit and multiplied by mass_scale to be the deflection + * in the lens equation expressed on the lens plane or multiplied by + * 4*pi*G*mass_scale to get the deflection angle caused by the plane lens. + * + * kappa and gamma need to by multiplied by mass_scale/Sigma_crit to get + * the traditional units for these where Sigma_crit are in the mass/units(ray)^2 + * NB : the units of sigma_backgound need to be mass/units(ray)^2 + * + * If periodic_buffer == true a periodic buffer is included. The ray is calculated as if there + * where identical copies of it on the borders of the tree's root branch. This is not the same thing as periodic boundary conditions, because + * there are not an infinite number of copies in all direction as for the DFT force solver. + * + * If inv_screening_scale2 != 0 the mass of cells are reduced by a factor of exp(-|ray - center of mass|^2*inv_screening_scale2) which + * screens the large scale geometry of the simulation on the sky. This is useful when the region is rectangular instead of circular. + * */ + +template +void TreeQuadParticles::force2D(const PosType *ray,PosType *alpha,KappaType *kappa,KappaType *gamma,KappaType *phi) const{ + + alpha[0]=alpha[1]=gamma[0]=gamma[1]=gamma[2]=0.0; + *kappa=*phi=0.0; + + if(Nparticles == 0) return; + + assert(tree); + QBiterator it(tree); + + if(periodic_buffer){ + PosType tmp_ray[2]; + + for(int i = -1;i<2;++i){ + for(int j = -1;j<2;++j){ + tmp_ray[0] = ray[0] + i*original_xl; + tmp_ray[1] = ray[1] + j*original_yl; + walkTree_iter(it,tmp_ray,alpha,kappa,gamma,phi); + } + } + }else{ + walkTree_iter(it,ray,alpha,kappa,gamma,phi); + } + + // Subtract off uniform mass sheet to compensate for the extra mass + // added to the universe in the halos. + alpha[0] -= ray[0]*sigma_background*(inv_screening_scale2 == 0); + alpha[1] -= ray[1]*sigma_background*(inv_screening_scale2 == 0); + + *kappa -= sigma_background; + + return; +} + +/** \brief Returns the halos that are within rmax of ray[] + */ +template +void TreeQuadParticles::neighbors(PosType ray[],PosType rmax,std::list &neighbors) const{ + QBiterator it(tree); + neighbors.clear(); + + PosType r2 = rmax*rmax; + bool decend=true; + it.movetop(); + do{ + int cut = Utilities::cutbox(ray,(*it)->boundary_p1,(*it)->boundary_p2,rmax); + decend = true; + + if(cut == 0){ // box outside circle + decend = false; + }else if(cut == 1){ // whole box inside circle + for(int i=0;i<(*it)->nparticles;++i) neighbors.push_back((*it)->particles[i]); + decend = false; + }else if((*it)->child0 == NULL){ // at leaf + for(int i=0;i<(*it)->nparticles;++i){ + if( (tree->xxp[(*it)->particles[i]][0] - ray[0])*(tree->xxp[(*it)->particles[i]][0] - ray[0]) + + (tree->xxp[(*it)->particles[i]][1] - ray[1])*(tree->xxp[(*it)->particles[i]][1] - ray[1]) < r2) neighbors.push_back((*it)->particles[i]); + } + decend = false; + } + }while(it.TreeWalkStep(decend)); + +} + +template +void TreeQuadParticles::walkTree_iter( + QBiterator &treeit, + const PosType *ray + ,PosType *alpha + ,KappaType *kappa + ,KappaType *gamma + ,KappaType *phi + ) const { + + PosType xcm[2],rcm2cell,rcm2,tmp,boxsize2; + IndexType i; + bool allowDescent=true; + unsigned long count=0,tmp_index; + PosType prefac; + //bool notscreen = true; + + assert(tree); + + treeit.movetop(); + //tree->moveTop(); + do{ + ++count; + allowDescent=false; + + /* + if(inv_screening_scale2 != 0) notscreen = BoxIntersectCircle(ray,3*sqrt(1.0/inv_screening_scale2) + ,(*treeit)->boundary_p1 + ,(*treeit)->boundary_p2); + else notscreen = true; + */ + //if(tree->current->nparticles > 0) + if((*treeit)->nparticles > 0) + { + + xcm[0]=(*treeit)->center[0]-ray[0]; + xcm[1]=(*treeit)->center[1]-ray[1]; + + rcm2cell = xcm[0]*xcm[0] + xcm[1]*xcm[1]; + + boxsize2 = ((*treeit)->boundary_p2[0]-(*treeit)->boundary_p1[0])*((*treeit)->boundary_p2[0]-(*treeit)->boundary_p1[0]); + + if( rcm2cell < ((*treeit)->rcrit_angle)*((*treeit)->rcrit_angle) || rcm2cell < 5.83*boxsize2) + { + + // includes rcrit_particle constraint + allowDescent=true; + + + // Treat all particles in a leaf as a point particle + if(treeit.atLeaf()) + { + + for(i = 0 ; i < (*treeit)->nparticles ; ++i) + { + + xcm[0] = tree->xp[(*treeit)->particles[i]][0] - ray[0]; + xcm[1] = tree->xp[(*treeit)->particles[i]][1] - ray[1]; + + + rcm2 = xcm[0]*xcm[0] + xcm[1]*xcm[1]; + double screening = exp(-rcm2*inv_screening_scale2); + if(rcm2 < 1e-20) rcm2 = 1e-20; + + //tmp_index = MultiMass*(*treeit)->particles[i]; + + //if(haloON){ prefac = halos[tmp_index]->get_mass(); } + //else{ prefac = masses[tmp_index]; } + + if(MultiMass) prefac = xxp[(*treeit)->particles[i]].Mass(); + else prefac = mass_fixed; + + prefac /= rcm2*PI/screening; + + tmp = -1.0*prefac; + + alpha[0] += tmp*xcm[0]; + alpha[1] += tmp*xcm[1]; + + + tmp = -2.0*prefac/rcm2; + + gamma[0] += 0.5*(xcm[0]*xcm[0]-xcm[1]*xcm[1])*tmp; + gamma[1] += xcm[0]*xcm[1]*tmp; + + *phi += prefac*rcm2*0.5*log(rcm2); + + } // end of for + } // end of if(tree->atLeaf()) + + + // Find the particles that intersect with ray and add them individually. + if(rcm2cell < 5.83*boxsize2) + { + + for(i = 0 ; i < (*treeit)->Nbig_particles ; ++i) + { + + tmp_index = (*treeit)->big_particles[i]; + + xcm[0] = tree->xp[tmp_index][0] - ray[0]; + xcm[1] = tree->xp[tmp_index][1] - ray[1]; + + rcm2 = xcm[0]*xcm[0] + xcm[1]*xcm[1]; + + if(rcm2 < 1e-20) rcm2 = 1e-20; + + //PosType size = sizes[tmp_index*MultiRadius]; + PosType size = xxp[tmp_index*MultiRadius].size(); + + // intersecting, subtract the point particle + if(rcm2 < 4*size*size) + { + b_spline_profile(xcm,sqrt(rcm2),xxp[MultiMass*tmp_index].Mass(),size,alpha,kappa,gamma,phi); + } + + } + } + + } + else + { // use whole cell + + allowDescent=false; + + double screening = exp(-rcm2cell*inv_screening_scale2); + double tmp = -1.0*(*treeit)->mass/rcm2cell/PI*screening; + + alpha[0] += tmp*xcm[0]; + alpha[1] += tmp*xcm[1]; + + { // taken out to speed up + tmp=-2.0*(*treeit)->mass/PI/rcm2cell/rcm2cell*screening; + gamma[0] += 0.5*(xcm[0]*xcm[0]-xcm[1]*xcm[1])*tmp; + gamma[1] += xcm[0]*xcm[1]*tmp; + + *phi += 0.5*(*treeit)->mass*log( rcm2cell )/PI*screening; + *phi -= 0.5*( (*treeit)->quad[0]*xcm[0]*xcm[0] + (*treeit)->quad[1]*xcm[1]*xcm[1] + 2*(*treeit)->quad[2]*xcm[0]*xcm[1] )/(PI*rcm2cell*rcm2cell)*screening; + } + + // quadrapole contribution + // the kappa and gamma are not calculated to this order + alpha[0] -= ((*treeit)->quad[0]*xcm[0] + (*treeit)->quad[2]*xcm[1]) + /(rcm2cell*rcm2cell)/PI*screening; + alpha[1] -= ((*treeit)->quad[1]*xcm[1] + (*treeit)->quad[2]*xcm[0]) + /(rcm2cell*rcm2cell)/PI*screening; + + tmp = 4*((*treeit)->quad[0]*xcm[0]*xcm[0] + (*treeit)->quad[1]*xcm[1]*xcm[1] + + 2*(*treeit)->quad[2]*xcm[0]*xcm[1])/(rcm2cell*rcm2cell*rcm2cell)/PI*screening; + + alpha[0] += tmp*xcm[0]; + alpha[1] += tmp*xcm[1]; + + } + } + }while(treeit.TreeWalkStep(allowDescent)); + + + // Subtract off uniform mass sheet to compensate for the extra mass + // added to the universe in the halos. + alpha[0] -= ray[0]*sigma_background*(inv_screening_scale2 == 0.0); + alpha[1] -= ray[1]*sigma_background*(inv_screening_scale2 == 0.0); + { // taken out to speed up + *kappa -= sigma_background; + // *phi -= sigma_background * sigma_background ; + } + + return; +} + + +/** \brief Force2D_recur calculates the defection, convergence and shear using + * the plane-lens approximation. + * + * This function should do the same work as TreeQuadParticles::force2D() except it is + * done recursively instead of iteratively. This is done to enable multi-threading + * of the force calculation. + * + * The output alpha[] is in units of mass_scale/Mpc, ie it needs to be + * divided by Sigma_crit and multiplied by mass_scale to be the deflection + * in the lens equation expressed on the lens plane or multiplied by + * 4*pi*G*mass_scale to get the deflection angle caused by the plane lens. + * + * kappa and gamma need to by multiplied by mass_scale/Sigma_crit to get + * the traditional units for these where Sigma_crit are in the mass/units(ray)^2 + * NB : the units of sigma_backgound need to be mass/units(ray)^2 + * + * If periodic_buffer == true a periodic buffer is included. The ray is calculated as if there + * where identical copies of it on the borders of the tree's root branch. This is not the same thing as periodic boundary conditions, because + * there are not an infinite number of copies in all direction as for the DFT force solver. + * + * If inv_screening_scale2 != 0 the mass of cells are reduced by a factor of exp(-|ray - center of mass|^2*inv_screening_scale2) which + * screens the large scale geometry of the simulation on the sky. This is useful when the region is rectangular instead of circular. + * */ + +template +void TreeQuadParticles::force2D_recur(const PosType *ray,PosType *alpha,KappaType *kappa,KappaType *gamma,KappaType *phi){ + + + alpha[0]=alpha[1]=gamma[0]=gamma[1]=gamma[2]=0.0; + *kappa=*phi=0.0; + + if(Nparticles == 0) return; + assert(tree); + + //walkTree_recur(tree->top,ray,&alpha[0],kappa,&gamma[0],phi); + + if(periodic_buffer){ + PosType tmp_ray[2],tmp_c[2]; + + // for points outside of primary zone shift to primary zone + //if(inbox(ray,tree->top->boundary_p1,tree->top->boundary_p2)){ + tmp_c[0] = ray[0]; + tmp_c[1] = ray[1]; + /*}else{ + int di[2]; + PosType center[2] = { (tree->top->boundary_p1[0] + tree->top->boundary_p2[0])/2 , + (tree->top->boundary_p1[1] + tree->top->boundary_p2[1])/2 }; - tmp = (gamma_h(arg1,size) + 2.0)*prefac/rcm2; + di[0] = (int)( 2*(ray[0] - center[0])/lx ); + di[1] = (int)( 2*(ray[1] - center[1])/ly ); - gamma[0] += 0.5*(xcm[0]*xcm[0]-xcm[1]*xcm[1])*tmp; - gamma[1] += xcm[0]*xcm[1]*tmp; + di[0] = (di[0] > 0) ? (di[0] + 1)/2 : (di[0] - 1)/2; + di[1] = (di[1] > 0) ? (di[1] + 1)/2 : (di[1] - 1)/2; - // TODO: makes sure the normalization of phi_h agrees with this - //*phi += (phi_h(arg1,size) + 0.5*log(rcm2))*prefac*rcm2; + tmp_c[0] = ray[0] - lx * di[0]; + tmp_c[1] = ray[1] - ly * di[1]; + + assert(inbox(tmp_c,tree->top->boundary_p1,tree->top->boundary_p2)); + }*/ + + for(int i = -1;i<2;++i){ + for(int j = -1;j<2;++j){ + tmp_ray[0] = tmp_c[0] + i*original_xl; + tmp_ray[1] = tmp_c[1] + j*original_yl; + walkTree_recur(tree->top,tmp_ray,alpha,kappa,gamma,phi); + } + } + }else{ + walkTree_recur(tree->top,ray,alpha,kappa,gamma,phi); } + + // Subtract off uniform mass sheet to compensate for the extra mass + // added to the universe in the halos. + alpha[0] -= ray[0]*sigma_background*(inv_screening_scale2 == 0); + alpha[1] -= ray[1]*sigma_background*(inv_screening_scale2 == 0); + + *kappa -= sigma_background; + + return; +} - QTreeNBHndl rotate_simulation(PosType **xp,IndexType Nparticles,IndexType *particles - ,PosType **coord,PosType theta,float *rsph,float *mass - ,bool MultiRadius,bool MultiMass); - QTreeNBHndl rotate_project(PosType **xp,IndexType Nparticles,IndexType *particles - ,PosType **coord,PosType theta,float *rsph,float *mass - ,bool MultiRadius,bool MultiMass); - void cuttoffscale(QTreeNBHndl tree,PosType *theta); - void walkTree_recur(QBranchNB *branch,PosType const *ray,PosType *alpha,KappaType *kappa,KappaType *gamma,KappaType *phi); - void walkTree_iter(QTreeNB::iterator &treeit, PosType const *ray,PosType *alpha,KappaType *kappa,KappaType *gamma - ,KappaType *phi) const; +template +void TreeQuadParticles::walkTree_recur(QBranchNB *branch,PosType const *ray,PosType *alpha,KappaType *kappa,KappaType *gamma, KappaType *phi){ + + PosType xcm[2],rcm2cell,rcm2,tmp,boxsize2; + IndexType i; + std::size_t tmp_index; + PosType prefac; + /*bool notscreen = true; + + if(inv_screening_scale2 != 0) notscreen = BoxIntersectCircle(ray,3*sqrt(1.0/inv_screening_scale2), branch->boundary_p1, branch->boundary_p2); + */ + if(branch->nparticles > 0) + { + xcm[0]=branch->center[0]-ray[0]; + xcm[1]=branch->center[1]-ray[1]; + + rcm2cell = xcm[0]*xcm[0] + xcm[1]*xcm[1]; + + boxsize2 = (branch->boundary_p2[0]-branch->boundary_p1[0])*(branch->boundary_p2[0]-branch->boundary_p1[0]); + + if( rcm2cell < (branch->rcrit_angle)*(branch->rcrit_angle) || rcm2cell < 5.83*boxsize2) + { + + // Treat all particles in a leaf as a point particle + if(tree->atLeaf(branch)) + { + + for(i = 0 ; i < branch->nparticles ; ++i){ + + xcm[0] = tree->xxp[branch->particles[i]][0] - ray[0]; + xcm[1] = tree->xxp[branch->particles[i]][1] - ray[1]; + + rcm2 = xcm[0]*xcm[0] + xcm[1]*xcm[1]; + double screening = exp(-rcm2*inv_screening_scale2); + if(rcm2 < 1e-20) rcm2 = 1e-20; + + tmp_index = MultiMass*branch->particles[i]; + + //if(haloON ) { prefac = halos[tmp_index]->get_mass(); } + //else{ prefac = masses[tmp_index]; } + prefac = xxp[tmp_index].mass(); + prefac /= rcm2*PI/screening; + + + alpha[0] += -1.0*prefac*xcm[0]; + alpha[1] += -1.0*prefac*xcm[1]; + + { + tmp = -2.0*prefac/rcm2; + + + gamma[0] += 0.5*(xcm[0]*xcm[0]-xcm[1]*xcm[1])*tmp; + gamma[1] += xcm[0]*xcm[1]*tmp; + + *phi += prefac*rcm2*0.5*log(rcm2); + } + } + } + + // Find the particles that intersect with ray and add them individually. + if(rcm2cell < 5.83*boxsize2) + { + + for(i = 0 ; i < branch->Nbig_particles ; ++i) + { + + tmp_index = branch->big_particles[i]; + + xcm[0] = tree->xxp[tmp_index][0] - ray[0]; + xcm[1] = tree->xxp[tmp_index][1] - ray[1]; + rcm2 = xcm[0]*xcm[0] + xcm[1]*xcm[1]; + + ///////////////////////////////////////// + if(rcm2 < 1e-20) rcm2 = 1e-20; + + PosType size = xxp[tmp_index*MultiRadius].size(); + + // intersecting, subtract the point particle + if(rcm2 < 4*size*size) + { + + b_spline_profile(xcm,sqrt(rcm2),xxp[MultiMass*tmp_index].mass(),size,alpha,kappa,gamma,phi); + + } + + } + } + + if(branch->child0 != NULL) + walkTree_recur(branch->child0,&ray[0],&alpha[0],kappa,&gamma[0],&phi[0]); + if(branch->child1 != NULL) + walkTree_recur(branch->child1,&ray[0],&alpha[0],kappa,&gamma[0],&phi[0]); + if(branch->child2 != NULL) + walkTree_recur(branch->child2,&ray[0],&alpha[0],kappa,&gamma[0],&phi[0]); + if(branch->child3 != NULL) + walkTree_recur(branch->child3,&ray[0],&alpha[0],kappa,&gamma[0],&phi[0]); + + } + else + { // use whole cell + + double screening = exp(-rcm2cell*inv_screening_scale2); + double tmp = -1.0*branch->mass/rcm2cell/PI*screening; + + alpha[0] += tmp*xcm[0]; + alpha[1] += tmp*xcm[1]; + + { // taken out to speed up + tmp=-2.0*branch->mass/PI/rcm2cell/rcm2cell*screening; + gamma[0] += 0.5*(xcm[0]*xcm[0]-xcm[1]*xcm[1])*tmp; + gamma[1] += xcm[0]*xcm[1]*tmp; + + *phi += 0.5*tree->current->mass*log( rcm2cell )/PI*screening; + *phi -= 0.5*( tree->current->quad[0]*xcm[0]*xcm[0] + tree->current->quad[1]*xcm[1]*xcm[1] + 2*tree->current->quad[2]*xcm[0]*xcm[1] )/(PI*rcm2cell*rcm2cell)*screening; + } + + // quadrapole contribution + // the kappa and gamma are not calculated to this order + alpha[0] -= (branch->quad[0]*xcm[0] + branch->quad[2]*xcm[1]) + /(rcm2cell*rcm2cell)/PI*screening; + alpha[1] -= (branch->quad[1]*xcm[1] + branch->quad[2]*xcm[0]) + /(rcm2cell*rcm2cell)/PI*screening; + + tmp = 4*(branch->quad[0]*xcm[0]*xcm[0] + branch->quad[1]*xcm[1]*xcm[1] + + 2*branch->quad[2]*xcm[0]*xcm[1])/(rcm2cell*rcm2cell*rcm2cell)/PI*screening; + + alpha[0] += tmp*xcm[0]; + alpha[1] += tmp*xcm[1]; + + return; + } + + } + return; +} +/** This is a diagnostic routine that prints the position of every point in a + * given branch of the tree. + */ +template +void TreeQuadParticles::printParticlesInBranch(unsigned long number){ + unsigned long i; + + tree->moveTop(); + do{ + if(tree->current->number == number){ + std::cout << tree->current->nparticles << std::endl; + for(i=0;icurrent->nparticles;++i){ + std::cout << xxp[tree->current->particles[i]][0] << " " + << xxp[tree->current->particles[i]][1] << std::endl; + } + return; + } + }while(tree->WalkStep(true)); + + return; +} +/** + * Prints to stdout the borders of each branch in the tree below level. + * If level < 0 or not specified the whole tree will be printed. + */ +template +void TreeQuadParticles::printBranchs(int level){ + + bool decend = true; + tree->moveTop(); + do{ + std::cout << tree->current->boundary_p1[0] << " " << tree->current->boundary_p1[1] << " " + << tree->current->boundary_p2[0] << " " << tree->current->boundary_p2[1] << std::endl; + if(tree->current->level == level) decend = false; + else decend = true; + }while(tree->WalkStep(decend)); + + return; +}; + +/** + * \brief TreeQuadHalo is a class for calculating the deflection, kappa and gamma by tree method. + * + * TreeQuadParticles is evolved from TreeSimple and TreeForce. It splits each cell into four equal area + * subcells instead of being a binary tree like TreeSimple. When the "particles" are given sizes + * the tree is built in such a way the large particles are stored in branches that are no smaller + * than their size. In this way particles are stored on all levels of the tree and not just in the + * leaves. This improves efficiency when particles of a wide range of sizes overlap in 2D. + * + * The default value of theta = 0.1 generally gives better than 1% accuracy on alpha. + * The shear and kappa is always more accurate than the deflection. + * + */ + +class LensHalo; + +class TreeQuadHalos { +public: + TreeQuadHalos( + LensHalo **my_halos + ,IndexType Npoints + ,PosType my_sigma_background = 0 + ,int bucket = 5 + ,PosType theta_force = 0.1 + ,bool my_periodic_buffer = false + ,PosType my_inv_screening_scale = 0 + ); + ~TreeQuadHalos(); + + void force2D(PosType const *ray,PosType *alpha,KappaType *kappa,KappaType *gamma + ,KappaType *phi) const; + + void force2D_recur(const PosType *ray,PosType *alpha,KappaType *kappa + ,KappaType *gamma,KappaType *phi); + + /// find all points within rmax of ray in 2D + void neighbors(PosType ray[],PosType rmax,std::list &neighbors) const; + void neighbors(PosType ray[],PosType rmax,std::vector &neighbors) const; + + void printParticlesInBranch(unsigned long number); + + void printBranchs(int level = -1); + +protected: + + PosType **xp; + bool MultiMass; + bool MultiRadius; + float *masses; + float *sizes; + + IndexType Nparticles; + PosType sigma_background; + int Nbucket; + + PosType force_theta; + + QTreeNB * tree; + IndexType *index; + + //bool haloON; + LensHalo **halos; + + PosType realray[2]; + int incell,incell2; + + /// if true there is one layer of peridic buffering + bool periodic_buffer; + PosType inv_screening_scale2; + PosType original_xl; // x-axis size of simulation used for peridic buffering. Requrement that it top branch be square my make it differ from the size of top branch. + PosType original_yl; // x-axis size of simulation used for peridic buffering. + + QTreeNB * BuildQTreeNB(PosType **xp,IndexType Nparticles,IndexType *particles); + void _BuildQTreeNB(IndexType nparticles,IndexType *particles); + + inline short WhichQuad(PosType *x,QBranchNB &branch); + + //inline bool atLeaf(); + inline bool inbox(const PosType *ray,const PosType *p1,const PosType *p2){ + return (ray[0]>=p1[0])*(ray[0]<=p2[0])*(ray[1]>=p1[1])*(ray[1]<=p2[1]); + } + //int cutbox(PosType *ray,PosType *p1,PosType *p2,float rmax); + + void CalcMoments(); + void rotate_coordinates(PosType **coord); + + //QTreeNBHndl rotate_simulation(PosType **xp,IndexType Nparticles,IndexType *particles + // ,PosType **coord,PosType theta,float *rsph,float *mass + // ,bool MultiRadius,bool MultiMass); + //QTreeNBHndl rotate_project(PosType **xp,IndexType Nparticles,IndexType *particles + // ,PosType **coord,PosType theta,float *rsph,float *mass + // ,bool MultiRadius,bool MultiMass); + void cuttoffscale(QTreeNB * tree,PosType *theta); + + void walkTree_recur(QBranchNB *branch,PosType const *ray,PosType *alpha,KappaType *kappa,KappaType *gamma,KappaType *phi); + void walkTree_iter(QBiterator &treeit, PosType const *ray,PosType *alpha,KappaType *kappa + ,KappaType *gamma,KappaType *phi) const; + PosType phiintconst; + +}; - }; #endif /* QUAD_TREE_H_ */ diff --git a/include/simpleTree.h b/include/simpleTree.h index 398ef349..700e30a7 100644 --- a/include/simpleTree.h +++ b/include/simpleTree.h @@ -10,7 +10,7 @@ #include "standard.h" #include "Tree.h" -#include "lens_halos.h" +//#include "lens_halos.h" /** \brief Box representing a branch in a tree. It has four children. Used in TreeNBStruct which is used in TreeForce. */ @@ -83,9 +83,10 @@ typedef int TreeNBElement; * * Most of the code in TreeNB.c and TreeDriverNB.c is duplicated here as private methods and a few public ones. */ +template class TreeSimple { public: - TreeSimple(PosType **xp,IndexType Npoints,int bucket = 5,int dimensions = 2,bool median = true); + TreeSimple(PType *xp,IndexType Npoints,int bucket = 5,int dimensions = 2,bool median = true); virtual ~TreeSimple(); /// \brief Finds the points within a circle around center and puts their index numbers in a list @@ -168,16 +169,16 @@ class TreeSimple { bool median_cut; int Nbucket; PosType realray[3]; - PosType **xp; + PType *xxp; - TreeNBHndl BuildTreeNB(PosType **xp,IndexType Nparticles,IndexType *particles,int Ndimensions,PosType theta); + TreeNBHndl BuildTreeNB(PType *xxp,IndexType Nparticles,IndexType *particles,int Ndimensions,PosType theta); void _BuildTreeNB(TreeNBHndl tree,IndexType nparticles,IndexType *particles); void _PointsWithin(PosType *ray,float *rmax,std::list &neighborkist); void _NearestNeighbors(PosType *ray,int Nneighbors,unsigned long *neighbors,PosType *rneighbors); void _NearestNeighbors(PosType *ray,PosType *realray,int Nneighbors,unsigned long *neighbors,PosType *rneighbors - ,TreeSimple::iterator &it,int &found) const ; + ,TreeSimple::iterator &it,int &found) const ; BranchNB *NewBranchNB(IndexType *particles,IndexType nparticles ,PosType boundary_p1[],PosType boundary_p2[] diff --git a/include/uniform_lens.h b/include/uniform_lens.h index 0cde002c..76373223 100644 --- a/include/uniform_lens.h +++ b/include/uniform_lens.h @@ -67,7 +67,7 @@ class LensHaloUniform: public LensHalo{ ~LensHaloUniform(); void assignParams(InputParams& params); - void PrintLens(bool show_substruct,bool show_stars) const; + void PrintLens(bool show_substruct,bool show_stars); /// Average surface density in Msun/Mpc^2 float getSigma_uniform() const {return Sigma_uniform;} /// Shear times the critical surface density diff --git a/include/utilities_slsim.h b/include/utilities_slsim.h index 578c44df..9e7747b0 100644 --- a/include/utilities_slsim.h +++ b/include/utilities_slsim.h @@ -224,7 +224,7 @@ namespace Utilities iterator& operator=(const iterator& rhs) { it = rhs.it; return *this; } reference operator*() { return (reference)(**it); } - const reference operator*() const { return (const reference)(**it); } + //const reference operator*() const { return (const reference)(**it); } pointer operator->() { return (pointer)(*it); } const pointer operator->() const { return (const pointer)(*it); } @@ -241,7 +241,7 @@ namespace Utilities iterator& operator-=(difference_type n) { it -= n; return *this; } reference operator[](difference_type n) { return (reference)*it[n]; } - const reference operator[](difference_type n) const { return (const reference)*it[n]; } + //const reference operator[](difference_type n) const { return (const reference)*it[n]; } friend iterator operator+(const iterator& i, difference_type n) { return iterator(i.it + n); } friend iterator operator+(difference_type n, const iterator& i) { return iterator(i.it + n); } From 6bd7c63e447956bf00350ed534f98835e2034beb Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Mon, 12 Nov 2018 20:16:21 +0100 Subject: [PATCH 071/227] Worked on reading gadget2 files. Worked on a generator class for LensHaloParticle *** not a working commit **** --- AnalyticNSIE/nsie.cpp | 2 +- FullRange/implant_stars.cpp | 2 +- Miscellaneous/CMakeLists.txt | 2 +- Miscellaneous/gadget.cpp | 14 +++ MultiPlane/particle_lens.cpp | 170 ++++++++++++++++++++++++----------- include/CMakeLists.txt | 3 +- include/lens_halos.h | 6 +- include/particle_halo.h | 125 ++++++++++++++++++-------- include/particle_types.h | 53 +++++++++++ include/quadTree.h | 10 --- 10 files changed, 281 insertions(+), 106 deletions(-) create mode 100644 Miscellaneous/gadget.cpp create mode 100644 include/particle_types.h diff --git a/AnalyticNSIE/nsie.cpp b/AnalyticNSIE/nsie.cpp index 1c5631b1..739b964c 100644 --- a/AnalyticNSIE/nsie.cpp +++ b/AnalyticNSIE/nsie.cpp @@ -340,7 +340,7 @@ KappaType LensHaloBaseNSIE::phiNSIE(PosType const *xt /// position on the ima // in order to validate the elliptization of phi. } - + return 0; } diff --git a/FullRange/implant_stars.cpp b/FullRange/implant_stars.cpp index be57ea04..f90837b8 100644 --- a/FullRange/implant_stars.cpp +++ b/FullRange/implant_stars.cpp @@ -179,7 +179,7 @@ void LensHalo::implant_stars( // ,false,false,5,2,false,star_theta_force); if(stars_implanted) delete star_tree; - star_tree = new TreeQuadParticles(stars_xp.data(),stars_N + star_tree = new TreeQuadParticles(stars_xp.data(),stars_N ,false,false,0,4,star_theta_force); // visit every branch to find center of mass and cutoff scale */ diff --git a/Miscellaneous/CMakeLists.txt b/Miscellaneous/CMakeLists.txt index 22b22795..99bca52a 100644 --- a/Miscellaneous/CMakeLists.txt +++ b/Miscellaneous/CMakeLists.txt @@ -1,4 +1,4 @@ add_sources( - gadget.cp + gadget.cpp gensim.cp ) diff --git a/Miscellaneous/gadget.cpp b/Miscellaneous/gadget.cpp new file mode 100644 index 00000000..599d63a7 --- /dev/null +++ b/Miscellaneous/gadget.cpp @@ -0,0 +1,14 @@ +#include "gadget.hh" + +#define SKIP {my_fread(&blksize,sizeof(int),1,fd); swap_Nbyte((char*)&blksize,1,4);} + + + + + + + + + + +/* */ diff --git a/MultiPlane/particle_lens.cpp b/MultiPlane/particle_lens.cpp index 7a149ee2..fa1fb83b 100644 --- a/MultiPlane/particle_lens.cpp +++ b/MultiPlane/particle_lens.cpp @@ -35,7 +35,7 @@ LensHaloParticles::LensHaloParticles(const std::string& simulation_filena switch (format) { case ascii: - readPositionFileASCII(simulation_filename,multimass,xxp); + readPositionFileASCII(simulation_filename,multimass,pp); break; default: std::cerr << "LensHaloParticles does not accept ." << std::endl; @@ -43,15 +43,17 @@ LensHaloParticles::LensHaloParticles(const std::string& simulation_filena } sizefile = simfile + "." + std::to_string(Nsmooth) + "sizes"; - if(!readSizesFile(sizefile,Nsmooth,min_size)){ + + if(!readSizesFile(sizefile,pp,Npoints,Nsmooth,min_size)){ + // calculate sizes //sizes.resize(Npoints); - calculate_smoothing(Nsmooth,xxp); + calculate_smoothing(Nsmooth,pp,Npoints); // save result to a file for future use - writeSizes(sizefile,Nsmooth,xxp); + writeSizes(sizefile,Nsmooth,pp,Npoints); //for(size_t i=0; i::LensHaloParticles(const std::string& simulation_filena mcenter *= 0.0; PosType max_mass = 0.0,min_mass = HUGE_VALF,mass=0; for(size_t i=0;i max_mass) ? xxp[multimass*i].mass() : max_mass; - min_mass = (xxp[multimass*i].mass() < min_mass) ? xxp[multimass*i].mass() : min_mass; + max_mass = (pp[multimass*i].mass() > max_mass) ? pp[multimass*i].mass() : max_mass; + min_mass = (pp[multimass*i].mass() < min_mass) ? pp[multimass*i].mass() : min_mass; } LensHalo::setMass(mass); @@ -82,11 +84,11 @@ LensHaloParticles::LensHaloParticles(const std::string& simulation_filena if(recenter){ PosType r2,r2max=0; for(size_t i=0;i r2max) r2max = r2; } @@ -96,9 +98,77 @@ LensHaloParticles::LensHaloParticles(const std::string& simulation_filena // rotate positions rotate_particles(theta_rotate[0],theta_rotate[1]); - qtree = new TreeQuadParticles >(xxp.data(),xxp.size(),-1,-1,0,20); + qtree = new TreeQuadParticles >(pp,Npoints,-1,-1,0,20); } +template +LensHaloParticles::LensHaloParticles( + PType *pdata + ,float redshift /// redshift of origin + ,const COSMOLOGY& cosmo /// cosmology + ,Point_2d theta_rotate /// rotation of particles around the origin + ,bool recenter /// center on center of mass + ,float MinPSize /// minimum particle size +):min_size(MinPSize),multimass(true) +{ + + LensHalo::setZlens(redshift); + LensHalo::setCosmology(cosmo); + LensHalo::set_flag_elliptical(false); + + stars_N = 0; + stars_implanted = false; + + Rmax = 1.0e3; + LensHalo::setRsize(Rmax); + + // convert from comoving to physical coordinates + PosType scale_factor = 1/(1+redshift); + mcenter *= 0.0; + PosType max_mass = 0.0,min_mass = HUGE_VALF,mass=0; + for(size_t i=0;i max_mass) ? pp[i].mass() : max_mass; + min_mass = (pp[i].mass() < min_mass) ? pp[i].mass() : min_mass; + } + LensHalo::setMass(mass); + + mcenter /= mass; + + std::cout << " Particle mass range : " << min_mass << " to " << max_mass << " ratio of : " << max_mass/min_mass << std::endl; + + + if(recenter){ + PosType r2,r2max=0; + for(size_t i=0;i r2max) r2max = r2; + } + + LensHalo::setRsize( sqrt(r2max) ); + } + + // rotate positions + rotate_particles(theta_rotate[0],theta_rotate[1]); + + qtree = new TreeQuadParticles >(pp,Npoints,-1,-1,0,20); +} + + + template LensHaloParticles::~LensHaloParticles(){ delete qtree; @@ -118,7 +188,7 @@ template void LensHaloParticles::rotate(Point_2d theta){ rotate_particles(theta[0],theta[1]); delete qtree; - qtree =new TreeQuadParticles >(xxp.data(),Npoints,multimass,true,0,20); + qtree =new TreeQuadParticles >(pp,Npoints,multimass,true,0,20); } /** \brief Reads number of particle and particle positons into Npoint and xp from a ASCII file. @@ -131,7 +201,7 @@ void LensHaloParticles::rotate(Point_2d theta){ template void LensHaloParticles::readPositionFileASCII(const std::string &filename ,bool multimass - ,std::vector &xxp + ,PType *pp ){ int ncoll = Utilities::IO::CountColumns(filename); @@ -181,7 +251,7 @@ void LensHaloParticles::readPositionFileASCII(const std::string &filename throw std::runtime_error("file reading error"); } - xxp.resize(Npoints); + pp = new PType[Npoints]; //xp = Utilities::PosTypeMatrix(Npoints,3); //if(multimass) masses.resize(Npoints); //else masses.push_back(tmp_mass); @@ -194,10 +264,10 @@ void LensHaloParticles::readPositionFileASCII(const std::string &filename if(str[0] == '#') continue; //for comments std::stringstream ss(str); - ss >> xxp[row][0]; - if(!(ss >> xxp[row][1])) std::cerr << "3 columns are expected in line " << row + ss >> pp[row][0]; + if(!(ss >> pp[row][1])) std::cerr << "3 columns are expected in line " << row << " of " << filename << std::endl; - if(!(ss >> xxp[row][2])) std::cerr << "3 columns are expected in line " << row + if(!(ss >> pp[row][2])) std::cerr << "3 columns are expected in line " << row << " of " << filename << std::endl; row++; @@ -207,12 +277,12 @@ void LensHaloParticles::readPositionFileASCII(const std::string &filename if(str[0] == '#') continue; //for comments std::stringstream ss(str); - ss >> xxp[row][0]; - if(!(ss >> xxp[row][1])) std::cerr << "4 columns are expected in line " << row + ss >> pp[row][0]; + if(!(ss >> pp[row][1])) std::cerr << "4 columns are expected in line " << row << " of " << filename << std::endl; - if(!(ss >> xxp[row][2])) std::cerr << "4 columns are expected in line " << row + if(!(ss >> pp[row][2])) std::cerr << "4 columns are expected in line " << row << " of " << filename << std::endl; - if(!(ss >> xxp[row].Mass)) std::cerr << "4 columns are expected in line " << row + if(!(ss >> pp[row].Mass)) std::cerr << "4 columns are expected in line " << row << " of " << filename << std::endl; row++; @@ -234,8 +304,11 @@ void LensHaloParticles::readPositionFileASCII(const std::string &filename } template -bool LensHaloParticles::readSizesFile(const std::string &filename,int Nsmooth - ,PosType min_size){ +bool LensHaloParticles::readSizesFile(const std::string &filename + ,PType * pp + ,size_t Npoints + ,int Nsmooth + ,PosType min_size){ std::ifstream myfile(filename); @@ -286,10 +359,10 @@ bool LensHaloParticles::readSizesFile(const std::string &filename,int Nsm if(str[0] == '#') continue; //for comments std::stringstream ss(str); - ss >> xxp[row].Size; - if(min_size > xxp[row].size() ) xxp[row].Size = min_size; - min = min < xxp[row].size() ? min : xxp[row].size(); - max = max > xxp[row].size() ? max : xxp[row].size(); + ss >> pp[row].Size; + if(min_size > pp[row].size() ) pp[row].Size = min_size; + min = min < pp[row].size() ? min : pp[row].size(); + max = max > pp[row].size() ? max : pp[row].size(); row++; } @@ -329,31 +402,29 @@ void LensHaloParticles::rotate_particles(PosType theta_x,PosType theta_y) for(size_t i=0;i -void LensHaloParticles::calculate_smoothing(int Nsmooth,std::vector &xxp - ){ +void LensHaloParticles::calculate_smoothing(int Nsmooth,PType *pp + ,size_t Npoints){ std::cout << "Calculating smoothing of particles ..." << std::endl << Nsmooth << " neighbors. If there are a lot of particles this could take a while." << std::endl; - size_t Npoints = xxp.size(); - // make 3d tree of particle postions - TreeSimple tree3d(xxp,Npoints,10,3,true); + TreeSimple tree3d(pp,Npoints,10,3,true); // find distance to nth neighbour for every particle if(Npoints < 1000){ IndexType neighbors[Nsmooth]; for(size_t i=0;i::calculate_smoothing(int Nsmooth,std::vector::smooth_(TreeSimple *tree3d,PType *xp,size_ template void LensHaloParticles::writeSizes(const std::string &filename,int Nsmooth - ,const std::vector &xxp + ,const PType *pp,size_t Npoints ){ std::ofstream myfile(filename); @@ -398,11 +469,10 @@ void LensHaloParticles::writeSizes(const std::string &filename,int Nsmoot std::cout << "Writing particle size information to file " << filename << " ...." << std::endl; - myfile << "# nparticles " << xxp.size() << std::endl; + myfile << "# nparticles " << Npoints << std::endl; myfile << "# nsmooth " << Nsmooth << std::endl; - size_t Npoints = xxp.size(); for(size_t i=0;i stars_xp; - TreeQuadParticles *star_tree; + std::vector stars_xp; + TreeQuadParticles *star_tree; int stars_N; PosType star_massscale; /// star masses relative to star_massscles diff --git a/include/particle_halo.h b/include/particle_halo.h index c4193a76..ef65ea4c 100644 --- a/include/particle_halo.h +++ b/include/particle_halo.h @@ -6,41 +6,20 @@ // // +things to do: + +protect particle data by storing it in a class that reads data and generates LensHaloParticles + +make multi-type halos center on a common cneter of mass + #ifndef GLAMER_particle_halo_h #define GLAMER_particle_halo_h #include "geometry.h" #include "quadTree.h" #include "simpleTree.h" - -/// Atomic data class for simulation particles with individual sizes and masses -template -struct ParticleData{ - T &operator[](int i){return x[i];} - T *operator*(){return x;} - T x[3]; - float Mass; - float Size; - int type; - - float size(){return Size;} - float mass(){return Mass;} -}; - -/// Atomic data class for simulation particles of the same size and mass -struct ParticleDataSimple{ - float &operator[](int i){return x[i];} - float *operator*(){return x;} - float x[3]; - - static float Mass; - static float Size; - - float size(){return ParticleDataSimple::Size;} - float mass(){return ParticleDataSimple::Mass;} -}; -float ParticleDataSimple::Size = 0; -float ParticleDataSimple::Mass = 0; +#include "particle_types.h" +#include "gadget.hh" enum SimFileFormats {ascii,gadget2}; @@ -77,6 +56,14 @@ class LensHaloParticles : public LensHalo ,bool my_multimass /// set to true is particles have different sizes ,PosType MinPSize /// minimum particle size ); + + LensHaloParticles(PType *pdata + ,float redshift /// redshift of origin + ,const COSMOLOGY& cosmo /// cosmology + ,Point_2d theta_rotate /// rotation of particles around the origin + ,bool recenter /// center on center of mass + ,float MinPSize /// minimum particle size + ); ~LensHaloParticles(); @@ -106,32 +93,38 @@ class LensHaloParticles : public LensHalo ); static void calculate_smoothing(int Nsmooth - ,std::vector &xxp - ); + ,PType *pp + ,size_t Npoints); static void readPositionFileASCII(const std::string &filename ,bool multimass - ,std::vector &xxp + ,PType *pp ); - + + + static void writeSizes(const std::string &filename ,int Nsmooth - ,const std::vector &xxp + ,const PType *pp + ,size_t Npoints ); + static bool readSizesFile(const std::string &filename + ,PType * pp + ,size_t Npoints + ,int Nsmooth + ,PosType min_size); + private: - Point_3d mcenter; void rotate_particles(PosType theta_x,PosType theta_y); static void smooth_(TreeSimple *tree3d,PType *xp,size_t N,int Nsmooth); - bool readSizesFile(const std::string& filename,int Nsmooth,PosType min_size); - void assignParams(InputParams& params); - std::vector xxp; + PType *pp; //PosType **xp; //std::vector masses; //std::vector sizes; @@ -149,5 +142,61 @@ class LensHaloParticles : public LensHalo TreeQuadParticles * qtree; }; +void makeHalosFromGadget(const std::string &filename + ,std::vector > &data + ,std::vector > > &halos + ,int Nsmooth + ,float redshift + ,Point_2d theta_rotate /// rotation of particles around the origin + ,const COSMOLOGY &cosmo + ){ + + + GadgetFile > gadget_file(filename,data); + + for(int n=0 ; n < gadget_file.numfiles ; ++n){ + gadget_file.openFile(); + gadget_file.readBlock("POS"); + gadget_file.readBlock("MASS"); + gadget_file.closeFile(); + } + + // sort by type + std::sort(data.begin(),data.end(),[](ParticleType &a1,ParticleType &a2){return a1.type < a2.type;}); + + ParticleType *pp; + + size_t skip = 0; + std::string sizefile; + for(int i = 0 ; i < 6 ; ++i){ //loop through type + if(gadget_file.npart[i] > 0){ + + pp = data.data() + skip; // pointer to first particle of type + size_t N = gadget_file.npart[i]; + + sizefile = filename + "_T" + std::to_string(i) + "S" + + std::to_string(Nsmooth) + "sizes"; + + if(!LensHaloParticles >::readSizesFile(sizefile,pp,gadget_file.npart[i],Nsmooth,0)){ + // calculate sizes + //sizes.resize(Npoints); + LensHaloParticles >::calculate_smoothing(Nsmooth,pp,N); + + // save result to a file for future use + LensHaloParticles >::writeSizes(sizefile,Nsmooth,pp,N); + } + + halos.emplace_back(pp + ,redshift + ,cosmo /// cosmology + ,theta_rotate /// rotation of particles around the origin + ,false + ,0); + + } + skip += gadget_file.npart[i]; + } +}; + #endif diff --git a/include/particle_types.h b/include/particle_types.h new file mode 100644 index 00000000..3d31275b --- /dev/null +++ b/include/particle_types.h @@ -0,0 +1,53 @@ +// +// particle_types.h +// GLAMER +// +// Created by Ben Metcalf on 12/11/2018. +// + +#ifndef particle_types_h +#define particle_types_h + +// Atomic data class for simulation particles with individual sizes and masses +template +struct ParticleType{ + T &operator[](int i){return x[i];} + T *operator*(){return x;} + T x[3]; + + float Mass; + float Size; + int type; + + float size(){return Size;} + float mass(){return Mass;} +}; + +/// Atomic data class for simulation particles of the same size and mass +struct ParticleTypeSimple{ + float &operator[](int i){return x[i];} + float *operator*(){return x;} + float x[3]; + + static float Mass; + static float Size; + + float size(){return ParticleTypeSimple::Size;} + float mass(){return ParticleTypeSimple::Mass;} +}; +float ParticleTypeSimple::Size = 0; +float ParticleTypeSimple::Mass = 0; + +/// Atomic data class for stars with different masses +struct StarType{ + double &operator[](int i){return x[i];} + double *operator*(){return x;} + double x[3]; + float Mass; + + float mass() const {return Mass;} + static float size() {return 0;} +}; + + +#endif /* particle_types_h */ diff --git a/include/quadTree.h b/include/quadTree.h index 3c16c70e..62f4fbf1 100644 --- a/include/quadTree.h +++ b/include/quadTree.h @@ -26,16 +26,6 @@ //short const treeNBdim = 2; -/// Atomic data class for stars with different masses -struct StarData{ - PosType &operator[](int i){return x[i];} - PosType *operator*(){return x;} - PosType x[3]; - float Mass; - - float mass() const {return Mass;} - static float size() {return 0;} -}; /** * \brief TreeQuadParticles is a class for calculating the deflection, kappa and gamma by tree method. From 227c12f690474747e3455e8fbf9dd709edd235c2 Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Thu, 15 Nov 2018 10:40:21 +0100 Subject: [PATCH 072/227] A working, but not completely tested new LensHaloParticles and MakeParticleLenses(). Temporarily removed Gadget functionality because header file was left on another computer. Templated TreeSimple and other routines. Added very useful Utilities::splitstring(). --- Miscellaneous/CMakeLists.txt | 1 - Miscellaneous/gadget.cpp | 4 - MultiPlane/particle_lens.cpp | 672 ++++++-------------- TreeCode/simpleTree.cpp | 1012 ----------------------------- TreeCode_link/double_sort.cpp | 37 -- TreeCode_link/utilities.cpp | 9 + include/CMakeLists.txt | 2 +- include/Tree.h | 40 +- include/particle_halo.h | 691 ++++++++++++++++++-- include/particle_types.h | 23 +- include/simpleTree.h | 1118 +++++++++++++++++++++++++++++++-- include/utilities_slsim.h | 12 +- 12 files changed, 1978 insertions(+), 1643 deletions(-) diff --git a/Miscellaneous/CMakeLists.txt b/Miscellaneous/CMakeLists.txt index 99bca52a..b6105de9 100644 --- a/Miscellaneous/CMakeLists.txt +++ b/Miscellaneous/CMakeLists.txt @@ -1,4 +1,3 @@ add_sources( gadget.cpp - gensim.cp ) diff --git a/Miscellaneous/gadget.cpp b/Miscellaneous/gadget.cpp index 599d63a7..535cb0c1 100644 --- a/Miscellaneous/gadget.cpp +++ b/Miscellaneous/gadget.cpp @@ -1,7 +1,3 @@ -#include "gadget.hh" - -#define SKIP {my_fread(&blksize,sizeof(int),1,fd); swap_Nbyte((char*)&blksize,1,4);} - diff --git a/MultiPlane/particle_lens.cpp b/MultiPlane/particle_lens.cpp index fa1fb83b..9e659d0b 100644 --- a/MultiPlane/particle_lens.cpp +++ b/MultiPlane/particle_lens.cpp @@ -1,528 +1,236 @@ -#include "slsimlib.h" -#include "particle_halo.h" #include #include #include +#include "slsimlib.h" +#include "particle_types.h" +#include "particle_halo.h" +#include "simpleTree.h" +#include "utilities_slsim.h" #ifdef ENABLE_FITS #include using namespace CCfits; #endif -template -LensHaloParticles::LensHaloParticles(const std::string& simulation_filename - ,SimFileFormats format - ,PosType redshift - ,int Nsmooth - ,const COSMOLOGY& cosmo - ,Point_2d theta_rotate - ,bool recenter - ,bool my_multimass - ,PosType MinPSize - ) -:min_size(MinPSize),multimass(my_multimass),simfile(simulation_filename) +// ************************************************************************************* +// ******************** methods for MakeParticleLenses ********************************* +// ************************************************************************************* +// ************************************************************************************* + +MakeParticleLenses::MakeParticleLenses( + const std::string &filename /// path / root name of gadget-2 snapshot + ,SimFileFormat format + ,int Nsmooth /// number of nearest neighbors used for smoothing + ,bool recenter /// recenter so that the LenHalos are centered on the center of mass + ):filename(filename),Nsmooth(Nsmooth) { - LensHalo::setZlens(redshift); - LensHalo::setCosmology(cosmo); - LensHalo::set_flag_elliptical(false); - - stars_N = 0; - stars_implanted = false; - Rmax = 1.0e3; - LensHalo::setRsize(Rmax); - switch (format) { - case ascii: - readPositionFileASCII(simulation_filename,multimass,pp); - break; - default: - std::cerr << "LensHaloParticles does not accept ." << std::endl; - throw std::invalid_argument("bad format"); - } - - sizefile = simfile + "." + std::to_string(Nsmooth) + "sizes"; - - if(!readSizesFile(sizefile,pp,Npoints,Nsmooth,min_size)){ - - // calculate sizes - //sizes.resize(Npoints); - calculate_smoothing(Nsmooth,pp,Npoints); - - // save result to a file for future use - writeSizes(sizefile,Nsmooth,pp,Npoints); - //for(size_t i=0; i max_mass) ? pp[multimass*i].mass() : max_mass; - min_mass = (pp[multimass*i].mass() < min_mass) ? pp[multimass*i].mass() : min_mass; + std::string sizefile = filename + "_S" + + std::to_string(Nsmooth) + ".glamb"; + + if(access( sizefile.c_str(), F_OK ) != -1){ + nparticles.resize(6,0); + readSizesB(sizefile,data,Nsmooth,nparticles,z_original); + }else{ + // processed file does not exist read the gadget file and find sizes + switch (format) { + case gadget2: + readGadget2(); + break; + case csv: + readCSV(); + default: + break; + } + + // write size file so that next time we wont have to do this + writeSizesB(sizefile,data,Nsmooth,nparticles,z_original); + } } - LensHalo::setMass(mass); - - mcenter /= mass; - std::cout << " Particle mass range : " << min_mass << " to " << max_mass << " ratio of : " << max_mass/min_mass << std::endl; - - + // find center of mass if(recenter){ - PosType r2,r2max=0; - for(size_t i=0;i r2max) r2max = r2; + double m=0; + Point_3d cm(0,0,0); + for(auto p : data){ + cm[0] += p[0]*p.Mass; + cm[1] += p[1]*p.Mass; + cm[2] += p[2]*p.Mass; + m += p.Mass; + } + cm /= m; + for(auto &p : data){ + p[0] -= cm[0]; + p[1] -= cm[1]; + p[2] -= cm[2]; } - - LensHalo::setRsize( sqrt(r2max) ); } - - // rotate positions - rotate_particles(theta_rotate[0],theta_rotate[1]); - - qtree = new TreeQuadParticles >(pp,Npoints,-1,-1,0,20); } -template -LensHaloParticles::LensHaloParticles( - PType *pdata - ,float redshift /// redshift of origin - ,const COSMOLOGY& cosmo /// cosmology - ,Point_2d theta_rotate /// rotation of particles around the origin - ,bool recenter /// center on center of mass - ,float MinPSize /// minimum particle size -):min_size(MinPSize),multimass(true) +MakeParticleLenses::MakeParticleLenses(const std::string &filename /// path / name of galmb file + ,bool recenter /// recenter so that the LenHalos are centered on the center of mass + ):filename(filename) { - - LensHalo::setZlens(redshift); - LensHalo::setCosmology(cosmo); - LensHalo::set_flag_elliptical(false); - - stars_N = 0; - stars_implanted = false; - - Rmax = 1.0e3; - LensHalo::setRsize(Rmax); - - // convert from comoving to physical coordinates - PosType scale_factor = 1/(1+redshift); - mcenter *= 0.0; - PosType max_mass = 0.0,min_mass = HUGE_VALF,mass=0; - for(size_t i=0;i max_mass) ? pp[i].mass() : max_mass; - min_mass = (pp[i].mass() < min_mass) ? pp[i].mass() : min_mass; - } - LensHalo::setMass(mass); - mcenter /= mass; - std::cout << " Particle mass range : " << min_mass << " to " << max_mass << " ratio of : " << max_mass/min_mass << std::endl; + nparticles.resize(6,0); + readSizesB(filename,data,Nsmooth,nparticles,z_original); + // find center of mass if(recenter){ - PosType r2,r2max=0; - for(size_t i=0;i r2max) r2max = r2; + double m=0; + Point_3d cm(0,0,0); + for(auto p : data){ + cm[0] += p[0]*p.Mass; + cm[1] += p[1]*p.Mass; + cm[2] += p[2]*p.Mass; + m += p.Mass; + } + cm /= m; + for(auto &p : data){ + p[0] -= cm[0]; + p[1] -= cm[1]; + p[2] -= cm[2]; } - - LensHalo::setRsize( sqrt(r2max) ); } - - // rotate positions - rotate_particles(theta_rotate[0],theta_rotate[1]); - - qtree = new TreeQuadParticles >(pp,Npoints,-1,-1,0,20); } +void MakeParticleLenses::CreateHalos(const COSMOLOGY &cosmo,double redshift){ - -template -LensHaloParticles::~LensHaloParticles(){ - delete qtree; -} - -template -void LensHaloParticles::force_halo(double *alpha,KappaType *kappa,KappaType *gamma,KappaType *phi - ,double const *xcm - ,bool subtract_point,PosType screening){ - qtree->force2D_recur(xcm,alpha,kappa,gamma,phi); + // put into proper units + float h = cosmo.gethubble(); - alpha[0] *= -1; - alpha[1] *= -1; -} - -template -void LensHaloParticles::rotate(Point_2d theta){ - rotate_particles(theta[0],theta[1]); - delete qtree; - qtree =new TreeQuadParticles >(pp,Npoints,multimass,true,0,20); -} + double length_unit = h; + double mass_unit = h; -/** \brief Reads number of particle and particle positons into Npoint and xp from a ASCII file. - * - * Data file must have the lines "# nparticles ***" and "# mass ***" in the header. All header - * lines must begin with a "# " - * - * Coordinates of particles are in physical Mpc units. - */ -template -void LensHaloParticles::readPositionFileASCII(const std::string &filename - ,bool multimass - ,PType *pp - ){ - - int ncoll = Utilities::IO::CountColumns(filename); - if(!multimass && ncoll != 3 ){ - std::cerr << filename << " should have three columns!" << std::endl; + for(auto &p : data){ + p[0] *= length_unit; + p[1] *= length_unit; + p[2] *= length_unit; + p.Size *= length_unit; + p.Mass *= mass_unit; } - if(multimass && ncoll != 4 ){ - std::cerr << filename << " should have four columns!" << std::endl; - } - - std::ifstream myfile(filename); - - size_t Npoints = 0; - // find number of particles - - if (myfile.is_open()){ - - float tmp_mass = 0.0; - std::string str,label; - int count =0; - while(std::getline(myfile, str)){ - std::stringstream ss(str); - ss >> label; - if(label == "#"){ - ss >> label; - if(label == "nparticles"){ - ss >> Npoints; - ++count; - } - if(!multimass){ - if(label == "mass"){ - ss >> tmp_mass; - ++count; - } - } - }else break; - if(multimass && count == 1 ) break; - if(!multimass && count == 2 ) break; - } - - if(count == 0){ - if(multimass) std::cerr << "File " << filename << " must have the header lines: " << std::endl - << "# nparticles ****" << std::endl << "# mass ****" << std::endl; - if(!multimass) std::cerr << "File " << filename << " must have the header lines: " << std::endl - << "# nparticles ****" << std::endl; - throw std::runtime_error("file reading error"); - } - - pp = new PType[Npoints]; - //xp = Utilities::PosTypeMatrix(Npoints,3); - //if(multimass) masses.resize(Npoints); - //else masses.push_back(tmp_mass); - - size_t row = 0; - - // read in particle positions - if(!multimass){ - while(std::getline(myfile, str) && row < Npoints){ - if(str[0] == '#') continue; //for comments - std::stringstream ss(str); + // create halos + ParticleType *pp; + size_t skip = 0; + Point_2d theta_rotate; + for(int i = 0 ; i < 6 ; ++i){ //loop through types + if(nparticles[i] > 0){ - ss >> pp[row][0]; - if(!(ss >> pp[row][1])) std::cerr << "3 columns are expected in line " << row - << " of " << filename << std::endl; - if(!(ss >> pp[row][2])) std::cerr << "3 columns are expected in line " << row - << " of " << filename << std::endl; - - row++; - } - }else{ - while(std::getline(myfile, str) && row < Npoints){ - if(str[0] == '#') continue; //for comments - std::stringstream ss(str); - - ss >> pp[row][0]; - if(!(ss >> pp[row][1])) std::cerr << "4 columns are expected in line " << row - << " of " << filename << std::endl; - if(!(ss >> pp[row][2])) std::cerr << "4 columns are expected in line " << row - << " of " << filename << std::endl; - if(!(ss >> pp[row].Mass)) std::cerr << "4 columns are expected in line " << row - << " of " << filename << std::endl; - - row++; - } + pp = data.data() + skip; // pointer to first particle of type + /* halos.emplace_back(pp + ,nparticles[i] + ,z_original + ,cosmo + ,theta_rotate + ,false + ,0);*/ + halos.push_back(new LensHaloParticles >( + pp + ,nparticles[i] + ,redshift + ,cosmo + ,theta_rotate + ,false + ,0) + ); + /*/ + /*LensHaloParticles > halo(pp + ,nparticles[i] + ,z_original + ,cosmo + ,theta_rotate + ,false + ,0); + */ } - - if(row != Npoints){ - std::cerr << "Number of data rows in " << filename << " does not match expected number of particles." - << std::endl; - throw std::runtime_error("file reading error"); - } - }else{ - std::cerr << "Unable to open file " << filename << std::endl; - throw std::runtime_error("file reading error"); + skip += nparticles[i]; } - - std::cout << Npoints << " particle positions read from file " << filename << std::endl; - -} - -template -bool LensHaloParticles::readSizesFile(const std::string &filename - ,PType * pp - ,size_t Npoints - ,int Nsmooth - ,PosType min_size){ - - std::ifstream myfile(filename); - - // find number of particles - - PosType min=HUGE_VALF,max=0.0; - if (myfile.is_open()){ - - std::string str,label; - int count =0; - size_t Ntmp; - int NStmp; - while(std::getline(myfile, str)){ - std::stringstream ss(str); - ss >> label; - if(label == "#"){ - ss >> label; - if(label == "nparticles"){ - ss >> Ntmp; - if(Ntmp != Npoints){ - std::cerr << "Number of particles in " << filename << " does not match expected number" << std::endl; - throw std::runtime_error("file reading error"); - } - ++count; - } - if(label == "nsmooth"){ - ss >> NStmp; - if(NStmp != Nsmooth) return false; - ++count; - } - - }else break; - if(count == 2) break; - } - - if(count != 2){ - std::cerr << "File " << filename << " must have the header lines: " << std::endl - << "# nparticles ****" << std::endl; - throw std::runtime_error("file reading error"); - } - - size_t row = 0; - - std::cout << "reading in particle sizes from " << filename << "..." << std::endl; - - // read in particle sizes - while(std::getline(myfile, str)){ - if(str[0] == '#') continue; //for comments - std::stringstream ss(str); - - ss >> pp[row].Size; - if(min_size > pp[row].size() ) pp[row].Size = min_size; - min = min < pp[row].size() ? min : pp[row].size(); - max = max > pp[row].size() ? max : pp[row].size(); - row++; - } - - if(row != Npoints){ - std::cerr << "Number of data rows in " << filename << " does not match expected number of particles." - << std::endl; - throw std::runtime_error("file reading error"); - } - }else{ - return false; +}; + +bool MakeParticleLenses::readCSV(){ + + std::ifstream file(filename); + std::string line = ""; + size_t ntot = 0; + //while (getline(file, line) && ntot < 1000) ntot++; // ???? + while (getline(file, line)) ntot++; + + std::cout << "counted " << ntot << " entries in CSV file " + << filename << std::endl; + std::cout << " attempting to read them..."; + data.resize(ntot); + + std::string delimiter = ","; + + ntot = 0; + file.seekg(0); + // Iterate through each line and split the content using delimeter + while (getline(file, line)) + { + std::vector vec; + Utilities::splitstring(line,vec,delimiter); + + data[ntot][0] = stof(vec[0]); + data[ntot][1] = stof(vec[1]); + data[ntot][2] = stof(vec[2]); + data[ntot].Mass = stof(vec[3]); + data[ntot].type = 0; + ++ntot; } + // Close the File + file.close(); - std::cout << Npoints << " particle sizes read from file " << filename << std::endl; - std::cout << " maximun particle sizes " << max << " minimum " << min << " Mpc" << std::endl; - - return true; -} - -template -void LensHaloParticles::rotate_particles(PosType theta_x,PosType theta_y){ - - if(theta_x == 0.0 && theta_y == 0.0) return; - - PosType coord[3][3]; - PosType cx,cy,sx,sy; - - cx = cos(theta_x); sx = sin(theta_x); - cy = cos(theta_y); sy = sin(theta_y); + std::cout << ntot << " particle positions read from file " << filename << std::endl; - coord[0][0] = cy; coord[1][0] = -sy*sx; coord[2][0] = cx; - coord[0][1] = 0; coord[1][1] = cx; coord[2][1] = sx; - coord[0][2] = -sy; coord[1][2] = -cy*sx; coord[2][2] = cy*cx; + nparticles = {ntot,0,0,0,0,0}; + masses = {0,0,0,0,0,0}; + LensHaloParticles >::calculate_smoothing(Nsmooth,data.data(),data.size()); - PosType tmp[3]; - int j; - /* rotate particle positions */ - for(size_t i=0;i -void LensHaloParticles::calculate_smoothing(int Nsmooth,PType *pp - ,size_t Npoints){ - - std::cout << "Calculating smoothing of particles ..." << std::endl - << Nsmooth << " neighbors. If there are a lot of particles this could take a while." << std::endl; - - // make 3d tree of particle postions - TreeSimple tree3d(pp,Npoints,10,3,true); + return true; +}; + +bool readGadget2(){ + + /*GadgetFile > gadget_file(filename,data); + z_original = gadget.redshift; + + for(int n=0 ; n < gadget_file.numfiles ; ++n){ + gadget_file.openFile(); + gadget_file.readBlock("POS"); + gadget_file.readBlock("MASS"); + gadget_file.closeFile(); + } + + // sort by type + std::sort(data.begin(),data.end(),[](ParticleType &a1,ParticleType &a2){return a1.type < a2.type;}); + + ParticleType *pp; + size_t skip = 0; + for(int i = 0 ; i < 6 ; ++i){ //loop through types + + nparticles.push_back(gadget_file.npart[i]); + masses.push_back(gadget_file.tabmass[i]); + + if(gadget_file.npart[i] > 0){ + pp = data.data() + skip; // pointer to first particle of type + size_t N = gadget_file.npart[i]; + + LensHaloParticles >::calculate_smoothing(Nsmooth,pp,N); + } + skip += gadget_file.npart[i]; + }*/ - // find distance to nth neighbour for every particle - if(Npoints < 1000){ - IndexType neighbors[Nsmooth]; - for(size_t i=0;i -void LensHaloParticles::smooth_(TreeSimple *tree3d,PType *xp,size_t N,int Nsmooth){ - IndexType neighbors[Nsmooth]; - for(size_t i=0;iNearestNeighbors(&xp[i][0],Nsmooth,sizesp + i,neighbors); - tree3d->NearestNeighbors(&xp[i][0],Nsmooth,&(xp[i].Size),neighbors); - } -} - -template -void LensHaloParticles::writeSizes(const std::string &filename,int Nsmooth - ,const PType *pp,size_t Npoints -){ - - std::ofstream myfile(filename); - - // find number of particles - - if (myfile.is_open()){ - - std::cout << "Writing particle size information to file " << filename << " ...." << std::endl; - - myfile << "# nparticles " << Npoints << std::endl; - myfile << "# nsmooth " << Nsmooth << std::endl; - for(size_t i=0;i -void LensHaloParticles::makeSIE( - std::string new_filename /// file name - ,PosType redshift /// redshift of particles - ,double particle_mass /// particle mass - ,double total_mass /// total mass of SIE - ,double sigma /// velocity dispersion in km/s - ,double q /// axis ratio - ,Utilities::RandomNumbers_NR &ran - ){ - - size_t Npoints = total_mass/particle_mass; - PosType Rmax = (1+redshift)*total_mass*Grav*lightspeed*lightspeed/sigma/sigma/2; - Point_3d point; - double qq = sqrt(q); - - std::ofstream datafile; - datafile.open(new_filename); - - datafile << "# nparticles " << Npoints << std::endl; - datafile << "# mass " << particle_mass << std::endl; - // create particles - for(size_t i=0; i< Npoints ;++i){ - point[0] = ran.gauss(); - point[1] = ran.gauss(); - point[2] = ran.gauss(); - - point *= Rmax*ran()/point.length(); - - point[0] *= qq; - point[1] /= qq; - - datafile << point[0] << " " << point[1] << " " - << point[2] << " " << std::endl; - - } - - datafile.close(); -} diff --git a/TreeCode/simpleTree.cpp b/TreeCode/simpleTree.cpp index 892e1b88..678b480a 100644 --- a/TreeCode/simpleTree.cpp +++ b/TreeCode/simpleTree.cpp @@ -11,1015 +11,3 @@ //PosType dummy; - -BranchNB::BranchNB(int Ndim){ - center = new PosType[Ndim*3]; - boundary_p1 = ¢er[Ndim]; - boundary_p2 = ¢er[2*Ndim]; -} -BranchNB::~BranchNB(){ - delete center; -} - -template -TreeSimple::TreeSimple(PType *xpt,IndexType Npoints,int bucket,int Ndimensions,bool median){ - index = new IndexType[Npoints]; - IndexType ii; - - Nbucket = bucket; - Ndim = Ndimensions; - median_cut = median; - Nparticles = Npoints; - - xxp = xpt; - - for(ii=0;ii -TreeSimple::~TreeSimple() -{ - freeTreeNB(tree); - delete[] index; - return; -} - -template -void TreeSimple::PointsWithinEllipse( - PosType *ray /// center of ellipse - ,float rmax /// major axis - ,float rmin /// minor axis - ,float posangle /// position angle of major axis, smallest angle between the x-axis and the long axis - ,std::list &neighborlist /// output neighbor list, will be emptied if it contains anything on entry - ){ - PosType x,y,cs,sn; - - - if(rmax < rmin){float tmp = rmax; rmax=rmin; rmin=tmp;} - - if(rmax <=0.0 || rmin <= 0.0){ neighborlist.clear(); return; } - - // find point within a circle circumscribes the ellipse - PointsWithinCircle(ray,rmax,neighborlist); - - cs = cos(posangle); - sn = sin(posangle); - // go through points within the circle and reject points outside the ellipse - for( std::list::iterator it = neighborlist.begin();it != neighborlist.end();){ - x = xxp[*it][0]*cs - xxp[*it][1]*sn; - y = xxp[*it][0]*sn + xxp[*it][1]*cs; - if( ( pow(x/rmax,2) + pow(y/rmin,2) ) > 1) it = neighborlist.erase(it); - else ++it; - } - return; -} - -template -void TreeSimple::PointsWithinCircle( - PosType *ray /// center of circle - ,float rmax /// radius of circle - ,std::list &neighborlist /// output neighbor list, will be emptied if it contains anything on entry - ){ - - neighborlist.clear(); - - realray[0]=ray[0]; - realray[1]=ray[1]; - - //cout << "ray = " << ray[0] << " " << ray[1] << " rmax = " << rmax << endl; - moveTopNB(tree); - if( inbox(ray,tree->current->boundary_p1,tree->current->boundary_p2) == 0 ){ - - ray[0] = (ray[0] > tree->current->boundary_p1[0]) ? ray[0] : tree->current->boundary_p1[0]; - ray[0] = (ray[0] < tree->current->boundary_p2[0]) ? ray[0] : tree->current->boundary_p2[0]; - - ray[1] = (ray[1] > tree->current->boundary_p1[1]) ? ray[1] : tree->current->boundary_p1[1]; - ray[1] = (ray[1] < tree->current->boundary_p2[1]) ? ray[1] : tree->current->boundary_p2[1]; - - //cout << "ray = " << ray[0] << " " << ray[1] << endl; - //ray[0]=DMAX(ray[0],tree->current->boundary_p1[0]); - //ray[0]=DMIN(ray[0],tree->current->boundary_p2[0]); - - //ray[1]=DMAX(ray[1],tree->current->boundary_p1[1]); - //ray[1]=DMIN(ray[1],tree->current->boundary_p2[1]); - } - incell=1; - - _PointsWithin(ray,&rmax,neighborlist); - - return; -} -/** - * Used in PointsWithinKist() to walk tree.*/ -template -void TreeSimple::_PointsWithin(PosType *ray,float *rmax,std::list &neighborlist){ - - int j,incell2=1; - unsigned long i; - PosType radius; - short pass; - - if(incell){ // not found cell yet - - if( inbox(ray,tree->current->boundary_p1,tree->current->boundary_p2) ){ - //cout << " In box" << endl; - - // found the box small enough - if( Utilities::cutbox(ray,tree->current->boundary_p1,tree->current->boundary_p2,*rmax)==1 - || atLeaf() ){ - //cout << " Found cell" << endl; - - // whole box in circle or a leaf with ray in it - - incell=0; - - ray[0]=realray[0]; - ray[1]=realray[1]; - - if( atLeaf() ){ - // if leaf calculate the distance to all the points in cell - for(i=0;icurrent->nparticles;++i){ - for(j=0,radius=0.0;j<2;++j) radius+=pow(xxp[tree->current->particles[i]][j]-ray[j],2); - if( radius < *rmax**rmax ){ - neighborlist.push_back(tree->current->particles[i]); - //cout << "add point to list" << neighborlist.size() << endl; - //InsertAfterCurrentKist(neighborlist,tree->current->particles[i]); - } - } - }else{ // put all of points in box into getCurrentKist(imagelist) - for(i=0;icurrent->nparticles;++i){ - neighborlist.push_back(tree->current->particles[i]); - //InsertAfterCurrentKist(neighborlist,tree->current->particles[i]); - //cout << "add point to list" << neighborlist.size() << endl; - - } - } - - }else{ // keep going down the tree - - if(tree->current->child1 !=NULL){ - moveToChildNB(tree,1); - _PointsWithin(ray,rmax,neighborlist); - moveUpNB(tree); - - incell2=incell; - } - - if(tree->current->child2 !=NULL){ - moveToChildNB(tree,2); - _PointsWithin(ray,rmax,neighborlist); - moveUpNB(tree); - } - - // if ray found in second child go back to first to search for neighbors - if( (incell2==1) && (incell==0) ){ - if(tree->current->child1 !=NULL){ - moveToChildNB(tree,1); - _PointsWithin(ray,rmax,neighborlist); - moveUpNB(tree); - } - } - } - } // not in the box - - }else{ // found cell - - pass=Utilities::cutbox(ray,tree->current->boundary_p1,tree->current->boundary_p2,*rmax); - // does radius cut into the box - if( pass ){ - //cout << " Cell found searching other cells" << endl; - if( atLeaf() ){ /* leaf case */ - - for(i=0;icurrent->nparticles;++i){ - for(j=0,radius=0.0;j<2;++j) radius+=pow(xxp[tree->current->particles[i]][j]-ray[j],2); - if( radius < *rmax**rmax ){ - neighborlist.push_back(tree->current->particles[i]); - //InsertAfterCurrentKist(neighborlist,tree->current->particles[i]); - //cout << "add point to list" << neighborlist.size() << endl; - - } - } - }else if(pass==1){ // whole box is inside radius - - for(i=0;icurrent->nparticles;++i){ - neighborlist.push_back(tree->current->particles[i]); - //InsertAfterCurrentKist(neighborlist,tree->current->particles[i]); - //cout << "add point to list" << neighborlist.size() << endl; - - } - }else{ - if(tree->current->child1 !=NULL){ - moveToChildNB(tree,1); - _PointsWithin(ray,rmax,neighborlist); - moveUpNB(tree); - } - - if(tree->current->child2 !=NULL){ - moveToChildNB(tree,2); - _PointsWithin(ray,rmax,neighborlist); - moveUpNB(tree); - } - } - - } - } - - return; -} - -/** - * \brief finds the nearest neighbors in whatever dimensions tree is defined in - * */ -template -void TreeSimple::NearestNeighbors( - PosType *ray /// position - ,int Nneighbors /// number of neighbors to be found - ,float *radius /// distance furthest neighbor found from ray[] - ,IndexType *neighborsout /// list of the indexes of the neighbors - ) const{ - IndexType i; - //static int count=0,oldNneighbors=-1; - short j; - - //Ndim = tree->Ndimensions; - - PosType rneighbors[Nneighbors+Nbucket]; - IndexType neighbors[Nneighbors+Nbucket]; - - if(tree->top->nparticles <= Nneighbors){ - ERROR_MESSAGE(); - printf("ERROR: in TreeSimple::NearestNeighbors, number of neighbors > total number of particles\n"); - exit(1); - } - - /* initalize distance to neighbors to a large number */ - for(i=0;itop->boundary_p2[0]-tree->top->boundary_p1[0])); - neighbors[i] = 0; - } - - PosType rlray[tree->Ndimensions]; - for(j=0;jNdimensions;++j) rlray[j]=ray[j]; - - //moveTopNB(tree); - - TreeSimple::iterator iter(tree->top); - if( inbox(ray,tree->current->boundary_p1,tree->current->boundary_p2) == 0 ){ - for(j=0;jNdimensions;++j){ - ray[j] = (ray[j] > tree->current->boundary_p1[j]) ? ray[j] : tree->current->boundary_p1[j]; - ray[j] = (ray[j] < tree->current->boundary_p2[j]) ? ray[j] : tree->current->boundary_p2[j]; - } - } - int notfound = 1; - - _NearestNeighbors(ray,rlray,Nneighbors,neighbors,rneighbors,iter,notfound); - - for(i=0;i -void TreeSimple::_NearestNeighbors(PosType *ray,PosType *rlray,int Nneighbors - ,unsigned long *neighbors,PosType *rneighbors - ,TreeSimple::iterator &it,int ¬found) const { - - int incellNB2=1; - IndexType i; - short j; - - if(notfound){ /* not found cell yet */ - - if( inbox(ray,(*it)->boundary_p1,(*it)->boundary_p2) ){ - - /* found the box small enough */ - if( (*it)->nparticles <= Nneighbors+Nbucket ){ - notfound=0; - for(j=0;jNdimensions;++j) ray[j]=rlray[j]; - - /* calculate the distance to all the particles in cell */ - for(i=0;i<(*it)->nparticles;++i){ - for(j=0,rneighbors[i]=0.0;jNdimensions;++j){ - rneighbors[i] += pow(tree->xp[(*it)->particles[i]][j]-ray[j],2); - } - rneighbors[i]=sqrt( rneighbors[i] ); - assert(rneighbors[i] < 10); - neighbors[i]=(*it)->particles[i]; - } - - Utilities::quicksort(neighbors,rneighbors,(*it)->nparticles); - - - }else{ /* keep going down the tree */ - - if(it.down(1)){ - //moveToChildNB(tree,1); - _NearestNeighbors(ray,rlray,Nneighbors,neighbors,rneighbors,it,notfound); - /*printf("moving up from level %i\n",(*it)->level);*/ - //moveUpNB(tree); - it.up(); - incellNB2=notfound; - } - - if(it.down(2)){ - /*printf("moving to child2 from level %i\n",(*it)->level);*/ - //moveToChildNB(tree,2); - _NearestNeighbors(ray,rlray,Nneighbors,neighbors,rneighbors,it,notfound); - /*printf("moving up from level %i\n",(*it)->level);*/ - //moveUpNB(tree); - it.up(); - } - - /** if ray found in second child go back to first to search for neighbors **/ - if( (incellNB2==1) && (notfound==0) ){ - if(it.down(1)){ - //moveToChildNB(tree,1); - _NearestNeighbors(ray,rlray,Nneighbors,neighbors,rneighbors,it,notfound); - //moveUpNB(tree); - it.up(); - } - } - } - } - }else{ // found cell - /* does radius cut into the box */ - if( Utilities::cutbox(ray,(*it)->boundary_p1,(*it)->boundary_p2,rneighbors[Nneighbors-1]) ){ - - if( ((*it)->child1 == NULL)*((*it)->child2 == NULL)){ /* leaf case */ - - /* combine found neighbors with particles in box and resort */ - for(i=Nneighbors;i<((*it)->nparticles+Nneighbors);++i){ - for(j=0,rneighbors[i]=0.0;jNdimensions;++j){ - rneighbors[i]+=pow(tree->xp[(*it)->particles[i-Nneighbors]][j]-ray[j],2); - } - rneighbors[i]=sqrt( rneighbors[i] ); - assert(rneighbors[i] < 10); - neighbors[i]=(*it)->particles[i-Nneighbors]; - } - - Utilities::quicksort(neighbors,rneighbors,Nneighbors+Nbucket); - - }else{ - - if(it.down(1)){ - //moveToChildNB(tree,1); - _NearestNeighbors(ray,rlray,Nneighbors,neighbors,rneighbors,it,notfound); - //moveUpNB(tree); - it.up(); - } - - if(it.down(2)){ - //moveToChildNB(tree,2); - _NearestNeighbors(ray,rlray,Nneighbors,neighbors,rneighbors,it,notfound); - //moveUpNB(tree); - it.up(); - } - } - } - } - return; -} - -template -void TreeSimple::_NearestNeighbors(PosType *ray,int Nneighbors,unsigned long *neighbors,PosType *rneighbors){ - - int incellNB2=1; - IndexType i; - short j; - - if(incell){ /* not found cell yet */ - - if( inbox(ray,tree->current->boundary_p1,tree->current->boundary_p2) ){ - - /* found the box small enough */ - if( tree->current->nparticles <= Nneighbors+Nbucket ){ - incell=0; - for(j=0;jNdimensions;++j) ray[j]=realray[j]; - - /* calculate the distance to all the particles in cell */ - for(i=0;icurrent->nparticles;++i){ - for(j=0,rneighbors[i]=0.0;jNdimensions;++j){ - rneighbors[i] += pow(tree->xp[tree->current->particles[i]][j]-ray[j],2); - } - rneighbors[i]=sqrt( rneighbors[i] ); - assert(rneighbors[i] < 10); - neighbors[i]=tree->current->particles[i]; - } - - Utilities::quicksort(neighbors,rneighbors,tree->current->nparticles); - - }else{ /* keep going down the tree */ - - if(tree->current->child1 !=NULL){ - moveToChildNB(tree,1); - _NearestNeighbors(ray,Nneighbors,neighbors,rneighbors); - /*printf("moving up from level %i\n",tree->current->level);*/ - moveUpNB(tree); - - incellNB2=incell; - } - - if(tree->current->child2 !=NULL){ - /*printf("moving to child2 from level %i\n",tree->current->level);*/ - moveToChildNB(tree,2); - _NearestNeighbors(ray,Nneighbors,neighbors,rneighbors); - /*printf("moving up from level %i\n",tree->current->level);*/ - moveUpNB(tree); - } - - /** if ray found in second child go back to first to search for neighbors **/ - if( (incellNB2==1) && (incell==0) ){ - if(tree->current->child1 !=NULL){ - moveToChildNB(tree,1); - _NearestNeighbors(ray,Nneighbors,neighbors,rneighbors); - moveUpNB(tree); - } - } - } - } - }else{ // found cell - /* does radius cut into the box */ - if( Utilities::cutbox(ray,tree->current->boundary_p1,tree->current->boundary_p2,rneighbors[Nneighbors-1]) ){ - - if( (tree->current->child1 == NULL)*(tree->current->child2 == NULL)){ /* leaf case */ - - /* combine found neighbors with particles in box and resort */ - for(i=Nneighbors;i<(tree->current->nparticles+Nneighbors);++i){ - for(j=0,rneighbors[i]=0.0;jNdimensions;++j){ - rneighbors[i]+=pow(tree->xp[tree->current->particles[i-Nneighbors]][j]-ray[j],2); - } - rneighbors[i]=sqrt( rneighbors[i] ); - assert(rneighbors[i] < 10); - neighbors[i]=tree->current->particles[i-Nneighbors]; - } - - Utilities::quicksort(neighbors,rneighbors,Nneighbors+Nbucket); - - }else{ - - if(tree->current->child1 !=NULL){ - moveToChildNB(tree,1); - _NearestNeighbors(ray,Nneighbors,neighbors,rneighbors); - moveUpNB(tree); - } - - if(tree->current->child2 !=NULL){ - moveToChildNB(tree,2); - _NearestNeighbors(ray,Nneighbors,neighbors,rneighbors); - moveUpNB(tree); - } - } - } - } - return; -} - -template -TreeNBHndl TreeSimple::BuildTreeNB(PType *xp,IndexType Nparticles,IndexType *particles,int Ndims - ,PosType theta){ - TreeNBHndl tree; - IndexType i; - short j; - PosType p1[3],p2[3],center[3]; - - assert(Ndim == Ndims); - - for(j=0;j p2[j] ) p2[j]=xp[i][j]; - } - } - - for(j=0;jxp = xp; - - /* build the tree */ - _BuildTreeNB(tree,Nparticles,particles); - - /* visit every branch to find center of mass and cutoff scale */ - moveTopNB(tree); - - return tree; -} - - -// tree must be created and first branch must be set before start -template -void TreeSimple::_BuildTreeNB(TreeNBHndl tree,IndexType nparticles,IndexType *particles){ - IndexType i,cut,dimension; - short j; - BranchNB *cbranch,branch1(Ndim),branch2(Ndim); - PosType xcut; - PosType *x; - - cbranch=tree->current; /* pointer to current branch */ - - cbranch->center[0] = (cbranch->boundary_p1[0] + cbranch->boundary_p2[0])/2; - cbranch->center[1] = (cbranch->boundary_p1[1] + cbranch->boundary_p2[1])/2; - cbranch->quad[0]=cbranch->quad[1]=cbranch->quad[2]=0; - - /* leaf case */ - if(cbranch->nparticles <= Nbucket){ - cbranch->big_particle=0; - return; - } - - /* initialize boundaries to old boundaries */ - for(j=0;jboundary_p1[j]; - branch1.boundary_p2[j]=cbranch->boundary_p2[j]; - - branch2.boundary_p1[j]=cbranch->boundary_p1[j]; - branch2.boundary_p2[j]=cbranch->boundary_p2[j]; - } - cbranch->big_particle=0; - - // **** makes sure force does not require nbucket at leaf - - /* set dimension to cut box */ - dimension=(cbranch->level % Ndim); - - x=(PosType *)malloc((cbranch->nparticles-cbranch->big_particle)*sizeof(PosType)); - for(i=cbranch->big_particle;inparticles;++i) x[i]=tree->xp[particles[i]][dimension]; - - if(median_cut){ - Utilities::quicksort(particles,x,cbranch->nparticles-cbranch->big_particle); - - cut=(cbranch->nparticles-cbranch->big_particle)/2; - branch1.boundary_p2[dimension]=x[cut]; - branch2.boundary_p1[dimension]=x[cut]; - }else{ - xcut=(cbranch->boundary_p1[dimension]+cbranch->boundary_p2[dimension])/2; - branch1.boundary_p2[dimension]=xcut; - branch2.boundary_p1[dimension]=xcut; - - Utilities::quickPartition(xcut,&cut,particles - ,x,cbranch->nparticles-cbranch->big_particle); - } - - /* set particle numbers and pointers to particles */ - branch1.prev=cbranch; - branch1.nparticles=cut; - branch1.particles=particles+cbranch->big_particle; - - branch2.prev=cbranch; - branch2.nparticles=cbranch->nparticles-cbranch->big_particle - cut; - if(cut < (cbranch->nparticles-cbranch->big_particle) ) - branch2.particles=&particles[cut+cbranch->big_particle]; - else branch2.particles=NULL; - - free(x); - - if(branch1.nparticles > 0) attachChildToCurrentNB(tree,branch1,1); - if(branch2.nparticles > 0) attachChildToCurrentNB(tree,branch2,2); - - // work out brothers for children - if( (cbranch->child1 != NULL) && (cbranch->child2 != NULL) ){ - cbranch->child1->brother = cbranch->child2; - cbranch->child2->brother = cbranch->brother; - } - if( (cbranch->child1 == NULL) && (cbranch->child2 != NULL) ) - cbranch->child2->brother = cbranch->brother; - if( (cbranch->child1 != NULL) && (cbranch->child2 == NULL) ) - cbranch->child1->brother = cbranch->brother; - - - if( branch1.nparticles > 0 ){ - moveToChildNB(tree,1); - _BuildTreeNB(tree,branch1.nparticles,branch1.particles); - moveUpNB(tree); - } - - if(branch2.nparticles > 0 ){ - moveToChildNB(tree,2); - _BuildTreeNB(tree,branch2.nparticles,branch2.particles); - moveUpNB(tree); - } - - return; -} - -/** - * visits every branch in tree to calculate - * two critical lengths rcrit_angle and rcrit_part - * and mark largest particle in each node subject - * to some conditions - * - * if the sph[] are negative rcrit_part = 0 - */ - -/***** Constructors/Destructors *****/ - -/************************************************************************ - * NewBranchNB - * Returns pointer to new BranchNB struct. Initializes children pointers to NULL, - * and sets data field to input. Private. - ************************************************************************/ -template -BranchNB *TreeSimple::NewBranchNB(IndexType *particles,IndexType nparticles - ,PosType boundary_p1[],PosType boundary_p2[] - ,PosType center[],int level,unsigned long branchNBnumber){ - - BranchNB *branchNB = new BranchNB(Ndim); - int i; - - if (!branchNB){ - ERROR_MESSAGE(); fprintf(stderr,"allocation failure in NewBranchNB()\n"); - exit(1); - } - branchNB->particles = particles; - branchNB->nparticles = nparticles; - - for(i=0;icenter[i]=center[i]; - branchNB->level=level; - - for(i=0;iboundary_p1[i]= boundary_p1[i]; - branchNB->boundary_p2[i]= boundary_p2[i]; - } - - branchNB->number=branchNBnumber; - - branchNB->child1 = NULL; - branchNB->child2 = NULL; - branchNB->prev = NULL; - branchNB->brother = NULL; - - return(branchNB); -} - -/************************************************************************ - * FreeBranchNB - * Frees memory pointed to by branchNB. Private. - ************************************************************************/ -template -void TreeSimple::FreeBranchNB(BranchNB *branchNB){ - - assert( branchNB != NULL); - delete branchNB; - - return; -} - -/************************************************************************ - * NewTreeNB - * Returns pointer to new TreeNB struct. Initializes top, last, and - * current pointers to NULL. Sets NbranchNBes field to 0. Exported. - ************************************************************************/ -template -TreeNBHndl TreeSimple::NewTreeNB(IndexType *particles,IndexType nparticles - ,PosType boundary_p1[],PosType boundary_p2[], - PosType center[],short Ndimensions){ - - TreeNBHndl tree; - - tree = new TreeNBStruct;// *)malloc(sizeof(TreeNBStruct)); - if (!tree){ - ERROR_MESSAGE(); fprintf(stderr,"allocation failure in NewTreeNB()\n"); - exit(1); - } - - tree->top= NewBranchNB(particles,nparticles,boundary_p1,boundary_p2,center,0,0); - if (!(tree->top)){ - ERROR_MESSAGE(); fprintf(stderr,"allocation failure in NewTreeNB()\n"); - exit(1); - } - - tree->Nbranches = 1; - tree->current = tree->top; - tree->Ndimensions=Ndimensions; - - return(tree); -} - -template -void TreeSimple::freeTreeNB(TreeNBHndl tree){ - /* free treeNB - * does not free the particle positions, masses or rsph - */ - - if(tree == NULL) return; - - emptyTreeNB(tree); - FreeBranchNB(tree->top); - delete tree; - - return; -} - -template -short TreeSimple::emptyTreeNB(TreeNBHndl tree){ - - moveTopNB(tree); - _freeTreeNB(tree,0); - - assert(tree->Nbranches == 1); - - return 1; -} - -template -void TreeSimple::_freeTreeNB(TreeNBHndl tree,short child){ - BranchNB *branch; - - assert( tree ); - assert( tree->current); - - if(tree->current->child1 != NULL){ - moveToChildNB(tree,1); - _freeTreeNB(tree,1); - } - - if(tree->current->child2 != NULL){ - moveToChildNB(tree,2); - _freeTreeNB(tree,2); - } - - if( (tree->current->child1 == NULL)*(tree->current->child2 == NULL) ){ - - if(atTopNB(tree)) return; - - branch = tree->current; - moveUpNB(tree); - FreeBranchNB(branch); - - /*printf("*** removing branch %i number of branches %i\n",branch->number - ,tree->Nbranches-1);*/ - - if(child==1) tree->current->child1 = NULL; - if(child==2) tree->current->child2 = NULL; - - --tree->Nbranches; - - return; - } - - return; -} - -/************************************************************************ - * isEmptyNB - * Returns "true" if the TreeNB is empty and "false" otherwise. Exported. - ************************************************************************/ -template -bool TreeSimple::isEmptyNB(TreeNBHndl tree){ - - assert(tree != NULL); - return(tree->Nbranches == 0); -} - -/************************************************************************ - * atTop - * Returns "true" if current is the same as top and "false" otherwise. - * Exported. - * Pre: !isEmptyNB(tree) - ************************************************************************/ -template -bool TreeSimple::atTopNB(TreeNBHndl tree){ - - assert(tree != NULL); - if( isEmptyNB(tree) ){ - ERROR_MESSAGE(); - fprintf(stderr, "TreeNB Error: calling atTop() on empty tree\n"); - exit(1); - } - return(tree->current == tree->top); -} - -/************************************************************************ - * noChild - * Returns "true" if the child of the current branchNB does not exist and "false" otherwise. - * Exported. - * Pre: !isEmptyNB(tree) - ************************************************************************/ -template -bool TreeSimple::noChildNB(TreeNBHndl tree){ - - assert(tree != NULL); - if( isEmptyNB(tree) ){ - - ERROR_MESSAGE(); fprintf(stderr, "TreeNB Error: calling atTop() on empty tree\n"); - exit(1); - } - - if( (tree->current->child1 == NULL) || (tree->current->child2 == NULL) ) return true; - return false; -} - -/************************************************************************ - * offEndNB - * Returns "true" if current is off end and "false" otherwise. Exported. - ************************************************************************/ -template -bool TreeSimple::offEndNB(TreeNBHndl tree){ - - assert(tree != NULL); - return(tree->current == NULL); -} - -/************************************************************************ - * getCurrentNB - * Returns the particuls of current. Exported. - * Pre: !offEndNB(tree) - ************************************************************************/ -template -void TreeSimple::getCurrentNB(TreeNBHndl tree,IndexType *particles,IndexType *nparticles){ - - assert(tree != NULL); - if( offEndNB(tree) ){ - - ERROR_MESSAGE(); fprintf(stderr, "TreeNB Error: calling getCurrent() when current is off end\n"); - exit(1); - } - - *nparticles=tree->current->nparticles; - particles=tree->current->particles; - - return; -} - -/************************************************************************ - * getNbranchesNB - * Returns the NbranchNBes of tree. Exported. - ************************************************************************/ -template -unsigned long TreeSimple::getNbranchesNB(TreeNBHndl tree){ - - assert(tree != NULL); - return(tree->Nbranches); -} - -/***** Manipulation procedures *****/ - -/************************************************************************ - * moveTopNB - * Moves current to the front of tree. Exported. - * Pre: !isEmptyNB(tree) - ************************************************************************/ -template -void TreeSimple::moveTopNB(TreeNBHndl tree){ - //std::cout << tree << std::endl; - //std::cout << tree->current << std::endl; - //std::cout << tree->top << std::endl; - - assert(tree != NULL); - if( isEmptyNB(tree) ){ - - ERROR_MESSAGE(); fprintf(stderr, "TreeNB Error: calling moveTopNB() on empty tree\n"); - exit(1); - } - - - tree->current = tree->top; - - return; -} - -/************************************************************************ - * movePrev - * Moves current to the branchNB before it in tree. This can move current - * off end. Exported. - * Pre: !offEndNB(tree) - ************************************************************************/ -template -void TreeSimple::moveUpNB(TreeNBHndl tree){ - - assert(tree != NULL); - if( offEndNB(tree) ){ - ERROR_MESSAGE(); fprintf(stderr, "TreeNB Error: call to moveUpNB() when current is off end\n"); - exit(1); - } - if( tree->current == tree->top ){ - ERROR_MESSAGE(); fprintf(stderr, "TreeNB Error: call to moveUpNB() tried to move off the top\n"); - exit(1); - } - - tree->current = tree->current->prev; /* can move off end */ - return; -} - -/************************************************************************ - * moveToChildNB - * Moves current to child branchNB after it in tree. This can move current off - * end. Exported. - * Pre: !offEndNB(tree) - ************************************************************************/ -template -void TreeSimple::moveToChildNB(TreeNBHndl tree,int child){ - - assert(tree != NULL); - if( offEndNB(tree) ){ - - ERROR_MESSAGE(); fprintf(stderr, "TreeNB Error: calling moveChildren() when current is off end\n"); - exit(1); - } - if(child==1){ - if( tree->current->child1 == NULL ){ - ERROR_MESSAGE(); fprintf(stderr, "TreeNB Error: moveToChildNB() typing to move to child1 when it doesn't exist\n"); - exit(1); - } - tree->current = tree->current->child1; - } - if(child==2){ - if( tree->current->child2 == NULL ){ - ERROR_MESSAGE(); fprintf(stderr, "TreeNB Error: moveToChildNB() typing to move to child2 when it doesn't exist\n"); - exit(1); - } - tree->current = tree->current->child2; - } - return; -} - -/************************************************************************ - * insertAfterCurrent - * Inserts a new BranchNB after the current branchNB in the tree and sets the - * data field of the new BranchNB to input. Exported. - * Pre: !offEndNB(tree) - ************************************************************************/ -template -void TreeSimple::insertChildToCurrentNB(TreeNBHndl tree, IndexType *particles,IndexType nparticles - ,PosType boundary_p1[],PosType boundary_p2[] - ,PosType center[],int child){ - - BranchNB *branchNB; - - /*printf("attaching child%i current paricle number %i\n",child,tree->current->nparticles);*/ - - branchNB = NewBranchNB(particles,nparticles,boundary_p1,boundary_p2,center - ,tree->current->level+1,tree->Nbranches); - - assert(tree != NULL); - - if( offEndNB(tree) ){ - - ERROR_MESSAGE(); fprintf(stderr, "TreeNB Error: calling insertChildToCurrentNB() when current is off end\n"); - exit(1); - } - - branchNB->prev = tree->current; - - if(child==1){ - if(tree->current->child1 != NULL){ - ERROR_MESSAGE(); fprintf(stderr, "TreeNB Error: calling insertChildToCurrentNB() when child1 alread exists\n"); - exit(1); - } - tree->current->child1 = branchNB; - } - if(child==2){ - if(tree->current->child2 != NULL){ - ERROR_MESSAGE(); - fprintf(stderr, "TreeNB Error: calling insertChildToCurrentNB() when child2 alread exists\n current level=%i Nbranches=%li\n" - ,tree->current->level,tree->Nbranches); - exit(1); - } - tree->current->child2 = branchNB; - } - - tree->Nbranches++; - - return; -} - - /* same as above but takes a branchNB structure */ -template -void TreeSimple::attachChildToCurrentNB(TreeNBHndl tree,BranchNB &data,int child){ - - insertChildToCurrentNB(tree,data.particles,data.nparticles - ,data.boundary_p1,data.boundary_p2,data.center,child); - return; -} - -// step for walking tree by iteration instead of recursion -template -bool TreeSimple::TreeNBWalkStep(TreeNBHndl tree,bool allowDescent){ - if(allowDescent && tree->current->child1 != NULL){ - moveToChildNB(tree,1); - return true; - } - if(allowDescent && tree->current->child2 != NULL){ - moveToChildNB(tree,2); - return true; - } - - if(tree->current->brother != NULL){ - tree->current=tree->current->brother; - return true; - } - return false; -} - diff --git a/TreeCode_link/double_sort.cpp b/TreeCode_link/double_sort.cpp index 35e9fc8f..a05175f9 100644 --- a/TreeCode_link/double_sort.cpp +++ b/TreeCode_link/double_sort.cpp @@ -353,41 +353,4 @@ namespace Utilities{ return ; } - // return 1 (0) if box is (not) within rmax of ray - int cutbox(const PosType* center,PosType *p1,PosType *p2,float rmax){ - /* returns: 0 if whole box is outside rmax from ray[] - * 1 if whole box is inside circle but ray is not in the box - * 2 if ray[] is inside box - * 3 if box intersects circle but ray[] is not inside box - */ - short i,tick=0; - PosType close[2],rtmp; - PosType tmp1,tmp2; - - // find closest point on box borders to ray[] - for(i=0;i<2;++i){ - if( center[i] < p1[i] ){ - close[i]=p1[i]; - }else if(center[i] > p2[i]){ - close[i]=p2[i]; - }else{ - close[i]=center[i]; - ++tick; - } - } - - if(tick==2) return 2; // ray is inside box - - for(i=0,rtmp=0;i<2;++i) rtmp += pow(center[i] - close[i],2); - - if(rtmp>rmax*rmax) return 0; // box is all outside circle - - // find farthest point on box border from ray[] - for(i=0,rtmp=0;i<2;++i) rtmp += ((tmp1 = pow(center[i]-p1[i],2)) > (tmp2=pow(center[i]-p2[i],2))) ? tmp1 : tmp2; - //for(i=0,rtmp=0;i<2;++i) rtmp += DMAX(pow(ray[i]-p1[i],2),pow(ray[i]-p2[i],2)); - - if(rtmp &vec + ,const std::string &delimiter){ + size_t pos = 0; + + while ((pos = line.find(delimiter)) != std::string::npos) { + vec.push_back(line.substr(0, pos)); + line.erase(0, pos + delimiter.length()); + } +} diff --git a/include/CMakeLists.txt b/include/CMakeLists.txt index 76729d3f..8c4f3b56 100644 --- a/include/CMakeLists.txt +++ b/include/CMakeLists.txt @@ -49,6 +49,6 @@ add_sources( concave_hull.h bands_etc.h gaussian.h - gadget.hh + #gadget.hh particle_types.h ) diff --git a/include/Tree.h b/include/Tree.h index 967b50d5..120a4a18 100644 --- a/include/Tree.h +++ b/include/Tree.h @@ -312,7 +312,6 @@ namespace Utilities{ ,Point *pointsarray,PosType *arr,unsigned long N); void quickPartitionPoints(PosType pivotvalue,unsigned long *pivotindex ,Point *pointarray,PosType (*func)(Point &p),unsigned long N); - int cutbox(const PosType* center,PosType *p1,PosType *p2,float rmax); void log_polar_grid(Point *i_points,PosType rmax,PosType rmin,PosType *center,long Ngrid); void findarea(ImageInfo *imageinfo); void writeCurves(int m, ImageInfo *critical, int Ncrit, int index); @@ -342,6 +341,45 @@ namespace Utilities{ PosType TwoDInterpolator(PosType *x,int Npixels,PosType range,PosType *center,PosType *map,bool init=true); PosType TwoDInterpolator(PosType *map); + // return 1 (0) if box is (not) within rmax of ray + template + int cutbox(const T* center,PosType *p1,PosType *p2,float rmax){ + /* returns: 0 if whole box is outside rmax from ray[] + * 1 if whole box is inside circle but ray is not in the box + * 2 if ray[] is inside box + * 3 if box intersects circle but ray[] is not inside box + */ + short i,tick=0; + PosType close[2],rtmp; + PosType tmp1,tmp2; + + // find closest point on box borders to ray[] + for(i=0;i<2;++i){ + if( center[i] < p1[i] ){ + close[i]=p1[i]; + }else if(center[i] > p2[i]){ + close[i]=p2[i]; + }else{ + close[i]=center[i]; + ++tick; + } + } + + if(tick==2) return 2; // ray is inside box + + for(i=0,rtmp=0;i<2;++i) rtmp += pow(center[i] - close[i],2); + + if(rtmp>rmax*rmax) return 0; // box is all outside circle + + // find farthest point on box border from ray[] + for(i=0,rtmp=0;i<2;++i) rtmp += ((tmp1 = pow(center[i]-p1[i],2)) > (tmp2=pow(center[i]-p2[i],2))) ? tmp1 : tmp2; + //for(i=0,rtmp=0;i<2;++i) rtmp += DMAX(pow(ray[i]-p1[i],2),pow(ray[i]-p2[i],2)); + + if(rtmp void quicksortPoints_multithread(Point *pointarray,PosType *arr,unsigned long N,int level=0){ diff --git a/include/particle_halo.h b/include/particle_halo.h index ef65ea4c..7262cd17 100644 --- a/include/particle_halo.h +++ b/include/particle_halo.h @@ -6,12 +6,6 @@ // // -things to do: - -protect particle data by storing it in a class that reads data and generates LensHaloParticles - -make multi-type halos center on a common cneter of mass - #ifndef GLAMER_particle_halo_h #define GLAMER_particle_halo_h @@ -19,9 +13,10 @@ make multi-type halos center on a common cneter of mass #include "quadTree.h" #include "simpleTree.h" #include "particle_types.h" -#include "gadget.hh" +#include "utilities_slsim.h" +#include "lens_halos.h" -enum SimFileFormats {ascii,gadget2}; +//#include "gadget.hh" /** * \brief A class that represents the lensing by a collection of simulation particles. @@ -32,7 +27,7 @@ enum SimFileFormats {ascii,gadget2}; time and memory consuming when there are a large number of particles. Input format: - ASCCI - a table of three floats for positions in comoving Mpc (no h factor). + ASCII - a table of three floats for positions in comoving Mpc (no h factor). The lines "# nparticles ...." and "# mass ...." must be in header at the top of the file. # is otherwise a comment character. Only one type of particle in a single input file. @@ -47,7 +42,7 @@ class LensHaloParticles : public LensHalo LensHaloParticles(const std::string& simulation_filename /// name of data files - ,SimFileFormats format /// format of data file + ,SimFileFormat format /// format of data file ,PosType redshift /// redshift of origin ,int Nsmooth /// number of neighbours for adaptive smoothing ,const COSMOLOGY& cosmo /// cosmology @@ -57,7 +52,8 @@ class LensHaloParticles : public LensHalo ,PosType MinPSize /// minimum particle size ); - LensHaloParticles(PType *pdata + LensHaloParticles(PType *pdata /// list of particles pdata[][i] should be the position in physical Mpc + ,size_t Npoints /// redshift of origin ,float redshift /// redshift of origin ,const COSMOLOGY& cosmo /// cosmology ,Point_2d theta_rotate /// rotation of particles around the origin @@ -101,8 +97,6 @@ class LensHaloParticles : public LensHalo ,PType *pp ); - - static void writeSizes(const std::string &filename ,int Nsmooth ,const PType *pp @@ -142,60 +136,653 @@ class LensHaloParticles : public LensHalo TreeQuadParticles * qtree; }; -void makeHalosFromGadget(const std::string &filename - ,std::vector > &data - ,std::vector > > &halos - ,int Nsmooth - ,float redshift - ,Point_2d theta_rotate /// rotation of particles around the origin - ,const COSMOLOGY &cosmo - ){ +template +LensHaloParticles::LensHaloParticles(const std::string& simulation_filename + ,SimFileFormat format + ,PosType redshift + ,int Nsmooth + ,const COSMOLOGY& cosmo + ,Point_2d theta_rotate + ,bool recenter + ,bool my_multimass + ,PosType MinPSize + ) +:min_size(MinPSize),multimass(my_multimass),simfile(simulation_filename) +{ + + LensHalo::setZlens(redshift); + LensHalo::setCosmology(cosmo); + LensHalo::set_flag_elliptical(false); + stars_N = 0; + stars_implanted = false; - GadgetFile > gadget_file(filename,data); + Rmax = 1.0e3; + LensHalo::setRsize(Rmax); - for(int n=0 ; n < gadget_file.numfiles ; ++n){ - gadget_file.openFile(); - gadget_file.readBlock("POS"); - gadget_file.readBlock("MASS"); - gadget_file.closeFile(); + switch (format) { + case ascii: + readPositionFileASCII(simulation_filename,multimass,pp); + break; + default: + std::cerr << "LensHaloParticles does not accept ." << std::endl; + throw std::invalid_argument("bad format"); } - // sort by type - std::sort(data.begin(),data.end(),[](ParticleType &a1,ParticleType &a2){return a1.type < a2.type;}); + sizefile = simfile + "." + std::to_string(Nsmooth) + "sizes"; - ParticleType *pp; + if(!readSizesFile(sizefile,pp,Npoints,Nsmooth,min_size)){ + + // calculate sizes + calculate_smoothing(Nsmooth,pp,Npoints); + + // save result to a file for future use + writeSizes(sizefile,Nsmooth,pp,Npoints); + //for(size_t i=0; i 0){ - - pp = data.data() + skip; // pointer to first particle of type - size_t N = gadget_file.npart[i]; + // convert from comoving to physical coordinates + PosType scale_factor = 1/(1+redshift); + mcenter *= 0.0; + PosType max_mass = 0.0,min_mass = HUGE_VALF,mass=0; + for(size_t i=0;i max_mass) ? pp[multimass*i].mass() : max_mass; + min_mass = (pp[multimass*i].mass() < min_mass) ? pp[multimass*i].mass() : min_mass; + } + LensHalo::setMass(mass); + + mcenter /= mass; + + std::cout << " Particle mass range : " << min_mass << " to " << max_mass << " ratio of : " << max_mass/min_mass << std::endl; + + + if(recenter){ + PosType r2,r2max=0; + for(size_t i=0;i r2max) r2max = r2; + } + + LensHalo::setRsize( sqrt(r2max) ); + } + + // rotate positions + rotate_particles(theta_rotate[0],theta_rotate[1]); + + qtree = new TreeQuadParticles >(pp,Npoints,-1,-1,0,20); +} + +template +LensHaloParticles::LensHaloParticles( + PType *pdata + ,size_t Nparticles + ,float redshift /// redshift of origin + ,const COSMOLOGY& cosmo /// cosmology + ,Point_2d theta_rotate /// rotation of particles around the origin + ,bool recenter /// center on center of mass + ,float MinPSize /// minimum particle size +):min_size(MinPSize),multimass(true),Npoints(Nparticles) +{ + + LensHalo::setZlens(redshift); + LensHalo::setCosmology(cosmo); + LensHalo::set_flag_elliptical(false); + + stars_N = 0; + stars_implanted = false; + + Rmax = 1.0e3; + LensHalo::setRsize(Rmax); + + // convert from comoving to physical coordinates + PosType scale_factor = 1/(1+redshift); + mcenter *= 0.0; + PosType max_mass = 0.0,min_mass = HUGE_VALF,mass=0; + for(size_t i=0;i max_mass) ? pp[i].mass() : max_mass; + min_mass = (pp[i].mass() < min_mass) ? pp[i].mass() : min_mass; + } + LensHalo::setMass(mass); + + mcenter /= mass; + + std::cout << " Particle mass range : " << min_mass << " to " << max_mass << " ratio of : " << max_mass/min_mass << std::endl; + + + if(recenter){ + PosType r2,r2max=0; + for(size_t i=0;i >::readSizesFile(sizefile,pp,gadget_file.npart[i],Nsmooth,0)){ - // calculate sizes - //sizes.resize(Npoints); - LensHaloParticles >::calculate_smoothing(Nsmooth,pp,N); + r2 = pp[i][0]*pp[i][0] + pp[i][1]*pp[i][1] + pp[i][2]*pp[i][2]; + if(r2 > r2max) r2max = r2; + } + + LensHalo::setRsize( sqrt(r2max) ); + } + + // rotate positions + rotate_particles(theta_rotate[0],theta_rotate[1]); + + qtree = new TreeQuadParticles >(pp,Npoints,-1,-1,0,20); +} + + + +template +LensHaloParticles::~LensHaloParticles(){ + delete qtree; +} + +template +void LensHaloParticles::force_halo(double *alpha,KappaType *kappa,KappaType *gamma,KappaType *phi + ,double const *xcm + ,bool subtract_point,PosType screening){ + qtree->force2D_recur(xcm,alpha,kappa,gamma,phi); + + alpha[0] *= -1; + alpha[1] *= -1; +} + +template +void LensHaloParticles::rotate(Point_2d theta){ + rotate_particles(theta[0],theta[1]); + delete qtree; + qtree =new TreeQuadParticles >(pp,Npoints,multimass,true,0,20); +} + +/** \brief Reads number of particle and particle positons into Npoint and xp from a ASCII file. + * + * Data file must have the lines "# nparticles ***" and "# mass ***" in the header. All header + * lines must begin with a "# " + * + * Coordinates of particles are in physical Mpc units. + */ +template +void LensHaloParticles::readPositionFileASCII(const std::string &filename + ,bool multimass + ,PType *pp + ){ + + int ncoll = Utilities::IO::CountColumns(filename); + if(!multimass && ncoll != 3 ){ + std::cerr << filename << " should have three columns!" << std::endl; + } + if(multimass && ncoll != 4 ){ + std::cerr << filename << " should have four columns!" << std::endl; + } + + std::ifstream myfile(filename); + + size_t Npoints = 0; + + // find number of particles + + if (myfile.is_open()){ + + float tmp_mass = 0.0; + std::string str,label; + int count =0; + while(std::getline(myfile, str)){ + std::stringstream ss(str); + ss >> label; + if(label == "#"){ + ss >> label; + if(label == "nparticles"){ + ss >> Npoints; + ++count; + } + if(!multimass){ + if(label == "mass"){ + ss >> tmp_mass; + ++count; + } + } + }else break; + if(multimass && count == 1 ) break; + if(!multimass && count == 2 ) break; + } + + if(count == 0){ + if(multimass) std::cerr << "File " << filename << " must have the header lines: " << std::endl + << "# nparticles ****" << std::endl << "# mass ****" << std::endl; + if(!multimass) std::cerr << "File " << filename << " must have the header lines: " << std::endl + << "# nparticles ****" << std::endl; + throw std::runtime_error("file reading error"); + } + + pp = new PType[Npoints]; + //xp = Utilities::PosTypeMatrix(Npoints,3); + //if(multimass) masses.resize(Npoints); + //else masses.push_back(tmp_mass); + + size_t row = 0; + + // read in particle positions + if(!multimass){ + while(std::getline(myfile, str) && row < Npoints){ + if(str[0] == '#') continue; //for comments + std::stringstream ss(str); - // save result to a file for future use - LensHaloParticles >::writeSizes(sizefile,Nsmooth,pp,N); + ss >> pp[row][0]; + if(!(ss >> pp[row][1])) std::cerr << "3 columns are expected in line " << row + << " of " << filename << std::endl; + if(!(ss >> pp[row][2])) std::cerr << "3 columns are expected in line " << row + << " of " << filename << std::endl; + + row++; } + }else{ + while(std::getline(myfile, str) && row < Npoints){ + if(str[0] == '#') continue; //for comments + std::stringstream ss(str); + + ss >> pp[row][0]; + if(!(ss >> pp[row][1])) std::cerr << "4 columns are expected in line " << row + << " of " << filename << std::endl; + if(!(ss >> pp[row][2])) std::cerr << "4 columns are expected in line " << row + << " of " << filename << std::endl; + if(!(ss >> pp[row].Mass)) std::cerr << "4 columns are expected in line " << row + << " of " << filename << std::endl; + + row++; + } + } + + if(row != Npoints){ + std::cerr << "Number of data rows in " << filename << " does not match expected number of particles." + << std::endl; + throw std::runtime_error("file reading error"); + } + }else{ + std::cerr << "Unable to open file " << filename << std::endl; + throw std::runtime_error("file reading error"); + } + + std::cout << Npoints << " particle positions read from file " << filename << std::endl; + +} + +template +bool LensHaloParticles::readSizesFile(const std::string &filename + ,PType * pp + ,size_t Npoints + ,int Nsmooth + ,PosType min_size){ + + std::ifstream myfile(filename); + + // find number of particles + + PosType min=HUGE_VALF,max=0.0; + if (myfile.is_open()){ + + std::string str,label; + int count =0; + size_t Ntmp; + int NStmp; + while(std::getline(myfile, str)){ + std::stringstream ss(str); + ss >> label; + if(label == "#"){ + ss >> label; + if(label == "nparticles"){ + ss >> Ntmp; + if(Ntmp != Npoints){ + std::cerr << "Number of particles in " << filename << " does not match expected number" << std::endl; + throw std::runtime_error("file reading error"); + } + ++count; + } + if(label == "nsmooth"){ + ss >> NStmp; + if(NStmp != Nsmooth) return false; + ++count; + } + + }else break; + if(count == 2) break; + } + + if(count != 2){ + std::cerr << "File " << filename << " must have the header lines: " << std::endl + << "# nparticles ****" << std::endl; + throw std::runtime_error("file reading error"); + } + + size_t row = 0; + + std::cout << "reading in particle sizes from " << filename << "..." << std::endl; + + // read in particle sizes + while(std::getline(myfile, str)){ + if(str[0] == '#') continue; //for comments + std::stringstream ss(str); - halos.emplace_back(pp - ,redshift - ,cosmo /// cosmology - ,theta_rotate /// rotation of particles around the origin - ,false - ,0); + ss >> pp[row].Size; + if(min_size > pp[row].size() ) pp[row].Size = min_size; + min = min < pp[row].size() ? min : pp[row].size(); + max = max > pp[row].size() ? max : pp[row].size(); + row++; + } + + if(row != Npoints){ + std::cerr << "Number of data rows in " << filename << " does not match expected number of particles." + << std::endl; + throw std::runtime_error("file reading error"); + } + }else{ + return false; + } + + std::cout << Npoints << " particle sizes read from file " << filename << std::endl; + std::cout << " maximun particle sizes " << max << " minimum " << min << " Mpc" << std::endl; + + return true; +} + +template +void LensHaloParticles::rotate_particles(PosType theta_x,PosType theta_y){ + + if(theta_x == 0.0 && theta_y == 0.0) return; + + PosType coord[3][3]; + PosType cx,cy,sx,sy; + + cx = cos(theta_x); sx = sin(theta_x); + cy = cos(theta_y); sy = sin(theta_y); + + coord[0][0] = cy; coord[1][0] = -sy*sx; coord[2][0] = cx; + coord[0][1] = 0; coord[1][1] = cx; coord[2][1] = sx; + coord[0][2] = -sy; coord[1][2] = -cy*sx; coord[2][2] = cy*cx; + + PosType tmp[3]; + int j; + /* rotate particle positions */ + for(size_t i=0;i +void LensHaloParticles::calculate_smoothing(int Nsmooth,PType *pp + ,size_t Npoints){ + + int nthreads = Utilities::GetNThreads(); + + std::cout << "Calculating smoothing of particles ..." << std::endl + << Nsmooth << " neighbors. If there are a lot of particles this could take a while." << std::endl; + + // make 3d tree of particle postions + TreeSimple tree3d(pp,Npoints,10,3,true); + + // find distance to nth neighbour for every particle + if(Npoints < 1000){ + IndexType neighbors[Nsmooth]; + for(size_t i=0;i::smooth_,&tree3d + ,&(pp[ii*chunksize]),N,Nsmooth); + } + for(int ii = 0; ii < nthreads ;++ii) thr[ii].join(); + } + std::cout << "done" << std::endl; +} + +template +void LensHaloParticles::smooth_(TreeSimple *tree3d,PType *xp,size_t N,int Nsmooth){ + + IndexType neighbors[Nsmooth]; + for(size_t i=0;iNearestNeighbors(&xp[i][0],Nsmooth,sizesp + i,neighbors); + tree3d->NearestNeighbors(&xp[i][0],Nsmooth,&(xp[i].Size),neighbors); + } +} + + +template +void LensHaloParticles::writeSizes(const std::string &filename,int Nsmooth + ,const PType *pp,size_t Npoints + ){ + + std::ofstream myfile(filename); + + // find number of particles + + if (myfile.is_open()){ + + std::cout << "Writing particle size information to file " << filename << " ...." << std::endl; + + myfile << "# nparticles " << Npoints << std::endl; + myfile << "# nsmooth " << Nsmooth << std::endl; + for(size_t i=0;i +void LensHaloParticles::makeSIE( + std::string new_filename /// file name + ,PosType redshift /// redshift of particles + ,double particle_mass /// particle mass + ,double total_mass /// total mass of SIE + ,double sigma /// velocity dispersion in km/s + ,double q /// axis ratio + ,Utilities::RandomNumbers_NR &ran + ){ + + size_t Npoints = total_mass/particle_mass; + PosType Rmax = (1+redshift)*total_mass*Grav*lightspeed*lightspeed/sigma/sigma/2; + Point_3d point; + double qq = sqrt(q); + + std::ofstream datafile; + datafile.open(new_filename); + + datafile << "# nparticles " << Npoints << std::endl; + datafile << "# mass " << particle_mass << std::endl; + // create particles + for(size_t i=0; i< Npoints ;++i){ + point[0] = ran.gauss(); + point[1] = ran.gauss(); + point[2] = ran.gauss(); + + point *= Rmax*ran()/point.length(); + + point[0] *= qq; + point[1] /= qq; + + datafile << point[0] << " " << point[1] << " " + << point[2] << " " << std::endl; + + } + + datafile.close(); +} + +/** \brief A class for constructing LensHalos from particles in a data file. + +

+ The particle data is stored in this structure so the LensHaloParticles should not be copied and then this object allowed to be destroyed. The halos will be destroyed when this structure is destroyed. + + A separate LensHaloParticles is made for each type of particle that is present in the gadget file. The nearest N neighbour smoothing is done in 3D on construction separately for + each type of particle. The smoothing sizes are automatically saved to files and used again + if the class is constructed again with the same file and smoothing number. + + The data file formats are : + + gadget2 - standard Gadget-2 output. A different LensHalo is + made for each type of particle. The nearest neighbour + smoothing scales are calculated within particle type. + The sph density of gas particles are not used. + + csv - CSV ascii format without header. The first three columns + are the positions and the 4th is the mass. There can be + more columns that are ignored. There is only one type of + particle. The lengths are in physical (not comoving) + Mpc/h and the masses are in Msun/h. The sizes are + calculated. + + glamb - This is a binary format internal to GLAMER used to store + the positions, masses and sizes of the particles. If + GLAMER has generated one, it should be all that is + needed to recreate the LensHaloParticles. + + ascii2 - This is the original ascii GLAMER format. + Three floats for positions in comoving Mpc (no h factor). + The lines "# nparticles ...." and "# mass ...." must be + in header at the top of the file. # is otherwise a + comment character. Only one type of particle in a single + input file. + <\p> +*/ +class MakeParticleLenses{ + +public: + + /// vector of LensHalos, one for each type of particle type + std::vector > *> halos; + + /// returns number of particles of each type + std::vector getnp(){return nparticles;} + + /// returns mass of particles of each type, if 0 they can have different masses + std::vector getmp(){return masses;} + + /// returns original redshift of snapshot, redshifts of the halos can be changed + double sim_redshift(){return z_original;} + + MakeParticleLenses(const std::string &filename /// path / root name of gadget-2 snapshot + ,SimFileFormat format + ,int Nsmooth /// number of nearest neighbors used for smoothing + ,bool recenter /// recenter so that the LenHalos are centered on the center of mass + ); + + MakeParticleLenses(const std::string &filename /// path / name of galmb file + ,bool recenter /// recenter so that the LenHalos are centered on the center of mass + ); + + ~MakeParticleLenses(){ + for(auto p : halos) delete p; + } + + void CreateHalos(const COSMOLOGY &cosmo,double redshift); + +private: + + std::vector > data; + const std::string filename; + int Nsmooth; + + double z_original; + std::vector nparticles; + std::vector masses; + Point_3d cm; + + // write a glamB format file with all required particle data + static void writeSizesB(const std::string &filename + ,std::vector > &pv + ,int Nsmooth,std::vector numbytype,double redshift){ + + assert(numbytype.size() == 6); + size_t ntot = pv.size(); + std::ofstream myfile(filename, std::ios::out | std::ios::binary); + if(!myfile.write((char*)&Nsmooth,sizeof(int))){ + std::cerr << "Unable to write to file " << filename << std::endl; + throw std::runtime_error("file writing error"); + }else{ + myfile.write((char*)&ntot,sizeof(size_t)); + myfile.write((char*)(numbytype.data()),sizeof(size_t)*6); + myfile.write((char*)&redshift,sizeof(double)); + myfile.write((char*)(pv.data()),sizeof(ParticleType)*ntot); + } + } + + // read a glamB format file + static bool readSizesB(const std::string &filename + ,std::vector > &pv + ,int &Nsmooth,std::vector &numbytype,double &redshift){ + + numbytype.resize(6); + size_t ntot = pv.size(); + std::ifstream myfile(filename, std::ios::in | std::ios::binary); + if(!myfile.read((char*)&Nsmooth,sizeof(int))){ + return false; + }else{ + myfile.read((char*)&ntot,sizeof(size_t)); + myfile.read((char*)(numbytype.data()),sizeof(size_t)*6); + myfile.read((char*)&redshift,sizeof(double)); + pv.resize(ntot); + myfile.read((char*)(pv.data()),sizeof(ParticleType)*ntot); + } + return true; + } + + // Read particle from Gadget-2 format file + bool readGadget2(); + + // Reads particles from first 4 columns of csv file + bool readCSV(); + }; diff --git a/include/particle_types.h b/include/particle_types.h index 3d31275b..bc8962c6 100644 --- a/include/particle_types.h +++ b/include/particle_types.h @@ -8,6 +8,8 @@ #ifndef particle_types_h #define particle_types_h +enum SimFileFormat {glamb,csv,gadget2,ascii}; + // Atomic data class for simulation particles with individual sizes and masses template struct ParticleType{ @@ -23,6 +25,23 @@ struct ParticleType{ float mass(){return Mass;} }; +// Atomic data class for simulation particles with individual sizes, masses and velocities +template +struct ParticleTypeV{ + T &operator[](int i){return x[i];} + T *operator*(){return x;} + T x[3]; + + T v[3]; + + float Mass; + float Size; + int type; + + float size(){return Size;} + float mass(){return Mass;} +}; + /// Atomic data class for simulation particles of the same size and mass struct ParticleTypeSimple{ float &operator[](int i){return x[i];} @@ -35,8 +54,8 @@ struct ParticleTypeSimple{ float size(){return ParticleTypeSimple::Size;} float mass(){return ParticleTypeSimple::Mass;} }; -float ParticleTypeSimple::Size = 0; -float ParticleTypeSimple::Mass = 0; +//float ParticleTypeSimple::Size = 0; +//float ParticleTypeSimple::Mass = 0; /// Atomic data class for stars with different masses struct StarType{ diff --git a/include/simpleTree.h b/include/simpleTree.h index 700e30a7..c1f5e1b1 100644 --- a/include/simpleTree.h +++ b/include/simpleTree.h @@ -12,11 +12,19 @@ #include "Tree.h" //#include "lens_halos.h" -/** \brief Box representing a branch in a tree. It has four children. Used in TreeNBStruct which is used in TreeForce. +/** \brief Box representing a branch in a tree. It has four children. + Used in TreeNBStruct which is used in TreeForce. */ struct BranchNB{ - BranchNB(int Ndim); - ~BranchNB(); + + BranchNB(int Ndim){ + center = new PosType[Ndim*3]; + boundary_p1 = ¢er[Ndim]; + boundary_p2 = ¢er[2*Ndim]; + } + ~BranchNB(){ + delete center; + } /// array of particles in BranchNB IndexType *particles; @@ -53,16 +61,16 @@ struct BranchNB{ /* force calculation */ PosType rcrit_part; //PosType cm[2]; /* projected center of mass */ - }; /** \brief - * TreeNB: Tree structure used for force calculation with particles (i.e. stars, Nbody or halos). + * TreeNBStruct: Tree structure used for force calculation with particles (i.e. stars, Nbody or halos). * * The tree also contains pointers to the list of positions, sizes and masses of the particles. * Also flags for the number of dimensions the tree is defined in (2 or 3), and if multiple * masses and sizes should be used. */ +template struct TreeNBStruct{ BranchNB *top; BranchNB *current; @@ -71,17 +79,17 @@ struct TreeNBStruct{ /// Dimension of tree, 2 or 3. This will dictate how the force is calculated. short Ndimensions; /// Array of particle positions - PosType **xp; + PType *pp; }; -typedef struct TreeNBStruct * TreeNBHndl; -typedef int TreeNBElement; +//typedef struct TreeNBStruct * TreeNBHndl; /** * \brief A C++ class wrapper for the bianary treeNB used in the Nobody * force calculation, but also useful for general purpose searches. * - * Most of the code in TreeNB.c and TreeDriverNB.c is duplicated here as private methods and a few public ones. + * Most of the code in TreeNB.c and TreeDriverNB.c is duplicated here as private + * methods and a few public ones. */ template class TreeSimple { @@ -89,13 +97,18 @@ class TreeSimple { TreeSimple(PType *xp,IndexType Npoints,int bucket = 5,int dimensions = 2,bool median = true); virtual ~TreeSimple(); - /// \brief Finds the points within a circle around center and puts their index numbers in a list - void PointsWithinCircle(PosType center[2],float radius,std::list &neighborkist); - /// \brief Finds the points within an ellipse around center and puts their index numbers in a list - void PointsWithinEllipse(PosType center[2],float a_max,float a_min,float posangle,std::list &neighborkist); - /// \brief Finds the nearest N neighbors and puts their index numbers in an array, also returns the distance to the Nth neighbor for calculating smoothing - void NearestNeighbors(PosType *ray,int Nneighbors,float *rsph,IndexType *neighbors) const; - + /// Finds the points within a circle around center and puts their index numbers in a list + template + void PointsWithinCircle(T center[2],float radius,std::list &neighborkist); + + /// Finds the points within an ellipse around center and puts their index numbers in a list + template + void PointsWithinEllipse(T center[2],float a_max,float a_min,float posangle,std::list &neighborkist); + + /// Finds the nearest N neighbors and puts their index numbers in an array, also returns the distance to the Nth neighbor for calculating smoothing + template + void NearestNeighbors(T *ray,int Nneighbors,float *rsph,IndexType *neighbors) const; + class iterator{ public: @@ -163,7 +176,7 @@ class TreeSimple { int Ndim;//,incell2; int incell; - TreeNBHndl tree; + TreeNBStruct* tree; IndexType *index; IndexType Nparticles; bool median_cut; @@ -171,57 +184,1070 @@ class TreeSimple { PosType realray[3]; PType *xxp; - TreeNBHndl BuildTreeNB(PType *xxp,IndexType Nparticles,IndexType *particles,int Ndimensions,PosType theta); - void _BuildTreeNB(TreeNBHndl tree,IndexType nparticles,IndexType *particles); + TreeNBStruct* BuildTreeNB(PType *xxp,IndexType Nparticles,IndexType *particles,int Ndimensions,PosType theta); + void _BuildTreeNB(TreeNBStruct* tree,IndexType nparticles,IndexType *particles); - void _PointsWithin(PosType *ray,float *rmax,std::list &neighborkist); - void _NearestNeighbors(PosType *ray,int Nneighbors,unsigned long *neighbors,PosType *rneighbors); + template + void _PointsWithin(T *ray,float *rmax,std::list &neighborkist); + + template + void _NearestNeighbors(T *ray,int Nneighbors,unsigned long *neighbors,PosType *rneighbors); - void _NearestNeighbors(PosType *ray,PosType *realray,int Nneighbors,unsigned long *neighbors,PosType *rneighbors + template + void _NearestNeighbors(T *ray,PosType *realray,int Nneighbors,unsigned long *neighbors,PosType *rneighbors ,TreeSimple::iterator &it,int &found) const ; BranchNB *NewBranchNB(IndexType *particles,IndexType nparticles ,PosType boundary_p1[],PosType boundary_p2[] ,PosType center[],int level,unsigned long branchNBnumber); void FreeBranchNB(BranchNB *branchNB); - TreeNBHndl NewTreeNB(IndexType *particles,IndexType nparticles + TreeNBStruct * NewTreeNB(IndexType *particles,IndexType nparticles ,PosType boundary_p1[],PosType boundary_p2[], PosType center[],short Ndimensions); - void freeTreeNB(TreeNBHndl tree); - short emptyTreeNB(TreeNBHndl tree); - void _freeTreeNB(TreeNBHndl tree,short child); - bool isEmptyNB(TreeNBHndl tree); - bool atTopNB(TreeNBHndl tree); - bool noChildNB(TreeNBHndl tree); - bool offEndNB(TreeNBHndl tree); - void getCurrentNB(TreeNBHndl tree,IndexType *particles,IndexType *nparticles); - unsigned long getNbranchesNB(TreeNBHndl tree); - void moveTopNB(TreeNBHndl tree); - void moveUpNB(TreeNBHndl tree); - void moveToChildNB(TreeNBHndl tree,int child); - void insertChildToCurrentNB(TreeNBHndl tree, IndexType *particles,IndexType nparticles + void freeTreeNB(TreeNBStruct * tree); + short emptyTreeNB(TreeNBStruct * tree); + void _freeTreeNB(TreeNBStruct * tree,short child); + bool isEmptyNB(TreeNBStruct * tree); + bool atTopNB(TreeNBStruct * tree); + bool noChildNB(TreeNBStruct * tree); + bool offEndNB(TreeNBStruct * tree); + void getCurrentNB(TreeNBStruct * tree,IndexType *particles,IndexType *nparticles); + unsigned long getNbranchesNB(TreeNBStruct * tree); + void moveTopNB(TreeNBStruct * tree); + void moveUpNB(TreeNBStruct * tree); + void moveToChildNB(TreeNBStruct * tree,int child); + void insertChildToCurrentNB(TreeNBStruct * tree, IndexType *particles,IndexType nparticles ,PosType boundary_p1[],PosType boundary_p2[] ,PosType center[],int child); - void attachChildToCurrentNB(TreeNBHndl tree,BranchNB &data,int child); - bool TreeNBWalkStep(TreeNBHndl tree,bool allowDescent); + void attachChildToCurrentNB(TreeNBStruct * tree,BranchNB &data,int child); + bool TreeNBWalkStep(TreeNBStruct * tree,bool allowDescent); inline bool atLeaf(){ return (tree->current->child1 == NULL)*(tree->current->child2 == NULL); } - inline bool inbox(const PosType* center,PosType *p1,PosType *p2) const{ - return (center[0]>=p1[0])*(center[0]<=p2[0])*(center[1]>=p1[1])*(center[1]<=p2[1]); - } - - /*TreeNBHndl rotate_simulation(PosType **xp,IndexType Nparticles,IndexType *particles + template + inline bool inbox(const T* center,PosType *p1,PosType *p2) const{ + return (center[0]>=p1[0])*(center[0]<=p2[0])*(center[1]>=p1[1])*(center[1]<=p2[1]); + } + + /*TreeNBStruct * rotate_simulation(PosType **xp,IndexType Nparticles,IndexType *particles ,PosType **coord,PosType theta,float *rsph,float *mass ,bool MultiRadius,bool MultiMass); - TreeNBHndl rotate_project(PosType **xp,IndexType Nparticles,IndexType *particles + TreeNBStruct * rotate_project(PosType **xp,IndexType Nparticles,IndexType *particles ,PosType **coord,PosType theta,float *rsph,float *mass ,bool MultiRadius,bool MultiMass); - TreeNBHndl spread_particles(PosType **xp,IndexType Nparticles,IndexType *particles + TreeNBStruct * spread_particles(PosType **xp,IndexType Nparticles,IndexType *particles ,PosType theta,float *rsph,float *mass,bool MultiRadius,bool MultiMass); - void cuttoffscale(TreeNBHndl tree,PosType *theta);*/ + void cuttoffscale(TreeNBStruct * tree,PosType *theta);*/ }; +template +TreeSimple::TreeSimple(PType *xpt,IndexType Npoints,int bucket,int Ndimensions,bool median){ + index = new IndexType[Npoints]; + IndexType ii; + + Nbucket = bucket; + Ndim = Ndimensions; + median_cut = median; + Nparticles = Npoints; + + xxp = xpt; + + for(ii=0;ii +TreeSimple::~TreeSimple() +{ + freeTreeNB(tree); + delete[] index; + return; +} + +template +template +void TreeSimple::PointsWithinEllipse( + T *ray /// center of ellipse + ,float rmax /// major axis + ,float rmin /// minor axis + ,float posangle /// position angle of major axis, smallest angle between the x-axis and the long axis + ,std::list &neighborlist /// output neighbor list, will be emptied if it contains anything on entry +){ + PosType x,y,cs,sn; + + + if(rmax < rmin){float tmp = rmax; rmax=rmin; rmin=tmp;} + + if(rmax <=0.0 || rmin <= 0.0){ neighborlist.clear(); return; } + + // find point within a circle circumscribes the ellipse + PointsWithinCircle(ray,rmax,neighborlist); + + cs = cos(posangle); + sn = sin(posangle); + // go through points within the circle and reject points outside the ellipse + for( std::list::iterator it = neighborlist.begin();it != neighborlist.end();){ + x = xxp[*it][0]*cs - xxp[*it][1]*sn; + y = xxp[*it][0]*sn + xxp[*it][1]*cs; + if( ( pow(x/rmax,2) + pow(y/rmin,2) ) > 1) it = neighborlist.erase(it); + else ++it; + } + return; +} + +template +template +void TreeSimple::PointsWithinCircle( + T *ray /// center of circle + ,float rmax /// radius of circle + ,std::list &neighborlist /// output neighbor list, will be emptied if it contains anything on entry +){ + + neighborlist.clear(); + + realray[0]=ray[0]; + realray[1]=ray[1]; + + //cout << "ray = " << ray[0] << " " << ray[1] << " rmax = " << rmax << endl; + moveTopNB(tree); + if( inbox(ray,tree->current->boundary_p1,tree->current->boundary_p2) == 0 ){ + + ray[0] = (ray[0] > tree->current->boundary_p1[0]) ? ray[0] : tree->current->boundary_p1[0]; + ray[0] = (ray[0] < tree->current->boundary_p2[0]) ? ray[0] : tree->current->boundary_p2[0]; + + ray[1] = (ray[1] > tree->current->boundary_p1[1]) ? ray[1] : tree->current->boundary_p1[1]; + ray[1] = (ray[1] < tree->current->boundary_p2[1]) ? ray[1] : tree->current->boundary_p2[1]; + + //cout << "ray = " << ray[0] << " " << ray[1] << endl; + //ray[0]=DMAX(ray[0],tree->current->boundary_p1[0]); + //ray[0]=DMIN(ray[0],tree->current->boundary_p2[0]); + + //ray[1]=DMAX(ray[1],tree->current->boundary_p1[1]); + //ray[1]=DMIN(ray[1],tree->current->boundary_p2[1]); + } + incell=1; + + _PointsWithin(ray,&rmax,neighborlist); + + return; +} +/** + * Used in PointsWithinKist() to walk tree.*/ +template +template +void TreeSimple::_PointsWithin(T *ray,float *rmax,std::list &neighborlist){ + + int j,incell2=1; + unsigned long i; + PosType radius; + short pass; + + if(incell){ // not found cell yet + + if( inbox(ray,tree->current->boundary_p1,tree->current->boundary_p2) ){ + //cout << " In box" << endl; + + // found the box small enough + if( Utilities::cutbox(ray,tree->current->boundary_p1,tree->current->boundary_p2,*rmax)==1 + || atLeaf() ){ + //cout << " Found cell" << endl; + + // whole box in circle or a leaf with ray in it + + incell=0; + + ray[0]=realray[0]; + ray[1]=realray[1]; + + if( atLeaf() ){ + // if leaf calculate the distance to all the points in cell + for(i=0;icurrent->nparticles;++i){ + for(j=0,radius=0.0;j<2;++j) radius+=pow(xxp[tree->current->particles[i]][j]-ray[j],2); + if( radius < *rmax**rmax ){ + neighborlist.push_back(tree->current->particles[i]); + //cout << "add point to list" << neighborlist.size() << endl; + //InsertAfterCurrentKist(neighborlist,tree->current->particles[i]); + } + } + }else{ // put all of points in box into getCurrentKist(imagelist) + for(i=0;icurrent->nparticles;++i){ + neighborlist.push_back(tree->current->particles[i]); + //InsertAfterCurrentKist(neighborlist,tree->current->particles[i]); + //cout << "add point to list" << neighborlist.size() << endl; + + } + } + + }else{ // keep going down the tree + + if(tree->current->child1 !=NULL){ + moveToChildNB(tree,1); + _PointsWithin(ray,rmax,neighborlist); + moveUpNB(tree); + + incell2=incell; + } + + if(tree->current->child2 !=NULL){ + moveToChildNB(tree,2); + _PointsWithin(ray,rmax,neighborlist); + moveUpNB(tree); + } + + // if ray found in second child go back to first to search for neighbors + if( (incell2==1) && (incell==0) ){ + if(tree->current->child1 !=NULL){ + moveToChildNB(tree,1); + _PointsWithin(ray,rmax,neighborlist); + moveUpNB(tree); + } + } + } + } // not in the box + + }else{ // found cell + + pass=Utilities::cutbox(ray,tree->current->boundary_p1,tree->current->boundary_p2,*rmax); + // does radius cut into the box + if( pass ){ + //cout << " Cell found searching other cells" << endl; + if( atLeaf() ){ /* leaf case */ + + for(i=0;icurrent->nparticles;++i){ + for(j=0,radius=0.0;j<2;++j) radius+=pow(xxp[tree->current->particles[i]][j]-ray[j],2); + if( radius < *rmax**rmax ){ + neighborlist.push_back(tree->current->particles[i]); + //InsertAfterCurrentKist(neighborlist,tree->current->particles[i]); + //cout << "add point to list" << neighborlist.size() << endl; + + } + } + }else if(pass==1){ // whole box is inside radius + + for(i=0;icurrent->nparticles;++i){ + neighborlist.push_back(tree->current->particles[i]); + //InsertAfterCurrentKist(neighborlist,tree->current->particles[i]); + //cout << "add point to list" << neighborlist.size() << endl; + + } + }else{ + if(tree->current->child1 !=NULL){ + moveToChildNB(tree,1); + _PointsWithin(ray,rmax,neighborlist); + moveUpNB(tree); + } + + if(tree->current->child2 !=NULL){ + moveToChildNB(tree,2); + _PointsWithin(ray,rmax,neighborlist); + moveUpNB(tree); + } + } + + } + } + + return; +} + +/** + * \brief finds the nearest neighbors in whatever dimensions tree is defined in + * */ +template +template +void TreeSimple::NearestNeighbors( + T *ray /// position + ,int Nneighbors /// number of neighbors to be found + ,float *radius /// distance furthest neighbor found from ray[] + ,IndexType *neighborsout /// list of the indexes of the neighbors +) const{ + IndexType i; + //static int count=0,oldNneighbors=-1; + short j; + + //Ndim = tree->Ndimensions; + + PosType rneighbors[Nneighbors+Nbucket]; + IndexType neighbors[Nneighbors+Nbucket]; + + if(tree->top->nparticles <= Nneighbors){ + ERROR_MESSAGE(); + printf("ERROR: in TreeSimple::NearestNeighbors, number of neighbors > total number of particles\n"); + exit(1); + } + + /* initalize distance to neighbors to a large number */ + for(i=0;itop->boundary_p2[0]-tree->top->boundary_p1[0])); + neighbors[i] = 0; + } + + PosType rlray[tree->Ndimensions]; + for(j=0;jNdimensions;++j) rlray[j]=ray[j]; + + //moveTopNB(tree); + + TreeSimple::iterator iter(tree->top); + if( inbox(ray,tree->current->boundary_p1,tree->current->boundary_p2) == 0 ){ + for(j=0;jNdimensions;++j){ + ray[j] = (ray[j] > tree->current->boundary_p1[j]) ? ray[j] : tree->current->boundary_p1[j]; + ray[j] = (ray[j] < tree->current->boundary_p2[j]) ? ray[j] : tree->current->boundary_p2[j]; + } + } + int notfound = 1; + + _NearestNeighbors(ray,rlray,Nneighbors,neighbors,rneighbors,iter,notfound); + + for(i=0;i +template +void TreeSimple::_NearestNeighbors(T *ray,PosType *rlray,int Nneighbors + ,unsigned long *neighbors,PosType *rneighbors + ,TreeSimple::iterator &it,int ¬found) const { + + int incellNB2=1; + IndexType i; + short j; + + if(notfound){ /* not found cell yet */ + + if( inbox(ray,(*it)->boundary_p1,(*it)->boundary_p2) ){ + + /* found the box small enough */ + if( (*it)->nparticles <= Nneighbors+Nbucket ){ + notfound=0; + for(j=0;jNdimensions;++j) ray[j]=rlray[j]; + + /* calculate the distance to all the particles in cell */ + for(i=0;i<(*it)->nparticles;++i){ + for(j=0,rneighbors[i]=0.0;jNdimensions;++j){ + rneighbors[i] += pow(tree->pp[(*it)->particles[i]][j]-ray[j],2); + } + rneighbors[i]=sqrt( rneighbors[i] ); + assert(rneighbors[i] < 10); + neighbors[i]=(*it)->particles[i]; + } + + Utilities::quicksort(neighbors,rneighbors,(*it)->nparticles); + + + }else{ /* keep going down the tree */ + + if(it.down(1)){ + //moveToChildNB(tree,1); + _NearestNeighbors(ray,rlray,Nneighbors,neighbors,rneighbors,it,notfound); + /*printf("moving up from level %i\n",(*it)->level);*/ + //moveUpNB(tree); + it.up(); + incellNB2=notfound; + } + + if(it.down(2)){ + /*printf("moving to child2 from level %i\n",(*it)->level);*/ + //moveToChildNB(tree,2); + _NearestNeighbors(ray,rlray,Nneighbors,neighbors,rneighbors,it,notfound); + /*printf("moving up from level %i\n",(*it)->level);*/ + //moveUpNB(tree); + it.up(); + } + + /** if ray found in second child go back to first to search for neighbors **/ + if( (incellNB2==1) && (notfound==0) ){ + if(it.down(1)){ + //moveToChildNB(tree,1); + _NearestNeighbors(ray,rlray,Nneighbors,neighbors,rneighbors,it,notfound); + //moveUpNB(tree); + it.up(); + } + } + } + } + }else{ // found cell + /* does radius cut into the box */ + if( Utilities::cutbox(ray,(*it)->boundary_p1,(*it)->boundary_p2,rneighbors[Nneighbors-1]) ){ + + if( ((*it)->child1 == NULL)*((*it)->child2 == NULL)){ /* leaf case */ + + /* combine found neighbors with particles in box and resort */ + for(i=Nneighbors;i<((*it)->nparticles+Nneighbors);++i){ + for(j=0,rneighbors[i]=0.0;jNdimensions;++j){ + rneighbors[i]+=pow(tree->pp[(*it)->particles[i-Nneighbors]][j]-ray[j],2); + } + rneighbors[i]=sqrt( rneighbors[i] ); + assert(rneighbors[i] < 10); + neighbors[i]=(*it)->particles[i-Nneighbors]; + } + + Utilities::quicksort(neighbors,rneighbors,Nneighbors+Nbucket); + + }else{ + + if(it.down(1)){ + //moveToChildNB(tree,1); + _NearestNeighbors(ray,rlray,Nneighbors,neighbors,rneighbors,it,notfound); + //moveUpNB(tree); + it.up(); + } + + if(it.down(2)){ + //moveToChildNB(tree,2); + _NearestNeighbors(ray,rlray,Nneighbors,neighbors,rneighbors,it,notfound); + //moveUpNB(tree); + it.up(); + } + } + } + } + return; +} + +template +template +void TreeSimple::_NearestNeighbors(T *ray,int Nneighbors,unsigned long *neighbors,PosType *rneighbors){ + + int incellNB2=1; + IndexType i; + short j; + + if(incell){ /* not found cell yet */ + + if( inbox(ray,tree->current->boundary_p1,tree->current->boundary_p2) ){ + + /* found the box small enough */ + if( tree->current->nparticles <= Nneighbors+Nbucket ){ + incell=0; + for(j=0;jNdimensions;++j) ray[j]=realray[j]; + + /* calculate the distance to all the particles in cell */ + for(i=0;icurrent->nparticles;++i){ + for(j=0,rneighbors[i]=0.0;jNdimensions;++j){ + rneighbors[i] += pow(tree->xp[tree->current->particles[i]][j]-ray[j],2); + } + rneighbors[i]=sqrt( rneighbors[i] ); + assert(rneighbors[i] < 10); + neighbors[i]=tree->current->particles[i]; + } + + Utilities::quicksort(neighbors,rneighbors,tree->current->nparticles); + + }else{ /* keep going down the tree */ + + if(tree->current->child1 !=NULL){ + moveToChildNB(tree,1); + _NearestNeighbors(ray,Nneighbors,neighbors,rneighbors); + /*printf("moving up from level %i\n",tree->current->level);*/ + moveUpNB(tree); + + incellNB2=incell; + } + + if(tree->current->child2 !=NULL){ + /*printf("moving to child2 from level %i\n",tree->current->level);*/ + moveToChildNB(tree,2); + _NearestNeighbors(ray,Nneighbors,neighbors,rneighbors); + /*printf("moving up from level %i\n",tree->current->level);*/ + moveUpNB(tree); + } + + /** if ray found in second child go back to first to search for neighbors **/ + if( (incellNB2==1) && (incell==0) ){ + if(tree->current->child1 !=NULL){ + moveToChildNB(tree,1); + _NearestNeighbors(ray,Nneighbors,neighbors,rneighbors); + moveUpNB(tree); + } + } + } + } + }else{ // found cell + /* does radius cut into the box */ + if( Utilities::cutbox(ray,tree->current->boundary_p1,tree->current->boundary_p2,rneighbors[Nneighbors-1]) ){ + + if( (tree->current->child1 == NULL)*(tree->current->child2 == NULL)){ /* leaf case */ + + /* combine found neighbors with particles in box and resort */ + for(i=Nneighbors;i<(tree->current->nparticles+Nneighbors);++i){ + for(j=0,rneighbors[i]=0.0;jNdimensions;++j){ + rneighbors[i]+=pow(tree->xp[tree->current->particles[i-Nneighbors]][j]-ray[j],2); + } + rneighbors[i]=sqrt( rneighbors[i] ); + assert(rneighbors[i] < 10); + neighbors[i]=tree->current->particles[i-Nneighbors]; + } + + Utilities::quicksort(neighbors,rneighbors,Nneighbors+Nbucket); + + }else{ + + if(tree->current->child1 !=NULL){ + moveToChildNB(tree,1); + _NearestNeighbors(ray,Nneighbors,neighbors,rneighbors); + moveUpNB(tree); + } + + if(tree->current->child2 !=NULL){ + moveToChildNB(tree,2); + _NearestNeighbors(ray,Nneighbors,neighbors,rneighbors); + moveUpNB(tree); + } + } + } + } + return; +} + +template +TreeNBStruct * TreeSimple::BuildTreeNB(PType *xp,IndexType Nparticles,IndexType *particles,int Ndims + ,PosType theta){ + TreeNBStruct * tree; + IndexType i; + short j; + PosType p1[3],p2[3],center[3]; + + assert(Ndim == Ndims); + + for(j=0;j p2[j] ) p2[j]=xp[i][j]; + } + } + + for(j=0;jpp = xp; + + /* build the tree */ + _BuildTreeNB(tree,Nparticles,particles); + + /* visit every branch to find center of mass and cutoff scale */ + moveTopNB(tree); + + return tree; +} + + +// tree must be created and first branch must be set before start +template +void TreeSimple::_BuildTreeNB(TreeNBStruct * tree,IndexType nparticles,IndexType *particles){ + IndexType i,cut,dimension; + short j; + BranchNB *cbranch,branch1(Ndim),branch2(Ndim); + PosType xcut; + PosType *x; + + cbranch=tree->current; /* pointer to current branch */ + + cbranch->center[0] = (cbranch->boundary_p1[0] + cbranch->boundary_p2[0])/2; + cbranch->center[1] = (cbranch->boundary_p1[1] + cbranch->boundary_p2[1])/2; + cbranch->quad[0]=cbranch->quad[1]=cbranch->quad[2]=0; + + /* leaf case */ + if(cbranch->nparticles <= Nbucket){ + cbranch->big_particle=0; + return; + } + + /* initialize boundaries to old boundaries */ + for(j=0;jboundary_p1[j]; + branch1.boundary_p2[j]=cbranch->boundary_p2[j]; + + branch2.boundary_p1[j]=cbranch->boundary_p1[j]; + branch2.boundary_p2[j]=cbranch->boundary_p2[j]; + } + cbranch->big_particle=0; + + // **** makes sure force does not require nbucket at leaf + + /* set dimension to cut box */ + dimension=(cbranch->level % Ndim); + + x=(PosType *)malloc((cbranch->nparticles-cbranch->big_particle)*sizeof(PosType)); + for(i=cbranch->big_particle;inparticles;++i) x[i]=tree->pp[particles[i]][dimension]; + + if(median_cut){ + Utilities::quicksort(particles,x,cbranch->nparticles-cbranch->big_particle); + + cut=(cbranch->nparticles-cbranch->big_particle)/2; + branch1.boundary_p2[dimension]=x[cut]; + branch2.boundary_p1[dimension]=x[cut]; + }else{ + xcut=(cbranch->boundary_p1[dimension]+cbranch->boundary_p2[dimension])/2; + branch1.boundary_p2[dimension]=xcut; + branch2.boundary_p1[dimension]=xcut; + + Utilities::quickPartition(xcut,&cut,particles + ,x,cbranch->nparticles-cbranch->big_particle); + } + + /* set particle numbers and pointers to particles */ + branch1.prev=cbranch; + branch1.nparticles=cut; + branch1.particles=particles+cbranch->big_particle; + + branch2.prev=cbranch; + branch2.nparticles=cbranch->nparticles-cbranch->big_particle - cut; + if(cut < (cbranch->nparticles-cbranch->big_particle) ) + branch2.particles=&particles[cut+cbranch->big_particle]; + else branch2.particles=NULL; + + free(x); + + if(branch1.nparticles > 0) attachChildToCurrentNB(tree,branch1,1); + if(branch2.nparticles > 0) attachChildToCurrentNB(tree,branch2,2); + + // work out brothers for children + if( (cbranch->child1 != NULL) && (cbranch->child2 != NULL) ){ + cbranch->child1->brother = cbranch->child2; + cbranch->child2->brother = cbranch->brother; + } + if( (cbranch->child1 == NULL) && (cbranch->child2 != NULL) ) + cbranch->child2->brother = cbranch->brother; + if( (cbranch->child1 != NULL) && (cbranch->child2 == NULL) ) + cbranch->child1->brother = cbranch->brother; + + + if( branch1.nparticles > 0 ){ + moveToChildNB(tree,1); + _BuildTreeNB(tree,branch1.nparticles,branch1.particles); + moveUpNB(tree); + } + + if(branch2.nparticles > 0 ){ + moveToChildNB(tree,2); + _BuildTreeNB(tree,branch2.nparticles,branch2.particles); + moveUpNB(tree); + } + + return; +} + +/** + * visits every branch in tree to calculate + * two critical lengths rcrit_angle and rcrit_part + * and mark largest particle in each node subject + * to some conditions + * + * if the sph[] are negative rcrit_part = 0 + */ + +/***** Constructors/Destructors *****/ + +/************************************************************************ + * NewBranchNB + * Returns pointer to new BranchNB struct. Initializes children pointers to NULL, + * and sets data field to input. Private. + ************************************************************************/ +template +BranchNB *TreeSimple::NewBranchNB(IndexType *particles,IndexType nparticles + ,PosType boundary_p1[],PosType boundary_p2[] + ,PosType center[],int level,unsigned long branchNBnumber){ + + BranchNB *branchNB = new BranchNB(Ndim); + int i; + + if (!branchNB){ + ERROR_MESSAGE(); fprintf(stderr,"allocation failure in NewBranchNB()\n"); + exit(1); + } + branchNB->particles = particles; + branchNB->nparticles = nparticles; + + for(i=0;icenter[i]=center[i]; + branchNB->level=level; + + for(i=0;iboundary_p1[i]= boundary_p1[i]; + branchNB->boundary_p2[i]= boundary_p2[i]; + } + + branchNB->number=branchNBnumber; + + branchNB->child1 = NULL; + branchNB->child2 = NULL; + branchNB->prev = NULL; + branchNB->brother = NULL; + + return(branchNB); +} + +/************************************************************************ + * FreeBranchNB + * Frees memory pointed to by branchNB. Private. + ************************************************************************/ +template +void TreeSimple::FreeBranchNB(BranchNB *branchNB){ + + assert( branchNB != NULL); + delete branchNB; + + return; +} + +/************************************************************************ + * NewTreeNB + * Returns pointer to new TreeNB struct. Initializes top, last, and + * current pointers to NULL. Sets NbranchNBes field to 0. Exported. + ************************************************************************/ +template +TreeNBStruct * TreeSimple::NewTreeNB(IndexType *particles,IndexType nparticles + ,PosType boundary_p1[],PosType boundary_p2[], + PosType center[],short Ndimensions){ + + TreeNBStruct * tree; + + tree = new TreeNBStruct;// *)malloc(sizeof(TreeNBStruct)); + if (!tree){ + ERROR_MESSAGE(); fprintf(stderr,"allocation failure in NewTreeNB()\n"); + exit(1); + } + + tree->top= NewBranchNB(particles,nparticles,boundary_p1,boundary_p2,center,0,0); + if (!(tree->top)){ + ERROR_MESSAGE(); fprintf(stderr,"allocation failure in NewTreeNB()\n"); + exit(1); + } + + tree->Nbranches = 1; + tree->current = tree->top; + tree->Ndimensions=Ndimensions; + + return(tree); +} + +template +void TreeSimple::freeTreeNB(TreeNBStruct * tree){ + /* free treeNB + * does not free the particle positions, masses or rsph + */ + + if(tree == NULL) return; + + emptyTreeNB(tree); + FreeBranchNB(tree->top); + delete tree; + + return; +} + +template +short TreeSimple::emptyTreeNB(TreeNBStruct * tree){ + + moveTopNB(tree); + _freeTreeNB(tree,0); + + assert(tree->Nbranches == 1); + + return 1; +} + +template +void TreeSimple::_freeTreeNB(TreeNBStruct * tree,short child){ + BranchNB *branch; + + assert( tree ); + assert( tree->current); + + if(tree->current->child1 != NULL){ + moveToChildNB(tree,1); + _freeTreeNB(tree,1); + } + + if(tree->current->child2 != NULL){ + moveToChildNB(tree,2); + _freeTreeNB(tree,2); + } + + if( (tree->current->child1 == NULL)*(tree->current->child2 == NULL) ){ + + if(atTopNB(tree)) return; + + branch = tree->current; + moveUpNB(tree); + FreeBranchNB(branch); + + /*printf("*** removing branch %i number of branches %i\n",branch->number + ,tree->Nbranches-1);*/ + + if(child==1) tree->current->child1 = NULL; + if(child==2) tree->current->child2 = NULL; + + --tree->Nbranches; + + return; + } + + return; +} + +/************************************************************************ + * isEmptyNB + * Returns "true" if the TreeNB is empty and "false" otherwise. Exported. + ************************************************************************/ +template +bool TreeSimple::isEmptyNB(TreeNBStruct * tree){ + + assert(tree != NULL); + return(tree->Nbranches == 0); +} + +/************************************************************************ + * atTop + * Returns "true" if current is the same as top and "false" otherwise. + * Exported. + * Pre: !isEmptyNB(tree) + ************************************************************************/ +template +bool TreeSimple::atTopNB(TreeNBStruct * tree){ + + assert(tree != NULL); + if( isEmptyNB(tree) ){ + ERROR_MESSAGE(); + fprintf(stderr, "TreeNB Error: calling atTop() on empty tree\n"); + exit(1); + } + return(tree->current == tree->top); +} + +/************************************************************************ + * noChild + * Returns "true" if the child of the current branchNB does not exist and "false" otherwise. + * Exported. + * Pre: !isEmptyNB(tree) + ************************************************************************/ +template +bool TreeSimple::noChildNB(TreeNBStruct * tree){ + + assert(tree != NULL); + if( isEmptyNB(tree) ){ + + ERROR_MESSAGE(); fprintf(stderr, "TreeNB Error: calling atTop() on empty tree\n"); + exit(1); + } + + if( (tree->current->child1 == NULL) || (tree->current->child2 == NULL) ) return true; + return false; +} + +/************************************************************************ + * offEndNB + * Returns "true" if current is off end and "false" otherwise. Exported. + ************************************************************************/ +template +bool TreeSimple::offEndNB(TreeNBStruct * tree){ + + assert(tree != NULL); + return(tree->current == NULL); +} + +/************************************************************************ + * getCurrentNB + * Returns the particuls of current. Exported. + * Pre: !offEndNB(tree) + ************************************************************************/ +template +void TreeSimple::getCurrentNB(TreeNBStruct * tree,IndexType *particles,IndexType *nparticles){ + + assert(tree != NULL); + if( offEndNB(tree) ){ + + ERROR_MESSAGE(); fprintf(stderr, "TreeNB Error: calling getCurrent() when current is off end\n"); + exit(1); + } + + *nparticles=tree->current->nparticles; + particles=tree->current->particles; + + return; +} + +/************************************************************************ + * getNbranchesNB + * Returns the NbranchNBes of tree. Exported. + ************************************************************************/ +template +unsigned long TreeSimple::getNbranchesNB(TreeNBStruct * tree){ + + assert(tree != NULL); + return(tree->Nbranches); +} + +/***** Manipulation procedures *****/ + +/************************************************************************ + * moveTopNB + * Moves current to the front of tree. Exported. + * Pre: !isEmptyNB(tree) + ************************************************************************/ +template +void TreeSimple::moveTopNB(TreeNBStruct * tree){ + //std::cout << tree << std::endl; + //std::cout << tree->current << std::endl; + //std::cout << tree->top << std::endl; + + assert(tree != NULL); + if( isEmptyNB(tree) ){ + + ERROR_MESSAGE(); fprintf(stderr, "TreeNB Error: calling moveTopNB() on empty tree\n"); + exit(1); + } + + + tree->current = tree->top; + + return; +} + +/************************************************************************ + * movePrev + * Moves current to the branchNB before it in tree. This can move current + * off end. Exported. + * Pre: !offEndNB(tree) + ************************************************************************/ +template +void TreeSimple::moveUpNB(TreeNBStruct * tree){ + + assert(tree != NULL); + if( offEndNB(tree) ){ + ERROR_MESSAGE(); fprintf(stderr, "TreeNB Error: call to moveUpNB() when current is off end\n"); + exit(1); + } + if( tree->current == tree->top ){ + ERROR_MESSAGE(); fprintf(stderr, "TreeNB Error: call to moveUpNB() tried to move off the top\n"); + exit(1); + } + + tree->current = tree->current->prev; /* can move off end */ + return; +} + +/************************************************************************ + * moveToChildNB + * Moves current to child branchNB after it in tree. This can move current off + * end. Exported. + * Pre: !offEndNB(tree) + ************************************************************************/ +template +void TreeSimple::moveToChildNB(TreeNBStruct * tree,int child){ + + assert(tree != NULL); + if( offEndNB(tree) ){ + + ERROR_MESSAGE(); fprintf(stderr, "TreeNB Error: calling moveChildren() when current is off end\n"); + exit(1); + } + if(child==1){ + if( tree->current->child1 == NULL ){ + ERROR_MESSAGE(); fprintf(stderr, "TreeNB Error: moveToChildNB() typing to move to child1 when it doesn't exist\n"); + exit(1); + } + tree->current = tree->current->child1; + } + if(child==2){ + if( tree->current->child2 == NULL ){ + ERROR_MESSAGE(); fprintf(stderr, "TreeNB Error: moveToChildNB() typing to move to child2 when it doesn't exist\n"); + exit(1); + } + tree->current = tree->current->child2; + } + return; +} + +/************************************************************************ + * insertAfterCurrent + * Inserts a new BranchNB after the current branchNB in the tree and sets the + * data field of the new BranchNB to input. Exported. + * Pre: !offEndNB(tree) + ************************************************************************/ +template +void TreeSimple::insertChildToCurrentNB(TreeNBStruct * tree, IndexType *particles,IndexType nparticles + ,PosType boundary_p1[],PosType boundary_p2[] + ,PosType center[],int child){ + + BranchNB *branchNB; + + /*printf("attaching child%i current paricle number %i\n",child,tree->current->nparticles);*/ + + branchNB = NewBranchNB(particles,nparticles,boundary_p1,boundary_p2,center + ,tree->current->level+1,tree->Nbranches); + + assert(tree != NULL); + + if( offEndNB(tree) ){ + + ERROR_MESSAGE(); fprintf(stderr, "TreeNB Error: calling insertChildToCurrentNB() when current is off end\n"); + exit(1); + } + + branchNB->prev = tree->current; + + if(child==1){ + if(tree->current->child1 != NULL){ + ERROR_MESSAGE(); fprintf(stderr, "TreeNB Error: calling insertChildToCurrentNB() when child1 alread exists\n"); + exit(1); + } + tree->current->child1 = branchNB; + } + if(child==2){ + if(tree->current->child2 != NULL){ + ERROR_MESSAGE(); + fprintf(stderr, "TreeNB Error: calling insertChildToCurrentNB() when child2 alread exists\n current level=%i Nbranches=%li\n" + ,tree->current->level,tree->Nbranches); + exit(1); + } + tree->current->child2 = branchNB; + } + + tree->Nbranches++; + + return; +} + +/* same as above but takes a branchNB structure */ +template +void TreeSimple::attachChildToCurrentNB(TreeNBStruct * tree,BranchNB &data,int child){ + + insertChildToCurrentNB(tree,data.particles,data.nparticles + ,data.boundary_p1,data.boundary_p2,data.center,child); + return; +} + +// step for walking tree by iteration instead of recursion +template +bool TreeSimple::TreeNBWalkStep(TreeNBStruct * tree,bool allowDescent){ + if(allowDescent && tree->current->child1 != NULL){ + moveToChildNB(tree,1); + return true; + } + if(allowDescent && tree->current->child2 != NULL){ + moveToChildNB(tree,2); + return true; + } + + if(tree->current->brother != NULL){ + tree->current=tree->current->brother; + return true; + } + return false; +} + #endif /* SIMP_TREE_H_ */ diff --git a/include/utilities_slsim.h b/include/utilities_slsim.h index 9e7747b0..159aefb8 100644 --- a/include/utilities_slsim.h +++ b/include/utilities_slsim.h @@ -1204,7 +1204,7 @@ namespace Utilities void read2columnfile( std::string filename /// input file name ,std::vector &x /// vector that will contain the first column - ,std::vector &y /// vector that will contain the first column + ,std::vector &y /// vector that will contain the second column ,std::string delineator = " " /// specific string the seporates columns, ex. ",", "|", etc. ,int skiplines = 0 ,bool verbose = false @@ -1288,8 +1288,8 @@ namespace Utilities void read3columnfile( std::string filename /// input file name ,std::vector &x /// vector that will contain the first column - ,std::vector &y /// vector that will contain the first column - ,std::vector &z /// vector that will contain the first column + ,std::vector &y /// vector that will contain the second column + ,std::vector &z /// vector that will contain the third column ,std::string delineator = " " /// specific string the seporates columns, ex. ",", "|", etc. ,bool verbose = false @@ -1404,7 +1404,7 @@ namespace Utilities /** \brief This function will read in all the numbers from a multi-column - ASCII data file. + ,space seporated ASCII data file. It will skip the comment lines if they are at the head of the file. The number of columns and rows are returned. The entry at row r and column c will be stored at data[c + column*r]. @@ -1541,7 +1541,7 @@ namespace Utilities number of columns and rows are returned. Comments must only be before the data. There must be a line with the - column lines after the comments and before the data. + column names after the comments and before the data. This function is not particularly fast for large amounts of data. If the number of rows is large it would be best to use data.reserve() to set the capacity of data large enough that no rellocation of memory occurs. @@ -1700,5 +1700,7 @@ namespace Utilities std::string filename; }; + /// split string into vector of seporate strings that were seporated by + void splitstring(std::string &line,std::vector &vec,const std::string &delimiter); } #endif From 27802f9a88102e50b846f15a228d861eaad5bd9a Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Fri, 16 Nov 2018 09:18:49 +0100 Subject: [PATCH 073/227] new file: include/gadget.hh --- include/gadget.hh | 678 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 678 insertions(+) create mode 100644 include/gadget.hh diff --git a/include/gadget.hh b/include/gadget.hh new file mode 100644 index 00000000..2ccfbe88 --- /dev/null +++ b/include/gadget.hh @@ -0,0 +1,678 @@ +#ifndef GADGET_HPP +#define GADGET_HPP + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "particle_types.h" + +#define int4bytes int + +using namespace std; + +template +constexpr std::size_t size_of(T const&) { + return sizeof(T); +} +//class gadget: public gensim { +template +class GadgetFile { + /* this is a specialized version of gensim which deals with GADGET files */ +public: + + bool multipleFiles; + int numfiles; + size_t ntot=0; + double time,redshift,omega,lambda,hubble; + std::string filebasename; + + GadgetFile (string inpfn,std::vector &data); + ~GadgetFile(){}; + + void checkMultiple (); + void openFile (); + void closeFile (); + + void readBlock (const char *blockname); + + int find_block(FILE *fd,const char *label); + + int read_gadget_head(int *npart,double *massarr,double *time,double *redshift,double *omega, double *lambda, double *hubble, FILE *fd); + + bool FileExists(string strFilename); + + std::vector &p_data; + + int npart[6]; + double masstab[6]; + +private: + + struct pvector{ + float x,y,z; + }; + + ifstream::pos_type size; + int4bytes blksize, swap; + + //float *rho, *mass, *u, *den, *pot; + //int *ptype; + //pvector *pos,*b,*vel; + FILE *fd; + int filecnt; + int np_file_start, np_file_end; + + int read_gadget_float(float *data,const char *label,FILE *fd); + + int read_gadget_float3(float *data,const char *label,FILE *fd); + size_t my_fread(void *ptr, size_t size, size_t nmemb, FILE * stream); + void swap_Nbyte(char *data,int n,int m); +}; + +template +GadgetFile::GadgetFile(string inpfn,std::vector &data): +multipleFiles(false),numfiles(0) +,filebasename(inpfn),swap(0),filecnt(0),np_file_start(0),np_file_end(-1), +p_data(data) +{ + checkMultiple(); +} + +template +void GadgetFile::checkMultiple(){ + + char *filename = new char [filebasename.size()+1]; + int n; + filename[filebasename.size()]=0; + memcpy(filename,filebasename.c_str(),filebasename.size()); + + bool exists=FileExists(filename); + if (exists) {cout << "The file exists..." << endl; + + if(!(fd = fopen(filename,"r"))) + { + printf("Cant open file <%s> !\n",filename); + exit(2); + }else{ + cout << "Reading header" << endl; + + /*----------- READ HEADER TO GET GLOBAL PROPERTIES -------------*/ + n = read_gadget_head(npart,masstab,&time,&redshift,&omega,&lambda,&hubble,fd); + + ntot=0; + for(int i=0;i<6;i++) + { + printf("PartSpecies %d, anz=%d, masstab=%f\n",i,npart[i],masstab[i]); + ntot += npart[i]; + } + } + fclose(fd); + } else { + cout << "The file does not exist or the simulation is splitted in multiple files..." << endl; + cout << "Trying multiple files..." << endl; + int nmaxf=10; + + for (int i=0; i<=nmaxf-1; i++) { + + std::ostringstream filenum; + string odot="."; + filenum << i; + string filebasename_=filebasename+odot+filenum.str(); + char *filename_ = new char [filebasename.size()+1]; + filename_[filebasename_.size()]=0; + memcpy(filename_,filebasename_.c_str(),filebasename_.size()); + bool exists=FileExists(filename_); + if (exists) {numfiles++;} + + } + if (numfiles>0) { + multipleFiles=true; + cout << "The simulation is splitted into " << numfiles << " files" << endl; + ntot=0; + for (int j=0; j<=numfiles-1; j++) { + std::ostringstream filenum; + string odot="."; + filenum << j; + string filebasename_=filebasename+odot+filenum.str(); + char *filename_ = new char [filebasename.size()+1]; + filename_[filebasename_.size()]=0; + memcpy(filename_,filebasename_.c_str(),filebasename_.size()); + + if(!(fd = fopen(filename_,"r"))) + { + printf("Cant open file <%s> !\n",filename_); + exit(2); + } + else + { + cout << "-> Reading header# " << j << endl; + + /*----------- READ HEADER TO GET GLOBAL PROPERTIES -------------*/ + n = read_gadget_head(npart,masstab,&time,&redshift,&omega,&lambda,&hubble,fd); + + for(int i=0;i<6;i++) + { + printf("* PartSpecies %d, anz=%d, masstab=%f\n",i,npart[i],masstab[i]); + ntot += npart[i]; + } + } + } + fclose(fd); + } + } + cout << "TOTAL NUMBER OF PARTICLES: " << ntot << endl; + //now create a vector of particles + printf("Allocating memory for a total of %zu particles...\n",ntot); + + p_data.resize(ntot); + + // pv.reserve(ntot); + // for (int k=0; k +void GadgetFile::openFile () { + + int n; + int nstart; + + fd=0; + + if (numfiles==0) { + + char *filename = new char [filebasename.size()+1]; + filename[filebasename.size()]=0; + memcpy(filename,filebasename.c_str(),filebasename.size()); + fd = fopen(filename,"r"); + + /*----------- RED HEADER TO GET GLOBAL PROPERTIES -------------*/ + n = read_gadget_head(npart,masstab,&time,&redshift,&omega,&lambda,&hubble,fd); + + + // now setting the particle type + nstart=0; + np_file_start=nstart; + + for (int ips=0; ips<6; ips++){ + if (npart[ips]>0){ + for (int i=nstart; i0){ + for (int i=nstart; isetPtype(ips); + p_data[i].type = ips; + } + nstart=nstart+npart[ips]; + np_in_file=np_in_file+npart[ips]; + } + } + np_file_end=np_file_start+np_in_file-1; + filecnt++; + } + //cout << "PARTICLES FROM " << np_file_start << " TO " << np_file_end << endl; +} + +template +void GadgetFile::closeFile () { + fclose(fd); +} + +/*-----------------------------------------------------------------------------*/ +/*-----------------------------------------------------------------------------*/ +/*---------------------------- High Level Routines ----------------------------*/ +/*-----------------------------------------------------------------------------*/ +/*-----------------------------------------------------------------------------*/ + +/*-----------------------------------------------------------------------------*/ +/*---------------------- Routine to read the header information ---------------*/ +/*-------- int *npart: List of Particle numbers for spezies [0..5] ---------*/ +/*-------- int *massarr: List of masses for spezies [0..5] -------------------*/ +/*-------- int *time: Time of snapshot ------------------------------------*/ +/*-------- int *massarr: Redshift of snapshot --------------------------------*/ +/*-------- FILE *fd: File handle -----------------------------------------*/ +/*-------- returns number of read bytes ---------------------------------------*/ +/*-----------------------------------------------------------------------------*/ +template +int GadgetFile::read_gadget_head(int *npart,double *massarr,double *time,double *redshift, double *omega, double *lambda, double *hubble, FILE *fd) +{ + int blocksize,dummysize; + + int dummyint; + int dummyarr[6]; + double boxl; + + blocksize = find_block(fd,"HEAD"); + + + if(blocksize <= 0) + { + printf("Block <%s> not fond !\n","HEAD"); + exit(5); + } + else + { + dummysize=blocksize - 6 * sizeof(int) - 8 * sizeof(double); + std::cout << blocksize << " " << dummysize << std::endl; + SKIP; + my_fread(npart,6*sizeof(int), 1, fd); swap_Nbyte((char*)npart,6,4); + my_fread(massarr,6*sizeof(double), 1, fd); swap_Nbyte((char*)massarr,6,8); + my_fread(time,sizeof(double), 1, fd); swap_Nbyte((char*)time,1,8); + my_fread(redshift,sizeof(double), 1, fd); swap_Nbyte((char*)redshift,1,8); + my_fread(&dummyint,sizeof(int), 1, fd); swap_Nbyte((char*)&dummyint,1,4); + my_fread(&dummyint,sizeof(int), 1, fd); swap_Nbyte((char*)&dummyint,1,4); + my_fread(dummyarr,6*sizeof(int), 1, fd); swap_Nbyte((char*)dummyarr,6,4); + my_fread(&dummyint,sizeof(int), 1, fd); swap_Nbyte((char*)&dummyint,1,4); + my_fread(&dummyint,sizeof(int), 1, fd); swap_Nbyte((char*)&dummyint,1,4); + my_fread(&boxl,sizeof(double), 1, fd); swap_Nbyte((char*)&boxl,1,8); + //my_fread(&omega_tmp,sizeof(double), 1, fd); swap_Nbyte((char*)&omega_tmp,1,8); + //my_fread(&lambda_tmp,sizeof(double), 1, fd); swap_Nbyte((char*)&lambda_tmp,1,8); + //my_fread(&hubble_tmp,sizeof(double), 1, fd); swap_Nbyte((char*)&hubble_tmp,1,8); + my_fread(omega,sizeof(double), 1, fd); swap_Nbyte((char*)omega,1,8); + my_fread(lambda,sizeof(double), 1, fd); swap_Nbyte((char*)lambda,1,8); + my_fread(hubble,sizeof(double), 1, fd); swap_Nbyte((char*)hubble,1,8); + fseek(fd,dummysize,1); + SKIP; + } + return(blocksize); +} + +/*-----------------------------------------------------------------------------*/ +/*---------------------- Routine to swap ENDIAN -------------------------------*/ +/*-------- char *data: Pointer to the data ---------------------------------*/ +/*-------- int n: Number of elements to swap --------------------------*/ +/*-------- int m: Size of single element to swap ----------------------*/ +/*-------- int,float = 4 ---------------------------------------*/ +/*-------- double = 8 ---------------------------------------*/ +/*-----------------------------------------------------------------------------*/ +template +void GadgetFile::swap_Nbyte(char *data,int n,int m) +{ + int i,j; + char old_data[16]; + + if(swap>0) + { + for(j=0;j +int GadgetFile::find_block(FILE *fd,const char *label) +{ + int4bytes blocksize=0; + char blocklabel[5]={" "}; + + rewind(fd); + + while(!feof(fd) && blocksize == 0) + { + SKIP; + if(blksize == 134217728) + { +#ifdef MY_DEBUG + printf("Enable ENDIAN swapping !\n"); +#endif + swap=1-swap; + swap_Nbyte((char*)&blksize,1,4); + } + if(blksize != 8) + { + printf("incorrect format (blksize=%d)!\n",blksize); + exit(1); + } + else + { + my_fread(blocklabel, 4*sizeof(char), 1, fd); + my_fread(&blocksize, sizeof(int4bytes), 1, fd); + swap_Nbyte((char*)&blocksize,1,4); +#ifdef MY_DEBUG + printf("Found Block <%s> with %d bytes\n",blocklabel,blocksize); +#endif + SKIP; + if(strcmp(label,blocklabel)!=0) + { + fseek(fd,blocksize,1); + blocksize=0; + } + } + } + return(blocksize-8); +} + +/* THE FOLLOWING METHODS WHERE TAKEN FROM THE readgadget.c FILE DISTRIBUTED BY + KLAUS DOLAG */ + + +/*---------------------- Basic routine to read data from a file ---------------*/ + template + size_t GadgetFile::my_fread(void *ptr, size_t size, size_t nmemb, FILE * stream) + { + size_t nread; + + if((nread = fread(ptr, size, nmemb, stream)) != nmemb) + { + printf("I/O error (fread) !\n"); + exit(3); + } + return nread; + } + + +template +bool GadgetFile::FileExists(string strFilename) { + struct stat stFileInfo; + bool blnReturn; + int intStat; + + // Attempt to get the file attributes + intStat = stat(strFilename.c_str(),&stFileInfo); + if(intStat == 0) { + // We were able to get the file attributes + // so the file obviously exists. + blnReturn = true; + } else { + // We were not able to get the file attributes. + // This may mean that we don't have permission to + // access the folder which contains this file. If you + // need to do that level of checking, lookup the + // return values of stat which will give you + // more details on why stat failed. + blnReturn = false; + } + + return(blnReturn); +} + +template +void GadgetFile::readBlock(const char *blockname) { + + float umass = 1.0; // ???? + + int n; + int np_in_file=np_file_end-np_file_start+1; + cout << string(blockname) << endl; + if (string(blockname)=="POS") { + cout << "Reading block "<< string(blockname) << endl; + pvector *pos=new pvector[ntot]; + cout << "...memory allocated." << endl; + n = read_gadget_float3((float*)pos,"POS ",fd); + cout << "...data are in memory." << endl; + for (int i=0; i<=np_in_file-1; i++) { + //pv[i+np_file_start]->setPos(pos[i].x,pos[i].y,pos[i].z); + p_data[i+np_file_start][0] = pos[i].x; + p_data[i+np_file_start][1] = pos[i].y; + p_data[i+np_file_start][2] = pos[i].z; + } + delete [] pos; + return; + } else if (string(blockname)=="VEL") { + cout << "Reading block "<< string(blockname) << endl; + pvector *vel=new pvector[ntot]; + cout << "...memory allocated..." << endl; + n = read_gadget_float3((float*)vel,"VEL ",fd); + cout << "...data is in memory." << endl; + for (int i=0; i<=np_in_file-1; i++) { + //pv[i+np_file_start]->setVel(vel[i].x,vel[i].y,vel[i].z); + p_data[i+np_file_start].Vel[0] = vel[i].x; + p_data[i+np_file_start].Vel[1] = vel[i].y; + p_data[i+np_file_start].Vel[2] = vel[i].z; + } + delete [] vel; + return; + } else if (string(blockname)=="MASS"){ + cout << "Reading block "<< string(blockname) << endl; + int nmass=0; + for (int i=0; i<=5; i++) { + if (masstab[i]==0.0) {nmass=nmass+npart[i];}} + float *mass=new float[nmass]; + cout << "...memory allocated..." << endl; + n = read_gadget_float((float*)mass,"MASS",fd); + cout << "...data is in memory." << endl; + int icounter=0; + for (int i=0; i<=np_in_file-1; i++) { + int ips=p_data[i+np_file_start].type; + if (masstab[ips] > 0.0) { + //pv[i+np_file_start]->setMass(masstab[ips]*umass); + p_data[i+np_file_start].Mass = masstab[ips]*umass; + } else { + //pv[i+np_file_start]->setMass(mass[icounter]*umass); + p_data[i+np_file_start].Mass = mass[icounter]*umass; + icounter++; + } + } + delete [] mass; + return; + }else if (string(blockname)=="RHO"){ + cout << "Reading block "<< string(blockname) << endl; + float *rho=new float[npart[0]]; + cout << "...memory allocated..." << endl; + n = read_gadget_float((float*)rho,"RHO ",fd); + cout << "...data is in memory." << endl; + for (int i=0; i<=npart[0]-1; i++) { + //pv[i+np_file_start]->setRho(rho[i]); + p_data[i+np_file_start].Rho = rho[i]; + } + delete [] rho; + return; + } else if (string(blockname)=="RHOD"){ + cout << "Reading block "<< string(blockname) << endl; + float *rho=new float[npart[1]]; + cout << "...memory allocated..." << endl; + n = read_gadget_float((float*)rho,"RHOD",fd); + cout << "...data is in memory." << endl; + int icount=0; + for (int i=npart[0]; i<=npart[0]+npart[1]-1; i++) { + //pv[i+np_file_start]->setRho(rho[icount]*umass); + p_data[i+np_file_start].Rho = rho[icount]*umass; + icount++; + } + delete [] rho; + return; + } else if (string(blockname)=="TEMP"){ + cout << "Reading block "<< string(blockname) << endl; + float *temp=new float[npart[0]]; + cout << "...memory allocated..." << endl; + n = read_gadget_float((float*)temp,"TEMP",fd); + cout << "...data is in memory." << endl; + int icount=0; + for (int i=0; i<=npart[0]-1; i++) { + //pv[i+np_file_start]->setTemp(temp[icount]); + p_data[i+np_file_start].Temp = temp[icount]; + icount++; + } + delete [] temp; + return; + }else{ + std::cerr << "*** No " << blockname << " in file(s) " << filebasename << " ***" << std::endl; + } + + // +} + +template<> +void GadgetFile >::readBlock(const char *blockname) { + + float umass = 1.0; // ???? + + int n; + int np_in_file=np_file_end-np_file_start+1; + cout << string(blockname) << endl; + if (string(blockname)=="POS") { + cout << "Reading block "<< string(blockname) << endl; + pvector *pos=new pvector[ntot]; + cout << "...memory allocated." << endl; + n = read_gadget_float3((float*)pos,"POS ",fd); + cout << "...data are in memory." << endl; + for (int i=0; i<=np_in_file-1; i++) { + //pv[i+np_file_start]->setPos(pos[i].x,pos[i].y,pos[i].z); + p_data[i+np_file_start][0] = pos[i].x; + p_data[i+np_file_start][1] = pos[i].y; + p_data[i+np_file_start][2] = pos[i].z; + } + delete [] pos; + return; + } else if (string(blockname)=="MASS"){ + cout << "Reading block "<< string(blockname) << endl; + int nmass=0; + for (int i=0; i<=5; i++) { + if (masstab[i]==0.0) {nmass=nmass+npart[i];}} + float *mass=new float[nmass]; + cout << "...memory allocated..." << endl; + n = read_gadget_float((float*)mass,"MASS",fd); + cout << "...data is in memory." << endl; + int icounter=0; + for (int i=0; i<=np_in_file-1; i++) { + int ips=p_data[i+np_file_start].type; + if (masstab[ips] > 0.0) { + //pv[i+np_file_start]->setMass(masstab[ips]*umass); + p_data[i+np_file_start].Mass = masstab[ips]*umass; + } else { + //pv[i+np_file_start]->setMass(mass[icounter]*umass); + p_data[i+np_file_start].Mass = mass[icounter]*umass; + icounter++; + } + } + delete [] mass; + return; + }else{ + std::cerr << "*** You cannot load " << blockname << " from file(s) " << filebasename << " into a ParticleType type. ***" << std::endl; + } +} + + +/*-----------------------------------------------------------------------------*/ +/*---------------------- Routine to read a 1D float array ---------------------*/ +/*-------- int *data: Pointer where the data are stored to ----------------*/ +/*-------- char *label: Identifyer for the datafield to read ----------------*/ +/*-------- FILE *fd: File handle -----------------------------------------*/ +/*-------- returns length of dataarray ----------------------------------------*/ +/*-----------------------------------------------------------------------------*/ +template +int GadgetFile::read_gadget_float(float *data,const char *label,FILE *fd) +{ + int blocksize; + + blocksize = find_block(fd,label); + if(blocksize <= 0) + { + printf("Block <%s> not fond !\n",label); + exit(5); + } + else + { +#ifdef MY_DEBUG + printf("Reading %d bytes of data from <%s>...\n",blocksize,label); +#endif + SKIP; + my_fread(data,blocksize, 1, fd); + swap_Nbyte((char*)data,blocksize/sizeof(float),4); + SKIP; + } + return(blocksize/sizeof(float)); +} + +/*-----------------------------------------------------------------------------*/ +/*---------------------- Routine to read a 3D float array ---------------------*/ +/*-------- int *data: Pointer where the data are stored to ----------------*/ +/*-------- char *label: Identifyer for the datafield to read ----------------*/ +/*-------- FILE *fd: File handle -----------------------------------------*/ +/*-------- returns length of dataarray ----------------------------------------*/ +/*-----------------------------------------------------------------------------*/ + template + int GadgetFile::read_gadget_float3(float *data,const char *label,FILE *fd) + { + int blocksize; + + blocksize = find_block(fd,label); + if(blocksize <= 0) + { + printf("Block <%s> not fond !\n",label); + exit(5); + } + else + { + #ifdef MY_DEBUG + printf("Reding %d bytes of data from <%s>...\n",blocksize,label); + #endif + SKIP; + my_fread(data,blocksize, 1, fd); + swap_Nbyte((char*)data,blocksize/sizeof(float),4); + SKIP; + } + return(blocksize/sizeof(float)/3); + } + + +#endif From 133baec70606dc81cc64183e87bbe3f323b3b48d Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Fri, 16 Nov 2018 18:48:13 +0100 Subject: [PATCH 074/227] Fixed a bug where it was not resetting a stream and so not reading the ascii file properly. --- MultiPlane/particle_lens.cpp | 37 +++++++++++++++++++++++------------- include/CMakeLists.txt | 2 +- include/particle_halo.h | 9 +++++---- 3 files changed, 30 insertions(+), 18 deletions(-) diff --git a/MultiPlane/particle_lens.cpp b/MultiPlane/particle_lens.cpp index 9e659d0b..6fa2e4de 100644 --- a/MultiPlane/particle_lens.cpp +++ b/MultiPlane/particle_lens.cpp @@ -6,6 +6,8 @@ #include "particle_halo.h" #include "simpleTree.h" #include "utilities_slsim.h" +#include "gadget.hh" + #ifdef ENABLE_FITS #include @@ -163,6 +165,8 @@ bool MakeParticleLenses::readCSV(){ std::string line = ""; size_t ntot = 0; //while (getline(file, line) && ntot < 1000) ntot++; // ???? + while (getline(file, line) && line[0] == '#'); + ++ntot; while (getline(file, line)) ntot++; std::cout << "counted " << ntot << " entries in CSV file " @@ -172,11 +176,15 @@ bool MakeParticleLenses::readCSV(){ std::string delimiter = ","; + //*** be able to read different types of csv files + ntot = 0; - file.seekg(0); + file.clear(); + file.seekg(0); // return to begining + //while (getline(file, line) && line[0] == '#'); + // Iterate through each line and split the content using delimeter - while (getline(file, line)) - { + while (getline(file, line)){ std::vector vec; Utilities::splitstring(line,vec,delimiter); @@ -187,6 +195,7 @@ bool MakeParticleLenses::readCSV(){ data[ntot].type = 0; ++ntot; } + // Close the File file.close(); @@ -199,18 +208,20 @@ bool MakeParticleLenses::readCSV(){ return true; }; -bool readGadget2(){ +bool MakeParticleLenses::readGadget2(){ - /*GadgetFile > gadget_file(filename,data); - z_original = gadget.redshift; + GadgetFile > gadget_file(filename,data); + z_original = gadget_file.redshift; for(int n=0 ; n < gadget_file.numfiles ; ++n){ - gadget_file.openFile(); - gadget_file.readBlock("POS"); - gadget_file.readBlock("MASS"); - gadget_file.closeFile(); + gadget_file.openFile(); + gadget_file.readBlock("POS"); + gadget_file.readBlock("MASS"); + gadget_file.closeFile(); } - + + // ????? **** convert to physical Mpc/h and Msun/h + // ???? *** can we store sizes in gadget blocks // sort by type std::sort(data.begin(),data.end(),[](ParticleType &a1,ParticleType &a2){return a1.type < a2.type;}); @@ -219,7 +230,7 @@ bool readGadget2(){ for(int i = 0 ; i < 6 ; ++i){ //loop through types nparticles.push_back(gadget_file.npart[i]); - masses.push_back(gadget_file.tabmass[i]); + masses.push_back(gadget_file.masstab[i]); if(gadget_file.npart[i] > 0){ pp = data.data() + skip; // pointer to first particle of type @@ -228,7 +239,7 @@ bool readGadget2(){ LensHaloParticles >::calculate_smoothing(Nsmooth,pp,N); } skip += gadget_file.npart[i]; - }*/ + } return true; }; diff --git a/include/CMakeLists.txt b/include/CMakeLists.txt index 8c4f3b56..76729d3f 100644 --- a/include/CMakeLists.txt +++ b/include/CMakeLists.txt @@ -49,6 +49,6 @@ add_sources( concave_hull.h bands_etc.h gaussian.h - #gadget.hh + gadget.hh particle_types.h ) diff --git a/include/particle_halo.h b/include/particle_halo.h index 7262cd17..d544b699 100644 --- a/include/particle_halo.h +++ b/include/particle_halo.h @@ -16,8 +16,6 @@ #include "utilities_slsim.h" #include "lens_halos.h" -//#include "gadget.hh" - /** * \brief A class that represents the lensing by a collection of simulation particles. @@ -546,9 +544,11 @@ void LensHaloParticles::calculate_smoothing(int Nsmooth,PType *pp std::cout << "Calculating smoothing of particles ..." << std::endl << Nsmooth << " neighbors. If there are a lot of particles this could take a while." << std::endl; + time_t to,t; + time(&to); + // make 3d tree of particle postions TreeSimple tree3d(pp,Npoints,10,3,true); - // find distance to nth neighbour for every particle if(Npoints < 1000){ IndexType neighbors[Nsmooth]; @@ -574,7 +574,8 @@ void LensHaloParticles::calculate_smoothing(int Nsmooth,PType *pp } for(int ii = 0; ii < nthreads ;++ii) thr[ii].join(); } - std::cout << "done" << std::endl; + time(&t); + std::cout << "done in " << difftime(t,to) << " secs" << std::endl; } template From b77c9f914ca71fa63cb526cb19602a90e96364ac Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Mon, 19 Nov 2018 21:27:39 +0100 Subject: [PATCH 075/227] Fixed bug in TreeSimple were the nearest neighbour distance was not bing found in N log N time. --- MultiPlane/particle_lens.cpp | 108 +++++++++++++++----- TreeCode_link/utilities.cpp | 1 + include/Tree.h | 1 + include/particle_halo.h | 25 ++--- include/particle_types.h | 2 +- include/simpleTree.h | 191 ++++++++++++++++++++++++++--------- 6 files changed, 242 insertions(+), 86 deletions(-) diff --git a/MultiPlane/particle_lens.cpp b/MultiPlane/particle_lens.cpp index 6fa2e4de..ae43ca2d 100644 --- a/MultiPlane/particle_lens.cpp +++ b/MultiPlane/particle_lens.cpp @@ -46,8 +46,17 @@ MakeParticleLenses::MakeParticleLenses( case gadget2: readGadget2(); break; - case csv: - readCSV(); + case csv3: + readCSV(3); + break; + case csv4: + readCSV(4); + break; + case csv5: + readCSV(5); + break; + case csv6: + readCSV(6); default: break; } @@ -159,13 +168,25 @@ void MakeParticleLenses::CreateHalos(const COSMOLOGY &cosmo,double redshift){ } }; -bool MakeParticleLenses::readCSV(){ +bool MakeParticleLenses::readCSV(int columns_used){ + + std::string delimiter = ","; + std::ifstream file(filename); std::string line = ""; size_t ntot = 0; //while (getline(file, line) && ntot < 1000) ntot++; // ???? while (getline(file, line) && line[0] == '#'); + std::vector vec; + Utilities::splitstring(line,vec,delimiter); + const int ncolumns = vec.size(); + + if(ncolumns < columns_used){ + std:cerr << "file " << filename <<" does not have enough columns." << std::endl; + throw std::invalid_argument("bad file"); + } + ++ntot; while (getline(file, line)) ntot++; @@ -174,28 +195,65 @@ bool MakeParticleLenses::readCSV(){ std::cout << " attempting to read them..."; data.resize(ntot); - std::string delimiter = ","; - //*** be able to read different types of csv files ntot = 0; file.clear(); file.seekg(0); // return to begining - //while (getline(file, line) && line[0] == '#'); - + while (getline(file, line) && line[0] == '#'); + // Iterate through each line and split the content using delimeter - while (getline(file, line)){ - std::vector vec; - Utilities::splitstring(line,vec,delimiter); + if(columns_used == 3){ + do{ + std::vector vec; + Utilities::splitstring(line,vec,delimiter); + + data[ntot][0] = stof(vec[0]); + data[ntot][1] = stof(vec[1]); + data[ntot][2] = stof(vec[2]); + ++ntot; + }while( getline(file, line) ); + }else if(columns_used == 4){ + do{ + std::vector vec; + Utilities::splitstring(line,vec,delimiter); + + data[ntot][0] = stof(vec[0]); + data[ntot][1] = stof(vec[1]); + data[ntot][2] = stof(vec[2]); + data[ntot].Mass = stof(vec[3]); + data[ntot].type = 0; + ++ntot; + }while( getline(file, line) ); + }else if(columns_used == 5){ + do{ + std::vector vec; + Utilities::splitstring(line,vec,delimiter); - data[ntot][0] = stof(vec[0]); - data[ntot][1] = stof(vec[1]); - data[ntot][2] = stof(vec[2]); - data[ntot].Mass = stof(vec[3]); - data[ntot].type = 0; - ++ntot; + data[ntot][0] = stof(vec[0]); + data[ntot][1] = stof(vec[1]); + data[ntot][2] = stof(vec[2]); + data[ntot].Mass = stof(vec[3]); + data[ntot].Size = stof(vec[4]); + data[ntot].type = 0; + ++ntot; + }while( getline(file, line) ); + }else if(columns_used == 6){ + do{ + std::vector vec; + Utilities::splitstring(line,vec,delimiter); + + data[ntot][0] = stof(vec[0]); + data[ntot][1] = stof(vec[1]); + data[ntot][2] = stof(vec[2]); + data[ntot].Mass = stof(vec[3]); + data[ntot].Size = stof(vec[4]); + data[ntot].type = stoi(vec[5]); + ++ntot; + }while( getline(file, line) ); + } - + // Close the File file.close(); @@ -229,16 +287,16 @@ bool MakeParticleLenses::readGadget2(){ size_t skip = 0; for(int i = 0 ; i < 6 ; ++i){ //loop through types - nparticles.push_back(gadget_file.npart[i]); - masses.push_back(gadget_file.masstab[i]); + nparticles.push_back(gadget_file.npart[i]); + masses.push_back(gadget_file.masstab[i]); - if(gadget_file.npart[i] > 0){ - pp = data.data() + skip; // pointer to first particle of type - size_t N = gadget_file.npart[i]; + if(gadget_file.npart[i] > 0){ + pp = data.data() + skip; // pointer to first particle of type + size_t N = gadget_file.npart[i]; - LensHaloParticles >::calculate_smoothing(Nsmooth,pp,N); - } - skip += gadget_file.npart[i]; + LensHaloParticles >::calculate_smoothing(Nsmooth,pp,N); + } + skip += gadget_file.npart[i]; } return true; diff --git a/TreeCode_link/utilities.cpp b/TreeCode_link/utilities.cpp index b018c5d8..9df0b8e1 100644 --- a/TreeCode_link/utilities.cpp +++ b/TreeCode_link/utilities.cpp @@ -902,4 +902,5 @@ void Utilities::splitstring(std::string &line,std::vector &vec vec.push_back(line.substr(0, pos)); line.erase(0, pos + delimiter.length()); } + vec.push_back(line.substr(0, pos)); } diff --git a/include/Tree.h b/include/Tree.h index 120a4a18..b96d85f8 100644 --- a/include/Tree.h +++ b/include/Tree.h @@ -305,6 +305,7 @@ namespace Utilities{ void quicksortPoints(Point *pointarray,PosType *arr,unsigned long N); void quicksortPoints(Point *pointarray,double (*func)(Point &),unsigned long N); + // sort particles and arr according to order of arr void quicksort(unsigned long *particles,PosType *arr,unsigned long N); void quickPartition(PosType pivotvalue,unsigned long *pivotindex,unsigned long *particles ,PosType *arr,unsigned long N); diff --git a/include/particle_halo.h b/include/particle_halo.h index d544b699..08705736 100644 --- a/include/particle_halo.h +++ b/include/particle_halo.h @@ -551,9 +551,9 @@ void LensHaloParticles::calculate_smoothing(int Nsmooth,PType *pp TreeSimple tree3d(pp,Npoints,10,3,true); // find distance to nth neighbour for every particle if(Npoints < 1000){ - IndexType neighbors[Nsmooth]; + //IndexType neighbors[Nsmooth]; for(size_t i=0;i::calculate_smoothing(int Nsmooth,PType *pp } template -void LensHaloParticles::smooth_(TreeSimple *tree3d,PType *xp,size_t N,int Nsmooth){ +void LensHaloParticles::smooth_(TreeSimple *tree3d,PType *pp,size_t N,int Nsmooth){ - IndexType neighbors[Nsmooth]; + //IndexType neighbors[Nsmooth]; for(size_t i=0;iNearestNeighbors(&xp[i][0],Nsmooth,sizesp + i,neighbors); - tree3d->NearestNeighbors(&xp[i][0],Nsmooth,&(xp[i].Size),neighbors); + //tree3d->NearestNeighbors(&xp[i][0],Nsmooth,&(xp[i].Size),neighbors); + pp[i].Size = tree3d->NNDistance(&(pp[i][0]),Nsmooth + 1); } } @@ -676,12 +677,12 @@ void LensHaloParticles::makeSIE( smoothing scales are calculated within particle type. The sph density of gas particles are not used. - csv - CSV ascii format without header. The first three columns - are the positions and the 4th is the mass. There can be - more columns that are ignored. There is only one type of - particle. The lengths are in physical (not comoving) - Mpc/h and the masses are in Msun/h. The sizes are - calculated. + csv3,csv4,csv5,csv6 - CSV ascii format without header. The first three columns + are the positions. Next columns are used for the other formats being and + interpreted as (column 4) masses are in Msun/h, (column 5) the paricle smoothing + size in Mpc/h and (column 6) an integer for type of particle. There can be more + columns in the file than are uesed. Different halos for different types is not yet + implemented in this case. Contact the developer is this is needed. glamb - This is a binary format internal to GLAMER used to store the positions, masses and sizes of the particles. If @@ -782,7 +783,7 @@ class MakeParticleLenses{ bool readGadget2(); // Reads particles from first 4 columns of csv file - bool readCSV(); + bool readCSV(int columns_used); }; diff --git a/include/particle_types.h b/include/particle_types.h index bc8962c6..48090e4f 100644 --- a/include/particle_types.h +++ b/include/particle_types.h @@ -8,7 +8,7 @@ #ifndef particle_types_h #define particle_types_h -enum SimFileFormat {glamb,csv,gadget2,ascii}; +enum SimFileFormat {glamb,csv3,csv4,csv5,csv6,gadget2,ascii}; // Atomic data class for simulation particles with individual sizes and masses template diff --git a/include/simpleTree.h b/include/simpleTree.h index c1f5e1b1..1dc9bb74 100644 --- a/include/simpleTree.h +++ b/include/simpleTree.h @@ -106,9 +106,12 @@ class TreeSimple { void PointsWithinEllipse(T center[2],float a_max,float a_min,float posangle,std::list &neighborkist); /// Finds the nearest N neighbors and puts their index numbers in an array, also returns the distance to the Nth neighbor for calculating smoothing + //template + //void NearestNeighbors(T *ray,int Nneighbors,float *rsph,IndexType *neighbors) const; + template - void NearestNeighbors(T *ray,int Nneighbors,float *rsph,IndexType *neighbors) const; - + T NNDistance(T *ray ,int Nneighbors) const; + class iterator{ public: @@ -127,7 +130,11 @@ class TreeSimple { return *this; } - + + bool operator==(const iterator &it){ + return (current == it.current)*(top == it.top); + } + BranchNB *operator*(){return current;} /// walk tree below branch last assigned to iterator. Returnes false if it has completed walking subtree. @@ -168,6 +175,13 @@ class TreeSimple { return false; } + bool atleaf(){ + return (current->child1 == NULL)*(current->child2 == NULL); + } + bool atTop(){ + return (current == top); + } + private: BranchNB *current; BranchNB *top; @@ -186,6 +200,10 @@ class TreeSimple { TreeNBStruct* BuildTreeNB(PType *xxp,IndexType Nparticles,IndexType *particles,int Ndimensions,PosType theta); void _BuildTreeNB(TreeNBStruct* tree,IndexType nparticles,IndexType *particles); + + template + void _findleaf(T *ray,TreeSimple::iterator &it) const; + template void _PointsWithin(T *ray,float *rmax,std::list &neighborkist); @@ -193,9 +211,9 @@ class TreeSimple { template void _NearestNeighbors(T *ray,int Nneighbors,unsigned long *neighbors,PosType *rneighbors); - template - void _NearestNeighbors(T *ray,PosType *realray,int Nneighbors,unsigned long *neighbors,PosType *rneighbors - ,TreeSimple::iterator &it,int &found) const ; + //template + //void _NearestNeighbors(T *ray,PosType *realray,int Nneighbors,unsigned long *neighbors,PosType *rneighbors + // ,TreeSimple::iterator &it,int &bool) const ; BranchNB *NewBranchNB(IndexType *particles,IndexType nparticles ,PosType boundary_p1[],PosType boundary_p2[] @@ -456,24 +474,16 @@ void TreeSimple::_PointsWithin(T *ray,float *rmax,std::list template -void TreeSimple::NearestNeighbors( - T *ray /// position - ,int Nneighbors /// number of neighbors to be found - ,float *radius /// distance furthest neighbor found from ray[] - ,IndexType *neighborsout /// list of the indexes of the neighbors +T TreeSimple::NNDistance( + T * ray /// position + ,int Nneighbors /// number of neighbors to be found ) const{ - IndexType i; - //static int count=0,oldNneighbors=-1; - short j; - - //Ndim = tree->Ndimensions; - - PosType rneighbors[Nneighbors+Nbucket]; - IndexType neighbors[Nneighbors+Nbucket]; if(tree->top->nparticles <= Nneighbors){ ERROR_MESSAGE(); @@ -481,54 +491,138 @@ void TreeSimple::NearestNeighbors( exit(1); } - /* initalize distance to neighbors to a large number */ - for(i=0;itop->boundary_p2[0]-tree->top->boundary_p1[0])); - neighbors[i] = 0; + std::vector tmp(Ndim); + + TreeSimple::iterator iter(tree->top); + + // if the real ray is outside of root box find the closest boundary point that is inside + for(int j=0;jNdimensions;++j){ + tmp[j] = (ray[j] > tree->current->boundary_p1[j]) ? ray[j] : tree->current->boundary_p1[j]; + tmp[j] = (ray[j] < tree->current->boundary_p2[j]) ? ray[j] : tree->current->boundary_p2[j]; + } + + //std::cout << tmp[0] << " " << tmp[1] << " " << tmp[2] << std::endl; + + // find leaf + _findleaf(tmp.data(),iter); + + //PosType tmp = (10*(tree->top->boundary_p2[0]-tree->top->boundary_p1[0])); + + while((*iter)->nparticles < Nneighbors) iter.up(); + auto pass = iter; + + std::vector r2neighbors((*iter)->nparticles); + for(int i=0 ; i < (*iter)->nparticles ;++i){ + for(int j=0;jNdimensions;++j){ + r2neighbors[i] += pow(tree->pp[(*iter)->particles[i]][j]-ray[j],2); + } } - PosType rlray[tree->Ndimensions]; - for(j=0;jNdimensions;++j) rlray[j]=ray[j]; + std::sort(r2neighbors.begin(),r2neighbors.end()); + r2neighbors.resize(Nneighbors); - //moveTopNB(tree); + PosType bestr = sqrt( r2neighbors.back() ); - TreeSimple::iterator iter(tree->top); - if( inbox(ray,tree->current->boundary_p1,tree->current->boundary_p2) == 0 ){ - for(j=0;jNdimensions;++j){ - ray[j] = (ray[j] > tree->current->boundary_p1[j]) ? ray[j] : tree->current->boundary_p1[j]; - ray[j] = (ray[j] < tree->current->boundary_p2[j]) ? ray[j] : tree->current->boundary_p2[j]; + int cutbox = 1; + for(int dim = 0 ; dim < Ndim ; ++dim){ + cutbox *= ( (ray[dim] - bestr) > (*iter)->boundary_p1[dim] ) + *( (ray[dim] + bestr) < (*iter)->boundary_p2[dim] ); + } + + while(!cutbox && !iter.atTop()){ + iter.up(); + cutbox = 1; + for(int dim = 0 ; dim < Ndim ; ++dim){ + cutbox *= ( (ray[dim] - bestr) > (*iter)->boundary_p1[dim] ) + *( (ray[dim] + bestr) < (*iter)->boundary_p2[dim] ); } } - int notfound = 1; - _NearestNeighbors(ray,rlray,Nneighbors,neighbors,rneighbors,iter,notfound); + auto end = (*iter)->brother; + + /// walk tree from the top + bool decend = true; + do{ + + if(iter == pass){ + decend =false; + }else{ + int cutbox = 1; + for(int dim = 0 ; dim < Ndim ; ++dim){ + cutbox *= ( (ray[dim] + bestr) > (*iter)->boundary_p1[dim] ) + *( (ray[dim] - bestr) < (*iter)->boundary_p2[dim] ); + } + + if(cutbox == 1){ + decend = true; + + if(iter.atleaf()){ + + for(int i=0 ; i<(*iter)->nparticles ;++i){ + PosType r2 = 0; + for(int j=0;jNdimensions;++j){ + r2 += pow(tree->pp[(*iter)->particles[i]][j]-ray[j],2); + } + + if(r2 < bestr*bestr){ + r2neighbors.back() = r2; + int ii = r2neighbors.size() - 1; + while(r2neighbors[ii] < r2neighbors[ii-1] && ii > 0){ + std::swap(r2neighbors[ii],r2neighbors[ii-1]); + --ii; + } + bestr = sqrt( r2neighbors.back() ); + } + } + decend = false; + } + }else{ + decend = false; + } + } + }while(iter.walk(decend) && !(*iter == end) ); - for(i=0;i +template +void TreeSimple::_findleaf(T *ray,TreeSimple::iterator &it) const { + + if(it.atleaf() ) return; + if( inbox(ray,(*it)->child1->boundary_p1,(*it)->child1->boundary_p2) ){ + it.down(1); + _findleaf(ray,it); + }else{ + it.down(2); + _findleaf(ray,it); + } + return; } +/* template template void TreeSimple::_NearestNeighbors(T *ray,PosType *rlray,int Nneighbors ,unsigned long *neighbors,PosType *rneighbors - ,TreeSimple::iterator &it,int ¬found) const { + ,TreeSimple::iterator &it,bool ¬found) const { int incellNB2=1; IndexType i; short j; - if(notfound){ /* not found cell yet */ + if(notfound){ // not found cell yet if( inbox(ray,(*it)->boundary_p1,(*it)->boundary_p2) ){ - /* found the box small enough */ + // found the box small enough if( (*it)->nparticles <= Nneighbors+Nbucket ){ notfound=0; for(j=0;jNdimensions;++j) ray[j]=rlray[j]; - /* calculate the distance to all the particles in cell */ + // calculate the distance to all the particles in cell for(i=0;i<(*it)->nparticles;++i){ for(j=0,rneighbors[i]=0.0;jNdimensions;++j){ rneighbors[i] += pow(tree->pp[(*it)->particles[i]][j]-ray[j],2); @@ -541,27 +635,27 @@ void TreeSimple::_NearestNeighbors(T *ray,PosType *rlray,int Nneighbors Utilities::quicksort(neighbors,rneighbors,(*it)->nparticles); - }else{ /* keep going down the tree */ + }else{ // keep going down the tree if(it.down(1)){ //moveToChildNB(tree,1); _NearestNeighbors(ray,rlray,Nneighbors,neighbors,rneighbors,it,notfound); - /*printf("moving up from level %i\n",(*it)->level);*/ + //printf("moving up from level %i\n",(*it)->level); //moveUpNB(tree); it.up(); incellNB2=notfound; } if(it.down(2)){ - /*printf("moving to child2 from level %i\n",(*it)->level);*/ + //printf("moving to child2 from level %i\n",(*it)->level); //moveToChildNB(tree,2); _NearestNeighbors(ray,rlray,Nneighbors,neighbors,rneighbors,it,notfound); - /*printf("moving up from level %i\n",(*it)->level);*/ + //printf("moving up from level %i\n",(*it)->level); //moveUpNB(tree); it.up(); } - /** if ray found in second child go back to first to search for neighbors **/ + // if ray found in second child go back to first to search for neighbors if( (incellNB2==1) && (notfound==0) ){ if(it.down(1)){ //moveToChildNB(tree,1); @@ -573,12 +667,12 @@ void TreeSimple::_NearestNeighbors(T *ray,PosType *rlray,int Nneighbors } } }else{ // found cell - /* does radius cut into the box */ + // does radius cut into the box if( Utilities::cutbox(ray,(*it)->boundary_p1,(*it)->boundary_p2,rneighbors[Nneighbors-1]) ){ - if( ((*it)->child1 == NULL)*((*it)->child2 == NULL)){ /* leaf case */ + if( ((*it)->child1 == NULL)*((*it)->child2 == NULL)){ // leaf case - /* combine found neighbors with particles in box and resort */ + // combine found neighbors with particles in box and resort for(i=Nneighbors;i<((*it)->nparticles+Nneighbors);++i){ for(j=0,rneighbors[i]=0.0;jNdimensions;++j){ rneighbors[i]+=pow(tree->pp[(*it)->particles[i-Nneighbors]][j]-ray[j],2); @@ -610,6 +704,7 @@ void TreeSimple::_NearestNeighbors(T *ray,PosType *rlray,int Nneighbors } return; } +*/ template template From a0c55039f951d9bb68e15e34f6f4724290307947 Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Tue, 20 Nov 2018 17:05:45 +0100 Subject: [PATCH 076/227] Trying to improve SimpleTree NN search. --- MultiPlane/particle_lens.cpp | 1 - include/particle_halo.h | 2 +- include/simpleTree.h | 23 ++++++++++++++++++----- 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/MultiPlane/particle_lens.cpp b/MultiPlane/particle_lens.cpp index ae43ca2d..5391792b 100644 --- a/MultiPlane/particle_lens.cpp +++ b/MultiPlane/particle_lens.cpp @@ -176,7 +176,6 @@ bool MakeParticleLenses::readCSV(int columns_used){ std::ifstream file(filename); std::string line = ""; size_t ntot = 0; - //while (getline(file, line) && ntot < 1000) ntot++; // ???? while (getline(file, line) && line[0] == '#'); std::vector vec; Utilities::splitstring(line,vec,delimiter); diff --git a/include/particle_halo.h b/include/particle_halo.h index 08705736..efcb6578 100644 --- a/include/particle_halo.h +++ b/include/particle_halo.h @@ -548,7 +548,7 @@ void LensHaloParticles::calculate_smoothing(int Nsmooth,PType *pp time(&to); // make 3d tree of particle postions - TreeSimple tree3d(pp,Npoints,10,3,true); + TreeSimple tree3d(pp,Npoints,2*Nsmooth,3,true); // find distance to nth neighbour for every particle if(Npoints < 1000){ //IndexType neighbors[Nsmooth]; diff --git a/include/simpleTree.h b/include/simpleTree.h index 1dc9bb74..c5683d73 100644 --- a/include/simpleTree.h +++ b/include/simpleTree.h @@ -140,6 +140,7 @@ class TreeSimple { /// walk tree below branch last assigned to iterator. Returnes false if it has completed walking subtree. bool walk(bool decend){ + ++count; if(decend && current->child1 != NULL){ current = current->child1; return true; @@ -156,12 +157,14 @@ class TreeSimple { } bool up(){ + ++count; if(current == top) return false; current = current->prev; return true; } bool down(int child){ + ++count; if(child == 1){ if(current->child1 == NULL) return false; current = current->child1; @@ -175,6 +178,10 @@ class TreeSimple { return false; } + void moveTop(){ + current = top; + } + bool atleaf(){ return (current->child1 == NULL)*(current->child2 == NULL); } @@ -182,7 +189,9 @@ class TreeSimple { return (current == top); } + size_t getcout(){return count;} private: + size_t count = 0; // ??? BranchNB *current; BranchNB *top; }; @@ -492,13 +501,12 @@ T TreeSimple::NNDistance( } std::vector tmp(Ndim); - TreeSimple::iterator iter(tree->top); // if the real ray is outside of root box find the closest boundary point that is inside for(int j=0;jNdimensions;++j){ tmp[j] = (ray[j] > tree->current->boundary_p1[j]) ? ray[j] : tree->current->boundary_p1[j]; - tmp[j] = (ray[j] < tree->current->boundary_p2[j]) ? ray[j] : tree->current->boundary_p2[j]; + tmp[j] = (ray[j] < tree->current->boundary_p2[j]) ? tmp[j] : tree->current->boundary_p2[j]; } //std::cout << tmp[0] << " " << tmp[1] << " " << tmp[2] << std::endl; @@ -508,7 +516,8 @@ T TreeSimple::NNDistance( //PosType tmp = (10*(tree->top->boundary_p2[0]-tree->top->boundary_p1[0])); - while((*iter)->nparticles < Nneighbors) iter.up(); + int levelup = 0; // ?? + while((*iter)->nparticles < Nneighbors){ iter.up(); ++levelup;} auto pass = iter; std::vector r2neighbors((*iter)->nparticles); @@ -529,15 +538,18 @@ T TreeSimple::NNDistance( *( (ray[dim] + bestr) < (*iter)->boundary_p2[dim] ); } + levelup = 0; // ??? while(!cutbox && !iter.atTop()){ iter.up(); + ++levelup; // ??? cutbox = 1; for(int dim = 0 ; dim < Ndim ; ++dim){ cutbox *= ( (ray[dim] - bestr) > (*iter)->boundary_p1[dim] ) *( (ray[dim] + bestr) < (*iter)->boundary_p2[dim] ); } - } + } + //iter.moveTop(); auto end = (*iter)->brother; /// walk tree from the top @@ -565,8 +577,8 @@ T TreeSimple::NNDistance( } if(r2 < bestr*bestr){ - r2neighbors.back() = r2; int ii = r2neighbors.size() - 1; + r2neighbors[ii] = r2; while(r2neighbors[ii] < r2neighbors[ii-1] && ii > 0){ std::swap(r2neighbors[ii],r2neighbors[ii-1]); --ii; @@ -582,6 +594,7 @@ T TreeSimple::NNDistance( } }while(iter.walk(decend) && !(*iter == end) ); + //std::cout << " count " << iter.getcout() << " branches :" << tree->Nbranches << std::endl; return bestr; } From 2957318521445f81306c15327c62796d69e7cd17 Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Wed, 21 Nov 2018 16:32:55 +0100 Subject: [PATCH 077/227] Update current center of mass when rotating a LensHaloParticle. --- FullRange/internal_rayshooter_multi.cpp | 2 +- MultiPlane/particle_lens.cpp | 10 +++++++++- include/particle_halo.h | 3 ++- 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/FullRange/internal_rayshooter_multi.cpp b/FullRange/internal_rayshooter_multi.cpp index 270ddb87..7f83bf05 100644 --- a/FullRange/internal_rayshooter_multi.cpp +++ b/FullRange/internal_rayshooter_multi.cpp @@ -394,7 +394,7 @@ void *compute_rays_parallel(void *_p) // --------------------------------------------------------------------------------------------- - // Conversion of dt from Mpc (physical Mpc) to Years (also possible into days) ----------------- + // Conversion of dt from Mpc (physical Mpc) to Years ----------------- p->i_points[i].dt *= MpcToSeconds * SecondToYears ; diff --git a/MultiPlane/particle_lens.cpp b/MultiPlane/particle_lens.cpp index be4203d6..00078446 100644 --- a/MultiPlane/particle_lens.cpp +++ b/MultiPlane/particle_lens.cpp @@ -313,6 +313,14 @@ void LensHaloParticles::rotate_particles(PosType theta_x,PosType theta_y){ } for(j=0;j<3;++j) xp[i][j]=tmp[j]; } + + for(j=0;j<3;++j) tmp[j]=0.0; + for(j=0;j<3;++j){ + tmp[0]+=coord[0][j]*mcenter[j]; + tmp[1]+=coord[1][j]*mcenter[j]; + tmp[2]+=coord[2][j]*mcenter[j]; + } + for(j=0;j<3;++j) mcenter[j]=tmp[j]; } void LensHaloParticles::calculate_smoothing(int Nsmooth){ @@ -422,4 +430,4 @@ void LensHaloParticles::makeSIE( } datafile.close(); -} \ No newline at end of file +} diff --git a/include/particle_halo.h b/include/particle_halo.h index a23f0f61..913e96d5 100644 --- a/include/particle_halo.h +++ b/include/particle_halo.h @@ -51,8 +51,9 @@ class LensHaloParticles : public LensHalo /// rotate the simulation around the origin of the simulation coordinates, (radians) void rotate(Point_2d theta); - /// center of mass in input coordinates + /// get current center of mass in input coordinates Point_3d CenterOfMass(){return mcenter;} + /** \brief This is a test class that makes a truncated SIE out of particles and puts it into a file in the right format for constructing a LensHaloParticles. This is useful for calculating the level of shot noise and finite source size. The particles are distributed in 3D according to the SIE profile with only the perpendicular coordinates (1st and 2nd) distorted into an elliptical shape. If the halo is rotated from the original orientation it will not be a oblate spheroid. From 6923e976387cdd82c3337e84ff9823688208ab02 Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Thu, 22 Nov 2018 10:53:31 +0100 Subject: [PATCH 078/227] Improved RAY class. --- TreeCode_link/TreeDriver.cpp | 21 ++++++++++++++++++ TreeCode_link/image_finder_kist.cpp | 2 +- include/InputParams.h | 6 +++++- include/image_info.h | 2 ++ include/point.h | 33 ++++++++++++++++++----------- 5 files changed, 50 insertions(+), 14 deletions(-) diff --git a/TreeCode_link/TreeDriver.cpp b/TreeCode_link/TreeDriver.cpp index 6fee9349..2b62a8b2 100644 --- a/TreeCode_link/TreeDriver.cpp +++ b/TreeCode_link/TreeDriver.cpp @@ -1474,6 +1474,27 @@ KappaType ImageInfo::aveTimeDelay() return tmp_dt; } +/// Computes the time delay averaged over the image +RAY ImageInfo::closestRay(const Point_2d &y){ + + Point *p; + + double r2min = HUGE; + for(Kist::iterator it = imagekist->begin() + ; it != imagekist->end() + ; ++it){ + + double dy2 = pow( (*it).image->x[0] - y[0],2 ) + + pow( (*it).image->x[1] - y[1],2 ); + + if(r2min > dy2){ + p = &(*it); + r2min = dy2; + } + } + + return RAY(p); +} ImageInfo & ImageInfo::operator+=(ImageInfo & rhs){ if(!(uniform_mag && rhs.uniform_mag diff --git a/TreeCode_link/image_finder_kist.cpp b/TreeCode_link/image_finder_kist.cpp index 1d866aa8..d28fb52a 100644 --- a/TreeCode_link/image_finder_kist.cpp +++ b/TreeCode_link/image_finder_kist.cpp @@ -1574,7 +1574,7 @@ void ImageFinding::find_images_microlens_exper( /** \ingroup ImageFindingL2 * - * \brief Finds images for a given source position and size + * \brief Finds images for a given source position and size. Not meant for high level user. * * image points are put into imageinfo[].imagekist * imageinfo[].points and imageinfo[].Npoints are not changed diff --git a/include/InputParams.h b/include/InputParams.h index eacfe9df..19c252c7 100644 --- a/include/InputParams.h +++ b/include/InputParams.h @@ -28,6 +28,7 @@ #include #endif +/// Type of mass function enum MassFuncType { PressSchechter, @@ -35,6 +36,7 @@ enum MassFuncType PowerLaw }; +/// Type of halo profile enum LensHaloType { null_lens, @@ -49,6 +51,8 @@ enum LensHaloType hern_lens, jaffe_lens }; + +/// format of mass map file enum PixelMapType { moka, @@ -65,7 +69,7 @@ enum GalaxyLensHaloType }; /// names of clump and sb models -typedef enum {nfw,powerlaw,pointmass} ClumpInternal; +enum ClumpInternal {nfw,powerlaw,pointmass}; /// Initial mass function type enum IMFtype {One,Mono,BrokenPowerLaw,Salpeter,SinglePowerLaw,Kroupa,Chabrier}; /// Photometric bands diff --git a/include/image_info.h b/include/image_info.h index 418cbab2..e63d3ad3 100644 --- a/include/image_info.h +++ b/include/image_info.h @@ -71,6 +71,8 @@ struct ImageInfo{ KappaType aveTimeDelay(); /// Computes the inverse magnification averaged over the image KappaType aveInvMag(); + /// finds the ray in the image that is closest to the point y on the source plane + RAY closestRay(const Point_2d &y); /// Print information about the image void PrintImageInfo(); void copy(const ImageInfo & image,bool copykists = true); diff --git a/include/point.h b/include/point.h index 7f29f870..a3a2bcf1 100644 --- a/include/point.h +++ b/include/point.h @@ -201,23 +201,32 @@ struct Point: public Point_2d{ //Point &operator=(const Point &p); }; -/** \brief A point that is automatically linked to its source point eliminating the - need for using LinkToSourcePoint(). Useful when a limited number of Points are needed and they do not need to be in continuous memory. +/** \brief Simple representaion of a light path giving position on the image and source planes and lensing quantities. */ -struct RAY: public Point{ - RAY(){ - image = &source_point; - image->image = this; +struct RAY{ + RAY(Point *p){ + x = p->x; + y = p->image->x; + kappa = p->kappa; + dt = p->dt; + + gamma[0] = p->gamma[0]; + gamma[1] = p->gamma[1]; + gamma[2] = p->gamma[2]; } ~RAY(){}; - /// postions on image plane - PosType *pos_image(){return x;}; - /// postions on source plane - PosType *pos_source(){return image->x;} + // image position + Point_2d x; + // source position + Point_2d y; -private: - Point source_point; + KappaType kappa,gamma[3],dt; + + KappaType invmag(){return (1-kappa)*(1-kappa) - gamma[0]*gamma[0] + - gamma[1]*gamma[1] + gamma[2]*gamma[2];} + + Point_2d alpha(){return x - y;} }; std::ostream &operator<<(std::ostream &os, Point const &p); From f13d0798c95872a884d6e5d6f134ab016b5bf394 Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Thu, 22 Nov 2018 17:57:10 +0100 Subject: [PATCH 079/227] Fixed bug that was affecting the time delay for halos in a tree structure. --- AnalyticNSIE/elliptic.cpp | 2 +- MultiPlane/lens.cpp | 4 ++-- MultiPlane/lens_halos.cpp | 5 ++--- include/InputParams.h | 12 ++++++------ include/lens_halos.h | 6 +++--- 5 files changed, 14 insertions(+), 15 deletions(-) diff --git a/AnalyticNSIE/elliptic.cpp b/AnalyticNSIE/elliptic.cpp index 9343aa5d..38c32c00 100644 --- a/AnalyticNSIE/elliptic.cpp +++ b/AnalyticNSIE/elliptic.cpp @@ -35,7 +35,7 @@ PosType Elliptic::DALPHAYDM::operator()(PosType m){ double p2 = x[0]*x[0]/ap/ap/ap/ap + x[1]*x[1]/bp/bp/bp/bp; // actually the inverse of equation (5) in Schramm 1990 PosType alpha[2]={0,0},tmp[2] = {m*(isohalo->getRsize()),0}; - KappaType kappa=0,gamma[2]={0,0},phi; + KappaType kappa=0,gamma[2]={0,0},phi=0; isohalo->force_halo(alpha,&kappa,gamma,&phi,tmp); // here we need an elliptical kappa but in forcehalo the only elliptical kappas implemented are based on Ansatz I+II diff --git a/MultiPlane/lens.cpp b/MultiPlane/lens.cpp index b501b8e5..70df8f0b 100644 --- a/MultiPlane/lens.cpp +++ b/MultiPlane/lens.cpp @@ -2826,7 +2826,7 @@ void Lens::readInputSimFileObservedGalaxies(bool verbose) void Lens::combinePlanes(bool verbose) { - if(verbose) + /*if(verbose) { std::cout << std::endl << "Lens::combinePlanes before clearing." << std::endl ; for(int i=0;ikappa_h(xiso)/PI/xiso/xiso; PosType alpha[2]={0,0},tm[2] = {m*rmx,0}; - KappaType kappam=0,gamma[2]={0,0},phi; + KappaType kappam=0,gamma[2]={0,0},phi=0; halo->force_halo_sym(alpha,&kappam,gamma,&phi,tm); @@ -475,7 +475,7 @@ class LensHalo{ KappaType kappa=halo->kappa_h(xiso)/PI/xiso/xiso; PosType alpha[2]={0,0},tm[2] = {m*rmx,0}; - KappaType kappam=0,gamma[2]={0,0},phi; + KappaType kappam=0,gamma[2]={0,0},phi=0; halo->force_halo_sym(alpha,&kappam,gamma,&phi,tm); double integrandA=m*kappa/(ap*ap*ap*bp*p2)*halo->mass; @@ -906,7 +906,7 @@ class LensHaloPowerLaw: public LensHalo{ } inline KappaType phi_int(PosType x) const{ //return alpha_int(x); - return -1.0*pow(x/xmax,-beta+2)/(-beta+2); + return -1.0*pow(x/xmax,-beta+2)/(2-beta); } }; From 42575adfcf67bc1eeb011bb7bf0ca1fb829d73e1 Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Thu, 22 Nov 2018 19:40:16 +0100 Subject: [PATCH 080/227] Further optimisation of simpleTree NN search. --- MultiPlane/particle_lens.cpp | 2 +- include/simpleTree.h | 47 +++++++++++++++++++++--------------- 2 files changed, 29 insertions(+), 20 deletions(-) diff --git a/MultiPlane/particle_lens.cpp b/MultiPlane/particle_lens.cpp index 5391792b..6deed156 100644 --- a/MultiPlane/particle_lens.cpp +++ b/MultiPlane/particle_lens.cpp @@ -182,7 +182,7 @@ bool MakeParticleLenses::readCSV(int columns_used){ const int ncolumns = vec.size(); if(ncolumns < columns_used){ - std:cerr << "file " << filename <<" does not have enough columns." << std::endl; + std::cerr << "file " << filename <<" does not have enough columns." << std::endl; throw std::invalid_argument("bad file"); } diff --git a/include/simpleTree.h b/include/simpleTree.h index c5683d73..a8397d98 100644 --- a/include/simpleTree.h +++ b/include/simpleTree.h @@ -522,7 +522,7 @@ T TreeSimple::NNDistance( std::vector r2neighbors((*iter)->nparticles); for(int i=0 ; i < (*iter)->nparticles ;++i){ - for(int j=0;jNdimensions;++j){ + for(int j=0;jpp[(*iter)->particles[i]][j]-ray[j],2); } } @@ -531,27 +531,27 @@ T TreeSimple::NNDistance( r2neighbors.resize(Nneighbors); PosType bestr = sqrt( r2neighbors.back() ); - + /* int cutbox = 1; for(int dim = 0 ; dim < Ndim ; ++dim){ cutbox *= ( (ray[dim] - bestr) > (*iter)->boundary_p1[dim] ) *( (ray[dim] + bestr) < (*iter)->boundary_p2[dim] ); } - levelup = 0; // ??? while(!cutbox && !iter.atTop()){ iter.up(); - ++levelup; // ??? cutbox = 1; for(int dim = 0 ; dim < Ndim ; ++dim){ cutbox *= ( (ray[dim] - bestr) > (*iter)->boundary_p1[dim] ) *( (ray[dim] + bestr) < (*iter)->boundary_p2[dim] ); } } + */ + + iter.moveTop(); + //auto end = (*iter)->brother; + iter.walk(true); - //iter.moveTop(); - auto end = (*iter)->brother; - /// walk tree from the top bool decend = true; do{ @@ -559,21 +559,30 @@ T TreeSimple::NNDistance( if(iter == pass){ decend =false; }else{ - int cutbox = 1; - for(int dim = 0 ; dim < Ndim ; ++dim){ - cutbox *= ( (ray[dim] + bestr) > (*iter)->boundary_p1[dim] ) - *( (ray[dim] - bestr) < (*iter)->boundary_p2[dim] ); - } + + BranchNB * cbranch = *iter; + + //int cutbox = 1; + //for(int dim = 0 ; dim < Ndim ; ++dim){ + // cutbox *= ( (ray[dim] + bestr) > (*iter)->boundary_p1[dim] ) + // *( (ray[dim] - bestr) < (*iter)->boundary_p2[dim] ); + //} - if(cutbox == 1){ + short dim = (cbranch->level-1) % Ndim; // this box was cut in this dimension + + bool cutbox = ( (ray[dim] + bestr) > cbranch->boundary_p1[dim] ) + *( (ray[dim] - bestr) < cbranch->boundary_p2[dim] ); + + + if(cutbox){ decend = true; if(iter.atleaf()){ - for(int i=0 ; i<(*iter)->nparticles ;++i){ + for(int i=0 ; i< cbranch->nparticles ;++i){ PosType r2 = 0; - for(int j=0;jNdimensions;++j){ - r2 += pow(tree->pp[(*iter)->particles[i]][j]-ray[j],2); + for(int j=0;jpp[cbranch->particles[i]][j]-ray[j],2); } if(r2 < bestr*bestr){ @@ -586,14 +595,14 @@ T TreeSimple::NNDistance( bestr = sqrt( r2neighbors.back() ); } } - decend = false; } }else{ decend = false; } } - }while(iter.walk(decend) && !(*iter == end) ); - +// }while(iter.walk(decend) && !(*iter == end) ); + }while(iter.walk(decend)); + //std::cout << " count " << iter.getcout() << " branches :" << tree->Nbranches << std::endl; return bestr; } From dd765782a3ad0a562f752e4a77f562fee3c57e35 Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Fri, 23 Nov 2018 17:20:01 +0100 Subject: [PATCH 081/227] Further optimisation of simpleTree.NNDistance() --- include/particle_halo.h | 6 ---- include/simpleTree.h | 80 +++++++++++++++++++++++++++-------------- 2 files changed, 53 insertions(+), 33 deletions(-) diff --git a/include/particle_halo.h b/include/particle_halo.h index efcb6578..530e7c0f 100644 --- a/include/particle_halo.h +++ b/include/particle_halo.h @@ -565,10 +565,6 @@ void LensHaloParticles::calculate_smoothing(int Nsmooth,PType *pp N = Npoints - ii*chunksize; }else N = chunksize; - //thr[ii] = std::thread(&LensHaloParticles::smooth_,this,&tree3d - // ,&(xp[ii*chunksize]),&(sizes[ii*chunksize]),N,Nsmooth); - //thr[ii] = std::thread(LensHaloParticles::smooth_,&tree3d - // ,&(pp[ii*chunksize]),N,Nsmooth); thr[ii] = std::thread(LensHaloParticles::smooth_,&tree3d ,&(pp[ii*chunksize]),N,Nsmooth); } @@ -583,8 +579,6 @@ void LensHaloParticles::smooth_(TreeSimple *tree3d,PType *pp,size_ //IndexType neighbors[Nsmooth]; for(size_t i=0;iNearestNeighbors(&xp[i][0],Nsmooth,sizesp + i,neighbors); - //tree3d->NearestNeighbors(&xp[i][0],Nsmooth,&(xp[i].Size),neighbors); pp[i].Size = tree3d->NNDistance(&(pp[i][0]),Nsmooth + 1); } } diff --git a/include/simpleTree.h b/include/simpleTree.h index a8397d98..89429193 100644 --- a/include/simpleTree.h +++ b/include/simpleTree.h @@ -8,6 +8,8 @@ #ifndef SIMP_TREE_H_ #define SIMP_TREE_H_ +#include + #include "standard.h" #include "Tree.h" //#include "lens_halos.h" @@ -32,10 +34,10 @@ struct BranchNB{ /// the number of particles that aren't in children IndexType big_particle; /// Size of largest particle in branch - PosType maxrsph; + //PosType maxrsph; /// center of mass PosType *center; - PosType mass; + //PosType mass; /// level in tree int level; unsigned long number; @@ -53,13 +55,13 @@ struct BranchNB{ /* projected quantities */ /// quadropole moment of branch - PosType quad[3]; + //PosType quad[3]; /// largest dimension of box - PosType rmax; + //PosType rmax; /// the critical distance below which a branch is opened in the - PosType rcrit_angle; + //PosType rcrit_angle; /* force calculation */ - PosType rcrit_part; + //PosType rcrit_part; //PosType cm[2]; /* projected center of mass */ }; @@ -520,17 +522,37 @@ T TreeSimple::NNDistance( while((*iter)->nparticles < Nneighbors){ iter.up(); ++levelup;} auto pass = iter; - std::vector r2neighbors((*iter)->nparticles); - for(int i=0 ; i < (*iter)->nparticles ;++i){ + //std::vector r2neighbors((*iter)->nparticles); + std::priority_queue r2heap; + + int i; + for(i=0 ; i < Nneighbors ;++i){ + PosType r2 = 0; + for(int j=0;jpp[(*iter)->particles[i]][j]-ray[j],2); + r2 += pow(tree->pp[(*iter)->particles[i]][j]-ray[j],2); + } + r2heap.push(r2); + } + + for(; i < (*iter)->nparticles ;++i){ + PosType r2 = 0; for(int j=0;jpp[(*iter)->particles[i]][j]-ray[j],2); + //r2neighbors[i] += pow(tree->pp[(*iter)->particles[i]][j]-ray[j],2); + r2 += pow(tree->pp[(*iter)->particles[i]][j]-ray[j],2); + } + if(r2 < r2heap.top()){ + r2heap.push(r2); + r2heap.pop(); } } + + //std::sort(r2neighbors.begin(),r2neighbors.end()); + //r2neighbors.resize(Nneighbors); - std::sort(r2neighbors.begin(),r2neighbors.end()); - r2neighbors.resize(Nneighbors); + //PosType bestr = sqrt( r2neighbors.back() ); - PosType bestr = sqrt( r2neighbors.back() ); + PosType bestr = sqrt( r2heap.top() ); /* int cutbox = 1; for(int dim = 0 ; dim < Ndim ; ++dim){ @@ -570,11 +592,8 @@ T TreeSimple::NNDistance( short dim = (cbranch->level-1) % Ndim; // this box was cut in this dimension - bool cutbox = ( (ray[dim] + bestr) > cbranch->boundary_p1[dim] ) - *( (ray[dim] - bestr) < cbranch->boundary_p2[dim] ); - - - if(cutbox){ + if(( (ray[dim] + bestr) > cbranch->boundary_p1[dim] ) + *( (ray[dim] - bestr) < cbranch->boundary_p2[dim] ) ){ decend = true; if(iter.atleaf()){ @@ -586,13 +605,18 @@ T TreeSimple::NNDistance( } if(r2 < bestr*bestr){ - int ii = r2neighbors.size() - 1; - r2neighbors[ii] = r2; - while(r2neighbors[ii] < r2neighbors[ii-1] && ii > 0){ - std::swap(r2neighbors[ii],r2neighbors[ii-1]); - --ii; - } - bestr = sqrt( r2neighbors.back() ); + + r2heap.push(r2); + r2heap.pop(); + bestr = sqrt( r2heap.top() ); + + //int ii = r2neighbors.size() - 1; + //r2neighbors[ii] = r2; + //while(r2neighbors[ii] < r2neighbors[ii-1] && ii > 0){ + // std::swap(r2neighbors[ii],r2neighbors[ii-1]); + // --ii; + //} + //bestr = sqrt( r2neighbors.back() ); } } } @@ -611,9 +635,11 @@ template template void TreeSimple::_findleaf(T *ray,TreeSimple::iterator &it) const { - if(it.atleaf() ) return; + if( it.atleaf() ) return; + + int d = (*it)->level % Ndim; - if( inbox(ray,(*it)->child1->boundary_p1,(*it)->child1->boundary_p2) ){ + if( (*it)->child1->boundary_p2[d] > ray[d] ){ it.down(1); _findleaf(ray,it); }else{ @@ -875,7 +901,7 @@ void TreeSimple::_BuildTreeNB(TreeNBStruct * tree,IndexType nparti cbranch->center[0] = (cbranch->boundary_p1[0] + cbranch->boundary_p2[0])/2; cbranch->center[1] = (cbranch->boundary_p1[1] + cbranch->boundary_p2[1])/2; - cbranch->quad[0]=cbranch->quad[1]=cbranch->quad[2]=0; + //cbranch->quad[0]=cbranch->quad[1]=cbranch->quad[2]=0; /* leaf case */ if(cbranch->nparticles <= Nbucket){ From 62f19bf8f800851cf8e1d007704359189f0ab335 Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Sat, 24 Nov 2018 15:01:12 +0100 Subject: [PATCH 082/227] Fixed a bug in in LensHaloParticles construction. Alloow for type to be in scv4 data ifile format. Added some documentation. --- MultiPlane/particle_lens.cpp | 43 ++++++++++++++++++++++++++++++------ include/particle_halo.h | 8 +++---- include/particle_types.h | 2 +- include/simpleTree.h | 4 ++-- 4 files changed, 43 insertions(+), 14 deletions(-) diff --git a/MultiPlane/particle_lens.cpp b/MultiPlane/particle_lens.cpp index 6deed156..f07c0367 100644 --- a/MultiPlane/particle_lens.cpp +++ b/MultiPlane/particle_lens.cpp @@ -29,13 +29,13 @@ MakeParticleLenses::MakeParticleLenses( - if(format == glamb ){ + if(format == glmb ){ nparticles.resize(6,0); readSizesB(filename,data,Nsmooth,nparticles,z_original); }else{ std::string sizefile = filename + "_S" - + std::to_string(Nsmooth) + ".glamb"; + + std::to_string(Nsmooth) + ".glmb"; if(access( sizefile.c_str(), F_OK ) != -1){ nparticles.resize(6,0); @@ -200,7 +200,8 @@ bool MakeParticleLenses::readCSV(int columns_used){ file.clear(); file.seekg(0); // return to begining while (getline(file, line) && line[0] == '#'); - + nparticles = {0,0,0,0,0,0}; + // Iterate through each line and split the content using delimeter if(columns_used == 3){ do{ @@ -210,8 +211,11 @@ bool MakeParticleLenses::readCSV(int columns_used){ data[ntot][0] = stof(vec[0]); data[ntot][1] = stof(vec[1]); data[ntot][2] = stof(vec[2]); + data[ntot].Mass = 1.0; ++ntot; }while( getline(file, line) ); + nparticles = {ntot,0,0,0,0,0}; + masses = {0,0,0,0,0,0}; }else if(columns_used == 4){ do{ std::vector vec; @@ -224,6 +228,8 @@ bool MakeParticleLenses::readCSV(int columns_used){ data[ntot].type = 0; ++ntot; }while( getline(file, line) ); + nparticles = {ntot,0,0,0,0,0}; + masses = {0,0,0,0,0,0}; }else if(columns_used == 5){ do{ std::vector vec; @@ -237,6 +243,8 @@ bool MakeParticleLenses::readCSV(int columns_used){ data[ntot].type = 0; ++ntot; }while( getline(file, line) ); + nparticles = {ntot,0,0,0,0,0}; + masses = {0,0,0,0,0,0}; }else if(columns_used == 6){ do{ std::vector vec; @@ -249,8 +257,19 @@ bool MakeParticleLenses::readCSV(int columns_used){ data[ntot].Size = stof(vec[4]); data[ntot].type = stoi(vec[5]); ++ntot; + ++nparticles[data[ntot].type]; }while( getline(file, line) ); - + + int ntypes = 0; + for(size_t n : nparticles){ + if(n > 0) ++ntypes; + } + + if(ntypes > 1){ + // sort by type + std::sort(data.begin(),data.end(),[](ParticleType &a1,ParticleType &a2){return a1.type < a2.type;}); + } + masses = {0,0,0,0,0,0}; } // Close the File @@ -258,9 +277,19 @@ bool MakeParticleLenses::readCSV(int columns_used){ std::cout << ntot << " particle positions read from file " << filename << std::endl; - nparticles = {ntot,0,0,0,0,0}; - masses = {0,0,0,0,0,0}; - LensHaloParticles >::calculate_smoothing(Nsmooth,data.data(),data.size()); + ParticleType *pp; + size_t skip = 0; + for(int i = 0 ; i < 6 ; ++i){ //loop through types + if(nparticles[i] > 0){ + pp = data.data() + skip; // pointer to first particle of type + size_t N = nparticles[i]; + + LensHaloParticles >::calculate_smoothing(Nsmooth,pp,N); + } + skip += nparticles[i]; + } + + //LensHaloParticles >::calculate_smoothing(Nsmooth,data.data(),data.size()); return true; }; diff --git a/include/particle_halo.h b/include/particle_halo.h index 530e7c0f..92ff1c59 100644 --- a/include/particle_halo.h +++ b/include/particle_halo.h @@ -234,7 +234,7 @@ LensHaloParticles::LensHaloParticles( ,Point_2d theta_rotate /// rotation of particles around the origin ,bool recenter /// center on center of mass ,float MinPSize /// minimum particle size -):min_size(MinPSize),multimass(true),Npoints(Nparticles) +):min_size(MinPSize),multimass(true),Npoints(Nparticles),pp(pdata) { LensHalo::setZlens(redshift); @@ -675,10 +675,10 @@ void LensHaloParticles::makeSIE( are the positions. Next columns are used for the other formats being and interpreted as (column 4) masses are in Msun/h, (column 5) the paricle smoothing size in Mpc/h and (column 6) an integer for type of particle. There can be more - columns in the file than are uesed. Different halos for different types is not yet - implemented in this case. Contact the developer is this is needed. + columns in the file than are uesed. In the case of csv4, when there are more then one + type of halo each type will be in a differeent LensHaloParticles with differnt smoothing. - glamb - This is a binary format internal to GLAMER used to store + glmb - This is a binary format internal to GLAMER used to store the positions, masses and sizes of the particles. If GLAMER has generated one, it should be all that is needed to recreate the LensHaloParticles. diff --git a/include/particle_types.h b/include/particle_types.h index 48090e4f..9de8e64c 100644 --- a/include/particle_types.h +++ b/include/particle_types.h @@ -8,7 +8,7 @@ #ifndef particle_types_h #define particle_types_h -enum SimFileFormat {glamb,csv3,csv4,csv5,csv6,gadget2,ascii}; +enum SimFileFormat {glmb,csv3,csv4,csv5,csv6,gadget2,ascii}; // Atomic data class for simulation particles with individual sizes and masses template diff --git a/include/simpleTree.h b/include/simpleTree.h index 89429193..e72616d5 100644 --- a/include/simpleTree.h +++ b/include/simpleTree.h @@ -905,7 +905,7 @@ void TreeSimple::_BuildTreeNB(TreeNBStruct * tree,IndexType nparti /* leaf case */ if(cbranch->nparticles <= Nbucket){ - cbranch->big_particle=0; + cbranch->big_particle = 0; return; } @@ -917,7 +917,7 @@ void TreeSimple::_BuildTreeNB(TreeNBStruct * tree,IndexType nparti branch2.boundary_p1[j]=cbranch->boundary_p1[j]; branch2.boundary_p2[j]=cbranch->boundary_p2[j]; } - cbranch->big_particle=0; + cbranch->big_particle = 0; // **** makes sure force does not require nbucket at leaf From 816f050735a4788db6fd1f09b721b2a128e6ad20 Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Sat, 24 Nov 2018 17:52:50 +0100 Subject: [PATCH 083/227] Added some documentation. Added MakeParticleLenses::radialCut() and cylindricalCut() --- MultiPlane/particle_lens.cpp | 137 ++++++++++++++++++++++++++++------- include/particle_halo.h | 41 ++++++++++- 2 files changed, 151 insertions(+), 27 deletions(-) diff --git a/MultiPlane/particle_lens.cpp b/MultiPlane/particle_lens.cpp index f07c0367..11107816 100644 --- a/MultiPlane/particle_lens.cpp +++ b/MultiPlane/particle_lens.cpp @@ -23,7 +23,7 @@ MakeParticleLenses::MakeParticleLenses( const std::string &filename /// path / root name of gadget-2 snapshot ,SimFileFormat format ,int Nsmooth /// number of nearest neighbors used for smoothing - ,bool recenter /// recenter so that the LenHalos are centered on the center of mass + ,bool recenter /// recenter so that the LenHalos are centered on the center of mass of all the particles ):filename(filename),Nsmooth(Nsmooth) { @@ -67,25 +67,29 @@ MakeParticleLenses::MakeParticleLenses( } // find center of mass + cm[0]=cm[1]=cm[2]=0; + double m=0; + for(auto p : data){ + cm[0] += p[0]*p.Mass; + cm[1] += p[1]*p.Mass; + cm[2] += p[2]*p.Mass; + m += p.Mass; + } + cm /= m; + + // subtract center of mass if(recenter){ - double m=0; - Point_3d cm(0,0,0); - for(auto p : data){ - cm[0] += p[0]*p.Mass; - cm[1] += p[1]*p.Mass; - cm[2] += p[2]*p.Mass; - m += p.Mass; - } - cm /= m; - for(auto &p : data){ + for(auto &p : data){ p[0] -= cm[0]; p[1] -= cm[1]; p[2] -= cm[2]; } + + cm[0]=cm[1]=cm[2]=0; } } -MakeParticleLenses::MakeParticleLenses(const std::string &filename /// path / name of galmb file +MakeParticleLenses::MakeParticleLenses(const std::string &filename /// path / name of glmb file ,bool recenter /// recenter so that the LenHalos are centered on the center of mass ):filename(filename) { @@ -94,23 +98,26 @@ MakeParticleLenses::MakeParticleLenses(const std::string &filename /// path / n nparticles.resize(6,0); readSizesB(filename,data,Nsmooth,nparticles,z_original); - // find center of mass + cm[0]=cm[1]=cm[2]=0; + double m=0; + for(auto p : data){ + cm[0] += p[0]*p.Mass; + cm[1] += p[1]*p.Mass; + cm[2] += p[2]*p.Mass; + m += p.Mass; + } + cm /= m; + + // subtract center of mass if(recenter){ - double m=0; - Point_3d cm(0,0,0); - for(auto p : data){ - cm[0] += p[0]*p.Mass; - cm[1] += p[1]*p.Mass; - cm[2] += p[2]*p.Mass; - m += p.Mass; - } - cm /= m; for(auto &p : data){ p[0] -= cm[0]; p[1] -= cm[1]; p[2] -= cm[2]; } + + cm[0]=cm[1]=cm[2]=0; } } @@ -130,6 +137,12 @@ void MakeParticleLenses::CreateHalos(const COSMOLOGY &cosmo,double redshift){ p.Mass *= mass_unit; } + // remove halos if they already exist + while(halos.size() > 0){ + delete halos.back(); + halos.pop_back(); + } + // create halos ParticleType *pp; size_t skip = 0; @@ -306,9 +319,16 @@ bool MakeParticleLenses::readGadget2(){ gadget_file.closeFile(); } - // ????? **** convert to physical Mpc/h and Msun/h - // ???? *** can we store sizes in gadget blocks - // sort by type + double a = 1.0e-3/(1 + z_original); + // **** convert to physical Mpc/h and Msun/h + for(auto &p : data){ + p[0] *= a; + p[1] *= a; + p[2] *= a; + p.Mass *= 1.0e10; + } + + // sort by type std::sort(data.begin(),data.end(),[](ParticleType &a1,ParticleType &a2){return a1.type < a2.type;}); ParticleType *pp; @@ -331,3 +351,70 @@ bool MakeParticleLenses::readGadget2(){ }; +// remove particles that are beyond radius (Mpc/h) of center +void MakeParticleLenses::radialCut(Point_2d center,double radius){ + + double radius2 = radius*radius; + double r2; + auto end = data.end(); + auto it = data.begin(); + size_t ntot = data.size(); + + while(it != end){ + r2 = ( (*it)[0]-center[0] )*( (*it)[0]-center[0] ) + + ( (*it)[1]-center[1] )*( (*it)[1]-center[1] ) + +( (*it)[2]-center[2] )*( (*it)[2]-center[2] ); + if(r2 > radius2){ + --nparticles[(*it).type]; + --end; + --ntot; + std::swap(*it,*end); + }else ++it; + } + + data.resize(ntot); + /// resort by type + int ntypes = 0; + for(size_t n : nparticles){ + if(n > 0) ++ntypes; + } + + if(ntypes > 1){ + // sort by type + std::sort(data.begin(),data.end(),[](ParticleType &a1,ParticleType &a2){return a1.type < a2.type;}); + } +} + +// remove particles that are beyond cylindrical radius (Mpc/h) of center +void MakeParticleLenses::cylindricalCut(Point_2d center,double radius){ + + double radius2 = radius*radius; + double r2; + auto end = data.end(); + auto it = data.begin(); + size_t ntot = data.size(); + + while(it != end){ + r2 = ( (*it)[0]-center[0] )*( (*it)[0]-center[0] ) + + ( (*it)[1]-center[1] )*( (*it)[1]-center[1] ); + if(r2 > radius2){ + --nparticles[(*it).type]; + --end; + --ntot; + std::swap(*it,*end); + }else ++it; + } + + data.resize(ntot); + /// resort by type + int ntypes = 0; + for(size_t n : nparticles){ + if(n > 0) ++ntypes; + } + + if(ntypes > 1){ + // sort by type + std::sort(data.begin(),data.end(),[](ParticleType &a1,ParticleType &a2){return a1.type < a2.type;}); + } +} + diff --git a/include/particle_halo.h b/include/particle_halo.h index 92ff1c59..7aad8e10 100644 --- a/include/particle_halo.h +++ b/include/particle_halo.h @@ -660,10 +660,14 @@ void LensHaloParticles::makeSIE(

The particle data is stored in this structure so the LensHaloParticles should not be copied and then this object allowed to be destroyed. The halos will be destroyed when this structure is destroyed. - A separate LensHaloParticles is made for each type of particle that is present in the gadget file. The nearest N neighbour smoothing is done in 3D on construction separately for + A separate LensHaloParticles is made for each type of particle that is present in the gadget file. + The nearest N neighbour smoothing is done in 3D on construction separately for each type of particle. The smoothing sizes are automatically saved to files and used again if the class is constructed again with the same file and smoothing number. + On construction the LensHalos are not constructed. You nead to run the `CreateHalos()` method + the them to be created. + The data file formats are : gadget2 - standard Gadget-2 output. A different LensHalo is @@ -675,7 +679,7 @@ void LensHaloParticles::makeSIE( are the positions. Next columns are used for the other formats being and interpreted as (column 4) masses are in Msun/h, (column 5) the paricle smoothing size in Mpc/h and (column 6) an integer for type of particle. There can be more - columns in the file than are uesed. In the case of csv4, when there are more then one + columns in the file than are uesed. In the case of csv6, when there are more then one type of halo each type will be in a differeent LensHaloParticles with differnt smoothing. glmb - This is a binary format internal to GLAMER used to store @@ -689,6 +693,32 @@ void LensHaloParticles::makeSIE( in header at the top of the file. # is otherwise a comment character. Only one type of particle in a single input file. + + example of use: + +std::string filename = "DataFiles/snap_69.txt"; +MakeParticleLenses halomaker( + filename + ,csv6,30,false + ); + +double zs = 2,zl -0.7; + +long seed = 88277394; +Lens lens(&seed,zs); + +halomaker.CreateHalos(cosmo,zl); +for(auto h : halomaker.halos){ + lens.insertMainHalo(h,zl, true); +} + +Point_2d center; + +GridMap gridmap(&lens,2049,center.x,400*arcsecTOradians); + +PixelMap pmap = gridmap.writePixelMapUniform(KAPPA); +pmap.printFITS("!" + filename + ".kappa.fits"); + <\p> */ class MakeParticleLenses{ @@ -723,6 +753,13 @@ class MakeParticleLenses{ void CreateHalos(const COSMOLOGY &cosmo,double redshift); + /// remove particles that are beyond radius (Mpc/h) from center + void radialCut(Point_2d center,double radius); + /// remove particles that are beyond cylindrical radius (Mpc/h) of center + void cylindricalCut(Point_2d center,double radius); + + /// returns the center of mass of all the particles + Point_3d getCenterOfMass(){return cm;} private: std::vector > data; From 64c85e44c7a4f7394c315b58d9f324bce1bc4928 Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Sun, 25 Nov 2018 12:44:57 +0100 Subject: [PATCH 084/227] Work on MakeParticleLenses class. Added MakeParticleLenses::getBoundingBox() Updated example in comments --- MultiPlane/particle_lens.cpp | 26 ++++++++++---- include/particle_halo.h | 68 ++++++++++++++++++++++++++---------- 2 files changed, 70 insertions(+), 24 deletions(-) diff --git a/MultiPlane/particle_lens.cpp b/MultiPlane/particle_lens.cpp index 11107816..9107bece 100644 --- a/MultiPlane/particle_lens.cpp +++ b/MultiPlane/particle_lens.cpp @@ -70,9 +70,12 @@ MakeParticleLenses::MakeParticleLenses( cm[0]=cm[1]=cm[2]=0; double m=0; for(auto p : data){ - cm[0] += p[0]*p.Mass; - cm[1] += p[1]*p.Mass; - cm[2] += p[2]*p.Mass; + for(int i=0 ; i < 3 ; ++i){ + cm[i] += p[i]*p.Mass; + + if(bbox_ll[i] > p[i] ) bbox_ll[i] = p[i]; + if(bbox_ur[i] < p[i] ) bbox_ur[i] = p[i]; + } m += p.Mass; } cm /= m; @@ -85,6 +88,10 @@ MakeParticleLenses::MakeParticleLenses( p[2] -= cm[2]; } + for(int i=0 ; i < 3 ; ++i){ + bbox_ll[i] -= cm[i]; + bbox_ur[i] -= cm[i]; + } cm[0]=cm[1]=cm[2]=0; } } @@ -102,9 +109,12 @@ MakeParticleLenses::MakeParticleLenses(const std::string &filename /// path / n cm[0]=cm[1]=cm[2]=0; double m=0; for(auto p : data){ - cm[0] += p[0]*p.Mass; - cm[1] += p[1]*p.Mass; - cm[2] += p[2]*p.Mass; + for(int i=0 ; i < 3 ; ++i){ + cm[i] += p[i]*p.Mass; + + if(bbox_ll[i] > p[i] ) bbox_ll[i] = p[i]; + if(bbox_ur[i] < p[i] ) bbox_ur[i] = p[i]; + } m += p.Mass; } cm /= m; @@ -117,6 +127,10 @@ MakeParticleLenses::MakeParticleLenses(const std::string &filename /// path / n p[2] -= cm[2]; } + for(int i=0 ; i < 3 ; ++i){ + bbox_ll[i] -= cm[i]; + bbox_ur[i] -= cm[i]; + } cm[0]=cm[1]=cm[2]=0; } } diff --git a/include/particle_halo.h b/include/particle_halo.h index 7aad8e10..c5e5b671 100644 --- a/include/particle_halo.h +++ b/include/particle_halo.h @@ -696,28 +696,52 @@ void LensHaloParticles::makeSIE( example of use: -std::string filename = "DataFiles/snap_69.txt"; -MakeParticleLenses halomaker( - filename - ,csv6,30,false - ); - -double zs = 2,zl -0.7; + COSMOLOGY cosmo(Planck); + + double zs = 2,zl = 0.5; + double Dl = cosmo.coorDist(zl); + + + std::string filename = "DataFiles/snap_058_centered.txt"; + + MakeParticleLenses halomaker(filename,csv4,30,false); + + Point_3d Xmax,Xmin; -long seed = 88277394; -Lens lens(&seed,zs); + halomaker.getBoundingBox(Xmin, Xmax); + + Point_3d c_mass = halomaker.getCenterOfMass(); + Point_2d center; -halomaker.CreateHalos(cosmo,zl); -for(auto h : halomaker.halos){ - lens.insertMainHalo(h,zl, true); -} + center[0] = c_mass[0]; + center[1] = c_mass[1]; -Point_2d center; + // cut out a cylinder, could also do a ball + halomaker.cylindricalCut(center,(Xmax[0]-Xmin[0]/2)); + + long seed = 88277394; + Lens lens(&seed,zs); + + double range = (Xmax[0]-Xmin[0])*1.05*cosmo.getHubble()/Dl; // angular range of simulation -GridMap gridmap(&lens,2049,center.x,400*arcsecTOradians); + center *= cosmo.gethubble()/Dl; // convert to angular coordinates + + halomaker.CreateHalos(cosmo,zl); -PixelMap pmap = gridmap.writePixelMapUniform(KAPPA); -pmap.printFITS("!" + filename + ".kappa.fits"); + for(auto h : halomaker.halos){ + lens.insertMainHalo(h,zl, true); + } + + GridMap gridmap(&lens, 2049,center.x,range); + + PixelMap pmap = gridmap.writePixelMapUniform(KAPPA); + pmap.printFITS("!" + filename + ".kappa.fits"); + + pmap = gridmap.writePixelMapUniform(ALPHA1); + pmap.printFITS("!" + filename + ".alpha1.fits"); + + pmap = gridmap.writePixelMapUniform(ALPHA2); + pmap.printFITS("!" + filename + ".alpha2.fits"); <\p> */ @@ -759,13 +783,21 @@ class MakeParticleLenses{ void cylindricalCut(Point_2d center,double radius); /// returns the center of mass of all the particles - Point_3d getCenterOfMass(){return cm;} + Point_3d getCenterOfMass() const{return cm;} + /// return the maximum and minimum coordinates of the particles in each dimension in for the original simulation in Mpc/h + void getBoundingBox(Point_3d &Xmin,Point_3d &Xmax) const{ + Xmin = bbox_ll; + Xmax = bbox_ur; + } private: std::vector > data; const std::string filename; int Nsmooth; + Point_3d bbox_ll; // minumum coordinate values of particles + Point_3d bbox_ur; // maximim coordinate values of particles + double z_original; std::vector nparticles; std::vector masses; From 4e9ee9d0fe53965dc14bc296d76d7d00f7b52cc3 Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Sun, 25 Nov 2018 14:23:50 +0100 Subject: [PATCH 085/227] Fixed bug in comment. --- include/particle_halo.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/particle_halo.h b/include/particle_halo.h index c5e5b671..4697381e 100644 --- a/include/particle_halo.h +++ b/include/particle_halo.h @@ -722,7 +722,7 @@ void LensHaloParticles::makeSIE( long seed = 88277394; Lens lens(&seed,zs); - double range = (Xmax[0]-Xmin[0])*1.05*cosmo.getHubble()/Dl; // angular range of simulation + double range = (Xmax[0]-Xmin[0])*1.05*cosmo.gethubble()/Dl; // angular range of simulation center *= cosmo.gethubble()/Dl; // convert to angular coordinates From 866e22bbf1cbb5bf21eea2c31980f23ac97b39fd Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Mon, 26 Nov 2018 18:17:11 +0100 Subject: [PATCH 086/227] Fixed some errors in the comments. --- TreeCode_link/Tree.cpp | 9 ++++++++- TreeCode_link/gridmap.cpp | 2 +- include/grid_maintenance.h | 24 ++++++++++++++++-------- include/gridmap.h | 2 +- 4 files changed, 26 insertions(+), 11 deletions(-) diff --git a/TreeCode_link/Tree.cpp b/TreeCode_link/Tree.cpp index 97003e72..dc59e851 100644 --- a/TreeCode_link/Tree.cpp +++ b/TreeCode_link/Tree.cpp @@ -950,5 +950,12 @@ void ImageFinding::CriticalCurve::CausticRange(Point_2d &my_p1,Point_2d &my_p2){ } - +std::ostream &operator<<(std::ostream &os, ImageFinding::CriticalCurve &p) { + + // caustic_center,caustic_area,critical_center,critical_area,z_source,type + os << p.caustic_center << "," << p.caustic_area << "," << p.critical_center << "," + << p.critical_area << "," << p.z_source << ",'" << p.type; + + return os; +} diff --git a/TreeCode_link/gridmap.cpp b/TreeCode_link/gridmap.cpp index 1645c97c..fc82dd0a 100644 --- a/TreeCode_link/gridmap.cpp +++ b/TreeCode_link/gridmap.cpp @@ -403,7 +403,7 @@ void GridMap::xygridpoints(Point *i_points,PosType range,const PosType *center,l return; } -PosType GridMap::EisnsteinArea() const{ +PosType GridMap::EinsteinArea() const{ size_t count = 0; size_t N = Ngrid_init*Ngrid_init2; for(size_t i=0;i Date: Mon, 26 Nov 2018 21:37:28 +0100 Subject: [PATCH 087/227] Fixed bug where particles sizes were still calculated for data file formats csv5 and csv6. --- MultiPlane/particle_lens.cpp | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/MultiPlane/particle_lens.cpp b/MultiPlane/particle_lens.cpp index 9107bece..d8e45cf7 100644 --- a/MultiPlane/particle_lens.cpp +++ b/MultiPlane/particle_lens.cpp @@ -304,16 +304,18 @@ bool MakeParticleLenses::readCSV(int columns_used){ std::cout << ntot << " particle positions read from file " << filename << std::endl; - ParticleType *pp; - size_t skip = 0; - for(int i = 0 ; i < 6 ; ++i){ //loop through types - if(nparticles[i] > 0){ - pp = data.data() + skip; // pointer to first particle of type - size_t N = nparticles[i]; + if(columns_used < 5){ + ParticleType *pp; + size_t skip = 0; + for(int i = 0 ; i < 6 ; ++i){ //loop through types + if(nparticles[i] > 0){ + pp = data.data() + skip; // pointer to first particle of type + size_t N = nparticles[i]; - LensHaloParticles >::calculate_smoothing(Nsmooth,pp,N); + LensHaloParticles >::calculate_smoothing(Nsmooth,pp,N); + } + skip += nparticles[i]; } - skip += nparticles[i]; } //LensHaloParticles >::calculate_smoothing(Nsmooth,data.data(),data.size()); From be8e078636cc285dcfcb42a7057b4b5992548a8b Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Wed, 28 Nov 2018 17:20:01 +0100 Subject: [PATCH 088/227] Made ImageFinding::printCriticalCurves() to make printing caustic information easily. --- TreeCode_link/Tree.cpp | 15 +++++++++++++-- include/grid_maintenance.h | 6 +++++- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/TreeCode_link/Tree.cpp b/TreeCode_link/Tree.cpp index dc59e851..85a2e17a 100644 --- a/TreeCode_link/Tree.cpp +++ b/TreeCode_link/Tree.cpp @@ -949,12 +949,23 @@ void ImageFinding::CriticalCurve::CausticRange(Point_2d &my_p1,Point_2d &my_p2){ } } +void ImageFinding::printCriticalCurves(std::string filename + ,const std::vector &critcurves){ + + filename = filename + ".csv"; + std::ofstream myfile(filename); + myfile << "caustic_center,caustic_area,critical_center,critical_area,z_source,type" + << std::endl; + for(auto cr : critcurves){ + myfile << cr << std::endl; + } +} -std::ostream &operator<<(std::ostream &os, ImageFinding::CriticalCurve &p) { +std::ostream &operator<<(std::ostream &os, const ImageFinding::CriticalCurve &p) { // caustic_center,caustic_area,critical_center,critical_area,z_source,type os << p.caustic_center << "," << p.caustic_area << "," << p.critical_center << "," - << p.critical_area << "," << p.z_source << ",'" << p.type; + << p.critical_area << "," << p.z_source << "," << p.type; return os; } diff --git a/include/grid_maintenance.h b/include/grid_maintenance.h index 62b0359b..b3f7c350 100644 --- a/include/grid_maintenance.h +++ b/include/grid_maintenance.h @@ -382,9 +382,13 @@ namespace ImageFinding{ void sort_out_points(Point *i_points,ImageInfo *imageinfo,double r_source,double y_source[]); } + + void printCriticalCurves(std::string filename + ,const std::vector &critcurves); } -std::ostream &operator<<(std::ostream &os, ImageFinding::CriticalCurve &p); + +std::ostream &operator<<(std::ostream &os, const ImageFinding::CriticalCurve &p); void saveImage(LensHaloMassMap *mokahalo, GridHndl grid, bool saveprofile=true); From 9f110bc046abfadc63e46cce01509dae486feed8 Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Fri, 30 Nov 2018 15:50:52 +0100 Subject: [PATCH 089/227] Added galaxies to field halos in automatic light cones. --- MultiPlane/lens.cpp | 19 ++++++++++++++----- MultiPlane/particle_lens.cpp | 2 ++ include/lens.h | 1 + include/particle_halo.h | 2 +- 4 files changed, 18 insertions(+), 6 deletions(-) diff --git a/MultiPlane/lens.cpp b/MultiPlane/lens.cpp index 70df8f0b..2b775d6b 100644 --- a/MultiPlane/lens.cpp +++ b/MultiPlane/lens.cpp @@ -1627,7 +1627,7 @@ void Lens::ComputeHalosDistributionVariables () */ void Lens::createFieldHalos(bool verbose,DM_Light_Division division_mode) { - std::cout << "Creating Field Halos from Mass Function" << std::endl; + //std::cout << "Creating Field Halos from Mass Function" << std::endl; //verbose = true; unsigned long i,j_max,k1,k2; @@ -1641,7 +1641,8 @@ void Lens::createFieldHalos(bool verbose,DM_Light_Division division_mode) size_t haloid=0; if (field_min_mass < 1.0e5) { - std::cout << "Are you sure you want the minimum field halo mass to be " << field_min_mass << " Msun?" << std::endl; + std::cout << "Are you sure you want the minimum field halo mass to be " + << field_min_mass << " Msun?" << std::endl; throw; } @@ -1652,6 +1653,7 @@ void Lens::createFieldHalos(bool verbose,DM_Light_Division division_mode) int Nhalos = static_cast(poidev(float(aveNhalosField), seed)); + std::cout << "Creating " << Nhalos << " field halos from mass function." << std::endl; for(int i=0;i < Nhalos;++i){ halo_zs_vec.push_back(Utilities::InterpolateYvec(NhalosbinZ,zbins,ran2(seed))); } @@ -2979,6 +2981,7 @@ void Lens::GenerateFieldHalos(double min_mass ,double field_of_view ,int Nplanes ,LensHaloType halo_type + ,GalaxyLensHaloType galaxy_type ,bool verbose ){ @@ -2993,10 +2996,16 @@ void Lens::GenerateFieldHalos(double min_mass field_buffer = 0.0; mass_func_PL_slope = 0; - flag_field_gal_on = false; + field_int_prof_type = halo_type; - field_int_prof_gal_type = nsie_gal; - + if(galaxy_type == null_gal){ + flag_field_gal_on = false; + field_int_prof_gal_type = nsie_gal; + }else{ + flag_field_gal_on = true; + field_int_prof_gal_type = galaxy_type; + } + NhalosbinZ.resize(Nzbins); Nhaloestot_Tab.resize(NZSamples); ComputeHalosDistributionVariables(); diff --git a/MultiPlane/particle_lens.cpp b/MultiPlane/particle_lens.cpp index d8e45cf7..8c36525d 100644 --- a/MultiPlane/particle_lens.cpp +++ b/MultiPlane/particle_lens.cpp @@ -305,6 +305,8 @@ bool MakeParticleLenses::readCSV(int columns_used){ std::cout << ntot << " particle positions read from file " << filename << std::endl; if(columns_used < 5){ + + // find sizes ParticleType *pp; size_t skip = 0; for(int i = 0 ; i < 6 ; ++i){ //loop through types diff --git a/include/lens.h b/include/lens.h index ff8a485d..88f86881 100644 --- a/include/lens.h +++ b/include/lens.h @@ -243,6 +243,7 @@ class Lens ,double field_of_view /// in square degrees ,int Nplanes /// number of lens planes ,LensHaloType halo_type = nfw_lens /// type of halo + ,GalaxyLensHaloType galaxy_type = null_gal /// type of galaxy, if null_gal no galaxy ,bool verbose = false ); diff --git a/include/particle_halo.h b/include/particle_halo.h index 845652fb..8b1d7613 100644 --- a/include/particle_halo.h +++ b/include/particle_halo.h @@ -557,7 +557,7 @@ void LensHaloParticles::calculate_smoothing(int Nsmooth,PType *pp } }else{ size_t chunksize = Npoints/nthreads; - std::thread thr[nthreads]; + std::vector thr(nthreads); size_t N; for(int ii = 0; ii < nthreads ;++ii){ From d4d3b0eb63b21e96d0f7daaf4294fe463ab7b8eb Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Fri, 30 Nov 2018 17:13:14 +0100 Subject: [PATCH 090/227] A few messages when reading files --- MultiPlane/particle_lens.cpp | 61 ++++++++++++++++++++++++++++++++++++ include/particle_halo.h | 4 +++ 2 files changed, 65 insertions(+) diff --git a/MultiPlane/particle_lens.cpp b/MultiPlane/particle_lens.cpp index d8e45cf7..d90d4d25 100644 --- a/MultiPlane/particle_lens.cpp +++ b/MultiPlane/particle_lens.cpp @@ -8,6 +8,9 @@ #include "utilities_slsim.h" #include "gadget.hh" +#ifdef ENABLE_HDF5 +#include "H5Cpp.h" +#endif #ifdef ENABLE_FITS #include @@ -258,6 +261,7 @@ bool MakeParticleLenses::readCSV(int columns_used){ nparticles = {ntot,0,0,0,0,0}; masses = {0,0,0,0,0,0}; }else if(columns_used == 5){ + std::cout << "Using the particle sizes from " << filename << std::endl; do{ std::vector vec; Utilities::splitstring(line,vec,delimiter); @@ -273,6 +277,9 @@ bool MakeParticleLenses::readCSV(int columns_used){ nparticles = {ntot,0,0,0,0,0}; masses = {0,0,0,0,0,0}; }else if(columns_used == 6){ + std::cout << "Using the particle sizes from " << filename << std::endl; + std::cout << "Using the particle type information from " << filename << std::endl; + do{ std::vector vec; Utilities::splitstring(line,vec,delimiter); @@ -323,6 +330,8 @@ bool MakeParticleLenses::readCSV(int columns_used){ return true; }; + + bool MakeParticleLenses::readGadget2(){ GadgetFile > gadget_file(filename,data); @@ -366,7 +375,59 @@ bool MakeParticleLenses::readGadget2(){ return true; }; +/* +#ifdef ENABLE_HDF5 +bool MakeParticleLenses::readHDF5(){ + + H5::H5File file(filename.c_str(), H5F_ACC_RDONLY ); + + std::vector sets = {"MASS","TYPE"}; + + for(auto set : sets){ + + H5::DataSet dataset = file.openDataSet(set.c_str()); + H5T_class_t type_class = dataset.getTypeClass(); + // Get class of datatype and print message if it's an integer. + if( type_class == H5T_INTEGER ) + { + cout << "Data set has INTEGER type" << endl; + //Get the integer datatype + H5::IntType intype = dataset.getIntType(); + // Get order of datatype and print message if it's a little endian. + H5std_string order_string; + H5T_order_t order = intype.getOrder( order_string ); + cout << order_string << endl; + + // Get size of the data element stored in file and print it. + size_t size = intype.getSize(); + cout << "Data size is " << size << endl; + }else if(type_class == H5T_FLOAT ){ + cout << "Data set has FLOAT type" << endl; + //Get the integer datatype + H5::FloatType intype = dataset.getFloatType(); + // Get order of datatype and print message if it's a little endian. + H5std_string order_string; + H5T_order_t order = intype.getOrder( order_string ); + cout << order_string << endl; + + // Get size of the data element stored in file and print it. + size_t size = intype.getSize(); + cout << "Data size is " << size << endl; + } + + // Get dataspace of the dataset. + + H5::DataSpace dataspace = dataset.getSpace(); + + // Get the number of dimensions in the dataspace. + int rank = dataspace.getSimpleExtentNdims(); + } + return true; +}; + +#endif +*/ // remove particles that are beyond radius (Mpc/h) of center void MakeParticleLenses::radialCut(Point_2d center,double radius){ diff --git a/include/particle_halo.h b/include/particle_halo.h index 845652fb..ec7741a8 100644 --- a/include/particle_halo.h +++ b/include/particle_halo.h @@ -848,6 +848,10 @@ class MakeParticleLenses{ // Reads particles from first 4 columns of csv file bool readCSV(int columns_used); +#ifdef ENABLE_HDF5 + bool readHDF5(); +#endif + }; From fdb89b6ff51586f6df78cba2bd06b1868f12a32b Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Tue, 4 Dec 2018 10:08:56 +0100 Subject: [PATCH 091/227] Fixed a possible bug with reconstructing the tree after rotation a LensHaloParticle. Added buffer to light cone. --- MultiPlane/lens.cpp | 6 ++++-- include/lens.h | 13 +++++++------ include/particle_halo.h | 11 ++++++----- 3 files changed, 17 insertions(+), 13 deletions(-) diff --git a/MultiPlane/lens.cpp b/MultiPlane/lens.cpp index 2b775d6b..a07c15d8 100644 --- a/MultiPlane/lens.cpp +++ b/MultiPlane/lens.cpp @@ -2982,8 +2982,10 @@ void Lens::GenerateFieldHalos(double min_mass ,int Nplanes ,LensHaloType halo_type ,GalaxyLensHaloType galaxy_type + ,double buffer ,bool verbose - ){ + ) +{ if(field_halos.size() >0 ){ std::cerr << "Lens:GenerateField() cannot be used in the lens already has field halos." << std::endl; @@ -2993,7 +2995,7 @@ void Lens::GenerateFieldHalos(double min_mass fieldofview = field_of_view; field_mass_func_type = mass_function; - field_buffer = 0.0; + field_buffer = buffer; mass_func_PL_slope = 0; diff --git a/include/lens.h b/include/lens.h index 88f86881..6edd748c 100644 --- a/include/lens.h +++ b/include/lens.h @@ -239,12 +239,13 @@ class Lens The cone is filled up until the redshift of the current zsource that is stored in the Lens class. The field is a circular on the sky. There is no clustering of the halos. */ void GenerateFieldHalos(double min_mass /// minimum mass of halos - ,MassFuncType mass_function /// type of mass function - ,double field_of_view /// in square degrees - ,int Nplanes /// number of lens planes - ,LensHaloType halo_type = nfw_lens /// type of halo - ,GalaxyLensHaloType galaxy_type = null_gal /// type of galaxy, if null_gal no galaxy - ,bool verbose = false + ,MassFuncType mass_function /// type of mass function + ,double field_of_view /// in square degrees + ,int Nplanes /// number of lens planes + ,LensHaloType halo_type = nfw_lens /// type of halo + ,GalaxyLensHaloType galaxy_type = null_gal /// type of galaxy, if null_gal no galaxy + ,double buffer = 1.0 /// buffer in Mpc for cone + ,bool verbose = false ); protected: diff --git a/include/particle_halo.h b/include/particle_halo.h index 8b1d7613..cdb05573 100644 --- a/include/particle_halo.h +++ b/include/particle_halo.h @@ -234,7 +234,7 @@ LensHaloParticles::LensHaloParticles( ,Point_2d theta_rotate /// rotation of particles around the origin ,bool recenter /// center on center of mass ,float MinPSize /// minimum particle size -):min_size(MinPSize),multimass(true),Npoints(Nparticles),pp(pdata) +):pp(pdata),min_size(MinPSize),multimass(true),Npoints(Nparticles) { LensHalo::setZlens(redshift); @@ -313,7 +313,8 @@ template void LensHaloParticles::rotate(Point_2d theta){ rotate_particles(theta[0],theta[1]); delete qtree; - qtree =new TreeQuadParticles >(pp,Npoints,multimass,true,0,20); + //qtree = new TreeQuadParticles >(pp,Npoints,multimass,true,0,20); + qtree = new TreeQuadParticles >(pp,Npoints,-1,-1,0,20); } /** \brief Reads number of particle and particle positons into Npoint and xp from a ASCII file. @@ -527,9 +528,9 @@ void LensHaloParticles::rotate_particles(PosType theta_x,PosType theta_y) for(size_t i=0;i Date: Wed, 5 Dec 2018 12:09:22 +0100 Subject: [PATCH 092/227] Added ImageFinding::mapCriticalCurves() and ImageFinding::mapCausticCurves(). That give PixelMaps of caustic / critical curve structure. Made some changes to have setTheta(0 take Point_2d --- TreeCode_link/Tree.cpp | 72 ++++++++++++++++++++++++++++++++++++++ include/geometry.h | 6 ++-- include/grid_maintenance.h | 19 +++++++++- include/image_processing.h | 6 +++- include/lens_halos.h | 2 ++ include/overzier_source.h | 4 +++ include/particle_halo.h | 3 +- include/source.h | 7 ++-- include/sourceAnaGalaxy.h | 4 ++- 9 files changed, 113 insertions(+), 10 deletions(-) diff --git a/TreeCode_link/Tree.cpp b/TreeCode_link/Tree.cpp index 85a2e17a..8d389dc6 100644 --- a/TreeCode_link/Tree.cpp +++ b/TreeCode_link/Tree.cpp @@ -970,3 +970,75 @@ std::ostream &operator<<(std::ostream &os, const ImageFinding::CriticalCurve &p) return os; } +PixelMap ImageFinding::mapCriticalCurves(const std::vector &critcurves + ,int Nx) { + + int i_max = 0,i=0; + double area = critcurves[0].critical_area,rmax,rmin,rave; + Point_2d p1,p2,p_min,p_max; + p1 = p2 = p_min = p_max = critcurves[0].critical_center; + + for(auto c : critcurves){ + if(c.critical_area > area){ + i_max = i; + area = c.critical_area; + } + c.CritRange(p1,p2); + + if(p1[0] < p_min[0]) p_min[0] = p1[0]; + if(p1[1] < p_min[1]) p_min[1] = p1[1]; + + if(p2[0] > p_max[0]) p_max[0] = p2[0]; + if(p2[1] > p_max[1]) p_max[1] = p2[1]; + + ++i; + } + + Point_2d center = (p_max + p_min)/2; + double range = 1.05*MAX(p_max[0]-p_min[0],p_max[1]-p_min[1]); + + PixelMap map(center.x,Nx,range/Nx); + + for(auto c : critcurves){ + map.AddCurve(c.critical_curve, c.type + 1); + } + + return map; +} + +PixelMap ImageFinding::mapCausticCurves(const std::vector &critcurves + ,int Nx) { + + int i_max = 0,i=0; + double area = critcurves[0].caustic_area; + Point_2d p1,p2,p_min,p_max; + p1 = p2 = p_min = p_max = critcurves[0].caustic_center; + + for(auto c : critcurves){ + if(c.caustic_area > area){ + i_max = i; + area = c.caustic_area; + } + c.CausticRange(p1,p2); + + if(p1[0] < p_min[0]) p_min[0] = p1[0]; + if(p1[1] < p_min[1]) p_min[1] = p1[1]; + + if(p2[0] > p_max[0]) p_max[0] = p2[0]; + if(p2[1] > p_max[1]) p_max[1] = p2[1]; + + ++i; + } + + Point_2d center = (p_max + p_min)/2; + double range = 1.05*MAX(p_max[0]-p_min[0],p_max[1]-p_min[1]); + + PixelMap map(center.x,Nx,range/Nx); + + for(auto c : critcurves){ + map.AddCurve(c.caustic_curve_intersecting, c.type + 1); + } + + return map; +} + diff --git a/include/geometry.h b/include/geometry.h index c2f0bdb0..a77e3476 100644 --- a/include/geometry.h +++ b/include/geometry.h @@ -247,15 +247,15 @@ namespace Utilities { /// the components of the Quaternion double v[4]; - /// returns the rotation Quaternion for a ratation around the x-axis + /// returns the rotation Quaternion for a rotation around the x-axis static Quaternion q_x_rotation(double theta){ return Quaternion(cos(theta/2),sin(theta/2),0,0); } - /// returns the rotation Quaternion for a ratation around the y-axis + /// returns the rotation Quaternion for a rotation around the y-axis static Quaternion q_y_rotation(double theta){ return Quaternion(cos(theta/2),0,-sin(theta/2),0); } - /// returns the rotation Quaternion for a ratation around the z-axis + /// returns the rotation Quaternion for a rotation around the z-axis static Quaternion q_z_rotation(double theta){ return Quaternion(cos(theta/2),0,0,sin(theta/2)); } diff --git a/include/grid_maintenance.h b/include/grid_maintenance.h index b3f7c350..c6c4e303 100644 --- a/include/grid_maintenance.h +++ b/include/grid_maintenance.h @@ -384,7 +384,24 @@ namespace ImageFinding{ } void printCriticalCurves(std::string filename - ,const std::vector &critcurves); + ,const std::vector &critcurves); + + /** \breaf Makes an image of the critical curves. The map will encompose all curves found. The + pixel values are the caustic type + 1 ( 2=radial,3=tangential,4=pseudo ) + */ + PixelMap mapCriticalCurves( + /// list of critical curves + const std::vector &critcurves, + /// number of pixels to each size + int Nx + ); + + /** \breaf Makes an image of the caustic curves. The map will encompose all curves found. The + pixel values are the caustic type + 1 ( 2=radial,3=tangential,4=pseudo ) + */ + PixelMap mapCausticCurves(const std::vector &critcurves /// list of critical curves + ,int Nx /// number of pixels to each size + ); } diff --git a/include/image_processing.h b/include/image_processing.h index d4fbbdcd..6a2db6cc 100644 --- a/include/image_processing.h +++ b/include/image_processing.h @@ -51,8 +51,12 @@ class PixelMap inline std::size_t getNy() const { return Ny; } inline double getRangeX() const { return rangeX; } inline double getRangeY() const { return rangeY; } - inline const double* getCenter() const{ return center; } + //inline double* getCenter() const{ return ¢er[0]; } void const getCenter(Point_2d &c) const{ c[0]=center[0]; c[1]=center[1];} + Point_2d getCenter() const{ + Point_2d c(center[0],center[1]); + return c; + } inline double getResolution() const { return resolution; } void Clean(); diff --git a/include/lens_halos.h b/include/lens_halos.h index f32ced83..843e2b23 100644 --- a/include/lens_halos.h +++ b/include/lens_halos.h @@ -80,6 +80,8 @@ class LensHalo{ void setTheta(PosType PosX, PosType PosY) { posHalo[0] = PosX ; posHalo[1] = PosY ; } /// set the position of the Halo in radians void setTheta(PosType *PosXY) { posHalo[0] = PosXY[0] ; posHalo[1] = PosXY[1] ; } + /// set the position of the Halo in radians + void setTheta(const Point_2d &p) { posHalo[0] = p[0] ; posHalo[1] = p[1] ; } /// get the position of the Halo in radians void getTheta(PosType * MyPosHalo) const { MyPosHalo[0] = posHalo[0] ; MyPosHalo[1] = posHalo[1]; } diff --git a/include/overzier_source.h b/include/overzier_source.h index 34b0dd6d..d2fb35f6 100644 --- a/include/overzier_source.h +++ b/include/overzier_source.h @@ -159,6 +159,10 @@ class SourceOverzierPlus : public SourceOverzier source_x[1] = my_y; spheroid.setTheta(my_x,my_y); } + virtual void setTheta(const Point_2d &p){ + source_x = p; + spheroid.setTheta(p[0],p[1]); + } void setBulgeAxisRatio(PosType q){ spheroid.setAxesRatio(q); } diff --git a/include/particle_halo.h b/include/particle_halo.h index cdb05573..e206b2f3 100644 --- a/include/particle_halo.h +++ b/include/particle_halo.h @@ -783,8 +783,9 @@ class MakeParticleLenses{ /// remove particles that are beyond cylindrical radius (Mpc/h) of center void cylindricalCut(Point_2d center,double radius); - /// returns the center of mass of all the particles + /// returns the original center of mass of all the particles Point_3d getCenterOfMass() const{return cm;} + /// return the maximum and minimum coordinates of the particles in each dimension in for the original simulation in Mpc/h void getBoundingBox(Point_3d &Xmin,Point_3d &Xmax) const{ Xmin = bbox_ll; diff --git a/include/source.h b/include/source.h index af1edb8c..d54d2820 100644 --- a/include/source.h +++ b/include/source.h @@ -75,9 +75,10 @@ class Source virtual inline void getTheta(Point_2d &x) const {x = source_x;} /// Reset the position of the source in radians - virtual inline void setTheta(PosType *xx){source_x[0] = xx[0]; source_x[1] = xx[1];} - virtual void setTheta(PosType my_x,PosType my_y){source_x[0] = my_x; source_x[1] = my_y;} - + virtual void setTheta(PosType *xx){source_x[0] = xx[0]; source_x[1] = xx[1];} + virtual void setTheta(PosType my_x,PosType my_y){source_x[0] = my_x; source_x[1] = my_y;} + virtual void setTheta(const Point_2d &p){source_x = p;} + /// In the case of a single plane lens, the ratio of angular size distances virtual inline PosType getDlDs(){return DlDs;} //TODO: BEN I think this need only be in the BLR source models diff --git a/include/sourceAnaGalaxy.h b/include/sourceAnaGalaxy.h index 883011f9..65f49558 100644 --- a/include/sourceAnaGalaxy.h +++ b/include/sourceAnaGalaxy.h @@ -96,7 +96,8 @@ class SourceMultiAnaGalaxy: public Source{ Point_2d getTheta(){return galaxies[index].getTheta();} /// Set angular position of current source. void setTheta(PosType my_theta[2]){galaxies[index].setTheta(my_theta);} - void setTheta(PosType my_x,PosType my_y){galaxies[index].setTheta(my_x, my_y);} + void setTheta(PosType my_x,PosType my_y){galaxies[index].setTheta(my_x, my_y);} + void setTheta(const Point_2d &p){galaxies[index].setTheta(p);} std::size_t getNumberOfGalaxies() const {return galaxies.size();} @@ -206,6 +207,7 @@ class SourceMultiShapelets: public Source{ /// Set angular position of current source. void setTheta(PosType my_theta[2]){galaxies[index].setTheta(my_theta);} void setTheta(PosType my_x,PosType my_y){galaxies[index].setTheta(my_x, my_y);} + void setTheta(const Point_2d &p){galaxies[index].setTheta(p);} /// Return redshift of current source. PosType getZ() const {return galaxies[index].getZ();} From 626d2f894f1e53afc6defd95ed2611ecc513e309 Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Thu, 6 Dec 2018 20:18:30 +0100 Subject: [PATCH 093/227] Fix bug with ordering of caustic coming out of find_crit() --- include/concave_hull.h | 331 +++++++++++++++++++++++++++++++++++------ 1 file changed, 282 insertions(+), 49 deletions(-) diff --git a/include/concave_hull.h b/include/concave_hull.h index fa7a23aa..d96433f6 100644 --- a/include/concave_hull.h +++ b/include/concave_hull.h @@ -104,7 +104,68 @@ namespace Utilities{ return; } -/** \brief Creates the concave hull of a group of 2 dimensional points + + /// Returns a vector of points on the convex hull in counter-clockwise order. + template + void convex_hull(std::vector &P,std::vector &hull_index) + { + + size_t n = P.size(); + + if(n <= 3){ + hull_index.resize(n); + size_t i = 0; + for(size_t &d : hull_index) d = i++; + return; + } + + std::vector hull(n + 1); + hull_index.resize(n + 1); + + size_t k = 0; + + // Sort points lexicographically + std::sort(P.begin(), P.end(), + [](T p1,T p2){ + if(p1[0]==p2[0]) return p1[1] < p2[1]; + return p1[0] < p2[0];}); + + + // Build lower hull + for (size_t i = 0; i < n; i++) { + while (k >= 2 && crossD(hull[k-2], hull[k-1], P[i]) <= 0){ + k--; + } + hull_index[k] = i; + hull[k++] = P[i]; + } + + // Build upper hull + for (long i = n-2, t = k+1; i >= 0; i--) { + while (k >= t && crossD(hull[k-2], hull[k-1], P[i]) <= 0){ + k--; + assert(k > 1); + } + hull_index[k] = i; + hull[k++] = P[i]; + } + + hull_index.resize(k); + hull_index.pop_back(); + + hull.resize(k); + hull.pop_back(); + + return; + } + + + struct Edge{ + Edge(size_t i,double l):length(l),index(i){}; + double length = 0; + size_t index = 0; + }; + /** \brief Creates the concave hull of a group of 2 dimensional points by the shrink-wrap algorithm. The type of the input vector points must have an operator []. @@ -135,19 +196,19 @@ void concave(std::vector &init_points if(init_points.size() == hull.size()) return; - std::list> index_length; - + std::list edges; std::list leftovers; hull.push_back(hull[0]); double tmp; + // find pairs of hull points that are further appart than scale for(int i=0;i scale) index_length.push_back(std::pair(i,tmp)); + if(tmp > scale) edges.emplace_back(i,tmp);// .push_back(std::pair(i,tmp)); } - if(index_length.size() == 0) return; + if(edges.size() == 0) return; if(TEST){ PosType cent[] ={0.5,0.5}; @@ -158,16 +219,12 @@ void concave(std::vector &init_points map.drawPoints(init_points,0.01,1.0); map.drawCurve(hull,2); - map.printFITS("!test_concave.fits"); } - - - // sort edges by length - index_length.sort([](const std::pair &p1,const std::pair &p2){return p1.second > p2.second;}); - assert(index_length.front().second >= index_length.back().second); + edges.sort([](const Edge &p1,const Edge &p2){return p1.length > p2.length;}); + assert(edges.front().length >= edges.back().length); { // make a list of points that are not already in the convex hull @@ -199,37 +256,42 @@ void concave(std::vector &init_points typename std::list::iterator p,nextpoint; //auto p = leftovers.begin(); - double minarea,area,co; + double minarea,area,co1,co2; size_t i; - Point_2d vo,v1; + Point_2d vo,v1,v2; - while(index_length.front().second > scale){ + int count = 0; + while(edges.front().length > scale ){ if(leftovers.size() == 0) break; // find the point which if added to this edge would change the area least minarea = HUGE_VAL; - i = index_length.front().first; + i = edges.front().index; for(p = leftovers.begin() ; p != leftovers.end() ; ++p){ - vo = subtract(hull[i+1], hull[i]); v1 = subtract(*p,hull[i]); - - area = vo^v1; - co = v1*vo; - - if(co > 0 && area > 0 && index_length.front().second > v1.length()){ - if(area < minarea){ - minarea = area; - nextpoint = p; + + if( edges.front().length > v1.length()){ + + vo = subtract(hull[i+1], hull[i]); + //v2 = subtract(*p,hull[i+1]); + area = vo^v1; + co1 = v1*vo; + //co2 = v2*vo; + + if( co1 > 0 && (area > 0)*(area < minarea) ){ +// if(co1 > 0 && area > 0 && index_length.front().second > v1.length()){ + minarea = area; + nextpoint = p; } } } if(minarea == HUGE_VAL){ // if there is no acceptable point for this edge continue with the second longest edge - index_length.pop_front(); + edges.pop_front(); continue; } @@ -239,30 +301,34 @@ void concave(std::vector &init_points hull.insert(hull.begin() + i + 1,tmp); // update index_length list - std::pair new1 = index_length.front(); - index_length.pop_front(); - new1.second = (subtract(hull[i],hull[i+1])).length(); - std::pair new2(i+1,(subtract(hull[i+1],hull[i+2])).length()); - - for(auto &p : index_length) if(p.first > i) ++(p.first); - - auto p = index_length.begin(); - for(; p != index_length.end() ; ++p){ - if((*p).second < new1.second){ - index_length.insert(p,new1); - break; + Edge new1 = edges.front(); + edges.pop_front(); + new1.length = (subtract(hull[i],hull[i+1])).length(); + Edge new2(i+1,(subtract(hull[i+1],hull[i+2])).length()); + + for(auto &p : edges) if(p.index > i) ++(p.index); + + auto p = edges.begin(); + if(new1.length > scale){ + for(p = edges.begin() ; p != edges.end() ; ++p){ + if((*p).length < new1.length){ + edges.insert(p,new1); + break; + } } + if(p==edges.end()) edges.push_back(new1); } - if(p==index_length.end()) index_length.push_back(new1); - - for(p = index_length.begin(); p != index_length.end() ; ++p){ - if((*p).second < new2.second){ - index_length.insert(p,new2); - break; - } + if(new2.length > scale){ + for(p = edges.begin(); p != edges.end() ; ++p){ + if((*p).length < new2.length){ + edges.insert(p,new2); + break; + } + } + if(p==edges.end()) edges.push_back(new2); } - if(p==index_length.end()) index_length.push_back(new2); - + + if(TEST){ PosType cent[] ={0.5,0.5}; PixelMap map(cent,256, 1.0/256); @@ -274,17 +340,184 @@ void concave(std::vector &init_points map.printFITS("!test_concave.fits"); } - - } + } hull.pop_back(); - Utilities::RemoveIntersections(hull); + //Utilities::RemoveIntersections(hull); std::swap(hull,hull_out); return; } + template + std::vector concave2(std::vector &init_points,double scale) + { + + //typedef typename InputIt::value_type point; + + bool TEST = false; + + std::vector hull; + + // find the convex hull + convex_hull(init_points,hull); + + if(init_points.size() == hull.size()) return; + + std::list edges; + std::list leftovers; + hull.push_back(hull[0]); + + double tmp; + + // find pairs of hull points that are further appart than scale + for(int i=0;i scale) edges.emplace_back(i,tmp);// .push_back(std::pair(i,tmp)); + } + + if(edges.size() == 0) return; + + if(TEST){ + PosType cent[] ={0.5,0.5}; + PixelMap map(cent,256, 1.0/256); + + map.drawgrid(10,0.5); + + map.drawPoints(init_points,0.01,1.0); + map.drawCurve(hull,2); + + map.printFITS("!test_concave.fits"); + } + + // sort edges by length + edges.sort([](const Edge &p1,const Edge &p2){return p1.length > p2.length;}); + assert(edges.front().length >= edges.back().length); + + + { // make a list of points that are not already in the convex hull + + hull.pop_back(); + std::vector index(hull.size()); + size_t i = 0; + for(size_t &ind: index) ind = i++; + std::sort(index.begin(),index.end(), + [&hull](size_t p1,size_t p2){ + if(hull[p1][0] == hull[p2][0]) return hull[p1][1] < hull[p2][1]; + return hull[p1][0] < hull[p2][0];}); + + size_t j = 0; + + for(auto pit = init_points.begin(); pit != init_points.end() ; ++pit){ + + if((hull[index[j]][0] == (*pit)[0])*(hull[index[j]][1] == (*pit)[1])){ + ++j; + }else{ + leftovers.push_back(*pit); + } + + } + assert(hull.size() + leftovers.size() == init_points.size()); + hull.push_back(hull[0]); + } + + typename std::list::iterator p,nextpoint; + //auto p = leftovers.begin(); + + double minarea,area,co1,co2; + size_t i; + Point_2d vo,v1,v2; + + while(edges.front().length > scale ){ + + if(leftovers.size() == 0) break; + + // find the point which if added to this edge would change the area least + minarea = HUGE_VAL; + i = edges.front().index; + + for(p = leftovers.begin() ; p != leftovers.end() ; ++p){ + + v1 = subtract(*p,hull[i]); + + if( edges.front().length > v1.length()){ + + vo = subtract(hull[i+1], hull[i]); + //v2 = subtract(*p,hull[i+1]); + area = vo^v1; + co1 = v1*vo; + //co2 = v2*vo; + + if( co1 > 0 && (area > 0)*(area < minarea) ){ + // if(co1 > 0 && area > 0 && index_length.front().second > v1.length()){ + minarea = area; + nextpoint = p; + } + } + } + + if(minarea == HUGE_VAL){ + // if there is no acceptable point for this edge continue with the second longest edge + edges.pop_front(); + continue; + } + + // insert new point into hull + T tmp = *nextpoint; + leftovers.erase(nextpoint); + hull.insert(hull.begin() + i + 1,tmp); + + // update index_length list + Edge new1 = edges.front(); + edges.pop_front(); + new1.length = (subtract(hull[i],hull[i+1])).length(); + Edge new2(i+1,(subtract(hull[i+1],hull[i+2])).length()); + + for(auto &p : edges) if(p.index > i) ++(p.index); + + auto p = edges.begin(); + if(new1.length > scale){ + for(p = edges.begin() ; p != edges.end() ; ++p){ + if((*p).length < new1.length){ + edges.insert(p,new1); + break; + } + } + if(p==edges.end()) edges.push_back(new1); + } + if(new2.length > scale){ + for(p = edges.begin(); p != edges.end() ; ++p){ + if((*p).length < new2.length){ + edges.insert(p,new2); + break; + } + } + if(p==edges.end()) edges.push_back(new2); + } + + + if(TEST){ + PosType cent[] ={0.5,0.5}; + PixelMap map(cent,256, 1.0/256); + + map.drawgrid(10,0.5); + + map.drawPoints(init_points,0.03,1.0); + map.drawCurve(hull,2); + + map.printFITS("!test_concave.fits"); + } + } + + hull.pop_back(); + + //Utilities::RemoveIntersections(hull); + + return hull; + } + + } #endif /* concave_hull_h */ From 76f3613a1777b059f9917fd6c1f5d70d9e431e8c Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Sat, 8 Dec 2018 14:25:03 +0100 Subject: [PATCH 094/227] Fixed bug in ordering of points in the critical curves. --- include/concave_hull.h | 331 +++++++++++++++++++++++++++++++++++------ 1 file changed, 282 insertions(+), 49 deletions(-) diff --git a/include/concave_hull.h b/include/concave_hull.h index fa7a23aa..d96433f6 100644 --- a/include/concave_hull.h +++ b/include/concave_hull.h @@ -104,7 +104,68 @@ namespace Utilities{ return; } -/** \brief Creates the concave hull of a group of 2 dimensional points + + /// Returns a vector of points on the convex hull in counter-clockwise order. + template + void convex_hull(std::vector &P,std::vector &hull_index) + { + + size_t n = P.size(); + + if(n <= 3){ + hull_index.resize(n); + size_t i = 0; + for(size_t &d : hull_index) d = i++; + return; + } + + std::vector hull(n + 1); + hull_index.resize(n + 1); + + size_t k = 0; + + // Sort points lexicographically + std::sort(P.begin(), P.end(), + [](T p1,T p2){ + if(p1[0]==p2[0]) return p1[1] < p2[1]; + return p1[0] < p2[0];}); + + + // Build lower hull + for (size_t i = 0; i < n; i++) { + while (k >= 2 && crossD(hull[k-2], hull[k-1], P[i]) <= 0){ + k--; + } + hull_index[k] = i; + hull[k++] = P[i]; + } + + // Build upper hull + for (long i = n-2, t = k+1; i >= 0; i--) { + while (k >= t && crossD(hull[k-2], hull[k-1], P[i]) <= 0){ + k--; + assert(k > 1); + } + hull_index[k] = i; + hull[k++] = P[i]; + } + + hull_index.resize(k); + hull_index.pop_back(); + + hull.resize(k); + hull.pop_back(); + + return; + } + + + struct Edge{ + Edge(size_t i,double l):length(l),index(i){}; + double length = 0; + size_t index = 0; + }; + /** \brief Creates the concave hull of a group of 2 dimensional points by the shrink-wrap algorithm. The type of the input vector points must have an operator []. @@ -135,19 +196,19 @@ void concave(std::vector &init_points if(init_points.size() == hull.size()) return; - std::list> index_length; - + std::list edges; std::list leftovers; hull.push_back(hull[0]); double tmp; + // find pairs of hull points that are further appart than scale for(int i=0;i scale) index_length.push_back(std::pair(i,tmp)); + if(tmp > scale) edges.emplace_back(i,tmp);// .push_back(std::pair(i,tmp)); } - if(index_length.size() == 0) return; + if(edges.size() == 0) return; if(TEST){ PosType cent[] ={0.5,0.5}; @@ -158,16 +219,12 @@ void concave(std::vector &init_points map.drawPoints(init_points,0.01,1.0); map.drawCurve(hull,2); - map.printFITS("!test_concave.fits"); } - - - // sort edges by length - index_length.sort([](const std::pair &p1,const std::pair &p2){return p1.second > p2.second;}); - assert(index_length.front().second >= index_length.back().second); + edges.sort([](const Edge &p1,const Edge &p2){return p1.length > p2.length;}); + assert(edges.front().length >= edges.back().length); { // make a list of points that are not already in the convex hull @@ -199,37 +256,42 @@ void concave(std::vector &init_points typename std::list::iterator p,nextpoint; //auto p = leftovers.begin(); - double minarea,area,co; + double minarea,area,co1,co2; size_t i; - Point_2d vo,v1; + Point_2d vo,v1,v2; - while(index_length.front().second > scale){ + int count = 0; + while(edges.front().length > scale ){ if(leftovers.size() == 0) break; // find the point which if added to this edge would change the area least minarea = HUGE_VAL; - i = index_length.front().first; + i = edges.front().index; for(p = leftovers.begin() ; p != leftovers.end() ; ++p){ - vo = subtract(hull[i+1], hull[i]); v1 = subtract(*p,hull[i]); - - area = vo^v1; - co = v1*vo; - - if(co > 0 && area > 0 && index_length.front().second > v1.length()){ - if(area < minarea){ - minarea = area; - nextpoint = p; + + if( edges.front().length > v1.length()){ + + vo = subtract(hull[i+1], hull[i]); + //v2 = subtract(*p,hull[i+1]); + area = vo^v1; + co1 = v1*vo; + //co2 = v2*vo; + + if( co1 > 0 && (area > 0)*(area < minarea) ){ +// if(co1 > 0 && area > 0 && index_length.front().second > v1.length()){ + minarea = area; + nextpoint = p; } } } if(minarea == HUGE_VAL){ // if there is no acceptable point for this edge continue with the second longest edge - index_length.pop_front(); + edges.pop_front(); continue; } @@ -239,30 +301,34 @@ void concave(std::vector &init_points hull.insert(hull.begin() + i + 1,tmp); // update index_length list - std::pair new1 = index_length.front(); - index_length.pop_front(); - new1.second = (subtract(hull[i],hull[i+1])).length(); - std::pair new2(i+1,(subtract(hull[i+1],hull[i+2])).length()); - - for(auto &p : index_length) if(p.first > i) ++(p.first); - - auto p = index_length.begin(); - for(; p != index_length.end() ; ++p){ - if((*p).second < new1.second){ - index_length.insert(p,new1); - break; + Edge new1 = edges.front(); + edges.pop_front(); + new1.length = (subtract(hull[i],hull[i+1])).length(); + Edge new2(i+1,(subtract(hull[i+1],hull[i+2])).length()); + + for(auto &p : edges) if(p.index > i) ++(p.index); + + auto p = edges.begin(); + if(new1.length > scale){ + for(p = edges.begin() ; p != edges.end() ; ++p){ + if((*p).length < new1.length){ + edges.insert(p,new1); + break; + } } + if(p==edges.end()) edges.push_back(new1); } - if(p==index_length.end()) index_length.push_back(new1); - - for(p = index_length.begin(); p != index_length.end() ; ++p){ - if((*p).second < new2.second){ - index_length.insert(p,new2); - break; - } + if(new2.length > scale){ + for(p = edges.begin(); p != edges.end() ; ++p){ + if((*p).length < new2.length){ + edges.insert(p,new2); + break; + } + } + if(p==edges.end()) edges.push_back(new2); } - if(p==index_length.end()) index_length.push_back(new2); - + + if(TEST){ PosType cent[] ={0.5,0.5}; PixelMap map(cent,256, 1.0/256); @@ -274,17 +340,184 @@ void concave(std::vector &init_points map.printFITS("!test_concave.fits"); } - - } + } hull.pop_back(); - Utilities::RemoveIntersections(hull); + //Utilities::RemoveIntersections(hull); std::swap(hull,hull_out); return; } + template + std::vector concave2(std::vector &init_points,double scale) + { + + //typedef typename InputIt::value_type point; + + bool TEST = false; + + std::vector hull; + + // find the convex hull + convex_hull(init_points,hull); + + if(init_points.size() == hull.size()) return; + + std::list edges; + std::list leftovers; + hull.push_back(hull[0]); + + double tmp; + + // find pairs of hull points that are further appart than scale + for(int i=0;i scale) edges.emplace_back(i,tmp);// .push_back(std::pair(i,tmp)); + } + + if(edges.size() == 0) return; + + if(TEST){ + PosType cent[] ={0.5,0.5}; + PixelMap map(cent,256, 1.0/256); + + map.drawgrid(10,0.5); + + map.drawPoints(init_points,0.01,1.0); + map.drawCurve(hull,2); + + map.printFITS("!test_concave.fits"); + } + + // sort edges by length + edges.sort([](const Edge &p1,const Edge &p2){return p1.length > p2.length;}); + assert(edges.front().length >= edges.back().length); + + + { // make a list of points that are not already in the convex hull + + hull.pop_back(); + std::vector index(hull.size()); + size_t i = 0; + for(size_t &ind: index) ind = i++; + std::sort(index.begin(),index.end(), + [&hull](size_t p1,size_t p2){ + if(hull[p1][0] == hull[p2][0]) return hull[p1][1] < hull[p2][1]; + return hull[p1][0] < hull[p2][0];}); + + size_t j = 0; + + for(auto pit = init_points.begin(); pit != init_points.end() ; ++pit){ + + if((hull[index[j]][0] == (*pit)[0])*(hull[index[j]][1] == (*pit)[1])){ + ++j; + }else{ + leftovers.push_back(*pit); + } + + } + assert(hull.size() + leftovers.size() == init_points.size()); + hull.push_back(hull[0]); + } + + typename std::list::iterator p,nextpoint; + //auto p = leftovers.begin(); + + double minarea,area,co1,co2; + size_t i; + Point_2d vo,v1,v2; + + while(edges.front().length > scale ){ + + if(leftovers.size() == 0) break; + + // find the point which if added to this edge would change the area least + minarea = HUGE_VAL; + i = edges.front().index; + + for(p = leftovers.begin() ; p != leftovers.end() ; ++p){ + + v1 = subtract(*p,hull[i]); + + if( edges.front().length > v1.length()){ + + vo = subtract(hull[i+1], hull[i]); + //v2 = subtract(*p,hull[i+1]); + area = vo^v1; + co1 = v1*vo; + //co2 = v2*vo; + + if( co1 > 0 && (area > 0)*(area < minarea) ){ + // if(co1 > 0 && area > 0 && index_length.front().second > v1.length()){ + minarea = area; + nextpoint = p; + } + } + } + + if(minarea == HUGE_VAL){ + // if there is no acceptable point for this edge continue with the second longest edge + edges.pop_front(); + continue; + } + + // insert new point into hull + T tmp = *nextpoint; + leftovers.erase(nextpoint); + hull.insert(hull.begin() + i + 1,tmp); + + // update index_length list + Edge new1 = edges.front(); + edges.pop_front(); + new1.length = (subtract(hull[i],hull[i+1])).length(); + Edge new2(i+1,(subtract(hull[i+1],hull[i+2])).length()); + + for(auto &p : edges) if(p.index > i) ++(p.index); + + auto p = edges.begin(); + if(new1.length > scale){ + for(p = edges.begin() ; p != edges.end() ; ++p){ + if((*p).length < new1.length){ + edges.insert(p,new1); + break; + } + } + if(p==edges.end()) edges.push_back(new1); + } + if(new2.length > scale){ + for(p = edges.begin(); p != edges.end() ; ++p){ + if((*p).length < new2.length){ + edges.insert(p,new2); + break; + } + } + if(p==edges.end()) edges.push_back(new2); + } + + + if(TEST){ + PosType cent[] ={0.5,0.5}; + PixelMap map(cent,256, 1.0/256); + + map.drawgrid(10,0.5); + + map.drawPoints(init_points,0.03,1.0); + map.drawCurve(hull,2); + + map.printFITS("!test_concave.fits"); + } + } + + hull.pop_back(); + + //Utilities::RemoveIntersections(hull); + + return hull; + } + + } #endif /* concave_hull_h */ From c8ef34c6718e46fd8234b926cab03991d3bf5f05 Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Sat, 8 Dec 2018 14:39:12 +0100 Subject: [PATCH 095/227] Fixed error where particle masses and positions where multiplied by h instead of divided. --- MultiPlane/lens.cpp | 3 ++- MultiPlane/particle_lens.cpp | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/MultiPlane/lens.cpp b/MultiPlane/lens.cpp index a07c15d8..b54975ac 100644 --- a/MultiPlane/lens.cpp +++ b/MultiPlane/lens.cpp @@ -2061,7 +2061,8 @@ void Lens::readInputSimFileMillennium(bool verbose,DM_Light_Division division_mo field_halos[j]->setZlens(z); if(field_int_prof_type != nsie_lens){ - field_halos[j]->initFromFile(mass*(1-field_galaxy_mass_fraction),seed,vmax,r_halfmass*cosmo.gethubble()); + field_halos[j]->initFromFile(mass*(1-field_galaxy_mass_fraction) + ,seed,vmax,r_halfmass*cosmo.gethubble()); } diff --git a/MultiPlane/particle_lens.cpp b/MultiPlane/particle_lens.cpp index 8c36525d..70b6f5f3 100644 --- a/MultiPlane/particle_lens.cpp +++ b/MultiPlane/particle_lens.cpp @@ -140,8 +140,8 @@ void MakeParticleLenses::CreateHalos(const COSMOLOGY &cosmo,double redshift){ // put into proper units float h = cosmo.gethubble(); - double length_unit = h; - double mass_unit = h; + double length_unit = 1.0/h; + double mass_unit = 1.0/h; for(auto &p : data){ p[0] *= length_unit; From 1e8a46e9dade6078e41b3e657b4dd75d8fcc4d57 Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Mon, 10 Dec 2018 14:24:51 +0100 Subject: [PATCH 096/227] Fixed bug in concave(). --- include/concave_hull.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/concave_hull.h b/include/concave_hull.h index d96433f6..8c53079a 100644 --- a/include/concave_hull.h +++ b/include/concave_hull.h @@ -364,7 +364,7 @@ void concave(std::vector &init_points // find the convex hull convex_hull(init_points,hull); - if(init_points.size() == hull.size()) return; + if(init_points.size() == hull.size()) return hull; std::list edges; std::list leftovers; @@ -378,7 +378,7 @@ void concave(std::vector &init_points if(tmp > scale) edges.emplace_back(i,tmp);// .push_back(std::pair(i,tmp)); } - if(edges.size() == 0) return; + if(edges.size() == 0) return hull; if(TEST){ PosType cent[] ={0.5,0.5}; From 64330b6924d3f4372ad7b0bc59d6a46755689e86 Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Mon, 10 Dec 2018 14:31:12 +0100 Subject: [PATCH 097/227] Fixed bug in Utilities::concave2() --- include/concave_hull.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/concave_hull.h b/include/concave_hull.h index d96433f6..8c53079a 100644 --- a/include/concave_hull.h +++ b/include/concave_hull.h @@ -364,7 +364,7 @@ void concave(std::vector &init_points // find the convex hull convex_hull(init_points,hull); - if(init_points.size() == hull.size()) return; + if(init_points.size() == hull.size()) return hull; std::list edges; std::list leftovers; @@ -378,7 +378,7 @@ void concave(std::vector &init_points if(tmp > scale) edges.emplace_back(i,tmp);// .push_back(std::pair(i,tmp)); } - if(edges.size() == 0) return; + if(edges.size() == 0) return hull; if(TEST){ PosType cent[] ={0.5,0.5}; From 0e51c6394fdca19e1754189b11cea213d34e8a01 Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Tue, 11 Dec 2018 11:31:34 +0100 Subject: [PATCH 098/227] Bug fix with conversions from comoving to physical particle positions. --- MultiPlane/particle_lens.cpp | 2 +- include/particle_halo.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/MultiPlane/particle_lens.cpp b/MultiPlane/particle_lens.cpp index dc64fa84..5e2f428f 100644 --- a/MultiPlane/particle_lens.cpp +++ b/MultiPlane/particle_lens.cpp @@ -143,7 +143,7 @@ void MakeParticleLenses::CreateHalos(const COSMOLOGY &cosmo,double redshift){ // put into proper units float h = cosmo.gethubble(); - double length_unit = 1.0/h; + double length_unit = (1 + redshift)/h; // comoving units double mass_unit = 1.0/h; for(auto &p : data){ diff --git a/include/particle_halo.h b/include/particle_halo.h index 7b066b93..25158c6e 100644 --- a/include/particle_halo.h +++ b/include/particle_halo.h @@ -50,7 +50,7 @@ class LensHaloParticles : public LensHalo ,PosType MinPSize /// minimum particle size ); - LensHaloParticles(PType *pdata /// list of particles pdata[][i] should be the position in physical Mpc + LensHaloParticles(PType *pdata /// list of particles pdata[][i] should be the position in comoving Mpc ,size_t Npoints /// redshift of origin ,float redshift /// redshift of origin ,const COSMOLOGY& cosmo /// cosmology From 3d589e67e5d530ea29cf3d709680c722f7569804 Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Tue, 11 Dec 2018 12:45:27 +0100 Subject: [PATCH 099/227] Rearranged things to conform to previous labelling of LensHaloParticles constructor and avoid converting distances between comoving and physical twice. --- MultiPlane/particle_lens.cpp | 2 +- include/particle_halo.h | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/MultiPlane/particle_lens.cpp b/MultiPlane/particle_lens.cpp index 5e2f428f..c7f595d0 100644 --- a/MultiPlane/particle_lens.cpp +++ b/MultiPlane/particle_lens.cpp @@ -143,7 +143,7 @@ void MakeParticleLenses::CreateHalos(const COSMOLOGY &cosmo,double redshift){ // put into proper units float h = cosmo.gethubble(); - double length_unit = (1 + redshift)/h; // comoving units + double length_unit = 1.0/h; // comoving units double mass_unit = 1.0/h; for(auto &p : data){ diff --git a/include/particle_halo.h b/include/particle_halo.h index 25158c6e..4bba5210 100644 --- a/include/particle_halo.h +++ b/include/particle_halo.h @@ -50,7 +50,7 @@ class LensHaloParticles : public LensHalo ,PosType MinPSize /// minimum particle size ); - LensHaloParticles(PType *pdata /// list of particles pdata[][i] should be the position in comoving Mpc + LensHaloParticles(PType *pdata /// list of particles pdata[][i] should be the position in physical Mpc ,size_t Npoints /// redshift of origin ,float redshift /// redshift of origin ,const COSMOLOGY& cosmo /// cosmology @@ -227,7 +227,7 @@ LensHaloParticles::LensHaloParticles(const std::string& simulation_filena template LensHaloParticles::LensHaloParticles( - PType *pdata + PType *pdata /// particle data (all physical distances) ,size_t Nparticles ,float redshift /// redshift of origin ,const COSMOLOGY& cosmo /// cosmology @@ -248,13 +248,13 @@ LensHaloParticles::LensHaloParticles( LensHalo::setRsize(Rmax); // convert from comoving to physical coordinates - PosType scale_factor = 1/(1+redshift); + //PosType scale_factor = 1/(1+redshift); mcenter *= 0.0; PosType max_mass = 0.0,min_mass = HUGE_VALF,mass=0; for(size_t i=0;i Date: Mon, 17 Dec 2018 15:29:35 +0100 Subject: [PATCH 100/227] Fix bug in Point_3d division by scalar. --- include/point.h | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/include/point.h b/include/point.h index a3a2bcf1..0f8db11e 100644 --- a/include/point.h +++ b/include/point.h @@ -92,10 +92,11 @@ struct Point_2d{ x[1]/=value; return *this; } - Point_2d & operator/(PosType value){ - x[0]/=value; - x[1]/=value; - return *this; + Point_2d operator/(PosType value) const{ + Point_2d tmp; + tmp[0] = x[0]/value; + tmp[1] = x[1]/value; + return tmp; } Point_2d & operator*=(PosType value){ x[0]*=value; @@ -483,14 +484,13 @@ struct Point_3d{ x[2]/=value; return *this; } - Point_3d & operator/(PosType value){ + Point_3d operator/(PosType value) const{ Point_3d tmp; - tmp[0] = x[0]/value; tmp[1] = x[1]/value; tmp[2] = x[2]/value; - return *this; + return tmp; } Point_3d & operator*=(PosType value){ x[0] *=value; From 08953ee5047869d8b13628d4a3753888f45d2a71 Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Mon, 17 Dec 2018 15:35:16 +0100 Subject: [PATCH 101/227] Fixed bug in dividing a Point_3d by a scalar. --- include/point.h | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/include/point.h b/include/point.h index a3a2bcf1..0f8db11e 100644 --- a/include/point.h +++ b/include/point.h @@ -92,10 +92,11 @@ struct Point_2d{ x[1]/=value; return *this; } - Point_2d & operator/(PosType value){ - x[0]/=value; - x[1]/=value; - return *this; + Point_2d operator/(PosType value) const{ + Point_2d tmp; + tmp[0] = x[0]/value; + tmp[1] = x[1]/value; + return tmp; } Point_2d & operator*=(PosType value){ x[0]*=value; @@ -483,14 +484,13 @@ struct Point_3d{ x[2]/=value; return *this; } - Point_3d & operator/(PosType value){ + Point_3d operator/(PosType value) const{ Point_3d tmp; - tmp[0] = x[0]/value; tmp[1] = x[1]/value; tmp[2] = x[2]/value; - return *this; + return tmp; } Point_3d & operator*=(PosType value){ x[0] *=value; From ad4c700bbe2fc1caa8b3fab0136c909e8986a7c0 Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Mon, 17 Dec 2018 15:32:29 +0100 Subject: [PATCH 102/227] Fixed some things in reading from a Gadget-2 data file. Added MakeParticleLenses::densest_particle(). Added MakeParticleLenses::Recenter(). Added MakeParticleLenses::getZoriginal(). --- MultiPlane/particle_lens.cpp | 49 +++++++++++++++++++++++--------- TreeCode_link/curve_routines.cpp | 10 +++++-- TreeCode_link/double_sort.cpp | 15 ++++++++++ include/List_old.h | 4 ++- include/Tree.h | 20 +++++++++---- include/gadget.hh | 2 ++ include/particle_halo.h | 15 ++++++++-- include/point.h | 6 ++++ 8 files changed, 96 insertions(+), 25 deletions(-) diff --git a/MultiPlane/particle_lens.cpp b/MultiPlane/particle_lens.cpp index c7f595d0..03734543 100644 --- a/MultiPlane/particle_lens.cpp +++ b/MultiPlane/particle_lens.cpp @@ -71,6 +71,9 @@ MakeParticleLenses::MakeParticleLenses( // find center of mass cm[0]=cm[1]=cm[2]=0; + bbox_ll[0] = bbox_ur[0] = data[0][0]; + bbox_ll[1] = bbox_ur[1] = data[0][1]; + bbox_ll[2] = bbox_ur[2] = data[0][2]; double m=0; for(auto p : data){ for(int i=0 ; i < 3 ; ++i){ @@ -124,18 +127,22 @@ MakeParticleLenses::MakeParticleLenses(const std::string &filename /// path / n // subtract center of mass if(recenter){ - for(auto &p : data){ - p[0] -= cm[0]; - p[1] -= cm[1]; - p[2] -= cm[2]; - } - - for(int i=0 ; i < 3 ; ++i){ - bbox_ll[i] -= cm[i]; - bbox_ur[i] -= cm[i]; - } - cm[0]=cm[1]=cm[2]=0; + Recenter(cm); + } +} + +void MakeParticleLenses::Recenter(Point_3d x){ + for(auto &p : data){ + p[0] -= x[0]; + p[1] -= x[1]; + p[2] -= x[2]; } + + bbox_ll -= x; + bbox_ur -= x; + cm -= x; + + Utilities::delete_container(halos); } void MakeParticleLenses::CreateHalos(const COSMOLOGY &cosmo,double redshift){ @@ -339,12 +346,12 @@ bool MakeParticleLenses::readGadget2(){ GadgetFile > gadget_file(filename,data); z_original = gadget_file.redshift; - for(int n=0 ; n < gadget_file.numfiles ; ++n){ + //for(int n=0 ; n < gadget_file.numfiles ; ++n){ gadget_file.openFile(); gadget_file.readBlock("POS"); gadget_file.readBlock("MASS"); gadget_file.closeFile(); - } + //} double a = 1.0e-3/(1 + z_original); // **** convert to physical Mpc/h and Msun/h @@ -464,6 +471,22 @@ void MakeParticleLenses::radialCut(Point_2d center,double radius){ } } +Point_3d MakeParticleLenses::densest_particle() const{ + + double dmax = -1,d; + Point_3d x; + for(auto p : data){ + d = p.mass()/p.size()/p.size()/p.size(); + if(d > dmax){ + dmax = d; + x[0] = p[0]; + x[1] = p[1]; + x[2] = p[2]; + } + } + return x; +} + // remove particles that are beyond cylindrical radius (Mpc/h) of center void MakeParticleLenses::cylindricalCut(Point_2d center,double radius){ diff --git a/TreeCode_link/curve_routines.cpp b/TreeCode_link/curve_routines.cpp index 781d38d5..bffcddf1 100644 --- a/TreeCode_link/curve_routines.cpp +++ b/TreeCode_link/curve_routines.cpp @@ -989,7 +989,10 @@ void walkcurve(Point *points,long Npoints,long *j,long *end){ if(delta){ if(i==(*end)) *end=(*j)+1; - for(k=i;k>(*j)+1;--k) SwapPointsInArray( &(points[k]) , &(points[k-1]) ); + for(k=i;k>(*j)+1;--k){ + assert(k < Npoints); + SwapPointsInArray( &(points[k]) , &(points[k-1]) ); + } ++(*j); i=(*j); step=1; @@ -1073,7 +1076,10 @@ short backtrack(Point *points,long Npoints,long *j,long jold,long *end){ // add neighbor to end of curve and restart if(i==*end) *end=*j+1; // move point to position after last one - for(k=i;k>(*j)+1;--k) SwapPointsInArray( &(points[k]),&(points[k-1]) ); + for(k=i;k>(*j)+1;--k){ + assert(k < Npoints); + SwapPointsInArray( &(points[k]),&(points[k-1]) ); + } //SwapPointsInArray(&(points[(*j)+1]),&(points[i]) ); ++(*j); return 1; diff --git a/TreeCode_link/double_sort.cpp b/TreeCode_link/double_sort.cpp index a05175f9..9940a8d3 100644 --- a/TreeCode_link/double_sort.cpp +++ b/TreeCode_link/double_sort.cpp @@ -130,16 +130,23 @@ namespace Utilities{ } else { k=(l+ir) >> 1; std::swap(arr[k],arr[l+1]); + assert(k < n + 1); + assert(l < n); SwapPointsInArray(&brr[k-1],&brr[l]); if (arr[l] > arr[ir]) { std::swap(arr[l],arr[ir]); + assert(l < n + 1); + assert(ir < n + 1); SwapPointsInArray(&brr[l-1],&brr[ir-1]); } if (arr[l+1] > arr[ir]) { + assert(l < n); + assert(ir < n+1); std::swap(arr[l+1],arr[ir]); SwapPointsInArray(&brr[l],&brr[ir-1]); } if (arr[l] > arr[l+1]) { + assert(l < n); std::swap(arr[l],arr[l+1]); SwapPointsInArray(&brr[l-1],&brr[l]); } @@ -153,6 +160,8 @@ namespace Utilities{ do j--; while (arr[j] > a); if (j < i) break; std::swap(arr[i],arr[j]); + assert(l < n + 1); + assert(j < n + 1); SwapPointsInArray(&brr[i-1],&brr[j-1]); } arr[l+1]=arr[j]; @@ -201,6 +210,7 @@ namespace Utilities{ // move pivot to end of array std::swap(arr[pivotindex],arr[N-1]); + assert(pivotindex < N); SwapPointsInArray(&pointarray[pivotindex],&pointarray[N-1]); newpivotindex=0; @@ -208,6 +218,7 @@ namespace Utilities{ for(i=0;i -/** \brief link list for points, uses the linking pointers within the Point type unlike Kist */ +/* \brief link list for points, uses the linking pointers within the Point type unlike Kist typedef struct PointList{ Point *top; Point *bottom; @@ -25,6 +25,7 @@ typedef struct PointList *ListHndl; routines for linked list of points ************************************************************/ +/* ListHndl NewList(void); Point *NewPoint(double *x,unsigned long id); void InsertAfterCurrent(ListHndl list,double *x,unsigned long id,Point *image); @@ -53,3 +54,4 @@ void EmptyList(ListHndl list); void UnionList(ListHndl list1,ListHndl list2); bool ArePointsUniqueList(ListHndl list); bool IntersectionList(ListHndl list1,ListHndl list2,ListHndl intersection); +*/ diff --git a/include/Tree.h b/include/Tree.h index b96d85f8..38aa7779 100644 --- a/include/Tree.h +++ b/include/Tree.h @@ -399,6 +399,7 @@ namespace Utilities{ // move pivot to end of array std::swap(arr[pivotindex],arr[N-1]); + assert(pivotindex < N); SwapPointsInArray(&pointarray[pivotindex],&pointarray[N-1]); newpivotindex=0; @@ -446,12 +447,14 @@ namespace Utilities{ pivotvalue=func(pointarray[pivotindex]); // move pivot to end of array + assert(pivotindex < N); SwapPointsInArray(&pointarray[pivotindex],&pointarray[N-1]); newpivotindex=0; // partition list and array for(i=0;i 500){ auto thread1 = std::async(std::launch::async, [&] { return quicksortPoints_multithread(pointarray,func,newpivotindex,level + 1); }); - quicksortPoints_multithread(&pointarray[newpivotindex+1],func,N-newpivotindex-1,level + 1); + quicksortPoints_multithread(&pointarray[newpivotindex+1],func + ,N-newpivotindex-1,level + 1); //thread1.wait(); }else{ quicksortPoints_multithread(pointarray,func,newpivotindex,level + 1); - quicksortPoints_multithread(&pointarray[newpivotindex+1],func,N-newpivotindex-1,level + 1); + quicksortPoints_multithread(&pointarray[newpivotindex+1],func + ,N-newpivotindex-1,level + 1); } return ; } @@ -489,6 +494,7 @@ namespace Utilities{ pivotvalue=func(array[pivotindex]); // move pivot to end of array + assert(pivotindex < N); SwapPointsInArray(&array[pivotindex],&array[N-1]); newpivotindex=0; @@ -503,13 +509,15 @@ namespace Utilities{ if(level < lev && N > 500){ auto thread1 = std::async(std::launch::async, [&] { - return quicksortPoints_multithread(array,func,newpivotindex,level + 1); }); - quicksort_multithread(&array[newpivotindex+1],func,N-newpivotindex-1,level + 1); - + return quicksortPoints_multithread(array,func + ,newpivotindex,level + 1); }); + quicksort_multithread(&array[newpivotindex+1],func + ,N-newpivotindex-1,level + 1); //thread1.wait(); }else{ quicksort_multithread(array,func,newpivotindex,level + 1); - quicksort_multithread(&array[newpivotindex+1],func,N-newpivotindex-1,level + 1); + quicksort_multithread(&array[newpivotindex+1],func + ,N-newpivotindex-1,level + 1); } return ; } diff --git a/include/gadget.hh b/include/gadget.hh index 2ccfbe88..ec680ff8 100644 --- a/include/gadget.hh +++ b/include/gadget.hh @@ -114,6 +114,8 @@ void GadgetFile::checkMultiple(){ printf("PartSpecies %d, anz=%d, masstab=%f\n",i,npart[i],masstab[i]); ntot += npart[i]; } + printf("Omage %f Lambda %f hubble %f\n",omega,lambda,hubble); + //numfiles = 1; } fclose(fd); } else { diff --git a/include/particle_halo.h b/include/particle_halo.h index 4bba5210..ed45c0b8 100644 --- a/include/particle_halo.h +++ b/include/particle_halo.h @@ -776,6 +776,10 @@ class MakeParticleLenses{ for(auto p : halos) delete p; } + /// recenter the particles to 3d point in physical Mpc/h units + /// If the halos have already been created they will be destroyed. + void Recenter(Point_3d x); + void CreateHalos(const COSMOLOGY &cosmo,double redshift); /// remove particles that are beyond radius (Mpc/h) from center @@ -786,21 +790,26 @@ class MakeParticleLenses{ /// returns the original center of mass of all the particles Point_3d getCenterOfMass() const{return cm;} + /// returns the location of the densest particle in (Mpc/h) + Point_3d densest_particle() const; + /// return the maximum and minimum coordinates of the particles in each dimension in for the original simulation in Mpc/h void getBoundingBox(Point_3d &Xmin,Point_3d &Xmax) const{ Xmin = bbox_ll; Xmax = bbox_ur; } -private: + + double getZoriginal(){return z_original;} - std::vector > data; + std::vector > data; // ??? +private: const std::string filename; int Nsmooth; Point_3d bbox_ll; // minumum coordinate values of particles Point_3d bbox_ur; // maximim coordinate values of particles - double z_original; + double z_original = -1; std::vector nparticles; std::vector masses; Point_3d cm; diff --git a/include/point.h b/include/point.h index 0f8db11e..98737d2d 100644 --- a/include/point.h +++ b/include/point.h @@ -478,6 +478,12 @@ struct Point_3d{ x[2]+=p.x[2]; return *this; } + Point_3d & operator-=(const Point_3d &p){ + x[0]-=p.x[0]; + x[1]-=p.x[1]; + x[2]-=p.x[2]; + return *this; + } Point_3d & operator/=(PosType value){ x[0]/=value; x[1]/=value; From 9a816c8b033e50af7bb03d7f35cc2a68eee3c7ed Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Thu, 20 Dec 2018 12:46:29 +0100 Subject: [PATCH 103/227] Fix to MakeParticlesLenses::radialCut() --- MultiPlane/particle_lens.cpp | 2 +- include/particle_halo.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/MultiPlane/particle_lens.cpp b/MultiPlane/particle_lens.cpp index 03734543..0f0e0d53 100644 --- a/MultiPlane/particle_lens.cpp +++ b/MultiPlane/particle_lens.cpp @@ -438,7 +438,7 @@ bool MakeParticleLenses::readHDF5(){ #endif */ // remove particles that are beyond radius (Mpc/h) of center -void MakeParticleLenses::radialCut(Point_2d center,double radius){ +void MakeParticleLenses::radialCut(Point_3d center,double radius){ double radius2 = radius*radius; double r2; diff --git a/include/particle_halo.h b/include/particle_halo.h index ed45c0b8..17a2e20a 100644 --- a/include/particle_halo.h +++ b/include/particle_halo.h @@ -783,7 +783,7 @@ class MakeParticleLenses{ void CreateHalos(const COSMOLOGY &cosmo,double redshift); /// remove particles that are beyond radius (Mpc/h) from center - void radialCut(Point_2d center,double radius); + void radialCut(Point_3d center,double radius); /// remove particles that are beyond cylindrical radius (Mpc/h) of center void cylindricalCut(Point_2d center,double radius); From c35ef92960df27e852c7aa2e6fd68f909da83995 Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Thu, 20 Dec 2018 13:39:03 +0100 Subject: [PATCH 104/227] Added the ability to ignore the type information in the gadget2 file using MakeParticleLenses --- MultiPlane/particle_lens.cpp | 26 +++++++++++++++----------- include/particle_halo.h | 5 +++-- 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/MultiPlane/particle_lens.cpp b/MultiPlane/particle_lens.cpp index 0f0e0d53..4b20f748 100644 --- a/MultiPlane/particle_lens.cpp +++ b/MultiPlane/particle_lens.cpp @@ -27,6 +27,7 @@ MakeParticleLenses::MakeParticleLenses( ,SimFileFormat format ,int Nsmooth /// number of nearest neighbors used for smoothing ,bool recenter /// recenter so that the LenHalos are centered on the center of mass of all the particles + ,bool ignore_type_in_smoothing ):filename(filename),Nsmooth(Nsmooth) { @@ -47,7 +48,7 @@ MakeParticleLenses::MakeParticleLenses( // processed file does not exist read the gadget file and find sizes switch (format) { case gadget2: - readGadget2(); + readGadget2(ignore_type_in_smoothing); break; case csv3: readCSV(3); @@ -332,16 +333,13 @@ bool MakeParticleLenses::readCSV(int columns_used){ } skip += nparticles[i]; } - } - - //LensHaloParticles >::calculate_smoothing(Nsmooth,data.data(),data.size()); - + } return true; }; -bool MakeParticleLenses::readGadget2(){ +bool MakeParticleLenses::readGadget2(bool ignore_type){ GadgetFile > gadget_file(filename,data); z_original = gadget_file.redshift; @@ -372,15 +370,21 @@ bool MakeParticleLenses::readGadget2(){ nparticles.push_back(gadget_file.npart[i]); masses.push_back(gadget_file.masstab[i]); - if(gadget_file.npart[i] > 0){ - pp = data.data() + skip; // pointer to first particle of type - size_t N = gadget_file.npart[i]; + if(!ignore_type){ + if(gadget_file.npart[i] > 0){ + pp = data.data() + skip; // pointer to first particle of type + size_t N = gadget_file.npart[i]; - LensHaloParticles >::calculate_smoothing(Nsmooth,pp,N); + LensHaloParticles >::calculate_smoothing(Nsmooth,pp,N); + } + skip += gadget_file.npart[i]; } - skip += gadget_file.npart[i]; } + if(ignore_type){ + LensHaloParticles >::calculate_smoothing(Nsmooth,data.data(),gadget_file.ntot); + } + return true; }; diff --git a/include/particle_halo.h b/include/particle_halo.h index 17a2e20a..e6959447 100644 --- a/include/particle_halo.h +++ b/include/particle_halo.h @@ -766,9 +766,10 @@ class MakeParticleLenses{ ,SimFileFormat format ,int Nsmooth /// number of nearest neighbors used for smoothing ,bool recenter /// recenter so that the LenHalos are centered on the center of mass + ,bool ignore_type_in_smoothing = false /// used only when format == gadget2, nearest neighbour smoothing is done amongst particles by type if set to false ); - MakeParticleLenses(const std::string &filename /// path / name of galmb file + MakeParticleLenses(const std::string &filename /// path / name of glmb file ,bool recenter /// recenter so that the LenHalos are centered on the center of mass ); @@ -854,7 +855,7 @@ class MakeParticleLenses{ } // Read particle from Gadget-2 format file - bool readGadget2(); + bool readGadget2(bool ignore_type); // Reads particles from first 4 columns of csv file bool readCSV(int columns_used); From 6b8657663cb82021219bb3eb75a7a51713f7ed31 Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Sun, 6 Jan 2019 17:57:08 +0100 Subject: [PATCH 105/227] Added multmap.h and multimap.cpp to make a LensHaloMultiMap class that used less memory by calculating the short and long range forces on two different maps. --- AnalyticNSIE/base_analens.cpp | 51 ---- FullRange/implant_stars.cpp | 2 - ImageProcessing/pixelize.cpp | 8 +- MultiPlane/CMakeLists.txt | 1 + MultiPlane/lens_halos.cpp | 4 +- MultiPlane/multimap.cpp | 515 ++++++++++++++++++++++++++++++++++ MultiPlane/profile.cpp | 2 + include/CMakeLists.txt | 1 + include/Tree.h | 2 +- include/lens_halos.h | 2 + include/multimap.h | 474 +++++++++++++++++++++++++++++++ 11 files changed, 1001 insertions(+), 61 deletions(-) create mode 100644 MultiPlane/multimap.cpp create mode 100644 include/multimap.h diff --git a/AnalyticNSIE/base_analens.cpp b/AnalyticNSIE/base_analens.cpp index 5b23bb62..bd01a8bd 100644 --- a/AnalyticNSIE/base_analens.cpp +++ b/AnalyticNSIE/base_analens.cpp @@ -1245,57 +1245,6 @@ PosType LensHalo::kappa_asym(PosType x,PosType theta){ -// The following lines are based on Ansatz IIIb and work according to the quality of the approximation - -/*PosType LensHalo::kappa_asym(PosType x,PosType theta){ - PosType F, f[3],g[3], kappa; - PosType phi=phi_int(x); - - //calcModesB(0.01*i, fratio, pa, mod1); - //std::cout << 0.01*i << " " << dhfunction(0.01*i) << " " << mod1[4] << " " << mod1[8] << " " << mod1[12] << " " << mod1[16] << " " << fratio << std::endl; - - faxial(x,theta,f); // for this to work calculate modes in faxial with calcModesB !! - gradial(x,g); - F=f[0]-1; - //beta=get_slope(); // only for fixed beta, i.e. PowerLaw - beta=bfunction(x); // only for NFW - double fac=1.0/(beta*beta/(2.-beta)/(2.-beta)); - kappa=f[0]*kappa_h(x)-0.5*f[2]*fac*phi;// w/o damping - //kappa=(1+F*g[0])*kappa_h(x)-0.5*phi*fac*(F*g[1]/x+F*g[2]+f[2]*g[0]/x/x)*x*x-F*g[1]*alpha_h(x)*x*x; /// with damping - - return kappa; -} - - - - void LensHalo::alpha_asym(PosType x,PosType theta, PosType alpha[]){ - PosType F,f[3],g[3],alpha_r,alpha_theta; - PosType phi=phi_int(x); - - faxial0(theta,f); - - F=f[0]-1; - gradial(x,g); - beta=get_slope(); - double fac=1.0/(beta*beta/(2-beta)/(2-beta)); - - //alpha_r=alpha_h(x)*f[0]; // w/o damping - //alpha_theta=f[1]*phi/x; // w/o damping - - alpha_r=alpha_h(x)*(1+F*g[0])+phi*fac*F*g[1]; // with damping - alpha_theta=f[1]*g[0]*phi*fac/x; // with damping - - //std::cout << "in alpha_asym: " << beta << " " << alpha_theta << std::endl; - - alpha[0] = (alpha_r*cos(theta) - alpha_theta*sin(theta))/cos(theta); - alpha[1] = (alpha_r*sin(theta) + alpha_theta*cos(theta))/sin(theta); - return; - } - -*/ - - - // NOTE THAT GAMMA_ASYM does not use Ansatz II yet! // In gamma_asym phi_int(x) is used, which calculates the potential unlike phi_h from alpha_h. diff --git a/FullRange/implant_stars.cpp b/FullRange/implant_stars.cpp index f90837b8..a9f7cce5 100644 --- a/FullRange/implant_stars.cpp +++ b/FullRange/implant_stars.cpp @@ -173,8 +173,6 @@ void LensHalo::implant_stars( stars_N = m; //std::printf("last star x = %e %e\n",stars_xp[stars_N-1][0],stars_xp[stars_N-1][1]); - - float dummy=0; //star_tree = new TreeForce(stars_xp,stars_N,star_masses,&dummy // ,false,false,5,2,false,star_theta_force); diff --git a/ImageProcessing/pixelize.cpp b/ImageProcessing/pixelize.cpp index cc5a1cb6..01e709ab 100644 --- a/ImageProcessing/pixelize.cpp +++ b/ImageProcessing/pixelize.cpp @@ -886,10 +886,10 @@ void PixelMap::printFITS(std::string filename, bool verbose) const phout.addKey("CTYPE2", "DEC--TAN", "the coordinate type for the second axis"); phout.addKey("CUNIT1", "deg ", "the coordinate unit for the first axis"); phout.addKey("CUNIT2", "deg ", "the coordinate unit for the second axis"); - phout.addKey("CDELT1", -180*resolution/PI, "partial of first axis coordinate w.r.t. x"); + phout.addKey("CDELT1", 180*resolution/PI, "partial of first axis coordinate w.r.t. x"); phout.addKey("CDELT2", 180*resolution/PI, "partial of second axis coordinate w.r.t. y"); phout.addKey("CROTA2", 0.0, ""); - phout.addKey("CD1_1", -180*resolution/PI, "partial of first axis coordinate w.r.t. x"); + phout.addKey("CD1_1", 180*resolution/PI, "partial of first axis coordinate w.r.t. x"); phout.addKey("CD1_2", 0.0, "partial of first axis coordinate w.r.t. y"); phout.addKey("CD2_1", 0.0, "partial of second axis coordinate w.r.t. x"); phout.addKey("CD2_2", 180*resolution/PI, "partial of second axis coordinate w.r.t. y"); @@ -949,10 +949,10 @@ void PixelMap::printFITS(std::string filename,std::vector::has_infinity) + Rmax = std::numeric_limits::infinity(); + else + Rmax = std::numeric_limits::max(); + + LensMap submap; + submap.read_header(fitsfilename,cosmo.gethubble()); + + long_range_map.boxlMpc = submap.boxlMpc; + + //std::size_t size = bigmap.nx*bigmap.ny; + rs2 = submap.boxlMpc*submap.boxlMpc/( 2*PI*submap.nx ); + res = submap.boxlMpc/submap.nx; + Noriginal[0] = submap.nx; + Noriginal[1] = submap.ny; + border_width = 4.5*sqrt(rs2)/res + 1; + + int desample = 2. * PI * sqrt(rs2) / res / 3. ; + + desample = sqrt(rs2) / res; // ???? test + + size_t nx = long_range_map.nx = submap.nx / desample ; + size_t ny = long_range_map.ny = nx * submap.ny * 1./ submap.nx ; + + long_range_map.surface_density.resize(nx*ny,0); + + long chunk = MIN(nx*ny/Noriginal[0],Noriginal[1]); // same size as long range grid + if( chunk == 0) chunk = MIN(100*desample,Noriginal[1]); + + std::vector first(2); + std::vector last(2); + + first[0] = 1; + last[0] = Noriginal[0]; + + size_t kj = 0; + for(size_t j = 0 ; j < Noriginal[1] ; ++j ){ + size_t jj = MIN(j/desample,ny-1); // ??? + assert(jj < ny); + //size_t kjj = nx*(j/desample); + size_t kjj = nx*jj; + + if( j%chunk == 0 ){ + first[1] = j+1; + last[1] = MIN(j + chunk,Noriginal[1]); + submap.read_sub(fitsfilename,first,last,cosmo.gethubble()); + kj = 0; + }else{ + kj += submap.nx; + assert(kj < submap.nx*submap.ny); + } + + for(size_t i = 0 ; i < Noriginal[0] ; ++i ){ + size_t ii = MIN(i/desample,nx-1); + assert(ii + kjj < long_range_map.surface_density.size() ); + assert(i + kj < submap.surface_density.size() ); + long_range_map.surface_density[ ii + kjj ] += submap.surface_density[ i + kj ]; + } + } + + double area = res*res*cosmo.gethubble()/mass_unit; //*** units ??? + // convert to + for(auto &p : long_range_map.surface_density){ + p /= area; + } + + wlr.rs2 = wsr.rs2 = rs2; + long_range_map.PreProcessFFTWMap(1.0,wlr); + + // ???? +// UNIT w; +// long_range_map.PreProcessFFTWMap(1.0,w); + + long_range_map.write("!" + fitsfile + "_lr.fits"); + + // ??? don + //bigmap.boxlMpc = long_range_map.boxlMpc; + //bigmap.PreProcessFFTWMap(1.0,wsr); + //bigmap.write("!" + fitsfile + "_sr.fits"); +}; + +void LensHaloMultiMap::submap(Point_2d ll,Point_2d ur){ + std::vector lower_left(2); + std::vector upper_right(2); + + ll = (ll - long_range_map.lowerleft)/res; + ur = (ur - long_range_map.lowerleft)/res; + lower_left[0] = lround(ll[0]); + lower_left[1] = lround(ll[1]); + + lower_left[0] = lround(ur[0]); + lower_left[1] = lround(ur[1]); + + submap(lower_left,upper_right); +} + +void LensHaloMultiMap::submap( + const std::vector &lower_left + ,const std::vector &upper_right + ){ + + // check range + + if( (upper_right[0] < 0) || (upper_right[1] < 0) || (lower_left[0] >= Noriginal[0] ) + || (lower_left[1] >= Noriginal[1]) ){ + std::cerr << "LensHaloMap : sub map is out of bounds" << std::endl; + throw std::invalid_argument("out of bounds"); + } + if( (upper_right[0] - lower_left[0]) > Noriginal[0] + || (upper_right[1] - lower_left[1]) > Noriginal[1] ){ + std::cerr << "LensHaloMap : sub map is too large" << std::endl; + throw std::invalid_argument("out of bounds"); + } + + std::vector first(2); + std::vector last(2); + + first[0] = lower_left[0] - border_width; + first[1] = lower_left[1] - border_width; + + last[0] = upper_right[0] + border_width; + last[1] = upper_right[1] + border_width; + + LensMap map; + + if( (first[0] > 0)*(first[1] > 0)*(last[0] < Noriginal[0])*(last[1] < Noriginal[1]) ){ + map.read_sub(fitsfilename,first,last,cosmo.gethubble()); + }else{ + + size_t nx = map.nx = last[0] - first[0] + 1; + size_t ny = map.ny = last[1] - first[1] + 1; + + // case where subfield overlaps edge + std::vector first_sub(2); + std::vector last_sub(2); + + first_sub[0] = 1; + last_sub[0] = Noriginal[0]; + + map.surface_density.resize(nx*ny); + + LensMap partmap; + + const size_t chunk = nx*ny/Noriginal[0]; + + size_t kk,k = chunk + 1; + long jj = first[1]; + for(size_t j = 0 ; j < ny ; ++j , ++jj ){ + size_t kj = nx*j; + + if( k >= chunk || jj < 0 || jj >= Noriginal[1] ){ + + if(jj < 0) jj += Noriginal[1]; + if(jj >= Noriginal[1] ) jj -= Noriginal[1]; + + first_sub[1] = jj + 1; last_sub[1] = MIN(jj + chunk + 1,Noriginal[1]); + partmap.read_sub(fitsfilename,first_sub,last_sub,cosmo.gethubble()); + k = 0; + }else{ + ++k; + } + + kk = k*Noriginal[0]; + long ii = first[0]; + for(size_t i = 0 ; i < nx ; ++i,++ii ){ + if(ii < 0) ii += Noriginal[0]; + if(ii >= Noriginal[0] ) ii -= Noriginal[0]; + + assert( i + kj < map.surface_density.size() ); + assert( ii + kk < partmap.surface_density.size() ); + map.surface_density[ i + kj ] = partmap.surface_density[ ii + kk ]; + } + } + + // need to do overlap region + map.boxlMpc = map.nx*res; + } + + double area = res*res*cosmo.gethubble()/mass_unit; //*** units ??? + // convert to + for(auto &p : map.surface_density){ + p /= area; + } + + map.PreProcessFFTWMap(1.0,wsr); + + // cut off bourders + + size_t nx = short_range_map.nx = upper_right[0] - lower_left[0] + 1; + size_t ny = short_range_map.ny = upper_right[1] - lower_left[1] + 1; + + short_range_map.surface_density.resize(nx*ny); + short_range_map.alpha1_bar.resize(nx*ny); + short_range_map.alpha2_bar.resize(nx*ny); + short_range_map.gamma1_bar.resize(nx*ny); + short_range_map.gamma2_bar.resize(nx*ny); + + for(long j=0 ; j < ny ; ++j){ + long kjj = (j + border_width )*map.nx; + long kj = nx*j; + long ii = border_width; + for(long i=0 ; i < nx ; ++i,++ii){ + short_range_map.surface_density[i + kj] = map.surface_density[ii + kjj]; + short_range_map.alpha1_bar[i + kj] = map.alpha1_bar[ii + kjj]; + short_range_map.alpha2_bar[i + kj] = map.alpha2_bar[ii + kjj]; + short_range_map.gamma1_bar[i + kj] = map.gamma1_bar[ii + kjj]; + short_range_map.gamma2_bar[i + kj] = map.gamma2_bar[ii + kjj]; + } + } + + short_range_map.center = short_range_map.lowerleft = short_range_map.upperright = long_range_map.lowerleft; + short_range_map.lowerleft[0] += lower_left[0]*res; + short_range_map.lowerleft[1] += lower_left[1]*res; + + short_range_map.upperright[0] += upper_right[0]*res; + short_range_map.upperright[1] += upper_right[1]*res; + + short_range_map.center = (map.lowerleft + map.upperright)/2; + short_range_map.boxlMpc = nx*res; + + short_range_map.write("!" + fitsfilename + "_sr.fits"); +} + + +void LensHaloMultiMap::force_halo(double *alpha + ,KappaType *kappa + ,KappaType *gamma + ,KappaType *phi + ,double const *xx + ,bool subtract_point + ,PosType screening + ) +{ + + // interpolate from the maps + + Utilities::Interpolator > interp(xx + ,long_range_map.nx,long_range_map.boxlMpc + ,long_range_map.ny + ,long_range_map.ny*long_range_map.boxlMpc/long_range_map.nx + ,long_range_map.center.x); + Utilities::Interpolator > short_interp(xx + ,short_range_map.nx,short_range_map.boxlMpc + ,short_range_map.ny + ,short_range_map.ny*short_range_map.boxlMpc/short_range_map.nx + ,short_range_map.center.x); + + assert(long_range_map.nx == long_range_map.ny); + + alpha[0] = interp.interpolate(long_range_map.alpha1_bar); + alpha[1] = interp.interpolate(long_range_map.alpha2_bar); + gamma[0] = interp.interpolate(long_range_map.gamma1_bar); + gamma[1] = interp.interpolate(long_range_map.gamma2_bar); + gamma[2] = 0.0; + *kappa = interp.interpolate(long_range_map.surface_density); + //*phi = interp.interpolate(long_range_map.phi_bar); + + alpha[0] += short_interp.interpolate(short_range_map.alpha1_bar); + alpha[1] += short_interp.interpolate(short_range_map.alpha2_bar); + gamma[0] += short_interp.interpolate(short_range_map.gamma1_bar); + gamma[1] += short_interp.interpolate(short_range_map.gamma2_bar); + *kappa += short_interp.interpolate(short_range_map.surface_density); + //*phi = interp.interpolate(short_range_map.phi_bar); + + return; +} + +void LensMap::read_header(std::string fits_input_file,float h){ + + std::auto_ptr ff(new CCfits::FITS (fits_input_file, CCfits::Read)); + + CCfits::PHDU *h0=&ff->pHDU(); + + h0->readAllKeys(); + + assert(h0->axes() >= 2); + + nx = h0->axis(0); + ny = h0->axis(1); + + try{ + /* these are always present in ea*/ + float wlow,wup,res; + h0->readKey ("CD1_1",res); // resolution in degrees + h0->readKey ("REDSHIFT",z); + h0->readKey ("WLOW",wlow); + h0->readKey ("WUP",wup); + + double D = 3*(pow(wup,4) - pow(wlow,4))/(pow(wup,3) - pow(wlow,3))/4; + if(wlow == wup) D = wlow; + D /= (1+z)*h; + + res *= degreesTOradians*D; + boxlMpc = res*nx; + } + catch(CCfits::HDU::NoSuchKeyword){ + + std::cerr << "LensMap fits map must have header keywords:" << std::endl + << " CD1_1 - length on other side in Mpc/h" << std::endl + << " REDSHIFT - redshift of lens" << std::endl + << " WLOW - closest radial distance in cMpc/h" << std::endl + << " WUP - furthest radial distance in cMpc/h" << std::endl; + + exit(1); + } + + + lowerleft = center; + lowerleft[0] -= boxlMpc/2; + lowerleft[1] -= boxlMpc*ny/nx/2; + + upperright = center; + upperright[0] += boxlMpc/2; + upperright[1] += boxlMpc*ny/nx/2; +} + +void LensMap::read(std::string fits_input_file,float h){ + + std:: cout << " reading lens density map file: " << fits_input_file << std:: endl; + std::auto_ptr ff(new CCfits::FITS (fits_input_file, CCfits::Read)); + + CCfits::PHDU *h0=&ff->pHDU(); + + h0->readAllKeys(); + + assert(h0->axes() >= 2); + + nx = h0->axis(0); + ny = h0->axis(1); + + size_t size = nx*ny; + + // principal HDU is read + h0->read(surface_density); + int nhdu = h0->axes(); + + if(nhdu > 2){ // file contains other lensing quantities + alpha1_bar.resize(size); + alpha2_bar.resize(size); + gamma1_bar.resize(size); + gamma2_bar.resize(size); + //gamma3_bar.resize(size); + phi_bar.resize(size); + + CCfits::ExtHDU &h1=ff->extension(1); + h1.read(alpha1_bar); + CCfits::ExtHDU &h2=ff->extension(2); + h2.read(alpha2_bar); + CCfits::ExtHDU &h3=ff->extension(3); + h3.read(gamma1_bar); + CCfits::ExtHDU &h4=ff->extension(4); + h4.read(gamma2_bar); + std::cout << *h0 << h1 << h2 << h3 << h4 << std::endl; + } + try{ + /* these are always present in ea*/ + float wlow,wup,res; + h0->readKey ("CD1_1",res); // recall you that MOKA Mpc/h + h0->readKey ("REDSHIFT",z); + h0->readKey ("WLOW",wlow); + h0->readKey ("WUP",wup); + + double D = 3*(pow(wup,4) - pow(wlow,4))/(pow(wup,3) - pow(wlow,3))/4; + D /= (1+z)*h; + + res *= degreesTOradians*D; + boxlMpc = res*nx; + } + catch(CCfits::HDU::NoSuchKeyword){ + + std::cerr << "LensMap fits map must have header keywords:" << std::endl + << " CD1_1 - length on other side in Mpc/h" << std::endl + << " REDSHIFT - redshift of lens" << std::endl + << " WLOW - closest radial distance in cMpc/h" << std::endl + << " WUP - furthest radial distance in cMpc/h" << std::endl; + exit(1); + } + + /// ??? set center + + lowerleft = center; + lowerleft[0] -= boxlMpc/2; + lowerleft[1] -= boxlMpc*ny/nx/2; + + upperright = center; + upperright[0] += boxlMpc/2; + upperright[1] += boxlMpc*ny/nx/2; +} + +void LensMap::read_sub(std::string fits_input_file + ,const std::vector &first + ,const std::vector &last + ,float h +){ + + //std:: cout << " reading lens density map file: " << fits_input_file << std:: endl; + std::auto_ptr ff(new CCfits::FITS (fits_input_file, CCfits::Read)); + + CCfits::PHDU *h0=&ff->pHDU(); + + h0->readAllKeys(); + + /* these are always present in ea*/ + float wlow,wup,res; + h0->readKey ("CD1_1",res); // resolution in + h0->readKey ("REDSHIFT",z); + h0->readKey ("WLOW",wlow); + h0->readKey ("WUP",wup); + + double D = 3*(pow(wup,4) - pow(wlow,4))/(pow(wup,3) - pow(wlow,3))/4; + if(wlow == wup) D = wup; + D /= (1+z)*h; + res *= degreesTOradians*D; + boxlMpc = res*nx; + + assert(h0->axes() >= 2); + std::vector stride = {1,1}; + + // principal HDU is read + h0->read(surface_density,first,last,stride); + + nx = h0->axis(0); + ny = h0->axis(1); + + ff.release(); + + lowerleft[0] = boxlMpc*(2*first[0] - nx)/2; + lowerleft[1] = boxlMpc*(2*first[1] - ny)/2; + + upperright[0] = boxlMpc*(2*last[0] - nx)/2; + upperright[1] = boxlMpc*(2*last[1] - ny)/2; + + center = (lowerleft + upperright)/2; + + nx = last[0] - first[0] + 1; + ny = last[1] - first[1] + 1; + + boxlMpc *= nx*1./h0->axis(0); +} + + +/** + * \brief write the fits file of the maps of all the lensing quantities + */ +void LensMap::write(std::string filename){ +#ifdef ENABLE_FITS + long naxis=2; + long naxes[2]={nx,ny}; + + std::auto_ptr fout(0); + + try{ + fout.reset(new FITS(filename,FLOAT_IMG,naxis,naxes)); + } + catch(FITS::CantCreate){ + std::cout << "Unable to open fits file " << filename << std::endl; + ERROR_MESSAGE(); + exit(1); + } + + std::vector naxex(2); + naxex[0]=nx; + naxex[1]=ny; + + PHDU *phout=&fout->pHDU(); + + phout->addKey("SIDEL2",boxlMpc,"Mpc/h"); + + phout->write( 1,nx*ny,surface_density ); + + ExtHDU *eh1=fout->addImage("alpah1", FLOAT_IMG, naxex); + eh1->write(1,nx*ny,alpha1_bar); + ExtHDU *eh2=fout->addImage("alpha2", FLOAT_IMG, naxex); + eh2->write(1,nx*ny,alpha2_bar); + + ExtHDU *eh3=fout->addImage("gamma1", FLOAT_IMG, naxex); + eh3->write(1,nx*ny,gamma1_bar); + ExtHDU *eh4=fout->addImage("gamma2", FLOAT_IMG, naxex); + eh4->write(1,nx*ny,gamma2_bar); + + std::cout << *phout << std::endl; +#else + std::cout << "Please enable the preprocessor flag ENABLE_FITS !" << std::endl; + exit(1); +#endif +} + diff --git a/MultiPlane/profile.cpp b/MultiPlane/profile.cpp index 06391574..3910bbc2 100644 --- a/MultiPlane/profile.cpp +++ b/MultiPlane/profile.cpp @@ -7,6 +7,7 @@ #include "../include/profile.h" +/* //TODO: CARLO Could this be made methods of a class? /// create profile of the maps for each lensing component - spherical simmetry is assumed // create profile of the maps for each lensing component - spherical simmetry is assumed @@ -168,3 +169,4 @@ double * estsigmacprof(std:: valarray q,int nx,int ny, std:: valarray + +#ifdef ENABLE_FITS +#include +#endif + +/** + * \brief The MOKA map structure, containing all quantities that define it + * + * The MOKA map, that is read in from the fits file. Its components include the + * lensing properties, as well as the cosmology, the size of the field of view, + * the redshifts of the lens and source, the properties of the cluster, etc. + * + * Note: To use this class requires setting the ENABLE_FITS compiler flag and linking + * the cfits library. + */ +struct LensMap{ + /// values for the map + std::valarray surface_density; // Msun / Mpc^2 + std::valarray alpha1_bar; + std::valarray alpha2_bar; + std::valarray gamma1_bar; + std::valarray gamma2_bar; + //std::valarray gamma3_bar; + std::valarray phi_bar; + //std::vector x; + int nx,ny; + // boxlMpc is Mpc/h for MOKA + /// lens and source properties + //double zlens,m,zsource,Dlens,DLS,DS,c,cS,fsub,mstar,minsubmass; + /// range in x direction, pixels are square + //double boxlarcsec,boxlrad; + double boxlMpc; + /// cosmology + //double omegam,omegal,h,wq; + //double inarcsec; + Point_2d center; + Point_2d lowerleft; + Point_2d upperright; + + double z; + + + LensMap(){}; + +#ifdef ENABLE_FITS + + LensMap(std::string fits_input_file,float h){ + read(fits_input_file,h); + } + + /// read an entire map + void read(std::string input_fits,float h); + + /// read only header information + void read_header(std::string input_fits,float h); + + /// read a subsection of the fits map + void read_sub(std::string input_fits + ,const std::vector &first + ,const std::vector &last + ,float h + ); + + void write(std::string filename); + +#endif + +#ifdef ENABLE_FFTW + // this calculates the other lensing quantities from the density map + //void PreProcessFFTWMap(float zerosize); + + //static double identity(double x){return 1;} + + template + void PreProcessFFTWMap(float zerosize,T Wphi_of_k); + //void PreProcessFFTWMap(float zerosize,std::function Wphi_of_k = identity); +#endif + +}; + +/** \brief A lens halo that calculates all lensing qunatities on two grids - a low res long range grid + * and a high res short range grid. This is done to reduce the required memory required. + * + * Note: To use this class requires setting the ENABLE_FITS compiler flag and linking + * the cfits library. + */ +class LensHaloMultiMap : public LensHalo +{ +public: + + LensHaloMultiMap( + std::string fitsfile /// Original fits map of the density + ,double z + ,double mass_unit + ,const COSMOLOGY &c + ); + + //double Wlr(double k2){return exp(-k2*rs2);} + + ~LensHaloMultiMap(){}; + + void submap( + const std::vector &lower_left + ,const std::vector &upper_right + ); + /// in coordinates relative to the center of the original map + void submap(Point_2d ll,Point_2d ur); + + void force_halo(double *alpha,KappaType *kappa,KappaType *gamma,KappaType *phi,double const *xcm,bool subtract_point=false,PosType screening = 1.0); + + void writeImage(std::string fn); + + /// return center in physical Mpc + Point_2d getCenter_lr() const { return long_range_map.center; } + /// return center in physical Mpc + Point_2d getCenter_sr() const { return short_range_map.center; } + + /// return range of long range map in physical Mpc + double getRangeMpc_lr() const { return long_range_map.boxlMpc; } + /// return range of long range map in physical Mpc + double getRangeMpc_sr() const { return short_range_map.boxlMpc; } + + /// return number of pixels on a x-axis side in original map + size_t getNx_lr() const { return long_range_map.nx; } + /// return number of pixels on a y-axis side in original map + size_t getNy_lr() const { return long_range_map.ny; } + + /// return number of pixels on a x-axis side in original map + size_t getNx_sr() const { return short_range_map.nx; } + /// return number of pixels on a y-axis side in original map + size_t getNy_sr() const { return short_range_map.ny; } + +private: + + const COSMOLOGY &cosmo; + + LensMap long_range_map; + LensMap short_range_map; + + double mass_unit; + + size_t Noriginal[2]; // number of pixels in each dimension in original image + double res; // resolution of original image and short rnage image in Mpc + long border_width; // width of short range maps padding + std::string fitsfilename; + + double rs2; + + //const COSMOLOGY& cosmo; + int zerosize; + + struct UNIT{ + int operator()(float k2){return 1;} + }; + struct WLR{ + float rs2; + float operator()(float k2){return exp(-k2*rs2);} + }; + struct WSR{ + float rs2; + float operator()(float k2){return 1 - exp(-k2*rs2);} + }; + + UNIT unit; + WSR wsr; + WLR wlr; +}; + + +/** + * \brief pre-process surface mass density map computing deflection angles and shear in FFT, + * generalized to work with rectangular maps + */ + +//void LensMap::PreProcessFFTWMap(float zerosize,std::function Wphi_of_k){ +template +void LensMap::PreProcessFFTWMap(float zerosize,T Wphi_of_k){ + + + // size of the new map in x and y directions, factor by which each size is increased + int Nnx=int(zerosize*nx); + int Nny=int(zerosize*ny); + double boxlx = boxlMpc*zerosize; + double boxly = boxlMpc*zerosize/nx*ny; + + std:: valarray Nmap; + try{ + Nmap.resize( Nnx*Nny ); + }catch(std::exception &e){ + std::cerr << "exception thrown in LensMap::PreProcessFFTWMap(): " << e.what() << std::endl; + } + // assume locate in a rectangular map and build up the new one + for( int j=0; j=int(Nnx/2-nx/2) && i=int(Nny/2-ny/2) && j=nx || jj>=ny){ + std::cout << " 1 error mapping " << ii << " " << jj << std::endl; + exit(1); + } + if(ii<0 || jj<0){ + std::cout << " 2 error mapping " << ii << " " << jj << std::endl; + exit(1); + } + Nmap[i+Nnx*j] = surface_density[ii+nx*jj]; + } + } + } + + //std::vector fNmap(Nny*(Nnx/2+1)); + //std::vector fphi( Nny*(Nnx/2+1) ); + fftw_complex *fphi = new fftw_complex[Nny*(Nnx/2+1)]; + + { + std::vector dNmap(Nnx*Nny); + std::vector input(Nnx*Nny); + //std::vector output(Nny*(Nnx/2+1)); + + for(int k=0;k fft( Nny*(Nnx/2+1) ); + std::vector realsp(Nnx*Nny); + + //fftw_plan pp = fftw_plan_dft_c2r_2d(Nny,Nnx,fft,realsp,FFTW_ESTIMATE); + fftw_plan pp = fftw_plan_dft_c2r_2d(Nny,Nnx,fft,realsp.data(),FFTW_MEASURE); + + // alpha1 + { + + // build modes for each pixel in the fourier space + for( int i=0; i Date: Sun, 6 Jan 2019 17:58:37 +0100 Subject: [PATCH 106/227] =?UTF-8?q?Make=20some=20changes=20to=20the=20?= =?UTF-8?q?=E2=80=9CMOKA=E2=80=9D=20structures=20to=20make=20them=20a=20bi?= =?UTF-8?q?t=20more=20organised=20and=20memory=20efficient.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- MultiPlane/MOKAfits.cpp | 350 +++++++++++++++------------------ MultiPlane/MOKAlens.cpp | 425 ++++++++++++++++------------------------ include/MOKAlens.h | 79 +++++--- 3 files changed, 377 insertions(+), 477 deletions(-) diff --git a/MultiPlane/MOKAfits.cpp b/MultiPlane/MOKAfits.cpp index b4d89377..85435d8d 100644 --- a/MultiPlane/MOKAfits.cpp +++ b/MultiPlane/MOKAfits.cpp @@ -72,10 +72,10 @@ void LensHaloMassMap::getDims(){ PHDU *h0=&ff->pHDU(); - map->nx = h0->axis(0); - map->ny = h0->axis(1); + map.nx = h0->axis(0); + map.ny = h0->axis(1); std:: cout << "nx ny " << std:: endl; - std:: cout << map->nx << " " << map->ny << std:: endl; + std:: cout << map.nx << " " << map.ny << std:: endl; } catch(FITS::CantOpen){ std::cout << "can not open " << MOKA_input_file << std::endl; @@ -90,8 +90,17 @@ void LensHaloMassMap::getDims(){ /** * \brief reads in the fits file for the MOKA or mass map and saves it in the structure map */ + void LensHaloMassMap::readMap(){ -#ifdef ENABLE_FITS + map.read(MOKA_input_file,zeromean,cosmo); + if(map.alpha1_bar.size() == 0){ + map.PreProcessFFTWMap(zerosize); + } +} + + +void MOKAmap::read(std::string MOKA_input_file,bool zeromean,const COSMOLOGY &cosmo){ + std:: cout << " reading lens density map file: " << MOKA_input_file << std:: endl; std::auto_ptr ff(new FITS (MOKA_input_file, Read)); @@ -99,10 +108,19 @@ void LensHaloMassMap::readMap(){ h0->readAllKeys(); + assert(h0->axes() >= 2); + + nx = h0->axis(0); + ny = h0->axis(1); + + size_t size = nx*ny; + + surface_density.resize(size); + // try to read MVIR, if it exists is a MOKA map bool moka; try { - h0->readKey ("MVIR",map->m); + h0->readKey ("MVIR",m); moka=true; } catch(CCfits::HDU::NoSuchKeyword) { @@ -110,43 +128,52 @@ void LensHaloMassMap::readMap(){ } // principal HDU is read - h0->read(map->convergence); + h0->read(surface_density); if(moka){ - // check if they exist if not it has to compute them + // check if other quantities exist if not it has to compute them int nhdu = h0->axis(2); if(nhdu==1){ + alpha1_bar.resize(0); //std:: cout << " preProcessing Map MOKA fits file has only KAPPA map" << std:: endl; - PreProcessFFTWMap(); + //PreProcessFFTWMap(zerosize); } - else{ + + if(nhdu != 1){ // file contains other lensing quantities + alpha1_bar.resize(size); + alpha2_bar.resize(size); + gamma1_bar.resize(size); + gamma2_bar.resize(size); + //gamma3_bar.resize(size); + phi_bar.resize(size); + ExtHDU &h1=ff->extension(1); - h1.read(map->alpha1); + h1.read(alpha1_bar); ExtHDU &h2=ff->extension(2); - h2.read(map->alpha2); + h2.read(alpha2_bar); ExtHDU &h3=ff->extension(3); - h3.read(map->gamma1); + h3.read(gamma1_bar); ExtHDU &h4=ff->extension(4); - h4.read(map->gamma2); + h4.read(gamma2_bar); std::cout << *h0 << h1 << h2 << h3 << h4 << std::endl; } try{ /* these are always present in each fits file created by MOKA */ - h0->readKey ("SIDEL",map->boxlarcsec); - h0->readKey ("SIDEL2",map->boxlMpc); // recall you that MOKA Mpc/h - h0->readKey ("ZLENS",map->zlens); - h0->readKey ("ZSOURCE",map->zsource); - h0->readKey ("OMEGA",map->omegam); - h0->readKey ("LAMBDA",map->omegal); - h0->readKey ("H",map->h); - h0->readKey ("W",map->wq); - h0->readKey ("MSTAR",map->mstar); - // h0->readKey ("MVIR",map->m); - h0->readKey ("CONCENTRATION",map->c); - h0->readKey ("DL",map->Dlens); - h0->readKey ("DLS",map->DLS); - h0->readKey ("DS",map->DS); + h0->readKey ("SIDEL",boxlarcsec); + h0->readKey ("SIDEL2",boxlMpc); // recall you that MOKA Mpc/h + h0->readKey ("ZLENS",zlens); + h0->readKey ("ZSOURCE",zsource); + h0->readKey ("OMEGA",omegam); + h0->readKey ("LAMBDA",omegal); + h0->readKey ("H",h); + h0->readKey ("W",wq); + h0->readKey ("MSTAR",mstar); + // h0->readKey ("MVIR",m); + h0->readKey ("CONCENTRATION",c); + h0->readKey ("DL",Dlens); + h0->readKey ("DLS",DLS); + h0->readKey ("DS",DS); } catch(CCfits::HDU::NoSuchKeyword){ std::cerr << "MOKA fits map must have header keywords:" << std::endl @@ -169,16 +196,16 @@ void LensHaloMassMap::readMap(){ }else{ // Pixelized mass map - int npixels = map->nx; + int npixels = nx; // cut a square! - // if(map->nynx) npixels = map->ny; - // std:: valarray mapbut(map->nx*map->ny); - // for(int i=0;inx;i++) for(int j=0;jny;j++){ - // mapbut[i+map->nx*j] = map->convergence[i+map->nx*j]; + // if(ny mapbut(nx*ny); + // for(int i=0;iconvergence.resize(npixels*npixels); + // convergence.resize(npixels*npixels); // keep it like it is, even if it is a rectangle - if(map->ny!=map->nx){ + if(ny!=nx){ std:: cout << " " << std:: endl; std:: cout << " the plane maps are rectangles! " << std:: endl; std:: cout << " " << std:: endl; @@ -220,36 +247,18 @@ void LensHaloMassMap::readMap(){ } } if(d2 != 0 ){ - /*double dll = ( d1 + d2 )*0.5; // comoving dists - - std:: vector zi; - int ni=2048; - std:: vector dli(ni); - Utilities::fill_linear(zi,ni,0.,5.); // max redshift should be around 2.5! - for(int i=0;izlens = Utilities::InterpolateYvec(dli,zi,dll); - */ - - map->zlens = cosmo.invComovingDist(( d1 + d2 )*0.5); + zlens = cosmo.invComovingDist(( d1 + d2 )*0.5); }else{ // if angular size distances are not set use ZLENS or REDSHIFT try { - h0->readKey("ZLENS",map->zlens); + h0->readKey("ZLENS",zlens); } catch(CCfits::HDU::NoSuchKeyword) { try { - h0->readKey("REDSHIFT",map->zlens); + h0->readKey("REDSHIFT",zlens); } catch(CCfits::HDU::NoSuchKeyword){ std::cout << "unable to read fits mass map header keywords" << std::endl << " either DLUP and DLLOW need to be set or ZLENS or REDSHIFT" << std::endl; @@ -263,27 +272,27 @@ void LensHaloMassMap::readMap(){ } } - if(map->zlens <= 0.0){ + if(zlens <= 0.0){ std::cerr << "Pixel map lens planes cannot have zero or negative redshifts!" << std::endl; throw std::runtime_error("Invalid header"); } - map->Dlens = cosmo.angDist(0.,map->zlens); // physical - double inarcsec = 180./M_PI/map->Dlens*60.*60.; + Dlens = cosmo.angDist(0.,zlens); // physical + double inarcsec = 180./M_PI/Dlens*60.*60.; double pixLMpc,pixelunit; try{ // physical size in degrees - h0->readKey("PHYSICALSIZE",map->boxlarcsec); - map->boxlarcsec=map->boxlarcsec*60.*60.; - pixLMpc = map->boxlarcsec/npixels/inarcsec; - map->boxlMpc = pixLMpc*npixels; + h0->readKey("PHYSICALSIZE",boxlarcsec); + boxlarcsec=boxlarcsec*60.*60.; + pixLMpc = boxlarcsec/npixels/inarcsec; + boxlMpc = pixLMpc*npixels; } catch(CCfits::HDU::NoSuchKeyword) { try{ // physical size in degrees - h0->readKey("PHYSICAL",map->boxlarcsec); - map->boxlarcsec=map->boxlarcsec*60.*60.; - pixLMpc = map->boxlarcsec/npixels/inarcsec; - map->boxlMpc = pixLMpc*npixels; + h0->readKey("PHYSICAL",boxlarcsec); + boxlarcsec=boxlarcsec*60.*60.; + pixLMpc = boxlarcsec/npixels/inarcsec; + boxlMpc = pixLMpc*npixels; } catch(CCfits::HDU::NoSuchKeyword) { std::cerr << "fits mass map must have header keywords:" << std::endl @@ -323,61 +332,28 @@ void LensHaloMassMap::readMap(){ exit(1); } } - /*catch(CCfits::HDU::NoSuchKeyword) { - - std::cerr << "fits mass map should have header keywords:" << std::endl - << " REDSHIFT - redshift of map plane" << std::endl - //<< " DLOW - ?? Mpc/h" << std::endl - //<< " DLUP - ?? Mpc/h" << std::endl - << " PHYSICALSIZE - size of map in the x-direction (degrees)" << std::endl - << " PIXELUNIT - pixel units in solar masses" << std::endl; - - std::cout << "unable to read map PHYSICALSIZE" << std::endl; - std::cout << "assuming is the MultiDark file" << std::endl; - map->boxlarcsec = 8.7*60.*60.; // W1 x-field of view - // map->boxlarcsec = 5.5*60.*60.; // W4 x-field of view - pixLMpc = map->boxlarcsec/npixels/inarcsec; - map->boxlMpc = pixLMpc*npixels; - pixelunit = 1.e+10/cosmo.gethubble()/pixLMpc/pixLMpc; // by hand - }*/ - - - // made square // need to be - // 1. take the part located in the left side - //for(int i=0;iconvergence[i+npixels*j] = mapbut[i+map->nx*j]*pixelunit; - // avkappa += map->convergence[i+npixels*j]; - // } - // 2. take the part located in the right side - // for(int i=0;iconvergence[i+npixels*j] = mapbut[(map->nx-npixels+i)+map->nx*j]*pixelunit; - // avkappa += map->convergence[i+npixels*j]; - // } - // avkappa /= (npixels*npixels); - - for(int i=0;inx;i++) for(int j=0;jny;j++){ - map->convergence[i+map->nx*j] *= pixelunit; + for(int i=0;inx;i++) for(int j=0;jny;j++){ - avkappa += map->convergence[i+map->nx*j]; + for(int i=0;inx*map->ny); + ave_sigma /= (nx*ny); - for(int i=0;inx;i++) for(int j=0;jny;j++){ - map->convergence[i+map->nx*j] = (map->convergence[i+map->nx*j] - avkappa); + for(int i=0;inx = map->ny = npixels; -#ifdef ENABLE_FFTW + // valid only to force the map to be square nx = ny = npixels; //std:: cout << " preProcessing Map " << std:: endl; // reducing the size of the maps // carlo test begin @@ -391,7 +367,7 @@ void LensHaloMassMap::readMap(){ for(int i=0;iconvergence,npixels,xi,yi,xh,xh); + double ki = getMapVal(convergence,npixels,xi,yi,xh,xh); kl[i+nl*j] = ki; if(ki!=ki){ std:: cout << xi << " " << yi << " " << ki << std:: endl; @@ -399,31 +375,22 @@ void LensHaloMassMap::readMap(){ } } std:: cout << " done " << std:: endl; - map->nx = map->ny = nl; - map->convergence.resize(nl*nl); + nx = ny = nl; + convergence.resize(nl*nl); std:: cout << " remapping " << std:: endl; for(int i=0;iconvergence[i+nl*j] = kl[i+nl*j]; + convergence[i+nl*j] = kl[i+nl*j]; } */ // carlo test end - PreProcessFFTWMap(); -#else - std::cout << "Please enable the preprocessor flag ENABLE_FFTW !" << std::endl; - exit(1); -#endif + //PreProcessFFTWMap(zerosize); } - // std:: cout << map->boxlMpc << " " << map->boxlarcsec << std:: endl; + // std:: cout << boxlMpc << " " << boxlarcsec << std:: endl; - for(size_t i=0;iconvergence.size();++i){ - assert(map->convergence[i] == map->convergence[i]); + for(size_t i=0;inx,map->ny}; + long naxes[2]={nx,ny}; std::auto_ptr fout(0); @@ -447,34 +417,34 @@ void LensHaloMassMap::writeImage(std::string filename){ } std::vector naxex(2); - naxex[0]=map->nx; - naxex[1]=map->ny; + naxex[0]=nx; + naxex[1]=ny; PHDU *phout=&fout->pHDU(); - phout->write( 1,map->nx*map->ny,map->convergence ); - - phout->addKey ("SIDEL",map->boxlarcsec,"arcsec"); - phout->addKey ("SIDEL2",map->boxlMpc,"Mpc/h"); - phout->addKey ("ZLENS",map->zlens,"lens redshift"); - phout->addKey ("ZSOURCE",map->zsource, "source redshift"); - phout->addKey ("OMEGA",map->omegam,"omega matter"); - phout->addKey ("LAMBDA",map->omegal,"omega lamda"); - phout->addKey ("H",map->h,"hubble/100"); - phout->addKey ("W",map->wq,"dark energy equation of state parameter"); - phout->addKey ("MSTAR",map->mstar,"stellar mass of the BCG in Msun/h"); - phout->addKey ("MVIR",map->m,"virial mass of the halo in Msun/h"); - phout->addKey ("CONCENTRATION",map->c,"NFW concentration"); - phout->addKey ("DL",map->Dlens,"Mpc/h"); - phout->addKey ("DLS",map->DLS,"Mpc/h"); - phout->addKey ("DS",map->DS,"Mpc/h"); + phout->write( 1,nx*ny,surface_density ); + + phout->addKey ("SIDEL",boxlarcsec,"arcsec"); + phout->addKey ("SIDEL2",boxlMpc,"Mpc/h"); + phout->addKey ("ZLENS",zlens,"lens redshift"); + phout->addKey ("ZSOURCE",zsource, "source redshift"); + phout->addKey ("OMEGA",omegam,"omega matter"); + phout->addKey ("LAMBDA",omegal,"omega lamda"); + phout->addKey ("H",h,"hubble/100"); + phout->addKey ("W",wq,"dark energy equation of state parameter"); + phout->addKey ("MSTAR",mstar,"stellar mass of the BCG in Msun/h"); + phout->addKey ("MVIR",m,"virial mass of the halo in Msun/h"); + phout->addKey ("CONCENTRATION",c,"NFW concentration"); + phout->addKey ("DL",Dlens,"Mpc/h"); + phout->addKey ("DLS",DLS,"Mpc/h"); + phout->addKey ("DS",DS,"Mpc/h"); ExtHDU *eh1=fout->addImage("gamma1", FLOAT_IMG, naxex); - eh1->write(1,map->nx*map->ny,map->gamma1); + eh1->write(1,nx*ny,gamma1_bar); ExtHDU *eh2=fout->addImage("gamma2", FLOAT_IMG, naxex); - eh2->write(1,map->nx*map->ny,map->gamma2); - ExtHDU *eh3=fout->addImage("gamma3", FLOAT_IMG, naxex); - eh3->write(1,map->nx*map->ny,map->gamma3); + eh2->write(1,nx*ny,gamma2_bar); + //ExtHDU *eh3=fout->addImage("gamma3", FLOAT_IMG, naxex); + //eh3->write(1,nx*ny,gamma3_bar); std::cout << *phout << std::endl; #else @@ -603,32 +573,31 @@ int fof(double l,std:: vector xci, std:: vector yci, std:: vecto * generalized to work with rectangular maps */ -void LensHaloMassMap::PreProcessFFTWMap(){ -#ifdef ENABLE_FFTW +void MOKAmap::PreProcessFFTWMap(float zerosize){ // initialize the quantities //int npix_filter = 0; // filter the map if you want on a given number of pixels: CHECK IT NOT IMPLEMENTED YET // size of the new map in x and y directions, factor by which each size is increased - int Nnx=int(zerosize*map->nx); - int Nny=int(zerosize*map->ny); - double Nboxlx = map->boxlMpc*zerosize; - double Nboxly = map->boxlMpc*zerosize/map->nx*map->ny; + int Nnx=int(zerosize*nx); + int Nny=int(zerosize*ny); + double Nboxlx = boxlMpc*zerosize; + double Nboxly = boxlMpc*zerosize/nx*ny; std:: valarray Nmap; try{ Nmap.resize( Nnx*Nny ); }catch(std::exception &e){ - std::cerr << "exception thrown in LensHaloMassMap::PreProcessFFTWMap(): " << e.what() << std::endl; + std::cerr << "exception thrown in MOKAmap::PreProcessFFTWMap(): " << e.what() << std::endl; } // assume locate in a rectangular map and build up the new one for( int j=0; j=int(Nnx/2-map->nx/2) && inx/2) && j>=int(Nny/2-map->ny/2) && jny/2)){ - int ii = i-int(Nnx/2-map->nx/2); - int jj = j-int(Nny/2-map->ny/2); + if(i>=int(Nnx/2-nx/2) && i=int(Nny/2-ny/2) && j=map->nx || jj>=map->ny){ + if(ii>=nx || jj>=ny){ std::cout << " 1 error mapping " << ii << " " << jj << std::endl; exit(1); } @@ -636,7 +605,7 @@ void LensHaloMassMap::PreProcessFFTWMap(){ std::cout << " 2 error mapping " << ii << " " << jj << std::endl; exit(1); } - Nmap[i+Nnx*j]=map->convergence[ii+map->nx*jj]; + Nmap[i+Nnx*j] = surface_density[ii+nx*jj]; } } } @@ -718,14 +687,14 @@ void LensHaloMassMap::PreProcessFFTWMap(){ fftw_execute( pp ); //fftw_destroy_plan(pp); - map->alpha1.resize(map->nx*map->ny); + alpha1_bar.resize(nx*ny); - for( int j=Nny/2-map->ny/2; jny/2; j++ ){ - for( int i=Nnx/2-map->nx/2; inx/2; i++ ){ - int ii = i-int(Nnx/2-map->nx/2); - int jj = j-int(Nny/2-map->ny/2); + for( int j=Nny/2-ny/2; jalpha1[ii+map->nx*jj] = -1*float(realsp[i+Nnx*j]/Nnx/Nny); + alpha1_bar[ii+nx*jj] = -1*float(realsp[i+Nnx*j]/Nnx/Nny); } } } @@ -752,14 +721,14 @@ void LensHaloMassMap::PreProcessFFTWMap(){ fftw_execute( pp ); //fftw_destroy_plan(pp); - map->alpha2.resize(map->nx*map->ny); + alpha2_bar.resize(nx*ny); - for( int j=Nny/2-map->ny/2; jny/2; j++ ){ - for( int i=Nnx/2-map->nx/2; inx/2; i++ ){ - int ii = i-int(Nnx/2-map->nx/2); - int jj = j-int(Nny/2-map->ny/2); + for( int j=Nny/2-ny/2; jalpha2[ii+map->nx*jj] = -1*float(realsp[i+Nnx*j]/Nnx/Nny); + alpha2_bar[ii+nx*jj] = -1*float(realsp[i+Nnx*j]/Nnx/Nny); } } } @@ -785,14 +754,14 @@ void LensHaloMassMap::PreProcessFFTWMap(){ fftw_execute( pp ); //fftw_destroy_plan(pp); - map->gamma1.resize(map->nx*map->ny); + gamma1_bar.resize(nx*ny); - for( int j=Nny/2-map->ny/2; jny/2; j++ ){ - for( int i=Nnx/2-map->nx/2; inx/2; i++ ){ - int ii = i-int(Nnx/2-map->nx/2); - int jj = j-int(Nny/2-map->ny/2); + for( int j=Nny/2-ny/2; jgamma1[ii+map->nx*jj] = float( realsp[i+Nnx*j]/Nnx/Nny); + gamma1_bar[ii+nx*jj] = float( realsp[i+Nnx*j]/Nnx/Nny); } } @@ -819,33 +788,26 @@ void LensHaloMassMap::PreProcessFFTWMap(){ fftw_execute( pp ); //fftw_destroy_plan(pp); - map->gamma2.resize(map->nx*map->ny); + gamma2_bar.resize(nx*ny); - for( int j=Nny/2-map->ny/2; jny/2; j++ ){ - for( int i=Nnx/2-map->nx/2; inx/2; i++ ){ - int ii = i-int(Nnx/2-map->nx/2); - int jj = j-int(Nny/2-map->ny/2); + for( int j=Nny/2-ny/2; jgamma2[ii+map->nx*jj] = float(-realsp[i+Nnx*j]/Nnx/Nny); + gamma2_bar[ii+nx*jj] = float(-realsp[i+Nnx*j]/Nnx/Nny); } } } - /* - double *phi = new double[Nnx*Nny]; - fftw_plan pp; - pp=fftw_plan_dft_c2r_2d(Nny,Nnx,fphi,phi,FFTW_ESTIMATE); - fftw_execute( pp ); - fftw_destroy_plan(pp); - delete[] phi; - */ - + // std:: cout << " remapping the map in the original size " << std:: endl; delete[] fft; delete[] realsp; delete[] fphi; -#endif + + phi_bar.resize(nx*ny); // ??? this needs to be calculated in the future } diff --git a/MultiPlane/MOKAlens.cpp b/MultiPlane/MOKAlens.cpp index a9bc5fc1..d1525a4b 100644 --- a/MultiPlane/MOKAlens.cpp +++ b/MultiPlane/MOKAlens.cpp @@ -12,7 +12,7 @@ using namespace std; /* * \brief center of mass of a map using the moving centre - */ + * void cmass(int n, std::valarray map, std:: vector x, double &xcm, double &ycm){ double shrink = 1.02; // 0.05% of the total number of pixels @@ -57,7 +57,7 @@ void cmass(int n, std::valarray map, std:: vector x, double &xcm xcm = nxcm; ycm = nycm; } -} +}*/ /** * \brief loads a mass map from a given filename @@ -71,7 +71,7 @@ maptype(my_maptype), cosmo(lenscosmo),zerosize(pixel_map_zeropad),zeromean(my_ze initMap(); // set redshift to value from map - setZlens(map->zlens); + setZlens(map.zlens); } /** \brief Create a LensHalo from a PixelMap representing the mass. @@ -97,10 +97,10 @@ LensHaloMassMap::LensHaloMassMap( LensHalo::setTheta(MassMap.getCenter()[0],MassMap.getCenter()[1]); - setZlensDist(map->zlens,cosmo); + setZlensDist(map.zlens,cosmo); //setZlens(redshift); // set redshift to value from map - //setZlens(map->zlens); + //setZlens(map.zlens); } /* @@ -125,46 +125,46 @@ LensHalo(),MOKA_input_file(""),maptype(pix_map),cosmo(lenscosmo),zerosize(pixel_ else Rmax = std::numeric_limits::max(); - map->nx = my_map.getNx(); - map->ny = my_map.getNy(); + map.nx = my_map.getNx(); + map.ny = my_map.getNy(); std::cout << "nx ny " << std::endl; - std::cout << map->nx << " " << map->ny << std::endl; + std::cout << map.nx << " " << map.ny << std::endl; - std::size_t size = map->nx*map->ny; + std::size_t size = map.nx*map.ny; - map->convergence.resize(size); - map->alpha1.resize(size,0.0); - map->alpha2.resize(size,0.0); - map->gamma1.resize(size,0.0); - map->gamma2.resize(size,0.0); - map->gamma3.resize(size,0.0); - map->phi.resize(size,0.0); + map.surface_density.resize(size); + map.alpha1.resize(size,0.0); + map.alpha2.resize(size,0.0); + map.gamma1.resize(size,0.0); + map.gamma2.resize(size,0.0); + map.gamma3.resize(size,0.0); + map.phi.resize(size,0.0); - map->boxlarcsec = my_map.getRangeX()/arcsecTOradians; - map->boxlrad = my_map.getRangeX(); - map->Dlens = Dist; - map->boxlMpc = map->boxlrad/Dist; // physical + map.boxlarcsec = my_map.getRangeX()/arcsecTOradians; + map.boxlrad = my_map.getRangeX(); + map.Dlens = Dist; + map.boxlMpc = map.boxlrad/Dist; // physical // convertion to solar masses per Mpc^2 - double convert = massconvertion/(my_map.getResolution()*my_map.getResolution()*map->Dlens*map->Dlens); + double convert = massconvertion/(my_map.getResolution()*my_map.getResolution()*map.Dlens*map.Dlens); - size_t Npixels = map->nx*map->ny; + size_t Npixels = map.nx*map.ny; LensHalo::mass = 0.0; for(size_t i=0 ; iconvergence[i] = my_map(i)*convert; + map.surface_density[i] = my_map(i)*convert; LensHalo::mass += my_map(i)*massconvertion; } - map->zsource = zsource; - map->zlens = zlens; + map.zsource = zsource; + map.zlens = zlens; - map->center[0] = map->center[1] = 0.0; - map->boxlrad = map->boxlarcsec*PI/180/3600.; + map.center[0] = map.center[1] = 0.0; + map.boxlrad = map.boxlarcsec*PI/180/3600.; PreProcessFFTWMap(); - assert(map->nx*map->ny == map->convergence.size()); + assert(map.nx*map.ny == map.surface_density.size()); LensHalo::setTheta(my_map.getCenter()[0],my_map.getCenter()[1]); @@ -187,12 +187,11 @@ LensHaloMassMap::LensHaloMassMap(InputParams& params, const COSMOLOGY& lenscosmo // set redshift if necessary if(LensHalo::getZlens() == -1) - setZlens(map->zlens); + setZlens(map.zlens); } LensHaloMassMap::~LensHaloMassMap() { - delete map; } void LensHaloMassMap::initMap() @@ -208,8 +207,6 @@ void LensHaloMassMap::initMap() exit(1); #endif - map = new MOKAmap(); - if(std::numeric_limits::has_infinity) Rmax = std::numeric_limits::infinity(); else @@ -217,49 +214,41 @@ void LensHaloMassMap::initMap() getDims(); - std::size_t size = map->nx*map->ny; - - map->convergence.resize(size); - map->alpha1.resize(size); - map->alpha2.resize(size); - map->gamma1.resize(size); - map->gamma2.resize(size); - map->gamma3.resize(size); - map->phi.resize(size); - + //std::size_t size = map.nx*map.ny; + readMap(); if(flag_background_field == 1) { - map->convergence = 0; - map->alpha1 = 0; - map->alpha2 = 0; - map->gamma1 = 0; - map->gamma2 = 0; - map->phi = 0; + map.surface_density = 0; + map.alpha1_bar = 0; + map.alpha2_bar = 0; + map.gamma1_bar = 0; + map.gamma2_bar = 0; + map.phi_bar = 0; } - map->center[0] = map->center[1] = 0.0; - map->boxlrad = map->boxlarcsec*PI/180/3600.; + map.center[0] = map.center[1] = 0.0; + map.boxlrad = map.boxlarcsec*PI/180/3600.; if(maptype == moka){ - Utilities::fill_linear(map->x, map->nx, -0.5*map->boxlMpc, 0.5*map->boxlMpc); // physical Mpc/h + //Utilities::fill_linear(map.x, map.nx, -0.5*map.boxlMpc, 0.5*map.boxlMpc); // physical Mpc/h /// converts to the code units std::cout << "converting the units of the MOKA map" << std::endl; - double fac = map->DS/map->DLS/map->Dlens*map->h/(4*PI*Grav); + double fac = map.DS/map.DLS/map.Dlens*map.h/(4*PI*Grav); - map->convergence *= fac; - map->gamma1 *= fac; - map->gamma2 *= fac; - map->phi *= fac; + map.surface_density *= fac; + map.gamma1_bar *= fac; + map.gamma2_bar *= fac; + map.phi_bar *= fac; fac = 1/(4*PI*Grav); - map->alpha1 *= fac; - map->alpha2 *= fac; + map.alpha1_bar *= fac; + map.alpha2_bar *= fac; checkCosmology(); }else{ @@ -269,25 +258,13 @@ void LensHaloMassMap::initMap() } } -void LensHaloMassMap::convertmap(MOKAmap *map,PixelMapType maptype){ +void LensHaloMassMap::convertmap(MOKAmap &map,PixelMapType maptype){ // TODO: convert units throw std::runtime_error("needs to be finished"); - map->center[0] *= map->Dlens;//(1+map->zlens); - map->center[1] *= map->Dlens;//(1+map->zlens); - - //float pixLMpc = map->boxlMpc/map->nx; // TODO: What if it isn't square? - //float fac = 1.e+10/pixLMpc/pixLMpc/cosmo->gethubble(); - - //map->convergence *= fac; - //map->gamma1 *= fac; - //map->gamma2 *= fac; - - // TODO: Need to check this - //map->alpha1 *= fac*pixLMpc; - //map->alpha2 *= fac*pixLMpc; - + map.center[0] *= map.Dlens;//(1+map.zlens); + map.center[1] *= map.Dlens;//(1+map.zlens) } /** @@ -302,64 +279,62 @@ void LensHaloMassMap::setMap( // must be a square map assert(inputmap.getNx() == inputmap.getNy()); - map = new MOKAmap(); - if(std::numeric_limits::has_infinity) Rmax = std::numeric_limits::infinity(); else Rmax = std::numeric_limits::max(); - map->nx = map->ny = inputmap.getNx(); - map->center[0] = map->center[1] = 0.0; + map.nx = map.ny = inputmap.getNx(); + map.center[0] = map.center[1] = 0.0; - std::size_t size = map->nx*map->ny; + std::size_t size = map.nx*map.ny; - map->convergence.resize(size); - map->alpha1.resize(size); - map->alpha2.resize(size); - map->gamma1.resize(size); - map->gamma2.resize(size); - map->gamma3.resize(size); - map->phi.resize(size); + map.surface_density.resize(size); + // map.alpha1_bar.resize(size); + // map.alpha2_bar.resize(size); + // map.gamma1_bar.resize(size); + // map.gamma2_bar.resize(size); + //map.gamma3_bar.resize(size); + // map.phi_bar.resize(size); - map->zlens = z; + map.zlens = z; - assert(map->nx !=0); + assert(map.nx !=0); // keep it like it is, even if is a rectangle - map->Dlens = cosmo.angDist(0.,map->zlens); // physical - map->boxlrad = inputmap.getRangeX(); - map->boxlarcsec = inputmap.getRangeX()/arcsecTOradians; - map->boxlMpc = inputmap.getRangeX()/map->Dlens; + map.Dlens = cosmo.angDist(0.,map.zlens); // physical + map.boxlrad = inputmap.getRangeX(); + map.boxlarcsec = inputmap.getRangeX()/arcsecTOradians; + map.boxlMpc = inputmap.getRangeX()/map.Dlens; - double pixelarea = inputmap.getResolution()*map->Dlens; + double pixelarea = inputmap.getResolution()*map.Dlens; pixelarea *= pixelarea; for(size_t i=0;iconvergence[i] = massconvertion*inputmap(i)/pixelarea; + map.surface_density[i] = massconvertion*inputmap(i)/pixelarea; } if(zeromean){ double avkappa = 0; for(size_t i=0;iconvergence[i]; + avkappa += map.surface_density[i]; } avkappa /= size; for(size_t i=0;iconvergence[i] -= avkappa; + map.surface_density[i] -= avkappa; } } // kappa is not divided by the critical surface density // they don't need to be preprocessed by fact // create alpha and gamma arrays by FFT - // valid only to force the map to be square map->nx = map->ny = npixels; + // valid only to force the map to be square map.nx = map.ny = npixels; #ifdef ENABLE_FFTW //std:: cout << " preProcessing Map " << std:: endl; - PreProcessFFTWMap(); + map.PreProcessFFTWMap(zerosize); #else std::cout << "Please enable the preprocessor flag ENABLE_FFTW !" << std::endl; exit(1); @@ -372,12 +347,12 @@ void LensHaloMassMap::setMap( /// checks that cosmology in the header of the input fits map is the same as the one set void LensHaloMassMap::checkCosmology() { - if(cosmo.getOmega_matter() == map->omegam) - std::cerr << "LensHaloMassMap: Omega_matter " << cosmo.getOmega_matter() << " (cosmology) != " << map->omegam << " (MOKA)" << std::endl; - if(cosmo.getOmega_lambda() == map->omegal) - std::cerr << "LensHaloMassMap: Omega_lambda " << cosmo.getOmega_lambda() << " (cosmology) != " << map->omegal << " (MOKA)" << std::endl; - if(cosmo.gethubble() == map->h) - std::cerr << "LensHaloMassMap: hubble " << cosmo.gethubble() << " (cosmology) != " << map->h << " (MOKA)" << std::endl; + if(cosmo.getOmega_matter() == map.omegam) + std::cerr << "LensHaloMassMap: Omega_matter " << cosmo.getOmega_matter() << " (cosmology) != " << map.omegam << " (MOKA)" << std::endl; + if(cosmo.getOmega_lambda() == map.omegal) + std::cerr << "LensHaloMassMap: Omega_lambda " << cosmo.getOmega_lambda() << " (cosmology) != " << map.omegal << " (MOKA)" << std::endl; + if(cosmo.gethubble() == map.h) + std::cerr << "LensHaloMassMap: hubble " << cosmo.gethubble() << " (cosmology) != " << map.h << " (MOKA)" << std::endl; } /** @@ -414,7 +389,7 @@ void LensHaloMassMap::assignParams(InputParams& params) * saves the image, by reading off the values from the image tree * and then saving to a fits file and computing the radial profile * of the convergence - */ + * void LensHaloMassMap::saveImage(GridHndl grid,bool saveprofiles){ std::stringstream f; std::string filename; @@ -429,18 +404,18 @@ void LensHaloMassMap::saveImage(GridHndl grid,bool saveprofiles){ PointList::iterator i_tree_pointlist_current(grid->i_tree->pointlist->Top()); do{ - long index = Utilities::IndexFromPosition((*i_tree_pointlist_current)->x,map->nx,map->boxlrad,map->center); + long index = Utilities::IndexFromPosition((*i_tree_pointlist_current)->x,map.nx,map.boxlrad,map.center); if(index > -1){ - map->convergence[index] = (*i_tree_pointlist_current)->kappa; - map->gamma1[index] = (*i_tree_pointlist_current)->gamma[0]; - map->gamma2[index] = (*i_tree_pointlist_current)->gamma[1]; - map->gamma3[index] = (*i_tree_pointlist_current)->gamma[2]; + map.surface_density[index] = (*i_tree_pointlist_current)->kappa; + map.gamma1[index] = (*i_tree_pointlist_current)->gamma[0]; + map.gamma2[index] = (*i_tree_pointlist_current)->gamma[1]; + map.gamma3[index] = (*i_tree_pointlist_current)->gamma[2]; } }while( (--i_tree_pointlist_current)==true ); writeImage(filename); - if(saveprofiles == true){ + /*if(saveprofiles == true){ std:: cout << " saving profile " << std:: endl; double RE3,xxc,yyc; @@ -461,36 +436,24 @@ void LensHaloMassMap::saveImage(GridHndl grid,bool saveprofiles){ filoutEinr << RE1 << " " << RE2 << " " << RE3 << std:: endl; filoutEinr.close(); } -} +}*/ /** * computing and saving the radial profile of the convergence, reduced tangential and parallel shear and of the shear - * */ + * * void LensHaloMassMap::saveProfiles(double &RE3,double &xxc,double &yyc){ - /* measuring the differential and cumulative profile*/ - double xmin = -map->boxlMpc*0.5; - double xmax = map->boxlMpc*0.5; - double drpix = map->boxlMpc/map->nx; + //* measuring the differential and cumulative profile * + double xmin = -map.boxlMpc*0.5; + double xmax = map.boxlMpc*0.5; + double drpix = map.boxlMpc/map.nx; int galaxiesPerBin = 64; - std::valarray pxdist(map->nx*map->ny); - std::valarray red_sgE(map->nx*map->ny),red_sgB(map->nx*map->ny),sgm(map->nx*map->ny); + std::valarray pxdist(map.nx*map.ny); + std::valarray red_sgE(map.nx*map.ny),red_sgB(map.nx*map.ny),sgm(map.nx*map.ny); int i, j; - /* - measure the center of mass - double xcm=0,ycm=0,tot=0; - for(i=0; inx; i++ ) for(j=0; jny; j++ ){ - xcm+=map->convergence[i+map->ny*j]*(xmin+(drpix*0.5)+i*drpix); - ycm+=map->convergence[i+map->ny*j]*(xmin+(drpix*0.5)+j*drpix); - tot+=map->convergence[i+map->ny*j]; - } - xcm/=tot; - ycm/=tot; - xxc = xcm; - yyc = ycm; - */ + // moving center double xcm,ycm; if(flag_background_field==1){ @@ -498,33 +461,33 @@ void LensHaloMassMap::saveProfiles(double &RE3,double &xxc,double &yyc){ ycm = 0.; } else{ - cmass(map->ny,map->convergence,map->x,xcm,ycm); + cmass(map.ny,map.surface_density,map.x,xcm,ycm); } xxc = xcm; yyc = ycm; - int ai = Utilities::locate(map->x,xxc); + int ai = Utilities::locate(map.x,xxc); ai = ((ai > 0) ? ai:0); - ai = ((ai < map->nx-1) ? ai:map->nx-1); - int bi = Utilities::locate(map->x,yyc); + ai = ((ai < map.nx-1) ? ai:map.nx-1); + int bi = Utilities::locate(map.x,yyc); bi = ((bi > 0) ? bi:0); - bi = ((bi < map->nx-1) ? bi:map->nx-1); + bi = ((bi < map.nx-1) ? bi:map.nx-1); std:: cout << " ------------ center ------------- " << std:: endl; std:: cout << " " << xxc << " " << yyc << std:: endl; std:: cout << " " << ai << " " << bi << std:: endl; std:: cout << " ------------------r ------------- " << std:: endl; - for(i=0; inx; i++ ) for(j=0; jny; j++ ){ - pxdist[i+map->ny*j]= sqrt(pow((xmin+(drpix*0.5)+i*drpix-xcm),2) + + for(i=0; ix[i]; - double dy=map->x[j]; + double dx=map.x[i]; + double dy=map.x[j]; double p=atan2( dy, dx ); // check gamma 1 and gamma 2 definition - red_sgE[i+map->ny*j] = (-map->gamma1[i+map->ny*j]*cos(2*p)-map->gamma2[i+map->ny*j]*sin(2*p))/(1.-map->convergence[i+map->ny*j]); - red_sgB[i+map->ny*j] = (map->gamma1[i+map->ny*j]*sin(2*p)-map->gamma2[i+map->ny*j]*cos(2*p))/(1.-map->convergence[i+map->ny*j]); - sgm[i+map->ny*j] = sqrt(pow(map->gamma1[i+map->ny*j],2) + pow(map->gamma2[i+map->ny*j],2)); + red_sgE[i+map.ny*j] = (-map.gamma1[i+map.ny*j]*cos(2*p)-map.gamma2[i+map.ny*j]*sin(2*p))/(1.-map.surface_density[i+map.ny*j]); + red_sgB[i+map.ny*j] = (map.gamma1[i+map.ny*j]*sin(2*p)-map.gamma2[i+map.ny*j]*cos(2*p))/(1.-map.surface_density[i+map.ny*j]); + sgm[i+map.ny*j] = sqrt(pow(map.gamma1[i+map.ny*j],2) + pow(map.gamma2[i+map.ny*j],2)); } - double dr0 = 8.*(0.5*map->boxlMpc)/(map->nx/2.); + double dr0 = 8.*(0.5*map.boxlMpc)/(map.nx/2.); int nbin = int(xmax/dr0); int nbggal=0; int ih; @@ -542,7 +505,7 @@ void LensHaloMassMap::saveProfiles(double &RE3,double &xxc,double &yyc){ infilebgXarcmin2.close(); } // number of galaxies per arcminsquare - double dntbggal=double(nbggal)*(map->boxlarcsec*map->boxlarcsec)/3600.; + double dntbggal=double(nbggal)*(map.boxlarcsec*map.boxlarcsec)/3600.; int ntbggal=int(dntbggal+0.5); std:: vector runi,runj; int ibut; @@ -552,11 +515,11 @@ void LensHaloMassMap::saveProfiles(double &RE3,double &xxc,double &yyc){ // fill the vector with random coordinates of background galaxies srand(ih+94108); for(lrun=0;lrunnx; - if(ibut>map->nx || ibut<0) std:: cout << "1. ibut out of npix range = " << ibut << std:: endl; + ibut = rand()%map.nx; + if(ibut>map.nx || ibut<0) std:: cout << "1. ibut out of npix range = " << ibut << std:: endl; runi.push_back(ibut); - ibut = rand()%map->nx; - if(ibut>map->nx || ibut<0) std:: cout << "2. ibut out of npix range = " << ibut << std:: endl; + ibut = rand()%map.nx; + if(ibut>map.nx || ibut<0) std:: cout << "2. ibut out of npix range = " << ibut << std:: endl; runj.push_back(ibut); } if(runi.size()>ntbggal || runj.size()>ntbggal){ @@ -565,7 +528,7 @@ void LensHaloMassMap::saveProfiles(double &RE3,double &xxc,double &yyc){ std:: cout << " runj.size() = " << runj.size() << std:: endl; } - dr0 = map->boxlMpc*sqrt(galaxiesPerBin/ntbggal); + dr0 = map.boxlMpc*sqrt(galaxiesPerBin/ntbggal); nbin = int(xmax/dr0); } @@ -577,16 +540,16 @@ void LensHaloMassMap::saveProfiles(double &RE3,double &xxc,double &yyc){ // - - - - - - - - - - - - - - - - - // TODO: Carlo: These are all memory leaks! They are never deleted! - double *kprofr = estprof(map->convergence,map->nx,map->ny,pxdist,dr0,xmax,runi,runj,ntbggal); - double *sigmakprof = estsigmaprof(map->convergence,map->nx,map->ny,pxdist,dr0,xmax,runi,runj,ntbggal,kprofr); - double *ckprofr = estcprof(map->convergence,map->nx,map->ny,pxdist,dr0,xmax,runi,runj,ntbggal); - double *sigmackprof = estsigmacprof(map->convergence,map->nx,map->ny,pxdist,dr0,xmax,runi,runj,ntbggal,kprofr); - double *gamma1profr = estprof(red_sgE,map->nx,map->ny,pxdist,dr0,xmax,runi,runj,ntbggal); // reduced shear - double *sigmagamma1prof = estsigmaprof(red_sgE,map->nx,map->ny,pxdist,dr0,xmax,runi,runj,ntbggal,gamma1profr); - double *gamma0profr = estprof(red_sgB,map->nx,map->ny,pxdist,dr0,xmax,runi,runj,ntbggal); - double *sigmagamma0prof = estsigmaprof(red_sgB,map->nx,map->ny,pxdist,dr0,xmax,runi,runj,ntbggal,gamma0profr); - double *gamma2profr = estprof(sgm,map->nx,map->ny,pxdist,dr0,xmax,runi,runj,ntbggal); - double *sigmagamma2prof = estsigmaprof(sgm,map->nx,map->ny,pxdist,dr0,xmax,runi,runj,ntbggal,gamma2profr); + double *kprofr = estprof(map.surface_density,map.nx,map.ny,pxdist,dr0,xmax,runi,runj,ntbggal); + double *sigmakprof = estsigmaprof(map.surface_density,map.nx,map.ny,pxdist,dr0,xmax,runi,runj,ntbggal,kprofr); + double *ckprofr = estcprof(map.surface_density,map.nx,map.ny,pxdist,dr0,xmax,runi,runj,ntbggal); + double *sigmackprof = estsigmacprof(map.surface_density,map.nx,map.ny,pxdist,dr0,xmax,runi,runj,ntbggal,kprofr); + double *gamma1profr = estprof(red_sgE,map.nx,map.ny,pxdist,dr0,xmax,runi,runj,ntbggal); // reduced shear + double *sigmagamma1prof = estsigmaprof(red_sgE,map.nx,map.ny,pxdist,dr0,xmax,runi,runj,ntbggal,gamma1profr); + double *gamma0profr = estprof(red_sgB,map.nx,map.ny,pxdist,dr0,xmax,runi,runj,ntbggal); + double *sigmagamma0prof = estsigmaprof(red_sgB,map.nx,map.ny,pxdist,dr0,xmax,runi,runj,ntbggal,gamma0profr); + double *gamma2profr = estprof(sgm,map.nx,map.ny,pxdist,dr0,xmax,runi,runj,ntbggal); + double *sigmagamma2prof = estsigmaprof(sgm,map.nx,map.ny,pxdist,dr0,xmax,runi,runj,ntbggal,gamma2profr); std::ostringstream fprof; if(flag_background_field==1) fprof << MOKA_input_file << "_only_noise_MAP_radial_prof.dat"; @@ -608,7 +571,7 @@ void LensHaloMassMap::saveProfiles(double &RE3,double &xxc,double &yyc){ << gamma1profr[l] << " " << sigmagamma1prof[l] << " " << gamma0profr[l] << " " << sigmagamma0prof[l] << " " << gamma2profr[l] << " " << sigmagamma2prof[l] << " " - << (dr0*l + dr0/2.)*map->inarcsec << " " << Aanulus*map->inarcsec*map->inarcsec << " " << + << (dr0*l + dr0/2.)*map.inarcsec << " " << Aanulus*map.inarcsec*map.inarcsec << " " << std:: endl; lprofFORre[l] = log10(kprofr[l] + gamma2profr[l]); lrOFr[l] = log10(dr0*l + dr0/2.); @@ -616,8 +579,8 @@ void LensHaloMassMap::saveProfiles(double &RE3,double &xxc,double &yyc){ filoutprof.close(); RE3 = Utilities::InterpolateYvec(lprofFORre,lrOFr,0.); if((RE3-lrOFr[0])<1e-4 || (RE3-lrOFr[nbin-1])<1e-4) RE3=0; - else RE3 = pow(10.,RE3)*map->inarcsec; -} + else RE3 = pow(10.,RE3)*map.inarcsec; +}*/ /** \ingroup DeflectionL2 * @@ -635,82 +598,39 @@ void LensHaloMassMap::force_halo(double *alpha ) { - /* - long index = Utilities::IndexFromPosition(xx,map->nx,map->boxlMpc/map->h,map->center); - - if(index > -1){ - alpha[0] = map->alpha1[index]; - alpha[1] = map->alpha2[index]; - gamma[0] = map->gamma1[index]; - gamma[1] = map->gamma2[index]; - gamma[2] = 0.0; - *kappa = map->convergence[index]; - } - else{ - alpha[0] = alpha[1] = 0.0; - gamma[0] = gamma[1] = gamma[2] = 0.0; - *kappa = 0.0; - } - */ - // interpolate from the maps - Utilities::Interpolator > interp(xx,map->nx,map->boxlMpc,map->ny - ,map->ny*map->boxlMpc/map->nx,map->center); + Utilities::Interpolator > interp(xx,map.nx,map.boxlMpc,map.ny + ,map.ny*map.boxlMpc/map.nx,map.center); - assert(map->nx == map->ny); - - //size_t N = map->nx * map->ny; - - map->convergence.size(); - - /*assert(map->alpha1.size() == N); - assert(map->alpha2.size() == N); - assert(map->gamma1.size() == N); - assert(map->gamma2.size() == N); - assert(map->convergence.size() == N); - assert(map->phi.size() == N); + assert(map.nx == map.ny); + + //size_t N = map.nx * map.ny; + /*assert(map.alpha1.size() == N); + assert(map.alpha2.size() == N); + assert(map.gamma1.size() == N); + assert(map.gamma2.size() == N); + assert(map.surface_density.size() == N); + assert(map.phi.size() == N); */ - alpha[0] = interp.interpolate(map->alpha1); - alpha[1] = interp.interpolate(map->alpha2); - gamma[0] = interp.interpolate(map->gamma1); - gamma[1] = interp.interpolate(map->gamma2); + alpha[0] = interp.interpolate(map.alpha1_bar); + alpha[1] = interp.interpolate(map.alpha2_bar); + gamma[0] = interp.interpolate(map.gamma1_bar); + gamma[1] = interp.interpolate(map.gamma2_bar); gamma[2] = 0.0; - *kappa = interp.interpolate(map->convergence); - *phi = interp.interpolate(map->phi); + *kappa = interp.interpolate(map.surface_density); + *phi = interp.interpolate(map.phi_bar); //assert(alpha[0] == alpha[0] && alpha[1] == alpha[1]); return; } -/** - * compute the signal of \lambda_r and \lambda_t - */ -void LensHaloMassMap::estSignLambdas(){ - map->Signlambdar.resize(map->nx*map->ny); - map->Signlambdat.resize(map->nx*map->ny); - double gamma,lambdar,lambdat; - int i, j; - for(i=0;inx;i++) - for(j=0;jny;j++){ - gamma = sqrt(pow(map->gamma1[i+map->ny*j],2) + - pow(map->gamma2[i+map->ny*j],2)); - lambdat=1-map->convergence[i+map->ny*j]-gamma; - lambdar=1-map->convergence[i+map->ny*j]+gamma; - - if(lambdar>=0) map->Signlambdar[i+map->ny*j]=1; - else map->Signlambdar[i+map->ny*j]=-1; - - if(lambdat>=0) map->Signlambdat[i+map->ny*j]=1; - else map->Signlambdat[i+map->ny*j]=-1; - } -} /** * measure the effective and the median Einstein radii of the connected critical * points present at the halo center - */ + * void LensHaloMassMap::EinsteinRadii(double &RE1, double &RE2, double &xxc, double &yyc){ double signV; // std:: vector xci1,yci1; @@ -727,26 +647,26 @@ void LensHaloMassMap::EinsteinRadii(double &RE1, double &RE2, double &xxc, doubl filoutcrit.open(filenamecrit.c_str()); // define the critical points in the map int i, j; - for(i=1;inx-1;i++) - for(j=1;jny-1;j++){ - signV=map->Signlambdar[i-1+map->ny*j]+map->Signlambdar[i+map->ny*(j-1)]+ - map->Signlambdar[i+1+map->ny*j]+map->Signlambdar[i+map->ny*(j+1)]; + for(i=1;ix[i]); - // yci1.push_back(map->x[j]); + // xci1.push_back(map.x[i]); + // yci1.push_back(map.x[j]); filoutcrit << "circle(" << i << "," << j << ",0.5)" << std:: endl; } - signV=map->Signlambdat[i-1+map->ny*j]+map->Signlambdat[i+map->ny*(j-1)]+ - map->Signlambdat[i+1+map->ny*j]+map->Signlambdat[i+map->ny*(j+1)]; + //signV=map.Signlambdat[i-1+map.ny*j]+map.Signlambdat[i+map.ny*(j-1)]+ + //map.Signlambdat[i+1+map.ny*j]+map.Signlambdat[i+map.ny*(j+1)]; if(fabs(signV)<4.){ - xci2.push_back(map->x[i]); - yci2.push_back(map->x[j]); + xci2.push_back(map.x[i]); + yci2.push_back(map.x[j]); filoutcrit << "circle(" << i << "," << j << ",0.5)" << std:: endl; } } filoutcrit.close(); - double pixDinL = map->boxlMpc/double(map->nx); - /* measure the Einstein radius */ + double pixDinL = map.boxlMpc/double(map.nx); + //* measure the Einstein radius * std:: vector xci,yci; //for(int ii=0;iiinarcsec; + double distcentre=sqrt((xercm-xxc)*(xercm-xxc)+(yercm-yyc)*(yercm-yyc))*map.inarcsec; std:: vector::iterator maxit, minit; // find the min and max elements in the vector maxit = max_element(xcpoints.begin(), xcpoints.end()); @@ -786,20 +706,20 @@ void LensHaloMassMap::EinsteinRadii(double &RE1, double &RE2, double &xxc, doubl double xmincpoints,xmaxcpoints; xmaxcpoints = *maxit; xmincpoints = *minit; - int imin = Utilities::locate(map->x,xmincpoints); + int imin = Utilities::locate(map.x,xmincpoints); imin = ((imin > 0) ? imin:0); - imin = ((imin < map->nx-1) ? imin:map->nx-1); - // imin=GSL_MIN( GSL_MAX( imin, 0 ), map->nx-1 ); - int imax = Utilities::locate(map->x,xmaxcpoints); + imin = ((imin < map.nx-1) ? imin:map.nx-1); + // imin=GSL_MIN( GSL_MAX( imin, 0 ), map.nx-1 ); + int imax = Utilities::locate(map.x,xmaxcpoints); imax = ((imax > 0) ? imax:0); - imax = ((imax < map->nx-1) ? imax:map->nx-1); - // imax=GSL_MIN( GSL_MAX( imax, 0 ), map->nx-1 ); + imax = ((imax < map.nx-1) ? imax:map.nx-1); + // imax=GSL_MIN( GSL_MAX( imax, 0 ), map.nx-1 ); std:: vector ysup,yinf,xsup,xinf; for(int ii=imin;ii<=imax;ii++){ std:: vectorybut; int condition=0; for(int ji=0;jix[ii]) ycounts; - for(int ji=0;jinx;ji++){ - if(map->x[ji]>=yinf[ii] && map->x[ji]<=ysup[ii]){ - ycounts.push_back(map->x[ji]); + for(int ji=0;ji=yinf[ii] && map.x[ji]<=ysup[ii]){ + ycounts.push_back(map.x[ji]); } } int ncounts = ycounts.size(); @@ -844,8 +764,8 @@ void LensHaloMassMap::EinsteinRadii(double &RE1, double &RE2, double &xxc, doubl for(int ii=nc-1;ii>=0;ii--){ RE.push_back(sqrt(pow(xsup[ii]-xercm,2) + pow(ysup[ii]-yercm,2))); } - RE1=map->inarcsec*sqrt(pixDinL*pixDinL*npixIN/M_PI); - RE2=map->inarcsec*Utilities::median(RE); + RE1=map.inarcsec*sqrt(pixDinL*pixDinL*npixIN/M_PI); + RE2=map.inarcsec*Utilities::median(RE); // if is not in the centre std:: cout << "distance " << distcentre << std:: endl; if(distcentre>1.5*RE2){ @@ -858,7 +778,8 @@ void LensHaloMassMap::EinsteinRadii(double &RE1, double &RE2, double &xxc, doubl RE1=0.; RE2=0.; } -} +}*/ + /** * saves MAP properties, computing the radial profile * of the convergence and shear @@ -875,7 +796,7 @@ void LensHaloMassMap::saveImage(bool saveprofiles){ writeImage(filename); - if(saveprofiles == true){ + /*if(saveprofiles == true){ std:: cout << " saving profile " << std:: endl; double RE3,xxc,yyc; saveProfiles(RE3,xxc,yyc); @@ -894,6 +815,6 @@ void LensHaloMassMap::saveImage(bool saveprofiles){ filoutEinr << "# effective median from_profles" << std:: endl; filoutEinr << RE1 << " " << RE2 << " " << RE3 << std:: endl; filoutEinr.close(); - } + }*/ } diff --git a/include/MOKAlens.h b/include/MOKAlens.h index ffec0fbc..09428867 100644 --- a/include/MOKAlens.h +++ b/include/MOKAlens.h @@ -1,8 +1,6 @@ /* * MOKAlens.h * - * Created on: Jun 8, 2012 - * Author: mpetkova */ @@ -17,6 +15,10 @@ #include +#ifdef ENABLE_FITS +#include +#endif + /** * \brief The MOKA map structure, containing all quantities that define it * @@ -29,16 +31,14 @@ */ struct MOKAmap{ /// values for the map - std::valarray convergence; - std::valarray alpha1; - std::valarray alpha2; - std::valarray gamma1; - std::valarray gamma2; - std::valarray gamma3; - std::valarray phi; - std::valarray Signlambdar; - std::valarray Signlambdat; - std::vector x; + std::valarray surface_density; // Msun / Mpc^2 + std::valarray alpha1_bar; + std::valarray alpha2_bar; + std::valarray gamma1_bar; + std::valarray gamma2_bar; + //std::valarray gamma3_bar; + std::valarray phi_bar; + //std::vector x; int nx,ny; // boxlMpc is Mpc/h for MOKA /// lens and source properties @@ -49,6 +49,25 @@ struct MOKAmap{ double omegam,omegal,h,wq; double inarcsec; double center[2]; + + MOKAmap(){}; + +#ifdef ENABLE_FITS + + MOKAmap(std::string MOKA_input_file,bool zeromean,const COSMOLOGY &cosmo){ + read(MOKA_input_file,zeromean,cosmo); + } + + void read(std::string MOKA_input_file,bool zeromean,const COSMOLOGY &cosmo); + void write(std::string filename); + +#endif + +#ifdef ENABLE_FFTW + // this calculates the other lensing quantities from the density map + void PreProcessFFTWMap(float zerosize); +#endif + }; /** @@ -96,16 +115,16 @@ class LensHaloMassMap : public LensHalo void checkCosmology(); void saveImage(bool saveprofile=true); - void saveKappaProfile(); - void saveGammaProfile(); - void saveProfiles(double &RE3, double &xxc, double &yyc); + //void saveKappaProfile(); + //void saveGammaProfile(); + //void saveProfiles(double &RE3, double &xxc, double &yyc); void force_halo(double *alpha,KappaType *kappa,KappaType *gamma,KappaType *phi,double const *xcm,bool subtract_point=false,PosType screening = 1.0); - void saveImage(GridHndl grid,bool saveprofiles); + //void saveImage(GridHndl grid,bool saveprofiles); - void estSignLambdas(); - void EinsteinRadii(double &RE1, double &RE2, double &xxc, double &yyc); + //void estSignLambdas(); + //void EinsteinRadii(double &RE1, double &RE2, double &xxc, double &yyc); void getDims(); void readMap(); @@ -115,40 +134,38 @@ class LensHaloMassMap : public LensHalo void writeImage(std::string fn); /// return center in physical Mpc - const double* getCenter() const { return map->center; } + const double* getCenter() const { return map.center; } /// return range of input map in rad - double getRangeRad() const { return map->boxlrad; } + double getRangeRad() const { return map.boxlrad; } /// return range of input map in physical Mpc - double getRangeMpc() const { return map->boxlMpc; } + double getRangeMpc() const { return map.boxlMpc; } /// /// return number of pixels on a side in original map size_t getN() const { - if(map->nx != map->ny) + if(map.nx != map.ny) throw std::runtime_error("mass map is not square"); - return map->nx; + return map.nx; } /// return number of pixels on a x-axis side in original map - size_t getNx() const { return map->nx; } + size_t getNx() const { return map.nx; } /// return number of pixels on a y-axis side in original map - size_t getNy() const { return map->ny; } + size_t getNy() const { return map.ny; } private: PixelMapType maptype; void initMap(); - void convertmap(MOKAmap *map,PixelMapType maptype); - MOKAmap* map; + void convertmap(MOKAmap &map,PixelMapType maptype); + MOKAmap map; const COSMOLOGY& cosmo; - void PreProcessFFTWMap(); int zerosize; bool zeromean; }; - - void make_friendship(int ii,int ji,int np,std:: vector &friends, std:: vector &pointdist); int fof(double l,std:: vector xci, std:: vector yci, std:: vector &groupid); -#endif /* MOKALENS_H_ */ +#endif +/* MOKALENS_H_ */ From 03bb2a3411c1beda52f2bfeffdb13abb801fcdaa Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Thu, 10 Jan 2019 11:17:48 +0100 Subject: [PATCH 107/227] Changed multi map maps to float. Some other small things. --- MultiPlane/multimap.cpp | 79 +++++++++++++++++--------------- TreeCode/quadTree.cpp | 2 +- TreeCode_link/Tree.cpp | 2 +- TreeCode_link/curve_routines.cpp | 4 +- include/concave_hull.h | 6 +-- include/multimap.h | 12 ++--- 6 files changed, 54 insertions(+), 51 deletions(-) diff --git a/MultiPlane/multimap.cpp b/MultiPlane/multimap.cpp index e96f31c1..5fd36b8b 100644 --- a/MultiPlane/multimap.cpp +++ b/MultiPlane/multimap.cpp @@ -14,7 +14,7 @@ LensHaloMultiMap::LensHaloMultiMap( ,double z ,double mass_unit ,const COSMOLOGY &c - ):LensHalo(),cosmo(c),fitsfilename(fitsfile),mass_unit(mass_unit) + ):LensHalo(),cosmo(c),mass_unit(mass_unit),fitsfilename(fitsfile) { zerosize = 1; @@ -54,7 +54,7 @@ LensHaloMultiMap::LensHaloMultiMap( long chunk = MIN(nx*ny/Noriginal[0],Noriginal[1]); // same size as long range grid if( chunk == 0) chunk = MIN(100*desample,Noriginal[1]); - + std::vector first(2); std::vector last(2); @@ -262,12 +262,12 @@ void LensHaloMultiMap::force_halo(double *alpha // interpolate from the maps - Utilities::Interpolator > interp(xx + Utilities::Interpolator > interp(xx ,long_range_map.nx,long_range_map.boxlMpc ,long_range_map.ny ,long_range_map.ny*long_range_map.boxlMpc/long_range_map.nx ,long_range_map.center.x); - Utilities::Interpolator > short_interp(xx + Utilities::Interpolator > short_interp(xx ,short_range_map.nx,short_range_map.boxlMpc ,short_range_map.ny ,short_range_map.ny*short_range_map.boxlMpc/short_range_map.nx @@ -297,22 +297,23 @@ void LensMap::read_header(std::string fits_input_file,float h){ std::auto_ptr ff(new CCfits::FITS (fits_input_file, CCfits::Read)); - CCfits::PHDU *h0=&ff->pHDU(); - - h0->readAllKeys(); + //CCfits::PHDU *h0=&ff->pHDU(); + CCfits::PHDU &h0 = ff->pHDU(); + + h0.readAllKeys(); - assert(h0->axes() >= 2); + assert(h0.axes() >= 2); - nx = h0->axis(0); - ny = h0->axis(1); + nx = h0.axis(0); + ny = h0.axis(1); try{ /* these are always present in ea*/ float wlow,wup,res; - h0->readKey ("CD1_1",res); // resolution in degrees - h0->readKey ("REDSHIFT",z); - h0->readKey ("WLOW",wlow); - h0->readKey ("WUP",wup); + h0.readKey ("CD1_1",res); // resolution in degrees + h0.readKey ("REDSHIFT",z); + h0.readKey ("WLOW",wlow); + h0.readKey ("WUP",wup); double D = 3*(pow(wup,4) - pow(wlow,4))/(pow(wup,3) - pow(wlow,3))/4; if(wlow == wup) D = wlow; @@ -347,20 +348,20 @@ void LensMap::read(std::string fits_input_file,float h){ std:: cout << " reading lens density map file: " << fits_input_file << std:: endl; std::auto_ptr ff(new CCfits::FITS (fits_input_file, CCfits::Read)); - CCfits::PHDU *h0=&ff->pHDU(); + CCfits::PHDU &h0 = ff->pHDU(); - h0->readAllKeys(); + h0.readAllKeys(); - assert(h0->axes() >= 2); + assert(h0.axes() >= 2); - nx = h0->axis(0); - ny = h0->axis(1); + nx = h0.axis(0); + ny = h0.axis(1); size_t size = nx*ny; // principal HDU is read - h0->read(surface_density); - int nhdu = h0->axes(); + h0.read(surface_density); + int nhdu = h0.axes(); if(nhdu > 2){ // file contains other lensing quantities alpha1_bar.resize(size); @@ -378,15 +379,15 @@ void LensMap::read(std::string fits_input_file,float h){ h3.read(gamma1_bar); CCfits::ExtHDU &h4=ff->extension(4); h4.read(gamma2_bar); - std::cout << *h0 << h1 << h2 << h3 << h4 << std::endl; + std::cout << h0 << h1 << h2 << h3 << h4 << std::endl; } try{ /* these are always present in ea*/ float wlow,wup,res; - h0->readKey ("CD1_1",res); // recall you that MOKA Mpc/h - h0->readKey ("REDSHIFT",z); - h0->readKey ("WLOW",wlow); - h0->readKey ("WUP",wup); + h0.readKey ("CD1_1",res); // recall you that MOKA Mpc/h + h0.readKey ("REDSHIFT",z); + h0.readKey ("WLOW",wlow); + h0.readKey ("WUP",wup); double D = 3*(pow(wup,4) - pow(wlow,4))/(pow(wup,3) - pow(wlow,3))/4; D /= (1+z)*h; @@ -424,16 +425,16 @@ void LensMap::read_sub(std::string fits_input_file //std:: cout << " reading lens density map file: " << fits_input_file << std:: endl; std::auto_ptr ff(new CCfits::FITS (fits_input_file, CCfits::Read)); - CCfits::PHDU *h0=&ff->pHDU(); + CCfits::PHDU &h0 = ff->pHDU(); - h0->readAllKeys(); + h0.readAllKeys(); /* these are always present in ea*/ float wlow,wup,res; - h0->readKey ("CD1_1",res); // resolution in - h0->readKey ("REDSHIFT",z); - h0->readKey ("WLOW",wlow); - h0->readKey ("WUP",wup); + h0.readKey ("CD1_1",res); // resolution in + h0.readKey ("REDSHIFT",z); + h0.readKey ("WLOW",wlow); + h0.readKey ("WUP",wup); double D = 3*(pow(wup,4) - pow(wlow,4))/(pow(wup,3) - pow(wlow,3))/4; if(wlow == wup) D = wup; @@ -441,14 +442,16 @@ void LensMap::read_sub(std::string fits_input_file res *= degreesTOradians*D; boxlMpc = res*nx; - assert(h0->axes() >= 2); + assert(h0.axes() >= 2); std::vector stride = {1,1}; // principal HDU is read - h0->read(surface_density,first,last,stride); - - nx = h0->axis(0); - ny = h0->axis(1); + std::cout << surface_density.size() << std::endl; + h0.read(surface_density,first,last,stride); + std::cout << surface_density.size() << std::endl; + + nx = h0.axis(0); + ny = h0.axis(1); ff.release(); @@ -463,7 +466,7 @@ void LensMap::read_sub(std::string fits_input_file nx = last[0] - first[0] + 1; ny = last[1] - first[1] + 1; - boxlMpc *= nx*1./h0->axis(0); + boxlMpc *= nx*1./h0.axis(0); } diff --git a/TreeCode/quadTree.cpp b/TreeCode/quadTree.cpp index 140ce529..7b9482d1 100644 --- a/TreeCode/quadTree.cpp +++ b/TreeCode/quadTree.cpp @@ -763,7 +763,7 @@ void TreeQuadHalos::walkTree_recur(QBranchNB *branch,PosType const *ray,PosType PosType xcm[2],rcm2cell,rcm2,tmp,boxsize2; IndexType i; std::size_t tmp_index; - PosType arg1, arg2, prefac; + PosType prefac; /*bool notscreen = true; if(inv_screening_scale2 != 0) notscreen = BoxIntersectCircle(ray,3*sqrt(1.0/inv_screening_scale2), branch->boundary_p1, branch->boundary_p2); diff --git a/TreeCode_link/Tree.cpp b/TreeCode_link/Tree.cpp index 8d389dc6..bca7e4d1 100644 --- a/TreeCode_link/Tree.cpp +++ b/TreeCode_link/Tree.cpp @@ -974,7 +974,7 @@ PixelMap ImageFinding::mapCriticalCurves(const std::vector &init_points typename std::list::iterator p,nextpoint; //auto p = leftovers.begin(); - double minarea,area,co1,co2; + double minarea,area,co1;//,co2; size_t i; Point_2d vo,v1,v2; - int count = 0; + //int count = 0; while(edges.front().length > scale ){ if(leftovers.size() == 0) break; @@ -426,7 +426,7 @@ void concave(std::vector &init_points typename std::list::iterator p,nextpoint; //auto p = leftovers.begin(); - double minarea,area,co1,co2; + double minarea,area,co1; size_t i; Point_2d vo,v1,v2; diff --git a/include/multimap.h b/include/multimap.h index 026e6cb1..a5227074 100644 --- a/include/multimap.h +++ b/include/multimap.h @@ -31,13 +31,13 @@ */ struct LensMap{ /// values for the map - std::valarray surface_density; // Msun / Mpc^2 - std::valarray alpha1_bar; - std::valarray alpha2_bar; - std::valarray gamma1_bar; - std::valarray gamma2_bar; + std::valarray surface_density; // Msun / Mpc^2 + std::valarray alpha1_bar; + std::valarray alpha2_bar; + std::valarray gamma1_bar; + std::valarray gamma2_bar; //std::valarray gamma3_bar; - std::valarray phi_bar; + std::valarray phi_bar; //std::vector x; int nx,ny; // boxlMpc is Mpc/h for MOKA From 6167830f27954da5a6053e224701c6010f7768d1 Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Thu, 10 Jan 2019 11:43:23 +0100 Subject: [PATCH 108/227] Fixed a problem with some compilers where lambda functions passed into std::sort() need to take const arguments. This was not a problem for clang. --- MultiPlane/particle_lens.cpp | 8 ++++---- TreeCode_link/utilities.cpp | 13 ++++++++----- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/MultiPlane/particle_lens.cpp b/MultiPlane/particle_lens.cpp index 4b20f748..0c25239f 100644 --- a/MultiPlane/particle_lens.cpp +++ b/MultiPlane/particle_lens.cpp @@ -309,7 +309,7 @@ bool MakeParticleLenses::readCSV(int columns_used){ if(ntypes > 1){ // sort by type - std::sort(data.begin(),data.end(),[](ParticleType &a1,ParticleType &a2){return a1.type < a2.type;}); + std::sort(data.begin(),data.end(),[](const ParticleType &a1,const ParticleType &a2){return a1.type < a2.type;}); } masses = {0,0,0,0,0,0}; } @@ -361,7 +361,7 @@ bool MakeParticleLenses::readGadget2(bool ignore_type){ } // sort by type - std::sort(data.begin(),data.end(),[](ParticleType &a1,ParticleType &a2){return a1.type < a2.type;}); + std::sort(data.begin(),data.end(),[](const ParticleType &a1,const ParticleType &a2){return a1.type < a2.type;}); ParticleType *pp; size_t skip = 0; @@ -471,7 +471,7 @@ void MakeParticleLenses::radialCut(Point_3d center,double radius){ if(ntypes > 1){ // sort by type - std::sort(data.begin(),data.end(),[](ParticleType &a1,ParticleType &a2){return a1.type < a2.type;}); + std::sort(data.begin(),data.end(),[](const ParticleType &a1,const ParticleType &a2){return a1.type < a2.type;}); } } @@ -520,7 +520,7 @@ void MakeParticleLenses::cylindricalCut(Point_2d center,double radius){ if(ntypes > 1){ // sort by type - std::sort(data.begin(),data.end(),[](ParticleType &a1,ParticleType &a2){return a1.type < a2.type;}); + std::sort(data.begin(),data.end(),[](const ParticleType &a1,const ParticleType &a2){return a1.type < a2.type;}); } } diff --git a/TreeCode_link/utilities.cpp b/TreeCode_link/utilities.cpp index 9df0b8e1..05b962d5 100644 --- a/TreeCode_link/utilities.cpp +++ b/TreeCode_link/utilities.cpp @@ -767,7 +767,7 @@ Utilities::XYcsvLookUp::XYcsvLookUp( //********************************** // sort by redshift - std::sort(data.begin(),data.end(), [this](std::vector &v1,std::vector &v2){return v1[Xindex] < v2[Xindex];}); + std::sort(data.begin(),data.end(), [this](const std::vector &v1,const std::vector &v2){return v1[Xindex] < v2[Xindex];}); //&v1,std::vector &v2){return v1[1] < v2[1];}); NinXbins = data.size()/Nxbins; Xborders.resize(Nxbins); @@ -792,7 +792,8 @@ Utilities::XYcsvLookUp::XYcsvLookUp( // sort by mass within x bins for(int i=0 ; i < Nxbins ; ++i){ - std::sort(borders[i],borders[i+1], [this](std::vector &v1,std::vector &v2){return v1[Yindex] < v2[Yindex];}); + std::sort(borders[i],borders[i+1], [this](const std::vector &v1 + ,const std::vector &v2){return v1[Yindex] < v2[Yindex];}); } current = data.begin(); @@ -839,7 +840,8 @@ Utilities::XYcsvLookUp::XYcsvLookUp( } // sort by redshift - std::sort(data.begin(),data.end(), [this](std::vector &v1,std::vector &v2){return v1[Xindex] < v2[Xindex];}); + std::sort(data.begin(),data.end(), [this](const std::vector &v1 + ,const std::vector &v2){return v1[Xindex] < v2[Xindex];}); size_t Nxbins = Xborders.size(); NinXbins = data.size()/Nxbins; @@ -859,7 +861,8 @@ Utilities::XYcsvLookUp::XYcsvLookUp( // sort by mass within x bins for(int i=0 ; i < Nxbins ; ++i){ - std::sort(borders[i],borders[i+1], [this](std::vector &v1,std::vector &v2){return v1[Yindex] < v2[Yindex];}); + std::sort(borders[i],borders[i+1], [this](const std::vector &v1 + ,const std::vector &v2){return v1[Yindex] < v2[Yindex];}); } current = data.begin(); @@ -868,7 +871,7 @@ Utilities::XYcsvLookUp::XYcsvLookUp( std::vector Utilities::XYcsvLookUp::find(double x,double y){ long xbin = Utilities::locate(Xborders, x); current = std::upper_bound(borders[xbin],borders[xbin+1],y - , [this](double y,std::vector &v1){return y < v1[Yindex];}); + , [this](double y,const std::vector &v1){return y < v1[Yindex];}); return *current; } From b56845ae858954f1d697f7a7ed78ba3bdd422b6b Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Wed, 16 Jan 2019 18:48:38 +0100 Subject: [PATCH 109/227] Some problem came up that makes all the values NAN. --- MultiPlane/multimap.cpp | 103 +++++++++++++++++++++++++++++++++++----- include/gridmap.h | 6 +++ include/multimap.h | 27 ++++++++--- include/point.h | 16 +++++++ 4 files changed, 135 insertions(+), 17 deletions(-) diff --git a/MultiPlane/multimap.cpp b/MultiPlane/multimap.cpp index 5fd36b8b..d3f83e95 100644 --- a/MultiPlane/multimap.cpp +++ b/MultiPlane/multimap.cpp @@ -17,6 +17,8 @@ LensHaloMultiMap::LensHaloMultiMap( ):LensHalo(),cosmo(c),mass_unit(mass_unit),fitsfilename(fitsfile) { + ff = new CCfits::FITS (fitsfilename, CCfits::Read); + zerosize = 1; setZlens(z); @@ -50,6 +52,14 @@ LensHaloMultiMap::LensHaloMultiMap( size_t nx = long_range_map.nx = submap.nx / desample ; size_t ny = long_range_map.ny = nx * submap.ny * 1./ submap.nx ; + { + Point_2d dmap = {submap.boxlMpc,ny*submap.boxlMpc/nx}; + dmap /= 2; + long_range_map.center = {0,0}; + long_range_map.upperright = long_range_map.center + dmap; + long_range_map.lowerleft = long_range_map.center - dmap; + } + long_range_map.surface_density.resize(nx*ny,0); long chunk = MIN(nx*ny/Noriginal[0],Noriginal[1]); // same size as long range grid @@ -71,7 +81,8 @@ LensHaloMultiMap::LensHaloMultiMap( if( j%chunk == 0 ){ first[1] = j+1; last[1] = MIN(j + chunk,Noriginal[1]); - submap.read_sub(fitsfilename,first,last,cosmo.gethubble()); + //submap.read_sub(fitsfilename,first,last,cosmo.gethubble()); + submap.read_sub(ff,first,last,cosmo.gethubble()); kj = 0; }else{ kj += submap.nx; @@ -83,6 +94,7 @@ LensHaloMultiMap::LensHaloMultiMap( assert(ii + kjj < long_range_map.surface_density.size() ); assert(i + kj < submap.surface_density.size() ); long_range_map.surface_density[ ii + kjj ] += submap.surface_density[ i + kj ]; + assert(!isnan(submap.surface_density[ i + kj ])); } } @@ -90,6 +102,7 @@ LensHaloMultiMap::LensHaloMultiMap( // convert to for(auto &p : long_range_map.surface_density){ p /= area; + assert(!isnan(p)); } wlr.rs2 = wsr.rs2 = rs2; @@ -101,6 +114,10 @@ LensHaloMultiMap::LensHaloMultiMap( long_range_map.write("!" + fitsfile + "_lr.fits"); + for(auto p : long_range_map.surface_density){ // ?????? + assert(!isnan(p)); + } + // ??? don //bigmap.boxlMpc = long_range_map.boxlMpc; //bigmap.PreProcessFFTWMap(1.0,wsr); @@ -116,8 +133,8 @@ void LensHaloMultiMap::submap(Point_2d ll,Point_2d ur){ lower_left[0] = lround(ll[0]); lower_left[1] = lround(ll[1]); - lower_left[0] = lround(ur[0]); - lower_left[1] = lround(ur[1]); + upper_right[0] = lround(ur[0]); + upper_right[1] = lround(ur[1]); submap(lower_left,upper_right); } @@ -182,7 +199,8 @@ void LensHaloMultiMap::submap( if(jj >= Noriginal[1] ) jj -= Noriginal[1]; first_sub[1] = jj + 1; last_sub[1] = MIN(jj + chunk + 1,Noriginal[1]); - partmap.read_sub(fitsfilename,first_sub,last_sub,cosmo.gethubble()); + //partmap.read_sub(fitsfilename,first_sub,last_sub,cosmo.gethubble()); + partmap.read_sub(ff,first_sub,last_sub,cosmo.gethubble()); k = 0; }else{ ++k; @@ -236,14 +254,14 @@ void LensHaloMultiMap::submap( } } - short_range_map.center = short_range_map.lowerleft = short_range_map.upperright = long_range_map.lowerleft; + short_range_map.lowerleft = short_range_map.upperright = long_range_map.lowerleft; short_range_map.lowerleft[0] += lower_left[0]*res; short_range_map.lowerleft[1] += lower_left[1]*res; short_range_map.upperright[0] += upper_right[0]*res; short_range_map.upperright[1] += upper_right[1]*res; - short_range_map.center = (map.lowerleft + map.upperright)/2; + short_range_map.center = (short_range_map.lowerleft + short_range_map.upperright)/2; short_range_map.boxlMpc = nx*res; short_range_map.write("!" + fitsfilename + "_sr.fits"); @@ -273,7 +291,7 @@ void LensHaloMultiMap::force_halo(double *alpha ,short_range_map.ny*short_range_map.boxlMpc/short_range_map.nx ,short_range_map.center.x); - assert(long_range_map.nx == long_range_map.ny); + //assert(long_range_map.nx == long_range_map.ny); alpha[0] = interp.interpolate(long_range_map.alpha1_bar); alpha[1] = interp.interpolate(long_range_map.alpha2_bar); @@ -283,6 +301,10 @@ void LensHaloMultiMap::force_halo(double *alpha *kappa = interp.interpolate(long_range_map.surface_density); //*phi = interp.interpolate(long_range_map.phi_bar); + assert(alpha[0] == alpha[0] && alpha[1] == alpha[1]); + assert(gamma[0] == gamma[0] && gamma[1] == gamma[1]); + assert(kappa == kappa); + alpha[0] += short_interp.interpolate(short_range_map.alpha1_bar); alpha[1] += short_interp.interpolate(short_range_map.alpha2_bar); gamma[0] += short_interp.interpolate(short_range_map.gamma1_bar); @@ -290,6 +312,10 @@ void LensHaloMultiMap::force_halo(double *alpha *kappa += short_interp.interpolate(short_range_map.surface_density); //*phi = interp.interpolate(short_range_map.phi_bar); + assert(alpha[0] == alpha[0] && alpha[1] == alpha[1]); + assert(gamma[0] == gamma[0] && gamma[1] == gamma[1]); + assert(kappa == kappa); + return; } @@ -423,8 +449,16 @@ void LensMap::read_sub(std::string fits_input_file ){ //std:: cout << " reading lens density map file: " << fits_input_file << std:: endl; - std::auto_ptr ff(new CCfits::FITS (fits_input_file, CCfits::Read)); - + std::unique_ptr ff(new CCfits::FITS (fits_input_file, CCfits::Read)); + /*std::unique_ptr ff(nullptr); + try{ + //std::unique_ptr ff(new CCfits::FITS (fits_input_file, CCfits::Read)); + + ff.reset(new CCfits::FITS (fits_input_file, CCfits::Read)); + } + catch(CCfits::FitsError){ + std::cerr << " error in opening fits file " << fits_input_file << std::endl; + }*/ CCfits::PHDU &h0 = ff->pHDU(); h0.readAllKeys(); @@ -446,9 +480,9 @@ void LensMap::read_sub(std::string fits_input_file std::vector stride = {1,1}; // principal HDU is read - std::cout << surface_density.size() << std::endl; + //std::cout << surface_density.size() << std::endl; h0.read(surface_density,first,last,stride); - std::cout << surface_density.size() << std::endl; + //std::cout << surface_density.size() << std::endl; nx = h0.axis(0); ny = h0.axis(1); @@ -470,6 +504,53 @@ void LensMap::read_sub(std::string fits_input_file } +void LensMap::read_sub(CCfits::FITS *ff + ,const std::vector &first + ,const std::vector &last + ,float h + ){ + CCfits::PHDU &h0 = ff->pHDU(); + + h0.readAllKeys(); + + // these are always present in each + float wlow,wup,res; + h0.readKey ("CD1_1",res); // resolution in + h0.readKey ("REDSHIFT",z); + h0.readKey ("WLOW",wlow); + h0.readKey ("WUP",wup); + + double D = 3*(pow(wup,4) - pow(wlow,4))/(pow(wup,3) - pow(wlow,3))/4; + if(wlow == wup) D = wup; + D /= (1+z)*h; + res *= degreesTOradians*D; + boxlMpc = res*nx; + + assert(h0.axes() >= 2); + std::vector stride = {1,1}; + + // principal HDU is read + //std::cout << surface_density.size() << std::endl; + h0.read(surface_density,first,last,stride); + //std::cout << surface_density.size() << std::endl; + + nx = h0.axis(0); + ny = h0.axis(1); + + lowerleft[0] = boxlMpc*(2*first[0] - nx)/2; + lowerleft[1] = boxlMpc*(2*first[1] - ny)/2; + + upperright[0] = boxlMpc*(2*last[0] - nx)/2; + upperright[1] = boxlMpc*(2*last[1] - ny)/2; + + center = (lowerleft + upperright)/2; + + nx = last[0] - first[0] + 1; + ny = last[1] - first[1] + 1; + + boxlMpc *= nx*1./h0.axis(0); +} + /** * \brief write the fits file of the maps of all the lensing quantities */ diff --git a/include/gridmap.h b/include/gridmap.h index 7ee7bf97..da4ebdbd 100644 --- a/include/gridmap.h +++ b/include/gridmap.h @@ -51,6 +51,12 @@ struct GridMap{ void writePixelMapUniform(PixelMap &map,LensingVariable lensvar); void writeFitsUniform(const PosType center[],size_t Nx,size_t Ny,LensingVariable lensvar,std::string filename); + /// this will make a fits map of the grid as is. + void writeFitsUniform(LensingVariable lensvar,std::string filename){ + PixelMap map = writePixelMapUniform(lensvar); + map.printFITS(filename); + } + /// returns a PixelMap with the flux in pixels at a resolution of res times the original resolution PixelMap getPixelMap(int res) const; /// update a PixelMap with the flux in pixels at a resolution of res times the original resolution. diff --git a/include/multimap.h b/include/multimap.h index a5227074..db942e03 100644 --- a/include/multimap.h +++ b/include/multimap.h @@ -30,6 +30,9 @@ * the cfits library. */ struct LensMap{ + + LensMap(){}; + /// values for the map std::valarray surface_density; // Msun / Mpc^2 std::valarray alpha1_bar; @@ -55,9 +58,6 @@ struct LensMap{ double z; - - LensMap(){}; - #ifdef ENABLE_FITS LensMap(std::string fits_input_file,float h){ @@ -76,6 +76,15 @@ struct LensMap{ ,const std::vector &last ,float h ); + + + void read_header(std::unique_ptr ff,float h); + + void read_sub(CCfits::FITS *ff + ,const std::vector &first + ,const std::vector &last + ,float h + ); void write(std::string filename); @@ -113,7 +122,9 @@ class LensHaloMultiMap : public LensHalo //double Wlr(double k2){return exp(-k2*rs2);} - ~LensHaloMultiMap(){}; + ~LensHaloMultiMap(){ + delete ff; + }; void submap( const std::vector &lower_left @@ -149,6 +160,7 @@ class LensHaloMultiMap : public LensHalo private: const COSMOLOGY &cosmo; + CCfits::FITS *ff; LensMap long_range_map; LensMap short_range_map; @@ -301,8 +313,11 @@ void LensMap::PreProcessFFTWMap(float zerosize,T Wphi_of_k){ double ky=(j Date: Mon, 21 Jan 2019 11:53:27 +0100 Subject: [PATCH 110/227] Fixed some bugs in LensHaloMuliMap and LensMap. Still seems like the short range forces are very small. --- MultiPlane/multimap.cpp | 79 +++++++----- MultiPlane/particle_lens.cpp | 11 +- include/multimap.h | 242 ++++++++++++++++------------------- 3 files changed, 156 insertions(+), 176 deletions(-) diff --git a/MultiPlane/multimap.cpp b/MultiPlane/multimap.cpp index d3f83e95..d1f31856 100644 --- a/MultiPlane/multimap.cpp +++ b/MultiPlane/multimap.cpp @@ -74,7 +74,7 @@ LensHaloMultiMap::LensHaloMultiMap( size_t kj = 0; for(size_t j = 0 ; j < Noriginal[1] ; ++j ){ size_t jj = MIN(j/desample,ny-1); // ??? - assert(jj < ny); + //assert(jj < ny); //size_t kjj = nx*(j/desample); size_t kjj = nx*jj; @@ -86,23 +86,23 @@ LensHaloMultiMap::LensHaloMultiMap( kj = 0; }else{ kj += submap.nx; - assert(kj < submap.nx*submap.ny); + //assert(kj < submap.nx*submap.ny); } for(size_t i = 0 ; i < Noriginal[0] ; ++i ){ size_t ii = MIN(i/desample,nx-1); - assert(ii + kjj < long_range_map.surface_density.size() ); - assert(i + kj < submap.surface_density.size() ); + //assert(ii + kjj < long_range_map.surface_density.size() ); + //assert(i + kj < submap.surface_density.size() ); long_range_map.surface_density[ ii + kjj ] += submap.surface_density[ i + kj ]; - assert(!isnan(submap.surface_density[ i + kj ])); + //assert(!isnan(submap.surface_density[ i + kj ])); } } - double area = res*res*cosmo.gethubble()/mass_unit; //*** units ??? + double area = res*res/mass_unit; //*** units ??? // convert to for(auto &p : long_range_map.surface_density){ p /= area; - assert(!isnan(p)); + //assert(!isnan(p)); } wlr.rs2 = wsr.rs2 = rs2; @@ -114,9 +114,9 @@ LensHaloMultiMap::LensHaloMultiMap( long_range_map.write("!" + fitsfile + "_lr.fits"); - for(auto p : long_range_map.surface_density){ // ?????? - assert(!isnan(p)); - } + //for(auto p : long_range_map.surface_density){ // ?????? + // assert(!isnan(p)); + //} // ??? don //bigmap.boxlMpc = long_range_map.boxlMpc; @@ -212,17 +212,18 @@ void LensHaloMultiMap::submap( if(ii < 0) ii += Noriginal[0]; if(ii >= Noriginal[0] ) ii -= Noriginal[0]; - assert( i + kj < map.surface_density.size() ); - assert( ii + kk < partmap.surface_density.size() ); + //assert( i + kj < map.surface_density.size() ); + //assert( ii + kk < partmap.surface_density.size() ); map.surface_density[ i + kj ] = partmap.surface_density[ ii + kk ]; } } - // need to do overlap region - map.boxlMpc = map.nx*res; } - - double area = res*res*cosmo.gethubble()/mass_unit; //*** units ??? + + // need to do overlap region + map.boxlMpc = map.nx*res; + + double area = res*res/mass_unit; //*** units ??? // convert to for(auto &p : map.surface_density){ p /= area; @@ -285,12 +286,7 @@ void LensHaloMultiMap::force_halo(double *alpha ,long_range_map.ny ,long_range_map.ny*long_range_map.boxlMpc/long_range_map.nx ,long_range_map.center.x); - Utilities::Interpolator > short_interp(xx - ,short_range_map.nx,short_range_map.boxlMpc - ,short_range_map.ny - ,short_range_map.ny*short_range_map.boxlMpc/short_range_map.nx - ,short_range_map.center.x); - + //assert(long_range_map.nx == long_range_map.ny); alpha[0] = interp.interpolate(long_range_map.alpha1_bar); @@ -301,21 +297,33 @@ void LensHaloMultiMap::force_halo(double *alpha *kappa = interp.interpolate(long_range_map.surface_density); //*phi = interp.interpolate(long_range_map.phi_bar); - assert(alpha[0] == alpha[0] && alpha[1] == alpha[1]); - assert(gamma[0] == gamma[0] && gamma[1] == gamma[1]); - assert(kappa == kappa); - - alpha[0] += short_interp.interpolate(short_range_map.alpha1_bar); - alpha[1] += short_interp.interpolate(short_range_map.alpha2_bar); - gamma[0] += short_interp.interpolate(short_range_map.gamma1_bar); - gamma[1] += short_interp.interpolate(short_range_map.gamma2_bar); - *kappa += short_interp.interpolate(short_range_map.surface_density); + //assert(alpha[0] == alpha[0] && alpha[1] == alpha[1]); + //assert(gamma[0] == gamma[0] && gamma[1] == gamma[1]); + //assert(kappa == kappa); + + if( (xx[0] > short_range_map.lowerleft[0])*(xx[0] < short_range_map.upperright[0]) + *(xx[1] > short_range_map.lowerleft[1])*(xx[1] < short_range_map.upperright[1]) + ){ + + Utilities::Interpolator > short_interp(xx + ,short_range_map.nx,short_range_map.boxlMpc + ,short_range_map.ny + ,short_range_map.ny*short_range_map.boxlMpc/short_range_map.nx + ,short_range_map.center.x); + + + alpha[0] += short_interp.interpolate(short_range_map.alpha1_bar); + alpha[1] += short_interp.interpolate(short_range_map.alpha2_bar); + gamma[0] += short_interp.interpolate(short_range_map.gamma1_bar); + gamma[1] += short_interp.interpolate(short_range_map.gamma2_bar); + *kappa += short_interp.interpolate(short_range_map.surface_density); //*phi = interp.interpolate(short_range_map.phi_bar); - assert(alpha[0] == alpha[0] && alpha[1] == alpha[1]); - assert(gamma[0] == gamma[0] && gamma[1] == gamma[1]); - assert(kappa == kappa); - + //assert(alpha[0] == alpha[0] && alpha[1] == alpha[1]); + //assert(gamma[0] == gamma[0] && gamma[1] == gamma[1]); + //assert(kappa == kappa); + } + return; } @@ -344,6 +352,7 @@ void LensMap::read_header(std::string fits_input_file,float h){ double D = 3*(pow(wup,4) - pow(wlow,4))/(pow(wup,3) - pow(wlow,3))/4; if(wlow == wup) D = wlow; D /= (1+z)*h; + //D /= (1+z); res *= degreesTOradians*D; boxlMpc = res*nx; diff --git a/MultiPlane/particle_lens.cpp b/MultiPlane/particle_lens.cpp index 4b20f748..741dfa23 100644 --- a/MultiPlane/particle_lens.cpp +++ b/MultiPlane/particle_lens.cpp @@ -192,16 +192,7 @@ void MakeParticleLenses::CreateHalos(const COSMOLOGY &cosmo,double redshift){ ,false ,0) ); - /*/ - /*LensHaloParticles > halo(pp - ,nparticles[i] - ,z_original - ,cosmo - ,theta_rotate - ,false - ,0); - */ - } + } skip += nparticles[i]; } }; diff --git a/include/multimap.h b/include/multimap.h index db942e03..e9e69d6b 100644 --- a/include/multimap.h +++ b/include/multimap.h @@ -116,7 +116,7 @@ class LensHaloMultiMap : public LensHalo LensHaloMultiMap( std::string fitsfile /// Original fits map of the density ,double z - ,double mass_unit + ,double mass_unit /// shoudl include h factors ,const COSMOLOGY &c ); @@ -130,18 +130,28 @@ class LensHaloMultiMap : public LensHalo const std::vector &lower_left ,const std::vector &upper_right ); - /// in coordinates relative to the center of the original map + /// in physical coordinates relative to the center of the original map void submap(Point_2d ll,Point_2d ur); void force_halo(double *alpha,KappaType *kappa,KappaType *gamma,KappaType *phi,double const *xcm,bool subtract_point=false,PosType screening = 1.0); void writeImage(std::string fn); - /// return center in physical Mpc + /// lower left of long range map in physical Mpc + Point_2d getLowerLeft_lr() const { return long_range_map.lowerleft; } + /// upper right of short range map in physical Mpc + Point_2d getUpperRight_lr() const { return long_range_map.upperright; } + /// center of short range map in physical Mpc Point_2d getCenter_lr() const { return long_range_map.center; } - /// return center in physical Mpc + + /// lower left of short range map in physical Mpc + Point_2d getLowerLeft_sr() const { return short_range_map.lowerleft; } + /// upper right of short range map in physical Mpc + Point_2d getUpperRight_sr() const { return short_range_map.upperright; } + /// center of short range map in physical Mpc Point_2d getCenter_sr() const { return short_range_map.center; } + /// return range of long range map in physical Mpc double getRangeMpc_lr() const { return long_range_map.boxlMpc; } /// return range of long range map in physical Mpc @@ -204,26 +214,40 @@ class LensHaloMultiMap : public LensHalo template void LensMap::PreProcessFFTWMap(float zerosize,T Wphi_of_k){ - + assert(surface_density.size() == nx*ny); + // size of the new map in x and y directions, factor by which each size is increased int Nnx=int(zerosize*nx); int Nny=int(zerosize*ny); double boxlx = boxlMpc*zerosize; double boxly = boxlMpc*zerosize/nx*ny; - std:: valarray Nmap; - try{ - Nmap.resize( Nnx*Nny ); - }catch(std::exception &e){ - std::cerr << "exception thrown in LensMap::PreProcessFFTWMap(): " << e.what() << std::endl; + int imin = (Nnx-nx)/2; + int imax = (Nnx+nx)/2; + int jmin = (Nny-ny)/2; + int jmax = (Nny+ny)/2; + + size_t Nkx = (Nnx/2+1); + + std::vector kxs(Nkx); + for( int i=0; i kys(Nny); + for( int j=0; j extended_map( Nnx*Nny ); + // assume locate in a rectangular map and build up the new one for( int j=0; j=int(Nnx/2-nx/2) && i=int(Nny/2-ny/2) && j=imin && i=jmin && j=nx || jj>=ny){ std::cout << " 1 error mapping " << ii << " " << jj << std::endl; @@ -233,138 +257,110 @@ void LensMap::PreProcessFFTWMap(float zerosize,T Wphi_of_k){ std::cout << " 2 error mapping " << ii << " " << jj << std::endl; exit(1); } - Nmap[i+Nnx*j] = surface_density[ii+nx*jj]; + extended_map[i+Nnx*j] = surface_density[ii+nx*jj]; + }else{ + extended_map[i+Nnx*j] = 0; } } } //std::vector fNmap(Nny*(Nnx/2+1)); //std::vector fphi( Nny*(Nnx/2+1) ); - fftw_complex *fphi = new fftw_complex[Nny*(Nnx/2+1)]; + fftw_complex *fphi = new fftw_complex[Nny*(Nkx)]; - { - std::vector dNmap(Nnx*Nny); - std::vector input(Nnx*Nny); - //std::vector output(Nny*(Nnx/2+1)); - - for(int k=0;k fft( Nny*(Nnx/2+1) ); + //std::vector fft( Nny*(Nkx) ); std::vector realsp(Nnx*Nny); - //fftw_plan pp = fftw_plan_dft_c2r_2d(Nny,Nnx,fft,realsp,FFTW_ESTIMATE); - fftw_plan pp = fftw_plan_dft_c2r_2d(Nny,Nnx,fft,realsp.data(),FFTW_MEASURE); + fftw_plan pp = fftw_plan_dft_c2r_2d(Nny,Nnx,fft,realsp.data(),FFTW_MEASURE); // alpha1 { // build modes for each pixel in the fourier space - for( int i=0; i Date: Mon, 21 Jan 2019 15:12:59 +0100 Subject: [PATCH 111/227] Fixed bug where the wrong pixel size was being used for the long range grid. --- MultiPlane/multimap.cpp | 2 +- include/multimap.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/MultiPlane/multimap.cpp b/MultiPlane/multimap.cpp index d1f31856..0a31b019 100644 --- a/MultiPlane/multimap.cpp +++ b/MultiPlane/multimap.cpp @@ -98,7 +98,7 @@ LensHaloMultiMap::LensHaloMultiMap( } } - double area = res*res/mass_unit; //*** units ??? + double area = pow(long_range_map.boxlMpc/long_range_map.nx,2)/mass_unit; //*** units ??? // convert to for(auto &p : long_range_map.surface_density){ p /= area; diff --git a/include/multimap.h b/include/multimap.h index e9e69d6b..95fa9d5a 100644 --- a/include/multimap.h +++ b/include/multimap.h @@ -320,7 +320,7 @@ void LensMap::PreProcessFFTWMap(float zerosize,T Wphi_of_k){ size_t k = i+(Nkx)*j; fft[k][0] = -kxs[i]*fphi[k][1]; fft[k][1] = kxs[i]*fphi[k][0]; - assert(!isnan(fft[k][0])); + //assert(!isnan(fft[k][0])); } } From 29a8f81ffed4381977e05eb9b7e9c09f39751e84 Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Mon, 21 Jan 2019 15:18:13 +0100 Subject: [PATCH 112/227] Detect infinite pixel values in input map. --- include/multimap.h | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/include/multimap.h b/include/multimap.h index e9e69d6b..f4615b71 100644 --- a/include/multimap.h +++ b/include/multimap.h @@ -258,15 +258,19 @@ void LensMap::PreProcessFFTWMap(float zerosize,T Wphi_of_k){ exit(1); } extended_map[i+Nnx*j] = surface_density[ii+nx*jj]; - }else{ + //float tmp = extended_map[i+Nnx*j]; + //assert(!isnan(extended_map[i+Nnx*j])); + if(isinf(extended_map[i+Nnx*j])) extended_map[i+Nnx*j] = 0; /// ???? + }else{ extended_map[i+Nnx*j] = 0; } + } } //std::vector fNmap(Nny*(Nnx/2+1)); //std::vector fphi( Nny*(Nnx/2+1) ); - fftw_complex *fphi = new fftw_complex[Nny*(Nkx)]; + fftw_complex *fphi = new fftw_complex[Nny*Nkx]; fftw_plan p = fftw_plan_dft_r2c_2d(Nny,Nnx,extended_map.data(),fphi,FFTW_ESTIMATE); @@ -282,6 +286,9 @@ void LensMap::PreProcessFFTWMap(float zerosize,T Wphi_of_k){ double k2 = kxs[i]*kxs[i] + kys[j]*kys[j]; size_t k = i+(Nkx)*j; + //assert(k < Nny*Nkx); + //assert(!isnan(fphi[k][0])); + // fphi //fphi[i+(Nkx)*j][0]= -2.*fNmap[i+(Nkx)*j][0]/k2; //fphi[i+(Nkx)*j][1]= -2.*fNmap[i+(Nkx)*j][1]/k2; @@ -320,7 +327,7 @@ void LensMap::PreProcessFFTWMap(float zerosize,T Wphi_of_k){ size_t k = i+(Nkx)*j; fft[k][0] = -kxs[i]*fphi[k][1]; fft[k][1] = kxs[i]*fphi[k][0]; - assert(!isnan(fft[k][0])); + //assert(!isnan(fft[k][0])); } } From 4ce09b08eda7cca8678d0e070d9fc1a38b3c170e Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Wed, 23 Jan 2019 16:02:09 +0100 Subject: [PATCH 113/227] Made single map mode for LensHaloMultiMap. --- MultiPlane/multimap.cpp | 126 ++++++++++++++++++++-------------------- include/multimap.h | 7 ++- 2 files changed, 69 insertions(+), 64 deletions(-) diff --git a/MultiPlane/multimap.cpp b/MultiPlane/multimap.cpp index 0a31b019..2b73902d 100644 --- a/MultiPlane/multimap.cpp +++ b/MultiPlane/multimap.cpp @@ -11,22 +11,22 @@ using namespace CCfits; LensHaloMultiMap::LensHaloMultiMap( std::string fitsfile /// Original fits map of the density - ,double z ,double mass_unit ,const COSMOLOGY &c ):LensHalo(),cosmo(c),mass_unit(mass_unit),fitsfilename(fitsfile) { - ff = new CCfits::FITS (fitsfilename, CCfits::Read); + bool single_grid = false; + try{ + ff = new CCfits::FITS (fitsfilename, CCfits::Read); + }catch(...){ + std::cerr << "CCfits throw an exception: probably file " << fitsfile << " does not exist." << std::endl; + } zerosize = 1; - - setZlens(z); - rscale = 1.0; LensHalo::setTheta(0,0); - setZlensDist(z,cosmo); if(std::numeric_limits::has_infinity) Rmax = std::numeric_limits::infinity(); @@ -36,6 +36,9 @@ LensHaloMultiMap::LensHaloMultiMap( LensMap submap; submap.read_header(fitsfilename,cosmo.gethubble()); + setZlens(submap.z); + setZlensDist(submap.z,cosmo); + long_range_map.boxlMpc = submap.boxlMpc; //std::size_t size = bigmap.nx*bigmap.ny; @@ -45,83 +48,82 @@ LensHaloMultiMap::LensHaloMultiMap( Noriginal[1] = submap.ny; border_width = 4.5*sqrt(rs2)/res + 1; - int desample = 2. * PI * sqrt(rs2) / res / 3. ; + if(single_grid){ + std::vector first = {1,1}; + std::vector last = {submap.nx,submap.ny}; + long_range_map.read_sub(ff,first,last,cosmo.gethubble()); + }else{ - desample = sqrt(rs2) / res; // ???? test + int desample = 2. * PI * sqrt(rs2) / res / 3. ; - size_t nx = long_range_map.nx = submap.nx / desample ; - size_t ny = long_range_map.ny = nx * submap.ny * 1./ submap.nx ; + desample = sqrt(rs2) / res; // ???? test - { - Point_2d dmap = {submap.boxlMpc,ny*submap.boxlMpc/nx}; - dmap /= 2; - long_range_map.center = {0,0}; - long_range_map.upperright = long_range_map.center + dmap; - long_range_map.lowerleft = long_range_map.center - dmap; - } + size_t nx = long_range_map.nx = submap.nx / desample ; + size_t ny = long_range_map.ny = nx * submap.ny * 1./ submap.nx ; - long_range_map.surface_density.resize(nx*ny,0); + { + Point_2d dmap = {submap.boxlMpc,ny*submap.boxlMpc/nx}; + dmap /= 2; + long_range_map.center = {0,0}; + long_range_map.upperright = long_range_map.center + dmap; + long_range_map.lowerleft = long_range_map.center - dmap; + } + + long_range_map.surface_density.resize(nx*ny,0); - long chunk = MIN(nx*ny/Noriginal[0],Noriginal[1]); // same size as long range grid - if( chunk == 0) chunk = MIN(100*desample,Noriginal[1]); + long chunk = MIN(nx*ny/Noriginal[0],Noriginal[1]); // same size as long range grid + if( chunk == 0) chunk = MIN(100*desample,Noriginal[1]); - std::vector first(2); - std::vector last(2); + std::vector first(2); + std::vector last(2); - first[0] = 1; - last[0] = Noriginal[0]; + first[0] = 1; + last[0] = Noriginal[0]; - size_t kj = 0; - for(size_t j = 0 ; j < Noriginal[1] ; ++j ){ - size_t jj = MIN(j/desample,ny-1); // ??? - //assert(jj < ny); - //size_t kjj = nx*(j/desample); - size_t kjj = nx*jj; + size_t kj = 0; + for(size_t j = 0 ; j < Noriginal[1] ; ++j ){ + size_t jj = MIN(j/desample,ny-1); // ??? + //assert(jj < ny); + //size_t kjj = nx*(j/desample); + size_t kjj = nx*jj; - if( j%chunk == 0 ){ - first[1] = j+1; - last[1] = MIN(j + chunk,Noriginal[1]); - //submap.read_sub(fitsfilename,first,last,cosmo.gethubble()); - submap.read_sub(ff,first,last,cosmo.gethubble()); - kj = 0; - }else{ - kj += submap.nx; - //assert(kj < submap.nx*submap.ny); - } + if( j%chunk == 0 ){ + first[1] = j+1; + last[1] = MIN(j + chunk,Noriginal[1]); + //submap.read_sub(fitsfilename,first,last,cosmo.gethubble()); + submap.read_sub(ff,first,last,cosmo.gethubble()); + kj = 0; + }else{ + kj += submap.nx; + //assert(kj < submap.nx*submap.ny); + } - for(size_t i = 0 ; i < Noriginal[0] ; ++i ){ - size_t ii = MIN(i/desample,nx-1); - //assert(ii + kjj < long_range_map.surface_density.size() ); - //assert(i + kj < submap.surface_density.size() ); - long_range_map.surface_density[ ii + kjj ] += submap.surface_density[ i + kj ]; - //assert(!isnan(submap.surface_density[ i + kj ])); + for(size_t i = 0 ; i < Noriginal[0] ; ++i ){ + size_t ii = MIN(i/desample,nx-1); + //assert(ii + kjj < long_range_map.surface_density.size() ); + //assert(i + kj < submap.surface_density.size() ); + //assert(!isinf(submap.surface_density[ i + kj ])); + //assert(!isinf(long_range_map.surface_density[ ii + kjj ])); + long_range_map.surface_density[ ii + kjj ] += submap.surface_density[ i + kj ]; + //assert(!isnan(submap.surface_density[ i + kj ])); + } } + wlr.rs2 = wsr.rs2 = rs2; } double area = pow(long_range_map.boxlMpc/long_range_map.nx,2)/mass_unit; //*** units ??? // convert to for(auto &p : long_range_map.surface_density){ + assert(!isinf(p)); + std::cout << p << " " ; // ???? p /= area; - //assert(!isnan(p)); + assert(!isinf(p)); } - wlr.rs2 = wsr.rs2 = rs2; - long_range_map.PreProcessFFTWMap(1.0,wlr); + if(single_grid) long_range_map.PreProcessFFTWMap(1.0,unit); + else long_range_map.PreProcessFFTWMap(1.0,wlr); - // ???? -// UNIT w; -// long_range_map.PreProcessFFTWMap(1.0,w); - long_range_map.write("!" + fitsfile + "_lr.fits"); - - //for(auto p : long_range_map.surface_density){ // ?????? - // assert(!isnan(p)); - //} - - // ??? don - //bigmap.boxlMpc = long_range_map.boxlMpc; - //bigmap.PreProcessFFTWMap(1.0,wsr); - //bigmap.write("!" + fitsfile + "_sr.fits"); }; void LensHaloMultiMap::submap(Point_2d ll,Point_2d ur){ diff --git a/include/multimap.h b/include/multimap.h index f4615b71..3fedd9e6 100644 --- a/include/multimap.h +++ b/include/multimap.h @@ -115,7 +115,6 @@ class LensHaloMultiMap : public LensHalo LensHaloMultiMap( std::string fitsfile /// Original fits map of the density - ,double z ,double mass_unit /// shoudl include h factors ,const COSMOLOGY &c ); @@ -257,10 +256,14 @@ void LensMap::PreProcessFFTWMap(float zerosize,T Wphi_of_k){ std::cout << " 2 error mapping " << ii << " " << jj << std::endl; exit(1); } + //assert(ii+nx*jj < surface_density.size()); + //assert(i+Nnx*j < extended_map.size()); extended_map[i+Nnx*j] = surface_density[ii+nx*jj]; //float tmp = extended_map[i+Nnx*j]; //assert(!isnan(extended_map[i+Nnx*j])); - if(isinf(extended_map[i+Nnx*j])) extended_map[i+Nnx*j] = 0; /// ???? + if(isinf(extended_map[i+Nnx*j])){ + extended_map[i+Nnx*j] = 0; /// ???? + } }else{ extended_map[i+Nnx*j] = 0; } From c3c910f7ef64c8bc6b71103f8ac71b70f8d2f7d3 Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Thu, 24 Jan 2019 18:09:35 +0100 Subject: [PATCH 114/227] Added Utilities::IO::writeCSV(). --- include/utilities_slsim.h | 49 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 46 insertions(+), 3 deletions(-) diff --git a/include/utilities_slsim.h b/include/utilities_slsim.h index 8a3b2feb..00f0e511 100644 --- a/include/utilities_slsim.h +++ b/include/utilities_slsim.h @@ -1609,9 +1609,52 @@ namespace Utilities ++ii; } } - - } + /** \brief write a CSV data file for some data vectors + + example: +

+ std::vector header = {"alpha","kappa","gamma"}; + + std::vector v1 = {1,2,3}; + std::vector v2 = {1.1,2.1,3.1}; + std::vector v3 = {3,4,5}; + + std::vector *> data; + data.push_back(&v1); + data.push_back(&v2); + data.push_back(&v3); + printCSV(std::cout,header,data); +

+ **/ + + template + void writeCSV(const std::string filename /// output file path/name + ,const std::vector header /// column labels + ,std::vector &data /// objects must have operator [] + ){ + + std::ofstream s(filename + ".csv"); + + int ncol = header.size(); + assert(ncol == data.size() ); + for(int i = 0 ; i < header.size()-1 ; ++i){ + s << header[i] << ","; + } + s << header.back() << std::endl; + + size_t nrow = data[0]->size(); + for(auto v : data) assert(nrow == v->size() ); + + for(size_t j = 0 ; j < nrow ; ++j){ + for(int i = 0 ; i < ncol-1 ; ++i){ + s << data[i]->operator[](j) << ","; + } + s << data.back()->operator[](j) << "\n"; + } + } + } // Utilities::IO + /*** \brief A class for reading and then looking up objects from a CSV catalog. The constructor will read in the whole catalog and sort it into Nxbins X-bins. Each @@ -1706,5 +1749,5 @@ namespace Utilities /// split string into vector of seporate strings that were seporated by void splitstring(std::string &line,std::vector &vec,const std::string &delimiter); -} +} // Utilities #endif From 57842d106393d3a6a133d87c013f000d7ce77360 Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Thu, 24 Jan 2019 18:16:41 +0100 Subject: [PATCH 115/227] Experimenting with parameters to get the MiltMap to work optimally. --- MultiPlane/multimap.cpp | 40 ++++++++++++++++++++++++++-------------- include/multimap.h | 2 ++ 2 files changed, 28 insertions(+), 14 deletions(-) diff --git a/MultiPlane/multimap.cpp b/MultiPlane/multimap.cpp index 2b73902d..94128f68 100644 --- a/MultiPlane/multimap.cpp +++ b/MultiPlane/multimap.cpp @@ -13,11 +13,10 @@ LensHaloMultiMap::LensHaloMultiMap( std::string fitsfile /// Original fits map of the density ,double mass_unit ,const COSMOLOGY &c - ):LensHalo(),cosmo(c),mass_unit(mass_unit),fitsfilename(fitsfile) + ,bool single_grid_mode + ):LensHalo(),single_grid(single_grid_mode),cosmo(c),mass_unit(mass_unit),fitsfilename(fitsfile) { - bool single_grid = false; - try{ ff = new CCfits::FITS (fitsfilename, CCfits::Read); }catch(...){ @@ -42,22 +41,33 @@ LensHaloMultiMap::LensHaloMultiMap( long_range_map.boxlMpc = submap.boxlMpc; //std::size_t size = bigmap.nx*bigmap.ny; + //rs2 = submap.boxlMpc*submap.boxlMpc/( 2*PI*submap.nx ); + rs2 = 2*4*submap.boxlMpc*submap.boxlMpc/( 2*PI*submap.nx ); rs2 = submap.boxlMpc*submap.boxlMpc/( 2*PI*submap.nx ); res = submap.boxlMpc/submap.nx; Noriginal[0] = submap.nx; Noriginal[1] = submap.ny; - border_width = 4.5*sqrt(rs2)/res + 1; - + //border_width = 4.5*sqrt(rs2)/res + 1; + border_width = 5*sqrt(rs2)/res + 1; // ???? + if(single_grid){ std::vector first = {1,1}; std::vector last = {submap.nx,submap.ny}; long_range_map.read_sub(ff,first,last,cosmo.gethubble()); + + { + Point_2d dmap = {submap.boxlMpc,submap.ny*submap.boxlMpc/submap.nx}; + dmap /= 2; + long_range_map.center = {0,0}; + long_range_map.upperright = long_range_map.center + dmap; + long_range_map.lowerleft = long_range_map.center - dmap; + } }else{ int desample = 2. * PI * sqrt(rs2) / res / 3. ; - - desample = sqrt(rs2) / res; // ???? test - + desample = sqrt(rs2) / res /2/2; // ???? test + desample = sqrt(rs2) / res /5; // ???? test + size_t nx = long_range_map.nx = submap.nx / desample ; size_t ny = long_range_map.ny = nx * submap.ny * 1./ submap.nx ; @@ -114,10 +124,7 @@ LensHaloMultiMap::LensHaloMultiMap( double area = pow(long_range_map.boxlMpc/long_range_map.nx,2)/mass_unit; //*** units ??? // convert to for(auto &p : long_range_map.surface_density){ - assert(!isinf(p)); - std::cout << p << " " ; // ???? p /= area; - assert(!isinf(p)); } if(single_grid) long_range_map.PreProcessFFTWMap(1.0,unit); @@ -127,6 +134,8 @@ LensHaloMultiMap::LensHaloMultiMap( }; void LensHaloMultiMap::submap(Point_2d ll,Point_2d ur){ + if(single_grid) return; + std::vector lower_left(2); std::vector upper_right(2); @@ -148,6 +157,8 @@ void LensHaloMultiMap::submap( // check range + if(single_grid) return; + if( (upper_right[0] < 0) || (upper_right[1] < 0) || (lower_left[0] >= Noriginal[0] ) || (lower_left[1] >= Noriginal[1]) ){ std::cerr << "LensHaloMap : sub map is out of bounds" << std::endl; @@ -302,6 +313,7 @@ void LensHaloMultiMap::force_halo(double *alpha //assert(alpha[0] == alpha[0] && alpha[1] == alpha[1]); //assert(gamma[0] == gamma[0] && gamma[1] == gamma[1]); //assert(kappa == kappa); + if(single_grid) return; if( (xx[0] > short_range_map.lowerleft[0])*(xx[0] < short_range_map.upperright[0]) *(xx[1] > short_range_map.lowerleft[1])*(xx[1] < short_range_map.upperright[1]) @@ -522,6 +534,9 @@ void LensMap::read_sub(CCfits::FITS *ff ){ CCfits::PHDU &h0 = ff->pHDU(); + nx = h0.axis(0); + ny = h0.axis(1); + h0.readAllKeys(); // these are always present in each @@ -545,9 +560,6 @@ void LensMap::read_sub(CCfits::FITS *ff h0.read(surface_density,first,last,stride); //std::cout << surface_density.size() << std::endl; - nx = h0.axis(0); - ny = h0.axis(1); - lowerleft[0] = boxlMpc*(2*first[0] - nx)/2; lowerleft[1] = boxlMpc*(2*first[1] - ny)/2; diff --git a/include/multimap.h b/include/multimap.h index 3fedd9e6..218cf0a9 100644 --- a/include/multimap.h +++ b/include/multimap.h @@ -117,6 +117,7 @@ class LensHaloMultiMap : public LensHalo std::string fitsfile /// Original fits map of the density ,double mass_unit /// shoudl include h factors ,const COSMOLOGY &c + ,bool single_grid_mode = false ); //double Wlr(double k2){return exp(-k2*rs2);} @@ -168,6 +169,7 @@ class LensHaloMultiMap : public LensHalo private: + bool single_grid; const COSMOLOGY &cosmo; CCfits::FITS *ff; From f2c1abe016a1b8e500a6d802b9d47cdb2e58f459 Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Mon, 28 Jan 2019 14:38:17 +0100 Subject: [PATCH 116/227] Put lots of test lines in that need to be removed. --- MultiPlane/multimap.cpp | 38 ++++++++++++++++++++++---------------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/MultiPlane/multimap.cpp b/MultiPlane/multimap.cpp index 94128f68..364e91db 100644 --- a/MultiPlane/multimap.cpp +++ b/MultiPlane/multimap.cpp @@ -44,11 +44,14 @@ LensHaloMultiMap::LensHaloMultiMap( //rs2 = submap.boxlMpc*submap.boxlMpc/( 2*PI*submap.nx ); rs2 = 2*4*submap.boxlMpc*submap.boxlMpc/( 2*PI*submap.nx ); rs2 = submap.boxlMpc*submap.boxlMpc/( 2*PI*submap.nx ); + rs2 = pow(submap.boxlMpc*1000,2); //??? + res = submap.boxlMpc/submap.nx; Noriginal[0] = submap.nx; Noriginal[1] = submap.ny; //border_width = 4.5*sqrt(rs2)/res + 1; border_width = 5*sqrt(rs2)/res + 1; // ???? + border_width = 0; // ??? if(single_grid){ std::vector first = {1,1}; @@ -67,6 +70,7 @@ LensHaloMultiMap::LensHaloMultiMap( int desample = 2. * PI * sqrt(rs2) / res / 3. ; desample = sqrt(rs2) / res /2/2; // ???? test desample = sqrt(rs2) / res /5; // ???? test + desample = 1; // ???? test size_t nx = long_range_map.nx = submap.nx / desample ; size_t ny = long_range_map.ny = nx * submap.ny * 1./ submap.nx ; @@ -141,11 +145,11 @@ void LensHaloMultiMap::submap(Point_2d ll,Point_2d ur){ ll = (ll - long_range_map.lowerleft)/res; ur = (ur - long_range_map.lowerleft)/res; - lower_left[0] = lround(ll[0]); - lower_left[1] = lround(ll[1]); + lower_left[0] = floor(ll[0]); + lower_left[1] = floor(ll[1]); - upper_right[0] = lround(ur[0]); - upper_right[1] = lround(ur[1]); + upper_right[0] = floor(ur[0]) - 1 ; + upper_right[1] = floor(ur[1]) - 1 ; submap(lower_left,upper_right); } @@ -173,11 +177,11 @@ void LensHaloMultiMap::submap( std::vector first(2); std::vector last(2); - first[0] = lower_left[0] - border_width; - first[1] = lower_left[1] - border_width; + first[0] = lower_left[0] - border_width + 1; + first[1] = lower_left[1] - border_width + 1; - last[0] = upper_right[0] + border_width; - last[1] = upper_right[1] + border_width; + last[0] = upper_right[0] + border_width + 1; + last[1] = upper_right[1] + border_width + 1; LensMap map; @@ -202,7 +206,7 @@ void LensHaloMultiMap::submap( const size_t chunk = nx*ny/Noriginal[0]; size_t kk,k = chunk + 1; - long jj = first[1]; + long jj = first[1]-1; for(size_t j = 0 ; j < ny ; ++j , ++jj ){ size_t kj = nx*j; @@ -220,7 +224,7 @@ void LensHaloMultiMap::submap( } kk = k*Noriginal[0]; - long ii = first[0]; + long ii = first[0]-1; for(size_t i = 0 ; i < nx ; ++i,++ii ){ if(ii < 0) ii += Noriginal[0]; if(ii >= Noriginal[0] ) ii -= Noriginal[0]; @@ -243,6 +247,7 @@ void LensHaloMultiMap::submap( } map.PreProcessFFTWMap(1.0,wsr); + //map.PreProcessFFTWMap(1.0,unit); // cut off bourders @@ -255,8 +260,9 @@ void LensHaloMultiMap::submap( short_range_map.gamma1_bar.resize(nx*ny); short_range_map.gamma2_bar.resize(nx*ny); - for(long j=0 ; j < ny ; ++j){ - long kjj = (j + border_width )*map.nx; + long jj = border_width; + for(long j=0 ; j < ny ; ++j,++jj){ + long kjj = jj*map.nx; long kj = nx*j; long ii = border_width; for(long i=0 ; i < nx ; ++i,++ii){ @@ -560,11 +566,11 @@ void LensMap::read_sub(CCfits::FITS *ff h0.read(surface_density,first,last,stride); //std::cout << surface_density.size() << std::endl; - lowerleft[0] = boxlMpc*(2*first[0] - nx)/2; - lowerleft[1] = boxlMpc*(2*first[1] - ny)/2; + lowerleft[0] = res*(2*first[0] - nx)/2; + lowerleft[1] = res*(2*first[1] - ny)/2; - upperright[0] = boxlMpc*(2*last[0] - nx)/2; - upperright[1] = boxlMpc*(2*last[1] - ny)/2; + upperright[0] = res*(2*last[0] - nx)/2; + upperright[1] = res*(2*last[1] - ny)/2; center = (lowerleft + upperright)/2; From c24db7e5d196fd7f718853c6891dc7d2b07c3ae2 Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Thu, 31 Jan 2019 12:46:22 +0100 Subject: [PATCH 117/227] Changes to alignment of small and large grids. Decoupled boulder size form rough grid scale. (f and g parameters) Seems to be working okay. --- MultiPlane/multimap.cpp | 244 ++++++++++++++++++++++++---------------- include/multimap.h | 52 +++++---- 2 files changed, 178 insertions(+), 118 deletions(-) diff --git a/MultiPlane/multimap.cpp b/MultiPlane/multimap.cpp index 364e91db..371c12c2 100644 --- a/MultiPlane/multimap.cpp +++ b/MultiPlane/multimap.cpp @@ -42,47 +42,52 @@ LensHaloMultiMap::LensHaloMultiMap( //std::size_t size = bigmap.nx*bigmap.ny; //rs2 = submap.boxlMpc*submap.boxlMpc/( 2*PI*submap.nx ); - rs2 = 2*4*submap.boxlMpc*submap.boxlMpc/( 2*PI*submap.nx ); - rs2 = submap.boxlMpc*submap.boxlMpc/( 2*PI*submap.nx ); - rs2 = pow(submap.boxlMpc*1000,2); //??? + //rs2 = 2*4*submap.boxlMpc*submap.boxlMpc/( 2*PI*submap.nx ); + rs2 = submap.boxlMpc*submap.boxlMpc/( 2*submap.nx )*g/f; - res = submap.boxlMpc/submap.nx; - Noriginal[0] = submap.nx; - Noriginal[1] = submap.ny; - //border_width = 4.5*sqrt(rs2)/res + 1; - border_width = 5*sqrt(rs2)/res + 1; // ???? - border_width = 0; // ??? + resolution = submap.boxlMpc/submap.nx; + //border_width = 4.5*sqrt(rs2)/res + 1; + border_width = f*sqrt(rs2)/resolution + 1; + int desample = sqrt(0.5*submap.nx)/sqrt(f*g); + + Noriginal[0] = submap.nx - submap.nx % desample; + Noriginal[1] = submap.ny - submap.ny % desample; + + size_t nx = long_range_map.nx = submap.nx / desample ; + size_t ny = long_range_map.ny = submap.ny / desample ; + if(single_grid){ std::vector first = {1,1}; - std::vector last = {submap.nx,submap.ny}; + + std::vector last = {(long)Noriginal[0],(long)Noriginal[1]}; long_range_map.read_sub(ff,first,last,cosmo.gethubble()); - { - Point_2d dmap = {submap.boxlMpc,submap.ny*submap.boxlMpc/submap.nx}; - dmap /= 2; - long_range_map.center = {0,0}; - long_range_map.upperright = long_range_map.center + dmap; - long_range_map.lowerleft = long_range_map.center - dmap; - } - }else{ - - int desample = 2. * PI * sqrt(rs2) / res / 3. ; - desample = sqrt(rs2) / res /2/2; // ???? test - desample = sqrt(rs2) / res /5; // ???? test - desample = 1; // ???? test + //double res = submap.boxlMpc/submap.nx; + //Point_2d dmap = {submap.boxlMpc,submap.ny*resolution}; + //dmap /= 2; + //long_range_map.center = {0,0}; + //long_range_map.upperright = long_range_map.center + dmap; + //long_range_map.lowerleft = long_range_map.center - dmap; + //long_range_map.boxlMpc = submap.boxlMpc; - size_t nx = long_range_map.nx = submap.nx / desample ; - size_t ny = long_range_map.ny = nx * submap.ny * 1./ submap.nx ; - - { - Point_2d dmap = {submap.boxlMpc,ny*submap.boxlMpc/nx}; - dmap /= 2; - long_range_map.center = {0,0}; - long_range_map.upperright = long_range_map.center + dmap; - long_range_map.lowerleft = long_range_map.center - dmap; - } + }else{ + double res = desample*submap.boxlMpc/submap.nx; + long_range_map.boxlMpc = nx*res; + + long_range_map.lowerleft = submap.lowerleft; + Point_2d dmap = {nx*res,ny*res}; + long_range_map.upperright = long_range_map.lowerleft + dmap; + long_range_map.center = (long_range_map.upperright + long_range_map.lowerleft)/2; + + /* + dmap /= 2; + long_range_map.center = {0,0}; + long_range_map.upperright = long_range_map.center + dmap; + long_range_map.lowerleft = long_range_map.center - dmap; + */ + long_range_map.surface_density.resize(nx*ny,0); long chunk = MIN(nx*ny/Noriginal[0],Noriginal[1]); // same size as long range grid @@ -95,8 +100,11 @@ LensHaloMultiMap::LensHaloMultiMap( last[0] = Noriginal[0]; size_t kj = 0; + size_t jj; for(size_t j = 0 ; j < Noriginal[1] ; ++j ){ - size_t jj = MIN(j/desample,ny-1); // ??? + jj = j/desample; + if( jj >= ny) break; + //assert(jj < ny); //size_t kjj = nx*(j/desample); size_t kjj = nx*jj; @@ -113,7 +121,8 @@ LensHaloMultiMap::LensHaloMultiMap( } for(size_t i = 0 ; i < Noriginal[0] ; ++i ){ - size_t ii = MIN(i/desample,nx-1); + size_t ii = i/desample; + if( ii >= nx) break; //assert(ii + kjj < long_range_map.surface_density.size() ); //assert(i + kj < submap.surface_density.size() ); //assert(!isinf(submap.surface_density[ i + kj ])); @@ -122,6 +131,8 @@ LensHaloMultiMap::LensHaloMultiMap( //assert(!isnan(submap.surface_density[ i + kj ])); } } + assert(jj == ny-1); + wlr.rs2 = wsr.rs2 = rs2; } @@ -133,8 +144,9 @@ LensHaloMultiMap::LensHaloMultiMap( if(single_grid) long_range_map.PreProcessFFTWMap(1.0,unit); else long_range_map.PreProcessFFTWMap(1.0,wlr); - - long_range_map.write("!" + fitsfile + "_lr.fits"); + //else long_range_map.PreProcessFFTWMap(1.0,unit); // ????? + + if(!single_grid) long_range_map.write("!" + fitsfile + "_lr.fits"); }; void LensHaloMultiMap::submap(Point_2d ll,Point_2d ur){ @@ -143,8 +155,8 @@ void LensHaloMultiMap::submap(Point_2d ll,Point_2d ur){ std::vector lower_left(2); std::vector upper_right(2); - ll = (ll - long_range_map.lowerleft)/res; - ur = (ur - long_range_map.lowerleft)/res; + ll = (ll - long_range_map.lowerleft)/resolution; + ur = (ur - long_range_map.lowerleft)/resolution; lower_left[0] = floor(ll[0]); lower_left[1] = floor(ll[1]); @@ -185,12 +197,12 @@ void LensHaloMultiMap::submap( LensMap map; - if( (first[0] > 0)*(first[1] > 0)*(last[0] < Noriginal[0])*(last[1] < Noriginal[1]) ){ + if( (first[0] > 0)*(first[1] > 0)*(last[0] <= Noriginal[0])*(last[1] <= Noriginal[1]) ){ map.read_sub(fitsfilename,first,last,cosmo.gethubble()); }else{ - size_t nx = map.nx = last[0] - first[0] + 1; - size_t ny = map.ny = last[1] - first[1] + 1; + size_t nx_big = map.nx = last[0] - first[0] + 1; + size_t ny_big = map.ny = last[1] - first[1] + 1; // case where subfield overlaps edge std::vector first_sub(2); @@ -199,16 +211,16 @@ void LensHaloMultiMap::submap( first_sub[0] = 1; last_sub[0] = Noriginal[0]; - map.surface_density.resize(nx*ny); + map.surface_density.resize(nx_big*ny_big); LensMap partmap; - const size_t chunk = nx*ny/Noriginal[0]; + const size_t chunk = nx_big*ny_big/Noriginal[0]; size_t kk,k = chunk + 1; long jj = first[1]-1; - for(size_t j = 0 ; j < ny ; ++j , ++jj ){ - size_t kj = nx*j; + for(size_t j = 0 ; j < ny_big ; ++j , ++jj ){ + size_t kj = nx_big*j; if( k >= chunk || jj < 0 || jj >= Noriginal[1] ){ @@ -225,7 +237,7 @@ void LensHaloMultiMap::submap( kk = k*Noriginal[0]; long ii = first[0]-1; - for(size_t i = 0 ; i < nx ; ++i,++ii ){ + for(size_t i = 0 ; i < nx_big ; ++i,++ii ){ if(ii < 0) ii += Noriginal[0]; if(ii >= Noriginal[0] ) ii -= Noriginal[0]; @@ -234,13 +246,12 @@ void LensHaloMultiMap::submap( map.surface_density[ i + kj ] = partmap.surface_density[ ii + kk ]; } } - + // need to do overlap region + map.boxlMpc = map.nx*resolution; } - // need to do overlap region - map.boxlMpc = map.nx*res; - double area = res*res/mass_unit; //*** units ??? + double area = resolution*resolution/mass_unit; //*** units ??? // convert to for(auto &p : map.surface_density){ p /= area; @@ -275,16 +286,16 @@ void LensHaloMultiMap::submap( } short_range_map.lowerleft = short_range_map.upperright = long_range_map.lowerleft; - short_range_map.lowerleft[0] += lower_left[0]*res; - short_range_map.lowerleft[1] += lower_left[1]*res; + short_range_map.lowerleft[0] += lower_left[0]*resolution; + short_range_map.lowerleft[1] += lower_left[1]*resolution; - short_range_map.upperright[0] += upper_right[0]*res; - short_range_map.upperright[1] += upper_right[1]*res; + short_range_map.upperright[0] += (upper_right[0] + 1)*resolution; + short_range_map.upperright[1] += (upper_right[1] + 1)*resolution; short_range_map.center = (short_range_map.lowerleft + short_range_map.upperright)/2; - short_range_map.boxlMpc = nx*res; + short_range_map.boxlMpc = short_range_map.nx*resolution; - short_range_map.write("!" + fitsfilename + "_sr.fits"); + if(!single_grid) short_range_map.write("!" + fitsfilename + "_sr.fits"); } @@ -299,31 +310,50 @@ void LensHaloMultiMap::force_halo(double *alpha { // interpolate from the maps + + if(single_grid){ + Utilities::Interpolator > interp(xx + ,long_range_map.nx,long_range_map.boxlMpc + ,long_range_map.ny + ,long_range_map.ny*long_range_map.boxlMpc/long_range_map.nx + ,long_range_map.center.x); + + //assert(long_range_map.nx == long_range_map.ny); + + alpha[0] = interp.interpolate(long_range_map.alpha1_bar); + alpha[1] = interp.interpolate(long_range_map.alpha2_bar); + gamma[0] = interp.interpolate(long_range_map.gamma1_bar); + gamma[1] = interp.interpolate(long_range_map.gamma2_bar); + gamma[2] = 0.0; + *kappa = interp.interpolate(long_range_map.surface_density); + //*phi = interp.interpolate(long_range_map.phi_bar); + + return; + } - Utilities::Interpolator > interp(xx + if( (xx[0] >= short_range_map.lowerleft[0])*(xx[0] <= short_range_map.upperright[0]) + *(xx[1] >= short_range_map.lowerleft[1])*(xx[1] <= short_range_map.upperright[1]) + ){ + + Utilities::Interpolator > interp(xx ,long_range_map.nx,long_range_map.boxlMpc ,long_range_map.ny ,long_range_map.ny*long_range_map.boxlMpc/long_range_map.nx ,long_range_map.center.x); - //assert(long_range_map.nx == long_range_map.ny); - - alpha[0] = interp.interpolate(long_range_map.alpha1_bar); - alpha[1] = interp.interpolate(long_range_map.alpha2_bar); - gamma[0] = interp.interpolate(long_range_map.gamma1_bar); - gamma[1] = interp.interpolate(long_range_map.gamma2_bar); - gamma[2] = 0.0; - *kappa = interp.interpolate(long_range_map.surface_density); - //*phi = interp.interpolate(long_range_map.phi_bar); + //assert(long_range_map.nx == long_range_map.ny); + + alpha[0] = interp.interpolate(long_range_map.alpha1_bar); + alpha[1] = interp.interpolate(long_range_map.alpha2_bar); + gamma[0] = interp.interpolate(long_range_map.gamma1_bar); + gamma[1] = interp.interpolate(long_range_map.gamma2_bar); + gamma[2] = 0.0; + *kappa = interp.interpolate(long_range_map.surface_density); + //*phi = interp.interpolate(long_range_map.phi_bar); - //assert(alpha[0] == alpha[0] && alpha[1] == alpha[1]); - //assert(gamma[0] == gamma[0] && gamma[1] == gamma[1]); - //assert(kappa == kappa); - if(single_grid) return; - - if( (xx[0] > short_range_map.lowerleft[0])*(xx[0] < short_range_map.upperright[0]) - *(xx[1] > short_range_map.lowerleft[1])*(xx[1] < short_range_map.upperright[1]) - ){ + //assert(alpha[0] == alpha[0] && alpha[1] == alpha[1]); + //assert(gamma[0] == gamma[0] && gamma[1] == gamma[1]); + //assert(kappa == kappa); Utilities::Interpolator > short_interp(xx ,short_range_map.nx,short_range_map.boxlMpc @@ -342,8 +372,14 @@ void LensHaloMultiMap::force_halo(double *alpha //assert(alpha[0] == alpha[0] && alpha[1] == alpha[1]); //assert(gamma[0] == gamma[0] && gamma[1] == gamma[1]); //assert(kappa == kappa); + }else{ + + alpha[0] = 0; + alpha[1] = 0; + gamma[0] = 0; + gamma[1] = 0; + *kappa = 0; } - return; } @@ -388,14 +424,15 @@ void LensMap::read_header(std::string fits_input_file,float h){ exit(1); } + double res = boxlMpc/nx; lowerleft = center; lowerleft[0] -= boxlMpc/2; - lowerleft[1] -= boxlMpc*ny/nx/2; + lowerleft[1] -= res*ny/2; upperright = center; upperright[0] += boxlMpc/2; - upperright[1] += boxlMpc*ny/nx/2; + upperright[1] += ny*res/2.; } void LensMap::read(std::string fits_input_file,float h){ @@ -460,20 +497,22 @@ void LensMap::read(std::string fits_input_file,float h){ exit(1); } - /// ??? set center + double res = nx/boxlMpc; + center *= 0; lowerleft = center; lowerleft[0] -= boxlMpc/2; - lowerleft[1] -= boxlMpc*ny/nx/2; - + lowerleft[1] -= res*ny/2; + upperright = center; upperright[0] += boxlMpc/2; - upperright[1] += boxlMpc*ny/nx/2; + upperright[1] += ny*res/2.; + } void LensMap::read_sub(std::string fits_input_file - ,const std::vector &first - ,const std::vector &last + ,const std::vector &first /// 2d vector for pixel of lower left, (1,1) offset + ,const std::vector &last /// 2d vector for pixel of upper right, (1,1) offset ,float h ){ @@ -513,23 +552,31 @@ void LensMap::read_sub(std::string fits_input_file h0.read(surface_density,first,last,stride); //std::cout << surface_density.size() << std::endl; + // set the full field lower left + lowerleft = {-boxlMpc/2,-res*ny/2}; + nx = h0.axis(0); ny = h0.axis(1); ff.release(); - lowerleft[0] = boxlMpc*(2*first[0] - nx)/2; - lowerleft[1] = boxlMpc*(2*first[1] - ny)/2; + lowerleft[0] += (first[0]-1)*res ; + lowerleft[1] += (first[1]-1)*res ; - upperright[0] = boxlMpc*(2*last[0] - nx)/2; - upperright[1] = boxlMpc*(2*last[1] - ny)/2; + //lowerleft[0] = boxlMpc*(2*first[0] - nx)/2 - boxlMpc/2; + //lowerleft[1] = boxlMpc*(2*first[1] - ny)/2 - res*ny/2; + + upperright = lowerleft; + + upperright[0] += last[0]*res; + upperright[1] += last[1]*res; center = (lowerleft + upperright)/2; nx = last[0] - first[0] + 1; ny = last[1] - first[1] + 1; - boxlMpc *= nx*1./h0.axis(0); + boxlMpc = res*nx; } @@ -540,8 +587,8 @@ void LensMap::read_sub(CCfits::FITS *ff ){ CCfits::PHDU &h0 = ff->pHDU(); - nx = h0.axis(0); - ny = h0.axis(1); + long nx_orig = h0.axis(0); + long ny_orig = h0.axis(1); h0.readAllKeys(); @@ -556,7 +603,7 @@ void LensMap::read_sub(CCfits::FITS *ff if(wlow == wup) D = wup; D /= (1+z)*h; res *= degreesTOradians*D; - boxlMpc = res*nx; + double boxlMpc_orig = res*nx_orig; assert(h0.axes() >= 2); std::vector stride = {1,1}; @@ -566,18 +613,21 @@ void LensMap::read_sub(CCfits::FITS *ff h0.read(surface_density,first,last,stride); //std::cout << surface_density.size() << std::endl; - lowerleft[0] = res*(2*first[0] - nx)/2; - lowerleft[1] = res*(2*first[1] - ny)/2; + // set the full field lower left + lowerleft = upperright = {-boxlMpc_orig/2,-res*ny_orig/2}; - upperright[0] = res*(2*last[0] - nx)/2; - upperright[1] = res*(2*last[1] - ny)/2; + lowerleft[0] += (first[0]-1)*res ; + lowerleft[1] += (first[1]-1)*res ; + + upperright[0] += last[0]*res; + upperright[1] += last[1]*res; center = (lowerleft + upperright)/2; nx = last[0] - first[0] + 1; ny = last[1] - first[1] + 1; - boxlMpc *= nx*1./h0.axis(0); + boxlMpc = res*nx; } /** diff --git a/include/multimap.h b/include/multimap.h index 218cf0a9..529562dc 100644 --- a/include/multimap.h +++ b/include/multimap.h @@ -31,7 +31,7 @@ */ struct LensMap{ - LensMap(){}; + LensMap():nx(0),ny(0),boxlMpc(0),z(0){}; /// values for the map std::valarray surface_density; // Msun / Mpc^2 @@ -126,6 +126,8 @@ class LensHaloMultiMap : public LensHalo delete ff; }; + const double f = 5,g = 6; + void submap( const std::vector &lower_left ,const std::vector &upper_right @@ -162,24 +164,30 @@ class LensHaloMultiMap : public LensHalo /// return number of pixels on a y-axis side in original map size_t getNy_lr() const { return long_range_map.ny; } - /// return number of pixels on a x-axis side in original map + /// return number of pixels on a x-axis side in short range map size_t getNx_sr() const { return short_range_map.nx; } - /// return number of pixels on a y-axis side in original map + /// return number of pixels on a y-axis side in short range map size_t getNy_sr() const { return short_range_map.ny; } + /// return number of pixels on a x-axis side in original map + size_t getNx() const { return Noriginal[0]; } + /// return number of pixels on a y-axis side in original map + size_t getNy() const { return Noriginal[1]; } + private: bool single_grid; const COSMOLOGY &cosmo; CCfits::FITS *ff; +public: // ????? LensMap long_range_map; LensMap short_range_map; - +private: // ????? double mass_unit; size_t Noriginal[2]; // number of pixels in each dimension in original image - double res; // resolution of original image and short rnage image in Mpc + double resolution; // resolution of original image and short rnage image in Mpc long border_width; // width of short range maps padding std::string fitsfilename; @@ -221,7 +229,7 @@ void LensMap::PreProcessFFTWMap(float zerosize,T Wphi_of_k){ int Nnx=int(zerosize*nx); int Nny=int(zerosize*ny); double boxlx = boxlMpc*zerosize; - double boxly = boxlMpc*zerosize/nx*ny; + double boxly = ny*boxlMpc*zerosize/nx; int imin = (Nnx-nx)/2; int imax = (Nnx+nx)/2; @@ -290,26 +298,28 @@ void LensMap::PreProcessFFTWMap(float zerosize,T Wphi_of_k){ double k2 = kxs[i]*kxs[i] + kys[j]*kys[j]; size_t k = i+(Nkx)*j; - - //assert(k < Nny*Nkx); - //assert(!isnan(fphi[k][0])); - - // fphi - //fphi[i+(Nkx)*j][0]= -2.*fNmap[i+(Nkx)*j][0]/k2; - //fphi[i+(Nkx)*j][1]= -2.*fNmap[i+(Nkx)*j][1]/k2; - - // fphi - fphi[k][0] *= -2./k2; - fphi[k][1] *= -2./k2; - // apply window function - fphi[k][0] *= Wphi_of_k(k2); - fphi[k][1] *= Wphi_of_k(k2); - // null for k2 = 0 no divergence if(k2 == 0){ fphi[k][0] = 0.; fphi[k][1] = 0.; + }else{ + + //assert(k < Nny*Nkx); + //assert(!isnan(fphi[k][0])); + + // fphi + //fphi[i+(Nkx)*j][0]= -2.*fNmap[i+(Nkx)*j][0]/k2; + //fphi[i+(Nkx)*j][1]= -2.*fNmap[i+(Nkx)*j][1]/k2; + + // fphi + fphi[k][0] *= -2./k2; + fphi[k][1] *= -2./k2; + + // apply window function + double w = Wphi_of_k(k2); + fphi[k][0] *= w; + fphi[k][1] *= w; } } } From 27ca29c53697c5275430b80745cc7558b90cab00 Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Thu, 7 Feb 2019 14:34:12 +0100 Subject: [PATCH 118/227] Added Utilities::IO::check_directory() test if directory exists. --- TreeCode_link/utilities.cpp | 9 +++++++++ include/image_processing.h | 2 +- include/utilities_slsim.h | 4 ++++ 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/TreeCode_link/utilities.cpp b/TreeCode_link/utilities.cpp index 9df0b8e1..3608c605 100644 --- a/TreeCode_link/utilities.cpp +++ b/TreeCode_link/utilities.cpp @@ -646,6 +646,15 @@ unsigned long prevpower(unsigned long k){ int GetNThreads(){return N_THREADS;} } +/// throws a runtime error if the directory does not exist +bool Utilities::IO::check_directory(std::string dir){ + struct stat sb; + if (stat(dir.c_str(), &sb) != 0 || !S_ISDIR(sb.st_mode)){ + return false; + } + return true; +} + void Utilities::IO::ReadFileNames( std::string dir /// path to directory containing fits files ,const std::string filespec /// string of charactors in file name that are matched. It can be an empty string. diff --git a/include/image_processing.h b/include/image_processing.h index 6a2db6cc..3ea32ac0 100644 --- a/include/image_processing.h +++ b/include/image_processing.h @@ -415,7 +415,7 @@ void _SplitFluxIntoPixels(TreeHndl ptree,Branch *leaf,double *leaf_sb); namespace Utilities{ void LoadFitsImages(std::string dir,const std::string& filespec,std::vector & images,int maxN,double resolution = -1,bool verbose = false); - void LoadFitsImages(std::string dir,std::vector filespecs,std::vector file_non_specs ,std::vector & images,std::vector & names,int maxN,double resolution = -1,bool verbose = false); + void LoadFitsImages(std::string dir,std::vector filespecs,std::vector file_non_specs ,std::vector & images,std::vector & names,int maxN,double resolution = -1,bool verbose = false); void ReadFileNames(std::string dir,const std::string filespec ,std::vector & filenames ,const std::string file_non_spec = " " diff --git a/include/utilities_slsim.h b/include/utilities_slsim.h index 00f0e511..f33588df 100644 --- a/include/utilities_slsim.h +++ b/include/utilities_slsim.h @@ -1201,6 +1201,7 @@ namespace Utilities /// returns the compiler variable N_THREADS that is maximum number of threads to be used. int GetNThreads(); + /// namespace for input/output utilities namespace IO{ /// /** \brief Read in data from an ASCII file with two columns */ @@ -1406,6 +1407,9 @@ namespace Utilities ,std::vector & filenames /// output vector of PixelMaps ,bool verbose); + /// check if the directory does not exist + bool check_directory(std::string dir); + /** \brief This function will read in all the numbers from a multi-column ,space seporated ASCII data file. From b1d34f3dd32db708b154dfcf8507d4570daec153 Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Fri, 1 Mar 2019 19:20:58 +0100 Subject: [PATCH 119/227] Changed SourceOverzierPlus so that after many randomizations it does not walk of to crazy town. --- Source/overzier.cpp | 176 +++++++++++++++++--------------------- include/overzier_source.h | 55 ++++++------ 2 files changed, 109 insertions(+), 122 deletions(-) diff --git a/Source/overzier.cpp b/Source/overzier.cpp index feb64811..c29dbe82 100644 --- a/Source/overzier.cpp +++ b/Source/overzier.cpp @@ -11,8 +11,7 @@ /// sets everything to zero SourceOverzier::SourceOverzier() -: haloID(0), Reff(0), Rh(0), PA(0), inclination(0), - cxx(0), cyy(0), cxy(0), sbDo(0), sbSo(0), mag(0), mag_bulge(0) +: haloID(0) { } @@ -38,46 +37,20 @@ SourceOverzier::~SourceOverzier() SourceOverzier::SourceOverzier(const SourceOverzier &s) :Source(s){ -/// bulge half light radius - Reff = s.Rh; - Rh = s.Rh; - PA = s.PA; - inclination = s.inclination; - - cxx = s.cxx; - cyy = s.cyy; - cxy = s.cxy; - sbDo = s.sbDo; - sbSo = s.sbSo; - mag = s.mag; - mag_bulge = s.mag_bulge; + + original = s.original; + current = s.current; + sedtype = s.sedtype; - - mag_map = s.mag_map; - bulge_mag_map = s.bulge_mag_map; } SourceOverzier& SourceOverzier::operator=(const SourceOverzier &s){ if(this == &s) return *this; Source::operator=(s); - /// bulge half light radius - Reff = s.Reff; - Rh = s.Rh; - PA = s.PA; - inclination = s.inclination; - - cxx = s.cxx; - cyy = s.cyy; - cxy = s.cxy; - sbDo = s.sbDo; - sbSo = s.sbSo; - mag = s.mag; - mag_bulge = s.mag_bulge; + + original = s.original; + current = s.current; sedtype = s.sedtype; - - mag_map = s.mag_map; - bulge_mag_map = s.bulge_mag_map; - return *this; } @@ -86,22 +59,22 @@ void SourceOverzier::setInternals(double my_mag,double my_mag_bulge,double my_Re haloID = my_id; - Reff = my_Reff*arcsecTOradians; - Rh = my_Rh*arcsecTOradians; - mag = my_mag; - mag_bulge = my_mag_bulge; - PA = my_PA; - inclination = incl; - - if(Rh > 0.0){ - cxx = ( pow(cos(PA),2) + pow(sin(PA)/cos(incl),2) )/Rh/Rh; - cyy = ( pow(sin(PA),2) + pow(cos(PA)/cos(incl),2) )/Rh/Rh; - cxy = ( 2*cos(PA)*sin(PA)*(1-pow(1/cos(incl),2)) )/Rh/Rh; + current.Reff = my_Reff*arcsecTOradians; + current.Rh = my_Rh*arcsecTOradians; + current.mag = my_mag; + current.mag_bulge = my_mag_bulge; + current.PA = my_PA; + current.inclination = incl; + + if(current.Rh > 0.0){ + current.cxx = ( pow(cos(current.PA),2) + pow(sin(current.PA)/cos(incl),2) )/current.Rh/current.Rh; + current.cyy = ( pow(sin(current.PA),2) + pow(cos(current.PA)/cos(incl),2) )/current.Rh/current.Rh; + current.cxy = ( 2*cos(current.PA)*sin(current.PA)*(1-pow(1/cos(incl),2)) )/current.Rh/current.Rh; }else{ - cxx = cyy = cxy = 0.0; + current.cxx = current.cyy = current.cxy = 0.0; } - renormalize(); + renormalize_current(); // redshift setZ(my_z); @@ -111,13 +84,15 @@ void SourceOverzier::setInternals(double my_mag,double my_mag_bulge,double my_Re // in the pure De Vacouleur/exponential disk case // 6.670 = 3.975*Re = 3.975*1.678*Rh float BtoT = getBtoT(); - setRadius(6.670*Rh*(1-BtoT)+18.936*Reff*BtoT); + setRadius(6.670*current.Rh*(1 - BtoT) + 18.936 * current.Reff * BtoT); // position if(my_theta != NULL) setTheta(my_theta[0], my_theta[1]); else setTheta(0, 0); + + original = current; } /// Surface brightness in erg/cm^2/sec/rad^2/Hz @@ -129,13 +104,14 @@ PosType SourceOverzier::SurfaceBrightness( x[0] = y[0]-getTheta()[0]; x[1] = y[1]-getTheta()[1]; - PosType R = cxx*x[0]*x[0] + cyy*x[1]*x[1] + cxy*x[0]*x[1],sb; + PosType R = current.cxx*x[0]*x[0] + current.cyy*x[1]*x[1] + current.cxy*x[0]*x[1],sb; R = sqrt(R); //sb = sbDo*exp(-(R)) + sbSo*exp(-7.6693*pow(R/Reff,0.25)); - sb = sbDo*exp(-R); + sb = current.sbDo*exp(-R); - if(Reff > 0.0) sb += sbSo*exp(-7.6693*pow((x[0]*x[0] + x[1]*x[1])/Reff/Reff,0.125)); + if(current.Reff > 0.0) sb += current.sbSo*exp(-7.6693*pow((x[0]*x[0] + x[1]*x[1]) + /current.Reff/current.Reff,0.125)); // if(sb < 1.0e-4*(sbDo + sbSo) ) return 0.0; sb *= pow(10,-0.4*48.6)*inv_hplanck; @@ -146,11 +122,11 @@ PosType SourceOverzier::SurfaceBrightness( } PosType SourceOverzier::getTotalFlux() const{ - return pow(10,-(48.6+mag)/2.5); + return pow(10,-(48.6 + current.mag)/2.5); } void SourceOverzier::printSource(){ - std::cout << "bulge half light radius: " << Reff*180*60*60/PI << " arcs disk scale hight: " << Rh*180*60*60/PI << " arcs" << std::endl; + std::cout << "bulge half light radius: " << current.Reff*180*60*60/PI << " arcs disk scale hight: " << current.Rh*180*60*60/PI << " arcs" << std::endl; } void SourceOverzier::assignParams(InputParams& /* params */) @@ -158,35 +134,37 @@ void SourceOverzier::assignParams(InputParams& /* params */) } PosType SourceOverzier::getMag(Band band) const { - return mag_map.at(band); + return current.mag_map.at(band); } PosType SourceOverzier::getMagBulge(Band band) const { - return bulge_mag_map.at(band); + return current.bulge_mag_map.at(band); } void SourceOverzier::setMag(Band band,PosType my_mag){ - mag_map[band] = my_mag; + current.mag_map[band] = my_mag; } void SourceOverzier::setMagBulge(Band band,PosType my_mag){ - bulge_mag_map[band] = my_mag; + current.bulge_mag_map[band] = my_mag; } void SourceOverzier::changeBand(Band band){ - mag = mag_map[band]; - mag_bulge = bulge_mag_map[band]; + current.mag = current.mag_map[band]; + current.mag_bulge = current.bulge_mag_map[band]; - renormalize(); + renormalize_current(); } -void SourceOverzier::renormalize(){ +void SourceOverzier::renormalize_current(){ float BtoT = getBtoT(); - if(Rh > 0.0) sbDo = pow(10,-mag/2.5)*0.159148*(1-BtoT)/pow(Rh,2); - else sbDo = 0.0; - if(Reff > 0.0) sbSo = pow(10,-mag/2.5)*94.484376*BtoT/pow(Reff,2); - else sbSo = 0.0; + if(current.Rh > 0.0) current.sbDo = pow(10,-current.mag/2.5)*0.159148*(1-BtoT) + /pow(current.Rh,2); + else current.sbDo = 0.0; + if(current.Reff > 0.0) current.sbSo = pow(10,-current.mag/2.5)*94.484376*BtoT + /pow(current.Reff,2); + else current.sbSo = 0.0; } @@ -214,9 +192,9 @@ SourceOverzier(my_mag,my_mag_bulge,my_Reff,my_Rh,my_PA,inclination,my_id,my_z,th spheroid.ReSet(my_mag_bulge,my_Reff,-my_PA + 10*(ran() - 0.5)*PI/180,index,q,my_z,theta); - cospa = cos(PA); - sinpa = sin(PA); - cosi = cos(inclination); + cospa = cos(current.PA); + sinpa = sin(current.PA); + cosi = cos(current.inclination); modes.resize(6); for(PosType &mod : modes){ @@ -290,16 +268,16 @@ PosType SourceOverzierPlus::SurfaceBrightness(PosType *y){ Point_2d z; PosType sb = 0; - if(xlength > 0 && sbDo > 0){ + if(xlength > 0 && current.sbDo > 0){ z[0] = cospa*x[0] - sinpa*x[1]; z[1] = ( sinpa*x[0] + cospa*x[1] )/cosi; //PosType R = sqrt( cxx*x[0]*x[0] + cyy*x[1]*x[1] + cxy*x[0]*x[1] ); - PosType R = z.length()/Rh; + PosType R = z.length()/current.Rh; PosType theta = atan2(z[1],z[0]); //disk - sb = sbDo*exp(-R); + sb = current.sbDo*exp(-R); //spiral arms PosType phir = mctalpha*log(R/0.5) + disk_phase; //PosType phiro = 0.4*mctalpha*log(R/0.5) + disk_phase; @@ -307,7 +285,7 @@ PosType SourceOverzierPlus::SurfaceBrightness(PosType *y){ sb *= 1 + Ad*cos(Narms*theta + phir); //else disk_sb *= 1 + Ad*cos(Narms*theta + phiro); }else{ - sb = sbDo; + sb = current.sbDo; } assert(sb >= 0.0); @@ -317,7 +295,7 @@ PosType SourceOverzierPlus::SurfaceBrightness(PosType *y){ //std::cout << "disk sb " << sb << std::endl; // bulge - if(Reff > 0.0){ + if(current.Reff > 0.0){ PosType perturb = 1.0,tmp; if(xlength > 0){ // bulge perturbations @@ -357,26 +335,29 @@ void SourceOverzierPlus::changeBand(Band band){ //mag = mag_map.at(band); //mag_bulge = bulge_mag_map.at(band); //SourceOverzier::renormalize(); - if(Reff > 0.0){ - spheroid.setMag(mag_bulge); + if(current.Reff > 0.0){ + spheroid.setMag(current.mag_bulge); } } void SourceOverzierPlus::randomize(Utilities::RandomNumbers_NR &ran){ + // reset to original parameters to prevent random walk + current = original; + float BtoT; { // SourceOverzier variables - Reff *= (1 + 0.2*(2*ran()-1.)); - Rh *= (1 + 0.2*(2*ran()-1.)); + current.Reff *= (1 + 0.2*(2*ran()-1.)); + current.Rh *= (1 + 0.2*(2*ran()-1.)); PosType tmp = 0.01*(2*ran()-1.); - for(auto mag = mag_map.begin() ; mag != mag_map.end() ; ++mag){ + for(auto mag = current.mag_map.begin() ; mag != current.mag_map.end() ; ++mag){ mag->second = mag->second + tmp; } - for(auto mag = bulge_mag_map.begin() ; mag != bulge_mag_map.end() ; ++mag){ + for(auto mag = current.bulge_mag_map.begin() ; mag != current.bulge_mag_map.end() ; ++mag){ mag->second = mag->second + tmp; } @@ -389,33 +370,33 @@ void SourceOverzierPlus::randomize(Utilities::RandomNumbers_NR &ran){ setJMag(getMag(J) + tmp); setKMag(getMag(Ks) + tmp); */ - mag += tmp; + current.mag += tmp; - PA = PI*ran(); - inclination = 1.0*PI/2*ran(); - if(cos(inclination)< 0.25) inclination = acos(0.25); + current.PA = PI*ran(); + current.inclination = 1.0*PI/2*ran(); + if(cos(current.inclination)< 0.25) current.inclination = acos(0.25); - cospa = cos(PA); - sinpa = sin(PA); - cosi = cos(inclination); - - if(Rh > 0.0){ - cxx = ( cospa*cospa + pow(sinpa/cosi,2) )/Rh/Rh; - cyy = ( sinpa*sinpa + pow(cospa/cosi,2) )/Rh/Rh; - cxy = ( 2*cospa*sinpa*(1-1./cosi/cosi) )/Rh/Rh; + cospa = cos(current.PA); + sinpa = sin(current.PA); + cosi = cos(current.inclination); + + if(current.Rh > 0.0){ + current.cxx = ( cospa*cospa + pow(sinpa/cosi,2) )/current.Rh/current.Rh; + current.cyy = ( sinpa*sinpa + pow(cospa/cosi,2) )/current.Rh/current.Rh; + current.cxy = ( 2*cospa*sinpa*(1-1./cosi/cosi) ) /current.Rh/current.Rh; }else{ - cxx = cyy = cxy = 0.0; + current.cxx = current.cyy = current.cxy = 0.0; } - SourceOverzier::renormalize(); + SourceOverzier::renormalize_current(); // radius // weighted mean between the radii that enclose 99% of the flux // in the pure De Vacouleur/exponential disk case // 6.670 = 3.975*Re = 3.975*1.678*Rh BtoT = getBtoT(); - setRadius(6.670*Rh*(1-BtoT)+18.936*Reff*BtoT); + setRadius(6.670*current.Rh*(1-BtoT) + 18.936*current.Reff*BtoT); } float minA = 0.01,maxA = 0.2; // minimum and maximum amplitude of arms @@ -430,15 +411,16 @@ void SourceOverzierPlus::randomize(Utilities::RandomNumbers_NR &ran){ // extra cersic component //double index = 4 + 3*(ran()-0.5)*2; - double index = 4*pow(MAX(BtoT,0.03),0.4)*pow(10,0.2*(ran()-0.5)); + double index = 4*pow(MAX(BtoT,0.03),0.4)*pow(10,0.2*(ran()-0.5)); + assert(index > 0.5 && index < 9); double q = 1 + (0.5-1)*ran(); /*delete spheroid; spheroid = new SourceSersic(mag_bulge,Reff/arcsecTOradians,-PA + 10*(ran() - 0.5)*PI/180,index,q,zsource,getTheta().x); */ - spheroid.ReSet(mag_bulge,Reff/arcsecTOradians,-PA + 10*(ran() - 0.5)*PI/180,index,q,zsource,getTheta().x); + spheroid.ReSet(current.mag_bulge,current.Reff/arcsecTOradians,-current.PA + 10*(ran() - 0.5)*PI/180,index,q,zsource,getTheta().x); for(PosType &mod : modes){ diff --git a/include/overzier_source.h b/include/overzier_source.h index d2fb35f6..4badb248 100644 --- a/include/overzier_source.h +++ b/include/overzier_source.h @@ -38,9 +38,9 @@ class SourceOverzier : public Source unsigned long getID() { return haloID; } /// get magnitude of whole galaxy. Which band this is in depends on which was passed in the constructor - PosType getMag() const { return mag; } + PosType getMag() const { return current.mag; } PosType getMag(Band band) const ; - PosType getMagBulge() const { return mag_bulge; } + PosType getMagBulge() const { return current.mag_bulge; } PosType getMagBulge(Band band) const; /* @@ -69,14 +69,14 @@ class SourceOverzier : public Source void setMagBulge(Band band,PosType my_mag); /// bulge half light radius in radians - PosType getReff() const { return Reff/(PI/180/60/60); } + PosType getReff() const { return current.Reff/(PI/180/60/60); } /// disk scale height in radians - PosType getRh() const { return Rh/(PI/180/60/60); } + PosType getRh() const { return current.Rh/(PI/180/60/60); } /// the bulge to total flux ratio - PosType getBtoT() const { return pow(10,(-mag_bulge + mag)/2.5); } - PosType getPA() const { return PA; } - PosType getInclination() const { return inclination;} + PosType getBtoT() const { return pow(10,(-current.mag_bulge + current.mag)/2.5); } + PosType getPA() const { return current.PA; } + PosType getInclination() const { return current.inclination;} float getSEDtype() const {return sedtype;} void setSEDtype(float s){ sedtype = s;} @@ -86,7 +86,7 @@ class SourceOverzier : public Source /** Returns minimum of the radii at which disk and bulge have a surf. brightness equal to a fraction f of the central one * TODO: Fabio: Needs to be tested and improved (Bulge is so steep in the center that output values are very small) */ - inline PosType getMinSize(PosType f) {return std::min(1.678*Reff*fabs(cos(inclination))*pow(-log (f)/7.67,4),Rh*(-log (f)/1.67));} + inline PosType getMinSize(PosType f) {return std::min(1.678*current.Reff*fabs(cos(current.inclination))*pow(-log (f)/7.67,4),current.Rh*(-log (f)/1.67));} static PosType *getx(SourceOverzier &sourceo){return sourceo.source_x.x;} @@ -94,31 +94,36 @@ class SourceOverzier : public Source float sedtype = -1; // renormalize the disk and bulge to agree with current mag and mag_bulge - void renormalize(); + void renormalize_current(); void assignParams(InputParams& params); /// haloID unsigned long haloID; - /// bulge half light radius - PosType Reff; - /// disk scale height - PosType Rh; + struct Params{ + /// bulge half light radius + PosType Reff=0; + /// disk scale height + PosType Rh=0; - //PosType BtoT; - PosType PA; - PosType inclination; + //PosType BtoT; + PosType PA=0; + PosType inclination=0; - PosType cxx,cyy,cxy; - PosType sbDo; - PosType sbSo; - PosType mag; - PosType mag_bulge; + PosType cxx=0,cyy=0,cxy=0; + PosType sbDo=0; + PosType sbSo=0; + PosType mag=0; + PosType mag_bulge=0; + + // colors + std::map mag_map; + std::map bulge_mag_map; + }; - // colors - std::map mag_map; - std::map bulge_mag_map; - + Params current; + Params original; + // optional position variables }; From 0e9ba214ee98f764e828689b30bead2b04ed089f Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Wed, 20 Mar 2019 11:13:47 +0100 Subject: [PATCH 120/227] Moved memory of original parameters from SourceOverzier to SourceOverzierPlus --- Source/overzier.cpp | 10 ++++------ include/overzier_source.h | 18 ++++++++++++++++-- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/Source/overzier.cpp b/Source/overzier.cpp index c29dbe82..2fa91c52 100644 --- a/Source/overzier.cpp +++ b/Source/overzier.cpp @@ -38,7 +38,6 @@ SourceOverzier::~SourceOverzier() SourceOverzier::SourceOverzier(const SourceOverzier &s) :Source(s){ - original = s.original; current = s.current; sedtype = s.sedtype; @@ -48,8 +47,7 @@ SourceOverzier& SourceOverzier::operator=(const SourceOverzier &s){ Source::operator=(s); - original = s.original; - current = s.current; + current = s.current; sedtype = s.sedtype; return *this; } @@ -91,8 +89,6 @@ void SourceOverzier::setInternals(double my_mag,double my_mag_bulge,double my_Re setTheta(my_theta[0], my_theta[1]); else setTheta(0, 0); - - original = current; } /// Surface brightness in erg/cm^2/sec/rad^2/Hz @@ -126,7 +122,7 @@ PosType SourceOverzier::getTotalFlux() const{ } void SourceOverzier::printSource(){ - std::cout << "bulge half light radius: " << current.Reff*180*60*60/PI << " arcs disk scale hight: " << current.Rh*180*60*60/PI << " arcs" << std::endl; + current.print(); } void SourceOverzier::assignParams(InputParams& /* params */) @@ -172,6 +168,7 @@ SourceOverzierPlus::SourceOverzierPlus(PosType my_mag,PosType my_mag_bulge,PosTy SourceOverzier(my_mag,my_mag_bulge,my_Reff,my_Rh,my_PA,inclination,my_id,my_z,theta) { //std::cout << "SourceOverzierPlus constructor" << std::endl; + original = current; float minA = 0.01,maxA = 0.2; // minimum and maximum amplitude of arms Narms = (ran() > 0.2) ? 2 : 4; // number of arms @@ -427,6 +424,7 @@ void SourceOverzierPlus::randomize(Utilities::RandomNumbers_NR &ran){ mod = 5.0e-2*ran(); } + //SourceOverzier::printSource(); } diff --git a/include/overzier_source.h b/include/overzier_source.h index 4badb248..3e1dcc3d 100644 --- a/include/overzier_source.h +++ b/include/overzier_source.h @@ -119,11 +119,23 @@ class SourceOverzier : public Source // colors std::map mag_map; std::map bulge_mag_map; + + void print(){ + /// bulge half light radius + std::cout << "Reff :" << Reff/arcsecTOradians << "arcsec "; + std::cout << "Rh :" << Rh/arcsecTOradians << " arcsec "; + std::cout << "PA :" << PA << " "; + std::cout << "inclination :" << inclination << " "; + std::cout << "sbDo :" << sbDo << " "; + std::cout << "sbSo :" << sbSo << " "; + std::cout << "mag :" << mag << " "; + std::cout << "mag_bulge :" << mag_bulge << " "; + + std::cout << "BtoT :" << pow(10,(-mag_bulge + mag)/2.5) << std::endl; + } }; Params current; - Params original; - // optional position variables }; @@ -180,5 +192,7 @@ class SourceOverzierPlus : public SourceOverzier std::vector modes; PosType disk_phase; PosType cospa,sinpa,cosi; + + SourceOverzier::Params original; // original parameters }; #endif /* GALAXIES_OVERZIER_H_ */ From 4e74fc3957b3ab6023ef2a8dcdafaa09c364565d Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Wed, 20 Mar 2019 11:15:43 +0100 Subject: [PATCH 121/227] Created ImageInfo::highestSurfaceBrightness() --- TreeCode_link/TreeDriver.cpp | 20 +++++++++++++++++++- include/image_info.h | 2 ++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/TreeCode_link/TreeDriver.cpp b/TreeCode_link/TreeDriver.cpp index 2b62a8b2..bf7250d5 100644 --- a/TreeCode_link/TreeDriver.cpp +++ b/TreeCode_link/TreeDriver.cpp @@ -1474,7 +1474,6 @@ KappaType ImageInfo::aveTimeDelay() return tmp_dt; } -/// Computes the time delay averaged over the image RAY ImageInfo::closestRay(const Point_2d &y){ Point *p; @@ -1495,6 +1494,25 @@ RAY ImageInfo::closestRay(const Point_2d &y){ return RAY(p); } + +RAY ImageInfo::highestSurfaceBrightnessRay(){ + Point *p; + + double sb = -1; + for(Kist::iterator it = imagekist->begin() + ; it != imagekist->end() + ; ++it){ + + double tsb = (*it).surface_brightness; + if(tsb > sb){ + p = &(*it); + sb = tsb; + } + } + + return RAY(p); +} + ImageInfo & ImageInfo::operator+=(ImageInfo & rhs){ if(!(uniform_mag && rhs.uniform_mag diff --git a/include/image_info.h b/include/image_info.h index e63d3ad3..b8ed73ac 100644 --- a/include/image_info.h +++ b/include/image_info.h @@ -73,6 +73,8 @@ struct ImageInfo{ KappaType aveInvMag(); /// finds the ray in the image that is closest to the point y on the source plane RAY closestRay(const Point_2d &y); + /// finds the ray in the image that has the highest surface brightness + RAY highestSurfaceBrightnessRay(); /// Print information about the image void PrintImageInfo(); void copy(const ImageInfo & image,bool copykists = true); From f06a4683c87cfb9c9ade488ed75ec5ec4f28305d Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Mon, 1 Apr 2019 18:53:27 +0200 Subject: [PATCH 122/227] Fixed the bug in SourceOverzierPlus the caused the randomisation zero out parameters. --- AnalyticNSIE/sourceAnaGalaxy.cpp | 128 +++++++++++++------------------ Source/overzier.cpp | 28 +++---- Source/sersic.cpp | 32 ++++++++ include/overzier_source.h | 12 +-- include/sersic_source.h | 19 +++-- 5 files changed, 117 insertions(+), 102 deletions(-) diff --git a/AnalyticNSIE/sourceAnaGalaxy.cpp b/AnalyticNSIE/sourceAnaGalaxy.cpp index 2896b0c2..7efc4e7a 100644 --- a/AnalyticNSIE/sourceAnaGalaxy.cpp +++ b/AnalyticNSIE/sourceAnaGalaxy.cpp @@ -178,7 +178,7 @@ void SourceMultiAnaGalaxy::readDataFileMillenn(Utilities::RandomNumbers_NR &ran) std::string f=","; std::stringstream buffer; - PosType mag,mag_bulge; + //PosType mag,mag_bulge; // read in data for(i=0,j=0 ; ; ++i){ @@ -219,87 +219,69 @@ void SourceMultiAnaGalaxy::readDataFileMillenn(Utilities::RandomNumbers_NR &ran) << " " << H_band_Bulge << " " << Ks_band_Bulge << " " << i1_Bulge << " " << i2_Bulge << std::endl; - if(mag < mag_limit){ - //std::cout << "good mag " << std::endl; - - /* - std::cout << galid << c << haloid << c << cx << c << cy << c << cz << c << ra << c << dec << c << z_geo << c << z_app - << c << dlum << c << vlos << c << incl - << c << acs435 << c << acs606 << c << acs775 << c << acs850 - << c << acs435_bulge << c << acs606_bulge << c << acs775_bulge << c << acs850_bulge - << c << type << c << mvir << c << rvir << c << vmax << c << stellarmass << c << bulgemass - << c << stellardiskradius << c << bulgesize - << c << inclination << c << pa << c << angdist << c << diskradius_arcsec << c << bulgesize_arcsec << std::endl; - */ - - // converting from Millennium conventions - theta[0] = -ra*PI/180; - theta[1] = dec*PI/180; - pa = (90 - pa)*PI/180; - inclination *= PI/180; - if(cos(inclination)< 0.5) inclination = acos(0.5); + // converting from Millennium conventions + theta[0] = -ra*PI/180; + theta[1] = dec*PI/180; + pa = (90 - pa)*PI/180; + inclination *= PI/180; + if(cos(inclination)< 0.5) inclination = acos(0.5); //std::cout << "did inclination" << std::endl; - - - if(j == 0){ - rangex[0] = rangex[1] = theta[0]; - rangey[0] = rangey[1] = theta[1]; - }else{ - //std::cout << "rang" << std::endl; - - if(theta[0] < rangex[0]) rangex[0] = theta[0]; - if(theta[0] > rangex[1]) rangex[1] = theta[0]; - if(theta[1] < rangey[0]) rangey[0] = theta[1]; - if(theta[1] > rangey[1]) rangey[1] = theta[1]; - } + if(j == 0){ + rangex[0] = rangex[1] = theta[0]; + rangey[0] = rangey[1] = theta[1]; + }else{ + //std::cout << "rang" << std::endl; + + if(theta[0] < rangex[0]) rangex[0] = theta[0]; + if(theta[0] > rangex[1]) rangex[1] = theta[0]; + if(theta[1] < rangey[0]) rangey[0] = theta[1]; + if(theta[1] > rangey[1]) rangey[1] = theta[1]; + } //std::cout << "adding to galaxies" << std::endl; /***************************/ - galaxies.push_back(SourceOverzierPlus(mag,mag_bulge,Ref,Rh - ,pa,inclination,HaloID,z_cosm,theta,ran)); - //std::cout << "filling last galaxy" << std::endl; - - galaxies.back().setMag(SDSS_U,SDSS_u); - galaxies.back().setMagBulge(SDSS_U,SDSS_u_Bulge); - galaxies.back().setMag(SDSS_G,SDSS_g); - galaxies.back().setMagBulge(SDSS_G,SDSS_g_Bulge); - galaxies.back().setMag(SDSS_R,SDSS_r); - galaxies.back().setMagBulge(SDSS_R,SDSS_r_Bulge); - galaxies.back().setMag(SDSS_I,SDSS_i); - galaxies.back().setMagBulge(SDSS_I,SDSS_i_Bulge); - galaxies.back().setMag(SDSS_Z,SDSS_z); - galaxies.back().setMagBulge(SDSS_Z,SDSS_z_Bulge); - galaxies.back().setMag(J,J_band); - galaxies.back().setMagBulge(J,J_band_Bulge); - galaxies.back().setMag(H,H_band); - galaxies.back().setMagBulge(H,H_band_Bulge); - galaxies.back().setMag(Ks,Ks_band); - galaxies.back().setMagBulge(Ks,Ks_band_Bulge); - - galaxies.back().setMag(Ks,Ks_band); - galaxies.back().setMagBulge(Ks,Ks_band_Bulge); - - galaxies.back().changeBand(band); - - // cluge ???? - // The Euclid bands are not actually read in - galaxies.back().setMag(EUC_VIS,SDSS_i); - galaxies.back().setMagBulge(EUC_VIS,SDSS_i_Bulge); - galaxies.back().setMag(EUC_H,H_band); - galaxies.back().setMagBulge(EUC_H,H_band_Bulge); - galaxies.back().setMag(EUC_J,J_band); - galaxies.back().setMagBulge(EUC_J,J_band_Bulge); + + SourceOverzierPlus galaxy(SDSS_i,SDSS_i_Bulge,Ref,Rh + ,pa,inclination,HaloID,z_cosm,theta,ran); + //std::cout << "filling last galaxy" << std::endl; + + galaxy.setMag(SDSS_U,SDSS_u); + galaxy.setMagBulge(SDSS_U,SDSS_u_Bulge); + galaxy.setMag(SDSS_G,SDSS_g); + galaxy.setMagBulge(SDSS_G,SDSS_g_Bulge); + galaxy.setMag(SDSS_R,SDSS_r); + galaxy.setMagBulge(SDSS_R,SDSS_r_Bulge); + galaxy.setMag(SDSS_I,SDSS_i); + galaxy.setMagBulge(SDSS_I,SDSS_i_Bulge); + galaxy.setMag(SDSS_Z,SDSS_z); + galaxy.setMagBulge(SDSS_Z,SDSS_z_Bulge); + galaxy.setMag(J,J_band); + galaxy.setMagBulge(J,J_band_Bulge); + galaxy.setMag(H,H_band); + galaxy.setMagBulge(H,H_band_Bulge); + galaxy.setMag(Ks,Ks_band); + galaxy.setMagBulge(Ks,Ks_band_Bulge); + + galaxy.setMag(Ks,Ks_band); + galaxy.setMagBulge(Ks,Ks_band_Bulge); + // The Euclid bands are not actually read in + galaxy.setMag(EUC_VIS,SDSS_i); + galaxy.setMagBulge(EUC_VIS,SDSS_i_Bulge); + galaxy.setMag(EUC_H,H_band); + galaxy.setMagBulge(EUC_H,H_band_Bulge); + galaxy.setMag(EUC_J,J_band); + galaxy.setMagBulge(EUC_J,J_band_Bulge); + + galaxy.changeBand(band); - //std::cout << "z:" << z_cosm << " mag " << SDSS_u << " Bulge to total " << pow(10,-(SDSS_u_Bulge-SDSS_u)/2.5) - // << " bulge size arcsec " << Ref << " disk size arcsec " << pa << " position angle " << pa << " inclination " << inclination - // << " theta = " << theta[0] << " " << theta[1] << std::endl; - + if(galaxy.getMag() < mag_limit ){ + galaxies.push_back(galaxy); assert(galaxies.back().getMag(band) == galaxies.back().getMag() ); //std::cout << " j in SourceMultiAnaGalaxy : " << j << std::endl; - ++j; - } + ++j; + } } std::cout << " closing file in SourceMultiAnaGalaxy : " << std::endl; diff --git a/Source/overzier.cpp b/Source/overzier.cpp index 2fa91c52..86f05a5b 100644 --- a/Source/overzier.cpp +++ b/Source/overzier.cpp @@ -10,10 +10,10 @@ #include "slsimlib.h" /// sets everything to zero -SourceOverzier::SourceOverzier() -: haloID(0) -{ -} +//SourceOverzier::SourceOverzier() +//: haloID(0) +//{ +//} SourceOverzier::SourceOverzier( PosType my_mag /// Total magnitude @@ -29,6 +29,8 @@ SourceOverzier::SourceOverzier( //std::cout << "SourceOverzier constructor" << std::endl; setInternals(my_mag,my_mag_bulge,my_Reff,my_Rh,my_PA,my_inclination,my_id,my_z,my_theta); + + assert(current.Reff != 0 || current.Rh !=0 ); } SourceOverzier::~SourceOverzier() @@ -39,7 +41,6 @@ SourceOverzier::SourceOverzier(const SourceOverzier &s) :Source(s){ current = s.current; - sedtype = s.sedtype; } SourceOverzier& SourceOverzier::operator=(const SourceOverzier &s){ @@ -47,7 +48,7 @@ SourceOverzier& SourceOverzier::operator=(const SourceOverzier &s){ Source::operator=(s); - current = s.current; + current = s.current; sedtype = s.sedtype; return *this; } @@ -57,6 +58,7 @@ void SourceOverzier::setInternals(double my_mag,double my_mag_bulge,double my_Re haloID = my_id; + if(my_Reff < 0.05) my_Reff = 0.05; // ???? current.Reff = my_Reff*arcsecTOradians; current.Rh = my_Rh*arcsecTOradians; current.mag = my_mag; @@ -198,6 +200,7 @@ SourceOverzier(my_mag,my_mag_bulge,my_Reff,my_Rh,my_PA,inclination,my_id,my_z,th mod = 2.0e-2*ran(); } + assert(original.Rh != 0 || original.Reff != 0); } SourceOverzierPlus::~SourceOverzierPlus(){ @@ -220,9 +223,8 @@ Narms(p.Narms),Ad(p.Ad),mctalpha(p.mctalpha),arm_alpha(p.arm_alpha) */ spheroid = p.spheroid; - - //*spheroid = *(p.spheroid); modes = p.modes; + original = p.original; } SourceOverzierPlus & SourceOverzierPlus::operator=(const SourceOverzierPlus &p){ @@ -238,17 +240,9 @@ SourceOverzierPlus & SourceOverzierPlus::operator=(const SourceOverzierPlus &p){ sinpa=p.sinpa; cosi=p.cosi; - /*delete spheroid; - spheroid = new SourceSersic(p.spheroid->getMag(),p.getReff() - ,p.spheroid->getPA() - ,p.spheroid->getSersicIndex() - ,p.spheroid->getAxesRatio() - ,p.spheroid->getZ() - ,p.spheroid->getTheta().x); - */ spheroid = p.spheroid; - modes = p.modes; + original = p.original; return *this; } diff --git a/Source/sersic.cpp b/Source/sersic.cpp index dd060d1a..399ca553 100644 --- a/Source/sersic.cpp +++ b/Source/sersic.cpp @@ -27,6 +27,38 @@ SourceSersic::SourceSersic( assert(my_Reff > 0); ReSet(my_mag,my_Reff,my_PA,my_index,my_q,my_z,my_theta); } +SourceSersic::SourceSersic(const SourceSersic &p){ + Reff = p.Reff; + mag = p.mag; + PA = p.PA; + index = p.index; + bn = p.bn; + q = p.q; + flux = p.flux; + I_r = p.I_r; + I_n = p.I_n; + I_q = p.I_q; + cosPA = p.cosPA; + sinPA = p.sinPA; +} +SourceSersic& SourceSersic::operator=(const SourceSersic &p){ + if(this == &p) return *this; + + Reff = p.Reff; + mag = p.mag; + PA = p.PA; + index = p.index; + bn = p.bn; + q = p.q; + flux = p.flux; + I_r = p.I_r; + I_n = p.I_n; + I_q = p.I_q; + cosPA = p.cosPA; + sinPA = p.sinPA; + + return *this; +} /// Reset all the parameters void SourceSersic::ReSet( diff --git a/include/overzier_source.h b/include/overzier_source.h index 3e1dcc3d..c4d4c292 100644 --- a/include/overzier_source.h +++ b/include/overzier_source.h @@ -22,7 +22,7 @@ class SourceOverzier : public Source { public: - SourceOverzier(); + //SourceOverzier(); SourceOverzier(PosType mag,PosType mag_bulge,PosType Reff,PosType Rh,PosType PA,PosType inclination,unsigned long my_id,PosType my_z=0,const PosType *theta=0); SourceOverzier(const SourceOverzier &s); @@ -68,10 +68,10 @@ class SourceOverzier : public Source /// magnitude in specific band void setMagBulge(Band band,PosType my_mag); - /// bulge half light radius in radians - PosType getReff() const { return current.Reff/(PI/180/60/60); } - /// disk scale height in radians - PosType getRh() const { return current.Rh/(PI/180/60/60); } + /// bulge half light radius in arcseconds + PosType getReff() const { return current.Reff/arcsecTOradians; } + /// disk scale height in arcseconds + PosType getRh() const { return current.Rh/arcsecTOradians; } /// the bulge to total flux ratio PosType getBtoT() const { return pow(10,(-current.mag_bulge + current.mag)/2.5); } @@ -122,7 +122,7 @@ class SourceOverzier : public Source void print(){ /// bulge half light radius - std::cout << "Reff :" << Reff/arcsecTOradians << "arcsec "; + std::cout << "Reff :" << Reff/arcsecTOradians << " arcsec "; std::cout << "Rh :" << Rh/arcsecTOradians << " arcsec "; std::cout << "PA :" << PA << " "; std::cout << "inclination :" << inclination << " "; diff --git a/include/sersic_source.h b/include/sersic_source.h index 02688ada..94c2b758 100644 --- a/include/sersic_source.h +++ b/include/sersic_source.h @@ -23,15 +23,20 @@ class SourceSersic : public Source SourceSersic(); SourceSersic(PosType mag,PosType Reff,PosType PA,PosType my_index,PosType my_q,PosType my_z,const PosType *theta=0); ~SourceSersic(); - + + SourceSersic(const SourceSersic &p); + SourceSersic & operator=(const SourceSersic &p); + + void ReSet(PosType mag,PosType Reff,PosType PA,PosType my_index,PosType my_q,PosType my_z,const PosType *theta=0); - /// calculates radius where the surface brightness drops by a factor f with respect to the central peak + /// calculates radius where the surface brightness drops by a factor f with respect to the central peak in radians inline PosType FractionRadius (PosType f) {return Reff*pow(-log (f)/bn,index);} inline PosType getSersicIndex() const { return index; } inline PosType getAxesRatio() const { return q; } - inline PosType getReff() const { return Reff*180*60*60/PI; } + /// in arcseconds + inline PosType getReff() const { return Reff/arcsecTOradians; } inline PosType getMag() const { return mag; } inline PosType getPA() const { return PA; } @@ -49,9 +54,10 @@ class SourceSersic : public Source I_q = 1./q; } - inline void setReff(PosType x) + inline void setReff(PosType x // in arcseconds + ) { - Reff = x*PI/180/60/60; + Reff = x*arcsecTOradians; I_r = 1./2./PI/Reff/Reff; updateRadius(); } @@ -83,7 +89,8 @@ class SourceSersic : public Source } void assignParams(InputParams& params); - PosType Reff; + + PosType Reff; // in radians PosType mag; PosType PA; PosType index; From 7d3001c6143f0603c2a0ca3107edc726bccd6de5 Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Fri, 5 Apr 2019 12:55:58 +0200 Subject: [PATCH 123/227] Fixed bug in Utilities::XYcsvLookUp::find() --- TreeCode_link/utilities.cpp | 5 ++++- include/lens_halos.h | 4 ---- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/TreeCode_link/utilities.cpp b/TreeCode_link/utilities.cpp index 05b962d5..80c78baa 100644 --- a/TreeCode_link/utilities.cpp +++ b/TreeCode_link/utilities.cpp @@ -777,7 +777,7 @@ Utilities::XYcsvLookUp::XYcsvLookUp( if(verbose){ std::cout << "min X : "<< data[0][Xindex] << " max X : " << data.back()[Xindex] << std::endl; - std::cout << "redshift bins " << std::endl; + std::cout << column_names[Xindex] << " Bins " << std::endl; } for(int i=1 ; i Utilities::XYcsvLookUp::find(double x,double y){ current = std::upper_bound(borders[xbin],borders[xbin+1],y , [this](double y,const std::vector &v1){return y < v1[Yindex];}); + if(current == borders[xbin+1]) current = borders[xbin+1] - 1; + //std::cout << "XYcsvLookUp boundaries :" << (*borders[xbin])[Yindex] << " " << (*borders[xbin+1])[Yindex] << std::endl; + //assert(fabs(y-(*current)[Yindex]) < 1); return *current; } double Utilities::XYcsvLookUp::Ymin(double x) const{ diff --git a/include/lens_halos.h b/include/lens_halos.h index 843e2b23..fdaee163 100644 --- a/include/lens_halos.h +++ b/include/lens_halos.h @@ -652,10 +652,6 @@ class LensHalo{ PosType *x; LensHalo* isohalo; }; - - - - }; /** From 9f65b3d977cd92e61eb36e9005bbac5770496fed Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Mon, 8 Apr 2019 22:26:07 +0200 Subject: [PATCH 124/227] Added DataFrame class. A lot of extra indentions. --- ImageProcessing/pixelize.cpp | 17 +++++++++-- MultiPlane/particle_lens.cpp | 10 +++--- TreeCode_link/utilities.cpp | 14 ++++----- include/gridmap.h | 46 ++++++++++++++-------------- include/image_processing.h | 7 +++-- include/point.h | 7 +++++ include/simpleTree.h | 6 ++-- include/sourceAnaGalaxy.h | 6 ++-- include/utilities_slsim.h | 59 +++++++++++++++++++++++++++++++++++- 9 files changed, 126 insertions(+), 46 deletions(-) diff --git a/ImageProcessing/pixelize.cpp b/ImageProcessing/pixelize.cpp index cc5a1cb6..2edcf29f 100644 --- a/ImageProcessing/pixelize.cpp +++ b/ImageProcessing/pixelize.cpp @@ -22,6 +22,12 @@ #include "source.h" #include "gridmap.h" +#include +#include +#include +#include + + /*#if __cplusplus < 201103L template void swap(std::valarray& x, std::valarray& y) @@ -152,10 +158,16 @@ PixelMap::PixelMap( ,double my_res /// resolution (rad) of fits image if not given in fits file, use default or -1 otherwise ) { + #ifdef ENABLE_FITS if(fitsfilename.empty()) throw std::invalid_argument("Please enter a valid filename for the FITS file input"); - + + + if(!Utilities::IO::file_exists(fitsfilename)){ + std::cerr << "Problem with inputfile " << fitsfilename << std::endl; + throw std::invalid_argument("bad file"); + } //std::auto_ptr fp(new CCfits::FITS(fitsfilename, CCfits::Read)); std::auto_ptr fp(0); @@ -166,7 +178,7 @@ PixelMap::PixelMap( catch (CCfits::FITS::CantOpen) { std::cerr << "Cannot open " << fitsfilename << std::endl; - exit(1); + throw std::invalid_argument("bad file"); } @@ -237,6 +249,7 @@ PixelMap::PixelMap( map_boundary_p2[0] = center[0] + (Nx*resolution)/2.; map_boundary_p2[1] = center[1] + (Ny*resolution)/2.; + map.resize(Nx*Ny); h0.read(map); //std::cout << "map size : " << map.size() << std::endl; #else diff --git a/MultiPlane/particle_lens.cpp b/MultiPlane/particle_lens.cpp index 0c25239f..852cc337 100644 --- a/MultiPlane/particle_lens.cpp +++ b/MultiPlane/particle_lens.cpp @@ -216,7 +216,7 @@ bool MakeParticleLenses::readCSV(int columns_used){ size_t ntot = 0; while (getline(file, line) && line[0] == '#'); std::vector vec; - Utilities::splitstring(line,vec,delimiter); + Utilities::IO::splitstring(line,vec,delimiter); const int ncolumns = vec.size(); if(ncolumns < columns_used){ @@ -244,7 +244,7 @@ bool MakeParticleLenses::readCSV(int columns_used){ if(columns_used == 3){ do{ std::vector vec; - Utilities::splitstring(line,vec,delimiter); + Utilities::IO::splitstring(line,vec,delimiter); data[ntot][0] = stof(vec[0]); data[ntot][1] = stof(vec[1]); @@ -257,7 +257,7 @@ bool MakeParticleLenses::readCSV(int columns_used){ }else if(columns_used == 4){ do{ std::vector vec; - Utilities::splitstring(line,vec,delimiter); + Utilities::IO::splitstring(line,vec,delimiter); data[ntot][0] = stof(vec[0]); data[ntot][1] = stof(vec[1]); @@ -272,7 +272,7 @@ bool MakeParticleLenses::readCSV(int columns_used){ std::cout << "Using the particle sizes from " << filename << std::endl; do{ std::vector vec; - Utilities::splitstring(line,vec,delimiter); + Utilities::IO::splitstring(line,vec,delimiter); data[ntot][0] = stof(vec[0]); data[ntot][1] = stof(vec[1]); @@ -290,7 +290,7 @@ bool MakeParticleLenses::readCSV(int columns_used){ do{ std::vector vec; - Utilities::splitstring(line,vec,delimiter); + Utilities::IO::splitstring(line,vec,delimiter); data[ntot][0] = stof(vec[0]); data[ntot][1] = stof(vec[1]); diff --git a/TreeCode_link/utilities.cpp b/TreeCode_link/utilities.cpp index 80c78baa..05974124 100644 --- a/TreeCode_link/utilities.cpp +++ b/TreeCode_link/utilities.cpp @@ -718,7 +718,7 @@ int Utilities::IO::NumberOfEntries(const std::string &string,char deliniator){ return number; } -Utilities::XYcsvLookUp::XYcsvLookUp( +Utilities::IO::XYcsvLookUp::XYcsvLookUp( std::string datafile /// input catalog file in csv format ,std::string Xlabel ,std::string Ylabel @@ -799,7 +799,7 @@ Utilities::XYcsvLookUp::XYcsvLookUp( current = data.begin(); } -Utilities::XYcsvLookUp::XYcsvLookUp( +Utilities::IO::XYcsvLookUp::XYcsvLookUp( std::string datafile /// input catalog file in csv format ,std::string Xlabel ,std::string Ylabel @@ -868,7 +868,7 @@ Utilities::XYcsvLookUp::XYcsvLookUp( current = data.begin(); } -std::vector Utilities::XYcsvLookUp::find(double x,double y){ +std::vector Utilities::IO::XYcsvLookUp::find(double x,double y){ long xbin = Utilities::locate(Xborders, x); current = std::upper_bound(borders[xbin],borders[xbin+1],y , [this](double y,const std::vector &v1){return y < v1[Yindex];}); @@ -878,16 +878,16 @@ std::vector Utilities::XYcsvLookUp::find(double x,double y){ //assert(fabs(y-(*current)[Yindex]) < 1); return *current; } -double Utilities::XYcsvLookUp::Ymin(double x) const{ +double Utilities::IO::XYcsvLookUp::Ymin(double x) const{ long xbin = Utilities::locate(Xborders, x); return (*borders[xbin])[Yindex]; } -double Utilities::XYcsvLookUp::Ymax(double x) const{ +double Utilities::IO::XYcsvLookUp::Ymax(double x) const{ long xbin = Utilities::locate(Xborders, x); return (*(borders[xbin+1]-1))[Yindex]; } -double Utilities::XYcsvLookUp::operator[](std::string label) const{ +double Utilities::IO::XYcsvLookUp::operator[](std::string label) const{ int i = 0; for(auto name : column_names){ if(name == label){ @@ -900,7 +900,7 @@ double Utilities::XYcsvLookUp::operator[](std::string label) const{ throw std::invalid_argument(label + " was not one of the columns of the galaxy data file :" + filename); } -void Utilities::splitstring(std::string &line,std::vector &vec +void Utilities::IO::splitstring(std::string &line,std::vector &vec ,const std::string &delimiter){ size_t pos = 0; diff --git a/include/gridmap.h b/include/gridmap.h index 7ee7bf97..0eee76b6 100644 --- a/include/gridmap.h +++ b/include/gridmap.h @@ -28,13 +28,13 @@ struct GridMap{ GridMap(LensHndl lens,unsigned long N1d,const double center[2],double range); - GridMap(LensHndl lens ,unsigned long Nx ,const PosType center[2] ,PosType rangeX ,PosType rangeY); + GridMap(LensHndl lens ,unsigned long Nx ,const PosType center[2] ,PosType rangeX ,PosType rangeY); ~GridMap(); - /// reshoot the rays for example when the source plane has been changed - void ReInitializeGrid(LensHndl lens); + /// reshoot the rays for example when the source plane has been changed + void ReInitializeGrid(LensHndl lens); double RefreshSurfaceBrightnesses(SourceHndl source); - void ClearSurfaceBrightnesses(); + void ClearSurfaceBrightnesses(); size_t getNumberOfPoints() const {return Ngrid_init*Ngrid_init2;} /// return initial number of grid points in each direction @@ -42,31 +42,31 @@ struct GridMap{ /// return initial range of gridded region. This is the distance from the first ray in a row to the last (unlike PixelMap) double getXRange(){return x_range;} double getYRange(){return x_range*axisratio;} - // resolution in radians - double getResolution(){return x_range/(Ngrid_init-1);} + // resolution in radians + double getResolution(){return x_range/(Ngrid_init-1);} - PixelMap writePixelMapUniform(const PosType center[],size_t Nx,size_t Ny,LensingVariable lensvar); - /// make pixel map of lensing quantities at the resolution of the GridMap - PixelMap writePixelMapUniform(LensingVariable lensvar); - void writePixelMapUniform(PixelMap &map,LensingVariable lensvar); - void writeFitsUniform(const PosType center[],size_t Nx,size_t Ny,LensingVariable lensvar,std::string filename); + PixelMap writePixelMapUniform(const PosType center[],size_t Nx,size_t Ny,LensingVariable lensvar); + /// make pixel map of lensing quantities at the resolution of the GridMap + PixelMap writePixelMapUniform(LensingVariable lensvar); + void writePixelMapUniform(PixelMap &map,LensingVariable lensvar); + void writeFitsUniform(const PosType center[],size_t Nx,size_t Ny,LensingVariable lensvar,std::string filename); - /// returns a PixelMap with the flux in pixels at a resolution of res times the original resolution - PixelMap getPixelMap(int res) const; - /// update a PixelMap with the flux in pixels at a resolution of res times the original resolution. - /// The map must have precisely the right size and center to match or an exception will be thrown. - /// Constructing the map with PixelMap getPixelMap(int res) will insure that it does. - void getPixelMap(PixelMap &map) const; + /// returns a PixelMap with the flux in pixels at a resolution of res times the original resolution + PixelMap getPixelMap(int res) const; + /// update a PixelMap with the flux in pixels at a resolution of res times the original resolution. + /// The map must have precisely the right size and center to match or an exception will be thrown. + /// Constructing the map with PixelMap getPixelMap(int res) will insure that it does. + void getPixelMap(PixelMap &map) const; - /// returns the area (radians^2) of the region with negative magnification at resolution of fixed grid - PosType EinsteinArea() const; + /// returns the area (radians^2) of the region with negative magnification at resolution of fixed grid + PosType EinsteinArea() const; - /// flux weighted magnification with current surface brightness averaged on the image plane - PosType magnification() const; + /// flux weighted magnification with current surface brightness averaged on the image plane + PosType magnification() const; - Point_2d getCenter(){return center;} + Point_2d getCenter(){return center;} - Point * operator[](size_t i){return i_points + i;}; + Point * operator[](size_t i){return i_points + i;}; private: void xygridpoints(Point *points,double range,const double *center,long Ngrid diff --git a/include/image_processing.h b/include/image_processing.h index 6a2db6cc..45bb81d6 100644 --- a/include/image_processing.h +++ b/include/image_processing.h @@ -281,15 +281,16 @@ class PixelMap void convolve(PixelMap &kernel,long center_x = 0,long center_y = 0); private: - std::valarray map; - void AddGrid_(const PointList &list,LensingVariable val); + std::valarray map; std::size_t Nx; std::size_t Ny; double resolution,rangeX,rangeY,center[2]; double map_boundary_p1[2],map_boundary_p2[2]; - double LeafPixelArea(IndexType i,Branch * branch1); + void AddGrid_(const PointList &list,LensingVariable val); + + double LeafPixelArea(IndexType i,Branch * branch1); void PointsWithinLeaf(Branch * branch1, std::list &neighborlist); bool inMapBox(Branch * branch1) const; bool inMapBox(double * branch1) const; diff --git a/include/point.h b/include/point.h index 98737d2d..001da283 100644 --- a/include/point.h +++ b/include/point.h @@ -103,6 +103,13 @@ struct Point_2d{ x[1]*=value; return *this; } + Point_2d operator*(PosType value) const{ + Point_2d tmp; + tmp[0] = x[0]*value; + tmp[1] = x[1]*value; + return tmp; + } + /// scalar product PosType operator*(const Point_2d &p){ return x[0]*p.x[0] + x[1]*p.x[1]; diff --git a/include/simpleTree.h b/include/simpleTree.h index e72616d5..af5f6402 100644 --- a/include/simpleTree.h +++ b/include/simpleTree.h @@ -92,6 +92,8 @@ struct TreeNBStruct{ * * Most of the code in TreeNB.c and TreeDriverNB.c is duplicated here as private * methods and a few public ones. + * + * PType must have a dereferencing operator [] that gives the value to be sorterd */ template class TreeSimple { @@ -101,10 +103,10 @@ class TreeSimple { /// Finds the points within a circle around center and puts their index numbers in a list template - void PointsWithinCircle(T center[2],float radius,std::list &neighborkist); + void PointsWithinCircle(T center[2],float radius,std::list &neighborkist); /// Finds the points within an ellipse around center and puts their index numbers in a list - template + template void PointsWithinEllipse(T center[2],float a_max,float a_min,float posangle,std::list &neighborkist); /// Finds the nearest N neighbors and puts their index numbers in an array, also returns the distance to the Nth neighbor for calculating smoothing diff --git a/include/sourceAnaGalaxy.h b/include/sourceAnaGalaxy.h index 65f49558..60bc53cd 100644 --- a/include/sourceAnaGalaxy.h +++ b/include/sourceAnaGalaxy.h @@ -195,9 +195,9 @@ class SourceMultiShapelets: public Source{ } void printSource(); - std::size_t getNumberOfGalaxies() const {return galaxies.size();} + std::size_t getNumberOfGalaxies() const {return galaxies.size();} /// number of galaxies - std::size_t size() const {return galaxies.size();} + std::size_t size() const {return galaxies.size();} /// Total flux coming from the current galaxy in erg/sec/Hz/cm^2 PosType getTotalFlux() const {return pow(10,-(48.6+galaxies[index].getMag())/2.5);} @@ -207,7 +207,7 @@ class SourceMultiShapelets: public Source{ /// Set angular position of current source. void setTheta(PosType my_theta[2]){galaxies[index].setTheta(my_theta);} void setTheta(PosType my_x,PosType my_y){galaxies[index].setTheta(my_x, my_y);} - void setTheta(const Point_2d &p){galaxies[index].setTheta(p);} + void setTheta(const Point_2d &p){galaxies[index].setTheta(p);} /// Return redshift of current source. PosType getZ() const {return galaxies[index].getZ();} diff --git a/include/utilities_slsim.h b/include/utilities_slsim.h index 8a3b2feb..11be8a69 100644 --- a/include/utilities_slsim.h +++ b/include/utilities_slsim.h @@ -1202,6 +1202,12 @@ namespace Utilities int GetNThreads(); namespace IO{ /// + + inline bool file_exists (const std::string& name) { + struct stat buffer; + return (stat (name.c_str(), &buffer) == 0); + } + /** \brief Read in data from an ASCII file with two columns */ template @@ -1610,7 +1616,7 @@ namespace Utilities } } - } + /*** \brief A class for reading and then looking up objects from a CSV catalog. @@ -1706,5 +1712,56 @@ namespace Utilities /// split string into vector of seporate strings that were seporated by void splitstring(std::string &line,std::vector &vec,const std::string &delimiter); + + +/** \brief class for impoting data from a csv file and allowing label string lookup like a data frame. + * + * +*/ +template< typename T> +class DataFrame{ +public: + DataFrame(std::string datafile /// input catalog file in csv format + ,size_t MaxNumber = 1000000 /// maximum number of entries read + ,char comment_char = '#' /// comment charactor for header + ,char deliniator = ',' /// deliniator between values + ):filename(datafile){ + Utilities::IO::ReadCSVnumerical1(datafile,data, column_names); + }; + + /// returns column by name + std::vector& operator[](std::string label){ + int i = 0; + for(auto name : column_names){ + if(name == label){ + return data[i]; + } + ++i; + } + for(auto c : column_names ) std::cout << c << " "; + std::cout << std::endl; + throw std::invalid_argument(label + " was not one of the columns of the galaxy data file :" + filename); + }; + + + /// returns column by number + std::vector& operator[](int i){ + return data[i]; + }; + + /// returns labels of the columns from the data file + std::vector labels() const{ + return column_names; + }; + + size_t number_of_rows(){return data[0].size();} + size_t number_of_columns(){return data.size();} +private: + std::vector > data; + std::vector column_names; + std::string filename; +}; + } } + #endif From d3d9faa404c5a36924917fc69247555fb7e75c0f Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Mon, 15 Apr 2019 21:37:55 +0200 Subject: [PATCH 125/227] Made copy constructors and assignment operators for LenHalo and LensHaloNFW. Added PixelMap writePixelMap(LensingVariable lensvar); Updated MOKAmap class. --- AnalyticNSIE/base_analens.cpp | 5 +- FullRange/implant_stars.cpp | 17 +- ImageProcessing/pixelize.cpp | 10 +- MultiPlane/MOKAfits.cpp | 244 +++++++++--------- MultiPlane/MOKAlens.cpp | 390 ++++++++++++++--------------- MultiPlane/lens_halos.cpp | 174 +++++++++++++ TreeCode_link/grid_maintenance.cpp | 18 +- include/MOKAlens.h | 20 +- include/base_analens.h | 4 + include/grid_maintenance.h | 3 + include/image_processing.h | 3 +- include/lens_halos.h | 58 +++-- include/particle_types.h | 4 +- include/utilities_slsim.h | 2 +- 14 files changed, 589 insertions(+), 363 deletions(-) diff --git a/AnalyticNSIE/base_analens.cpp b/AnalyticNSIE/base_analens.cpp index 5b23bb62..6552ec39 100644 --- a/AnalyticNSIE/base_analens.cpp +++ b/AnalyticNSIE/base_analens.cpp @@ -437,10 +437,7 @@ LensHaloBaseNSIE::~LensHaloBaseNSIE(){ if(stars_N > 0 && stars_implanted){ // std::cout << "deleting stars" << endl; //delete[] star_masses; - delete[] stars; - delete[] star_region; - delete[] star_Sigma; - Utilities::free_PosTypeMatrix(star_xdisk,star_Nregions,2); + star_xdisk.clear(); delete star_tree; } } diff --git a/FullRange/implant_stars.cpp b/FullRange/implant_stars.cpp index f90837b8..e796f9b0 100644 --- a/FullRange/implant_stars.cpp +++ b/FullRange/implant_stars.cpp @@ -39,15 +39,14 @@ void LensHalo::implant_stars( if(!(stars_implanted) ){ //star_masses = new float[stars_N]; - stars = new unsigned long[stars_N]; + stars_index.resize(stars_N); stars_xp.resize(stars_N); star_theta_force = 1.0e-1; assert(Nregions > 0); star_Nregions = Nregions; - star_region = new PosType[Nregions]; - star_Sigma = new PosType[Nregions]; - star_xdisk = Utilities::PosTypeMatrix(Nregions,2); - + star_region.resize(Nregions); + star_Sigma.resize(Nregions); + star_xdisk.resize(Nregions); } PosType mean_mstar[Nregions]; @@ -205,10 +204,10 @@ void LensHalo::remove_stars(){ if(stars_implanted){ delete star_tree; - delete stars; - delete star_region; - delete star_Sigma; - Utilities::free_PosTypeMatrix(star_xdisk,star_Nregions,2); + stars_index.clear();; + star_region.clear(); + star_Sigma.clear(); + star_xdisk.clear(); star_Nregions = 0; stars_implanted = false; diff --git a/ImageProcessing/pixelize.cpp b/ImageProcessing/pixelize.cpp index 2edcf29f..6563e802 100644 --- a/ImageProcessing/pixelize.cpp +++ b/ImageProcessing/pixelize.cpp @@ -353,7 +353,15 @@ PixelMap::PixelMap( // map.resize(0); //} -PixelMap& PixelMap::operator=(PixelMap other) +PixelMap& PixelMap::operator=(const PixelMap &other) +{ + if(this != &other){ + PixelMap copy(other); + PixelMap::swap(*this, copy); + } + return *this; +} +PixelMap& PixelMap::operator=(PixelMap &&other) { if(this != &other){ PixelMap::swap(*this, other); diff --git a/MultiPlane/MOKAfits.cpp b/MultiPlane/MOKAfits.cpp index b4d89377..a1e6a9dd 100644 --- a/MultiPlane/MOKAfits.cpp +++ b/MultiPlane/MOKAfits.cpp @@ -72,10 +72,10 @@ void LensHaloMassMap::getDims(){ PHDU *h0=&ff->pHDU(); - map->nx = h0->axis(0); - map->ny = h0->axis(1); + map.nx = h0->axis(0); + map.ny = h0->axis(1); std:: cout << "nx ny " << std:: endl; - std:: cout << map->nx << " " << map->ny << std:: endl; + std:: cout << map.nx << " " << map.ny << std:: endl; } catch(FITS::CantOpen){ std::cout << "can not open " << MOKA_input_file << std::endl; @@ -102,7 +102,7 @@ void LensHaloMassMap::readMap(){ // try to read MVIR, if it exists is a MOKA map bool moka; try { - h0->readKey ("MVIR",map->m); + h0->readKey ("MVIR",map.m); moka=true; } catch(CCfits::HDU::NoSuchKeyword) { @@ -110,7 +110,7 @@ void LensHaloMassMap::readMap(){ } // principal HDU is read - h0->read(map->convergence); + h0->read(map.convergence); if(moka){ // check if they exist if not it has to compute them @@ -122,31 +122,31 @@ void LensHaloMassMap::readMap(){ } else{ ExtHDU &h1=ff->extension(1); - h1.read(map->alpha1); + h1.read(map.alpha1); ExtHDU &h2=ff->extension(2); - h2.read(map->alpha2); + h2.read(map.alpha2); ExtHDU &h3=ff->extension(3); - h3.read(map->gamma1); + h3.read(map.gamma1); ExtHDU &h4=ff->extension(4); - h4.read(map->gamma2); + h4.read(map.gamma2); std::cout << *h0 << h1 << h2 << h3 << h4 << std::endl; } try{ /* these are always present in each fits file created by MOKA */ - h0->readKey ("SIDEL",map->boxlarcsec); - h0->readKey ("SIDEL2",map->boxlMpc); // recall you that MOKA Mpc/h - h0->readKey ("ZLENS",map->zlens); - h0->readKey ("ZSOURCE",map->zsource); - h0->readKey ("OMEGA",map->omegam); - h0->readKey ("LAMBDA",map->omegal); - h0->readKey ("H",map->h); - h0->readKey ("W",map->wq); - h0->readKey ("MSTAR",map->mstar); - // h0->readKey ("MVIR",map->m); - h0->readKey ("CONCENTRATION",map->c); - h0->readKey ("DL",map->Dlens); - h0->readKey ("DLS",map->DLS); - h0->readKey ("DS",map->DS); + h0->readKey ("SIDEL",map.boxlarcsec); + h0->readKey ("SIDEL2",map.boxlMpc); // recall you that MOKA Mpc/h + h0->readKey ("ZLENS",map.zlens); + h0->readKey ("ZSOURCE",map.zsource); + h0->readKey ("OMEGA",map.omegam); + h0->readKey ("LAMBDA",map.omegal); + h0->readKey ("H",map.h); + h0->readKey ("W",map.wq); + h0->readKey ("MSTAR",map.mstar); + // h0->readKey ("MVIR",map.m); + h0->readKey ("CONCENTRATION",map.c); + h0->readKey ("DL",map.Dlens); + h0->readKey ("DLS",map.DLS); + h0->readKey ("DS",map.DS); } catch(CCfits::HDU::NoSuchKeyword){ std::cerr << "MOKA fits map must have header keywords:" << std::endl @@ -169,16 +169,16 @@ void LensHaloMassMap::readMap(){ }else{ // Pixelized mass map - int npixels = map->nx; + int npixels = map.nx; // cut a square! - // if(map->nynx) npixels = map->ny; - // std:: valarray mapbut(map->nx*map->ny); - // for(int i=0;inx;i++) for(int j=0;jny;j++){ - // mapbut[i+map->nx*j] = map->convergence[i+map->nx*j]; + // if(map.ny mapbut(map.nx*map.ny); + // for(int i=0;iconvergence.resize(npixels*npixels); + // map.convergence.resize(npixels*npixels); // keep it like it is, even if it is a rectangle - if(map->ny!=map->nx){ + if(map.ny!=map.nx){ std:: cout << " " << std:: endl; std:: cout << " the plane maps are rectangles! " << std:: endl; std:: cout << " " << std:: endl; @@ -235,21 +235,21 @@ void LensHaloMassMap::readMap(){ // set the redshift of the plane half distance between // d1 and d2 - map->zlens = Utilities::InterpolateYvec(dli,zi,dll); + map.zlens = Utilities::InterpolateYvec(dli,zi,dll); */ - map->zlens = cosmo.invComovingDist(( d1 + d2 )*0.5); + map.zlens = cosmo.invComovingDist(( d1 + d2 )*0.5); }else{ // if angular size distances are not set use ZLENS or REDSHIFT try { - h0->readKey("ZLENS",map->zlens); + h0->readKey("ZLENS",map.zlens); } catch(CCfits::HDU::NoSuchKeyword) { try { - h0->readKey("REDSHIFT",map->zlens); + h0->readKey("REDSHIFT",map.zlens); } catch(CCfits::HDU::NoSuchKeyword){ std::cout << "unable to read fits mass map header keywords" << std::endl << " either DLUP and DLLOW need to be set or ZLENS or REDSHIFT" << std::endl; @@ -263,27 +263,27 @@ void LensHaloMassMap::readMap(){ } } - if(map->zlens <= 0.0){ + if(map.zlens <= 0.0){ std::cerr << "Pixel map lens planes cannot have zero or negative redshifts!" << std::endl; throw std::runtime_error("Invalid header"); } - map->Dlens = cosmo.angDist(0.,map->zlens); // physical - double inarcsec = 180./M_PI/map->Dlens*60.*60.; + map.Dlens = cosmo.angDist(0.,map.zlens); // physical + double inarcsec = 180./M_PI/map.Dlens*60.*60.; double pixLMpc,pixelunit; try{ // physical size in degrees - h0->readKey("PHYSICALSIZE",map->boxlarcsec); - map->boxlarcsec=map->boxlarcsec*60.*60.; - pixLMpc = map->boxlarcsec/npixels/inarcsec; - map->boxlMpc = pixLMpc*npixels; + h0->readKey("PHYSICALSIZE",map.boxlarcsec); + map.boxlarcsec=map.boxlarcsec*60.*60.; + pixLMpc = map.boxlarcsec/npixels/inarcsec; + map.boxlMpc = pixLMpc*npixels; } catch(CCfits::HDU::NoSuchKeyword) { try{ // physical size in degrees - h0->readKey("PHYSICAL",map->boxlarcsec); - map->boxlarcsec=map->boxlarcsec*60.*60.; - pixLMpc = map->boxlarcsec/npixels/inarcsec; - map->boxlMpc = pixLMpc*npixels; + h0->readKey("PHYSICAL",map.boxlarcsec); + map.boxlarcsec=map.boxlarcsec*60.*60.; + pixLMpc = map.boxlarcsec/npixels/inarcsec; + map.boxlMpc = pixLMpc*npixels; } catch(CCfits::HDU::NoSuchKeyword) { std::cerr << "fits mass map must have header keywords:" << std::endl @@ -334,10 +334,10 @@ void LensHaloMassMap::readMap(){ std::cout << "unable to read map PHYSICALSIZE" << std::endl; std::cout << "assuming is the MultiDark file" << std::endl; - map->boxlarcsec = 8.7*60.*60.; // W1 x-field of view - // map->boxlarcsec = 5.5*60.*60.; // W4 x-field of view - pixLMpc = map->boxlarcsec/npixels/inarcsec; - map->boxlMpc = pixLMpc*npixels; + map.boxlarcsec = 8.7*60.*60.; // W1 x-field of view + // map.boxlarcsec = 5.5*60.*60.; // W4 x-field of view + pixLMpc = map.boxlarcsec/npixels/inarcsec; + map.boxlMpc = pixLMpc*npixels; pixelunit = 1.e+10/cosmo.gethubble()/pixLMpc/pixLMpc; // by hand }*/ @@ -346,37 +346,37 @@ void LensHaloMassMap::readMap(){ // made square // need to be // 1. take the part located in the left side //for(int i=0;iconvergence[i+npixels*j] = mapbut[i+map->nx*j]*pixelunit; - // avkappa += map->convergence[i+npixels*j]; + // map.convergence[i+npixels*j] = mapbut[i+map.nx*j]*pixelunit; + // avkappa += map.convergence[i+npixels*j]; // } // 2. take the part located in the right side // for(int i=0;iconvergence[i+npixels*j] = mapbut[(map->nx-npixels+i)+map->nx*j]*pixelunit; - // avkappa += map->convergence[i+npixels*j]; + // map.convergence[i+npixels*j] = mapbut[(map.nx-npixels+i)+map.nx*j]*pixelunit; + // avkappa += map.convergence[i+npixels*j]; // } // avkappa /= (npixels*npixels); - for(int i=0;inx;i++) for(int j=0;jny;j++){ - map->convergence[i+map->nx*j] *= pixelunit; + for(int i=0;inx;i++) for(int j=0;jny;j++){ - avkappa += map->convergence[i+map->nx*j]; + for(int i=0;inx*map->ny); + avkappa /= (map.nx*map.ny); - for(int i=0;inx;i++) for(int j=0;jny;j++){ - map->convergence[i+map->nx*j] = (map->convergence[i+map->nx*j] - avkappa); + for(int i=0;inx = map->ny = npixels; + // valid only to force the map to be square map.nx = map.ny = npixels; #ifdef ENABLE_FFTW //std:: cout << " preProcessing Map " << std:: endl; // reducing the size of the maps @@ -391,7 +391,7 @@ void LensHaloMassMap::readMap(){ for(int i=0;iconvergence,npixels,xi,yi,xh,xh); + double ki = getMapVal(map.convergence,npixels,xi,yi,xh,xh); kl[i+nl*j] = ki; if(ki!=ki){ std:: cout << xi << " " << yi << " " << ki << std:: endl; @@ -399,11 +399,11 @@ void LensHaloMassMap::readMap(){ } } std:: cout << " done " << std:: endl; - map->nx = map->ny = nl; - map->convergence.resize(nl*nl); + map.nx = map.ny = nl; + map.convergence.resize(nl*nl); std:: cout << " remapping " << std:: endl; for(int i=0;iconvergence[i+nl*j] = kl[i+nl*j]; + map.convergence[i+nl*j] = kl[i+nl*j]; } */ // carlo test end @@ -414,10 +414,10 @@ void LensHaloMassMap::readMap(){ #endif } - // std:: cout << map->boxlMpc << " " << map->boxlarcsec << std:: endl; + // std:: cout << map.boxlMpc << " " << map.boxlarcsec << std:: endl; - for(size_t i=0;iconvergence.size();++i){ - assert(map->convergence[i] == map->convergence[i]); + for(size_t i=0;inx,map->ny}; + long naxes[2]={map.nx,map.ny}; std::auto_ptr fout(0); @@ -447,34 +447,34 @@ void LensHaloMassMap::writeImage(std::string filename){ } std::vector naxex(2); - naxex[0]=map->nx; - naxex[1]=map->ny; + naxex[0]=map.nx; + naxex[1]=map.ny; PHDU *phout=&fout->pHDU(); - phout->write( 1,map->nx*map->ny,map->convergence ); + phout->write( 1,map.nx*map.ny,map.convergence ); - phout->addKey ("SIDEL",map->boxlarcsec,"arcsec"); - phout->addKey ("SIDEL2",map->boxlMpc,"Mpc/h"); - phout->addKey ("ZLENS",map->zlens,"lens redshift"); - phout->addKey ("ZSOURCE",map->zsource, "source redshift"); - phout->addKey ("OMEGA",map->omegam,"omega matter"); - phout->addKey ("LAMBDA",map->omegal,"omega lamda"); - phout->addKey ("H",map->h,"hubble/100"); - phout->addKey ("W",map->wq,"dark energy equation of state parameter"); - phout->addKey ("MSTAR",map->mstar,"stellar mass of the BCG in Msun/h"); - phout->addKey ("MVIR",map->m,"virial mass of the halo in Msun/h"); - phout->addKey ("CONCENTRATION",map->c,"NFW concentration"); - phout->addKey ("DL",map->Dlens,"Mpc/h"); - phout->addKey ("DLS",map->DLS,"Mpc/h"); - phout->addKey ("DS",map->DS,"Mpc/h"); + phout->addKey ("SIDEL",map.boxlarcsec,"arcsec"); + phout->addKey ("SIDEL2",map.boxlMpc,"Mpc/h"); + phout->addKey ("ZLENS",map.zlens,"lens redshift"); + phout->addKey ("ZSOURCE",map.zsource, "source redshift"); + phout->addKey ("OMEGA",map.omegam,"omega matter"); + phout->addKey ("LAMBDA",map.omegal,"omega lamda"); + phout->addKey ("H",map.h,"hubble/100"); + phout->addKey ("W",map.wq,"dark energy equation of state parameter"); + phout->addKey ("MSTAR",map.mstar,"stellar mass of the BCG in Msun/h"); + phout->addKey ("MVIR",map.m,"virial mass of the halo in Msun/h"); + phout->addKey ("CONCENTRATION",map.c,"NFW concentration"); + phout->addKey ("DL",map.Dlens,"Mpc/h"); + phout->addKey ("DLS",map.DLS,"Mpc/h"); + phout->addKey ("DS",map.DS,"Mpc/h"); ExtHDU *eh1=fout->addImage("gamma1", FLOAT_IMG, naxex); - eh1->write(1,map->nx*map->ny,map->gamma1); + eh1->write(1,map.nx*map.ny,map.gamma1); ExtHDU *eh2=fout->addImage("gamma2", FLOAT_IMG, naxex); - eh2->write(1,map->nx*map->ny,map->gamma2); + eh2->write(1,map.nx*map.ny,map.gamma2); ExtHDU *eh3=fout->addImage("gamma3", FLOAT_IMG, naxex); - eh3->write(1,map->nx*map->ny,map->gamma3); + eh3->write(1,map.nx*map.ny,map.gamma3); std::cout << *phout << std::endl; #else @@ -609,10 +609,10 @@ void LensHaloMassMap::PreProcessFFTWMap(){ //int npix_filter = 0; // filter the map if you want on a given number of pixels: CHECK IT NOT IMPLEMENTED YET // size of the new map in x and y directions, factor by which each size is increased - int Nnx=int(zerosize*map->nx); - int Nny=int(zerosize*map->ny); - double Nboxlx = map->boxlMpc*zerosize; - double Nboxly = map->boxlMpc*zerosize/map->nx*map->ny; + int Nnx=int(zerosize*map.nx); + int Nny=int(zerosize*map.ny); + double Nboxlx = map.boxlMpc*zerosize; + double Nboxly = map.boxlMpc*zerosize/map.nx*map.ny; std:: valarray Nmap; try{ @@ -624,11 +624,11 @@ void LensHaloMassMap::PreProcessFFTWMap(){ for( int j=0; j=int(Nnx/2-map->nx/2) && inx/2) && j>=int(Nny/2-map->ny/2) && jny/2)){ - int ii = i-int(Nnx/2-map->nx/2); - int jj = j-int(Nny/2-map->ny/2); + if(i>=int(Nnx/2-map.nx/2) && i=int(Nny/2-map.ny/2) && j=map->nx || jj>=map->ny){ + if(ii>=map.nx || jj>=map.ny){ std::cout << " 1 error mapping " << ii << " " << jj << std::endl; exit(1); } @@ -636,7 +636,7 @@ void LensHaloMassMap::PreProcessFFTWMap(){ std::cout << " 2 error mapping " << ii << " " << jj << std::endl; exit(1); } - Nmap[i+Nnx*j]=map->convergence[ii+map->nx*jj]; + Nmap[i+Nnx*j]=map.convergence[ii+map.nx*jj]; } } } @@ -718,14 +718,14 @@ void LensHaloMassMap::PreProcessFFTWMap(){ fftw_execute( pp ); //fftw_destroy_plan(pp); - map->alpha1.resize(map->nx*map->ny); + map.alpha1.resize(map.nx*map.ny); - for( int j=Nny/2-map->ny/2; jny/2; j++ ){ - for( int i=Nnx/2-map->nx/2; inx/2; i++ ){ - int ii = i-int(Nnx/2-map->nx/2); - int jj = j-int(Nny/2-map->ny/2); + for( int j=Nny/2-map.ny/2; jalpha1[ii+map->nx*jj] = -1*float(realsp[i+Nnx*j]/Nnx/Nny); + map.alpha1[ii+map.nx*jj] = -1*float(realsp[i+Nnx*j]/Nnx/Nny); } } } @@ -752,14 +752,14 @@ void LensHaloMassMap::PreProcessFFTWMap(){ fftw_execute( pp ); //fftw_destroy_plan(pp); - map->alpha2.resize(map->nx*map->ny); + map.alpha2.resize(map.nx*map.ny); - for( int j=Nny/2-map->ny/2; jny/2; j++ ){ - for( int i=Nnx/2-map->nx/2; inx/2; i++ ){ - int ii = i-int(Nnx/2-map->nx/2); - int jj = j-int(Nny/2-map->ny/2); + for( int j=Nny/2-map.ny/2; jalpha2[ii+map->nx*jj] = -1*float(realsp[i+Nnx*j]/Nnx/Nny); + map.alpha2[ii+map.nx*jj] = -1*float(realsp[i+Nnx*j]/Nnx/Nny); } } } @@ -785,14 +785,14 @@ void LensHaloMassMap::PreProcessFFTWMap(){ fftw_execute( pp ); //fftw_destroy_plan(pp); - map->gamma1.resize(map->nx*map->ny); + map.gamma1.resize(map.nx*map.ny); - for( int j=Nny/2-map->ny/2; jny/2; j++ ){ - for( int i=Nnx/2-map->nx/2; inx/2; i++ ){ - int ii = i-int(Nnx/2-map->nx/2); - int jj = j-int(Nny/2-map->ny/2); + for( int j=Nny/2-map.ny/2; jgamma1[ii+map->nx*jj] = float( realsp[i+Nnx*j]/Nnx/Nny); + map.gamma1[ii+map.nx*jj] = float( realsp[i+Nnx*j]/Nnx/Nny); } } @@ -819,14 +819,14 @@ void LensHaloMassMap::PreProcessFFTWMap(){ fftw_execute( pp ); //fftw_destroy_plan(pp); - map->gamma2.resize(map->nx*map->ny); + map.gamma2.resize(map.nx*map.ny); - for( int j=Nny/2-map->ny/2; jny/2; j++ ){ - for( int i=Nnx/2-map->nx/2; inx/2; i++ ){ - int ii = i-int(Nnx/2-map->nx/2); - int jj = j-int(Nny/2-map->ny/2); + for( int j=Nny/2-map.ny/2; jgamma2[ii+map->nx*jj] = float(-realsp[i+Nnx*j]/Nnx/Nny); + map.gamma2[ii+map.nx*jj] = float(-realsp[i+Nnx*j]/Nnx/Nny); } } diff --git a/MultiPlane/MOKAlens.cpp b/MultiPlane/MOKAlens.cpp index a9bc5fc1..7902d3c4 100644 --- a/MultiPlane/MOKAlens.cpp +++ b/MultiPlane/MOKAlens.cpp @@ -71,7 +71,7 @@ maptype(my_maptype), cosmo(lenscosmo),zerosize(pixel_map_zeropad),zeromean(my_ze initMap(); // set redshift to value from map - setZlens(map->zlens); + setZlens(map.zlens); } /** \brief Create a LensHalo from a PixelMap representing the mass. @@ -97,10 +97,10 @@ LensHaloMassMap::LensHaloMassMap( LensHalo::setTheta(MassMap.getCenter()[0],MassMap.getCenter()[1]); - setZlensDist(map->zlens,cosmo); + setZlensDist(map.zlens,cosmo); //setZlens(redshift); // set redshift to value from map - //setZlens(map->zlens); + //setZlens(map.zlens); } /* @@ -125,46 +125,46 @@ LensHalo(),MOKA_input_file(""),maptype(pix_map),cosmo(lenscosmo),zerosize(pixel_ else Rmax = std::numeric_limits::max(); - map->nx = my_map.getNx(); - map->ny = my_map.getNy(); + map.nx = my_map.getNx(); + map.ny = my_map.getNy(); std::cout << "nx ny " << std::endl; - std::cout << map->nx << " " << map->ny << std::endl; + std::cout << map.nx << " " << map.ny << std::endl; - std::size_t size = map->nx*map->ny; + std::size_t size = map.nx*map.ny; - map->convergence.resize(size); - map->alpha1.resize(size,0.0); - map->alpha2.resize(size,0.0); - map->gamma1.resize(size,0.0); - map->gamma2.resize(size,0.0); - map->gamma3.resize(size,0.0); - map->phi.resize(size,0.0); + map.convergence.resize(size); + map.alpha1.resize(size,0.0); + map.alpha2.resize(size,0.0); + map.gamma1.resize(size,0.0); + map.gamma2.resize(size,0.0); + map.gamma3.resize(size,0.0); + map.phi.resize(size,0.0); - map->boxlarcsec = my_map.getRangeX()/arcsecTOradians; - map->boxlrad = my_map.getRangeX(); - map->Dlens = Dist; - map->boxlMpc = map->boxlrad/Dist; // physical + map.boxlarcsec = my_map.getRangeX()/arcsecTOradians; + map.boxlrad = my_map.getRangeX(); + map.Dlens = Dist; + map.boxlMpc = map.boxlrad/Dist; // physical // convertion to solar masses per Mpc^2 - double convert = massconvertion/(my_map.getResolution()*my_map.getResolution()*map->Dlens*map->Dlens); + double convert = massconvertion/(my_map.getResolution()*my_map.getResolution()*map.Dlens*map.Dlens); - size_t Npixels = map->nx*map->ny; + size_t Npixels = map.nx*map.ny; LensHalo::mass = 0.0; for(size_t i=0 ; iconvergence[i] = my_map(i)*convert; + map.convergence[i] = my_map(i)*convert; LensHalo::mass += my_map(i)*massconvertion; } - map->zsource = zsource; - map->zlens = zlens; + map.zsource = zsource; + map.zlens = zlens; - map->center[0] = map->center[1] = 0.0; - map->boxlrad = map->boxlarcsec*PI/180/3600.; + map.center[0] = map.center[1] = 0.0; + map.boxlrad = map.boxlarcsec*PI/180/3600.; PreProcessFFTWMap(); - assert(map->nx*map->ny == map->convergence.size()); + assert(map.nx*map.ny == map.convergence.size()); LensHalo::setTheta(my_map.getCenter()[0],my_map.getCenter()[1]); @@ -187,12 +187,11 @@ LensHaloMassMap::LensHaloMassMap(InputParams& params, const COSMOLOGY& lenscosmo // set redshift if necessary if(LensHalo::getZlens() == -1) - setZlens(map->zlens); + setZlens(map.zlens); } LensHaloMassMap::~LensHaloMassMap() { - delete map; } void LensHaloMassMap::initMap() @@ -208,7 +207,7 @@ void LensHaloMassMap::initMap() exit(1); #endif - map = new MOKAmap(); + //map = new MOKAmap(); if(std::numeric_limits::has_infinity) Rmax = std::numeric_limits::infinity(); @@ -217,49 +216,49 @@ void LensHaloMassMap::initMap() getDims(); - std::size_t size = map->nx*map->ny; + std::size_t size = map.nx*map.ny; - map->convergence.resize(size); - map->alpha1.resize(size); - map->alpha2.resize(size); - map->gamma1.resize(size); - map->gamma2.resize(size); - map->gamma3.resize(size); - map->phi.resize(size); + map.convergence.resize(size); + map.alpha1.resize(size); + map.alpha2.resize(size); + map.gamma1.resize(size); + map.gamma2.resize(size); + map.gamma3.resize(size); + map.phi.resize(size); readMap(); if(flag_background_field == 1) { - map->convergence = 0; - map->alpha1 = 0; - map->alpha2 = 0; - map->gamma1 = 0; - map->gamma2 = 0; - map->phi = 0; + map.convergence = 0; + map.alpha1 = 0; + map.alpha2 = 0; + map.gamma1 = 0; + map.gamma2 = 0; + map.phi = 0; } - map->center[0] = map->center[1] = 0.0; - map->boxlrad = map->boxlarcsec*PI/180/3600.; + map.center[0] = map.center[1] = 0.0; + map.boxlrad = map.boxlarcsec*PI/180/3600.; if(maptype == moka){ - Utilities::fill_linear(map->x, map->nx, -0.5*map->boxlMpc, 0.5*map->boxlMpc); // physical Mpc/h + Utilities::fill_linear(map.x, map.nx, -0.5*map.boxlMpc, 0.5*map.boxlMpc); // physical Mpc/h /// converts to the code units std::cout << "converting the units of the MOKA map" << std::endl; - double fac = map->DS/map->DLS/map->Dlens*map->h/(4*PI*Grav); + double fac = map.DS/map.DLS/map.Dlens*map.h/(4*PI*Grav); - map->convergence *= fac; - map->gamma1 *= fac; - map->gamma2 *= fac; - map->phi *= fac; + map.convergence *= fac; + map.gamma1 *= fac; + map.gamma2 *= fac; + map.phi *= fac; fac = 1/(4*PI*Grav); - map->alpha1 *= fac; - map->alpha2 *= fac; + map.alpha1 *= fac; + map.alpha2 *= fac; checkCosmology(); }else{ @@ -269,26 +268,26 @@ void LensHaloMassMap::initMap() } } -void LensHaloMassMap::convertmap(MOKAmap *map,PixelMapType maptype){ +//void LensHaloMassMap::convertmap(MOKAmap *map,PixelMapType maptype){ // TODO: convert units - throw std::runtime_error("needs to be finished"); +// throw std::runtime_error("needs to be finished"); - map->center[0] *= map->Dlens;//(1+map->zlens); - map->center[1] *= map->Dlens;//(1+map->zlens); +// map.center[0] *= map.Dlens;//(1+map.zlens); +// map.center[1] *= map.Dlens;//(1+map.zlens); - //float pixLMpc = map->boxlMpc/map->nx; // TODO: What if it isn't square? + //float pixLMpc = map.boxlMpc/map.nx; // TODO: What if it isn't square? //float fac = 1.e+10/pixLMpc/pixLMpc/cosmo->gethubble(); - //map->convergence *= fac; - //map->gamma1 *= fac; - //map->gamma2 *= fac; + //map.convergence *= fac; + //map.gamma1 *= fac; + //map.gamma2 *= fac; // TODO: Need to check this - //map->alpha1 *= fac*pixLMpc; - //map->alpha2 *= fac*pixLMpc; + //map.alpha1 *= fac*pixLMpc; + //map.alpha2 *= fac*pixLMpc; -} +//} /** * \brief reads in the fits file for the MOKA or mass map and saves it in the structure map @@ -302,61 +301,60 @@ void LensHaloMassMap::setMap( // must be a square map assert(inputmap.getNx() == inputmap.getNy()); - map = new MOKAmap(); + //map = new MOKAmap(); if(std::numeric_limits::has_infinity) Rmax = std::numeric_limits::infinity(); else Rmax = std::numeric_limits::max(); - map->nx = map->ny = inputmap.getNx(); - map->center[0] = map->center[1] = 0.0; + map.nx = map.ny = inputmap.getNx(); - std::size_t size = map->nx*map->ny; + std::size_t size = map.nx*map.ny; - map->convergence.resize(size); - map->alpha1.resize(size); - map->alpha2.resize(size); - map->gamma1.resize(size); - map->gamma2.resize(size); - map->gamma3.resize(size); - map->phi.resize(size); + map.convergence.resize(size); + map.alpha1.resize(size); + map.alpha2.resize(size); + map.gamma1.resize(size); + map.gamma2.resize(size); + map.gamma3.resize(size); + map.phi.resize(size); - map->zlens = z; + map.zlens = z; - assert(map->nx !=0); + assert(map.nx !=0); // keep it like it is, even if is a rectangle - map->Dlens = cosmo.angDist(0.,map->zlens); // physical - map->boxlrad = inputmap.getRangeX(); - map->boxlarcsec = inputmap.getRangeX()/arcsecTOradians; - map->boxlMpc = inputmap.getRangeX()/map->Dlens; + map.Dlens = cosmo.angDist(0.,map.zlens); // physical + map.boxlrad = inputmap.getRangeX(); + map.boxlarcsec = inputmap.getRangeX()/arcsecTOradians; + map.boxlMpc = inputmap.getRangeX()/map.Dlens; - double pixelarea = inputmap.getResolution()*map->Dlens; + double pixelarea = inputmap.getResolution()*map.Dlens; pixelarea *= pixelarea; for(size_t i=0;iconvergence[i] = massconvertion*inputmap(i)/pixelarea; + map.convergence[i] = massconvertion*inputmap(i)/pixelarea; } if(zeromean){ double avkappa = 0; for(size_t i=0;iconvergence[i]; + avkappa += map.convergence[i]; } avkappa /= size; for(size_t i=0;iconvergence[i] -= avkappa; + map.convergence[i] -= avkappa; } } // kappa is not divided by the critical surface density // they don't need to be preprocessed by fact // create alpha and gamma arrays by FFT - // valid only to force the map to be square map->nx = map->ny = npixels; + // valid only to force the map to be square map.nx = map.ny = npixels; #ifdef ENABLE_FFTW //std:: cout << " preProcessing Map " << std:: endl; PreProcessFFTWMap(); @@ -372,12 +370,12 @@ void LensHaloMassMap::setMap( /// checks that cosmology in the header of the input fits map is the same as the one set void LensHaloMassMap::checkCosmology() { - if(cosmo.getOmega_matter() == map->omegam) - std::cerr << "LensHaloMassMap: Omega_matter " << cosmo.getOmega_matter() << " (cosmology) != " << map->omegam << " (MOKA)" << std::endl; - if(cosmo.getOmega_lambda() == map->omegal) - std::cerr << "LensHaloMassMap: Omega_lambda " << cosmo.getOmega_lambda() << " (cosmology) != " << map->omegal << " (MOKA)" << std::endl; - if(cosmo.gethubble() == map->h) - std::cerr << "LensHaloMassMap: hubble " << cosmo.gethubble() << " (cosmology) != " << map->h << " (MOKA)" << std::endl; + if(cosmo.getOmega_matter() == map.omegam) + std::cerr << "LensHaloMassMap: Omega_matter " << cosmo.getOmega_matter() << " (cosmology) != " << map.omegam << " (MOKA)" << std::endl; + if(cosmo.getOmega_lambda() == map.omegal) + std::cerr << "LensHaloMassMap: Omega_lambda " << cosmo.getOmega_lambda() << " (cosmology) != " << map.omegal << " (MOKA)" << std::endl; + if(cosmo.gethubble() == map.h) + std::cerr << "LensHaloMassMap: hubble " << cosmo.gethubble() << " (cosmology) != " << map.h << " (MOKA)" << std::endl; } /** @@ -429,12 +427,12 @@ void LensHaloMassMap::saveImage(GridHndl grid,bool saveprofiles){ PointList::iterator i_tree_pointlist_current(grid->i_tree->pointlist->Top()); do{ - long index = Utilities::IndexFromPosition((*i_tree_pointlist_current)->x,map->nx,map->boxlrad,map->center); + long index = Utilities::IndexFromPosition((*i_tree_pointlist_current)->x,map.nx,map.boxlrad,map.center.x); if(index > -1){ - map->convergence[index] = (*i_tree_pointlist_current)->kappa; - map->gamma1[index] = (*i_tree_pointlist_current)->gamma[0]; - map->gamma2[index] = (*i_tree_pointlist_current)->gamma[1]; - map->gamma3[index] = (*i_tree_pointlist_current)->gamma[2]; + map.convergence[index] = (*i_tree_pointlist_current)->kappa; + map.gamma1[index] = (*i_tree_pointlist_current)->gamma[0]; + map.gamma2[index] = (*i_tree_pointlist_current)->gamma[1]; + map.gamma3[index] = (*i_tree_pointlist_current)->gamma[2]; } }while( (--i_tree_pointlist_current)==true ); @@ -469,22 +467,22 @@ void LensHaloMassMap::saveImage(GridHndl grid,bool saveprofiles){ * */ void LensHaloMassMap::saveProfiles(double &RE3,double &xxc,double &yyc){ /* measuring the differential and cumulative profile*/ - double xmin = -map->boxlMpc*0.5; - double xmax = map->boxlMpc*0.5; - double drpix = map->boxlMpc/map->nx; + double xmin = -map.boxlMpc*0.5; + double xmax = map.boxlMpc*0.5; + double drpix = map.boxlMpc/map.nx; int galaxiesPerBin = 64; - std::valarray pxdist(map->nx*map->ny); - std::valarray red_sgE(map->nx*map->ny),red_sgB(map->nx*map->ny),sgm(map->nx*map->ny); + std::valarray pxdist(map.nx*map.ny); + std::valarray red_sgE(map.nx*map.ny),red_sgB(map.nx*map.ny),sgm(map.nx*map.ny); int i, j; /* measure the center of mass double xcm=0,ycm=0,tot=0; - for(i=0; inx; i++ ) for(j=0; jny; j++ ){ - xcm+=map->convergence[i+map->ny*j]*(xmin+(drpix*0.5)+i*drpix); - ycm+=map->convergence[i+map->ny*j]*(xmin+(drpix*0.5)+j*drpix); - tot+=map->convergence[i+map->ny*j]; + for(i=0; iny,map->convergence,map->x,xcm,ycm); + cmass(map.ny,map.convergence,map.x,xcm,ycm); } xxc = xcm; yyc = ycm; - int ai = Utilities::locate(map->x,xxc); + int ai = Utilities::locate(map.x,xxc); ai = ((ai > 0) ? ai:0); - ai = ((ai < map->nx-1) ? ai:map->nx-1); - int bi = Utilities::locate(map->x,yyc); + ai = ((ai < map.nx-1) ? ai:map.nx-1); + int bi = Utilities::locate(map.x,yyc); bi = ((bi > 0) ? bi:0); - bi = ((bi < map->nx-1) ? bi:map->nx-1); + bi = ((bi < map.nx-1) ? bi:map.nx-1); std:: cout << " ------------ center ------------- " << std:: endl; std:: cout << " " << xxc << " " << yyc << std:: endl; std:: cout << " " << ai << " " << bi << std:: endl; std:: cout << " ------------------r ------------- " << std:: endl; - for(i=0; inx; i++ ) for(j=0; jny; j++ ){ - pxdist[i+map->ny*j]= sqrt(pow((xmin+(drpix*0.5)+i*drpix-xcm),2) + + for(i=0; ix[i]; - double dy=map->x[j]; + double dx=map.x[i]; + double dy=map.x[j]; double p=atan2( dy, dx ); // check gamma 1 and gamma 2 definition - red_sgE[i+map->ny*j] = (-map->gamma1[i+map->ny*j]*cos(2*p)-map->gamma2[i+map->ny*j]*sin(2*p))/(1.-map->convergence[i+map->ny*j]); - red_sgB[i+map->ny*j] = (map->gamma1[i+map->ny*j]*sin(2*p)-map->gamma2[i+map->ny*j]*cos(2*p))/(1.-map->convergence[i+map->ny*j]); - sgm[i+map->ny*j] = sqrt(pow(map->gamma1[i+map->ny*j],2) + pow(map->gamma2[i+map->ny*j],2)); + red_sgE[i+map.ny*j] = (-map.gamma1[i+map.ny*j]*cos(2*p)-map.gamma2[i+map.ny*j]*sin(2*p))/(1.-map.convergence[i+map.ny*j]); + red_sgB[i+map.ny*j] = (map.gamma1[i+map.ny*j]*sin(2*p)-map.gamma2[i+map.ny*j]*cos(2*p))/(1.-map.convergence[i+map.ny*j]); + sgm[i+map.ny*j] = sqrt(pow(map.gamma1[i+map.ny*j],2) + pow(map.gamma2[i+map.ny*j],2)); } - double dr0 = 8.*(0.5*map->boxlMpc)/(map->nx/2.); + double dr0 = 8.*(0.5*map.boxlMpc)/(map.nx/2.); int nbin = int(xmax/dr0); int nbggal=0; int ih; @@ -542,7 +540,7 @@ void LensHaloMassMap::saveProfiles(double &RE3,double &xxc,double &yyc){ infilebgXarcmin2.close(); } // number of galaxies per arcminsquare - double dntbggal=double(nbggal)*(map->boxlarcsec*map->boxlarcsec)/3600.; + double dntbggal=double(nbggal)*(map.boxlarcsec*map.boxlarcsec)/3600.; int ntbggal=int(dntbggal+0.5); std:: vector runi,runj; int ibut; @@ -552,11 +550,11 @@ void LensHaloMassMap::saveProfiles(double &RE3,double &xxc,double &yyc){ // fill the vector with random coordinates of background galaxies srand(ih+94108); for(lrun=0;lrunnx; - if(ibut>map->nx || ibut<0) std:: cout << "1. ibut out of npix range = " << ibut << std:: endl; + ibut = rand()%map.nx; + if(ibut>map.nx || ibut<0) std:: cout << "1. ibut out of npix range = " << ibut << std:: endl; runi.push_back(ibut); - ibut = rand()%map->nx; - if(ibut>map->nx || ibut<0) std:: cout << "2. ibut out of npix range = " << ibut << std:: endl; + ibut = rand()%map.nx; + if(ibut>map.nx || ibut<0) std:: cout << "2. ibut out of npix range = " << ibut << std:: endl; runj.push_back(ibut); } if(runi.size()>ntbggal || runj.size()>ntbggal){ @@ -565,7 +563,7 @@ void LensHaloMassMap::saveProfiles(double &RE3,double &xxc,double &yyc){ std:: cout << " runj.size() = " << runj.size() << std:: endl; } - dr0 = map->boxlMpc*sqrt(galaxiesPerBin/ntbggal); + dr0 = map.boxlMpc*sqrt(galaxiesPerBin/ntbggal); nbin = int(xmax/dr0); } @@ -577,16 +575,16 @@ void LensHaloMassMap::saveProfiles(double &RE3,double &xxc,double &yyc){ // - - - - - - - - - - - - - - - - - // TODO: Carlo: These are all memory leaks! They are never deleted! - double *kprofr = estprof(map->convergence,map->nx,map->ny,pxdist,dr0,xmax,runi,runj,ntbggal); - double *sigmakprof = estsigmaprof(map->convergence,map->nx,map->ny,pxdist,dr0,xmax,runi,runj,ntbggal,kprofr); - double *ckprofr = estcprof(map->convergence,map->nx,map->ny,pxdist,dr0,xmax,runi,runj,ntbggal); - double *sigmackprof = estsigmacprof(map->convergence,map->nx,map->ny,pxdist,dr0,xmax,runi,runj,ntbggal,kprofr); - double *gamma1profr = estprof(red_sgE,map->nx,map->ny,pxdist,dr0,xmax,runi,runj,ntbggal); // reduced shear - double *sigmagamma1prof = estsigmaprof(red_sgE,map->nx,map->ny,pxdist,dr0,xmax,runi,runj,ntbggal,gamma1profr); - double *gamma0profr = estprof(red_sgB,map->nx,map->ny,pxdist,dr0,xmax,runi,runj,ntbggal); - double *sigmagamma0prof = estsigmaprof(red_sgB,map->nx,map->ny,pxdist,dr0,xmax,runi,runj,ntbggal,gamma0profr); - double *gamma2profr = estprof(sgm,map->nx,map->ny,pxdist,dr0,xmax,runi,runj,ntbggal); - double *sigmagamma2prof = estsigmaprof(sgm,map->nx,map->ny,pxdist,dr0,xmax,runi,runj,ntbggal,gamma2profr); + double *kprofr = estprof(map.convergence,map.nx,map.ny,pxdist,dr0,xmax,runi,runj,ntbggal); + double *sigmakprof = estsigmaprof(map.convergence,map.nx,map.ny,pxdist,dr0,xmax,runi,runj,ntbggal,kprofr); + double *ckprofr = estcprof(map.convergence,map.nx,map.ny,pxdist,dr0,xmax,runi,runj,ntbggal); + double *sigmackprof = estsigmacprof(map.convergence,map.nx,map.ny,pxdist,dr0,xmax,runi,runj,ntbggal,kprofr); + double *gamma1profr = estprof(red_sgE,map.nx,map.ny,pxdist,dr0,xmax,runi,runj,ntbggal); // reduced shear + double *sigmagamma1prof = estsigmaprof(red_sgE,map.nx,map.ny,pxdist,dr0,xmax,runi,runj,ntbggal,gamma1profr); + double *gamma0profr = estprof(red_sgB,map.nx,map.ny,pxdist,dr0,xmax,runi,runj,ntbggal); + double *sigmagamma0prof = estsigmaprof(red_sgB,map.nx,map.ny,pxdist,dr0,xmax,runi,runj,ntbggal,gamma0profr); + double *gamma2profr = estprof(sgm,map.nx,map.ny,pxdist,dr0,xmax,runi,runj,ntbggal); + double *sigmagamma2prof = estsigmaprof(sgm,map.nx,map.ny,pxdist,dr0,xmax,runi,runj,ntbggal,gamma2profr); std::ostringstream fprof; if(flag_background_field==1) fprof << MOKA_input_file << "_only_noise_MAP_radial_prof.dat"; @@ -608,7 +606,7 @@ void LensHaloMassMap::saveProfiles(double &RE3,double &xxc,double &yyc){ << gamma1profr[l] << " " << sigmagamma1prof[l] << " " << gamma0profr[l] << " " << sigmagamma0prof[l] << " " << gamma2profr[l] << " " << sigmagamma2prof[l] << " " - << (dr0*l + dr0/2.)*map->inarcsec << " " << Aanulus*map->inarcsec*map->inarcsec << " " << + << (dr0*l + dr0/2.)*map.inarcsec << " " << Aanulus*map.inarcsec*map.inarcsec << " " << std:: endl; lprofFORre[l] = log10(kprofr[l] + gamma2profr[l]); lrOFr[l] = log10(dr0*l + dr0/2.); @@ -616,7 +614,7 @@ void LensHaloMassMap::saveProfiles(double &RE3,double &xxc,double &yyc){ filoutprof.close(); RE3 = Utilities::InterpolateYvec(lprofFORre,lrOFr,0.); if((RE3-lrOFr[0])<1e-4 || (RE3-lrOFr[nbin-1])<1e-4) RE3=0; - else RE3 = pow(10.,RE3)*map->inarcsec; + else RE3 = pow(10.,RE3)*map.inarcsec; } /** \ingroup DeflectionL2 @@ -636,15 +634,15 @@ void LensHaloMassMap::force_halo(double *alpha { /* - long index = Utilities::IndexFromPosition(xx,map->nx,map->boxlMpc/map->h,map->center); + long index = Utilities::IndexFromPosition(xx,map.nx,map.boxlMpc/map.h,map.center); if(index > -1){ - alpha[0] = map->alpha1[index]; - alpha[1] = map->alpha2[index]; - gamma[0] = map->gamma1[index]; - gamma[1] = map->gamma2[index]; + alpha[0] = map.alpha1[index]; + alpha[1] = map.alpha2[index]; + gamma[0] = map.gamma1[index]; + gamma[1] = map.gamma2[index]; gamma[2] = 0.0; - *kappa = map->convergence[index]; + *kappa = map.convergence[index]; } else{ alpha[0] = alpha[1] = 0.0; @@ -655,29 +653,29 @@ void LensHaloMassMap::force_halo(double *alpha // interpolate from the maps - Utilities::Interpolator > interp(xx,map->nx,map->boxlMpc,map->ny - ,map->ny*map->boxlMpc/map->nx,map->center); + Utilities::Interpolator > interp(xx,map.nx,map.boxlMpc,map.ny + ,map.ny*map.boxlMpc/map.nx,map.center.x); - assert(map->nx == map->ny); + assert(map.nx == map.ny); - //size_t N = map->nx * map->ny; + //size_t N = map.nx * map.ny; - map->convergence.size(); + map.convergence.size(); - /*assert(map->alpha1.size() == N); - assert(map->alpha2.size() == N); - assert(map->gamma1.size() == N); - assert(map->gamma2.size() == N); - assert(map->convergence.size() == N); - assert(map->phi.size() == N); + /*assert(map.alpha1.size() == N); + assert(map.alpha2.size() == N); + assert(map.gamma1.size() == N); + assert(map.gamma2.size() == N); + assert(map.convergence.size() == N); + assert(map.phi.size() == N); */ - alpha[0] = interp.interpolate(map->alpha1); - alpha[1] = interp.interpolate(map->alpha2); - gamma[0] = interp.interpolate(map->gamma1); - gamma[1] = interp.interpolate(map->gamma2); + alpha[0] = interp.interpolate(map.alpha1); + alpha[1] = interp.interpolate(map.alpha2); + gamma[0] = interp.interpolate(map.gamma1); + gamma[1] = interp.interpolate(map.gamma2); gamma[2] = 0.0; - *kappa = interp.interpolate(map->convergence); - *phi = interp.interpolate(map->phi); + *kappa = interp.interpolate(map.convergence); + *phi = interp.interpolate(map.phi); //assert(alpha[0] == alpha[0] && alpha[1] == alpha[1]); @@ -688,22 +686,22 @@ void LensHaloMassMap::force_halo(double *alpha * compute the signal of \lambda_r and \lambda_t */ void LensHaloMassMap::estSignLambdas(){ - map->Signlambdar.resize(map->nx*map->ny); - map->Signlambdat.resize(map->nx*map->ny); + map.Signlambdar.resize(map.nx*map.ny); + map.Signlambdat.resize(map.nx*map.ny); double gamma,lambdar,lambdat; int i, j; - for(i=0;inx;i++) - for(j=0;jny;j++){ - gamma = sqrt(pow(map->gamma1[i+map->ny*j],2) + - pow(map->gamma2[i+map->ny*j],2)); - lambdat=1-map->convergence[i+map->ny*j]-gamma; - lambdar=1-map->convergence[i+map->ny*j]+gamma; + for(i=0;i=0) map->Signlambdar[i+map->ny*j]=1; - else map->Signlambdar[i+map->ny*j]=-1; + if(lambdar>=0) map.Signlambdar[i+map.ny*j]=1; + else map.Signlambdar[i+map.ny*j]=-1; - if(lambdat>=0) map->Signlambdat[i+map->ny*j]=1; - else map->Signlambdat[i+map->ny*j]=-1; + if(lambdat>=0) map.Signlambdat[i+map.ny*j]=1; + else map.Signlambdat[i+map.ny*j]=-1; } } @@ -727,25 +725,25 @@ void LensHaloMassMap::EinsteinRadii(double &RE1, double &RE2, double &xxc, doubl filoutcrit.open(filenamecrit.c_str()); // define the critical points in the map int i, j; - for(i=1;inx-1;i++) - for(j=1;jny-1;j++){ - signV=map->Signlambdar[i-1+map->ny*j]+map->Signlambdar[i+map->ny*(j-1)]+ - map->Signlambdar[i+1+map->ny*j]+map->Signlambdar[i+map->ny*(j+1)]; + for(i=1;ix[i]); - // yci1.push_back(map->x[j]); + // xci1.push_back(map.x[i]); + // yci1.push_back(map.x[j]); filoutcrit << "circle(" << i << "," << j << ",0.5)" << std:: endl; } - signV=map->Signlambdat[i-1+map->ny*j]+map->Signlambdat[i+map->ny*(j-1)]+ - map->Signlambdat[i+1+map->ny*j]+map->Signlambdat[i+map->ny*(j+1)]; + signV=map.Signlambdat[i-1+map.ny*j]+map.Signlambdat[i+map.ny*(j-1)]+ + map.Signlambdat[i+1+map.ny*j]+map.Signlambdat[i+map.ny*(j+1)]; if(fabs(signV)<4.){ - xci2.push_back(map->x[i]); - yci2.push_back(map->x[j]); + xci2.push_back(map.x[i]); + yci2.push_back(map.x[j]); filoutcrit << "circle(" << i << "," << j << ",0.5)" << std:: endl; } } filoutcrit.close(); - double pixDinL = map->boxlMpc/double(map->nx); + double pixDinL = map.boxlMpc/double(map.nx); /* measure the Einstein radius */ std:: vector xci,yci; //for(int ii=0;iiinarcsec; + double distcentre=sqrt((xercm-xxc)*(xercm-xxc)+(yercm-yyc)*(yercm-yyc))*map.inarcsec; std:: vector::iterator maxit, minit; // find the min and max elements in the vector maxit = max_element(xcpoints.begin(), xcpoints.end()); @@ -786,20 +784,20 @@ void LensHaloMassMap::EinsteinRadii(double &RE1, double &RE2, double &xxc, doubl double xmincpoints,xmaxcpoints; xmaxcpoints = *maxit; xmincpoints = *minit; - int imin = Utilities::locate(map->x,xmincpoints); + int imin = Utilities::locate(map.x,xmincpoints); imin = ((imin > 0) ? imin:0); - imin = ((imin < map->nx-1) ? imin:map->nx-1); - // imin=GSL_MIN( GSL_MAX( imin, 0 ), map->nx-1 ); - int imax = Utilities::locate(map->x,xmaxcpoints); + imin = ((imin < map.nx-1) ? imin:map.nx-1); + // imin=GSL_MIN( GSL_MAX( imin, 0 ), map.nx-1 ); + int imax = Utilities::locate(map.x,xmaxcpoints); imax = ((imax > 0) ? imax:0); - imax = ((imax < map->nx-1) ? imax:map->nx-1); - // imax=GSL_MIN( GSL_MAX( imax, 0 ), map->nx-1 ); + imax = ((imax < map.nx-1) ? imax:map.nx-1); + // imax=GSL_MIN( GSL_MAX( imax, 0 ), map.nx-1 ); std:: vector ysup,yinf,xsup,xinf; for(int ii=imin;ii<=imax;ii++){ std:: vectorybut; int condition=0; for(int ji=0;jix[ii]) ycounts; - for(int ji=0;jinx;ji++){ - if(map->x[ji]>=yinf[ii] && map->x[ji]<=ysup[ii]){ - ycounts.push_back(map->x[ji]); + for(int ji=0;ji=yinf[ii] && map.x[ji]<=ysup[ii]){ + ycounts.push_back(map.x[ji]); } } int ncounts = ycounts.size(); @@ -844,8 +842,8 @@ void LensHaloMassMap::EinsteinRadii(double &RE1, double &RE2, double &xxc, doubl for(int ii=nc-1;ii>=0;ii--){ RE.push_back(sqrt(pow(xsup[ii]-xercm,2) + pow(ysup[ii]-yercm,2))); } - RE1=map->inarcsec*sqrt(pixDinL*pixDinL*npixIN/M_PI); - RE2=map->inarcsec*Utilities::median(RE); + RE1=map.inarcsec*sqrt(pixDinL*pixDinL*npixIN/M_PI); + RE2=map.inarcsec*Utilities::median(RE); // if is not in the centre std:: cout << "distance " << distcentre << std:: endl; if(distcentre>1.5*RE2){ diff --git a/MultiPlane/lens_halos.cpp b/MultiPlane/lens_halos.cpp index 58678519..44d98c37 100644 --- a/MultiPlane/lens_halos.cpp +++ b/MultiPlane/lens_halos.cpp @@ -46,6 +46,180 @@ LensHalo::LensHalo(InputParams& params,bool needRsize){ elliptical_flag = false; } +LensHalo::LensHalo(const LensHalo &h){ + + idnumber = h.idnumber; /// Identification number of halo. It is not always used. + Dist = h.Dist; + posHalo[0] = h.posHalo[0]; posHalo[1] = h.posHalo[1]; + zlens = h. zlens; + mass = h.mass; + Rsize = h.Rsize; + mnorm = h.mnorm; + Rmax = h.Rmax; + + stars_index = stars_index; + stars_xp = h.stars_xp; + + stars_N = h.stars_N; + star_theta_force = h.star_theta_force; + + if(stars_N > 1){ + star_tree = new TreeQuadParticles(stars_xp.data(),stars_N + ,false,false,0,4,star_theta_force); + }else{ + star_tree = nullptr; + } + star_massscale = h.star_massscale; + star_fstars = h.star_fstars; + + star_Nregions = h.star_Nregions; + star_region = star_region; + + beta = h.beta; + + Rmax_to_Rsize_ratio = h.Rmax_to_Rsize_ratio; + rscale = h.rscale; + + stars_implanted = h.stars_implanted; + main_stars_imf_type = h.main_stars_imf_type; + main_stars_min_mass = h. main_stars_min_mass; + main_stars_max_mass = h.main_stars_max_mass; + main_ellip_method = h.main_ellip_method; + bend_mstar =h.bend_mstar; + lo_mass_slope = h.lo_mass_slope; + hi_mass_slope = h.hi_mass_slope; + + star_Sigma = h.star_Sigma; + star_xdisk = h.star_xdisk; + + xmax = h.xmax; + mass_norm_factor = h.mass_norm_factor; + pa = h.pa; + fratio = h.fratio; + elliptical_flag = h.elliptical_flag; + switch_flag = h.switch_flag; + //Nmod = h.Nmod; +}; + +LensHalo & LensHalo::operator=(LensHalo &&h){ + + if (this != &h){ + + idnumber = h.idnumber; /// Identification number of halo. It is not always used. + Dist = h.Dist; + posHalo[0] = h.posHalo[0]; posHalo[1] = h.posHalo[1]; + zlens = h. zlens; + mass = h.mass; + Rsize = h.Rsize; + mnorm = h.mnorm; + Rmax = h.Rmax; + + stars_index = stars_index; + stars_xp = h.stars_xp; + + stars_N = h.stars_N; + star_theta_force = h.star_theta_force; + + delete star_tree; + star_tree = h.star_tree; + h.star_tree = nullptr; + + star_massscale = h.star_massscale; + star_fstars = h.star_fstars; + + star_Nregions = h.star_Nregions; + star_region = star_region; + + beta = h.beta; + + Rmax_to_Rsize_ratio = h.Rmax_to_Rsize_ratio; + rscale = h.rscale; + + stars_implanted = h.stars_implanted; + main_stars_imf_type = h.main_stars_imf_type; + main_stars_min_mass = h. main_stars_min_mass; + main_stars_max_mass = h.main_stars_max_mass; + main_ellip_method = h.main_ellip_method; + bend_mstar =h.bend_mstar; + lo_mass_slope = h.lo_mass_slope; + hi_mass_slope = h.hi_mass_slope; + + star_Sigma = h.star_Sigma; + star_xdisk = h.star_xdisk; + + xmax = h.xmax; + mass_norm_factor = h.mass_norm_factor; + pa = h.pa; + fratio = h.fratio; + elliptical_flag = h.elliptical_flag; + switch_flag = h.switch_flag; + //Nmod = h.Nmod; + + } + return *this; +}; + + +LensHalo & LensHalo::operator=(const LensHalo &h){ + + if(this == &h) return *this; + + idnumber = h.idnumber; /// Identification number of halo. It is not always used. + Dist = h.Dist; + posHalo[0] = h.posHalo[0]; posHalo[1] = h.posHalo[1]; + zlens = h. zlens; + mass = h.mass; + Rsize = h.Rsize; + mnorm = h.mnorm; + Rmax = h.Rmax; + + stars_index = stars_index; + stars_xp = h.stars_xp; + + stars_N = h.stars_N; + star_theta_force = h.star_theta_force; + + if(stars_N > 1){ + star_tree = new TreeQuadParticles(stars_xp.data(),stars_N + ,false,false,0,4,star_theta_force); + }else{ + star_tree = nullptr; + } + star_massscale = h.star_massscale; + star_fstars = h.star_fstars; + + star_Nregions = h.star_Nregions; + star_region = star_region; + + beta = h.beta; + + Rmax_to_Rsize_ratio = h.Rmax_to_Rsize_ratio; + rscale = h.rscale; + + stars_implanted = h.stars_implanted; + main_stars_imf_type = h.main_stars_imf_type; + main_stars_min_mass = h. main_stars_min_mass; + main_stars_max_mass = h.main_stars_max_mass; + main_ellip_method = h.main_ellip_method; + bend_mstar =h.bend_mstar; + lo_mass_slope = h.lo_mass_slope; + hi_mass_slope = h.hi_mass_slope; + + star_Sigma = h.star_Sigma; + star_xdisk = h.star_xdisk; + + xmax = h.xmax; + mass_norm_factor = h.mass_norm_factor; + pa = h.pa; + fratio = h.fratio; + elliptical_flag = h.elliptical_flag; + switch_flag = h.switch_flag; + //Nmod = h.Nmod; + + return *this; +}; + + void LensHalo::initFromMassFunc(float my_mass, float my_Rsize, float my_rscale , PosType my_slope, long *seed){ mass = my_mass; diff --git a/TreeCode_link/grid_maintenance.cpp b/TreeCode_link/grid_maintenance.cpp index e9f8fd80..90e338d6 100644 --- a/TreeCode_link/grid_maintenance.cpp +++ b/TreeCode_link/grid_maintenance.cpp @@ -1057,10 +1057,22 @@ PixelMap Grid::writePixelMap( ,size_t Ny /// number of pixels in image in on dimension ,PosType resolution /// resolution of image in radians ,LensingVariable lensvar /// which quantity is to be displayed - ){ +){ PixelMap map(center, Nx, Ny, resolution); map.AddGrid(*this,lensvar); - + + return map; +} +/// Outputs a PixelMap of the lensing quantities of a fixed grid +PixelMap Grid::writePixelMap( + LensingVariable lensvar /// which quantity is to be displayed +){ + + Branch *branch = i_tree->getTop(); + double resolution = (branch->boundary_p2[0] - branch->boundary_p1[0])/Ngrid_init; + PixelMap map(branch->center, Ngrid_init, Ngrid_init2, resolution); + map.AddGrid(*this,lensvar); + return map; } /** \brief Make a fits map that is automatically centered on the grid and has approximately the same range as the grid. Nx can be used to change the resolution. Nx = grid.getInitNgrid() will give the initial grid resolution @@ -1086,7 +1098,6 @@ void Grid::writePixeFits( * grid pixels in one pixelmap pixel it uses one at random. This is meant * for uniform maps to make equal sized PixelMaps. */ - void Grid::writeFitsUniform( const PosType center[] /// center of image ,size_t Nx /// number of pixels in image in on dimension @@ -1179,6 +1190,7 @@ PixelMap Grid::writePixelMapUniform( return map; } + void Grid::writePixelMapUniform( PixelMap &map ,LensingVariable lensvar /// which quantity is to be displayed diff --git a/include/MOKAlens.h b/include/MOKAlens.h index ffec0fbc..80221cfd 100644 --- a/include/MOKAlens.h +++ b/include/MOKAlens.h @@ -48,7 +48,7 @@ struct MOKAmap{ /// cosmology double omegam,omegal,h,wq; double inarcsec; - double center[2]; + Point_2d center; }; /** @@ -115,28 +115,28 @@ class LensHaloMassMap : public LensHalo void writeImage(std::string fn); /// return center in physical Mpc - const double* getCenter() const { return map->center; } + Point_2d getCenter() const { return map.center; } /// return range of input map in rad - double getRangeRad() const { return map->boxlrad; } + double getRangeRad() const { return map.boxlrad; } /// return range of input map in physical Mpc - double getRangeMpc() const { return map->boxlMpc; } + double getRangeMpc() const { return map.boxlMpc; } /// /// return number of pixels on a side in original map size_t getN() const { - if(map->nx != map->ny) + if(map.nx != map.ny) throw std::runtime_error("mass map is not square"); - return map->nx; + return map.nx; } /// return number of pixels on a x-axis side in original map - size_t getNx() const { return map->nx; } + size_t getNx() const { return map.nx; } /// return number of pixels on a y-axis side in original map - size_t getNy() const { return map->ny; } - + size_t getNy() const { return map.ny; } + private: PixelMapType maptype; void initMap(); void convertmap(MOKAmap *map,PixelMapType maptype); - MOKAmap* map; + MOKAmap map; const COSMOLOGY& cosmo; void PreProcessFFTWMap(); int zerosize; diff --git a/include/base_analens.h b/include/base_analens.h index 3b16dd73..1c2d43d7 100644 --- a/include/base_analens.h +++ b/include/base_analens.h @@ -142,6 +142,10 @@ class LensHaloBaseNSIE : public LensHalo{ protected: + // make it uncopyable + LensHaloBaseNSIE & operator=(const LensHaloBaseNSIE &h){}; + LensHaloBaseNSIE(const LensHaloBaseNSIE &h){}; + /// critical surface density PosType Sigma_crit; /// the time delay scale in days/Mpc^2 diff --git a/include/grid_maintenance.h b/include/grid_maintenance.h index c6c4e303..e23c019d 100644 --- a/include/grid_maintenance.h +++ b/include/grid_maintenance.h @@ -75,6 +75,9 @@ struct Grid{ PixelMap writePixelMap(const double center[],size_t Npixels,double resolution,LensingVariable lensvar); PixelMap writePixelMap(const double center[],size_t Nx,size_t Ny,double resolution,LensingVariable lensvar); + /// With the initial boundaries and resolution, ie no refinement + PixelMap writePixelMap(LensingVariable lensvar); + PixelMap writePixelMapUniform(const PosType center[],size_t Nx,size_t Ny,LensingVariable lensvar); void writePixelMapUniform(PixelMap &map,LensingVariable lensvar); void writeFitsUniform(const PosType center[],size_t Nx,size_t Ny,LensingVariable lensvar,std::string filename); diff --git a/include/image_processing.h b/include/image_processing.h index 45bb81d6..43acddae 100644 --- a/include/image_processing.h +++ b/include/image_processing.h @@ -42,7 +42,8 @@ class PixelMap map.resize(0); }; - PixelMap& operator=(PixelMap other); + PixelMap& operator=(const PixelMap &other); + PixelMap& operator=(PixelMap &&other); inline bool valid() const { return map.size(); }; inline std::size_t size() const { return map.size(); }; diff --git a/include/lens_halos.h b/include/lens_halos.h index fdaee163..58d88340 100644 --- a/include/lens_halos.h +++ b/include/lens_halos.h @@ -46,8 +46,18 @@ class LensHalo{ LensHalo(PosType z,COSMOLOGY &cosmo); LensHalo(InputParams& params,COSMOLOGY &cosmo,bool needRsize = true); LensHalo(InputParams& params,bool needRsize = true); + LensHalo(const LensHalo &h); + + LensHalo(LensHalo &&h):star_tree(nullptr){ + *this = std::move(h); + } + virtual ~LensHalo(); + LensHalo & operator=(const LensHalo &h); + LensHalo & operator=(LensHalo &&h); + + /** get the Rmax which is larger than Rsize in Mpc. This is the region exterior to which the halo will be considered a point mass. Between Rsize and Rmax the deflection and shear are interpolated. */ @@ -195,8 +205,8 @@ class LensHalo{ void setID(size_t id){idnumber = id;} PosType renormalization(PosType r_max); - PosType mnorm; - + + private: size_t idnumber; /// Identification number of halo. It is not always used. PosType Dist; @@ -207,17 +217,14 @@ class LensHalo{ // This is the size of the halo beyond which it does not have the profile expected profile. float Rsize = 0; - protected: + PosType mnorm; + // Beyond Rmax the halo will be treated as a point mass. Between Rsize and Rmax the deflection // and shear are interpolated. For circularly symmetric lenses Rmax should be equal to Rsize float Rmax; - // make LensHalo uncopyable - void operator=(LensHalo &){}; - LensHalo(LensHalo &){}; - PosType alpha_int(PosType x) const; PosType norm_int(PosType r_max); @@ -246,9 +253,10 @@ class LensHalo{ PosType operator ()(PosType x) {return halo.gfunction(x)/x ;} }; - IndexType *stars; + std::vector stars_index; std::vector stars_xp; TreeQuadParticles *star_tree; + int stars_N; PosType star_massscale; /// star masses relative to star_massscles @@ -256,7 +264,7 @@ class LensHalo{ PosType star_fstars; PosType star_theta_force; int star_Nregions; - PosType *star_region; + std::vector star_region; PosType beta; void substract_stars_disks(PosType const *ray,PosType *alpha ,KappaType *kappa,KappaType *gamma); @@ -274,7 +282,7 @@ class LensHalo{ /// The factor by which Rmax is larger than Rsize - const float Rmax_to_Rsize_ratio = 1.2; + float Rmax_to_Rsize_ratio = 1.2; /// scale length or core size. Different meaning in different cases. Not used in NSIE case. float rscale; @@ -291,8 +299,8 @@ class LensHalo{ PosType lo_mass_slope; PosType hi_mass_slope; /// parameters for stellar mass function: minimal and maximal stellar mass, bending point for a broken power law IMF - PosType *star_Sigma; - PosType **star_xdisk; + std::vector star_Sigma; + std::vector star_xdisk; /// point mass case @@ -678,6 +686,30 @@ class LensHaloNFW: public LensHalo{ ,int my_stars_N ,EllipMethod my_ellip_method=Pseudo); LensHaloNFW(InputParams& params); + LensHaloNFW(const LensHaloNFW &h):LensHalo(h){ + ++count; + gmax = h.gmax; + } + LensHaloNFW(const LensHaloNFW &&h):LensHalo(std::move(h)){ + ++count; + gmax = h.gmax; + } + + LensHaloNFW & operator=(const LensHaloNFW &h){ + if(this != &h){ + LensHalo::operator=(h); + gmax = h.gmax; + } + return *this; + } + LensHaloNFW & operator=(const LensHaloNFW &&h){ + if(this != &h){ + LensHalo::operator=(std::move(h)); + gmax = h.gmax; + } + return *this; + } + virtual ~LensHaloNFW(); PosType ffunction(PosType x) const; @@ -759,8 +791,6 @@ class LensHaloNFW: public LensHalo{ private: PosType gmax; - - }; // ******************** diff --git a/include/particle_types.h b/include/particle_types.h index 9de8e64c..0a27abb6 100644 --- a/include/particle_types.h +++ b/include/particle_types.h @@ -60,8 +60,8 @@ struct ParticleTypeSimple{ /// Atomic data class for stars with different masses struct StarType{ double &operator[](int i){return x[i];} - double *operator*(){return x;} - double x[3]; + Point_3d operator*(){return x;} + Point_3d x; float Mass; float mass() const {return Mass;} diff --git a/include/utilities_slsim.h b/include/utilities_slsim.h index 11be8a69..e7455b0a 100644 --- a/include/utilities_slsim.h +++ b/include/utilities_slsim.h @@ -1538,8 +1538,8 @@ namespace Utilities data[i].push_back(to_numeric(cell)); i = (i+1)%columns; } - } + return 1; } /** \brief Read numerical data from a csv file with a header From 144bda3d796b41a66aa067402ad23a263fec9c82 Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Wed, 17 Apr 2019 19:01:43 +0200 Subject: [PATCH 126/227] Made DataFrame a little more efficient. --- FullRange/implant_stars.cpp | 2 +- include/utilities_slsim.h | 38 ++++++++++++++++++++++--------------- 2 files changed, 24 insertions(+), 16 deletions(-) diff --git a/FullRange/implant_stars.cpp b/FullRange/implant_stars.cpp index e796f9b0..71fcf289 100644 --- a/FullRange/implant_stars.cpp +++ b/FullRange/implant_stars.cpp @@ -173,7 +173,7 @@ void LensHalo::implant_stars( //std::printf("last star x = %e %e\n",stars_xp[stars_N-1][0],stars_xp[stars_N-1][1]); - float dummy=0; + //float dummy=0; //star_tree = new TreeForce(stars_xp,stars_N,star_masses,&dummy // ,false,false,5,2,false,star_theta_force); diff --git a/include/utilities_slsim.h b/include/utilities_slsim.h index e7455b0a..4d72cff2 100644 --- a/include/utilities_slsim.h +++ b/include/utilities_slsim.h @@ -1726,23 +1726,30 @@ class DataFrame{ ,char comment_char = '#' /// comment charactor for header ,char deliniator = ',' /// deliniator between values ):filename(datafile){ - Utilities::IO::ReadCSVnumerical1(datafile,data, column_names); + Utilities::IO::ReadCSVnumerical1(datafile,data, column_names); + for(int i=0 ; i& operator[](std::string label){ - int i = 0; - for(auto name : column_names){ - if(name == label){ - return data[i]; - } - ++i; - } - for(auto c : column_names ) std::cout << c << " "; - std::cout << std::endl; - throw std::invalid_argument(label + " was not one of the columns of the galaxy data file :" + filename); + std::vector& operator[](const std::string &label){ + return data[datamap[label]]; + for(auto c : column_names ) std::cout << c << " "; + std::cout << std::endl; + throw std::invalid_argument(label + " was not one of the columns of the galaxy data file :" + filename); }; + /// add a column to the data frame. This does a copy. + void add_column(const std::string &name,const std::vector &vec){ + if(vec.size() != data[0].size()){ + std::cerr << "Added column to DataFrame needs to have the same size." << std::endl; + throw std::invalid_argument("wrong size"); + } + column_names.push_back(name); + data.push_back(vec); + data[name] = data.size()-1; + }; /// returns column by number std::vector& operator[](int i){ @@ -1757,9 +1764,10 @@ class DataFrame{ size_t number_of_rows(){return data[0].size();} size_t number_of_columns(){return data.size();} private: - std::vector > data; - std::vector column_names; - std::string filename; + std::map datamap; + std::vector > data; + std::vector column_names; + std::string filename; }; } } From 467e72858058f682be5e270bc21f38e751ad9deb Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Thu, 18 Apr 2019 22:26:36 +0200 Subject: [PATCH 127/227] Changed Lens::insertMainHalo() and Lens::replaceMainHalo() to take a reference and copy the inserted halo. Made a move assignment and move constructor for Lens. Made copy constructor and assignment operators for LensPlanes. --- MultiPlane/lens.cpp | 10 +-- MultiPlane/planes.cpp | 41 +++++++++- include/MOKAlens.h | 2 +- include/lens.h | 163 +++++++++++++++++++++++++++++++++++--- include/planes.h | 34 ++++++-- include/quadTree.h | 1 + include/sourceAnaGalaxy.h | 24 +++--- 7 files changed, 242 insertions(+), 33 deletions(-) diff --git a/MultiPlane/lens.cpp b/MultiPlane/lens.cpp index b54975ac..689c37f9 100644 --- a/MultiPlane/lens.cpp +++ b/MultiPlane/lens.cpp @@ -236,9 +236,7 @@ Lens::Lens(InputParams& params, long* my_seed, const COSMOLOGY &cosmoset, bool v Lens::~Lens() { Utilities::delete_container(lensing_planes); - - //Utilities::free_PosTypeMatrix(halo_pos, field_halos.size(), 3); - + Utilities::delete_container(field_halos); Utilities::delete_container(substructure.halos); std::cout << "In Lens structure" << std::endl; @@ -1478,7 +1476,7 @@ void Lens::clearMainHalos(bool verbose) * * The angular position of the halo should be preserved, but the x coordinates may change */ - +/* void Lens::insertMainHalo(LensHalo* halo,PosType zlens, bool addplanes,bool verbose) { halo->setCosmology(cosmo); @@ -1492,6 +1490,7 @@ void Lens::insertMainHalo(LensHalo* halo,PosType zlens, bool addplanes,bool verb combinePlanes(verbose); } +*/ /** * \brief Inserts a sequense of main lens halos and ads them to the existing ones. @@ -1528,7 +1527,7 @@ void Lens::insertMainHalos(LensHalo** halos, std::size_t Nhalos,bool addplanes, * Note that this does not delete the halos that were there. It just removes * them from the lens. */ -void Lens::replaceMainHalo(LensHalo* halo,PosType zlens, bool addplanes,bool verbose) +/*void Lens::replaceMainHalo(LensHalo* halo,PosType zlens, bool addplanes,bool verbose) { main_halos.clear(); @@ -1542,6 +1541,7 @@ void Lens::replaceMainHalo(LensHalo* halo,PosType zlens, bool addplanes,bool ver createMainPlanes(); combinePlanes(verbose); } +*/ /** * \brief Inserts a sequense of main lens halos and remove all previous ones. diff --git a/MultiPlane/planes.cpp b/MultiPlane/planes.cpp index 0451784e..d695aa4a 100644 --- a/MultiPlane/planes.cpp +++ b/MultiPlane/planes.cpp @@ -17,12 +17,51 @@ LensPlaneTree::LensPlaneTree(LensHaloHndl *my_halos,IndexType Nhalos if(inv_screening_scale != 0) halo_tree = new TreeQuadHalos(my_halos,Nhalos,my_sigma_background,5,0.1,true,inv_screening_scale); else halo_tree = new TreeQuadHalos(my_halos,Nhalos,my_sigma_background); } - +LensPlaneTree::LensPlaneTree(const LensPlaneTree &p) +: LensPlane() +{ + halos = p.halos; + + if(p.halo_tree->inv_screening_scale2 != 0){ + halo_tree = new TreeQuadHalos(halos.data(),halos.size() + ,p.halo_tree->sigma_background + ,5,0.1,true,sqrt(p.halo_tree->inv_screening_scale2)); + }else{ + halo_tree = new TreeQuadHalos(halos.data(),halos.size(),p.halo_tree->sigma_background); + } +} LensPlaneTree::~LensPlaneTree(){ delete halo_tree; } +LensPlaneTree & LensPlaneTree::operator=(const LensPlaneTree &p){ + if(&p != this){ + halos = p.halos; + + if(p.halo_tree->inv_screening_scale2 != 0){ + halo_tree = new TreeQuadHalos(halos.data(),halos.size() + ,p.halo_tree->sigma_background + ,5,0.1,true,sqrt(p.halo_tree->inv_screening_scale2)); + }else{ + halo_tree = new TreeQuadHalos(halos.data(),halos.size(),p.halo_tree->sigma_background); + } + } + + return *this; +} + +LensPlaneTree & LensPlaneTree::operator=(LensPlaneTree &&p){ + if(&p != this){ + std::swap(halos,p.halos); + std::swap(halo_tree,p.halo_tree); + } + + return *this; +} + + + void LensPlaneTree::force(PosType *alpha,KappaType *kappa,KappaType *gamma ,KappaType *phi,PosType *xx){ halo_tree->force2D_recur(xx,alpha,kappa,gamma,phi); diff --git a/include/MOKAlens.h b/include/MOKAlens.h index 80221cfd..3cad24ce 100644 --- a/include/MOKAlens.h +++ b/include/MOKAlens.h @@ -65,7 +65,7 @@ class LensHaloMassMap : public LensHalo { public: LensHaloMassMap(const std::string& filename - , PixelMapType maptype + ,PixelMapType maptype ,int pixel_map_zeropad ,bool my_zeromean , const COSMOLOGY& lenscosmo diff --git a/include/lens.h b/include/lens.h index 6edd748c..80647591 100644 --- a/include/lens.h +++ b/include/lens.h @@ -68,14 +68,12 @@ GLAMER_TEST_USES(LensTest) *
*/ -class Lens -{ +class Lens{ public: Lens(long* seed, PosType z_source,CosmoParamSet cosmoset = WMAP5yr, bool verbose = false); Lens(InputParams& params, long* my_seed, CosmoParamSet cosmoset = WMAP5yr, bool verbose = false); Lens(long* seed, PosType z_source,const COSMOLOGY &cosmo, bool verbose = false); Lens(InputParams& params, long* my_seed, const COSMOLOGY &cosmo, bool verbose = false); - //Lens(Lens &lens); ~Lens(); @@ -133,13 +131,68 @@ class Lens /// inserts a single main lens halo and adds it to the existing ones - void insertMainHalo(LensHalo* halo,PosType zlens, bool addplanes,bool verbose = false); + //void insertMainHalo(LensHalo* halo,PosType zlens, bool addplanes,bool verbose = false); + +/* void insertMainHalo(LensHalo *halo, bool addplanes,bool verbose) + { +// LensHaloNFW * halo = new LensHaloNFW(halo_in); + halo->setCosmology(cosmo); + main_halos.push_back(halo); + + flag_switch_main_halo_on = true; + + if(addplanes) addMainHaloToPlane(halo); + else addMainHaloToNearestPlane(halo); + + combinePlanes(verbose); + } +*/ + + template + void insertMainHalo(const T &halo_in, bool addplanes,bool verbose=false) + { + + T * halo = new T(halo_in); + halo->setCosmology(cosmo); + main_halos.push_back(halo); + + flag_switch_main_halo_on = true; + + if(addplanes) addMainHaloToPlane(halo); + else addMainHaloToNearestPlane(halo); + + combinePlanes(verbose); + } + + /** + * \brief Inserts a single main lens halo and deletes all previous ones. + * Then all lensing planes are updated accordingly. + * + * Note that this does not delete the halos that were there. It just removes + * them from the lens. + */ + template + void replaceMainHalo(const T &halo_in,bool addplanes,bool verbose=false) + { + main_halos.clear(); + + T * halo = new T(halo_in); + halo->setCosmology(cosmo); + main_halos.push_back(halo); + + flag_switch_main_halo_on = true; + + Utilities::delete_container(main_planes); + createMainPlanes(); + combinePlanes(verbose); + } + /// inserts a sequence of main lens halos and adds them to the existing ones void insertMainHalos(LensHalo** halos, std::size_t Nhalos,bool addplanes,bool verbose = false); /// replaces existing main halos with a single main halo - void replaceMainHalo(LensHalo* halo,PosType zlens, bool addplanes,bool verbose = false); + //void replaceMainHalo(LensHalo* halo,PosType zlens, bool addplanes,bool verbose = false); /// replaces existing main halos with a sequence of main halos void replaceMainHalos(LensHalo** halos, std::size_t Nhalos,bool verbose = false); @@ -181,9 +234,6 @@ class Lens template HaloType* getMainHalo(std::size_t i); - - - void rayshooterInternal(unsigned long Npoints, Point *i_points, bool RSIverbose = false); void info_rayshooter(Point *i_point ,std::vector & ang_positions @@ -248,6 +298,102 @@ class Lens ,bool verbose = false ); + Lens & operator=(Lens &&lens){ + + fieldofview = lens.fieldofview; + seed = lens.seed; + init_seed = lens.init_seed; + cosmo = lens.cosmo; + toggle_source_plane = lens.toggle_source_plane; + dDs_implant = lens.dDs_implant; + zs_implant = lens.zs_implant; + Ds_implant = lens.Ds_implant; + index_of_new_sourceplane = lens.index_of_new_sourceplane; + zsource = lens.zsource; + + NZSamples = lens.NZSamples; + zbins = lens.zbins; + NhalosbinZ = lens.NhalosbinZ; + Nhaloestot_Tab = lens.Nhaloestot_Tab; + aveNhalosField = lens.aveNhalosField; + Logm = lens.Logm; + NhalosbinMass = lens.NhalosbinMass; + sigma_back_Tab = lens.sigma_back_Tab; + + flag_switch_deflection_off = lens.flag_switch_deflection_off; + flag_switch_lensing_off = lens.flag_switch_lensing_off; + + Dl = lens.Dl; + dDl = lens.dDl; + plane_redshifts = lens.plane_redshifts; + charge = lens.charge; + flag_switch_field_off = lens.flag_switch_field_off; + + field_halos = lens.field_halos; + + field_Nplanes_original = lens.field_Nplanes_original; + field_Nplanes_current = lens.field_Nplanes_current; + + field_plane_redshifts = lens.field_plane_redshifts; + field_plane_redshifts_original = lens.field_plane_redshifts_original; + field_Dl = lens.field_Dl; + field_Dl_original = lens.field_Dl_original; + + substructure = lens.substructure; + + WasInsertSubStructuresCalled = lens.WasInsertSubStructuresCalled; + field_mass_func_type = lens.field_mass_func_type; + mass_func_PL_slope = lens.mass_func_PL_slope; + field_min_mass = lens.field_min_mass; + field_int_prof_type = lens.field_int_prof_type; + field_prof_internal_slope = lens.field_prof_internal_slope; + + flag_field_gal_on = lens.flag_field_gal_on; + field_int_prof_gal_type = lens.field_int_prof_gal_type; + field_int_prof_gal_slope = lens.field_int_prof_gal_slope; + + redshift_planes_file = lens.redshift_planes_file; + read_redshift_planes = lens.read_redshift_planes; + + field_input_sim_file = lens.field_input_sim_file; + field_input_sim_format = lens.field_input_sim_format; + + sim_input_flag = lens.sim_input_flag; + read_sim_file = lens.read_sim_file; + field_buffer = lens.field_buffer; + + flag_switch_main_halo_on = lens.flag_switch_main_halo_on; + + main_plane_redshifts = lens.main_plane_redshifts; + main_Dl = lens.main_Dl; + + main_halo_type = lens.main_halo_type; + main_galaxy_halo_type = lens.main_galaxy_halo_type; + + pixel_map_input_file = lens.pixel_map_input_file; + pixel_map_on = lens.pixel_map_on; + pixel_map_zeropad = lens.pixel_map_zeropad; + pixel_map_zeromean = lens.pixel_map_zeromean; + + central_point_sphere = lens.central_point_sphere; + sim_angular_radius = lens.sim_angular_radius; + inv_ang_screening_scale = lens.inv_ang_screening_scale; + + std::swap(lensing_planes,lens.lensing_planes); + std::swap(field_planes,lens.field_planes); + swap(main_halos,lens.main_halos); /// MixedVector cannot be copyed + std::swap(main_planes,lens.main_planes); + + return *this; + } + Lens(Lens &&lens){ + *this = std::move(lens); + } + +private: + Lens & operator=(const Lens &lens); // block copy + Lens(const Lens &lens); + protected: /// field of view in square degrees PosType fieldofview; @@ -282,7 +428,6 @@ class Lens void quicksort(LensHaloHndl *halo,PosType **pos,unsigned long N); -private: /* generation */ /// create the lens planes void buildPlanes(InputParams& params, bool verbose); diff --git a/include/planes.h b/include/planes.h index 2db344a0..48091c65 100644 --- a/include/planes.h +++ b/include/planes.h @@ -31,13 +31,17 @@ class LensPlane{ class LensPlaneTree : public LensPlane{ public: LensPlaneTree(LensHaloHndl *my_halos,IndexType Nhalos,PosType my_sigma_background,PosType my_inv_screening_scale = 0); + LensPlaneTree(const LensPlaneTree &p); + LensPlaneTree(LensPlaneTree &&p){ + std::swap(*this,p); + } + ~LensPlaneTree(); + LensPlaneTree & operator=(const LensPlaneTree &p); + LensPlaneTree & operator=(LensPlaneTree &&p); + void force(PosType *alpha,KappaType *kappa,KappaType *gamma,KappaType *phi,PosType *xx); - void closestHalos(PosType *xx,std::list neighbors){ - - }; - void addHalo(LensHalo* halo); void removeHalo(LensHalo* halo); @@ -48,7 +52,6 @@ class LensPlaneTree : public LensPlane{ private: std::vector halos; - TreeQuadHalos * halo_tree; }; @@ -60,7 +63,26 @@ class LensPlaneTree : public LensPlane{ class LensPlaneSingular : public LensPlane{ public: LensPlaneSingular(LensHaloHndl *my_halos, IndexType Nhalos); - ~LensPlaneSingular(); + LensPlaneSingular(const LensPlaneSingular &p){ + halos = p.halos; + } + LensPlaneSingular(LensPlaneSingular &&p){ + std::swap(halos,p.halos); + } + ~LensPlaneSingular(); + + LensPlaneSingular & operator=(const LensPlaneSingular &p){ + if(&p != this){ + halos = p.halos; + } + return *this; + } + LensPlaneSingular & operator=(LensPlaneSingular &&p){ + if(&p != this){ + std::swap(halos,p.halos); + } + return *this; + } void force(PosType *alpha,KappaType *kappa,KappaType *gamma,KappaType *phi,PosType *xx); diff --git a/include/quadTree.h b/include/quadTree.h index 62f4fbf1..b0b9b34c 100644 --- a/include/quadTree.h +++ b/include/quadTree.h @@ -1192,6 +1192,7 @@ class TreeQuadHalos { ); ~TreeQuadHalos(); + friend class LensPlaneTree; void force2D(PosType const *ray,PosType *alpha,KappaType *kappa,KappaType *gamma ,KappaType *phi) const; diff --git a/include/sourceAnaGalaxy.h b/include/sourceAnaGalaxy.h index 60bc53cd..95ee653c 100644 --- a/include/sourceAnaGalaxy.h +++ b/include/sourceAnaGalaxy.h @@ -182,22 +182,22 @@ bool idcompare(SourceOverzierPlus s1,SourceOverzierPlus s2); */ class SourceMultiShapelets: public Source{ public: - SourceMultiShapelets(InputParams& params); - ~SourceMultiShapelets(); - void sortInRedshift(); - void sortInMag(); - - /// Surface brightness of current galaxy. - PosType SurfaceBrightness(PosType* x) { + + SourceMultiShapelets(InputParams& params); + ~SourceMultiShapelets(); + void sortInRedshift(); + void sortInMag(); + /// Surface brightness of current galaxy. + PosType SurfaceBrightness(PosType* x) { PosType sb = galaxies[index].SurfaceBrightness(x); if (sb < sb_limit) return 0.; return sb; - } + } void printSource(); - std::size_t getNumberOfGalaxies() const {return galaxies.size();} + std::size_t getNumberOfGalaxies() const {return galaxies.size();} /// number of galaxies - std::size_t size() const {return galaxies.size();} + std::size_t size() const {return galaxies.size();} /// Total flux coming from the current galaxy in erg/sec/Hz/cm^2 PosType getTotalFlux() const {return pow(10,-(48.6+galaxies[index].getMag())/2.5);} @@ -207,7 +207,7 @@ class SourceMultiShapelets: public Source{ /// Set angular position of current source. void setTheta(PosType my_theta[2]){galaxies[index].setTheta(my_theta);} void setTheta(PosType my_x,PosType my_y){galaxies[index].setTheta(my_x, my_y);} - void setTheta(const Point_2d &p){galaxies[index].setTheta(p);} + void setTheta(const Point_2d &p){galaxies[index].setTheta(p);} /// Return redshift of current source. PosType getZ() const {return galaxies[index].getZ();} @@ -222,6 +222,8 @@ class SourceMultiShapelets: public Source{ index = i; return galaxies[index]; } + size_t getIndex() const {return index;} + /** The indexing operator can be used to change the "current" source that is returned when the surface brightness is subsequently * called. */ From 00e0849b88c37da95738e350a5c6563604ff6911 Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Thu, 18 Apr 2019 23:18:27 +0200 Subject: [PATCH 128/227] Copy constructor and assignment for LensHaloMassMap --- MultiPlane/MOKAlens.cpp | 6 +++--- include/MOKAlens.h | 41 +++++++++++++++++++++++++++++++++++++---- 2 files changed, 40 insertions(+), 7 deletions(-) diff --git a/MultiPlane/MOKAlens.cpp b/MultiPlane/MOKAlens.cpp index 7902d3c4..2f92cbc9 100644 --- a/MultiPlane/MOKAlens.cpp +++ b/MultiPlane/MOKAlens.cpp @@ -63,7 +63,7 @@ void cmass(int n, std::valarray map, std:: vector x, double &xcm * \brief loads a mass map from a given filename */ -LensHaloMassMap::LensHaloMassMap(const std::string& filename, PixelMapType my_maptype,int pixel_map_zeropad,bool my_zeromean, const COSMOLOGY& lenscosmo) +LensHaloMassMap::LensHaloMassMap(const std::string& filename, PixelMapType my_maptype,int pixel_map_zeropad,bool my_zeromean, COSMOLOGY& lenscosmo) : LensHalo(), MOKA_input_file(filename), flag_MOKA_analyze(0), flag_background_field(0), maptype(my_maptype), cosmo(lenscosmo),zerosize(pixel_map_zeropad),zeromean(my_zeromean) @@ -86,7 +86,7 @@ LensHaloMassMap::LensHaloMassMap( ,double redshift /// redshift of lens ,int pixel_map_zeropad /// factor by which to zero pad in FFTs, ex. 4 ,bool my_zeromean /// if true, subtracts average density - ,const COSMOLOGY& lenscosmo /// cosmology + ,COSMOLOGY& lenscosmo /// cosmology ) :LensHalo() , flag_MOKA_analyze(0), flag_background_field(0),maptype(pix_map),cosmo(lenscosmo),zerosize(pixel_map_zeropad),zeromean(my_zeromean) @@ -176,7 +176,7 @@ LensHalo(),MOKA_input_file(""),maptype(pix_map),cosmo(lenscosmo),zerosize(pixel_ * * In the future this could be used to read in individual PixelDMaps or other types of maps if the type were specified in the paramfile. */ -LensHaloMassMap::LensHaloMassMap(InputParams& params, const COSMOLOGY& lenscosmo) +LensHaloMassMap::LensHaloMassMap(InputParams& params, COSMOLOGY& lenscosmo) : LensHalo(), maptype(moka), cosmo(lenscosmo) { // read in parameters diff --git a/include/MOKAlens.h b/include/MOKAlens.h index 3cad24ce..15eafd53 100644 --- a/include/MOKAlens.h +++ b/include/MOKAlens.h @@ -68,12 +68,12 @@ class LensHaloMassMap : public LensHalo ,PixelMapType maptype ,int pixel_map_zeropad ,bool my_zeromean - , const COSMOLOGY& lenscosmo + ,COSMOLOGY& lenscosmo ); //LensHaloMassMap(PixelMap &map,double massconvertion,double zlens,double zsource,int pixel_map_zeropad,const COSMOLOGY& lenscosmo); - LensHaloMassMap(InputParams& params, const COSMOLOGY& lenscosmo); + LensHaloMassMap(InputParams& params, COSMOLOGY& lenscosmo); LensHaloMassMap( const PixelMap &MassMap /// mass map in solar mass units @@ -81,8 +81,41 @@ class LensHaloMassMap : public LensHalo ,double redshift /// redshift of lens ,int pixel_map_zeropad /// factor by which to zero pad in FFTs, ex. 4 ,bool my_zeromean /// if true, subtracts average density - ,const COSMOLOGY& lenscosmo /// cosmology + ,COSMOLOGY& lenscosmo /// cosmology ); + + LensHaloMassMap(const LensHaloMassMap &h):cosmo(h.cosmo){ +// LensHaloMassMap(const LensHaloMassMap &h){ + maptype = h.maptype; + map = h.map; + zerosize = h.zerosize; + zeromean = h.zeromean; + } + LensHaloMassMap(LensHaloMassMap &&h):cosmo(h.cosmo){ +// LensHaloMassMap(LensHaloMassMap &&h){ + *this = std::move(h); + } + + LensHaloMassMap & operator=(LensHaloMassMap &h){ + if(&h != this){ + //cosmo = h.cosmo; + maptype = h.maptype; + map = h.map; + zerosize = h.zerosize; + zeromean = h.zeromean; + } + return *this; + } + LensHaloMassMap & operator=(LensHaloMassMap &&h){ + if(&h != this){ + //cosmo = h.cosmo; + maptype = h.maptype; + map = std::move(h.map); + zerosize = h.zerosize; + zeromean = h.zeromean; + } + return *this; + } ~LensHaloMassMap(); @@ -137,7 +170,7 @@ class LensHaloMassMap : public LensHalo void initMap(); void convertmap(MOKAmap *map,PixelMapType maptype); MOKAmap map; - const COSMOLOGY& cosmo; + COSMOLOGY& cosmo; void PreProcessFFTWMap(); int zerosize; bool zeromean; From c0b8d76f74c8cc627c5d4c4b9c52659c80a9f467 Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Fri, 19 Apr 2019 09:49:16 +0200 Subject: [PATCH 129/227] Changed Lens::insertHalos() and Lens::replaceHalos() so that they take possession of the halos. --- ImageProcessing/observation.cpp | 6 ++-- MultiPlane/lens.cpp | 14 ++++---- TreeCode/quadTree.cpp | 4 +-- TreeCode_link/Tree.cpp | 2 +- include/base_analens.h | 2 +- include/concave_hull.h | 6 ++-- include/lens.h | 64 ++++++++++++++++++++++++++++++--- 7 files changed, 76 insertions(+), 22 deletions(-) diff --git a/ImageProcessing/observation.cpp b/ImageProcessing/observation.cpp index b5f75489..bf6cda95 100644 --- a/ImageProcessing/observation.cpp +++ b/ImageProcessing/observation.cpp @@ -196,7 +196,8 @@ Npix_x(Npix_x),Npix_y(Npix_y) * \param seeing FWHM in arcsecs of the image */ Observation::Observation(float diameter, float transmission, float exp_time, int exp_num, float back_mag, float ron, size_t Npix_x,size_t Npix_y,float seeing): -diameter(diameter), transmission(transmission), exp_time(exp_time), exp_num(exp_num), back_mag(back_mag), ron(ron), seeing(seeing),Npix_x(Npix_x),Npix_y(Npix_y) +diameter(diameter), transmission(transmission), exp_time(exp_time), exp_num(exp_num), back_mag(back_mag), ron(ron) +,Npix_x(Npix_x),Npix_y(Npix_y),seeing(seeing) { mag_zeropoint = 2.5*log10(diameter*diameter*transmission*PI/4./hplanck) - 48.6; telescope = false; @@ -214,7 +215,8 @@ diameter(diameter), transmission(transmission), exp_time(exp_time), exp_num(exp_ * \param oversample Oversampling rate of the PSF image */ Observation::Observation(float diameter, float transmission, float exp_time, int exp_num, float back_mag, float ron, std::string psf_file,size_t Npix_x,size_t Npix_y, float oversample): -diameter(diameter), transmission(transmission), exp_time(exp_time), exp_num(exp_num), back_mag(back_mag), ron(ron), oversample(oversample),Npix_x(Npix_x),Npix_y(Npix_y) +diameter(diameter), transmission(transmission), exp_time(exp_time), exp_num(exp_num), back_mag(back_mag),Npix_x(Npix_x),Npix_y(Npix_y) + , ron(ron), oversample(oversample) { mag_zeropoint = 2.5*log10(diameter*diameter*transmission*PI/4./hplanck) - 48.6; diff --git a/MultiPlane/lens.cpp b/MultiPlane/lens.cpp index 689c37f9..65898248 100644 --- a/MultiPlane/lens.cpp +++ b/MultiPlane/lens.cpp @@ -1492,7 +1492,7 @@ void Lens::insertMainHalo(LensHalo* halo,PosType zlens, bool addplanes,bool verb } */ -/** +/* * \brief Inserts a sequense of main lens halos and ads them to the existing ones. * Then all lensing planes are updated accordingly. * If addplanes is true new planes will be added otherwise @@ -1501,7 +1501,7 @@ void Lens::insertMainHalo(LensHalo* halo,PosType zlens, bool addplanes,bool verb * * The angular position of the halo should be preserved, but the x coordinates may change */ -void Lens::insertMainHalos(LensHalo** halos, std::size_t Nhalos,bool addplanes, bool verbose) +/*void Lens::insertMainHalos(LensHalo** halos, std::size_t Nhalos,bool addplanes, bool verbose) { for(std::size_t i = 0; i < Nhalos; ++i) { @@ -1515,12 +1515,9 @@ void Lens::insertMainHalos(LensHalo** halos, std::size_t Nhalos,bool addplanes, flag_switch_main_halo_on = true; combinePlanes(verbose); -} - - +}*/ - -/** +/* * \brief Inserts a single main lens halo and deletes all previous ones. * Then all lensing planes are updated accordingly. * @@ -1550,7 +1547,7 @@ void Lens::insertMainHalos(LensHalo** halos, std::size_t Nhalos,bool addplanes, * them from the lens. * Then all lensing planes are updated accordingly. */ -void Lens::replaceMainHalos(LensHalo** halos, std::size_t Nhalos,bool verbose) +/*void Lens::replaceMainHalos(LensHalo** halos, std::size_t Nhalos,bool verbose) { main_halos.clear(); @@ -1567,6 +1564,7 @@ void Lens::replaceMainHalos(LensHalo** halos, std::size_t Nhalos,bool verbose) createMainPlanes(); combinePlanes(verbose); } + */ /** diff --git a/TreeCode/quadTree.cpp b/TreeCode/quadTree.cpp index 140ce529..0570fb9b 100644 --- a/TreeCode/quadTree.cpp +++ b/TreeCode/quadTree.cpp @@ -534,7 +534,7 @@ void TreeQuadHalos::walkTree_iter( IndexType i; bool allowDescent=true; unsigned long count=0,tmp_index; - PosType arg1, arg2, prefac; + PosType prefac; //bool notscreen = true; assert(tree); @@ -763,7 +763,7 @@ void TreeQuadHalos::walkTree_recur(QBranchNB *branch,PosType const *ray,PosType PosType xcm[2],rcm2cell,rcm2,tmp,boxsize2; IndexType i; std::size_t tmp_index; - PosType arg1, arg2, prefac; + PosType prefac; /*bool notscreen = true; if(inv_screening_scale2 != 0) notscreen = BoxIntersectCircle(ray,3*sqrt(1.0/inv_screening_scale2), branch->boundary_p1, branch->boundary_p2); diff --git a/TreeCode_link/Tree.cpp b/TreeCode_link/Tree.cpp index 8d389dc6..5f9579fa 100644 --- a/TreeCode_link/Tree.cpp +++ b/TreeCode_link/Tree.cpp @@ -974,7 +974,7 @@ PixelMap ImageFinding::mapCriticalCurves(const std::vector &init_points typename std::list::iterator p,nextpoint; //auto p = leftovers.begin(); - double minarea,area,co1,co2; + double minarea,area,co1;//,co2; size_t i; Point_2d vo,v1,v2; - int count = 0; + //int count = 0; while(edges.front().length > scale ){ if(leftovers.size() == 0) break; @@ -426,7 +426,7 @@ void concave(std::vector &init_points typename std::list::iterator p,nextpoint; //auto p = leftovers.begin(); - double minarea,area,co1,co2; + double minarea,area,co1; size_t i; Point_2d vo,v1,v2; diff --git a/include/lens.h b/include/lens.h index 80647591..9638aeea 100644 --- a/include/lens.h +++ b/include/lens.h @@ -168,13 +168,12 @@ class Lens{ * \brief Inserts a single main lens halo and deletes all previous ones. * Then all lensing planes are updated accordingly. * - * Note that this does not delete the halos that were there. It just removes - * them from the lens. + * Note that this does delete all the halos that were there. */ template void replaceMainHalo(const T &halo_in,bool addplanes,bool verbose=false) { - main_halos.clear(); + Utilities::delete_container(main_halos); // ???? T * halo = new T(halo_in); halo->setCosmology(cosmo); @@ -187,15 +186,69 @@ class Lens{ combinePlanes(verbose); } + /** + * \brief Inserts a sequense of main lens halos and adds them to the existing ones. + * Then all lensing planes are updated accordingly. + * If addplanes is true new planes will be added otherwise + * the halo is added to the nearest plane and a plane is added only + * if none exited on entry. + * + * The angular position of the halo should be preserved, but the x coordinates may change + * The halos are copied so the input halos can be destoyed without affecting the Lens. + */ + template + void insertMainHalos(std::vector &my_halos,bool addplanes, bool verbose) + { + T* ptr; + //for(std::size_t i = 0; i < my_halos.size() ; ++i) + for(T &h : my_halos){ + ptr = new T(h); + ptr->setCosmology(cosmo); + ptr->setDist(cosmo); + main_halos.push_back( ptr ); + if(addplanes) addMainHaloToPlane( ptr ); + else addMainHaloToNearestPlane( ptr ); + } + + flag_switch_main_halo_on = true; + + combinePlanes(verbose); + } + /** + * \brief Inserts a sequense of main lens halos and remove all previous ones. + * + * Note that this does delete the halos that were there. + * Then all lensing planes are updated accordingly. + */ + template + void replaceMainHalos(std::vector &my_halos,bool verbose) + { + Utilities::delete_container(main_halos); // ???? + + T* ptr; + //for(std::size_t i = 0; i < my_halos.size() ; ++i) + for(T &h : my_halos){ + ptr = new T(h); + ptr->setCosmology(cosmo); + ptr->setDist(cosmo); + main_halos.push_back( ptr ); + } + + flag_switch_main_halo_on = true; + + Utilities::delete_container(main_planes); + createMainPlanes(); + combinePlanes(verbose); + } /// inserts a sequence of main lens halos and adds them to the existing ones - void insertMainHalos(LensHalo** halos, std::size_t Nhalos,bool addplanes,bool verbose = false); + //void insertMainHalos(LensHalo** halos, std::size_t Nhalos,bool addplanes,bool verbose = false); /// replaces existing main halos with a single main halo //void replaceMainHalo(LensHalo* halo,PosType zlens, bool addplanes,bool verbose = false); /// replaces existing main halos with a sequence of main halos - void replaceMainHalos(LensHalo** halos, std::size_t Nhalos,bool verbose = false); + // replaceMainHalos(LensHalo** halos, std::size_t Nhalos,bool verbose = false); /** \brief Add substructures to the lens. @@ -230,6 +283,7 @@ class Lens{ /// get single main halo LensHalo* getMainHalo(std::size_t i); + /// get single main halo of given type template HaloType* getMainHalo(std::size_t i); From 5b0053f4b229b1028f0fdcf603659338ce805fc4 Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Fri, 19 Apr 2019 12:29:47 +0200 Subject: [PATCH 130/227] Implemented copy and move methods for MOKAmap --- include/MOKAlens.h | 119 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 119 insertions(+) diff --git a/include/MOKAlens.h b/include/MOKAlens.h index 15eafd53..f55f8e66 100644 --- a/include/MOKAlens.h +++ b/include/MOKAlens.h @@ -49,6 +49,125 @@ struct MOKAmap{ double omegam,omegal,h,wq; double inarcsec; Point_2d center; + + + MOKAmap(){} + MOKAmap(const MOKAmap &map){ + convergence = map.convergence; + alpha1 = map.alpha1; + alpha2 = map.alpha2; + gamma1 = map.gamma1; + gamma2 = map.gamma2; + gamma3 = map.gamma3; + phi = map.phi; + Signlambdar = map.Signlambdar; + Signlambdat = map.Signlambdat; + x = map.x; + nx = map.nx; + ny = map.ny; + zlens = map.zlens; + m = map.m; + zsource = map.zsource; + Dlens = map.Dlens; + DLS = map.DLS; + DS = map.DS; + c = map.c; + cS = map.cS; + fsub = map.fsub; + mstar = map.mstar; + minsubmass = map.minsubmass; + boxlarcsec = map.boxlarcsec; + boxlMpc = map.boxlMpc; + boxlrad = map.boxlrad; + omegam = map.omegam; + omegal = map.omegal; + h = map.h; + wq = map.wq; + inarcsec = map.inarcsec; + center = map.center; + } + + MOKAmap(MOKAmap &&map){ + *this = std::move(map); + } + + MOKAmap & operator=(MOKAmap &&map){ + if(this != &map){ + convergence = std::move(map.convergence); + alpha1 = std::move(map.alpha1); + alpha2 = std::move(map.alpha2); + gamma1 = std::move(map.gamma1); + gamma2 = std::move(map.gamma2); + gamma3 = std::move(map.gamma3); + phi = std::move(map.phi); + Signlambdar = std::move(map.Signlambdar); + Signlambdat = std::move(map.Signlambdat); + x = std::move(map.x); + nx = map.nx; + ny = map.ny; + zlens = map.zlens; + m = map.m; + zsource = map.zsource; + Dlens = map.Dlens; + DLS = map.DLS; + DS = map.DS; + c = map.c; + cS = map.cS; + fsub = map.fsub; + mstar = map.mstar; + minsubmass = map.minsubmass; + boxlarcsec = map.boxlarcsec; + boxlMpc = map.boxlMpc; + boxlrad = map.boxlrad; + omegam = map.omegam; + omegal = map.omegal; + h = map.h; + wq = map.wq; + inarcsec = map.inarcsec; + center = map.center; + } + + return *this; + } + MOKAmap & operator=(const MOKAmap &map){ + if(this != &map){ + convergence = map.convergence; + alpha1 = map.alpha1; + alpha2 = map.alpha2; + gamma1 = map.gamma1; + gamma2 = map.gamma2; + gamma3 = map.gamma3; + phi = map.phi; + Signlambdar = map.Signlambdar; + Signlambdat = map.Signlambdat; + x = map.x; + nx = map.nx; + ny = map.ny; + zlens = map.zlens; + m = map.m; + zsource = map.zsource; + Dlens = map.Dlens; + DLS = map.DLS; + DS = map.DS; + c = map.c; + cS = map.cS; + fsub = map.fsub; + mstar = map.mstar; + minsubmass = map.minsubmass; + boxlarcsec = map.boxlarcsec; + boxlMpc = map.boxlMpc; + boxlrad = map.boxlrad; + omegam = map.omegam; + omegal = map.omegal; + h = map.h; + wq = map.wq; + inarcsec = map.inarcsec; + center = map.center; + } + + return *this; + } + }; /** From 33b086a272234e8829d16393b959e33f04c5ae2c Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Mon, 22 Apr 2019 15:18:05 +0200 Subject: [PATCH 131/227] Added Grid::MapSurfaceBrightness(). Added Grid::EinsteinArea() Added move operators for Grid. --- MultiPlane/lens.cpp | 3 +- TreeCode_link/grid_maintenance.cpp | 25 +++++++++++ include/Tree.h | 2 - include/grid_maintenance.h | 41 ++++++++++++++++-- include/gridmap.h | 67 ++++++++++++++++++++---------- 5 files changed, 107 insertions(+), 31 deletions(-) diff --git a/MultiPlane/lens.cpp b/MultiPlane/lens.cpp index 65898248..9e17d623 100644 --- a/MultiPlane/lens.cpp +++ b/MultiPlane/lens.cpp @@ -236,10 +236,9 @@ Lens::Lens(InputParams& params, long* my_seed, const COSMOLOGY &cosmoset, bool v Lens::~Lens() { Utilities::delete_container(lensing_planes); - Utilities::delete_container(field_halos); Utilities::delete_container(substructure.halos); - std::cout << "In Lens structure" << std::endl; + //std::cout << "In Lens destructor" << std::endl; } /// read in Cosmological Parameters diff --git a/TreeCode_link/grid_maintenance.cpp b/TreeCode_link/grid_maintenance.cpp index 90e338d6..d9e0a468 100644 --- a/TreeCode_link/grid_maintenance.cpp +++ b/TreeCode_link/grid_maintenance.cpp @@ -306,6 +306,19 @@ PosType Grid::RefreshSurfaceBrightnesses(SourceHndl source){ return total; } + +PosType Grid::EinsteinArea() const { + PosType total=0; + PointList::iterator it; + it = (i_tree->pointlist->Top()); + size_t N = i_tree->pointlist->size(); + for(unsigned long i=0 ; i < N ; ++i,--it){ + if( (*it)->invmag < 0) total += (*it)->gridsize * (*it)->gridsize; + } + + return total; +} + /** * \brief Reset the surface brightness and in_image flag in every point on image and source planes to zero (false) */ @@ -1075,6 +1088,18 @@ PixelMap Grid::writePixelMap( return map; } + +PixelMap Grid::MapSurfaceBrightness(double resolution){ + Branch *branch = i_tree->getTop(); + int Nx = (int)( (branch->boundary_p2[0] - branch->boundary_p1[0])/resolution ); + int Ny = (int)( (branch->boundary_p2[1] - branch->boundary_p1[1])/resolution ); + + PixelMap map(branch->center,Nx,Ny,resolution); + map.AddGridBrightness(*this); + + return map; +} + /** \brief Make a fits map that is automatically centered on the grid and has approximately the same range as the grid. Nx can be used to change the resolution. Nx = grid.getInitNgrid() will give the initial grid resolution */ void Grid::writePixeFits( diff --git a/include/Tree.h b/include/Tree.h index 38aa7779..fe7bbde3 100644 --- a/include/Tree.h +++ b/include/Tree.h @@ -195,8 +195,6 @@ struct TreeStruct{ void _FindBox(TreeStruct::iterator ¤t,const PosType* ray) const; void _NearestNeighbor(TreeStruct::iterator ¤t,int Nneighbors,Point **neighborpoints,PosType *rneighbors,short *direction,TreeStruct::Globals &glabs) const; - - }; typedef struct TreeStruct *TreeHndl; diff --git a/include/grid_maintenance.h b/include/grid_maintenance.h index e23c019d..11c64cb7 100644 --- a/include/grid_maintenance.h +++ b/include/grid_maintenance.h @@ -38,7 +38,8 @@ struct Grid{ double RefreshSurfaceBrightnesses(SourceHndl source); double ClearSurfaceBrightnesses(); unsigned long getNumberOfPoints() const; - + /// area of region with negative magnification + PosType EinsteinArea() const; /// tree on image plane TreeHndl i_tree; @@ -78,20 +79,53 @@ struct Grid{ /// With the initial boundaries and resolution, ie no refinement PixelMap writePixelMap(LensingVariable lensvar); + /// make image of surface brightness + void MapSurfaceBrightness(PixelMap &map){ + map.Clean(); + map.AddGridBrightness(*this); + } + /// map a map of the whole gridded area with given resolution + PixelMap MapSurfaceBrightness(double resolution); + PixelMap writePixelMapUniform(const PosType center[],size_t Nx,size_t Ny,LensingVariable lensvar); void writePixelMapUniform(PixelMap &map,LensingVariable lensvar); void writeFitsUniform(const PosType center[],size_t Nx,size_t Ny,LensingVariable lensvar,std::string filename); + Grid(Grid &&grid){ + *this = std::move(grid); + } + + Grid & operator=(Grid &&grid){ + i_tree = grid.i_tree; + grid.i_tree = nullptr; + s_tree = grid.s_tree; + grid.s_tree = nullptr; + neighbors = grid.neighbors; + grid.neighbors = nullptr; + trashkist = grid.trashkist; + grid.trashkist = nullptr; + + Ngrid_init = grid.Ngrid_init; + Ngrid_init2 = grid.Ngrid_init2; + Ngrid_block = grid.Ngrid_block; + initialized = grid.initialized; + maglimit = grid.maglimit; + pointID = grid.pointID; + axisratio = grid.axisratio; + + return *this; + } + private: void xygridpoints(Point *points,double range,const double *center,long Ngrid ,short remove_center); /// one dimensional size of initial grid - const int Ngrid_init; + int Ngrid_init; int Ngrid_init2; /// one dimensional number of cells a cell will be divided into on each refinement step - const int Ngrid_block; + int Ngrid_block; bool initialized; Kist * trashkist; @@ -105,7 +139,6 @@ struct Grid{ unsigned long pointID; PosType axisratio; void writePixelMapUniform_(const PointList &list,PixelMap *map,LensingVariable val); - static std::mutex grid_mutex; }; diff --git a/include/gridmap.h b/include/gridmap.h index 0eee76b6..7cb04866 100644 --- a/include/gridmap.h +++ b/include/gridmap.h @@ -28,13 +28,13 @@ struct GridMap{ GridMap(LensHndl lens,unsigned long N1d,const double center[2],double range); - GridMap(LensHndl lens ,unsigned long Nx ,const PosType center[2] ,PosType rangeX ,PosType rangeY); + GridMap(LensHndl lens ,unsigned long Nx ,const PosType center[2] ,PosType rangeX ,PosType rangeY); ~GridMap(); /// reshoot the rays for example when the source plane has been changed - void ReInitializeGrid(LensHndl lens); + void ReInitializeGrid(LensHndl lens); double RefreshSurfaceBrightnesses(SourceHndl source); - void ClearSurfaceBrightnesses(); + void ClearSurfaceBrightnesses(); size_t getNumberOfPoints() const {return Ngrid_init*Ngrid_init2;} /// return initial number of grid points in each direction @@ -42,38 +42,59 @@ struct GridMap{ /// return initial range of gridded region. This is the distance from the first ray in a row to the last (unlike PixelMap) double getXRange(){return x_range;} double getYRange(){return x_range*axisratio;} - // resolution in radians - double getResolution(){return x_range/(Ngrid_init-1);} + // resolution in radians + double getResolution(){return x_range/(Ngrid_init-1);} - PixelMap writePixelMapUniform(const PosType center[],size_t Nx,size_t Ny,LensingVariable lensvar); - /// make pixel map of lensing quantities at the resolution of the GridMap - PixelMap writePixelMapUniform(LensingVariable lensvar); - void writePixelMapUniform(PixelMap &map,LensingVariable lensvar); - void writeFitsUniform(const PosType center[],size_t Nx,size_t Ny,LensingVariable lensvar,std::string filename); + PixelMap writePixelMapUniform(const PosType center[],size_t Nx,size_t Ny,LensingVariable lensvar); + /// make pixel map of lensing quantities at the resolution of the GridMap + PixelMap writePixelMapUniform(LensingVariable lensvar); + void writePixelMapUniform(PixelMap &map,LensingVariable lensvar); + void writeFitsUniform(const PosType center[],size_t Nx,size_t Ny,LensingVariable lensvar,std::string filename); - /// returns a PixelMap with the flux in pixels at a resolution of res times the original resolution - PixelMap getPixelMap(int res) const; - /// update a PixelMap with the flux in pixels at a resolution of res times the original resolution. - /// The map must have precisely the right size and center to match or an exception will be thrown. - /// Constructing the map with PixelMap getPixelMap(int res) will insure that it does. - void getPixelMap(PixelMap &map) const; + /// returns a PixelMap with the flux in pixels at a resolution of res times the original resolution + PixelMap getPixelMap(int res) const; + /// update a PixelMap with the flux in pixels at a resolution of res times the original resolution. + /// The map must have precisely the right size and center to match or an exception will be thrown. + /// Constructing the map with PixelMap getPixelMap(int res) will insure that it does. + void getPixelMap(PixelMap &map) const; - /// returns the area (radians^2) of the region with negative magnification at resolution of fixed grid - PosType EinsteinArea() const; + /// returns the area (radians^2) of the region with negative magnification at resolution of fixed grid + PosType EinsteinArea() const; - /// flux weighted magnification with current surface brightness averaged on the image plane - PosType magnification() const; + /// flux weighted magnification with current surface brightness averaged on the image plane + PosType magnification() const; - Point_2d getCenter(){return center;} + Point_2d getCenter(){return center;} - Point * operator[](size_t i){return i_points + i;}; + Point * operator[](size_t i){return i_points + i;}; + GridMap(GridMap &&grid){ + *this = std::move(grid); + } + + GridMap & operator=(GridMap &&grid){ + Ngrid_init = grid.Ngrid_init; + Ngrid_init2 = grid.Ngrid_init2; + pointID = grid.pointID; + axisratio = grid.axisratio; + x_range = grid.x_range; + + i_points = grid.i_points; + grid.i_points = nullptr; + s_points = grid.s_points; + grid.s_points = nullptr; + + center = grid.center; + + return *this; + } + private: void xygridpoints(Point *points,double range,const double *center,long Ngrid ,short remove_center); /// one dimensional size of initial grid - const int Ngrid_init; + int Ngrid_init; int Ngrid_init2; unsigned long pointID; From c0e6171168e767eaf17743f4bb420421bb4bb06a Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Mon, 29 Apr 2019 20:17:30 +0200 Subject: [PATCH 132/227] Constructor SourceMultiShaplets. copy & assignment for LensHaloRealNSIE. --- AnalyticNSIE/source.cpp | 12 +++++++++++- TreeCode_link/find_crit.cpp | 2 +- TreeCode_link/tree_maintenance.cpp | 8 ++++---- include/MOKAlens.h | 4 +++- include/lens.h | 1 + include/lens_halos.h | 28 ++++++++++++++++++++++------ include/point.h | 11 +++++++++++ include/source.h | 2 -- include/sourceAnaGalaxy.h | 20 +++++++++++++------- 9 files changed, 66 insertions(+), 22 deletions(-) diff --git a/AnalyticNSIE/source.cpp b/AnalyticNSIE/source.cpp index 2ff53eac..25094959 100644 --- a/AnalyticNSIE/source.cpp +++ b/AnalyticNSIE/source.cpp @@ -738,6 +738,17 @@ SourceMultiShapelets::SourceMultiShapelets(InputParams& params) readCatalog(); } +SourceMultiShapelets::SourceMultiShapelets(std::string &my_shapelets_folder,Band my_band,double my_mag_limit,double my_sb_limit) +: Source(),index(0),mag_limit(my_mag_limit),band(my_band),shapelets_folder(my_shapelets_folder) +{ + + if(sb_limit == -1) + setSBlimit_magarcsec(30.); + else + sb_limit = pow(10,-0.4*(48.6+sb_limit))*pow(180*60*60/PI,2)/hplanck; + + readCatalog(); +} SourceMultiShapelets::~SourceMultiShapelets() { } @@ -777,7 +788,6 @@ void SourceMultiShapelets::assignParams(InputParams& params){ exit(1); } - if(!params.get("source_sb_limit",sb_limit)) setSBlimit_magarcsec(30.); else diff --git a/TreeCode_link/find_crit.cpp b/TreeCode_link/find_crit.cpp index f8e21411..02b8a95d 100644 --- a/TreeCode_link/find_crit.cpp +++ b/TreeCode_link/find_crit.cpp @@ -312,7 +312,7 @@ void ImageFinding::find_crit( if(TEST){ //********************* test lines **************************** - // This tests that every every radial or pseudo critical line is near at + // This tests that every radial or pseudo critical line is near at // least one negative mag point Kist nkist; for(auto &crit : crtcurve){ diff --git a/TreeCode_link/tree_maintenance.cpp b/TreeCode_link/tree_maintenance.cpp index e9e2b315..8d397881 100644 --- a/TreeCode_link/tree_maintenance.cpp +++ b/TreeCode_link/tree_maintenance.cpp @@ -886,10 +886,10 @@ void TreeStruct::_AddPoint(TreeStruct::iterator ¤t){ locateD(x-1,(*current)->npoints,xcut,&cut); } - assert(branch1->boundary_p1[0] < branch1->boundary_p2[0]); - assert(branch1->boundary_p1[1] < branch1->boundary_p2[1]); - assert(branch2->boundary_p1[0] < branch2->boundary_p2[0]); - assert(branch2->boundary_p1[1] < branch2->boundary_p2[1]); + assert(branch1->boundary_p1[0] <= branch1->boundary_p2[0]); + assert(branch1->boundary_p1[1] <= branch1->boundary_p2[1]); + assert(branch2->boundary_p1[0] <= branch2->boundary_p2[0]); + assert(branch2->boundary_p1[1] <= branch2->boundary_p2[1]); assert((*current)->boundary_p1[0] < (*current)->boundary_p2[0]); assert((*current)->boundary_p1[1] < (*current)->boundary_p2[1]); delete[] x; diff --git a/include/MOKAlens.h b/include/MOKAlens.h index f55f8e66..df0b01a9 100644 --- a/include/MOKAlens.h +++ b/include/MOKAlens.h @@ -203,7 +203,7 @@ class LensHaloMassMap : public LensHalo ,COSMOLOGY& lenscosmo /// cosmology ); - LensHaloMassMap(const LensHaloMassMap &h):cosmo(h.cosmo){ + LensHaloMassMap(const LensHaloMassMap &h):LensHalo(h),cosmo(h.cosmo){ // LensHaloMassMap(const LensHaloMassMap &h){ maptype = h.maptype; map = h.map; @@ -217,6 +217,7 @@ class LensHaloMassMap : public LensHalo LensHaloMassMap & operator=(LensHaloMassMap &h){ if(&h != this){ + LensHalo::operator=(h); //cosmo = h.cosmo; maptype = h.maptype; map = h.map; @@ -227,6 +228,7 @@ class LensHaloMassMap : public LensHalo } LensHaloMassMap & operator=(LensHaloMassMap &&h){ if(&h != this){ + LensHalo::operator=(h); //cosmo = h.cosmo; maptype = h.maptype; map = std::move(h.map); diff --git a/include/lens.h b/include/lens.h index 9638aeea..15818668 100644 --- a/include/lens.h +++ b/include/lens.h @@ -721,3 +721,4 @@ inline HaloType* Lens::getMainHalo(std::size_t i) typedef Lens* LensHndl; #endif /* MULTIPLANE_H_ */ + diff --git a/include/lens_halos.h b/include/lens_halos.h index 58d88340..0fb37d2d 100644 --- a/include/lens_halos.h +++ b/include/lens_halos.h @@ -945,18 +945,34 @@ class LensHaloPowerLaw: public LensHalo{ class LensHaloRealNSIE : public LensHalo{ public: - /*LensHaloRealNSIE(){ - sigma = zlens = fratio = pa = rcore = 0.; - }*/ - - //LensHaloRealNSIE(float my_mass,float my_Rsize,PosType my_zlens,float my_rscale,float my_sigma, float my_rcore,float my_fratio,float my_pa,int my_stars_N); - + /// explicit constructor, Warning: If my_rcore > 0.0 and my_fratio < 1 then the mass will be somewhat less than my_mass. LensHaloRealNSIE(float my_mass,PosType my_zlens,float my_sigma ,float my_rcore,float my_fratio,float my_pa,int my_stars_N); /// Warning: If my_rcore > 0.0 and my_fratio < 1 then the mass will be somewhat less than my_mass. LensHaloRealNSIE(InputParams& params); + + LensHaloRealNSIE(const LensHaloRealNSIE &h): + LensHalo(h) + { + ++objectCount; + sigma = h.sigma; + fratio = h.fratio; + pa = h.pa; + rcore = h.rcore; + } + + LensHaloRealNSIE &operator=(const LensHaloRealNSIE &h){ + if(&h == this) return *this; + LensHalo::operator=(h); + sigma = h.sigma; + fratio = h.fratio; + pa = h.pa; + rcore = h.rcore; + return *this; + } + ~LensHaloRealNSIE(); /// overridden function to calculate the lensing properties diff --git a/include/point.h b/include/point.h index 001da283..fed3fd36 100644 --- a/include/point.h +++ b/include/point.h @@ -129,6 +129,7 @@ struct Point_2d{ return x[0]*x[0] + x[1]*x[1]; } + // rotates the point void rotate(PosType theta){ PosType c = cos(theta),s = sin(theta); PosType tmp = x[0]; @@ -136,6 +137,16 @@ struct Point_2d{ x[1] = c*x[1] + s*tmp; } + /// returns a copy of the point that it rotated + Point_2d rotated(PosType theta) const{ + Point_2d p; + PosType c = cos(theta),s = sin(theta); + p[0] = c*x[0] - s*x[1]; + p[1] = c*x[1] + s*x[0]; + + return p; + } + /// rescale to make a unit length vector void unitize(){ PosType s = length(); diff --git a/include/source.h b/include/source.h index d54d2820..0777236c 100644 --- a/include/source.h +++ b/include/source.h @@ -45,8 +45,6 @@ class Source return *this; } - - // in lens.cpp // TODO: make SurfaceBrightness take a const double* /// Surface brightness of source in grid coordinates not source centered coordinates. diff --git a/include/sourceAnaGalaxy.h b/include/sourceAnaGalaxy.h index 95ee653c..f17e3f36 100644 --- a/include/sourceAnaGalaxy.h +++ b/include/sourceAnaGalaxy.h @@ -184,6 +184,9 @@ class SourceMultiShapelets: public Source{ public: SourceMultiShapelets(InputParams& params); + /// Reads in sources from a catalog. + SourceMultiShapelets(std::string &my_shapelets_folder,Band my_band,double my_mag_limit,double my_sb_limit = -1); + ~SourceMultiShapelets(); void sortInRedshift(); void sortInMag(); @@ -227,12 +230,16 @@ class SourceMultiShapelets: public Source{ /** The indexing operator can be used to change the "current" source that is returned when the surface brightness is subsequently * called. */ - SourceShapelets& operator[] (std::size_t i){ - if(i < galaxies.size()) - return galaxies[i]; - return galaxies[index]; - } - + SourceShapelets& operator[] (std::size_t i){ + if(i < galaxies.size()) + return galaxies[i]; + return galaxies[index]; + } + + SourceShapelets& back(){ + return galaxies.back(); + } + const SourceShapelets& operator[] (std::size_t i) const { if(i < galaxies.size()) return galaxies[i]; @@ -265,7 +272,6 @@ class SourceMultiShapelets: public Source{ void readCatalog(); std::string shapelets_folder; - }; bool redshiftcompare_shap(SourceShapelets s1,SourceShapelets s2); From 658156bb06767e43139b985a1dd45c1a031d7f97 Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Thu, 2 May 2019 15:13:32 +0200 Subject: [PATCH 133/227] Cosmetic --- MultiPlane/lens.cpp | 2 +- Source/overzier.cpp | 3 ++- include/overzier_source.h | 4 +++- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/MultiPlane/lens.cpp b/MultiPlane/lens.cpp index 9e17d623..0130206d 100644 --- a/MultiPlane/lens.cpp +++ b/MultiPlane/lens.cpp @@ -83,7 +83,7 @@ Lens::Lens(long* my_seed,PosType z_source, const COSMOLOGY &cosmoset,bool verbos //flag_switch_lensing_off = false; PosType ztmp = zsource; - combinePlanes(true); + combinePlanes(verbose); if(zsource != ztmp) ResetSourcePlane(ztmp,false); //std::cout << "number of field halos :" << field_halos.size() << std::endl; } diff --git a/Source/overzier.cpp b/Source/overzier.cpp index 86f05a5b..ddc8de15 100644 --- a/Source/overzier.cpp +++ b/Source/overzier.cpp @@ -184,7 +184,8 @@ SourceOverzier(my_mag,my_mag_bulge,my_Reff,my_Rh,my_PA,inclination,my_id,my_z,th //double index = 4*pow(MAX(getBtoT(),0.03),0.4)*pow(10,0.2*(ran()-0.5)); double index = ran() + 3.5 ; - double q = 1 + (0.5-1)*ran(); + + double q = 1 - 0.5*ran(); spheroid.setSersicIndex(index); //spheroid = new SourceSersic(my_mag_bulge,my_Reff,-my_PA + 10*(ran() - 0.5)*PI/180,index,q,my_z,theta); diff --git a/include/overzier_source.h b/include/overzier_source.h index c4d4c292..810ce962 100644 --- a/include/overzier_source.h +++ b/include/overzier_source.h @@ -75,7 +75,9 @@ class SourceOverzier : public Source /// the bulge to total flux ratio PosType getBtoT() const { return pow(10,(-current.mag_bulge + current.mag)/2.5); } + /// position angle in radians PosType getPA() const { return current.PA; } + /// inclination in radians PosType getInclination() const { return current.inclination;} float getSEDtype() const {return sedtype;} void setSEDtype(float s){ sedtype = s;} @@ -125,7 +127,7 @@ class SourceOverzier : public Source std::cout << "Reff :" << Reff/arcsecTOradians << " arcsec "; std::cout << "Rh :" << Rh/arcsecTOradians << " arcsec "; std::cout << "PA :" << PA << " "; - std::cout << "inclination :" << inclination << " "; + std::cout << "inclination :" << inclination << " radians"; std::cout << "sbDo :" << sbDo << " "; std::cout << "sbSo :" << sbSo << " "; std::cout << "mag :" << mag << " "; From 33afce0b438650597a8315e20ed7ce76b105c06b Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Sun, 5 May 2019 10:20:40 +0200 Subject: [PATCH 134/227] Add LensHaloDisk class. --- MultiPlane/lens_halos.cpp | 2 +- include/CMakeLists.txt | 1 + include/disk.h | 111 ++++++++++++++++++++++++++++++++++++++ include/geometry.h | 7 ++- include/lens_halos.h | 4 +- include/particle_halo.h | 13 +++-- 6 files changed, 131 insertions(+), 7 deletions(-) create mode 100644 include/disk.h diff --git a/MultiPlane/lens_halos.cpp b/MultiPlane/lens_halos.cpp index 44d98c37..791da6df 100644 --- a/MultiPlane/lens_halos.cpp +++ b/MultiPlane/lens_halos.cpp @@ -20,7 +20,7 @@ LensHalo::LensHalo(){ zlens = 0.0; } -LensHalo::LensHalo(PosType z,COSMOLOGY &cosmo){ +LensHalo::LensHalo(PosType z,const COSMOLOGY &cosmo){ rscale = 1.0; mass = Rsize = Rmax = xmax = posHalo[0] = posHalo[1] = 0.0; stars_implanted = false; diff --git a/include/CMakeLists.txt b/include/CMakeLists.txt index 76729d3f..1a08ddd1 100644 --- a/include/CMakeLists.txt +++ b/include/CMakeLists.txt @@ -51,4 +51,5 @@ add_sources( gaussian.h gadget.hh particle_types.h + disk.h ) diff --git a/include/disk.h b/include/disk.h new file mode 100644 index 00000000..f40652c2 --- /dev/null +++ b/include/disk.h @@ -0,0 +1,111 @@ +#include "particle_halo.h" + +using Utilities::Geometry::Quaternion; + +template +class LensHaloDisk:public LensHaloParticles > { + + LensHaloDisk(double mass + ,double disk_scale /// scale length in Mpc + ,double Rperp /// vertical scale length in Mpc + ,double mass_res + ,float my_inclination /// disk inclination (radians) + ,float my_PA /// position angle (radians) + ,Utilities::RandomNumbers_NR &ran + ,float redshift + ,const COSMOLOGY &cosmo + ,int Nsmooth = 64 + ): + LensHaloParticles >(redshift,cosmo),qrot_invers(1,0,0,0), + Rscale(disk_scale),Rhight(Rperp),PA(my_PA),inclination(my_inclination) + { + + /// set up base Lenshalo + size_t N = (size_t)(mass/mass_res + 1); + particles.resize(N); + + double r,theta; + + size_t i = 0; + for(auto &p : particles){ + r = -Rscale * log(1 - (float)(i) / N ); + theta = 2*PI*ran(); + + p.x[0] = r*cos(theta); + p.x[1] = r*sin(theta); + p.x[2] = -Rhight * log( 1 - ran() ); + p.x[2] *= 2*(int)(2*ran()) - 1; // random sign + + p.Mass = mass_res; + ++i; + } + + Rmax = -3 * Rscale * log(1 - (float)(N-1) / N ); + LensHalo::setRsize( Rmax ); + + LensHaloParticles >::calculate_smoothing(Nsmooth,particles.data(),particles.size()); + + // rotate particles to required inclination and position angle + Quaternion R = Quaternion::q_z_rotation(PA)*Quaternion::q_x_rotation(inclination); + rotate_all(R); + qrot_invers = R.conj(); + + mcenter *= 0.0; + LensHalo::setMass(mass); + + qtree = new TreeQuadParticles >(particles.data(),particles.size(),-1,-1,0,20); + } + + /// Reorient the disk + void reorient(float my_inclination,float my_PA){ + inclination = my_inclination; + PA=my_PA; + + Quaternion R = Quaternion::q_z_rotation(PA)*Quaternion::q_x_rotation(inclination)*qrot_invers; + rotate_all(R); + qrot_invers = Quaternion::q_x_rotation(-inclination)*Quaternion::q_z_rotation(-PA); + + // reconstruct LensHaloParticles base class + delete qtree; + qtree = new TreeQuadParticles >(particles.data(),particles.size(),-1,-1,0,20); + } + + LensHaloDisk(LensHaloDisk &&h):LensHaloParticles >(std::move(h)){ + *this = std::move(h); + } + + LensHaloDisk & operator=(LensHaloDisk &&h){ + LensHaloParticles >::operator=(std::move(h)); + particles=std::move(h.particles); + + qrot_invers = h.qrot_invers; // rotation that brings the disk back to face on + Rscale = h.Rscale; + Rhight = h.Rhight; + PA = h.PA; + inclination = h.inclination; + + return *this; + } + +private: + + void rotate_all(Quaternion &R){ + Quaternion q(1,0,0,0); + for(auto &p : particles){ + q[0] = 0; q[1] = p[0]; q[2] = p[1]; q[3] = p[2]; + q.Rotate(R); + p[0] = q[1]; p[1] = q[2]; p[2] = q[3]; + } + } + + + Utilities::Geometry::Quaternion qrot_invers; // rotation that brings the disk back to face on + double Rscale; + double Rhight; + double PA; + double inclination; + + std::vector > particles; +}; + + diff --git a/include/geometry.h b/include/geometry.h index a77e3476..0fdfa131 100644 --- a/include/geometry.h +++ b/include/geometry.h @@ -88,13 +88,16 @@ namespace Utilities { class Quaternion{ public: + Quaternion(){ + v[0] = v[1] = v[2] = v[3] = 0; + } Quaternion(double s,double x,double y,double z){ v[0] = s; v[1] = x; v[2] = y; v[3] = z; } - Quaternion(const Quaternion &q){ + Quaternion(const Quaternion &q){ v[0] = q.v[0]; v[1] = q.v[1]; v[2] = q.v[2]; @@ -246,6 +249,8 @@ namespace Utilities { /// the components of the Quaternion double v[4]; + /// components, 0,1,2,3 + double & operator[](int i){return v[i];} /// returns the rotation Quaternion for a rotation around the x-axis static Quaternion q_x_rotation(double theta){ diff --git a/include/lens_halos.h b/include/lens_halos.h index 0fb37d2d..f439266a 100644 --- a/include/lens_halos.h +++ b/include/lens_halos.h @@ -43,7 +43,7 @@ class LensHalo{ public: LensHalo(); - LensHalo(PosType z,COSMOLOGY &cosmo); + LensHalo(PosType z,const COSMOLOGY &cosmo); LensHalo(InputParams& params,COSMOLOGY &cosmo,bool needRsize = true); LensHalo(InputParams& params,bool needRsize = true); LensHalo(const LensHalo &h); @@ -214,7 +214,7 @@ class LensHalo{ PosType posHalo[2]; PosType zlens; float mass; - // This is the size of the halo beyond which it does not have the profile expected profile. + // This is the size of the halo beyond which it does not have the expected profile. float Rsize = 0; protected: diff --git a/include/particle_halo.h b/include/particle_halo.h index e6959447..8149bfa1 100644 --- a/include/particle_halo.h +++ b/include/particle_halo.h @@ -58,7 +58,7 @@ class LensHaloParticles : public LensHalo ,bool recenter /// center on center of mass ,float MinPSize /// minimum particle size ); - + ~LensHaloParticles(); void force_halo(double *alpha,KappaType *kappa,KappaType *gamma,KappaType *phi,double const *xcm @@ -107,8 +107,15 @@ class LensHaloParticles : public LensHalo ,int Nsmooth ,PosType min_size); -private: +protected: + // constructure for derived classes + LensHaloParticles(float redshift /// redshift of origin + ,const COSMOLOGY& cosmo /// cosmology + ): LensHalo(redshift,cosmo){} +protected: + + Point_3d mcenter; void rotate_particles(PosType theta_x,PosType theta_y); @@ -116,6 +123,7 @@ class LensHaloParticles : public LensHalo void assignParams(InputParams& params); + PType *pp; //PosType **xp; //std::vector masses; @@ -866,5 +874,4 @@ class MakeParticleLenses{ }; - #endif From 7c94c708be3a1a1da75afd4d2a2bda680486ae34 Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Tue, 7 May 2019 18:12:08 +0200 Subject: [PATCH 135/227] Fixed some things in LensHaloParticle<> that were causing compiler errors. --- include/disk.h | 56 ++++++++++++++++++++++++++++++++------------------ 1 file changed, 36 insertions(+), 20 deletions(-) diff --git a/include/disk.h b/include/disk.h index f40652c2..f1ab5b58 100644 --- a/include/disk.h +++ b/include/disk.h @@ -1,15 +1,19 @@ #include "particle_halo.h" +#ifndef GLAMER_disk_halo_h +#define GLAMER_disk_halo_h + using Utilities::Geometry::Quaternion; template class LensHaloDisk:public LensHaloParticles > { +public: LensHaloDisk(double mass ,double disk_scale /// scale length in Mpc ,double Rperp /// vertical scale length in Mpc ,double mass_res - ,float my_inclination /// disk inclination (radians) + ,float my_inclination /// disk inclination, 0 is face on (radians) ,float my_PA /// position angle (radians) ,Utilities::RandomNumbers_NR &ran ,float redshift @@ -40,8 +44,8 @@ class LensHaloDisk:public LensHaloParticles > { ++i; } - Rmax = -3 * Rscale * log(1 - (float)(N-1) / N ); - LensHalo::setRsize( Rmax ); + LensHalo::Rmax = -3 * Rscale * log(1 - (float)(N-1) / N ); + LensHalo::setRsize( LensHalo::Rmax ); LensHaloParticles >::calculate_smoothing(Nsmooth,particles.data(),particles.size()); @@ -50,28 +54,24 @@ class LensHaloDisk:public LensHaloParticles > { rotate_all(R); qrot_invers = R.conj(); - mcenter *= 0.0; + LensHaloParticles >::mcenter *= 0.0; LensHalo::setMass(mass); - qtree = new TreeQuadParticles >(particles.data(),particles.size(),-1,-1,0,20); + LensHaloParticles >::qtree = new TreeQuadParticles >(particles.data(),particles.size(),-1,-1,0,20); } - /// Reorient the disk - void reorient(float my_inclination,float my_PA){ - inclination = my_inclination; - PA=my_PA; + LensHaloDisk(LensHaloDisk &&h):LensHaloParticles >(std::move(h)){ + particles=std::move(h.particles); - Quaternion R = Quaternion::q_z_rotation(PA)*Quaternion::q_x_rotation(inclination)*qrot_invers; - rotate_all(R); - qrot_invers = Quaternion::q_x_rotation(-inclination)*Quaternion::q_z_rotation(-PA); + //??? This and the move assignment operator depends on the move operator + // for the std::vector keeping the pointers valid. I think this is always + // the case but it is not required by the C++ standard. - // reconstruct LensHaloParticles base class - delete qtree; - qtree = new TreeQuadParticles >(particles.data(),particles.size(),-1,-1,0,20); - } - - LensHaloDisk(LensHaloDisk &&h):LensHaloParticles >(std::move(h)){ - *this = std::move(h); + qrot_invers = h.qrot_invers; // rotation that brings the disk back to face on + Rscale = h.Rscale; + Rhight = h.Rhight; + PA = h.PA; + inclination = h.inclination; } LensHaloDisk & operator=(LensHaloDisk &&h){ @@ -86,6 +86,22 @@ class LensHaloDisk:public LensHaloParticles > { return *this; } + + + /// Reorient the disk + void reorient(float my_inclination,float my_PA){ + inclination = my_inclination; + PA=my_PA; + + Quaternion R = Quaternion::q_z_rotation(PA)*Quaternion::q_x_rotation(inclination)*qrot_invers; + rotate_all(R); + qrot_invers = Quaternion::q_x_rotation(-inclination)*Quaternion::q_z_rotation(-PA); + + // reconstruct LensHaloParticles base class + delete LensHaloParticles >::qtree; + LensHaloParticles >::qtree = new TreeQuadParticles >(particles.data(),particles.size(),-1,-1,0,20); + } + private: @@ -108,4 +124,4 @@ class LensHaloDisk:public LensHaloParticles > { std::vector > particles; }; - +#endif From 9cc626cfd344694e835339f846d88a669b8e271d Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Mon, 13 May 2019 12:23:07 +0200 Subject: [PATCH 136/227] Fixed problem with LensHalDisk's PA. Added Lens::moveinMainHalo() to avoid copying complicated LensHalo's, in particular LensHaloParticles. --- AnalyticNSIE/readfiles_uni.cpp | 27 +++--- Source/overzier.cpp | 5 +- include/disk.h | 154 +++++++++++++++++++-------------- include/geometry.h | 13 ++- include/lens.h | 22 +++++ include/overzier_source.h | 14 ++- include/particle_halo.h | 118 +++++++++++++++++-------- include/uniform_lens.h | 2 +- 8 files changed, 232 insertions(+), 123 deletions(-) diff --git a/AnalyticNSIE/readfiles_uni.cpp b/AnalyticNSIE/readfiles_uni.cpp index ba271d6a..9edcf355 100644 --- a/AnalyticNSIE/readfiles_uni.cpp +++ b/AnalyticNSIE/readfiles_uni.cpp @@ -76,11 +76,13 @@ LensHaloUniform::~LensHaloUniform(){ void LensHaloUniform::setCosmology(const COSMOLOGY& cosmo) { PosType zlens = LensHalo::getZlens(); - Dl = cosmo.angDist(0,zlens); // In comoving Mpc - Ds = cosmo.angDist(0,zsource_reference); - Dls = cosmo.angDist(zlens,zsource_reference); - SigmaCrit = Ds/Dl/Dls/(4*PI*Grav); + //Dl = cosmo.angDist(0,zlens); // In comoving Mpc + //Ds = cosmo.angDist(0,zsource_reference); + //Dls = cosmo.angDist(zlens,zsource_reference); + //SigmaCrit = Ds/Dl/Dls/(4*PI*Grav); + SigmaCrit = cosmo.SigmaCrit(zlens,zsource_reference); + Sigma_uniform = kappa_uniform*SigmaCrit; gammaCrit_uniform[0] = gamma_uniform[0]*SigmaCrit; gammaCrit_uniform[1] = gamma_uniform[1]*SigmaCrit; @@ -100,19 +102,20 @@ void LensHaloUniform::force_halo( ,PosType screening ) { - PosType alpha_tmp[2]; - KappaType gamma_tmp[3], phi_tmp; - - gamma_tmp[0] = gamma_tmp[1] = gamma_tmp[2] = 0.0; - alpha_tmp[0] = alpha_tmp[1] = 0.0; - phi_tmp = 0.0; + //PosType alpha_tmp[2]; + //KappaType gamma_tmp[3], phi_tmp; - *kappa += lens_expand(perturb_modes,xcm,alpha,gamma,&phi_tmp); + //gamma_tmp[0] = gamma_tmp[1] = gamma_tmp[2] = 0.0; + //alpha_tmp[0] = alpha_tmp[1] = 0.0; + //phi_tmp = 0.0; + //*kappa += lens_expand(perturb_modes,xcm,alpha,gamma,&phi_tmp); + *kappa += lens_expand(perturb_modes,xcm,alpha,gamma,phi); + alpha[0] *= -1; alpha[1] *= -1; - *phi += phi_tmp ; + //*phi += phi_tmp ; // add stars for microlensing if(stars_N > 0 && stars_implanted) diff --git a/Source/overzier.cpp b/Source/overzier.cpp index ddc8de15..d547345f 100644 --- a/Source/overzier.cpp +++ b/Source/overzier.cpp @@ -169,6 +169,7 @@ void SourceOverzier::renormalize_current(){ SourceOverzierPlus::SourceOverzierPlus(PosType my_mag,PosType my_mag_bulge,PosType my_Reff,PosType my_Rh,PosType my_PA,PosType inclination,unsigned long my_id,PosType my_z,const PosType *theta,Utilities::RandomNumbers_NR &ran): SourceOverzier(my_mag,my_mag_bulge,my_Reff,my_Rh,my_PA,inclination,my_id,my_z,theta) { + assert(my_mag_bulge >= my_mag); //std::cout << "SourceOverzierPlus constructor" << std::endl; original = current; @@ -365,9 +366,7 @@ void SourceOverzierPlus::randomize(Utilities::RandomNumbers_NR &ran){ current.mag += tmp; current.PA = PI*ran(); - current.inclination = 1.0*PI/2*ran(); - if(cos(current.inclination)< 0.25) current.inclination = acos(0.25); - + current.inclination = MIN(ran()*PI/2,0.95*PI/2); cospa = cos(current.PA); sinpa = sin(current.PA); diff --git a/include/disk.h b/include/disk.h index f1ab5b58..d9712b93 100644 --- a/include/disk.h +++ b/include/disk.h @@ -9,79 +9,37 @@ template class LensHaloDisk:public LensHaloParticles > { public: - LensHaloDisk(double mass - ,double disk_scale /// scale length in Mpc - ,double Rperp /// vertical scale length in Mpc - ,double mass_res - ,float my_inclination /// disk inclination, 0 is face on (radians) - ,float my_PA /// position angle (radians) - ,Utilities::RandomNumbers_NR &ran + LensHaloDisk( + double mass /// mass of disk + ,double disk_scale /// scale hight of disk + ,double Rperp /// vertical scale hight of disk + ,double mass_res /// mass resolution, mass of particles + ,float my_PA /// position angle in radians + ,float my_inclination /// inclination of disk in radians, 0 is face on + ,Utilities::RandomNumbers_NR &ran /// random number generator ,float redshift ,const COSMOLOGY &cosmo - ,int Nsmooth = 64 - ): - LensHaloParticles >(redshift,cosmo),qrot_invers(1,0,0,0), - Rscale(disk_scale),Rhight(Rperp),PA(my_PA),inclination(my_inclination) - { - - /// set up base Lenshalo - size_t N = (size_t)(mass/mass_res + 1); - particles.resize(N); - - double r,theta; - - size_t i = 0; - for(auto &p : particles){ - r = -Rscale * log(1 - (float)(i) / N ); - theta = 2*PI*ran(); - - p.x[0] = r*cos(theta); - p.x[1] = r*sin(theta); - p.x[2] = -Rhight * log( 1 - ran() ); - p.x[2] *= 2*(int)(2*ran()) - 1; // random sign - - p.Mass = mass_res; - ++i; - } - - LensHalo::Rmax = -3 * Rscale * log(1 - (float)(N-1) / N ); - LensHalo::setRsize( LensHalo::Rmax ); - - LensHaloParticles >::calculate_smoothing(Nsmooth,particles.data(),particles.size()); - - // rotate particles to required inclination and position angle - Quaternion R = Quaternion::q_z_rotation(PA)*Quaternion::q_x_rotation(inclination); - rotate_all(R); - qrot_invers = R.conj(); - - LensHaloParticles >::mcenter *= 0.0; - LensHalo::setMass(mass); - - LensHaloParticles >::qtree = new TreeQuadParticles >(particles.data(),particles.size(),-1,-1,0,20); - } + ,int Nsmooth = 64 /// number of neighbors used in smoothing + ); - LensHaloDisk(LensHaloDisk &&h):LensHaloParticles >(std::move(h)){ - particles=std::move(h.particles); - - //??? This and the move assignment operator depends on the move operator - // for the std::vector keeping the pointers valid. I think this is always - // the case but it is not required by the C++ standard. - + LensHaloDisk(LensHaloDisk &&h): + LensHaloParticles >(std::move(h)), + particles(LensHaloParticles >::trash_collector) + { qrot_invers = h.qrot_invers; // rotation that brings the disk back to face on Rscale = h.Rscale; Rhight = h.Rhight; - PA = h.PA; + zpa = h.zpa; inclination = h.inclination; } LensHaloDisk & operator=(LensHaloDisk &&h){ LensHaloParticles >::operator=(std::move(h)); - particles=std::move(h.particles); qrot_invers = h.qrot_invers; // rotation that brings the disk back to face on Rscale = h.Rscale; Rhight = h.Rhight; - PA = h.PA; + zpa = h.zpa; inclination = h.inclination; return *this; @@ -91,37 +49,99 @@ class LensHaloDisk:public LensHaloParticles > { /// Reorient the disk void reorient(float my_inclination,float my_PA){ inclination = my_inclination; - PA=my_PA; + zpa = -my_PA; - Quaternion R = Quaternion::q_z_rotation(PA)*Quaternion::q_x_rotation(inclination)*qrot_invers; + Quaternion R = Quaternion::q_z_rotation(zpa)*Quaternion::q_x_rotation(inclination)*qrot_invers; rotate_all(R); - qrot_invers = Quaternion::q_x_rotation(-inclination)*Quaternion::q_z_rotation(-PA); + qrot_invers = Quaternion::q_x_rotation(-inclination)*Quaternion::q_z_rotation(-zpa); // reconstruct LensHaloParticles base class delete LensHaloParticles >::qtree; LensHaloParticles >::qtree = new TreeQuadParticles >(particles.data(),particles.size(),-1,-1,0,20); } - - + /// inclination in radians, 0 is face on + float getInclination(){return inclination;} + /// postion angle in radians, + float getPA(){return -zpa;} + private: + std::vector > &particles; + void rotate_all(Quaternion &R){ Quaternion q(1,0,0,0); for(auto &p : particles){ q[0] = 0; q[1] = p[0]; q[2] = p[1]; q[3] = p[2]; - q.Rotate(R); + q.RotInplace(R); p[0] = q[1]; p[1] = q[2]; p[2] = q[3]; } } - Utilities::Geometry::Quaternion qrot_invers; // rotation that brings the disk back to face on double Rscale; double Rhight; - double PA; + double zpa; double inclination; +}; + +template +LensHaloDisk::LensHaloDisk( + double mass + ,double disk_scale + ,double Rperp + ,double mass_res + ,float my_PA + ,float my_inclination + ,Utilities::RandomNumbers_NR &ran + ,float redshift + ,const COSMOLOGY &cosmo + ,int Nsmooth + ): +LensHaloParticles >(redshift,cosmo), +particles(LensHaloParticles >::trash_collector), +qrot_invers(1,0,0,0),Rscale(disk_scale),Rhight(Rperp),zpa(-my_PA),inclination(my_inclination) +{ + + /// set up base Lenshalo + size_t N = (size_t)(mass/mass_res + 1); + particles.resize(N); + LensHaloParticles >::pp = particles.data(); + LensHaloParticles >::Npoints = particles.size(); + + double r,theta; + + size_t i = 0; + for(auto &p : particles){ + r = -Rscale * log(1 - (float)(i) / N ); + theta = 2*PI*ran(); + + p.x[0] = r*cos(theta); + p.x[1] = r*sin(theta); + p.x[2] = -Rhight * log( 1 - ran() ); + p.x[2] *= 2*(int)(2*ran()) - 1; // random sign + + p.Mass = mass_res; + ++i; + } + + LensHalo::Rmax = -3 * Rscale * log(1 - (float)(N-1) / N ); + LensHalo::setRsize( LensHalo::Rmax ); + + LensHaloParticles >::calculate_smoothing(Nsmooth,particles.data(),particles.size()); + + // rotate particles to required inclination and position angle + Quaternion R = Quaternion::q_z_rotation(zpa)*Quaternion::q_x_rotation(inclination); + rotate_all(R); + qrot_invers = R.conj(); + + LensHaloParticles >::mcenter *= 0.0; + LensHalo::setMass(mass); + + LensHaloParticles >::min_size=0; + LensHaloParticles >::multimass=false; - std::vector > particles; + Point_2d no_rotation; + LensHaloParticles >::set_up(redshift,cosmo,no_rotation,false); }; #endif diff --git a/include/geometry.h b/include/geometry.h index 0fdfa131..40d546a3 100644 --- a/include/geometry.h +++ b/include/geometry.h @@ -74,6 +74,9 @@ namespace Utilities { // apply the rotation q = q.Rotate(R); + // rotate in place, same as above but with one less copy + q.RotInplace(R); + // convert to a Point_3d Point_3d p = q.to_point_3d(); @@ -179,18 +182,24 @@ namespace Utilities { return R*(*this)*R.conj(); } + /** returns the Quaternion rotated with the rotation Quaternion R. + It is assumed that R has norm = 1, ie ||R|| = 1 */ + void RotInplace(const Quaternion &R){ + *this = R*(*this)*R.conj(); + } + /// rotate a Point_3d using a rotation Quaternion static Point_3d Rotate(const Point_3d &p,const Quaternion &R){ Quaternion q(p); - q.Rotate(R); + q.RotInplace(R); return q.to_point_3d(); } /// rotate a SpericalPoint using a rotation Quaternion static SphericalPoint Rotate(const SphericalPoint &p,const Quaternion &R){ Quaternion q(p); - q.Rotate(R); + q.RotInplace(R); return q.to_SpericalPoint(); } diff --git a/include/lens.h b/include/lens.h index 15818668..a494b101 100644 --- a/include/lens.h +++ b/include/lens.h @@ -164,6 +164,28 @@ class Lens{ combinePlanes(verbose); } + /** \brief This has the same effect as insertMainHalo(), but the halo is not + copied, it is moved. + + The Lens will take possession of the halo and will destroy it when it is destroyed. + This is to avoid copying halos that take up a lot of memory and require + a lot of time to copy like LensHaloParticles(). + **/ + template + void moveinMainHalo(T &halo_in, bool addplanes,bool verbose=false) + { + T * halo = new T(std::move(halo_in)); + halo->setCosmology(cosmo); + main_halos.push_back(halo); + + flag_switch_main_halo_on = true; + + if(addplanes) addMainHaloToPlane(halo); + else addMainHaloToNearestPlane(halo); + + combinePlanes(verbose); + } + /** * \brief Inserts a single main lens halo and deletes all previous ones. * Then all lensing planes are updated accordingly. diff --git a/include/overzier_source.h b/include/overzier_source.h index 810ce962..23d9d9f5 100644 --- a/include/overzier_source.h +++ b/include/overzier_source.h @@ -145,7 +145,19 @@ class SourceOverzierPlus : public SourceOverzier { public: //SourceOverzierPlus(); - SourceOverzierPlus(PosType mag,PosType BtoT,PosType Reff,PosType Rh,PosType PA,PosType inclination,unsigned long my_id,PosType my_z,const PosType *theta,Utilities::RandomNumbers_NR &ran); + SourceOverzierPlus( + PosType my_mag /// total magnitude + ,PosType my_mag_bulge /// magnitude of bulge + ,PosType my_Reff /// effective radius of bulge + ,PosType my_Rh /// scale hight of disk + ,PosType my_PA /// position angle + ,PosType inclination /// inclination + ,unsigned long my_id + ,PosType my_z + ,const PosType *theta + ,Utilities::RandomNumbers_NR &ran + ); + ~SourceOverzierPlus(); SourceOverzierPlus(const SourceOverzierPlus &p); diff --git a/include/particle_halo.h b/include/particle_halo.h index 8149bfa1..732192dd 100644 --- a/include/particle_halo.h +++ b/include/particle_halo.h @@ -50,17 +50,41 @@ class LensHaloParticles : public LensHalo ,PosType MinPSize /// minimum particle size ); - LensHaloParticles(PType *pdata /// list of particles pdata[][i] should be the position in physical Mpc + LensHaloParticles(std::vector &pvector /// list of particles pdata[][i] should be the position in physical Mpc, the class takes possession of the data and leaves the vector empty ,size_t Npoints /// redshift of origin ,float redshift /// redshift of origin ,const COSMOLOGY& cosmo /// cosmology ,Point_2d theta_rotate /// rotation of particles around the origin ,bool recenter /// center on center of mass ,float MinPSize /// minimum particle size - ); - + ):min_size(MinPSize),multimass(true) + { + std::swap(pvector,trash_collector); + pp = trash_collector.data(); + Npoints = trash_collector.size(); + set_up(redshift,cosmo,theta_rotate,recenter); + } ~LensHaloParticles(); + LensHaloParticles(LensHaloParticles &&h):LensHalo(std::move(h)){ + mcenter = h.mcenter; + trash_collector =std::move(h.trash_collector); + pp = h.pp; // note: this depends on std:move keeping the pointer valid if it is constructed with the public constructor + h.pp = nullptr; + + min_size = h.min_size; + multimass = h.multimass; + center = h.center; + Npoints = h.Npoints; + simfile = h.simfile; + sizefile = h.sizefile; + + qtree = h.qtree; + h.qtree = nullptr; + } + LensHaloParticles & operator=(LensHaloParticles &&h); + + void force_halo(double *alpha,KappaType *kappa,KappaType *gamma,KappaType *phi,double const *xcm ,bool subtract_point=false,PosType screening = 1.0); @@ -90,10 +114,7 @@ class LensHaloParticles : public LensHalo ,PType *pp ,size_t Npoints); - static void readPositionFileASCII(const std::string &filename - ,bool multimass - ,PType *pp - ); + void readPositionFileASCII(const std::string &filename); static void writeSizes(const std::string &filename ,int Nsmooth @@ -107,27 +128,35 @@ class LensHaloParticles : public LensHalo ,int Nsmooth ,PosType min_size); + friend class MakeParticleLenses; + protected: // constructure for derived classes LensHaloParticles(float redshift /// redshift of origin ,const COSMOLOGY& cosmo /// cosmology ): LensHalo(redshift,cosmo){} -protected: + // This constructor is really only for use by MakeParticleLenses. It does not take + // possession of the data and so they will not be deleted on destruction + LensHaloParticles(PType *pdata /// particle data (all physical distances) + ,size_t Nparticles + ,float redshift /// redshift of origin + ,const COSMOLOGY& cosmo /// cosmology + ,Point_2d theta_rotate /// rotation of particles around the origin + ,bool recenter /// center on center of mass + ,float MinPSize /// minimum particle size + ):pp(pdata),min_size(MinPSize),multimass(true),Npoints(Nparticles) + { + set_up(redshift,cosmo,theta_rotate,recenter); + } - - Point_3d mcenter; void rotate_particles(PosType theta_x,PosType theta_y); - static void smooth_(TreeSimple *tree3d,PType *xp,size_t N,int Nsmooth); - void assignParams(InputParams& params); - + Point_3d mcenter; PType *pp; - //PosType **xp; - //std::vector masses; - //std::vector sizes; + std::vector trash_collector; PosType min_size; bool multimass; @@ -140,6 +169,7 @@ class LensHaloParticles : public LensHalo std::string sizefile; TreeQuadParticles * qtree; + void set_up(float redshift,const COSMOLOGY& cosmo,Point_2d theta_rotate,bool recenter); }; template @@ -168,10 +198,10 @@ LensHaloParticles::LensHaloParticles(const std::string& simulation_filena switch (format) { case ascii: - readPositionFileASCII(simulation_filename,multimass,pp); + readPositionFileASCII(simulation_filename); break; default: - std::cerr << "LensHaloParticles does not accept ." << std::endl; + std::cerr << "LensHaloParticles does not accept format of particle data file." << std::endl; throw std::invalid_argument("bad format"); } @@ -234,17 +264,13 @@ LensHaloParticles::LensHaloParticles(const std::string& simulation_filena } template -LensHaloParticles::LensHaloParticles( - PType *pdata /// particle data (all physical distances) - ,size_t Nparticles - ,float redshift /// redshift of origin - ,const COSMOLOGY& cosmo /// cosmology - ,Point_2d theta_rotate /// rotation of particles around the origin - ,bool recenter /// center on center of mass - ,float MinPSize /// minimum particle size -):pp(pdata),min_size(MinPSize),multimass(true),Npoints(Nparticles) -{ - +void LensHaloParticles::set_up( + float redshift /// redshift of origin + ,const COSMOLOGY& cosmo /// cosmology + ,Point_2d theta_rotate /// rotation of particles around the origin + ,bool recenter /// center on center of mass +){ + LensHalo::setZlens(redshift); LensHalo::setCosmology(cosmo); LensHalo::set_flag_elliptical(false); @@ -260,10 +286,7 @@ LensHaloParticles::LensHaloParticles( mcenter *= 0.0; PosType max_mass = 0.0,min_mass = HUGE_VALF,mass=0; for(size_t i=0;i::LensHaloParticles( std::cout << " Particle mass range : " << min_mass << " to " << max_mass << " ratio of : " << max_mass/min_mass << std::endl; - if(recenter){ PosType r2,r2max=0; for(size_t i=0;i::rotate(Point_2d theta){ */ template void LensHaloParticles::readPositionFileASCII(const std::string &filename - ,bool multimass - ,PType *pp ){ int ncoll = Utilities::IO::CountColumns(filename); @@ -385,7 +405,8 @@ void LensHaloParticles::readPositionFileASCII(const std::string &filename throw std::runtime_error("file reading error"); } - pp = new PType[Npoints]; + trash_collector.resize(Npoints); // this is here just to make sure this is deleted + pp = trash_collector.data(); //xp = Utilities::PosTypeMatrix(Npoints,3); //if(multimass) masses.resize(Npoints); //else masses.push_back(tmp_mass); @@ -664,6 +685,29 @@ void LensHaloParticles::makeSIE( datafile.close(); } +template +LensHaloParticles & LensHaloParticles::operator=(LensHaloParticles &&h){ + if(this == &h) return *this; + LensHalo::operator=(std::move(h)); + mcenter = h.mcenter; + trash_collector =std::move(h.trash_collector); + pp = h.pp; // note: this depends on std:move keeping the pointer valid if it is constructed with the public constructor + h.pp = nullptr; + + min_size = h.min_size; + multimass = h.multimass; + center = h.center; + Npoints = h.Npoints; + simfile = h.simfile; + sizefile = h.sizefile; + + qtree = h.qtree; + h.qtree = nullptr; + + return *this; +} + + /** \brief A class for constructing LensHalos from particles in a data file.

diff --git a/include/uniform_lens.h b/include/uniform_lens.h index 76373223..562d16ca 100644 --- a/include/uniform_lens.h +++ b/include/uniform_lens.h @@ -74,7 +74,7 @@ class LensHaloUniform: public LensHalo{ float* getGammaCrit_uniform(){return gammaCrit_uniform;} /// Unitless convergence float getKappa_uniform() const {return Sigma_uniform/SigmaCrit;} - /// Shear times the critical surface density + /// Magnification if there are no other lenses PosType getAveMag() const { return 1.0/( pow(1-Sigma_uniform/SigmaCrit,2) - (gammaCrit_uniform[0]*gammaCrit_uniform[0] + gammaCrit_uniform[1]*gammaCrit_uniform[1])/SigmaCrit/SigmaCrit );} /// Critical surface density From c1301d40e828c6f2a0703f70f0cf1ce678824af3 Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Mon, 13 May 2019 21:24:14 +0200 Subject: [PATCH 137/227] Worked some more on LensHaloUniform. --- AnalyticNSIE/readfiles_uni.cpp | 137 +++++++-------------------------- MultiPlane/lens.cpp | 3 +- include/uniform_lens.h | 132 +++++++++++-------------------- 3 files changed, 75 insertions(+), 197 deletions(-) diff --git a/AnalyticNSIE/readfiles_uni.cpp b/AnalyticNSIE/readfiles_uni.cpp index 9edcf355..66ebe94d 100644 --- a/AnalyticNSIE/readfiles_uni.cpp +++ b/AnalyticNSIE/readfiles_uni.cpp @@ -16,7 +16,7 @@ using namespace std; * Sets many parameters within the lens model, source model and * force calculation. */ - +/* LensHaloUniform::LensHaloUniform(InputParams& params, const COSMOLOGY& cosmo, bool verbose) : LensHalo(params) { @@ -34,8 +34,9 @@ LensHaloUniform::LensHaloUniform(InputParams& params, const COSMOLOGY& cosmo, bo setCosmology(cosmo); if(verbose) PrintLens(false,false); } +*/ -LensHaloUniform::LensHaloUniform(InputParams& params, bool verbose): LensHalo(){ +/*LensHaloUniform::LensHaloUniform(InputParams& params, bool verbose): LensHalo(){ assignParams(params); @@ -49,47 +50,26 @@ LensHaloUniform::LensHaloUniform(InputParams& params, bool verbose): LensHalo(){ if(verbose) PrintLens(false,false); } +*/ -LensHaloUniform::LensHaloUniform(double zlens,double zsource,double kappa,Point_2d &gamma,COSMOLOGY &cosmo): LensHalo(zlens,cosmo),kappa_uniform(kappa),zsource_reference(zsource){ - - gamma_uniform[0] = gamma[0]; - gamma_uniform[1] = gamma[1]; - gamma_uniform[2] = 0; - - stars_N = 0; +LensHaloUniform::LensHaloUniform(double zlens,double Sigma,Point_2d &Shear,COSMOLOGY &cosmo): LensHalo(zlens,cosmo){ if(std::numeric_limits::has_infinity){ Rmax = std::numeric_limits::infinity(); }else{ Rmax = std::numeric_limits::max(); } - perturb_Nmodes=3; - perturb_modes = new PosType[3]; - setCosmology(cosmo); + perturb_modes[0] = Sigma; + perturb_modes[1] = Shear[0]; + perturb_modes[2] = Shear[1]; + + assert(Shear[0] == Shear[0] && Shear[1] == Shear[1]); + assert(Sigma == Sigma); + } LensHaloUniform::~LensHaloUniform(){ - -} - -void LensHaloUniform::setCosmology(const COSMOLOGY& cosmo) -{ - PosType zlens = LensHalo::getZlens(); - //Dl = cosmo.angDist(0,zlens); // In comoving Mpc - //Ds = cosmo.angDist(0,zsource_reference); - //Dls = cosmo.angDist(zlens,zsource_reference); - //SigmaCrit = Ds/Dl/Dls/(4*PI*Grav); - - SigmaCrit = cosmo.SigmaCrit(zlens,zsource_reference); - - Sigma_uniform = kappa_uniform*SigmaCrit; - gammaCrit_uniform[0] = gamma_uniform[0]*SigmaCrit; - gammaCrit_uniform[1] = gamma_uniform[1]*SigmaCrit; - - perturb_modes[0] = Sigma_uniform; - perturb_modes[1] = gammaCrit_uniform[0]; - perturb_modes[2] = gammaCrit_uniform[1]; } void LensHaloUniform::force_halo( @@ -102,14 +82,6 @@ void LensHaloUniform::force_halo( ,PosType screening ) { - //PosType alpha_tmp[2]; - //KappaType gamma_tmp[3], phi_tmp; - - //gamma_tmp[0] = gamma_tmp[1] = gamma_tmp[2] = 0.0; - //alpha_tmp[0] = alpha_tmp[1] = 0.0; - //phi_tmp = 0.0; - - //*kappa += lens_expand(perturb_modes,xcm,alpha,gamma,&phi_tmp); *kappa += lens_expand(perturb_modes,xcm,alpha,gamma,phi); alpha[0] *= -1; @@ -117,12 +89,9 @@ void LensHaloUniform::force_halo( //*phi += phi_tmp ; - // add stars for microlensing - if(stars_N > 0 && stars_implanted) - { - force_stars(alpha,kappa,gamma,xcm); - } - + assert(alpha[0] == alpha[0] && alpha[1] == alpha[1]); + assert(gamma[0] == gamma[0] && gamma[1] == gamma[1]); + assert(kappa == kappa); } PosType LensHaloUniform::lens_expand(PosType *mod @@ -165,21 +134,20 @@ PosType LensHaloUniform::lens_expand(PosType *mod *phi = r*r*(mod[0] + mod[1]*cos2theta + mod[2]*sin2theta)/2; } - //printf(" lens_expand *phi = %e\n",*phi); return mod[0]; } void LensHalo::assignParams_stars(InputParams& params){ - - if(!params.get("main_stars_fraction",star_fstars)) error_message1("main_stars_fraction",params.filename()); - if(star_fstars < 0 || star_fstars > 1){ - ERROR_MESSAGE(); - cout << "main_stars_fraction cannot be less than 0 or larger than 1 in file " << params.filename() < 1){ + ERROR_MESSAGE(); + cout << "main_stars_fraction cannot be less than 0 or larger than 1 in file " << params.filename() < 0){ - if(!params.get("kappa_uniform",kappa_uniform)) error_message1("kappa_uniform",params.filename()); - if(!params.get("gamma_uniform_1",gamma_uniform[0])) error_message1("gamma_uniform_1",params.filename()); - if(!params.get("gamma_uniform_2",gamma_uniform[1])) error_message1("gamma_uniform_2",params.filename()); - if(!params.get("zsource_reference",zsource_reference)) error_message1("zsource_reference",params.filename()); - if(!params.get("main_stars_N",stars_N)) error_message1("main_stars_N",params.filename()); - else if(stars_N){ - assignParams_stars(params); - } - return; -} - -/** \ingroup ImageFinding - * \brief Prints the parameters of the analytic lens to stdout - */ -void LensHaloUniform::PrintLens(bool show_substruct,bool show_stars) -{ - // uni lens parameters only - cout << endl << "**Host lens model**" << endl; - // redshifts - cout << "zlens " << getZlens() << endl; - cout << "kappa " << Sigma_uniform/SigmaCrit << endl; - cout << "gamma " << gammaCrit_uniform[0]/SigmaCrit << " " << gammaCrit_uniform[1]/SigmaCrit << endl; - - if (stars_implanted) PrintStars(show_stars); + return; } - -/* creates a single star halo in pos (x,y) -void LensHalo::implant_stars( - PosType x, PosType y,long *seed, IMFtype type){ - - if(Nregions <= 0) return; - - PosType ** centers = new PosType*[Nregions]; - for (int i = 0; i < Nregions; ++i){ - centers[i] = new PosType[2]; - - centers[i][0] = x[i]; - centers[i][1] = y[i]; - } - implant_stars(centers,Nregions,seed,type); - - for (int i = 0; i < Nregions; ++i) delete[] centers[i]; - delete[] centers; -}*/ - - diff --git a/MultiPlane/lens.cpp b/MultiPlane/lens.cpp index 0130206d..2f990aaf 100644 --- a/MultiPlane/lens.cpp +++ b/MultiPlane/lens.cpp @@ -1406,7 +1406,8 @@ void Lens::createMainHalos(InputParams& params) main_halos.push_back(new LensHaloAnaNSIE(params)); break; case uni_lens: - main_halos.push_back(new LensHaloUniform(params)); + throw std::invalid_argument("Can't add uniform lens in this way now"); +// main_halos.push_back(new LensHaloUniform(params)); break; case moka_lens: main_halos.push_back(new LensHaloMassMap(params, cosmo)); diff --git a/include/uniform_lens.h b/include/uniform_lens.h index 562d16ca..4642ca2c 100644 --- a/include/uniform_lens.h +++ b/include/uniform_lens.h @@ -11,107 +11,67 @@ #include "base_analens.h" /** - * \brief A uniform surface density and shear with or without stars. + * \brief A uniform surface density and shear lens. * + * Note the units of the input shear and surface density are not Sigma_crit's. + * This lens will extend to infinity in the angular directions. * - *

- *
- *  Warning:Once this lens is fully constructed it is independent of the source redshift so
- *  if the source redshift is change through the Lens class the input kappa and gamma values will
- *  longer be the correct ones because of the change in the critical density.
- *
- * Input Parameters in parameter file:
- *
- *  ****  Uniform model parameters
- *	kappa_uniform
- *  gamma_uniform_1
- *  gamma_uniform_2
- *
- *
- *
- *
- *  **** Stars parameters
- *	main_stars_N                 Total number of stars that will be used in the simulation.  If zero the other star parameters are not needed.
- *	main_stars_fraction                 Fraction of surface denity in stars.
- *	main_stars_mass             Mass of stars.
- *
- *	main_stars_mass_function  Choose between: One, Mono, Salpeter, BrokenPowerLaw, SinglePowerLaw, Chabrier.
- *						   One - all stellar masses set to 1 Msol - default value in case one does not give the IMFtype option in function implant_stars()
- *						   Mono - all stellar masses set to the same value - given in main_stars_min_mass and main_stars_max_mass, Note main_stars_min_mass and main_stars_max_mass must be equal.
- *						   Salpeter - Salpeter power law IMF with slope -2.35. Note: main_stars_min_mass and main_stars_max_mass must be defined and different from each other.
- * 						   BrokenPowerLaw - IMF for which two slopes (the low mass slope 'main_stars_lo_mass_slope' and the high mass slope 'main_stars_hi_mass_slope') can be defined as well as a bending point (main_stars_bending_point).
- *                         SinglePowerLaw - Like Salpeter but with main_stars_lo_mass_slope=main_stars_hi_mass_slope as a free parameter.
- * 						   Chabrier - Chabrier IMF with exponential cut-off toward low mass, main_stars_bending_point is fixed to 1.0 Msol, Slope of powerlaw above 1 Msol is fixed to -2.3. Note that the three Chabrier Parameters are defined in implant_stars.cpp and (for now) fixed to [0.086,0.22,0.57].
- *	main_stars_min_mass          	   Lower mass limit of above IMFs (not used for IMFs One and Mono)
- *	main_stars_max_mass			   Upper mass limit of above IMFs (not used for IMFs One and Mono)
- *  main_stars_bending_point      	   Bending mass for BrokenPowerLaw
- *  main_stars_lo_mass_slope            	   index of the powerlaw (this is the index of the low mass end below main_stars_bending_point if BrokenPowerLaw is selected)
- *  main_stars_hi_mass_slope			   	   index of the powerlaw above main_stars_bending_point only used for BrokenPowerLaw. Must be equal to main_stars_lo_mass_slope for SingelPowerLaw.
- *
- *  The stars are not initially present.  They must be implanted later.
- *
* */ class LensHaloUniform: public LensHalo{ public: - LensHaloUniform(InputParams& params, const COSMOLOGY& cosmo, bool verbose = false); - LensHaloUniform(InputParams& params,bool verbose =false); - /// Direct constructor without any stars - LensHaloUniform(double zlens /// lens redshift - ,double zsource /// source redshift - ,double kappa /// convergence - ,Point_2d &gamma /// shear - ,COSMOLOGY &cosmo /// cosmology to translate into surface density + /// Direct constructor without any stars + LensHaloUniform(double zlens /// lens redshift + ,double Sigma /// surface density of lens in Msun/Mpc^2 + ,Point_2d &Shear /// shear in M_sun/Mpc^2 ie gamma * Sigma_crit + ,COSMOLOGY &cosmo ); - ~LensHaloUniform(); - - void assignParams(InputParams& params); - void PrintLens(bool show_substruct,bool show_stars); - /// Average surface density in Msun/Mpc^2 - float getSigma_uniform() const {return Sigma_uniform;} - /// Shear times the critical surface density - float* getGammaCrit_uniform(){return gammaCrit_uniform;} - /// Unitless convergence - float getKappa_uniform() const {return Sigma_uniform/SigmaCrit;} - /// Magnification if there are no other lenses - PosType getAveMag() const { return 1.0/( pow(1-Sigma_uniform/SigmaCrit,2) - - (gammaCrit_uniform[0]*gammaCrit_uniform[0] + gammaCrit_uniform[1]*gammaCrit_uniform[1])/SigmaCrit/SigmaCrit );} - /// Critical surface density - PosType getSigmaCrit() const { return SigmaCrit;} - - //PosType getPerturb_beta(){return perturb_beta;} - //int getPerturb_Nmodes(){return perturb_Nmodes;} /// this includes two for external shear - //PosType *perturb_modes; ///first two are shear - /// overridden function to calculate the lensing properties - //void force_halo(PosType *alpha,KappaType *kappa,KappaType *gamma,PosType *xcm,bool subtract_point=false); + ~LensHaloUniform(); + /// surface density in Msun/Mpc^2 + float getSigma() const {return perturb_modes[0];}; + /// shear in Msun/Mpc^2 + Point_2d getShear() const{ + Point_2d p(perturb_modes[1],perturb_modes[2]); + return p; + } + + /// reset the shear keeping the redshift of the plane fixed + void resetGamma(const Point_2d &Shear /// Shear in units of critical density Msun/Mpc^2, ie gamma * Sigma_crit + ){ + perturb_modes[1] = Shear[0]; + perturb_modes[2] = Shear[1]; + } + + /// reset the convergence keeping the redshift of the plane fixed + void resetKappa(double Sigma /// Surface density in Msun/Mpc^2, ie kappa * Sigma_crit + ){ + perturb_modes[0] = Sigma;; + } + void force_halo(PosType *alpha,KappaType *kappa,KappaType *gamma,KappaType *phi,PosType const *xcm,bool subtract_point=false,PosType screening = 1.0); - + + LensHaloUniform(const LensHaloUniform &h):LensHalo(h){ + perturb_modes[0] = h.perturb_modes[0]; + perturb_modes[1] = h.perturb_modes[1]; + perturb_modes[2] = h.perturb_modes[2]; + } + + LensHaloUniform & operator=(const LensHaloUniform &h){ + if(this == &h) return *this; + LensHalo::operator=(h); + perturb_modes[0] = h.perturb_modes[0]; + perturb_modes[1] = h.perturb_modes[1]; + perturb_modes[2] = h.perturb_modes[2]; + return *this; + } protected: - virtual void setCosmology(const COSMOLOGY& cosmo); - - PosType SigmaCrit; - PosType *perturb_modes; ///first two are shear + PosType perturb_modes[3]; ///first two are shear PosType lens_expand(PosType *mod,PosType const *x,PosType *alpha,KappaType *gamma,KappaType *phi); - - /// Surface density of lens in Msun/ Mpc^2 - float Sigma_uniform; - /// gamma*Sigma_crit - float gammaCrit_uniform[3]; - /// input values for kappa and gamma - float kappa_uniform, gamma_uniform[3]; - // perturbations to host. These are protected so that in some derived classes they can or cann't be changed. - int perturb_Nmodes; /// this includes two for external shear - PosType perturb_beta; - PosType *perturb_rms; - - /// redshift for which uniform kappa and gamma are valid - float zsource_reference; - PosType Dl, Ds, Dls; // In comoving Mpc }; From 5422949b55122084bc4e325dce725fac275b721c Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Thu, 16 May 2019 18:13:55 +0200 Subject: [PATCH 138/227] Suppressed verbosity of LensHaloParticles --- MultiPlane/particle_lens.cpp | 3 ++- include/disk.h | 2 +- include/particle_halo.h | 15 ++++++++++----- 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/MultiPlane/particle_lens.cpp b/MultiPlane/particle_lens.cpp index 852cc337..d3ac2ac9 100644 --- a/MultiPlane/particle_lens.cpp +++ b/MultiPlane/particle_lens.cpp @@ -190,8 +190,9 @@ void MakeParticleLenses::CreateHalos(const COSMOLOGY &cosmo,double redshift){ ,cosmo ,theta_rotate ,false - ,0) + ,0,false) ); + /*/ /*LensHaloParticles > halo(pp ,nparticles[i] diff --git a/include/disk.h b/include/disk.h index d9712b93..712a287f 100644 --- a/include/disk.h +++ b/include/disk.h @@ -141,7 +141,7 @@ qrot_invers(1,0,0,0),Rscale(disk_scale),Rhight(Rperp),zpa(-my_PA),inclination(my LensHaloParticles >::multimass=false; Point_2d no_rotation; - LensHaloParticles >::set_up(redshift,cosmo,no_rotation,false); + LensHaloParticles >::set_up(redshift,cosmo,no_rotation,false,false); }; #endif diff --git a/include/particle_halo.h b/include/particle_halo.h index 732192dd..eeda5247 100644 --- a/include/particle_halo.h +++ b/include/particle_halo.h @@ -48,6 +48,7 @@ class LensHaloParticles : public LensHalo ,bool recenter /// center on center of mass ,bool my_multimass /// set to true is particles have different sizes ,PosType MinPSize /// minimum particle size + ,bool verbose=false ); LensHaloParticles(std::vector &pvector /// list of particles pdata[][i] should be the position in physical Mpc, the class takes possession of the data and leaves the vector empty @@ -57,12 +58,13 @@ class LensHaloParticles : public LensHalo ,Point_2d theta_rotate /// rotation of particles around the origin ,bool recenter /// center on center of mass ,float MinPSize /// minimum particle size + ,bool verbose=false ):min_size(MinPSize),multimass(true) { std::swap(pvector,trash_collector); pp = trash_collector.data(); Npoints = trash_collector.size(); - set_up(redshift,cosmo,theta_rotate,recenter); + set_up(redshift,cosmo,theta_rotate,recenter,verbose); } ~LensHaloParticles(); @@ -145,9 +147,10 @@ class LensHaloParticles : public LensHalo ,Point_2d theta_rotate /// rotation of particles around the origin ,bool recenter /// center on center of mass ,float MinPSize /// minimum particle size + ,bool verbose ):pp(pdata),min_size(MinPSize),multimass(true),Npoints(Nparticles) { - set_up(redshift,cosmo,theta_rotate,recenter); + set_up(redshift,cosmo,theta_rotate,recenter,verbose); } void rotate_particles(PosType theta_x,PosType theta_y); @@ -169,7 +172,7 @@ class LensHaloParticles : public LensHalo std::string sizefile; TreeQuadParticles * qtree; - void set_up(float redshift,const COSMOLOGY& cosmo,Point_2d theta_rotate,bool recenter); + void set_up(float redshift,const COSMOLOGY& cosmo,Point_2d theta_rotate,bool recenter,bool verbose); }; template @@ -182,6 +185,7 @@ LensHaloParticles::LensHaloParticles(const std::string& simulation_filena ,bool recenter ,bool my_multimass ,PosType MinPSize + ,bool verbose ) :min_size(MinPSize),multimass(my_multimass),simfile(simulation_filename) { @@ -240,7 +244,7 @@ LensHaloParticles::LensHaloParticles(const std::string& simulation_filena mcenter /= mass; - std::cout << " Particle mass range : " << min_mass << " to " << max_mass << " ratio of : " << max_mass/min_mass << std::endl; + if(verbose) std::cout << " Particle mass range : " << min_mass << " to " << max_mass << " ratio of : " << max_mass/min_mass << std::endl; if(recenter){ @@ -269,6 +273,7 @@ void LensHaloParticles::set_up( ,const COSMOLOGY& cosmo /// cosmology ,Point_2d theta_rotate /// rotation of particles around the origin ,bool recenter /// center on center of mass + ,bool verbose ){ LensHalo::setZlens(redshift); @@ -300,7 +305,7 @@ void LensHaloParticles::set_up( mcenter /= mass; - std::cout << " Particle mass range : " << min_mass << " to " << max_mass << " ratio of : " << max_mass/min_mass << std::endl; + if(verbose) std::cout << " Particle mass range : " << min_mass << " to " << max_mass << " ratio of : " << max_mass/min_mass << std::endl; if(recenter){ PosType r2,r2max=0; From 065c578431b52190dbe3460f635193af80c9c676 Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Fri, 24 May 2019 12:24:03 +0200 Subject: [PATCH 139/227] Improvements to LneHaloMultiMap and LensMap. --- AnalyticNSIE/sourceAnaGalaxy.cpp | 4 +- MultiPlane/lens_halos.cpp | 2 +- MultiPlane/multimap.cpp | 175 +++++++++++++++++++++++++------ include/lens_halos.h | 2 +- include/multimap.h | 56 ++++++---- include/utilities_slsim.h | 7 ++ 6 files changed, 188 insertions(+), 58 deletions(-) diff --git a/AnalyticNSIE/sourceAnaGalaxy.cpp b/AnalyticNSIE/sourceAnaGalaxy.cpp index 2896b0c2..8b08c860 100644 --- a/AnalyticNSIE/sourceAnaGalaxy.cpp +++ b/AnalyticNSIE/sourceAnaGalaxy.cpp @@ -219,7 +219,7 @@ void SourceMultiAnaGalaxy::readDataFileMillenn(Utilities::RandomNumbers_NR &ran) << " " << H_band_Bulge << " " << Ks_band_Bulge << " " << i1_Bulge << " " << i2_Bulge << std::endl; - if(mag < mag_limit){ + //if(mag < mag_limit){ //std::cout << "good mag " << std::endl; /* @@ -299,7 +299,7 @@ void SourceMultiAnaGalaxy::readDataFileMillenn(Utilities::RandomNumbers_NR &ran) assert(galaxies.back().getMag(band) == galaxies.back().getMag() ); //std::cout << " j in SourceMultiAnaGalaxy : " << j << std::endl; ++j; - } + //} } std::cout << " closing file in SourceMultiAnaGalaxy : " << std::endl; diff --git a/MultiPlane/lens_halos.cpp b/MultiPlane/lens_halos.cpp index a570b8e3..a74188a2 100644 --- a/MultiPlane/lens_halos.cpp +++ b/MultiPlane/lens_halos.cpp @@ -20,7 +20,7 @@ LensHalo::LensHalo(){ zlens = 0.0; } -LensHalo::LensHalo(PosType z,COSMOLOGY &cosmo){ +LensHalo::LensHalo(PosType z,const COSMOLOGY &cosmo){ rscale = 1.0; mass = Rsize = Rmax = xmax = posHalo[0] = posHalo[1] = 0.0; stars_implanted = false; diff --git a/MultiPlane/multimap.cpp b/MultiPlane/multimap.cpp index 371c12c2..918fc661 100644 --- a/MultiPlane/multimap.cpp +++ b/MultiPlane/multimap.cpp @@ -11,10 +11,11 @@ using namespace CCfits; LensHaloMultiMap::LensHaloMultiMap( std::string fitsfile /// Original fits map of the density + ,double redshift ,double mass_unit ,const COSMOLOGY &c ,bool single_grid_mode - ):LensHalo(),single_grid(single_grid_mode),cosmo(c),mass_unit(mass_unit),fitsfilename(fitsfile) + ):LensHalo(redshift,c),single_grid(single_grid_mode),cosmo(c),mass_unit(mass_unit),fitsfilename(fitsfile) { try{ @@ -33,10 +34,10 @@ LensHaloMultiMap::LensHaloMultiMap( Rmax = std::numeric_limits::max(); LensMap submap; - submap.read_header(fitsfilename,cosmo.gethubble()); + submap.read_header(fitsfilename,cosmo.gethubble(),getZlens()); - setZlens(submap.z); - setZlensDist(submap.z,cosmo); + setZlens(redshift); + setZlensDist(redshift,cosmo); long_range_map.boxlMpc = submap.boxlMpc; @@ -56,12 +57,17 @@ LensHaloMultiMap::LensHaloMultiMap( size_t nx = long_range_map.nx = submap.nx / desample ; size_t ny = long_range_map.ny = submap.ny / desample ; + + if( Utilities::IO::checkfile(fitsfile + "_lr.fits") ){ - if(single_grid){ + std::cout << " reading file " << fitsfile + "_lr.fits .. " << std::endl; + long_range_map.Myread(fitsfile + "_lr.fits"); + + }else if(single_grid){ std::vector first = {1,1}; std::vector last = {(long)Noriginal[0],(long)Noriginal[1]}; - long_range_map.read_sub(ff,first,last,cosmo.gethubble()); + long_range_map.read_sub(ff,first,last,cosmo.gethubble(),getZlens()); //double res = submap.boxlMpc/submap.nx; //Point_2d dmap = {submap.boxlMpc,submap.ny*resolution}; @@ -113,22 +119,21 @@ LensHaloMultiMap::LensHaloMultiMap( first[1] = j+1; last[1] = MIN(j + chunk,Noriginal[1]); //submap.read_sub(fitsfilename,first,last,cosmo.gethubble()); - submap.read_sub(ff,first,last,cosmo.gethubble()); + submap.read_sub(ff,first,last,cosmo.gethubble(),getZlens()); kj = 0; }else{ kj += submap.nx; //assert(kj < submap.nx*submap.ny); } + double tmp; for(size_t i = 0 ; i < Noriginal[0] ; ++i ){ size_t ii = i/desample; if( ii >= nx) break; - //assert(ii + kjj < long_range_map.surface_density.size() ); - //assert(i + kj < submap.surface_density.size() ); - //assert(!isinf(submap.surface_density[ i + kj ])); - //assert(!isinf(long_range_map.surface_density[ ii + kjj ])); - long_range_map.surface_density[ ii + kjj ] += submap.surface_density[ i + kj ]; - //assert(!isnan(submap.surface_density[ i + kj ])); + tmp = long_range_map.surface_density[ ii + kjj ] += submap.surface_density[ i + kj ]; + + max_pix = MAX(max_pix,tmp); + min_pix = MIN(min_pix,tmp); } } assert(jj == ny-1); @@ -142,21 +147,27 @@ LensHaloMultiMap::LensHaloMultiMap( p /= area; } - if(single_grid) long_range_map.PreProcessFFTWMap(1.0,unit); - else long_range_map.PreProcessFFTWMap(1.0,wlr); - //else long_range_map.PreProcessFFTWMap(1.0,unit); // ????? - - if(!single_grid) long_range_map.write("!" + fitsfile + "_lr.fits"); + if( !Utilities::IO::checkfile(fitsfile + "_lr.fits") ){ + if(single_grid){ + long_range_map.PreProcessFFTWMap(1.0,unit); + }else{ + long_range_map.PreProcessFFTWMap(1.0,wlr); + long_range_map.write("!" + fitsfile + "_lr.fits"); + } + } }; -void LensHaloMultiMap::submap(Point_2d ll,Point_2d ur){ +void LensHaloMultiMap::submapPhys(Point_2d ll,Point_2d ur){ if(single_grid) return; std::vector lower_left(2); std::vector upper_right(2); ll = (ll - long_range_map.lowerleft)/resolution; + assert(ll[0] >= 0); assert(ll[1] >= 0); ur = (ur - long_range_map.lowerleft)/resolution; + assert(ur[0] >= 0); assert(ur[1] >= 0); + lower_left[0] = floor(ll[0]); lower_left[1] = floor(ll[1]); @@ -177,6 +188,8 @@ void LensHaloMultiMap::submap( if( (upper_right[0] < 0) || (upper_right[1] < 0) || (lower_left[0] >= Noriginal[0] ) || (lower_left[1] >= Noriginal[1]) ){ + + std::cerr << "LensHaloMap : sub map is out of bounds" << std::endl; std::cerr << "LensHaloMap : sub map is out of bounds" << std::endl; throw std::invalid_argument("out of bounds"); } @@ -198,7 +211,7 @@ void LensHaloMultiMap::submap( LensMap map; if( (first[0] > 0)*(first[1] > 0)*(last[0] <= Noriginal[0])*(last[1] <= Noriginal[1]) ){ - map.read_sub(fitsfilename,first,last,cosmo.gethubble()); + map.read_sub(fitsfilename,first,last,cosmo.gethubble(),getZlens()); }else{ size_t nx_big = map.nx = last[0] - first[0] + 1; @@ -229,7 +242,7 @@ void LensHaloMultiMap::submap( first_sub[1] = jj + 1; last_sub[1] = MIN(jj + chunk + 1,Noriginal[1]); //partmap.read_sub(fitsfilename,first_sub,last_sub,cosmo.gethubble()); - partmap.read_sub(ff,first_sub,last_sub,cosmo.gethubble()); + partmap.read_sub(ff,first_sub,last_sub,cosmo.gethubble(),getZlens()); k = 0; }else{ ++k; @@ -237,13 +250,17 @@ void LensHaloMultiMap::submap( kk = k*Noriginal[0]; long ii = first[0]-1; + double tmp; for(size_t i = 0 ; i < nx_big ; ++i,++ii ){ if(ii < 0) ii += Noriginal[0]; if(ii >= Noriginal[0] ) ii -= Noriginal[0]; //assert( i + kj < map.surface_density.size() ); //assert( ii + kk < partmap.surface_density.size() ); - map.surface_density[ i + kj ] = partmap.surface_density[ ii + kk ]; + tmp = map.surface_density[ i + kj ] = partmap.surface_density[ ii + kk ]; + + max_pix = MAX(max_pix,tmp); + min_pix = MIN(min_pix,tmp); } } // need to do overlap region @@ -383,7 +400,42 @@ void LensHaloMultiMap::force_halo(double *alpha return; } -void LensMap::read_header(std::string fits_input_file,float h){ +LensMap::LensMap(LensMap &&m){ + surface_density=std::move(m.surface_density); + alpha1_bar =std::move(m.alpha1_bar); + alpha2_bar =std::move(m.alpha2_bar); + gamma1_bar =std::move(m.gamma1_bar); + gamma2_bar =std::move(m.gamma2_bar); + phi_bar = std::move(m.phi_bar); + + nx = m.nx; + ny = m.ny; + boxlMpc = m.boxlMpc; + center = m.center; + lowerleft = m.lowerleft; + upperright = m.upperright; +} + +LensMap& LensMap::operator=(LensMap &&m){ + if(&m==this) return *this; + + surface_density=std::move(m.surface_density); + alpha1_bar =std::move(m.alpha1_bar); + alpha2_bar =std::move(m.alpha2_bar); + gamma1_bar =std::move(m.gamma1_bar); + gamma2_bar =std::move(m.gamma2_bar); + phi_bar = std::move(m.phi_bar); + + nx = m.nx; + ny = m.ny; + boxlMpc = m.boxlMpc; + center = m.center; + lowerleft = m.lowerleft; + upperright = m.upperright; + + return *this; +} +void LensMap::read_header(std::string fits_input_file,float h,float z){ std::auto_ptr ff(new CCfits::FITS (fits_input_file, CCfits::Read)); @@ -401,7 +453,7 @@ void LensMap::read_header(std::string fits_input_file,float h){ /* these are always present in ea*/ float wlow,wup,res; h0.readKey ("CD1_1",res); // resolution in degrees - h0.readKey ("REDSHIFT",z); + //h0.readKey ("REDSHIFT",z); h0.readKey ("WLOW",wlow); h0.readKey ("WUP",wup); @@ -435,7 +487,7 @@ void LensMap::read_header(std::string fits_input_file,float h){ upperright[1] += ny*res/2.; } -void LensMap::read(std::string fits_input_file,float h){ +void LensMap::read(std::string fits_input_file,float h,float z){ std:: cout << " reading lens density map file: " << fits_input_file << std:: endl; std::auto_ptr ff(new CCfits::FITS (fits_input_file, CCfits::Read)); @@ -477,7 +529,7 @@ void LensMap::read(std::string fits_input_file,float h){ /* these are always present in ea*/ float wlow,wup,res; h0.readKey ("CD1_1",res); // recall you that MOKA Mpc/h - h0.readKey ("REDSHIFT",z); + //h0.readKey ("REDSHIFT",z); h0.readKey ("WLOW",wlow); h0.readKey ("WUP",wup); @@ -510,10 +562,65 @@ void LensMap::read(std::string fits_input_file,float h){ } +void LensMap::Myread(std::string fits_input_file){ + + std:: cout << " reading lens density map file: " << fits_input_file << std:: endl; + std::auto_ptr ff(new CCfits::FITS (fits_input_file, CCfits::Read)); + + CCfits::PHDU &h0 = ff->pHDU(); + + h0.readAllKeys(); + + assert(h0.axes() >= 2); + + nx = h0.axis(0); + ny = h0.axis(1); + + size_t size = nx*ny; + + // principal HDU is read + h0.read(surface_density); + int nhdu = h0.axes(); + + // file contains other lensing quantities + alpha1_bar.resize(size); + alpha2_bar.resize(size); + gamma1_bar.resize(size); + gamma2_bar.resize(size); + //phi_bar.resize(size); + + CCfits::ExtHDU &h1=ff->extension(1); + h1.read(alpha1_bar); + CCfits::ExtHDU &h2=ff->extension(2); + h2.read(alpha2_bar); + CCfits::ExtHDU &h3=ff->extension(3); + h3.read(gamma1_bar); + CCfits::ExtHDU &h4=ff->extension(4); + h4.read(gamma2_bar); + std::cout << h0 << h1 << h2 << h3 << h4 << std::endl; + + /* these are always present in ea*/ + //h0.readKey ("REDSHIFT",z); + h0.readKey("SIDEL2",boxlMpc); + + double res = nx/boxlMpc; + center *= 0; + + lowerleft = center; + lowerleft[0] -= boxlMpc/2; + lowerleft[1] -= res*ny/2; + + upperright = center; + upperright[0] += boxlMpc/2; + upperright[1] += ny*res/2.; +} + + void LensMap::read_sub(std::string fits_input_file ,const std::vector &first /// 2d vector for pixel of lower left, (1,1) offset ,const std::vector &last /// 2d vector for pixel of upper right, (1,1) offset ,float h + ,float z ){ //std:: cout << " reading lens density map file: " << fits_input_file << std:: endl; @@ -534,7 +641,7 @@ void LensMap::read_sub(std::string fits_input_file /* these are always present in ea*/ float wlow,wup,res; h0.readKey ("CD1_1",res); // resolution in - h0.readKey ("REDSHIFT",z); + //h0.readKey ("REDSHIFT",z); h0.readKey ("WLOW",wlow); h0.readKey ("WUP",wup); @@ -584,6 +691,7 @@ void LensMap::read_sub(CCfits::FITS *ff ,const std::vector &first ,const std::vector &last ,float h + ,float z ){ CCfits::PHDU &h0 = ff->pHDU(); @@ -595,7 +703,7 @@ void LensMap::read_sub(CCfits::FITS *ff // these are always present in each float wlow,wup,res; h0.readKey ("CD1_1",res); // resolution in - h0.readKey ("REDSHIFT",z); + //h0.readKey ("REDSHIFT",z); h0.readKey ("WLOW",wlow); h0.readKey ("WUP",wup); @@ -631,9 +739,13 @@ void LensMap::read_sub(CCfits::FITS *ff } /** - * \brief write the fits file of the maps of all the lensing quantities + * \brief write the fits file of the maps of all the lensing quantities. + * + * Unlike the read operations this will not have the h factors in everything so + * when reading from a file created by is you should set h to 1 */ -void LensMap::write(std::string filename){ +void LensMap::write(std::string filename + ){ #ifdef ENABLE_FITS long naxis=2; long naxes[2]={nx,ny}; @@ -655,7 +767,8 @@ void LensMap::write(std::string filename){ PHDU *phout=&fout->pHDU(); - phout->addKey("SIDEL2",boxlMpc,"Mpc/h"); + phout->addKey("SIDEL2",boxlMpc,""); + //phout->addKey("REDSHIFT",z,""); phout->write( 1,nx*ny,surface_density ); diff --git a/include/lens_halos.h b/include/lens_halos.h index a10a1e33..1626ee01 100644 --- a/include/lens_halos.h +++ b/include/lens_halos.h @@ -43,7 +43,7 @@ class LensHalo{ public: LensHalo(); - LensHalo(PosType z,COSMOLOGY &cosmo); + LensHalo(PosType z,const COSMOLOGY &cosmo); LensHalo(InputParams& params,COSMOLOGY &cosmo,bool needRsize = true); LensHalo(InputParams& params,bool needRsize = true); virtual ~LensHalo(); diff --git a/include/multimap.h b/include/multimap.h index 529562dc..8359320e 100644 --- a/include/multimap.h +++ b/include/multimap.h @@ -31,7 +31,11 @@ */ struct LensMap{ - LensMap():nx(0),ny(0),boxlMpc(0),z(0){}; + LensMap():nx(0),ny(0),boxlMpc(0){}; + + // move operators + LensMap(LensMap &&m); + LensMap& operator=(LensMap &&m); /// values for the map std::valarray surface_density; // Msun / Mpc^2 @@ -39,42 +43,36 @@ struct LensMap{ std::valarray alpha2_bar; std::valarray gamma1_bar; std::valarray gamma2_bar; - //std::valarray gamma3_bar; std::valarray phi_bar; - //std::vector x; int nx,ny; - // boxlMpc is Mpc/h for MOKA - /// lens and source properties - //double zlens,m,zsource,Dlens,DLS,DS,c,cS,fsub,mstar,minsubmass; - /// range in x direction, pixels are square - //double boxlarcsec,boxlrad; double boxlMpc; - /// cosmology - //double omegam,omegal,h,wq; - //double inarcsec; Point_2d center; Point_2d lowerleft; Point_2d upperright; - double z; + //double z; #ifdef ENABLE_FITS - LensMap(std::string fits_input_file,float h){ - read(fits_input_file,h); + LensMap(std::string fits_input_file,float h,float z){ + read(fits_input_file,h,z); } /// read an entire map - void read(std::string input_fits,float h); + void read(std::string input_fits,float h,float z); + /// read from a file that has been generated with LensMap::write() + void Myread(std::string fits_input_file); + /// read only header information - void read_header(std::string input_fits,float h); + void read_header(std::string input_fits,float h,float z); /// read a subsection of the fits map void read_sub(std::string input_fits ,const std::vector &first ,const std::vector &last ,float h + ,float z ); @@ -84,6 +82,7 @@ struct LensMap{ ,const std::vector &first ,const std::vector &last ,float h + ,float z ); void write(std::string filename); @@ -115,25 +114,30 @@ class LensHaloMultiMap : public LensHalo LensHaloMultiMap( std::string fitsfile /// Original fits map of the density - ,double mass_unit /// shoudl include h factors + ,double redshift + ,double mass_unit /// should include h factors ,const COSMOLOGY &c ,bool single_grid_mode = false ); - //double Wlr(double k2){return exp(-k2*rs2);} - ~LensHaloMultiMap(){ delete ff; }; const double f = 5,g = 6; + /// Set highres map be specifying the corners in pixel values void submap( const std::vector &lower_left ,const std::vector &upper_right ); - /// in physical coordinates relative to the center of the original map - void submap(Point_2d ll,Point_2d ur); + /// Sets the highres smaller map in physical coordinates relative to the center of the original map, periodic boundary conditions apply + void submapPhys(Point_2d ll,Point_2d ur); + + void submapAngular(Point_2d ll,Point_2d ur){ + double D = getDist(); + submapPhys(ll*D,ur*D); + } void force_halo(double *alpha,KappaType *kappa,KappaType *gamma,KappaType *phi,double const *xcm,bool subtract_point=false,PosType screening = 1.0); @@ -173,6 +177,9 @@ class LensHaloMultiMap : public LensHalo size_t getNx() const { return Noriginal[0]; } /// return number of pixels on a y-axis side in original map size_t getNy() const { return Noriginal[1]; } + + double getMax() const {return max_pix;} + double getMin() const {return min_pix;} private: @@ -180,6 +187,9 @@ class LensHaloMultiMap : public LensHalo const COSMOLOGY &cosmo; CCfits::FITS *ff; + double max_pix = std::numeric_limits::lowest(); + double min_pix = std::numeric_limits::max(); + public: // ????? LensMap long_range_map; LensMap short_range_map; @@ -195,7 +205,7 @@ class LensHaloMultiMap : public LensHalo //const COSMOLOGY& cosmo; int zerosize; - + struct UNIT{ int operator()(float k2){return 1;} }; @@ -471,7 +481,7 @@ void LensMap::PreProcessFFTWMap(float zerosize,T Wphi_of_k){ for( int i=imin; i From c59c95371a06b7c46a2484613cf1b84477cee3e7 Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Sat, 25 May 2019 11:20:11 +0200 Subject: [PATCH 140/227] Added some asserts to test if shapelets has zero flux in some bands. --- AnalyticNSIE/source.cpp | 5 +++++ include/disk.h | 2 +- include/source.h | 2 +- 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/AnalyticNSIE/source.cpp b/AnalyticNSIE/source.cpp index 25094959..0801d9c4 100644 --- a/AnalyticNSIE/source.cpp +++ b/AnalyticNSIE/source.cpp @@ -502,6 +502,8 @@ void SourceShapelets::setActiveBand(Band band) flux = std::numeric_limits::epsilon(); else flux = pow(10,-0.4*(mag+48.6))*inv_hplanck; + + assert(flux > 0); current_band = band; return; } @@ -529,6 +531,7 @@ SourceShapelets::SourceShapelets( source_r = my_scale; flux = pow(10,-0.4*(mag+48.6))*inv_hplanck; + assert(flux > 0.0); NormalizeFlux(); } @@ -582,6 +585,7 @@ SourceShapelets::SourceShapelets( #endif flux = pow(10,-0.4*(mag+48.6))*inv_hplanck; + assert(flux > 0); NormalizeFlux(); @@ -686,6 +690,7 @@ PosType SourceShapelets::SurfaceBrightness(PosType *y) } sb *= exp(-0.5*(y_norm[0]*y_norm[0]+y_norm[1]*y_norm[1]) )/source_r; sb *= flux/coeff_flux; + assert(flux > 0); return max(sb,std::numeric_limits::epsilon()); } diff --git a/include/disk.h b/include/disk.h index 712a287f..082e8a79 100644 --- a/include/disk.h +++ b/include/disk.h @@ -124,7 +124,7 @@ qrot_invers(1,0,0,0),Rscale(disk_scale),Rhight(Rperp),zpa(-my_PA),inclination(my ++i; } - LensHalo::Rmax = -3 * Rscale * log(1 - (float)(N-1) / N ); + LensHalo::Rmax = -5 * Rscale * log(1 - (float)(N-1) / N ); LensHalo::setRsize( LensHalo::Rmax ); LensHaloParticles >::calculate_smoothing(Nsmooth,particles.data(),particles.size()); diff --git a/include/source.h b/include/source.h index 0777236c..a5a89f59 100644 --- a/include/source.h +++ b/include/source.h @@ -202,7 +202,7 @@ class SourceShapelets: public Source{ void printSource(); inline PosType getTotalFlux() const {return flux;} inline PosType getRadius() const {return source_r*10.;} - inline PosType getMag() const {return mag;} + inline PosType getMag() const { assert(current_band != NoBand); return mag;} inline PosType getMag(Band band) const {return mag_map.at(band);} inline Band getBand() const{return current_band;} inline long getID() const {return id;} From 4234b05be83293d960eb32016c1fefaef47012bb Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Sat, 25 May 2019 22:05:15 +0200 Subject: [PATCH 141/227] Filtered out shapelet sources with -99 in some filters. --- AnalyticNSIE/source.cpp | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/AnalyticNSIE/source.cpp b/AnalyticNSIE/source.cpp index 0801d9c4..b4f98dc1 100644 --- a/AnalyticNSIE/source.cpp +++ b/AnalyticNSIE/source.cpp @@ -359,9 +359,9 @@ void SourcePixelled::assignParams(InputParams& params){} * */ PosType Source::changeFilter( - std::string filter_in /// file with the old observing filter - , std::string filter_out /// file with the new observing filter - , std::string sed /// file with the galaxy spectral energy distribution + std::string filter_in /// file with the old observing filter + , std::string filter_out /// file with the new observing filter + , std::string sed /// file with the galaxy spectral energy distribution ) { @@ -770,19 +770,22 @@ void SourceMultiShapelets::readCatalog() { SourceShapelets s(shap_file.c_str()); //s.setActiveBand(band); - if (s.getMag() > 0. && s.getMag() < mag_limit) + if (s.getMag() > 0. && s.getMag() < mag_limit + && s.getMag(EUC_J) > 0 && s.getMag(EUC_H) > 0 ){ galaxies.push_back(s); - shap_input.close(); - } - else if (i == 1) - { - std::cout << "Can't open file " << shap_file << std::endl; - ERROR_MESSAGE(); - throw std::runtime_error(" Cannot open file."); - exit(1); + shap_input.close(); + } + /*else if (i == 1) + { + std::cout << "Can't open file " << shap_file << std::endl; + ERROR_MESSAGE(); + throw std::runtime_error(" Cannot open file."); + exit(1); + }*/ } - } + std::cout << galaxies.size() << " shapelet sources out of " << max_num + << " passed selection." << std::endl; band = galaxies[0].getBand(); } From 87c4ffadffa81f977e7cc15cb37011c76804a1f6 Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Sun, 26 May 2019 12:16:02 +0200 Subject: [PATCH 142/227] Fixed bug in LensHalo move assignment operator. --- include/disk.h | 3 +++ 1 file changed, 3 insertions(+) diff --git a/include/disk.h b/include/disk.h index 082e8a79..e740ef27 100644 --- a/include/disk.h +++ b/include/disk.h @@ -34,8 +34,11 @@ class LensHaloDisk:public LensHaloParticles > { } LensHaloDisk & operator=(LensHaloDisk &&h){ + assert(&h != this); + LensHaloParticles >::operator=(std::move(h)); + particles = LensHaloParticles >::trash_collector; qrot_invers = h.qrot_invers; // rotation that brings the disk back to face on Rscale = h.Rscale; Rhight = h.Rhight; From e0e5689cfab513eb79a6795ea67095682dae2255 Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Sun, 26 May 2019 15:36:33 +0200 Subject: [PATCH 143/227] Assert statement. --- include/grid_maintenance.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/include/grid_maintenance.h b/include/grid_maintenance.h index 11c64cb7..bbac9350 100644 --- a/include/grid_maintenance.h +++ b/include/grid_maintenance.h @@ -96,6 +96,8 @@ struct Grid{ } Grid & operator=(Grid &&grid){ + assert(&grid != this); + i_tree = grid.i_tree; grid.i_tree = nullptr; s_tree = grid.s_tree; From 38514160b30ce429439257362de3d5d800cb73e5 Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Sun, 26 May 2019 19:59:59 +0200 Subject: [PATCH 144/227] Changed possible problem in move cc LensHaloParticle --- include/particle_halo.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/particle_halo.h b/include/particle_halo.h index eeda5247..8569c6b5 100644 --- a/include/particle_halo.h +++ b/include/particle_halo.h @@ -71,7 +71,7 @@ class LensHaloParticles : public LensHalo LensHaloParticles(LensHaloParticles &&h):LensHalo(std::move(h)){ mcenter = h.mcenter; trash_collector =std::move(h.trash_collector); - pp = h.pp; // note: this depends on std:move keeping the pointer valid if it is constructed with the public constructor + pp = trash_collector.data(); h.pp = nullptr; min_size = h.min_size; From e52ef8a8e06a5d46924097d4f8289a8afb098a67 Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Mon, 27 May 2019 11:14:25 +0200 Subject: [PATCH 145/227] Suppressed some verbosity. --- include/particle_halo.h | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/include/particle_halo.h b/include/particle_halo.h index 8569c6b5..0f99754b 100644 --- a/include/particle_halo.h +++ b/include/particle_halo.h @@ -114,7 +114,8 @@ class LensHaloParticles : public LensHalo static void calculate_smoothing(int Nsmooth ,PType *pp - ,size_t Npoints); + ,size_t Npoints + ,bool verbose = false); void readPositionFileASCII(const std::string &filename); @@ -572,11 +573,13 @@ void LensHaloParticles::rotate_particles(PosType theta_x,PosType theta_y) template void LensHaloParticles::calculate_smoothing(int Nsmooth,PType *pp - ,size_t Npoints){ + ,size_t Npoints + ,bool verbose + ){ int nthreads = Utilities::GetNThreads(); - std::cout << "Calculating smoothing of particles ..." << std::endl + if(verbose) std::cout << "Calculating smoothing of particles ..." << std::endl << Nsmooth << " neighbors. If there are a lot of particles this could take a while." << std::endl; time_t to,t; @@ -606,7 +609,7 @@ void LensHaloParticles::calculate_smoothing(int Nsmooth,PType *pp for(int ii = 0; ii < nthreads ;++ii) thr[ii].join(); } time(&t); - std::cout << "done in " << difftime(t,to) << " secs" << std::endl; + if(verbose) std::cout << "done in " << difftime(t,to) << " secs" << std::endl; } template From fc56a87b8acd34bee0826bc6cd1c23a4ed7f3ff6 Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Mon, 27 May 2019 13:36:17 +0200 Subject: [PATCH 146/227] Put a check in Lens::getMainHalo<> --- include/lens.h | 1 + 1 file changed, 1 insertion(+) diff --git a/include/lens.h b/include/lens.h index a494b101..1ca7c3b1 100644 --- a/include/lens.h +++ b/include/lens.h @@ -737,6 +737,7 @@ inline LensHalo* Lens::getMainHalo(std::size_t i) template inline HaloType* Lens::getMainHalo(std::size_t i) { + if(main_halos.size() == 0 ) return nullptr; return main_halos.at(i); } From 5f37ca7cc3804afc46dc18a384a9fc9a083918b6 Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Wed, 29 May 2019 20:15:34 +0200 Subject: [PATCH 147/227] Fixed bug that was not setting the bands correctly in the SourceOverzierPlus class after randomisation. --- AnalyticNSIE/sourceAnaGalaxy.cpp | 12 ++--- Source/overzier.cpp | 78 ++++++++++++++++++-------------- include/disk.h | 2 +- include/overzier_source.h | 35 ++++++++++---- include/sourceAnaGalaxy.h | 6 +-- include/utilities_slsim.h | 3 ++ 6 files changed, 81 insertions(+), 55 deletions(-) diff --git a/AnalyticNSIE/sourceAnaGalaxy.cpp b/AnalyticNSIE/sourceAnaGalaxy.cpp index 7efc4e7a..19d2f1e1 100644 --- a/AnalyticNSIE/sourceAnaGalaxy.cpp +++ b/AnalyticNSIE/sourceAnaGalaxy.cpp @@ -14,7 +14,7 @@ SourceMultiAnaGalaxy::SourceMultiAnaGalaxy( PosType mag /// Total magnitude ,PosType mag_bulge /// magnitude of Bulge ,PosType Reff /// Bulge half light radius (arcs) - ,PosType Rh /// disk scale hight (arcs) + ,PosType Rdisk /// disk scale hight (arcs) ,PosType PA /// Position angle (radians) ,PosType inclination /// inclination of disk (radians) ,PosType my_z /// redshift of source @@ -22,7 +22,7 @@ SourceMultiAnaGalaxy::SourceMultiAnaGalaxy( ,Utilities::RandomNumbers_NR &ran ): Source(),index(0){ - galaxies.push_back(SourceOverzierPlus(mag,mag_bulge,Reff,Rh,PA,inclination,0,my_z,my_theta,ran)); + galaxies.push_back(SourceOverzierPlus(mag,mag_bulge,Reff,Rdisk,PA,inclination,0,my_z,my_theta,ran)); } /** Constructor for passing in a pointer to the galaxy model or a list of galaxies instead of constructing it internally. * Useful when there is a list of pre-allocated sources. The redshifts and sky positions need to be set separately. @@ -71,7 +71,7 @@ void SourceMultiAnaGalaxy::readDataFileMillenn(Utilities::RandomNumbers_NR &ran) unsigned long i,j; unsigned long GalID,HaloID; - PosType ra,dec,z_cosm,z_app,Dlum,inclination,pa,Rh,Ref,SDSS_u,SDSS_g + PosType ra,dec,z_cosm,z_app,Dlum,inclination,pa,Rdisk,Ref,SDSS_u,SDSS_g ,SDSS_r,SDSS_i,SDSS_z,J_band,H_band,Ks_band,i1,i2 ,SDSS_u_Bulge,SDSS_g_Bulge,SDSS_r_Bulge,SDSS_i_Bulge,SDSS_z_Bulge ,J_band_Bulge,H_band_Bulge,Ks_band_Bulge,i1_Bulge,i2_Bulge; @@ -147,7 +147,7 @@ void SourceMultiAnaGalaxy::readDataFileMillenn(Utilities::RandomNumbers_NR &ran) addr[6] = &Dlum; addr[7] = &inclination; addr[8] = &pa; - addr[9] = &Rh; + addr[9] = &Rdisk; addr[10] = &Ref; addr[11] = &SDSS_u; @@ -242,7 +242,7 @@ void SourceMultiAnaGalaxy::readDataFileMillenn(Utilities::RandomNumbers_NR &ran) //std::cout << "adding to galaxies" << std::endl; /***************************/ - SourceOverzierPlus galaxy(SDSS_i,SDSS_i_Bulge,Ref,Rh + SourceOverzierPlus galaxy(SDSS_i,SDSS_i_Bulge,Ref,Rdisk ,pa,inclination,HaloID,z_cosm,theta,ran); //std::cout << "filling last galaxy" << std::endl; @@ -353,7 +353,7 @@ void SourceMultiAnaGalaxy::multiplier( theta[1] = x1[1] + (x2[1] - x1[1])*ran(); galaxies.push_back(SourceOverzierPlus(galaxies[i].getMag(),galaxies[i].getMagBulge() - ,galaxies[i].getReff(),galaxies[i].getRh(),ran()*PI,ran()*2*PI + ,galaxies[i].getReff(),galaxies[i].getRdisk(),ran()*PI,ran()*2*PI ,Nold+NtoAdd,galaxies[i].getZ(),theta,ran)); ++NtoAdd; diff --git a/Source/overzier.cpp b/Source/overzier.cpp index d547345f..a15b89bd 100644 --- a/Source/overzier.cpp +++ b/Source/overzier.cpp @@ -19,7 +19,7 @@ SourceOverzier::SourceOverzier( PosType my_mag /// Total magnitude ,double my_mag_bulge /// Bulge to total ratio ,double my_Reff /// Bulge half light radius (arcs) - ,double my_Rh /// disk scale hight (arcs) + ,double my_Rdisk /// disk scale hight (arcs) ,double my_PA /// Position angle (radians) ,double my_inclination /// inclination of disk (radians) ,unsigned long my_id /// id number @@ -28,9 +28,11 @@ SourceOverzier::SourceOverzier( ){ //std::cout << "SourceOverzier constructor" << std::endl; - setInternals(my_mag,my_mag_bulge,my_Reff,my_Rh,my_PA,my_inclination,my_id,my_z,my_theta); + setInternals(my_mag,my_mag_bulge,my_Reff + ,my_Rdisk,my_PA,my_inclination + ,my_id,my_z,my_theta); - assert(current.Reff != 0 || current.Rh !=0 ); + assert(current.Reff != 0 || current.Rdisk !=0 ); } SourceOverzier::~SourceOverzier() @@ -54,22 +56,22 @@ SourceOverzier& SourceOverzier::operator=(const SourceOverzier &s){ } /// Sets internal variables. If default constructor is used this must be called before the surface brightness function. -void SourceOverzier::setInternals(double my_mag,double my_mag_bulge,double my_Reff,double my_Rh,double my_PA,double incl,unsigned long my_id,double my_z,const double *my_theta){ +void SourceOverzier::setInternals(double my_mag,double my_mag_bulge,double my_Reff,double my_Rdisk,double my_PA,double incl,unsigned long my_id,double my_z,const double *my_theta){ haloID = my_id; if(my_Reff < 0.05) my_Reff = 0.05; // ???? current.Reff = my_Reff*arcsecTOradians; - current.Rh = my_Rh*arcsecTOradians; + current.Rdisk = my_Rdisk*arcsecTOradians; current.mag = my_mag; current.mag_bulge = my_mag_bulge; current.PA = my_PA; current.inclination = incl; - if(current.Rh > 0.0){ - current.cxx = ( pow(cos(current.PA),2) + pow(sin(current.PA)/cos(incl),2) )/current.Rh/current.Rh; - current.cyy = ( pow(sin(current.PA),2) + pow(cos(current.PA)/cos(incl),2) )/current.Rh/current.Rh; - current.cxy = ( 2*cos(current.PA)*sin(current.PA)*(1-pow(1/cos(incl),2)) )/current.Rh/current.Rh; + if(current.Rdisk > 0.0){ + current.cxx = ( pow(cos(current.PA),2) + pow(sin(current.PA)/cos(incl),2) )/current.Rdisk/current.Rdisk; + current.cyy = ( pow(sin(current.PA),2) + pow(cos(current.PA)/cos(incl),2) )/current.Rdisk/current.Rdisk; + current.cxy = ( 2*cos(current.PA)*sin(current.PA)*(1-pow(1/cos(incl),2)) )/current.Rdisk/current.Rdisk; }else{ current.cxx = current.cyy = current.cxy = 0.0; } @@ -82,9 +84,9 @@ void SourceOverzier::setInternals(double my_mag,double my_mag_bulge,double my_Re // radius // weighted mean between the radii that enclose 99% of the flux // in the pure De Vacouleur/exponential disk case - // 6.670 = 3.975*Re = 3.975*1.678*Rh + // 6.670 = 3.975*Re = 3.975*1.678*Rdisk float BtoT = getBtoT(); - setRadius(6.670*current.Rh*(1 - BtoT) + 18.936 * current.Reff * BtoT); + setRadius(6.670*current.Rdisk*(1 - BtoT) + 18.936 * current.Reff * BtoT); // position if(my_theta != NULL) @@ -139,14 +141,6 @@ PosType SourceOverzier::getMagBulge(Band band) const { return current.bulge_mag_map.at(band); } -void SourceOverzier::setMag(Band band,PosType my_mag){ - current.mag_map[band] = my_mag; - } - -void SourceOverzier::setMagBulge(Band band,PosType my_mag){ - current.bulge_mag_map[band] = my_mag; - } - void SourceOverzier::changeBand(Band band){ current.mag = current.mag_map[band]; @@ -157,8 +151,8 @@ void SourceOverzier::setMagBulge(Band band,PosType my_mag){ void SourceOverzier::renormalize_current(){ float BtoT = getBtoT(); - if(current.Rh > 0.0) current.sbDo = pow(10,-current.mag/2.5)*0.159148*(1-BtoT) - /pow(current.Rh,2); + if(current.Rdisk > 0.0) current.sbDo = pow(10,-current.mag/2.5)*0.159148*(1-BtoT) + /pow(current.Rdisk,2); else current.sbDo = 0.0; if(current.Reff > 0.0) current.sbSo = pow(10,-current.mag/2.5)*94.484376*BtoT /pow(current.Reff,2); @@ -166,8 +160,8 @@ void SourceOverzier::renormalize_current(){ } -SourceOverzierPlus::SourceOverzierPlus(PosType my_mag,PosType my_mag_bulge,PosType my_Reff,PosType my_Rh,PosType my_PA,PosType inclination,unsigned long my_id,PosType my_z,const PosType *theta,Utilities::RandomNumbers_NR &ran): -SourceOverzier(my_mag,my_mag_bulge,my_Reff,my_Rh,my_PA,inclination,my_id,my_z,theta) +SourceOverzierPlus::SourceOverzierPlus(PosType my_mag,PosType my_mag_bulge,PosType my_Reff,PosType my_Rdisk,PosType my_PA,PosType inclination,unsigned long my_id,PosType my_z,const PosType *theta,Utilities::RandomNumbers_NR &ran): +SourceOverzier(my_mag,my_mag_bulge,my_Reff,my_Rdisk,my_PA,inclination,my_id,my_z,theta) { assert(my_mag_bulge >= my_mag); //std::cout << "SourceOverzierPlus constructor" << std::endl; @@ -202,7 +196,7 @@ SourceOverzier(my_mag,my_mag_bulge,my_Reff,my_Rh,my_PA,inclination,my_id,my_z,th mod = 2.0e-2*ran(); } - assert(original.Rh != 0 || original.Reff != 0); + assert(original.Rdisk != 0 || original.Reff != 0); } SourceOverzierPlus::~SourceOverzierPlus(){ @@ -224,6 +218,15 @@ Narms(p.Narms),Ad(p.Ad),mctalpha(p.mctalpha),arm_alpha(p.arm_alpha) ,p.spheroid->getTheta().x); */ + Narms=p.Narms; + Ad=p.Ad; + mctalpha=p.mctalpha; + arm_alpha=p.arm_alpha; + disk_phase=p.disk_phase; + cospa=p.cospa; + sinpa=p.sinpa; + cosi=p.cosi; + spheroid = p.spheroid; modes = p.modes; original = p.original; @@ -266,7 +269,7 @@ PosType SourceOverzierPlus::SurfaceBrightness(PosType *y){ z[1] = ( sinpa*x[0] + cospa*x[1] )/cosi; //PosType R = sqrt( cxx*x[0]*x[0] + cyy*x[1]*x[1] + cxy*x[0]*x[1] ); - PosType R = z.length()/current.Rh; + PosType R = z.length()/current.Rdisk; PosType theta = atan2(z[1],z[0]); //disk @@ -341,8 +344,11 @@ void SourceOverzierPlus::randomize(Utilities::RandomNumbers_NR &ran){ float BtoT; { // SourceOverzier variables - current.Reff *= (1 + 0.2*(2*ran()-1.)); - current.Rh *= (1 + 0.2*(2*ran()-1.)); + float minsize = 0.01*arcsecTOradians; + current.Reff *= MAX( (1 + 0.2*(2*ran()-1.)) + , minsize ); + current.Rdisk *= MAX( (1 + 0.2*(2*ran()-1.)) + , minsize); PosType tmp = 0.01*(2*ran()-1.); @@ -366,16 +372,16 @@ void SourceOverzierPlus::randomize(Utilities::RandomNumbers_NR &ran){ current.mag += tmp; current.PA = PI*ran(); - current.inclination = MIN(ran()*PI/2,0.95*PI/2); + current.inclination = MIN(ran()*PI/2,0.8*PI/2); cospa = cos(current.PA); sinpa = sin(current.PA); cosi = cos(current.inclination); - if(current.Rh > 0.0){ - current.cxx = ( cospa*cospa + pow(sinpa/cosi,2) )/current.Rh/current.Rh; - current.cyy = ( sinpa*sinpa + pow(cospa/cosi,2) )/current.Rh/current.Rh; - current.cxy = ( 2*cospa*sinpa*(1-1./cosi/cosi) ) /current.Rh/current.Rh; + if(current.Rdisk > 0.0){ + current.cxx = ( cospa*cospa + pow(sinpa/cosi,2) )/current.Rdisk/current.Rdisk; + current.cyy = ( sinpa*sinpa + pow(cospa/cosi,2) )/current.Rdisk/current.Rdisk; + current.cxy = ( 2*cospa*sinpa*(1-1./cosi/cosi) ) /current.Rdisk/current.Rdisk; }else{ current.cxx = current.cyy = current.cxy = 0.0; } @@ -385,9 +391,9 @@ void SourceOverzierPlus::randomize(Utilities::RandomNumbers_NR &ran){ // radius // weighted mean between the radii that enclose 99% of the flux // in the pure De Vacouleur/exponential disk case - // 6.670 = 3.975*Re = 3.975*1.678*Rh + // 6.670 = 3.975*Re = 3.975*1.678*Rdisk BtoT = getBtoT(); - setRadius(6.670*current.Rh*(1-BtoT) + 18.936*current.Reff*BtoT); + setRadius(6.670*current.Rdisk*(1-BtoT) + 18.936*current.Reff*BtoT); } float minA = 0.01,maxA = 0.2; // minimum and maximum amplitude of arms @@ -403,7 +409,9 @@ void SourceOverzierPlus::randomize(Utilities::RandomNumbers_NR &ran){ //double index = 4 + 3*(ran()-0.5)*2; - double index = 4*pow(MAX(BtoT,0.03),0.4)*pow(10,0.2*(ran()-0.5)); + //double index = 4*pow(MAX(BtoT,0.03),0.4)*pow(10,0.2*(ran()-0.5)); + //double index = 4*pow(10,0.2*(ran()-0.5)); + double index = ran() + 3.5; assert(index > 0.5 && index < 9); double q = 1 + (0.5-1)*ran(); diff --git a/include/disk.h b/include/disk.h index e740ef27..b3c63431 100644 --- a/include/disk.h +++ b/include/disk.h @@ -127,7 +127,7 @@ qrot_invers(1,0,0,0),Rscale(disk_scale),Rhight(Rperp),zpa(-my_PA),inclination(my ++i; } - LensHalo::Rmax = -5 * Rscale * log(1 - (float)(N-1) / N ); + LensHalo::Rmax = -8 * Rscale * log(1 - (float)(N-1) / N ); LensHalo::setRsize( LensHalo::Rmax ); LensHaloParticles >::calculate_smoothing(Nsmooth,particles.data(),particles.size()); diff --git a/include/overzier_source.h b/include/overzier_source.h index 23d9d9f5..aa300ea7 100644 --- a/include/overzier_source.h +++ b/include/overzier_source.h @@ -23,13 +23,13 @@ class SourceOverzier : public Source { public: //SourceOverzier(); - SourceOverzier(PosType mag,PosType mag_bulge,PosType Reff,PosType Rh,PosType PA,PosType inclination,unsigned long my_id,PosType my_z=0,const PosType *theta=0); + SourceOverzier(PosType mag,PosType mag_bulge,PosType Reff,PosType Rdisk,PosType PA,PosType inclination,unsigned long my_id,PosType my_z=0,const PosType *theta=0); SourceOverzier(const SourceOverzier &s); SourceOverzier& operator=(const SourceOverzier &s); virtual ~SourceOverzier(); - void setInternals(PosType mag,PosType BtoT,PosType Reff,PosType Rh,PosType PA,PosType inclination,unsigned long my_id,PosType my_z=0,const PosType *my_theta=0); + void setInternals(PosType mag,PosType BtoT,PosType Reff,PosType Rdisk,PosType PA,PosType inclination,unsigned long my_id,PosType my_z=0,const PosType *my_theta=0); virtual PosType SurfaceBrightness(PosType *x); PosType getTotalFlux() const; void printSource(); @@ -63,15 +63,18 @@ class SourceOverzier : public Source */ /// magnitude in specific band - void setMag(Band band,PosType my_mag); - + virtual void setMag(Band band,PosType my_mag){ + current.mag_map[band] = my_mag; + }; /// magnitude in specific band - void setMagBulge(Band band,PosType my_mag); + virtual void setMagBulge(Band band,PosType my_mag){ + current.bulge_mag_map[band] = my_mag; + } /// bulge half light radius in arcseconds PosType getReff() const { return current.Reff/arcsecTOradians; } /// disk scale height in arcseconds - PosType getRh() const { return current.Rh/arcsecTOradians; } + PosType getRdisk() const { return current.Rdisk/arcsecTOradians; } /// the bulge to total flux ratio PosType getBtoT() const { return pow(10,(-current.mag_bulge + current.mag)/2.5); } @@ -88,7 +91,7 @@ class SourceOverzier : public Source /** Returns minimum of the radii at which disk and bulge have a surf. brightness equal to a fraction f of the central one * TODO: Fabio: Needs to be tested and improved (Bulge is so steep in the center that output values are very small) */ - inline PosType getMinSize(PosType f) {return std::min(1.678*current.Reff*fabs(cos(current.inclination))*pow(-log (f)/7.67,4),current.Rh*(-log (f)/1.67));} + inline PosType getMinSize(PosType f) {return std::min(1.678*current.Reff*fabs(cos(current.inclination))*pow(-log (f)/7.67,4),current.Rdisk*(-log (f)/1.67));} static PosType *getx(SourceOverzier &sourceo){return sourceo.source_x.x;} @@ -103,10 +106,11 @@ class SourceOverzier : public Source unsigned long haloID; struct Params{ + /// bulge half light radius PosType Reff=0; /// disk scale height - PosType Rh=0; + PosType Rdisk=0; //PosType BtoT; PosType PA=0; @@ -125,7 +129,7 @@ class SourceOverzier : public Source void print(){ /// bulge half light radius std::cout << "Reff :" << Reff/arcsecTOradians << " arcsec "; - std::cout << "Rh :" << Rh/arcsecTOradians << " arcsec "; + std::cout << "Rdisk :" << Rdisk/arcsecTOradians << " arcsec "; std::cout << "PA :" << PA << " "; std::cout << "inclination :" << inclination << " radians"; std::cout << "sbDo :" << sbDo << " "; @@ -149,7 +153,7 @@ class SourceOverzierPlus : public SourceOverzier PosType my_mag /// total magnitude ,PosType my_mag_bulge /// magnitude of bulge ,PosType my_Reff /// effective radius of bulge - ,PosType my_Rh /// scale hight of disk + ,PosType my_Rdisk /// scale hight of disk ,PosType my_PA /// position angle ,PosType inclination /// inclination ,unsigned long my_id @@ -169,6 +173,17 @@ class SourceOverzierPlus : public SourceOverzier PosType SurfaceBrightness(PosType *y); + /// magnitude in specific band + void setMag(Band band,PosType my_mag){ + current.mag_map[band] = my_mag; + original.mag_map[band] = my_mag; + } + /// magnitude in specific band + void setMagBulge(Band band,PosType my_mag){ + current.bulge_mag_map[band] = my_mag; + original.bulge_mag_map[band] = my_mag; + } + int getNarms() const {return Narms;} PosType getArmAmplitude() const {return Ad;} PosType getArmAlpha() const {return arm_alpha;} diff --git a/include/sourceAnaGalaxy.h b/include/sourceAnaGalaxy.h index f17e3f36..b56ac61e 100644 --- a/include/sourceAnaGalaxy.h +++ b/include/sourceAnaGalaxy.h @@ -31,7 +31,7 @@ */ class SourceMultiAnaGalaxy: public Source{ public: - SourceMultiAnaGalaxy(PosType mag, PosType mag_bulge, PosType Reff, PosType Rh, PosType PA, PosType inclination,PosType my_z,PosType *my_theta,Utilities::RandomNumbers_NR &ran); + SourceMultiAnaGalaxy(PosType mag, PosType mag_bulge, PosType Reff, PosType Rdisk, PosType PA, PosType inclination,PosType my_z,PosType *my_theta,Utilities::RandomNumbers_NR &ran); SourceMultiAnaGalaxy(SourceOverzierPlus *my_galaxy); SourceMultiAnaGalaxy(InputParams& params,Utilities::RandomNumbers_NR &ran); ~SourceMultiAnaGalaxy(); @@ -80,7 +80,7 @@ class SourceMultiAnaGalaxy: public Source{ /// Return redshift of current source. //PosType getZ() const {return galaxies[index].getZ();} PosType getZ() const {return galaxies[index].getZ();} - //PosType getRadius() const {return max(galaxies[index]->Reff,galaxies[index]->Rh);} + //PosType getRadius() const {return max(galaxies[index]->Reff,galaxies[index]->Rdisk);} PosType getRadius() const {return galaxies[index].getRadius();} /// Set redshift of current source. Only changes the redshift while leaving position fixed. void setZ(PosType my_z){ galaxies[index].setZ(my_z);} @@ -214,7 +214,7 @@ class SourceMultiShapelets: public Source{ /// Return redshift of current source. PosType getZ() const {return galaxies[index].getZ();} - //PosType getRadius() const {return max(galaxies[index]->Reff,galaxies[index]->Rh);} + //PosType getRadius() const {return max(galaxies[index]->Reff,galaxies[index]->Rdisk);} PosType getRadius() const {return galaxies[index].getRadius();} /** Used to change the "current" source that is returned when the surface brightness is subsequently diff --git a/include/utilities_slsim.h b/include/utilities_slsim.h index 4d72cff2..e22df819 100644 --- a/include/utilities_slsim.h +++ b/include/utilities_slsim.h @@ -1734,6 +1734,9 @@ class DataFrame{ /// returns column by name std::vector& operator[](const std::string &label){ + if(datamap.find(label) == datamap.end()){ + throw std::invalid_argument("no label"); + } return data[datamap[label]]; for(auto c : column_names ) std::cout << c << " "; std::cout << std::endl; From a4351156dd8f95305fe54018dd345c2fa227bb9c Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Thu, 30 May 2019 18:36:42 +0200 Subject: [PATCH 148/227] Corrected things related to the position angle of the Sources and the LensHaloDisk so they all use the same definition. --- Source/overzier.cpp | 14 ++++++-------- include/disk.h | 33 ++++++++++++++++++++++++++------- include/overzier_source.h | 2 +- 3 files changed, 33 insertions(+), 16 deletions(-) diff --git a/Source/overzier.cpp b/Source/overzier.cpp index a15b89bd..6bd2c557 100644 --- a/Source/overzier.cpp +++ b/Source/overzier.cpp @@ -183,12 +183,10 @@ SourceOverzier(my_mag,my_mag_bulge,my_Reff,my_Rdisk,my_PA,inclination,my_id,my_z double q = 1 - 0.5*ran(); spheroid.setSersicIndex(index); - //spheroid = new SourceSersic(my_mag_bulge,my_Reff,-my_PA + 10*(ran() - 0.5)*PI/180,index,q,my_z,theta); - - spheroid.ReSet(my_mag_bulge,my_Reff,-my_PA + 10*(ran() - 0.5)*PI/180,index,q,my_z,theta); + spheroid.ReSet(my_mag_bulge,my_Reff,my_PA + 10*(ran() - 0.5)*PI/180,index,q,my_z,theta); cospa = cos(current.PA); - sinpa = sin(current.PA); + sinpa = sin(-current.PA); cosi = cos(current.inclination); modes.resize(6); @@ -372,10 +370,10 @@ void SourceOverzierPlus::randomize(Utilities::RandomNumbers_NR &ran){ current.mag += tmp; current.PA = PI*ran(); - current.inclination = MIN(ran()*PI/2,0.8*PI/2); + current.inclination = ran()*0.90*PI/2; cospa = cos(current.PA); - sinpa = sin(current.PA); + sinpa = sin(-current.PA); cosi = cos(current.inclination); if(current.Rdisk > 0.0){ @@ -416,10 +414,10 @@ void SourceOverzierPlus::randomize(Utilities::RandomNumbers_NR &ran){ double q = 1 + (0.5-1)*ran(); /*delete spheroid; - spheroid = new SourceSersic(mag_bulge,Reff/arcsecTOradians,-PA + 10*(ran() - 0.5)*PI/180,index,q,zsource,getTheta().x); + spheroid = new SourceSersic(mag_bulge,Reff/arcsecTOradians,PA + 10*(ran() - 0.5)*PI/180,index,q,zsource,getTheta().x); */ - spheroid.ReSet(current.mag_bulge,current.Reff/arcsecTOradians,-current.PA + 10*(ran() - 0.5)*PI/180,index,q,zsource,getTheta().x); + spheroid.ReSet(current.mag_bulge,current.Reff/arcsecTOradians,current.PA + 5*(ran() - 0.5)*PI/180,index,q,zsource,getTheta().x); for(PosType &mod : modes){ diff --git a/include/disk.h b/include/disk.h index b3c63431..ca4d25ae 100644 --- a/include/disk.h +++ b/include/disk.h @@ -52,7 +52,7 @@ class LensHaloDisk:public LensHaloParticles > { /// Reorient the disk void reorient(float my_inclination,float my_PA){ inclination = my_inclination; - zpa = -my_PA; + zpa = my_PA; Quaternion R = Quaternion::q_z_rotation(zpa)*Quaternion::q_x_rotation(inclination)*qrot_invers; rotate_all(R); @@ -65,7 +65,7 @@ class LensHaloDisk:public LensHaloParticles > { /// inclination in radians, 0 is face on float getInclination(){return inclination;} /// postion angle in radians, - float getPA(){return -zpa;} + float getPA(){return zpa;} private: @@ -102,7 +102,8 @@ LensHaloDisk::LensHaloDisk( ): LensHaloParticles >(redshift,cosmo), particles(LensHaloParticles >::trash_collector), -qrot_invers(1,0,0,0),Rscale(disk_scale),Rhight(Rperp),zpa(-my_PA),inclination(my_inclination) +qrot_invers(1,0,0,0),Rscale(disk_scale),Rhight(Rperp),zpa(my_PA), +inclination(my_inclination) { /// set up base Lenshalo @@ -111,12 +112,30 @@ qrot_invers(1,0,0,0),Rscale(disk_scale),Rhight(Rperp),zpa(-my_PA),inclination(my LensHaloParticles >::pp = particles.data(); LensHaloParticles >::Npoints = particles.size(); - double r,theta; - + double r,theta=0; + double dt = PI + PI*ran()/10.; + + //std::cout << "dt = " << dt/PI << std::endl; size_t i = 0; + double deltaF = 1.0/(N-1); + double x = 0; for(auto &p : particles){ - r = -Rscale * log(1 - (float)(i) / N ); - theta = 2*PI*ran(); + r = Rscale * x; + + // quadratic recursive approximation + //x = x + 0.5*( sqrt(x + 4*(1-x)*exp(x)*deltaF ) - x) + ///(1-x); + + if(x==0){ + x = sqrt(deltaF); + }else{ + x = x + deltaF*exp(x)/x; + } + + assert(!isnan(x)); + //r = -Rscale * log(1 - (float)(i) / N ); + //theta = 2*PI*ran(); + theta += dt; p.x[0] = r*cos(theta); p.x[1] = r*sin(theta); diff --git a/include/overzier_source.h b/include/overzier_source.h index aa300ea7..4211aa2d 100644 --- a/include/overzier_source.h +++ b/include/overzier_source.h @@ -155,7 +155,7 @@ class SourceOverzierPlus : public SourceOverzier ,PosType my_Reff /// effective radius of bulge ,PosType my_Rdisk /// scale hight of disk ,PosType my_PA /// position angle - ,PosType inclination /// inclination + ,PosType inclination /// inclination in radians ,unsigned long my_id ,PosType my_z ,const PosType *theta From 4f9dc0b6630ac5031d94590b264cf48d880bc948 Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Sun, 2 Jun 2019 14:52:47 +0200 Subject: [PATCH 149/227] Changed InputParameters osstream to give all parakeets. --- InputParameters/InputParams.cpp | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/InputParameters/InputParams.cpp b/InputParameters/InputParams.cpp index 2415b183..80c2dc77 100644 --- a/InputParameters/InputParams.cpp +++ b/InputParameters/InputParams.cpp @@ -215,7 +215,23 @@ void InputParams::print_unused() const std::ostream &operator<<(std::ostream &os, InputParams const &p) { std::size_t n = 0; + os << "Used :" << std::endl; for(InputParams::const_iterator it = p.params.begin(); it != p.params.end(); ++it) + { + if(p.use_counter.is_used(it->first)) + { + InputParams::const_iterator comment = p.comments.find(it->first); + if(comment != p.comments.end()) + printrow(os, it->first, it->second, comment->second); + else + printrow(os, it->first, it->second); + ++n; + } + } + + + os << "Unused :" << std::endl; + for(InputParams::const_iterator it = p.params.begin(); it != p.params.end(); ++it) { if(!p.use_counter.is_used(it->first)) { @@ -227,7 +243,6 @@ std::ostream &operator<<(std::ostream &os, InputParams const &p) { ++n; } } - return os; } From 223033304e347ea7cc69fd210df1054c358e079a Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Mon, 3 Jun 2019 15:39:15 +0200 Subject: [PATCH 150/227] Added Grid::magnification() --- ImageProcessing/observation.cpp | 6 +++--- TreeCode_link/grid_maintenance.cpp | 15 +++++++++++++++ include/grid_maintenance.h | 2 ++ 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/ImageProcessing/observation.cpp b/ImageProcessing/observation.cpp index bf6cda95..fde00e1b 100644 --- a/ImageProcessing/observation.cpp +++ b/ImageProcessing/observation.cpp @@ -43,7 +43,7 @@ Npix_x(Npix_x),Npix_y(Npix_y) back_mag = 22.8; ron = 5.; seeing = 0.18; - pix_size = .1/60./60./180.*PI; + pix_size = .1*arcsecTOradians; break; case Euclid_Y: diameter = 119.; @@ -53,7 +53,7 @@ Npix_x(Npix_x),Npix_y(Npix_y) back_mag = 22.57; ron = 5.; seeing = 0.3; - pix_size = .3/60./60./180.*PI; + pix_size = .3*arcsecTOradians; break; case Euclid_J: diameter = 119.; @@ -63,7 +63,7 @@ Npix_x(Npix_x),Npix_y(Npix_y) back_mag = 22.53; ron = 5.; seeing = 0.3; - pix_size = .3/60./60./180.*PI; + pix_size = .3*arcsecTOradians; break; case Euclid_H: diameter = 119.; diff --git a/TreeCode_link/grid_maintenance.cpp b/TreeCode_link/grid_maintenance.cpp index d9e0a468..32110e67 100644 --- a/TreeCode_link/grid_maintenance.cpp +++ b/TreeCode_link/grid_maintenance.cpp @@ -319,6 +319,21 @@ PosType Grid::EinsteinArea() const { return total; } +PosType Grid::magnification() const{ + double mag = 0,flux = 0; + + PointList::iterator it; + it = (i_tree->pointlist->Top()); + size_t N = i_tree->pointlist->size(); + for(unsigned long i=0 ; i < N ; ++i,--it){ + double area = (*it)->gridsize*(*it)->gridsize; + mag += (*it)->surface_brightness*fabs((*it)->invmag)*area; + flux += (*it)->surface_brightness*area; + } + + return flux/mag; +} + /** * \brief Reset the surface brightness and in_image flag in every point on image and source planes to zero (false) */ diff --git a/include/grid_maintenance.h b/include/grid_maintenance.h index bbac9350..028a995d 100644 --- a/include/grid_maintenance.h +++ b/include/grid_maintenance.h @@ -118,6 +118,8 @@ struct Grid{ return *this; } + /// flux weighted magnification + PosType magnification() const; private: void xygridpoints(Point *points,double range,const double *center,long Ngrid ,short remove_center); From fe620a711625fcf7d8470979f48b85cb4d6f8c5c Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Mon, 24 Jun 2019 09:56:32 +0200 Subject: [PATCH 151/227] Changed Utilities::IO::ReadCSVnumerical2() so that it works without column headis --- include/utilities_slsim.h | 35 ++++++++++++++++++++++++++--------- 1 file changed, 26 insertions(+), 9 deletions(-) diff --git a/include/utilities_slsim.h b/include/utilities_slsim.h index e22df819..10a2db07 100644 --- a/include/utilities_slsim.h +++ b/include/utilities_slsim.h @@ -1550,8 +1550,8 @@ namespace Utilities It will skip the comment lines if they are at the head of the file. The number of columns and rows are returned. - Comments must only be before the data. There must be a line with the - column names after the comments and before the data. + Comments must only be before the data. Then if header==true there + should be a line of column names. If header!=true there are non column names. This function is not particularly fast for large amounts of data. If the number of rows is large it would be best to use data.reserve() to set the capacity of data large enough that no rellocation of memory occurs. @@ -1563,12 +1563,13 @@ namespace Utilities ,std::vector &column_names /// list of column names ,size_t Nmax = 1000000 ,char comment_char = '#' /// comment charactor for header - ,char deliniator = ',' /// deliniator between values + ,char deliniator = ',' /// deliniator between values + ,bool header = true /// false if there are no column names ){ std::ifstream file(filename); - // find number of particles + if (!file.is_open()){ std::cerr << "file " << filename << " cann't be opened." << std::endl; throw std::runtime_error("no file"); @@ -1584,25 +1585,39 @@ namespace Utilities std::stringstream lineStream(line); std::string cell; column_names.empty(); - while(std::getline(lineStream,cell, ',')) + + while(std::getline(lineStream,cell,deliniator)) { - column_names.push_back(cell); + column_names.push_back(cell); } + // This checks for a trailing comma with no data after it. if (!lineStream && cell.empty()) { - column_names.push_back(""); + column_names.push_back(""); } int columns = NumberOfEntries(line,deliniator); + size_t ii = 0; for(auto &v : data ) v.empty(); + + if(!header){ + data.emplace_back(columns); + int i=0; + for(auto a : column_names){ + data.back()[i++] = to_numeric(a); + } + ++ii; + } + + while(std::getline(file,line) && ii < Nmax){ data.emplace_back(columns); - // read the names + // read the numbers std::stringstream lineStream(line); std::string cell; @@ -1610,10 +1625,12 @@ namespace Utilities while(std::getline(lineStream,cell, deliniator)) { data.back()[i] = to_numeric(cell); - i = (i+1)%columns; + ++i; } ++ii; } + + return 1; } From dc042a237e185d1ecb26d70d79a82e5097d01bd4 Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Mon, 24 Jun 2019 16:43:43 +0200 Subject: [PATCH 152/227] modified: README.md --- README.md | 132 ++++++++++++++++++++++++------------------------------ 1 file changed, 58 insertions(+), 74 deletions(-) diff --git a/README.md b/README.md index cf688068..de453cbc 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ Documentation {#mainpage} ============= -*GLAMER* is a very flexible computer code for doing simulations of gravitational +*GLAMER* is a flexible computer code for doing simulations of gravitational lensing and modeling of gravitational lenses. This page describes the steps to doing a simple simulation and where in the @@ -15,94 +15,72 @@ Getting started --------------- `GLAMER` is in the form of a C++ library that can be linked into your code. -For instructions on installing and linking the library see -http://metcalf1.bo.astro.it/wiki/projects/glamer/GLAMER.html +For instructions on installing and linking the library see [here](http://metcalf1.bo.astro.it/wiki/projects/glamer/GLAMER.html) Read in Parameters ------------------- +---------------------------- + +Original all objects where constructed from a parameter file through the InputParams class. Many of these constructors still exist, but this method has been depricated. It is recommended that the classes be constructed from thier other constructors now. + -Within your main code the first thing to do is to read in the input parameter -file. Your parameter file should contains all the parameters required to run the -simulation. A sample parameter file is provided in the repository. A particular -simulation will not require all the parameters to be specified. The parameter -file should be read by constructing an `InputParams` object. See the -`InputParams` class for options. If a parameter is required, but not present in -the parameter file the program should throw an exception and notify you of which -parameter needs to be included. If a parameters is in the parameters file that -is not recognized a warning will be printed. - -For a list of currently acceptable parameters with very short descriptions use -`InputParams::sample()`. - -### Example - -In `main()` one needs to first read in the parameter file. -This is done by constructing a `InputParams` object - -~~~{.cpp} -InputParams params(paramfile); -~~~ - -where `paramfile` is a string containing the path and file name of your -parameter file. A sample parameter file with all allowed parameters can be -generated by `InputParams::sample()`. This can be printed to a usable parameters -file with `InputParams::PrintToFile()`. - -~~~{.cpp} -#include "InputParams.h" - -int main(int argc, const char* argv[]) -{ - // create InputParams with sample values - InputParams params = InputParams::sample(); - - // print sample parameter file - params.PrintToFile("sample_paramfile"); - - // done - return EXIT_SUCCESS; -} -~~~ - - -Construct a Source ------------------- -The source constructor takes the `InputParams` object and constructs one or many -sources within a MixedVector member. See the class Source and all +Original all objects where constructed from a parameter file through the InputParams class. Many of these constructors still exist, but this method has been depricated. It is recommended that the classes be constructed from thier other constructors now. -`SourceUniform source(params)` -: Makes the source a uniform surface brightness circle +LensHalo classes +----------------- +To represent the mass that couses the lensing one or more LensHalo objects are constructed. A full list of LensHalos can be found [here](file:///Users/bmetcalf/Glamer/Doc/html/classes.html). Some examples are: -`SourceMultiAnaGalaxy source(params)` -: Makes many individual sources. - Where they are read in is set in the parameter file. -Construct a Lens ----------------- +`LensHaloNFW` - a NFW halo -A lens is constructed +`LensHaloMassMap` - a mass sheet represented with a pixelized map -~~~{.cpp} -Lens lens(params, &seed); -~~~ +`LensHaloParticles` - a mass distribution represented by particles or point masses possibly from a simulation -Multiple components to the lens are stored within the lens. See the `Lens` class -documentationfor how to access them. These can be read in from a file which are -generally called "field" lenses. The "main" lens(es) can be set by parameters -in the parameter file or added by hand within `main()`. +`LensHaloRealNSIE` - a non-singular isothermal ellipical lens +`LensHaloPowerLaw` - a lens with a power-law mass profile -Construct the Grid + + +Source Class ------------------ -The `Grid` structure contains the image points and thier source points along -with other information. Without further grid refinement the `Grid` can be used -to make a map of deflection, convergence, shear or time-delay. `Grid` contains -functions for outputing these. If no further grid refinement is desired for image or caustic finding, -the `GridMap` structure can be used which requires signifcantly less memory overhead. +Sources represent anything that produces a visible image infront of, within or behind the lens. + +Some of them are: + +`SourceUniform` : Makes the source a uniform surface brightness circle + +`SourceGaussian` : Source with a Gaussian profile + +`SourceShapelet` : a source made from a shapelet decomposition + +`SourceSersic` : a source with a Sersic profile + +`SourcePixelled` : a source made from a pixelized image of an object + + +Lens class +---------- + +The lens class contains the LensHalos and keeps the information about the light-cone. + +LensHalos are inserted into the Lens object with there redshift and angular position. Once it is created the Lens owns the LensHalos. + +`Lens::ResetSourcePlane()` is used to change the redshift of the source plane. It can be at lower redshift than some or all of the LensHalos. + +Main halos are ones that are inserted indiviually. Field halos are those that are generated as a population throughout the light cone. These are stored differently within the Lens. + +Grid & GridMap +-------------- + +The `Grid` structure contains the image points and thier source points along with other information. Without further grid refinement the Grid can be used to make a map of deflection, convergence, shear or time-delay. Grid contains functions for outputing these. If no further grid refinement is desired for image or caustic finding, the `GridMap` structure can be used which requires signifcantly less memory overhead. + +To change the source plane surface brightness use `Grid::RefreshSurfaceBrightnesses ()`. + Image finding and Grid refinement @@ -120,6 +98,12 @@ These take a `Grid` and a `Lens` object. The found images are then stored in an array of `ImageInfo` structures. +Observational effects +---------------------- + +The `Observation` class is used to simulate pdf, noise, etc. + + Output ------ @@ -128,4 +112,4 @@ on sources (kappa, gamma, etc.). The ImageInfo structure has some information about the images found and contains a linked list of all the points within the image. Each point contains information about the kappa, gamma, source position, time-delay and in some cases the the surface brightness at that point. -# CosmoLib + From 5e3d612d035b631ebf1d6ac5e2823c5ba47e6ad6 Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Mon, 24 Jun 2019 18:07:26 +0200 Subject: [PATCH 153/227] Changes to comments. --- README.md | 2 +- include/analytic_lens.h | 2 +- include/base_analens.h | 2 +- include/disk.h | 7 +++++++ include/lens_halos.h | 9 +++++---- 5 files changed, 15 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index de453cbc..81e193c9 100644 --- a/README.md +++ b/README.md @@ -56,7 +56,7 @@ Some of them are: `SourceGaussian` : Source with a Gaussian profile -`SourceShapelet` : a source made from a shapelet decomposition +`SourceShapelets` : a source made from a shapelet decomposition `SourceSersic` : a source with a Sersic profile diff --git a/include/analytic_lens.h b/include/analytic_lens.h index 2be3df00..f1b22603 100644 --- a/include/analytic_lens.h +++ b/include/analytic_lens.h @@ -85,7 +85,7 @@ class LensHaloFit : public LensHaloBaseNSIE{ }; /** - * \brief An "analytic" model to represent a lens on a single plane. + * \brief A NSIE lens with distortions in shape, substructures and stars included. * * The lens consists of a "host" lens which is a non-singular isothermal ellipsoid (NSIE) plus axial distortion * modes, substructures and stars. LensHaloAnaNSIE differs from a LensHaloBaseNSIE in that there are additional diff --git a/include/base_analens.h b/include/base_analens.h index dc01ea5c..34bb3c17 100644 --- a/include/base_analens.h +++ b/include/base_analens.h @@ -13,7 +13,7 @@ #include "InputParams.h" #include "lens_halos.h" -/** +/* * \brief An "analytic" model to represent a lens on a single plane. * * The lens consists of a "host" lens which is a non-singular isothermal ellipsoid (NSIE) plus axial distortion diff --git a/include/disk.h b/include/disk.h index ca4d25ae..713992ca 100644 --- a/include/disk.h +++ b/include/disk.h @@ -5,6 +5,13 @@ using Utilities::Geometry::Quaternion; +/** \brief Creates a exponential disk out of particles. + + The disk is created out of particles and the smoothing done by nearest-N neighbour + B-spline smoothing as if they came from a simulation, but they are placed more regularly + so that the surface density is relatively smooth. + */ + template class LensHaloDisk:public LensHaloParticles > { diff --git a/include/lens_halos.h b/include/lens_halos.h index f439266a..572fd515 100644 --- a/include/lens_halos.h +++ b/include/lens_halos.h @@ -15,7 +15,7 @@ #include "particle_types.h" /** - * \brief A base class for all types of lensing halos. + * \brief A base class for all types of lensing "halos" which are any mass distribution that cause lensing. * * It contains the mass, maximum radius (Rsize), and scale radius (rscale) of the halo, * as well as the redshift (zlens). @@ -873,9 +873,7 @@ class LensHaloPseudoNFW: public LensHalo{ }; -/** \ingroup DeflectionL2 - * - * \brief A class for calculating the deflection, kappa and gamma caused by a collection of halos +/** \brief A class for calculating the deflection, kappa and gamma caused by a collection of halos * with truncated power-law mass profiles. * *The truncation is in 2d not 3d. \f$ \Sigma \propto r^{-\beta} \f$ so beta would usually be positive. @@ -942,7 +940,10 @@ class LensHaloPowerLaw: public LensHalo{ +/** \brief Represents a non-singular isothermal elliptical lens +This is a true NSIE lens rather than an expansion that approximates one. +*/ class LensHaloRealNSIE : public LensHalo{ public: From ec3afc5f648ffe4f11c6a677f6fbb7a0d185ba81 Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Tue, 25 Jun 2019 11:58:34 +0200 Subject: [PATCH 154/227] Fixed bug in Observation class. Updated some of the documentation pages. --- INSTALL.md | 45 +++++++++++++++++++++------------ ImageProcessing/observation.cpp | 3 +++ README.md | 43 ++++++++++++++++++++----------- 3 files changed, 60 insertions(+), 31 deletions(-) diff --git a/INSTALL.md b/INSTALL.md index 8c60ae82..8dc3ff0b 100644 --- a/INSTALL.md +++ b/INSTALL.md @@ -26,7 +26,7 @@ binary installation packages available on the *CMake* website. For Linux, *CMake* can be found in most package managers natively. -For Mac OS X, *CMake* is available from both [Homebrew] and [MacPorts]. +For Mac OS X, *CMake* is available from both [Homebrew], [MacPorts] or [conda]. ### Using CMake @@ -47,27 +47,25 @@ the following: 4. Build using your usual method of choice. -Requirements +Dependancies ------------ Before starting to configure and build *GLAMER*, it is advisable to resolve all necessary dependencies, since the cmake configuration will automatically detect settings based on the libraries it can find. -The *GLAMER* library does not need any external libraries for a setup with -reduced functionality. However, most configuration options introduce further -dependencies, which are listed below. +All the dependancies should be available through the package managment system [conda]. It is recommended that you use this to install and update the dependencies. -Library | Required by | Description -----------|---------------|----------------------------------------------------- -[CFITSIO] | `ENABLE_FITS` | Library for reading and writing files in the FITS format. -[CCfits]ยน | `ENABLE_FITS` | C++ wrapper for *CFITSIO*. -[FFTW] | `ENABLE_FFTW` | Library for Fast Fourier Transform algorithms. -[GSL] | `ENABLE_GSL` | Library for scientific computation. +The *GLAMER* library requires some libraries by default and others are optional. -ยน Special care has to be taken when using CCfits with newer versions of the -clang compiler (which is the default in Xcode starting from Mavericks). Please -see the *CCfits* [documentation page][ccfitsdoc] for details. + +Library | Required by || Description +----------|---------------||----------------------------------------------------- +[CFITSIO] | `ENABLE_FITS` |required| Library for reading and writing files in the FITS format. +[CCfits]ยน | `ENABLE_FITS` |required| C++ wrapper for *CFITSIO*. +[FFTW] | `ENABLE_FFTW` |required| Library for Fast Fourier Transform algorithms. +[GSL] | `ENABLE_GSL` |optional| Library for scientific computation +[HDF5 C++] | `ENABLE_HDF5` |optional| I/O in hdf5 format Building the library @@ -89,7 +87,7 @@ In order to build *GLAMER* using cmake, the following steps are necessary: maybe some of the *GLAMER*-specific options. Here we generate Unix Makefiles and enable *FITS* and *FFTW* functions. - $ cmake .. -G "Unix Makefiles" -DENABLE_FITS=ON -DENABLE_FFTW=ON + $ cmake .. -G "Unix Makefiles" [-DENABLE_GSL=ON] 3. At this point, you are presented with a Makefile or project inside the `build` directory which you can use to build the library. @@ -170,7 +168,10 @@ where to find the libraries. ### Creating new projects The easiest way to start a project is to copy the sample project from the -`examples/project` folder. In the enclosed `CMakeLists.txt` file, all instances +`examples/project` folder or clone one of the example projects from the [Glenco Github page]("https://github.com/glenco"). The new project should be in its own directory outside of the *GLAMER* directory tree. + + +In the enclosed `CMakeLists.txt` file, all instances of the "sample" name have to be replaced with the actual name of the project. Sources and headers can be added to the appropriate lists. @@ -207,6 +208,16 @@ subsequently be used with the build tool of choice. $ mkdir build $ cmake .. -G Xcode $ open myproject.xcodeproj +or + + $ cd myproject + $ ls + CMakeLists.txt main.cpp + $ mkdir build + $ cmake .. + $ make +If you are not using Xcode. + **Note:** Building the project will not build the GLAMER libraries automatically! @@ -220,3 +231,5 @@ Building the project will not build the GLAMER libraries automatically! [ccfitsdoc]: https://heasarc.gsfc.nasa.gov/fitsio/CCfits/html/index.html "CCfits documentation" [fftw]: http://www.fftw.org "FFTW Home Page" [gsl]: http://www.gnu.org/software/gsl/ "GNU Scientific Library" +[HDF5 C++]: https://support.hdfgroup.org/HDF5/doc/cpplus_RM/index.html" +[conda]:https://docs.conda.io/projects/conda/en/latest/" \ No newline at end of file diff --git a/ImageProcessing/observation.cpp b/ImageProcessing/observation.cpp index fde00e1b..3c2a3a2f 100644 --- a/ImageProcessing/observation.cpp +++ b/ImageProcessing/observation.cpp @@ -534,6 +534,9 @@ void Observation::ApplyPSF(PixelMap &pmap) */ void Observation::CorrelateNoise(PixelMap &pmap) { + + if(sqrt_noise_power.size()==0)return; + if(pmap.getNx() != pmap.getNy()){ std::cout << "Observation::AddNoise() Doesn't work on nonsquare maps" << std::endl; throw std::runtime_error("nonsquare"); diff --git a/README.md b/README.md index 81e193c9..89205de1 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ Documentation {#mainpage} ============= *GLAMER* is a flexible computer code for doing simulations of gravitational -lensing and modeling of gravitational lenses. +lensing and modelling of gravitational lenses. This page describes the steps to doing a simple simulation and where in the documentation to find further information. The installation and usage of @@ -18,18 +18,23 @@ Getting started For instructions on installing and linking the library see [here](http://metcalf1.bo.astro.it/wiki/projects/glamer/GLAMER.html) +An overview of how the code is structured follows. + + Read in Parameters ---------------------------- -Original all objects where constructed from a parameter file through the InputParams class. Many of these constructors still exist, but this method has been depricated. It is recommended that the classes be constructed from thier other constructors now. - +Originally all objects where constructed from a parameter file through the InputParams class. Many of these constructors still exist, but this method has been deprecated. It is recommended that the classes be constructed from their other constructors now. +Cosmology +--------- -Original all objects where constructed from a parameter file through the InputParams class. Many of these constructors still exist, but this method has been depricated. It is recommended that the classes be constructed from thier other constructors now. +First you must create an instance of the `COSMOLOGY` class. This is taken as an argument to many of the constructors and contains functions for calculating cosmological quantities. Several standard cosmological parameters sets are available +or a new set can be chosen. LensHalo classes ----------------- -To represent the mass that couses the lensing one or more LensHalo objects are constructed. A full list of LensHalos can be found [here](file:///Users/bmetcalf/Glamer/Doc/html/classes.html). Some examples are: +To represent the mass that causes the lensing one or more `LensHalo` objects must be constructed. A full list of LensHalos can be found [here](file:///Users/bmetcalf/Glamer/Doc/html/classes.html). Some examples are: @@ -39,16 +44,17 @@ To represent the mass that couses the lensing one or more LensHalo objects are c `LensHaloParticles` - a mass distribution represented by particles or point masses possibly from a simulation -`LensHaloRealNSIE` - a non-singular isothermal ellipical lens +`LensHaloRealNSIE` - a non-singular isothermal elliptical lens `LensHaloPowerLaw` - a lens with a power-law mass profile +`LensHaloUniform` - to create a uniform shear and/or convergence Source Class ------------------ -Sources represent anything that produces a visible image infront of, within or behind the lens. +Sources represent anything that produces a visible image in front of, within or behind the lens. Some of them are: @@ -68,18 +74,21 @@ Lens class The lens class contains the LensHalos and keeps the information about the light-cone. -LensHalos are inserted into the Lens object with there redshift and angular position. Once it is created the Lens owns the LensHalos. +`LensHalo`s are inserted into the `Lens` object with there redshift and angular position. Once it is created the `Lens` owns copies of the `LensHalo`s. (Large LensHalos can be moved into the Lens using a move copy to avoid too much memory use.) `Lens::ResetSourcePlane()` is used to change the redshift of the source plane. It can be at lower redshift than some or all of the LensHalos. -Main halos are ones that are inserted indiviually. Field halos are those that are generated as a population throughout the light cone. These are stored differently within the Lens. +*Main halos* are ones that are inserted into the lens. Field halos are those that are generated within the Lens as a population throughout the light cone. These are stored differently within the `Lens`. + Grid & GridMap -------------- -The `Grid` structure contains the image points and thier source points along with other information. Without further grid refinement the Grid can be used to make a map of deflection, convergence, shear or time-delay. Grid contains functions for outputing these. If no further grid refinement is desired for image or caustic finding, the `GridMap` structure can be used which requires signifcantly less memory overhead. +The `Grid` structure contains the image points and their source points along with other information. Without further grid refinement the Grid can be used to make a map of deflection, convergence, shear or time-delay. `Grid` contains functions for outputting these. If no further grid refinement is desired for image or caustic finding, the `GridMap` structure can be used which requires significantly less memory overhead and is faster. + +When a `Grid` is constructed the initial grid of rays are shot. If no more refinement is needed the `Lens` is no longer needed. If source plane is changed a new `Grid` needs to be constructed for that source plane. -To change the source plane surface brightness use `Grid::RefreshSurfaceBrightnesses ()`. +To change the source plane surface brightness use `Grid::RefreshSurfaceBrightnesses ()`. This takes a `Source` object. @@ -104,12 +113,16 @@ Observational effects The `Observation` class is used to simulate pdf, noise, etc. +PixelMap class +-------------- + +A `PixelMap` is an all purpose class to represent regular grids such as images and mass maps. It also allows for input / output with fits files. + + Output ------ The `Grid` has some direct output functions for making maps that do not depend -on sources (kappa, gamma, etc.). The ImageInfo structure has some information -about the images found and contains a linked list of all the points within the +on sources (kappa, gamma, etc.). The ImageInfo structure has information about the images found and contains a linked list of all the points within the image. Each point contains information about the kappa, gamma, source position, -time-delay and in some cases the the surface brightness at that point. - +time-delay and in some cases the the surface brightness at that image position. From 57cdd4cdd61d0d8d4394acebb7b17be168f94aa0 Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Tue, 25 Jun 2019 17:17:09 +0200 Subject: [PATCH 155/227] Bug fix in constructor of LensHaloParticles when reading from a ASCII file. --- INSTALL.md | 2 +- MultiPlane/particle_lens.cpp | 7 ++++++- include/particle_halo.h | 3 ++- include/sersic_source.h | 10 +++++++++- 4 files changed, 18 insertions(+), 4 deletions(-) diff --git a/INSTALL.md b/INSTALL.md index 8dc3ff0b..5b64fba9 100644 --- a/INSTALL.md +++ b/INSTALL.md @@ -168,7 +168,7 @@ where to find the libraries. ### Creating new projects The easiest way to start a project is to copy the sample project from the -`examples/project` folder or clone one of the example projects from the [Glenco Github page]("https://github.com/glenco"). The new project should be in its own directory outside of the *GLAMER* directory tree. +`examples/project` folder or clone one of the example projects from the [Glenco Github page]("https://github.com/glenco"), specifically [Example1]("https://github.com/glenco/Example1"), [ExampleImage]("https://github.com/glenco/ExampleImage") and [ParticleExample]("https://github.com/glenco/ParticleExample"). The new project should be in its own directory outside of the *GLAMER* directory tree. In the enclosed `CMakeLists.txt` file, all instances diff --git a/MultiPlane/particle_lens.cpp b/MultiPlane/particle_lens.cpp index d3ac2ac9..798ba652 100644 --- a/MultiPlane/particle_lens.cpp +++ b/MultiPlane/particle_lens.cpp @@ -35,7 +35,10 @@ MakeParticleLenses::MakeParticleLenses( if(format == glmb ){ nparticles.resize(6,0); - readSizesB(filename,data,Nsmooth,nparticles,z_original); + if(!readSizesB(filename,data,Nsmooth,nparticles,z_original)){ + std::cerr << " Cannot read file " << filename << std::endl; + throw std::invalid_argument("Can't find file."); + } }else{ std::string sizefile = filename + "_S" @@ -62,6 +65,8 @@ MakeParticleLenses::MakeParticleLenses( case csv6: readCSV(6); default: + std::cerr << "Data file formate is not supported in MakeParticleLens." << std::endl; + throw std::invalid_argument("missing format"); break; } diff --git a/include/particle_halo.h b/include/particle_halo.h index 0f99754b..6f6b5d64 100644 --- a/include/particle_halo.h +++ b/include/particle_halo.h @@ -345,6 +345,7 @@ void LensHaloParticles::force_halo(double *alpha,KappaType *kappa,KappaTy alpha[1] *= -1; } +/// rotate simulation template void LensHaloParticles::rotate(Point_2d theta){ rotate_particles(theta[0],theta[1]); @@ -374,7 +375,7 @@ void LensHaloParticles::readPositionFileASCII(const std::string &filename std::ifstream myfile(filename); - size_t Npoints = 0; + //size_t Npoints = 0; // find number of particles diff --git a/include/sersic_source.h b/include/sersic_source.h index 94c2b758..ab232d5f 100644 --- a/include/sersic_source.h +++ b/include/sersic_source.h @@ -21,7 +21,15 @@ class SourceSersic : public Source public: /// sets values to invalid values SourceSersic(); - SourceSersic(PosType mag,PosType Reff,PosType PA,PosType my_index,PosType my_q,PosType my_z,const PosType *theta=0); + SourceSersic( + double my_mag /// Total magnitude + ,double my_Reff /// Bulge half light radius (arcs) + ,double my_PA /// Position angle (radians) + ,double my_index /// Sersic index + ,double my_q /// axes ratio + ,double my_z /// redshift + ,const double *my_theta=0 /// optional angular position on the sky + ); ~SourceSersic(); SourceSersic(const SourceSersic &p); From 24f0c9434e3ce6dd0718645947fce8fb48e7d8b0 Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Wed, 26 Jun 2019 19:18:52 +0200 Subject: [PATCH 156/227] Documentation --- INSTALL.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/INSTALL.md b/INSTALL.md index 5b64fba9..935c5296 100644 --- a/INSTALL.md +++ b/INSTALL.md @@ -66,6 +66,7 @@ Library | Required by || Description [FFTW] | `ENABLE_FFTW` |required| Library for Fast Fourier Transform algorithms. [GSL] | `ENABLE_GSL` |optional| Library for scientific computation [HDF5 C++] | `ENABLE_HDF5` |optional| I/O in hdf5 format +[HealPix] | ENABLE_HEALPIX |optional| C++ Healpix interface Building the library @@ -232,4 +233,5 @@ Building the project will not build the GLAMER libraries automatically! [fftw]: http://www.fftw.org "FFTW Home Page" [gsl]: http://www.gnu.org/software/gsl/ "GNU Scientific Library" [HDF5 C++]: https://support.hdfgroup.org/HDF5/doc/cpplus_RM/index.html" +[HealPix]:https://healpix.jpl.nasa.gov/html/Healpix_cxx/index.html" [conda]:https://docs.conda.io/projects/conda/en/latest/" \ No newline at end of file From e7ee4626a5af1c973a7fa1317b2de012975b7b5c Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Thu, 27 Jun 2019 11:11:18 +0200 Subject: [PATCH 157/227] Small bug fix. --- TreeCode_link/TreeDriver.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/TreeCode_link/TreeDriver.cpp b/TreeCode_link/TreeDriver.cpp index 2b62a8b2..01433b29 100644 --- a/TreeCode_link/TreeDriver.cpp +++ b/TreeCode_link/TreeDriver.cpp @@ -1479,7 +1479,7 @@ RAY ImageInfo::closestRay(const Point_2d &y){ Point *p; - double r2min = HUGE; + double r2min = HUGE_VALL; for(Kist::iterator it = imagekist->begin() ; it != imagekist->end() ; ++it){ From be0171e4d3a3dd5d672a076741e2eca4bcb09e89 Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Fri, 28 Jun 2019 11:47:25 +0200 Subject: [PATCH 158/227] Removed groups in doxygen comments --- AnalyticNSIE/lens_expand.cpp | 2 +- AnalyticNSIE/nsie.cpp | 16 ++++++++-------- Fitlens/fitlens.cpp | 14 +++++++------- FullRange/implant_stars.cpp | 2 +- FullRange/internal_rayshooter_multi.cpp | 2 +- ImageProcessing/pixelize.cpp | 2 +- MultiPlane/MOKAlens.cpp | 2 +- TreeCode_link/KistDriver.cpp | 18 +++++++++--------- TreeCode_link/Tree.cpp | 16 ++++++++-------- TreeCode_link/TreeDriver.cpp | 2 +- TreeCode_link/curve_routines.cpp | 8 ++++---- TreeCode_link/dirtycode.cpp | 4 ++-- TreeCode_link/divide_images.cpp | 12 ++++++------ TreeCode_link/double_sort.cpp | 2 +- TreeCode_link/find_crit.cpp | 4 ++-- TreeCode_link/grid_maintenance.cpp | 16 ++++++++-------- TreeCode_link/gridmap.cpp | 4 ++-- TreeCode_link/image_finder_kist.cpp | 10 +++++----- TreeCode_link/map_images.cpp | 6 +++--- TreeCode_link/map_imagesISOP.cpp | 2 +- TreeCode_link/peak_refinement.cpp | 2 +- TreeCode_link/tree_maintenance.cpp | 24 ++++++++++++------------ TreeCode_link/utilities.cpp | 6 +++--- include/Kist.h | 2 +- include/Tree.h | 4 ++-- include/doc.h | 13 ++++--------- include/grid_maintenance.h | 2 +- include/gridmap.h | 2 +- include/image_processing.h | 4 ++-- include/lens_halos.h | 10 +++++----- include/overzier_source.h | 5 +++++ include/slsimlib.h | 2 +- include/source.h | 1 + 33 files changed, 111 insertions(+), 110 deletions(-) diff --git a/AnalyticNSIE/lens_expand.cpp b/AnalyticNSIE/lens_expand.cpp index 35029280..a186de1a 100644 --- a/AnalyticNSIE/lens_expand.cpp +++ b/AnalyticNSIE/lens_expand.cpp @@ -1,4 +1,4 @@ -/** \ingroup FitLensL2 +/** * lens_expand.c * * Created on: Feb 22, 2010 diff --git a/AnalyticNSIE/nsie.cpp b/AnalyticNSIE/nsie.cpp index 739b964c..60e1f26a 100644 --- a/AnalyticNSIE/nsie.cpp +++ b/AnalyticNSIE/nsie.cpp @@ -8,7 +8,7 @@ #include "base_analens.h" #include -/** \ingroup DeflectionL2 \ingroup function +/** * \brief Deflection angle for non-singular isothermal ellipsoid in units of Einstein radii */ void alphaNSIE( @@ -81,7 +81,7 @@ void alphaNSIE( return; } -/**\ingroup DeflectionL2 \ingroup function +/** * \brief Convergence for non-singular isothermal ellipsoid, units \f$ \frac{r_{einstein}}{units(x)} \f$ * or \f$ \frac{\sigma^2}{\Sigma_{crit}G\, units(xt) } \f$ */ @@ -104,7 +104,7 @@ KappaType kappaNSIE( return 0.5*sqrt(f/(b2+bc*bc)); } -/**\ingroup DeflectionL2 \ingroup function +/** * \brief Shear for non-singular isothermal ellipsoid, units \f$ \frac{r_{einstein}}{units(x)} \f$ * or \f$ \frac{\sigma^2}{\Sigma_{crit}G\, units(xt) } \f$ * */ @@ -143,7 +143,7 @@ void gammaNSIE( return; } -/** \ingroup function +/** * \brief Elliptical radius \f$ R^2 = x^2 + f^2 y^2 \f$ of a NonSingular Isothermal Ellipsoid */ @@ -158,7 +158,7 @@ PosType rmaxNSIE( return sqrt( pow(mass*Grav*lightspeed*lightspeed*f/PI/sigma/sigma + rc,2) - rc*rc ); } -/** \ingroup function +/** * \brief Elliptical radius \f$ R^2 = x^2 + f^2 y^2 \f$ given f and position angle of model */ PosType ellipticRadiusNSIE( @@ -206,7 +206,7 @@ namespace Utilities{ -/**\ingroup function +/** * * Structure that does allow the integration of alphaNSIE in phiNSIE. * @@ -249,7 +249,7 @@ struct alphaForInt { -/**\ingroup function +/** * * Compute the potential for the NSIE (in physical Mpc) by integration of alphaNSIE. * @@ -345,7 +345,7 @@ KappaType LensHaloBaseNSIE::phiNSIE(PosType const *xt /// position on the ima -/**\ingroup function +/** * * Quadropole moment of an elliptically truncated NSIE * Units are unit[mass]*unit[Rsize]^2 diff --git a/Fitlens/fitlens.cpp b/Fitlens/fitlens.cpp index 90456e2a..01312f1f 100644 --- a/Fitlens/fitlens.cpp +++ b/Fitlens/fitlens.cpp @@ -8,7 +8,7 @@ static double betaT,*modT,**xobT,**dx_subT,sigGT,*modTT,*modoT,**vT,x_centerT[2] static int NmodT,NsourcesT,NimagesT,*pairingT,degenT,Nmin; static double oldsm;//,tang[2],length,yot[2],radsourceT; -/** \ingroup FitLens +/** * * \brief Wrapper that allows simple lens to be found with a single * lens with a single source and translates result into data structures used in the other code. @@ -42,7 +42,7 @@ void LensHaloFit::FindLensSimple( -/** \ingroup FitLens +/** * * \brief Same as FindLensSimple but with some tests in it. * @@ -276,7 +276,7 @@ bool LensHaloFit::SafeFindLensSimple( } -/** \ingroup FitLens +/** * * \brief Wrapper that allows simple lens to be found with a single * lens with a single source and translates result into data structures used in the other code. @@ -482,7 +482,7 @@ void LensHaloFit::FindLensSimple( -/** \ingroup FitLensL2 +/** L2 * \brief Find most elliptical lens * ****** @@ -738,7 +738,7 @@ double minEllip(double *par){ return sm; } -/** \ingroup FitLensL2 +/** L2 * * \brief Calculates a lens that fits the image positions * @@ -918,7 +918,7 @@ void LensHaloFit::get_perturbmodes(std::vector & ListModes) -/** \ingroup FitLensL2 +/** L2 * * \brief calculate the sources position, surface density and magnification at x * given lens model @@ -1031,7 +1031,7 @@ double LensHaloAnaNSIE::deflect_translated(double beta,double *mod,double *x,dou return kappa; } -/** \ingroup FitLensL2 +/** L2 * \brief find degenerate model most like modo modulo a normalization */ double regularize(int Nmax,int Nmin,int N,int Nsources,int degen diff --git a/FullRange/implant_stars.cpp b/FullRange/implant_stars.cpp index 71fcf289..d60b99e6 100644 --- a/FullRange/implant_stars.cpp +++ b/FullRange/implant_stars.cpp @@ -11,7 +11,7 @@ using namespace std; -/** \ingroup ChangeLens +/** * \brief Implant or randomize stars into the lens around the images. * * The first time this is called the memory for the stars is allocated. diff --git a/FullRange/internal_rayshooter_multi.cpp b/FullRange/internal_rayshooter_multi.cpp index 7f83bf05..24f39184 100644 --- a/FullRange/internal_rayshooter_multi.cpp +++ b/FullRange/internal_rayshooter_multi.cpp @@ -7,7 +7,7 @@ #include "slsimlib.h" -/** \ingroup DeflectionL2 +/** * * \brief This is the function that does the deflection calculation with multiple lens planes. * diff --git a/ImageProcessing/pixelize.cpp b/ImageProcessing/pixelize.cpp index 6563e802..5e66e758 100644 --- a/ImageProcessing/pixelize.cpp +++ b/ImageProcessing/pixelize.cpp @@ -996,7 +996,7 @@ void PixelMap::printFITS(std::string filename,std::vector * neighbors) c // *************************************************/ return; } -/** \ingroup LowLevel +/** * A recessive function that was used in FindAllBoxNeighborsKist(). * It has been known to cause stack overflow. Use _FindAllBoxNeighborsKist_iter instead. */ @@ -144,7 +144,7 @@ void TreeStruct::_FindAllBoxNeighborsKist(Branch *leaf,TreeStruct::iterator &cur return; } -/** \ingroup LowLevel +/** * Used in FindAllBoxNeighborsKist to walk tree for neighbors. */ void TreeStruct::_FindAllBoxNeighborsKist_iter(Branch *leaf,TreeStruct::iterator ¤t,Kist * neighbors) const{ @@ -182,7 +182,7 @@ void TreeStruct::_FindAllBoxNeighborsKist_iter(Branch *leaf,TreeStruct::iterator return; } -/** \ingroup ImageFundingL2 +/** * \brief Finds points within an ellipse * * This becomes less efficient when the ellipse is very elongated. Could @@ -219,7 +219,7 @@ void TreeStruct::PointsWithinEllipKist( } return; } -/** \ingroup ImageFindingL2 +/** * \brief Finds all points in tree that lie within rmax of the point ray[] * * markpoints = 0 does not change in_image variable in any point, gives a list of neighbors @@ -268,7 +268,7 @@ PosType TreeStruct::PointsWithinKist( return maxgridsize; } -/** \ingroup LowLevel +/** * Used in PointsWithinKist() to walk tree.*/ void TreeStruct::_PointsWithinKist(TreeStruct::iterator ¤t,PosType *rmax,Kist * neighborkist ,short markpoints,PosType *maxgridsize,TreeStruct::Globals &globs) const{ @@ -438,7 +438,7 @@ void TreeStruct::_PointsWithinKist(TreeStruct::iterator ¤t,PosType *rmax,K return; } -/** \ingroup ImageFindingL2 +/** * * \brief Finds all points within a circle. Much simpler, iterative algorithm. * @@ -548,7 +548,7 @@ void TreeStruct::PointsWithinKist_iter(const PosType* center,float rmin,float rm } } -/** \ingroup ImageFundingL2 +/** * * \brief Finds nearest neighbor points to ray. * @@ -582,4 +582,4 @@ Point * TreeStruct::NearestNeighborKist(const PosType* center,int Nneighbors,Kis return point; } -*/ \ No newline at end of file +*/ diff --git a/TreeCode_link/Tree.cpp b/TreeCode_link/Tree.cpp index 5f9579fa..1db9091c 100644 --- a/TreeCode_link/Tree.cpp +++ b/TreeCode_link/Tree.cpp @@ -18,7 +18,7 @@ * Returns pointer to new Branch struct. Initializes children pointers to NULL, * and sets data field to input. Private. ************************************************************************/ -/** \ingroup ConstructorL2 +/** * * Gives each branch a unique number even if branches are destroed. */ @@ -146,7 +146,7 @@ void Branch::print(){ std::cout << " refined " << refined << std::endl; } -/** \ingroup ConstructorL2 +/** * void FreeBranch(Branch *branch){ @@ -156,7 +156,7 @@ void FreeBranch(Branch *branch){ return; }*/ - /** \ingroup ConstructorL2 + /** **/ Point *NewPointArray( unsigned long N /// number of points in array @@ -173,7 +173,7 @@ Point *NewPointArray( return points; } -/** \ingroup ConstructorL2 +/** * */ void FreePointArray(Point *array,bool NewXs){ @@ -246,7 +246,7 @@ void TreeStruct::construct_root( //return(tree); } -/** \ingroup ConstructorL2 +/** * \brief Free tree and the linked list of points in it. */ TreeStruct::~TreeStruct(){ @@ -758,7 +758,7 @@ void PrintPoint(Point *point){ std::cout << " gamma = " << point->gamma[0] << " " << point->gamma[1]; std::cout << " invmag " << point->in_image << std::endl; } -/** \ingroup Constructor +/** * Make an array of imageinfo types. */ ImageInfo::ImageInfo(){ @@ -793,7 +793,7 @@ ImageInfo::ImageInfo(){ return imageinfo; }*/ -/** \ingroup Constructor +/** * Destructor of imageinfo types. */ ImageInfo::~ImageInfo(){ @@ -866,7 +866,7 @@ OldImageInfo::~OldImageInfo(){ delete outerborder; } -/** \ingroup LowLevel +/** * step for walking tree by iteration instead of recursion * bool TreeStruct::TreeWalkStep(bool allowDescent){ diff --git a/TreeCode_link/TreeDriver.cpp b/TreeCode_link/TreeDriver.cpp index 01433b29..05955f5b 100644 --- a/TreeCode_link/TreeDriver.cpp +++ b/TreeCode_link/TreeDriver.cpp @@ -10,7 +10,7 @@ //static PosType realray[2]; //Point *point_global; -/** \ingroup ImageFundingL2 +/** * * \brief Finds nearest neighbor points to ray. * diff --git a/TreeCode_link/curve_routines.cpp b/TreeCode_link/curve_routines.cpp index bffcddf1..69decc8a 100644 --- a/TreeCode_link/curve_routines.cpp +++ b/TreeCode_link/curve_routines.cpp @@ -139,7 +139,7 @@ void split_order_curve4(OldImageInfo *curves,int Maxcurves,int *Ncurves){ } namespace Utilities{ - /** \ingroup Utill + /** * * \brief Orders points on a closed curve. * @@ -464,7 +464,7 @@ namespace Utilities{ return true; }*/ -/* \ingroup Utill +/* * * \brief Finds area within a curve by summing every cell. * @@ -1597,7 +1597,7 @@ void splitlist(ListHndl imagelist,OldImageInfo *images,int *Nimages,int Maximage * perform generic tasks. */ namespace Utilities{ - /** \ingroup Utill + /** * \brief windings(): winding number test for a point in a polygon * Returns: Number of times a curves winds around the point x. * @@ -1774,7 +1774,7 @@ namespace Utilities{ return wn; } - /** \ingroup Utill + /** * * \brief determines whether a point is inside a curve, that has been stretched 1.2 times * returns the area of the stretched curve diff --git a/TreeCode_link/dirtycode.cpp b/TreeCode_link/dirtycode.cpp index e13d275e..27d21c66 100644 --- a/TreeCode_link/dirtycode.cpp +++ b/TreeCode_link/dirtycode.cpp @@ -7,7 +7,7 @@ #include "slsimlib.h" -/** \ingroup ImageFindingL2 +/** * * \brief Divides kist of points into friends-of-friends groups with. Does not need * a tree to be built, but takes N^2 time so it is not good for a large number of points. @@ -69,7 +69,7 @@ void _DirtyFoF(Kist * neighbors,Kist * wholekist,PosType linkingle return ; } -/** \ingroup ImageFindingL2 +/** * * \brief Divides kist of points into groups. * diff --git a/TreeCode_link/divide_images.cpp b/TreeCode_link/divide_images.cpp index 6897182a..19921358 100644 --- a/TreeCode_link/divide_images.cpp +++ b/TreeCode_link/divide_images.cpp @@ -6,7 +6,7 @@ */ #include "slsimlib.h" -/** \ingroup ImageFindingL2 +/** * \brief finds the points that are within the circular source and divides * the images. * @@ -35,7 +35,7 @@ void find_divide_images(TreeHndl i_tree,TreeHndl s_tree return; } -/** \ingroup functions +/** s * * Returns the genus of an image by counting the number of disconnected outer borders. * The in_image flag must be set to NO for all points on the grid and are returned to @@ -104,7 +104,7 @@ int ImageGenus(TreeHndl i_tree,ImageInfo *imageinfo){ } -/* \ingroup ImageFindingL2 +/* * divide_images * * \brief `Reorders the image points up into separate images that are linked by cell @@ -213,7 +213,7 @@ void divide_images(TreeHndl i_tree,ImageInfo *imageinfo return; }*/ -/** \ingroup ImageFindingL2 +/** * * divide_images_kist * @@ -391,7 +391,7 @@ void divide_images_kist( return; } -/** \ingroup ImageFindingL2 +/** * * \brief recursive function that un-marks all the points that are attached * to point by cell neighbors that were previously marked in_image==YES @@ -436,7 +436,7 @@ void partition_images(Point *point,unsigned long *N_in_image,TreeHndl i_tree){ return; } -/** \ingroup ImageFindingL2 +/** * * finds all the points in i_tree with in_image = YES that are connected to point * by cell neighbors of cell neighbors. The resulting kist of points diff --git a/TreeCode_link/double_sort.cpp b/TreeCode_link/double_sort.cpp index 9940a8d3..d7aaa749 100644 --- a/TreeCode_link/double_sort.cpp +++ b/TreeCode_link/double_sort.cpp @@ -189,7 +189,7 @@ namespace Utilities{ #undef NSTACK #undef NRANSI - /** \ingroup Utill + /** * \brief Sorts points from smallest to largest according to the value of arr[]. * Sorts arr[] and pointarray[] simultaneously. */ diff --git a/TreeCode_link/find_crit.cpp b/TreeCode_link/find_crit.cpp index 02b8a95d..08d0de4f 100644 --- a/TreeCode_link/find_crit.cpp +++ b/TreeCode_link/find_crit.cpp @@ -12,7 +12,7 @@ #define NMAXCRITS 1000 using namespace std; -/** \ingroup ImageFinding +/** Finding * * \brief Finds critical curves and caustics. * @@ -1662,7 +1662,7 @@ void ImageFinding::IF_routines::refine_crit_in_image( -/** \ingroup ImageFinding +/** Finding * * \brief Finds iso kappa contours. * diff --git a/TreeCode_link/grid_maintenance.cpp b/TreeCode_link/grid_maintenance.cpp index 32110e67..37524a45 100644 --- a/TreeCode_link/grid_maintenance.cpp +++ b/TreeCode_link/grid_maintenance.cpp @@ -12,7 +12,7 @@ std::mutex Grid::grid_mutex; -/** \ingroup Constructor +/** * \brief Constructor for initializing square grid. * * Note: Deflection solver must be specified before creating a Grid. @@ -53,7 +53,7 @@ Grid::Grid( } -/** \ingroup Constructor +/** * \brief Constructor for initializing rectangular grid. * * Cells of grid will always be square with initial resolution rangeX/(Nx-1). @@ -127,7 +127,7 @@ Grid::Grid( } -/** \ingroup Constructor +/** * \brief Destructor for a Grid. Frees all memory. */ Grid::~Grid(){ @@ -140,7 +140,7 @@ Grid::~Grid(){ return; } -/** \ingroup ImageFinding +/** Finding * \brief Reinitializes the grid so that it is back to the original coarse grid, but if * the lens has changed the source positions will be updated. */ @@ -227,7 +227,7 @@ void Grid::ReInitializeGrid(LensHndl lens){ } -/** \ingroup ImageFinding +/** Finding * \brief Reshoot the rays with the same image postions. * * The source positions and source tree are updated to the current lens model. @@ -279,7 +279,7 @@ void Grid::ReShoot(LensHndl lens){ } -/** \ingroup ImageFinding +/** Finding * \brief Recalculate surface brightness at every point without changing the positions of the grid or any lens properties. * * Recalculate the surface brightness at all points on the grid. @@ -352,7 +352,7 @@ PosType Grid::ClearSurfaceBrightnesses(){ return total; } -/** \ingroup ImageFinding +/** Finding * \brief Returns number of points on image plane. */ unsigned long Grid::getNumberOfPoints() const{ @@ -363,7 +363,7 @@ unsigned long Grid::getNumberOfPoints() const{ return i_tree->getTop()->npoints; } -/** \ingroup ImageFindingL2 +/** * * \brief Fundamental function used to divide a leaf in the tree into nine subcells. * diff --git a/TreeCode_link/gridmap.cpp b/TreeCode_link/gridmap.cpp index fc82dd0a..5740f3f2 100644 --- a/TreeCode_link/gridmap.cpp +++ b/TreeCode_link/gridmap.cpp @@ -12,7 +12,7 @@ std::mutex GridMap::grid_mutex; -/** \ingroup Constructor +/** * \brief Constructor for initializing rectangular grid. * * Cells of grid will always be square with initial resolution rangeX/(Nx-1). @@ -77,7 +77,7 @@ GridMap::GridMap( } } -/** \ingroup Constructor +/** * \brief Constructor for initializing square grid. * * Note: Deflection solver must be specified before creating a GridMap. diff --git a/TreeCode_link/image_finder_kist.cpp b/TreeCode_link/image_finder_kist.cpp index d28fb52a..b3fa04f4 100644 --- a/TreeCode_link/image_finder_kist.cpp +++ b/TreeCode_link/image_finder_kist.cpp @@ -15,7 +15,7 @@ static const float FracResTarget = 4.0e-4; //static const float telescope_low = 0.01; //extern const PosType initialgridsize; -/** \ingroup ImageFinding +/** Finding * * \brief Finds images given a source position and size. * @@ -501,7 +501,7 @@ PosType ImageFinding::Temporary::mindyfunc(PosType *x){ + (y[1]-point->image->x[1])*(y[1]-point->image->x[1]); } -/** \ingroup ImageFinding +/** Finding * * \brief Finds images given a source position and size. * @@ -1572,7 +1572,7 @@ void ImageFinding::find_images_microlens_exper( } -/** \ingroup ImageFindingL2 +/** * * \brief Finds images for a given source position and size. Not meant for high level user. * @@ -1770,7 +1770,7 @@ void ImageFinding::image_finder_kist(LensHndl lens, PosType *y_source,PosType r_ } -/** \ingroup ImageFindingL2 +/** * *\brief Refines every point in the given image and its outer border that satisfies the refinement criterion. * @@ -1957,7 +1957,7 @@ int ImageFinding::IF_routines::refine_grid_kist( } -/** \ingroup ImageFindingL2 +/** * * \brief Finds inner and outer borders of an image using bordering box method. * diff --git a/TreeCode_link/map_images.cpp b/TreeCode_link/map_images.cpp index 37aa76c5..4b5a09a6 100644 --- a/TreeCode_link/map_images.cpp +++ b/TreeCode_link/map_images.cpp @@ -14,7 +14,7 @@ const PosType target_all = 1.0e-3; const float tol_UniformMag = 1.0e-3; const bool verbose = false; -/** \ingroup ImageFinding +/** Finding * \brief Find images and refine them based on their surface brightness distribution. * * Uses ImageFinding::find_images_kist() to initially find and refine images and then uses a surface brightness @@ -575,7 +575,7 @@ void ImageFinding::map_images( return ; } -/** \ingroup ImageFinding +/** Finding * \brief Find the images without any additional grid refinement or ray shooting. * * The images and surface brighnesses are found. This will be much faster than map_images(), @@ -1216,7 +1216,7 @@ bool IF_routines::RefinePoint(Point *point,TreeHndl i_tree,PosType image_area,Po return false; } */ -/** \ingroup mageFindingL2 +/** * \brief Checks to see if the image has a nearly uniform magnification across it and thus can be considered linearly * distorted. */ diff --git a/TreeCode_link/map_imagesISOP.cpp b/TreeCode_link/map_imagesISOP.cpp index b6548e9d..ce367200 100644 --- a/TreeCode_link/map_imagesISOP.cpp +++ b/TreeCode_link/map_imagesISOP.cpp @@ -12,7 +12,7 @@ const bool verbose = false; const PosType FracResTarget = 3.0e-5; -/** \ingroup ImageFinding +/** Finding * \brief Find images and refine them based on their surface brightness distribution. * * Uses ImageFinding::find_images_kist() to initially find and refine images and then using diff --git a/TreeCode_link/peak_refinement.cpp b/TreeCode_link/peak_refinement.cpp index b0ac94a4..ae07a4df 100644 --- a/TreeCode_link/peak_refinement.cpp +++ b/TreeCode_link/peak_refinement.cpp @@ -17,7 +17,7 @@ using namespace std; namespace ImageFinding{ -/** \ingroup ImageFinding +/** Finding * * \brief Refines the grid based on the convergence so that high density regions have high resolution. * diff --git a/TreeCode_link/tree_maintenance.cpp b/TreeCode_link/tree_maintenance.cpp index 8d397881..491f081f 100644 --- a/TreeCode_link/tree_maintenance.cpp +++ b/TreeCode_link/tree_maintenance.cpp @@ -142,7 +142,7 @@ TreeStruct::TreeStruct(Point *xp,unsigned long Npoints,short my_median_cut,PosTy //return tree; } -/** \ingroup ImageFindingL2 +/** * \brief Fill a tree with points. The previous tree structure will be destroyed. Used for refilling. */ void TreeStruct::FillTree(Point *xp,unsigned long Npoints){ @@ -171,7 +171,7 @@ void TreeStruct::FillTree(Point *xp,unsigned long Npoints){ return ; } -/** \ingroup ImageFindingL2 +/** * \brief Rebuilds the tree from the points that are already in the tree->pointlist * * This is not the best function because it copies all the points @@ -250,7 +250,7 @@ TreeStruct * TreeStruct::spawn(TreeStruct::iterator ¤t){ return newTree; } -/** \ingroup ImageFindingL2 +/** * \brief Empty tree of all point leaving a tree with an empty root. * * The points are freed, but the list structure is not destroyed. @@ -294,7 +294,7 @@ short TreeStruct::emptyTree(){ return 1; } -/** \ingroup LowLevel +/** * \brief Recursively free branches */ void TreeStruct::_freeBranches(TreeStruct::iterator ¤t,short child){ @@ -331,7 +331,7 @@ void TreeStruct::_freeBranches(TreeStruct::iterator ¤t,short child){ return; } -/** \ingroup LowLevel +/** * \brief Iteratively free branches * * Frees all the branches of the tree so there is only the stump. @@ -372,7 +372,7 @@ void TreeStruct::_freeBranches_iter(){ } -/** \ingroup LowLevel +/** * \brief Recursively build tree from points in its linked list. */ void TreeStruct::_BuildTree(TreeStruct::iterator ¤t){ @@ -539,7 +539,7 @@ void TreeStruct::_BuildTree(TreeStruct::iterator ¤t){ return; } -/** \ingroup ImageFindingL2 +/** * \brief Expands tree by adding points */ int TreeStruct::AddPointsToTree(Point *xpoint,unsigned long Nadd){ @@ -1040,7 +1040,7 @@ void TreeStruct::_AddPoint(TreeStruct::iterator ¤t){ } } -/** \ingroup ImageFinding +/** * \brief THIS DOES NOT WORK YET!!! * * Reduces the size of the tree by removing points and branches that are no longer needed. @@ -1128,7 +1128,7 @@ unsigned long Grid::PruneTrees( -/** \ingroup ImageFindingL2 +/** * * \brief Prune off points that are below a resolution and in an annulus on the * source plane. @@ -1428,7 +1428,7 @@ unsigned long Grid::PrunePointsOutside( return count; } -/** \ingroup ImageFindingL2 +/** * \brief Empty trash points. * * Frees point arrays whose heads are stored in trashlist. @@ -1473,7 +1473,7 @@ void CollectTrash(Kist * trashkist,bool check){ return; } -/** \ingroup ImageFindingL2 +/** * * Frees all branches of the tree below the current branch in i_tree * if that branch is square and i_tree->current->refined == true. @@ -1664,7 +1664,7 @@ unsigned long FreeBranchesBelow(TreeStruct::iterator &i_tree_current,TreeHndl i_ return count; } -/** \ingroup LowLevel +/** * Removes current from a tree if it is a leaf. * Will not remove root of tree. * diff --git a/TreeCode_link/utilities.cpp b/TreeCode_link/utilities.cpp index 05974124..8c4b02b6 100644 --- a/TreeCode_link/utilities.cpp +++ b/TreeCode_link/utilities.cpp @@ -87,7 +87,7 @@ void log_polar_grid(Point *i_points,PosType rmax,PosType rmin,PosType *center,lo return; } -/** \ingroup Utill +/** * * The two functions below are inverses of each other for converting * between a 1d array index and a square grid of positions @@ -177,7 +177,7 @@ long IndexFromPosition(PosType x,long Npixels,PosType range,PosType center){ return -1; } - /** \ingroup Utill + /** * \brief bilinear interpolation from a map. * * Out of bounds points return 0. map is a i dimensional array representing a 2 dimensional map. @@ -236,7 +236,7 @@ long IndexFromPosition(PosType x,long Npixels,PosType range,PosType center){ } -/** \ingroup Utill +/** * This function finds the largest power of 2 that is < k */ unsigned long prevpower(unsigned long k){ diff --git a/include/Kist.h b/include/Kist.h index 405c2f53..a18c935a 100644 --- a/include/Kist.h +++ b/include/Kist.h @@ -22,7 +22,7 @@ struct KistUnit{ //typedef struct Point Data; // change this to make a kist of other objects -/** \ingroup ImageFindingL2 +/** * \brief A Kist is a class template for a linked list of any data type (default is Point). * * Multiple Kists of the same data can be made without copying data. Memory is allocated in diff --git a/include/Tree.h b/include/Tree.h index fe7bbde3..3c19b14b 100644 --- a/include/Tree.h +++ b/include/Tree.h @@ -291,7 +291,7 @@ void findborders(TreeHndl i_tree,ImageInfo *imageinfo); Point *LinkToSourcePoints(Point *i_points,unsigned long Npoints); -/// \ingroup Util +/// namespace Utilities{ ///Separation squared between two positions in 2 dimensions. inline PosType sepSQR(PosType *xx,PosType *yy){ @@ -521,7 +521,7 @@ namespace Utilities{ } - /** \ingroup Utill + /** * \brief Bilinear interpolation class for interpolating from a 2D uniform grid. * * Out of bounds points return 0. map is a i dimensional array representing a 2 dimensional map. diff --git a/include/doc.h b/include/doc.h index 5acad532..754d387e 100644 --- a/include/doc.h +++ b/include/doc.h @@ -15,28 +15,25 @@ /** \defgroup ImageFinding Image Finding - * \ingroup HighLevel + * * * \brief Routines for finding and characterizing images. * */ /** \defgroup ChangeLens Lens Manipulation - * \ingroup HighLevel * * \brief Routines for adjusting the lens. * */ /** \defgroup FitLens Lens Fitting - * \ingroup HighLevel * * \brief Routines for fitting a lens to observations. * */ /** \defgroup Image Image Processing - * \ingroup HighLevel * * \brief These routines are used for putting images into pixels, simulating PSF smoothing and noise, * fitting profiles etc. @@ -50,7 +47,6 @@ */ /** \defgroup ConstructorL2 Constructors and Destructors - * \ingroup MidLevel * * \brief Routines for initializing and freeing the data types. These have not yet been moved to C++ * form. They a left over from C. @@ -58,28 +54,27 @@ */ /** \defgroup ImageFindingL2 Image Finding - * \ingroup MidLevel * * \brief Routines for finding and characterizing images. * */ /** \defgroup DeflectionL2 Deflection Solver - * \ingroup MidLevel + * * * \brief Routines for calculating the deflection of a ray. * */ /** \defgroup FitLensL2 Lens Fitting - * \ingroup MidLevel + * * * \brief Routines for fitting a lens to observations. * */ /** \defgroup ImageL2 Image Processing - * \ingroup MidLevel + * * * \brief These routines are used for putting images into pixels, simulating PSF smoothing and noise, * fitting profiles etc. diff --git a/include/grid_maintenance.h b/include/grid_maintenance.h index 028a995d..faccd043 100644 --- a/include/grid_maintenance.h +++ b/include/grid_maintenance.h @@ -18,7 +18,7 @@ class LensHaloBaseNSIE; class LensHaloMassMap; -/** \ingroup ImageFinding +/** * \brief Structure to contain both source and image trees. * It is not yet used, but may be useful. */ diff --git a/include/gridmap.h b/include/gridmap.h index 7cb04866..eb60c5d7 100644 --- a/include/gridmap.h +++ b/include/gridmap.h @@ -16,7 +16,7 @@ #include "source.h" #include -/** \ingroup ImageFinding +/** * \brief A simplified version of the Grid structure for making non-adaptive maps of the lensing quantities (kappa, gamma, etc...) * * GripMap is faster and uses less memory than Grid. It does not construct the tree structures for the points diff --git a/include/image_processing.h b/include/image_processing.h index 43acddae..24783926 100644 --- a/include/image_processing.h +++ b/include/image_processing.h @@ -20,7 +20,7 @@ struct Grid; struct GridMap; -/** \ingroup Image +/** * \brief Takes image structure and pixelizes the flux into regular pixel grid which then * can be exported as a fits file, smoothed, etc. like an image. * @@ -340,7 +340,7 @@ typedef enum {Euclid_VIS,Euclid_Y,Euclid_J,Euclid_H,KiDS_u,KiDS_g,KiDS_r,KiDS_i, typedef enum {counts_x_sec, flux} unitType; -/** \ingroup Image +/** * \brief It creates a realistic image from the output of a ray-tracing simulation. * * It translates pixel values in observed units (counts/sec), applies PSF and noise. diff --git a/include/lens_halos.h b/include/lens_halos.h index 572fd515..c1a73879 100644 --- a/include/lens_halos.h +++ b/include/lens_halos.h @@ -982,7 +982,7 @@ class LensHaloRealNSIE : public LensHalo{ /// get the velocity dispersion float get_sigma(){return sigma;}; - /// get the NSIE radius + // get the NSIE radius //float get_Rsize(){return Rsize;}; /// get the axis ratio float get_fratio(){return fratio;}; @@ -993,13 +993,13 @@ class LensHaloRealNSIE : public LensHalo{ /// set the velocity dispersion void set_sigma(float my_sigma){sigma=my_sigma;}; - /// set the NSIE radius + // set the NSIE radius //void set_Rsize(float my_Rsize){Rsize=my_Rsize;}; ///set the axis ratio void set_fratio(float my_fratio){fratio=my_fratio;}; /// set the position angle void set_pa(float my_pa){pa=my_pa;}; - /// set the core radius + /// set the core radius Einstein radius void set_rcore(float my_rcore){rcore=my_rcore;}; protected: @@ -1046,7 +1046,7 @@ class LensHaloRealNSIE : public LensHalo{ -/** \ingroup DeflectionL2 +/** * * \brief A class for calculating the deflection, kappa and gamma caused by a collection of halos * with truncated Hernquist mass profiles. @@ -1124,7 +1124,7 @@ class LensHaloHernquist: public LensHalo{ PosType gmax; }; -/** \ingroup DeflectionL2 +/** * * \brief A class for calculating the deflection, kappa and gamma caused by a collection of halos * with truncated Jaffe mass profiles. diff --git a/include/overzier_source.h b/include/overzier_source.h index 4211aa2d..3a5b383e 100644 --- a/include/overzier_source.h +++ b/include/overzier_source.h @@ -145,6 +145,11 @@ class SourceOverzier : public Source // optional position variables }; +/** \brief Adds some extra features to the SourceOverzier source like spiral + * arms, and randomizations. + * + */ + class SourceOverzierPlus : public SourceOverzier { public: diff --git a/include/slsimlib.h b/include/slsimlib.h index 3cd0fdc8..1c2346fc 100644 --- a/include/slsimlib.h +++ b/include/slsimlib.h @@ -1,5 +1,5 @@ /** \file - * \ingroup \HighLevel + * * * \brief Master header file for all routines in SLsimLib. Should be the only header file that needs to be included. * diff --git a/include/source.h b/include/source.h index a5a89f59..4a33b45b 100644 --- a/include/source.h +++ b/include/source.h @@ -123,6 +123,7 @@ class SourcePixelled: public Source{ SourcePixelled(PosType my_z, PosType* center, int Npixels, PosType resolution, PosType* arr_val); SourcePixelled(const PixelMap& gal_map, PosType z, PosType factor = 1.); SourcePixelled(InputParams& params); + ~SourcePixelled(); PosType SurfaceBrightness(PosType *y); void printSource(); From becf3c6f4480a433460e76937684f9346c93e9dc Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Fri, 28 Jun 2019 13:34:33 +0200 Subject: [PATCH 159/227] Change to comments --- include/image_processing.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/image_processing.h b/include/image_processing.h index 24783926..e6a9b45d 100644 --- a/include/image_processing.h +++ b/include/image_processing.h @@ -19,14 +19,14 @@ // forward declaration struct Grid; struct GridMap; +class Source; + /** * \brief Takes image structure and pixelizes the flux into regular pixel grid which then * can be exported as a fits file, smoothed, etc. like an image. * */ -class Source; - class PixelMap { public: From 32a2274a7b520e7020d39efd7c4405fa71b24e95 Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Fri, 28 Jun 2019 16:06:59 +0200 Subject: [PATCH 160/227] Fixed bug in PA of LensHaloNSIE to agree with others. Fixed bug in Lens::replaceMainHalos() --- MultiPlane/lens_halos.cpp | 13 +++++++------ Source/overzier.cpp | 2 +- include/lens.h | 8 +++++++- include/lens_halos.h | 10 ++++++++++ include/utilities_slsim.h | 14 +++++++++++++- 5 files changed, 38 insertions(+), 9 deletions(-) diff --git a/MultiPlane/lens_halos.cpp b/MultiPlane/lens_halos.cpp index 791da6df..e55a9bca 100644 --- a/MultiPlane/lens_halos.cpp +++ b/MultiPlane/lens_halos.cpp @@ -887,12 +887,12 @@ std::vector LensHaloRealNSIE::q_table; std::vector LensHaloRealNSIE::Fofq_table; LensHaloRealNSIE::LensHaloRealNSIE( - float my_mass - ,PosType my_zlens + float my_mass /// mass, sets truncation radius + ,PosType my_zlens /// redshift ,float my_sigma /// in km/s ,float my_rcore /// in units of R_einstein - ,float my_fratio - ,float my_pa + ,float my_fratio /// axis ratio + ,float my_pa /// postion angle ,int my_stars_N) :LensHalo(){ rscale=1.0; @@ -901,7 +901,7 @@ LensHaloRealNSIE::LensHaloRealNSIE( sigma=my_sigma, rcore=my_rcore; - fratio=my_fratio, pa=my_pa, stars_N=my_stars_N; + fratio=my_fratio, pa = PI/2 - my_pa, stars_N=my_stars_N; stars_implanted = false; if(fratio != 1.0) elliptical_flag = true; @@ -920,6 +920,7 @@ LensHaloRealNSIE::LensHaloRealNSIE( LensHalo::setMass(MassBy1DIntegation(LensHalo::getRsize())); } + units = pow(sigma/lightspeed,2)/Grav;///sqrt(fratio); // mass/distance(physical); } LensHaloRealNSIE::LensHaloRealNSIE(InputParams& params):LensHalo(params,false){ @@ -947,6 +948,7 @@ LensHaloRealNSIE::LensHaloRealNSIE(InputParams& params):LensHalo(params,false){ LensHalo::setMass(MassBy1DIntegation(LensHalo::getRsize()) ); } + units = pow(sigma/lightspeed,2)/Grav;///sqrt(fratio); // mass/distance(physical) } void LensHaloRealNSIE::assignParams(InputParams& params){ @@ -1417,7 +1419,6 @@ void LensHaloRealNSIE::force_halo( if(rcm2 < 1e-20) rcm2 = 1e-20; if(rcm2 < Rmax*Rmax){ //PosType ellipR = ellipticRadiusNSIE(xcm,fratio,pa); - float units = pow(sigma/lightspeed,2)/Grav;///sqrt(fratio); // mass/distance(physical) // std::cout << "rsize , rmax, mass_norm =" << LensHalo::getRsize() << " , " << Rmax << " , " << mass_norm_factor << std::endl; if(rcm2 > LensHalo::getRsize()*LensHalo::getRsize()) //if(ellipR > LensHalo::getRsize()*LensHalo::getRsize()) diff --git a/Source/overzier.cpp b/Source/overzier.cpp index 6bd2c557..0bc84e78 100644 --- a/Source/overzier.cpp +++ b/Source/overzier.cpp @@ -17,7 +17,7 @@ SourceOverzier::SourceOverzier( PosType my_mag /// Total magnitude - ,double my_mag_bulge /// Bulge to total ratio + ,double my_mag_bulge /// magnitude of bulge ,double my_Reff /// Bulge half light radius (arcs) ,double my_Rdisk /// disk scale hight (arcs) ,double my_PA /// Position angle (radians) diff --git a/include/lens.h b/include/lens.h index 1ca7c3b1..a034c483 100644 --- a/include/lens.h +++ b/include/lens.h @@ -195,7 +195,13 @@ class Lens{ template void replaceMainHalo(const T &halo_in,bool addplanes,bool verbose=false) { - Utilities::delete_container(main_halos); // ???? + //Utilities::delete_container(main_halos); // ???? + /*while(main_halos.size() > 0){ + delete main_halos.back(); + main_halos.pop_back(); + }*/ + + main_halos.clear(); // ???? is this a memory leak ???? T * halo = new T(halo_in); halo->setCosmology(cosmo); diff --git a/include/lens_halos.h b/include/lens_halos.h index c1a73879..bd36edeb 100644 --- a/include/lens_halos.h +++ b/include/lens_halos.h @@ -962,6 +962,7 @@ class LensHaloRealNSIE : public LensHalo{ fratio = h.fratio; pa = h.pa; rcore = h.rcore; + units = h.units; } LensHaloRealNSIE &operator=(const LensHaloRealNSIE &h){ @@ -971,6 +972,7 @@ class LensHaloRealNSIE : public LensHalo{ fratio = h.fratio; pa = h.pa; rcore = h.rcore; + units = h.units; return *this; } @@ -1002,8 +1004,16 @@ class LensHaloRealNSIE : public LensHalo{ /// set the core radius Einstein radius void set_rcore(float my_rcore){rcore=my_rcore;}; + void setZlens(PosType my_zlens){ + LensHalo::setZlens(my_zlens); + + } + + protected: + float units; + static size_t objectCount; static std::vector q_table; static std::vector Fofq_table; diff --git a/include/utilities_slsim.h b/include/utilities_slsim.h index 10a2db07..13f4c520 100644 --- a/include/utilities_slsim.h +++ b/include/utilities_slsim.h @@ -420,7 +420,7 @@ namespace Utilities { return *items[i]; } - + /// indexed access for type SubclassT template SubclassT& get(std::size_t i) @@ -648,6 +648,18 @@ namespace Utilities return *this; } + /// back of list + BaseT& back() + { + return items.back(); + } + + /// back of list + const BaseT& back() const + { + return items.back(); + } + /// add an object of type SubclassT to the vector void push_back(BaseT* obj) { From b13745974236b667e928f6bee418c358d8a89c6a Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Mon, 1 Jul 2019 09:17:14 +0200 Subject: [PATCH 161/227] Added some other forms of the SphericalPoint -> 2d projections routines. --- TreeCode_link/geometry.cpp | 52 +++++++++++++++++++++++++++++++++----- include/geometry.h | 6 ++++- 2 files changed, 51 insertions(+), 7 deletions(-) diff --git a/TreeCode_link/geometry.cpp b/TreeCode_link/geometry.cpp index 8d0dfa99..96ebb2ba 100644 --- a/TreeCode_link/geometry.cpp +++ b/TreeCode_link/geometry.cpp @@ -30,16 +30,37 @@ void Utilities::Geometry::SphericalPoint::cartisianTOspherical(PosType const x[] * onto a tangent plane. */ void Utilities::Geometry::SphericalPoint::StereographicProjection( - const SphericalPoint ¢ral /// point on the sphere where the tangent plane touches - ,PosType x[] /// 2D output coordinate on projection - ) const{ - + const SphericalPoint ¢ral /// point on the sphere where the tangent plane touches + ,PosType x[] /// 2D output coordinate on projection +) const{ + PosType k = 2/( 1 + sin(central.theta)*sin(theta) + cos(central.theta)*cos(theta)*cos(phi - central.phi) ); x[0] = k*(cos(theta)*sin(phi - central.phi)); x[1] = k*(cos(central.theta)*sin(theta) - sin(central.theta)*cos(theta)*cos(phi - central.phi)); } - +void Utilities::Geometry::SphericalPoint::StereographicProjection( + const SphericalPoint ¢ral /// point on the sphere where the tangent plane touches + ,Point_2d &x /// 2D output coordinate on projection +) const{ + + PosType k = 2/( 1 + sin(central.theta)*sin(theta) + cos(central.theta)*cos(theta)*cos(phi - central.phi) ); + + x[0] = k*(cos(theta)*sin(phi - central.phi)); + x[1] = k*(cos(central.theta)*sin(theta) - sin(central.theta)*cos(theta)*cos(phi - central.phi)); +} + +Point_2d Utilities::Geometry::SphericalPoint::StereographicProjection( + const SphericalPoint ¢ral /// point on the sphere where the tangent plane touches +) const{ + + PosType k = 2/( 1 + sin(central.theta)*sin(theta) + cos(central.theta)*cos(theta)*cos(phi - central.phi) ); + + return Point_2d(k*(cos(theta)*sin(phi - central.phi)),k*(cos(central.theta)*sin(theta) - sin(central.theta)*cos(theta)*cos(phi - central.phi))); +// x[0] = k*(cos(theta)*sin(phi - central.phi)); +// x[1] = k*(cos(central.theta)*sin(theta) - sin(central.theta)*cos(theta)*cos(phi - central.phi)); +} + /** \brief Calculates the orthographic projection of the point onto a plane. * * The result is in radian units. Near the central point this is a rectolinear projection @@ -52,7 +73,15 @@ void Utilities::Geometry::SphericalPoint::OrthographicProjection( x[0] = cos(theta)*sin(phi - central.phi); x[1] = cos(central.theta)*sin(theta) - sin(central.theta)*cos(theta)*cos(phi - central.phi); } - + +Point_2d Utilities::Geometry::SphericalPoint::OrthographicProjection( + const SphericalPoint ¢ral /// point on the sphere where the tangent plane touches + ) const{ + + return Point_2d( cos(theta)*sin(phi - central.phi), + cos(central.theta)*sin(theta) - sin(central.theta)*cos(theta)*cos(phi - central.phi) ); +} + /** \brief Convert from an orthographic projection of the plane onto the unit sphere */ void Utilities::Geometry::SphericalPoint::InverseOrthographicProjection( @@ -66,6 +95,17 @@ void Utilities::Geometry::SphericalPoint::InverseOrthographicProjection( phi = central.phi + atan2(x[0]*sin(c),rho*cos(central.theta)*cos(c) - x[1]*sin(central.theta)*sin(c) ); } +void Utilities::Geometry::SphericalPoint::InverseOrthographicProjection( + const SphericalPoint ¢ral /// point on the sphere where the tangent plane touches + ,const Point_2d &x /// 2D output coordinate on projection +){ + PosType rho = sqrt(x[0]*x[0] + x[1]*x[1]); + PosType c = asin(rho); + r=1.0; + theta = asin( cos(c)*sin(central.theta) + x[1]*sin(c)*cos(central.theta)/rho ); + phi = central.phi + atan2(x[0]*sin(c),rho*cos(central.theta)*cos(c) + - x[1]*sin(central.theta)*sin(c) ); +} /// 3 dimensional distance between points PosType Utilities::Geometry::Seporation(const SphericalPoint &p1,const SphericalPoint &p2){ diff --git a/include/geometry.h b/include/geometry.h index 40d546a3..c289dcaf 100644 --- a/include/geometry.h +++ b/include/geometry.h @@ -39,8 +39,12 @@ namespace Utilities { void sphericalTOcartisian(PosType x[]) const; void cartisianTOspherical(PosType const x[]); void StereographicProjection(const SphericalPoint ¢ral,PosType x[]) const; + void StereographicProjection(const SphericalPoint ¢ral,Point_2d &x) const; + Point_2d StereographicProjection(const SphericalPoint ¢ral) const; void OrthographicProjection(const SphericalPoint ¢ral,PosType x[]) const; + Point_2d OrthographicProjection(const SphericalPoint ¢ral) const; void InverseOrthographicProjection(const SphericalPoint ¢ral,PosType const x[]); + void InverseOrthographicProjection(const SphericalPoint ¢ral,const Point_2d &x); }; /** \brief Quaternion class that is especially useful for rotations. @@ -52,7 +56,7 @@ namespace Utilities { #include "geometry.h" - { + { using Utilities::Geometry::SphericalPoint; using Utilities::Geometry::Quaternion; From 942ec864a9f472a28b40103b60c376a2f9ffce75 Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Thu, 4 Jul 2019 13:00:08 +0200 Subject: [PATCH 162/227] Working on fixing bug in Utilities::IO::ReadCSVnumerical2(). --- include/utilities_slsim.h | 45 ++++++++++++++++++++++----------------- 1 file changed, 25 insertions(+), 20 deletions(-) diff --git a/include/utilities_slsim.h b/include/utilities_slsim.h index 13f4c520..3351ecca 100644 --- a/include/utilities_slsim.h +++ b/include/utilities_slsim.h @@ -1597,33 +1597,37 @@ namespace Utilities std::stringstream lineStream(line); std::string cell; column_names.empty(); - - while(std::getline(lineStream,cell,deliniator)) - { - column_names.push_back(cell); - } - - // This checks for a trailing comma with no data after it. - if (!lineStream && cell.empty()) - { - column_names.push_back(""); - } - - int columns = NumberOfEntries(line,deliniator); - size_t ii = 0; - - for(auto &v : data ) v.empty(); - if(!header){ - data.emplace_back(columns); + if(header){ + while(std::getline(lineStream,cell,deliniator)) + { + column_names.push_back(cell); + } + + // This checks for a trailing comma with no data after it. + if (!lineStream && cell.empty()) + { + column_names.push_back(""); + } + }else{ + + while(std::getline(lineStream,cell,deliniator)) + { + column_names.push_back(cell); + } + + data.emplace_back(column_names.size()); int i=0; for(auto a : column_names){ data.back()[i++] = to_numeric(a); } - ++ii; + ++ii; } - + + int columns = NumberOfEntries(line,deliniator); + + for(auto &v : data ) v.empty(); while(std::getline(file,line) && ii < Nmax){ @@ -1636,6 +1640,7 @@ namespace Utilities int i=0; while(std::getline(lineStream,cell, deliniator)) { + assert(i < columns); data.back()[i] = to_numeric(cell); ++i; } From 4cc9e902fd58754133bbca6d94fce758883de709 Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Thu, 18 Jul 2019 14:24:18 +0200 Subject: [PATCH 163/227] Added move operators for LensHaloMultiMap class. --- MultiPlane/multimap.cpp | 2 +- include/multimap.h | 54 ++++++++++++++++++++++++++++++++++++----- 2 files changed, 49 insertions(+), 7 deletions(-) diff --git a/MultiPlane/multimap.cpp b/MultiPlane/multimap.cpp index cee1a326..665b4e5a 100644 --- a/MultiPlane/multimap.cpp +++ b/MultiPlane/multimap.cpp @@ -13,7 +13,7 @@ LensHaloMultiMap::LensHaloMultiMap( std::string fitsfile /// Original fits map of the density ,double redshift ,double mass_unit - ,const COSMOLOGY &c + ,COSMOLOGY &c ,bool single_grid_mode ):LensHalo(redshift,c),single_grid(single_grid_mode),cosmo(c),mass_unit(mass_unit),fitsfilename(fitsfile) { diff --git a/include/multimap.h b/include/multimap.h index 8359320e..08eaee95 100644 --- a/include/multimap.h +++ b/include/multimap.h @@ -116,7 +116,7 @@ class LensHaloMultiMap : public LensHalo std::string fitsfile /// Original fits map of the density ,double redshift ,double mass_unit /// should include h factors - ,const COSMOLOGY &c + ,COSMOLOGY &c ,bool single_grid_mode = false ); @@ -156,7 +156,6 @@ class LensHaloMultiMap : public LensHalo Point_2d getUpperRight_sr() const { return short_range_map.upperright; } /// center of short range map in physical Mpc Point_2d getCenter_sr() const { return short_range_map.center; } - /// return range of long range map in physical Mpc double getRangeMpc_lr() const { return long_range_map.boxlMpc; } @@ -181,18 +180,61 @@ class LensHaloMultiMap : public LensHalo double getMax() const {return max_pix;} double getMin() const {return min_pix;} + void operator =(LensHaloMultiMap &&m){ + cosmo = m.cosmo; + long_range_map = std::move(m.long_range_map); + short_range_map = std::move(m.short_range_map); + single_grid = m.single_grid; + ff = m.ff; + m.ff = nullptr; + max_pix = m.max_pix; + min_pix = m.min_pix; + mass_unit = m.mass_unit; + Noriginal[0] = m.Noriginal[0]; + Noriginal[1] = m.Noriginal[1]; + resolution = m.resolution; + border_width = m.border_width; + fitsfilename = m.fitsfilename; + rs2 = m.rs2; + zerosize = m.zerosize; + unit = m.unit; + wsr = m.wsr; + wlr = m.wlr; + } + LensHaloMultiMap(LensHaloMultiMap &&m):cosmo(m.cosmo){ + long_range_map = std::move(m.long_range_map); + short_range_map = std::move(m.short_range_map); + single_grid = m.single_grid; + ff = m.ff; + m.ff = nullptr; + max_pix = m.max_pix; + min_pix = m.min_pix; + mass_unit = m.mass_unit; + Noriginal[0] = m.Noriginal[0]; + Noriginal[1] = m.Noriginal[1]; + resolution = m.resolution; + border_width = m.border_width; + fitsfilename = m.fitsfilename; + rs2 = m.rs2; + zerosize = m.zerosize; + unit = m.unit; + wsr = m.wsr; + wlr = m.wlr; + } + +public: // ????? + LensMap long_range_map; + LensMap short_range_map; + private: bool single_grid; - const COSMOLOGY &cosmo; + COSMOLOGY &cosmo; CCfits::FITS *ff; double max_pix = std::numeric_limits::lowest(); double min_pix = std::numeric_limits::max(); -public: // ????? - LensMap long_range_map; - LensMap short_range_map; private: // ????? double mass_unit; From edc79d7fd0e57fe7c4289c18aef7e40f350d5d54 Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Thu, 18 Jul 2019 18:24:18 +0200 Subject: [PATCH 164/227] Corrected move operators for LensHaloMultiMap that were not copying the base class. --- MultiPlane/multimap.cpp | 4 ++-- include/multimap.h | 4 +++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/MultiPlane/multimap.cpp b/MultiPlane/multimap.cpp index 665b4e5a..b85f320e 100644 --- a/MultiPlane/multimap.cpp +++ b/MultiPlane/multimap.cpp @@ -549,7 +549,7 @@ void LensMap::read(std::string fits_input_file,float h,float z){ exit(1); } - double res = nx/boxlMpc; + double res = boxlMpc/nx; center *= 0; lowerleft = center; @@ -603,7 +603,7 @@ void LensMap::Myread(std::string fits_input_file){ //h0.readKey ("REDSHIFT",z); h0.readKey("SIDEL2",boxlMpc); - double res = nx/boxlMpc; + double res = boxlMpc/nx; center *= 0; lowerleft = center; diff --git a/include/multimap.h b/include/multimap.h index 08eaee95..6bb488a0 100644 --- a/include/multimap.h +++ b/include/multimap.h @@ -181,6 +181,7 @@ class LensHaloMultiMap : public LensHalo double getMin() const {return min_pix;} void operator =(LensHaloMultiMap &&m){ + LensHalo::operator=(std::move(m)); cosmo = m.cosmo; long_range_map = std::move(m.long_range_map); short_range_map = std::move(m.short_range_map); @@ -201,7 +202,8 @@ class LensHaloMultiMap : public LensHalo wsr = m.wsr; wlr = m.wlr; } - LensHaloMultiMap(LensHaloMultiMap &&m):cosmo(m.cosmo){ + LensHaloMultiMap(LensHaloMultiMap &&m):LensHalo(std::move(m)) + ,cosmo(m.cosmo){ long_range_map = std::move(m.long_range_map); short_range_map = std::move(m.short_range_map); single_grid = m.single_grid; From 99efd1b55174e5aeba8d6cebca66cfc9721c41fc Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Sat, 20 Jul 2019 15:21:30 +0200 Subject: [PATCH 165/227] Modifications to Lens class removed some verbosity Removed the dangerous default cosmology in some of the constructors. --- MultiPlane/lens.cpp | 4 ++-- include/lens.h | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/MultiPlane/lens.cpp b/MultiPlane/lens.cpp index 2f990aaf..d83aa194 100644 --- a/MultiPlane/lens.cpp +++ b/MultiPlane/lens.cpp @@ -1226,7 +1226,8 @@ void Lens::addMainHaloToPlane(LensHalo* halo) main_plane_redshifts.push_back(halo_z); main_Dl.push_back(halo_Dl); //halo->setDist(halo_Dl/(1+halo_z)); - halo->setZlensDist(halo_z,cosmo); + if(halo_z != halo->getZlens()) + halo->setZlensDist(halo_z,cosmo); } else if((main_Dl[i] - halo_Dl) < MIN_PLANE_DIST) { @@ -2924,7 +2925,6 @@ void Lens::combinePlanes(bool verbose) // output resulting setup if(verbose) - { std::cout << "\ncombinePlanes()" << "\n---------------" << std::endl; std::cout << "\nz:"; diff --git a/include/lens.h b/include/lens.h index a034c483..82a91006 100644 --- a/include/lens.h +++ b/include/lens.h @@ -70,8 +70,8 @@ GLAMER_TEST_USES(LensTest) class Lens{ public: - Lens(long* seed, PosType z_source,CosmoParamSet cosmoset = WMAP5yr, bool verbose = false); - Lens(InputParams& params, long* my_seed, CosmoParamSet cosmoset = WMAP5yr, bool verbose = false); + Lens(long* seed, PosType z_source,CosmoParamSet cosmoset, bool verbose = false); + Lens(InputParams& params, long* my_seed, CosmoParamSet cosmoset, bool verbose = false); Lens(long* seed, PosType z_source,const COSMOLOGY &cosmo, bool verbose = false); Lens(InputParams& params, long* my_seed, const COSMOLOGY &cosmo, bool verbose = false); @@ -225,7 +225,7 @@ class Lens{ * The halos are copied so the input halos can be destoyed without affecting the Lens. */ template - void insertMainHalos(std::vector &my_halos,bool addplanes, bool verbose) + void insertMainHalos(std::vector &my_halos,bool addplanes, bool verbose=false) { T* ptr; //for(std::size_t i = 0; i < my_halos.size() ; ++i) From 142ba0576337918ccfa2e3578cb395f5d66775d7 Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Sat, 20 Jul 2019 15:34:30 +0200 Subject: [PATCH 166/227] Changes to LensHaloMultiMap class Corrected it so that long-range map has exactly the same size as the input map. This results in the long-range map not having square pixels. The FFT has not been changed for this yet. --- MultiPlane/multimap.cpp | 220 ++++++++++++++++++++++++---------------- include/multimap.h | 32 +++--- include/point.h | 5 +- 3 files changed, 154 insertions(+), 103 deletions(-) diff --git a/MultiPlane/multimap.cpp b/MultiPlane/multimap.cpp index b85f320e..418c799e 100644 --- a/MultiPlane/multimap.cpp +++ b/MultiPlane/multimap.cpp @@ -32,14 +32,18 @@ LensHaloMultiMap::LensHaloMultiMap( Rmax = std::numeric_limits::infinity(); else Rmax = std::numeric_limits::max(); - - LensMap submap; - submap.read_header(fitsfilename,cosmo.gethubble(),getZlens()); - + setZlens(redshift); setZlensDist(redshift,cosmo); - long_range_map.boxlMpc = submap.boxlMpc; + double D = LensHalo::getDist(); + + LensMap submap; + submap.read_header(fitsfilename,D); + //,cosmo.gethubble(),getZlens()); + + + //long_range_map.boxlMpc = submap.boxlMpc; //std::size_t size = bigmap.nx*bigmap.ny; //rs2 = submap.boxlMpc*submap.boxlMpc/( 2*PI*submap.nx ); @@ -52,11 +56,8 @@ LensHaloMultiMap::LensHaloMultiMap( int desample = sqrt(0.5*submap.nx)/sqrt(f*g); - Noriginal[0] = submap.nx - submap.nx % desample; - Noriginal[1] = submap.ny - submap.ny % desample; - - size_t nx = long_range_map.nx = submap.nx / desample ; - size_t ny = long_range_map.ny = submap.ny / desample ; + Noriginal[0] = submap.nx; + Noriginal[1] = submap.ny; if( Utilities::IO::file_exists(fitsfile + "_lr.fits") ){ @@ -67,7 +68,8 @@ LensHaloMultiMap::LensHaloMultiMap( std::vector first = {1,1}; std::vector last = {(long)Noriginal[0],(long)Noriginal[1]}; - long_range_map.read_sub(ff,first,last,cosmo.gethubble(),getZlens()); + long_range_map.read_sub(ff,first,last,getDist()); + //,cosmo.gethubble(),getZlens()); //double res = submap.boxlMpc/submap.nx; //Point_2d dmap = {submap.boxlMpc,submap.ny*resolution}; @@ -79,24 +81,34 @@ LensHaloMultiMap::LensHaloMultiMap( }else{ - double res = desample*submap.boxlMpc/submap.nx; - long_range_map.boxlMpc = nx*res; - long_range_map.lowerleft = submap.lowerleft; - Point_2d dmap = {nx*res,ny*res}; - long_range_map.upperright = long_range_map.lowerleft + dmap; + long_range_map.upperright = submap.upperright; + long_range_map.boxlMpc = submap.boxlMpc; + long_range_map.center = (long_range_map.upperright + long_range_map.lowerleft)/2; - - /* - dmap /= 2; - long_range_map.center = {0,0}; - long_range_map.upperright = long_range_map.center + dmap; - long_range_map.lowerleft = long_range_map.center - dmap; - */ + + Point_2d range = long_range_map.upperright - long_range_map.lowerleft; + + long_range_map.nx = submap.nx/desample; + double res_x = long_range_map.boxlMpc/long_range_map.nx; + + // get the ny that makes the pixels clossest to square + double Ly = submap.y_range(); + long_range_map.ny = (int)( Ly/res_x ); + if(Ly - long_range_map.ny*res_x > res_x/2) long_range_map.ny += 1; + + size_t nx = long_range_map.nx; + size_t ny = long_range_map.ny; - long_range_map.surface_density.resize(nx*ny,0); + double res_y = submap.y_range()/ny; + + std::cout << "ratio of low resolution resolutions " + << res_x/res_y << std::endl; + + size_t N = long_range_map.ny*long_range_map.nx; + long_range_map.surface_density.resize(N,0); - long chunk = MIN(nx*ny/Noriginal[0],Noriginal[1]); // same size as long range grid + long chunk = MIN(N/Noriginal[0],Noriginal[1]); // same size as long range grid if( chunk == 0) chunk = MIN(100*desample,Noriginal[1]); std::vector first(2); @@ -108,7 +120,7 @@ LensHaloMultiMap::LensHaloMultiMap( size_t kj = 0; size_t jj; for(size_t j = 0 ; j < Noriginal[1] ; ++j ){ - jj = j/desample; + jj = j*resolution/res_y; if( jj >= ny) break; //assert(jj < ny); @@ -119,7 +131,8 @@ LensHaloMultiMap::LensHaloMultiMap( first[1] = j+1; last[1] = MIN(j + chunk,Noriginal[1]); //submap.read_sub(fitsfilename,first,last,cosmo.gethubble()); - submap.read_sub(ff,first,last,cosmo.gethubble(),getZlens()); + submap.read_sub(ff,first,last,getDist()); + //,cosmo.gethubble(),getZlens()); kj = 0; }else{ kj += submap.nx; @@ -128,7 +141,7 @@ LensHaloMultiMap::LensHaloMultiMap( double tmp; for(size_t i = 0 ; i < Noriginal[0] ; ++i ){ - size_t ii = i/desample; + size_t ii = i*resolution/res_x; if( ii >= nx) break; tmp = long_range_map.surface_density[ ii + kjj ] += submap.surface_density[ i + kj ]; @@ -141,7 +154,13 @@ LensHaloMultiMap::LensHaloMultiMap( wlr.rs2 = wsr.rs2 = rs2; } - double area = pow(long_range_map.boxlMpc/long_range_map.nx,2)/mass_unit; //*** units ??? +// double area = pow(long_range_map.boxlMpc/long_range_map.nx,2)/mass_unit; //*** units ??? + //double area = long_range_map.x_resolution() + //*long_range_map.y_resolution()/mass_unit/resolution/resolution; //*** units ??? + double area = long_range_map.x_resolution() + *long_range_map.y_resolution()/mass_unit; //*** units ??? + //double area = 1.0/mass_unit; + // convert to for(auto &p : long_range_map.surface_density){ p /= area; @@ -163,11 +182,28 @@ void LensHaloMultiMap::submapPhys(Point_2d ll,Point_2d ur){ std::vector lower_left(2); std::vector upper_right(2); + //Point_2d range = long_range_map.upperright - long_range_map.lowerleft; + + //std::cout << "ang res = " << resolution/getDist()/arcsecTOradians + //<< " arcsec" << std::endl; + //std::cout << "lower left angle " << ll/getDist() << std::endl; + //std::cout << "lower left angle " << long_range_map.lowerleft/getDist() << std::endl; + + ll = (ll - long_range_map.lowerleft)/resolution; - assert(ll[0] >= 0); assert(ll[1] >= 0); + //std::cout << "lower left pixels = " << ll << std::endl; + assert(ll[0] > -0.1 ); + assert(ll[1] > -0.1 ); + if(ll[0] < 0) ll[0] = 0; + if(ll[1] < 0) ll[1] = 0; + ur = (ur - long_range_map.lowerleft)/resolution; - assert(ur[0] >= 0); assert(ur[1] >= 0); - + + std::cout << ur << std::endl; + std::cout << long_range_map.nx << " " << long_range_map.ny << std::endl; + assert(ur[0] < long_range_map.boxlMpc/resolution + 0.1); + assert(ur[1] < long_range_map.boxlMpc/resolution + 0.1); + lower_left[0] = floor(ll[0]); lower_left[1] = floor(ll[1]); @@ -190,7 +226,7 @@ void LensHaloMultiMap::submap( || (lower_left[1] >= Noriginal[1]) ){ std::cerr << "LensHaloMap : sub map is out of bounds" << std::endl; - std::cerr << "LensHaloMap : sub map is out of bounds" << std::endl; + throw std::invalid_argument("out of bounds"); } if( (upper_right[0] - lower_left[0]) > Noriginal[0] @@ -211,7 +247,8 @@ void LensHaloMultiMap::submap( LensMap map; if( (first[0] > 0)*(first[1] > 0)*(last[0] <= Noriginal[0])*(last[1] <= Noriginal[1]) ){ - map.read_sub(fitsfilename,first,last,cosmo.gethubble(),getZlens()); + map.read_sub(fitsfilename,first,last,getDist()); + //,cosmo.gethubble(),getZlens()); }else{ size_t nx_big = map.nx = last[0] - first[0] + 1; @@ -242,7 +279,8 @@ void LensHaloMultiMap::submap( first_sub[1] = jj + 1; last_sub[1] = MIN(jj + chunk + 1,Noriginal[1]); //partmap.read_sub(fitsfilename,first_sub,last_sub,cosmo.gethubble()); - partmap.read_sub(ff,first_sub,last_sub,cosmo.gethubble(),getZlens()); + partmap.read_sub(ff,first_sub,last_sub,getDist()); + //,cosmo.gethubble(),getZlens()); k = 0; }else{ ++k; @@ -270,6 +308,8 @@ void LensHaloMultiMap::submap( double area = resolution*resolution/mass_unit; //*** units ??? // convert to + //double area = 1.0/mass_unit; //*** units ??? + // convert to for(auto &p : map.surface_density){ p /= area; } @@ -435,7 +475,8 @@ LensMap& LensMap::operator=(LensMap &&m){ return *this; } -void LensMap::read_header(std::string fits_input_file,float h,float z){ +void LensMap::read_header(std::string fits_input_file + ,double angDist){ std::auto_ptr ff(new CCfits::FITS (fits_input_file, CCfits::Read)); @@ -448,22 +489,23 @@ void LensMap::read_header(std::string fits_input_file,float h,float z){ nx = h0.axis(0); ny = h0.axis(1); + double phys_res; try{ /* these are always present in ea*/ - float wlow,wup,res; - h0.readKey ("CD1_1",res); // resolution in degrees + //float wlow,wup, + h0.readKey ("CD1_1",phys_res); // resolution in degrees //h0.readKey ("REDSHIFT",z); - h0.readKey ("WLOW",wlow); - h0.readKey ("WUP",wup); + //h0.readKey ("WLOW",wlow); + //h0.readKey ("WUP",wup); - double D = 3*(pow(wup,4) - pow(wlow,4))/(pow(wup,3) - pow(wlow,3))/4; - if(wlow == wup) D = wlow; - D /= (1+z)*h; + //double D = 3*(pow(wup,4) - pow(wlow,4))/(pow(wup,3) - pow(wlow,3))/4; + //if(wlow == wup) D = wlow; + //D /= (1+z)*h; //D /= (1+z); - res *= degreesTOradians*D; - boxlMpc = res*nx; + phys_res *= degreesTOradians*angDist; + boxlMpc = phys_res*nx; } catch(CCfits::HDU::NoSuchKeyword){ @@ -476,18 +518,19 @@ void LensMap::read_header(std::string fits_input_file,float h,float z){ exit(1); } - double res = boxlMpc/nx; - + //double phys_res = boxlMpc/nx; + + center *= 0; lowerleft = center; lowerleft[0] -= boxlMpc/2; - lowerleft[1] -= res*ny/2; + lowerleft[1] -= phys_res*ny/2; upperright = center; upperright[0] += boxlMpc/2; - upperright[1] += ny*res/2.; + upperright[1] += ny*phys_res/2.; } -void LensMap::read(std::string fits_input_file,float h,float z){ +void LensMap::read(std::string fits_input_file,double angDist){ std:: cout << " reading lens density map file: " << fits_input_file << std:: endl; std::auto_ptr ff(new CCfits::FITS (fits_input_file, CCfits::Read)); @@ -523,20 +566,20 @@ void LensMap::read(std::string fits_input_file,float h,float z){ h3.read(gamma1_bar); CCfits::ExtHDU &h4=ff->extension(4); h4.read(gamma2_bar); - std::cout << h0 << h1 << h2 << h3 << h4 << std::endl; + //std::cout << h0 << h1 << h2 << h3 << h4 << std::endl; } try{ /* these are always present in ea*/ float wlow,wup,res; - h0.readKey ("CD1_1",res); // recall you that MOKA Mpc/h + h0.readKey ("CD1_1",res); // angular resolution degrees //h0.readKey ("REDSHIFT",z); - h0.readKey ("WLOW",wlow); - h0.readKey ("WUP",wup); + //h0.readKey ("WLOW",wlow); + //h0.readKey ("WUP",wup); - double D = 3*(pow(wup,4) - pow(wlow,4))/(pow(wup,3) - pow(wlow,3))/4; - D /= (1+z)*h; + //double D = 3*(pow(wup,4) - pow(wlow,4))/(pow(wup,3) - pow(wlow,3))/4; + //D /= (1+z)*h; - res *= degreesTOradians*D; + res *= degreesTOradians*angDist; boxlMpc = res*nx; } catch(CCfits::HDU::NoSuchKeyword){ @@ -549,17 +592,16 @@ void LensMap::read(std::string fits_input_file,float h,float z){ exit(1); } - double res = boxlMpc/nx; + double phys_res = boxlMpc/nx; center *= 0; lowerleft = center; lowerleft[0] -= boxlMpc/2; - lowerleft[1] -= res*ny/2; + lowerleft[1] -= phys_res*ny/2; upperright = center; upperright[0] += boxlMpc/2; - upperright[1] += ny*res/2.; - + upperright[1] += ny*phys_res/2.; } void LensMap::Myread(std::string fits_input_file){ @@ -597,30 +639,33 @@ void LensMap::Myread(std::string fits_input_file){ h3.read(gamma1_bar); CCfits::ExtHDU &h4=ff->extension(4); h4.read(gamma2_bar); - std::cout << h0 << h1 << h2 << h3 << h4 << std::endl; + //std::cout << h0 << h1 << h2 << h3 << h4 << std::endl; /* these are always present in ea*/ //h0.readKey ("REDSHIFT",z); - h0.readKey("SIDEL2",boxlMpc); - - double res = boxlMpc/nx; + double yrange; + h0.readKey("SIDEL1",boxlMpc); + h0.readKey("SIDEL2",yrange); + + //double res = boxlMpc/nx; center *= 0; lowerleft = center; lowerleft[0] -= boxlMpc/2; - lowerleft[1] -= res*ny/2; + lowerleft[1] -= yrange/2; upperright = center; upperright[0] += boxlMpc/2; - upperright[1] += ny*res/2.; + upperright[1] += yrange/2.; } void LensMap::read_sub(std::string fits_input_file ,const std::vector &first /// 2d vector for pixel of lower left, (1,1) offset ,const std::vector &last /// 2d vector for pixel of upper right, (1,1) offset - ,float h - ,float z + ,double angDist + // ,float h + // ,float z ){ //std:: cout << " reading lens density map file: " << fits_input_file << std:: endl; @@ -639,16 +684,16 @@ void LensMap::read_sub(std::string fits_input_file h0.readAllKeys(); /* these are always present in ea*/ - float wlow,wup,res; + float res; h0.readKey ("CD1_1",res); // resolution in //h0.readKey ("REDSHIFT",z); - h0.readKey ("WLOW",wlow); - h0.readKey ("WUP",wup); + //h0.readKey ("WLOW",wlow); + //h0.readKey ("WUP",wup); - double D = 3*(pow(wup,4) - pow(wlow,4))/(pow(wup,3) - pow(wlow,3))/4; - if(wlow == wup) D = wup; - D /= (1+z)*h; - res *= degreesTOradians*D; +// double D = 3*(pow(wup,4) - pow(wlow,4))/(pow(wup,3) - pow(wlow,3))/4; +// if(wlow == wup) D = wup; +// D /= (1+z)*h; + res *= degreesTOradians*angDist; boxlMpc = res*nx; assert(h0.axes() >= 2); @@ -690,8 +735,9 @@ void LensMap::read_sub(std::string fits_input_file void LensMap::read_sub(CCfits::FITS *ff ,const std::vector &first ,const std::vector &last - ,float h - ,float z + ,double angDist +// ,float h +// ,float z ){ CCfits::PHDU &h0 = ff->pHDU(); @@ -704,13 +750,13 @@ void LensMap::read_sub(CCfits::FITS *ff float wlow,wup,res; h0.readKey ("CD1_1",res); // resolution in //h0.readKey ("REDSHIFT",z); - h0.readKey ("WLOW",wlow); - h0.readKey ("WUP",wup); + //h0.readKey ("WLOW",wlow); + //h0.readKey ("WUP",wup); - double D = 3*(pow(wup,4) - pow(wlow,4))/(pow(wup,3) - pow(wlow,3))/4; - if(wlow == wup) D = wup; - D /= (1+z)*h; - res *= degreesTOradians*D; + //double D = 3*(pow(wup,4) - pow(wlow,4))/(pow(wup,3) - pow(wlow,3))/4; + //if(wlow == wup) D = wup; + //D /= (1+z)*h; + res *= degreesTOradians * angDist; double boxlMpc_orig = res*nx_orig; assert(h0.axes() >= 2); @@ -767,8 +813,8 @@ void LensMap::write(std::string filename PHDU *phout=&fout->pHDU(); - phout->addKey("SIDEL2",boxlMpc,""); - //phout->addKey("REDSHIFT",z,""); + phout->addKey("SIDEL1",boxlMpc,"x range in physical Mpc"); + phout->addKey("SIDEL2",y_range(),"y range in physical Mpc"); phout->write( 1,nx*ny,surface_density ); @@ -782,7 +828,7 @@ void LensMap::write(std::string filename ExtHDU *eh4=fout->addImage("gamma2", FLOAT_IMG, naxex); eh4->write(1,nx*ny,gamma2_bar); - std::cout << *phout << std::endl; + //std::cout << *phout << std::endl; #else std::cout << "Please enable the preprocessor flag ENABLE_FITS !" << std::endl; exit(1); diff --git a/include/multimap.h b/include/multimap.h index 6bb488a0..a8738d88 100644 --- a/include/multimap.h +++ b/include/multimap.h @@ -50,39 +50,47 @@ struct LensMap{ Point_2d lowerleft; Point_2d upperright; + double x_resolution(){return boxlMpc/nx;} + double y_resolution(){return (upperright[1]-lowerleft[1])/ny;} + double x_range(){return boxlMpc;} + double y_range(){return (upperright[1]-lowerleft[1]);} + //double z; #ifdef ENABLE_FITS - LensMap(std::string fits_input_file,float h,float z){ - read(fits_input_file,h,z); + LensMap(std::string fits_input_file,double angDist){ + read(fits_input_file,angDist); } /// read an entire map - void read(std::string input_fits,float h,float z); + void read(std::string input_fits,double angDist);//,float h,float z); /// read from a file that has been generated with LensMap::write() void Myread(std::string fits_input_file); /// read only header information - void read_header(std::string input_fits,float h,float z); + //void read_header(std::string input_fits,float h,float z); + void read_header(std::string input_fits,double angDist); /// read a subsection of the fits map void read_sub(std::string input_fits - ,const std::vector &first - ,const std::vector &last - ,float h - ,float z - ); + ,const std::vector &first + ,const std::vector &last + ,double Dist +// ,float h +// ,float z + ); - void read_header(std::unique_ptr ff,float h); + //void read_header(std::unique_ptr ff,float h); void read_sub(CCfits::FITS *ff ,const std::vector &first ,const std::vector &last - ,float h - ,float z + ,double Dist +// ,float h +// ,float z ); void write(std::string filename); diff --git a/include/point.h b/include/point.h index edebd376..369aa48f 100644 --- a/include/point.h +++ b/include/point.h @@ -104,10 +104,7 @@ struct Point_2d{ return *this; } Point_2d operator*(PosType value) const{ - Point_2d tmp; - tmp[0] = x[0]*value; - tmp[1] = x[1]*value; - return tmp; + return Point_2d(x[0]*value,x[1]*value); } /// scalar product From c558affeb4113ec7f23f66fb5526d974866e0655 Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Sun, 21 Jul 2019 14:38:50 +0200 Subject: [PATCH 167/227] Added PixelMap::paste() --- ImageProcessing/pixelize.cpp | 40 ++++++++++++++++++++++++++++++------ include/image_processing.h | 14 ++++++++++++- 2 files changed, 47 insertions(+), 7 deletions(-) diff --git a/ImageProcessing/pixelize.cpp b/ImageProcessing/pixelize.cpp index b781bf1d..e2bb6f35 100644 --- a/ImageProcessing/pixelize.cpp +++ b/ImageProcessing/pixelize.cpp @@ -391,12 +391,6 @@ void PixelMap::swap(PixelMap &map1,PixelMap &map2) return; } -/// Zero the whole map -void PixelMap::Clean() -{ - map *= 0; -} - /// Multiplies the whole map by a scalar factor void PixelMap::Renormalize(PosType factor) { @@ -2216,6 +2210,40 @@ void PixelMap::copy_in( } } } +void PixelMap::paste(const PixelMap& pmap){ + + if(resolution < pmap.resolution){ + std::cerr << "PixeLMap::paste() resolution of image pasted in must of equal or higher resolution" << std::endl; + throw std::invalid_argument("low resolution"); + } + + // case where the maps do not overlap + if( (map_boundary_p1[0] > pmap.map_boundary_p2[0]) || (pmap.map_boundary_p1[0] > map_boundary_p2[0]) + || (map_boundary_p1[1] > pmap.map_boundary_p2[1]) || (pmap.map_boundary_p1[1] > map_boundary_p2[1]) + ){ + return; + } + + double x[2]; + + if(map.size() < pmap.map.size() ){ + for(size_t i=0 ; i < map.size() ; ++i ){ + find_position(x,i); + long j = pmap.find_index(x[0], x[1]); + if(j >= 0 ){ + map[i] = pmap(j); + } + } + }else{ + for(size_t j=0 ; j < pmap.map.size() ; ++j ){ + pmap.find_position(x,j); + long i = find_index(x[0], x[1]); + if(i >= 0 ){ + map[i] = pmap(j); + } + } + } +} void PixelMap::recenter(PosType c[2] /// new center ){ diff --git a/include/image_processing.h b/include/image_processing.h index 3794d146..9b1732d4 100644 --- a/include/image_processing.h +++ b/include/image_processing.h @@ -60,7 +60,8 @@ class PixelMap } inline double getResolution() const { return resolution; } - void Clean(); + // Zero the whole map + void Clean(){map *= 0;} void AddImages(ImageInfo *imageinfo,int Nimages,float rescale = 1.); void AddImages(std::vector &imageinfo,int Nimages,float rescale = 1.); @@ -80,6 +81,17 @@ class PixelMap of the map within the area of overlaping pixels. **/ void copy_in(const PixelMap& pmap); + + /** \brief Replace overlaping pixel values with those of the input map. + + No attempt is made to interpolate or average the pixels of pmap. No integration + is done. If the resolution of input map is higher than the reslution of the map then + the pixels values will just be that of the last pixel visited while going through them. + + This can be used to sew tiles together into a larger map. + **/ + void paste(const PixelMap& pmap); + /** \brief copy a PixelMap that must be the same without creating a new one.. This avoids calling a any constructor or destructor. From b9d453315d378680cb58541459d13b6367206a07 Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Tue, 23 Jul 2019 12:50:24 +0200 Subject: [PATCH 168/227] Added subtraction average mass as default in LensHaloMultiMap class. --- MultiPlane/multimap.cpp | 9 +++++++++ include/multimap.h | 1 + 2 files changed, 10 insertions(+) diff --git a/MultiPlane/multimap.cpp b/MultiPlane/multimap.cpp index 418c799e..1c7bcbff 100644 --- a/MultiPlane/multimap.cpp +++ b/MultiPlane/multimap.cpp @@ -14,6 +14,7 @@ LensHaloMultiMap::LensHaloMultiMap( ,double redshift ,double mass_unit ,COSMOLOGY &c + ,bool subtract_ave ,bool single_grid_mode ):LensHalo(redshift,c),single_grid(single_grid_mode),cosmo(c),mass_unit(mass_unit),fitsfilename(fitsfile) { @@ -162,8 +163,16 @@ LensHaloMultiMap::LensHaloMultiMap( //double area = 1.0/mass_unit; // convert to + double ave = 0; for(auto &p : long_range_map.surface_density){ p /= area; + ave += p; + } + if(subtract_ave){ + ave /= long_range_map.surface_density.size(); + for(float &p : long_range_map.surface_density){ + p -= ave; + } } if( !Utilities::IO::file_exists(fitsfile + "_lr.fits") ){ diff --git a/include/multimap.h b/include/multimap.h index a8738d88..2fe69ef7 100644 --- a/include/multimap.h +++ b/include/multimap.h @@ -125,6 +125,7 @@ class LensHaloMultiMap : public LensHalo ,double redshift ,double mass_unit /// should include h factors ,COSMOLOGY &c + ,bool subtract_ave = true /// subtract the average of the full field ,bool single_grid_mode = false ); From 61093aafcf966032b3a3fba0123c4db0c3e16104 Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Tue, 23 Jul 2019 12:52:28 +0200 Subject: [PATCH 169/227] Some test lines. --- MultiPlane/multimap.cpp | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/MultiPlane/multimap.cpp b/MultiPlane/multimap.cpp index 418c799e..7603d467 100644 --- a/MultiPlane/multimap.cpp +++ b/MultiPlane/multimap.cpp @@ -199,10 +199,11 @@ void LensHaloMultiMap::submapPhys(Point_2d ll,Point_2d ur){ ur = (ur - long_range_map.lowerleft)/resolution; - std::cout << ur << std::endl; - std::cout << long_range_map.nx << " " << long_range_map.ny << std::endl; - assert(ur[0] < long_range_map.boxlMpc/resolution + 0.1); - assert(ur[1] < long_range_map.boxlMpc/resolution + 0.1); + std::cout << "ur = " << ur << std::endl; + std::cout << "dim. original " << Noriginal[0] + << " " << Noriginal[1] << std::endl; + assert(ur[0] < long_range_map.x_range()/resolution + 0.1); + assert(ur[1] < long_range_map.y_range()/resolution + 0.1); lower_left[0] = floor(ll[0]); lower_left[1] = floor(ll[1]); From 3cb8f6bab3dd5c9e7ec94ba5354577be5b8befb9 Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Tue, 23 Jul 2019 16:32:39 +0200 Subject: [PATCH 170/227] Changed GridMap::writePixelMapUniform(LensingVariable lensvar) because there were some striping. --- TreeCode_link/gridmap.cpp | 58 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 56 insertions(+), 2 deletions(-) diff --git a/TreeCode_link/gridmap.cpp b/TreeCode_link/gridmap.cpp index 5740f3f2..8dc2f5c2 100644 --- a/TreeCode_link/gridmap.cpp +++ b/TreeCode_link/gridmap.cpp @@ -236,10 +236,64 @@ PixelMap GridMap::writePixelMapUniform( size_t Ny = Ngrid_init2; PixelMap map( center.x, Nx, Ny,x_range/(Nx-1) ); - map.Clean(); - writePixelMapUniform(map,lensvar); + size_t N = map.size(); + assert(N == Nx*Ny); + double tmp2[2]; + switch (lensvar) { + case ALPHA: + for(size_t i=0 ; ix[0]; + tmp2[1] = i_points[i].x[1] - i_points[i].image->x[1]; + map[i] = sqrt(tmp2[0]*tmp2[0] + tmp2[1]*tmp2[1]); + } + break; + case ALPHA1: + for(size_t i=0 ; ix[0]); + break; + case ALPHA2: + for(size_t i=0 ; ix[1]); + break; + case KAPPA: + for(size_t i=0 ; i Date: Tue, 23 Jul 2019 17:19:56 +0200 Subject: [PATCH 171/227] Changed LensHaloMultiMap::LensHaloMultiMap so that the long range map is not rescaled every time. --- MultiPlane/multimap.cpp | 43 +++++++++++++++++++++-------------------- 1 file changed, 22 insertions(+), 21 deletions(-) diff --git a/MultiPlane/multimap.cpp b/MultiPlane/multimap.cpp index 2d546e65..01b47408 100644 --- a/MultiPlane/multimap.cpp +++ b/MultiPlane/multimap.cpp @@ -60,7 +60,9 @@ LensHaloMultiMap::LensHaloMultiMap( Noriginal[0] = submap.nx; Noriginal[1] = submap.ny; - if( Utilities::IO::file_exists(fitsfile + "_lr.fits") ){ + bool long_range_file_exists = Utilities::IO::file_exists(fitsfile + "_lr.fits"); + + if( long_range_file_exists ){ std::cout << " reading file " << fitsfile + "_lr.fits .. " << std::endl; long_range_map.Myread(fitsfile + "_lr.fits"); @@ -153,29 +155,28 @@ LensHaloMultiMap::LensHaloMultiMap( assert(jj == ny-1); wlr.rs2 = wsr.rs2 = rs2; - } -// double area = pow(long_range_map.boxlMpc/long_range_map.nx,2)/mass_unit; //*** units ??? - //double area = long_range_map.x_resolution() - //*long_range_map.y_resolution()/mass_unit/resolution/resolution; //*** units ??? - double area = long_range_map.x_resolution() - *long_range_map.y_resolution()/mass_unit; //*** units ??? - //double area = 1.0/mass_unit; + + // double area = pow(long_range_map.boxlMpc/long_range_map.nx,2)/mass_unit; //*** units ??? + //double area = long_range_map.x_resolution() + //*long_range_map.y_resolution()/mass_unit/resolution/resolution; //*** units ??? + double area = long_range_map.x_resolution() + *long_range_map.y_resolution()/mass_unit; //*** units ??? + //double area = 1.0/mass_unit; - // convert to - double ave = 0; - for(auto &p : long_range_map.surface_density){ - p /= area; - ave += p; - } - if(subtract_ave){ - ave /= long_range_map.surface_density.size(); - for(float &p : long_range_map.surface_density){ - p -= ave; + // convert to + double ave = 0; + for(auto &p : long_range_map.surface_density){ + p /= area; + ave += p; } - } - - if( !Utilities::IO::file_exists(fitsfile + "_lr.fits") ){ + if(subtract_ave){ + ave /= long_range_map.surface_density.size(); + for(float &p : long_range_map.surface_density){ + p -= ave; + } + } + if(single_grid){ long_range_map.PreProcessFFTWMap(1.0,unit); }else{ From 6017cf49fbe2447d6f60ad2d9f2317995a4063ef Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Tue, 23 Jul 2019 16:32:39 +0200 Subject: [PATCH 172/227] Changed GridMap::writePixelMapUniform(LensingVariable lensvar) because there were some striping. --- TreeCode_link/gridmap.cpp | 58 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 56 insertions(+), 2 deletions(-) diff --git a/TreeCode_link/gridmap.cpp b/TreeCode_link/gridmap.cpp index 5740f3f2..8dc2f5c2 100644 --- a/TreeCode_link/gridmap.cpp +++ b/TreeCode_link/gridmap.cpp @@ -236,10 +236,64 @@ PixelMap GridMap::writePixelMapUniform( size_t Ny = Ngrid_init2; PixelMap map( center.x, Nx, Ny,x_range/(Nx-1) ); - map.Clean(); - writePixelMapUniform(map,lensvar); + size_t N = map.size(); + assert(N == Nx*Ny); + double tmp2[2]; + switch (lensvar) { + case ALPHA: + for(size_t i=0 ; ix[0]; + tmp2[1] = i_points[i].x[1] - i_points[i].image->x[1]; + map[i] = sqrt(tmp2[0]*tmp2[0] + tmp2[1]*tmp2[1]); + } + break; + case ALPHA1: + for(size_t i=0 ; ix[0]); + break; + case ALPHA2: + for(size_t i=0 ; ix[1]); + break; + case KAPPA: + for(size_t i=0 ; i Date: Wed, 24 Jul 2019 13:04:51 +0200 Subject: [PATCH 173/227] Fixed initialisations of kernels when long-range map is read in. --- MultiPlane/multimap.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/MultiPlane/multimap.cpp b/MultiPlane/multimap.cpp index 01b47408..8fc5a79a 100644 --- a/MultiPlane/multimap.cpp +++ b/MultiPlane/multimap.cpp @@ -51,6 +51,8 @@ LensHaloMultiMap::LensHaloMultiMap( //rs2 = 2*4*submap.boxlMpc*submap.boxlMpc/( 2*PI*submap.nx ); rs2 = submap.boxlMpc*submap.boxlMpc/( 2*submap.nx )*g/f; + wlr.rs2 = wsr.rs2 = rs2; + resolution = submap.boxlMpc/submap.nx; //border_width = 4.5*sqrt(rs2)/res + 1; border_width = f*sqrt(rs2)/resolution + 1; @@ -153,9 +155,6 @@ LensHaloMultiMap::LensHaloMultiMap( } } assert(jj == ny-1); - - wlr.rs2 = wsr.rs2 = rs2; - // double area = pow(long_range_map.boxlMpc/long_range_map.nx,2)/mass_unit; //*** units ??? //double area = long_range_map.x_resolution() From 5b6f0cfcc37c62a04143628e8f652dd641415638 Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Fri, 26 Jul 2019 16:47:45 +0200 Subject: [PATCH 174/227] Corrected setup so that it doesn't rescale the long range force when it has already been saved. --- MultiPlane/multimap.cpp | 115 ++++++++++++++++++++++------------------ include/multimap.h | 22 ++++---- 2 files changed, 73 insertions(+), 64 deletions(-) diff --git a/MultiPlane/multimap.cpp b/MultiPlane/multimap.cpp index 01b47408..4d2d108f 100644 --- a/MultiPlane/multimap.cpp +++ b/MultiPlane/multimap.cpp @@ -41,38 +41,41 @@ LensHaloMultiMap::LensHaloMultiMap( LensMap submap; submap.read_header(fitsfilename,D); - //,cosmo.gethubble(),getZlens()); - //long_range_map.boxlMpc = submap.boxlMpc; //std::size_t size = bigmap.nx*bigmap.ny; //rs2 = submap.boxlMpc*submap.boxlMpc/( 2*PI*submap.nx ); //rs2 = 2*4*submap.boxlMpc*submap.boxlMpc/( 2*PI*submap.nx ); - rs2 = submap.boxlMpc*submap.boxlMpc/( 2*submap.nx )*g/f; + rs2 = submap.boxlMpc*submap.boxlMpc/( 2*submap.nx )*gfactor/ffactor; + + wlr.rs2 = wsr.rs2 = rs2; - resolution = submap.boxlMpc/submap.nx; - //border_width = 4.5*sqrt(rs2)/res + 1; - border_width = f*sqrt(rs2)/resolution + 1; + resolution = submap.boxlMpc / submap.nx ; - int desample = sqrt(0.5*submap.nx)/sqrt(f*g); + //border_width = 4.5*sqrt(rs2)/res + 1; + border_width = ffactor * sqrt(rs2) / resolution + 1; + + int desample = sqrt(0.5*submap.nx) / sqrt(ffactor * gfactor); + //int desample = 1; // ???? Noriginal[0] = submap.nx; Noriginal[1] = submap.ny; bool long_range_file_exists = Utilities::IO::file_exists(fitsfile + "_lr.fits"); - if( long_range_file_exists ){ + long_range_file_exists = false; // ????? + + if( long_range_file_exists && !single_grid ){ std::cout << " reading file " << fitsfile + "_lr.fits .. " << std::endl; long_range_map.Myread(fitsfile + "_lr.fits"); }else if(single_grid){ std::vector first = {1,1}; - std::vector last = {(long)Noriginal[0],(long)Noriginal[1]}; + long_range_map.read_sub(ff,first,last,getDist()); - //,cosmo.gethubble(),getZlens()); //double res = submap.boxlMpc/submap.nx; //Point_2d dmap = {submap.boxlMpc,submap.ny*resolution}; @@ -81,6 +84,26 @@ LensHaloMultiMap::LensHaloMultiMap( //long_range_map.upperright = long_range_map.center + dmap; //long_range_map.lowerleft = long_range_map.center - dmap; //long_range_map.boxlMpc = submap.boxlMpc; + + double area = long_range_map.x_resolution() + *long_range_map.y_resolution()/mass_unit; //*** units ??? + + //double area = 1.0/mass_unit; + + // convert to + double ave = 0; + for(auto &p : long_range_map.surface_density){ + p /= area; + ave += p; + } + if(subtract_ave){ + ave /= long_range_map.surface_density.size(); + for(float &p : long_range_map.surface_density){ + p -= ave; + } + } + + long_range_map.PreProcessFFTWMap(1.0,unit); }else{ @@ -93,19 +116,20 @@ LensHaloMultiMap::LensHaloMultiMap( Point_2d range = long_range_map.upperright - long_range_map.lowerleft; long_range_map.nx = submap.nx/desample; - double res_x = long_range_map.boxlMpc/long_range_map.nx; - + double res_x = long_range_map.boxlMpc / long_range_map.nx; + // get the ny that makes the pixels clossest to square double Ly = submap.y_range(); long_range_map.ny = (int)( Ly/res_x ); + //long_range_map.ny = (int)( Ly/res_x ) - 1; // ???? if(Ly - long_range_map.ny*res_x > res_x/2) long_range_map.ny += 1; size_t nx = long_range_map.nx; size_t ny = long_range_map.ny; double res_y = submap.y_range()/ny; - - std::cout << "ratio of low resolution resolutions " + + std::cout << "ratio of low resolution pixel dimensions " << res_x/res_y << std::endl; size_t N = long_range_map.ny*long_range_map.nx; @@ -135,7 +159,7 @@ LensHaloMultiMap::LensHaloMultiMap( last[1] = MIN(j + chunk,Noriginal[1]); //submap.read_sub(fitsfilename,first,last,cosmo.gethubble()); submap.read_sub(ff,first,last,getDist()); - //,cosmo.gethubble(),getZlens()); + kj = 0; }else{ kj += submap.nx; @@ -154,8 +178,6 @@ LensHaloMultiMap::LensHaloMultiMap( } assert(jj == ny-1); - wlr.rs2 = wsr.rs2 = rs2; - // double area = pow(long_range_map.boxlMpc/long_range_map.nx,2)/mass_unit; //*** units ??? //double area = long_range_map.x_resolution() @@ -177,12 +199,8 @@ LensHaloMultiMap::LensHaloMultiMap( } } - if(single_grid){ - long_range_map.PreProcessFFTWMap(1.0,unit); - }else{ - long_range_map.PreProcessFFTWMap(1.0,wlr); - long_range_map.write("!" + fitsfile + "_lr.fits"); - } + long_range_map.PreProcessFFTWMap(1.0,wlr); + long_range_map.write("!" + fitsfile + "_lr.fits"); } }; @@ -258,8 +276,10 @@ void LensHaloMultiMap::submap( LensMap map; if( (first[0] > 0)*(first[1] > 0)*(last[0] <= Noriginal[0])*(last[1] <= Noriginal[1]) ){ - map.read_sub(fitsfilename,first,last,getDist()); - //,cosmo.gethubble(),getZlens()); + //map.read_sub(fitsfilename,first,last,getDist()); + + map.read_sub(ff,first,last,getDist()); + }else{ size_t nx_big = map.nx = last[0] - first[0] + 1; @@ -291,7 +311,7 @@ void LensHaloMultiMap::submap( first_sub[1] = jj + 1; last_sub[1] = MIN(jj + chunk + 1,Noriginal[1]); //partmap.read_sub(fitsfilename,first_sub,last_sub,cosmo.gethubble()); partmap.read_sub(ff,first_sub,last_sub,getDist()); - //,cosmo.gethubble(),getZlens()); + k = 0; }else{ ++k; @@ -538,7 +558,7 @@ void LensMap::read_header(std::string fits_input_file upperright = center; upperright[0] += boxlMpc/2; - upperright[1] += ny*phys_res/2.; + upperright[1] += phys_res*ny/2.; } void LensMap::read(std::string fits_input_file,double angDist){ @@ -591,7 +611,7 @@ void LensMap::read(std::string fits_input_file,double angDist){ //D /= (1+z)*h; res *= degreesTOradians*angDist; - boxlMpc = res*nx; + boxlMpc = res * nx; } catch(CCfits::HDU::NoSuchKeyword){ @@ -603,16 +623,16 @@ void LensMap::read(std::string fits_input_file,double angDist){ exit(1); } - double phys_res = boxlMpc/nx; + double phys_res = boxlMpc/ nx; center *= 0; lowerleft = center; lowerleft[0] -= boxlMpc/2; - lowerleft[1] -= phys_res*ny/2; + lowerleft[1] -= phys_res * ny /2; upperright = center; upperright[0] += boxlMpc/2; - upperright[1] += ny*phys_res/2.; + upperright[1] += phys_res * ny /2.; } void LensMap::Myread(std::string fits_input_file){ @@ -671,30 +691,20 @@ void LensMap::Myread(std::string fits_input_file){ } +/* void LensMap::read_sub(std::string fits_input_file ,const std::vector &first /// 2d vector for pixel of lower left, (1,1) offset ,const std::vector &last /// 2d vector for pixel of upper right, (1,1) offset ,double angDist - // ,float h - // ,float z ){ //std:: cout << " reading lens density map file: " << fits_input_file << std:: endl; std::unique_ptr ff(new CCfits::FITS (fits_input_file, CCfits::Read)); - /*std::unique_ptr ff(nullptr); - try{ - //std::unique_ptr ff(new CCfits::FITS (fits_input_file, CCfits::Read)); - - ff.reset(new CCfits::FITS (fits_input_file, CCfits::Read)); - } - catch(CCfits::FitsError){ - std::cerr << " error in opening fits file " << fits_input_file << std::endl; - }*/ CCfits::PHDU &h0 = ff->pHDU(); h0.readAllKeys(); - /* these are always present in ea*/ + // these are always present in each float res; h0.readKey ("CD1_1",res); // resolution in //h0.readKey ("REDSHIFT",z); @@ -717,6 +727,8 @@ void LensMap::read_sub(std::string fits_input_file // set the full field lower left lowerleft = {-boxlMpc/2,-res*ny/2}; + upperright = lowerleft; + nx = h0.axis(0); ny = h0.axis(1); @@ -729,11 +741,9 @@ void LensMap::read_sub(std::string fits_input_file //lowerleft[0] = boxlMpc*(2*first[0] - nx)/2 - boxlMpc/2; //lowerleft[1] = boxlMpc*(2*first[1] - ny)/2 - res*ny/2; - upperright = lowerleft; - upperright[0] += last[0]*res; upperright[1] += last[1]*res; - + center = (lowerleft + upperright)/2; nx = last[0] - first[0] + 1; @@ -741,15 +751,14 @@ void LensMap::read_sub(std::string fits_input_file boxlMpc = res*nx; } - +*/ void LensMap::read_sub(CCfits::FITS *ff ,const std::vector &first ,const std::vector &last ,double angDist -// ,float h -// ,float z ){ + CCfits::PHDU &h0 = ff->pHDU(); long nx_orig = h0.axis(0); @@ -758,7 +767,8 @@ void LensMap::read_sub(CCfits::FITS *ff h0.readAllKeys(); // these are always present in each - float wlow,wup,res; + //float wlow,wup,res; + float res; h0.readKey ("CD1_1",res); // resolution in //h0.readKey ("REDSHIFT",z); //h0.readKey ("WLOW",wlow); @@ -768,7 +778,7 @@ void LensMap::read_sub(CCfits::FITS *ff //if(wlow == wup) D = wup; //D /= (1+z)*h; res *= degreesTOradians * angDist; - double boxlMpc_orig = res*nx_orig; + double boxlMpc_orig = res * nx_orig; assert(h0.axes() >= 2); std::vector stride = {1,1}; @@ -793,6 +803,9 @@ void LensMap::read_sub(CCfits::FITS *ff ny = last[1] - first[1] + 1; boxlMpc = res*nx; + + + std::cout << "Subs map resolution : " << x_resolution() << " " << y_resolution() << std::endl; } /** diff --git a/include/multimap.h b/include/multimap.h index 2fe69ef7..8f4da8d4 100644 --- a/include/multimap.h +++ b/include/multimap.h @@ -50,8 +50,8 @@ struct LensMap{ Point_2d lowerleft; Point_2d upperright; - double x_resolution(){return boxlMpc/nx;} - double y_resolution(){return (upperright[1]-lowerleft[1])/ny;} + double x_resolution(){return boxlMpc / nx ;} // nx or nx-1 ???? + double y_resolution(){return (upperright[1]-lowerleft[1])/ny;} // ???? double x_range(){return boxlMpc;} double y_range(){return (upperright[1]-lowerleft[1]);} @@ -74,13 +74,11 @@ struct LensMap{ void read_header(std::string input_fits,double angDist); /// read a subsection of the fits map - void read_sub(std::string input_fits - ,const std::vector &first - ,const std::vector &last - ,double Dist -// ,float h -// ,float z - ); +// void read_sub(std::string input_fits +// ,const std::vector &first +// ,const std::vector &last +// ,double Dist +// ); //void read_header(std::unique_ptr ff,float h); @@ -89,8 +87,6 @@ struct LensMap{ ,const std::vector &first ,const std::vector &last ,double Dist -// ,float h -// ,float z ); void write(std::string filename); @@ -133,7 +129,8 @@ class LensHaloMultiMap : public LensHalo delete ff; }; - const double f = 5,g = 6; + //const double ffactor = 5,gfactor = 6; + const double ffactor = 10,gfactor = 10; // ?????? /// Set highres map be specifying the corners in pixel values void submap( @@ -246,7 +243,6 @@ class LensHaloMultiMap : public LensHalo double max_pix = std::numeric_limits::lowest(); double min_pix = std::numeric_limits::max(); -private: // ????? double mass_unit; size_t Noriginal[2]; // number of pixels in each dimension in original image From 17e12124aba11d1dac4e4e122a9978f6d6fad1fb Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Sat, 27 Jul 2019 19:26:31 +0200 Subject: [PATCH 175/227] Solved problem with different resolutions for full and multimap ray-shooting. The test cases work very well with this. Added LensMap::evaluate() which enforces the consistency of scales. --- MultiPlane/multimap.cpp | 140 +++++++++++++++++++++------------------- include/multimap.h | 2 +- 2 files changed, 76 insertions(+), 66 deletions(-) diff --git a/MultiPlane/multimap.cpp b/MultiPlane/multimap.cpp index 4d2d108f..e862004d 100644 --- a/MultiPlane/multimap.cpp +++ b/MultiPlane/multimap.cpp @@ -387,6 +387,57 @@ void LensHaloMultiMap::submap( } +bool LensMap::evaluate(const double *x,float &sigma,float *gamma,double *alpha) { + + //double fx = ((x[0] - center[0])/range + 0.5)*(nx-1); + //double fy = ((x[1] - center[1])/range_y + 0.5)*(ny-1); + //std::cout << "( " << fx << " " << fy << " "; + + double fx = (x[0] - lowerleft[0]) / x_resolution(); + double fy = (x[1] - lowerleft[1]) / y_resolution(); + + if( (fx>=0)*(fx=0)*(fy > interp(xx - ,long_range_map.nx,long_range_map.boxlMpc - ,long_range_map.ny - ,long_range_map.ny*long_range_map.boxlMpc/long_range_map.nx - ,long_range_map.center.x); - - //assert(long_range_map.nx == long_range_map.ny); - - alpha[0] = interp.interpolate(long_range_map.alpha1_bar); - alpha[1] = interp.interpolate(long_range_map.alpha2_bar); - gamma[0] = interp.interpolate(long_range_map.gamma1_bar); - gamma[1] = interp.interpolate(long_range_map.gamma2_bar); - gamma[2] = 0.0; - *kappa = interp.interpolate(long_range_map.surface_density); - //*phi = interp.interpolate(long_range_map.phi_bar); - + + long_range_map.evaluate(xx,*kappa,gamma,alpha); return; } if( (xx[0] >= short_range_map.lowerleft[0])*(xx[0] <= short_range_map.upperright[0]) *(xx[1] >= short_range_map.lowerleft[1])*(xx[1] <= short_range_map.upperright[1]) ){ - - Utilities::Interpolator > interp(xx - ,long_range_map.nx,long_range_map.boxlMpc - ,long_range_map.ny - ,long_range_map.ny*long_range_map.boxlMpc/long_range_map.nx - ,long_range_map.center.x); - - //assert(long_range_map.nx == long_range_map.ny); - - alpha[0] = interp.interpolate(long_range_map.alpha1_bar); - alpha[1] = interp.interpolate(long_range_map.alpha2_bar); - gamma[0] = interp.interpolate(long_range_map.gamma1_bar); - gamma[1] = interp.interpolate(long_range_map.gamma2_bar); - gamma[2] = 0.0; - *kappa = interp.interpolate(long_range_map.surface_density); - //*phi = interp.interpolate(long_range_map.phi_bar); - - //assert(alpha[0] == alpha[0] && alpha[1] == alpha[1]); - //assert(gamma[0] == gamma[0] && gamma[1] == gamma[1]); - //assert(kappa == kappa); - - Utilities::Interpolator > short_interp(xx - ,short_range_map.nx,short_range_map.boxlMpc - ,short_range_map.ny - ,short_range_map.ny*short_range_map.boxlMpc/short_range_map.nx - ,short_range_map.center.x); - - - alpha[0] += short_interp.interpolate(short_range_map.alpha1_bar); - alpha[1] += short_interp.interpolate(short_range_map.alpha2_bar); - gamma[0] += short_interp.interpolate(short_range_map.gamma1_bar); - gamma[1] += short_interp.interpolate(short_range_map.gamma2_bar); - *kappa += short_interp.interpolate(short_range_map.surface_density); - //*phi = interp.interpolate(short_range_map.phi_bar); - - //assert(alpha[0] == alpha[0] && alpha[1] == alpha[1]); - //assert(gamma[0] == gamma[0] && gamma[1] == gamma[1]); - //assert(kappa == kappa); + + long_range_map.evaluate(xx,*kappa,gamma,alpha); + + float t_kappa,t_gamma[3]; + double t_alpha[2]; + short_range_map.evaluate(xx,t_kappa,t_gamma,t_alpha); + + alpha[0] += t_alpha[0]; + alpha[1] += t_alpha[1]; + gamma[0] += t_gamma[0]; + gamma[1] += t_gamma[1]; + *kappa += t_kappa; + }else{ alpha[0] = 0; @@ -541,10 +553,9 @@ void LensMap::read_header(std::string fits_input_file catch(CCfits::HDU::NoSuchKeyword){ std::cerr << "LensMap fits map must have header keywords:" << std::endl - << " CD1_1 - length on other side in Mpc/h" << std::endl - << " REDSHIFT - redshift of lens" << std::endl - << " WLOW - closest radial distance in cMpc/h" << std::endl - << " WUP - furthest radial distance in cMpc/h" << std::endl; + << " CD1_1 - length on other side in Mpc/h" << std::endl; + //<< " WLOW - closest radial distance in cMpc/h" << std::endl + //<< " WUP - furthest radial distance in cMpc/h" << std::endl; exit(1); } @@ -601,7 +612,7 @@ void LensMap::read(std::string fits_input_file,double angDist){ } try{ /* these are always present in ea*/ - float wlow,wup,res; + float res; h0.readKey ("CD1_1",res); // angular resolution degrees //h0.readKey ("REDSHIFT",z); //h0.readKey ("WLOW",wlow); @@ -616,10 +627,10 @@ void LensMap::read(std::string fits_input_file,double angDist){ catch(CCfits::HDU::NoSuchKeyword){ std::cerr << "LensMap fits map must have header keywords:" << std::endl - << " CD1_1 - length on other side in Mpc/h" << std::endl - << " REDSHIFT - redshift of lens" << std::endl - << " WLOW - closest radial distance in cMpc/h" << std::endl - << " WUP - furthest radial distance in cMpc/h" << std::endl; + << " CD1_1 - length on other side in Mpc/h" << std::endl; + //<< " REDSHIFT - redshift of lens" << std::endl + //<< " WLOW - closest radial distance in cMpc/h" << std::endl + //<< " WUP - furthest radial distance in cMpc/h" << std::endl; exit(1); } @@ -672,7 +683,7 @@ void LensMap::Myread(std::string fits_input_file){ h4.read(gamma2_bar); //std::cout << h0 << h1 << h2 << h3 << h4 << std::endl; - /* these are always present in ea*/ + // these are always present in each //h0.readKey ("REDSHIFT",z); double yrange; h0.readKey("SIDEL1",boxlMpc); @@ -804,7 +815,6 @@ void LensMap::read_sub(CCfits::FITS *ff boxlMpc = res*nx; - std::cout << "Subs map resolution : " << x_resolution() << " " << y_resolution() << std::endl; } diff --git a/include/multimap.h b/include/multimap.h index 8f4da8d4..f10ff3fb 100644 --- a/include/multimap.h +++ b/include/multimap.h @@ -55,7 +55,7 @@ struct LensMap{ double x_range(){return boxlMpc;} double y_range(){return (upperright[1]-lowerleft[1]);} - //double z; + bool evaluate(const double *x,float &sigma,float *gamma,double *alpha); #ifdef ENABLE_FITS From a366acb0a35d2e25d7dde4f86687c60a297fd411 Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Mon, 29 Jul 2019 15:42:50 +0200 Subject: [PATCH 176/227] Change to comments. --- MultiPlane/multimap.cpp | 3 +-- include/multimap.h | 12 ++++++------ 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/MultiPlane/multimap.cpp b/MultiPlane/multimap.cpp index e862004d..d3f9b3e8 100644 --- a/MultiPlane/multimap.cpp +++ b/MultiPlane/multimap.cpp @@ -57,7 +57,6 @@ LensHaloMultiMap::LensHaloMultiMap( border_width = ffactor * sqrt(rs2) / resolution + 1; int desample = sqrt(0.5*submap.nx) / sqrt(ffactor * gfactor); - //int desample = 1; // ???? Noriginal[0] = submap.nx; Noriginal[1] = submap.ny; @@ -815,7 +814,7 @@ void LensMap::read_sub(CCfits::FITS *ff boxlMpc = res*nx; - std::cout << "Subs map resolution : " << x_resolution() << " " << y_resolution() << std::endl; + //std::cout << "Subs map resolution : " << x_resolution() << " " << y_resolution() << std::endl; } /** diff --git a/include/multimap.h b/include/multimap.h index f10ff3fb..f86e8b26 100644 --- a/include/multimap.h +++ b/include/multimap.h @@ -50,8 +50,8 @@ struct LensMap{ Point_2d lowerleft; Point_2d upperright; - double x_resolution(){return boxlMpc / nx ;} // nx or nx-1 ???? - double y_resolution(){return (upperright[1]-lowerleft[1])/ny;} // ???? + double x_resolution(){return boxlMpc / nx ;} + double y_resolution(){return (upperright[1]-lowerleft[1])/ny;} double x_range(){return boxlMpc;} double y_range(){return (upperright[1]-lowerleft[1]);} @@ -129,9 +129,9 @@ class LensHaloMultiMap : public LensHalo delete ff; }; - //const double ffactor = 5,gfactor = 6; - const double ffactor = 10,gfactor = 10; // ?????? - + const double ffactor = 5,gfactor = 5; + //const double ffactor = 10,gfactor = 10; + /// Set highres map be specifying the corners in pixel values void submap( const std::vector &lower_left @@ -230,7 +230,7 @@ class LensHaloMultiMap : public LensHalo wlr = m.wlr; } -public: // ????? +public: LensMap long_range_map; LensMap short_range_map; From 672009e23e3a70563ba28346def471b7b69afaa1 Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Thu, 1 Aug 2019 12:02:24 +0200 Subject: [PATCH 177/227] Added Grid::AddSurfaceBrightness() GridMap::AddSurfaceBrightness() --- TreeCode_link/grid_maintenance.cpp | 41 +++++++++++++++++++++++------- TreeCode_link/gridmap.cpp | 13 ++++++++++ include/grid_maintenance.h | 2 ++ include/gridmap.h | 19 +++++++++++++- 4 files changed, 65 insertions(+), 10 deletions(-) diff --git a/TreeCode_link/grid_maintenance.cpp b/TreeCode_link/grid_maintenance.cpp index 37524a45..284a2cfa 100644 --- a/TreeCode_link/grid_maintenance.cpp +++ b/TreeCode_link/grid_maintenance.cpp @@ -290,21 +290,44 @@ void Grid::ReShoot(LensHndl lens){ * returns the sum of the surface brightnesses */ PosType Grid::RefreshSurfaceBrightnesses(SourceHndl source){ - PosType total=0,tmp; + PosType total=0,tmp; PointList::iterator s_tree_pointlist_it; s_tree_pointlist_it.current = (s_tree->pointlist->Top()); - for(unsigned long i=0;ipointlist->size();++i,--s_tree_pointlist_it){ - tmp = source->SurfaceBrightness((*s_tree_pointlist_it)->x); - (*s_tree_pointlist_it)->surface_brightness = (*s_tree_pointlist_it)->image->surface_brightness + for(unsigned long i=0;ipointlist->size();++i,--s_tree_pointlist_it){ + tmp = source->SurfaceBrightness((*s_tree_pointlist_it)->x); + (*s_tree_pointlist_it)->surface_brightness = (*s_tree_pointlist_it)->image->surface_brightness = tmp; - total += tmp;//*pow( s_tree->pointlist->current->gridsize,2); - assert((*s_tree_pointlist_it)->surface_brightness >= 0.0); - (*s_tree_pointlist_it)->in_image = (*s_tree_pointlist_it)->image->in_image + total += tmp;//*pow( s_tree->pointlist->current->gridsize,2); + assert((*s_tree_pointlist_it)->surface_brightness >= 0.0); + (*s_tree_pointlist_it)->in_image = (*s_tree_pointlist_it)->image->in_image = NO; - } + } - return total; + return total; +} +/** + * \brief Recalculate surface brightness just like Grid::RefreshSurfaceBrightness but + * the new source is added to any sources that were already there. + * + * returns the sum of the surface brightnesses from the new source + */ +PosType Grid::AddSurfaceBrightnesses(SourceHndl source){ + PosType total=0,tmp; + + PointList::iterator s_tree_pointlist_it; + s_tree_pointlist_it.current = (s_tree->pointlist->Top()); + for(unsigned long i=0;ipointlist->size();++i,--s_tree_pointlist_it){ + tmp = source->SurfaceBrightness((*s_tree_pointlist_it)->x); + (*s_tree_pointlist_it)->surface_brightness += tmp; + (*s_tree_pointlist_it)->image->surface_brightness += tmp; + total += tmp;//*pow( s_tree->pointlist->current->gridsize,2); + assert((*s_tree_pointlist_it)->surface_brightness >= 0.0); + (*s_tree_pointlist_it)->in_image = (*s_tree_pointlist_it)->image->in_image + = NO; + } + + return total; } PosType Grid::EinsteinArea() const { diff --git a/TreeCode_link/gridmap.cpp b/TreeCode_link/gridmap.cpp index 8dc2f5c2..7cc6aa82 100644 --- a/TreeCode_link/gridmap.cpp +++ b/TreeCode_link/gridmap.cpp @@ -194,6 +194,19 @@ double GridMap::RefreshSurfaceBrightnesses(SourceHndl source){ return total; } +double GridMap::AddSurfaceBrightnesses(SourceHndl source){ + PosType total=0,tmp; + + for(size_t i=0;i SurfaceBrightness(s_points[i].x); + s_points[i].surface_brightness += tmp; + s_points[i].image->surface_brightness += tmp; + total += tmp; + s_points[i].in_image = s_points[i].image->in_image = NO; + } + + return total; +} void GridMap::ClearSurfaceBrightnesses(){ diff --git a/include/grid_maintenance.h b/include/grid_maintenance.h index faccd043..83f638fa 100644 --- a/include/grid_maintenance.h +++ b/include/grid_maintenance.h @@ -36,7 +36,9 @@ struct Grid{ unsigned long PrunePointsOutside(double resolution,double *y,double r_in ,double r_out); double RefreshSurfaceBrightnesses(SourceHndl source); + double AddSurfaceBrightnesses(SourceHndl source); double ClearSurfaceBrightnesses(); + unsigned long getNumberOfPoints() const; /// area of region with negative magnification PosType EinsteinArea() const; diff --git a/include/gridmap.h b/include/gridmap.h index eb60c5d7..afb65720 100644 --- a/include/gridmap.h +++ b/include/gridmap.h @@ -33,7 +33,24 @@ struct GridMap{ /// reshoot the rays for example when the source plane has been changed void ReInitializeGrid(LensHndl lens); - double RefreshSurfaceBrightnesses(SourceHndl source); + /** Finding + * \brief Recalculate surface brightness at every point without changing the positions of the gridmap or any lens properties. + * + * Recalculate the surface brightness at all points on the gridmap. + * This is useful when changing the source model while preserving changes in the grid. + * Both i_tree and s_tree are both changed although only s_tree shows up here. + * + * returns the sum of the surface brightnesses + */ + double RefreshSurfaceBrightnesses(SourceHndl source); + /** + * \brief Recalculate surface brightness just like GridMap::RefreshSurfaceBrightness but + * the new source is added to any sources that were already there. + * + * returns the sum of the surface brightnesses from the new source + */ + double AddSurfaceBrightnesses(SourceHndl source); + void ClearSurfaceBrightnesses(); size_t getNumberOfPoints() const {return Ngrid_init*Ngrid_init2;} From 740d8595a3f0e4cd3e127b4e17d79736a8a34500 Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Sun, 11 Aug 2019 11:50:01 +0200 Subject: [PATCH 178/227] Comment changes. --- include/gridmap.h | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/include/gridmap.h b/include/gridmap.h index afb65720..3e2325c0 100644 --- a/include/gridmap.h +++ b/include/gridmap.h @@ -23,8 +23,6 @@ * and thus cannot be used for adaptive mapping or image finding. */ -//class PixelMap; - struct GridMap{ GridMap(LensHndl lens,unsigned long N1d,const double center[2],double range); @@ -33,7 +31,7 @@ struct GridMap{ /// reshoot the rays for example when the source plane has been changed void ReInitializeGrid(LensHndl lens); - /** Finding + /** * \brief Recalculate surface brightness at every point without changing the positions of the gridmap or any lens properties. * * Recalculate the surface brightness at all points on the gridmap. @@ -59,7 +57,7 @@ struct GridMap{ /// return initial range of gridded region. This is the distance from the first ray in a row to the last (unlike PixelMap) double getXRange(){return x_range;} double getYRange(){return x_range*axisratio;} - // resolution in radians + /// resolution in radians, this is range / (N-1) double getResolution(){return x_range/(Ngrid_init-1);} PixelMap writePixelMapUniform(const PosType center[],size_t Nx,size_t Ny,LensingVariable lensvar); @@ -126,4 +124,4 @@ struct GridMap{ static std::mutex grid_mutex; }; -#endif /* defined(__GLAMER__gridmap__) */ +#endif // defined(__GLAMER__gridmap__) From 96dbd828315bc3523b8f30c285f4ce47f99b6f62 Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Wed, 14 Aug 2019 16:35:55 +0200 Subject: [PATCH 179/227] added some things to SphericalPoint which will conflict with the same things added on another branch. --- TreeCode_link/geometry.cpp | 12 ++++++++---- include/geometry.h | 11 +++++++++-- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/TreeCode_link/geometry.cpp b/TreeCode_link/geometry.cpp index 96ebb2ba..221dd5a8 100644 --- a/TreeCode_link/geometry.cpp +++ b/TreeCode_link/geometry.cpp @@ -11,11 +11,15 @@ #include "point.h" /// output cartisian coordinates of the point -void Utilities::Geometry::SphericalPoint::sphericalTOcartisian(PosType x[]) const{ +void Utilities::Geometry::SphericalPoint::TOcartisian(PosType x[]) const{ x[0] = r*cos(theta)*cos(phi); x[1] = r*cos(theta)*sin(phi); x[2] = r*sin(theta); } + /// output cartisian coordinates of the point +Point_3d Utilities::Geometry::SphericalPoint::TOcartisian() const{ + return Point_3d(r*cos(theta)*cos(phi),r*cos(theta)*sin(phi),r*sin(theta)); +} /// set the spherical coordinates of the point from the cartisian coordinates void Utilities::Geometry::SphericalPoint::cartisianTOspherical(PosType const x[]){ @@ -23,7 +27,7 @@ void Utilities::Geometry::SphericalPoint::cartisianTOspherical(PosType const x[] theta = asin(x[2]/r); phi = atan2(x[1],x[0]); } - + /** \brief Calculates the stereographic projection of the point onto a plane. * * The result is in radian units. Near the central point this is a rectolinear projection @@ -111,8 +115,8 @@ void Utilities::Geometry::SphericalPoint::InverseOrthographicProjection( PosType Utilities::Geometry::Seporation(const SphericalPoint &p1,const SphericalPoint &p2){ PosType x1[3],x2[3]; - p1.sphericalTOcartisian(x1); - p2.sphericalTOcartisian(x2); + p1.TOcartisian(x1); + p2.TOcartisian(x2); return sqrt( (x1[0]-x2[0])*(x1[0]-x2[0]) + (x1[1]-x2[1])*(x1[1]-x2[1]) + (x1[2]-x2[2])*(x1[2]-x2[2]) ); } diff --git a/include/geometry.h b/include/geometry.h index c289dcaf..309a7019 100644 --- a/include/geometry.h +++ b/include/geometry.h @@ -30,13 +30,20 @@ namespace Utilities { public: SphericalPoint(PosType r,PosType theta,PosType phi):r(r),theta(theta),phi(phi){}; SphericalPoint():r(0),theta(0),phi(0){}; + SphericalPoint(Point_3d &x){ + r = sqrt( x[0]*x[0] + x[1]*x[1] +x[2]*x[2]); + theta = asin(x[2]/r); + phi = atan2(x[1],x[0]); + } PosType r; PosType theta; PosType phi; /// output Cartesian coordinates of the point - void sphericalTOcartisian(PosType x[]) const; + void TOcartisian(PosType x[]) const; + Point_3d TOcartisian() const; + void cartisianTOspherical(PosType const x[]); void StereographicProjection(const SphericalPoint ¢ral,PosType x[]) const; void StereographicProjection(const SphericalPoint ¢ral,Point_2d &x) const; @@ -117,7 +124,7 @@ namespace Utilities { v[3] = p[2]; } Quaternion(SphericalPoint sp){ - sp.sphericalTOcartisian(v+1); + sp.TOcartisian(v+1); v[0] = 0; } From da68020af501a8190eec905e35d14bd5b7f95ed2 Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Sat, 17 Aug 2019 16:42:07 +0200 Subject: [PATCH 180/227] Bug fix in PixelMap::find_islands() --- ImageProcessing/pixelize.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ImageProcessing/pixelize.cpp b/ImageProcessing/pixelize.cpp index 5e66e758..9c2f3a95 100644 --- a/ImageProcessing/pixelize.cpp +++ b/ImageProcessing/pixelize.cpp @@ -733,7 +733,7 @@ int PixelMap::count_islands(std::vector &pixel_index) const{ *group_boundary = *test; *test = tmp; ++group_boundary; - --test; + //--test; } } ++current; From 13a5ef7ca35c3cc610d398d3d429ad2d349f4daa Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Wed, 28 Aug 2019 13:31:38 +0200 Subject: [PATCH 181/227] Change to comment. --- include/image_info.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/image_info.h b/include/image_info.h index e63d3ad3..3839e292 100644 --- a/include/image_info.h +++ b/include/image_info.h @@ -69,7 +69,7 @@ struct ImageInfo{ unsigned long getNimagePoints(){return imagekist->Nunits();} /// Computes the time delay averaged over the image KappaType aveTimeDelay(); - /// Computes the inverse magnification averaged over the image + /// Computes the inverse magnification averaged over the image, WARNING: This is not always a good measure of the magnification. It is often better to use ImageInfo::area and the orginial size of the source near or do a flux waited average. KappaType aveInvMag(); /// finds the ray in the image that is closest to the point y on the source plane RAY closestRay(const Point_2d &y); From 8fcbd2d8a6f1304083235ec7e5b5a18617fa2e56 Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Tue, 17 Sep 2019 20:56:52 +0200 Subject: [PATCH 182/227] Added Point_3d operator*(double) --- include/point.h | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/include/point.h b/include/point.h index fed3fd36..9d73ea5d 100644 --- a/include/point.h +++ b/include/point.h @@ -526,7 +526,11 @@ struct Point_3d{ PosType operator*(const Point_3d &p){ return x[0]*p.x[0] + x[1]*p.x[1] + x[2]*p.x[2]; } - + + Point_3d operator*(PosType f){ + return Point_3d(x[0]*f,x[1]*f,x[2]*f); + } + /// length PosType length(){ return sqrt(x[0]*x[0] + x[1]*x[1] + x[2]*x[2]); From 60f7cda9fe8262120f9a76749016f8661d0f2f6e Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Mon, 7 Oct 2019 19:03:26 +0200 Subject: [PATCH 183/227] Not much. --- include/image_processing.h | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/include/image_processing.h b/include/image_processing.h index e6a9b45d..4e7fc811 100644 --- a/include/image_processing.h +++ b/include/image_processing.h @@ -137,7 +137,8 @@ class PixelMap void smooth(double sigma); inline double getValue(std::size_t i) const { return map[i]; } - inline double & operator[](std::size_t i) { return map[i]; }; + inline double & operator[](std::size_t i) { return map[i]; }; + const double & operator[](std::size_t i) const { return map[i]; }; inline double operator()(std::size_t i) const { return map[i]; }; inline double operator()(std::size_t i,std::size_t j) const { return map[i + Nx*j]; }; @@ -152,8 +153,10 @@ class PixelMap friend PixelMap operator*(const PixelMap&, const PixelMap&); PixelMap& operator*=(PosType b); - friend PixelMap operator*(const PixelMap&, PosType b); - + friend PixelMap operator*(const PixelMap&, PosType b); + /// eliment wise multiplictioan + PixelMap operator*=(const PixelMap &m) const; + std::valarray& data() { return map; } /// Check whether two PixelMaps agree in their physical dimensions. From 8ea628d4c8cb8caf562a8ee2e428b7ff72a8d433 Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Tue, 8 Oct 2019 17:37:02 +0200 Subject: [PATCH 184/227] Added Lens::rayshooter(RAY &ray) --- FullRange/internal_rayshooter_multi.cpp | 7 +++ TreeCode_link/Tree.cpp | 27 +++++++++- TreeCode_link/TreeDriver.cpp | 8 +-- include/lens.h | 10 +++- include/point.h | 70 ++++++++++++++++++++----- 5 files changed, 104 insertions(+), 18 deletions(-) diff --git a/FullRange/internal_rayshooter_multi.cpp b/FullRange/internal_rayshooter_multi.cpp index 24f39184..860accd1 100644 --- a/FullRange/internal_rayshooter_multi.cpp +++ b/FullRange/internal_rayshooter_multi.cpp @@ -61,6 +61,13 @@ struct TmpParams{ /** \brief This function calculates the deflection, shear, convergence, rotation and time-delay of rays in parallel. */ +void Lens::rayshooter(RAY &ray){ + Point im(ray.x); + Point so; + im.image = &so; + rayshooterInternal(1,&im); + ray = im; +}; void Lens::rayshooterInternal( unsigned long Npoints /// number of points to be shot , Point *i_points /// point on the image plane diff --git a/TreeCode_link/Tree.cpp b/TreeCode_link/Tree.cpp index 1db9091c..965edbcb 100644 --- a/TreeCode_link/Tree.cpp +++ b/TreeCode_link/Tree.cpp @@ -71,8 +71,33 @@ Point::Point():Point_2d(0,0){ gamma[0] = gamma[1] = gamma[2] = 0; invmag = 1; flag = false; -} +}; + +Point::Point(const Point_2d &p):Point_2d(p){ + head = 0; + in_image = NO; + surface_brightness = 0; + leaf = nullptr; + image = nullptr; + next = prev = nullptr; + kappa = dt = gridsize = 0; + gamma[0] = gamma[1] = gamma[2] = 0; + invmag = 1; + flag = false; +}; +Point::Point(PosType x,PosType y):Point_2d(x,y){ + head = 0; + in_image = NO; + surface_brightness = 0; + leaf = nullptr; + image = nullptr; + next = prev = nullptr; + kappa = dt = gridsize = 0; + gamma[0] = gamma[1] = gamma[2] = 0; + invmag = 1; + flag = false; +}; /// print out all member data for testing purposes void Point::Print(){ diff --git a/TreeCode_link/TreeDriver.cpp b/TreeCode_link/TreeDriver.cpp index 05955f5b..d6f8b414 100644 --- a/TreeCode_link/TreeDriver.cpp +++ b/TreeCode_link/TreeDriver.cpp @@ -1474,10 +1474,10 @@ KappaType ImageInfo::aveTimeDelay() return tmp_dt; } -/// Computes the time delay averaged over the image +/// Find the losest ray within the image to the source point y RAY ImageInfo::closestRay(const Point_2d &y){ - Point *p; + RAY p; double r2min = HUGE_VALL; for(Kist::iterator it = imagekist->begin() @@ -1488,12 +1488,12 @@ RAY ImageInfo::closestRay(const Point_2d &y){ + pow( (*it).image->x[1] - y[1],2 ); if(r2min > dy2){ - p = &(*it); + p = *it; r2min = dy2; } } - return RAY(p); + return p; } ImageInfo & ImageInfo::operator+=(ImageInfo & rhs){ diff --git a/include/lens.h b/include/lens.h index a034c483..0c53ecc9 100644 --- a/include/lens.h +++ b/include/lens.h @@ -315,7 +315,15 @@ class Lens{ /// get single main halo of given type template HaloType* getMainHalo(std::size_t i); - + + /**\brief Using to shoot a single ray + + ray.x should be set to the image position. + The kappa,gamma,deflection, time-delay and + source position will be calculated at that + image point. + */ + void rayshooter(RAY &ray); void rayshooterInternal(unsigned long Npoints, Point *i_points, bool RSIverbose = false); void info_rayshooter(Point *i_point ,std::vector & ang_positions diff --git a/include/point.h b/include/point.h index 9d73ea5d..0c4cfb6a 100644 --- a/include/point.h +++ b/include/point.h @@ -173,6 +173,8 @@ struct Branch; struct Point: public Point_2d{ Point(); + Point(const Point_2d &p); + Point(PosType x,PosType y); Point *next; // pointer to next point in linked list Point *prev; Point *image; // pointer to point on image or source plane @@ -223,28 +225,72 @@ struct Point: public Point_2d{ /** \brief Simple representaion of a light path giving position on the image and source planes and lensing quantities. */ struct RAY{ - RAY(Point *p){ - x = p->x; - y = p->image->x; - kappa = p->kappa; - dt = p->dt; + RAY(const Point &p){ + x = p.x; + y = p.image->x; + kappa = p.kappa; + dt = p.dt; - gamma[0] = p->gamma[0]; - gamma[1] = p->gamma[1]; - gamma[2] = p->gamma[2]; - } + gamma[0] = p.gamma[0]; + gamma[1] = p.gamma[1]; + gamma[2] = p.gamma[2]; + }; + RAY(const RAY &p){ + x = p.x; + y = p.y; + kappa = p.kappa; + dt = p.dt; + + gamma[0] = p.gamma[0]; + gamma[1] = p.gamma[1]; + gamma[2] = p.gamma[2]; + }; + + RAY & operator=(const Point &p){ + x = p.x; + y = p.image->x; + kappa = p.kappa; + dt = p.dt; + + gamma[0] = p.gamma[0]; + gamma[1] = p.gamma[1]; + gamma[2] = p.gamma[2]; + + return *this; + }; + + RAY & operator=(const RAY &p){ + x = p.x; + y = p.y; + kappa = p.kappa; + dt = p.dt; + + gamma[0] = p.gamma[0]; + gamma[1] = p.gamma[1]; + gamma[2] = p.gamma[2]; + + return *this; + }; + ~RAY(){}; - // image position + /// image position Point_2d x; - // source position + /// source position Point_2d y; - KappaType kappa,gamma[3],dt; + /// convergence + KappaType kappa; + /// shear + KappaType gamma[3]; + /// time-delay + KappaType dt; + /// inverse of the magnification KappaType invmag(){return (1-kappa)*(1-kappa) - gamma[0]*gamma[0] - gamma[1]*gamma[1] + gamma[2]*gamma[2];} + /// deflection angle Point_2d alpha(){return x - y;} }; From da2573b7cfade3e686d39e54c2cb05b2d60996cd Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Wed, 9 Oct 2019 14:48:53 +0200 Subject: [PATCH 185/227] Needed a default RAY constructor. --- include/point.h | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/include/point.h b/include/point.h index 0c4cfb6a..91f6dedf 100644 --- a/include/point.h +++ b/include/point.h @@ -225,6 +225,11 @@ struct Point: public Point_2d{ /** \brief Simple representaion of a light path giving position on the image and source planes and lensing quantities. */ struct RAY{ + RAY(){ + kappa = dt = 0.0; + gamma[0] = gamma[1] = gamma[2] = 0.0; + }; + RAY(const Point &p){ x = p.x; y = p.image->x; From ca424642867a0f1a4b4c0dc3b7692652672b9988 Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Thu, 24 Oct 2019 17:48:12 +0200 Subject: [PATCH 186/227] Creating CPFITS classes to avoid CCFits once and for all. --- include/CMakeLists.txt | 1 + include/cpfits.h | 273 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 274 insertions(+) create mode 100644 include/cpfits.h diff --git a/include/CMakeLists.txt b/include/CMakeLists.txt index 1a08ddd1..26ee4d65 100644 --- a/include/CMakeLists.txt +++ b/include/CMakeLists.txt @@ -52,4 +52,5 @@ add_sources( gadget.hh particle_types.h disk.h + cpfits.h ) diff --git a/include/cpfits.h b/include/cpfits.h new file mode 100644 index 00000000..d25d0ed8 --- /dev/null +++ b/include/cpfits.h @@ -0,0 +1,273 @@ +// +// cpfits.h +// GLAMER +// +// Created by Robert Benton Metcalf on 24/10/2019. +// +// This is a class that is meant to replace the CCFITS library +// so that it can be eliminated as a dependancy. +// +// These classes are for reading from and writing to fits files. +// Only +// +// for more infermation of cfitio routines see : +// https://heasarc.gsfc.nasa.gov/docs/software/fitsio/quick/node9.html + +#ifndef cpfits_h +#define cpfits_h + +#include +#include + #include "fitsio.h" + +/// types of fits tables +enum HDUtype {IMAGE_HDU, ASCII_TBL, BINARY_TBL}; + +class COFITS_BASE{ + +protected: + CPFITS_BASE()::status(0){} + + fitsfile *fptr; + int status; +public: + + ///////////////////////////////// + // HDU-level Routines + ///////////////////////////////// + + /// returns the number of tabels + int get_num_hdus(){ + int hdunum; + return fits_get_num_hdus(fptr, &hdunum, &status); + } + /// returns the current table number, 1... + int get_current_hdu_num(){ + int hdunum; + return fits_get_hdu_num(fptr,&hdunum); + } + + /// change the current table number + HDUtype change_hdu(int i){ + int hdunum = get_num_hdus(); + if(i > hdum || i < 1){ + throw std::invalid + } + HDUtype hdutype; + fits_movabs_hdu(fptr,i,&hdutype,&status); + + return hdutype; + } + + HDUtype get_current_hdu_type(){ + HDUtype hdutype; + return fits_get_hdu_type(fptr,&hdutype,&status) + } + + ///////////////////////////////// + // image information of current HDU + ///////////////////////////////// + + /** \brief gives the data type and dimensions of the current table + BYTE_IMG = 8 ( 8-bit byte pixels, 0 - 255) + SHORT_IMG = 16 (16 bit integer pixels) + LONG_IMG = 32 (32-bit integer pixels) + LONGLONG_IMG = 64 (64-bit integer pixels) + FLOAT_IMG = -32 (32-bit floating point pixels) + DOUBLE_IMG = -64 (64-bit floating point pixels) + */ + void imageInfo( + ,int &bitpix /// type of data + ,std::vector &size + ){ + int ndim /// number of dimensions (keyword NAXIS) + size.resize(10); + fits_get_img_param(fptr,10,&bitpix,&ndim,size.data(),&status) + size.resize(ndim); + } + + /// number of keywords for current table, returns != 0 if keyname is not found + int nkyewords(){ + int keysexist; + return fits_get_hdrspace(fptr,&keysexist,NULL,&status); + } + int readKey(std::string &keyname,double &value){ + return fits_read_key(fptr,TDOUBLE,keyname.c_string, + &value,NULL,&status); + } + int readKey(std::string &keyname,float &value){ + return fits_read_key(fptr,TFLOAT,keyname.c_string, + &value,NULL,&status); + } + int readKey(std::string &keyname,int &value){ + return fits_read_key(fptr,TINT,keyname.c_string, + &value,NULL,&status); + } + int readKey(std::string &keyname,size_t &value){ + return fits_read_key(fptr,TULONG,keyname.c_string, + &value,NULL,&status); + } + +} + +/// read only fits file interface +class CPFITS_READ : public CPFITS_BASE { +private: + // make it uncopyable + CPFITS_READ(CPFITS_READ &); + CPFITS_READ operator=(CPFITS_READ ); + +public: + CPFITS_READ(std::string filename,bool verboe = false) { + fits_open_file(&fptr,filename.c, READONLY, &status); + + // print any error messages + if (status) fits_report_error(stderr, status); + + if(verbose){ + std::cout << "Opening file : " << filename << endl; + std::cout << " status : " << status << endl; + } + } + + ~CFITS_READ(){ + fits_close_file(fptr, &status); + if (status) fits_report_error(stderr, status); + } + + /// close old and reopen a new file + void reset(std::string filename){ + fits_close_file(fptr, &status); + assert(status==0); + fits_open_file(&fptr,filename.c, READONLY, &status); + // print any error messages + if (status) fits_report_error(stderr, status); + } + + /// read the whole image into a vector + int read(std::vector &output){ + assert(start.size()==2); + + std::vector size; + int dtype; + imageInfo(dtype,size); + long nelements=1; + for(long n : size) nelements *= n; + std::vector start(nelements,1); + output.resize(nelements); + + return fits_read_pix(fptr,TDOUBLE,start.data(),nelements + ,NULL,output.data(),NULL, &status); + } + /// read the whole image into a vector + int read(std::vector &output){ + assert(start.size()==2); + + std::vector size; + int dtype; + imageInfo(dtype,size); + long nelements=1; + for(long n : size) nelements *= n; + std::vector start(nelements,1); + output.resize(nelements); + + return fits_read_pix(fptr,TFLOAT,start.data(),nelements + ,NULL,output.data(),NULL, &status); + } + + /// read nelements in order from image to output array + int read_block(double *output,long nelements,long * start){ + return fits_read_pix(fptr,TDOUBLE,start,nelements + ,NULL,output,NULL, &status); + } + int read_block(float *output,long nelements,long * start){ + return fits_read_pix(fptr,TFLOAT,start,nelements + ,NULL,output,NULL, &status); + } + + + /// read a rectangular subset of the image + int read_subset(double *output,long *lowerleft,long *uppertright){ + long inc[2] = {1,1}; // this is a step + return fits_read_subset(fptr,TDOUBLE,lowerleft,upperright,&inc + ,NULL,output,NULL,&status); + } + /// read a rectangular subset of the image + int read_subset(float *output,long *lowerleft,long *uppertright){ + long inc[2] = {1,1}; // this is a step + return fits_read_subset(fptr,TFLOAT,lowerleft,upperright,&inc + ,NULL,output,NULL,&status); + } + +} + +class CPFITS_WRITE : public CPFITS_BASE { + +private: + // make it uncopyable + CPFITS_WRITE(CPFITS_READ &); + CPFITS_WRITE operator=(CPFITS_WRITE ); + +public: + CPFITS_WRITE(std::string filename,bool verboe = false) { + fits_create_file(&fptr, filename.c_string(), &status); + + + // print any error messages + if (status) fits_report_error(stderr, status); + + if(verbose){ + std::cout << "Creating file : " << filename << endl; + std::cout << " status : " << status << endl; + } + } + + ~CFITS_WRITE(){ + fits_close_file(fptr, &status); + if (status) fits_report_error(stderr, status); + } + + /// add a new image or cube to the file + int write_image(std::vector &im,std::vector &size){ + + assert(im.size() == size[0]*siz[1]); + fits_create_img(fptr,DOUBLE_IMG,size.size(), + size.data(), &status); + std::vector fpixel(size.sizs(),1); + return fits_write_pix(fptr,TDOUBLE,fpixel.data(), + im.size(),im.data(),&status); + } + int writeImage(std::vector &im,std::vector &size){ + assert(im.size() == size[0]*siz[1]); + fits_create_img(fptr,FLOAT_IMG,size.size(), + size.data(), &status); + std::vector fpixel(size.sizs(),1); + return fits_write_pix(fptr,TFLOAT,fpixel.data(), + im.size(),im.data(),&status); + } + + /// add or replace a key value in the header + int writeKey(std::string &key,double value,std::string &comment ){ + return fits_update_key(fptr,TDOUBLE,key.c_string(), + &value,comment.c_string(),&status); + } + int writeKey(std::string &key,float value,std::string &comment ){ + return fits_update_key(fptr,TFLOAT,key.c_string(), + &value,comment.c_string(),&status); + } + int writeKey(std::string &key,int value,std::string &comment ){ + return fits_update_key(fptr,TINT,key.c_string(), + &value,comment.c_string(),&status); + } + int writeKey(std::string &key,long value,std::string &comment ){ + return fits_update_key(fptr,TLONG,key.c_string(), + &value,comment.c_string(),&status); + } + int writeKey(std::string &key,size_t value,std::string &comment ){ + return fits_update_key(fptr,TULONG,key.c_string(), + &value,comment.c_string(),&status); + } + +} + +#endif /* cpfits_h */ From e002a404ba30442f1d42f67a43095df281018c28 Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Thu, 24 Oct 2019 18:35:25 +0200 Subject: [PATCH 187/227] Fixed compilation problems. --- include/cpfits.h | 111 ++++++++++++++++++++++++----------------------- 1 file changed, 56 insertions(+), 55 deletions(-) diff --git a/include/cpfits.h b/include/cpfits.h index d25d0ed8..ab2d7bb6 100644 --- a/include/cpfits.h +++ b/include/cpfits.h @@ -18,15 +18,12 @@ #include #include - #include "fitsio.h" +#include "fitsio.h" -/// types of fits tables -enum HDUtype {IMAGE_HDU, ASCII_TBL, BINARY_TBL}; - -class COFITS_BASE{ +class CPFITS_BASE{ protected: - CPFITS_BASE()::status(0){} + CPFITS_BASE():status(0){} fitsfile *fptr; int status; @@ -48,20 +45,20 @@ class COFITS_BASE{ } /// change the current table number - HDUtype change_hdu(int i){ + int change_hdu(int i){ int hdunum = get_num_hdus(); - if(i > hdum || i < 1){ - throw std::invalid + if(i > hdunum || i < 1){ + throw std::invalid_argument("out of range"); } - HDUtype hdutype; + int hdutype; fits_movabs_hdu(fptr,i,&hdutype,&status); return hdutype; } - HDUtype get_current_hdu_type(){ - HDUtype hdutype; - return fits_get_hdu_type(fptr,&hdutype,&status) + int get_current_hdu_type(){ + int hdutype; + return fits_get_hdu_type(fptr,&hdutype,&status); } ///////////////////////////////// @@ -77,12 +74,12 @@ class COFITS_BASE{ DOUBLE_IMG = -64 (64-bit floating point pixels) */ void imageInfo( - ,int &bitpix /// type of data + int &bitpix /// type of data ,std::vector &size ){ - int ndim /// number of dimensions (keyword NAXIS) + int ndim; /// number of dimensions (keyword NAXIS) size.resize(10); - fits_get_img_param(fptr,10,&bitpix,&ndim,size.data(),&status) + fits_get_img_param(fptr,10,&bitpix,&ndim,size.data(),&status); size.resize(ndim); } @@ -92,23 +89,23 @@ class COFITS_BASE{ return fits_get_hdrspace(fptr,&keysexist,NULL,&status); } int readKey(std::string &keyname,double &value){ - return fits_read_key(fptr,TDOUBLE,keyname.c_string, + return fits_read_key(fptr,TDOUBLE,keyname.c_str(), &value,NULL,&status); } int readKey(std::string &keyname,float &value){ - return fits_read_key(fptr,TFLOAT,keyname.c_string, + return fits_read_key(fptr,TFLOAT,keyname.c_str(), &value,NULL,&status); } int readKey(std::string &keyname,int &value){ - return fits_read_key(fptr,TINT,keyname.c_string, + return fits_read_key(fptr,TINT,keyname.c_str(), &value,NULL,&status); } int readKey(std::string &keyname,size_t &value){ - return fits_read_key(fptr,TULONG,keyname.c_string, + return fits_read_key(fptr,TULONG,keyname.c_str(), &value,NULL,&status); } -} +}; /// read only fits file interface class CPFITS_READ : public CPFITS_BASE { @@ -118,19 +115,19 @@ class CPFITS_READ : public CPFITS_BASE { CPFITS_READ operator=(CPFITS_READ ); public: - CPFITS_READ(std::string filename,bool verboe = false) { - fits_open_file(&fptr,filename.c, READONLY, &status); + CPFITS_READ(std::string filename,bool verbose = false) { + fits_open_file(&fptr,filename.c_str(), READONLY, &status); // print any error messages if (status) fits_report_error(stderr, status); if(verbose){ - std::cout << "Opening file : " << filename << endl; - std::cout << " status : " << status << endl; + std::cout << "Opening file : " << filename << std::endl; + std::cout << " status : " << status << std::endl; } } - ~CFITS_READ(){ + ~CPFITS_READ(){ fits_close_file(fptr, &status); if (status) fits_report_error(stderr, status); } @@ -139,14 +136,13 @@ class CPFITS_READ : public CPFITS_BASE { void reset(std::string filename){ fits_close_file(fptr, &status); assert(status==0); - fits_open_file(&fptr,filename.c, READONLY, &status); + fits_open_file(&fptr,filename.c_str(), READONLY, &status); // print any error messages if (status) fits_report_error(stderr, status); } /// read the whole image into a vector int read(std::vector &output){ - assert(start.size()==2); std::vector size; int dtype; @@ -161,7 +157,6 @@ class CPFITS_READ : public CPFITS_BASE { } /// read the whole image into a vector int read(std::vector &output){ - assert(start.size()==2); std::vector size; int dtype; @@ -187,19 +182,22 @@ class CPFITS_READ : public CPFITS_BASE { /// read a rectangular subset of the image - int read_subset(double *output,long *lowerleft,long *uppertright){ + int read_subset(double *output,long *lowerleft,long *upperright){ long inc[2] = {1,1}; // this is a step - return fits_read_subset(fptr,TDOUBLE,lowerleft,upperright,&inc + return fits_read_subset(fptr,TDOUBLE,lowerleft,upperright,inc ,NULL,output,NULL,&status); + //int fits_read_subset(fitsfile *fptr, int datatype, long *fpixel, + // long *lpixel, long *inc, void *nulval, void *array, + // int *anynul, int *status) } /// read a rectangular subset of the image - int read_subset(float *output,long *lowerleft,long *uppertright){ + int read_subset(float *output,long *lowerleft,long *upperright){ long inc[2] = {1,1}; // this is a step - return fits_read_subset(fptr,TFLOAT,lowerleft,upperright,&inc + return fits_read_subset(fptr,TFLOAT,lowerleft,upperright,inc ,NULL,output,NULL,&status); } -} +}; class CPFITS_WRITE : public CPFITS_BASE { @@ -209,65 +207,68 @@ class CPFITS_WRITE : public CPFITS_BASE { CPFITS_WRITE operator=(CPFITS_WRITE ); public: - CPFITS_WRITE(std::string filename,bool verboe = false) { - fits_create_file(&fptr, filename.c_string(), &status); + CPFITS_WRITE(std::string filename,bool verbose = false) { + fits_create_file(&fptr, filename.c_str(), &status); // print any error messages if (status) fits_report_error(stderr, status); if(verbose){ - std::cout << "Creating file : " << filename << endl; - std::cout << " status : " << status << endl; + std::cout << "Creating file : " << filename << std::endl; + std::cout << " status : " << status << std::endl; } } - ~CFITS_WRITE(){ + ~CPFITS_WRITE(){ fits_close_file(fptr, &status); if (status) fits_report_error(stderr, status); } /// add a new image or cube to the file int write_image(std::vector &im,std::vector &size){ - - assert(im.size() == size[0]*siz[1]); + size_t n=1; + for(size_t i : size) n *= i; + assert(im.size() == n); fits_create_img(fptr,DOUBLE_IMG,size.size(), size.data(), &status); - std::vector fpixel(size.sizs(),1); + std::vector fpixel(size.size(),1); return fits_write_pix(fptr,TDOUBLE,fpixel.data(), im.size(),im.data(),&status); } int writeImage(std::vector &im,std::vector &size){ - assert(im.size() == size[0]*siz[1]); + size_t n=1; + for(size_t i : size) n *= i; + assert(im.size() == n); fits_create_img(fptr,FLOAT_IMG,size.size(), size.data(), &status); - std::vector fpixel(size.sizs(),1); + std::vector fpixel(size.size(),1); return fits_write_pix(fptr,TFLOAT,fpixel.data(), im.size(),im.data(),&status); } /// add or replace a key value in the header int writeKey(std::string &key,double value,std::string &comment ){ - return fits_update_key(fptr,TDOUBLE,key.c_string(), - &value,comment.c_string(),&status); + return fits_update_key(fptr,TDOUBLE,key.c_str(), + &value,comment.c_str(),&status); } int writeKey(std::string &key,float value,std::string &comment ){ - return fits_update_key(fptr,TFLOAT,key.c_string(), - &value,comment.c_string(),&status); + return fits_update_key(fptr,TFLOAT,key.c_str(), + &value,comment.c_str(),&status); } int writeKey(std::string &key,int value,std::string &comment ){ - return fits_update_key(fptr,TINT,key.c_string(), - &value,comment.c_string(),&status); + return fits_update_key(fptr,TINT,key.c_str(), + &value,comment.c_str(),&status); } int writeKey(std::string &key,long value,std::string &comment ){ - return fits_update_key(fptr,TLONG,key.c_string(), - &value,comment.c_string(),&status); + return fits_update_key(fptr,TLONG,key.c_str(), + &value,comment.c_str(),&status); } int writeKey(std::string &key,size_t value,std::string &comment ){ - return fits_update_key(fptr,TULONG,key.c_string(), - &value,comment.c_string(),&status); + return fits_update_key(fptr,TULONG,key.c_str(), + &value,comment.c_str(),&status); } -} +}; #endif /* cpfits_h */ From c5fba1a819da72f7c6645c6c9df2613a28d51df6 Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Thu, 24 Oct 2019 23:06:10 +0200 Subject: [PATCH 188/227] Moved mulitmap things over to CPFITS_READ instead of CCFits --- MultiPlane/multimap.cpp | 77 ++++++++++++++++++++++++----------------- include/multimap.h | 40 +++++++++++---------- 2 files changed, 68 insertions(+), 49 deletions(-) diff --git a/MultiPlane/multimap.cpp b/MultiPlane/multimap.cpp index ce291bc1..68ce4891 100644 --- a/MultiPlane/multimap.cpp +++ b/MultiPlane/multimap.cpp @@ -16,14 +16,18 @@ LensHaloMultiMap::LensHaloMultiMap( ,COSMOLOGY &c ,bool subtract_ave ,bool single_grid_mode - ):LensHalo(redshift,c),single_grid(single_grid_mode),cosmo(c),mass_unit(mass_unit),fitsfilename(fitsfile) + ): +LensHalo(redshift,c),single_grid(single_grid_mode),cosmo(c),cpfits(fitsfile) +,mass_unit(mass_unit),fitsfilename(fitsfile) { - try{ +/* try{ ff = new CCfits::FITS (fitsfilename, CCfits::Read); }catch(...){ std::cerr << "CCfits throw an exception: probably file " << fitsfile << " does not exist." << std::endl; } + */ + zerosize = 1; rscale = 1.0; @@ -72,8 +76,9 @@ LensHaloMultiMap::LensHaloMultiMap( std::vector first = {1,1}; std::vector last = {(long)Noriginal[0],(long)Noriginal[1]}; - long_range_map.read_sub(ff,first,last,getDist()); - + //long_range_map.read_sub(ff,first,last,getDist()); + long_range_map.read_sub(cpfits,first,last,getDist()); + //double res = submap.boxlMpc/submap.nx; //Point_2d dmap = {submap.boxlMpc,submap.ny*resolution}; //dmap /= 2; @@ -154,8 +159,8 @@ LensHaloMultiMap::LensHaloMultiMap( if( j%chunk == 0 ){ first[1] = j+1; last[1] = MIN(j + chunk,Noriginal[1]); - //submap.read_sub(fitsfilename,first,last,cosmo.gethubble()); - submap.read_sub(ff,first,last,getDist()); + //submap.read_sub(ff,first,last,getDist()); + submap.read_sub(cpfits,first,last,getDist()); kj = 0; }else{ @@ -274,7 +279,8 @@ void LensHaloMultiMap::submap( if( (first[0] > 0)*(first[1] > 0)*(last[0] <= Noriginal[0])*(last[1] <= Noriginal[1]) ){ //map.read_sub(fitsfilename,first,last,getDist()); - map.read_sub(ff,first,last,getDist()); + //map.read_sub(ff,first,last,getDist()); + map.read_sub(cpfits,first,last,getDist()); }else{ @@ -305,9 +311,9 @@ void LensHaloMultiMap::submap( if(jj >= Noriginal[1] ) jj -= Noriginal[1]; first_sub[1] = jj + 1; last_sub[1] = MIN(jj + chunk + 1,Noriginal[1]); - //partmap.read_sub(fitsfilename,first_sub,last_sub,cosmo.gethubble()); - partmap.read_sub(ff,first_sub,last_sub,getDist()); - + //partmap.read_sub(ff,first_sub,last_sub,getDist()); + partmap.read_sub(cpfits,first_sub,last_sub,getDist()); + k = 0; }else{ ++k; @@ -760,44 +766,53 @@ void LensMap::read_sub(std::string fits_input_file } */ -void LensMap::read_sub(CCfits::FITS *ff - ,const std::vector &first - ,const std::vector &last +//void LensMap::read_sub(CCfits::FITS *ff +void LensMap::read_sub(CPFITS_READ &cpfits + ,std::vector &first + ,std::vector &last ,double angDist ){ - CCfits::PHDU &h0 = ff->pHDU(); + //CCfits::PHDU &h0 = ff->pHDU(); + + int bitpix; + std::vector sizes; + cpfits.imageInfo(bitpix,sizes); - long nx_orig = h0.axis(0); - long ny_orig = h0.axis(1); - - h0.readAllKeys(); + //long nx_orig = h0.axis(0); + //long ny_orig = h0.axis(1); + + //h0.readAllKeys(); // these are always present in each //float wlow,wup,res; float res; - h0.readKey ("CD1_1",res); // resolution in - //h0.readKey ("REDSHIFT",z); - //h0.readKey ("WLOW",wlow); - //h0.readKey ("WUP",wup); + cpfits.readKey("CD1_1",res); + //h0.readKey ("CD1_1",res); // resolution in - //double D = 3*(pow(wup,4) - pow(wlow,4))/(pow(wup,3) - pow(wlow,3))/4; - //if(wlow == wup) D = wup; - //D /= (1+z)*h; res *= degreesTOradians * angDist; - double boxlMpc_orig = res * nx_orig; - - assert(h0.axes() >= 2); + //double boxlMpc_orig = res * nx_orig; + double boxlMpc_orig = res * sizes[0]; + + assert(sizes.size() >= 2); std::vector stride = {1,1}; // principal HDU is read //std::cout << surface_density.size() << std::endl; - h0.read(surface_density,first,last,stride); + //h0.read(surface_density,first,last,stride); + cpfits.read_subset(&(surface_density[0]) + ,first.data(),last.data()); + + float tmp; + cpfits.read_subset(&tmp + ,first.data(),last.data()); + //std::cout << surface_density.size() << std::endl; // set the full field lower left - lowerleft = upperright = {-boxlMpc_orig/2,-res*ny_orig/2}; - + //lowerleft = upperright = {-boxlMpc_orig/2,-res*ny_orig/2}; + lowerleft = upperright = {-boxlMpc_orig/2,-res*sizes[1]/2}; + lowerleft[0] += (first[0]-1)*res ; lowerleft[1] += (first[1]-1)*res ; diff --git a/include/multimap.h b/include/multimap.h index f86e8b26..1304a6ae 100644 --- a/include/multimap.h +++ b/include/multimap.h @@ -12,12 +12,13 @@ #include "InputParams.h" #include "lens_halos.h" #include "grid_maintenance.h" +#include "cpfits.h" #include -#ifdef ENABLE_FITS -#include -#endif +//#ifdef ENABLE_FITS +//#include +//#endif /** * \brief The MOKA map structure, containing all quantities that define it @@ -82,10 +83,10 @@ struct LensMap{ //void read_header(std::unique_ptr ff,float h); - - void read_sub(CCfits::FITS *ff - ,const std::vector &first - ,const std::vector &last + //void read_sub(CCfits::FITS *ff + void read_sub(CPFITS_READ &cpfits + ,std::vector &first + ,std::vector &last ,double Dist ); @@ -117,16 +118,15 @@ class LensHaloMultiMap : public LensHalo public: LensHaloMultiMap( - std::string fitsfile /// Original fits map of the density + std::string fitsfile /// Original fits map of the density ,double redshift - ,double mass_unit /// should include h factors + ,double mass_unit /// should include h factors ,COSMOLOGY &c - ,bool subtract_ave = true /// subtract the average of the full field + ,bool subtract_ave = true /// subtract the average of the full field ,bool single_grid_mode = false ); ~LensHaloMultiMap(){ - delete ff; }; const double ffactor = 5,gfactor = 5; @@ -192,8 +192,8 @@ class LensHaloMultiMap : public LensHalo long_range_map = std::move(m.long_range_map); short_range_map = std::move(m.short_range_map); single_grid = m.single_grid; - ff = m.ff; - m.ff = nullptr; + cpfits = std::move(m.cpfits); + //m.cpfits = nullptr; max_pix = m.max_pix; min_pix = m.min_pix; mass_unit = m.mass_unit; @@ -208,13 +208,16 @@ class LensHaloMultiMap : public LensHalo wsr = m.wsr; wlr = m.wlr; } - LensHaloMultiMap(LensHaloMultiMap &&m):LensHalo(std::move(m)) - ,cosmo(m.cosmo){ + + LensHaloMultiMap(LensHaloMultiMap &&m): + LensHalo(std::move(m)),cosmo(m.cosmo),cpfits(std::move(m.cpfits)) + { long_range_map = std::move(m.long_range_map); short_range_map = std::move(m.short_range_map); single_grid = m.single_grid; - ff = m.ff; - m.ff = nullptr; + //cpfits = std::move(m.cpfits); + //cpfits = m.cpfits; + //m.cpfits = nullptr; max_pix = m.max_pix; min_pix = m.min_pix; mass_unit = m.mass_unit; @@ -238,7 +241,8 @@ class LensHaloMultiMap : public LensHalo bool single_grid; COSMOLOGY &cosmo; - CCfits::FITS *ff; + //CCfits::FITS *ff; + CPFITS_READ cpfits; double max_pix = std::numeric_limits::lowest(); double min_pix = std::numeric_limits::max(); From 95bb649733134f030e8e8430bde232b6dd35f475 Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Thu, 24 Oct 2019 23:14:11 +0200 Subject: [PATCH 189/227] Made some fixes to CPFITS --- include/cpfits.h | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/include/cpfits.h b/include/cpfits.h index ab2d7bb6..bebf3400 100644 --- a/include/cpfits.h +++ b/include/cpfits.h @@ -24,6 +24,18 @@ class CPFITS_BASE{ protected: CPFITS_BASE():status(0){} + + CPFITS_BASE(CPFITS_BASE &&ff){ + fptr = ff.fptr; + ff.fptr = nullptr; + status = ff.status; + } + + void operator=(CPFITS_BASE &&ff){ + fptr = ff.fptr; + ff.fptr = nullptr; + status = ff.status; + } fitsfile *fptr; int status; @@ -88,19 +100,19 @@ class CPFITS_BASE{ int keysexist; return fits_get_hdrspace(fptr,&keysexist,NULL,&status); } - int readKey(std::string &keyname,double &value){ + int readKey(std::string keyname,double &value){ return fits_read_key(fptr,TDOUBLE,keyname.c_str(), &value,NULL,&status); } - int readKey(std::string &keyname,float &value){ + int readKey(std::string keyname,float &value){ return fits_read_key(fptr,TFLOAT,keyname.c_str(), &value,NULL,&status); } - int readKey(std::string &keyname,int &value){ + int readKey(std::string keyname,int &value){ return fits_read_key(fptr,TINT,keyname.c_str(), &value,NULL,&status); } - int readKey(std::string &keyname,size_t &value){ + int readKey(std::string keyname,size_t &value){ return fits_read_key(fptr,TULONG,keyname.c_str(), &value,NULL,&status); } @@ -111,8 +123,8 @@ class CPFITS_BASE{ class CPFITS_READ : public CPFITS_BASE { private: // make it uncopyable - CPFITS_READ(CPFITS_READ &); - CPFITS_READ operator=(CPFITS_READ ); + //CPFITS_READ(CPFITS_READ &); + //CPFITS_READ operator=(CPFITS_READ ); public: CPFITS_READ(std::string filename,bool verbose = false) { @@ -132,6 +144,12 @@ class CPFITS_READ : public CPFITS_BASE { if (status) fits_report_error(stderr, status); } + CPFITS_READ(CPFITS_READ &&ff):CPFITS_BASE(std::move(ff)){}; + + void operator=(CPFITS_READ &&ff){ + CPFITS_BASE::operator=(std::move(ff)); + } + /// close old and reopen a new file void reset(std::string filename){ fits_close_file(fptr, &status); From b9915846661b52453b14bb76a64e70b7dfc4b1ef Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Fri, 25 Oct 2019 15:09:47 +0200 Subject: [PATCH 190/227] Making this compatible with CPFITS --- MultiPlane/multimap.cpp | 52 +++++++++++++++++++++++++---------------- 1 file changed, 32 insertions(+), 20 deletions(-) diff --git a/MultiPlane/multimap.cpp b/MultiPlane/multimap.cpp index 68ce4891..9925a80e 100644 --- a/MultiPlane/multimap.cpp +++ b/MultiPlane/multimap.cpp @@ -5,6 +5,7 @@ #include "slsimlib.h" #include "multimap.h" +#include using namespace std; using namespace CCfits; @@ -523,32 +524,43 @@ LensMap& LensMap::operator=(LensMap &&m){ void LensMap::read_header(std::string fits_input_file ,double angDist){ - std::auto_ptr ff(new CCfits::FITS (fits_input_file, CCfits::Read)); - - //CCfits::PHDU *h0=&ff->pHDU(); - CCfits::PHDU &h0 = ff->pHDU(); + CPFITS_READ cpfits(fits_input_file); +// std::auto_ptr ff(new CCfits::FITS (fits_input_file, CCfits::Read)); +// CCfits::PHDU &h0 = ff->pHDU(); - h0.readAllKeys(); +// h0.readAllKeys(); - assert(h0.axes() >= 2); + std::vector size; + int bitpix; + cpfits.imageInfo(bitpix,size); - nx = h0.axis(0); - ny = h0.axis(1); + assert(size.size() >=2); + //assert(h0.axes() >= 2); + //nx = h0.axis(0); + //ny = h0.axis(1); + + nx = size[0]; + ny = size[1]; + double phys_res; - try{ - /* these are always present in ea*/ - //float wlow,wup, - h0.readKey ("CD1_1",phys_res); // resolution in degrees - //h0.readKey ("REDSHIFT",z); - //h0.readKey ("WLOW",wlow); - //h0.readKey ("WUP",wup); + if(cpfits.readKey("CD_1",phys_res)){ + std::cerr << "LensMap fits map must have header keywords:" << std::endl + << " CD1_1 - length on other side in Mpc/h" << std::endl; + //<< " WLOW - closest radial distance in cMpc/h" << std::endl + //<< " WUP - furthest radial distance in cMpc/h" << std::endl; - //double D = 3*(pow(wup,4) - pow(wlow,4))/(pow(wup,3) - pow(wlow,3))/4; - //if(wlow == wup) D = wlow; - //D /= (1+z)*h; - //D /= (1+z); + exit(1); + } + phys_res *= degreesTOradians*angDist; + boxlMpc = phys_res*nx; + + /* + + try{ + h0.readKey ("CD1_1",phys_res); // resolution in degrees + phys_res *= degreesTOradians*angDist; boxlMpc = phys_res*nx; } @@ -561,7 +573,7 @@ void LensMap::read_header(std::string fits_input_file exit(1); } - +*/ //double phys_res = boxlMpc/nx; center *= 0; From 30fe7c2404245360faf080e8c4cfcd1efec2a2af Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Fri, 25 Oct 2019 15:13:36 +0200 Subject: [PATCH 191/227] Improvements to CPFITS. --- AnalyticNSIE/source.cpp | 67 ++++++++++++++++++++----------- ImageProcessing/observation.cpp | 68 +++++++++++++++++--------------- include/cpfits.h | 70 +++++++++++++++++++++++++++------ 3 files changed, 140 insertions(+), 65 deletions(-) diff --git a/AnalyticNSIE/source.cpp b/AnalyticNSIE/source.cpp index b4f98dc1..1984d4a1 100644 --- a/AnalyticNSIE/source.cpp +++ b/AnalyticNSIE/source.cpp @@ -7,11 +7,11 @@ #include "slsimlib.h" #include - -#ifdef ENABLE_FITS -#include +//#ifdef ENABLE_FITS +//#include //#include -#endif +//#endif +#include "cpfits.h" using namespace std; @@ -553,12 +553,12 @@ SourceShapelets::SourceShapelets( setTheta(0, 0); ang = my_ang; -#ifdef ENABLE_FITS if(shap_file.empty()) throw std::invalid_argument("Please enter a valid filename for the FITS file input"); - //std::auto_ptr fp(new CCfits::FITS(shap_file.c_str(), CCfits::Read)); - + CPFITS_READ cpfits(shap_file.c_str()); + + /* std::auto_ptr fp(0); try { @@ -578,12 +578,16 @@ SourceShapelets::SourceShapelets( h0.readKey("ID", id); n2 = n1; h0.read(coeff); + /***/ -#else - std::cerr << "Please enable the preprocessor flag ENABLE_FITS !" << std::endl; - exit(1); -#endif - + cpfits.readKey("BETA", source_r); + source_r *= 0.03/180./60./60.*PI; + cpfits.readKey("DIM", n1); + cpfits.readKey("ID", id); + n2 = n1; + vector size; + cpfits.read(coeff,size); + flux = pow(10,-0.4*(mag+48.6))*inv_hplanck; assert(flux > 0); @@ -605,12 +609,15 @@ SourceShapelets::SourceShapelets( setTheta(0, 0); ang = my_ang; -#ifdef ENABLE_FITS + if(shap_file.empty()) throw std::invalid_argument("Please enter a valid filename for the FITS file input"); - + + CPFITS_READ cpfits(shap_file.c_str()); + //std::auto_ptr fp(new CCfits::FITS(shap_file.c_str(), CCfits::Read)); + /* std::auto_ptr fp(0); try { @@ -638,20 +645,36 @@ SourceShapelets::SourceShapelets( h0.readKey("MAG_g_KIDS",mag_map[KiDS_G]); // g band obtained from SED fitting h0.readKey("MAG_r_KIDS",mag_map[KiDS_R]); // r band obtained from SED fitting h0.readKey("MAG_i_KIDS",mag_map[KiDS_I]); // i band obtained from SED fitting +*/ + + cpfits.readKey("BETA", source_r); + source_r *= 0.03/180./60./60.*PI; + cpfits.readKey("SED_TYPE",sed_type); + + cpfits.readKey("MAG_B",mag_map[F435W]); // ACS F435W band magnitude + cpfits.readKey("MAG_V",mag_map[F606W]); // ACS F606W band magnitude + cpfits.readKey("MAG_I",mag_map[F775W]); // ACS F775W band magnitude + cpfits.readKey("MAG_Z",mag_map[F850LP]);// ACS F850LP band magnitude + cpfits.readKey("MAG_J",mag_map[F110W]); // NIC3 F110W band magnitude + cpfits.readKey("MAG_H",mag_map[F160W]); // NIC3 F160W band magnitude + cpfits.readKey("MAG_u_KIDS",mag_map[KiDS_U]); // u band obtained from SED fitting + cpfits.readKey("MAG_g_KIDS",mag_map[KiDS_G]); // g band obtained from SED fitting + cpfits.readKey("MAG_r_KIDS",mag_map[KiDS_R]); // r band obtained from SED fitting + cpfits.readKey("MAG_i_KIDS",mag_map[KiDS_I]); // i band obtained from SED fitting + // by default, the magnitude is the one in the i band, // whose image has been used for shapelets decomposition setActiveBand(KiDS_I); - h0.readKey("REDSHIFT", zsource); - h0.readKey("ID", id); - h0.readKey("DIM", n1); + cpfits.readKey("REDSHIFT", zsource); + cpfits.readKey("ID", id); + cpfits.readKey("DIM", n1); + n2 = n1; - h0.read(coeff); -#else - std::cerr << "Please enable the preprocessor flag ENABLE_FITS !" << std::endl; - exit(1); -#endif + std::vector size; + cpfits.read(coeff,size); + // ??? kluge mag_map[EUC_VIS] = mag_map.at(KiDS_I); diff --git a/ImageProcessing/observation.cpp b/ImageProcessing/observation.cpp index 3c2a3a2f..f1b7982d 100644 --- a/ImageProcessing/observation.cpp +++ b/ImageProcessing/observation.cpp @@ -8,10 +8,11 @@ #include #include "slsimlib.h" -#ifdef ENABLE_FITS -#include -//#include -#endif +//#ifdef ENABLE_FITS +//#include +//#endif + +#include "cpfits.h" #ifdef ENABLE_FFTW #include "fftw3.h" @@ -196,8 +197,7 @@ Npix_x(Npix_x),Npix_y(Npix_y) * \param seeing FWHM in arcsecs of the image */ Observation::Observation(float diameter, float transmission, float exp_time, int exp_num, float back_mag, float ron, size_t Npix_x,size_t Npix_y,float seeing): -diameter(diameter), transmission(transmission), exp_time(exp_time), exp_num(exp_num), back_mag(back_mag), ron(ron) -,Npix_x(Npix_x),Npix_y(Npix_y),seeing(seeing) +Npix_x(Npix_x),Npix_y(Npix_y),diameter(diameter),transmission(transmission),exp_time(exp_time), exp_num(exp_num), back_mag(back_mag),ron(ron),seeing(seeing) { mag_zeropoint = 2.5*log10(diameter*diameter*transmission*PI/4./hplanck) - 48.6; telescope = false; @@ -215,15 +215,13 @@ diameter(diameter), transmission(transmission), exp_time(exp_time), exp_num(exp_ * \param oversample Oversampling rate of the PSF image */ Observation::Observation(float diameter, float transmission, float exp_time, int exp_num, float back_mag, float ron, std::string psf_file,size_t Npix_x,size_t Npix_y, float oversample): -diameter(diameter), transmission(transmission), exp_time(exp_time), exp_num(exp_num), back_mag(back_mag),Npix_x(Npix_x),Npix_y(Npix_y) - , ron(ron), oversample(oversample) +Npix_x(Npix_x),Npix_y(Npix_y),diameter(diameter), transmission(transmission), exp_time(exp_time), exp_num(exp_num), back_mag(back_mag) , ron(ron), oversample(oversample) { - mag_zeropoint = 2.5*log10(diameter*diameter*transmission*PI/4./hplanck) - 48.6; + mag_zeropoint = 2.5*log10(diameter*diameter*transmission*PI/4./hplanck) - 48.6; -#ifdef ENABLE_FITS + CPFITS_READ cpfits(psf_file); - //std::auto_ptr fp (new CCfits::FITS (psf_file.c_str(), CCfits::Read)); - +/* std::auto_ptr fp(0); try { @@ -234,26 +232,27 @@ diameter(diameter), transmission(transmission), exp_time(exp_time), exp_num(exp_ std::cerr << "Cannot open " << psf_file << std::endl; exit(1); } - +*/ - CCfits::PHDU *h0=&fp->pHDU(); - int side_psf = h0->axis(0); - int N_psf = side_psf*side_psf; - map_psf.resize(N_psf); - h0->read(map_psf); - -#else - std::cout << "Please enable the preprocessor flag ENABLE_FITS !" << std::endl; - exit(1); -#endif - telescope = false; - } + //CCfits::PHDU *h0=&fp->pHDU(); + std::vector sizes; + cpfits.read(map_psf,sizes); + //int side_psf = h0->axis(0); + //int side_psf = sizes[0]; + //int N_psf = side_psf*side_psf; + //map_psf.resize(N_psf); + //h0->read(map_psf); + telescope = false; +} /// Reads in and sets the PSF from a fits file. If the pixel size of the fits is different (smaller) than the one of the telescope, it must be specified. void Observation::setPSF(std::string psf_file /// name of fits file with psf , float os /// over sampling factor ) { + + CPFITS_READ cpfits(psf_file); + /* #ifdef ENABLE_FITS // std::auto_ptr fp (new CCfits::FITS (psf_file.c_str(), CCfits::Read)); @@ -267,18 +266,23 @@ void Observation::setPSF(std::string psf_file /// name of fits file with psf std::cerr << "Cannot open " << psf_file << std::endl; exit(1); } +*/ + //CCfits::PHDU *h0=&fp->pHDU(); + std::vector size; + cpfits.read(map_psf, size); - CCfits::PHDU *h0=&fp->pHDU(); - int side_psf = h0->axis(0); - int N_psf = side_psf*side_psf; - map_psf.resize(N_psf); - h0->read(map_psf); - + //int side_psf = h0->axis(0); + //int side_psf = size[0]; + //int N_psf = side_psf*side_psf; + //map_psf.resize(N_psf); + //h0->read(map_psf); + + /* #else std::cout << "Please enable the preprocessor flag ENABLE_FITS !" << std::endl; exit(1); #endif - + */ oversample = os; } diff --git a/include/cpfits.h b/include/cpfits.h index bebf3400..2e38465e 100644 --- a/include/cpfits.h +++ b/include/cpfits.h @@ -45,15 +45,17 @@ class CPFITS_BASE{ // HDU-level Routines ///////////////////////////////// - /// returns the number of tabels + /// returns the number of tabels or images int get_num_hdus(){ int hdunum; - return fits_get_num_hdus(fptr, &hdunum, &status); + fits_get_num_hdus(fptr, &hdunum, &status); + return hdunum; } /// returns the current table number, 1... int get_current_hdu_num(){ int hdunum; - return fits_get_hdu_num(fptr,&hdunum); + fits_get_hdu_num(fptr,&hdunum); + return hdunum; } /// change the current table number @@ -70,7 +72,8 @@ class CPFITS_BASE{ int get_current_hdu_type(){ int hdutype; - return fits_get_hdu_type(fptr,&hdutype,&status); + fits_get_hdu_type(fptr,&hdutype,&status); + return hdutype; } ///////////////////////////////// @@ -131,8 +134,10 @@ class CPFITS_READ : public CPFITS_BASE { fits_open_file(&fptr,filename.c_str(), READONLY, &status); // print any error messages - if (status) fits_report_error(stderr, status); - + if (status){ + fits_report_error(stderr, status); + throw std::invalid_argument("missing file"); + } if(verbose){ std::cout << "Opening file : " << filename << std::endl; std::cout << " status : " << status << std::endl; @@ -160,9 +165,8 @@ class CPFITS_READ : public CPFITS_BASE { } /// read the whole image into a vector - int read(std::vector &output){ + int read(std::vector &output,std::vector &size){ - std::vector size; int dtype; imageInfo(dtype,size); long nelements=1; @@ -174,9 +178,8 @@ class CPFITS_READ : public CPFITS_BASE { ,NULL,output.data(),NULL, &status); } /// read the whole image into a vector - int read(std::vector &output){ + int read(std::vector &output,std::vector &size){ - std::vector size; int dtype; imageInfo(dtype,size); long nelements=1; @@ -187,6 +190,31 @@ class CPFITS_READ : public CPFITS_BASE { return fits_read_pix(fptr,TFLOAT,start.data(),nelements ,NULL,output.data(),NULL, &status); } + /// read the whole image into a valarray + int read(std::valarray &output,std::vector &size){ + + int dtype; + imageInfo(dtype,size); + long nelements=1; + for(long n : size) nelements *= n; + std::vector start(nelements,1); + output.resize(nelements); + + return fits_read_pix(fptr,TFLOAT,start.data(),nelements + ,NULL,&output[0],NULL, &status); + } + int read(std::valarray &output,std::vector &size){ + + int dtype; + imageInfo(dtype,size); + long nelements=1; + for(long n : size) nelements *= n; + std::vector start(nelements,1); + output.resize(nelements); + + return fits_read_pix(fptr,TDOUBLE,start.data(),nelements + ,NULL,&output[0],NULL, &status); + } /// read nelements in order from image to output array int read_block(double *output,long nelements,long * start){ @@ -254,7 +282,7 @@ class CPFITS_WRITE : public CPFITS_BASE { return fits_write_pix(fptr,TDOUBLE,fpixel.data(), im.size(),im.data(),&status); } - int writeImage(std::vector &im,std::vector &size){ + int write_image(std::vector &im,std::vector &size){ size_t n=1; for(size_t i : size) n *= i; assert(im.size() == n); @@ -264,6 +292,26 @@ class CPFITS_WRITE : public CPFITS_BASE { return fits_write_pix(fptr,TFLOAT,fpixel.data(), im.size(),im.data(),&status); } + int write_image(std::valarray &im,std::vector &size){ + size_t n=1; + for(size_t i : size) n *= i; + assert(im.size() == n); + fits_create_img(fptr,DOUBLE_IMG,size.size(), + size.data(), &status); + std::vector fpixel(size.size(),1); + return fits_write_pix(fptr,TDOUBLE,fpixel.data(), + im.size(),&im[0],&status); + } + int write_image(std::valarray &im,std::vector &size){ + size_t n=1; + for(size_t i : size) n *= i; + assert(im.size() == n); + fits_create_img(fptr,FLOAT_IMG,size.size(), + size.data(), &status); + std::vector fpixel(size.size(),1); + return fits_write_pix(fptr,TFLOAT,fpixel.data(), + im.size(),&im[0],&status); + } /// add or replace a key value in the header int writeKey(std::string &key,double value,std::string &comment ){ From cc8490fc7420e7f825c5f9ef0efee4daf0298c61 Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Sat, 26 Oct 2019 12:36:04 +0200 Subject: [PATCH 192/227] Removed CCFits from multimap.cpp. --- MultiPlane/multimap.cpp | 194 +++++++++++++++++++++------------------- 1 file changed, 102 insertions(+), 92 deletions(-) diff --git a/MultiPlane/multimap.cpp b/MultiPlane/multimap.cpp index 9925a80e..ddae25e4 100644 --- a/MultiPlane/multimap.cpp +++ b/MultiPlane/multimap.cpp @@ -5,10 +5,8 @@ #include "slsimlib.h" #include "multimap.h" -#include using namespace std; -using namespace CCfits; LensHaloMultiMap::LensHaloMultiMap( std::string fitsfile /// Original fits map of the density @@ -589,56 +587,73 @@ void LensMap::read_header(std::string fits_input_file void LensMap::read(std::string fits_input_file,double angDist){ std:: cout << " reading lens density map file: " << fits_input_file << std:: endl; - std::auto_ptr ff(new CCfits::FITS (fits_input_file, CCfits::Read)); - CCfits::PHDU &h0 = ff->pHDU(); + CPFITS_READ cpfits(fits_input_file); - h0.readAllKeys(); + //std::auto_ptr ff(new CCfits::FITS (fits_input_file, CCfits::Read)); + //CCfits::PHDU &h0 = ff->pHDU(); - assert(h0.axes() >= 2); + //h0.readAllKeys(); - nx = h0.axis(0); - ny = h0.axis(1); + int n_images = cpfits.get_num_hdus(); + + std::vector dims; + cpfits.read(surface_density, dims); + assert(dims.size() == 2); - size_t size = nx*ny; + nx = dims[0]; + ny = dims[1]; // principal HDU is read - h0.read(surface_density); - int nhdu = h0.axes(); - - if(nhdu > 2){ // file contains other lensing quantities - alpha1_bar.resize(size); - alpha2_bar.resize(size); - gamma1_bar.resize(size); - gamma2_bar.resize(size); - //gamma3_bar.resize(size); - phi_bar.resize(size); + //h0.read(surface_density); + //int nhdu = h0.axes(); + + if(n_images > 1){ // file contains other lensing quantities + + assert(n_images == 6); + + cpfits.change_hdu(2); + cpfits.read(alpha1_bar,dims); + + cpfits.change_hdu(3); + cpfits.read(alpha2_bar,dims); + + cpfits.change_hdu(4); + cpfits.read(gamma1_bar,dims); + + cpfits.change_hdu(5); + cpfits.read(gamma2_bar,dims); + + cpfits.change_hdu(6); + cpfits.read(phi_bar,dims); + + //alpha1_bar.resize(size); + //alpha2_bar.resize(size); + //gamma1_bar.resize(size); + //gamma2_bar.resize(size); + //phi_bar.resize(size); - CCfits::ExtHDU &h1=ff->extension(1); - h1.read(alpha1_bar); - CCfits::ExtHDU &h2=ff->extension(2); - h2.read(alpha2_bar); - CCfits::ExtHDU &h3=ff->extension(3); - h3.read(gamma1_bar); - CCfits::ExtHDU &h4=ff->extension(4); - h4.read(gamma2_bar); + //CCfits::ExtHDU &h1=ff->extension(1); + //h1.read(alpha1_bar); + //CCfits::ExtHDU &h2=ff->extension(2); + //h2.read(alpha2_bar); + //CCfits::ExtHDU &h3=ff->extension(3); + //h3.read(gamma1_bar); + //CCfits::ExtHDU &h4=ff->extension(4); + //h4.read(gamma2_bar); //std::cout << h0 << h1 << h2 << h3 << h4 << std::endl; } - try{ + + int err = 0; + { /* these are always present in ea*/ float res; - h0.readKey ("CD1_1",res); // angular resolution degrees - //h0.readKey ("REDSHIFT",z); - //h0.readKey ("WLOW",wlow); - //h0.readKey ("WUP",wup); - - //double D = 3*(pow(wup,4) - pow(wlow,4))/(pow(wup,3) - pow(wlow,3))/4; - //D /= (1+z)*h; - + err += cpfits.readKey ("CD1_1",res); // angular resolution degrees + res *= degreesTOradians*angDist; boxlMpc = res * nx; } - catch(CCfits::HDU::NoSuchKeyword){ + if(err != 0){ std::cerr << "LensMap fits map must have header keywords:" << std::endl << " CD1_1 - length on other side in Mpc/h" << std::endl; @@ -663,45 +678,32 @@ void LensMap::read(std::string fits_input_file,double angDist){ void LensMap::Myread(std::string fits_input_file){ std:: cout << " reading lens density map file: " << fits_input_file << std:: endl; - std::auto_ptr ff(new CCfits::FITS (fits_input_file, CCfits::Read)); - - CCfits::PHDU &h0 = ff->pHDU(); - - h0.readAllKeys(); + CPFITS_READ cpfits(fits_input_file); - assert(h0.axes() >= 2); + assert(cpfits.get_num_hdus() == 5); - nx = h0.axis(0); - ny = h0.axis(1); + double yrange; + cpfits.readKey("SIDEL1",boxlMpc); + cpfits.readKey("SIDEL2",yrange); + + std::vector dims; + cpfits.read(surface_density,dims); - size_t size = nx*ny; + assert(dims.size() == 2 ); + nx = dims[0]; + ny = dims[1]; - // principal HDU is read - h0.read(surface_density); - //int nhdu = h0.axes(); + cpfits.change_hdu(2); + cpfits.read(alpha1_bar,dims); - // file contains other lensing quantities - alpha1_bar.resize(size); - alpha2_bar.resize(size); - gamma1_bar.resize(size); - gamma2_bar.resize(size); - //phi_bar.resize(size); - - CCfits::ExtHDU &h1=ff->extension(1); - h1.read(alpha1_bar); - CCfits::ExtHDU &h2=ff->extension(2); - h2.read(alpha2_bar); - CCfits::ExtHDU &h3=ff->extension(3); - h3.read(gamma1_bar); - CCfits::ExtHDU &h4=ff->extension(4); - h4.read(gamma2_bar); - //std::cout << h0 << h1 << h2 << h3 << h4 << std::endl; - - // these are always present in each - //h0.readKey ("REDSHIFT",z); - double yrange; - h0.readKey("SIDEL1",boxlMpc); - h0.readKey("SIDEL2",yrange); + cpfits.change_hdu(3); + cpfits.read(alpha2_bar,dims); + + cpfits.change_hdu(4); + cpfits.read(gamma1_bar,dims); + + cpfits.change_hdu(5); + cpfits.read(gamma2_bar,dims); //double res = boxlMpc/nx; center *= 0; @@ -849,12 +851,14 @@ void LensMap::read_sub(CPFITS_READ &cpfits */ void LensMap::write(std::string filename ){ -#ifdef ENABLE_FITS - long naxis=2; - long naxes[2]={nx,ny}; +//#ifdef ENABLE_FITS + //long naxis=2; + //long naxes[2]={nx,ny}; - std::auto_ptr fout(0); + CPFITS_WRITE cpfits(filename,false); + /* + std::auto_ptr fout(0); try{ fout.reset(new FITS(filename,FLOAT_IMG,naxis,naxes)); } @@ -863,32 +867,38 @@ void LensMap::write(std::string filename ERROR_MESSAGE(); exit(1); } + */ std::vector naxex(2); naxex[0]=nx; naxex[1]=ny; - PHDU *phout=&fout->pHDU(); + //PHDU *phout=&fout->pHDU(); - phout->addKey("SIDEL1",boxlMpc,"x range in physical Mpc"); - phout->addKey("SIDEL2",y_range(),"y range in physical Mpc"); + cpfits.writeKey("SIDEL1",boxlMpc,"x range in physical Mpc"); + cpfits.writeKey("SIDEL2",y_range(),"y range in physical Mpc"); - phout->write( 1,nx*ny,surface_density ); - - ExtHDU *eh1=fout->addImage("alpah1", FLOAT_IMG, naxex); - eh1->write(1,nx*ny,alpha1_bar); - ExtHDU *eh2=fout->addImage("alpha2", FLOAT_IMG, naxex); - eh2->write(1,nx*ny,alpha2_bar); + cpfits.write_image(surface_density,naxex); + cpfits.write_image(alpha1_bar,naxex); + cpfits.write_image(alpha2_bar,naxex); + cpfits.write_image(gamma1_bar,naxex); + cpfits.write_image(gamma2_bar,naxex); + cpfits.write_image(phi_bar,naxex); + + //ExtHDU *eh1=fout->addImage("alpah1", FLOAT_IMG, naxex); + //eh1->write(1,nx*ny,alpha1_bar); + //ExtHDU *eh2=fout->addImage("alpha2", FLOAT_IMG, naxex); + //eh2->write(1,nx*ny,alpha2_bar); - ExtHDU *eh3=fout->addImage("gamma1", FLOAT_IMG, naxex); - eh3->write(1,nx*ny,gamma1_bar); - ExtHDU *eh4=fout->addImage("gamma2", FLOAT_IMG, naxex); - eh4->write(1,nx*ny,gamma2_bar); + //ExtHDU *eh3=fout->addImage("gamma1", FLOAT_IMG, naxex); + //eh3->write(1,nx*ny,gamma1_bar); + //ExtHDU *eh4=fout->addImage("gamma2", FLOAT_IMG, naxex); + //eh4->write(1,nx*ny,gamma2_bar); //std::cout << *phout << std::endl; -#else - std::cout << "Please enable the preprocessor flag ENABLE_FITS !" << std::endl; - exit(1); -#endif +//#else +// std::cout << "Please enable the preprocessor flag ENABLE_FITS !" << std::endl; +// exit(1); +//#endif } From a44a4303a41910a4b0d3392654eadaa51b067d3f Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Sun, 27 Oct 2019 16:18:59 +0100 Subject: [PATCH 193/227] Changed everything over to CPFITS --- AnalyticNSIE/source.cpp | 58 ------- ImageProcessing/observation.cpp | 41 +---- ImageProcessing/pixelize.cpp | 249 +++++++++++----------------- InputParameters/InputParams.cpp | 55 ++----- MultiPlane/MOKAfits.cpp | 277 +++++++++++++------------------- MultiPlane/lens_halos.cpp | 12 +- MultiPlane/multimap.cpp | 109 +++---------- MultiPlane/particle_lens.cpp | 5 - include/MOKAlens.h | 4 - include/cpfits.h | 37 +++-- include/gadget.hh | 5 +- include/image_processing.h | 4 +- include/multimap.h | 12 +- 13 files changed, 283 insertions(+), 585 deletions(-) diff --git a/AnalyticNSIE/source.cpp b/AnalyticNSIE/source.cpp index 1984d4a1..13d9db2c 100644 --- a/AnalyticNSIE/source.cpp +++ b/AnalyticNSIE/source.cpp @@ -7,10 +7,6 @@ #include "slsimlib.h" #include -//#ifdef ENABLE_FITS -//#include -//#include -//#endif #include "cpfits.h" using namespace std; @@ -558,28 +554,6 @@ SourceShapelets::SourceShapelets( CPFITS_READ cpfits(shap_file.c_str()); - /* - std::auto_ptr fp(0); - try - { - fp.reset( new CCfits::FITS(shap_file.c_str(), CCfits::Read) ); - } - catch (CCfits::FITS::CantOpen) - { - std::cerr << "Cannot open " << shap_file << std::endl; - exit(1); - } - - CCfits::PHDU& h0 = fp->pHDU(); - - h0.readKey("BETA", source_r); - source_r *= 0.03/180./60./60.*PI; - h0.readKey("DIM", n1); - h0.readKey("ID", id); - n2 = n1; - h0.read(coeff); - /***/ - cpfits.readKey("BETA", source_r); source_r *= 0.03/180./60./60.*PI; cpfits.readKey("DIM", n1); @@ -614,38 +588,6 @@ SourceShapelets::SourceShapelets( throw std::invalid_argument("Please enter a valid filename for the FITS file input"); CPFITS_READ cpfits(shap_file.c_str()); - - //std::auto_ptr fp(new CCfits::FITS(shap_file.c_str(), CCfits::Read)); - - /* - std::auto_ptr fp(0); - try - { - fp.reset( new CCfits::FITS(shap_file.c_str(), CCfits::Read) ); - } - catch (CCfits::FITS::CantOpen) - { - std::cerr << "Cannot open " << shap_file << std::endl; - exit(1); - } - - CCfits::PHDU& h0 = fp->pHDU(); - - h0.readKey("BETA", source_r); - source_r *= 0.03/180./60./60.*PI; - h0.readKey("SED_TYPE",sed_type); - - h0.readKey("MAG_B",mag_map[F435W]); // ACS F435W band magnitude - h0.readKey("MAG_V",mag_map[F606W]); // ACS F606W band magnitude - h0.readKey("MAG_I",mag_map[F775W]); // ACS F775W band magnitude - h0.readKey("MAG_Z",mag_map[F850LP]);// ACS F850LP band magnitude - h0.readKey("MAG_J",mag_map[F110W]); // NIC3 F110W band magnitude - h0.readKey("MAG_H",mag_map[F160W]); // NIC3 F160W band magnitude - h0.readKey("MAG_u_KIDS",mag_map[KiDS_U]); // u band obtained from SED fitting - h0.readKey("MAG_g_KIDS",mag_map[KiDS_G]); // g band obtained from SED fitting - h0.readKey("MAG_r_KIDS",mag_map[KiDS_R]); // r band obtained from SED fitting - h0.readKey("MAG_i_KIDS",mag_map[KiDS_I]); // i band obtained from SED fitting -*/ cpfits.readKey("BETA", source_r); source_r *= 0.03/180./60./60.*PI; diff --git a/ImageProcessing/observation.cpp b/ImageProcessing/observation.cpp index f1b7982d..379b687b 100644 --- a/ImageProcessing/observation.cpp +++ b/ImageProcessing/observation.cpp @@ -8,10 +8,6 @@ #include #include "slsimlib.h" -//#ifdef ENABLE_FITS -//#include -//#endif - #include "cpfits.h" #ifdef ENABLE_FFTW @@ -221,20 +217,6 @@ Npix_x(Npix_x),Npix_y(Npix_y),diameter(diameter), transmission(transmission), ex CPFITS_READ cpfits(psf_file); -/* - std::auto_ptr fp(0); - try - { - fp.reset( new CCfits::FITS (psf_file.c_str(), CCfits::Read) ); - } - catch (CCfits::FITS::CantOpen) - { - std::cerr << "Cannot open " << psf_file << std::endl; - exit(1); - } -*/ - - //CCfits::PHDU *h0=&fp->pHDU(); std::vector sizes; cpfits.read(map_psf,sizes); //int side_psf = h0->axis(0); @@ -252,22 +234,7 @@ void Observation::setPSF(std::string psf_file /// name of fits file with psf { CPFITS_READ cpfits(psf_file); - /* -#ifdef ENABLE_FITS - // std::auto_ptr fp (new CCfits::FITS (psf_file.c_str(), CCfits::Read)); - - std::auto_ptr fp(0); - try - { - fp.reset( new CCfits::FITS (psf_file.c_str(), CCfits::Read) ); - } - catch (CCfits::FITS::CantOpen) - { - std::cerr << "Cannot open " << psf_file << std::endl; - exit(1); - } -*/ - //CCfits::PHDU *h0=&fp->pHDU(); + std::vector size; cpfits.read(map_psf, size); @@ -277,12 +244,6 @@ void Observation::setPSF(std::string psf_file /// name of fits file with psf //map_psf.resize(N_psf); //h0->read(map_psf); - /* -#else - std::cout << "Please enable the preprocessor flag ENABLE_FITS !" << std::endl; - exit(1); -#endif - */ oversample = os; } diff --git a/ImageProcessing/pixelize.cpp b/ImageProcessing/pixelize.cpp index b4742bed..aa75bce0 100644 --- a/ImageProcessing/pixelize.cpp +++ b/ImageProcessing/pixelize.cpp @@ -7,10 +7,7 @@ #include "slsimlib.h" -#ifdef ENABLE_FITS -#include -//#include -#endif +#include "cpfits.h" #include #include @@ -87,7 +84,7 @@ Nx(other.Nx), Ny(other.Ny), resolution(other.resolution), rangeX(other.rangeX), // move constructor PixelMap::PixelMap(PixelMap&& other) -:Nx(0),Ny(0),map(std::move(other.map)),resolution(0), rangeX(0), rangeY(0){ +:map(std::move(other.map)),Nx(0),Ny(0),resolution(0), rangeX(0), rangeY(0){ Nx = other.Nx; Ny = other.Ny; @@ -158,8 +155,7 @@ PixelMap::PixelMap( ,double my_res /// resolution (rad) of fits image if not given in fits file, use default or -1 otherwise ) { - -#ifdef ENABLE_FITS + if(fitsfilename.empty()) throw std::invalid_argument("Please enter a valid filename for the FITS file input"); @@ -168,33 +164,22 @@ PixelMap::PixelMap( std::cerr << "Problem with inputfile " << fitsfilename << std::endl; throw std::invalid_argument("bad file"); } - //std::auto_ptr fp(new CCfits::FITS(fitsfilename, CCfits::Read)); - - std::auto_ptr fp(0); - try - { - fp.reset( new CCfits::FITS (fitsfilename, CCfits::Read) ); - } - catch (CCfits::FITS::CantOpen) - { - std::cerr << "Cannot open " << fitsfilename << std::endl; - throw std::invalid_argument("bad file"); - } + CPFITS_READ cpfits(fitsfilename); + + std::vector cpsize; + int bitpix; + cpfits.imageInfo(bitpix, cpsize); - CCfits::PHDU& h0 = fp->pHDU(); - - //const CCfits::ExtMap *h1=&fp->extension(); + Nx = cpsize[0]; + Ny = cpsize[1]; - Nx = h0.axis(0); - Ny = h0.axis(1); + int err = 0; - try - { - h0.readKey("CRVAL1", center[0]); - h0.readKey("CRVAL2", center[1]); - } - catch(CCfits::HDU::NoSuchKeyword) + err += cpfits.readKey("CRVAL1", center[0]); + err += cpfits.readKey("CRVAL2", center[1]); + + if(err != 0) { center[0] = 0.0; center[1] = 0.0; @@ -202,33 +187,34 @@ PixelMap::PixelMap( if(my_res == -1){ // read the resolution - try + err = 0; { double cdelt2; - h0.readKey("CDELT1", my_res); - h0.readKey("CDELT2", cdelt2); + err += cpfits.readKey("CDELT1", my_res); + err += cpfits.readKey("CDELT2", cdelt2); if(std::abs(my_res) - std::abs(cdelt2) > 1e-6) throw std::runtime_error("non-square pixels in FITS file " + fitsfilename); } - catch(CCfits::HDU::NoSuchKeyword) + if(err != 0) { - try{ + err = 0; + { double cd12, cd21, cd22; - h0.readKey("CD1_1", my_res); - h0.readKey("CD1_2", cd12); - h0.readKey("CD2_1", cd21); - h0.readKey("CD2_2", cd22); + err += cpfits.readKey("CD1_1", my_res); + err += cpfits.readKey("CD1_2", cd12); + err += cpfits.readKey("CD2_1", cd21); + err += cpfits.readKey("CD2_2", cd22); if(std::abs(my_res) - std::abs(cd22) > 1e-6) throw std::runtime_error("non-square pixels in FITS file " + fitsfilename); if(cd12 || cd21) throw std::runtime_error("pixels not aligned with coordinates in FITS file " + fitsfilename); } - catch(CCfits::HDU::NoSuchKeyword){ + if(err != 0){ double ps; - try{ - h0.readKey("PHYSICALSIZE",ps); - } - catch(CCfits::HDU::NoSuchKeyword){ + + err = cpfits.readKey("PHYSICALSIZE",ps); + + if(err != 0){ std::cerr << "PixelMap input fits field must have header keywords:" << std::endl << " PHYSICALSIZE - size of map in degrees" < fout(new CCfits::FITS(filename, FLOAT_IMG, naxis, naxes)); - - std::auto_ptr fout(0); - try - { - fout.reset( new CCfits::FITS(filename, FLOAT_IMG, naxis, naxes) ); - } - catch (CCfits::FITS::CantOpen) - { - std::cerr << "Cannot open " << filename << std::endl; - exit(1); - } - std::vector naxex(2); naxex[0] = Nx; naxex[1] = Ny; - - CCfits::PHDU& phout = fout->pHDU(); - - phout.write(1, map.size(), map); - - phout.addKey("WCSAXES", 2, "number of World Coordinate System axes"); - phout.addKey("CRPIX1", 0.5*(naxex[0]+1), "x-coordinate of reference pixel"); - phout.addKey("CRPIX2", 0.5*(naxex[1]+1), "y-coordinate of reference pixel"); - phout.addKey("CRVAL1", 0.0, "first axis value at reference pixel"); - phout.addKey("CRVAL2", 0.0, "second axis value at reference pixel"); - phout.addKey("CTYPE1", "RA---TAN", "the coordinate type for the first axis"); - phout.addKey("CTYPE2", "DEC--TAN", "the coordinate type for the second axis"); - phout.addKey("CUNIT1", "deg ", "the coordinate unit for the first axis"); - phout.addKey("CUNIT2", "deg ", "the coordinate unit for the second axis"); - phout.addKey("CDELT1", 180*resolution/PI, "partial of first axis coordinate w.r.t. x"); - phout.addKey("CDELT2", 180*resolution/PI, "partial of second axis coordinate w.r.t. y"); - phout.addKey("CROTA2", 0.0, ""); - phout.addKey("CD1_1", 180*resolution/PI, "partial of first axis coordinate w.r.t. x"); - phout.addKey("CD1_2", 0.0, "partial of first axis coordinate w.r.t. y"); - phout.addKey("CD2_1", 0.0, "partial of second axis coordinate w.r.t. x"); - phout.addKey("CD2_2", 180*resolution/PI, "partial of second axis coordinate w.r.t. y"); - - phout.addKey("Nx", Nx, ""); - phout.addKey("Ny", Ny, ""); - phout.addKey("range x", map_boundary_p2[0]-map_boundary_p1[0], "radians"); - phout.addKey("RA", center[0], "radians"); - phout.addKey("DEC", center[1], "radians"); - - if(verbose) - std::cout << phout << std::endl; -#else - std::cerr << "Please enable the preprocessor flag ENABLE_FITS !" << std::endl; - exit(1); -#endif -} - -void PixelMap::printFITS(std::string filename,std::vector> &extra_header_info, bool verbose) const + + cpfits.write_image(map,naxex); // write the map + + cpfits.writeKey("WCSAXES", 2, "number of World Coordinate System axes"); + cpfits.writeKey("CRPIX1", 0.5*(naxex[0]+1), "x-coordinate of reference pixel"); + cpfits.writeKey("CRPIX2", 0.5*(naxex[1]+1), "y-coordinate of reference pixel"); + cpfits.writeKey("CRVAL1", 0.0, "first axis value at reference pixel"); + cpfits.writeKey("CRVAL2", 0.0, "second axis value at reference pixel"); + //cpfits.writeKey("CTYPE1", "RA---TAN", "the coordinate type for the first axis"); + //cpfits.writeKey("CTYPE2", "DEC--TAN", "the coordinate type for the second axis"); + //cpfits.writeKey("CUNIT1", "deg ", "the coordinate unit for the first axis"); + //cpfits.writeKey("CUNIT2", "deg ", "the coordinate unit for the second axis"); + cpfits.writeKey("CDELT1", 180*resolution/PI, "partial of first axis coordinate w.r.t. x"); + cpfits.writeKey("CDELT2", 180*resolution/PI, "partial of second axis coordinate w.r.t. y"); + cpfits.writeKey("CROTA2", 0.0, ""); + cpfits.writeKey("CD1_1", 180*resolution/PI, "partial of first axis coordinate w.r.t. x"); + cpfits.writeKey("CD1_2", 0.0, "partial of first axis coordinate w.r.t. y"); + cpfits.writeKey("CD2_1", 0.0, "partial of second axis coordinate w.r.t. x"); + cpfits.writeKey("CD2_2", 180*resolution/PI, "partial of second axis coordinate w.r.t. y"); + + cpfits.writeKey("Nx", Nx, ""); + cpfits.writeKey("Ny", Ny, ""); + cpfits.writeKey("range x", map_boundary_p2[0]-map_boundary_p1[0], "radians"); + cpfits.writeKey("RA", center[0], "radians"); + cpfits.writeKey("DEC", center[1], "radians"); + +} + +void PixelMap::printFITS(std::string filename + ,std::vector> &extra_header_info, bool verbose) { -#ifdef ENABLE_FITS + if(filename.empty()) throw std::invalid_argument("Please enter a valid filename for the FITS file output"); - long naxis = 2; - long naxes[2] = {(long)Nx, (long)Ny}; - - // might throw CCfits::FITS::CantCreate - //std::auto_ptr fout(new CCfits::FITS(filename, FLOAT_IMG, naxis, naxes)); + CPFITS_WRITE cpfits(filename,false); - std::auto_ptr fout(0); - try - { - fout.reset( new CCfits::FITS(filename, FLOAT_IMG, naxis, naxes) ); - } - catch (CCfits::FITS::CantOpen) - { - std::cerr << "Cannot open " << filename << std::endl; - exit(1); - } - std::vector naxex(2); naxex[0] = Nx; naxex[1] = Ny; - - CCfits::PHDU& phout = fout->pHDU(); - - phout.write(1, map.size(), map); - - phout.addKey("WCSAXES", 2, "number of World Coordinate System axes"); - phout.addKey("CRPIX1", 0.5*(naxex[0]+1), "x-coordinate of reference pixel"); - phout.addKey("CRPIX2", 0.5*(naxex[1]+1), "y-coordinate of reference pixel"); - phout.addKey("CRVAL1", 0.0, "first axis value at reference pixel"); - phout.addKey("CRVAL2", 0.0, "second axis value at reference pixel"); - phout.addKey("CTYPE1", "RA---TAN", "the coordinate type for the first axis"); - phout.addKey("CTYPE2", "DEC--TAN", "the coordinate type for the second axis"); - phout.addKey("CUNIT1", "deg ", "the coordinate unit for the first axis"); - phout.addKey("CUNIT2", "deg ", "the coordinate unit for the second axis"); - phout.addKey("CDELT1", 180*resolution/PI, "partial of first axis coordinate w.r.t. x"); - phout.addKey("CDELT2", 180*resolution/PI, "partial of second axis coordinate w.r.t. y"); - phout.addKey("CROTA2", 0.0, ""); - phout.addKey("CD1_1", 180*resolution/PI, "partial of first axis coordinate w.r.t. x"); - phout.addKey("CD1_2", 0.0, "partial of first axis coordinate w.r.t. y"); - phout.addKey("CD2_1", 0.0, "partial of second axis coordinate w.r.t. x"); - phout.addKey("CD2_2", 180*resolution/PI, "partial of second axis coordinate w.r.t. y"); - - phout.addKey("Nx", Nx, ""); - phout.addKey("Ny", Ny, ""); - phout.addKey("range x", map_boundary_p2[0]-map_boundary_p1[0], "radians"); - phout.addKey("RA", center[0], "radians"); - phout.addKey("DEC", center[1], "radians"); + + cpfits.write_image(map,naxex); + + cpfits.writeKey("WCSAXES", 2, "number of World Coordinate System axes"); + cpfits.writeKey("CRPIX1", 0.5*(naxex[0]+1), "x-coordinate of reference pixel"); + cpfits.writeKey("CRPIX2", 0.5*(naxex[1]+1), "y-coordinate of reference pixel"); + cpfits.writeKey("CRVAL1", 0.0, "first axis value at reference pixel"); + cpfits.writeKey("CRVAL2", 0.0, "second axis value at reference pixel"); + //cpfits.writeKey("CTYPE1", "RA---TAN", "the coordinate type for the first axis"); + //cpfits.writeKey("CTYPE2", "DEC--TAN", "the coordinate type for the second axis"); + //cpfits.writeKey("CUNIT1", "deg ", "the coordinate unit for the first axis"); + //cpfits.writeKey("CUNIT2", "deg ", "the coordinate unit for the second axis"); + cpfits.writeKey("CDELT1", 180*resolution/PI, "partial of first axis coordinate w.r.t. x"); + cpfits.writeKey("CDELT2", 180*resolution/PI, "partial of second axis coordinate w.r.t. y"); + cpfits.writeKey("CROTA2", 0.0, ""); + cpfits.writeKey("CD1_1", 180*resolution/PI, "partial of first axis coordinate w.r.t. x"); + cpfits.writeKey("CD1_2", 0.0, "partial of first axis coordinate w.r.t. y"); + cpfits.writeKey("CD2_1", 0.0, "partial of second axis coordinate w.r.t. x"); + cpfits.writeKey("CD2_2", 180*resolution/PI, "partial of second axis coordinate w.r.t. y"); + + cpfits.writeKey("Nx", Nx, ""); + cpfits.writeKey("Ny", Ny, ""); + cpfits.writeKey("range x", map_boundary_p2[0]-map_boundary_p1[0], "radians"); + cpfits.writeKey("RA", center[0], "radians"); + cpfits.writeKey("DEC", center[1], "radians"); for(auto hp : extra_header_info){ - phout.addKey(std::get<0>(hp),std::get<1>(hp),std::get<2>(hp)); + cpfits.writeKey(std::get<0>(hp),std::get<1>(hp),std::get<2>(hp)); } - - if(verbose) - std::cout << phout << std::endl; -#else - std::cerr << "Please enable the preprocessor flag ENABLE_FITS !" << std::endl; - exit(1); -#endif } /** diff --git a/InputParameters/InputParams.cpp b/InputParameters/InputParams.cpp index 80c2dc77..6a1a2182 100644 --- a/InputParameters/InputParams.cpp +++ b/InputParameters/InputParams.cpp @@ -7,9 +7,7 @@ #include "slsimlib.h" #include -#ifdef ENABLE_FITS -#include -#endif +#include "cpfits.h" namespace { @@ -278,31 +276,14 @@ void InputParams::PrintToFile(std::string filename, bool strip_unused) const */ void InputParams::readMOKA() { -#ifdef ENABLE_FITS std::cout << "Reading MOKA FITS parameters...\n" << std::endl; std::string MOKA_input_file; if(!get("MOKA_input_file", MOKA_input_file)) throw new std::runtime_error("Parameter MOKA_input_file must be set for MOKA_input_params to work!"); - try - { - //std::auto_ptr ff(new CCfits::FITS(MOKA_input_file, CCfits::Read)); - - std::auto_ptr ff(0); - try - { - ff.reset( new CCfits::FITS(MOKA_input_file, CCfits::Read) ); - } - catch (CCfits::FITS::CantOpen) - { - std::cerr << "Cannot open " << MOKA_input_file << std::endl; - exit(1); - } - - - CCfits::PHDU* h0 = &ff->pHDU(); - + CPFITS_READ cpfits("MOKA_input_file"); + double sidel; // box side length in arc seconds double zlens; // redshift of lens double zsource; // redshift of source @@ -310,15 +291,17 @@ void InputParams::readMOKA() double omega_l; // omega_lambda double hubble; // hubble constant H/100 - try{ - h0->readKey("SIDEL", sidel); - h0->readKey("ZLENS", zlens); - h0->readKey("ZSOURCE", zsource); - h0->readKey("OMEGA", omega_m); - h0->readKey("LAMBDA", omega_l); - h0->readKey("H", hubble); - } - catch(CCfits::HDU::NoSuchKeyword){ + + int error = 0; + + error += cpfits.readKey("SIDEL", sidel); + error += cpfits.readKey("ZLENS", zlens); + error += cpfits.readKey("ZSOURCE", zsource); + error += cpfits.readKey("OMEGA", omega_m); + error += cpfits.readKey("LAMBDA", omega_l); + error += cpfits.readKey("H", hubble); + + if(error != 0){ std::cerr << "MOKA fits file must have header keywords:" << std::endl << " SIDEL - length on a side" << std::endl << " ZLENS - redshift of lens" << std::endl @@ -354,16 +337,6 @@ void InputParams::readMOKA() params["hubble"] = to_str(hubble); comments["hubble"] = "# [MOKA]"; std::cout << std::left << std::setw(24) << "hubble" << std::setw(12) << hubble << "# [MOKA]" << std::endl; - } - catch(CCfits::FITS::CantOpen) - { - std::cout << "can not open " << MOKA_input_file << std::endl; - exit(1); - } -#else - std::cout << "Please enable the preprocessor flag ENABLE_FITS !" << std::endl; - exit(1); -#endif } /** \brief Assigns to "value" the value of the parameter called "label". diff --git a/MultiPlane/MOKAfits.cpp b/MultiPlane/MOKAfits.cpp index 2b55ba51..0ec2ea73 100644 --- a/MultiPlane/MOKAfits.cpp +++ b/MultiPlane/MOKAfits.cpp @@ -8,11 +8,7 @@ #include "MOKAlens.h" #include -#ifdef ENABLE_FITS -#include -using namespace CCfits; - -#endif +#include "cpfits.h" #ifdef ENABLE_FFTW #include "fftw3.h" @@ -66,25 +62,16 @@ using namespace CCfits; // carlo test end void LensHaloMassMap::getDims(){ -#ifdef ENABLE_FITS - try{ - std::auto_ptr ff(new FITS (MOKA_input_file, Read)); - - PHDU *h0=&ff->pHDU(); - - map.nx = h0->axis(0); - map.ny = h0->axis(1); - std:: cout << "nx ny " << std:: endl; - std:: cout << map.nx << " " << map.ny << std:: endl; - } - catch(FITS::CantOpen){ - std::cout << "can not open " << MOKA_input_file << std::endl; - exit(1); - } -#else - std::cout << "Please enable the preprocessor flag ENABLE_FITS !" << std::endl; - exit(1); -#endif + + CPFITS_READ cpfits(MOKA_input_file); + std::vector size; + int bitpix; + cpfits.imageInfo(bitpix,size); + + map.nx = size[0]; + map.ny = size[1]; + std:: cout << "nx ny " << std:: endl; + std:: cout << map.nx << " " << map.ny << std:: endl; } /** @@ -102,45 +89,46 @@ void LensHaloMassMap::readMap(){ void MOKAmap::read(std::string MOKA_input_file,bool zeromean,const COSMOLOGY &cosmo){ std:: cout << " reading lens density map file: " << MOKA_input_file << std:: endl; - std::auto_ptr ff(new FITS (MOKA_input_file, Read)); - - PHDU *h0=&ff->pHDU(); + CPFITS_READ cpfits(MOKA_input_file); + std::vector size; + cpfits.read(surface_density,size); - h0->readAllKeys(); + assert(size.size() == 2); - assert(h0->axes() >= 2); + nx = size[0]; + ny = size[1]; - nx = h0->axis(0); - ny = h0->axis(1); - - size_t size = nx*ny; - - surface_density.resize(size); + //size_t size = nx*ny; + //surface_density.resize(size); // try to read MVIR, if it exists is a MOKA map bool moka; - try { - h0->readKey ("MVIR",m); + + if(cpfits.readKey ("MVIR",m)){ moka=true; - } - catch(CCfits::HDU::NoSuchKeyword) { + }else{ moka=false; } - - // principal HDU is read - h0->read(surface_density); - + int n_images = cpfits.get_num_hdus(); + if(moka){ // check if other quantities exist if not it has to compute them - int nhdu = h0->axis(2); - if(nhdu==1){ - alpha1_bar.resize(0); - //std:: cout << " preProcessing Map MOKA fits file has only KAPPA map" << std:: endl; - //PreProcessFFTWMap(zerosize); - } - - if(nhdu != 1){ // file contains other lensing quantities + if(n_images >= 5){ // file contains other lensing quantities + + cpfits.change_hdu(2); + cpfits.read(alpha1_bar,size); + + cpfits.change_hdu(3); + cpfits.read(alpha2_bar,size); + + cpfits.change_hdu(4); + cpfits.read(gamma1_bar,size); + + cpfits.change_hdu(5); + cpfits.read(gamma2_bar,size); + + /* alpha1_bar.resize(size); alpha2_bar.resize(size); gamma1_bar.resize(size); @@ -159,25 +147,32 @@ void MOKAmap::read(std::string MOKA_input_file,bool zeromean,const COSMOLOGY &co ExtHDU &h4=ff->extension(4); h4.read(gamma2_bar); std::cout << *h0 << h1 << h2 << h3 << h4 << std::endl; + */ + + if(n_images == 6){ + cpfits.change_hdu(6); + cpfits.read(phi_bar,size); + } } - try{ + int error = 0; + // these are always present in each fits file created by MOKA - h0->readKey ("SIDEL",boxlarcsec); - h0->readKey ("SIDEL2",boxlMpc); // recall you that MOKA Mpc/h - h0->readKey ("ZLENS",zlens); - h0->readKey ("ZSOURCE",zsource); - h0->readKey ("OMEGA",omegam); - h0->readKey ("LAMBDA",omegal); - h0->readKey ("H",h); - h0->readKey ("W",wq); - h0->readKey ("MSTAR",mstar); - // h0->readKey ("MVIR",m); - h0->readKey ("CONCENTRATION",c); - h0->readKey ("DL",Dlens); - h0->readKey ("DLS",DLS); - h0->readKey ("DS",DS); - } - catch(CCfits::HDU::NoSuchKeyword){ + error += cpfits.readKey ("SIDEL",boxlarcsec); + error += cpfits.readKey ("SIDEL2",boxlMpc); // recall you that MOKA Mpc/h + error += cpfits.readKey ("ZLENS",zlens); + error += cpfits.readKey ("ZSOURCE",zsource); + error += cpfits.readKey ("OMEGA",omegam); + error += cpfits.readKey ("LAMBDA",omegal); + error += cpfits.readKey ("H",h); + error += cpfits.readKey ("W",wq); + error += cpfits.readKey ("MSTAR",mstar); + // error += cpfits.readKey ("MVIR",m); + error += cpfits.readKey ("CONCENTRATION",c); + error += cpfits.readKey ("DL",Dlens); + error += cpfits.readKey ("DLS",DLS); + error += cpfits.readKey ("DS",DS); + + if(error != 0){ std::cerr << "MOKA fits map must have header keywords:" << std::endl << " SIDEL - length on a side in Mpc/h" << std::endl << " SIDEL2 - length on other side in Mpc/h" << std::endl @@ -200,8 +195,6 @@ void MOKAmap::read(std::string MOKA_input_file,bool zeromean,const COSMOLOGY &co int npixels = nx; - // cut a square! - // } // keep it like it is, even if it is a rectangle if(ny!=nx){ std:: cout << " " << std:: endl; @@ -220,27 +213,18 @@ void MOKAmap::read(std::string MOKA_input_file,bool zeromean,const COSMOLOGY &co // for(int i=0;ireadKey("WLOW",d1); + int error = cpfits.readKey("WLOW",d1); d1=d1/cosmo.gethubble(); - } - catch(CCfits::HDU::NoSuchKeyword){ - try{ - h0->readKey("DLLOW",d1); - } - catch(CCfits::HDU::NoSuchKeyword){ + + if(error){ + if(cpfits.readKey("DLLOW",d1)){ d1 = d2 = 0; } - try{ - h0->readKey("WUP",d2); - d2=d2/cosmo.gethubble(); - } - catch(CCfits::HDU::NoSuchKeyword){ - try{ - h0->readKey("DLUP",d2); - } - catch(CCfits::HDU::NoSuchKeyword){ + error = cpfits.readKey("WUP",d2); + d2=d2/cosmo.gethubble(); + if(error){ + if(cpfits.readKey("DLUP",d2)){ d1 = d2 = 0; } } @@ -248,22 +232,12 @@ void MOKAmap::read(std::string MOKA_input_file,bool zeromean,const COSMOLOGY &co zlens = cosmo.invComovingDist(( d1 + d2 )*0.5); }else{ - // if angular size distances are not set use ZLENS or REDSHIFT - - try { - h0->readKey("ZLENS",zlens); - } - catch(CCfits::HDU::NoSuchKeyword) { - try { - h0->readKey("REDSHIFT",zlens); - } - catch(CCfits::HDU::NoSuchKeyword){ + if(cpfits.readKey("ZLENS",zlens)){ + if(cpfits.readKey("REDSHIFT",zlens)){ std::cout << "unable to read fits mass map header keywords" << std::endl << " either DLUP and DLLOW need to be set or ZLENS or REDSHIFT" << std::endl; exit(1); } - - } } @@ -278,22 +252,20 @@ void MOKAmap::read(std::string MOKA_input_file,bool zeromean,const COSMOLOGY &co Dlens = cosmo.angDist(0.,zlens); // physical double inarcsec = 180./M_PI/Dlens*60.*60.; double pixLMpc,pixelunit; - try{ - // physical size in degrees - h0->readKey("PHYSICALSIZE",boxlarcsec); + + // physical size in degrees + int error = cpfits.readKey("PHYSICALSIZE",boxlarcsec); + boxlarcsec=boxlarcsec*60.*60.; + pixLMpc = boxlarcsec/npixels/inarcsec; + boxlMpc = pixLMpc*npixels; + if(error){ + // physical size in degrees + error = cpfits.readKey("PHYSICAL",boxlarcsec); boxlarcsec=boxlarcsec*60.*60.; pixLMpc = boxlarcsec/npixels/inarcsec; boxlMpc = pixLMpc*npixels; - } - catch(CCfits::HDU::NoSuchKeyword) { - try{ - // physical size in degrees - h0->readKey("PHYSICAL",boxlarcsec); - boxlarcsec=boxlarcsec*60.*60.; - pixLMpc = boxlarcsec/npixels/inarcsec; - boxlMpc = pixLMpc*npixels; - } - catch(CCfits::HDU::NoSuchKeyword) { + + if(error){ std::cerr << "fits mass map must have header keywords:" << std::endl << " REDSHIFT - redshift of map plane" << std::endl //<< " DLOW - ?? Mpc/h" << std::endl @@ -303,23 +275,18 @@ void MOKAmap::read(std::string MOKA_input_file,bool zeromean,const COSMOLOGY &co std::cout << " unable to read map PIXELUNITS" << std::endl; exit(1); - } - } + error = cpfits.readKey("PIXELUNIT",pixelunit); + pixelunit=pixelunit/pixLMpc/pixLMpc; - try { - h0->readKey("PIXELUNIT",pixelunit); + if(error) { + + error = cpfits.readKey("PIXELUNI",pixelunit); pixelunit=pixelunit/pixLMpc/pixLMpc; - } - catch(CCfits::HDU::NoSuchKeyword) { - try { - h0->readKey("PIXELUNI",pixelunit); - pixelunit=pixelunit/pixLMpc/pixLMpc; - } - catch(CCfits::HDU::NoSuchKeyword) { + if(error) { std::cerr << "fits mass map must have header keywords:" << std::endl << " REDSHIFT - redshift of map plane" << std::endl //<< " DLOW - ?? Mpc/h" << std::endl @@ -403,56 +370,42 @@ void LensHaloMassMap::writeImage(std::string filename){ map.write(filename); } void MOKAmap::write(std::string filename){ -#ifdef ENABLE_FITS - long naxis=2; - long naxes[2]={nx,ny}; - std::auto_ptr fout(0); - - try{ - fout.reset(new FITS(filename,FLOAT_IMG,naxis,naxes)); - } - catch(FITS::CantCreate){ - std::cout << "Unable to open fits file " << filename << std::endl; - ERROR_MESSAGE(); - exit(1); - } + CPFITS_WRITE cpfits(filename,false); std::vector naxex(2); naxex[0]=nx; naxex[1]=ny; - PHDU *phout=&fout->pHDU(); + cpfits.write_image(surface_density, naxex); + //PHDU *phout=&fout->pHDU(); + //phout->write( 1,nx*ny,surface_density ); - phout->write( 1,nx*ny,surface_density ); + cpfits.writeKey ("SIDEL",boxlarcsec,"arcsec"); + cpfits.writeKey ("SIDEL2",boxlMpc,"Mpc/h"); + cpfits.writeKey ("ZLENS",zlens,"lens redshift"); + cpfits.writeKey ("ZSOURCE",zsource, "source redshift"); + cpfits.writeKey ("OMEGA",omegam,"omega matter"); + cpfits.writeKey ("LAMBDA",omegal,"omega lamda"); + cpfits.writeKey ("H",h,"hubble/100"); + cpfits.writeKey ("W",wq,"dark energy equation of state parameter"); + cpfits.writeKey ("MSTAR",mstar,"stellar mass of the BCG in Msun/h"); + cpfits.writeKey ("MVIR",m,"virial mass of the halo in Msun/h"); + cpfits.writeKey ("CONCENTRATION",c,"NFW concentration"); + cpfits.writeKey ("DL",Dlens,"Mpc/h"); + cpfits.writeKey ("DLS",DLS,"Mpc/h"); + cpfits.writeKey ("DS",DS,"Mpc/h"); - phout->addKey ("SIDEL",boxlarcsec,"arcsec"); - phout->addKey ("SIDEL2",boxlMpc,"Mpc/h"); - phout->addKey ("ZLENS",zlens,"lens redshift"); - phout->addKey ("ZSOURCE",zsource, "source redshift"); - phout->addKey ("OMEGA",omegam,"omega matter"); - phout->addKey ("LAMBDA",omegal,"omega lamda"); - phout->addKey ("H",h,"hubble/100"); - phout->addKey ("W",wq,"dark energy equation of state parameter"); - phout->addKey ("MSTAR",mstar,"stellar mass of the BCG in Msun/h"); - phout->addKey ("MVIR",m,"virial mass of the halo in Msun/h"); - phout->addKey ("CONCENTRATION",c,"NFW concentration"); - phout->addKey ("DL",Dlens,"Mpc/h"); - phout->addKey ("DLS",DLS,"Mpc/h"); - phout->addKey ("DS",DS,"Mpc/h"); + cpfits.write_image(gamma1_bar, naxex); + cpfits.write_image(gamma2_bar, naxex); - ExtHDU *eh1=fout->addImage("gamma1", FLOAT_IMG, naxex); - eh1->write(1,nx*ny,gamma1_bar); - ExtHDU *eh2=fout->addImage("gamma2", FLOAT_IMG, naxex); - eh2->write(1,nx*ny,gamma2_bar); + //ExtHDU *eh1=fout->addImage("gamma1", FLOAT_IMG, naxex); + //eh1->write(1,nx*ny,gamma1_bar); + //ExtHDU *eh2=fout->addImage("gamma2", FLOAT_IMG, naxex); + //eh2->write(1,nx*ny,gamma2_bar); //ExtHDU *eh3=fout->addImage("gamma3", FLOAT_IMG, naxex); //eh3->write(1,nx*ny,gamma3_bar); - - std::cout << *phout << std::endl; -#else - std::cout << "Please enable the preprocessor flag ENABLE_FITS !" << std::endl; - exit(1); -#endif + //std::cout << *phout << std::endl; } /** diff --git a/MultiPlane/lens_halos.cpp b/MultiPlane/lens_halos.cpp index 3f4e2cd7..695e89aa 100644 --- a/MultiPlane/lens_halos.cpp +++ b/MultiPlane/lens_halos.cpp @@ -57,7 +57,7 @@ LensHalo::LensHalo(const LensHalo &h){ mnorm = h.mnorm; Rmax = h.Rmax; - stars_index = stars_index; + stars_index = h.stars_index; stars_xp = h.stars_xp; stars_N = h.stars_N; @@ -73,7 +73,7 @@ LensHalo::LensHalo(const LensHalo &h){ star_fstars = h.star_fstars; star_Nregions = h.star_Nregions; - star_region = star_region; + star_region = h.star_region; beta = h.beta; @@ -114,7 +114,7 @@ LensHalo & LensHalo::operator=(LensHalo &&h){ mnorm = h.mnorm; Rmax = h.Rmax; - stars_index = stars_index; + stars_index = h.stars_index; stars_xp = h.stars_xp; stars_N = h.stars_N; @@ -128,7 +128,7 @@ LensHalo & LensHalo::operator=(LensHalo &&h){ star_fstars = h.star_fstars; star_Nregions = h.star_Nregions; - star_region = star_region; + star_region = h.star_region; beta = h.beta; @@ -173,7 +173,7 @@ LensHalo & LensHalo::operator=(const LensHalo &h){ mnorm = h.mnorm; Rmax = h.Rmax; - stars_index = stars_index; + stars_index = h.stars_index; stars_xp = h.stars_xp; stars_N = h.stars_N; @@ -189,7 +189,7 @@ LensHalo & LensHalo::operator=(const LensHalo &h){ star_fstars = h.star_fstars; star_Nregions = h.star_Nregions; - star_region = star_region; + star_region = h.star_region; beta = h.beta; diff --git a/MultiPlane/multimap.cpp b/MultiPlane/multimap.cpp index ddae25e4..903a8087 100644 --- a/MultiPlane/multimap.cpp +++ b/MultiPlane/multimap.cpp @@ -20,13 +20,6 @@ LensHalo(redshift,c),single_grid(single_grid_mode),cosmo(c),cpfits(fitsfile) ,mass_unit(mass_unit),fitsfilename(fitsfile) { -/* try{ - ff = new CCfits::FITS (fitsfilename, CCfits::Read); - }catch(...){ - std::cerr << "CCfits throw an exception: probably file " << fitsfile << " does not exist." << std::endl; - } - */ - zerosize = 1; rscale = 1.0; @@ -51,9 +44,9 @@ LensHalo(redshift,c),single_grid(single_grid_mode),cosmo(c),cpfits(fitsfile) //rs2 = submap.boxlMpc*submap.boxlMpc/( 2*PI*submap.nx ); //rs2 = 2*4*submap.boxlMpc*submap.boxlMpc/( 2*PI*submap.nx ); rs2 = submap.boxlMpc*submap.boxlMpc/( 2*submap.nx )*gfactor/ffactor; - wlr.rs2 = wsr.rs2 = rs2; + resolution = submap.x_resolution(); //border_width = 4.5*sqrt(rs2)/res + 1; border_width = ffactor * sqrt(rs2) / resolution + 1; @@ -117,21 +110,21 @@ LensHalo(redshift,c),single_grid(single_grid_mode),cosmo(c),cpfits(fitsfile) Point_2d range = long_range_map.upperright - long_range_map.lowerleft; long_range_map.nx = submap.nx/desample; - double res_x = long_range_map.boxlMpc / long_range_map.nx; + double lr_res_x = long_range_map.boxlMpc / long_range_map.nx; // get the ny that makes the pixels clossest to square double Ly = submap.y_range(); - long_range_map.ny = (int)( Ly/res_x ); + long_range_map.ny = (int)( Ly/lr_res_x ); //long_range_map.ny = (int)( Ly/res_x ) - 1; // ???? - if(Ly - long_range_map.ny*res_x > res_x/2) long_range_map.ny += 1; + if(Ly - long_range_map.ny*lr_res_x > lr_res_x/2) long_range_map.ny += 1; size_t nx = long_range_map.nx; size_t ny = long_range_map.ny; - double res_y = submap.y_range()/ny; + double lr_res_y = submap.y_range()/ny; std::cout << "ratio of low resolution pixel dimensions " - << res_x/res_y << std::endl; + << lr_res_x/lr_res_y << std::endl; size_t N = long_range_map.ny*long_range_map.nx; long_range_map.surface_density.resize(N,0); @@ -147,8 +140,13 @@ LensHalo(redshift,c),single_grid(single_grid_mode),cosmo(c),cpfits(fitsfile) size_t kj = 0; size_t jj; + + double res_tmp = resolution / lr_res_y; + //double res_tmp = resolution / res_y; for(size_t j = 0 ; j < Noriginal[1] ; ++j ){ - jj = j*resolution/res_y; + + //jj = j / desample; + jj = j * res_tmp; if( jj >= ny) break; //assert(jj < ny); @@ -167,9 +165,11 @@ LensHalo(redshift,c),single_grid(single_grid_mode),cosmo(c),cpfits(fitsfile) //assert(kj < submap.nx*submap.ny); } - double tmp; + double tmp,res_tmp_x = resolution/lr_res_x; for(size_t i = 0 ; i < Noriginal[0] ; ++i ){ - size_t ii = i*resolution/res_x; + size_t ii = i * res_tmp_x; + //size_t ii = i / desample; + if( ii >= nx) break; tmp = long_range_map.surface_density[ ii + kjj ] += submap.surface_density[ i + kj ]; @@ -523,26 +523,19 @@ void LensMap::read_header(std::string fits_input_file ,double angDist){ CPFITS_READ cpfits(fits_input_file); -// std::auto_ptr ff(new CCfits::FITS (fits_input_file, CCfits::Read)); -// CCfits::PHDU &h0 = ff->pHDU(); - -// h0.readAllKeys(); std::vector size; int bitpix; cpfits.imageInfo(bitpix,size); - assert(size.size() >=2); - //assert(h0.axes() >= 2); - //nx = h0.axis(0); - //ny = h0.axis(1); - + assert(size.size() ==2); + nx = size[0]; ny = size[1]; double phys_res; - if(cpfits.readKey("CD_1",phys_res)){ + if(cpfits.readKey("CD1_1",phys_res)){ std::cerr << "LensMap fits map must have header keywords:" << std::endl << " CD1_1 - length on other side in Mpc/h" << std::endl; //<< " WLOW - closest radial distance in cMpc/h" << std::endl @@ -553,27 +546,6 @@ void LensMap::read_header(std::string fits_input_file phys_res *= degreesTOradians*angDist; boxlMpc = phys_res*nx; - - /* - - try{ - h0.readKey ("CD1_1",phys_res); // resolution in degrees - - phys_res *= degreesTOradians*angDist; - boxlMpc = phys_res*nx; - } - catch(CCfits::HDU::NoSuchKeyword){ - - std::cerr << "LensMap fits map must have header keywords:" << std::endl - << " CD1_1 - length on other side in Mpc/h" << std::endl; - //<< " WLOW - closest radial distance in cMpc/h" << std::endl - //<< " WUP - furthest radial distance in cMpc/h" << std::endl; - - exit(1); - } -*/ - //double phys_res = boxlMpc/nx; - center *= 0; lowerleft = center; lowerleft[0] -= boxlMpc/2; @@ -590,11 +562,6 @@ void LensMap::read(std::string fits_input_file,double angDist){ CPFITS_READ cpfits(fits_input_file); - //std::auto_ptr ff(new CCfits::FITS (fits_input_file, CCfits::Read)); - //CCfits::PHDU &h0 = ff->pHDU(); - - //h0.readAllKeys(); - int n_images = cpfits.get_num_hdus(); std::vector dims; @@ -604,10 +571,6 @@ void LensMap::read(std::string fits_input_file,double angDist){ nx = dims[0]; ny = dims[1]; - // principal HDU is read - //h0.read(surface_density); - //int nhdu = h0.axes(); - if(n_images > 1){ // file contains other lensing quantities assert(n_images == 6); @@ -626,22 +589,6 @@ void LensMap::read(std::string fits_input_file,double angDist){ cpfits.change_hdu(6); cpfits.read(phi_bar,dims); - - //alpha1_bar.resize(size); - //alpha2_bar.resize(size); - //gamma1_bar.resize(size); - //gamma2_bar.resize(size); - //phi_bar.resize(size); - - //CCfits::ExtHDU &h1=ff->extension(1); - //h1.read(alpha1_bar); - //CCfits::ExtHDU &h2=ff->extension(2); - //h2.read(alpha2_bar); - //CCfits::ExtHDU &h3=ff->extension(3); - //h3.read(gamma1_bar); - //CCfits::ExtHDU &h4=ff->extension(4); - //h4.read(gamma2_bar); - //std::cout << h0 << h1 << h2 << h3 << h4 << std::endl; } int err = 0; @@ -780,29 +727,23 @@ void LensMap::read_sub(std::string fits_input_file } */ -//void LensMap::read_sub(CCfits::FITS *ff void LensMap::read_sub(CPFITS_READ &cpfits ,std::vector &first ,std::vector &last ,double angDist ){ - //CCfits::PHDU &h0 = ff->pHDU(); - int bitpix; std::vector sizes; cpfits.imageInfo(bitpix,sizes); //long nx_orig = h0.axis(0); //long ny_orig = h0.axis(1); - - //h0.readAllKeys(); - + // these are always present in each //float wlow,wup,res; float res; cpfits.readKey("CD1_1",res); - //h0.readKey ("CD1_1",res); // resolution in res *= degreesTOradians * angDist; //double boxlMpc_orig = res * nx_orig; @@ -811,16 +752,13 @@ void LensMap::read_sub(CPFITS_READ &cpfits assert(sizes.size() >= 2); std::vector stride = {1,1}; + surface_density.resize( (last[0]-first[0]+1) * (last[1]-first[1]+1) ); // principal HDU is read //std::cout << surface_density.size() << std::endl; //h0.read(surface_density,first,last,stride); cpfits.read_subset(&(surface_density[0]) ,first.data(),last.data()); - float tmp; - cpfits.read_subset(&tmp - ,first.data(),last.data()); - //std::cout << surface_density.size() << std::endl; // set the full field lower left @@ -872,18 +810,17 @@ void LensMap::write(std::string filename std::vector naxex(2); naxex[0]=nx; naxex[1]=ny; + cpfits.write_image(surface_density,naxex); //PHDU *phout=&fout->pHDU(); - cpfits.writeKey("SIDEL1",boxlMpc,"x range in physical Mpc"); cpfits.writeKey("SIDEL2",y_range(),"y range in physical Mpc"); - cpfits.write_image(surface_density,naxex); cpfits.write_image(alpha1_bar,naxex); cpfits.write_image(alpha2_bar,naxex); cpfits.write_image(gamma1_bar,naxex); cpfits.write_image(gamma2_bar,naxex); - cpfits.write_image(phi_bar,naxex); + //cpfits.write_image(phi_bar,naxex); //ExtHDU *eh1=fout->addImage("alpah1", FLOAT_IMG, naxex); //eh1->write(1,nx*ny,alpha1_bar); diff --git a/MultiPlane/particle_lens.cpp b/MultiPlane/particle_lens.cpp index 3ec07e45..41b0523e 100644 --- a/MultiPlane/particle_lens.cpp +++ b/MultiPlane/particle_lens.cpp @@ -12,11 +12,6 @@ #include "H5Cpp.h" #endif -#ifdef ENABLE_FITS -#include -using namespace CCfits; -#endif - // ************************************************************************************* // ******************** methods for MakeParticleLenses ********************************* // ************************************************************************************* diff --git a/include/MOKAlens.h b/include/MOKAlens.h index d75ea601..2b3a76f8 100644 --- a/include/MOKAlens.h +++ b/include/MOKAlens.h @@ -15,10 +15,6 @@ #include -#ifdef ENABLE_FITS -#include -#endif - /** * \brief The MOKA map structure, containing all quantities that define it * diff --git a/include/cpfits.h b/include/cpfits.h index 2e38465e..8e8a0877 100644 --- a/include/cpfits.h +++ b/include/cpfits.h @@ -51,7 +51,7 @@ class CPFITS_BASE{ fits_get_num_hdus(fptr, &hdunum, &status); return hdunum; } - /// returns the current table number, 1... + /// returns the current image / table number, starts with 1 not 0 int get_current_hdu_num(){ int hdunum; fits_get_hdu_num(fptr,&hdunum); @@ -103,6 +103,8 @@ class CPFITS_BASE{ int keysexist; return fits_get_hdrspace(fptr,&keysexist,NULL,&status); } + + /// read a key value for the current table / image int readKey(std::string keyname,double &value){ return fits_read_key(fptr,TDOUBLE,keyname.c_str(), &value,NULL,&status); @@ -253,16 +255,29 @@ class CPFITS_WRITE : public CPFITS_BASE { CPFITS_WRITE operator=(CPFITS_WRITE ); public: - CPFITS_WRITE(std::string filename,bool verbose = false) { - fits_create_file(&fptr, filename.c_str(), &status); + CPFITS_WRITE(std::string filename,bool append = false,bool verbose = false) { + if(!append){ + if(verbose) std::cout << "Creating file : " << filename << std::endl; + if(filename[0] != '!') filename = "!" + filename; + fits_create_file(&fptr, filename.c_str(), &status); + }else{ + fits_open_file(&fptr,filename.c_str(),READWRITE,&status); + if(status==104){ // create file if it does not exist + status = 0; + fits_create_file(&fptr, filename.c_str(), &status); + + if(verbose) std::cout << "Creating file : " << filename << std::endl; + }else{ + if(verbose) std::cout << "Appending to file : " << filename << std::endl; + } + } // print any error messages if (status) fits_report_error(stderr, status); if(verbose){ - std::cout << "Creating file : " << filename << std::endl; - std::cout << " status : " << status << std::endl; + std::cout << " status : " << status << std::endl; } } @@ -313,24 +328,24 @@ class CPFITS_WRITE : public CPFITS_BASE { im.size(),&im[0],&status); } - /// add or replace a key value in the header - int writeKey(std::string &key,double value,std::string &comment ){ + /// add or replace a key value in the header of the current table / image + int writeKey(std::string key,double value,std::string comment ){ return fits_update_key(fptr,TDOUBLE,key.c_str(), &value,comment.c_str(),&status); } - int writeKey(std::string &key,float value,std::string &comment ){ + int writeKey(std::string key,float value,std::string comment ){ return fits_update_key(fptr,TFLOAT,key.c_str(), &value,comment.c_str(),&status); } - int writeKey(std::string &key,int value,std::string &comment ){ + int writeKey(std::string key,int value,std::string comment ){ return fits_update_key(fptr,TINT,key.c_str(), &value,comment.c_str(),&status); } - int writeKey(std::string &key,long value,std::string &comment ){ + int writeKey(std::string key,long value,std::string comment ){ return fits_update_key(fptr,TLONG,key.c_str(), &value,comment.c_str(),&status); } - int writeKey(std::string &key,size_t value,std::string &comment ){ + int writeKey(std::string key,size_t value,std::string comment ){ return fits_update_key(fptr,TULONG,key.c_str(), &value,comment.c_str(),&status); } diff --git a/include/gadget.hh b/include/gadget.hh index ec680ff8..e7041d60 100644 --- a/include/gadget.hh +++ b/include/gadget.hh @@ -80,9 +80,8 @@ private: template GadgetFile::GadgetFile(string inpfn,std::vector &data): -multipleFiles(false),numfiles(0) -,filebasename(inpfn),swap(0),filecnt(0),np_file_start(0),np_file_end(-1), -p_data(data) +multipleFiles(false),numfiles(0),filebasename(inpfn),p_data(data),swap(0) +,filecnt(0),np_file_start(0),np_file_end(-1) { checkMultiple(); } diff --git a/include/image_processing.h b/include/image_processing.h index f10ad451..01f236f1 100644 --- a/include/image_processing.h +++ b/include/image_processing.h @@ -143,8 +143,8 @@ class PixelMap void AssignValue(std::size_t i, double value); void printASCII() const; void printASCIItoFile(std::string filename) const; - void printFITS(std::string filename, bool verbose = false) const; - void printFITS(std::string filename,std::vector> &extra_header_info, bool verbose) const; + void printFITS(std::string filename, bool verbose = false); + void printFITS(std::string filename,std::vector> &extra_header_info, bool verbose); void smooth(double sigma); diff --git a/include/multimap.h b/include/multimap.h index 1304a6ae..1c3df81e 100644 --- a/include/multimap.h +++ b/include/multimap.h @@ -16,10 +16,6 @@ #include -//#ifdef ENABLE_FITS -//#include -//#endif - /** * \brief The MOKA map structure, containing all quantities that define it * @@ -81,10 +77,7 @@ struct LensMap{ // ,double Dist // ); - - //void read_header(std::unique_ptr ff,float h); - //void read_sub(CCfits::FITS *ff - void read_sub(CPFITS_READ &cpfits + void read_sub(CPFITS_READ &cpfits ,std::vector &first ,std::vector &last ,double Dist @@ -241,7 +234,6 @@ class LensHaloMultiMap : public LensHalo bool single_grid; COSMOLOGY &cosmo; - //CCfits::FITS *ff; CPFITS_READ cpfits; double max_pix = std::numeric_limits::lowest(); @@ -250,7 +242,7 @@ class LensHaloMultiMap : public LensHalo double mass_unit; size_t Noriginal[2]; // number of pixels in each dimension in original image - double resolution; // resolution of original image and short rnage image in Mpc + double resolution; // resolution of original image and short rnage image in Mpc long border_width; // width of short range maps padding std::string fitsfilename; From 427491b4a6588d5b8b33ff329a24ce7113311160 Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Mon, 28 Oct 2019 12:20:36 +0100 Subject: [PATCH 194/227] Removed unnecessary error when destructing CPFTS_BASE --- MultiPlane/multimap.cpp | 36 ++++++++++++++++++++++++++++++------ include/cpfits.h | 6 +++--- 2 files changed, 33 insertions(+), 9 deletions(-) diff --git a/MultiPlane/multimap.cpp b/MultiPlane/multimap.cpp index 903a8087..9e3bdecf 100644 --- a/MultiPlane/multimap.cpp +++ b/MultiPlane/multimap.cpp @@ -5,6 +5,7 @@ #include "slsimlib.h" #include "multimap.h" +#include "Tree.h" using namespace std; @@ -275,9 +276,8 @@ void LensHaloMultiMap::submap( LensMap map; - if( (first[0] > 0)*(first[1] > 0)*(last[0] <= Noriginal[0])*(last[1] <= Noriginal[1]) ){ + if( (first[0] > 1)*(first[1] > 1)*(last[0] <= Noriginal[0])*(last[1] <= Noriginal[1]) ){ //map.read_sub(fitsfilename,first,last,getDist()); - //map.read_sub(ff,first,last,getDist()); map.read_sub(cpfits,first,last,getDist()); @@ -289,12 +289,36 @@ void LensHaloMultiMap::submap( // case where subfield overlaps edge std::vector first_sub(2); std::vector last_sub(2); + + map.surface_density.resize(nx_big*ny_big); + + /*////////////////////////////////////////////////////////////// + std::valarray v; + { + first_sub[0] = MAX(first[0],1); + first_sub[1] = MAX(first[1],1); + last_sub[0] = MIN(last[0],Noriginal[0]); + last_sub[1] = MIN(last[1],Noriginal[1]); + + long nx = (last_sub[0]-first_sub[0]+1); + long ny = (last_sub[1]-first_sub[1]+1); + + v.resize(nx*ny); + cpfits.read_subset(&v[0], first_sub.data(), last_sub.data() ); + + for(long i = 0; i < nx ; ++i){ + long ii = i - first[0] + 1; + for(long j = 0; j < ny ; ++j){ + long jj = j - first[1] + 1; + map.surface_density[jj + ii*Noriginal[1]] = v[j+i*ny]; + } + } + } +////////////////////////////////////////////////////////////// */ first_sub[0] = 1; last_sub[0] = Noriginal[0]; - map.surface_density.resize(nx_big*ny_big); - LensMap partmap; const size_t chunk = nx_big*ny_big/Noriginal[0]; @@ -728,8 +752,8 @@ void LensMap::read_sub(std::string fits_input_file */ void LensMap::read_sub(CPFITS_READ &cpfits - ,std::vector &first - ,std::vector &last + ,std::vector &first // 1 to n + ,std::vector &last // 1 to n ,double angDist ){ diff --git a/include/cpfits.h b/include/cpfits.h index 8e8a0877..95a0a081 100644 --- a/include/cpfits.h +++ b/include/cpfits.h @@ -147,8 +147,8 @@ class CPFITS_READ : public CPFITS_BASE { } ~CPFITS_READ(){ - fits_close_file(fptr, &status); if (status) fits_report_error(stderr, status); + fits_close_file(fptr, &status); } CPFITS_READ(CPFITS_READ &&ff):CPFITS_BASE(std::move(ff)){}; @@ -282,9 +282,9 @@ class CPFITS_WRITE : public CPFITS_BASE { } ~CPFITS_WRITE(){ - fits_close_file(fptr, &status); if (status) fits_report_error(stderr, status); - } + fits_close_file(fptr, &status); + } /// add a new image or cube to the file int write_image(std::vector &im,std::vector &size){ From 6fd133953148e51bb3f3d497605162e4e8d5e751 Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Mon, 11 Nov 2019 17:04:38 +0100 Subject: [PATCH 195/227] Changed LensHaloMultiMap to allow long range force map can be saved in another directory. --- MultiPlane/multimap.cpp | 21 +++++++++++++++------ include/multimap.h | 2 ++ 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/MultiPlane/multimap.cpp b/MultiPlane/multimap.cpp index 9e3bdecf..75b81a5c 100644 --- a/MultiPlane/multimap.cpp +++ b/MultiPlane/multimap.cpp @@ -11,14 +11,16 @@ using namespace std; LensHaloMultiMap::LensHaloMultiMap( std::string fitsfile /// Original fits map of the density + ,std::string dir_data ,double redshift ,double mass_unit ,COSMOLOGY &c ,bool subtract_ave ,bool single_grid_mode + ,std::string dir_scratch ): LensHalo(redshift,c),single_grid(single_grid_mode),cosmo(c),cpfits(fitsfile) -,mass_unit(mass_unit),fitsfilename(fitsfile) +,mass_unit(mass_unit),fitsfilename(dir_data + fitsfile) { zerosize = 1; @@ -56,14 +58,20 @@ LensHalo(redshift,c),single_grid(single_grid_mode),cosmo(c),cpfits(fitsfile) Noriginal[0] = submap.nx; Noriginal[1] = submap.ny; - bool long_range_file_exists = Utilities::IO::file_exists(fitsfile + "_lr.fits"); + string lr_file = fitsfile + "_lr.fits"; + if(dir_scratch == ""){ + lr_file = dir_data + lr_file; + }else{ + lr_file = dir_scratch + lr_file; + } + bool long_range_file_exists = Utilities::IO::file_exists(lr_file); - long_range_file_exists = false; // ????? + //long_range_file_exists = false; // ????? if( long_range_file_exists && !single_grid ){ std::cout << " reading file " << fitsfile + "_lr.fits .. " << std::endl; - long_range_map.Myread(fitsfile + "_lr.fits"); + long_range_map.Myread(lr_file); }else if(single_grid){ std::vector first = {1,1}; @@ -201,7 +209,8 @@ LensHalo(redshift,c),single_grid(single_grid_mode),cosmo(c),cpfits(fitsfile) } long_range_map.PreProcessFFTWMap(1.0,wlr); - long_range_map.write("!" + fitsfile + "_lr.fits"); + long_range_map.write("!" + lr_file); + } }; @@ -408,7 +417,7 @@ void LensHaloMultiMap::submap( short_range_map.center = (short_range_map.lowerleft + short_range_map.upperright)/2; short_range_map.boxlMpc = short_range_map.nx*resolution; - if(!single_grid) short_range_map.write("!" + fitsfilename + "_sr.fits"); + //if(!single_grid) short_range_map.write("!" + fitsfilename + "_sr.fits"); } diff --git a/include/multimap.h b/include/multimap.h index 1c3df81e..e6e79b66 100644 --- a/include/multimap.h +++ b/include/multimap.h @@ -112,11 +112,13 @@ class LensHaloMultiMap : public LensHalo LensHaloMultiMap( std::string fitsfile /// Original fits map of the density + ,std::string dir_data ,double redshift ,double mass_unit /// should include h factors ,COSMOLOGY &c ,bool subtract_ave = true /// subtract the average of the full field ,bool single_grid_mode = false + ,std::string dir_scratch = "" /// directory for saving long rang force if different than directory where fitsfile is ); ~LensHaloMultiMap(){ From ca453f815b43d10731d1dd893f19ba637c174beb Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Mon, 11 Nov 2019 18:32:23 +0100 Subject: [PATCH 196/227] paths in MultiMap --- MultiPlane/multimap.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/MultiPlane/multimap.cpp b/MultiPlane/multimap.cpp index 75b81a5c..bf36f7e9 100644 --- a/MultiPlane/multimap.cpp +++ b/MultiPlane/multimap.cpp @@ -19,7 +19,7 @@ LensHaloMultiMap::LensHaloMultiMap( ,bool single_grid_mode ,std::string dir_scratch ): -LensHalo(redshift,c),single_grid(single_grid_mode),cosmo(c),cpfits(fitsfile) +LensHalo(redshift,c),single_grid(single_grid_mode),cosmo(c),cpfits(dir_data + fitsfile) ,mass_unit(mass_unit),fitsfilename(dir_data + fitsfile) { From 793d36d136b3752aed4fef87b1ea009efedbbd35 Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Fri, 15 Nov 2019 11:50:28 +0100 Subject: [PATCH 197/227] Changes to make the multi map zero padded and use less space. --- MultiPlane/multimap.cpp | 27 +++-- include/multimap.h | 225 +++++++++++++++++++++++++++++++++++++++- 2 files changed, 238 insertions(+), 14 deletions(-) diff --git a/MultiPlane/multimap.cpp b/MultiPlane/multimap.cpp index bf36f7e9..3ee1ca5c 100644 --- a/MultiPlane/multimap.cpp +++ b/MultiPlane/multimap.cpp @@ -23,6 +23,8 @@ LensHalo(redshift,c),single_grid(single_grid_mode),cosmo(c),cpfits(dir_data + fi ,mass_unit(mass_unit),fitsfilename(dir_data + fitsfile) { + *** need to pad long range field + zerosize = 1; rscale = 1.0; @@ -101,7 +103,7 @@ LensHalo(redshift,c),single_grid(single_grid_mode),cosmo(c),cpfits(dir_data + fi } if(subtract_ave){ ave /= long_range_map.surface_density.size(); - for(float &p : long_range_map.surface_density){ + for(auto &p : long_range_map.surface_density){ p -= ave; } } @@ -124,7 +126,7 @@ LensHalo(redshift,c),single_grid(single_grid_mode),cosmo(c),cpfits(dir_data + fi // get the ny that makes the pixels clossest to square double Ly = submap.y_range(); long_range_map.ny = (int)( Ly/lr_res_x ); - //long_range_map.ny = (int)( Ly/res_x ) - 1; // ???? + //long_range_map.ny = (int)( Ly/res_x ); // ???? if(Ly - long_range_map.ny*lr_res_x > lr_res_x/2) long_range_map.ny += 1; size_t nx = long_range_map.nx; @@ -203,7 +205,7 @@ LensHalo(redshift,c),single_grid(single_grid_mode),cosmo(c),cpfits(dir_data + fi } if(subtract_ave){ ave /= long_range_map.surface_density.size(); - for(float &p : long_range_map.surface_density){ + for(auto &p : long_range_map.surface_density){ p -= ave; } } @@ -246,8 +248,8 @@ void LensHaloMultiMap::submapPhys(Point_2d ll,Point_2d ur){ lower_left[0] = floor(ll[0]); lower_left[1] = floor(ll[1]); - upper_right[0] = floor(ur[0]) - 1 ; - upper_right[1] = floor(ur[1]) - 1 ; + upper_right[0] = floor(ur[0]) + 1 ; + upper_right[1] = floor(ur[1]) + 1 ; submap(lower_left,upper_right); } @@ -299,9 +301,9 @@ void LensHaloMultiMap::submap( std::vector first_sub(2); std::vector last_sub(2); - map.surface_density.resize(nx_big*ny_big); + map.surface_density.resize(nx_big*ny_big,0); - /*////////////////////////////////////////////////////////////// + ////////////////////////////////////////////////////////////// std::valarray v; { first_sub[0] = MAX(first[0],1); @@ -319,12 +321,12 @@ void LensHaloMultiMap::submap( long ii = i - first[0] + 1; for(long j = 0; j < ny ; ++j){ long jj = j - first[1] + 1; - map.surface_density[jj + ii*Noriginal[1]] = v[j+i*ny]; + map.surface_density[jj + ii*ny_big] = v[j+i*ny]; } } } ////////////////////////////////////////////////////////////// */ - + /* first_sub[0] = 1; last_sub[0] = Noriginal[0]; @@ -365,7 +367,7 @@ void LensHaloMultiMap::submap( max_pix = MAX(max_pix,tmp); min_pix = MIN(min_pix,tmp); } - } + }*/ // need to do overlap region map.boxlMpc = map.nx*resolution; } @@ -379,9 +381,12 @@ void LensHaloMultiMap::submap( p /= area; } - map.PreProcessFFTWMap(1.0,wsr); + *** + //map.PreProcessFFTWMap(1.0,wsr); + map.PreProcessFFTWMap(wsr); //map.PreProcessFFTWMap(1.0,unit); + map.write("test_field.fits"); // cut off bourders size_t nx = short_range_map.nx = upper_right[0] - lower_left[0] + 1; diff --git a/include/multimap.h b/include/multimap.h index e6e79b66..27de0100 100644 --- a/include/multimap.h +++ b/include/multimap.h @@ -35,7 +35,7 @@ struct LensMap{ LensMap& operator=(LensMap &&m); /// values for the map - std::valarray surface_density; // Msun / Mpc^2 + std::valarray surface_density; // Msun / Mpc^2 std::valarray alpha1_bar; std::valarray alpha2_bar; std::valarray gamma1_bar; @@ -95,6 +95,8 @@ struct LensMap{ template void PreProcessFFTWMap(float zerosize,T Wphi_of_k); + template + void PreProcessFFTWMap(T Wphi_of_k); //void PreProcessFFTWMap(float zerosize,std::function Wphi_of_k = identity); #endif @@ -323,8 +325,8 @@ void LensMap::PreProcessFFTWMap(float zerosize,T Wphi_of_k){ std::cout << " 2 error mapping " << ii << " " << jj << std::endl; exit(1); } - //assert(ii+nx*jj < surface_density.size()); - //assert(i+Nnx*j < extended_map.size()); + assert(ii+nx*jj < surface_density.size()); + assert(i+Nnx*j < extended_map.size()); extended_map[i+Nnx*j] = surface_density[ii+nx*jj]; //float tmp = extended_map[i+Nnx*j]; //assert(!isnan(extended_map[i+Nnx*j])); @@ -541,6 +543,223 @@ void LensMap::PreProcessFFTWMap(float zerosize,T Wphi_of_k){ phi_bar.resize(nx*ny); // ??? this needs to be calculated in the future } +/// no padding +template +void LensMap::PreProcessFFTWMap(T Wphi_of_k){ + + assert(surface_density.size() == nx*ny); + + // size of the new map in x and y directions, factor by which each size is increased + //int Nnx=int(zerosize*nx); + //int Nny=int(zerosize*ny); + //double boxlx = boxlMpc*zerosize; + //double boxly = ny*boxlMpc*zerosize/nx; + + //int imin = (Nnx-nx)/2; + //int imax = (Nnx+nx)/2; + //int jmin = (Nny-ny)/2; + //int jmax = (Nny+ny)/2; + + size_t Nkx = (nx/2+1); + + std::vector kxs(Nkx); + for( int i=0; i kys(ny); + for( int j=0; j extended_map( Nnx*Nny ); + fftw_complex *fphi = new fftw_complex[ny*Nkx]; + + **** + //float *fp = &(surface_density[0]); + fftw_plan p = fftw_plan_dft_r2c_2d(ny,nx,&(surface_density[0]) + ,fphi,FFTW_ESTIMATE); + + fftw_execute( p ); + + // fourier space + // std:: cout << " allocating fourier space maps " << std:: endl; + + // build modes for each pixel in the fourier space + for( int i=0; i fft( Nny*(Nkx) ); + std::vector realsp(nx*ny); + + fftw_plan pp = fftw_plan_dft_c2r_2d(ny,nx,fft,realsp.data(),FFTW_MEASURE); + + // alpha1 + { + + // build modes for each pixel in the fourier space + for( int i=0; i Date: Fri, 15 Nov 2019 12:58:40 +0100 Subject: [PATCH 198/227] Put padding on long range grid. --- MultiPlane/multimap.cpp | 12 +++++++----- include/multimap.h | 1 - 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/MultiPlane/multimap.cpp b/MultiPlane/multimap.cpp index 3ee1ca5c..74769d28 100644 --- a/MultiPlane/multimap.cpp +++ b/MultiPlane/multimap.cpp @@ -22,9 +22,7 @@ LensHaloMultiMap::LensHaloMultiMap( LensHalo(redshift,c),single_grid(single_grid_mode),cosmo(c),cpfits(dir_data + fitsfile) ,mass_unit(mass_unit),fitsfilename(dir_data + fitsfile) { - - *** need to pad long range field - + zerosize = 1; rscale = 1.0; @@ -203,6 +201,7 @@ LensHalo(redshift,c),single_grid(single_grid_mode),cosmo(c),cpfits(dir_data + fi p /= area; ave += p; } + if(subtract_ave){ ave /= long_range_map.surface_density.size(); for(auto &p : long_range_map.surface_density){ @@ -210,7 +209,10 @@ LensHalo(redshift,c),single_grid(single_grid_mode),cosmo(c),cpfits(dir_data + fi } } - long_range_map.PreProcessFFTWMap(1.0,wlr); + double padd = 1 + 2 * border_width * resolution / long_range_map.x_range(); + padd = MAX(padd,2); + + long_range_map.PreProcessFFTWMap(padd,wlr); long_range_map.write("!" + lr_file); } @@ -381,7 +383,7 @@ void LensHaloMultiMap::submap( p /= area; } - *** + //*** ??? //map.PreProcessFFTWMap(1.0,wsr); map.PreProcessFFTWMap(wsr); //map.PreProcessFFTWMap(1.0,unit); diff --git a/include/multimap.h b/include/multimap.h index 27de0100..3e71a971 100644 --- a/include/multimap.h +++ b/include/multimap.h @@ -576,7 +576,6 @@ void LensMap::PreProcessFFTWMap(T Wphi_of_k){ //std::vector extended_map( Nnx*Nny ); fftw_complex *fphi = new fftw_complex[ny*Nkx]; - **** //float *fp = &(surface_density[0]); fftw_plan p = fftw_plan_dft_r2c_2d(ny,nx,&(surface_density[0]) ,fphi,FFTW_ESTIMATE); From 062d8f1921186d1f4e8d5636c2cce9535e75a6ba Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Sun, 17 Nov 2019 14:54:11 +0100 Subject: [PATCH 199/227] Fixed problem with striping in gamma_1 --- MultiPlane/multimap.cpp | 85 +++++++++++++++++--------------- include/cpfits.h | 2 +- include/multimap.h | 105 +++++++++++++++++----------------------- 3 files changed, 91 insertions(+), 101 deletions(-) diff --git a/MultiPlane/multimap.cpp b/MultiPlane/multimap.cpp index 74769d28..f5fc7f0b 100644 --- a/MultiPlane/multimap.cpp +++ b/MultiPlane/multimap.cpp @@ -20,7 +20,7 @@ LensHaloMultiMap::LensHaloMultiMap( ,std::string dir_scratch ): LensHalo(redshift,c),single_grid(single_grid_mode),cosmo(c),cpfits(dir_data + fitsfile) -,mass_unit(mass_unit),fitsfilename(dir_data + fitsfile) +,ave_ang_sd(0),mass_unit(mass_unit),fitsfilename(dir_data + fitsfile) { zerosize = 1; @@ -70,8 +70,13 @@ LensHalo(redshift,c),single_grid(single_grid_mode),cosmo(c),cpfits(dir_data + fi if( long_range_file_exists && !single_grid ){ - std::cout << " reading file " << fitsfile + "_lr.fits .. " << std::endl; + std::cout << " reading file " << lr_file << " .. " << std::endl; long_range_map.Myread(lr_file); + CPFITS_READ cpfits(lr_file); + if(cpfits.readKey("ave_ang_sd", ave_ang_sd)){ + std::cerr << "need ave_ang_sd in " << lr_file << std::endl; + throw std::invalid_argument("need ave_ang_sd in "); + } }else if(single_grid){ std::vector first = {1,1}; @@ -89,20 +94,20 @@ LensHalo(redshift,c),single_grid(single_grid_mode),cosmo(c),cpfits(dir_data + fi //long_range_map.boxlMpc = submap.boxlMpc; double area = long_range_map.x_resolution() - *long_range_map.y_resolution()/mass_unit; //*** units ??? + *long_range_map.y_resolution()/mass_unit; //double area = 1.0/mass_unit; // convert to - double ave = 0; + ave_ang_sd = 0; for(auto &p : long_range_map.surface_density){ p /= area; - ave += p; + ave_ang_sd += p; } - if(subtract_ave){ - ave /= long_range_map.surface_density.size(); - for(auto &p : long_range_map.surface_density){ - p -= ave; + ave_ang_sd /= long_range_map.surface_density.size(); + if(subtract_ave){ + for(auto &p : long_range_map.surface_density){ + p -= ave_ang_sd; } } @@ -124,7 +129,7 @@ LensHalo(redshift,c),single_grid(single_grid_mode),cosmo(c),cpfits(dir_data + fi // get the ny that makes the pixels clossest to square double Ly = submap.y_range(); long_range_map.ny = (int)( Ly/lr_res_x ); - //long_range_map.ny = (int)( Ly/res_x ); // ???? + if(Ly - long_range_map.ny*lr_res_x > lr_res_x/2) long_range_map.ny += 1; size_t nx = long_range_map.nx; @@ -180,6 +185,7 @@ LensHalo(redshift,c),single_grid(single_grid_mode),cosmo(c),cpfits(dir_data + fi //size_t ii = i / desample; if( ii >= nx) break; + tmp = long_range_map.surface_density[ ii + kjj ] += submap.surface_density[ i + kj ]; max_pix = MAX(max_pix,tmp); @@ -188,24 +194,21 @@ LensHalo(redshift,c),single_grid(single_grid_mode),cosmo(c),cpfits(dir_data + fi } assert(jj == ny-1); - // double area = pow(long_range_map.boxlMpc/long_range_map.nx,2)/mass_unit; //*** units ??? - //double area = long_range_map.x_resolution() - //*long_range_map.y_resolution()/mass_unit/resolution/resolution; //*** units ??? double area = long_range_map.x_resolution() - *long_range_map.y_resolution()/mass_unit; //*** units ??? - //double area = 1.0/mass_unit; - - // convert to - double ave = 0; + *long_range_map.y_resolution()/mass_unit; + + // convert to surface density + ave_ang_sd = 0; for(auto &p : long_range_map.surface_density){ p /= area; - ave += p; + ave_ang_sd += p; } - + + ave_ang_sd /= long_range_map.surface_density.size(); + if(subtract_ave){ - ave /= long_range_map.surface_density.size(); for(auto &p : long_range_map.surface_density){ - p -= ave; + p -= ave_ang_sd; } } @@ -215,6 +218,8 @@ LensHalo(redshift,c),single_grid(single_grid_mode),cosmo(c),cpfits(dir_data + fi long_range_map.PreProcessFFTWMap(padd,wlr); long_range_map.write("!" + lr_file); + CPFITS_WRITE tmp_cpfits(lr_file,true); + tmp_cpfits.writeKey("ave_ang_sd", ave_ang_sd,"average angulare density"); } }; @@ -288,26 +293,35 @@ void LensHaloMultiMap::submap( last[1] = upper_right[1] + border_width + 1; LensMap map; + double area = resolution*resolution/mass_unit; if( (first[0] > 1)*(first[1] > 1)*(last[0] <= Noriginal[0])*(last[1] <= Noriginal[1]) ){ //map.read_sub(fitsfilename,first,last,getDist()); //map.read_sub(ff,first,last,getDist()); map.read_sub(cpfits,first,last,getDist()); + + // convert to relative surface angular surface density + for(auto &p : map.surface_density){ + p /= area; + p -= ave_ang_sd; + } + }else{ + // case where subfield overlaps edge + size_t nx_big = map.nx = last[0] - first[0] + 1; size_t ny_big = map.ny = last[1] - first[1] + 1; - // case where subfield overlaps edge std::vector first_sub(2); std::vector last_sub(2); - map.surface_density.resize(nx_big*ny_big,0); + map.surface_density.resize(nx_big * ny_big,0); ////////////////////////////////////////////////////////////// - std::valarray v; { + std::valarray v; first_sub[0] = MAX(first[0],1); first_sub[1] = MAX(first[1],1); last_sub[0] = MIN(last[0],Noriginal[0]); @@ -323,7 +337,7 @@ void LensHaloMultiMap::submap( long ii = i - first[0] + 1; for(long j = 0; j < ny ; ++j){ long jj = j - first[1] + 1; - map.surface_density[jj + ii*ny_big] = v[j+i*ny]; + map.surface_density[jj + ii*ny_big] = v[j + i*ny] / area - ave_ang_sd;; } } } @@ -373,23 +387,16 @@ void LensHaloMultiMap::submap( // need to do overlap region map.boxlMpc = map.nx*resolution; } - - - double area = resolution*resolution/mass_unit; //*** units ??? - // convert to - //double area = 1.0/mass_unit; //*** units ??? - // convert to - for(auto &p : map.surface_density){ - p /= area; - } + - //*** ??? + std::cout << "density (0,0) " << map.surface_density[0] << std::endl; //map.PreProcessFFTWMap(1.0,wsr); map.PreProcessFFTWMap(wsr); //map.PreProcessFFTWMap(1.0,unit); - + std::cout << "density (0,0) " << map.surface_density[0] << std::endl; + map.write("test_field.fits"); - // cut off bourders + // cut off bounders size_t nx = short_range_map.nx = upper_right[0] - lower_left[0] + 1; size_t ny = short_range_map.ny = upper_right[1] - lower_left[1] + 1; @@ -464,7 +471,7 @@ bool LensMap::evaluate(const double *x,float &sigma,float *gamma,double *alpha) + c * alpha2_bar[index+1+nx] + d * alpha2_bar[index+nx]; gamma[0] = a * gamma1_bar[index] + b * gamma1_bar[index+1] - + c * alpha1_bar[index+1+nx] + d * alpha1_bar[index+nx]; + + c * gamma1_bar[index+1+nx] + d * gamma1_bar[index+nx]; gamma[1] = a * gamma2_bar[index] + b * gamma2_bar[index+1] + c * gamma2_bar[index+1+nx] + d * gamma2_bar[index+nx]; gamma[2] = 0.0; diff --git a/include/cpfits.h b/include/cpfits.h index 95a0a081..3e5a5dca 100644 --- a/include/cpfits.h +++ b/include/cpfits.h @@ -104,7 +104,7 @@ class CPFITS_BASE{ return fits_get_hdrspace(fptr,&keysexist,NULL,&status); } - /// read a key value for the current table / image + /// read a key value for the current table / image, returns 0 if the key word does not exit int readKey(std::string keyname,double &value){ return fits_read_key(fptr,TDOUBLE,keyname.c_str(), &value,NULL,&status); diff --git a/include/multimap.h b/include/multimap.h index 3e71a971..285ab583 100644 --- a/include/multimap.h +++ b/include/multimap.h @@ -204,6 +204,7 @@ class LensHaloMultiMap : public LensHalo unit = m.unit; wsr = m.wsr; wlr = m.wlr; + ave_ang_sd = m.ave_ang_sd; } LensHaloMultiMap(LensHaloMultiMap &&m): @@ -228,6 +229,8 @@ class LensHaloMultiMap : public LensHalo unit = m.unit; wsr = m.wsr; wlr = m.wlr; + ave_ang_sd = m.ave_ang_sd; + } public: @@ -240,6 +243,8 @@ class LensHaloMultiMap : public LensHalo COSMOLOGY &cosmo; CPFITS_READ cpfits; + double ave_ang_sd; + double max_pix = std::numeric_limits::lowest(); double min_pix = std::numeric_limits::max(); @@ -287,6 +292,7 @@ void LensMap::PreProcessFFTWMap(float zerosize,T Wphi_of_k){ // size of the new map in x and y directions, factor by which each size is increased int Nnx=int(zerosize*nx); int Nny=int(zerosize*ny); + size_t NN = Nnx*Nny; double boxlx = boxlMpc*zerosize; double boxly = ny*boxlMpc*zerosize/nx; @@ -297,18 +303,19 @@ void LensMap::PreProcessFFTWMap(float zerosize,T Wphi_of_k){ size_t Nkx = (Nnx/2+1); + double tmp = 2.*M_PI/boxlx; std::vector kxs(Nkx); for( int i=0; i kys(Nny); for( int j=0; j extended_map( Nnx*Nny ); + std::vector extended_map( NN ); // assume locate in a rectangular map and build up the new one for( int j=0; j kxs(Nkx); for( int i=0; i kys(ny); for( int j=0; j fft( Nny*(Nkx) ); - std::vector realsp(nx*ny); + std::vector realsp(NN); fftw_plan pp = fftw_plan_dft_c2r_2d(ny,nx,fft,realsp.data(),FFTW_MEASURE); @@ -632,7 +638,7 @@ void LensMap::PreProcessFFTWMap(T Wphi_of_k){ for( int i=0; i Date: Sun, 17 Nov 2019 15:12:44 +0100 Subject: [PATCH 200/227] Removed some test output lines. --- MultiPlane/multimap.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/MultiPlane/multimap.cpp b/MultiPlane/multimap.cpp index f5fc7f0b..7b431bb0 100644 --- a/MultiPlane/multimap.cpp +++ b/MultiPlane/multimap.cpp @@ -389,13 +389,13 @@ void LensHaloMultiMap::submap( } - std::cout << "density (0,0) " << map.surface_density[0] << std::endl; + //std::cout << "density (0,0) " << map.surface_density[0] << std::endl; //map.PreProcessFFTWMap(1.0,wsr); map.PreProcessFFTWMap(wsr); //map.PreProcessFFTWMap(1.0,unit); - std::cout << "density (0,0) " << map.surface_density[0] << std::endl; + //std::cout << "density (0,0) " << map.surface_density[0] << std::endl; - map.write("test_field.fits"); + //map.write("test_field.fits"); // cut off bounders size_t nx = short_range_map.nx = upper_right[0] - lower_left[0] + 1; From 09a55e7c9cd45aceb2870f47290a3b2bebf5b41f Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Mon, 18 Nov 2019 21:44:16 +0100 Subject: [PATCH 201/227] Changed PixelMap constructor from file to read RA and DEC keywords. --- ImageProcessing/pixelize.cpp | 13 +++++++++--- MultiPlane/multimap.cpp | 38 +++++++++++++++++++++++++++--------- include/multimap.h | 30 +++++++++++++++------------- 3 files changed, 55 insertions(+), 26 deletions(-) diff --git a/ImageProcessing/pixelize.cpp b/ImageProcessing/pixelize.cpp index aa75bce0..8357d5a9 100644 --- a/ImageProcessing/pixelize.cpp +++ b/ImageProcessing/pixelize.cpp @@ -176,8 +176,14 @@ PixelMap::PixelMap( int err = 0; + err += cpfits.readKey("RA", center[0]); + err += cpfits.readKey("DEC", center[1]); + + if(err != 0){ + err = 0; err += cpfits.readKey("CRVAL1", center[0]); err += cpfits.readKey("CRVAL2", center[1]); + } if(err != 0) { @@ -876,8 +882,8 @@ void PixelMap::printFITS(std::string filename, bool verbose) cpfits.writeKey("Nx", Nx, ""); cpfits.writeKey("Ny", Ny, ""); cpfits.writeKey("range x", map_boundary_p2[0]-map_boundary_p1[0], "radians"); - cpfits.writeKey("RA", center[0], "radians"); - cpfits.writeKey("DEC", center[1], "radians"); + cpfits.writeKey("RA", center[0], "radians, center"); + cpfits.writeKey("DEC", center[1], "radians, center"); } @@ -2147,8 +2153,9 @@ void PixelMap::copy_in( } void PixelMap::paste(const PixelMap& pmap){ - if(resolution < pmap.resolution){ + if(resolution < pmap.resolution * 0.9){ // ????? std::cerr << "PixeLMap::paste() resolution of image pasted in must of equal or higher resolution" << std::endl; + std::cerr << resolution << " " << pmap.resolution << " dres/res " << (pmap.resolution-pmap.resolution)/pmap.resolution << std::endl; throw std::invalid_argument("low resolution"); } diff --git a/MultiPlane/multimap.cpp b/MultiPlane/multimap.cpp index 7b431bb0..922c6fa7 100644 --- a/MultiPlane/multimap.cpp +++ b/MultiPlane/multimap.cpp @@ -66,7 +66,7 @@ LensHalo(redshift,c),single_grid(single_grid_mode),cosmo(c),cpfits(dir_data + fi } bool long_range_file_exists = Utilities::IO::file_exists(lr_file); - //long_range_file_exists = false; // ????? + ///long_range_file_exists = false; if( long_range_file_exists && !single_grid ){ @@ -310,7 +310,6 @@ void LensHaloMultiMap::submap( }else{ // case where subfield overlaps edge - size_t nx_big = map.nx = last[0] - first[0] + 1; size_t ny_big = map.ny = last[1] - first[1] + 1; @@ -326,18 +325,20 @@ void LensHaloMultiMap::submap( first_sub[1] = MAX(first[1],1); last_sub[0] = MIN(last[0],Noriginal[0]); last_sub[1] = MIN(last[1],Noriginal[1]); + + Point_2d offset(first_sub[0] - first[0] , first_sub[1] - first[1] ); - long nx = (last_sub[0]-first_sub[0]+1); - long ny = (last_sub[1]-first_sub[1]+1); + long nx = last_sub[0] - first_sub[0] + 1; + long ny = last_sub[1] - first_sub[1] + 1; v.resize(nx*ny); cpfits.read_subset(&v[0], first_sub.data(), last_sub.data() ); - for(long i = 0; i < nx ; ++i){ - long ii = i - first[0] + 1; - for(long j = 0; j < ny ; ++j){ - long jj = j - first[1] + 1; - map.surface_density[jj + ii*ny_big] = v[j + i*ny] / area - ave_ang_sd;; + long jj = offset[1]; + for(long j = 0; j < ny ; ++j,++jj){ + long ii = offset[0]; + for(long i = 0; i < nx ; ++i,++ii){ + map.surface_density[ii + jj*nx_big] = v[i + j*nx] / area - ave_ang_sd;; } } } @@ -431,6 +432,7 @@ void LensHaloMultiMap::submap( short_range_map.center = (short_range_map.lowerleft + short_range_map.upperright)/2; short_range_map.boxlMpc = short_range_map.nx*resolution; + for(auto g : short_range_map.gamma2_bar) assert( !isnan(g) ); // ????? //if(!single_grid) short_range_map.write("!" + fitsfilename + "_sr.fits"); } @@ -461,6 +463,8 @@ bool LensMap::evaluate(const double *x,float &sigma,float *gamma,double *alpha) double c = fx*fy; double d = (1-fx)*fy; + //for(auto g : gamma2_bar ) assert(!isnan(g)); // ????? + // bilinear interpolation sigma = a * surface_density[index] + b * surface_density[index+1] + c * surface_density[index+1+nx] + d * surface_density[index+nx]; @@ -476,6 +480,18 @@ bool LensMap::evaluate(const double *x,float &sigma,float *gamma,double *alpha) + c * gamma2_bar[index+1+nx] + d * gamma2_bar[index+nx]; gamma[2] = 0.0; + // ??? + if(isnan(gamma[1])){ + std::cerr << index+1+nx << " < " << gamma2_bar.size() << std::endl; + std::cerr << alpha[0] << " " << alpha[1] << std::endl; + std::cerr << gamma[0] << " " << gamma[1] << " " << gamma[2] << std::endl; + + std::cerr << gamma2_bar[index] << " " << gamma2_bar[index+1] + << " " << gamma2_bar[index+1+nx] << " " << gamma2_bar[index+nx] << std::endl; + + + assert(!isnan(gamma[1])); + } return false; } @@ -510,10 +526,14 @@ void LensHaloMultiMap::force_halo(double *alpha long_range_map.evaluate(xx,*kappa,gamma,alpha); + assert(gamma[0] == gamma[0] && gamma[1] == gamma[1]); /// ???? + float t_kappa,t_gamma[3]; double t_alpha[2]; short_range_map.evaluate(xx,t_kappa,t_gamma,t_alpha); + assert(t_gamma[0] == t_gamma[0] && t_gamma[1] == t_gamma[1]); /// ???? + alpha[0] += t_alpha[0]; alpha[1] += t_alpha[1]; gamma[0] += t_gamma[0]; diff --git a/include/multimap.h b/include/multimap.h index 285ab583..94cd655d 100644 --- a/include/multimap.h +++ b/include/multimap.h @@ -182,7 +182,8 @@ class LensHaloMultiMap : public LensHalo double getMax() const {return max_pix;} double getMin() const {return min_pix;} - + double getResolutionMpc() const {return resolution;} + void operator =(LensHaloMultiMap &&m){ LensHalo::operator=(std::move(m)); cosmo = m.cosmo; @@ -251,7 +252,7 @@ class LensHaloMultiMap : public LensHalo double mass_unit; size_t Noriginal[2]; // number of pixels in each dimension in original image - double resolution; // resolution of original image and short rnage image in Mpc + double resolution; // resolution of original image and short range image in Mpc long border_width; // width of short range maps padding std::string fitsfilename; @@ -336,10 +337,10 @@ void LensMap::PreProcessFFTWMap(float zerosize,T Wphi_of_k){ assert(i+Nnx*j < extended_map.size()); extended_map[i+Nnx*j] = surface_density[ii+nx*jj]; //float tmp = extended_map[i+Nnx*j]; - //assert(!isnan(extended_map[i+Nnx*j])); - //if(isinf(extended_map[i+Nnx*j])){ - // extended_map[i+Nnx*j] = 0; - //} + assert(!isnan(extended_map[i+Nnx*j])); + if(isinf(extended_map[i+Nnx*j])){ + extended_map[i+Nnx*j] = 0; + } }else{ extended_map[i+Nnx*j] = 0; } @@ -414,7 +415,7 @@ void LensMap::PreProcessFFTWMap(float zerosize,T Wphi_of_k){ fftw_execute( pp ); - alpha1_bar.resize(nx*ny); + alpha1_bar.resize(nx*ny,0); for( int j=jmin; j Date: Fri, 22 Nov 2019 11:39:32 +0100 Subject: [PATCH 202/227] Made a more efficient method PixleMap:PowerSpectrum() --- TreeCode_link/utilities.cpp | 115 ++++++++++++++++++++++++++++++++++-- include/image_processing.h | 4 +- include/utilities_slsim.h | 11 +++- 3 files changed, 122 insertions(+), 8 deletions(-) diff --git a/TreeCode_link/utilities.cpp b/TreeCode_link/utilities.cpp index 5440d22d..5f424919 100644 --- a/TreeCode_link/utilities.cpp +++ b/TreeCode_link/utilities.cpp @@ -431,15 +431,15 @@ unsigned long prevpower(unsigned long k){ ,double zeropaddingfactor ) { - // go in the fourir space doing the zero padding + // go in the fourier space doing the zero padding int zerosize = 4; int nl = ll.size(); Pl.resize(nl); // size of the new map in x and y directions, factor by which each size is increased - int Nnx=int(zerosize*nx); - int Nny=int(zerosize*ny); + size_t Nnx=int(zerosize*nx); + size_t Nny=int(zerosize*ny); double Nboxlx = boxlx*zerosize; double Nboxly = boxly*zerosize; @@ -477,7 +477,7 @@ unsigned long prevpower(unsigned long k){ fftw_complex *fNb=new fftw_complex[Nny*(Nnx/2+1)]; fftw_complex *Nfcc=new fftw_complex[Nny*(Nnx/2+1)]; //size_t *ik=new size_t[]; - fftw_complex *output=new fftw_complex[Nny*(Nnx/2+1)]; + //fftw_complex *output=new fftw_complex[Nny*(Nnx/2+1)]; for(int i=0;i &aa + ,int nx + ,int ny + ,double boxlx + ,double boxly + ,std::vector &ll + ,std::vector &Pl + ) + { + int nl = ll.size(); + Pl.resize(nl); + + // size of the new map in x and y directions, factor by which each size is increased + //size_t Nnx=int(zerosize*nx); + //size_t Nny=int(zerosize*ny); + //double Nboxlx = boxlx*zerosize; + //double Nboxly = boxly*zerosize; + + + // estimate the fourier transform + // now we go in the Fourier Space + //double *dNa=new double[nx*ny]; + //double *dNb=new double[nx*ny]; + + fftw_complex *fNa=new fftw_complex[ny*(nx/2+1)]; + //fftw_complex *fNb=new fftw_complex[ny*(nx/2+1)]; + //fftw_complex *Nfcc=new fftw_complex[ny*(nx/2+1)]; + //size_t *ik=new size_t[]; + //fftw_complex *output=new fftw_complex[ny*(nx/2+1)]; + + fftw_plan p1 = fftw_plan_dft_r2c_2d(ny,nx,&(aa[0]),fNa,FFTW_ESTIMATE); + fftw_execute( p1 ); + fftw_destroy_plan(p1); + + //double *ks=new double[ny*(nx/2+1)]; + std::vector ks(ny*(nx/2+1)); + + // fb and fa are then used to compute the power spectrum + // build modes + for( int i=0; i ik(ny*(nx/2+1)); + Utilities::sort_indexes(ks,ik); + + double *nk=new double[ny*(nx/2+1)]; + double *nc=new double[ny*(nx/2+1)]; + // sorted vectors + for(int i=0; i bink(nl); + // build the binned power spectrum + Utilities::fill_linear(bink,nl,log10(nk[1]),log10(nk[ny*(nx/2+1)-1])); + double lk1,lk2; + for(int i=0;i=lk1 && log10(nk[j])0){ + Pl[i]=Pl[i]/double(nin); + ll[i]=ll[i]/double(nin); + } + } + //delete [] dNa; + //delete [] dNb; + //delete [] output; + delete [] fNa; + //delete [] fNb; + //delete [] Nfcc; + delete [] nk; + delete [] nc; + } + #endif std::valarray AdaptiveSmooth(const std::valarray &map_in,size_t Nx,size_t Ny,double value){ diff --git a/include/image_processing.h b/include/image_processing.h index 01f236f1..744fec63 100644 --- a/include/image_processing.h +++ b/include/image_processing.h @@ -235,10 +235,10 @@ class PixelMap if(power_spectrum.size() != lvec.size()) throw std::invalid_argument("these must be the same size"); - if(overwrite) Utilities::powerspectrum2d(map,map,Nx,Ny,rangeX,rangeY, lvec, power_spectrum); + if(overwrite) Utilities::powerspectrum2d(map,Nx,Ny,rangeX,rangeY, lvec, power_spectrum); else{ std::vector tmp_power(power_spectrum.size()); - Utilities::powerspectrum2d(map,map,Nx,Ny,rangeX,rangeY, lvec, tmp_power); + Utilities::powerspectrum2d(map,Nx,Ny,rangeX,rangeY, lvec, tmp_power); for(size_t ii=0;ii &Pl /// output binned power spectrum ,double zeropaddingfactor = 4 ); + void powerspectrum2d( + std::valarray &aa /// first realspace map to be + ,int nx /// number of pixels in x direction + ,int ny /// number of pixels in y direction + ,double boxlx /// range of image in x direction + ,double boxly /// range of image in y direction + ,std::vector &ll /// output multiplot number of bins + ,std::vector &Pl /// output binned power spectrum + ); #endif @@ -1668,7 +1677,7 @@ namespace Utilities data.push_back(&v1); data.push_back(&v2); data.push_back(&v3); - printCSV(std::cout,header,data); + writeCSV(filename,header,data);

**/ From 39cc8f9f1b2859fd1f1e1acded062995225c33b5 Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Fri, 22 Nov 2019 11:40:52 +0100 Subject: [PATCH 203/227] Added angular resolution as a member of LensHaloMultiMap --- ImageProcessing/pixelize.cpp | 2 +- MultiPlane/multimap.cpp | 19 ++++++++++++------- include/multimap.h | 15 +++++++++++---- 3 files changed, 24 insertions(+), 12 deletions(-) diff --git a/ImageProcessing/pixelize.cpp b/ImageProcessing/pixelize.cpp index 8357d5a9..ae202058 100644 --- a/ImageProcessing/pixelize.cpp +++ b/ImageProcessing/pixelize.cpp @@ -2153,7 +2153,7 @@ void PixelMap::copy_in( } void PixelMap::paste(const PixelMap& pmap){ - if(resolution < pmap.resolution * 0.9){ // ????? + if(resolution < pmap.resolution){ std::cerr << "PixeLMap::paste() resolution of image pasted in must of equal or higher resolution" << std::endl; std::cerr << resolution << " " << pmap.resolution << " dres/res " << (pmap.resolution-pmap.resolution)/pmap.resolution << std::endl; throw std::invalid_argument("low resolution"); diff --git a/MultiPlane/multimap.cpp b/MultiPlane/multimap.cpp index 922c6fa7..19960bd6 100644 --- a/MultiPlane/multimap.cpp +++ b/MultiPlane/multimap.cpp @@ -50,6 +50,8 @@ LensHalo(redshift,c),single_grid(single_grid_mode),cosmo(c),cpfits(dir_data + fi wlr.rs2 = wsr.rs2 = rs2; resolution = submap.x_resolution(); + angular_resolution = submap.angular_pixel_size; + //border_width = 4.5*sqrt(rs2)/res + 1; border_width = ffactor * sqrt(rs2) / resolution + 1; @@ -565,6 +567,7 @@ LensMap::LensMap(LensMap &&m){ center = m.center; lowerleft = m.lowerleft; upperright = m.upperright; + angular_pixel_size = m.angular_pixel_size; } LensMap& LensMap::operator=(LensMap &&m){ @@ -583,6 +586,8 @@ LensMap& LensMap::operator=(LensMap &&m){ center = m.center; lowerleft = m.lowerleft; upperright = m.upperright; + angular_pixel_size = m.angular_pixel_size; + return *this; } @@ -602,15 +607,14 @@ void LensMap::read_header(std::string fits_input_file double phys_res; - if(cpfits.readKey("CD1_1",phys_res)){ + if(cpfits.readKey("CD1_1",angular_pixel_size)){ std::cerr << "LensMap fits map must have header keywords:" << std::endl - << " CD1_1 - length on other side in Mpc/h" << std::endl; - //<< " WLOW - closest radial distance in cMpc/h" << std::endl - //<< " WUP - furthest radial distance in cMpc/h" << std::endl; + << " CD1_1 - angular resolution must exit" << std::endl; exit(1); } - phys_res *= degreesTOradians*angDist; + angular_pixel_size *= degreesTOradians; + phys_res = angular_pixel_size*angDist; boxlMpc = phys_res*nx; center *= 0; @@ -662,9 +666,10 @@ void LensMap::read(std::string fits_input_file,double angDist){ { /* these are always present in ea*/ float res; - err += cpfits.readKey ("CD1_1",res); // angular resolution degrees + err += cpfits.readKey ("CD1_1",angular_pixel_size); // angular resolution degrees - res *= degreesTOradians*angDist; + angular_pixel_size *= degreesTOradians; + res = angular_pixel_size*angDist; boxlMpc = res * nx; } if(err != 0){ diff --git a/include/multimap.h b/include/multimap.h index 94cd655d..f3d0c6e8 100644 --- a/include/multimap.h +++ b/include/multimap.h @@ -28,7 +28,7 @@ */ struct LensMap{ - LensMap():nx(0),ny(0),boxlMpc(0){}; + LensMap():nx(0),ny(0),boxlMpc(0),angular_pixel_size(0){}; // move operators LensMap(LensMap &&m); @@ -43,13 +43,16 @@ struct LensMap{ std::valarray phi_bar; int nx,ny; double boxlMpc; + double angular_pixel_size; // in radians Point_2d center; - Point_2d lowerleft; - Point_2d upperright; + Point_2d lowerleft; /// boundery with centred grid + Point_2d upperright; /// double x_resolution(){return boxlMpc / nx ;} double y_resolution(){return (upperright[1]-lowerleft[1])/ny;} + // # of pixels times resolution double x_range(){return boxlMpc;} + // # of pixels times resolution double y_range(){return (upperright[1]-lowerleft[1]);} bool evaluate(const double *x,float &sigma,float *gamma,double *alpha); @@ -183,7 +186,8 @@ class LensHaloMultiMap : public LensHalo double getMax() const {return max_pix;} double getMin() const {return min_pix;} double getResolutionMpc() const {return resolution;} - + double getResolutionAngular() const {return angular_resolution;} + void operator =(LensHaloMultiMap &&m){ LensHalo::operator=(std::move(m)); cosmo = m.cosmo; @@ -198,6 +202,7 @@ class LensHaloMultiMap : public LensHalo Noriginal[0] = m.Noriginal[0]; Noriginal[1] = m.Noriginal[1]; resolution = m.resolution; + angular_resolution = m.angular_resolution; border_width = m.border_width; fitsfilename = m.fitsfilename; rs2 = m.rs2; @@ -223,6 +228,7 @@ class LensHaloMultiMap : public LensHalo Noriginal[0] = m.Noriginal[0]; Noriginal[1] = m.Noriginal[1]; resolution = m.resolution; + angular_resolution = m.angular_resolution; border_width = m.border_width; fitsfilename = m.fitsfilename; rs2 = m.rs2; @@ -253,6 +259,7 @@ class LensHaloMultiMap : public LensHalo size_t Noriginal[2]; // number of pixels in each dimension in original image double resolution; // resolution of original image and short range image in Mpc + double angular_resolution; // angular resolution of original image long border_width; // width of short range maps padding std::string fitsfilename; From c58dd30a142b4aa77973e54a8140c908b4844aa0 Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Sat, 23 Nov 2019 18:12:49 +0100 Subject: [PATCH 204/227] Improved PixelMap::PowerSpectrum() to make it more efficient. --- ImageProcessing/pixelize.cpp | 4 ++-- TreeCode_link/utilities.cpp | 35 +++++++++-------------------------- 2 files changed, 11 insertions(+), 28 deletions(-) diff --git a/ImageProcessing/pixelize.cpp b/ImageProcessing/pixelize.cpp index ae202058..e55b6f1d 100644 --- a/ImageProcessing/pixelize.cpp +++ b/ImageProcessing/pixelize.cpp @@ -198,7 +198,7 @@ PixelMap::PixelMap( double cdelt2; err += cpfits.readKey("CDELT1", my_res); err += cpfits.readKey("CDELT2", cdelt2); - if(std::abs(my_res) - std::abs(cdelt2) > 1e-6) + if(err == 0 && std::abs(my_res) - std::abs(cdelt2) > 1e-6) throw std::runtime_error("non-square pixels in FITS file " + fitsfilename); } if(err != 0) @@ -210,7 +210,7 @@ PixelMap::PixelMap( err += cpfits.readKey("CD1_2", cd12); err += cpfits.readKey("CD2_1", cd21); err += cpfits.readKey("CD2_2", cd22); - if(std::abs(my_res) - std::abs(cd22) > 1e-6) + if(err ==0 && std::abs(my_res) - std::abs(cd22) > 1e-6) throw std::runtime_error("non-square pixels in FITS file " + fitsfilename); if(cd12 || cd21) throw std::runtime_error("pixels not aligned with coordinates in FITS file " + fitsfilename); diff --git a/TreeCode_link/utilities.cpp b/TreeCode_link/utilities.cpp index 5f424919..05af3d7a 100644 --- a/TreeCode_link/utilities.cpp +++ b/TreeCode_link/utilities.cpp @@ -492,6 +492,7 @@ unsigned long prevpower(unsigned long k){ //double *ks=new double[Nny*(Nnx/2+1)]; std::vector ks(Nny*(Nnx/2+1)); + double tmp = Nboxlx*Nboxly * pow(1.0/Nnx/Nny,2); // fb and fa are then used to compute the power spectrum // build modes @@ -505,9 +506,9 @@ unsigned long prevpower(unsigned long k){ // rescale respect to the box size ks[i+(Nnx/2+1)*j] = sqrt(kx*kx/Nboxlx/Nboxlx + ky*ky/Nboxly/Nboxly)*2.*M_PI; Nfcc[i+(Nnx/2+1)*j][0] = (fNa[i+(Nnx/2+1)*j][0]*fNb[i+(Nnx/2+1)*j][0] + - fNa[i+(Nnx/2+1)*j][1]*fNb[i+(Nnx/2+1)*j][1])*pow(1./2/M_PI/Nnx/Nny,2)*Nboxlx*Nboxly; + fNa[i+(Nnx/2+1)*j][1]*fNb[i+(Nnx/2+1)*j][1]) * tmp; Nfcc[i+(Nnx/2+1)*j][1] = -(fNa[i+(Nnx/2+1)*j][0]*fNb[i+(Nnx/2+1)*j][1] - - fNa[i+(Nnx/2+1)*j][1]*fNb[i+(Nnx/2+1)*j][0])*pow(1./2/M_PI/Nnx/Nny,2)*Nboxlx*Nboxly; + fNa[i+(Nnx/2+1)*j][1]*fNb[i+(Nnx/2+1)*j][0]) * tmp; } } std::vector ik(Nny*(Nnx/2+1)); @@ -570,23 +571,7 @@ unsigned long prevpower(unsigned long k){ int nl = ll.size(); Pl.resize(nl); - // size of the new map in x and y directions, factor by which each size is increased - //size_t Nnx=int(zerosize*nx); - //size_t Nny=int(zerosize*ny); - //double Nboxlx = boxlx*zerosize; - //double Nboxly = boxly*zerosize; - - - // estimate the fourier transform - // now we go in the Fourier Space - //double *dNa=new double[nx*ny]; - //double *dNb=new double[nx*ny]; - fftw_complex *fNa=new fftw_complex[ny*(nx/2+1)]; - //fftw_complex *fNb=new fftw_complex[ny*(nx/2+1)]; - //fftw_complex *Nfcc=new fftw_complex[ny*(nx/2+1)]; - //size_t *ik=new size_t[]; - //fftw_complex *output=new fftw_complex[ny*(nx/2+1)]; fftw_plan p1 = fftw_plan_dft_r2c_2d(ny,nx,&(aa[0]),fNa,FFTW_ESTIMATE); fftw_execute( p1 ); @@ -595,13 +580,16 @@ unsigned long prevpower(unsigned long k){ //double *ks=new double[ny*(nx/2+1)]; std::vector ks(ny*(nx/2+1)); + //double tmp = pow(1./2/M_PI/nx/ny,2)*boxlx*boxly; + double tmp = boxlx*boxly * pow(1.0/nx/ny,2); + + // fb and fa are then used to compute the power spectrum // build modes for( int i=0; i ik(ny*(nx/2+1)); @@ -652,19 +640,14 @@ unsigned long prevpower(unsigned long k){ ll[i]=ll[i]/double(nin); } } - //delete [] dNa; - //delete [] dNb; - //delete [] output; delete [] fNa; - //delete [] fNb; - //delete [] Nfcc; delete [] nk; delete [] nc; } #endif - std::valarray AdaptiveSmooth(const std::valarray &map_in,size_t Nx,size_t Ny,double value){ +std::valarray AdaptiveSmooth(const std::valarray &map_in,size_t Nx,size_t Ny,double value){ std::valarray map_out(map_in.size()); long r,area; From 38746adca68a09c0b1fe788e8af891771ac08fe0 Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Sun, 24 Nov 2019 17:26:44 +0100 Subject: [PATCH 205/227] Changed LensHaloMultiMap so that it will read/write subfields once they are done. --- MultiPlane/multimap.cpp | 136 ++++++++++++++++++---------------------- include/multimap.h | 16 +++-- 2 files changed, 71 insertions(+), 81 deletions(-) diff --git a/MultiPlane/multimap.cpp b/MultiPlane/multimap.cpp index 19960bd6..818169bc 100644 --- a/MultiPlane/multimap.cpp +++ b/MultiPlane/multimap.cpp @@ -15,11 +15,12 @@ LensHaloMultiMap::LensHaloMultiMap( ,double redshift ,double mass_unit ,COSMOLOGY &c + ,bool write_subfields + ,std::string dir_scratch ,bool subtract_ave ,bool single_grid_mode - ,std::string dir_scratch ): -LensHalo(redshift,c),single_grid(single_grid_mode),cosmo(c),cpfits(dir_data + fitsfile) +LensHalo(redshift,c),write_shorts(write_subfields),single_grid(single_grid_mode),cosmo(c),cpfits(dir_data + fitsfile) ,ave_ang_sd(0),mass_unit(mass_unit),fitsfilename(dir_data + fitsfile) { @@ -223,6 +224,16 @@ LensHalo(redshift,c),single_grid(single_grid_mode),cosmo(c),cpfits(dir_data + fi CPFITS_WRITE tmp_cpfits(lr_file,true); tmp_cpfits.writeKey("ave_ang_sd", ave_ang_sd,"average angulare density"); } + + /// set prefix to subfield maps + size_t lastindex = fitsfile.find_last_of("."); + + subfield_filename = fitsfile.substr(0, lastindex) + "_sr"; + if(dir_scratch == ""){ + subfield_filename = dir_data + subfield_filename; + }else{ + subfield_filename = dir_scratch + subfield_filename; + } }; void LensHaloMultiMap::submapPhys(Point_2d ll,Point_2d ur){ @@ -285,6 +296,26 @@ void LensHaloMultiMap::submap( throw std::invalid_argument("out of bounds"); } + /// construct file name and have it printed + std::string sr_file = subfield_filename + to_string(lower_left[0]) + "-" + to_string(lower_left[1]) + "-" + + to_string(upper_right[0]) + "-" + to_string(upper_right[1]) + "_sr.fits"; + bool short_range_file_exists = Utilities::IO::file_exists(sr_file); + + if(short_range_file_exists && write_shorts){ + short_range_map.Myread( sr_file ); + + short_range_map.lowerleft = short_range_map.upperright = long_range_map.lowerleft; + short_range_map.lowerleft[0] += lower_left[0]*resolution; + short_range_map.lowerleft[1] += lower_left[1]*resolution; + + short_range_map.upperright[0] += (upper_right[0] + 1)*resolution; + short_range_map.upperright[1] += (upper_right[1] + 1)*resolution; + + short_range_map.center = (short_range_map.lowerleft + short_range_map.upperright)/2; + short_range_map.boxlMpc = short_range_map.nx*resolution; + + }else{ + std::vector first(2); std::vector last(2); @@ -294,9 +325,10 @@ void LensHaloMultiMap::submap( last[0] = upper_right[0] + border_width + 1; last[1] = upper_right[1] + border_width + 1; - LensMap map; double area = resolution*resolution/mass_unit; + LensMap map; + if( (first[0] > 1)*(first[1] > 1)*(last[0] <= Noriginal[0])*(last[1] <= Noriginal[1]) ){ //map.read_sub(fitsfilename,first,last,getDist()); //map.read_sub(ff,first,last,getDist()); @@ -319,84 +351,36 @@ void LensHaloMultiMap::submap( std::vector last_sub(2); map.surface_density.resize(nx_big * ny_big,0); - - ////////////////////////////////////////////////////////////// - { - std::valarray v; - first_sub[0] = MAX(first[0],1); - first_sub[1] = MAX(first[1],1); - last_sub[0] = MIN(last[0],Noriginal[0]); - last_sub[1] = MIN(last[1],Noriginal[1]); + + std::valarray v; + first_sub[0] = MAX(first[0],1); + first_sub[1] = MAX(first[1],1); + last_sub[0] = MIN(last[0],Noriginal[0]); + last_sub[1] = MIN(last[1],Noriginal[1]); - Point_2d offset(first_sub[0] - first[0] , first_sub[1] - first[1] ); + Point_2d offset(first_sub[0] - first[0] , first_sub[1] - first[1] ); - long nx = last_sub[0] - first_sub[0] + 1; - long ny = last_sub[1] - first_sub[1] + 1; + long nx = last_sub[0] - first_sub[0] + 1; + long ny = last_sub[1] - first_sub[1] + 1; - v.resize(nx*ny); - cpfits.read_subset(&v[0], first_sub.data(), last_sub.data() ); + v.resize(nx*ny); + cpfits.read_subset(&v[0], first_sub.data(), last_sub.data() ); - long jj = offset[1]; - for(long j = 0; j < ny ; ++j,++jj){ - long ii = offset[0]; - for(long i = 0; i < nx ; ++i,++ii){ - map.surface_density[ii + jj*nx_big] = v[i + j*nx] / area - ave_ang_sd;; - } + long jj = offset[1]; + for(long j = 0; j < ny ; ++j,++jj){ + long ii = offset[0]; + for(long i = 0; i < nx ; ++i,++ii){ + map.surface_density[ii + jj*nx_big] = v[i + j*nx] / area - ave_ang_sd;; } } -////////////////////////////////////////////////////////////// */ - /* - first_sub[0] = 1; - last_sub[0] = Noriginal[0]; - - LensMap partmap; - const size_t chunk = nx_big*ny_big/Noriginal[0]; - - size_t kk,k = chunk + 1; - long jj = first[1]-1; - for(size_t j = 0 ; j < ny_big ; ++j , ++jj ){ - size_t kj = nx_big*j; - - if( k >= chunk || jj < 0 || jj >= Noriginal[1] ){ - - if(jj < 0) jj += Noriginal[1]; - if(jj >= Noriginal[1] ) jj -= Noriginal[1]; - - first_sub[1] = jj + 1; last_sub[1] = MIN(jj + chunk + 1,Noriginal[1]); - //partmap.read_sub(ff,first_sub,last_sub,getDist()); - partmap.read_sub(cpfits,first_sub,last_sub,getDist()); - - k = 0; - }else{ - ++k; - } - - kk = k*Noriginal[0]; - long ii = first[0]-1; - double tmp; - for(size_t i = 0 ; i < nx_big ; ++i,++ii ){ - if(ii < 0) ii += Noriginal[0]; - if(ii >= Noriginal[0] ) ii -= Noriginal[0]; - - //assert( i + kj < map.surface_density.size() ); - //assert( ii + kk < partmap.surface_density.size() ); - tmp = map.surface_density[ i + kj ] = partmap.surface_density[ ii + kk ]; - - max_pix = MAX(max_pix,tmp); - min_pix = MIN(min_pix,tmp); - } - }*/ // need to do overlap region map.boxlMpc = map.nx*resolution; } - - //std::cout << "density (0,0) " << map.surface_density[0] << std::endl; //map.PreProcessFFTWMap(1.0,wsr); map.PreProcessFFTWMap(wsr); //map.PreProcessFFTWMap(1.0,unit); - //std::cout << "density (0,0) " << map.surface_density[0] << std::endl; //map.write("test_field.fits"); // cut off bounders @@ -434,8 +418,9 @@ void LensHaloMultiMap::submap( short_range_map.center = (short_range_map.lowerleft + short_range_map.upperright)/2; short_range_map.boxlMpc = short_range_map.nx*resolution; - for(auto g : short_range_map.gamma2_bar) assert( !isnan(g) ); // ????? - //if(!single_grid) short_range_map.write("!" + fitsfilename + "_sr.fits"); + if(write_shorts) short_range_map.write("!" + sr_file); +} + } @@ -700,18 +685,18 @@ void LensMap::Myread(std::string fits_input_file){ CPFITS_READ cpfits(fits_input_file); assert(cpfits.get_num_hdus() == 5); - double yrange; cpfits.readKey("SIDEL1",boxlMpc); cpfits.readKey("SIDEL2",yrange); - + cpfits.readKey("CENTER_X",center[0]); + cpfits.readKey("CENTER_Y",center[1]); + std::vector dims; cpfits.read(surface_density,dims); - assert(dims.size() == 2 ); nx = dims[0]; ny = dims[1]; - + cpfits.change_hdu(2); cpfits.read(alpha1_bar,dims); @@ -723,9 +708,6 @@ void LensMap::Myread(std::string fits_input_file){ cpfits.change_hdu(5); cpfits.read(gamma2_bar,dims); - - //double res = boxlMpc/nx; - center *= 0; lowerleft = center; lowerleft[0] -= boxlMpc/2; @@ -887,6 +869,8 @@ void LensMap::write(std::string filename //PHDU *phout=&fout->pHDU(); cpfits.writeKey("SIDEL1",boxlMpc,"x range in physical Mpc"); cpfits.writeKey("SIDEL2",y_range(),"y range in physical Mpc"); + cpfits.writeKey("CENTER_X",center[0],"center of field x"); + cpfits.writeKey("CENTER_Y",center[1],"center of field y"); cpfits.write_image(alpha1_bar,naxex); cpfits.write_image(alpha2_bar,naxex); diff --git a/include/multimap.h b/include/multimap.h index f3d0c6e8..6bfdc060 100644 --- a/include/multimap.h +++ b/include/multimap.h @@ -121,10 +121,11 @@ class LensHaloMultiMap : public LensHalo ,double redshift ,double mass_unit /// should include h factors ,COSMOLOGY &c - ,bool subtract_ave = true /// subtract the average of the full field - ,bool single_grid_mode = false + ,bool write_subfields = false /// write subfields to be read if they already exist ,std::string dir_scratch = "" /// directory for saving long rang force if different than directory where fitsfile is - ); + ,bool subtract_ave = true /// subtract the average of the full field + ,bool single_grid_mode = false /// don't do the short & long range decomposition + ); ~LensHaloMultiMap(){ }; @@ -211,6 +212,8 @@ class LensHaloMultiMap : public LensHalo wsr = m.wsr; wlr = m.wlr; ave_ang_sd = m.ave_ang_sd; + subfield_filename = m.subfield_filename; + write_shorts = m.write_shorts; } LensHaloMultiMap(LensHaloMultiMap &&m): @@ -237,7 +240,8 @@ class LensHaloMultiMap : public LensHalo wsr = m.wsr; wlr = m.wlr; ave_ang_sd = m.ave_ang_sd; - + subfield_filename = m.subfield_filename; + write_shorts = m.write_shorts; } public: @@ -246,6 +250,7 @@ class LensHaloMultiMap : public LensHalo private: + bool write_shorts; bool single_grid; COSMOLOGY &cosmo; CPFITS_READ cpfits; @@ -262,7 +267,8 @@ class LensHaloMultiMap : public LensHalo double angular_resolution; // angular resolution of original image long border_width; // width of short range maps padding std::string fitsfilename; - + std::string subfield_filename; + double rs2; //const COSMOLOGY& cosmo; From 4a2d1ce69c9624c28f9b1e0d45fbcffee3f5d234 Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Tue, 26 Nov 2019 17:37:56 +0100 Subject: [PATCH 206/227] Fixed some bugs in CPFITS that came from not resetting the status to 0 every time a cfitsio routine is used. --- ImageProcessing/pixelize.cpp | 14 +-- MultiPlane/MOKAfits.cpp | 4 +- MultiPlane/multimap.cpp | 8 +- include/cpfits.h | 191 +++++++++++++++++++++++------------ include/multimap.h | 2 +- 5 files changed, 142 insertions(+), 77 deletions(-) diff --git a/ImageProcessing/pixelize.cpp b/ImageProcessing/pixelize.cpp index e55b6f1d..0e5aebcc 100644 --- a/ImageProcessing/pixelize.cpp +++ b/ImageProcessing/pixelize.cpp @@ -165,11 +165,11 @@ PixelMap::PixelMap( throw std::invalid_argument("bad file"); } - CPFITS_READ cpfits(fitsfilename); - std::vector cpsize; - int bitpix; - cpfits.imageInfo(bitpix, cpsize); + + CPFITS_READ cpfits(fitsfilename); + //int bitpix; + cpfits.imageDimensions(cpsize); Nx = cpsize[0]; Ny = cpsize[1]; @@ -179,13 +179,13 @@ PixelMap::PixelMap( err += cpfits.readKey("RA", center[0]); err += cpfits.readKey("DEC", center[1]); - if(err != 0){ + if(err){ err = 0; err += cpfits.readKey("CRVAL1", center[0]); err += cpfits.readKey("CRVAL2", center[1]); } - if(err != 0) + if(err) { center[0] = 0.0; center[1] = 0.0; @@ -242,6 +242,7 @@ PixelMap::PixelMap( map_boundary_p2[1] = center[1] + (Ny*resolution)/2.; cpfits.read(map,cpsize); + //std::cout << "map size : " << map[0] << std::endl; //std::cout << "map size : " << map.size() << std::endl; } @@ -412,6 +413,7 @@ PixelMap& PixelMap::operator+=(const PixelMap& rhs) // TODO: maybe check if PixelMaps agree, but is slower if(Nx != rhs.getNx() || Ny != rhs.getNy()) throw std::runtime_error("Dimensions of maps are not compatible"); + for(size_t i=0;i size; - int bitpix; - cpfits.imageInfo(bitpix,size); + //int bitpix; + cpfits.imageDimensions(size); map.nx = size[0]; map.ny = size[1]; diff --git a/MultiPlane/multimap.cpp b/MultiPlane/multimap.cpp index 818169bc..3e3c6e2f 100644 --- a/MultiPlane/multimap.cpp +++ b/MultiPlane/multimap.cpp @@ -582,8 +582,8 @@ void LensMap::read_header(std::string fits_input_file CPFITS_READ cpfits(fits_input_file); std::vector size; - int bitpix; - cpfits.imageInfo(bitpix,size); + //int bitpix; + cpfits.imageDimensions(size); assert(size.size() ==2); @@ -787,9 +787,9 @@ void LensMap::read_sub(CPFITS_READ &cpfits ,double angDist ){ - int bitpix; + //int bitpix; std::vector sizes; - cpfits.imageInfo(bitpix,sizes); + cpfits.imageDimensions(sizes); //long nx_orig = h0.axis(0); //long ny_orig = h0.axis(1); diff --git a/include/cpfits.h b/include/cpfits.h index 3e5a5dca..7580dfbd 100644 --- a/include/cpfits.h +++ b/include/cpfits.h @@ -23,22 +23,26 @@ class CPFITS_BASE{ protected: - CPFITS_BASE():status(0){} + CPFITS_BASE(){} CPFITS_BASE(CPFITS_BASE &&ff){ fptr = ff.fptr; ff.fptr = nullptr; - status = ff.status; } void operator=(CPFITS_BASE &&ff){ fptr = ff.fptr; ff.fptr = nullptr; - status = ff.status; } + void check_status(int status,std::string s = "Error in CPFITS"){ + if(status){ + fits_report_error(stderr, status); + std::cerr << s << std::endl; + throw std::invalid_argument(s); + } + } fitsfile *fptr; - int status; public: ///////////////////////////////// @@ -48,7 +52,9 @@ class CPFITS_BASE{ /// returns the number of tabels or images int get_num_hdus(){ int hdunum; + int status = 0; fits_get_num_hdus(fptr, &hdunum, &status); + check_status(status); return hdunum; } /// returns the current image / table number, starts with 1 not 0 @@ -65,14 +71,19 @@ class CPFITS_BASE{ throw std::invalid_argument("out of range"); } int hdutype; + int status = 0; fits_movabs_hdu(fptr,i,&hdutype,&status); - + check_status(status); + reset_imageInfo(); + return hdutype; } int get_current_hdu_type(){ - int hdutype; + int hdutype,status = 0; fits_get_hdu_type(fptr,&hdutype,&status); + check_status(status); + return hdutype; } @@ -88,40 +99,61 @@ class CPFITS_BASE{ FLOAT_IMG = -32 (32-bit floating point pixels) DOUBLE_IMG = -64 (64-bit floating point pixels) */ - void imageInfo( - int &bitpix /// type of data - ,std::vector &size - ){ - int ndim; /// number of dimensions (keyword NAXIS) - size.resize(10); - fits_get_img_param(fptr,10,&bitpix,&ndim,size.data(),&status); - size.resize(ndim); + void reset_imageInfo(){ + + int status = 0; + fits_get_img_type(fptr,&bitpix,&status); + check_status(status); + fits_get_img_dim(fptr,&Ndims,&status); + check_status(status); + sizes.resize(Ndims); + fits_get_img_size(fptr,Ndims,sizes.data(),&status); + check_status(status); + + //std::cout << sizes.size() << std::endl; + } + void imageDimensions(std::vector &my_sizes){ + my_sizes.resize(sizes.size()); + my_sizes = sizes; } /// number of keywords for current table, returns != 0 if keyname is not found int nkyewords(){ - int keysexist; + int keysexist,status=0; return fits_get_hdrspace(fptr,&keysexist,NULL,&status); } /// read a key value for the current table / image, returns 0 if the key word does not exit int readKey(std::string keyname,double &value){ + int status = 0; return fits_read_key(fptr,TDOUBLE,keyname.c_str(), &value,NULL,&status); } int readKey(std::string keyname,float &value){ + int status = 0; return fits_read_key(fptr,TFLOAT,keyname.c_str(), &value,NULL,&status); } int readKey(std::string keyname,int &value){ + int status = 0; return fits_read_key(fptr,TINT,keyname.c_str(), &value,NULL,&status); } int readKey(std::string keyname,size_t &value){ + int status = 0; return fits_read_key(fptr,TULONG,keyname.c_str(), &value,NULL,&status); } + int readKey(std::string keyname,long &value){ + int status = 0; + return fits_read_key(fptr,TLONG,keyname.c_str(), + &value,NULL,&status); + } +protected: + int Ndims; + std::vector sizes; + int bitpix; }; /// read only fits file interface @@ -133,13 +165,13 @@ class CPFITS_READ : public CPFITS_BASE { public: CPFITS_READ(std::string filename,bool verbose = false) { + int status = 0; fits_open_file(&fptr,filename.c_str(), READONLY, &status); - // print any error messages - if (status){ - fits_report_error(stderr, status); - throw std::invalid_argument("missing file"); - } + check_status(status,"Problem with input fits file."); + reset_imageInfo(); + + if(verbose){ std::cout << "Opening file : " << filename << std::endl; std::cout << " status : " << status << std::endl; @@ -147,8 +179,9 @@ class CPFITS_READ : public CPFITS_BASE { } ~CPFITS_READ(){ - if (status) fits_report_error(stderr, status); + int status = 0; fits_close_file(fptr, &status); + check_status(status,"Problem closing fits file!"); } CPFITS_READ(CPFITS_READ &&ff):CPFITS_BASE(std::move(ff)){}; @@ -159,71 +192,83 @@ class CPFITS_READ : public CPFITS_BASE { /// close old and reopen a new file void reset(std::string filename){ + int status = 0; fits_close_file(fptr, &status); - assert(status==0); + check_status(status); fits_open_file(&fptr,filename.c_str(), READONLY, &status); - // print any error messages - if (status) fits_report_error(stderr, status); + check_status(status); + reset_imageInfo(); } /// read the whole image into a vector int read(std::vector &output,std::vector &size){ - int dtype; - imageInfo(dtype,size); + //int dtype; + imageDimensions(size); long nelements=1; for(long n : size) nelements *= n; - std::vector start(nelements,1); + std::vector start(size.size(),1); + output.resize(nelements); - + int status = 0; return fits_read_pix(fptr,TDOUBLE,start.data(),nelements ,NULL,output.data(),NULL, &status); } /// read the whole image into a vector int read(std::vector &output,std::vector &size){ - int dtype; - imageInfo(dtype,size); + //int dtype; + imageDimensions(size); long nelements=1; for(long n : size) nelements *= n; - std::vector start(nelements,1); + std::vector start(size.size(),1); output.resize(nelements); + int status = 0; return fits_read_pix(fptr,TFLOAT,start.data(),nelements ,NULL,output.data(),NULL, &status); } /// read the whole image into a valarray int read(std::valarray &output,std::vector &size){ - int dtype; - imageInfo(dtype,size); + //int dtype; + imageDimensions(size); long nelements=1; for(long n : size) nelements *= n; - std::vector start(nelements,1); + std::vector start(size.size(),1); + output.resize(nelements); + int status = 0; return fits_read_pix(fptr,TFLOAT,start.data(),nelements ,NULL,&output[0],NULL, &status); } - int read(std::valarray &output,std::vector &size){ - - int dtype; - imageInfo(dtype,size); - long nelements=1; - for(long n : size) nelements *= n; - std::vector start(nelements,1); + int read(std::valarray &output,std::vector &sizes){ + //std::cout << sizes.size() << std::endl; + imageDimensions(sizes); + std::cout << sizes.size() << std::endl; + std::cout << sizes[0] << " " << sizes[1] << std::endl; + long long nelements=1; + for(long n : sizes) nelements *= n; + std::vector start(sizes.size(),1); output.resize(nelements); - return fits_read_pix(fptr,TDOUBLE,start.data(),nelements - ,NULL,&output[0],NULL, &status); + int status = 0; + int error = fits_read_pix(fptr,TDOUBLE,start.data(),nelements + ,NULL,&output[0],NULL, &status); + check_status(status,"PixMap Read Failure"); + + return error; } /// read nelements in order from image to output array int read_block(double *output,long nelements,long * start){ + int status =0; return fits_read_pix(fptr,TDOUBLE,start,nelements ,NULL,output,NULL, &status); } int read_block(float *output,long nelements,long * start){ + int status = 0; return fits_read_pix(fptr,TFLOAT,start,nelements ,NULL,output,NULL, &status); } @@ -232,15 +277,14 @@ class CPFITS_READ : public CPFITS_BASE { /// read a rectangular subset of the image int read_subset(double *output,long *lowerleft,long *upperright){ long inc[2] = {1,1}; // this is a step + int status = 0; return fits_read_subset(fptr,TDOUBLE,lowerleft,upperright,inc ,NULL,output,NULL,&status); - //int fits_read_subset(fitsfile *fptr, int datatype, long *fpixel, - // long *lpixel, long *inc, void *nulval, void *array, - // int *anynul, int *status) } /// read a rectangular subset of the image int read_subset(float *output,long *lowerleft,long *upperright){ long inc[2] = {1,1}; // this is a step + int status = 0; return fits_read_subset(fptr,TFLOAT,lowerleft,upperright,inc ,NULL,output,NULL,&status); } @@ -260,92 +304,111 @@ class CPFITS_WRITE : public CPFITS_BASE { if(!append){ if(verbose) std::cout << "Creating file : " << filename << std::endl; if(filename[0] != '!') filename = "!" + filename; + int status = 0; fits_create_file(&fptr, filename.c_str(), &status); - }else{ + check_status(status); + }else{ + int status = 0; fits_open_file(&fptr,filename.c_str(),READWRITE,&status); + reset_imageInfo(); + if(status==104){ // create file if it does not exist status = 0; fits_create_file(&fptr, filename.c_str(), &status); - + check_status(status); + if(verbose) std::cout << "Creating file : " << filename << std::endl; }else{ if(verbose) std::cout << "Appending to file : " << filename << std::endl; + check_status(status); } } - - // print any error messages - if (status) fits_report_error(stderr, status); - - if(verbose){ - std::cout << " status : " << status << std::endl; - } } ~CPFITS_WRITE(){ - if (status) fits_report_error(stderr, status); - fits_close_file(fptr, &status); + int status = 0; + fits_close_file(fptr, &status); + check_status(status); } /// add a new image or cube to the file - int write_image(std::vector &im,std::vector &size){ + void write_image(std::vector &im,std::vector &size){ size_t n=1; for(size_t i : size) n *= i; assert(im.size() == n); + int status=0; fits_create_img(fptr,DOUBLE_IMG,size.size(), size.data(), &status); + check_status(status); std::vector fpixel(size.size(),1); - return fits_write_pix(fptr,TDOUBLE,fpixel.data(), + fits_write_pix(fptr,TDOUBLE,fpixel.data(), im.size(),im.data(),&status); + check_status(status); } - int write_image(std::vector &im,std::vector &size){ + void write_image(std::vector &im,std::vector &size){ size_t n=1; for(size_t i : size) n *= i; assert(im.size() == n); + int status = 0; fits_create_img(fptr,FLOAT_IMG,size.size(), size.data(), &status); + check_status(status); std::vector fpixel(size.size(),1); - return fits_write_pix(fptr,TFLOAT,fpixel.data(), + fits_write_pix(fptr,TFLOAT,fpixel.data(), im.size(),im.data(),&status); + check_status(status); } - int write_image(std::valarray &im,std::vector &size){ + void write_image(std::valarray &im,std::vector &size){ size_t n=1; for(size_t i : size) n *= i; assert(im.size() == n); + int status = 0; fits_create_img(fptr,DOUBLE_IMG,size.size(), size.data(), &status); + check_status(status); + std::vector fpixel(size.size(),1); - return fits_write_pix(fptr,TDOUBLE,fpixel.data(), + fits_write_pix(fptr,TDOUBLE,fpixel.data(), im.size(),&im[0],&status); + check_status(status); } int write_image(std::valarray &im,std::vector &size){ size_t n=1; for(size_t i : size) n *= i; assert(im.size() == n); + int status = 0; fits_create_img(fptr,FLOAT_IMG,size.size(), size.data(), &status); + check_status(status); std::vector fpixel(size.size(),1); - return fits_write_pix(fptr,TFLOAT,fpixel.data(), + fits_write_pix(fptr,TFLOAT,fpixel.data(), im.size(),&im[0],&status); + check_status(status); } /// add or replace a key value in the header of the current table / image int writeKey(std::string key,double value,std::string comment ){ + int status = 0; return fits_update_key(fptr,TDOUBLE,key.c_str(), &value,comment.c_str(),&status); } int writeKey(std::string key,float value,std::string comment ){ + int status = 0; return fits_update_key(fptr,TFLOAT,key.c_str(), &value,comment.c_str(),&status); } int writeKey(std::string key,int value,std::string comment ){ + int status = 0; return fits_update_key(fptr,TINT,key.c_str(), &value,comment.c_str(),&status); } int writeKey(std::string key,long value,std::string comment ){ + int status = 0; return fits_update_key(fptr,TLONG,key.c_str(), &value,comment.c_str(),&status); } int writeKey(std::string key,size_t value,std::string comment ){ + int status = 0; return fits_update_key(fptr,TULONG,key.c_str(), &value,comment.c_str(),&status); } diff --git a/include/multimap.h b/include/multimap.h index 6bfdc060..22d09d06 100644 --- a/include/multimap.h +++ b/include/multimap.h @@ -523,7 +523,7 @@ void LensMap::PreProcessFFTWMap(float zerosize,T Wphi_of_k){ gamma2_bar[i+nx*j] = float(-realsp[ii+Nnx*jj]/NN); } } - for(auto &a : gamma2_bar) assert(!isnan(a)); // ??? + //for(auto &a : gamma2_bar) assert(!isnan(a)); // ??? } // kappa - this is done over because of the window in Fourier space From 00798ca1185d8b88d281865983c4dfaf391ab599 Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Thu, 28 Nov 2019 10:14:08 +0100 Subject: [PATCH 207/227] Comments and removed test lines. --- MultiPlane/multimap.cpp | 11 ++++++----- include/cpfits.h | 6 +++--- include/multimap.h | 18 +++++++----------- 3 files changed, 16 insertions(+), 19 deletions(-) diff --git a/MultiPlane/multimap.cpp b/MultiPlane/multimap.cpp index 3e3c6e2f..20649d1e 100644 --- a/MultiPlane/multimap.cpp +++ b/MultiPlane/multimap.cpp @@ -649,18 +649,18 @@ void LensMap::read(std::string fits_input_file,double angDist){ int err = 0; { - /* these are always present in ea*/ + /* these are always present in each*/ float res; - err += cpfits.readKey ("CD1_1",angular_pixel_size); // angular resolution degrees + err = cpfits.readKey ("CD1_1",angular_pixel_size); // angular resolution degrees angular_pixel_size *= degreesTOradians; res = angular_pixel_size*angDist; boxlMpc = res * nx; } - if(err != 0){ + if(err){ std::cerr << "LensMap fits map must have header keywords:" << std::endl - << " CD1_1 - length on other side in Mpc/h" << std::endl; + << " CD1_1 - angular resolution" << std::endl; //<< " REDSHIFT - redshift of lens" << std::endl //<< " WLOW - closest radial distance in cMpc/h" << std::endl //<< " WUP - furthest radial distance in cMpc/h" << std::endl; @@ -681,7 +681,8 @@ void LensMap::read(std::string fits_input_file,double angDist){ void LensMap::Myread(std::string fits_input_file){ - std:: cout << " reading lens density map file: " << fits_input_file << std:: endl; + std:: cout << " reading lens density map file: " + << fits_input_file << std:: endl; CPFITS_READ cpfits(fits_input_file); assert(cpfits.get_num_hdus() == 5); diff --git a/include/cpfits.h b/include/cpfits.h index 7580dfbd..88c0d5fe 100644 --- a/include/cpfits.h +++ b/include/cpfits.h @@ -246,8 +246,8 @@ class CPFITS_READ : public CPFITS_BASE { int read(std::valarray &output,std::vector &sizes){ //std::cout << sizes.size() << std::endl; imageDimensions(sizes); - std::cout << sizes.size() << std::endl; - std::cout << sizes[0] << " " << sizes[1] << std::endl; + //std::cout << sizes.size() << std::endl; + //std::cout << sizes[0] << " " << sizes[1] << std::endl; long long nelements=1; for(long n : sizes) nelements *= n; std::vector start(sizes.size(),1); @@ -372,7 +372,7 @@ class CPFITS_WRITE : public CPFITS_BASE { im.size(),&im[0],&status); check_status(status); } - int write_image(std::valarray &im,std::vector &size){ + void write_image(std::valarray &im,std::vector &size){ size_t n=1; for(size_t i : size) n *= i; assert(im.size() == n); diff --git a/include/multimap.h b/include/multimap.h index 22d09d06..b857ce51 100644 --- a/include/multimap.h +++ b/include/multimap.h @@ -36,11 +36,12 @@ struct LensMap{ /// values for the map std::valarray surface_density; // Msun / Mpc^2 - std::valarray alpha1_bar; - std::valarray alpha2_bar; - std::valarray gamma1_bar; - std::valarray gamma2_bar; - std::valarray phi_bar; + std::valarray alpha1_bar; // Msun / Mpc + std::valarray alpha2_bar; // Msun / Mpc + std::valarray gamma1_bar; // Msun / Mpc^2 + std::valarray gamma2_bar; // Msun / Mpc^2 + std::valarray phi_bar; // Msun + int nx,ny; double boxlMpc; double angular_pixel_size; // in radians @@ -328,8 +329,7 @@ void LensMap::PreProcessFFTWMap(float zerosize,T Wphi_of_k){ kys[j] *= tmp; } - - std::vector extended_map( NN ); + std::vector extended_map( NN ); // assume locate in a rectangular map and build up the new one for( int j=0; j Date: Sun, 1 Dec 2019 12:57:02 +0100 Subject: [PATCH 208/227] Added LensMap::write(std::string filename,LensingVariable quant); --- MultiPlane/multimap.cpp | 76 +++++++++++++++++++++++------------------ include/multimap.h | 2 ++ 2 files changed, 45 insertions(+), 33 deletions(-) diff --git a/MultiPlane/multimap.cpp b/MultiPlane/multimap.cpp index 3e3c6e2f..e8b5d835 100644 --- a/MultiPlane/multimap.cpp +++ b/MultiPlane/multimap.cpp @@ -843,24 +843,8 @@ void LensMap::read_sub(CPFITS_READ &cpfits */ void LensMap::write(std::string filename ){ -//#ifdef ENABLE_FITS - //long naxis=2; - //long naxes[2]={nx,ny}; - CPFITS_WRITE cpfits(filename,false); - /* - std::auto_ptr fout(0); - try{ - fout.reset(new FITS(filename,FLOAT_IMG,naxis,naxes)); - } - catch(FITS::CantCreate){ - std::cout << "Unable to open fits file " << filename << std::endl; - ERROR_MESSAGE(); - exit(1); - } - */ - std::vector naxex(2); naxex[0]=nx; naxex[1]=ny; @@ -876,22 +860,48 @@ void LensMap::write(std::string filename cpfits.write_image(alpha2_bar,naxex); cpfits.write_image(gamma1_bar,naxex); cpfits.write_image(gamma2_bar,naxex); - //cpfits.write_image(phi_bar,naxex); - - //ExtHDU *eh1=fout->addImage("alpah1", FLOAT_IMG, naxex); - //eh1->write(1,nx*ny,alpha1_bar); - //ExtHDU *eh2=fout->addImage("alpha2", FLOAT_IMG, naxex); - //eh2->write(1,nx*ny,alpha2_bar); - - //ExtHDU *eh3=fout->addImage("gamma1", FLOAT_IMG, naxex); - //eh3->write(1,nx*ny,gamma1_bar); - //ExtHDU *eh4=fout->addImage("gamma2", FLOAT_IMG, naxex); - //eh4->write(1,nx*ny,gamma2_bar); - - //std::cout << *phout << std::endl; -//#else -// std::cout << "Please enable the preprocessor flag ENABLE_FITS !" << std::endl; -// exit(1); -//#endif +} + +void LensMap::write(std::string filename + ,LensingVariable quant){ + + if( boxlMpc != angular_pixel_size*nx){ + ERROR_MESSAGE(); + std::cerr << " This function is meant for and angular map and not a physical unit map " << std::endl; + throw std::invalid_argument(""); + } + + CPFITS_WRITE cpfits(filename,false); + + std::vector naxex(2); + naxex[0]=nx; + naxex[1]=ny; + + //PHDU *phout=&fout->pHDU(); + cpfits.writeKey("CD1_1",angular_pixel_size /degreesTOradians,"pixel size in degrees"); + cpfits.writeKey("SIDEL1",boxlMpc,"x range in radians"); + cpfits.writeKey("SIDEL2",y_range(),"y range in radians"); + cpfits.writeKey("CENTER_X",center[0],"center of field x"); + cpfits.writeKey("CENTER_Y",center[1],"center of field y"); + + switch (quant) { + case KAPPA: + cpfits.write_image(surface_density,naxex); + break; + case GAMMA1: + cpfits.write_image(gamma1_bar,naxex); + break; + case GAMMA2: + cpfits.write_image(gamma2_bar,naxex); + break; + case ALPHA1: + cpfits.write_image(alpha1_bar,naxex); + break; + case ALPHA2: + cpfits.write_image(alpha2_bar,naxex); + break; + default: + break; + } } diff --git a/include/multimap.h b/include/multimap.h index 22d09d06..af7f4cf1 100644 --- a/include/multimap.h +++ b/include/multimap.h @@ -87,6 +87,8 @@ struct LensMap{ ); void write(std::string filename); + /// meant to output directly in angulare units and lensing quantities + void write(std::string filename,LensingVariable quant); #endif From 7cd47000ca8b43b65cb6c57ffcb3aaa5e0564aa4 Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Sun, 1 Dec 2019 17:47:48 +0100 Subject: [PATCH 209/227] Made LensMap::nx,ny size_t 's --- include/multimap.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/multimap.h b/include/multimap.h index af7f4cf1..8bf3a18b 100644 --- a/include/multimap.h +++ b/include/multimap.h @@ -41,7 +41,7 @@ struct LensMap{ std::valarray gamma1_bar; std::valarray gamma2_bar; std::valarray phi_bar; - int nx,ny; + size_t nx,ny; double boxlMpc; double angular_pixel_size; // in radians Point_2d center; From 1fce0fe885dc5d23057eb53fd61704c11e5e0e0d Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Mon, 2 Dec 2019 11:56:57 +0100 Subject: [PATCH 210/227] Fixed bug in LensMap::write() Added some information to fits headers produced by LensMap::write() --- MultiPlane/multimap.cpp | 36 +++++++++++++++++++++++++++--------- include/cpfits.h | 13 +++++++++++-- 2 files changed, 38 insertions(+), 11 deletions(-) diff --git a/MultiPlane/multimap.cpp b/MultiPlane/multimap.cpp index e8b5d835..7b99b0b8 100644 --- a/MultiPlane/multimap.cpp +++ b/MultiPlane/multimap.cpp @@ -855,11 +855,21 @@ void LensMap::write(std::string filename cpfits.writeKey("SIDEL2",y_range(),"y range in physical Mpc"); cpfits.writeKey("CENTER_X",center[0],"center of field x"); cpfits.writeKey("CENTER_Y",center[1],"center of field y"); - + cpfits.writeKey("QUANTITY","KAPPA","lensing quantity"); + cpfits.writeKey("UNITS"," Mpc^-2","units of values"); + cpfits.write_image(alpha1_bar,naxex); + cpfits.writeKey("QUANTITY","ALPHA1","lensing quantity"); + cpfits.writeKey("UNITS"," Mpc^-1","units of values"); cpfits.write_image(alpha2_bar,naxex); + cpfits.writeKey("QUANTITY","ALPHA2","lensing quantity"); + cpfits.writeKey("UNITS"," Mpc^-1","units of values"); cpfits.write_image(gamma1_bar,naxex); + cpfits.writeKey("QUANTITY","GAMMA1","lensing quantity"); + cpfits.writeKey("UNITS"," Mpc^-2","units of values"); cpfits.write_image(gamma2_bar,naxex); + cpfits.writeKey("QUANTITY","GAMMA2","lensing quantity"); + cpfits.writeKey("UNITS"," Mpc^-2","units of values"); } void LensMap::write(std::string filename @@ -877,13 +887,6 @@ void LensMap::write(std::string filename naxex[0]=nx; naxex[1]=ny; - //PHDU *phout=&fout->pHDU(); - cpfits.writeKey("CD1_1",angular_pixel_size /degreesTOradians,"pixel size in degrees"); - cpfits.writeKey("SIDEL1",boxlMpc,"x range in radians"); - cpfits.writeKey("SIDEL2",y_range(),"y range in radians"); - cpfits.writeKey("CENTER_X",center[0],"center of field x"); - cpfits.writeKey("CENTER_Y",center[1],"center of field y"); - switch (quant) { case KAPPA: cpfits.write_image(surface_density,naxex); @@ -894,14 +897,29 @@ void LensMap::write(std::string filename case GAMMA2: cpfits.write_image(gamma2_bar,naxex); break; + case GAMMA: + { + std::valarray gamma = sqrt( gamma1_bar*gamma1_bar + gamma2_bar*gamma2_bar ); + cpfits.write_image(gamma,naxex); + } + break; case ALPHA1: cpfits.write_image(alpha1_bar,naxex); - break; + cpfits.writeKey("UNITS","radians","units of values"); + break; case ALPHA2: cpfits.write_image(alpha2_bar,naxex); + cpfits.writeKey("UNITS","radians","units of values"); break; default: break; } + + cpfits.writeKey("CD1_1",angular_pixel_size /degreesTOradians,"pixel size in degrees"); + cpfits.writeKey("SIDEL1",boxlMpc,"x range in radians"); + cpfits.writeKey("SIDEL2",y_range(),"y range in radians"); + cpfits.writeKey("CENTER_X",center[0],"center of field x"); + cpfits.writeKey("CENTER_Y",center[1],"center of field y"); + } diff --git a/include/cpfits.h b/include/cpfits.h index 7580dfbd..c33a2ce3 100644 --- a/include/cpfits.h +++ b/include/cpfits.h @@ -329,7 +329,7 @@ class CPFITS_WRITE : public CPFITS_BASE { int status = 0; fits_close_file(fptr, &status); check_status(status); - } + } /// add a new image or cube to the file void write_image(std::vector &im,std::vector &size){ @@ -367,12 +367,13 @@ class CPFITS_WRITE : public CPFITS_BASE { size.data(), &status); check_status(status); + status = 0; std::vector fpixel(size.size(),1); fits_write_pix(fptr,TDOUBLE,fpixel.data(), im.size(),&im[0],&status); check_status(status); } - int write_image(std::valarray &im,std::vector &size){ + void write_image(std::valarray &im,std::vector &size){ size_t n=1; for(size_t i : size) n *= i; assert(im.size() == n); @@ -387,6 +388,14 @@ class CPFITS_WRITE : public CPFITS_BASE { } /// add or replace a key value in the header of the current table / image + int writeKey(std::string key,std::string value,std::string comment ){ + int status = 0; + char *s = strdup(value.c_str()); + int error = fits_write_key(fptr,TSTRING,key.c_str(), + s,comment.c_str(),&status); + delete[] s; + return error; + } int writeKey(std::string key,double value,std::string comment ){ int status = 0; return fits_update_key(fptr,TDOUBLE,key.c_str(), From cd2cec7ce9b3c2ebc4c306c2a5a4080b835e69b2 Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Mon, 2 Dec 2019 17:17:35 +0100 Subject: [PATCH 211/227] Removed some required keywords for input PixelMap fits fiels. --- ImageProcessing/pixelize.cpp | 14 +++++++------- MultiPlane/multimap.cpp | 1 + 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/ImageProcessing/pixelize.cpp b/ImageProcessing/pixelize.cpp index 0e5aebcc..7d13f855 100644 --- a/ImageProcessing/pixelize.cpp +++ b/ImageProcessing/pixelize.cpp @@ -207,13 +207,13 @@ PixelMap::PixelMap( { double cd12, cd21, cd22; err += cpfits.readKey("CD1_1", my_res); - err += cpfits.readKey("CD1_2", cd12); - err += cpfits.readKey("CD2_1", cd21); - err += cpfits.readKey("CD2_2", cd22); - if(err ==0 && std::abs(my_res) - std::abs(cd22) > 1e-6) - throw std::runtime_error("non-square pixels in FITS file " + fitsfilename); - if(cd12 || cd21) - throw std::runtime_error("pixels not aligned with coordinates in FITS file " + fitsfilename); + //err += cpfits.readKey("CD1_2", cd12); + //err += cpfits.readKey("CD2_1", cd21); + //err += cpfits.readKey("CD2_2", cd22); + //if(err==0 && std::abs(my_res) - std::abs(cd22) > 1e-6) + // throw std::runtime_error("non-square pixels in FITS file " + fitsfilename); + //if(cd12 || cd21) + // throw std::runtime_error("pixels not aligned with coordinates in FITS file " + fitsfilename); } if(err != 0){ double ps; diff --git a/MultiPlane/multimap.cpp b/MultiPlane/multimap.cpp index 7b99b0b8..37a2c188 100644 --- a/MultiPlane/multimap.cpp +++ b/MultiPlane/multimap.cpp @@ -915,6 +915,7 @@ void LensMap::write(std::string filename break; } + cpfits.writeKey("CD1_1",angular_pixel_size /degreesTOradians,"pixel size in degrees"); cpfits.writeKey("CD1_1",angular_pixel_size /degreesTOradians,"pixel size in degrees"); cpfits.writeKey("SIDEL1",boxlMpc,"x range in radians"); cpfits.writeKey("SIDEL2",y_range(),"y range in radians"); From 5065b321d577679ca7a7e599598544bcedbe866a Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Mon, 2 Dec 2019 17:34:56 +0100 Subject: [PATCH 212/227] Added the option of not calculating the deflection in LensMap::PreProcessFFTWMap. --- include/multimap.h | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/include/multimap.h b/include/multimap.h index 8bf3a18b..b24611ef 100644 --- a/include/multimap.h +++ b/include/multimap.h @@ -99,9 +99,9 @@ struct LensMap{ //static double identity(double x){return 1;} template - void PreProcessFFTWMap(float zerosize,T Wphi_of_k); + void PreProcessFFTWMap(float zerosize,T Wphi_of_k,bool do_alpha = true); template - void PreProcessFFTWMap(T Wphi_of_k); + void PreProcessFFTWMap(T Wphi_of_k,bool do_alpah = true); //void PreProcessFFTWMap(float zerosize,std::function Wphi_of_k = identity); #endif @@ -301,7 +301,7 @@ class LensHaloMultiMap : public LensHalo //void LensMap::PreProcessFFTWMap(float zerosize,std::function Wphi_of_k){ template -void LensMap::PreProcessFFTWMap(float zerosize,T Wphi_of_k){ +void LensMap::PreProcessFFTWMap(float zerosize,T Wphi_of_k,bool do_alpha){ assert(surface_density.size() == nx*ny); @@ -414,6 +414,7 @@ void LensMap::PreProcessFFTWMap(float zerosize,T Wphi_of_k){ fftw_plan pp = fftw_plan_dft_c2r_2d(Nny,Nnx,fft,realsp.data(),FFTW_MEASURE); + if(do_alpha){ // alpha1 { @@ -470,6 +471,7 @@ void LensMap::PreProcessFFTWMap(float zerosize,T Wphi_of_k){ } } } + } // gamma1 { @@ -563,12 +565,12 @@ void LensMap::PreProcessFFTWMap(float zerosize,T Wphi_of_k){ delete[] fft; delete[] fphi; - phi_bar.resize(nx*ny,0); // ??? this needs to be calculated in the future + //phi_bar.resize(nx*ny,0); // ??? this needs to be calculated in the future } /// no padding template -void LensMap::PreProcessFFTWMap(T Wphi_of_k){ +void LensMap::PreProcessFFTWMap(T Wphi_of_k,bool do_alpha){ assert(surface_density.size() == nx*ny); @@ -648,6 +650,7 @@ void LensMap::PreProcessFFTWMap(T Wphi_of_k){ fftw_plan pp = fftw_plan_dft_c2r_2d(ny,nx,fft,realsp.data(),FFTW_MEASURE); + if(do_alpha){ // alpha1 { @@ -688,6 +691,7 @@ void LensMap::PreProcessFFTWMap(T Wphi_of_k){ alpha2_bar.resize(NN); for( size_t i=0; i Date: Fri, 6 Dec 2019 13:26:15 +0100 Subject: [PATCH 213/227] Removed last check when closing a fits file in the destructor of CPFITS_READ. --- include/cpfits.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/cpfits.h b/include/cpfits.h index c33a2ce3..b1562f21 100644 --- a/include/cpfits.h +++ b/include/cpfits.h @@ -181,7 +181,7 @@ class CPFITS_READ : public CPFITS_BASE { ~CPFITS_READ(){ int status = 0; fits_close_file(fptr, &status); - check_status(status,"Problem closing fits file!"); + //check_status(status,"Problem closing fits file!"); } CPFITS_READ(CPFITS_READ &&ff):CPFITS_BASE(std::move(ff)){}; From 971f36ff1be339fbdee35e86d4251ad23930278c Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Sat, 7 Dec 2019 18:53:25 +0100 Subject: [PATCH 214/227] Corrected a bug in LensHaloMultiMap --- MultiPlane/multimap.cpp | 97 +++++++++++++++++++++-------------------- 1 file changed, 50 insertions(+), 47 deletions(-) diff --git a/MultiPlane/multimap.cpp b/MultiPlane/multimap.cpp index 37a2c188..d32f028d 100644 --- a/MultiPlane/multimap.cpp +++ b/MultiPlane/multimap.cpp @@ -259,7 +259,7 @@ void LensHaloMultiMap::submapPhys(Point_2d ll,Point_2d ur){ ur = (ur - long_range_map.lowerleft)/resolution; - std::cout << "ur = " << ur << std::endl; + //std::cout << "ur = " << ur << std::endl; std::cout << "dim. original " << Noriginal[0] << " " << Noriginal[1] << std::endl; assert(ur[0] < long_range_map.x_range()/resolution + 0.1); @@ -316,67 +316,70 @@ void LensHaloMultiMap::submap( }else{ - std::vector first(2); - std::vector last(2); + std::vector first(2); + std::vector last(2); - first[0] = lower_left[0] - border_width + 1; - first[1] = lower_left[1] - border_width + 1; + first[0] = lower_left[0] - border_width + 1; + first[1] = lower_left[1] - border_width + 1; - last[0] = upper_right[0] + border_width + 1; - last[1] = upper_right[1] + border_width + 1; + last[0] = upper_right[0] + border_width + 1; + last[1] = upper_right[1] + border_width + 1; - double area = resolution*resolution/mass_unit; - LensMap map; + double area = resolution*resolution/mass_unit; + LensMap map; - - if( (first[0] > 1)*(first[1] > 1)*(last[0] <= Noriginal[0])*(last[1] <= Noriginal[1]) ){ - //map.read_sub(fitsfilename,first,last,getDist()); - //map.read_sub(ff,first,last,getDist()); - map.read_sub(cpfits,first,last,getDist()); - - // convert to relative surface angular surface density - for(auto &p : map.surface_density){ - p /= area; - p -= ave_ang_sd; - } + if( (first[0] > 1)*(first[1] > 1)*(last[0] <= Noriginal[0])*(last[1] <= Noriginal[1]) ){ + + map.nx = last[0] - first[0] + 1; + map.ny = last[1] - first[1] + 1; + map.surface_density.resize( map.nx * map.ny ); + cpfits.read_subset(&map.surface_density[0],first.data(),last.data() ); + + // convert to relative surface angular surface density + for(auto &p : map.surface_density){ + p /= area; + p -= ave_ang_sd; + } + + map.boxlMpc = map.nx*resolution; - }else{ + }else{ - // case where subfield overlaps edge - size_t nx_big = map.nx = last[0] - first[0] + 1; - size_t ny_big = map.ny = last[1] - first[1] + 1; + // case where subfield overlaps edge + size_t nx_big = map.nx = last[0] - first[0] + 1; + size_t ny_big = map.ny = last[1] - first[1] + 1; - std::vector first_sub(2); - std::vector last_sub(2); + std::vector first_sub(2); + std::vector last_sub(2); - map.surface_density.resize(nx_big * ny_big,0); + map.surface_density.resize(nx_big * ny_big,0); - std::valarray v; - first_sub[0] = MAX(first[0],1); - first_sub[1] = MAX(first[1],1); - last_sub[0] = MIN(last[0],Noriginal[0]); - last_sub[1] = MIN(last[1],Noriginal[1]); + std::valarray v; + first_sub[0] = MAX(first[0],1); + first_sub[1] = MAX(first[1],1); + last_sub[0] = MIN(last[0],Noriginal[0]); + last_sub[1] = MIN(last[1],Noriginal[1]); - Point_2d offset(first_sub[0] - first[0] , first_sub[1] - first[1] ); + Point_2d offset(first_sub[0] - first[0] , first_sub[1] - first[1] ); - long nx = last_sub[0] - first_sub[0] + 1; - long ny = last_sub[1] - first_sub[1] + 1; + long nx = last_sub[0] - first_sub[0] + 1; + long ny = last_sub[1] - first_sub[1] + 1; - v.resize(nx*ny); - cpfits.read_subset(&v[0], first_sub.data(), last_sub.data() ); + v.resize(nx*ny); + cpfits.read_subset(&v[0], first_sub.data(), last_sub.data() ); - long jj = offset[1]; - for(long j = 0; j < ny ; ++j,++jj){ - long ii = offset[0]; - for(long i = 0; i < nx ; ++i,++ii){ - map.surface_density[ii + jj*nx_big] = v[i + j*nx] / area - ave_ang_sd;; + long jj = offset[1]; + for(long j = 0; j < ny ; ++j,++jj){ + long ii = offset[0]; + for(long i = 0; i < nx ; ++i,++ii){ + map.surface_density[ii + jj*nx_big] = v[i + j*nx] / area - ave_ang_sd;; + } } - } - // need to do overlap region - map.boxlMpc = map.nx*resolution; - } + // need to do overlap region + map.boxlMpc = map.nx*resolution; + } //map.PreProcessFFTWMap(1.0,wsr); map.PreProcessFFTWMap(wsr); @@ -788,7 +791,7 @@ void LensMap::read_sub(CPFITS_READ &cpfits ){ //int bitpix; - std::vector sizes; + std::vector sizes(2); cpfits.imageDimensions(sizes); //long nx_orig = h0.axis(0); From cff45058257386c28e999852f90e3bba9084c3e0 Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Sun, 8 Dec 2019 20:36:05 +0100 Subject: [PATCH 215/227] take out some comments --- MultiPlane/multimap.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/MultiPlane/multimap.cpp b/MultiPlane/multimap.cpp index d32f028d..48b25152 100644 --- a/MultiPlane/multimap.cpp +++ b/MultiPlane/multimap.cpp @@ -260,8 +260,8 @@ void LensHaloMultiMap::submapPhys(Point_2d ll,Point_2d ur){ ur = (ur - long_range_map.lowerleft)/resolution; //std::cout << "ur = " << ur << std::endl; - std::cout << "dim. original " << Noriginal[0] - << " " << Noriginal[1] << std::endl; + //std::cout << "dim. original " << Noriginal[0] + //<< " " << Noriginal[1] << std::endl; assert(ur[0] < long_range_map.x_range()/resolution + 0.1); assert(ur[1] < long_range_map.y_range()/resolution + 0.1); From eb82826865f0bad97edc0f4509c3ec51dfa6fff8 Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Mon, 9 Dec 2019 18:24:20 +0100 Subject: [PATCH 216/227] Some spelling corrections. --- TreeCode_link/grid_maintenance.cpp | 2 +- include/grid_maintenance.h | 2 +- include/particle_halo.h | 3 +-- 3 files changed, 3 insertions(+), 4 deletions(-) diff --git a/TreeCode_link/grid_maintenance.cpp b/TreeCode_link/grid_maintenance.cpp index 284a2cfa..d626efe6 100644 --- a/TreeCode_link/grid_maintenance.cpp +++ b/TreeCode_link/grid_maintenance.cpp @@ -1140,7 +1140,7 @@ PixelMap Grid::MapSurfaceBrightness(double resolution){ /** \brief Make a fits map that is automatically centered on the grid and has approximately the same range as the grid. Nx can be used to change the resolution. Nx = grid.getInitNgrid() will give the initial grid resolution */ -void Grid::writePixeFits( +void Grid::writePixelFits( size_t Nx /// number of pixels in image in x dimension ,LensingVariable lensvar /// which quantity is to be displayed ,std::string filename /// file name for image -- .kappa.fits, .gamma1.fits, etc will be appended diff --git a/include/grid_maintenance.h b/include/grid_maintenance.h index 83f638fa..1fc1b372 100644 --- a/include/grid_maintenance.h +++ b/include/grid_maintenance.h @@ -70,7 +70,7 @@ struct Grid{ ,std::string filename /// file name for image -- .kappa.fits, .gamma1.fits, etc will be appended ); - void writePixeFits(size_t Nx /// number of pixels in image in x dimension + void writePixelFits(size_t Nx /// number of pixels in image in x dimension ,LensingVariable lensvar /// which quantity is to be displayed ,std::string filename /// file name for image -- .kappa.fits, .gamma1.fits, etc will be appended ); diff --git a/include/particle_halo.h b/include/particle_halo.h index 6f6b5d64..0afa025e 100644 --- a/include/particle_halo.h +++ b/include/particle_halo.h @@ -52,7 +52,6 @@ class LensHaloParticles : public LensHalo ); LensHaloParticles(std::vector &pvector /// list of particles pdata[][i] should be the position in physical Mpc, the class takes possession of the data and leaves the vector empty - ,size_t Npoints /// redshift of origin ,float redshift /// redshift of origin ,const COSMOLOGY& cosmo /// cosmology ,Point_2d theta_rotate /// rotation of particles around the origin @@ -325,7 +324,7 @@ void LensHaloParticles::set_up( // rotate positions rotate_particles(theta_rotate[0],theta_rotate[1]); - qtree = new TreeQuadParticles >(pp,Npoints,-1,-1,0,20); + qtree = new TreeQuadParticles(pp,Npoints,-1,-1,0,20); } From 66ded8c16d46ff14312fe28e1194362c31ce0f69 Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Wed, 11 Dec 2019 16:13:03 +0100 Subject: [PATCH 217/227] Fixed a bug and updated SourceUniform and SourceGaussian --- AnalyticNSIE/source.cpp | 17 ++++++++--------- MultiPlane/lens_halos.cpp | 1 - include/particle_halo.h | 10 +++++----- include/source.h | 23 +++++++++++++++++++---- include/standard.h | 4 ++++ 5 files changed, 36 insertions(+), 19 deletions(-) diff --git a/AnalyticNSIE/source.cpp b/AnalyticNSIE/source.cpp index b4f98dc1..1e441cad 100644 --- a/AnalyticNSIE/source.cpp +++ b/AnalyticNSIE/source.cpp @@ -15,24 +15,23 @@ using namespace std; -SourceUniform::SourceUniform(InputParams& params) : Source(){ - assignParams(params); -} +//SourceUniform::SourceUniform(InputParams& params) : Source(){ +// assignParams(params); +//} -SourceUniform::SourceUniform(PosType *position,PosType z,PosType radius_in_radians): +SourceUniform::SourceUniform(Point_2d position,PosType z,PosType radius_in_radians): Source() { source_r = radius_in_radians; - source_x[0] = position[0]; - source_x[1] = position[1]; + source_x = position; setSBlimit_magarcsec(100.); zsource = z; } -SourceGaussian::SourceGaussian(InputParams& params) : Source(){ - assignParams(params); -} +//SourceGaussian::SourceGaussian(InputParams& params) : Source(){ +// assignParams(params); +//} SourceBLR::SourceBLR(InputParams& params) : Source(){ assignParams(params); diff --git a/MultiPlane/lens_halos.cpp b/MultiPlane/lens_halos.cpp index e55a9bca..98ae1bf2 100644 --- a/MultiPlane/lens_halos.cpp +++ b/MultiPlane/lens_halos.cpp @@ -219,7 +219,6 @@ LensHalo & LensHalo::operator=(const LensHalo &h){ return *this; }; - void LensHalo::initFromMassFunc(float my_mass, float my_Rsize, float my_rscale , PosType my_slope, long *seed){ mass = my_mass; diff --git a/include/particle_halo.h b/include/particle_halo.h index 0afa025e..15d8e424 100644 --- a/include/particle_halo.h +++ b/include/particle_halo.h @@ -58,7 +58,7 @@ class LensHaloParticles : public LensHalo ,bool recenter /// center on center of mass ,float MinPSize /// minimum particle size ,bool verbose=false - ):min_size(MinPSize),multimass(true) + ):LensHalo(redshift,cosmo), min_size(MinPSize),multimass(true) { std::swap(pvector,trash_collector); pp = trash_collector.data(); @@ -148,7 +148,7 @@ class LensHaloParticles : public LensHalo ,bool recenter /// center on center of mass ,float MinPSize /// minimum particle size ,bool verbose - ):pp(pdata),min_size(MinPSize),multimass(true),Npoints(Nparticles) + ):LensHalo(redshift,cosmo),pp(pdata),min_size(MinPSize),multimass(true),Npoints(Nparticles) { set_up(redshift,cosmo,theta_rotate,recenter,verbose); } @@ -187,7 +187,7 @@ LensHaloParticles::LensHaloParticles(const std::string& simulation_filena ,PosType MinPSize ,bool verbose ) -:min_size(MinPSize),multimass(my_multimass),simfile(simulation_filename) +:LensHalo(redshift,cosmo),min_size(MinPSize),multimass(my_multimass),simfile(simulation_filename) { LensHalo::setZlens(redshift); @@ -276,8 +276,8 @@ void LensHaloParticles::set_up( ,bool verbose ){ - LensHalo::setZlens(redshift); - LensHalo::setCosmology(cosmo); + //LensHalo::setZlens(redshift); + //LensHalo::setCosmology(cosmo); LensHalo::set_flag_elliptical(false); stars_N = 0; diff --git a/include/source.h b/include/source.h index 4a33b45b..c547f4a5 100644 --- a/include/source.h +++ b/include/source.h @@ -229,8 +229,8 @@ class SourceShapelets: public Source{ /// A uniform surface brightness circular source. class SourceUniform : public Source{ public: - SourceUniform(InputParams& params); - SourceUniform(PosType *position /// postion on the sky in radians + //SourceUniform(InputParams& params); + SourceUniform(Point_2d position /// postion on the sky in radians ,PosType z /// redshift of source ,PosType radius_in_radians /// radius of source in radians ); @@ -242,10 +242,25 @@ class SourceUniform : public Source{ PosType getTotalFlux() const {return PI*source_r*source_r;} }; -/// A source with a Gaussian surface brightness profile +/*** \brief A source with a Gaussian surface brightness profile + + This is normalized so that it is equal to 1 at the center + */ class SourceGaussian : public Source{ public: - SourceGaussian(InputParams& params); + //SourceGaussian(InputParams& params); + SourceGaussian( + Point_2d position /// postion of source (radians) + ,double r_size /// angular scale size (radians) + ,double z): /// redshift + Source(),source_gauss_r2(r_size*r_size) + { + zsource = z; + source_r = 5*sqrt(source_gauss_r2); + source_x = position; + setSBlimit_magarcsec(100.); + } + ~SourceGaussian(); /// internal scale parameter diff --git a/include/standard.h b/include/standard.h index 9621deb9..03dfb8c7 100644 --- a/include/standard.h +++ b/include/standard.h @@ -57,6 +57,10 @@ #define inv_hplanck 1.50919e26 // in 1/(erg*sec) #endif +#ifndef kmpersecTOmpcperday +#define kmpersecTOmpcperday 2.80003e-15 +#endif + #ifndef error_message #define error_message #define ERROR_MESSAGE() std::cout << "ERROR: file: " << __FILE__ << " line: " << __LINE__ << std::endl; From f8e8b47376bf851b936c29d2a6b27baa6abf68ba Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Thu, 12 Dec 2019 16:29:43 +0100 Subject: [PATCH 218/227] Fixed scaling of k modes in LensMap::PreProcessFFTWMap() so that it works on rectangular regions. Did some cleanup. --- MultiPlane/multimap.cpp | 20 +++++++------------- include/multimap.h | 19 +++---------------- 2 files changed, 10 insertions(+), 29 deletions(-) diff --git a/MultiPlane/multimap.cpp b/MultiPlane/multimap.cpp index f1c2f8ed..e9e50d74 100644 --- a/MultiPlane/multimap.cpp +++ b/MultiPlane/multimap.cpp @@ -70,7 +70,6 @@ LensHalo(redshift,c),write_shorts(write_subfields),single_grid(single_grid_mode) bool long_range_file_exists = Utilities::IO::file_exists(lr_file); ///long_range_file_exists = false; - if( long_range_file_exists && !single_grid ){ std::cout << " reading file " << lr_file << " .. " << std::endl; @@ -129,7 +128,7 @@ LensHalo(redshift,c),write_shorts(write_subfields),single_grid(single_grid_mode) long_range_map.nx = submap.nx/desample; double lr_res_x = long_range_map.boxlMpc / long_range_map.nx; - // get the ny that makes the pixels clossest to square + // get the ny that makes the pixels closest to square double Ly = submap.y_range(); long_range_map.ny = (int)( Ly/lr_res_x ); @@ -215,10 +214,10 @@ LensHalo(redshift,c),write_shorts(write_subfields),single_grid(single_grid_mode) } } - double padd = 1 + 2 * border_width * resolution / long_range_map.x_range(); - padd = MAX(padd,2); + double padd_lr = 1 + 2 * border_width * resolution / long_range_map.x_range(); + padd_lr = MAX(padd_lr,2); - long_range_map.PreProcessFFTWMap(padd,wlr); + long_range_map.PreProcessFFTWMap(padd_lr,wlr); long_range_map.write("!" + lr_file); CPFITS_WRITE tmp_cpfits(lr_file,true); @@ -470,7 +469,7 @@ bool LensMap::evaluate(const double *x,float &sigma,float *gamma,double *alpha) + c * gamma2_bar[index+1+nx] + d * gamma2_bar[index+nx]; gamma[2] = 0.0; - // ??? + /* if(isnan(gamma[1])){ std::cerr << index+1+nx << " < " << gamma2_bar.size() << std::endl; std::cerr << alpha[0] << " " << alpha[1] << std::endl; @@ -481,7 +480,7 @@ bool LensMap::evaluate(const double *x,float &sigma,float *gamma,double *alpha) assert(!isnan(gamma[1])); - } + }*/ return false; } @@ -505,25 +504,20 @@ void LensHaloMultiMap::force_halo(double *alpha // interpolate from the maps if(single_grid){ - long_range_map.evaluate(xx,*kappa,gamma,alpha); return; } if( (xx[0] >= short_range_map.lowerleft[0])*(xx[0] <= short_range_map.upperright[0]) *(xx[1] >= short_range_map.lowerleft[1])*(xx[1] <= short_range_map.upperright[1]) - ){ + ){ long_range_map.evaluate(xx,*kappa,gamma,alpha); - assert(gamma[0] == gamma[0] && gamma[1] == gamma[1]); /// ???? - float t_kappa,t_gamma[3]; double t_alpha[2]; short_range_map.evaluate(xx,t_kappa,t_gamma,t_alpha); - assert(t_gamma[0] == t_gamma[0] && t_gamma[1] == t_gamma[1]); /// ???? - alpha[0] += t_alpha[0]; alpha[1] += t_alpha[1]; gamma[0] += t_gamma[0]; diff --git a/include/multimap.h b/include/multimap.h index 0aa57ee4..4ec75165 100644 --- a/include/multimap.h +++ b/include/multimap.h @@ -96,16 +96,12 @@ struct LensMap{ #ifdef ENABLE_FFTW // this calculates the other lensing quantities from the density map - //void PreProcessFFTWMap(float zerosize); - - //static double identity(double x){return 1;} template void PreProcessFFTWMap(float zerosize,T Wphi_of_k,bool do_alpha = true); template void PreProcessFFTWMap(T Wphi_of_k,bool do_alpha = true); - //void PreProcessFFTWMap(float zerosize,std::function Wphi_of_k = identity); -#endif + #endif }; @@ -301,7 +297,6 @@ class LensHaloMultiMap : public LensHalo * generalized to work with rectangular maps */ -//void LensMap::PreProcessFFTWMap(float zerosize,std::function Wphi_of_k){ template void LensMap::PreProcessFFTWMap(float zerosize,T Wphi_of_k,bool do_alpha){ @@ -326,6 +321,7 @@ void LensMap::PreProcessFFTWMap(float zerosize,T Wphi_of_k,bool do_alpha){ for( int i=0; i kys(Nny); for( int j=0; j kys(ny); for( int j=0; j extended_map( Nnx*Nny ); fftw_complex *fphi = new fftw_complex[ny*Nkx]; From f259df101577291b9677a715a264a043260e0ef2 Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Mon, 16 Dec 2019 19:07:32 +0100 Subject: [PATCH 219/227] Renamed LensVariable DT to DELAYT --- ImageProcessing/pixelize.cpp | 2 +- TreeCode_link/find_crit.cpp | 6 +++--- TreeCode_link/grid_maintenance.cpp | 6 +++--- TreeCode_link/gridmap.cpp | 6 +++--- include/standard.h | 2 +- 5 files changed, 11 insertions(+), 11 deletions(-) diff --git a/ImageProcessing/pixelize.cpp b/ImageProcessing/pixelize.cpp index 7d13f855..9a51ca1c 100644 --- a/ImageProcessing/pixelize.cpp +++ b/ImageProcessing/pixelize.cpp @@ -1414,7 +1414,7 @@ void PixelMap::AddGrid_(const PointList &list,LensingVariable val){ case INVMAG: tmp = (*pl_it)->invmag/resolution/resolution; break; - case DT: + case DELAYT: tmp = (*pl_it)->dt/resolution/resolution; break; default: diff --git a/TreeCode_link/find_crit.cpp b/TreeCode_link/find_crit.cpp index 08d0de4f..e2d764ef 100644 --- a/TreeCode_link/find_crit.cpp +++ b/TreeCode_link/find_crit.cpp @@ -1772,7 +1772,7 @@ void ImageFinding::find_contour( ,bool ordercurve /// Order the curve so that it can be drawn or used to find the winding number. ,bool dividecurves /// Divide the critical curves into seporate curves by whether they are attached ,double contour_value /// value at which the contour is wanted - ,LensingVariable contour_type /// KAPPA, INVMAG or DT + ,LensingVariable contour_type /// KAPPA, INVMAG or DELAYT ,bool verbose ){ @@ -1803,7 +1803,7 @@ void ImageFinding::find_contour( value = (*i_tree_pointlist_it)->invmag; maxval = minpoint->invmag; break; - case DT: + case DELAYT: value = (*i_tree_pointlist_it)->dt; maxval = minpoint->dt; break; @@ -1864,7 +1864,7 @@ void ImageFinding::find_contour( case INVMAG: value = newpoint_kist.getCurrent()->invmag; break; - case DT: + case DELAYT: value = newpoint_kist.getCurrent()->dt; break; default: diff --git a/TreeCode_link/grid_maintenance.cpp b/TreeCode_link/grid_maintenance.cpp index d626efe6..0660f899 100644 --- a/TreeCode_link/grid_maintenance.cpp +++ b/TreeCode_link/grid_maintenance.cpp @@ -1051,7 +1051,7 @@ void Grid::writeFits( std::string tag; switch (lensvar) { - case DT: + case DELAYT: tag = ".dt.fits"; break; case ALPHA1: @@ -1171,7 +1171,7 @@ void Grid::writeFitsUniform( std::string tag; switch (lensvar) { - case DT: + case DELAYT: tag = ".dt.fits"; break; case ALPHA1: @@ -1331,7 +1331,7 @@ void Grid::writePixelMapUniform_(const PointList &list,PixelMap *map,LensingVari case INVMAG: tmp = (*list_it)->invmag; break; - case DT: + case DELAYT: tmp = (*list_it)->dt; break; default: diff --git a/TreeCode_link/gridmap.cpp b/TreeCode_link/gridmap.cpp index 7cc6aa82..fdab5451 100644 --- a/TreeCode_link/gridmap.cpp +++ b/TreeCode_link/gridmap.cpp @@ -297,7 +297,7 @@ PixelMap GridMap::writePixelMapUniform( for(size_t i=0 ; i Date: Tue, 17 Dec 2019 16:45:16 +0100 Subject: [PATCH 220/227] Made changes to time-delay calculation. Added LensHalo::map_variables() to output variable for individual LensHalo. Included potential calculation in LensMap and mokaMap classes. --- AnalyticNSIE/base_analens.cpp | 6 +-- AnalyticNSIE/hernquist_lens.cpp | 1 + FullRange/internal_rayshooter_multi.cpp | 23 +++++++---- MultiPlane/MOKAfits.cpp | 29 ++++++++++++-- MultiPlane/lens.cpp | 19 +++++++-- MultiPlane/lens_halos.cpp | 44 +++++++++++++++++++++ include/image_processing.h | 1 + include/lens.h | 14 +++++-- include/lens_halos.h | 33 ++++++++++++++-- include/multimap.h | 52 +++++++++++++++++++++++-- include/source.h | 1 + include/standard.h | 2 +- 12 files changed, 196 insertions(+), 29 deletions(-) diff --git a/AnalyticNSIE/base_analens.cpp b/AnalyticNSIE/base_analens.cpp index eaf94fd3..990417a8 100644 --- a/AnalyticNSIE/base_analens.cpp +++ b/AnalyticNSIE/base_analens.cpp @@ -15,9 +15,9 @@ using namespace std; void LensHaloBaseNSIE::force_halo( PosType *alpha /// mass/PhysMpc - ,KappaType *kappa /// surface mass density - ,KappaType *gamma - ,KappaType *phi + ,KappaType *kappa /// surface mass density , mass / /PhysMpc/PhysMpc + ,KappaType *gamma /// mass / /PhysMpc/PhysMpc + ,KappaType *phi /// mass ,PosType const *xcm /// Position in PhysMpc ,bool subtract_point /// if true contribution from a point mass is subtracted ,PosType screening /// the factor by which to scale the mass for screening of the point mass subtraction diff --git a/AnalyticNSIE/hernquist_lens.cpp b/AnalyticNSIE/hernquist_lens.cpp index 37d48ef8..14de7978 100644 --- a/AnalyticNSIE/hernquist_lens.cpp +++ b/AnalyticNSIE/hernquist_lens.cpp @@ -3,6 +3,7 @@ * Author: D. Leier */ +#include "image_processing.h" #include "lens_halos.h" diff --git a/FullRange/internal_rayshooter_multi.cpp b/FullRange/internal_rayshooter_multi.cpp index 860accd1..25feadfb 100644 --- a/FullRange/internal_rayshooter_multi.cpp +++ b/FullRange/internal_rayshooter_multi.cpp @@ -52,6 +52,7 @@ struct TmpParams{ PosType* plane_redshifts; PosType* Dl; PosType* dDl; + PosType* dTl; bool verbose; }; @@ -105,7 +106,7 @@ void Lens::rayshooterInternal( int NLastPlane; - PosType tmpDs,tmpdDs,tmpZs; + PosType tmpDs,tmpdDs,tmpdTs,tmpZs; // If there are no points to shoot, then we quit. if(Npoints == 0) return; @@ -118,10 +119,12 @@ void Lens::rayshooterInternal( assert(NLastPlane <= lensing_planes.size()); tmpDs = Dl[index_of_new_sourceplane]; tmpdDs = dDl[index_of_new_sourceplane]; + tmpdTs = dTl[index_of_new_sourceplane]; tmpZs = plane_redshifts[index_of_new_sourceplane]; Dl[index_of_new_sourceplane] = Ds_implant; dDl[index_of_new_sourceplane] = dDs_implant; + dTl[index_of_new_sourceplane] = dTs_implant; plane_redshifts[index_of_new_sourceplane] = zs_implant; } else{ NLastPlane = lensing_planes.size(); } @@ -156,6 +159,7 @@ void Lens::rayshooterInternal( thread_params[i].plane_redshifts = &plane_redshifts[0]; thread_params[i].Dl = &Dl[0]; thread_params[i].dDl = &dDl[0]; + thread_params[i].dTl = &dTl[0]; thread_params[i].NPlanes = NLastPlane; thread_params[i].verbose = RSIVerbose; rc = pthread_create(&threads[i], NULL, compute_rays_parallel, (void*) &thread_params[i]); @@ -175,6 +179,7 @@ void Lens::rayshooterInternal( // The initial values for the plane are reset here Dl[index_of_new_sourceplane] = tmpDs; dDl[index_of_new_sourceplane] = tmpdDs; + dTl[index_of_new_sourceplane] = tmpdTs; plane_redshifts[index_of_new_sourceplane] = tmpZs; } @@ -257,7 +262,9 @@ void *compute_rays_parallel(void *_p) // Time delay at first plane : position on the observer plane is (0,0) => no need to take difference of positions. - p->i_points[i].dt = 0.5*( p->i_points[i].image->x[0]*p->i_points[i].image->x[0] + p->i_points[i].image->x[1]*p->i_points[i].image->x[1] )/ p->dDl[0] ; + p->i_points[i].dt = 0; + + //0.5*( p->i_points[i].image->x[0]*p->i_points[i].image->x[0] + p->i_points[i].image->x[1]*p->i_points[i].image->x[1] )/ p->dDl[0] ; // TEST : showing initial quantities @@ -366,7 +373,7 @@ void *compute_rays_parallel(void *_p) // Geometric time delay with added potential - p->i_points[i].dt += 0.5*( (xplus[0] - xminus[0])*(xplus[0] - xminus[0]) + (xplus[1] - xminus[1])*(xplus[1] - xminus[1]) )/p->dDl[j+1] - (1 + p->plane_redshifts[j]) * phi * p->charge ; /// in Mpc + p->i_points[i].dt += 0.5*( (xplus[0] - xminus[0])*(xplus[0] - xminus[0]) + (xplus[1] - xminus[1])*(xplus[1] - xminus[1]) ) * p->dTl[j+1] /p->dDl[j+1] /p->dDl[j+1] - phi * p->charge ; /// in Mpc ??? // Check that the 1+z factor must indeed be there (because the x positions have been rescaled, so it may be different compared to the draft). @@ -382,7 +389,7 @@ void *compute_rays_parallel(void *_p) // Subtracting off a term that makes the unperturbed ray to have zero time delay - p->i_points[i].dt -= 0.5*( p->i_points[i].image->x[0]*p->i_points[i].image->x[0] + p->i_points[i].image->x[1]*p->i_points[i].image->x[1] ) / p->Dl[p->NPlanes]; + //p->i_points[i].dt -= 0.5*( p->i_points[i].image->x[0]*p->i_points[i].image->x[0] + p->i_points[i].image->x[1]*p->i_points[i].image->x[1] ) / p->Dl[p->NPlanes]; // Convert units back to angles. @@ -422,8 +429,6 @@ void *compute_rays_parallel(void *_p) // =============================== if(verbose) std::cout << "RSI final : X X | " << p->Dl[p->NPlanes] << " | " << p->i_points[i].kappa << " " << p->i_points[i].gamma[0] << " " << p->i_points[i].gamma[1] << " " << p->i_points[i].gamma[2] << " " << p->i_points[i].invmag << " | " << p->i_points[i].dt << std::endl ; - - } // End of the main loop. return 0; @@ -777,7 +782,7 @@ void Lens::info_rayshooter( int NLastPlane = lensing_planes.size() ; - PosType tmpDs,tmpdDs,tmpZs; + PosType tmpDs,tmpdDs,tmpdTs,tmpZs; // If there are no points to shoot, then we quit. @@ -789,10 +794,12 @@ void Lens::info_rayshooter( assert(NLastPlane <= lensing_planes.size()); tmpDs = Dl[index_of_new_sourceplane]; tmpdDs = dDl[index_of_new_sourceplane]; + tmpdTs = dTl[index_of_new_sourceplane]; tmpZs = plane_redshifts[index_of_new_sourceplane]; Dl[index_of_new_sourceplane] = Ds_implant; dDl[index_of_new_sourceplane] = dDs_implant; + dTl[index_of_new_sourceplane] = dTs_implant; plane_redshifts[index_of_new_sourceplane] = zs_implant; } else @@ -801,6 +808,7 @@ void Lens::info_rayshooter( tmpDs = cosmo.angDist(zsource); assert(plane_redshifts[NLastPlane] == zsource); tmpdDs = cosmo.angDist(plane_redshifts[NLastPlane-1], zsource); + tmpdTs = cosmo.radDist(plane_redshifts[NLastPlane-1], zsource); tmpZs = zsource ; } @@ -910,6 +918,7 @@ void Lens::info_rayshooter( // The initial values for the plane are reset here Dl[index_of_new_sourceplane] = tmpDs; dDl[index_of_new_sourceplane] = tmpdDs; + dTl[index_of_new_sourceplane] = tmpdTs; plane_redshifts[index_of_new_sourceplane] = tmpZs; } diff --git a/MultiPlane/MOKAfits.cpp b/MultiPlane/MOKAfits.cpp index a0b943bc..35ef8e4a 100644 --- a/MultiPlane/MOKAfits.cpp +++ b/MultiPlane/MOKAfits.cpp @@ -753,14 +753,37 @@ void MOKAmap::PreProcessFFTWMap(float zerosize){ } } } - + // phi - this is done over because of the window in Fourier space + { + + for( int i=0; i 0) dDs_implant = dDl[j]; - else dDs_implant = Ds_implant; + if(j > 0) dDs_implant = dDl[j]; + else dDs_implant = Ds_implant; + + if(j > 0) dTs_implant = dTl[j]; + else dTs_implant = cosmo.radDist(0,z); }else{ // if nearest==false or the source is at higher redshift than the last plane use the real redshift Ds_implant = Ds; zs_implant = z; + if(j > 0) dDs_implant = cosmo.coorDist(plane_redshifts[j-1],z); //Ds - Dl[j-1]; else dDs_implant = Ds; + + if(j > 0) dTs_implant = cosmo.radDist(plane_redshifts[j-1],z); //Ds - Dl[j-1]; + else dTs_implant = cosmo.radDist(0,z); } if(verbose) std::cout << "Source on plane " << j << " zs " << zs_implant << " Ds " << Ds << " dDs " << dDs_implant << std::endl; diff --git a/MultiPlane/lens_halos.cpp b/MultiPlane/lens_halos.cpp index bcf6e82f..d601ddb9 100644 --- a/MultiPlane/lens_halos.cpp +++ b/MultiPlane/lens_halos.cpp @@ -259,6 +259,50 @@ void LensHalo::PrintStars(bool show_stars) } } +PixelMap LensHalo::map_variables( + LensingVariable lensvar /// lensing variable - KAPPA, ALPHA1, ALPHA2, GAMMA1, GAMMA2 or PHI + ,size_t Nx + ,size_t Ny + ,double res /// resolution in physical Mpc on the lens plane +){ + + Point_2d center; + PixelMap map(center.data(),Nx,Ny,res); + Point_2d x,alpha; + size_t N = Nx*Ny; + KappaType kappa,phi,gamma[3]; + for(size_t i = 0 ; i < N ; ++i){ + map.find_position(x.data(),i); + force_halo(alpha.data(),&kappa,gamma,&phi,x.data()); + + switch (lensvar) { + case ALPHA1: + map[i] = alpha[0]; + break; + case ALPHA2: + map[i] = alpha[1]; + break; + case GAMMA1: + map[i] = gamma[0]; + break; + case GAMMA2: + map[i] = gamma[1]; + break; + case KAPPA: + map[i] = kappa; + break; + case PHI: + map[i] = phi; + break; + + default: + break; + } + } + return map; +} + + /// calculates the deflection etc. caused by stars alone void LensHalo::force_stars( PosType *alpha /// mass/Mpc diff --git a/include/image_processing.h b/include/image_processing.h index 744fec63..069b2f34 100644 --- a/include/image_processing.h +++ b/include/image_processing.h @@ -14,6 +14,7 @@ #endif #include "utilities_slsim.h" +#include "image_processing.h" #include "source.h" // forward declaration diff --git a/include/lens.h b/include/lens.h index 0adcfba2..4bc03fb2 100644 --- a/include/lens.h +++ b/include/lens.h @@ -396,6 +396,7 @@ class Lens{ cosmo = lens.cosmo; toggle_source_plane = lens.toggle_source_plane; dDs_implant = lens.dDs_implant; + dTs_implant = lens.dTs_implant; zs_implant = lens.zs_implant; Ds_implant = lens.Ds_implant; index_of_new_sourceplane = lens.index_of_new_sourceplane; @@ -415,6 +416,7 @@ class Lens{ Dl = lens.Dl; dDl = lens.dDl; + dTl = lens.dTl; plane_redshifts = lens.plane_redshifts; charge = lens.charge; flag_switch_field_off = lens.flag_switch_field_off; @@ -507,8 +509,10 @@ class Lens{ /// turns source plane on and off bool toggle_source_plane; - /// the distance from the source to the next plane - PosType dDs_implant; + /// the distance from the source to the next plane + PosType dDs_implant; + /// the distance from the source to the next plane + PosType dTs_implant; PosType zs_implant,Ds_implant; /// This is the index of the plane at one larger distance than the new source distance int index_of_new_sourceplane; @@ -603,8 +607,10 @@ class Lens{ std::vector lensing_planes; /// Dl[j = 0...] angular diameter distances, comoving std::vector Dl; - /// dDl[j] is the distance between plane j-1 and j plane, comoving - std::vector dDl; + /// dDl[j] is the distance between plane j-1 and j plane, comoving + std::vector dDl; + /// dTl[j] is the lookback-time between plane j-1 and j plane, comoving + std::vector dTl; /// Redshifts of lens planes, 0...Nplanes. Last one is the source redshift. std::vector plane_redshifts; /// charge for the tree force solver (4*pi*G) diff --git a/include/lens_halos.h b/include/lens_halos.h index 37e05e20..6b59aa6e 100644 --- a/include/lens_halos.h +++ b/include/lens_halos.h @@ -13,6 +13,8 @@ //#include "point.h" #include "quadTree.h" #include "particle_types.h" +#include "image_processing.h" + /** * \brief A base class for all types of lensing "halos" which are any mass distribution that cause lensing. @@ -142,9 +144,16 @@ class LensHalo{ /// set cosmology for halo virtual void setCosmology(const COSMOLOGY& cosmo) {} - /// calculate the lensing properties -- deflection, convergence, and shear - // if not overriden by derived class it uses alpha_h(), gamma_h(), etc. of the - // derived case or for a point source in this class + /* calculate the lensing properties -- deflection, convergence, and shear + if not overwritten by derived class it uses alpha_h(), gamma_h(), etc. of the + derived case or for a point source in this class + + Units : + ALPHA - mass/PhysMpc - ALPHA / Sig_crit / Dl is the deflection in radians + KAPPA - surface mass density , mass / /PhysMpc/PhysMpc - KAPPA / Sig_crit is the convergence + GAMMA - mass / /PhysMpc/PhysMpc - GAMMA / Sig_crit is the shear + PHI - mass - PHI / Sig_crit is the lensing potential + */ virtual void force_halo(PosType *alpha,KappaType *kappa,KappaType *gamma,KappaType *phi,PosType const *xcm,bool subtract_point=false,PosType screening=1.0); /// force tree calculation for stars @@ -208,7 +217,23 @@ class LensHalo{ PosType renormalization(PosType r_max); - + /** \brief Map a PixelMap of the surface, density, potential and potential gradient + + Units : + ALPHA - mass/PhysMpc - ALPHA / Sig_crit / Dl is the deflection in radians + KAPPA - surface mass density , mass / /PhysMpc/PhysMpc - KAPPA / Sig_crit is the convergence + GAMMA - mass / /PhysMpc/PhysMpc - GAMMA / Sig_crit is the shear + PHI - mass - PHI / Sig_crit is the lensing potential + + centred on (0,0) in LenHalo coordinates + */ + PixelMap map_variables( + LensingVariable lensvar /// lensing variable - KAPPA, ALPHA1, ALPHA2, GAMMA1, GAMMA2 or PHI + ,size_t Nx + ,size_t Ny + ,double res /// resolution in physical Mpc on the lens plane + ); + private: size_t idnumber; /// Identification number of halo. It is not always used. PosType Dist; diff --git a/include/multimap.h b/include/multimap.h index 4ec75165..bea35353 100644 --- a/include/multimap.h +++ b/include/multimap.h @@ -554,11 +554,39 @@ void LensMap::PreProcessFFTWMap(float zerosize,T Wphi_of_k,bool do_alpha){ } } + // phi - this is done over because of the window in Fourier space + { + + // build modes for each pixel in the fourier space + for( int i=0; i Date: Wed, 18 Dec 2019 15:17:42 +0100 Subject: [PATCH 221/227] Cleanup some comments. --- MultiPlane/multimap.cpp | 18 ++++-------------- TreeCode_link/utilities.cpp | 1 - 2 files changed, 4 insertions(+), 15 deletions(-) diff --git a/MultiPlane/multimap.cpp b/MultiPlane/multimap.cpp index e9e50d74..c92d990c 100644 --- a/MultiPlane/multimap.cpp +++ b/MultiPlane/multimap.cpp @@ -279,7 +279,6 @@ void LensHaloMultiMap::submap( ){ // check range - if(single_grid) return; if( (upper_right[0] < 0) || (upper_right[1] < 0) || (lower_left[0] >= Noriginal[0] ) @@ -380,12 +379,9 @@ void LensHaloMultiMap::submap( map.boxlMpc = map.nx*resolution; } - //map.PreProcessFFTWMap(1.0,wsr); map.PreProcessFFTWMap(wsr); - //map.PreProcessFFTWMap(1.0,unit); - //map.write("test_field.fits"); - // cut off bounders + // cut off bounders size_t nx = short_range_map.nx = upper_right[0] - lower_left[0] + 1; size_t ny = short_range_map.ny = upper_right[1] - lower_left[1] + 1; @@ -428,10 +424,6 @@ void LensHaloMultiMap::submap( bool LensMap::evaluate(const double *x,float &sigma,float *gamma,double *alpha) { - //double fx = ((x[0] - center[0])/range + 0.5)*(nx-1); - //double fy = ((x[1] - center[1])/range_y + 0.5)*(ny-1); - //std::cout << "( " << fx << " " << fy << " "; - double fx = (x[0] - lowerleft[0]) / x_resolution(); double fy = (x[1] - lowerleft[1]) / y_resolution(); @@ -452,8 +444,6 @@ bool LensMap::evaluate(const double *x,float &sigma,float *gamma,double *alpha) double c = fx*fy; double d = (1-fx)*fy; - //for(auto g : gamma2_bar ) assert(!isnan(g)); // ????? - // bilinear interpolation sigma = a * surface_density[index] + b * surface_density[index+1] + c * surface_density[index+1+nx] + d * surface_density[index+nx]; @@ -719,9 +709,9 @@ void LensMap::Myread(std::string fits_input_file){ /* void LensMap::read_sub(std::string fits_input_file - ,const std::vector &first /// 2d vector for pixel of lower left, (1,1) offset - ,const std::vector &last /// 2d vector for pixel of upper right, (1,1) offset - ,double angDist + ,const std::vector &first /// 2d vector for pixel of lower left, (1,1) offset + ,const std::vector &last /// 2d vector for pixel of upper right, (1,1) offset + ,double angDist ){ //std:: cout << " reading lens density map file: " << fits_input_file << std:: endl; diff --git a/TreeCode_link/utilities.cpp b/TreeCode_link/utilities.cpp index 05af3d7a..a63d1d59 100644 --- a/TreeCode_link/utilities.cpp +++ b/TreeCode_link/utilities.cpp @@ -583,7 +583,6 @@ unsigned long prevpower(unsigned long k){ //double tmp = pow(1./2/M_PI/nx/ny,2)*boxlx*boxly; double tmp = boxlx*boxly * pow(1.0/nx/ny,2); - // fb and fa are then used to compute the power spectrum // build modes for( int i=0; i Date: Wed, 18 Dec 2019 16:20:58 +0100 Subject: [PATCH 222/227] Syntax fix. --- TreeCode_link/TreeDriver.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/TreeCode_link/TreeDriver.cpp b/TreeCode_link/TreeDriver.cpp index 788a3449..c2b860f8 100644 --- a/TreeCode_link/TreeDriver.cpp +++ b/TreeCode_link/TreeDriver.cpp @@ -1511,7 +1511,7 @@ RAY ImageInfo::highestSurfaceBrightnessRay(){ } } - return RAY(p); + return RAY(*p); } ImageInfo & ImageInfo::operator+=(ImageInfo & rhs){ From 2bd6dad144f5b679515beba54e20bc51595b4c23 Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Sun, 22 Dec 2019 16:53:01 +0100 Subject: [PATCH 223/227] Fixed some output for critical curves. --- TreeCode_link/Tree.cpp | 5 +++-- include/grid_maintenance.h | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/TreeCode_link/Tree.cpp b/TreeCode_link/Tree.cpp index d1621ab9..af8e7cdf 100644 --- a/TreeCode_link/Tree.cpp +++ b/TreeCode_link/Tree.cpp @@ -979,7 +979,7 @@ void ImageFinding::printCriticalCurves(std::string filename filename = filename + ".csv"; std::ofstream myfile(filename); - myfile << "caustic_center,caustic_area,critical_center,critical_area,z_source,type" + myfile << "caustic_center_x,caustic_center_y,caustic_area,critical_center_x,critical_center_y,critical_area,z_source,type" << std::endl; for(auto cr : critcurves){ myfile << cr << std::endl; @@ -989,7 +989,8 @@ void ImageFinding::printCriticalCurves(std::string filename std::ostream &operator<<(std::ostream &os, const ImageFinding::CriticalCurve &p) { // caustic_center,caustic_area,critical_center,critical_area,z_source,type - os << p.caustic_center << "," << p.caustic_area << "," << p.critical_center << "," + os << p.caustic_center[0] << "," << p.caustic_center[1] << "," << p.caustic_area + << "," << p.critical_center[0] << "," << p.critical_center[1] << "," << p.critical_area << "," << p.z_source << "," << p.type; return os; diff --git a/include/grid_maintenance.h b/include/grid_maintenance.h index 1fc1b372..280b49a7 100644 --- a/include/grid_maintenance.h +++ b/include/grid_maintenance.h @@ -427,7 +427,7 @@ namespace ImageFinding{ void printCriticalCurves(std::string filename ,const std::vector &critcurves); - + /** \breaf Makes an image of the critical curves. The map will encompose all curves found. The pixel values are the caustic type + 1 ( 2=radial,3=tangential,4=pseudo ) */ From 2cab07b836133ba53288702cc319895aa9d689e6 Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Mon, 23 Dec 2019 18:27:43 +0100 Subject: [PATCH 224/227] Removed CCfits from the documentation. --- INSTALL.md | 1 - include/grid_maintenance.h | 1 + 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/INSTALL.md b/INSTALL.md index 935c5296..0f115933 100644 --- a/INSTALL.md +++ b/INSTALL.md @@ -62,7 +62,6 @@ The *GLAMER* library requires some libraries by default and others are optional. Library | Required by || Description ----------|---------------||----------------------------------------------------- [CFITSIO] | `ENABLE_FITS` |required| Library for reading and writing files in the FITS format. -[CCfits]ยน | `ENABLE_FITS` |required| C++ wrapper for *CFITSIO*. [FFTW] | `ENABLE_FFTW` |required| Library for Fast Fourier Transform algorithms. [GSL] | `ENABLE_GSL` |optional| Library for scientific computation [HDF5 C++] | `ENABLE_HDF5` |optional| I/O in hdf5 format diff --git a/include/grid_maintenance.h b/include/grid_maintenance.h index 280b49a7..9f13e0eb 100644 --- a/include/grid_maintenance.h +++ b/include/grid_maintenance.h @@ -217,6 +217,7 @@ namespace ImageFinding{ std::vector ellipse_curve; PosType z_source; + /// type of caustic, 0 -not defined, 1 -radial, 2 - tangential,3 - pseudo CritType type; /// estimated number of intersections of the caustic, -1 if not set int caustic_intersections; From 18a29f1bdc8e7025894f12ace81b07f97823582b Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Mon, 23 Dec 2019 18:45:18 +0100 Subject: [PATCH 225/227] Updated INSTALL.md --- INSTALL.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/INSTALL.md b/INSTALL.md index 0f115933..42c01568 100644 --- a/INSTALL.md +++ b/INSTALL.md @@ -63,7 +63,7 @@ Library | Required by || Description ----------|---------------||----------------------------------------------------- [CFITSIO] | `ENABLE_FITS` |required| Library for reading and writing files in the FITS format. [FFTW] | `ENABLE_FFTW` |required| Library for Fast Fourier Transform algorithms. -[GSL] | `ENABLE_GSL` |optional| Library for scientific computation +[GSL] | `ENABLE_GSL` |optional| Library for scientific computation [HDF5 C++] | `ENABLE_HDF5` |optional| I/O in hdf5 format [HealPix] | ENABLE_HEALPIX |optional| C++ Healpix interface @@ -231,6 +231,6 @@ Building the project will not build the GLAMER libraries automatically! [ccfitsdoc]: https://heasarc.gsfc.nasa.gov/fitsio/CCfits/html/index.html "CCfits documentation" [fftw]: http://www.fftw.org "FFTW Home Page" [gsl]: http://www.gnu.org/software/gsl/ "GNU Scientific Library" -[HDF5 C++]: https://support.hdfgroup.org/HDF5/doc/cpplus_RM/index.html" -[HealPix]:https://healpix.jpl.nasa.gov/html/Healpix_cxx/index.html" -[conda]:https://docs.conda.io/projects/conda/en/latest/" \ No newline at end of file +[HDF5 C++]: https://support.hdfgroup.org/HDF5/doc/cpplus_RM/index.html "HDF5 C++" +[HealPix]:https://healpix.jpl.nasa.gov/html/Healpix_cxx/index.html"HealPix" +[conda]:https://docs.conda.io/projects/conda/en/latest/ "conda" \ No newline at end of file From fec61a7ece305bb617d135b6c274d91ae6e20ef8 Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Mon, 23 Dec 2019 18:53:38 +0100 Subject: [PATCH 226/227] Upate INSTALL.md --- INSTALL.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/INSTALL.md b/INSTALL.md index 42c01568..99a59b8c 100644 --- a/INSTALL.md +++ b/INSTALL.md @@ -231,6 +231,6 @@ Building the project will not build the GLAMER libraries automatically! [ccfitsdoc]: https://heasarc.gsfc.nasa.gov/fitsio/CCfits/html/index.html "CCfits documentation" [fftw]: http://www.fftw.org "FFTW Home Page" [gsl]: http://www.gnu.org/software/gsl/ "GNU Scientific Library" -[HDF5 C++]: https://support.hdfgroup.org/HDF5/doc/cpplus_RM/index.html "HDF5 C++" -[HealPix]:https://healpix.jpl.nasa.gov/html/Healpix_cxx/index.html"HealPix" -[conda]:https://docs.conda.io/projects/conda/en/latest/ "conda" \ No newline at end of file +[HDF5 C++]: https://support.hdfgroup.org/HDF5/doc/cpplus_RM/ "HDF5 C++" +[HealPix]: https://healpix.jpl.nasa.gov/html/Healpix_cxx/ "HealPix" +[conda]: https://docs.conda.io/projects/conda/en/latest/ "conda" \ No newline at end of file From 24d27d0c44f1718df3f8b99f7469e4c4edf23fea Mon Sep 17 00:00:00 2001 From: RB Metcalf Date: Thu, 23 Jan 2020 15:53:07 +0100 Subject: [PATCH 227/227] Comments. --- include/particle_halo.h | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/include/particle_halo.h b/include/particle_halo.h index 15d8e424..aa997e77 100644 --- a/include/particle_halo.h +++ b/include/particle_halo.h @@ -19,6 +19,8 @@ /** * \brief A class that represents the lensing by a collection of simulation particles. + You can create a LensHaloParticles<> directly from a file, but it is recommended that you use the MakeParticleLenses class to create them and then move them to a Lens object. + Smoothing is done according to the density of particles in 3D. Smoothing sizes are either read in from a file (names simulation_filename + "." + Nsmooth + "sizes") or calculated if the file does not exist (in which case the file is created). This can be @@ -38,7 +40,6 @@ class LensHaloParticles : public LensHalo { public: - LensHaloParticles(const std::string& simulation_filename /// name of data files ,SimFileFormat format /// format of data file ,PosType redshift /// redshift of origin @@ -838,7 +839,7 @@ class MakeParticleLenses{ } /// recenter the particles to 3d point in physical Mpc/h units - /// If the halos have already been created they will be destroyed. + /// If the LensHalos have already been created they will be destroyed. void Recenter(Point_3d x); void CreateHalos(const COSMOLOGY &cosmo,double redshift);