9#ifndef __GLAMER__gridmap__
10#define __GLAMER__gridmap__
16#include "concave_hull.h"
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);
76 void ClearSurfaceBrightnesses();
78 size_t getNumberOfPoints()
const {
return Ngrid_init*Ngrid_init2;}
84 double getYRange()
const {
return x_range*axisratio;}
106 ,std::string filename
133 Point_2d getCenter(){
return center;}
135 Point * operator[](
size_t i){
return i_points.data() + i;};
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;
145 std::swap(i_points,grid.i_points);
146 std::swap(s_points,grid.s_points);
148 center = grid.center;
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;
159 i_points = grid.i_points;
160 s_points = grid.s_points;
162 center = grid.center;
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;
172 std::swap(i_points,grid.i_points);
173 std::swap(s_points,grid.s_points);
175 center = grid.center;
181 Triangle(
size_t i,
size_t j,
size_t k){
187 size_t & operator[](
int i){
return index[i];}
196 std::list<RAY> find_images(std::vector<Point_2d> &ys
197 ,std::vector<int> &multiplicity
203 ,std::vector<Point_2d> &image_points
204 ,std::vector<Triangle> &triangles
216 ,std::vector<bool> &hits_edge
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];
223 std::vector<int> multiplicities(N);
224 find_images(source_points,multiplicities);
226 std::vector<bool> bitmap(N,
false);
227 for(
size_t i=0 ; i<N ; ++i){
228 if(multiplicities[i] > 1) bitmap[i] =
true;
299 void find_crit(std::vector<std::vector<Point_2d> > &points
300 ,std::vector<bool> &hits_boundary
301 ,std::vector<CritType> &crit_type
308 std::vector<std::vector<Point_2d> > &curves
309 ,std::vector<bool> &hits_boundary
339 bool incurve(
long k,std::vector<Point_2d> &curve)
const;
342 std::vector<Point> NewPointArray(
size_t N){
343 std::vector<Point> p(N);
348 void xygridpoints(
Point *points,
double range,
const double *center,
long Ngrid
349 ,
short remove_center);
355 unsigned long pointID;
361 std::vector<Point> i_points;
362 std::vector<Point> s_points;
365 bool to_refine(
long i,
long j,
double total,
double f)
const ;
366 static std::mutex grid_mutex;
368 void _find_images_(
Point_2d *ys,
int *multiplicity,
long Nys,std::list<RAY> &rays)
const;
371 void limited_image_search(
Point_2d &y
372 ,std::vector<size_t> &cell_numbers
373 ,std::vector<Triangle> &triangles
383 throw std::invalid_argument(
"resf must be > 0");
390 PixelMap<T> map(center.x,(Ngrid_init-1)/resf + 1 ,(Ngrid_init2-1)/resf + 1,resf*x_range/(Ngrid_init-1));
392 int factor = resf*resf;
394 size_t n = Ngrid_init*Ngrid_init2;
395 for(
size_t i=0 ; i<n ; ++i){
397 map.data()[index] = i_points[i].surface_brightness/factor;
407 map.
Renormalize(map.getResolution()*map.getResolution());
416 int resf = (Ngrid_init-1)/(map.getNx()-1);
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.");
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!");
427 throw std::invalid_argument(
"resf must be > 0");
432 int factor = resf*resf;
445 long nx = map.getNx();
446 for(
size_t i = 0 ; i < Ngrid_init ; ++i){
448 for(
size_t j = 0 ; j < Ngrid_init2 ; ++j){
450 map.data()[ii + nx * jj ] +=
451 i_points[ i + Ngrid_init * j].surface_brightness/factor;
455 map.
Renormalize(map.getResolution()*map.getResolution());
467 const PosType center[]
473 if(getNumberOfPoints() == 0 )
return PixelMap<T>();
486 ,std::string filename
496 size_t Nx = Ngrid_init;
497 size_t Ny = Ngrid_init2;
499 PixelMap<T> map( center.x, Nx, Ny,x_range/(Nx-1) );
501 size_t N = map.size();
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]);
514 for(
size_t i=0 ; i<N ; ++i)
515 map[i] = (i_points[i].x[0] - i_points[i].image->x[0]);
518 for(
size_t i=0 ; i<N ; ++i)
519 map[i] = (i_points[i].x[1] - i_points[i].image->x[1]);
522 for(
size_t i=0 ; i<N ; ++i)
523 map[i] = i_points[i].kappa();
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]);
533 for(
size_t i=0 ; i<N ; ++i)
534 map[i] = i_points[i].gamma1();
537 for(
size_t i=0 ; i<N ; ++i)
538 map[i] = i_points[i].gamma2();
541 for(
size_t i=0 ; i<N ; ++i)
542 map[i] = i_points[i].gamma3();
545 for(
size_t i=0 ; i<N ; ++i)
546 map[i] = i_points[i].invmag();
549 for(
size_t i=0 ; i<N ; ++i)
550 map[i] = i_points[i].dt;
553 for(
size_t i=0 ; i<N ; ++i)
554 map[i] = i_points[i].surface_brightness;
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");
570 if(getNumberOfPoints() ==0 )
return;
577 std::vector<std::thread> thr;
582 chunk_size = getNumberOfPoints()/nthreads;
583 if(chunk_size == 0) nthreads /= 2;
584 }
while(chunk_size == 0);
586 size_t size = chunk_size;
587 for(
int ii = 0; ii < nthreads ;++ii){
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));
592 for(
auto &t : thr) t.join();
601 for(
size_t i = 0; i< size; ++i){
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]);
609 tmp = (points[i].x[0] - points[i].image->x[0]);
612 tmp = (points[i].x[1] - points[i].image->x[1]);
615 tmp = points[i].kappa();
618 tmp2[0] = points[i].gamma1();
619 tmp2[1] = points[i].gamma2();
620 tmp = sqrt(tmp2[0]*tmp2[0] + tmp2[1]*tmp2[1]);
623 tmp = points[i].gamma1();
626 tmp = points[i].gamma2();
629 tmp = points[i].gamma3();
632 tmp = points[i].invmag();
638 tmp = points[i].surface_brightness;
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");
648 if(index != -1)(*map)[index] = tmp;
654 const PosType center[]
658 ,std::string filename
667 tag =
".alpha1.fits";
670 tag =
".alpha2.fits";
679 tag =
".gamma1.fits";
682 tag =
".gamma2.fits";
685 tag =
".gamma3.fits";
691 tag =
".invmag.fits";
694 tag =
".surfbright.fits";
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
@ GAMMA3
third component of shear
@ ALPHA2
y component of deflection
@ SurfBrightness
Surface brightness.
@ GAMMA1
first component of shear
@ GAMMA
magnitude of shear
@ 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