GLAMERDOC++
Gravitational Lensing Code Library
Loading...
Searching...
No Matches
grid_maintenance.h
1/*
2 * grid_maintenance.h
3 *
4 * Created on: Oct 12, 2011
5 * Author: bmetcalf
6 */
7
8#ifndef _grid_maintenance_declare_
9#define _grid_maintenance_declare_
10
11#include "lens.h"
12#include "point.h"
13#include "Tree.h"
14#include "source.h"
15#include <mutex>
16#include <utilities_slsim.h>
17#include "concave_hull.h"
18
19class LensHaloBaseNSIE;
20
24struct Grid{
25
26 Grid(LensHndl lens,unsigned long N1d,const double center[2],double range);
27 Grid(LensHndl lens ,unsigned long Nx ,const PosType center[2] ,PosType rangeX ,PosType rangeY);
28 ~Grid();
29
30 Grid ReInitialize(LensHndl lens);
31 //void ReShoot(LensHndl lens);
32 void zoom(LensHndl lens,double *center,double scale,Branch *top = NULL);
33
34 //unsigned long PruneTrees(double resolution,bool useSB,double fluxlimit);
35 //unsigned long PrunePointsOutside(double resolution,double *y,double r_in ,double r_out);
36
37 double RefreshSurfaceBrightnesses(Source* source);
38
39 double AddSurfaceBrightnesses(Source* source);
40
41
43 Point_2d y_source
44 ,PosType r_source_max
45 ,PosType luminosity
46 ,bool verbose=false
47 );
49 Point_2d y_source
50 ,PosType r_source
51 ,PosType z_source
52 ,std::vector<RAY> &images
53 ,bool verbose=false
54 );
55
57
58 /*** Refine the gris based on the smoothness of the surface brightness.
59 Return new total flux.
60
61 May be slow.
62 */
63 double refine_on_surfacebrightness(Lens &lens,Source &source);
64
65 unsigned long getNumberOfPoints() const;
67 PosType EinsteinArea() const;
68
70 TreeHndl i_tree;
72 TreeHndl s_tree;
73
75 int getInitNgrid(){return Ngrid_init;}
77 int getNgrid_block(){return Ngrid_block;}
79 double getInitRange(){return i_tree->getTop()->boundary_p2[0] - i_tree->getTop()->boundary_p1[0];}
80 Point_2d getInitCenter();
81 Point * RefineLeaf(LensHndl lens,Point *point);
82 Point * RefineLeaves(LensHndl lens,std::vector<Point *>& points);
83 void ClearAllMarks();
84
85 //void test_mag_matrix();
86 template <typename T>
87 void writeFits(const double center[],size_t Npixels,double resolution,LensingVariable lensvar,std::string filename);
88 template <typename T>
89 void writeFits(const double center[],size_t Nx,size_t Ny,double resolution,LensingVariable lensvar,std::string filename);
90
92 template <typename T>
93 void writeFits(
94 double strech
95 ,LensingVariable lensvar
96 ,std::string filename
97 );
98 template <typename T>
99 void writePixelFits(size_t Nx
100 ,LensingVariable lensvar
101 ,std::string filename
102 );
103 template <typename T>
104 void writeFitsVector(const double center[],size_t Npixels,double resolution,LensingVariable lensvar,std::string filename);
105 template <typename T>
106 PixelMap<T> writePixelMap(const double center[],size_t Npixels,double resolution,LensingVariable lensvar);
107 template <typename T>
108 PixelMap<T> writePixelMap(const double center[],size_t Nx,size_t Ny,double resolution,LensingVariable lensvar);
109
111 template <typename T>
113
115 template<typename T>
117 map.Clean();
118 map.AddGridBrightness(*this);
119 }
120
121 template <typename T>
122 PixelMap<T> MapSurfaceBrightness(double resolution);
123
124 template <typename T>
125 PixelMap<T> writePixelMapUniform(const PosType center[],size_t Nx,size_t Ny,LensingVariable lensvar);
126 template<typename T>
128
129 template <typename T>
130 void writeFitsUniform(const PosType center[],size_t Nx,size_t Ny,LensingVariable lensvar,std::string filename);
131
132 void find_images(
133 PosType *y_source
134 ,PosType r_source
135 ,int &Nimages
136 ,std::vector<ImageInfo> &imageinfo
137 ,unsigned long &Nimagepoints
138 );
139
140 void map_images(
141 Lens* lens
142 ,Source *source
143 ,int *Nimages
144 ,std::vector<ImageInfo> &imageinfo
145 ,PosType xmax
146 ,PosType xmin
147 ,PosType initial_size
148 ,ExitCriterion criterion
149 ,bool FindCenter
150 ,bool divide_images
151 );
152
153 Grid(Grid &&grid){
154 *this = std::move(grid);
155 }
156
157 Grid operator=(Grid &grid) = delete;
158 Grid(Grid &grid) = delete;
159
160 Grid & operator=(Grid &&grid){
161 assert(&grid != this);
162
163 i_tree = grid.i_tree;
164 grid.i_tree = nullptr;
165 s_tree = grid.s_tree;
166 grid.s_tree = nullptr;
167 neighbors = grid.neighbors;
168 grid.neighbors = nullptr;
169 //trashkist = grid.trashkist;
170 //grid.trashkist = nullptr;
171
172 Ngrid_init = grid.Ngrid_init;
173 Ngrid_init2 = grid.Ngrid_init2;
174 Ngrid_block = grid.Ngrid_block;
175 initialized = grid.initialized;
176 maglimit = grid.maglimit;
177 pointID = grid.pointID;
178 axisratio = grid.axisratio;
179
180 point_factory.clear();
181 point_factory=std::move(grid.point_factory);
182
183 return *this;
184 }
185
187 PosType magnification(double sblimit=-1.0e12) const;
188 PosType UnlensedFlux(double sblimit=-1.0e12) const;
189 PosType LensedFlux(double sblimit=-1.0e12) const;
190
191 //PosType magnification2() const;
192 //PosType magnification3() const;
194 Point_2d centroid() const;
195
196 private:
197 void xygridpoints(Point *points,double range,const double *center,long Ngrid
198 ,short remove_center);
199
201 int Ngrid_init;
202 int Ngrid_init2;
203
205 int Ngrid_block;
206 bool initialized;
207 //Kist<Point> * trashkist;
208
209 double maglimit;
210 Kist<Point> * neighbors;
211 bool find_mag_matrix(double *a,Point *p0,Point *p1,Point *p2);
212
213 bool uniform_mag_from_deflect(double *a,Point *point);
214 //bool uniform_mag_from_shooter(double *a,Point *point);
215
216 double mag_from_deflect(Point *point) const;
217
218 unsigned long pointID;
219 PosType axisratio;
220
221 template <typename T>
222 void writePixelMapUniform_(Point *head,size_t N
223 ,PixelMap<T> *map,LensingVariable val);
224
225 // cluge to make compatible with old method of producing points
226 Point * NewPointArray(size_t N){
227 Point * p = point_factory(N);
228 p[0].head = N;
229 for(size_t i=1; i < N ; ++i) p[i].head = 0;
230 return p;
231 }
232 MemmoryBank<Point> point_factory;
233 static std::mutex grid_mutex;
234};
235
236typedef struct Grid* GridHndl;
237
239std::string to_string(CritType crit);
240
241// in image_finder_kist.c
242namespace ImageFinding{
243
244 struct CriticalCurve{
245
246 CriticalCurve(){
247 critical_center[0] = critical_center[1] = 0.0;
248 caustic_center[0] = caustic_center[1] = 0.0;
249 critical_area = 0.0;
250 caustic_area = 0.0;
251 contour_ell = 0.0;
252 ellipse_area = 0.0;
253 z_source = 0.0;
254 type = CritType::ND;
255 caustic_intersections = -1;
256 touches_edge = false;
257 };
258
259 CriticalCurve(const CriticalCurve &p){
260 critcurve = p.critcurve;
261 //critical_curve = p.critical_curve;
262 caustic_curve_outline = p.caustic_curve_outline;
263 caustic_curve_intersecting = p.caustic_curve_intersecting;
264 critical_center = p.critical_center;
265 caustic_center = p.caustic_center;
266 critical_area = p.critical_area;
267 caustic_area = p.caustic_area;
268 ellipse_curve = p.ellipse_curve;
269 contour_ell = p.contour_ell;
270 ellipse_area = p.ellipse_area;
271 z_source = p.z_source;
272 type = p.type;
273 caustic_intersections = p.caustic_intersections;
274 touches_edge = p.touches_edge;
275 }
276
277 CriticalCurve & operator=(const CriticalCurve &p){
278 if(this == &p) return *this;
279
280 critcurve = p.critcurve;
281 //critical_curve = p.critical_curve;
282 caustic_curve_outline = p.caustic_curve_outline;
283 caustic_curve_intersecting = p.caustic_curve_intersecting;
284 critical_center = p.critical_center;
285 caustic_center = p.caustic_center;
286 critical_area = p.critical_area;
287 caustic_area = p.caustic_area;
288 ellipse_curve = p.ellipse_curve;
289 contour_ell = p.contour_ell;
290 ellipse_area = p.ellipse_area;
291 z_source = p.z_source;
292 type = p.type;
293 caustic_intersections = p.caustic_intersections;
294 touches_edge = p.touches_edge;
295 return *this;
296 }
297
298 std::vector<RAY> critcurve;
299 std::vector<Point_2d> caustic_curve_outline;
300 std::vector<Point_2d> caustic_curve_intersecting;
301 std::vector<Point_2d> ellipse_curve;
302
303 PosType z_source;
305 CritType type;
307 int caustic_intersections;
308
310 Point_2d critical_center;
312 Point_2d caustic_center;
313
315 PosType critical_area;
317 PosType caustic_area;
318
320 PosType contour_ell;
322 PosType ellipse_area;
323
325 bool touches_edge;
326
328
329 bool inCausticCurve(Point_2d &x){
330 return Utilities::inhull(x.x,caustic_curve_outline);
331 }
332
334 bool intersectingCausticCurve(Point_2d &x,double r){
335 return Utilities::circleIntersetsCurve(x, r, caustic_curve_outline);
336 }
337
338
340 bool inCriticalCurve(Point_2d &x){
341 return Utilities::inhull<RAY>(x.x,critcurve);
342 }
343
345 bool EntirelyinCausticCurve(Point_2d &x, PosType sourceRadius)
346 {
347 // Testing if the center of the source is within the caustic :
348 bool IsInCurve = Utilities::inCurve(x,caustic_curve_outline);
349
350 // Testing now that it is not too close from it (i.e. farther than source radius) :
351 int i=0; // index going over the different points of the caustic curve
352 PosType DistSourceToCautic; // distance between the source center and the considered point of the caustic line.
353
354 if(IsInCurve == true) // center inside the caustic
355 {
356 while(i<caustic_curve_outline.size()) // testing that the source size does not overlap with the caustic line.
357 {
358 DistSourceToCautic = sqrt((caustic_curve_outline[i].x[0] - x.x[0])*(caustic_curve_outline[i].x[0] - x.x[0]) + (caustic_curve_outline[i].x[1] - x.x[1])*(caustic_curve_outline[i].x[1] - x.x[1]));
359 if (DistSourceToCautic < sourceRadius) return false ; // source too close, we return false and don't consider the point.
360 i++;
361 }
362 return true ; // if not we return true (the source is valid)
363 }
364 else return false ; // center not inside the caustic
365 }
366
368 bool EntirelyinCriticalCurve(Point_2d x, PosType sourceRadius)
369 {
370 // Testing if the center of the source is within the critical curve :
371 bool IsInCurve = Utilities::inCurve(x,caustic_curve_outline);
372
373 // Testing now that it is not too close from it (i.e. farther than source radius) :
374 int i=0; // index going over the different points of the critical curve
375 PosType DistSourceToCritCurve; // distance between the source center and the considered point of the critical line.
376
377 if(IsInCurve == true) // center inside the critical line
378 {
379 while(i<critcurve.size()) // testing that the source size does not overlap with the critical line.
380 {
381 DistSourceToCritCurve = sqrt((critcurve[i].x[0] - x.x[0])*(critcurve[i].x[0] - x.x[0]) + (critcurve[i].x[1] - x.x[1])*(critcurve[i].x[1] - x.x[1]));
382 if (DistSourceToCritCurve < sourceRadius) return false ; // source too close, we return false and don't consider the point.
383 i++;
384 }
385 return true ; // if not we return true (the source is valid)
386 }
387 else return false ; // center not inside the critical line
388 }
389
390
393 void RandomSourcesWithinCaustic(
394 int N
395 ,std::vector<Point_2d> &y
397 );
398
399 Point_2d RandomSourceWithinCaustic(
401 );
402
405 void RandomSourcesNearCaustic(double R
406 ,int N
407 ,std::vector<Point_2d> &y
409 );
412 Point_2d RandomSourceNearCaustic(double R
414 );
415
416
419 void RandomSourceStrictlyWithinCaustic(int N
420 ,std::vector<Point_2d> &y
422 ,PosType sourceRadius
423 ,PosType distSourceToCaustic
424 );
426 void CritRange(Point_2d &p1,Point_2d &p2) const;
428 void CausticRange(Point_2d &p1,Point_2d &p2) const;
429
431 void CriticalRadius(PosType &rmax,PosType &rmin,PosType &rave) const{
432 if(critcurve.size() < 2){
433 rave = rmax = rmin = 0.0;
434 return;
435 }
436
437 rave = rmin = rmax = (critical_center - critcurve[0].x).length();
438 PosType rad;
439
440 for(size_t ii=1; ii< critcurve.size(); ++ii){
441 rad = (critical_center - critcurve[ii].x).length();
442
443 rave += rad;
444 if(rad < rmin) rmin = rad;
445 if(rad > rmax) rmax = rad;
446 }
447
448 rave /= critcurve.size();
449 }
451 void CausticRadius(PosType &rmax,PosType &rmin,PosType &rave) const{
452 if(caustic_curve_outline.size() < 2){
453 rave = rmax = rmin = 0.0;
454 return;
455 }
456
457 rave = rmin = rmax = (caustic_center - caustic_curve_outline[0]).length();
458
459 PosType rad;
460
461 for(size_t ii=1; ii< caustic_curve_outline.size(); ++ii){
462 rad = (caustic_center - caustic_curve_outline[ii]).length();
463
464 rave += rad;
465 if(rad < rmin) rmin = rad;
466 if(rad > rmax) rmax = rad;
467 }
468
469 rave /= caustic_curve_outline.size();
470 }
471
473 double AreaNearCaustic(double R
474 );
475
476 private:
477 Point_2d p1,p2;
478 };
479
480 void find_images_kist(LensHndl lens,PosType *y_source,PosType r_source,GridHndl grid
481 ,int *Nimages,std::vector<ImageInfo> &imageinfo,unsigned long *Nimagepoints
482 ,PosType initial_size,bool splitimages,short edge_refinement
483 ,bool verbose = false);
484
485 //void find_image_simple(LensHndl lens,Point_2d y_source,PosType z_source,Point_2d &image_x
486 // ,PosType xtol2,PosType &fret);
487
488 void find_images_microlens(LensHndl lens,double *y_source,double r_source,GridHndl grid
489 ,int *Nimages,std::vector<ImageInfo> &imageinfo,unsigned long *Nimagepoints
490 ,double initial_size,double mu_min,bool splitimages,short edge_refinement
491 ,bool verbose);
492
493 void find_images_microlens_exper(LensHndl lens,PosType *y_source,PosType r_source
494 ,GridHndl grid,int *Nimages,std::vector<ImageInfo> &imageinfo,unsigned long *Nimagepoints,PosType initial_size ,PosType mu_min
495 ,bool splitimages,short edge_refinement,bool verbose);
496
497 void image_finder_kist(LensHndl lens, PosType *y_source,PosType r_source,GridHndl grid
498 ,int *Nimages,std::vector<ImageInfo> &imageinfo,unsigned long *Nimagepoints
499 ,short splitparities,short true_images);
500
501
502 void find_crit(LensHndl lens,GridHndl grid,std::vector<CriticalCurve> &crtcurve,int *Ncrits
503 ,double resolution,double invmag_min = 0.0,bool verbose = false,bool test=false);
504 void find_crit(Lens &lens,GridMap &gridmap,std::vector<CriticalCurve> &crtcurves,bool verbose = false);
505
506 // finds the contours of magnification and source plane curve
507 void find_magnification_contour(
508 Lens &lens
509 ,GridMap &gridmap
510 ,double invmag
511 ,std::vector<std::vector<RAY> > &contour
512 ,std::vector<bool> &hits_boundary
513 );
514
515 //void find_crit2(LensHndl lens,GridHndl grid,std::vector<CriticalCurve> &critcurve,int *Ncrits
516 // ,double resolution,bool *orderingsuccess,bool ordercurve,bool dividecurves,double invmag_min = 0.0,bool verbose = false);
517
518 CritType find_pseudo(ImageInfo &pseudocurve,ImageInfo &negimage
519 ,PosType pseudolimit,LensHndl lens,GridHndl grid
520 ,PosType resolution,Kist<Point> &paritypoints,bool TEST=false);
521
522 void find_contour(LensHndl lens,GridHndl grid,std::vector<CriticalCurve> &contour,int *Ncrits,PosType resolution,bool *orderingsuccess,bool ordercurve, bool dividecurves, double contour_value,LensingVariable contour_type,bool verbose = false);
523
524 namespace Temporary{
525 //PosType *y;
526 //Lens * lens;
527 //Point *point;
528
529 PosType mindyfunc(PosType *x);
530 }
531
532 namespace IF_routines{
533 int refine_grid_kist(LensHndl lens,GridHndl grid,ImageInfo *imageinfo
534 ,int Nimages,double res_target,short criterion
535 ,Kist<Point> * newpointkist = NULL,bool batch=true);
536
537
538 void refine_crit_in_image(LensHndl lens,GridHndl grid,double r_source,double x_source[],double resolution);
539
540 int refine_grid(LensHndl lens,GridHndl grid,OldImageInfo *imageinfo
541 ,unsigned long Nimages,double res_target,short criterion,bool batch=true);
542
543 long refine_edges(LensHndl lens,GridHndl grid,ImageInfo *imageinfo
544 ,int Nimages,double res_target,short criterion
545 ,Kist<Point> * newpointkist = NULL,bool batch=true);
546
547 long refine_edges2(LensHndl lens,double *y_source,double r_source,GridHndl grid
548 ,ImageInfo *imageinfo,bool *image_overlap,int Nimages,double res_target
549 ,short criterion,bool batch=true);
550
551 void sort_out_points(Point *i_points,ImageInfo *imageinfo,double r_source,double y_source[]);
552
553 }
554
555 void printCriticalCurves(std::string filename
556 ,const std::vector<ImageFinding::CriticalCurve> &critcurves);
557
561 template<typename T>
564 const std::vector<ImageFinding::CriticalCurve> &critcurves,
566 int Nx
567 );
568
572 template<typename T>
573 PixelMap<T> mapCausticCurves(const std::vector<ImageFinding::CriticalCurve> &critcurves
574 ,int Nx
575 );
576}
577
578
579std::ostream &operator<<(std::ostream &os, const ImageFinding::CriticalCurve &p);
580
582template <typename T>
584 const PosType center[]
585 ,size_t Npixels
586 ,PosType resolution
587 ,LensingVariable lensvar
588 ,std::string filename
589 ){
590 writeFits<T>(center,Npixels,Npixels,resolution,lensvar,filename);
591}
592
593template <typename T>
595 const PosType center[]
596 ,size_t Nx
597 ,size_t Ny
598 ,PosType resolution
599 ,LensingVariable lensvar
600 ,std::string filename
601 ){
602 PixelMap<T> map(center, Nx,Ny, resolution);
603 std::string tag;
604
605 switch (lensvar) {
607 tag = ".dt.fits";
608 break;
610 tag = ".alpha1.fits";
611 break;
613 tag = ".alpha2.fits";
614 break;
616 tag = ".alpha.fits";
617 break;
619 tag = ".kappa.fits";
620 break;
622 tag = ".gamma1.fits";
623 break;
625 tag = ".gamma2.fits";
626 break;
628 tag = ".gamma3.fits";
629 break;
631 tag = ".gamma.fits";
632 break;
634 tag = ".invmag.fits";
635 break;
637 tag = ".surfbright.fits";
638 break;
639 default:
640 break;
641 }
642
643 map.AddGrid(*this,lensvar);
644 map.printFITS(filename + tag);
645
646 return;
647}
648
650template <typename T>
652 const PosType center[]
653 ,size_t Npixels
654 ,PosType resolution
655 ,LensingVariable lensvar
656 ){
657
658 return writePixelMap<T>(center,Npixels,Npixels,resolution,lensvar);
659}
660
661template <typename T>
663 const PosType center[]
664 ,size_t Nx
665 ,size_t Ny
666 ,PosType resolution
667 ,LensingVariable lensvar
668){
669 PixelMap<T> map(center, Nx, Ny, resolution);
670 map.AddGrid(*this,lensvar);
671
672 return map;
673}
674
675template <typename T>
677 LensingVariable lensvar
678){
679
680 Branch *branch = i_tree->getTop();
681 double resolution = (branch->boundary_p2[0] - branch->boundary_p1[0])/Ngrid_init;
682 PixelMap<T> map(branch->center, Ngrid_init, Ngrid_init2, resolution);
683 map.AddGrid(*this,lensvar);
684
685 return map;
686}
687
688template <typename T>
690 Branch *branch = i_tree->getTop();
691 int Nx = (int)( (branch->boundary_p2[0] - branch->boundary_p1[0])/resolution );
692 int Ny = (int)( (branch->boundary_p2[1] - branch->boundary_p1[1])/resolution );
693
694 PixelMap<T> map(branch->center,Nx,Ny,resolution);
695 map.AddGridBrightness(*this);
696
697 return map;
698}
699
702template <typename T>
704 size_t Nx
705 ,LensingVariable lensvar
706 ,std::string filename
707){
708
709 Point_2d center = getInitCenter();
710 PosType resolution = getInitRange()/Nx;
711 size_t Ny = (size_t)(Nx*axisratio);
712 writeFits<T>(center.x,Nx,Ny,resolution, lensvar, filename);
713
714 return;
715}
716
724template <typename T>
726 const PosType center[]
727 ,size_t Nx
728 ,size_t Ny
729 ,LensingVariable lensvar
730 ,std::string filename
731 ){
732 std::string tag;
733
734 switch (lensvar) {
736 tag = ".dt.fits";
737 break;
739 tag = ".alpha1.fits";
740 break;
742 tag = ".alpha2.fits";
743 break;
745 tag = ".alpha.fits";
746 break;
748 tag = ".kappa.fits";
749 break;
751 tag = ".gamma1.fits";
752 break;
754 tag = ".gamma2.fits";
755 break;
757 tag = ".gamma3.fits";
758 break;
760 tag = ".gamma.fits";
761 break;
763 tag = ".invmag.fits";
764 break;
766 tag = ".surfbright.fits";
767 break;
768 default:
769 break;
770 }
771
772 PixelMap<T> map = writePixelMapUniform<T>(center,Nx,Ny,lensvar);
773 map.printFITS(filename + tag);
774}
775
783template <typename T>
785 const PosType center[]
786 ,size_t Nx
787 ,size_t Ny
788 ,LensingVariable lensvar
789 ){
790
791 if(getNumberOfPoints() ==0 ) return PixelMap<T>();
792 PixelMap<T> map(center, Nx, Ny,i_tree->pointlist.Top()->gridsize);
793 map.Clean();
794
795 //int Nblocks = Utilities::GetNThreads();
796 int Nblocks = 16;
797
798 //std::vector<PointList> lists(Nblocks);
799
800 std::vector<Point *> heads(Nblocks);
801 std::vector<size_t> sizes(Nblocks,0);
802
803 bool allowDecent;
804 TreeStruct::iterator i_tree_it(i_tree);
805 int i = 0;
806
807 do{
808 if((*i_tree_it)->level == 4){
809
810 heads[i] = (*i_tree_it)->points;
811 sizes[i] = (*i_tree_it)->npoints;
812
813 //lists[i].setTop( (*i_tree_it)->points );
814 //lists[i].setN( (*i_tree_it)->npoints );
815 ++i;
816 allowDecent = false;
817 }else{
818 allowDecent = true;
819 }
820 }while(i_tree_it.TreeWalkStep(allowDecent) && i < Nblocks);
821
822 std::vector<std::thread> thrs;
823 for(int ii = 0; ii < i ;++ii){
824 //writePixelMapUniform_(heads[ii],sizes[ii],&map,lensvar);
825 //thrs.push_back(std::thread(&Grid::writePixelMapUniform_,this,lists[ii],&map,lensvar));
826
827 thrs.push_back(std::thread(&Grid::writePixelMapUniform_<T>,this,heads[ii],sizes[ii],&map,lensvar));
828 }
829 for(int ii = 0; ii < i ;++ii) thrs[ii].join();
830
831 return map;
832}
833
834template <typename T>
836 PixelMap<T> &map
837 ,LensingVariable lensvar
838 ){
839
840 if(getNumberOfPoints() ==0 ) return;
841
842 map.Clean();
843 int Nblocks = 16;
844 //std::vector<PointList> lists(Nblocks);
845 TreeStruct::iterator i_tree_it(i_tree);
846
847 std::vector<Point *> heads(Nblocks);
848 std::vector<size_t> sizes(Nblocks,0);
849
850
851 bool allowDecent;
852 i_tree_it.movetop();
853 int i = 0;
854 do{
855 if((*i_tree_it)->level == 4){
856 assert(i < 16);
857 //lists[i].setTop( (*i_tree_it)->points );
858 //lists[i].setN( (*i_tree_it)->npoints );
859 heads[i] = (*i_tree_it)->points;
860 sizes[i] = (*i_tree_it)->npoints;
861
862 ++i;
863 allowDecent = false;
864 }else{
865 allowDecent = true;
866 }
867 }while(i_tree_it.TreeWalkStep(allowDecent) && i < Nblocks);
868
869 std::vector<std::thread> thr;
870 for(int ii = 0; ii < i ;++ii){
871 thr.push_back(std::thread(&Grid::writePixelMapUniform_<T>,this,heads[ii],sizes[ii],&map,lensvar));
872 }
873 for(auto &t : thr) t.join();
874}
875
876template <typename T>
877void Grid::writePixelMapUniform_(Point *head,size_t N,PixelMap<T> *map,LensingVariable val){
878 double tmp;
879 PosType tmp2[2];
880 long index;
881
882 Point *ppoint = head;
883
884 for(size_t i = 0; i< N; ++i){
885
886 switch (val) {
888 tmp2[0] = ppoint->x[0] - ppoint->image->x[0];
889 tmp2[1] = ppoint->x[1] - ppoint->image->x[1];
890 tmp = sqrt(tmp2[0]*tmp2[0] + tmp2[1]*tmp2[1]);
891 break;
893 tmp = (ppoint->x[0] - ppoint->image->x[0]);
894 break;
896 tmp = (ppoint->x[1] - ppoint->image->x[1]);
897 break;
899 tmp = ppoint->kappa();
900 break;
902 tmp2[0] = ppoint->gamma1();
903 tmp2[1] = ppoint->gamma2();
904 tmp = sqrt(tmp2[0]*tmp2[0] + tmp2[1]*tmp2[1]);
905 break;
907 tmp = ppoint->gamma1();
908 break;
910 tmp = ppoint->gamma2();
911 break;
913 tmp = ppoint->gamma3();
914 break;
916 tmp = ppoint->invmag();
917 break;
919 tmp = ppoint->dt;
920 break;
922 tmp = ppoint->surface_brightness;
923 break;
924 default:
925 std::cerr << "PixelMap<T>::AddGrid() does not work for the input LensingVariable" << std::endl;
926 throw std::runtime_error("PixelMap<T>::AddGrid() does not work for the input LensingVariable");
927 break;
928 // If this list is to be expanded to include ALPHA or GAMMA take care to add them as vectors
929 }
930
931 index = map->find_index(ppoint->x);
932 if(index != -1)(*map)[index] = tmp;
933
934 ppoint = ppoint->next;
935 }
936}
937
939template <typename T>
941 const PosType center[]
942 ,size_t Npixels
943 ,PosType resolution
944 ,LensingVariable lensvar
945 ,std::string filename
946 ){
947 //throw std::runtime_error("Not done yet!");
948
949 PosType range = Npixels*resolution,tmp_x[2];
950 ImageInfo tmp_image,tmp_image_theta;
951 size_t i;
952 std::string tag;
953
954 i_tree->PointsWithinKist(center,range/sqrt(2.),tmp_image.imagekist,0);
955 i_tree->PointsWithinKist(center,range/sqrt(2.),tmp_image_theta.imagekist,0);
956
957 std::vector<PosType> tmp_sb_vec(tmp_image.imagekist->Nunits());
958
959 for(tmp_image.imagekist->MoveToTop(),i=0;i<tmp_sb_vec.size();++i,tmp_image.imagekist->Down()){
960 tmp_sb_vec[i] = tmp_image.imagekist->getCurrent()->surface_brightness;
961 switch (lensvar) {
963 tmp_x[0] = tmp_image.imagekist->getCurrent()->x[0]
964 - tmp_image.imagekist->getCurrent()->image->x[0];
965
966 tmp_x[1] = tmp_image.imagekist->getCurrent()->x[1]
967 - tmp_image.imagekist->getCurrent()->image->x[1];
968
969 tmp_image.imagekist->getCurrent()->surface_brightness = sqrt( tmp_x[0]*tmp_x[0] + tmp_x[1]*tmp_x[1]);
970 tmp_image_theta.imagekist->getCurrent()->surface_brightness = atan2(tmp_x[1],tmp_x[0]);
971
972 tag = ".alphaV.fits";
973 break;
975
976 tmp_x[0] = tmp_image.imagekist->getCurrent()->gamma1();
977 tmp_x[1] = tmp_image.imagekist->getCurrent()->gamma2();
978
979 tmp_image.imagekist->getCurrent()->surface_brightness = sqrt( tmp_x[0]*tmp_x[0] + tmp_x[1]*tmp_x[1]);
980 tmp_image_theta.imagekist->getCurrent()->surface_brightness = atan2(tmp_x[1],tmp_x[0])/2;
981
982 tag = ".gammaV.fits";
983 break;
984 default:
985 std::cout << "Grid::writeFitsVector() does not support the LensVariable you are using." << std::endl;
986 return;
987 }
988 }
989
990 PixelMap<T> map_m(center, Npixels, resolution),map_t(center,Npixels,resolution);
991
992 map_m.Clean();
993 map_m.AddImages(&tmp_image,1,-1);
994 map_m = PixelMap<T>(map_m,4);
995 map_m = PixelMap<T>(map_m,1/4.);
996
997 map_t.Clean();
998 map_t.AddImages(&tmp_image_theta,1,-1);
999
1000 map_m.printFITS(filename + tag);
1001
1002 for(tmp_image.imagekist->MoveToTop(),i=0;i<tmp_sb_vec.size();++i,tmp_image.imagekist->Down())
1003 tmp_image.imagekist->getCurrent()->surface_brightness = tmp_sb_vec[i];
1004}
1005
1006template <typename T>
1007void Grid::writeFits(double strech,LensingVariable lensvar ,std::string filename){
1008 Point_2d center = getInitCenter();
1009 size_t N1 = (size_t)(strech*Ngrid_init);
1010 size_t N2 = (size_t)(strech*Ngrid_init2);
1011
1012 writeFits<T>(center.x,N1,N2,getInitRange()/N1,lensvar,filename);
1013}
1014
1015
1016
1017#endif
A class to represents a lens with multiple planes.
Definition lens.h:71
Image structure that can be manipulated and exported to/from fits files.
Definition pixelmap.h:42
void AddGridBrightness(Grid &grid)
Add an image from a the surface brightnesses of a Grid to the PixelMap.
Definition pixelmap.cpp:685
void AddImages(ImageInfo *imageinfo, int Nimages, float rescale=1.)
Add an image to the map.
Definition pixelmap.cpp:641
long find_index(PosType const x[], long &ix, long &iy) const
get the index for a position, returns -1 if out of map, this version returns the 2D grid coordinates
Definition pixelmap.cpp:2227
void AddGrid(const Grid &grid, T value=1.0)
Fills in pixels where the image plane points in the grid are located with the value given.
Definition pixelmap.cpp:1936
void printFITS(std::string filename, bool Xflip=false, bool ctype=false, bool verbose=false)
Output the pixel map as a fits file.
Definition pixelmap.cpp:1318
Base class for all sources.
Definition source.h:44
A iterator class fore TreeStruct that allows for movement through the tree without changing anything ...
Definition Tree.h:47
bool TreeWalkStep(bool allowDescent)
step for walking tree by iteration instead of recursion.
Definition tree_maintenance.cpp:1610
This is a class for generating random numbers. It simplifies and fool proofs initialization and allow...
Definition utilities_slsim.h:1059
The ImageFinding namespace is for functions related to finding and mapping images.
Definition grid_maintenance.h:242
PixelMap< T > mapCausticCurves(const std::vector< ImageFinding::CriticalCurve > &critcurves, int Nx)
Makes an image of the caustic curves. The map will encompose all curves found. The pixel values are t...
Definition Tree.cpp:1064
void find_images_microlens(LensHndl lens, double *y_source, double r_source, GridHndl grid, int *Nimages, std::vector< ImageInfo > &imageinfo, unsigned long *Nimagepoints, double initial_size, double mu_min, bool splitimages, short edge_refinement, bool verbose)
Finds images given a source position and size.
Definition image_finder_kist.cpp:465
void find_images_kist(LensHndl lens, PosType *y_source, PosType r_source, GridHndl grid, int *Nimages, std::vector< ImageInfo > &imageinfo, unsigned long *Nimagepoints, PosType initial_size, bool splitimages, short edge_refinement, bool verbose=false)
Finds images given a source position and size.
Definition image_finder_kist.cpp:41
PixelMap< T > mapCriticalCurves(const std::vector< ImageFinding::CriticalCurve > &critcurves, int Nx)
Makes an image of the critical curves. The map will encompose all curves found. The pixel values are ...
Definition Tree.cpp:1026
void image_finder_kist(LensHndl lens, PosType *y_source, PosType r_source, GridHndl grid, int *Nimages, std::vector< ImageInfo > &imageinfo, unsigned long *Nimagepoints, short splitparities, short true_images)
Finds images for a given source position and size. Not meant for high level user.
Definition image_finder_kist.cpp:1530
CritType find_pseudo(ImageInfo &pseudocurve, ImageInfo &negimage, PosType pseudolimit, LensHndl lens, GridHndl grid, PosType resolution, Kist< Point > &paritypoints, bool TEST=false)
Definition find_crit.cpp:1122
void find_contour(LensHndl lens, GridHndl grid, std::vector< CriticalCurve > &contour, int *Ncrits, PosType resolution, bool *orderingsuccess, bool ordercurve, bool dividecurves, double contour_value, LensingVariable contour_type, bool verbose=false)
Finds iso kappa contours.
Definition find_crit.cpp:1912
void find_images_microlens_exper(LensHndl lens, PosType *y_source, PosType r_source, GridHndl grid, int *Nimages, std::vector< ImageInfo > &imageinfo, unsigned long *Nimagepoints, PosType initial_size, PosType mu_min, bool splitimages, short edge_refinement, bool verbose)
experimental version of find_image_microlens()
Definition image_finder_kist.cpp:1027
void find_crit(LensHndl lens, GridHndl grid, std::vector< CriticalCurve > &crtcurve, int *Ncrits, double resolution, double invmag_min=0.0, bool verbose=false, bool test=false)
Finds critical curves and caustics.
Definition find_crit.cpp:37
bool inhull< RAY >(PosType x[], const std::vector< RAY > &H)
finds in x is within the curve discribed by the H[].x points ie image points
Definition concave_hull.h:1131
bool circleIntersetsCurve(const Point_2d &x, double r, const std::vector< Point_2d > &v)
returns true if a circle of radius r around the point x intersects with the curve v....
Definition curve_routines.cpp:2936
bool inCurve(const Ptype &x, const std::vector< Ptype > &H)
returns true if x is within the polygon H
Definition concave_hull.h:1021
LensingVariable
output lensing variables
Definition standard.h:89
@ ALPHA
magnitude of deflection in radians
Definition standard.h:91
@ GAMMA2
second component of shear
Definition standard.h:97
@ DELAYT
time delay
Definition standard.h:90
@ GAMMA3
third component of shear
Definition standard.h:98
@ ALPHA2
y component of deflection
Definition standard.h:93
@ SurfBrightness
Surface brightness.
Definition standard.h:101
@ GAMMA1
first component of shear
Definition standard.h:96
@ GAMMA
magnitude of shear
Definition standard.h:95
@ KAPPA
convergence
Definition standard.h:94
@ ALPHA1
x component of deflection
Definition standard.h:92
@ INVMAG
inverse of magnification
Definition standard.h:99
The box representing a branch of a binary tree structure. Used specifically in TreeStruct for organiz...
Definition point.h:638
Structure to contain both source and image trees.
Definition grid_maintenance.h:24
void map_images(Lens *lens, Source *source, int *Nimages, std::vector< ImageInfo > &imageinfo, PosType xmax, PosType xmin, PosType initial_size, ExitCriterion criterion, bool FindCenter, bool divide_images)
Find images and refine them based on their surface brightness distribution.
Definition map_images.cpp:27
double RefreshSurfaceBrightnesses(Source *source)
Reshoot the rays with the same image postions.
Definition grid_maintenance.cpp:315
void zoom(LensHndl lens, double *center, double scale, Branch *top=NULL)
Test if point is in a region of uniform magnification using the kappa and gamma calculated from the r...
Definition grid_maintenance.cpp:1233
double ClearSurfaceBrightnesses()
Reset the surface brightness and in_image flag in every point on image and source planes to zero (fal...
Definition grid_maintenance.cpp:500
double AddSurfaceBrightnesses(Source *source)
Recalculate surface brightness just like Grid::RefreshSurfaceBrightness but the new source is added t...
Definition grid_maintenance.cpp:344
PosType EinsteinArea() const
area of region with negative magnification
Definition grid_maintenance.cpp:368
PixelMap< T > writePixelMap(const double center[], size_t Npixels, double resolution, LensingVariable lensvar)
Outputs a PixelMap of the lensing quantities of a fixed grid.
Definition grid_maintenance.h:651
int getInitNgrid()
return initial number of grid points in each direction
Definition grid_maintenance.h:75
~Grid()
Destructor for a Grid. Frees all memory.
Definition grid_maintenance.cpp:136
void ClearAllMarks()
Rest all in_image markers to False.
Definition grid_maintenance.cpp:984
void writeFitsUniform(const PosType center[], size_t Nx, size_t Ny, LensingVariable lensvar, std::string filename)
Output a fits map of the without distribution the pixels.
Definition grid_maintenance.h:725
void MapSurfaceBrightness(PixelMap< T > &map)
make image of surface brightness
Definition grid_maintenance.h:116
unsigned long getNumberOfPoints() const
Returns number of points on image plane.
Definition grid_maintenance.cpp:518
void find_point_source_images(Point_2d y_source, PosType r_source, PosType z_source, std::vector< RAY > &images, bool verbose=false)
This function finds all the images for a circular source of radius r_source, then finds the points wi...
Definition image_finder.cpp:1094
void writeFits(const double center[], size_t Npixels, double resolution, LensingVariable lensvar, std::string filename)
Outputs a fits image of a lensing variable of choice.
Definition grid_maintenance.h:583
Grid(LensHndl lens, unsigned long N1d, const double center[2], double range)
Constructor for initializing square grid.
Definition grid_maintenance.cpp:22
Point * RefineLeaves(LensHndl lens, std::vector< Point * > &points)
Same as RefineLeaf() but multiple points can be passed. The rays are shot all together so that more p...
Definition grid_maintenance.cpp:681
double mark_closest_point_source_images(Point_2d y_source, PosType r_source_max, PosType luminosity, bool verbose=false)
This function finds all the images for a circular source of radius r_source, then finds the points wi...
Definition image_finder.cpp:1061
Grid ReInitialize(LensHndl lens)
Returns a new grid that has not been refined but has the same intial image grid, but calculated with ...
Definition grid_maintenance.cpp:148
double getInitRange()
return initial range of gridded region
Definition grid_maintenance.h:79
void find_images(PosType *y_source, PosType r_source, int &Nimages, std::vector< ImageInfo > &imageinfo, unsigned long &Nimagepoints)
Finds images for a given source position and size. It seporates images of different pairities.
Definition image_finder.cpp:951
TreeHndl s_tree
tree on source plane
Definition grid_maintenance.h:72
PosType magnification(double sblimit=-1.0e12) const
flux weighted local magnification that does not take multiple imaging into effect
Definition grid_maintenance.cpp:398
TreeHndl i_tree
tree on image plane
Definition grid_maintenance.h:70
Point_2d centroid() const
centroid of flux
Definition grid_maintenance.cpp:456
void writePixelFits(size_t Nx, LensingVariable lensvar, std::string filename)
Make a fits map that is automatically centered on the grid and has approximately the same range as th...
Definition grid_maintenance.h:703
Point * RefineLeaf(LensHndl lens, Point *point)
Fundamental function used to divide a leaf in the tree into nine subcells.
Definition grid_maintenance.cpp:543
void writeFitsVector(const double center[], size_t Npixels, double resolution, LensingVariable lensvar, std::string filename)
Outputs a fits file for making plots of vector fields.
Definition grid_maintenance.h:940
PixelMap< T > writePixelMapUniform(const PosType center[], size_t Nx, size_t Ny, LensingVariable lensvar)
Make a Pixel map of the without distribution the pixels.
Definition grid_maintenance.h:784
int getNgrid_block()
return number of cells in each dimension into which each cell is divided when a refinement is made
Definition grid_maintenance.h:77
A simplified version of the Grid structure for making non-adaptive maps of the lensing quantities (ka...
Definition gridmap.h:31
Structure for storing information about images or curves.
Definition image_info.h:20
Kist< Point > * imagekist
Array of points in image, SHOULD NOT BE USED IN FAVOR OF imagekist! Still used by caustic finding rou...
Definition image_info.h:47
This is an old version that should not be used anymore in favor of ImageInfo.
Definition image_info.h:109
Class for representing points or vectors in 2 dimensions. Not that the dereferencing operator is over...
Definition point.h:48
A point on the source or image plane that contains a position and the lensing quantities.
Definition point.h:421