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);
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
399 ,
bool use_image_guess
403 RAY find_image_min(
const RAY &in_ray
413 ,std::vector<Point_2d> &boundary
416 void find_images_min_parallel(std::vector<RAY> &rays
418 ,std::vector<bool> &success
425 std::vector<RAY> find_images(
Point_2d y_source
436 std::vector<RAY> find_images(
GridMap &init_grid
458 short ResetSourcePlane(PosType z,
bool nearest=
false,
bool verbose =
false);
461 void FindSourcePlane(
471 PosType getSourceZ(){
472 if(toggle_source_plane){
475 return plane_redshifts.back();
479 PosType getZmax()
const{
return plane_redshifts.back();}
493 void TurnFieldOn() { flag_switch_field_off = false ; }
499 bool getfieldOff()
const {
return flag_switch_field_off ;}
505 void GenerateFieldHalos(
double min_mass
507 ,
double field_of_view
512 ,
bool verbose =
false
519 init_seed = lens.init_seed;
521 toggle_source_plane = lens.toggle_source_plane;
523 zs_implant = lens.zs_implant;
524 zsource = lens.zsource;
526 NZSamples = lens.NZSamples;
528 NhalosbinZ = lens.NhalosbinZ;
529 Nhaloestot_Tab = lens.Nhaloestot_Tab;
530 aveNhalosField = lens.aveNhalosField;
532 NhalosbinMass = lens.NhalosbinMass;
533 sigma_back_Tab = lens.sigma_back_Tab;
535 flag_switch_deflection_off = lens.flag_switch_deflection_off;
536 flag_switch_lensing_off = lens.flag_switch_lensing_off;
541 plane_redshifts = lens.plane_redshifts;
542 charge = lens.charge;
543 flag_switch_field_off = lens.flag_switch_field_off;
545 field_halos = lens.field_halos;
547 field_Nplanes_original = lens.field_Nplanes_original;
548 field_Nplanes_current = lens.field_Nplanes_current;
550 field_plane_redshifts = lens.field_plane_redshifts;
551 field_plane_redshifts_original = lens.field_plane_redshifts_original;
552 field_Dl = lens.field_Dl;
553 field_Dl_original = lens.field_Dl_original;
558 field_mass_func_type = lens.field_mass_func_type;
559 mass_func_PL_slope = lens.mass_func_PL_slope;
560 field_min_mass = lens.field_min_mass;
561 field_int_prof_type = lens.field_int_prof_type;
562 field_prof_internal_slope = lens.field_prof_internal_slope;
564 flag_field_gal_on = lens.flag_field_gal_on;
565 field_int_prof_gal_type = lens.field_int_prof_gal_type;
566 field_int_prof_gal_slope = lens.field_int_prof_gal_slope;
568 redshift_planes_file = lens.redshift_planes_file;
569 read_redshift_planes = lens.read_redshift_planes;
571 field_input_sim_file = lens.field_input_sim_file;
572 field_input_sim_format = lens.field_input_sim_format;
574 sim_input_flag = lens.sim_input_flag;
575 read_sim_file = lens.read_sim_file;
576 field_buffer = lens.field_buffer;
578 flag_switch_main_halo_on = lens.flag_switch_main_halo_on;
580 main_plane_redshifts = lens.main_plane_redshifts;
581 main_Dl = lens.main_Dl;
583 main_halo_type = lens.main_halo_type;
584 main_galaxy_halo_type = lens.main_galaxy_halo_type;
586 pixel_map_input_file = lens.pixel_map_input_file;
587 pixel_map_on = lens.pixel_map_on;
588 pixel_map_zeropad = lens.pixel_map_zeropad;
589 pixel_map_zeromean = lens.pixel_map_zeromean;
591 central_point_sphere = lens.central_point_sphere;
592 sim_angular_radius = lens.sim_angular_radius;
593 inv_ang_screening_scale = lens.inv_ang_screening_scale;
595 std::swap(lensing_planes,lens.lensing_planes);
596 std::swap(field_planes,lens.field_planes);
597 swap(main_halos,lens.main_halos);
598 std::swap(main_planes,lens.main_planes);
603 *
this = std::move(lens);
607 std::vector<PosType> get_plane_redshifts () {
return plane_redshifts; }
618 GLAMER_TEST_FRIEND(LensTest)
621 void _find_images_(std::vector<RAY> &images
629 void _find_images_min_parallel_(
RAY *rays
633 ,std::vector<bool> &success
646 void compute_points_parallel(
int start
651 ,
bool verbose =
false);
654 void compute_rays_parallel(
int start
657 ,
bool verbose =
false);
661 void defaultParams(PosType zsource,
bool verbose =
true);
664 bool toggle_source_plane;
672 void quicksort(LensHaloHndl *halo,PosType **pos,
unsigned long N);
680 void setFieldDistFromFile();
682 void setupFieldPlanes();
685 void ComputeHalosDistributionVariables ();
687 enum DM_Light_Division {All_DM,Moster};
689 void createFieldHalos(
bool verbose,DM_Light_Division division = Moster);
692 void readInputSimFileMillennium(
bool verbose,DM_Light_Division division = Moster);
695 void readInputSimFileMultiDarkHalos(
bool verbose,DM_Light_Division division = Moster);
698 void readInputSimFileObservedGalaxies(
bool verbose);
701 void createFieldPlanes(
bool verbose);
706 void createMainPlanes();
708 void addMainHaloToPlane(
LensHalo* halo);
709 void addMainHaloToNearestPlane(
LensHalo* halo);
712 void combinePlanes(
bool verbose);
718 const int Nzbins = 64 ;
720 const int Nmassbin=64;
724 std::vector<PosType> zbins ;
726 std::vector<PosType> NhalosbinZ ;
728 std::vector<PosType> Nhaloestot_Tab ;
730 PosType aveNhalosField ;
732 std::vector<PosType> Logm;
734 std::vector<std::vector<PosType>> NhalosbinMass;
736 std::vector<PosType> sigma_back_Tab;
741 size_t getNFieldHalos()
const {
return field_halos.size();}
747 bool flag_switch_deflection_off;
748 bool flag_switch_lensing_off;
751 std::vector<LensPlane *> lensing_planes;
753 std::vector<PosType> Dl;
755 std::vector<PosType> dDl;
757 std::vector<PosType> dTl;
759 std::vector<PosType> plane_redshifts;
765 bool flag_switch_field_off;
768 std::vector<LensHalo *> field_halos;
770 std::size_t field_Nplanes_original;
771 std::size_t field_Nplanes_current;
774 std::vector<LensPlane*> field_planes;
776 std::vector<PosType> field_plane_redshifts;
778 std::vector<PosType> field_plane_redshifts_original;
780 std::vector<PosType> field_Dl;
782 std::vector<PosType> field_Dl_original;
813 PosType mass_func_PL_slope;
815 PosType field_min_mass;
819 PosType field_prof_internal_slope;
822 bool flag_field_gal_on;
826 PosType field_int_prof_gal_slope;
831 std::string redshift_planes_file;
832 bool read_redshift_planes;
834 std::string field_input_sim_file;
835 HaloCatFormats field_input_sim_format;
843 PosType field_buffer;
847 bool flag_switch_main_halo_on;
853 std::vector<LensPlane*> main_planes;
855 std::vector<PosType> main_plane_redshifts;
857 std::vector<PosType> main_Dl;
866 std::string pixel_map_input_file;
871 int pixel_map_zeropad;
872 bool pixel_map_zeromean;
873 void readPixelizedDensity();
878 PosType sim_angular_radius;
880 PosType inv_ang_screening_scale;
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);
906typedef Lens* LensHndl;
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);
1022 assert(alpha[0] == alpha[0] && alpha[1] == alpha[1]);
1023 assert(gamma[0] == gamma[0] && gamma[1] == gamma[1]);
1024 assert(kappa == kappa);
1025 if(std::isinf(kappa)) { std::cout <<
"xx = " << xx[0] <<
" " << xx[1] << std::endl ;}
1026 assert(!std::isinf(kappa));
1028 G[0] = kappa + gamma[0]; G[1] = gamma[1];
1029 G[2] = gamma[1]; G[3] = kappa - gamma[0];
1035 G *= charge * Dl[j] / (1 + plane_redshifts[j]);
1037 assert(gamma[0] == gamma[0] && gamma[1] == gamma[1]);
1038 assert(kappa == kappa);
1045 SumPrevAlphas[0] -= charge * alpha[0] ;
1046 SumPrevAlphas[1] -= charge * alpha[1] ;
1049 aa = dDl[j+1] / Dl[j+1];
1050 bb = Dl[j] / Dl[j+1];
1056 if(!flag_switch_deflection_off){
1057 theta[0] = bb * theta[0] + aa * SumPrevAlphas[0];
1058 theta[1] = bb * theta[1] + aa * SumPrevAlphas[1];
1064 SumPrevAG -= (G * (i_points[i].A)) ;
1067 i_points[i].A = i_points[i].A * bb + SumPrevAG * aa;
1080 if(flag_switch_deflection_off){
1081 i_points[i].A = Matrix2x2<KappaType>::I() - SumPrevAG;
1088 i_points[i].dt *= MpcToSeconds * SecondToYears ;
1093 i_points[i].image->A = i_points[i].A;
1094 i_points[i].image->dt = i_points[i].dt;
1100 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
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:496
Lens & operator=(Lens &&lens)
Definition lens.h:515
void TurnFieldOff()
set flag_switch_field_off, turn the field On/Off :
Definition lens.h:492
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:485
const COSMOLOGY & getCosmo()
returns a const reference to the cosmology so that constant functions can be used,...
Definition lens.h:489
void PrintCosmology()
print the cosmological parameters
Definition lens.h:482
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
Lens(long *seed, PosType z_source, CosmoParamSet cosmoset, bool verbose=false)
Creates an empty lens. Main halos and field halos need to be inserted by hand from the user.
Definition lens.cpp:38
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:615
void RevertSourcePlane()
Revert the source redshift to the value it was when the Lens was created.
Definition lens.h:469
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
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:503
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:421
Simple representaion of a light path giving position on the image and source planes and lensing quant...
Definition point.h:517