12#include "lens_halos.h"
13#include "grid_maintenance.h"
25 void initialize(
size_t _nx_,
size_t _ny_){
29 std::vector<double> rdummy(nx*ny);
30 size_t Nkx = (nx/2+1);
31 fftw_complex *cdummy =
new fftw_complex[ny*Nkx];
33 plan_r2c = fftw_plan_dft_r2c_2d(ny,nx,rdummy.data(),cdummy
35 plan_c2r = fftw_plan_dft_c2r_2d(ny,nx,cdummy,rdummy.data()
41 fftw_destroy_plan(plan_r2c);
42 fftw_destroy_plan(plan_c2r);
58 LensMap():nx(0),ny(0),boxlMpc(0),angular_pixel_size(0){};
66 std::valarray<float> alpha1_bar;
67 std::valarray<float> alpha2_bar;
68 std::valarray<float> gamma1_bar;
69 std::valarray<float> gamma2_bar;
70 std::valarray<float> phi_bar;
74 double angular_pixel_size;
81 double y_resolution(){
return (
upperright[1]-lowerleft[1])/ny;}
83 double x_range(){
return boxlMpc;}
85 double y_range(){
return (
upperright[1]-lowerleft[1]);}
87 bool evaluate(
const PosType *x,KappaType &sigma,KappaType *gamma,PosType *alpha);
89 LensMap(std::string fits_input_file,
double angDist){
90 read(fits_input_file,angDist);
94 void read(std::string input_fits,
double angDist);
96 void Myread(std::string fits_input_file);
101 void read_header(std::string input_fits,
double angDist);
111 ,std::vector<long> &first
112 ,std::vector<long> &last
116 void write(std::string filename);
128 ,my_fftw_plan &plan_padded
129 ,
bool do_alpha =
true);
134 ,
bool do_alpha =
true);
140 ,
bool do_alpha =
true);
144 ,
bool do_alpha =
true);
146 void make_fftw_plans(
151 size_t Nnx=int(zerosize*nx);
152 size_t Nny=int(zerosize*ny);
157 std::vector<double> rdummy(Nnx*Nny);
158 size_t Nkx = (Nnx/2+1);
159 fftw_complex *cdummy =
new fftw_complex[Nny*Nkx];
161 plans.plan_r2c = fftw_plan_dft_r2c_2d(Nny,Nnx,rdummy.data(),cdummy
163 plans.plan_c2r = fftw_plan_dft_c2r_2d(Nny,Nnx,cdummy,rdummy.data()
182 ,std::string dir_data
186 ,
int number_of_subfields
188 ,
bool write_subfields =
false
189 ,std::string dir_scratch =
""
190 ,
bool subtract_ave =
true
199 plans_set_up =
false;
207 static my_fftw_plan plan_long_range;
209 static std::mutex mutex_multimap;
210 static bool plans_set_up;
211 static size_t nx_sub;
212 static size_t ny_sub;
213 static size_t nx_long;
214 static size_t ny_long;
215 static size_t nx_sub_extended;
216 static size_t ny_sub_extended;
231 ,
const std::vector<long> &lower_left_pix
236 void resetsubmapAngular(
int i,
Point_2d ll){
237 if(i >= short_range_maps.size())
throw std::invalid_argument(
"");
242 void force_halo(
double *alpha,KappaType *kappa,KappaType *gamma,KappaType *phi,
double const *xcm,
bool subtract_point=
false,PosType screening = 1.0);
244 void writeImage(std::string fn);
269 size_t getNx_lr()
const {
return long_range_map.nx; }
271 size_t getNy_lr()
const {
return long_range_map.ny; }
279 size_t getNx()
const {
return Noriginal[0]; }
281 size_t getNy()
const {
return Noriginal[1]; }
283 double getMax()
const {
return max_pix;}
284 double getMin()
const {
return min_pix;}
285 double getResolutionMpc()
const {
return resolution_mpc;}
286 double getResolutionAngular()
const {
return angular_resolution;}
291 long_range_map = std::move(m.long_range_map);
292 short_range_maps = std::move(m.short_range_maps);
294 cpfits = std::move(m.cpfits);
298 mass_unit = m.mass_unit;
299 Noriginal[0] = m.Noriginal[0];
300 Noriginal[1] = m.Noriginal[1];
301 resolution_mpc = m.resolution_mpc;
302 angular_resolution = m.angular_resolution;
303 border_width_pix = m.border_width_pix;
304 fitsfilename = m.fitsfilename;
306 rsmooth2 = m.rsmooth2;
307 zerosize = m.zerosize;
311 wsr_smooth = m.wsr_smooth;
312 ave_ang_sd = m.ave_ang_sd;
313 subfield_filename = m.subfield_filename;
314 write_shorts = m.write_shorts;
320 LensHalo(std::move(m)),cosmo(m.cosmo),cpfits(std::move(m.cpfits))
322 long_range_map = std::move(m.long_range_map);
323 short_range_maps = std::move(m.short_range_maps);
330 mass_unit = m.mass_unit;
331 Noriginal[0] = m.Noriginal[0];
332 Noriginal[1] = m.Noriginal[1];
333 resolution_mpc = m.resolution_mpc;
334 angular_resolution = m.angular_resolution;
335 border_width_pix = m.border_width_pix;
336 fitsfilename = m.fitsfilename;
338 rsmooth2 = m.rsmooth2;
339 zerosize = m.zerosize;
343 wsr_smooth = m.wsr_smooth;
344 ave_ang_sd = m.ave_ang_sd;
345 subfield_filename = m.subfield_filename;
346 write_shorts = m.write_shorts;
353 std::vector<LensMap> short_range_maps;
364 double max_pix = std::numeric_limits<double>::lowest();
365 double min_pix = std::numeric_limits<double>::max();
371 double resolution_mpc;
372 double angular_resolution;
373 long border_width_pix;
374 std::string fitsfilename;
375 std::string subfield_filename;
383 void setsubmap(
LensMap &short_range_map
384 ,
const std::vector<long> &lower_left
388 int operator()(
float k2){
return 1;}
392 float operator()(
float k2){
return exp(-k2*rs2);}
396 float operator()(
float k2){
return 1 - exp(-k2*rs2);}
402 float operator()(
float k2){
return (1 - exp(-k2*rs2)) * exp(-k2*r_sm2);}
406 WSRSMOOTH wsr_smooth;
421 my_fftw_plan plan_padded;
423 make_fftw_plans(plan_padded,zerosize);
433 ,my_fftw_plan &plan_padded
439 int Nnx=int(zerosize*nx);
440 int Nny=int(zerosize*ny);
442 double boxlx = boxlMpc*zerosize;
443 double boxly = ny*boxlMpc*zerosize/nx;
445 assert( (plan_padded.nx == Nnx)*(plan_padded.ny == Nny) );
447 int imin = (Nnx-nx)/2;
448 int imax = (Nnx+nx)/2;
449 int jmin = (Nny-ny)/2;
450 int jmax = (Nny+ny)/2;
452 size_t Nkx = (Nnx/2+1);
454 double tmp = 2.*M_PI/boxlx;
455 std::vector<double> kxs(Nkx);
456 for(
int i=0; i<Nkx; i++ ){
460 std::vector<double> kys(Nny);
461 for(
int j=0; j<Nny; j++ ){
462 kys[j]=(j<Nny/2)?
double(j):double(j-Nny);
466 std::vector<double> extended_map( NN );
469 for(
int j=0; j<Nny; j++ ){
470 for(
int i=0; i<Nnx; i++ ){
471 if(i>=imin && i<imax && j>=jmin && j<jmax){
475 if(ii>=nx || jj>=ny){
476 std::cerr <<
" 1 error mapping " << ii <<
" " << jj << std::endl;
480 std::cerr <<
" 2 error mapping " << ii <<
" " << jj << std::endl;
484 assert(i+Nnx*j < extended_map.size());
487 assert(!isnan(extended_map[i+Nnx*j]));
488 if(isinf(extended_map[i+Nnx*j])){
489 extended_map[i+Nnx*j] = 0;
492 extended_map[i+Nnx*j] = 0;
500 fftw_complex *fphi =
new fftw_complex[Nny*Nkx];
502 fftw_execute_dft_r2c(plan_padded.plan_r2c
503 ,extended_map.data(),fphi);
510 for(
int i=0; i<Nkx; i++ ){
511 for(
int j=0; j<Nny; j++ ){
513 double k2 = kxs[i]*kxs[i] + kys[j]*kys[j];
514 size_t k = i+(Nkx)*j;
530 fphi[k][0] *= -2./k2;
531 fphi[k][1] *= -2./k2;
534 double w = Wphi_of_k(k2);
541 fftw_complex *fft=
new fftw_complex[Nny*(Nkx)];
545 std::vector<double> realsp(Nnx*Nny);
551 for(
int i=0; i<Nkx; i++ ){
552 for(
int j=0; j<Nny; j++ ){
554 size_t k = i+(Nkx)*j;
555 fft[k][0] = -kxs[i]*fphi[k][1];
556 fft[k][1] = kxs[i]*fphi[k][0];
561 fftw_execute_dft_c2r(plan_padded.plan_c2r,fft,realsp.data());
563 alpha1_bar.resize(nx*ny,0);
565 for(
int j=jmin; j<jmax; j++ ){
567 for(
int i=imin; i<imax; i++ ){
570 alpha1_bar[ii+nx*jj] = -1*float(realsp[i+Nnx*j]/NN);
578 for(
int j=0; j<Nny; j++ ){
579 for(
int i=0; i<Nkx; i++ ){
580 size_t k = i+(Nkx)*j;
583 fft[k][0] = -kys[j]*fphi[k][1];
584 fft[k][1] = kys[j]*fphi[k][0];
589 fftw_execute_dft_c2r(plan_padded.plan_c2r,fft,realsp.data());
591 alpha2_bar.resize(nx*ny,0);
593 for(
int j=jmin; j<jmax; j++ ){
595 for(
int i=imin; i<imax; i++ ){
598 alpha2_bar[ii+nx*jj] = -1*float(realsp[i+Nnx*j]/NN);
606 for(
int i=0; i<Nkx; i++ ){
607 for(
int j=0; j<Nny; j++ ){
609 size_t k = i+(Nkx)*j;
611 fft[k][0] = 0.5*(kxs[i]*kxs[i]-kys[j]*kys[j])*fphi[k][0];
612 fft[k][1] = 0.5*(kxs[i]*kxs[i]-kys[j]*kys[j])*fphi[k][1];
616 fftw_execute_dft_c2r(plan_padded.plan_c2r,fft,realsp.data());
618 gamma1_bar.resize(nx*ny,0);
620 for(
int j=jmin; j<jmax; j++ ){
622 for(
int i=imin; i<imax; i++ ){
625 gamma1_bar[ii+nx*jj] = float( realsp[i+Nnx*j]/NN);
632 for(
int i=0; i<Nkx; i++ ){
633 for(
int j=0; j<Nny; j++ ){
635 size_t k = i+(Nkx)*j;
638 fft[k][0] = kxs[i]*kys[j]*fphi[k][0];
639 fft[k][1] = kxs[i]*kys[j]*fphi[k][1];
644 fftw_execute_dft_c2r(plan_padded.plan_c2r,fft,realsp.data());
646 gamma2_bar.resize(nx*ny);
648 for(
int j=0; j<ny; j++ ){
650 for(
int i=0; i<nx; i++ ){
653 gamma2_bar[i+nx*j] = float(-realsp[ii+Nnx*jj]/NN);
662 for(
int i=0; i<Nkx; i++ ){
663 for(
int j=0; j<Nny; j++ ){
665 double k2 = -(kxs[i]*kxs[i] + kys[j]*kys[j])/2;
667 size_t k = i+(Nkx)*j;
670 fft[k][0] = k2*fphi[k][0];
671 fft[k][1] = k2*fphi[k][1];
676 assert( (plan_padded.nx == Nnx)*(plan_padded.ny == Nny) );
677 fftw_execute_dft_c2r(plan_padded.plan_c2r,fft,realsp.data());
679 for(
int j=jmin; j<jmax; j++ ){
681 for(
int i=imin; i<imax; i++ ){
694 for(
int i=0; i<Nkx; i++ ){
695 for(
int j=0; j<Nny; j++ ){
696 size_t k = i+(Nkx)*j;
699 fft[k][0] = fphi[k][0];
700 fft[k][1] = fphi[k][1];
705 assert( (plan_padded.nx == Nnx)*(plan_padded.ny == Nny) );
706 fftw_execute_dft_c2r(plan_padded.plan_c2r,fft,realsp.data());
708 phi_bar.resize(nx*ny);
709 for(
int j=jmin; j<jmax; j++ ){
711 for(
int i=imin; i<imax; i++ ){
714 phi_bar[ii+nx*jj] = float(realsp[i+Nnx*j]/NN);
731 make_fftw_plans(plan,1.0);
742 assert( (plan.nx == nx)*(plan.ny == ny) );
746 size_t Nkx = (nx/2+1);
749 double tmp = 2.*M_PI/boxlMpc;
751 std::vector<double> kxs(Nkx);
752 for(
int i=0; i<Nkx; i++ ){
755 tmp = 2.*M_PI * nx /boxlMpc / ny;
756 std::vector<double> kys(ny);
757 for(
int j=0; j<ny; j++ ){
758 kys[j]=(j<ny/2)?
double(j):double(j-ny);
763 fftw_complex *fphi =
new fftw_complex[ny*Nkx];
764 fftw_complex *fft=
new fftw_complex[ny*(Nkx)];
765 std::vector<double> realsp(NN);
783 for(
int i=0; i<Nkx; i++ ){
784 for(
int j=0; j<ny; j++ ){
786 double k2 = kxs[i]*kxs[i] + kys[j]*kys[j];
787 size_t k = i+(Nkx)*j;
803 fphi[k][0] *= -2./k2;
804 fphi[k][1] *= -2./k2;
807 double w = Wphi_of_k(k2);
819 for(
int i=0; i<Nkx; i++ ){
820 for(
int j=0; j<ny; j++ ){
822 size_t k = i + Nkx * j;
823 fft[k][0] = -kxs[i]*fphi[k][1];
824 fft[k][1] = kxs[i]*fphi[k][0];
829 fftw_execute_dft_c2r(plan.plan_c2r,fft,realsp.data());
831 alpha1_bar.resize(NN);
832 for(
size_t i=0; i<NN; i++ ) alpha1_bar[i] = -1*
float(realsp[i]/NN);
839 for(
int j=0; j<ny; j++ ){
840 for(
int i=0; i<Nkx; i++ ){
841 size_t k = i+(Nkx)*j;
844 fft[k][0] = -kys[j]*fphi[k][1];
845 fft[k][1] = kys[j]*fphi[k][0];
850 fftw_execute_dft_c2r(plan.plan_c2r,fft,realsp.data());
852 alpha2_bar.resize(NN);
853 for(
size_t i=0; i<NN; i++ ) alpha2_bar[i] = -1*
float(realsp[i]/NN);
860 for(
int i=0; i<Nkx; i++ ){
861 for(
int j=0; j<ny; j++ ){
862 size_t k = i+(Nkx)*j;
863 fft[k][0] = fphi[k][0];
864 fft[k][1] = fphi[k][1];
868 fftw_execute_dft_c2r(plan.plan_c2r,fft,realsp.data());
871 for(
size_t i=0; i<NN; i++ ) phi_bar[i] =
float(realsp[i]/NN);
878 for(
int i=0; i<Nkx; i++ ){
879 for(
int j=0; j<ny; j++ ){
881 size_t k = i+(Nkx)*j;
882 double tmp = 0.5*(kxs[i]*kxs[i] - kys[j]*kys[j]);
884 fft[k][0] = tmp * fphi[k][0];
885 fft[k][1] = tmp * fphi[k][1];
889 fftw_execute_dft_c2r(plan.plan_c2r,fft,realsp.data());
891 gamma1_bar.resize(NN);
892 for(
size_t i=0; i<NN; i++ ) gamma1_bar[i] =
float( realsp[i]/NN);
898 for(
int i=0; i<Nkx; i++ ){
899 for(
int j=0; j<ny; j++ ){
901 size_t k = i+(Nkx)*j;
902 double tmp = kxs[i]*kys[j];
904 fft[k][0] = tmp * fphi[k][0];
905 fft[k][1] = tmp * fphi[k][1];
910 fftw_execute_dft_c2r(plan.plan_c2r,fft,realsp.data());
912 gamma2_bar.resize(NN);
913 for(
size_t i=0; i<NN; i++ ) gamma2_bar[i] =
float(-realsp[i]/NN);
920 for(
int i=0; i<Nkx; i++ ){
921 for(
int j=0; j<ny; j++ ){
923 double k2 = -(kxs[i]*kxs[i] + kys[j]*kys[j])/2;
925 size_t k = i+(Nkx)*j;
928 fft[k][0] = k2*fphi[k][0];
929 fft[k][1] = k2*fphi[k][1];
933 fftw_execute_dft_c2r(plan.plan_c2r,fft,realsp.data());
The cosmology and all the functions required to calculated quantities based on the cosmology.
Definition cosmo.h:52
read only fits file interface
Definition cpfits.h:232
A base class for all types of lensing "halos" which are any mass distribution that cause lensing.
Definition lens_halos.h:56
PosType getDist() const
Definition lens_halos.h:118
LensHalo & operator=(const LensHalo &h)
Definition lens_halos.cpp:169
A lens halo that calculates all lensing quantities on two grids - a low res long range grid and a hig...
Definition multimap.h:177
size_t getNy_sr() const
return number of pixels on a y-axis side in short range map (margins are hidden internally)
Definition multimap.h:276
Point_2d getCenter_lr() const
center of short range map in physical Mpc
Definition multimap.h:251
size_t getNx_sr() const
return number of pixels on a x-axis side in short range map (margins are hidden internally)
Definition multimap.h:274
static my_fftw_plan plan_short_range
these plans will be shared between all instances of LensHaloMultiMap
Definition multimap.h:205
void resetsubmap(int i, const std::vector< long > &lower_left_pix)
Definition multimap.cpp:400
Point_2d getLowerLeft_sr(int i) const
lower left of short range map in physical Mpc
Definition multimap.h:254
void resetsubmapPhys(int i, Point_2d ll)
Sets the least highres smaller map in physical coordinates relative to the center of the original map...
Definition multimap.cpp:386
int NumberOfShortRangeMaps()
the number of short range grids
Definition multimap.h:261
LensHaloMultiMap(std::string fitsfile, std::string dir_data, double redshift, double mass_unit, double npane, int number_of_subfields, COSMOLOGY &c, bool write_subfields=false, std::string dir_scratch="", bool subtract_ave=true, double ffactor=5, double gfactor=5, double rsmooth2=0)
Definition multimap.cpp:26
size_t getNy_lr() const
return number of pixels on a y-axis side in original map
Definition multimap.h:271
size_t getNx_lr() const
return number of pixels on a x-axis side in original map
Definition multimap.h:269
Point_2d getLowerLeft_lr() const
lower left of long range map in physical Mpc
Definition multimap.h:247
Point_2d getCenter_sr(int i) const
center of short range map in physical Mpc
Definition multimap.h:258
double getRangeMpc_sr() const
return range of long range map in physical Mpc
Definition multimap.h:266
Point_2d getUpperRight_lr() const
upper right of short range map in physical Mpc
Definition multimap.h:249
size_t getNx() const
return number of pixels on a x-axis side in original map
Definition multimap.h:279
Point_2d getUpperRight_sr(int i) const
upper right of short range map in physical Mpc
Definition multimap.h:256
size_t getNy() const
return number of pixels on a y-axis side in original map
Definition multimap.h:281
double getRangeMpc_lr() const
return range of long range map in physical Mpc
Definition multimap.h:264
LensingVariable
output lensing variables
Definition standard.h:89
The MOKA map structure, containing all quantities that define it.
Definition multimap.h:56
void read_sub(CPFITS_READ &cpfits, std::vector< long > &first, std::vector< long > &last, double Dist)
read a subsection of the fits map
Definition multimap.cpp:917
void ProcessFFTs(float zerosize, T Wphi_of_k, my_fftw_plan &plan_padded, bool do_alpha=true)
a thread safe version of ProcessFFTs
Definition multimap.h:430
void read(std::string input_fits, double angDist)
read an entire map
Definition multimap.cpp:747
void Myread(std::string fits_input_file)
read from a file that has been generated with LensMap::write()
Definition multimap.cpp:814
std::valarray< double > surface_density
values for the map
Definition multimap.h:65
void read_header(std::string input_fits, double angDist)
read only header information
Definition multimap.cpp:711
Point_2d upperright
boundery with centred grid
Definition multimap.h:77
double x_resolution()
resolution in Mpc
Definition multimap.h:80
void write(std::string filename)
write the fits file of the maps of all the lensing quantities.
Definition multimap.cpp:977
Class for representing points or vectors in 2 dimensions. Not that the dereferencing operator is over...
Definition point.h:48