#include #include #include #include #include "myalloc.h" void spline(double *x, double *y, int n, double yp0, double ypn1, double *y2); double splint(double *xa, double *ya, double *y2a, int n, double x); void compute_PSF(double *rt,double drt, double at, double shft, double **A2_real,double **A2_imag, int Npup0, int Nimg,double *rhos, double **P); void compute_PSF2(double *rt,double drt, double at, double shft, double **A2_real,double **A2_imag, int Npup0, int Nimg,double *rhos, double **P); int Nocc, Npup; int Ns = 0, K = 1; double units = 1; // 1 = meters, 1e+6 = microns double pi, pi2; double lambda; double f; double b, c, d; int n; double z, R; double rho1; double dr; main() { int i,j,k,lam=5,sgn; Nocc=100000; Npup=2000; z = 21; R = 0.5*0.36*89*1e-3; c = 0.5*89*1e-3, d = 0.5*100*1e-3; lambda = 5.32e-7; rho1 = d; pi = 4*atan(1); pi2 = pi/2; double *r=NULL, *rho=NULL; double *A = NULL; char line[80]; char fname[50]; FILE *fp; /**/ REALLOC(r, Nocc, double); REALLOC(A, Nocc, double); for (i=0; i c) { A[i] = 0; } else { A[i] = 1; } } dr = r[1]-r[0]; fp = fopen("Aweb","w"); for (i=0; i xx*xx) continue; E_real[px][py] += cos(-2*pi*(rhos[px]*rt[i]+rhos[py]*rt[j])/(lambda*f))*Er[i][j] - sin(-2*pi*(rhos[px]*rt[i]+rhos[py]*rt[j])/(lambda*f))*Ei[i][j]; E_imag[px][py] += cos(-2*pi*(rhos[px]*rt[i]+rhos[py]*rt[j])/(lambda*f))*Ei[i][j] + sin(-2*pi*(rhos[px]*rt[i]+rhos[py]*rt[j])/(lambda*f))*Er[i][j]; } } E_real[px][py] *= 2*pi*dx*dx/(lambda*f); E_imag[px][py] *= 2*pi*dx*dx/(lambda*f); P[px][py] = E_real[px][py]*E_real[px][py] + E_imag[px][py]*E_imag[px][py]; } } } // Faster n^3 algorithm void compute_PSF2(double *rt,double dx, double at, double shft, double **Er,double **Ei, int Npup0, int Nimg, double *rhos, double **P) { int i, j, px, py; // Fourier Transform to image plane electric field double drho = 11.2e-6 * units; // THIS IS 3 TIMES TOO SMALL double **E_real=NULL; REALLOC(E_real, 2*Nimg+1, double *); double **E_imag=NULL; REALLOC(E_imag, 2*Nimg+1, double *); E_real+=Nimg; E_imag+=Nimg; for (px=-Nimg; px<=Nimg; px++) { E_real[px]=NULL;REALLOC(E_real[px], 2*Nimg+1, double); E_real[px]+=Nimg; } for (px=-Nimg; px<=Nimg; px++) { E_imag[px]=NULL;REALLOC(E_imag[px], 2*Nimg+1, double); E_imag[px]+=Nimg; } double **Etmp_real=NULL; REALLOC(Etmp_real, 2*Npup0+1, double *); double **Etmp_imag=NULL; REALLOC(Etmp_imag, 2*Npup0+1, double *); Etmp_real+=Npup0; Etmp_imag+=Npup0; for (i=-Npup0; i<=Npup0; i++) { Etmp_real[i]=NULL; REALLOC(Etmp_real[i], 2*Nimg+1, double); Etmp_real[i]+=Nimg; } for (i=-Npup0; i<=Npup0; i++) { Etmp_imag[i]=NULL; REALLOC(Etmp_imag[i], 2*Nimg+1, double); Etmp_imag[i]+=Nimg; } int xx = (int) (at/dx); int ss = (int) (shft/dx); for (px=-Nimg; px<=Nimg; px++) { rhos[px] = px*drho; } for (py=-Nimg; py<=Nimg; py++) { for (i=ss-xx; i<=ss+xx; i++) { if (i<-Npup0 || i>Npup0) { printf("help! ss=%d, xx=%d, Npup0=%d \n", ss,xx,Npup0); fflush(stdout); } Etmp_real[i][py] = 0; Etmp_imag[i][py] = 0; for (j=-xx; j<=xx; j++) { if ((i-ss)*(i-ss)+j*j > xx*xx) continue; Etmp_real[i][py] += cos(-2*pi*(rhos[py]*rt[j])/(lambda*f))*Er[i][j] - sin(-2*pi*(rhos[py]*rt[j])/(lambda*f))*Ei[i][j]; Etmp_imag[i][py] += cos(-2*pi*(rhos[py]*rt[j])/(lambda*f))*Ei[i][j] + sin(-2*pi*(rhos[py]*rt[j])/(lambda*f))*Er[i][j]; } } } for (px=-Nimg; px<=Nimg; px++) { for (py=-Nimg; py<=Nimg; py++) { E_real[px][py] = 0; E_imag[px][py] = 0; for (i=-Npup0; i<=Npup0; i++) { E_real[px][py] += cos(-2*pi*(rhos[px]*rt[i])/(lambda*f))*Etmp_real[i][py] - sin(-2*pi*(rhos[px]*rt[i])/(lambda*f))*Etmp_imag[i][py]; E_imag[px][py] += cos(-2*pi*(rhos[px]*rt[i])/(lambda*f))*Etmp_imag[i][py] + sin(-2*pi*(rhos[px]*rt[i])/(lambda*f))*Etmp_real[i][py]; } E_real[px][py] *= 2*pi*dx*dx/(lambda*f); E_imag[px][py] *= 2*pi*dx*dx/(lambda*f); } } for (px=-Nimg; px<=Nimg; px++) { for (py=-Nimg; py<=Nimg; py++) { P[px][py] = E_real[px][py]*E_real[px][py] + E_imag[px][py]*E_imag[px][py]; } } } void spline(double x[], double y[], int n, double yp0, double ypn1, double y2[]) /* Given arrays x[0..n-1] and y[0..n-1] containing a tabulated function, i.e., * y_i = f(x_i), with x_0 < x_1 < ... < x_n-1, and given values yp0 and ypn1 * for the first derivative of the interpolating function at points x_0 and * x_n-1, respectively, this routine returns an array y2[0..n-1] that contains * the second derivatives of the interpolating function at the tabulated * points x_i. */ { int i; for (i=1; i 1) { k = (khi+klo) >> 1; if (xa[k] > x) khi = k; else klo = k; } h = xa[khi] - xa[klo]; if (h == 0.0) {printf("spline error \n"); return 0;} a = (xa[khi]-x)/h; b = (x-xa[klo])/h; return a*ya[klo]+b*ya[khi]+((a*a*a-a)*y2a[klo]+(b*b*b-b)*y2a[khi])*(h*h)/6; }