10 #include "utilities_slsim.h"
16 GLAMER_TEST_USES(LensTest)
73 Lens(
long* seed, PosType z_source,CosmoParamSet cosmoset,
bool verbose =
false);
75 Lens(
long* seed, PosType z_source,
const COSMOLOGY &cosmo,
bool verbose =
false);
87 PosType
getfov()
const {
return fieldofview;};
88 void setfov(PosType fov){fieldofview=fov;};
91 void resetFieldNplanes(std::size_t field_Nplanes,
bool verbose =
false);
95 void resetFieldHalos(
bool verbose =
false);
98 void printMultiLens();
103 if(flag_switch_main_halo_on)
104 return main_halos[0]->getZlens();
107 std::cerr <<
"error, no main lens present" << std::endl;
113 if(flag_switch_main_halo_on)
114 return cosmo.angDist( main_halos[0]->getZlens());
117 std::cerr <<
"error, no main lens present" << std::endl;
125 void clearMainHalos(
bool verbose=
false);
127 template<
typename HaloType>
132 template <
typename T>
136 T * halo =
new T(halo_in);
137 halo->setCosmology(cosmo);
138 main_halos.push_back(halo);
140 flag_switch_main_halo_on =
true;
142 if(addplanes) addMainHaloToPlane(halo);
143 else addMainHaloToNearestPlane(halo);
145 combinePlanes(verbose);
155 template <
typename T>
158 T * halo =
new T(std::move(halo_in));
159 halo->setCosmology(cosmo);
160 main_halos.push_back(halo);
162 flag_switch_main_halo_on =
true;
164 if(addplanes) addMainHaloToPlane(halo);
165 else addMainHaloToNearestPlane(halo);
167 combinePlanes(verbose);
176 template <
typename T>
181 T * halo =
new T(halo_in);
182 halo->setCosmology(cosmo);
183 main_halos.push_back(halo);
185 flag_switch_main_halo_on =
true;
189 combinePlanes(verbose);
202 template <
typename T>
207 for(T &h : my_halos){
209 ptr->setCosmology(cosmo);
211 main_halos.push_back( ptr );
212 if(addplanes) addMainHaloToPlane( ptr );
213 else addMainHaloToNearestPlane( ptr );
216 flag_switch_main_halo_on =
true;
218 combinePlanes(verbose);
226 template <
typename T>
234 for(T &h : my_halos){
236 ptr->setCosmology(cosmo);
238 main_halos.push_back( ptr );
241 flag_switch_main_halo_on =
true;
245 combinePlanes(verbose);
276 std::size_t getNMainHalos()
const;
278 template<
typename HaloType>
279 std::size_t getNMainHalos()
const;
282 LensHalo* getMainHalo(std::size_t i);
285 template<
typename HaloType>
286 HaloType* getMainHalo(std::size_t i);
295 void rayshooter(
RAY &ray);
303 void rayshooterInternal(
unsigned long Npoints
305 ,
bool RSIverbose =
false
313 void rayshooterInternal(
unsigned long Npoints
315 ,std::vector<double> &source_zs
316 ,
bool RSIverbose =
false
319 void rayshooterInternal(
unsigned long Npoints
322 void rayshooterInternal(
RAY &ray);
324 void info_rayshooter(
RAY &i_point
325 ,std::vector<Point_2d> & ang_positions
326 ,std::vector<KappaType> & kappa_on_planes
327 ,std::vector<std::vector<LensHalo*>> & halo_neighbors
329 ,KappaType &kappa_max
330 ,KappaType gamma_max[]
334 ,
bool verbose =
false
338 void mass_on_planes(
const std::vector<RAY> &rays
339 ,std::vector<double> &masses
340 ,
bool verbose =
false
372 ,
bool use_image_guess
376 RAY find_image_min(
const RAY &in_ray
386 ,std::vector<Point_2d> &boundary
389 void find_images_min_parallel(std::vector<RAY> &rays
391 ,std::vector<bool> &success
398 std::vector<RAY> find_images(
Point_2d y_source
409 std::vector<RAY> find_images(
GridMap &init_grid
431 short ResetSourcePlane(PosType z,
bool nearest=
false,
bool verbose =
false);
434 void FindSourcePlane(
444 PosType getSourceZ(){
445 if(toggle_source_plane){
448 return plane_redshifts.back();
452 PosType getZmax()
const{
return plane_redshifts.back();}
458 PosType
getSigmaCrit(PosType zsource)
const{
return cosmo.SigmaCrit(getZlens(), zsource); }
466 void TurnFieldOn() { flag_switch_field_off = false ; }
472 bool getfieldOff()
const {
return flag_switch_field_off ;}
479 init_seed = lens.init_seed;
481 toggle_source_plane = lens.toggle_source_plane;
483 zs_implant = lens.zs_implant;
484 zsource = lens.zsource;
486 NZSamples = lens.NZSamples;
488 NhalosbinZ = lens.NhalosbinZ;
489 Nhaloestot_Tab = lens.Nhaloestot_Tab;
490 aveNhalosField = lens.aveNhalosField;
492 NhalosbinMass = lens.NhalosbinMass;
493 sigma_back_Tab = lens.sigma_back_Tab;
495 flag_switch_deflection_off = lens.flag_switch_deflection_off;
496 flag_switch_lensing_off = lens.flag_switch_lensing_off;
501 plane_redshifts = lens.plane_redshifts;
502 charge = lens.charge;
503 flag_switch_field_off = lens.flag_switch_field_off;
505 field_halos = lens.field_halos;
507 field_Nplanes_original = lens.field_Nplanes_original;
508 field_Nplanes_current = lens.field_Nplanes_current;
510 field_plane_redshifts = lens.field_plane_redshifts;
511 field_plane_redshifts_original = lens.field_plane_redshifts_original;
512 field_Dl = lens.field_Dl;
513 field_Dl_original = lens.field_Dl_original;
518 field_mass_func_type = lens.field_mass_func_type;
519 mass_func_PL_slope = lens.mass_func_PL_slope;
520 field_min_mass = lens.field_min_mass;
521 field_int_prof_type = lens.field_int_prof_type;
522 field_prof_internal_slope = lens.field_prof_internal_slope;
524 flag_field_gal_on = lens.flag_field_gal_on;
525 field_int_prof_gal_type = lens.field_int_prof_gal_type;
526 field_int_prof_gal_slope = lens.field_int_prof_gal_slope;
528 redshift_planes_file = lens.redshift_planes_file;
529 read_redshift_planes = lens.read_redshift_planes;
531 field_input_sim_file = lens.field_input_sim_file;
532 field_input_sim_format = lens.field_input_sim_format;
534 sim_input_flag = lens.sim_input_flag;
535 read_sim_file = lens.read_sim_file;
536 field_buffer = lens.field_buffer;
538 flag_switch_main_halo_on = lens.flag_switch_main_halo_on;
540 main_plane_redshifts = lens.main_plane_redshifts;
541 main_Dl = lens.main_Dl;
543 main_halo_type = lens.main_halo_type;
544 main_galaxy_halo_type = lens.main_galaxy_halo_type;
546 pixel_map_input_file = lens.pixel_map_input_file;
547 pixel_map_on = lens.pixel_map_on;
548 pixel_map_zeropad = lens.pixel_map_zeropad;
549 pixel_map_zeromean = lens.pixel_map_zeromean;
551 central_point_sphere = lens.central_point_sphere;
552 sim_angular_radius = lens.sim_angular_radius;
553 inv_ang_screening_scale = lens.inv_ang_screening_scale;
555 std::swap(lensing_planes,lens.lensing_planes);
556 std::swap(field_planes,lens.field_planes);
557 swap(main_halos,lens.main_halos);
558 std::swap(main_planes,lens.main_planes);
563 *
this = std::move(lens);
567 std::vector<PosType> get_plane_redshifts () {
return plane_redshifts; }
578 GLAMER_TEST_FRIEND(LensTest)
581 void _find_images_(std::vector<RAY> &images
589 void _find_images_min_parallel_(
RAY *rays
593 ,std::vector<bool> &success
606 void compute_points_parallel(
int start
611 ,
bool verbose =
false);
614 void compute_rays_parallel(
int start
617 ,
bool verbose =
false);
621 void defaultParams(PosType zsource,
bool verbose =
true);
624 bool toggle_source_plane;
632 void quicksort(
LensHaloHndl *halo,PosType **pos,
unsigned long N);
640 void setFieldDistFromFile();
642 void setupFieldPlanes();
645 void ComputeHalosDistributionVariables ();
647 enum DM_Light_Division {All_DM,Moster};
649 void createFieldHalos(
bool verbose,DM_Light_Division division = Moster);
652 void readInputSimFileMillennium(
bool verbose,DM_Light_Division division = Moster);
655 void readInputSimFileMultiDarkHalos(
bool verbose,DM_Light_Division division = Moster);
658 void readInputSimFileObservedGalaxies(
bool verbose);
661 void createFieldPlanes(
bool verbose);
666 void createMainPlanes();
668 void addMainHaloToPlane(
LensHalo* halo);
669 void addMainHaloToNearestPlane(
LensHalo* halo);
672 void combinePlanes(
bool verbose);
678 const int Nzbins = 64 ;
680 const int Nmassbin=64;
684 std::vector<PosType> zbins ;
686 std::vector<PosType> NhalosbinZ ;
688 std::vector<PosType> Nhaloestot_Tab ;
690 PosType aveNhalosField ;
692 std::vector<PosType> Logm;
694 std::vector<std::vector<PosType>> NhalosbinMass;
696 std::vector<PosType> sigma_back_Tab;
701 size_t getNFieldHalos()
const {
return field_halos.size();}
707 bool flag_switch_deflection_off;
708 bool flag_switch_lensing_off;
711 std::vector<LensPlane *> lensing_planes;
713 std::vector<PosType> Dl;
715 std::vector<PosType> dDl;
717 std::vector<PosType> dTl;
719 std::vector<PosType> plane_redshifts;
725 bool flag_switch_field_off;
728 std::vector<LensHalo *> field_halos;
730 std::size_t field_Nplanes_original;
731 std::size_t field_Nplanes_current;
734 std::vector<LensPlane*> field_planes;
736 std::vector<PosType> field_plane_redshifts;
738 std::vector<PosType> field_plane_redshifts_original;
740 std::vector<PosType> field_Dl;
742 std::vector<PosType> field_Dl_original;
773 PosType mass_func_PL_slope;
775 PosType field_min_mass;
779 PosType field_prof_internal_slope;
782 bool flag_field_gal_on;
786 PosType field_int_prof_gal_slope;
791 std::string redshift_planes_file;
792 bool read_redshift_planes;
794 std::string field_input_sim_file;
795 HaloCatFormats field_input_sim_format;
803 PosType field_buffer;
807 bool flag_switch_main_halo_on;
813 std::vector<LensPlane*> main_planes;
815 std::vector<PosType> main_plane_redshifts;
817 std::vector<PosType> main_Dl;
826 std::string pixel_map_input_file;
831 int pixel_map_zeropad;
832 bool pixel_map_zeromean;
833 void readPixelizedDensity();
838 PosType sim_angular_radius;
840 PosType inv_ang_screening_scale;
847 void GenerateFieldHalos(
double min_mass
849 ,
double field_of_view
854 ,
bool verbose =
false
862 return main_halos.
size();
865 template<
typename HaloType>
868 return main_halos.
size<HaloType>();
873 return main_halos.
at(i);
876 template<
typename HaloType>
879 if(main_halos.
size<HaloType>() == 0 )
return nullptr;
880 return main_halos.
at<HaloType>(i);
886 template <
typename P>
887 void Lens::compute_points_parallel(
int start
894 int end = start + chunk_size;
902 KappaType kappa,gamma[3];
905 Matrix2x2<PosType> G;
907 PosType SumPrevAlphas[2];
908 Matrix2x2<PosType> SumPrevAG;
912 long jmax = lensing_planes.size();
917 if(source_zs[0] == plane_redshifts.back() ){
918 Dls_Ds = dDl.back() / Dl.back();
919 D_Ds = Dl[Dl.size() - 2] / Dl.back();
924 if(jmax > 0) D_Ds = Dl[jmax-1] / Ds;
929 for(i = start; i < end; i++)
932 if(i_points[i].in_image == MAYBE)
continue;
935 theta = i_points[i].ptr_y();
937 theta[0] = i_points[i].x[0];
938 theta[1] = i_points[i].x[1];
941 SumPrevAlphas[0] = theta[0];
942 SumPrevAlphas[1] = theta[1];
951 i_points[i].A.setToI();
955 if(flag_switch_lensing_off)
957 i_points[i].image->A.setToI();
970 if(jmax > 0) D_Ds = Dl[jmax-1]/Ds;
976 for(j = 0; j < jmax ; ++j)
979 double Dphysical = Dl[j]/(1 + plane_redshifts[j]);
981 xx[0] = theta[0] * Dphysical;
982 xx[1] = theta[1] * Dphysical;
985 assert(xx[0] == xx[0] && xx[1] == xx[1]);
989 lensing_planes[j]->force(alpha,&kappa,gamma,&phi,xx);
999 assert(alpha[0] == alpha[0] && alpha[1] == alpha[1]);
1000 assert(gamma[0] == gamma[0] && gamma[1] == gamma[1]);
1001 assert(kappa == kappa);
1002 if(std::isinf(kappa)) { std::cout <<
"xx = " << xx[0] <<
" " << xx[1] << std::endl ;}
1003 assert(!std::isinf(kappa));
1005 G[0] = kappa + gamma[0]; G[1] = gamma[1];
1006 G[2] = gamma[1]; G[3] = kappa - gamma[0];
1012 G *= charge * Dl[j] / (1 + plane_redshifts[j]);
1014 assert(gamma[0] == gamma[0] && gamma[1] == gamma[1]);
1015 assert(kappa == kappa);
1022 SumPrevAlphas[0] -= charge * alpha[0] ;
1023 SumPrevAlphas[1] -= charge * alpha[1] ;
1026 aa = dDl[j+1] / Dl[j+1];
1027 bb = Dl[j] / Dl[j+1];
1033 if(!flag_switch_deflection_off){
1034 theta[0] = bb * theta[0] + aa * SumPrevAlphas[0];
1035 theta[1] = bb * theta[1] + aa * SumPrevAlphas[1];
1041 SumPrevAG -= (G * (i_points[i].A)) ;
1044 i_points[i].A = i_points[i].A * bb + SumPrevAG * aa;
1057 if(flag_switch_deflection_off){
1058 i_points[i].A = Matrix2x2<KappaType>::I() - SumPrevAG;
1065 i_points[i].dt *= MpcToSeconds * SecondToYears ;
1070 i_points[i].image->A = i_points[i].A;
1071 i_points[i].image->dt = i_points[i].dt;
1077 if(verbose) std::cout <<
"RSI final : X X | " << i <<
" " << source_zs[i] <<
" | " << i_points[i].kappa() <<
" " << i_points[i].gamma1() <<
" " << i_points[i].gamma2() <<
" " << i_points[i].gamma3() <<
" " << i_points[i].invmag() <<
" | " << i_points[i].dt << std::endl ;