GLAMERDOC++
Gravitational Lensing Code Library
particle_halo2.h
Go to the documentation of this file.
1 
31 #ifndef GLAMER_particle_halo2_h
32 #define GLAMER_particle_halo2_h
33 #include <thread>
34 #include <mutex>
35 
36 #include "geometry.h"
37 #include "quadTree.h"
38 #include "simpleTree.h"
39 #include "particle_types.h"
40 #include "utilities_slsim.h"
41 #include "lens_halos.h"
42 #include "oTreeNB.h"
43 #include "gadget.hh"
44 
66 template<typename DType = float >
68 {
69  public:
71  const std::string& simulation_filename
72  ,SimFileFormat format
73  ,PosType redshift
74  ,double my_inv_area_phys
75  ,PosType mass_particle
76  ,const COSMOLOGY& cosmo
77  ,int my_Nsmooth = 5
78  ,float Nbucket = 8
79  ,float theta = 0.1
80  ,bool recenter = false
81  ,bool verbose = false
82  ) : LensHalo(redshift,cosmo),particle_mass(mass_particle)
83  ,inv_area(my_inv_area_phys),Nsmooth(my_Nsmooth),theta2(theta*theta)
85  {
86 
87  switch (format) {
89  readPositionFileASCII(simulation_filename);
90  break;
91  default:
92  std::cerr << "LensHaloParticles does not accept format of particle data file." << std::endl;
93  throw std::invalid_argument("bad format");
94  }
95  if(verbose) std::cout << " Read " << pp.size() << " particles from " << simulation_filename << std::endl;
96 
97  // convert from comoving to physical coordinates
98  double scale_factor = 1.0/(1 + redshift);
99  for(auto &p : pp){
100  p[0] *= scale_factor;
101  p[1] *= scale_factor;
102  p[2] *= scale_factor;
103  }
104  setUp(recenter,verbose);
105  }
106 
109  std::vector<Point_3d<DType> > &pvector
110  ,PosType redshift
111  ,double my_inv_area_phys
112  ,PosType mass_particle
113  ,const COSMOLOGY& cosmo
114  ,int my_Nsmooth = 5
115  ,float Nbucket = 8
116  ,float theta = 0.1
117  ,bool recenter = false
118  ,bool verbose = false
119  ) : LensHalo(redshift,cosmo),particle_mass(mass_particle)
120  ,inv_area(my_inv_area_phys),Nsmooth(my_Nsmooth),theta2(theta*theta)
121  ,Nbucket(Nbucket)
122  {
123  std::swap(pp, pvector); // take ownership of the data
124  setUp(recenter,verbose);
125  }
126 
127  ~LensHaloParticles(){};
128 
130  void force_halo(double *alpha
131  ,KappaType *kappa
132  ,KappaType *gamma
133  ,KappaType *phi
134  ,double const *xcm
135  ,bool subtract_point=false,PosType screening = 1 // here so that it overrides the LensHalo::force_halo
136  ){
137 
138  otree->force2D(xcm,particle_mass,Nsmooth,theta2,inv_area
139  ,alpha,kappa,gamma,phi);
140  };
141 
142  size_t getN() const { return pp.size(); };
143 
145  Point_3d<DType> CenterOfMass(){return mcenter;}
146 
147  LensHaloParticles(LensHaloParticles &&h):LensHalo(std::move(h)){
148  mcenter = h.mcenter;
149  pp = std::move(h.pp);
150  inv_area = h.inv_area;
151  Nsmooth = h.Nsmooth;
152  theta2 = h.theta2;
153  particle_mass = h.particle_mass;
154  Nbucket = h.Nbucket;
155  otree = std::move(h.otree);
156  }
157  LensHaloParticles & operator=(LensHaloParticles &&h){
158  if(this == &h) return *this;
159  LensHalo::operator=(std::move(h));
160  mcenter = h.mcenter;
161  pp = std::move(h.pp);
162  inv_area = h.inv_area;
163  Nsmooth = h.Nsmooth;
164  theta2 = h.theta2;
165  particle_mass = h.particle_mass;
166  Nbucket = h.Nbucket;
167  otree = std::move(h.otree);
168  return *this;
169  }
170 
173  void rotate(
174  PosType theta_x
175  ,PosType theta_y
176  ,Point_3d<DType> &xo);
177 
178  // reset the opening angle used in the tree force calculation
179  void resetForceAngle(double theta){
180  theta2 = theta*theta;
181  }
182 
189 
205  static std::vector< LensHaloParticles<DType> > make(
206  std::string filename
207  ,SimFileFormat format
208  ,float umass
209  ,float ulength
210  ,double inv_area
211  ,const COSMOLOGY& cosmo
212  ,int Nsmooth = 5
213  ,float Nbucket = 8
214  ,float theta = 0.1
215  ,DType rmax = 0
216  ,Point_3d<DType> xo = Point_3d<DType>(0,0,0)
217  ,bool verbose = false
218  ){
219  double r2max= rmax*rmax;
220 
221  switch (format) {
222  case SimFileFormat::gadget2 :
223  {
224 
225  std::vector<Point_3d<DType> > tmpdata;
226  GadgetFile<Point_3d<DType> > gadget_file(filename,tmpdata);
227  gadget_file.openFile();
228  gadget_file.readPOS();
229  gadget_file.closeFile();
230 
231  if(rmax <= 0){
232  r2max = 0; // no excision
233  xo[0] = gadget_file.boxl/2; // center of box
234  xo[1] = gadget_file.boxl/2;
235  xo[2] = gadget_file.boxl/2;
236  }
237  // convert to physical Mpc
238  const double a = ulength*1.0e-6/(1 + gadget_file.redshift)/gadget_file.hubble;
239  // split particles into different types
240  std::vector< std::vector<Point_3d<DType> > > pos(6);
241  size_t n=0;
242  int ntypes = 0;
243  Point_3d<DType> dx;
244  for(int i=0; i<6 ; ++i){
245  pos[i].resize(gadget_file.npart[i]);
246  if(gadget_file.npart[i] > 0) ++ntypes;
247  if(r2max > 0){
248  size_t k = 0;
249  for(size_t j=0; j<gadget_file.npart[i]; ++j){
250  pos[i][k] = tmpdata[n++];
251  dx = pos[i][k] - xo;
252  if(dx.length_sqr() < r2max){
253  pos[i][k] -= xo;
254  pos[i][k] *= a; // convert to physical Mpc
255  ++k;
256  }
257  }
258  pos[i].resize(k); // resize to number of particles in sphere
259  gadget_file.npart[i] = k; // update number of particles
260  }else{
261  for(size_t j=0; j<gadget_file.npart[i]; ++j){
262  pos[i][j] = (tmpdata[n++]-xo)*a;
263  }
264  }
265  }
266  tmpdata.clear();
267 
268  std::vector< LensHaloParticles<DType> > halos;
269  ntypes = 0;
270  for(int i=0; i<6 ; ++i){
271  if(gadget_file.npart[i] > 0){
272  halos.push_back( LensHaloParticles<DType>(
273  pos[i]
274  ,gadget_file.redshift
275  ,inv_area
276  ,gadget_file.masstab[i]*umass/gadget_file.hubble
277  ,cosmo
278  ,Nsmooth
279  ,Nbucket
280  ,theta
281  ,false
282  ,verbose)
283  );
284  ++ntypes;
285  }
286  }
287  return halos;
288  }
289  break;
290  default:
291  std::cerr << "LensHaloParticles does not accept format of particle data file." << std::endl;
292  throw std::invalid_argument("bad format");
293  }
294  }
295 
297  Point_3d<DType> &p1,
298  Point_3d<DType> &p2
299  ) const {
300  otree->getBoundingBox(p1,p2);
301  }
302 
304  void swapout(std::vector<Point_3d<DType> > &pvector){
305  std::swap(pp, pvector);
306  otree.reset();
307  }
308 protected :
309  Point_3d<DType> mcenter;
310  //DType *pp;
311  std::vector<Point_3d<DType> > pp;
312  int Nsmooth;
313  int Nbucket;
314  double theta2;
315  double particle_mass;
316 
317  PosType inv_area;
318  std::unique_ptr<OTreeNB<Point_3d<DType>, DType > > otree;
319 
320  void readPositionFileASCII(const std::string &filename);
321  // construct tree, particles positions must already by stored in comoving Mpc
322  void setUp(
323  bool recenter
324  ,bool verbose
325  );
326 
327  // protected constructor for creating an empty one
329  PosType redshift
330  ,double my_inv_area_phys
331  ,PosType mass_particle
332  ,const COSMOLOGY& cosmo
333  ,int my_Nsmooth = 5
334  ,float Nbucket = 8
335  ,float theta = 0.1
336  ,bool recenter = false
337  ,bool verbose = false
338  ) : LensHalo(redshift,cosmo),particle_mass(mass_particle)
339  ,inv_area(my_inv_area_phys),Nsmooth(my_Nsmooth),theta2(theta*theta)
340  ,Nbucket(Nbucket)
341  {
342  }
343 };
344 
345 // construct tree, particles positions must already by stored in comoving Mpc
346 template<typename DType>
348  bool recenter
349  ,bool verbose
350  ){
351  long Npoints = pp.size();
352 
353  Point_3d<double> mcenter(0,0,0);
354 
355  for(auto &p : pp){
356  mcenter[0] += p[0];
357  mcenter[1] += p[1];
358  mcenter[2] += p[2];
359  }
360 
361  mcenter /= pp.size();
362 
363  if(verbose) std::cout << " Center of mass : " << mcenter << std::endl;
364 
365  if(recenter){
366  PosType r2,r2max=0;
367  for(auto &p : pp){
368  p[0] -= mcenter[0];
369  p[1] -= mcenter[1];
370  p[2] -= mcenter[2];
371  }
372  mcenter *= 0;
373  }
374 
375  LensHalo::setTheta(0,0);
376  LensHalo::mass = particle_mass * pp.size();
377  LensHalo::set_flag_elliptical(false); // shouldn't do anything
378 
379  if(verbose) std::cout << " Creating particle tree ... " << std::endl;
380  otree.reset( new OTreeNB<Point_3d<DType>,DType >(pp.data(),pp.size()) );
381  otree->build(Nbucket);
382  if(verbose) std::cout << "done." << std::endl;
383 };
384 
385 template<typename DType>
386 void LensHaloParticles<DType>::readPositionFileASCII(const std::string &filename
387  ){
388 
389  int ncoll = Utilities::IO::CountColumns(filename);
390  if(ncoll < 3 ){
391  std::cerr << filename << " should have three columns!" << std::endl;
392  }
393  if(ncoll > 4 ){
394  std::cerr << filename << " Warning! : LensHaloParticles is not using masses." << std::endl;
395  }
396 
397  std::ifstream myfile(filename);
398  long Npoints = 0;
399  // find number of particles
400  if (myfile.is_open()){
401 
402  float tmp_mass = 0.0;
403  std::string str,label;
404  int count =0;
405  while(std::getline(myfile, str)){
406  std::stringstream ss(str);
407  ss >> label;
408  if(label == "#"){
409  ss >> label;
410  if(label == "nparticles"){
411  ss >> Npoints;
412  ++count;
413  }
414  {
415  if(label == "mass"){
416  ss >> tmp_mass;
417  ++count;
418  }
419  }
420  }else break;
421  if(count == 2 ) break;
422  }
423 
424  if(count == 0){
425  std::cerr << "File " << filename << " must have the header lines: " << std::endl
426  << "# nparticles ****" << std::endl;
427  throw std::runtime_error("file reading error");
428  }
429 
430  assert(Npoints > 0);
431  pp.resize(Npoints);
432  size_t row = 0;
433 
434  // read in particle positions
435  do{
436  if(str[0] == '#') continue; //for comments
437  std::stringstream ss(str);
438 
439  ss >> pp[row][0];
440  if(!(ss >> pp[row][1])) std::cerr << "3 columns are expected in line " << row
441  << " of " << filename << std::endl;
442  if(!(ss >> pp[row][2])) std::cerr << "3 columns are expected in line " << row
443  << " of " << filename << std::endl;
444 
445  row++;
446  }while(std::getline(myfile, str) && row < Npoints);
447 
448  if(row != Npoints){
449  std::cerr << "Number of data rows in " << filename << " does not match expected number of particles."
450  << std::endl;
451  throw std::runtime_error("file reading error");
452  }
453  }else{
454  std::cerr << "Unable to open file " << filename << std::endl;
455  throw std::runtime_error("file reading error");
456  }
457 
458  std::cout << Npoints << " particle positions read from file " << filename << std::endl;
459 };
460 
461 template<typename DType>
462 void LensHaloParticles<DType>::rotate(PosType theta_x,PosType theta_y
463  ,Point_3d<DType> &xo){
464 
465  if(theta_x == 0.0 && theta_y == 0.0) return;
466 
467  PosType coord[3][3];
468  PosType cx,cy,sx,sy;
469 
470  cx = cos(theta_x); sx = sin(theta_x);
471  cy = cos(theta_y); sy = sin(theta_y);
472 
473  coord[0][0] = cy; coord[1][0] = -sy*sx; coord[2][0] = cx;
474  coord[0][1] = 0; coord[1][1] = cx; coord[2][1] = sx;
475  coord[0][2] = -sy; coord[1][2] = -cy*sx; coord[2][2] = cy*cx;
476 
477  Point_3d<DType> tmp;
478  // rotate particle positions
479  for(auto &p : pp){
480  tmp *= 0;
481  for(int j=0;j<3;++j){
482  double tmp2 = p[j] - xo[j];
483  tmp[0] += coord[0][j]*tmp2;
484  tmp[1] += coord[1][j]*tmp2;
485  tmp[2] += coord[2][j]*tmp2;
486  }
487  for(int j=0;j<3;++j) p[j]=tmp[j] + xo[j];
488  }
489 
490  // rotate center of mass
491  tmp *= 0.0;
492  for(int j=0;j<3;++j){
493  tmp[0] += coord[0][j]*(mcenter[j] - xo[j]);
494  tmp[1] += coord[1][j]*(mcenter[j] - xo[j]);
495  tmp[2] += coord[2][j]*(mcenter[j] - xo[j]);
496  }
497  mcenter = tmp + xo;
498 
499  otree.reset( new OTreeNB<Point_3d<DType> >(pp.data(),pp.size()) );
500  otree->build(Nbucket);
501 };
502 
503 template<typename DType>
505 
506  if(xo[0] == 0.0 && xo[1] == 0.0) return;
507 
508  // translate particle positions
509  for(auto &p : pp){
510  for(int j=0;j<3;++j) p[j] += xo[j];
511  }
512 
513  mcenter += xo;
514 
516  otree.reset( new OTreeNB<Point_3d<DType> >(pp.data(),pp.size()) );
517  otree->build(Nbucket);
518 };
519 
523 template<typename DType = float >
525 {
526 public:
527 
529  std::vector<Point_3d<DType> > &pvector
530  ,PosType redshift
531  ,double my_inv_area_phys
532  ,std::vector<DType> Masses
533  ,const COSMOLOGY& cosmo
534  ,int my_Nsmooth = 5
535  ,float Nbucket = 8
536  ,float theta = 0.1
537  ,bool recenter = false
538  ,bool verbose = false
539  ) : LensHaloParticles<DType>(pvector,redshift,my_inv_area_phys,1,cosmo,my_Nsmooth
540  ,Nbucket,theta,recenter,verbose)
541  {
542  std::swap(masses,Masses);
543  this->otree->calcMoments(masses.data());
544  };
545 
547  : LensHaloParticles<DType>(std::move(h)),
548  masses(std::move(h.masses))
549  {
550  }
551 
552  LensHaloParticlesM & operator=(LensHaloParticlesM &&h) {
553  if (this == &h) return *this;
555  masses = std::move(h.masses);
556  return *this;
557  }
559  void force_halo(double *alpha
560  ,KappaType *kappa
561  ,KappaType *gamma
562  ,KappaType *phi
563  ,double const *xcm
564  ,bool subtract_point=false
565  ,PosType screening = 1 // here so that it overrides the LensHalo::force_halo
566  ){
567 
568  this->otree->force2D(xcm,masses.data(),this->Nsmooth,this->theta2,this->inv_area
569  ,alpha,kappa,gamma,phi);
570  };
572  void swapoutMasses(std::vector<DType> &Mvector){
573  std::swap(masses, Mvector);
574  this->otree->calcMoments(masses.data());
575  }
576  private:
577  std::vector<DType> masses;
578 };
579 
580 
584 template<typename DType = float >
586 {
587 public:
589  const std::string& simulation_filename
590  ,SimFileFormat format
591  ,PosType redshift
592  ,double my_inv_area_phys
593  ,PosType mass_particle
594  ,Point_2d x_hole
595  ,DType hole_radius
596  ,const COSMOLOGY& cosmo
597  ,int my_Nsmooth = 5
598  ,float Nbucket = 8
599  ,float theta = 0.1
600  ,bool recenter = false
601  ,bool verbose = false
602  ) : LensHaloParticles<DType>(simulation_filename,format,redshift,my_inv_area_phys,mass_particle,cosmo,my_Nsmooth,Nbucket,theta,recenter,verbose)
603  ,xc(x_hole),rhole(hole_radius)
604  {};
605 
607  std::vector<Point_3d<DType> > &pvector
608  ,PosType redshift
609  ,double my_inv_area_phys
610  ,PosType mass_particle
611  ,Point_2d x_hole
612  ,DType hole_radius
613  ,const COSMOLOGY& cosmo
614  ,int my_Nsmooth = 5
615  ,float Nbucket = 8
616  ,float theta = 0.1
617  ,bool recenter = false
618  ,bool verbose = false
619  ) : LensHaloParticles<DType>(pvector,redshift,my_inv_area_phys,mass_particle,cosmo,my_Nsmooth
620  ,Nbucket,theta,recenter,verbose),xc(x_hole),rhole(hole_radius)
621  {
622  };
623 
625  : LensHaloParticles<DType>(std::move(h)),
626  xc(std::move(h.xc)),
627  rhole(h.rhole)
628  {}
629 
630  LensHaloParticlesO & operator=(LensHaloParticlesO &&h) {
631  if (this == &h) return *this;
633  xc = std::move(h.xc);
634  rhole = h.rhole;
635  return *this;
636  }
638  void force_halo(double *alpha
639  ,KappaType *kappa
640  ,KappaType *gamma
641  ,KappaType *phi
642  ,double const *xcm
643  ,bool subtract_point=false
644  ,PosType screening = 1 // here so that it overrides the LensHalo::force_halo
645  ){
646 
647  this->otree->force2D_hole(xcm,this->particle_mass,this->Nsmooth,this->theta2,this->inv_area
648  ,rhole,xc.data()
649  ,alpha,kappa,gamma,phi);
650  };
651 
652  void setHole(
653  Point_2d &x_hole,DType hole_radius
654  ){
655  xc = x_hole;
656  rhole = hole_radius;
657  };
658 
659  private:
660  Point_2d xc;
661  PosType rhole;
662 };
663 
670 template<typename DType = float >
672 {
673 public:
675  const std::string& simulation_filename
676  ,SimFileFormat format
677  ,PosType redshift
678  ,double my_inv_area_phys
679  ,PosType mass_particle
680  ,Point_2d lower_left
681  ,Point_2d upper_right
682  ,const COSMOLOGY& cosmo
683  ,int my_Nsmooth = 5
684  ,float Nbucket = 8
685  ,float theta = 0.1
686  ,bool recenter = false
687  ,bool verbose = false
688  ) : LensHaloParticles<DType>(simulation_filename,format,redshift,my_inv_area_phys,mass_particle,cosmo,my_Nsmooth,Nbucket,theta,recenter,verbose)
689  ,length(upper_right-lower_left)
690  {// check that particles are within bounding box
691  Point_3d<float> p1,p2;
692  this->otree->getBoundingBox(p1,p2);
693  double Dl = cosmo.angDist(redshift);
694  length *= Dl;
695  p1 /= Dl;
696  p2 /= Dl;
697  if( lower_left[0] > p1[0]
698  || lower_left[1] > p1[1]
699  || upper_right[0] < p2[0]
700  || upper_right[1] < p2[1]
701  ){
702 
703  std::cerr << "Error : LensParticlesP bounding box does not contain all the particles"
704  << std::endl;
705  std::cerr << " angular field " << lower_left << " " << upper_right << std::endl;
706  std::cerr << " bounding box " << p1 << " " << p2 << std::endl;
707  throw std::runtime_error("Bad bounding box.");
708  }
709  };
710 
712  std::vector<Point_3d<DType> > &pvector
713  ,PosType redshift
714  ,double my_inv_area_phys
715  ,PosType mass_particle
716  ,Point_2d lower_left
717  ,Point_2d upper_right
718  ,const COSMOLOGY& cosmo
719  ,int my_Nsmooth = 5
720  ,float Nbucket = 8
721  ,float theta = 0.1
722  ,bool recenter = false
723  ,bool verbose = false
724  ) : LensHaloParticles<DType>(pvector,redshift,my_inv_area_phys,mass_particle,cosmo,my_Nsmooth
725  ,Nbucket,theta,recenter,verbose),length(upper_right-lower_left)
726  {
727  // check that particles are within bounding box
728  Point_3d<float> p1,p2;
729  this->otree->getBoundingBox(p1,p2);
730  double Dl = cosmo.angDist(redshift);
731  length *= Dl;
732  p1 /= Dl;
733  p2 /= Dl;
734  if( lower_left[0] > p1[0]
735  || lower_left[1] > p1[1]
736  || upper_right[0] < p2[0]
737  || upper_right[1] < p2[1]
738  ){
739 
740  std::cerr << "Error : LensParticlesP bounding box does not contain all the particles"
741  << std::endl;
742  std::cerr << " angular field " << lower_left << " " << upper_right << std::endl;
743  std::cerr << " bounding box " << p1 << " " << p2 << std::endl;
744  throw std::runtime_error("Bad bounding box.");
745  }
746  };
747 
749  : LensHaloParticles<DType>(std::move(h)),
750  length(h.length)
751  {}
752 
753  LensHaloParticlesP & operator=(LensHaloParticlesP &&h) {
754  if (this == &h) return *this;
756  length = h.length;
757  return *this;
758  }
760  void force_halo(double *alpha
761  ,KappaType *kappa
762  ,KappaType *gamma
763  ,KappaType *phi
764  ,double const *xcm
765  ,bool subtract_point=false
766  ,PosType screening = 1 // here so that it overrides the LensHalo::force_halo
767  ){
768 
769  Point_2d xx;
770  for(int i = -1 ; i <= 1 ; ++i ){
771  xx[0] = xcm[0] + i*length[0];
772  for(int j = -1 ; j <= 1 ; ++j ){
773  xx[1] = xcm[1] + j*length[1];
774  this->otree->force2D(xx.x,this->particle_mass,this->Nsmooth,this->theta2,this->inv_area
775  ,alpha,kappa,gamma,phi);
776  }
777  }
778  };
779 
780  private:
781  Point_2d length;
782 };
783 
787 template<typename DType = float >
789 {
790 public:
791 
793  std::vector<Point_3d<DType> > &pvector
794  ,PosType redshift
795  ,double my_inv_area_phys
796  ,std::vector<DType> Masses
797  ,Point_2d x_hole
798  ,DType hole_radius
799  ,const COSMOLOGY& cosmo
800  ,int my_Nsmooth = 5
801  ,float Nbucket = 8
802  ,float theta = 0.1
803  ,bool recenter = false
804  ,bool verbose = false
805  ) : LensHaloParticles<DType>(pvector,redshift,my_inv_area_phys,1,cosmo,my_Nsmooth
806  ,Nbucket,theta,recenter,verbose),xc(x_hole),rhole(hole_radius)
807  {
808  std::swap(masses,Masses);
809  this->otree->calcMoments(masses.data());
810  };
811 
813  : LensHaloParticles<DType>(std::move(h)),
814  xc(std::move(h.xc)),
815  rhole(h.rhole),
816  masses(std::move(h.masses))
817  {
818 
819  }
820 
821  LensHaloParticlesMO & operator=(LensHaloParticlesMO &&h) {
822  if (this == &h) return *this;
824  xc = std::move(h.xc);
825  rhole = h.rhole;
826  masses = std::move(h.masses);
827  return *this;
828  }
830  void force_halo(double *alpha
831  ,KappaType *kappa
832  ,KappaType *gamma
833  ,KappaType *phi
834  ,double const *xcm
835  ,bool subtract_point=false
836  ,PosType screening = 1 // here so that it overrides the LensHalo::force_halo
837  ){
838 
839  this->otree->force2D_hole(xcm,masses.data(),this->Nsmooth,this->theta2,this->inv_area
840  ,rhole,xc.data()
841  ,alpha,kappa,gamma,phi);
842  };
843 
844  void setHole(
845  Point_2d &x_hole,DType hole_radius
846  ){
847  xc[0] = x_hole[0];
848  xc[1] = x_hole[1];
849  rhole = hole_radius;
850  };
851 
852  private:
853  Point_2d xc;
854  PosType rhole;
855  std::vector<DType> masses;
856 };
857 
863 template<typename HType >
864 class LensHaloH : public LensHaloParticles<float>
865 {
866 public:
867 
869  std::vector<HType> &halo_vector
870  ,PosType redshift
871  ,double my_inv_area_phys
872  ,float hole_radius_angle
873  ,const COSMOLOGY& cosmo
874  ,float Nbucket = 4
875  ,float theta = 0.1
876  ,bool recenter = false
877  ,bool verbose = false
878  ) : LensHaloParticles<float>(redshift,my_inv_area_phys,1,cosmo,0
879  ,Nbucket,theta,recenter,verbose),rhole(hole_radius_angle*cosmo.angDist(redshift))
880  {
881  std::swap(halo_vector,halos);
882  size_t N = halos.size();
883  this->pp.resize(N);
884  double Dl = cosmo.angDist(redshift);
885  Point_2d p;
886  for(size_t i=0 ; i<N ; ++i){
887  p = halos[i].getTheta();
888  this->pp[i][0] = p[0]*Dl;
889  this->pp[i][1] = p[1]*Dl;
890  this->pp[i][2] = 0;
891  halos[i].setRsize(1.0e3);
892  }
893 
894  this->setUp(recenter,verbose);
895  this->otree->calcMoments(halos);
896  };
897 
898  LensHaloH(LensHaloH &&h)
899  : LensHaloParticles<float>(std::move(h)),
900  rhole(h.rhole),
901  halos(std::move(h.halos))
902  {
903  }
904 
905  LensHaloH & operator=(LensHaloH &&h) {
906  if (this == &h) return *this;
908  rhole = h.rhole;
909  halos = std::move(h.halos);
910  return *this;
911  }
913  void force_halo(double *alpha
914  ,KappaType *kappa
915  ,KappaType *gamma
916  ,KappaType *phi
917  ,double const *xcm
918  ,bool subtract_point=false
919  ,PosType screening = 1 // here so that it overrides the LensHalo::force_halo
920  ){
921 
922  Point_2d xx;
923  this->otree->force2D_halo(xcm,halos.data()
924  ,this->theta2,this->inv_area
925  ,rhole,xcm
926  ,alpha,kappa,gamma,phi);
927  };
928 
930  void swapoutHalos(std::vector<HType> &hvector){
931  std::swap(halos, hvector);
932  this->otree.reset();
933  }
934  private:
935  PosType rhole;
936  std::vector<HType> halos;
937 };
938 
947 template<typename HType >
948 class LensHaloHP : public LensHaloParticles<float>
949 {
950 public:
951 
953  std::vector<HType> &halo_vector
954  ,PosType redshift
955  ,Point_2d lower_left
956  ,Point_2d upper_right
957  ,float hole_radius_angle
958  ,const COSMOLOGY& cosmo
959  ,int my_Nsmooth = 0
960  ,float Nbucket = 4
961  ,float theta = 0.1
962  ,bool recenter = false
963  ,bool verbose = false
964  ) : LensHaloParticles<float>(redshift
965  ,0,1,cosmo,my_Nsmooth
966  ,Nbucket,theta,recenter,verbose),rhole(hole_radius_angle*cosmo.angDist(redshift))
967  ,length(upper_right-lower_left)
968  {
969  std::swap(halo_vector,halos);
970  size_t N = halos.size();
971  this->pp.resize(N);
972  double Dl = cosmo.angDist(redshift);
973 
974  Point_2d p;
975  for(size_t i=0 ; i<N ; ++i){
976  p = halos[i].getTheta();
977  this->pp[i][0] = p[0]*Dl;
978  this->pp[i][1] = p[1]*Dl;
979  this->pp[i][2] = 0;
980  halos[i].setRsize(1.0e3);
981  }
982  length *= Dl;
983  LensHaloParticles<float>::inv_area = 1.0/length[0]/length[1];
984  this->setUp(recenter,verbose);
985  this->otree->calcMoments(halos);
986 
987  // check that particles are within bounding box
988  Point_3d<float> p1,p2;
989  this->otree->getBoundingBox(p1,p2);
990  p1 /= Dl;
991  p2 /= Dl;
992  if( lower_left[0] > p1[0]
993  || lower_left[1] > p1[1]
994  || upper_right[0] < p2[0]
995  || upper_right[1] < p2[1]
996  ){
997 
998  std::cerr << "Error : LensHaloHP bounding box does not contain all the halos"
999  << std::endl;
1000  std::cerr << " angular field " << lower_left << " " << upper_right << std::endl;
1001  std::cerr << " bounding box " << p1 << " " << p2 << std::endl;
1002  throw std::runtime_error("Bad bounding box.");
1003  }
1004  };
1005 
1006  LensHaloHP(LensHaloHP &&h)
1007  : LensHaloParticles<float>(std::move(h)),
1008  rhole(h.rhole),
1009  halos(std::move(h.halos)),
1010  length(h.length)
1011  {
1012  }
1013 
1014  LensHaloHP & operator=(LensHaloHP &&h) {
1015  if (this == &h) return *this;
1017  rhole = h.rhole;
1018  halos = std::move(h.halos);
1019  length = h.length;
1020  return *this;
1021  }
1023  void force_halo(double *alpha
1024  ,KappaType *kappa
1025  ,KappaType *gamma
1026  ,KappaType *phi
1027  ,double const *xcm
1028  ,bool subtract_point=false
1029  ,PosType screening = 1 // here so that it overrides the LensHalo::force_halo
1030  ){
1031 
1032  Point_2d xx,zero(0,0);
1033  for(int i = -1 ; i <= 1 ; ++i ){
1034  xx[0] = xcm[0] + i*length[0];
1035  for(int j = -1 ; j <= 1 ; ++j ){
1036  xx[1] = xcm[1] + j*length[1];;
1037  //this->otree->force2D_halo(xx.x,halos.data(),this->Nsmooth,this->theta2,this->inv_area
1038  // ,rhole,alpha,kappa,gamma,phi);
1039  this->otree->force2D_halo(xx.x,halos.data(),this->theta2,this->inv_area
1040  ,rhole,xx.x
1041  ,alpha,kappa,gamma,phi);
1042  }
1043  }
1044  };
1045 
1046  private:
1047  PosType rhole;
1048  std::vector<HType> halos;
1049  Point_2d length;
1050 };
1051 
1052 #endif
LensHalo::operator=
LensHalo & operator=(const LensHalo &h)
Definition: lens_halos.cpp:169
LensHaloParticles::rotate
void rotate(PosType theta_x, PosType theta_y, Point_3d< DType > &xo)
Definition: particle_halo2.h:462
SimFileFormat::gadget2
@ gadget2
Gadget 2 output file format.
LensHaloHP
LensHalo for collecting LensHalos into one with pseudo-periodic boundary conditions.
Definition: particle_halo2.h:949
LensHaloParticlesMO::LensHaloParticlesMO
LensHaloParticlesMO(std::vector< Point_3d< DType > > &pvector, PosType redshift, double my_inv_area_phys, std::vector< DType > Masses, Point_2d x_hole, DType hole_radius, const COSMOLOGY &cosmo, int my_Nsmooth=5, float Nbucket=8, float theta=0.1, bool recenter=false, bool verbose=false)
Definition: particle_halo2.h:792
LensHaloParticles::Nbucket
int Nbucket
number of buckets in tree
Definition: particle_halo2.h:313
LensHaloParticles::LensHaloParticles
LensHaloParticles(const std::string &simulation_filename, SimFileFormat format, PosType redshift, double my_inv_area_phys, PosType mass_particle, const COSMOLOGY &cosmo, int my_Nsmooth=5, float Nbucket=8, float theta=0.1, bool recenter=false, bool verbose=false)
Definition: particle_halo2.h:70
SimFileFormat::ascii
@ ascii
the original ascii GLAMER format.
LensHaloParticlesMO::force_halo
void force_halo(double *alpha, KappaType *kappa, KappaType *gamma, KappaType *phi, double const *xcm, bool subtract_point=false, PosType screening=1)
does not zero lens quantities
Definition: particle_halo2.h:830
LensHaloParticles::LensHaloParticles
LensHaloParticles(PosType redshift, double my_inv_area_phys, PosType mass_particle, const COSMOLOGY &cosmo, int my_Nsmooth=5, float Nbucket=8, float theta=0.1, bool recenter=false, bool verbose=false)
Definition: particle_halo2.h:328
LensHaloParticlesO
LensHaloParticles only with a hole cut out of it.
Definition: particle_halo2.h:586
LensHaloH::LensHaloH
LensHaloH(std::vector< HType > &halo_vector, PosType redshift, double my_inv_area_phys, float hole_radius_angle, const COSMOLOGY &cosmo, float Nbucket=4, float theta=0.1, bool recenter=false, bool verbose=false)
Definition: particle_halo2.h:868
LensHaloParticlesO::force_halo
void force_halo(double *alpha, KappaType *kappa, KappaType *gamma, KappaType *phi, double const *xcm, bool subtract_point=false, PosType screening=1)
does not zero lens quantities
Definition: particle_halo2.h:638
LensHalo
A base class for all types of lensing "halos" which are any mass distribution that cause lensing.
Definition: lens_halos.h:57
LensHaloParticles::swapout
void swapout(std::vector< Point_3d< DType > > &pvector)
swap out the particle positions, leaving the LensHaloParticles empty
Definition: particle_halo2.h:304
Point_2d
Class for representing points or vectors in 2 dimensions. Not that the dereferencing operator is over...
Definition: point.h:48
LensHaloParticles::Nsmooth
int Nsmooth
number of neighbours for adaptive smoothing
Definition: particle_halo2.h:312
LensHaloParticlesP
LensHaloParticles only with pseudo-periodic boundary conditions.
Definition: particle_halo2.h:672
LensHaloParticles::force_halo
void force_halo(double *alpha, KappaType *kappa, KappaType *gamma, KappaType *phi, double const *xcm, bool subtract_point=false, PosType screening=1)
does not zero lens quantities
Definition: particle_halo2.h:130
LensHaloHP::LensHaloHP
LensHaloHP(std::vector< HType > &halo_vector, PosType redshift, Point_2d lower_left, Point_2d upper_right, float hole_radius_angle, const COSMOLOGY &cosmo, int my_Nsmooth=0, float Nbucket=4, float theta=0.1, bool recenter=false, bool verbose=false)
Definition: particle_halo2.h:952
LensHaloParticles::CenterOfMass
Point_3d< DType > CenterOfMass()
get current center of mass in input coordinates
Definition: particle_halo2.h:145
LensHaloParticlesMO
LensHaloParticlesM only with a hole cut out of it.
Definition: particle_halo2.h:789
LensHaloParticles::LensHaloParticles
LensHaloParticles(std::vector< Point_3d< DType > > &pvector, PosType redshift, double my_inv_area_phys, PosType mass_particle, const COSMOLOGY &cosmo, int my_Nsmooth=5, float Nbucket=8, float theta=0.1, bool recenter=false, bool verbose=false)
construct directly from a vector of positions
Definition: particle_halo2.h:108
LensHaloParticlesP::force_halo
void force_halo(double *alpha, KappaType *kappa, KappaType *gamma, KappaType *phi, double const *xcm, bool subtract_point=false, PosType screening=1)
does not zero lens quantities
Definition: particle_halo2.h:760
LensHaloParticles::theta2
double theta2
square of opening angle for tree
Definition: particle_halo2.h:314
OTreeNB
OTreeNB: Tree structure used for force calculation with particles (i.e. stars, Nbody or halos).
Definition: oTreeNB.h:42
LensHaloParticlesP::LensHaloParticlesP
LensHaloParticlesP(const std::string &simulation_filename, SimFileFormat format, PosType redshift, double my_inv_area_phys, PosType mass_particle, Point_2d lower_left, Point_2d upper_right, const COSMOLOGY &cosmo, int my_Nsmooth=5, float Nbucket=8, float theta=0.1, bool recenter=false, bool verbose=false)
Definition: particle_halo2.h:674
Point_2d::length
PosType length() const
length
Definition: point.h:134
LensHalo::setTheta
void setTheta(PosType PosX, PosType PosY)
set the position of the Halo in radians
Definition: lens_halos.h:107
LensHaloParticles
A class that represents the lensing by a collection of simulation particles by tree code.
Definition: particle_halo2.h:68
LensHaloH::force_halo
void force_halo(double *alpha, KappaType *kappa, KappaType *gamma, KappaType *phi, double const *xcm, bool subtract_point=false, PosType screening=1)
does not zero lens quantities
Definition: particle_halo2.h:913
LensHaloParticles::getBoundingBox
void getBoundingBox(Point_3d< DType > &p1, Point_3d< DType > &p2) const
Definition: particle_halo2.h:296
LensHaloParticlesM::force_halo
void force_halo(double *alpha, KappaType *kappa, KappaType *gamma, KappaType *phi, double const *xcm, bool subtract_point=false, PosType screening=1)
does not zero lens quantities
Definition: particle_halo2.h:559
LensHaloParticles::setUp
void setUp(bool recenter, bool verbose)
Definition: particle_halo2.h:347
LensHaloHP::force_halo
void force_halo(double *alpha, KappaType *kappa, KappaType *gamma, KappaType *phi, double const *xcm, bool subtract_point=false, PosType screening=1)
does not zero lens quantities
Definition: particle_halo2.h:1023
Utilities::IO::CountColumns
int CountColumns(std::string filename, char comment_char='#', char deliniator=' ')
Count the number of columns in a ASCII data file.
Definition: utilities.cpp:614
LensHaloH::swapoutHalos
void swapoutHalos(std::vector< HType > &hvector)
swap out the halos, leaving the LensHaloH empty
Definition: particle_halo2.h:930
LensHaloParticles::translate_particles
void translate_particles(Point_3d< DType > &xo)
Definition: particle_halo2.h:504
LensHaloParticles::particle_mass
double particle_mass
mass
Definition: particle_halo2.h:315
LensHaloH
LensHalo for collecting LensHalos into one.
Definition: particle_halo2.h:865
LensHaloParticlesO::LensHaloParticlesO
LensHaloParticlesO(const std::string &simulation_filename, SimFileFormat format, PosType redshift, double my_inv_area_phys, PosType mass_particle, Point_2d x_hole, DType hole_radius, const COSMOLOGY &cosmo, int my_Nsmooth=5, float Nbucket=8, float theta=0.1, bool recenter=false, bool verbose=false)
Definition: particle_halo2.h:588
LensHaloParticlesM::swapoutMasses
void swapoutMasses(std::vector< DType > &Mvector)
swap out the masses, leaving the LensHaloParticlesM empty
Definition: particle_halo2.h:572
Point_3d
Class for representing points or vectors in 3 dimensions. Not that the dereferencing operator is over...
Definition: point.h:932
LensHaloParticles::make
static std::vector< LensHaloParticles< DType > > make(std::string filename, SimFileFormat format, float umass, float ulength, double inv_area, const COSMOLOGY &cosmo, int Nsmooth=5, float Nbucket=8, float theta=0.1, DType rmax=0, Point_3d< DType > xo=Point_3d< DType >(0, 0, 0), bool verbose=false)
Definition: particle_halo2.h:205
particle_types.h
LensHaloParticlesO::LensHaloParticlesO
LensHaloParticlesO(std::vector< Point_3d< DType > > &pvector, PosType redshift, double my_inv_area_phys, PosType mass_particle, Point_2d x_hole, DType hole_radius, const COSMOLOGY &cosmo, int my_Nsmooth=5, float Nbucket=8, float theta=0.1, bool recenter=false, bool verbose=false)
Definition: particle_halo2.h:606
LensHaloParticlesM
LensHaloParticles but where the "particles" have different masses.
Definition: particle_halo2.h:525
LensHaloParticlesM::LensHaloParticlesM
LensHaloParticlesM(std::vector< Point_3d< DType > > &pvector, PosType redshift, double my_inv_area_phys, std::vector< DType > Masses, const COSMOLOGY &cosmo, int my_Nsmooth=5, float Nbucket=8, float theta=0.1, bool recenter=false, bool verbose=false)
Definition: particle_halo2.h:528
SimFileFormat
SimFileFormat
Definition: particle_types.h:32
LensHaloParticlesP::LensHaloParticlesP
LensHaloParticlesP(std::vector< Point_3d< DType > > &pvector, PosType redshift, double my_inv_area_phys, PosType mass_particle, Point_2d lower_left, Point_2d upper_right, const COSMOLOGY &cosmo, int my_Nsmooth=5, float Nbucket=8, float theta=0.1, bool recenter=false, bool verbose=false)
Definition: particle_halo2.h:711