GLAMERDOC++
Gravitational Lensing Code Library
lens.h
1 /*
2  * multiplane.h
3  *
4  */
5 
6 #ifndef MULTIPLANE_H_
7 #define MULTIPLANE_H_
8 
9 #include "quadTree.h"
10 #include "utilities_slsim.h"
11 #include "planes.h"
12 #include "geometry.h"
13 
14 #include <map>
15 
16 GLAMER_TEST_USES(LensTest)
17 
18 
71 class Lens{
72 public:
73  Lens(long* seed, PosType z_source,CosmoParamSet cosmoset, bool verbose = false);
74  //Lens(InputParams& params, long* my_seed, CosmoParamSet cosmoset, bool verbose = false);
75  Lens(long* seed, PosType z_source,const COSMOLOGY &cosmo, bool verbose = false);
76  //Lens(InputParams& params, long* my_seed, const COSMOLOGY &cosmo, bool verbose = false);
77 
78  ~Lens();
79 
81  bool set;
82 
84  int getNplanes() const {return lensing_planes.size();}
85 
87  PosType getfov() const {return fieldofview;};
88  void setfov(PosType fov){fieldofview=fov;};
89 
91  void resetFieldNplanes(std::size_t field_Nplanes, bool verbose = false);
92 
95  void resetFieldHalos(bool verbose = false);
96 
98  void printMultiLens();
99 
100 
102  PosType getZlens() const{
103  if(flag_switch_main_halo_on)
104  return main_halos[0]->getZlens();
105  else{
106  ERROR_MESSAGE();
107  std::cerr << "error, no main lens present" << std::endl;
108  exit(1);
109  }
110  }
112  PosType getAngDistLens() const{
113  if(flag_switch_main_halo_on)
114  return cosmo.angDist( main_halos[0]->getZlens());
115  else{
116  ERROR_MESSAGE();
117  std::cerr << "error, no main lens present" << std::endl;
118  exit(1);
119  }
120  }
121 
122  Utilities::Geometry::SphericalPoint<> getCenter() const {return central_point_sphere;}
123 
125  void clearMainHalos(bool verbose=false);
127  template<typename HaloType>
128  void clearMainHalo(bool verbose=false);
129 
130 
132  //void insertMainHalo(LensHalo* halo,PosType zlens, bool addplanes,bool verbose = false);
133 
134 /* void insertMainHalo(LensHalo *halo, bool addplanes,bool verbose)
135  {
136 // LensHaloNFW * halo = new LensHaloNFW(halo_in);
137  halo->setCosmology(cosmo);
138  main_halos.push_back(halo);
139 
140  flag_switch_main_halo_on = true;
141 
142  if(addplanes) addMainHaloToPlane(halo);
143  else addMainHaloToNearestPlane(halo);
144 
145  combinePlanes(verbose);
146  }
147 */
148 
149  template <typename T>
150  void insertMainHalo(const T &halo_in, bool addplanes,bool verbose=false)
151  {
152 
153  T * halo = new T(halo_in);
154  halo->setCosmology(cosmo);
155  main_halos.push_back(halo);
156 
157  flag_switch_main_halo_on = true;
158 
159  if(addplanes) addMainHaloToPlane(halo);
160  else addMainHaloToNearestPlane(halo);
161 
162  combinePlanes(verbose);
163  }
164 
172  template <typename T>
173  void moveinMainHalo(T &halo_in, bool addplanes,bool verbose=false)
174  {
175  T * halo = new T(std::move(halo_in));
176  halo->setCosmology(cosmo);
177  main_halos.push_back(halo);
178 
179  flag_switch_main_halo_on = true;
180 
181  if(addplanes) addMainHaloToPlane(halo);
182  else addMainHaloToNearestPlane(halo);
183 
184  combinePlanes(verbose);
185  }
186 
193  template <typename T>
194  void replaceMainHalo(const T &halo_in,bool addplanes,bool verbose=false)
195  {
196  main_halos.clear();
197 
198  T * halo = new T(halo_in);
199  halo->setCosmology(cosmo);
200  main_halos.push_back(halo);
201 
202  flag_switch_main_halo_on = true;
203 
204  Utilities::delete_container(main_planes);
205  createMainPlanes();
206  combinePlanes(verbose);
207  }
208 
219  template <typename T>
220  void insertMainHalos(std::vector<T> &my_halos,bool addplanes, bool verbose=false)
221  {
222  T* ptr;
223  //for(std::size_t i = 0; i < my_halos.size() ; ++i)
224  for(T &h : my_halos){
225  ptr = new T(h);
226  ptr->setCosmology(cosmo);
227  ptr->setDist(cosmo);
228  main_halos.push_back( ptr );
229  if(addplanes) addMainHaloToPlane( ptr );
230  else addMainHaloToNearestPlane( ptr );
231  }
232 
233  flag_switch_main_halo_on = true;
234 
235  combinePlanes(verbose);
236  }
243  template <typename T>
244  void replaceMainHalos(std::vector<T> &my_halos,bool verbose)
245  {
246 
247  main_halos.clear();
248 
249  T* ptr;
250  //for(std::size_t i = 0; i < my_halos.size() ; ++i)
251  for(T &h : my_halos){
252  ptr = new T(h);
253  ptr->setCosmology(cosmo);
254  ptr->setDist(cosmo);
255  main_halos.push_back( ptr );
256  }
257 
258  flag_switch_main_halo_on = true;
259 
260  Utilities::delete_container(main_planes);
261  createMainPlanes();
262  combinePlanes(verbose);
263  }
264 
266  //void insertMainHalos(LensHalo** halos, std::size_t Nhalos,bool addplanes,bool verbose = false);
267 
269  //void replaceMainHalo(LensHalo* halo,PosType zlens, bool addplanes,bool verbose = false);
270 
272  // replaceMainHalos(LensHalo** halos, std::size_t Nhalos,bool verbose = false);
273 
274 
275 // /* \brief Add substructures to the lens.
276 //
277 // This method is meant for inserting substructure to a main lens. All the substructure will be at
278 // one redshift. The mass function follows a power law. The density of substructures is constant within
279 // a circular region. The tidal truncation is controlled through the parameter density_contrast which is
280 // the average density within the substructures orbit in units of the average density to the universe at
281 // the redshift where they are places. For example density_contrast=200 would give them the truncation radius appropriate at R_200.
282 // */
283 // void insertSubstructures(
284 // PosType Rregion /// radius of region in which substructures are inserted (radians)
285 // ,PosType center[] /// center of region in which the substructures are inserted (radians)
286 // ,PosType NumberDensity /// number density per radian^2 of all substructures
287 // ,PosType Mass_min /// minimum mass of substructures
288 // ,PosType Mass_max /// maximum mass of substructures
289 // ,PosType redshift /// redshift of substructures
290 // ,PosType alpha /// index of mass function (dN/dm \propto m^alpha)
291 // ,PosType density_contrast ///
292 // ,bool verbose
293 // );
294 //
295 // /** \brief This function will randomize the substructure without changing the region, mass function, etc.
296 //
297 // The Lens::insertSubstructures() function must have been called on this instance of the Lens before.
298 // */
299 // void resetSubstructure(bool verbose = false);
300 //
301 
303  std::size_t getNMainHalos() const;
305  template<typename HaloType>
306  std::size_t getNMainHalos() const;
307 
309  LensHalo* getMainHalo(std::size_t i);
310 
312  template<typename HaloType>
313  HaloType* getMainHalo(std::size_t i);
314 
322  void rayshooter(RAY &ray);
323 
330  void rayshooterInternal(unsigned long Npoints
331  ,Point *i_points
332  ,bool RSIverbose = false
333  );
334 
340  void rayshooterInternal(unsigned long Npoints
341  ,LinkedPoint *i_points
342  ,std::vector<double> &source_zs
343  ,bool RSIverbose = false
344  );
345 
346  void rayshooterInternal(unsigned long Npoints
347  ,RAY *rays
348  );
349  void rayshooterInternal(RAY &ray);
350 
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
355  ,LensHalo &halo_max
356  ,KappaType &kappa_max
357  ,KappaType gamma_max[]
358  ,PosType rmax
359  ,int tag = 0
360  ,short mode = 0
361  ,bool verbose = false
362  );
363 
365  void mass_on_planes(const std::vector<RAY> &rays
366  ,std::vector<double> &masses
367  ,bool verbose = false
368  );
369 
374  /*RAY find_image_min(
375  Point_2d y_source /// input position of source (radians)
376  ,Point_2d &x_image /// initial guess for image postion (radians)
377  ,PosType z_source /// redshift of source
378  ,PosType ytol2 /// target tolerance in source position squared
379  ,PosType &fret ///
380  ,int sign=0 /// sign of magnification, it is found automatically if left out
381  );*/
382 
393  RAY find_image_min(
394  Point &p
395  ,double zs
396  ,PosType ytol2
397  ,PosType &dy2
398  ,bool use_image_guess
399  );
404  RAY find_image_min(const RAY &in_ray
405  ,PosType ytol2
406  );
407 
409 // RAY find_image_min(
410 // RAY &p /// p[] is
411 // ,PosType ytol2 /// target tolerance in source position squared
412 // ,PosType &dy2 /// final value of Delta y ^2
413 // ,std::vector<Point_2d> &boundary /// image will be limited to within this boundary
414 // );
415  RAY find_image_min(
416  Point &p
417  ,double zs
418  ,PosType ytol2
419  ,PosType &dy2
420  ,std::vector<Point_2d> &boundary
421  );
422 
424  std::vector<RAY> find_images(Point_2d y_source
425  ,double z_source
426  ,Point_2d center
427  ,double range
428  ,double stop_res
429  );
431  std::vector<RAY> find_images(GridMap &init_grid
432  ,Point_2d y_source
433  ,double z_source
434  ,double stop_res
435  );
436 
437  void find_images_min_parallel(std::vector<RAY> &rays
438  ,double ytol2
439  ,std::vector<bool> &success
440  );
441 // void find_point_source_images(
442 // Grid &grid
443 // ,Point_2d y_source
444 // ,PosType r_source
445 // ,PosType z_source
446 // ,std::vector<RAY> &images
447 // ,double dytol2
448 // ,double dxtol
449 // ,bool verbose = false
450 // );
451 
452 
453  // methods used for use with implanted sources
454 
456  short ResetSourcePlane(PosType z,bool nearest=false,bool verbose = false);
457 
459  void FindSourcePlane(
460  PosType zs
461  ,long &jmax
462  ,double &Dls
463  ,double &Ds
464  );
465 
467  void RevertSourcePlane(){ toggle_source_plane = false;}
468  //void ImplantSource(unsigned long index,CosmoHndl cosmo);
469  PosType getSourceZ(){
470  if(toggle_source_plane){
471  return zs_implant;
472  }else{
473  return plane_redshifts.back();
474  }
475  }
476 
477  PosType getZmax() const{return plane_redshifts.back();}
478 
480  void PrintCosmology() { cosmo.PrintCosmology(); }
481 
483  PosType getSigmaCrit(PosType zsource) const{ return cosmo.SigmaCrit(getZlens(), zsource); }
484 
485 
487  const COSMOLOGY & getCosmo(){return cosmo;}
488 
490  void TurnFieldOff() { flag_switch_field_off = true ; }
491  void TurnFieldOn() { flag_switch_field_off = false ; }
492 
494  PosType getFieldMinMass() const { return field_min_mass ; }
495 
496  // get the field_Off value :
497  bool getfieldOff() const {return flag_switch_field_off ;}
498 
503  void GenerateFieldHalos(double min_mass
504  ,MassFuncType mass_function
505  ,double field_of_view
506  ,int Nplanes
507  ,LensHaloType halo_type = LensHaloType::nfw_lens
508  ,GalaxyLensHaloType galaxy_type = GalaxyLensHaloType::null_gal
509  ,double buffer = 1.0
510  ,bool verbose = false
511  );
512 
513  Lens & operator=(Lens &&lens){
514 
515  fieldofview = lens.fieldofview;
516  seed = lens.seed;
517  init_seed = lens.init_seed;
518  cosmo = lens.cosmo;
519  toggle_source_plane = lens.toggle_source_plane;
520 
521  zs_implant = lens.zs_implant;
522  zsource = lens.zsource;
523 
524  NZSamples = lens.NZSamples;
525  zbins = lens.zbins;
526  NhalosbinZ = lens.NhalosbinZ;
527  Nhaloestot_Tab = lens.Nhaloestot_Tab;
528  aveNhalosField = lens.aveNhalosField;
529  Logm = lens.Logm;
530  NhalosbinMass = lens.NhalosbinMass;
531  sigma_back_Tab = lens.sigma_back_Tab;
532 
533  flag_switch_deflection_off = lens.flag_switch_deflection_off;
534  flag_switch_lensing_off = lens.flag_switch_lensing_off;
535 
536  Dl = lens.Dl;
537  dDl = lens.dDl;
538  dTl = lens.dTl;
539  plane_redshifts = lens.plane_redshifts;
540  charge = lens.charge;
541  flag_switch_field_off = lens.flag_switch_field_off;
542 
543  field_halos = lens.field_halos;
544 
545  field_Nplanes_original = lens.field_Nplanes_original;
546  field_Nplanes_current = lens.field_Nplanes_current;
547 
548  field_plane_redshifts = lens.field_plane_redshifts;
549  field_plane_redshifts_original = lens.field_plane_redshifts_original;
550  field_Dl = lens.field_Dl;
551  field_Dl_original = lens.field_Dl_original;
552 
553  //substructure = lens.substructure;
554 
555  //WasInsertSubStructuresCalled = lens.WasInsertSubStructuresCalled;
556  field_mass_func_type = lens.field_mass_func_type;
557  mass_func_PL_slope = lens.mass_func_PL_slope;
558  field_min_mass = lens.field_min_mass;
559  field_int_prof_type = lens.field_int_prof_type;
560  field_prof_internal_slope = lens.field_prof_internal_slope;
561 
562  flag_field_gal_on = lens.flag_field_gal_on;
563  field_int_prof_gal_type = lens.field_int_prof_gal_type;
564  field_int_prof_gal_slope = lens.field_int_prof_gal_slope;
565 
566  redshift_planes_file = lens.redshift_planes_file;
567  read_redshift_planes = lens.read_redshift_planes;
568 
569  field_input_sim_file = lens.field_input_sim_file;
570  field_input_sim_format = lens.field_input_sim_format;
571 
572  sim_input_flag = lens.sim_input_flag;
573  read_sim_file = lens.read_sim_file;
574  field_buffer = lens.field_buffer;
575 
576  flag_switch_main_halo_on = lens.flag_switch_main_halo_on;
577 
578  main_plane_redshifts = lens.main_plane_redshifts;
579  main_Dl = lens.main_Dl;
580 
581  main_halo_type = lens.main_halo_type;
582  main_galaxy_halo_type = lens.main_galaxy_halo_type;
583 
584  pixel_map_input_file = lens.pixel_map_input_file;
585  pixel_map_on = lens.pixel_map_on;
586  pixel_map_zeropad = lens.pixel_map_zeropad;
587  pixel_map_zeromean = lens.pixel_map_zeromean;
588 
589  central_point_sphere = lens.central_point_sphere;
590  sim_angular_radius = lens.sim_angular_radius;
591  inv_ang_screening_scale = lens.inv_ang_screening_scale;
592 
593  std::swap(lensing_planes,lens.lensing_planes);
594  std::swap(field_planes,lens.field_planes);
595  swap(main_halos,lens.main_halos);
596  std::swap(main_planes,lens.main_planes);
597 
598  return *this;
599  }
600  Lens(Lens &&lens){
601  *this = std::move(lens);
602  }
603 
604  // get the adress of field_plane_redshifts
605  std::vector<PosType> get_plane_redshifts () { return plane_redshifts; }
606 
607 private:
608  Lens & operator=(const Lens &lens); // block copy
609  Lens(const Lens &lens);
610 
611 protected:
613  PosType fieldofview;
614 
615 private:
616  GLAMER_TEST_FRIEND(LensTest)
617 
618 
619  void _find_images_(std::vector<RAY> &images
620  ,RAY &center
621  ,double range
622  ,double stop_res
623  );
624 
625  void _find_images_min_parallel_(RAY *rays
626  ,size_t begin
627  ,size_t end
628  ,double ytol2
629  ,std::vector<bool> &success
630  );
631 
632  // seed for random field generation
633  long *seed;
634 
635  long init_seed;
636  //InputParams init_params;
637 
638  // the cosmology
639  COSMOLOGY cosmo;
640 
641  template<typename P>
642  void compute_points_parallel(int start
643  ,int chunk_size
644  ,P *i_point
645  ,double *source_zs
646  ,bool multiZs
647  ,bool verbose = false);
648 
649  // this is a version that uses RAYs instead of Points or LinkedPoints and must be kept sinked with the above
650  void compute_rays_parallel(int start
651  ,int chunk_size
652  ,RAY *rays
653  ,bool verbose = false);
654 
655  //void readCosmology(InputParams& params);
656  //void assignParams(InputParams& params,bool verbose = false);
657  void defaultParams(PosType zsource,bool verbose = true);
658 
660  bool toggle_source_plane;
661 
662  // redhsift of true source plane if rest from original
663  PosType zs_implant;
664 
666  PosType zsource;
667 
668  void quicksort(LensHaloHndl *halo,PosType **pos,unsigned long N);
669 
670  // create the lens planes
671  //void buildPlanes(InputParams& params, bool verbose);
672 
674  void setFieldDist();
676  void setFieldDistFromFile();
678  void setupFieldPlanes();
681  void ComputeHalosDistributionVariables ();
682 
683  enum DM_Light_Division {All_DM,Moster};
684 
685  void createFieldHalos(bool verbose,DM_Light_Division division = Moster);
686 
688  void readInputSimFileMillennium(bool verbose,DM_Light_Division division = Moster);
689 
691  void readInputSimFileMultiDarkHalos(bool verbose,DM_Light_Division division = Moster);
692 
694  void readInputSimFileObservedGalaxies(bool verbose);
695 
697  void createFieldPlanes(bool verbose);
698 
699  // generate main halo from the parameter file
700  //void createMainHalos(InputParams& params);
702  void createMainPlanes();
704  void addMainHaloToPlane(LensHalo* halo);
705  void addMainHaloToNearestPlane(LensHalo* halo);
706 
708  void combinePlanes(bool verbose);
709 
710  /* Variables used by buildPlanes, createFieldHalos, and createFieldPlanes */
712 
714  const int Nzbins = 64 ;
716  const int Nmassbin=64;
718  int NZSamples = 50;
719  // table for redshift bins for mass function
720  std::vector<PosType> zbins ;
721  // table of number of halos per redshift bins for mass function
722  std::vector<PosType> NhalosbinZ ;
724  std::vector<PosType> Nhaloestot_Tab ;
726  PosType aveNhalosField ;
728  std::vector<PosType> Logm;
730  std::vector<std::vector<PosType>> NhalosbinMass;
732  std::vector<PosType> sigma_back_Tab;
733 
734  /* ----- */
735 
737  size_t getNFieldHalos() const {return field_halos.size();}
739  //size_t getNSubHalos() const {return substructure.halos.size();}
740 
741 private: /* force calculation */
743  bool flag_switch_deflection_off;
744  bool flag_switch_lensing_off;
745 
747  std::vector<LensPlane *> lensing_planes;
749  std::vector<PosType> Dl;
751  std::vector<PosType> dDl;
753  std::vector<PosType> dTl;
755  std::vector<PosType> plane_redshifts;
757  PosType charge;
758 
759 private: /* field */
761  bool flag_switch_field_off;
762 
764  std::vector<LensHalo *> field_halos;
766  std::size_t field_Nplanes_original;
767  std::size_t field_Nplanes_current;
768 
770  std::vector<LensPlane*> field_planes;
772  std::vector<PosType> field_plane_redshifts;
774  std::vector<PosType> field_plane_redshifts_original;
776  std::vector<PosType> field_Dl;
778  std::vector<PosType> field_Dl_original;
779 
780 // struct SubStructureInfo{
781 // // things for substructures
782 // /// vector of all substructure halos
783 // std::vector<LensHaloNFW *> halos;
784 // LensPlane *plane;
785 // PosType Rregion = 0;
786 // PosType Mmax = 0;
787 // PosType Mmin = 0;
788 // PosType alpha = 0;
789 // PosType Ndensity = 0;
790 // Point_2d center;
791 // PosType rho_tidal = 0;
792 // // Added quantities for the resetting of the substructure
793 // // (when WasInsertSubStructuresCalled = MAYBE) :
794 // PosType redshift = 0;
795 // bool verbose = false;
796 // };
797 //
798 // SubStructureInfo substructure;
799 
801  // Boo WasInsertSubStructuresCalled = NO ;
802 
804  //PosType **halo_pos;
805 
807  MassFuncType field_mass_func_type;
809  PosType mass_func_PL_slope;
811  PosType field_min_mass;
813  LensHaloType field_int_prof_type;
815  PosType field_prof_internal_slope;
816 
818  bool flag_field_gal_on;
820  GalaxyLensHaloType field_int_prof_gal_type;
822  PosType field_int_prof_gal_slope;
823 
824  // mass fraction in the host galaxy
825  //PosType field_galaxy_mass_fraction;
826 
827  std::string redshift_planes_file;
828  bool read_redshift_planes;
829 
830  std::string field_input_sim_file;
831  HaloCatFormats field_input_sim_format;
832 
833  bool sim_input_flag;
834  //std::string input_gal_file;
835  //bool gal_input_flag;
836  bool read_sim_file;
837 
839  PosType field_buffer;
840 
841 private: /* main */
843  bool flag_switch_main_halo_on;
844 
849  std::vector<LensPlane*> main_planes;
851  std::vector<PosType> main_plane_redshifts;
853  std::vector<PosType> main_Dl;
854 
856  LensHaloType main_halo_type;
858  GalaxyLensHaloType main_galaxy_halo_type;
859 
860 private: /* input */
862  std::string pixel_map_input_file;
863 
865  short pixel_map_on;
867  int pixel_map_zeropad;
868  bool pixel_map_zeromean;
869  void readPixelizedDensity();
870 
874  PosType sim_angular_radius;
876  PosType inv_ang_screening_scale;
877 
878  struct MINyFunction{
879  MINyFunction(Lens &mylens,Point_2d y,int sign):lens(mylens),y(y),sign(sign),r2max(0){}
880 
881  double operator()(double *x){
882  point.x[0] = x[1];
883  point.x[1] = x[2];
884  lens.rayshooterInternal(1,&point);
885  double r2 = (y[0]-point.image->x[0])*(y[0]-point.image->x[0])
886  + (y[1]-point.image->x[1])*(y[1]-point.image->x[1]);
887 
888  r2max = MAX(r2,r2max);
889  return r2 + r2max*abs(sign - sgn(point.invmag()));
890  }
891 
892  Lens &lens;
893  Point_2d y;
894  int sign;
895  double r2max;
896  LinkedPoint point;
897  };
898 };
899 
900 inline std::size_t Lens::getNMainHalos() const
901 {
902  return main_halos.size();
903 }
904 
905 template<typename HaloType>
906 inline std::size_t Lens::getNMainHalos() const
907 {
908  return main_halos.size<HaloType>();
909 }
910 
911 inline LensHalo* Lens::getMainHalo(std::size_t i)
912 {
913  return main_halos.at(i);
914 }
915 
916 template<typename HaloType>
917 inline HaloType* Lens::getMainHalo(std::size_t i)
918 {
919  if(main_halos.size<HaloType>() == 0 ) return nullptr;
920  return main_halos.at<HaloType>(i);
921 }
922 
923 typedef Lens* LensHndl;
924 
925 // P should be Point or LinkedPoint
926 template <typename P>
927 void Lens::compute_points_parallel(int start
928  ,int chunk_size
929  ,P *i_points
930  ,double *source_zs
931  ,bool multiZs
932  ,bool verbose)
933 {
934  int end = start + chunk_size;
935 
936  int i, j;
937 
938  PosType xx[2];
939  PosType aa,bb;
940  PosType alpha[2];
941 
942  KappaType kappa,gamma[3];
943  KappaType phi;
944 
945  Matrix2x2<PosType> G;
946 
947  PosType SumPrevAlphas[2];
948  Matrix2x2<PosType> SumPrevAG;
949 
950  PosType *theta;
951 
952  long jmax = lensing_planes.size();
953  double Dls_Ds; // this is the ratio between of the distance between the last lens plane and the source to the distance to the source
954  double D_Ds; // this is the ratio between of the distance to the last lens plane and the source to the distance to the source
955 
956  if(!multiZs){
957  if(source_zs[0] == plane_redshifts.back() ){
958  Dls_Ds = dDl.back() / Dl.back();
959  D_Ds = Dl[Dl.size() - 2] / Dl.back();
960  }else{
961  PosType Dls,Ds;
962  FindSourcePlane(source_zs[0],jmax,Dls,Ds);
963  Dls_Ds = Dls / Ds;
964  if(jmax > 0) D_Ds = Dl[jmax-1] / Ds;
965  }
966  }
967 
968  // Main loop : loop over the points of the image
969  for(i = start; i < end; i++)
970  {
971  // In case e.g. a temporary point is outside of the grid.
972  if(i_points[i].in_image == MAYBE) continue;
973 
974  //theta = i_points[i].image->x;
975  theta = i_points[i].ptr_y();
976 
977  theta[0] = i_points[i].x[0];
978  theta[1] = i_points[i].x[1];
979 
980  // Initializing SumPrevAlphas :
981  SumPrevAlphas[0] = theta[0];
982  SumPrevAlphas[1] = theta[1];
983 
984  // Initializing SumPrevAG :
985  SumPrevAG.setToI();
986 
987  // Setting phi on the first plane.
988  phi = 0.0;
989 
990  // Default values :
991  i_points[i].A.setToI();
992  i_points[i].dt = 0;
993 
994  // In case we don't want to compute the values :
995  if(flag_switch_lensing_off)
996  {
997  i_points[i].image->A.setToI();
998  continue;
999  }
1000 
1001  // Time delay at first plane : position on the observer plane is (0,0) => no need to take difference of positions.
1002  i_points[i].dt = 0;
1003 
1004  //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] ;
1005 
1006  if(multiZs){
1007  PosType Dls,Ds;
1008  FindSourcePlane(source_zs[i],jmax,Dls,Ds);
1009  Dls_Ds = Dls / Ds;
1010  if(jmax > 0) D_Ds = Dl[jmax-1]/Ds;
1011  }
1012 
1013  // Begining of the loop through the planes :
1014  // Each iteration leaves i_point[i].image on plane (j+1)
1015 
1016  for(j = 0; j < jmax ; ++j)
1017  {
1018 
1019  double Dphysical = Dl[j]/(1 + plane_redshifts[j]);
1020  // convert to physical coordinates on the plane j, just for force calculation
1021  xx[0] = theta[0] * Dphysical;
1022  xx[1] = theta[1] * Dphysical;
1023  // PhysMpc = ComMpc / (1+z)
1024 
1025  assert(xx[0] == xx[0] && xx[1] == xx[1]);
1026 
1028 
1029  lensing_planes[j]->force(alpha,&kappa,gamma,&phi,xx);
1030  // Computed in physical coordinates, xx is in PhysMpc.
1031 
1033 
1034  assert(alpha[0] == alpha[0] && alpha[1] == alpha[1]);
1035  assert(gamma[0] == gamma[0] && gamma[1] == gamma[1]);
1036  assert(kappa == kappa);
1037  if(std::isinf(kappa)) { std::cout << "xx = " << xx[0] << " " << xx[1] << std::endl ;}
1038  assert(!std::isinf(kappa));
1039 
1040  G[0] = kappa + gamma[0]; G[1] = gamma[1];
1041  G[2] = gamma[1]; G[3] = kappa - gamma[0];
1042 
1043  /* multiply by fac to obtain 1/comoving_distance/physical_distance
1044  * such that a multiplication with the charge (in units of physical distance)
1045  * will result in a 1/comoving_distance quantity */
1046 
1047  G *= charge * Dl[j] / (1 + plane_redshifts[j]);
1048 
1049  assert(gamma[0] == gamma[0] && gamma[1] == gamma[1]);
1050  assert(kappa == kappa);
1051  //assert(phi == phi);
1052 
1053  // This computes \vec{x}^{j+1} in terms of \vec{x}^{j}
1054  // according to the corrected Eq. (18) of paper GLAMER II ---------------------------------
1055 
1056  // Adding the j-plane alpha contribution to the sum \Sum_{k=1}^{j} \vec{alpha_j} :
1057  SumPrevAlphas[0] -= charge * alpha[0] ;
1058  SumPrevAlphas[1] -= charge * alpha[1] ;
1059 
1060  if(j < jmax-1 ){
1061  aa = dDl[j+1] / Dl[j+1];
1062  bb = Dl[j] / Dl[j+1];
1063  }else{
1064  aa = Dls_Ds;
1065  bb = D_Ds;
1066  }
1067 
1068  if(!flag_switch_deflection_off){
1069  theta[0] = bb * theta[0] + aa * SumPrevAlphas[0];
1070  theta[1] = bb * theta[1] + aa * SumPrevAlphas[1];
1071  }
1072 
1073  // ----------------------------------------------------------------------------------------
1074 
1075  // Sum_{k=1}^{j} Dl[k] A^k.G^k
1076  SumPrevAG -= (G * (i_points[i].A)) ;
1077 
1078  // Computation of the "plus quantities", i.e. the next plane quantities :
1079  i_points[i].A = i_points[i].A * bb + SumPrevAG * aa;
1080 
1081  // ----------------------------------------------
1082 
1083  // Geometric time delay with added potential
1084  //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 ???
1085 
1086  // 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).
1087  // Remark : Here the true lensing potential is not "phi" but "phi * p->charge = phi * 4 pi G".
1088 
1089 
1090  } // End of the loop going through the planes
1091 
1092  if(flag_switch_deflection_off){
1093  i_points[i].A = Matrix2x2<KappaType>::I() - SumPrevAG;
1094  }
1095 
1096  // Subtracting off a term that makes the unperturbed ray to have zero time delay
1097  //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[NLastPlane];
1098 
1099  // Conversion of dt from Mpc (physical Mpc) to Years -----------------
1100  i_points[i].dt *= MpcToSeconds * SecondToYears ;
1101 
1102  // ---------------------------------------------------------------------------------------------
1103 
1104  // Putting the final values of the quantities in the image point -----
1105  i_points[i].image->A = i_points[i].A;
1106  i_points[i].image->dt = i_points[i].dt;
1107  // ------------------------------------------------------------------------
1108 
1109 
1110  // TEST : showing final quantities
1111  // ------------------------------=
1112  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 ;
1113 
1114  } // End of the main loop.
1115 
1116 }
1117 
1118 #endif /* MULTIPLANE_H_ */
1119 
Utilities::MixedVector::at
BaseT & at(std::size_t i)
indexed access with bounds checking
Definition: utilities_slsim.h:496
Lens::getfov
PosType getfov() const
field of view in square degrees
Definition: lens.h:87
Lens::insertMainHalos
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
Lens::replaceMainHalos
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
Lens::FindSourcePlane
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
Lens::PrintCosmology
void PrintCosmology()
print the cosmological parameters
Definition: lens.h:480
Lens::getCosmo
const COSMOLOGY & getCosmo()
returns a const reference to the cosmology so that constant functions can be used,...
Definition: lens.h:487
Lens::clearMainHalo
void clearMainHalo(bool verbose=false)
remaove all main halo of given type
LinkedPoint
A point that automatically has an image point.
Definition: point.h:496
LensHalo
A base class for all types of lensing "halos" which are any mass distribution that cause lensing.
Definition: lens_halos.h:56
MassFuncType
MassFuncType
Type of mass function.
Definition: InputParams.h:36
Lens::getNMainHalos
std::size_t getNMainHalos() const
inserts a sequence of main lens halos and adds them to the existing ones
Definition: lens.h:900
Point_2d
Class for representing points or vectors in 2 dimensions. Not that the dereferencing operator is over...
Definition: point.h:48
Lens::fieldofview
PosType fieldofview
field of view in square degrees
Definition: lens.h:613
GridMap
A simplified version of the Grid structure for making non-adaptive maps of the lensing quantities (ka...
Definition: gridmap.h:31
Lens::replaceMainHalo
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
Lens::moveinMainHalo
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
Utilities::MixedVector< LensHalo * >
Utilities::MixedVector::size
std::size_t size() const
number of all elements
Definition: utilities_slsim.h:531
Utilities::Geometry::SphericalPoint
represents a point in spherical coordinates, theta = 0 is equator
Definition: geometry.h:30
Lens::getAngDistLens
PosType getAngDistLens() const
Angular size distance (Mpc) to first main lens plane.
Definition: lens.h:112
Lens::getFieldMinMass
PosType getFieldMinMass() const
get the field min mass :
Definition: lens.h:494
Lens::TurnFieldOff
void TurnFieldOff()
set flag_switch_field_off, turn the field On/Off :
Definition: lens.h:490
RAY
Simple representaion of a light path giving position on the image and source planes and lensing quant...
Definition: point.h:510
Lens::getMainHalo
LensHalo * getMainHalo(std::size_t i)
get single main halo
Definition: lens.h:911
GalaxyLensHaloType
GalaxyLensHaloType
Definition: InputParams.h:66
Lens::getSigmaCrit
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
LensHaloType
LensHaloType
Type of halo profile.
Definition: InputParams.h:44
Lens::RevertSourcePlane
void RevertSourcePlane()
Revert the source redshift to the value it was when the Lens was created.
Definition: lens.h:467
Lens::getZlens
PosType getZlens() const
Redshift of first main lens plane.
Definition: lens.h:102
Lens::insertMainHalo
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
Point
A point on the source or image plane that contains a position and the lensing quantities.
Definition: point.h:414
Utilities::delete_container
void delete_container(Container &c)
delete the objects that are pointed to in a container of pointers
Definition: utilities_slsim.h:975
Lens::operator=
Lens & operator=(Lens &&lens)
Definition: lens.h:513
Lens::getNplanes
int getNplanes() const
the total number of lens planes
Definition: lens.h:84
Lens
A class to represents a lens with multiple planes.
Definition: lens.h:71
Lens::set
bool set
marks if the lens has been setup.
Definition: lens.h:81