GLAMERDOC++
Gravitational Lensing Code Library
Loading...
Searching...
No Matches
gridmap.h
1//
2// gridmap.h
3// GLAMER
4//
5// Created by bmetcalf on 8/19/14.
6//
7//
8
9#ifndef __GLAMER__gridmap__
10#define __GLAMER__gridmap__
11
12#include <iostream>
13#include <memory>
14#include "lens.h"
15#include "point.h"
16#include "concave_hull.h"
17#include "Tree.h"
18#include "source.h"
19#include <mutex>
20
31struct GridMap{
32
33 friend class Lens;
34
35 GridMap(LensHndl lens,unsigned long N1d,const double center[2],double range);
36 GridMap(LensHndl lens ,unsigned long Nx ,const PosType center[2] ,PosType rangeX ,PosType rangeY);
38 GridMap(unsigned long N1d,const double center[2],double range);
39 ~GridMap();
40
43
45 void deLens();
55 double RefreshSurfaceBrightnesses(Source* source);
62
69 double AddSurfaceBrightnesses(Source* source);
70
72 Point_2d image_point(size_t index){return i_points[index];}
74 Point_2d source_point(size_t index){return s_points[index];}
75
76 void ClearSurfaceBrightnesses();
77 void assertNAN(); // check for nan in surface prightness
78 size_t getNumberOfPoints() const {return Ngrid_init*Ngrid_init2;}
79
81 int getInitNgrid() const {return Ngrid_init;}
83 double getXRange() const {return x_range;}
84 double getYRange() const {return x_range*axisratio;}
86 double getResolution() const {return x_range/(Ngrid_init-1);}
87
89 template<typename T>
92 template <typename T>
93 void writeFits(LensingVariable lensvar,std::string filensame);
94
95 template<typename T>
97 template <typename T>
98 void writeFitsUniform(const PosType center[],size_t Nx,size_t Ny,LensingVariable lensvar,std::string filename);
99 template<typename T>
100 PixelMap<T> writePixelMapUniform(const PosType center[],size_t Nx,size_t Ny,LensingVariable lensvar);
101
103 template<typename T>
105 LensingVariable lensvar
106 ,std::string filename
107 ){
108 PixelMap<T> map = writePixelMap<T>(lensvar);
109 map.printFITS(filename);
110 }
111
113 template<typename T>
114 PixelMap<T> getPixelMapFlux(int res) const;
115
119 template<typename T>
120 void getPixelMapFlux(PixelMap<T> &map) const;
121
123 PosType EinsteinArea() const;
124
127 //PosType magnification_invmag() const;
128 //PosType magnification2() const;
129
131 Point_2d centroid() const;
132
133 Point_2d getCenter(){return center;}
134
135 Point * operator[](size_t i){return i_points.data() + i;};
136
137 GridMap(GridMap &&grid){
138
139 Ngrid_init = grid.Ngrid_init;
140 Ngrid_init2 = grid.Ngrid_init2;
141 pointID = grid.pointID;
142 axisratio = grid.axisratio;
143 x_range = grid.x_range;
144
145 std::swap(i_points,grid.i_points);
146 std::swap(s_points,grid.s_points);
147
148 center = grid.center;
149 }
150
151 GridMap(GridMap &grid){
152
153 Ngrid_init = grid.Ngrid_init;
154 Ngrid_init2 = grid.Ngrid_init2;
155 pointID = grid.pointID;
156 axisratio = grid.axisratio;
157 x_range = grid.x_range;
158
159 i_points = grid.i_points;
160 s_points = grid.s_points;
161
162 center = grid.center;
163 }
164
165 GridMap & operator=(GridMap &&grid){
166 Ngrid_init = grid.Ngrid_init;
167 Ngrid_init2 = grid.Ngrid_init2;
168 pointID = grid.pointID;
169 axisratio = grid.axisratio;
170 x_range = grid.x_range;
171
172 std::swap(i_points,grid.i_points);
173 std::swap(s_points,grid.s_points);
174
175 center = grid.center;
176
177 return *this;
178 }
179
180 struct Triangle{
181 Triangle(size_t i,size_t j,size_t k){
182 index[0] = i;
183 index[1] = j;
184 index[2] = k;
185 }
186 size_t index[3];
187 size_t & operator[](int i){return index[i];}
188 };
189
190
191 /*** \brief Returns a list of RAYs from a set of source positions.
192
193 The image positions are found in parallel by the triangle method. The order of the
194 output rays will be the same as the sources with multiple images consecutive.
195 */
196 std::list<RAY> find_images(std::vector<Point_2d> &ys
197 ,std::vector<int> &multiplicity
198 ) const;
199
202 void find_images(Point_2d y
203 ,std::vector<Point_2d> &image_points
204 ,std::vector<Triangle> &triangles
205 ) const ;
206
215 void find_boundaries_of_caustics(std::vector<std::vector<Point_2d> > &boundaries
216 ,std::vector<bool> &hits_edge
217 ){
218
219 size_t N=Ngrid_init*Ngrid_init2;
220 std::vector<Point_2d> source_points(N);
221 for(size_t i=0 ; i<N ; ++i) source_points[i] = i_points[i];
222
223 std::vector<int> multiplicities(N);
224 find_images(source_points,multiplicities);
225
226 std::vector<bool> bitmap(N,false);
227 for(size_t i=0 ; i<N ; ++i){
228 if(multiplicities[i] > 1) bitmap[i] = true;
229 }
230
231 Utilities::find_boundaries<Point_2d>(bitmap,Ngrid_init,boundaries,hits_edge);
232 }
233
237 PosType magnificationFlux(Source &source) const ;
238
246 double magnificationTr() const ;
247
250 double magnificationTr(std::vector<size_t> &pixels) const ;
251
253 double AreaCellOnSourcePlane(size_t k) const;
254
255
256// depricated version of PixalMap::magnificationTr()
257// double magnificationTr2() const {
258//
259// size_t k;
260// size_t k1;
261// size_t k2;
262//
263// double flux_source=0.0,flux_image=0.0;
264// for(size_t i=1 ; i< Ngrid_init-1 ; i++){
265// for(size_t j=1 ; j< Ngrid_init2-1 ; j++){
266//
267// k = i + j*Ngrid_init;
268//
269// assert( s_points[k].surface_brightness == i_points[k].surface_brightness);
270// double sb = s_points[k].surface_brightness;
271//
272// if(sb !=0.0){
273// flux_source += AreaCellOnSourcePlane(k) *sb;
274// flux_source += AreaCellOnSourcePlane(k-1) *sb;
275// flux_source += AreaCellOnSourcePlane(k-1-Ngrid_init) *sb;
276// flux_source += AreaCellOnSourcePlane(k-Ngrid_init) *sb;
277// }
278//
279// flux_image += sb;
280// }
281// }
282//
283// return flux_image * getResolution() * getResolution() * 4 / flux_source;
284// }
285
292 double AddPointSource(const Point_2d &y,double flux);
293
299 void find_crit(std::vector<std::vector<Point_2d> > &points
300 ,std::vector<bool> &hits_boundary
301 ,std::vector<CritType> &crit_type
302 );
303
308 std::vector<std::vector<Point_2d> > &curves
309 ,std::vector<bool> &hits_boundary
310 ,double invmag
311 );
312
313// void find_crit_boundary(std::vector<std::vector<Point_2d> > &points
314// ,std::vector<bool> &hits_boundary
315// ) const;
316
317private:
318 GridMap & operator=(GridMap &grid);
319
320 /* Depricated to Utilities::find_boundaries<>()
321
322 finds ordered boundaries to regions where bitmap == true
323
324 This can be used to find critical curves or contours.
325 `bitmap` should be the same size as the `Gridmap`
326 If the boundary curve touches the edge of the `GridMap` it will be indicated in `hits_boundary` as
327 `true`.
328
329 Boundaries will never cross or lead off the grid. On the edges they will leave the edge pixels out even if they should be in. This is a technical comprimise.
330 */
331// void find_boundaries(std::vector<bool> &bitmap // = true inside
332// ,std::vector<std::vector<Point_2d> > &points
333// ,std::vector<bool> &hits_edge
334// ,bool add_to_vector=false
335// );
336//
337
338 // curve must be in pixel units
339 bool incurve(long k,std::vector<Point_2d> &curve) const;
340
341 // cluge to make compatible with old method of producing points
342 std::vector<Point> NewPointArray(size_t N){
343 std::vector<Point> p(N);
344 p[0].head = N;
345 return p;
346 }
347
348 void xygridpoints(Point *points,double range,const double *center,long Ngrid
349 ,short remove_center);
350
352 int Ngrid_init;
353 int Ngrid_init2;
354
355 unsigned long pointID;
356 PosType axisratio;
357 PosType x_range;
358 template<typename T>
359 void writePixelMapUniform_(Point* points,size_t size,PixelMap<T> *map,LensingVariable val);
360
361 std::vector<Point> i_points;
362 std::vector<Point> s_points;
363 Point_2d center;
364
365 bool to_refine(long i,long j,double total,double f) const ;
366 static std::mutex grid_mutex;
367
368 void _find_images_(Point_2d *ys,int *multiplicity,long Nys,std::list<RAY> &rays) const;
369
370 // find if there are images of y in specific cells
371 void limited_image_search(Point_2d &y
372 ,std::vector<size_t> &cell_numbers
373 ,std::vector<Triangle> &triangles
374 ) const;
375};
376
378template<typename T>
380
381 if(resf <=0){
382 ERROR_MESSAGE();
383 throw std::invalid_argument("resf must be > 0");
384 }
385
386 // The number of pixels on a side of the new map will be
387 // N = (Ngrid_init-1)/resf + 1;
388 // so that the resolution is resf x the GridMap resolution
389
390 PixelMap<T> map(center.x,(Ngrid_init-1)/resf + 1 ,(Ngrid_init2-1)/resf + 1,resf*x_range/(Ngrid_init-1));
391
392 int factor = resf*resf;
393 size_t index;
394 size_t n = Ngrid_init*Ngrid_init2;
395 for(size_t i=0 ; i<n ; ++i){
396 index = map.find_index(i_points[i].x);
397 map.data()[index] = i_points[i].surface_brightness/factor;
398 }
399
400 //for(size_t i = 0 ; i < Ngrid_init ; ++i){
401 // for(size_t j = 0 ; j < Ngrid_init2 ; ++j){
402 // map.data()[i/resf + map.getNx() * (j / resf)] +=
403 // i_points[ i + Ngrid_init * j].surface_brightness/factor;
404 // }
405 //}
406
407 map.Renormalize(map.getResolution()*map.getResolution());
408
409 return map;
410}
411
413template<typename T>
415
416 int resf = (Ngrid_init-1)/(map.getNx()-1);
417
418 if(resf*map.getNx() != Ngrid_init-1+resf) throw std::invalid_argument("PixelMap does not match GripMap! Use the other GridMap::getPixelMapFlux() to contruct a PixelMap.");
419 if(resf*map.getNy() != Ngrid_init2-1+resf) throw std::invalid_argument("PixelMap does not match GripMap! Use the other GridMap::getPixelMapFlux() to contruct a PixelMap.");
420 //if(map.getResolution() != x_range*resf/(Ngrid_init-1)) throw std::invalid_argument("PixelMap does not match GripMap resolution! Use the other GridMap::getPixelMapFlux() to contruct a PixelMap.");
421
422 if(map.getCenter()[0] != center[0]) throw std::invalid_argument("PixelMap does not match GripMap!");
423 if(map.getCenter()[1] != center[1]) throw std::invalid_argument("PixelMap does not match GripMap!");
424
425 if(resf <=0){
426 ERROR_MESSAGE();
427 throw std::invalid_argument("resf must be > 0");
428 }
429
430 map.Clean();
431
432 int factor = resf*resf;
433/* size_t index;
434 size_t n = Ngrid_init*Ngrid_init2;
435 std::vector<int> counts(map.size(),0);
436 for(size_t i=0 ; i<n ; ++i){
437 index = map.find_index(i_points[i].x);
438 ++counts[index];
439 map.data()[index] = i_points[i].surface_brightness;
440 }
441 index = map.size();
442 for(size_t i=0 ; i<index ; ++i) map[i] /= counts[i];
443 */
444
445 long nx = map.getNx();
446 for(size_t i = 0 ; i < Ngrid_init ; ++i){
447 size_t ii = i/resf;
448 for(size_t j = 0 ; j < Ngrid_init2 ; ++j){
449 size_t jj = j/resf;
450 map.data()[ii + nx * jj ] +=
451 i_points[ i + Ngrid_init * j].surface_brightness/factor;
452 }
453 }
454
455 map.Renormalize(map.getResolution()*map.getResolution());
456}
457
465template<typename T>
467 const PosType center[]
468 ,size_t Nx
469 ,size_t Ny
470 ,LensingVariable lensvar
471){
472
473 if(getNumberOfPoints() == 0 ) return PixelMap<T>();
474 PixelMap<T> map(center, Nx, Ny,x_range/(Nx-1));
475
476 map.Clean();
477
478 writePixelMapUniform(map,lensvar);
479
480 return map;
481}
482
483template<typename T>
485 LensingVariable lensvar
486 ,std::string filename
487 ){
488 PixelMap<T> map = writePixelMap<T>(lensvar);
489 map.printFITS(filename);
490}
491
492template<typename T>
494 LensingVariable lensvar
495){
496 size_t Nx = Ngrid_init;
497 size_t Ny = Ngrid_init2;
498
499 PixelMap<T> map( center.x, Nx, Ny,x_range/(Nx-1) );
500
501 size_t N = map.size();
502 assert(N == Nx*Ny);
503
504 double tmp2[2];
505 switch (lensvar) {
507 for(size_t i=0 ; i<N ; ++i){
508 tmp2[0] = i_points[i].x[0] - i_points[i].image->x[0];
509 tmp2[1] = i_points[i].x[1] - i_points[i].image->x[1];
510 map[i] = sqrt(tmp2[0]*tmp2[0] + tmp2[1]*tmp2[1]);
511 }
512 break;
514 for(size_t i=0 ; i<N ; ++i)
515 map[i] = (i_points[i].x[0] - i_points[i].image->x[0]);
516 break;
518 for(size_t i=0 ; i<N ; ++i)
519 map[i] = (i_points[i].x[1] - i_points[i].image->x[1]);
520 break;
522 for(size_t i=0 ; i<N ; ++i)
523 map[i] = i_points[i].kappa();
524 break;
526 for(size_t i=0 ; i<N ; ++i){
527 tmp2[0] = i_points[i].gamma1();
528 tmp2[1] = i_points[i].gamma2();
529 map[i] = sqrt(tmp2[0]*tmp2[0] + tmp2[1]*tmp2[1]);
530 }
531 break;
533 for(size_t i=0 ; i<N ; ++i)
534 map[i] = i_points[i].gamma1();
535 break;
537 for(size_t i=0 ; i<N ; ++i)
538 map[i] = i_points[i].gamma2();
539 break;
541 for(size_t i=0 ; i<N ; ++i)
542 map[i] = i_points[i].gamma3();
543 break;
545 for(size_t i=0 ; i<N ; ++i)
546 map[i] = i_points[i].invmag();
547 break;
549 for(size_t i=0 ; i<N ; ++i)
550 map[i] = i_points[i].dt;
551 break;
553 for(size_t i=0 ; i<N ; ++i)
554 map[i] = i_points[i].surface_brightness;
555 break;
556 default:
557 std::cerr << "GridMap::writePixelMapUniform() does not work for the input LensingVariable" << std::endl;
558 throw std::runtime_error("GridMap::writePixelMapUniform() does not work for the input LensingVariable");
559 break;
560 // If this list is to be expanded to include ALPHA or GAMMA take care to add them as vectors
561 }
562 return map;
563}
564template <typename T>
566 PixelMap<T> &map
567 ,LensingVariable lensvar
568){
569
570 if(getNumberOfPoints() ==0 ) return;
571
572 map.Clean();
573
574 //writePixelMapUniform_(i_points,getNumberOfPoints(),&map,lensvar);
575 //return;
576
577 std::vector<std::thread> thr;
578 int nthreads = Utilities::GetNThreads();
579
580 int chunk_size;
581 do{
582 chunk_size = getNumberOfPoints()/nthreads;
583 if(chunk_size == 0) nthreads /= 2;
584 }while(chunk_size == 0);
585
586 size_t size = chunk_size;
587 for(int ii = 0; ii < nthreads ;++ii){
588 if(ii == nthreads-1)
589 size = getNumberOfPoints() - (nthreads-1)*chunk_size;
590 thr.push_back(std::thread(&GridMap::writePixelMapUniform_<T>,this,&(i_points[ii*chunk_size]),size,&map,lensvar));
591 }
592 for(auto &t : thr) t.join();
593}
594
595template <typename T>
596void GridMap::writePixelMapUniform_(Point* points,size_t size,PixelMap<T> *map,LensingVariable val){
597 double tmp;
598 PosType tmp2[2];
599 long index;
600
601 for(size_t i = 0; i< size; ++i){
602 switch (val) {
604 tmp2[0] = points[i].x[0] - points[i].image->x[0];
605 tmp2[1] = points[i].x[1] - points[i].image->x[1];
606 tmp = sqrt(tmp2[0]*tmp2[0] + tmp2[1]*tmp2[1]);
607 break;
609 tmp = (points[i].x[0] - points[i].image->x[0]);
610 break;
612 tmp = (points[i].x[1] - points[i].image->x[1]);
613 break;
615 tmp = points[i].kappa();
616 break;
618 tmp2[0] = points[i].gamma1();
619 tmp2[1] = points[i].gamma2();
620 tmp = sqrt(tmp2[0]*tmp2[0] + tmp2[1]*tmp2[1]);
621 break;
623 tmp = points[i].gamma1();
624 break;
626 tmp = points[i].gamma2();
627 break;
629 tmp = points[i].gamma3();
630 break;
632 tmp = points[i].invmag();
633 break;
635 tmp = points[i].dt;
636 break;
638 tmp = points[i].surface_brightness;
639 break;
640 default:
641 std::cerr << "PixelMap<T>::AddGrid() does not work for the input LensingVariable" << std::endl;
642 throw std::runtime_error("PixelMap<T>::AddGrid() does not work for the input LensingVariable");
643 break;
644 // If this list is to be expanded to include ALPHA or GAMMA take care to add them as vectors
645 }
646
647 index = map->find_index(points[i].x);
648 if(index != -1)(*map)[index] = tmp;
649 }
650}
651
652template <typename T>
654 const PosType center[]
655 ,size_t Nx
656 ,size_t Ny
657 ,LensingVariable lensvar
658 ,std::string filename
659){
660 std::string tag;
661
662 switch (lensvar) {
664 tag = ".dt.fits";
665 break;
667 tag = ".alpha1.fits";
668 break;
670 tag = ".alpha2.fits";
671 break;
673 tag = ".alpha.fits";
674 break;
676 tag = ".kappa.fits";
677 break;
679 tag = ".gamma1.fits";
680 break;
682 tag = ".gamma2.fits";
683 break;
685 tag = ".gamma3.fits";
686 break;
688 tag = ".gamma.fits";
689 break;
691 tag = ".invmag.fits";
692 break;
694 tag = ".surfbright.fits";
695 break;
696 default:
697 break;
698 }
699
700 PixelMap<T> map = writePixelMapUniform<T>(center,Nx,Ny,lensvar);
701 map.printFITS(filename + tag);
702}
703
704#endif // defined(__GLAMER__gridmap__)
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 printFITS(std::string filename, bool Xflip=false, bool verbose=false)
Output the pixel map as a fits file.
Definition pixelmap.cpp:1309
void Renormalize(T factor)
Multiplies the whole map by a scalar factor.
Definition pixelmap.cpp:506
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
Base class for all sources.
Definition source.h:44
void find_boundaries(std::vector< bool > &bitmap, long nx, std::vector< std::vector< P > > &points, std::vector< bool > &hits_edge, bool add_to_vector=false, bool outer_only=false)
finds ordered boundaries to regions where bitmap == true
Definition concave_hull.h:201
int GetNThreads()
returns the compiler variable N_THREADS that is maximum number of threads to be used.
Definition utilities.cpp:553
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
A simplified version of the Grid structure for making non-adaptive maps of the lensing quantities (ka...
Definition gridmap.h:31
double AddSurfaceBrightnesses(Source *source)
Recalculate surface brightness just like GridMap::RefreshSurfaceBrightness but the new source is adde...
Definition gridmap.cpp:256
Point_2d image_point(size_t index)
get the image point for a index number
Definition gridmap.h:72
PosType EinsteinArea() const
returns the area (radians^2) of the region with negative magnification at resolution of fixed grid
Definition gridmap.cpp:319
PixelMap< T > getPixelMapFlux(int res) const
returns a PixelMap with the flux in pixels at a resolution of res times the original resolution
Definition gridmap.h:379
double getXRange() const
return initial range of gridded region. This is the distance from the first ray in a row to the last ...
Definition gridmap.h:83
double RefreshSurfaceBrightnesses(Source *source)
Recalculate surface brightness at every point without changing the positions of the gridmap or any le...
Definition gridmap.cpp:170
double AddPointSource(const Point_2d &y, double flux)
add flux to the rays that are nearest to the source on the source plane for each image
Definition gridmap.cpp:462
Point_2d source_point(size_t index)
get the image point for a index number
Definition gridmap.h:74
void find_magnification_contour(std::vector< std::vector< Point_2d > > &curves, std::vector< bool > &hits_boundary, double invmag)
Find image-plane contours of magnification. This is usually only used within ImageFinding:: functio...
Definition gridmap.cpp:490
PixelMap< T > writePixelMap(LensingVariable lensvar)
make pixel map of lensing quantities at the resolution of the GridMap
Definition gridmap.h:493
void writeFitsUniform(const PosType center[], size_t Nx, size_t Ny, LensingVariable lensvar, std::string filename)
Definition gridmap.h:653
void writeFitsUniform(LensingVariable lensvar, std::string filename)
this will make a fits map of the grid as is.
Definition gridmap.h:104
double getResolution() const
resolution in radians, this is range / (N-1)
Definition gridmap.h:86
GridMap ReInitialize(LensHndl lens)
reshoot the rays for example when the source plane has been changed
Definition gridmap.cpp:144
double AdaptiveRefreshSurfaceBrightnesses(Lens &lens, Source &source)
Definition gridmap.cpp:185
double magnificationTr() const
calculate the LOCAL magnification by triangel method weighted by interpolated surface brightness
Definition gridmap.cpp:351
void writePixelMapUniform(PixelMap< T > &map, LensingVariable lensvar)
Definition gridmap.h:565
PosType magnificationFlux(Source &source) const
Definition gridmap.cpp:340
int getInitNgrid() const
return initial number of grid points in each direction
Definition gridmap.h:81
GridMap(LensHndl lens, unsigned long N1d, const double center[2], double range)
Constructor for initializing square grid.
Definition gridmap.cpp:89
void find_boundaries_of_caustics(std::vector< std::vector< Point_2d > > &boundaries, std::vector< bool > &hits_edge)
finds the boundary of the region on the source plane where there are more than one image
Definition gridmap.h:215
double AreaCellOnSourcePlane(size_t k) const
area of a cell (pixel size region with its lower left at point k) on source plane - calculated by tri...
Definition gridmap.cpp:448
void writeFits(LensingVariable lensvar, std::string filensame)
fits output of lensing quantities at the resolution of the GridMap
Definition gridmap.h:484
Point_2d centroid() const
returns centroid of flux on the grid
Definition gridmap.cpp:910
void find_crit(std::vector< std::vector< Point_2d > > &points, std::vector< bool > &hits_boundary, std::vector< CritType > &crit_type)
Find critical curves. This is usually not used outside of ImageFinding::find_crit()
Definition gridmap.cpp:521
void deLens()
resets to state without lensing
Definition gridmap.cpp:158
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