GLAMERDOC++
Gravitational Lensing Code Library
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 
31 struct 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();
61  double AdaptiveRefreshSurfaceBrightnesses(Lens &lens,Source &source);
62 
69  double AddSurfaceBrightnesses(SourceHndl 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 
91  void writeFits(LensingVariable lensvar,std::string filensame);
92 
94  void writeFitsUniform(const PosType center[],size_t Nx,size_t Ny,LensingVariable lensvar,std::string filename);
95  PixelMap writePixelMapUniform(const PosType center[],size_t Nx,size_t Ny,LensingVariable lensvar);
96 
99  LensingVariable lensvar
100  ,std::string filename
101  ){
102  PixelMap map = writePixelMap(lensvar);
103  map.printFITS(filename);
104  }
105 
107  PixelMap getPixelMapFlux(int res) const;
111  void getPixelMapFlux(PixelMap &map) const;
112 
114  PosType EinsteinArea() const;
115 
118  //PosType magnification_invmag() const;
119  //PosType magnification2() const;
120 
122  Point_2d centroid() const;
123 
124  Point_2d getCenter(){return center;}
125 
126  Point * operator[](size_t i){return i_points.data() + i;};
127 
128  GridMap(GridMap &&grid){
129 
130  Ngrid_init = grid.Ngrid_init;
131  Ngrid_init2 = grid.Ngrid_init2;
132  pointID = grid.pointID;
133  axisratio = grid.axisratio;
134  x_range = grid.x_range;
135 
136  std::swap(i_points,grid.i_points);
137  std::swap(s_points,grid.s_points);
138 
139  center = grid.center;
140  }
141 
142  GridMap(GridMap &grid){
143 
144  Ngrid_init = grid.Ngrid_init;
145  Ngrid_init2 = grid.Ngrid_init2;
146  pointID = grid.pointID;
147  axisratio = grid.axisratio;
148  x_range = grid.x_range;
149 
150  i_points = grid.i_points;
151  s_points = grid.s_points;
152 
153  center = grid.center;
154  }
155 
156  GridMap & operator=(GridMap &&grid){
157  Ngrid_init = grid.Ngrid_init;
158  Ngrid_init2 = grid.Ngrid_init2;
159  pointID = grid.pointID;
160  axisratio = grid.axisratio;
161  x_range = grid.x_range;
162 
163  std::swap(i_points,grid.i_points);
164  std::swap(s_points,grid.s_points);
165 
166  center = grid.center;
167 
168  return *this;
169  }
170 
171  struct Triangle{
172  Triangle(size_t i,size_t j,size_t k){
173  index[0] = i;
174  index[1] = j;
175  index[2] = k;
176  }
177  size_t index[3];
178  size_t & operator[](int i){return index[i];}
179  };
180 
181 
182  /*** \brief Returns a list of RAYs from a set of source positions.
183 
184  The image positions are found in parallel by the triangle method. The order of the
185  output rays will be the same as the sources with multiple images consecutive.
186  */
187  std::list<RAY> find_images(std::vector<Point_2d> &ys
188  ,std::vector<int> &multiplicity
189  ) const;
190 
193  void find_images(Point_2d y
194  ,std::vector<Point_2d> &image_points
195  ,std::vector<Triangle> &triangles
196  ) const ;
197 
206  void find_boundaries_of_caustics(std::vector<std::vector<Point_2d> > &boundaries
207  ,std::vector<bool> &hits_edge
208  ){
209 
210  size_t N=Ngrid_init*Ngrid_init2;
211  std::vector<Point_2d> source_points(N);
212  for(size_t i=0 ; i<N ; ++i) source_points[i] = i_points[i];
213 
214  std::vector<int> multiplicities(N);
215  find_images(source_points,multiplicities);
216 
217  std::vector<bool> bitmap(N,false);
218  for(size_t i=0 ; i<N ; ++i){
219  if(multiplicities[i] > 1) bitmap[i] = true;
220  }
221 
222  Utilities::find_boundaries<Point_2d>(bitmap,Ngrid_init,boundaries,hits_edge);
223  }
224 
228  PosType magnificationFlux(Source &source) const ;
229 
237  double magnificationTr() const ;
238 
241  double magnificationTr(std::vector<size_t> &pixels) const ;
242 
244  double AreaCellOnSourcePlane(size_t k) const;
245 
246 
247 // depricated version of PixalMap::magnificationTr()
248 // double magnificationTr2() const {
249 //
250 // size_t k;
251 // size_t k1;
252 // size_t k2;
253 //
254 // double flux_source=0.0,flux_image=0.0;
255 // for(size_t i=1 ; i< Ngrid_init-1 ; i++){
256 // for(size_t j=1 ; j< Ngrid_init2-1 ; j++){
257 //
258 // k = i + j*Ngrid_init;
259 //
260 // assert( s_points[k].surface_brightness == i_points[k].surface_brightness);
261 // double sb = s_points[k].surface_brightness;
262 //
263 // if(sb !=0.0){
264 // flux_source += AreaCellOnSourcePlane(k) *sb;
265 // flux_source += AreaCellOnSourcePlane(k-1) *sb;
266 // flux_source += AreaCellOnSourcePlane(k-1-Ngrid_init) *sb;
267 // flux_source += AreaCellOnSourcePlane(k-Ngrid_init) *sb;
268 // }
269 //
270 // flux_image += sb;
271 // }
272 // }
273 //
274 // return flux_image * getResolution() * getResolution() * 4 / flux_source;
275 // }
276 
283  double AddPointSource(const Point_2d &y,double flux);
284 
290  void find_crit(std::vector<std::vector<Point_2d> > &points
291  ,std::vector<bool> &hits_boundary
292  ,std::vector<CritType> &crit_type
293  );
294 
295 // void find_crit_boundary(std::vector<std::vector<Point_2d> > &points
296 // ,std::vector<bool> &hits_boundary
297 // ) const;
298 
299 private:
300  GridMap & operator=(GridMap &grid);
301 
302  /* Depricated to Utilities::find_boundaries<>()
303 
304  finds ordered boundaries to regions where bitmap == true
305 
306  This can be used to find critical curves or contours.
307  `bitmap` should be the same size as the `Gridmap`
308  If the boundary curve touches the edge of the `GridMap` it will be indicated in `hits_boundary` as
309  `true`.
310 
311  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.
312  */
313 // void find_boundaries(std::vector<bool> &bitmap // = true inside
314 // ,std::vector<std::vector<Point_2d> > &points
315 // ,std::vector<bool> &hits_edge
316 // ,bool add_to_vector=false
317 // );
318 //
319 
320  // curve must be in pixel units
321  bool incurve(long k,std::vector<Point_2d> &curve) const;
322 
323  // cluge to make compatible with old method of producing points
324  std::vector<Point> NewPointArray(size_t N){
325  std::vector<Point> p(N);
326  p[0].head = N;
327  return p;
328  }
329 
330  void xygridpoints(Point *points,double range,const double *center,long Ngrid
331  ,short remove_center);
332 
334  int Ngrid_init;
335  int Ngrid_init2;
336 
337  unsigned long pointID;
338  PosType axisratio;
339  PosType x_range;
340  void writePixelMapUniform_(Point* points,size_t size,PixelMap *map,LensingVariable val);
341 
342  std::vector<Point> i_points;
343  std::vector<Point> s_points;
344  Point_2d center;
345 
346  bool to_refine(long i,long j,double total,double f) const ;
347  static std::mutex grid_mutex;
348 
349  void _find_images_(Point_2d *ys,int *multiplicity,long Nys,std::list<RAY> &rays) const;
350 
351  // find if there are images of y in specific cells
352  void limited_image_search(Point_2d &y
353  ,std::vector<size_t> &cell_numbers
354  ,std::vector<Triangle> &triangles
355  ) const;
356 };
357 
358 #endif // defined(__GLAMER__gridmap__)
GridMap::writePixelMapUniform
void writePixelMapUniform(PixelMap &map, LensingVariable lensvar)
Definition: gridmap.cpp:463
GridMap::EinsteinArea
PosType EinsteinArea() const
returns the area (radians^2) of the region with negative magnification at resolution of fixed grid
Definition: gridmap.cpp:630
Source
Base class for all sources.
Definition: source.h:45
GridMap::getXRange
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
GridMap::centroid
Point_2d centroid() const
returns centroid of flux on the grid
Definition: gridmap.cpp:1246
GridMap::getPixelMapFlux
PixelMap getPixelMapFlux(int res) const
returns a PixelMap with the flux in pixels at a resolution of res times the original resolution
Definition: gridmap.cpp:172
PixelMap::printFITS
void printFITS(std::string filename, bool Xflip=false, bool verbose=false)
Output the pixel map as a fits file.
Definition: pixelize.cpp:1248
GridMap::source_point
Point_2d source_point(size_t index)
get the image point for a index number
Definition: gridmap.h:74
Point_2d
Class for representing points or vectors in 2 dimensions. Not that the dereferencing operator is over...
Definition: point.h:48
GridMap::deLens
void deLens()
resets to state without lensing
Definition: gridmap.cpp:158
GridMap
A simplified version of the Grid structure for making non-adaptive maps of the lensing quantities (ka...
Definition: gridmap.h:31
GridMap::magnificationTr
double magnificationTr() const
calculate the LOCAL magnification by triangel method weighted by interpolated surface brightness
Definition: gridmap.cpp:662
GridMap::find_boundaries_of_caustics
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:206
GridMap::magnificationFlux
PosType magnificationFlux(Source &source) const
Definition: gridmap.cpp:651
GridMap::writeFits
void writeFits(LensingVariable lensvar, std::string filensame)
fits output of lensing quantities at the resolution of the GridMap
Definition: gridmap.cpp:384
GridMap::AddPointSource
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:773
GridMap::ReInitialize
GridMap ReInitialize(LensHndl lens)
reshoot the rays for example when the source plane has been changed
Definition: gridmap.cpp:144
GridMap::image_point
Point_2d image_point(size_t index)
get the image point for a index number
Definition: gridmap.h:72
GridMap::getInitNgrid
int getInitNgrid() const
return initial number of grid points in each direction
Definition: gridmap.h:81
GridMap::getResolution
double getResolution() const
resolution in radians, this is range / (N-1)
Definition: gridmap.h:86
GridMap::find_crit
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:801
GridMap::AdaptiveRefreshSurfaceBrightnesses
double AdaptiveRefreshSurfaceBrightnesses(Lens &lens, Source &source)
Definition: gridmap.cpp:257
GridMap::GridMap
GridMap(LensHndl lens, unsigned long N1d, const double center[2], double range)
Constructor for initializing square grid.
Definition: gridmap.cpp:89
GridMap::writeFitsUniform
void writeFitsUniform(const PosType center[], size_t Nx, size_t Ny, LensingVariable lensvar, std::string filename)
Definition: gridmap.cpp:549
GridMap::RefreshSurfaceBrightnesses
double RefreshSurfaceBrightnesses(SourceHndl source)
Recalculate surface brightness at every point without changing the positions of the gridmap or any le...
Definition: gridmap.cpp:242
GridMap::writePixelMap
PixelMap writePixelMap(LensingVariable lensvar)
make pixel map of lensing quantities at the resolution of the GridMap
Definition: gridmap.cpp:392
Point
A point on the source or image plane that contains a position and the lensing quantities.
Definition: point.h:414
PixelMap
Image structure that can be manipulated and exported to/from fits files.
Definition: image_processing.h:48
LensingVariable
LensingVariable
output lensing variables
Definition: standard.h:89
GridMap::AddSurfaceBrightnesses
double AddSurfaceBrightnesses(SourceHndl source)
Recalculate surface brightness just like GridMap::RefreshSurfaceBrightness but the new source is adde...
Definition: gridmap.cpp:328
Lens::operator=
Lens & operator=(Lens &&lens)
Definition: lens.h:513
GridMap::writeFitsUniform
void writeFitsUniform(LensingVariable lensvar, std::string filename)
this will make a fits map of the grid as is.
Definition: gridmap.h:98
Lens
A class to represents a lens with multiple planes.
Definition: lens.h:71
GridMap::AreaCellOnSourcePlane
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:759