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 RAY ray(
size_t index){
return i_points[index];}
78 void ClearSurfaceBrightnesses();
80 size_t getNumberOfPoints()
const {
return Ngrid_init*Ngrid_init2;}
86 double getYRange()
const {
return x_range*axisratio;}
108 ,std::string filename
135 Point_2d getCenter(){
return center;}
137 Point * operator[](
size_t i){
return i_points.data() + i;};
141 Ngrid_init = grid.Ngrid_init;
142 Ngrid_init2 = grid.Ngrid_init2;
143 pointID = grid.pointID;
144 axisratio = grid.axisratio;
145 x_range = grid.x_range;
147 std::swap(i_points,grid.i_points);
148 std::swap(s_points,grid.s_points);
150 center = grid.center;
155 Ngrid_init = grid.Ngrid_init;
156 Ngrid_init2 = grid.Ngrid_init2;
157 pointID = grid.pointID;
158 axisratio = grid.axisratio;
159 x_range = grid.x_range;
161 i_points = grid.i_points;
162 s_points = grid.s_points;
164 center = grid.center;
168 Ngrid_init = grid.Ngrid_init;
169 Ngrid_init2 = grid.Ngrid_init2;
170 pointID = grid.pointID;
171 axisratio = grid.axisratio;
172 x_range = grid.x_range;
174 std::swap(i_points,grid.i_points);
175 std::swap(s_points,grid.s_points);
177 center = grid.center;
183 Triangle(
size_t i,
size_t j,
size_t k){
193 Triangle(
const Triangle &tri){
198 Triangle & operator=(
const Triangle &tri){
205 std::vector<long> index = {0,0,0};
206 long & operator[](
int i){
return index[i];}
207 long operator[](
int i)
const {
return index[i];}
210 Rectangle(
size_t i,
size_t j){
218 Rectangle(
const Rectangle &rec){
222 Rectangle & operator=(Rectangle &rec){
228 std::vector<long> index = {0,0};
229 long & operator[](
int i){
return index[i];}
230 long operator[](
int i)
const {
return index[i];}
234 inline bool touch(
const Rectangle &tr1,
const Rectangle &tr2)
const;
237 std::vector<Rectangle> merge_boxes(
238 std::list<Triangle> &triangles
240 std::vector<Rectangle> merge_boxes(
241 std::vector<Triangle> &triangles
254 std::list<RAY>
find_images(std::vector<Point_2d> &ys
255 ,std::vector<int> &multiplicity
265 ,std::vector<Point_2d> &image_points
266 ,std::vector<Triangle> &triangles
279 ,std::vector<bool> &hits_edge
282 size_t N=Ngrid_init*Ngrid_init2;
283 std::vector<Point_2d> source_points(N);
284 for(
size_t i=0 ; i<N ; ++i) source_points[i] = i_points[i];
286 std::vector<int> multiplicities(N);
289 std::vector<bool> bitmap(N,
false);
290 for(
size_t i=0 ; i<N ; ++i){
291 if(multiplicities[i] > 1) bitmap[i] =
true;
362 void find_crit(std::vector<std::vector<Point_2d> > &points
363 ,std::vector<bool> &hits_boundary
364 ,std::vector<CritType> &crit_type
371 std::vector<std::vector<Point_2d> > &curves
372 ,std::vector<bool> &hits_boundary
379 int getNx(){
return Ngrid_init;}
380 int getNy(){
return Ngrid_init2;}
385 bool incurve(
long k,std::vector<Point_2d> &curve)
const;
388 std::vector<Point> NewPointArray(
size_t N){
389 std::vector<Point> p(N);
394 void xygridpoints(Point *points,
double range,
const double *center,
long Ngrid
395 ,
short remove_center);
401 unsigned long pointID;
405 void writePixelMapUniform_(Point* points,
size_t size,PixelMap<T> *map,
LensingVariable val);
407 std::vector<Point> i_points;
408 std::vector<Point> s_points;
411 bool to_refine(
long i,
long j,
double total,
double f)
const ;
412 static std::mutex grid_mutex;
414 void _find_images_(Point_2d *ys,
int *multiplicity,
long Nys,std::list<RAY> &rays)
const;
415 void _find_images2_(
size_t j1
417 ,std::list<Point_2d> &image_points
418 ,std::list<Triangle> &triangles
423 void limited_image_search(Point_2d &y
424 ,std::vector<size_t> &cell_numbers
425 ,std::vector<Triangle> &triangles
440 ,x_range/(Ngrid_init-1));
443 size_t n = Ngrid_init*Ngrid_init2;
444 for(
size_t i=0 ; i<n ; ++i){
446 map.data()[index] = i_points[i].surface_brightness;
456 map.
Renormalize(map.getResolution()*map.getResolution());
467 if(map.getNx() != Ngrid_init)
throw std::invalid_argument(
"PixelMap does not match GripMap! Use the other GridMap::getPixelMapFlux() to contruct a PixelMap.");
468 if(map.getNy() != Ngrid_init2)
throw std::invalid_argument(
"PixelMap does not match GripMap! Use the other GridMap::getPixelMapFlux() to contruct a PixelMap.");
471 if(map.getCenter()[0] != center[0])
throw std::invalid_argument(
"PixelMap does not match GripMap!");
472 if(map.getCenter()[1] != center[1])
throw std::invalid_argument(
"PixelMap does not match GripMap!");
475 if((map.getResolution()-res) < 1.0e-6*res )
throw std::invalid_argument(
"PixelMap resolution does not match GripMap!");
488 size_t n = Ngrid_init*Ngrid_init2;
489 for(
size_t i=0 ; i<n ; ++i){
491 map.data()[index] = i_points[i].surface_brightness;
495 map.
Renormalize(map.getResolution()*map.getResolution());
507 const PosType center[]
513 if(getNumberOfPoints() == 0 )
return PixelMap<T>();
526 ,std::string filename
536 size_t Nx = Ngrid_init;
537 size_t Ny = Ngrid_init2;
539 PixelMap<T> map( center.x, Nx, Ny,x_range/(Nx-1) );
541 size_t N = map.size();
547 for(
size_t i=0 ; i<N ; ++i){
548 tmp2[0] = i_points[i].x[0] - i_points[i].image->x[0];
549 tmp2[1] = i_points[i].x[1] - i_points[i].image->x[1];
550 map[i] = sqrt(tmp2[0]*tmp2[0] + tmp2[1]*tmp2[1]);
554 for(
size_t i=0 ; i<N ; ++i)
555 map[i] = (i_points[i].x[0] - i_points[i].image->x[0]);
558 for(
size_t i=0 ; i<N ; ++i)
559 map[i] = (i_points[i].x[1] - i_points[i].image->x[1]);
562 for(
size_t i=0 ; i<N ; ++i)
563 map[i] = i_points[i].kappa();
566 for(
size_t i=0 ; i<N ; ++i){
567 tmp2[0] = i_points[i].gamma1();
568 tmp2[1] = i_points[i].gamma2();
569 map[i] = sqrt(tmp2[0]*tmp2[0] + tmp2[1]*tmp2[1]);
573 for(
size_t i=0 ; i<N ; ++i)
574 map[i] = i_points[i].gamma1();
577 for(
size_t i=0 ; i<N ; ++i)
578 map[i] = i_points[i].gamma2();
581 for(
size_t i=0 ; i<N ; ++i)
582 map[i] = i_points[i].gamma3();
585 for(
size_t i=0 ; i<N ; ++i)
586 map[i] = i_points[i].invmag();
589 for(
size_t i=0 ; i<N ; ++i)
590 map[i] = i_points[i].dt;
593 for(
size_t i=0 ; i<N ; ++i)
594 map[i] = i_points[i].surface_brightness;
597 std::cerr <<
"GridMap::writePixelMapUniform() does not work for the input LensingVariable" << std::endl;
598 throw std::runtime_error(
"GridMap::writePixelMapUniform() does not work for the input LensingVariable");
610 if(getNumberOfPoints() ==0 )
return;
617 std::vector<std::thread> thr;
622 chunk_size = getNumberOfPoints()/nthreads;
623 if(chunk_size == 0) nthreads /= 2;
624 }
while(chunk_size == 0);
626 size_t size = chunk_size;
627 for(
int ii = 0; ii < nthreads ;++ii){
629 size = getNumberOfPoints() - (nthreads-1)*chunk_size;
630 thr.push_back(std::thread(&GridMap::writePixelMapUniform_<T>,
this,&(i_points[ii*chunk_size]),size,&map,lensvar));
632 for(
auto &t : thr) t.join();
641 for(
size_t i = 0; i< size; ++i){
644 tmp2[0] = points[i].x[0] - points[i].image->x[0];
645 tmp2[1] = points[i].x[1] - points[i].image->x[1];
646 tmp = sqrt(tmp2[0]*tmp2[0] + tmp2[1]*tmp2[1]);
649 tmp = (points[i].x[0] - points[i].image->x[0]);
652 tmp = (points[i].x[1] - points[i].image->x[1]);
655 tmp = points[i].kappa();
658 tmp2[0] = points[i].gamma1();
659 tmp2[1] = points[i].gamma2();
660 tmp = sqrt(tmp2[0]*tmp2[0] + tmp2[1]*tmp2[1]);
663 tmp = points[i].gamma1();
666 tmp = points[i].gamma2();
669 tmp = points[i].gamma3();
672 tmp = points[i].invmag();
678 tmp = points[i].surface_brightness;
681 std::cerr <<
"PixelMap<T>::AddGrid() does not work for the input LensingVariable" << std::endl;
682 throw std::runtime_error(
"PixelMap<T>::AddGrid() does not work for the input LensingVariable");
688 if(index != -1)(*map)[index] = tmp;
694 const PosType center[]
698 ,std::string filename
707 tag =
".alpha1.fits";
710 tag =
".alpha2.fits";
719 tag =
".gamma1.fits";
722 tag =
".gamma2.fits";
725 tag =
".gamma3.fits";
731 tag =
".invmag.fits";
734 tag =
".surfbright.fits";
Image structure that can be manipulated and exported to/from fits files.
Definition pixelmap.h:42
void Renormalize(T factor)
Multiplies the whole map by a scalar factor.
Definition pixelmap.cpp:509
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 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
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
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
PixelMap< T > getPixelMapFlux() const
returns a PixelMap with the flux in pixels at a resolution of res times the original resolution
Definition gridmap.h:431
double AddSurfaceBrightnesses(Source *source)
Recalculate surface brightness just like GridMap::RefreshSurfaceBrightness but the new source is adde...
Definition gridmap.cpp:260
Point_2d image_point(size_t index)
get the image point for a index number
Definition gridmap.h:72
std::list< RAY > find_images(std::vector< Point_2d > &ys, std::vector< int > &multiplicity) const
Returns a list of RAYs from a set of source positions.
Definition gridmap.cpp:927
PosType EinsteinArea() const
returns the area (radians^2) of the region with negative magnification at resolution of fixed grid
Definition gridmap.cpp:323
RAY ray(size_t index)
get the image point for a index number
Definition gridmap.h:76
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:85
double RefreshSurfaceBrightnesses(Source *source)
Recalculate surface brightness at every point without changing the positions of the gridmap or any le...
Definition gridmap.cpp:174
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:466
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:494
PixelMap< T > writePixelMap(LensingVariable lensvar)
make pixel map of lensing quantities at the resolution of the GridMap
Definition gridmap.h:533
void writeFitsUniform(const PosType center[], size_t Nx, size_t Ny, LensingVariable lensvar, std::string filename)
Definition gridmap.h:693
void writeFitsUniform(LensingVariable lensvar, std::string filename)
this will make a fits map of the grid as is.
Definition gridmap.h:106
double getResolution() const
resolution in radians, this is range / (N-1)
Definition gridmap.h:88
GridMap ReInitialize(LensHndl lens)
reshoot the rays for example when the source plane has been changed
Definition gridmap.cpp:148
double AdaptiveRefreshSurfaceBrightnesses(Lens &lens, Source &source)
Definition gridmap.cpp:189
double magnificationTr() const
calculate the LOCAL magnification by triangel method weighted by interpolated surface brightness
Definition gridmap.cpp:355
void writePixelMapUniform(PixelMap< T > &map, LensingVariable lensvar)
Definition gridmap.h:605
PosType magnificationFlux(Source &source) const
Definition gridmap.cpp:344
int getInitNgrid() const
return initial number of grid points in each direction
Definition gridmap.h:83
GridMap(LensHndl lens, unsigned long N1d, const double center[2], double range)
Constructor for initializing square grid.
Definition gridmap.cpp:93
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:278
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:452
void writeFits(LensingVariable lensvar, std::string filensame)
fits output of lensing quantities at the resolution of the GridMap
Definition gridmap.h:524
Point_2d centroid() const
returns centroid of flux on the grid
Definition gridmap.cpp:915
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:527
void deLens()
resets to state without lensing
Definition gridmap.cpp:162
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
Simple representaion of a light path giving position on the image and source planes and lensing quant...
Definition point.h:517