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  template <typename T>
133  void insertMainHalo(const T &halo_in, bool addplanes,bool verbose=false)
134  {
135 
136  T * halo = new T(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 
155  template <typename T>
156  void moveinMainHalo(T &halo_in, bool addplanes,bool verbose=false)
157  {
158  T * halo = new T(std::move(halo_in));
159  halo->setCosmology(cosmo);
160  main_halos.push_back(halo);
161 
162  flag_switch_main_halo_on = true;
163 
164  if(addplanes) addMainHaloToPlane(halo);
165  else addMainHaloToNearestPlane(halo);
166 
167  combinePlanes(verbose);
168  }
169 
176  template <typename T>
177  void replaceMainHalo(const T &halo_in,bool addplanes,bool verbose=false)
178  {
179  main_halos.clear();
180 
181  T * halo = new T(halo_in);
182  halo->setCosmology(cosmo);
183  main_halos.push_back(halo);
184 
185  flag_switch_main_halo_on = true;
186 
187  Utilities::delete_container(main_planes);
188  createMainPlanes();
189  combinePlanes(verbose);
190  }
191 
202  template <typename T>
203  void insertMainHalos(std::vector<T> &my_halos,bool addplanes, bool verbose=false)
204  {
205  T* ptr;
206  //for(std::size_t i = 0; i < my_halos.size() ; ++i)
207  for(T &h : my_halos){
208  ptr = new T(h);
209  ptr->setCosmology(cosmo);
210  ptr->setDist(cosmo);
211  main_halos.push_back( ptr );
212  if(addplanes) addMainHaloToPlane( ptr );
213  else addMainHaloToNearestPlane( ptr );
214  }
215 
216  flag_switch_main_halo_on = true;
217 
218  combinePlanes(verbose);
219  }
226  template <typename T>
227  void replaceMainHalos(std::vector<T> &my_halos,bool verbose)
228  {
229 
230  main_halos.clear();
231 
232  T* ptr;
233  //for(std::size_t i = 0; i < my_halos.size() ; ++i)
234  for(T &h : my_halos){
235  ptr = new T(h);
236  ptr->setCosmology(cosmo);
237  ptr->setDist(cosmo);
238  main_halos.push_back( ptr );
239  }
240 
241  flag_switch_main_halo_on = true;
242 
243  Utilities::delete_container(main_planes);
244  createMainPlanes();
245  combinePlanes(verbose);
246  }
247 
248 // /* \brief Add substructures to the lens.
249 //
250 // This method is meant for inserting substructure to a main lens. All the substructure will be at
251 // one redshift. The mass function follows a power law. The density of substructures is constant within
252 // a circular region. The tidal truncation is controlled through the parameter density_contrast which is
253 // the average density within the substructures orbit in units of the average density to the universe at
254 // the redshift where they are places. For example density_contrast=200 would give them the truncation radius appropriate at R_200.
255 // */
256 // void insertSubstructures(
257 // PosType Rregion /// radius of region in which substructures are inserted (radians)
258 // ,PosType center[] /// center of region in which the substructures are inserted (radians)
259 // ,PosType NumberDensity /// number density per radian^2 of all substructures
260 // ,PosType Mass_min /// minimum mass of substructures
261 // ,PosType Mass_max /// maximum mass of substructures
262 // ,PosType redshift /// redshift of substructures
263 // ,PosType alpha /// index of mass function (dN/dm \propto m^alpha)
264 // ,PosType density_contrast ///
265 // ,bool verbose
266 // );
267 //
268 // /** \brief This function will randomize the substructure without changing the region, mass function, etc.
269 //
270 // The Lens::insertSubstructures() function must have been called on this instance of the Lens before.
271 // */
272 // void resetSubstructure(bool verbose = false);
273 //
274 
276  std::size_t getNMainHalos() const;
278  template<typename HaloType>
279  std::size_t getNMainHalos() const;
280 
282  LensHalo* getMainHalo(std::size_t i);
283 
285  template<typename HaloType>
286  HaloType* getMainHalo(std::size_t i);
287 
295  void rayshooter(RAY &ray);
296 
303  void rayshooterInternal(unsigned long Npoints
304  ,Point *i_points
305  ,bool RSIverbose = false
306  );
307 
313  void rayshooterInternal(unsigned long Npoints
314  ,LinkedPoint *i_points
315  ,std::vector<double> &source_zs
316  ,bool RSIverbose = false
317  );
318 
319  void rayshooterInternal(unsigned long Npoints
320  ,RAY *rays
321  );
322  void rayshooterInternal(RAY &ray);
323 
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
328  ,LensHalo &halo_max
329  ,KappaType &kappa_max
330  ,KappaType gamma_max[]
331  ,PosType rmax
332  ,int tag = 0
333  ,short mode = 0
334  ,bool verbose = false
335  );
336 
338  void mass_on_planes(const std::vector<RAY> &rays
339  ,std::vector<double> &masses
340  ,bool verbose = false
341  );
342 
347  /*RAY find_image_min(
348  Point_2d y_source /// input position of source (radians)
349  ,Point_2d &x_image /// initial guess for image postion (radians)
350  ,PosType z_source /// redshift of source
351  ,PosType ytol2 /// target tolerance in source position squared
352  ,PosType &fret ///
353  ,int sign=0 /// sign of magnification, it is found automatically if left out
354  );*/
355 
367  RAY find_image_min(
368  Point &p
369  ,double zs
370  ,PosType ytol2
371  ,PosType &dy2
372  ,bool use_image_guess
373  );
376  RAY find_image_min(const RAY &in_ray
377  ,PosType ytol2
378  );
379 
381  RAY find_image_min(
382  Point &p
383  ,double zs
384  ,PosType ytol2
385  ,PosType &dy2
386  ,std::vector<Point_2d> &boundary
387  );
389  void find_images_min_parallel(std::vector<RAY> &rays
390  ,double ytol2
391  ,std::vector<bool> &success
392  );
393 
398  std::vector<RAY> find_images(Point_2d y_source
399  ,double z_source
400  ,Point_2d center
401  ,double range
402  ,double stop_res
403  );
409  std::vector<RAY> find_images(GridMap &init_grid
410  ,Point_2d y_source
411  ,double z_source
412  ,double stop_res
413  );
414 
415 
416 // void find_point_source_images(
417 // Grid &grid
418 // ,Point_2d y_source
419 // ,PosType r_source
420 // ,PosType z_source
421 // ,std::vector<RAY> &images
422 // ,double dytol2
423 // ,double dxtol
424 // ,bool verbose = false
425 // );
426 
427 
428  // methods used for use with implanted sources
429 
431  short ResetSourcePlane(PosType z,bool nearest=false,bool verbose = false);
432 
434  void FindSourcePlane(
435  PosType zs
436  ,long &jmax
437  ,double &Dls
438  ,double &Ds
439  );
440 
442  void RevertSourcePlane(){ toggle_source_plane = false;}
443  //void ImplantSource(unsigned long index,CosmoHndl cosmo);
444  PosType getSourceZ(){
445  if(toggle_source_plane){
446  return zs_implant;
447  }else{
448  return plane_redshifts.back();
449  }
450  }
451 
452  PosType getZmax() const{return plane_redshifts.back();}
453 
455  void PrintCosmology() { cosmo.PrintCosmology(); }
456 
458  PosType getSigmaCrit(PosType zsource) const{ return cosmo.SigmaCrit(getZlens(), zsource); }
459 
460 
462  const COSMOLOGY & getCosmo(){return cosmo;}
463 
465  void TurnFieldOff() { flag_switch_field_off = true ; }
466  void TurnFieldOn() { flag_switch_field_off = false ; }
467 
469  PosType getFieldMinMass() const { return field_min_mass ; }
470 
471  // get the field_Off value :
472  bool getfieldOff() const {return flag_switch_field_off ;}
473 
474 
475  Lens & operator=(Lens &&lens){
476 
477  fieldofview = lens.fieldofview;
478  seed = lens.seed;
479  init_seed = lens.init_seed;
480  cosmo = lens.cosmo;
481  toggle_source_plane = lens.toggle_source_plane;
482 
483  zs_implant = lens.zs_implant;
484  zsource = lens.zsource;
485 
486  NZSamples = lens.NZSamples;
487  zbins = lens.zbins;
488  NhalosbinZ = lens.NhalosbinZ;
489  Nhaloestot_Tab = lens.Nhaloestot_Tab;
490  aveNhalosField = lens.aveNhalosField;
491  Logm = lens.Logm;
492  NhalosbinMass = lens.NhalosbinMass;
493  sigma_back_Tab = lens.sigma_back_Tab;
494 
495  flag_switch_deflection_off = lens.flag_switch_deflection_off;
496  flag_switch_lensing_off = lens.flag_switch_lensing_off;
497 
498  Dl = lens.Dl;
499  dDl = lens.dDl;
500  dTl = lens.dTl;
501  plane_redshifts = lens.plane_redshifts;
502  charge = lens.charge;
503  flag_switch_field_off = lens.flag_switch_field_off;
504 
505  field_halos = lens.field_halos;
506 
507  field_Nplanes_original = lens.field_Nplanes_original;
508  field_Nplanes_current = lens.field_Nplanes_current;
509 
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;
514 
515  //substructure = lens.substructure;
516 
517  //WasInsertSubStructuresCalled = lens.WasInsertSubStructuresCalled;
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;
523 
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;
527 
528  redshift_planes_file = lens.redshift_planes_file;
529  read_redshift_planes = lens.read_redshift_planes;
530 
531  field_input_sim_file = lens.field_input_sim_file;
532  field_input_sim_format = lens.field_input_sim_format;
533 
534  sim_input_flag = lens.sim_input_flag;
535  read_sim_file = lens.read_sim_file;
536  field_buffer = lens.field_buffer;
537 
538  flag_switch_main_halo_on = lens.flag_switch_main_halo_on;
539 
540  main_plane_redshifts = lens.main_plane_redshifts;
541  main_Dl = lens.main_Dl;
542 
543  main_halo_type = lens.main_halo_type;
544  main_galaxy_halo_type = lens.main_galaxy_halo_type;
545 
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;
550 
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;
554 
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);
559 
560  return *this;
561  }
562  Lens(Lens &&lens){
563  *this = std::move(lens);
564  }
565 
566  // get the adress of field_plane_redshifts
567  std::vector<PosType> get_plane_redshifts () { return plane_redshifts; }
568 
569 private:
570  Lens & operator=(const Lens &lens); // block copy
571  Lens(const Lens &lens);
572 
573 protected:
575  PosType fieldofview;
576 
577 private:
578  GLAMER_TEST_FRIEND(LensTest)
579 
580 
581  void _find_images_(std::vector<RAY> &images
582  ,Point_2d &y
583  ,Point_2d ll
584  ,Point_2d ur
585  ,double resolution
586  ,double stop_res
587  );
588 
589  void _find_images_min_parallel_(RAY *rays
590  ,size_t begin
591  ,size_t end
592  ,double ytol2
593  ,std::vector<bool> &success
594  );
595 
596  // seed for random field generation
597  long *seed;
598 
599  long init_seed;
600  //InputParams init_params;
601 
602  // the cosmology
603  COSMOLOGY cosmo;
604 
605  template<typename P>
606  void compute_points_parallel(int start
607  ,int chunk_size
608  ,P *i_point
609  ,double *source_zs
610  ,bool multiZs
611  ,bool verbose = false);
612 
613  // this is a version that uses RAYs instead of Points or LinkedPoints and must be kept sinked with the above
614  void compute_rays_parallel(int start
615  ,int chunk_size
616  ,RAY *rays
617  ,bool verbose = false);
618 
619  //void readCosmology(InputParams& params);
620  //void assignParams(InputParams& params,bool verbose = false);
621  void defaultParams(PosType zsource,bool verbose = true);
622 
624  bool toggle_source_plane;
625 
626  // redhsift of true source plane if rest from original
627  PosType zs_implant;
628 
630  PosType zsource;
631 
632  void quicksort(LensHaloHndl *halo,PosType **pos,unsigned long N);
633 
634  // create the lens planes
635  //void buildPlanes(InputParams& params, bool verbose);
636 
638  void setFieldDist();
640  void setFieldDistFromFile();
642  void setupFieldPlanes();
645  void ComputeHalosDistributionVariables ();
646 
647  enum DM_Light_Division {All_DM,Moster};
648 
649  void createFieldHalos(bool verbose,DM_Light_Division division = Moster);
650 
652  void readInputSimFileMillennium(bool verbose,DM_Light_Division division = Moster);
653 
655  void readInputSimFileMultiDarkHalos(bool verbose,DM_Light_Division division = Moster);
656 
658  void readInputSimFileObservedGalaxies(bool verbose);
659 
661  void createFieldPlanes(bool verbose);
662 
663  // generate main halo from the parameter file
664  //void createMainHalos(InputParams& params);
666  void createMainPlanes();
668  void addMainHaloToPlane(LensHalo* halo);
669  void addMainHaloToNearestPlane(LensHalo* halo);
670 
672  void combinePlanes(bool verbose);
673 
674  /* Variables used by buildPlanes, createFieldHalos, and createFieldPlanes */
676 
678  const int Nzbins = 64 ;
680  const int Nmassbin=64;
682  int NZSamples = 50;
683  // table for redshift bins for mass function
684  std::vector<PosType> zbins ;
685  // table of number of halos per redshift bins for mass function
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;
697 
698  /* ----- */
699 
701  size_t getNFieldHalos() const {return field_halos.size();}
703  //size_t getNSubHalos() const {return substructure.halos.size();}
704 
705 private: /* force calculation */
707  bool flag_switch_deflection_off;
708  bool flag_switch_lensing_off;
709 
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;
721  PosType charge;
722 
723 private: /* field */
725  bool flag_switch_field_off;
726 
728  std::vector<LensHalo *> field_halos;
730  std::size_t field_Nplanes_original;
731  std::size_t field_Nplanes_current;
732 
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;
743 
744 // struct SubStructureInfo{
745 // // things for substructures
746 // /// vector of all substructure halos
747 // std::vector<LensHaloNFW *> halos;
748 // LensPlane *plane;
749 // PosType Rregion = 0;
750 // PosType Mmax = 0;
751 // PosType Mmin = 0;
752 // PosType alpha = 0;
753 // PosType Ndensity = 0;
754 // Point_2d center;
755 // PosType rho_tidal = 0;
756 // // Added quantities for the resetting of the substructure
757 // // (when WasInsertSubStructuresCalled = MAYBE) :
758 // PosType redshift = 0;
759 // bool verbose = false;
760 // };
761 //
762 // SubStructureInfo substructure;
763 
765  // Boo WasInsertSubStructuresCalled = NO ;
766 
768  //PosType **halo_pos;
769 
771  MassFuncType field_mass_func_type;
773  PosType mass_func_PL_slope;
775  PosType field_min_mass;
777  LensHaloType field_int_prof_type;
779  PosType field_prof_internal_slope;
780 
782  bool flag_field_gal_on;
784  GalaxyLensHaloType field_int_prof_gal_type;
786  PosType field_int_prof_gal_slope;
787 
788  // mass fraction in the host galaxy
789  //PosType field_galaxy_mass_fraction;
790 
791  std::string redshift_planes_file;
792  bool read_redshift_planes;
793 
794  std::string field_input_sim_file;
795  HaloCatFormats field_input_sim_format;
796 
797  bool sim_input_flag;
798  //std::string input_gal_file;
799  //bool gal_input_flag;
800  bool read_sim_file;
801 
803  PosType field_buffer;
804 
805 private: /* main */
807  bool flag_switch_main_halo_on;
808 
813  std::vector<LensPlane*> main_planes;
815  std::vector<PosType> main_plane_redshifts;
817  std::vector<PosType> main_Dl;
818 
820  LensHaloType main_halo_type;
822  GalaxyLensHaloType main_galaxy_halo_type;
823 
824 private: /* input */
826  std::string pixel_map_input_file;
827 
829  short pixel_map_on;
831  int pixel_map_zeropad;
832  bool pixel_map_zeromean;
833  void readPixelizedDensity();
834 
838  PosType sim_angular_radius;
840  PosType inv_ang_screening_scale;
841 
842  /* TO BE DEPRICATED
843  Add random halos to the light cone according to standard structure formation theory. A new realization of the light-cone can be made with Lens::resetFieldHalos() after this function is called once.
844 
845  The cone is filled up until the redshift of the current zsource that is stored in the Lens class. The field is a circular on the sky. There is no clustering of the halos.
846  */
847  void GenerateFieldHalos(double min_mass
848  ,MassFuncType mass_function
849  ,double field_of_view
850  ,int Nplanes
851  ,LensHaloType halo_type = LensHaloType::nfw_lens
852  ,GalaxyLensHaloType galaxy_type = GalaxyLensHaloType::null_gal
853  ,double buffer = 1.0
854  ,bool verbose = false
855  );
856 
857 
858 };
859 
860 inline std::size_t Lens::getNMainHalos() const
861 {
862  return main_halos.size();
863 }
864 
865 template<typename HaloType>
866 inline std::size_t Lens::getNMainHalos() const
867 {
868  return main_halos.size<HaloType>();
869 }
870 
871 inline LensHalo* Lens::getMainHalo(std::size_t i)
872 {
873  return main_halos.at(i);
874 }
875 
876 template<typename HaloType>
877 inline HaloType* Lens::getMainHalo(std::size_t i)
878 {
879  if(main_halos.size<HaloType>() == 0 ) return nullptr;
880  return main_halos.at<HaloType>(i);
881 }
882 
883 typedef Lens* LensHndl;
884 
885 // P should be Point or LinkedPoint
886 template <typename P>
887 void Lens::compute_points_parallel(int start
888  ,int chunk_size
889  ,P *i_points
890  ,double *source_zs
891  ,bool multiZs
892  ,bool verbose)
893 {
894  int end = start + chunk_size;
895 
896  int i, j;
897 
898  PosType xx[2];
899  PosType aa,bb;
900  PosType alpha[2];
901 
902  KappaType kappa,gamma[3];
903  KappaType phi;
904 
905  Matrix2x2<PosType> G;
906 
907  PosType SumPrevAlphas[2];
908  Matrix2x2<PosType> SumPrevAG;
909 
910  PosType *theta;
911 
912  long jmax = lensing_planes.size();
913  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
914  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
915 
916  if(!multiZs){
917  if(source_zs[0] == plane_redshifts.back() ){
918  Dls_Ds = dDl.back() / Dl.back();
919  D_Ds = Dl[Dl.size() - 2] / Dl.back();
920  }else{
921  PosType Dls,Ds;
922  FindSourcePlane(source_zs[0],jmax,Dls,Ds);
923  Dls_Ds = Dls / Ds;
924  if(jmax > 0) D_Ds = Dl[jmax-1] / Ds;
925  }
926  }
927 
928  // Main loop : loop over the points of the image
929  for(i = start; i < end; i++)
930  {
931  // In case e.g. a temporary point is outside of the grid.
932  if(i_points[i].in_image == MAYBE) continue;
933 
934  //theta = i_points[i].image->x;
935  theta = i_points[i].ptr_y();
936 
937  theta[0] = i_points[i].x[0];
938  theta[1] = i_points[i].x[1];
939 
940  // Initializing SumPrevAlphas :
941  SumPrevAlphas[0] = theta[0];
942  SumPrevAlphas[1] = theta[1];
943 
944  // Initializing SumPrevAG :
945  SumPrevAG.setToI();
946 
947  // Setting phi on the first plane.
948  phi = 0.0;
949 
950  // Default values :
951  i_points[i].A.setToI();
952  i_points[i].dt = 0;
953 
954  // In case we don't want to compute the values :
955  if(flag_switch_lensing_off)
956  {
957  i_points[i].image->A.setToI();
958  continue;
959  }
960 
961  // Time delay at first plane : position on the observer plane is (0,0) => no need to take difference of positions.
962  i_points[i].dt = 0;
963 
964  //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] ;
965 
966  if(multiZs){
967  PosType Dls,Ds;
968  FindSourcePlane(source_zs[i],jmax,Dls,Ds);
969  Dls_Ds = Dls / Ds;
970  if(jmax > 0) D_Ds = Dl[jmax-1]/Ds;
971  }
972 
973  // Begining of the loop through the planes :
974  // Each iteration leaves i_point[i].image on plane (j+1)
975 
976  for(j = 0; j < jmax ; ++j)
977  {
978 
979  double Dphysical = Dl[j]/(1 + plane_redshifts[j]);
980  // convert to physical coordinates on the plane j, just for force calculation
981  xx[0] = theta[0] * Dphysical;
982  xx[1] = theta[1] * Dphysical;
983  // PhysMpc = ComMpc / (1+z)
984 
985  assert(xx[0] == xx[0] && xx[1] == xx[1]);
986 
988 
989  lensing_planes[j]->force(alpha,&kappa,gamma,&phi,xx);
990  // Computed in physical coordinates, xx is in PhysMpc.
991 
993 
994  // if(alpha[0] != alpha[0] || alpha[1] != alpha[1]){
995  // std::cout << "xx[0] = " << xx[0] << " xx[1] = " << xx[1] << std::endl ;
996  // std::cout << "alpha = " << alpha[0] << " " << alpha[1] << std::endl ;
997  // throw std::runtime_error("alpha is NaN");
998  // }
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));
1004 
1005  G[0] = kappa + gamma[0]; G[1] = gamma[1];
1006  G[2] = gamma[1]; G[3] = kappa - gamma[0];
1007 
1008  /* multiply by fac to obtain 1/comoving_distance/physical_distance
1009  * such that a multiplication with the charge (in units of physical distance)
1010  * will result in a 1/comoving_distance quantity */
1011 
1012  G *= charge * Dl[j] / (1 + plane_redshifts[j]);
1013 
1014  assert(gamma[0] == gamma[0] && gamma[1] == gamma[1]);
1015  assert(kappa == kappa);
1016  //assert(phi == phi);
1017 
1018  // This computes \vec{x}^{j+1} in terms of \vec{x}^{j}
1019  // according to the corrected Eq. (18) of paper GLAMER II ---------------------------------
1020 
1021  // Adding the j-plane alpha contribution to the sum \Sum_{k=1}^{j} \vec{alpha_j} :
1022  SumPrevAlphas[0] -= charge * alpha[0] ;
1023  SumPrevAlphas[1] -= charge * alpha[1] ;
1024 
1025  if(j < jmax-1 ){
1026  aa = dDl[j+1] / Dl[j+1];
1027  bb = Dl[j] / Dl[j+1];
1028  }else{
1029  aa = Dls_Ds;
1030  bb = D_Ds;
1031  }
1032 
1033  if(!flag_switch_deflection_off){
1034  theta[0] = bb * theta[0] + aa * SumPrevAlphas[0];
1035  theta[1] = bb * theta[1] + aa * SumPrevAlphas[1];
1036  }
1037 
1038  // ----------------------------------------------------------------------------------------
1039 
1040  // Sum_{k=1}^{j} Dl[k] A^k.G^k
1041  SumPrevAG -= (G * (i_points[i].A)) ;
1042 
1043  // Computation of the "plus quantities", i.e. the next plane quantities :
1044  i_points[i].A = i_points[i].A * bb + SumPrevAG * aa;
1045 
1046  // ----------------------------------------------
1047 
1048  // Geometric time delay with added potential
1049  //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 ???
1050 
1051  // 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).
1052  // Remark : Here the true lensing potential is not "phi" but "phi * p->charge = phi * 4 pi G".
1053 
1054 
1055  } // End of the loop going through the planes
1056 
1057  if(flag_switch_deflection_off){
1058  i_points[i].A = Matrix2x2<KappaType>::I() - SumPrevAG;
1059  }
1060 
1061  // Subtracting off a term that makes the unperturbed ray to have zero time delay
1062  //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];
1063 
1064  // Conversion of dt from Mpc (physical Mpc) to Years -----------------
1065  i_points[i].dt *= MpcToSeconds * SecondToYears ;
1066 
1067  // ---------------------------------------------------------------------------------------------
1068 
1069  // Putting the final values of the quantities in the image point -----
1070  i_points[i].image->A = i_points[i].A;
1071  i_points[i].image->dt = i_points[i].dt;
1072  // ------------------------------------------------------------------------
1073 
1074 
1075  // TEST : showing final quantities
1076  // ------------------------------=
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 ;
1078 
1079  } // End of the main loop.
1080 
1081 }
1082 
1083 #endif /* MULTIPLANE_H_ */
1084 
Utilities::MixedVector::at
BaseT & at(std::size_t i)
indexed access with bounds checking
Definition: utilities_slsim.h:523
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:203
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:227
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:455
Lens::getCosmo
const COSMOLOGY & getCosmo()
returns a const reference to the cosmology so that constant functions can be used,...
Definition: lens.h:462
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:541
LensHalo
A base class for all types of lensing "halos" which are any mass distribution that cause lensing.
Definition: lens_halos.h:57
MassFuncType
MassFuncType
Type of mass function.
Definition: InputParams.h:36
Lens::getNMainHalos
std::size_t getNMainHalos() const
get number of main halos
Definition: lens.h:860
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:575
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:177
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:156
Utilities::MixedVector< LensHalo * >
Utilities::MixedVector::size
std::size_t size() const
number of all elements
Definition: utilities_slsim.h:558
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:469
Lens::TurnFieldOff
void TurnFieldOff()
set flag_switch_field_off, turn the field On/Off :
Definition: lens.h:465
RAY
Simple representaion of a light path giving position on the image and source planes and lensing quant...
Definition: point.h:555
Lens::getMainHalo
LensHalo * getMainHalo(std::size_t i)
get single main halo
Definition: lens.h:871
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:458
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:442
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:133
Point
A point on the source or image plane that contains a position and the lensing quantities.
Definition: point.h:459
Utilities::delete_container
void delete_container(Container &c)
delete the objects that are pointed to in a container of pointers
Definition: utilities_slsim.h:1002
Lens::operator=
Lens & operator=(Lens &&lens)
Definition: lens.h:475
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