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//class LensHaloMassMap;
21
25struct Grid{
26
27 Grid(LensHndl lens,unsigned long N1d,const double center[2],double range);
28 Grid(LensHndl lens ,unsigned long Nx ,const PosType center[2] ,PosType rangeX ,PosType rangeY);
29 ~Grid();
30
32 //void ReShoot(LensHndl lens);
33 void zoom(LensHndl lens,double *center,double scale,Branch *top = NULL);
34
35 //unsigned long PruneTrees(double resolution,bool useSB,double fluxlimit);
36 //unsigned long PrunePointsOutside(double resolution,double *y,double r_in ,double r_out);
37
38 double RefreshSurfaceBrightnesses(Source* source);
39
40 double AddSurfaceBrightnesses(Source* source);
41
42
44 Point_2d y_source
45 ,PosType r_source_max
46 ,PosType luminosity
47 ,bool verbose=false
48 );
50 Point_2d y_source
51 ,PosType r_source
52 ,PosType z_source
53 ,std::vector<RAY> &images
54 ,bool verbose=false
55 );
56
58
59 /*** Refine the gris based on the smoothness of the surface brightness.
60 Return new total flux.
61
62 May be slow.
63 */
64 double refine_on_surfacebrightness(Lens &lens,Source &source);
65
66 unsigned long getNumberOfPoints() const;
68 PosType EinsteinArea() const;
69
74
76 int getInitNgrid(){return Ngrid_init;}
78 int getNgrid_block(){return Ngrid_block;}
80 double getInitRange(){return i_tree->getTop()->boundary_p2[0] - i_tree->getTop()->boundary_p1[0];}
81 Point_2d getInitCenter();
82 Point * RefineLeaf(LensHndl lens,Point *point);
83 Point * RefineLeaves(LensHndl lens,std::vector<Point *>& points);
84 void ClearAllMarks();
85
86 //void test_mag_matrix();
87 template <typename T>
88 void writeFits(const double center[],size_t Npixels,double resolution,LensingVariable lensvar,std::string filename);
89 template <typename T>
90 void writeFits(const double center[],size_t Nx,size_t Ny,double resolution,LensingVariable lensvar,std::string filename);
91
93 template <typename T>
94 void writeFits(
95 double strech
96 ,LensingVariable lensvar
97 ,std::string filename
98 );
99 template <typename T>
100 void writePixelFits(size_t Nx
101 ,LensingVariable lensvar
102 ,std::string filename
103 );
104 template <typename T>
105 void writeFitsVector(const double center[],size_t Npixels,double resolution,LensingVariable lensvar,std::string filename);
106 template <typename T>
107 PixelMap<T> writePixelMap(const double center[],size_t Npixels,double resolution,LensingVariable lensvar);
108 template <typename T>
109 PixelMap<T> writePixelMap(const double center[],size_t Nx,size_t Ny,double resolution,LensingVariable lensvar);
110
112 template <typename T>
114
116 template<typename T>
118 map.Clean();
119 map.AddGridBrightness(*this);
120 }
122 template <typename T>
123 PixelMap<T> MapSurfaceBrightness(double resolution);
124
125 template <typename T>
126 PixelMap<T> writePixelMapUniform(const PosType center[],size_t Nx,size_t Ny,LensingVariable lensvar);
127 template<typename T>
129
130 template <typename T>
131 void writeFitsUniform(const PosType center[],size_t Nx,size_t Ny,LensingVariable lensvar,std::string filename);
132
133 void find_images(
134 PosType *y_source
135 ,PosType r_source
136 ,int &Nimages
137 ,std::vector<ImageInfo> &imageinfo
138 ,unsigned long &Nimagepoints
139 );
140
141 void map_images(
142 Lens* lens
143 ,Source *source
144 ,int *Nimages
145 ,std::vector<ImageInfo> &imageinfo
146 ,PosType xmax
147 ,PosType xmin
148 ,PosType initial_size
149 ,ExitCriterion criterion
150 ,bool FindCenter
151 ,bool divide_images
152 );
153
154 Grid(Grid &&grid){
155 *this = std::move(grid);
156 }
157
158 Grid operator=(Grid &grid) = delete;
159 Grid(Grid &grid) = delete;
160
161 Grid & operator=(Grid &&grid){
162 assert(&grid != this);
163
164 i_tree = grid.i_tree;
165 grid.i_tree = nullptr;
166 s_tree = grid.s_tree;
167 grid.s_tree = nullptr;
168 neighbors = grid.neighbors;
169 grid.neighbors = nullptr;
170 //trashkist = grid.trashkist;
171 //grid.trashkist = nullptr;
172
173 Ngrid_init = grid.Ngrid_init;
174 Ngrid_init2 = grid.Ngrid_init2;
175 Ngrid_block = grid.Ngrid_block;
176 initialized = grid.initialized;
177 maglimit = grid.maglimit;
178 pointID = grid.pointID;
179 axisratio = grid.axisratio;
180
181 point_factory.clear();
182 point_factory=std::move(grid.point_factory);
183
184 return *this;
185 }
186
188 PosType magnification(double sblimit=-1.0e12) const;
189 PosType UnlensedFlux(double sblimit=-1.0e12) const;
190 PosType LensedFlux(double sblimit=-1.0e12) const;
191
192 //PosType magnification2() const;
193 //PosType magnification3() const;
195 Point_2d centroid() const;
196
197 private:
198 void xygridpoints(Point *points,double range,const double *center,long Ngrid
199 ,short remove_center);
200
202 int Ngrid_init;
203 int Ngrid_init2;
204
206 int Ngrid_block;
207 bool initialized;
208 //Kist<Point> * trashkist;
209
210 double maglimit;
211 Kist<Point> * neighbors;
212 bool find_mag_matrix(double *a,Point *p0,Point *p1,Point *p2);
213
214 bool uniform_mag_from_deflect(double *a,Point *point);
215 //bool uniform_mag_from_shooter(double *a,Point *point);
216
217 double mag_from_deflect(Point *point) const;
218
219 unsigned long pointID;
220 PosType axisratio;
221
222 template <typename T>
223 void writePixelMapUniform_(Point *head,size_t N
224 ,PixelMap<T> *map,LensingVariable val);
225
226 // cluge to make compatible with old method of producing points
227 Point * NewPointArray(size_t N){
228 Point * p = point_factory(N);
229 p[0].head = N;
230 for(size_t i=1; i < N ; ++i) p[i].head = 0;
231 return p;
232 }
233 MemmoryBank<Point> point_factory;
234 static std::mutex grid_mutex;
235};
236
237typedef struct Grid* GridHndl;
238
240std::string to_string(CritType crit);
241
242// in image_finder_kist.c
243namespace ImageFinding{
244
245 struct CriticalCurve{
246
247 CriticalCurve(){
248 critical_center[0] = critical_center[1] = 0.0;
249 caustic_center[0] = caustic_center[1] = 0.0;
250 critical_area = 0.0;
251 caustic_area = 0.0;
252 contour_ell = 0.0;
253 ellipse_area = 0.0;
254 z_source = 0.0;
255 type = CritType::ND;
256 caustic_intersections = -1;
257 touches_edge = false;
258 };
259
260 CriticalCurve(const CriticalCurve &p){
261 critcurve = p.critcurve;
262 //critical_curve = p.critical_curve;
263 caustic_curve_outline = p.caustic_curve_outline;
264 caustic_curve_intersecting = p.caustic_curve_intersecting;
265 critical_center = p.critical_center;
266 caustic_center = p.caustic_center;
267 critical_area = p.critical_area;
268 caustic_area = p.caustic_area;
269 ellipse_curve = p.ellipse_curve;
270 contour_ell = p.contour_ell;
271 ellipse_area = p.ellipse_area;
272 z_source = p.z_source;
273 type = p.type;
274 caustic_intersections = p.caustic_intersections;
275 touches_edge = p.touches_edge;
276 }
277
278 CriticalCurve & operator=(const CriticalCurve &p){
279 if(this == &p) return *this;
280
281 critcurve = p.critcurve;
282 //critical_curve = p.critical_curve;
283 caustic_curve_outline = p.caustic_curve_outline;
284 caustic_curve_intersecting = p.caustic_curve_intersecting;
285 critical_center = p.critical_center;
286 caustic_center = p.caustic_center;
287 critical_area = p.critical_area;
288 caustic_area = p.caustic_area;
289 ellipse_curve = p.ellipse_curve;
290 contour_ell = p.contour_ell;
291 ellipse_area = p.ellipse_area;
292 z_source = p.z_source;
293 type = p.type;
294 caustic_intersections = p.caustic_intersections;
295 touches_edge = p.touches_edge;
296 return *this;
297 }
298
299 std::vector<RAY> critcurve;
300 std::vector<Point_2d> caustic_curve_outline;
301 std::vector<Point_2d> caustic_curve_intersecting;
302 std::vector<Point_2d> ellipse_curve;
303
304 PosType z_source;
306 CritType type;
308 int caustic_intersections;
309
311 Point_2d critical_center;
313 Point_2d caustic_center;
314
316 PosType critical_area;
318 PosType caustic_area;
319
321 PosType contour_ell;
323 PosType ellipse_area;
324
326 bool touches_edge;
327
329
330 bool inCausticCurve(Point_2d &x){
331 return Utilities::inhull(x.x,caustic_curve_outline);
332 }
333
335 bool intersectingCausticCurve(Point_2d &x,double r){
336 return Utilities::circleIntersetsCurve(x, r, caustic_curve_outline);
337 }
338
339
341 bool inCriticalCurve(Point_2d &x){
342 return Utilities::inhull<RAY>(x.x,critcurve);
343 }
344
346 bool EntirelyinCausticCurve(Point_2d &x, PosType sourceRadius)
347 {
348 // Testing if the center of the source is within the caustic :
349 bool IsInCurve = Utilities::inCurve(x,caustic_curve_outline);
350
351 // Testing now that it is not too close from it (i.e. farther than source radius) :
352 int i=0; // index going over the different points of the caustic curve
353 PosType DistSourceToCautic; // distance between the source center and the considered point of the caustic line.
354
355 if(IsInCurve == true) // center inside the caustic
356 {
357 while(i<caustic_curve_outline.size()) // testing that the source size does not overlap with the caustic line.
358 {
359 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]));
360 if (DistSourceToCautic < sourceRadius) return false ; // source too close, we return false and don't consider the point.
361 i++;
362 }
363 return true ; // if not we return true (the source is valid)
364 }
365 else return false ; // center not inside the caustic
366 }
367
369 bool EntirelyinCriticalCurve(Point_2d x, PosType sourceRadius)
370 {
371 // Testing if the center of the source is within the critical curve :
372 bool IsInCurve = Utilities::inCurve(x,caustic_curve_outline);
373
374 // Testing now that it is not too close from it (i.e. farther than source radius) :
375 int i=0; // index going over the different points of the critical curve
376 PosType DistSourceToCritCurve; // distance between the source center and the considered point of the critical line.
377
378 if(IsInCurve == true) // center inside the critical line
379 {
380 while(i<critcurve.size()) // testing that the source size does not overlap with the critical line.
381 {
382 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]));
383 if (DistSourceToCritCurve < sourceRadius) return false ; // source too close, we return false and don't consider the point.
384 i++;
385 }
386 return true ; // if not we return true (the source is valid)
387 }
388 else return false ; // center not inside the critical line
389 }
390
391
394 void RandomSourcesWithinCaustic(
395 int N
396 ,std::vector<Point_2d> &y
398 );
399
400 Point_2d RandomSourceWithinCaustic(
402 );
403
406 void RandomSourcesNearCaustic(double R
407 ,int N
408 ,std::vector<Point_2d> &y
410 );
413 Point_2d RandomSourceNearCaustic(double R
415 );
416
417
420 void RandomSourceStrictlyWithinCaustic(int N
421 ,std::vector<Point_2d> &y
423 ,PosType sourceRadius
424 ,PosType distSourceToCaustic
425 );
427 void CritRange(Point_2d &p1,Point_2d &p2);
429 void CausticRange(Point_2d &p1,Point_2d &p2);
430
432 void CriticalRadius(PosType &rmax,PosType &rmin,PosType &rave) const{
433 if(critcurve.size() < 2){
434 rave = rmax = rmin = 0.0;
435 return;
436 }
437
438 rave = rmin = rmax = (critical_center - critcurve[0].x).length();
439 PosType rad;
440
441 for(size_t ii=1; ii< critcurve.size(); ++ii){
442 rad = (critical_center - critcurve[ii].x).length();
443
444 rave += rad;
445 if(rad < rmin) rmin = rad;
446 if(rad > rmax) rmax = rad;
447 }
448
449 rave /= critcurve.size();
450 }
452 void CausticRadius(PosType &rmax,PosType &rmin,PosType &rave) const{
453 if(caustic_curve_outline.size() < 2){
454 rave = rmax = rmin = 0.0;
455 return;
456 }
457
458 rave = rmin = rmax = (caustic_center - caustic_curve_outline[0]).length();
459
460 PosType rad;
461
462 for(size_t ii=1; ii< caustic_curve_outline.size(); ++ii){
463 rad = (caustic_center - caustic_curve_outline[ii]).length();
464
465 rave += rad;
466 if(rad < rmin) rmin = rad;
467 if(rad > rmax) rmax = rad;
468 }
469
470 rave /= caustic_curve_outline.size();
471 }
472
474 double AreaNearCaustic(double R
475 );
476
477 private:
478 Point_2d p1,p2;
479 };
480
481 void find_images_kist(LensHndl lens,PosType *y_source,PosType r_source,GridHndl grid
482 ,int *Nimages,std::vector<ImageInfo> &imageinfo,unsigned long *Nimagepoints
483 ,PosType initial_size,bool splitimages,short edge_refinement
484 ,bool verbose = false);
485
486 //void find_image_simple(LensHndl lens,Point_2d y_source,PosType z_source,Point_2d &image_x
487 // ,PosType xtol2,PosType &fret);
488
489 void find_images_microlens(LensHndl lens,double *y_source,double r_source,GridHndl grid
490 ,int *Nimages,std::vector<ImageInfo> &imageinfo,unsigned long *Nimagepoints
491 ,double initial_size,double mu_min,bool splitimages,short edge_refinement
492 ,bool verbose);
493
494 void find_images_microlens_exper(LensHndl lens,PosType *y_source,PosType r_source
495 ,GridHndl grid,int *Nimages,std::vector<ImageInfo> &imageinfo,unsigned long *Nimagepoints,PosType initial_size ,PosType mu_min
496 ,bool splitimages,short edge_refinement,bool verbose);
497
498 void image_finder_kist(LensHndl lens, PosType *y_source,PosType r_source,GridHndl grid
499 ,int *Nimages,std::vector<ImageInfo> &imageinfo,unsigned long *Nimagepoints
500 ,short splitparities,short true_images);
501
502
503 void find_crit(LensHndl lens,GridHndl grid,std::vector<CriticalCurve> &crtcurve,int *Ncrits
504 ,double resolution,double invmag_min = 0.0,bool verbose = false,bool test=false);
505 void find_crit(Lens &lens,GridMap &gridmap,std::vector<CriticalCurve> &crtcurves,bool verbose = false);
506
507 // finds the contours of magnification and source plane curve
508 void find_magnification_contour(
509 Lens &lens
510 ,GridMap &gridmap
511 ,double invmag
512 ,std::vector<std::vector<RAY> > &contour
513 ,std::vector<bool> &hits_boundary
514 );
515
516 //void find_crit2(LensHndl lens,GridHndl grid,std::vector<CriticalCurve> &critcurve,int *Ncrits
517 // ,double resolution,bool *orderingsuccess,bool ordercurve,bool dividecurves,double invmag_min = 0.0,bool verbose = false);
518
519 CritType find_pseudo(ImageInfo &pseudocurve,ImageInfo &negimage
520 ,PosType pseudolimit,LensHndl lens,GridHndl grid
521 ,PosType resolution,Kist<Point> &paritypoints,bool TEST=false);
522
523 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);
524
525 namespace Temporary{
526 //PosType *y;
527 //Lens * lens;
528 //Point *point;
529
530 PosType mindyfunc(PosType *x);
531 }
532
533 namespace IF_routines{
534 int refine_grid_kist(LensHndl lens,GridHndl grid,ImageInfo *imageinfo
535 ,int Nimages,double res_target,short criterion
536 ,Kist<Point> * newpointkist = NULL,bool batch=true);
537
538
539 void refine_crit_in_image(LensHndl lens,GridHndl grid,double r_source,double x_source[],double resolution);
540
541 int refine_grid(LensHndl lens,GridHndl grid,OldImageInfo *imageinfo
542 ,unsigned long Nimages,double res_target,short criterion,bool batch=true);
543
544 long refine_edges(LensHndl lens,GridHndl grid,ImageInfo *imageinfo
545 ,int Nimages,double res_target,short criterion
546 ,Kist<Point> * newpointkist = NULL,bool batch=true);
547
548 long refine_edges2(LensHndl lens,double *y_source,double r_source,GridHndl grid
549 ,ImageInfo *imageinfo,bool *image_overlap,int Nimages,double res_target
550 ,short criterion,bool batch=true);
551
552 void sort_out_points(Point *i_points,ImageInfo *imageinfo,double r_source,double y_source[]);
553
554 }
555
556 void printCriticalCurves(std::string filename
557 ,const std::vector<ImageFinding::CriticalCurve> &critcurves);
558
562 template<typename T>
565 const std::vector<ImageFinding::CriticalCurve> &critcurves,
567 int Nx
568 );
569
573 template<typename T>
574 PixelMap<T> mapCausticCurves(const std::vector<ImageFinding::CriticalCurve> &critcurves
575 ,int Nx
576 );
577}
578
579
580std::ostream &operator<<(std::ostream &os, const ImageFinding::CriticalCurve &p);
581
582//void saveImage(LensHaloMassMap *mokahalo, GridHndl grid, bool saveprofile=true);
583
584
586template <typename T>
588 const PosType center[]
589 ,size_t Npixels
590 ,PosType resolution
591 ,LensingVariable lensvar
592 ,std::string filename
593 ){
594 writeFits<T>(center,Npixels,Npixels,resolution,lensvar,filename);
595}
597template <typename T>
599 const PosType center[]
600 ,size_t Nx
601 ,size_t Ny
602 ,PosType resolution
603 ,LensingVariable lensvar
604 ,std::string filename
605 ){
606 PixelMap<T> map(center, Nx,Ny, resolution);
607 std::string tag;
608
609 switch (lensvar) {
611 tag = ".dt.fits";
612 break;
614 tag = ".alpha1.fits";
615 break;
617 tag = ".alpha2.fits";
618 break;
620 tag = ".alpha.fits";
621 break;
623 tag = ".kappa.fits";
624 break;
626 tag = ".gamma1.fits";
627 break;
629 tag = ".gamma2.fits";
630 break;
632 tag = ".gamma3.fits";
633 break;
635 tag = ".gamma.fits";
636 break;
638 tag = ".invmag.fits";
639 break;
641 tag = ".surfbright.fits";
642 break;
643 default:
644 break;
645 }
646
647 map.AddGrid(*this,lensvar);
648 map.printFITS(filename + tag);
649
650 return;
651}
652
654template <typename T>
656 const PosType center[]
657 ,size_t Npixels
658 ,PosType resolution
659 ,LensingVariable lensvar
660 ){
661
662 return writePixelMap<T>(center,Npixels,Npixels,resolution,lensvar);
663}
665template <typename T>
667 const PosType center[]
668 ,size_t Nx
669 ,size_t Ny
670 ,PosType resolution
671 ,LensingVariable lensvar
672){
673 PixelMap<T> map(center, Nx, Ny, resolution);
674 map.AddGrid(*this,lensvar);
675
676 return map;
677}
679template <typename T>
681 LensingVariable lensvar
682){
683
684 Branch *branch = i_tree->getTop();
685 double resolution = (branch->boundary_p2[0] - branch->boundary_p1[0])/Ngrid_init;
686 PixelMap<T> map(branch->center, Ngrid_init, Ngrid_init2, resolution);
687 map.AddGrid(*this,lensvar);
688
689 return map;
690}
691
692template <typename T>
694 Branch *branch = i_tree->getTop();
695 int Nx = (int)( (branch->boundary_p2[0] - branch->boundary_p1[0])/resolution );
696 int Ny = (int)( (branch->boundary_p2[1] - branch->boundary_p1[1])/resolution );
697
698 PixelMap<T> map(branch->center,Nx,Ny,resolution);
699 map.AddGridBrightness(*this);
700
701 return map;
702}
703
706template <typename T>
708 size_t Nx
709 ,LensingVariable lensvar
710 ,std::string filename
711){
712
713 Point_2d center = getInitCenter();
714 PosType resolution = getInitRange()/Nx;
715 size_t Ny = (size_t)(Nx*axisratio);
716 writeFits<T>(center.x,Nx,Ny,resolution, lensvar, filename);
717
718 return;
719}
720
728template <typename T>
730 const PosType center[]
731 ,size_t Nx
732 ,size_t Ny
733 ,LensingVariable lensvar
734 ,std::string filename
735 ){
736 std::string tag;
737
738 switch (lensvar) {
740 tag = ".dt.fits";
741 break;
743 tag = ".alpha1.fits";
744 break;
746 tag = ".alpha2.fits";
747 break;
749 tag = ".alpha.fits";
750 break;
752 tag = ".kappa.fits";
753 break;
755 tag = ".gamma1.fits";
756 break;
758 tag = ".gamma2.fits";
759 break;
761 tag = ".gamma3.fits";
762 break;
764 tag = ".gamma.fits";
765 break;
767 tag = ".invmag.fits";
768 break;
770 tag = ".surfbright.fits";
771 break;
772 default:
773 break;
774 }
775
776 PixelMap<T> map = writePixelMapUniform<T>(center,Nx,Ny,lensvar);
777 map.printFITS(filename + tag);
778}
779
787template <typename T>
789 const PosType center[]
790 ,size_t Nx
791 ,size_t Ny
792 ,LensingVariable lensvar
793 ){
794
795 if(getNumberOfPoints() ==0 ) return PixelMap<T>();
796 PixelMap<T> map(center, Nx, Ny,i_tree->pointlist.Top()->gridsize);
797 map.Clean();
798
799 //int Nblocks = Utilities::GetNThreads();
800 int Nblocks = 16;
801
802 //std::vector<PointList> lists(Nblocks);
803
804 std::vector<Point *> heads(Nblocks);
805 std::vector<size_t> sizes(Nblocks,0);
806
807 bool allowDecent;
808 TreeStruct::iterator i_tree_it(i_tree);
809 int i = 0;
810
811 do{
812 if((*i_tree_it)->level == 4){
813
814 heads[i] = (*i_tree_it)->points;
815 sizes[i] = (*i_tree_it)->npoints;
816
817 //lists[i].setTop( (*i_tree_it)->points );
818 //lists[i].setN( (*i_tree_it)->npoints );
819 ++i;
820 allowDecent = false;
821 }else{
822 allowDecent = true;
823 }
824 }while(i_tree_it.TreeWalkStep(allowDecent) && i < Nblocks);
825
826 std::vector<std::thread> thrs;
827 for(int ii = 0; ii < i ;++ii){
828 //writePixelMapUniform_(heads[ii],sizes[ii],&map,lensvar);
829 //thrs.push_back(std::thread(&Grid::writePixelMapUniform_,this,lists[ii],&map,lensvar));
830
831 thrs.push_back(std::thread(&Grid::writePixelMapUniform_<T>,this,heads[ii],sizes[ii],&map,lensvar));
832 }
833 for(int ii = 0; ii < i ;++ii) thrs[ii].join();
834
835 return map;
836}
837
838template <typename T>
840 PixelMap<T> &map
841 ,LensingVariable lensvar
842 ){
843
844 if(getNumberOfPoints() ==0 ) return;
845
846 map.Clean();
847 int Nblocks = 16;
848 //std::vector<PointList> lists(Nblocks);
849 TreeStruct::iterator i_tree_it(i_tree);
850
851 std::vector<Point *> heads(Nblocks);
852 std::vector<size_t> sizes(Nblocks,0);
853
854
855 bool allowDecent;
856 i_tree_it.movetop();
857 int i = 0;
858 do{
859 if((*i_tree_it)->level == 4){
860 assert(i < 16);
861 //lists[i].setTop( (*i_tree_it)->points );
862 //lists[i].setN( (*i_tree_it)->npoints );
863 heads[i] = (*i_tree_it)->points;
864 sizes[i] = (*i_tree_it)->npoints;
865
866 ++i;
867 allowDecent = false;
868 }else{
869 allowDecent = true;
870 }
871 }while(i_tree_it.TreeWalkStep(allowDecent) && i < Nblocks);
872
873 std::vector<std::thread> thr;
874 for(int ii = 0; ii < i ;++ii){
875 thr.push_back(std::thread(&Grid::writePixelMapUniform_<T>,this,heads[ii],sizes[ii],&map,lensvar));
876 }
877 for(auto &t : thr) t.join();
878}
879
880template <typename T>
881void Grid::writePixelMapUniform_(Point *head,size_t N,PixelMap<T> *map,LensingVariable val){
882 double tmp;
883 PosType tmp2[2];
884 long index;
885
886 Point *ppoint = head;
887
888 for(size_t i = 0; i< N; ++i){
889
890 switch (val) {
892 tmp2[0] = ppoint->x[0] - ppoint->image->x[0];
893 tmp2[1] = ppoint->x[1] - ppoint->image->x[1];
894 tmp = sqrt(tmp2[0]*tmp2[0] + tmp2[1]*tmp2[1]);
895 break;
897 tmp = (ppoint->x[0] - ppoint->image->x[0]);
898 break;
900 tmp = (ppoint->x[1] - ppoint->image->x[1]);
901 break;
903 tmp = ppoint->kappa();
904 break;
906 tmp2[0] = ppoint->gamma1();
907 tmp2[1] = ppoint->gamma2();
908 tmp = sqrt(tmp2[0]*tmp2[0] + tmp2[1]*tmp2[1]);
909 break;
911 tmp = ppoint->gamma1();
912 break;
914 tmp = ppoint->gamma2();
915 break;
917 tmp = ppoint->gamma3();
918 break;
920 tmp = ppoint->invmag();
921 break;
923 tmp = ppoint->dt;
924 break;
926 tmp = ppoint->surface_brightness;
927 break;
928 default:
929 std::cerr << "PixelMap<T>::AddGrid() does not work for the input LensingVariable" << std::endl;
930 throw std::runtime_error("PixelMap<T>::AddGrid() does not work for the input LensingVariable");
931 break;
932 // If this list is to be expanded to include ALPHA or GAMMA take care to add them as vectors
933 }
934
935 index = map->find_index(ppoint->x);
936 if(index != -1)(*map)[index] = tmp;
937
938 ppoint = ppoint->next;
939 }
940}
941
943template <typename T>
945 const PosType center[]
946 ,size_t Npixels
947 ,PosType resolution
948 ,LensingVariable lensvar
949 ,std::string filename
950 ){
951 //throw std::runtime_error("Not done yet!");
952
953 PosType range = Npixels*resolution,tmp_x[2];
954 ImageInfo tmp_image,tmp_image_theta;
955 size_t i;
956 std::string tag;
957
958 i_tree->PointsWithinKist(center,range/sqrt(2.),tmp_image.imagekist,0);
959 i_tree->PointsWithinKist(center,range/sqrt(2.),tmp_image_theta.imagekist,0);
960
961 std::vector<PosType> tmp_sb_vec(tmp_image.imagekist->Nunits());
962
963 for(tmp_image.imagekist->MoveToTop(),i=0;i<tmp_sb_vec.size();++i,tmp_image.imagekist->Down()){
964 tmp_sb_vec[i] = tmp_image.imagekist->getCurrent()->surface_brightness;
965 switch (lensvar) {
967 tmp_x[0] = tmp_image.imagekist->getCurrent()->x[0]
968 - tmp_image.imagekist->getCurrent()->image->x[0];
969
970 tmp_x[1] = tmp_image.imagekist->getCurrent()->x[1]
971 - tmp_image.imagekist->getCurrent()->image->x[1];
972
973 tmp_image.imagekist->getCurrent()->surface_brightness = sqrt( tmp_x[0]*tmp_x[0] + tmp_x[1]*tmp_x[1]);
974 tmp_image_theta.imagekist->getCurrent()->surface_brightness = atan2(tmp_x[1],tmp_x[0]);
975
976 tag = ".alphaV.fits";
977 break;
979
980 tmp_x[0] = tmp_image.imagekist->getCurrent()->gamma1();
981 tmp_x[1] = tmp_image.imagekist->getCurrent()->gamma2();
982
983 tmp_image.imagekist->getCurrent()->surface_brightness = sqrt( tmp_x[0]*tmp_x[0] + tmp_x[1]*tmp_x[1]);
984 tmp_image_theta.imagekist->getCurrent()->surface_brightness = atan2(tmp_x[1],tmp_x[0])/2;
985
986 tag = ".gammaV.fits";
987 break;
988 default:
989 std::cout << "Grid::writeFitsVector() does not support the LensVariable you are using." << std::endl;
990 return;
991 }
992 }
993
994 PixelMap<T> map_m(center, Npixels, resolution),map_t(center,Npixels,resolution);
995
996 map_m.Clean();
997 map_m.AddImages(&tmp_image,1,-1);
998 map_m = PixelMap<T>(map_m,4);
999 map_m = PixelMap<T>(map_m,1/4.);
1000
1001 map_t.Clean();
1002 map_t.AddImages(&tmp_image_theta,1,-1);
1003
1004 map_m.printFITS(filename + tag);
1005
1006 for(tmp_image.imagekist->MoveToTop(),i=0;i<tmp_sb_vec.size();++i,tmp_image.imagekist->Down())
1007 tmp_image.imagekist->getCurrent()->surface_brightness = tmp_sb_vec[i];
1008}
1009
1010template <typename T>
1011void Grid::writeFits(double strech,LensingVariable lensvar ,std::string filename){
1012 Point_2d center = getInitCenter();
1013 size_t N1 = (size_t)(strech*Ngrid_init);
1014 size_t N2 = (size_t)(strech*Ngrid_init2);
1015
1016 writeFits<T>(center.x,N1,N2,getInitRange()/N1,lensvar,filename);
1017}
1018
1019
1020
1021#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:682
void printFITS(std::string filename, bool Xflip=false, bool verbose=false)
Output the pixel map as a fits file.
Definition pixelmap.cpp:1309
void AddImages(ImageInfo *imageinfo, int Nimages, float rescale=1.)
Add an image to the map.
Definition pixelmap.cpp:638
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:2215
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:1924
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:243
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:1125
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:1915
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:1122
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:3023
LensingVariable
output lensing variables
Definition standard.h:89
@ ALPHA
magnitude of deflection in radians
@ GAMMA2
second component of shear
@ DELAYT
time delay
@ GAMMA3
third component of shear
@ ALPHA2
y component of deflection
@ SurfBrightness
Surface brightness.
@ GAMMA1
first component of shear
@ GAMMA
magnitude of shear
@ KAPPA
convergence
@ ALPHA1
x component of deflection
@ INVMAG
inverse of magnification
The box representing a branch of a binary tree structure. Used specifically in TreeStruct for organiz...
Definition point.h:628
Structure to contain both source and image trees.
Definition grid_maintenance.h:25
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:655
int getInitNgrid()
return initial number of grid points in each direction
Definition grid_maintenance.h:76
~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:729
void MapSurfaceBrightness(PixelMap< T > &map)
make image of surface brightness
Definition grid_maintenance.h:117
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:587
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:80
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:73
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:71
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:707
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:944
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:788
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:78
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
Definition point.h:670
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:414
Tree: Exported struct.
Definition Tree.h:31
PointList pointlist
list of points
Definition Tree.h:125
PosType PointsWithinKist(const PosType *center, PosType rmax, Kist< Point > *neighborkist, short markpoints) const
Finds all points in tree that lie within rmax of the point ray[].
Definition KistDriver.cpp:263
Branch * getTop()
root branch
Definition Tree.h:122