10#include "utilities_slsim.h"
16GLAMER_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>
149 template <
typename T>
153 T * halo =
new T(halo_in);
154 halo->setCosmology(cosmo);
155 main_halos.push_back(halo);
157 flag_switch_main_halo_on =
true;
159 if(addplanes) addMainHaloToPlane(halo);
160 else addMainHaloToNearestPlane(halo);
162 combinePlanes(verbose);
172 template <
typename T>
175 T * halo =
new T(std::move(halo_in));
176 halo->setCosmology(cosmo);
177 main_halos.push_back(halo);
179 flag_switch_main_halo_on =
true;
181 if(addplanes) addMainHaloToPlane(halo);
182 else addMainHaloToNearestPlane(halo);
184 combinePlanes(verbose);
193 template <
typename T>
198 T * halo =
new T(halo_in);
199 halo->setCosmology(cosmo);
200 main_halos.push_back(halo);
202 flag_switch_main_halo_on =
true;
206 combinePlanes(verbose);
219 template <
typename T>
224 for(T &h : my_halos){
226 ptr->setCosmology(cosmo);
228 main_halos.push_back( ptr );
229 if(addplanes) addMainHaloToPlane( ptr );
230 else addMainHaloToNearestPlane( ptr );
233 flag_switch_main_halo_on =
true;
235 combinePlanes(verbose);
243 template <
typename T>
251 for(T &h : my_halos){
253 ptr->setCosmology(cosmo);
255 main_halos.push_back( ptr );
258 flag_switch_main_halo_on =
true;
262 combinePlanes(verbose);
303 std::size_t getNMainHalos()
const;
305 template<
typename HaloType>
306 std::size_t getNMainHalos()
const;
309 LensHalo* getMainHalo(std::size_t i);
312 template<
typename HaloType>
313 HaloType* getMainHalo(std::size_t i);
322 void rayshooter(
RAY &ray);
330 void rayshooterInternal(
unsigned long Npoints
332 ,
bool RSIverbose =
false
340 void rayshooterInternal(
unsigned long Npoints
342 ,std::vector<double> &source_zs
343 ,
bool RSIverbose =
false
346 void rayshooterInternal(
unsigned long Npoints
349 void rayshooterInternal(
RAY &ray);
351 void info_rayshooter(
RAY &i_point
352 ,std::vector<Point_2d> & ang_positions
353 ,std::vector<KappaType> & kappa_on_planes
354 ,std::vector<std::vector<LensHalo*>> & halo_neighbors
356 ,KappaType &kappa_max
357 ,KappaType gamma_max[]
361 ,
bool verbose =
false
365 void mass_on_planes(
const std::vector<RAY> &rays
366 ,std::vector<double> &masses
367 ,
bool verbose =
false
398 ,
bool use_image_guess
404 RAY find_image_min(
const RAY &in_ray
420 ,std::vector<Point_2d> &boundary
424 std::vector<RAY> find_images(
Point_2d y_source
431 std::vector<RAY> find_images(
GridMap &init_grid
437 void find_images_min_parallel(std::vector<RAY> &rays
439 ,std::vector<bool> &success
456 short ResetSourcePlane(PosType z,
bool nearest=
false,
bool verbose =
false);
459 void FindSourcePlane(
469 PosType getSourceZ(){
470 if(toggle_source_plane){
473 return plane_redshifts.back();
477 PosType getZmax()
const{
return plane_redshifts.back();}
491 void TurnFieldOn() { flag_switch_field_off = false ; }
497 bool getfieldOff()
const {
return flag_switch_field_off ;}
504 init_seed = lens.init_seed;
506 toggle_source_plane = lens.toggle_source_plane;
508 zs_implant = lens.zs_implant;
509 zsource = lens.zsource;
511 NZSamples = lens.NZSamples;
513 NhalosbinZ = lens.NhalosbinZ;
514 Nhaloestot_Tab = lens.Nhaloestot_Tab;
515 aveNhalosField = lens.aveNhalosField;
517 NhalosbinMass = lens.NhalosbinMass;
518 sigma_back_Tab = lens.sigma_back_Tab;
520 flag_switch_deflection_off = lens.flag_switch_deflection_off;
521 flag_switch_lensing_off = lens.flag_switch_lensing_off;
526 plane_redshifts = lens.plane_redshifts;
527 charge = lens.charge;
528 flag_switch_field_off = lens.flag_switch_field_off;
530 field_halos = lens.field_halos;
532 field_Nplanes_original = lens.field_Nplanes_original;
533 field_Nplanes_current = lens.field_Nplanes_current;
535 field_plane_redshifts = lens.field_plane_redshifts;
536 field_plane_redshifts_original = lens.field_plane_redshifts_original;
537 field_Dl = lens.field_Dl;
538 field_Dl_original = lens.field_Dl_original;
543 field_mass_func_type = lens.field_mass_func_type;
544 mass_func_PL_slope = lens.mass_func_PL_slope;
545 field_min_mass = lens.field_min_mass;
546 field_int_prof_type = lens.field_int_prof_type;
547 field_prof_internal_slope = lens.field_prof_internal_slope;
549 flag_field_gal_on = lens.flag_field_gal_on;
550 field_int_prof_gal_type = lens.field_int_prof_gal_type;
551 field_int_prof_gal_slope = lens.field_int_prof_gal_slope;
553 redshift_planes_file = lens.redshift_planes_file;
554 read_redshift_planes = lens.read_redshift_planes;
556 field_input_sim_file = lens.field_input_sim_file;
557 field_input_sim_format = lens.field_input_sim_format;
559 sim_input_flag = lens.sim_input_flag;
560 read_sim_file = lens.read_sim_file;
561 field_buffer = lens.field_buffer;
563 flag_switch_main_halo_on = lens.flag_switch_main_halo_on;
565 main_plane_redshifts = lens.main_plane_redshifts;
566 main_Dl = lens.main_Dl;
568 main_halo_type = lens.main_halo_type;
569 main_galaxy_halo_type = lens.main_galaxy_halo_type;
571 pixel_map_input_file = lens.pixel_map_input_file;
572 pixel_map_on = lens.pixel_map_on;
573 pixel_map_zeropad = lens.pixel_map_zeropad;
574 pixel_map_zeromean = lens.pixel_map_zeromean;
576 central_point_sphere = lens.central_point_sphere;
577 sim_angular_radius = lens.sim_angular_radius;
578 inv_ang_screening_scale = lens.inv_ang_screening_scale;
580 std::swap(lensing_planes,lens.lensing_planes);
581 std::swap(field_planes,lens.field_planes);
582 swap(main_halos,lens.main_halos);
583 std::swap(main_planes,lens.main_planes);
588 *
this = std::move(lens);
592 std::vector<PosType> get_plane_redshifts () {
return plane_redshifts; }
603 GLAMER_TEST_FRIEND(LensTest)
606 void _find_images_(std::vector<RAY> &images
612 void _find_images_min_parallel_(
RAY *rays
616 ,std::vector<bool> &success
629 void compute_points_parallel(
int start
634 ,
bool verbose =
false);
637 void compute_rays_parallel(
int start
640 ,
bool verbose =
false);
644 void defaultParams(PosType zsource,
bool verbose =
true);
647 bool toggle_source_plane;
655 void quicksort(
LensHaloHndl *halo,PosType **pos,
unsigned long N);
663 void setFieldDistFromFile();
665 void setupFieldPlanes();
668 void ComputeHalosDistributionVariables ();
670 enum DM_Light_Division {All_DM,Moster};
672 void createFieldHalos(
bool verbose,DM_Light_Division division = Moster);
675 void readInputSimFileMillennium(
bool verbose,DM_Light_Division division = Moster);
678 void readInputSimFileMultiDarkHalos(
bool verbose,DM_Light_Division division = Moster);
681 void readInputSimFileObservedGalaxies(
bool verbose);
684 void createFieldPlanes(
bool verbose);
689 void createMainPlanes();
691 void addMainHaloToPlane(
LensHalo* halo);
692 void addMainHaloToNearestPlane(
LensHalo* halo);
695 void combinePlanes(
bool verbose);
701 const int Nzbins = 64 ;
703 const int Nmassbin=64;
707 std::vector<PosType> zbins ;
709 std::vector<PosType> NhalosbinZ ;
711 std::vector<PosType> Nhaloestot_Tab ;
713 PosType aveNhalosField ;
715 std::vector<PosType> Logm;
717 std::vector<std::vector<PosType>> NhalosbinMass;
719 std::vector<PosType> sigma_back_Tab;
724 size_t getNFieldHalos()
const {
return field_halos.size();}
730 bool flag_switch_deflection_off;
731 bool flag_switch_lensing_off;
734 std::vector<LensPlane *> lensing_planes;
736 std::vector<PosType> Dl;
738 std::vector<PosType> dDl;
740 std::vector<PosType> dTl;
742 std::vector<PosType> plane_redshifts;
748 bool flag_switch_field_off;
751 std::vector<LensHalo *> field_halos;
753 std::size_t field_Nplanes_original;
754 std::size_t field_Nplanes_current;
757 std::vector<LensPlane*> field_planes;
759 std::vector<PosType> field_plane_redshifts;
761 std::vector<PosType> field_plane_redshifts_original;
763 std::vector<PosType> field_Dl;
765 std::vector<PosType> field_Dl_original;
796 PosType mass_func_PL_slope;
798 PosType field_min_mass;
802 PosType field_prof_internal_slope;
805 bool flag_field_gal_on;
809 PosType field_int_prof_gal_slope;
814 std::string redshift_planes_file;
815 bool read_redshift_planes;
817 std::string field_input_sim_file;
818 HaloCatFormats field_input_sim_format;
826 PosType field_buffer;
830 bool flag_switch_main_halo_on;
836 std::vector<LensPlane*> main_planes;
838 std::vector<PosType> main_plane_redshifts;
840 std::vector<PosType> main_Dl;
849 std::string pixel_map_input_file;
854 int pixel_map_zeropad;
855 bool pixel_map_zeromean;
856 void readPixelizedDensity();
861 PosType sim_angular_radius;
863 PosType inv_ang_screening_scale;
870 void GenerateFieldHalos(
double min_mass
872 ,
double field_of_view
877 ,
bool verbose =
false
885 return main_halos.
size();
888template<
typename HaloType>
891 return main_halos.
size<HaloType>();
896 return main_halos.
at(i);
899template<
typename HaloType>
902 if(main_halos.
size<HaloType>() == 0 )
return nullptr;
903 return main_halos.
at<HaloType>(i);
910void Lens::compute_points_parallel(
int start
917 int end = start + chunk_size;
925 KappaType kappa,gamma[3];
928 Matrix2x2<PosType> G;
930 PosType SumPrevAlphas[2];
931 Matrix2x2<PosType> SumPrevAG;
935 long jmax = lensing_planes.size();
940 if(source_zs[0] == plane_redshifts.back() ){
941 Dls_Ds = dDl.back() / Dl.back();
942 D_Ds = Dl[Dl.size() - 2] / Dl.back();
947 if(jmax > 0) D_Ds = Dl[jmax-1] / Ds;
952 for(i = start; i < end; i++)
955 if(i_points[i].in_image == MAYBE)
continue;
958 theta = i_points[i].ptr_y();
960 theta[0] = i_points[i].x[0];
961 theta[1] = i_points[i].x[1];
964 SumPrevAlphas[0] = theta[0];
965 SumPrevAlphas[1] = theta[1];
974 i_points[i].A.setToI();
978 if(flag_switch_lensing_off)
980 i_points[i].image->A.setToI();
993 if(jmax > 0) D_Ds = Dl[jmax-1]/Ds;
999 for(j = 0; j < jmax ; ++j)
1002 double Dphysical = Dl[j]/(1 + plane_redshifts[j]);
1004 xx[0] = theta[0] * Dphysical;
1005 xx[1] = theta[1] * Dphysical;
1008 assert(xx[0] == xx[0] && xx[1] == xx[1]);
1012 lensing_planes[j]->force(alpha,&kappa,gamma,&phi,xx);
1017 assert(alpha[0] == alpha[0] && alpha[1] == alpha[1]);
1018 assert(gamma[0] == gamma[0] && gamma[1] == gamma[1]);
1019 assert(kappa == kappa);
1020 if(std::isinf(kappa)) { std::cout <<
"xx = " << xx[0] <<
" " << xx[1] << std::endl ;}
1021 assert(!std::isinf(kappa));
1023 G[0] = kappa + gamma[0]; G[1] = gamma[1];
1024 G[2] = gamma[1]; G[3] = kappa - gamma[0];
1030 G *= charge * Dl[j] / (1 + plane_redshifts[j]);
1032 assert(gamma[0] == gamma[0] && gamma[1] == gamma[1]);
1033 assert(kappa == kappa);
1040 SumPrevAlphas[0] -= charge * alpha[0] ;
1041 SumPrevAlphas[1] -= charge * alpha[1] ;
1044 aa = dDl[j+1] / Dl[j+1];
1045 bb = Dl[j] / Dl[j+1];
1051 if(!flag_switch_deflection_off){
1052 theta[0] = bb * theta[0] + aa * SumPrevAlphas[0];
1053 theta[1] = bb * theta[1] + aa * SumPrevAlphas[1];
1059 SumPrevAG -= (G * (i_points[i].A)) ;
1062 i_points[i].A = i_points[i].A * bb + SumPrevAG * aa;
1075 if(flag_switch_deflection_off){
1076 i_points[i].A = Matrix2x2<KappaType>::I() - SumPrevAG;
1083 i_points[i].dt *= MpcToSeconds * SecondToYears ;
1088 i_points[i].image->A = i_points[i].A;
1089 i_points[i].image->dt = i_points[i].dt;
1095 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 ;
The cosmology and all the functions required to calculated quantities based on the cosmology.
Definition cosmo.h:52
void PrintCosmology(short physical=0) const
Print cosmological parameters.
Definition cosmo.cpp:359
double SigmaCrit(double zlens, double zsource) const
The lensing critical density in Msun / Mpc^2.
Definition cosmo.cpp:1540
double angDist(double zo, double z) const
The angular size distance in units Mpc.
Definition cosmo.cpp:704
A base class for all types of lensing "halos" which are any mass distribution that cause lensing.
Definition lens_halos.h:56
A class to represents a lens with multiple planes.
Definition lens.h:71
bool set
marks if the lens has been setup.
Definition lens.h:81
int getNplanes() const
the total number of lens planes
Definition lens.h:84
void replaceMainHalos(std::vector< T > &my_halos, bool verbose)
Inserts a sequense of main lens halos and remove all previous ones.
Definition lens.h:244
void insertMainHalo(const T &halo_in, bool addplanes, bool verbose=false)
inserts a single main lens halo and adds it to the existing ones
Definition lens.h:150
PosType getFieldMinMass() const
get the field min mass :
Definition lens.h:494
Lens & operator=(Lens &&lens)
Definition lens.h:500
void TurnFieldOff()
set flag_switch_field_off, turn the field On/Off :
Definition lens.h:490
PosType getfov() const
field of view in square degrees
Definition lens.h:87
PosType getSigmaCrit(PosType zsource) const
returns the critical density at the main lens in Msun/ Mpc^2 for a source at zsource
Definition lens.h:483
const COSMOLOGY & getCosmo()
returns a const reference to the cosmology so that constant functions can be used,...
Definition lens.h:487
void PrintCosmology()
print the cosmological parameters
Definition lens.h:480
void FindSourcePlane(PosType zs, long &jmax, double &Dls, double &Ds)
find information on the position of the source plane with respect to the lens planes
Definition lens.cpp:2659
PosType getAngDistLens() const
Angular size distance (Mpc) to first main lens plane.
Definition lens.h:112
void moveinMainHalo(T &halo_in, bool addplanes, bool verbose=false)
This has the same effect as insertMainHalo(), but the halo is not copied, it is moved.
Definition lens.h:173
void replaceMainHalo(const T &halo_in, bool addplanes, bool verbose=false)
Inserts a single main lens halo and deletes all previous ones. Then all lensing planes are updated ac...
Definition lens.h:194
PosType getZlens() const
Redshift of first main lens plane.
Definition lens.h:102
void clearMainHalo(bool verbose=false)
remaove all main halo of given type
std::size_t getNMainHalos() const
inserts a sequence of main lens halos and adds them to the existing ones
Definition lens.h:883
LensHalo * getMainHalo(std::size_t i)
get single main halo
Definition lens.h:894
PosType fieldofview
field of view in square degrees
Definition lens.h:600
void RevertSourcePlane()
Revert the source redshift to the value it was when the Lens was created.
Definition lens.h:467
void insertMainHalos(std::vector< T > &my_halos, bool addplanes, bool verbose=false)
Inserts a sequense of main lens halos and adds them to the existing ones. Then all lensing planes are...
Definition lens.h:220
represents a point in spherical coordinates, theta = 0 is equator
Definition geometry.h:30
A container that can hold mixed objects all derived from a base class and retains the ability to acce...
Definition utilities_slsim.h:272
std::size_t size() const
number of all elements
Definition utilities_slsim.h:549
BaseT & at(std::size_t i)
indexed access with bounds checking
Definition utilities_slsim.h:514
void delete_container(Container &c)
delete the objects that are pointed to in a container of pointers
Definition utilities_slsim.h:993
A simplified version of the Grid structure for making non-adaptive maps of the lensing quantities (ka...
Definition gridmap.h:31
A point that automatically has an image point.
Definition point.h:496
Class for representing points or vectors in 2 dimensions. Not that the dereferencing operator is over...
Definition point.h:48
A point on the source or image plane that contains a position and the lensing quantities.
Definition point.h:414
Simple representaion of a light path giving position on the image and source planes and lensing quant...
Definition point.h:510