### Blame view

SimulateSpectrum.cpp 16.4 KB
 ```1 2 3 4``` `````` #include #include #include #include `````` `5` `````` #include "globals.h" `````` `6` `````` #include `````` ```7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140``` `````` #include //#include "cufft.h" using namespace std; #define pi 3.14159 typedef complex scComplex; extern int cbessjyva(double v,complex z,double &vm,complex*cjv, complex*cyv,complex*cjvp,complex*cyvp); extern int bessjyv(double v,double x,double &vm,double *jv,double *yv, double *djv,double *dyv); complex Jl_neg(complex x) { //this function computes the bessel function of the first kind Jl(x) for l = -0.5 return ( sqrt(2.0/pi) * cos(x) )/sqrt(x); } double Jl_neg(double x) { //this function computes the bessel function of the first kind Jl(x) for l = -0.5 return ( sqrt(2.0/pi) * cos(x) )/sqrt(x); } double Yl_neg(double x) { //this function computes the bessel function of the second kind Yl(x) for l = -0.5; return ( sqrt(2.0/pi) * sin(x) )/sqrt(x); } void computeB(complex* B, double radius, complex refIndex, double lambda, int Nl) { double k = (2*pi)/lambda; int b = 2; //allocate space for the real bessel functions double* jv = (double*)malloc(sizeof(double)*(Nl+b)); double* yv = (double*)malloc(sizeof(double)*(Nl+b)); double* jvp = (double*)malloc(sizeof(double)*(Nl+b)); double* yvp = (double*)malloc(sizeof(double)*(Nl+b)); //allocate space for the complex bessel functions complex* cjv = (complex*)malloc(sizeof(complex)*(Nl+b)); complex* cyv = (complex*)malloc(sizeof(complex)*(Nl+b)); complex* cjvp = (complex*)malloc(sizeof(complex)*(Nl+b)); complex* cyvp = (complex*)malloc(sizeof(complex)*(Nl+b)); double kr = k*radius; complex knr = k*refIndex*(double)radius; complex n = refIndex; //compute the bessel functions for k*r double vm;// = Nl - 1; bessjyv((Nl)+0.5, kr, vm, jv, yv, jvp, yvp); //cout<<"Nl: "< scale_knr = sqrt(pi/(2.0*knr)); complex numer, denom; double j_kr; double y_kr; complex j_knr; complex j_d_knr; double j_d_kr; complex h_kr; complex h_d_kr; complex h_neg; complex h_pos; //cout<<"B coefficients:"<(j_kr, y_kr); //compute the derivatives if(l == 0) { //spherical bessel functions for l=0 j_d_kr = scale_kr * (Jl_neg(kr) - (jv[l] + kr*jv[l+1])/kr )/2.0; j_d_knr = scale_knr * ( Jl_neg(knr) - (cjv[l] + knr*cjv[l+1])/knr )/2.0; h_neg = complex(scale_kr*Jl_neg(kr), scale_kr*Yl_neg(kr)); h_pos = complex(scale_kr*jv[l+1], scale_kr*yv[l+1]); h_d_kr = (h_neg - (h_kr + kr*h_pos)/kr)/2.0; } else { //spherical bessel functions j_d_kr = scale_kr * (jv[l-1] - (jv[l] + kr*jv[l+1])/kr )/2.0; j_d_knr = scale_knr * ( cjv[l-1] - (cjv[l] + knr*cjv[l+1])/knr )/2.0; h_neg = complex(scale_kr*jv[l-1], scale_kr*yv[l-1]); h_pos = complex(scale_kr*jv[l+1], scale_kr*yv[l+1]); h_d_kr = (h_neg - (h_kr + kr*h_pos)/kr)/2.0; } numer = j_kr*j_d_knr*n - j_knr*j_d_kr; denom = j_knr*h_d_kr - h_kr*j_d_knr*n; B[l] = numer/denom; //B[l] = scComplex(temp.real(), temp.imag()); //cout< integrateUi(double cAngleI, double cAngleO, double oAngleI, double oAngleO, double M = 2*pi) { /*This function integrates the incident field of magnitude M in the far zone in order to evaluate the field at the central pixel of a detector. cNAi = condenser inner angle cNAo = condenser outer angle oNAi = objective inner angle oNAo = objective outer angle M = field magnitude*/ double alphaIn = max(cAngleI, oAngleI); double alphaOut = min(cAngleO,oAngleO); complex Ui; if(alphaIn > alphaOut) Ui = complex(0.0, 0.0); else Ui = complex(M * 2 * pi * (cos(alphaIn) - cos(alphaOut)), 0.0f); return Ui; } void computeCondenserAlpha(double* alpha, int Nl, double cAngleI, double cAngleO) { /*This function computes the condenser integral in order to build the field of incident light alpha = list of Nl floating point values representing the condenser alpha as a function of l Nl = number of orders in the incident field cAngleI, cAngleO = inner and outer condenser angles (inner and outer NA)*/ //compute the Legendre polynomials for the condenser aperature double* PcNAo = (double*)malloc(sizeof(double)*(Nl+1)); Legendre(PcNAo, cos(cAngleO), Nl+1); double* PcNAi = (double*)malloc(sizeof(double)*(Nl+1)); Legendre(PcNAi, cos(cAngleI), Nl+1); for(int l=0; l integrateUs(double r, double lambda, complex eta, `````` ```195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226``` `````` double cAngleI, double cAngleO, double oAngleI, double oAngleO, double M = 2*pi) { /*This function integrates the incident field of magnitude M in the far zone in order to evaluate the field at the central pixel of a detector. r = sphere radius lambda = wavelength eta = index of refraction cNAi = condenser inner NA cNAo = condenser outer NA oNAi = objective inner NA oNAo = objective outer NA M = field magnitude*/ //compute the required number of orders double k = 2*pi/lambda; int Nl = (int)ceil( k + 4 * exp(log(k*r)/3) + 3 ); //compute the material coefficients B complex* B = (complex*)malloc(sizeof(complex)*Nl); //compute the Legendre polynomials for the condenser and objective aperatures double* PcNAo = (double*)malloc(sizeof(double)*(Nl+1)); Legendre(PcNAo, cos(cAngleO), Nl+1); double* PcNAi = (double*)malloc(sizeof(double)*(Nl+1)); Legendre(PcNAi, cos(cAngleI), Nl+1); double* PoNAo = (double*)malloc(sizeof(double)*(Nl+1)); Legendre(PoNAo, cos(oAngleO), Nl+1); double* PoNAi = (double*)malloc(sizeof(double)*(Nl+1)); Legendre(PoNAi, cos(oAngleI), Nl+1); //store the index of refraction; complex IR(eta.real(), eta.imag()); `````` `227` `````` `````` ```228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251``` `````` //compute the scattering coefficients computeB(B, r, IR, lambda, Nl); //aperature terms for the condenser (alpha) and objective (beta) double alpha; double beta; double c; complex Us(0.0, 0.0); for(int l=0; l Ui = integrateUi(cAngleI, cAngleO, oAngleI, oAngleO, 2*pi); double I0 = Ui.real() * Ui.real() + Ui.imag() * Ui.imag(); I0 *= scaleI0; `````` `297` `````` `````` ```298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319``` `````` //double I; SpecPair temp; double nu; complex eta; complex Us, U; double vecLen = 0.0; for(unsigned int i=0; i(EtaN[i].A, EtaK[i].A); else eta = complex(baseIR, 0.0); //integrate the scattered field at the detector position Us = integrateUs(radius, lambda, eta, cAngleI, cAngleO, oAngleI, oAngleO, 2*pi); U = Us + Ui; double I = U.real() * U.real() + U.imag() * U.imag(); `````` `320` `````` `````` ```321 322 323 324 325 326 327 328 329 330``` `````` temp.nu = nu; //set the spectrum value based on the current display type if(dispSimType == AbsorbanceSpecType) temp.A = -log10(I/I0); else temp.A = I; if(dispNormalize) vecLen += temp.A * temp.A; `````` ```331 332``` `````` SimSpectrum.push_back(temp); `````` ```333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354``` `````` } vecLen = sqrt(vecLen); if(dispNormalize) for(unsigned int i=0; i* B, int Nl, int nLambda) { double nu; complex eta; double* Lambda = (double*)malloc(sizeof(double) * nLambda); //for each wavenumber nu for(unsigned int i=0; i(EtaN[i].A, EtaK[i].A); else eta = complex(baseIR, 0.0); //allocate memory for the scattering coefficients `````` `421` `````` //complex* B = (complex*)malloc(sizeof(complex)*Nl); `````` ```422 423 424 425 426 427 428 429``` `````` complex IR(eta.real(), eta.imag()); computeB(&B[i * Nl], radius, IR, Lambda[i], Nl); } } void computeOpticalParameters(double& cAngleI, double& cAngleO, double& oAngleI, double& oAngleO, double& I0, double* alpha, int Nl) { `````` `430` `````` computeCassegrainAngles(cAngleI, cAngleO, oAngleI, oAngleO); `````` ```431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447``` `````` //evaluate the incident field intensity I0 = 0.0; complex Ui; Ui = integrateUi(cAngleI, cAngleO, oAngleI, oAngleO, 2*pi); I0 = Ui.real()*2*pi; //compute alpha (condenser integral) computeCondenserAlpha(alpha, Nl, cAngleI, cAngleO); } void gpuDetectorSpectrum(int numSamples) { //integrate across the objective aperature and calculate the resulting intensity on a detector PD.StartTimer(SIMULATE_SPECTRUM); //clear the previous spectrum `````` `448` `````` SimSpectrum.clear(); `````` ```449 450 451 452 453 454 455 456 457 458``` `````` //compute Nl (maximum order of the spectrum) int Nl = computeNl(); double* alpha = (double*)malloc(sizeof(double)*(Nl + 1)); double cAngleI, cAngleO, oAngleI, oAngleO, I0; computeOpticalParameters(cAngleI, cAngleO, oAngleI, oAngleO, I0, alpha, Nl); //allocate space for a list of wavelengths int nLambda = EtaK.size(); `````` `459` `````` `````` ```460 461 462``` `````` //allocate space for the 2D array (Nl x nu) of scattering coefficients complex* B = (complex*)malloc(sizeof(complex) * Nl * nLambda); computeBArray(B, Nl, nLambda); `````` `463` `````` `````` `464` `````` `````` `465` `````` `````` ```466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511``` `````` //allocate temporary space for the spectrum double* I = (double*)malloc(sizeof(double) * EtaK.size()); //compute the spectrum on the GPU PD.StartTimer(SIMULATE_GPU); cudaComputeSpectrum(I, (double*)B, alpha, Nl, nLambda, oAngleI, oAngleO, cAngleI, cAngleO, numSamples); PD.EndTimer(SIMULATE_GPU); updateSpectrum(I, I0, nLambda); PD.EndTimer(SIMULATE_SPECTRUM); } void SimulateSpectrum() { if(pointDetector) pointSpectrum(); else gpuDetectorSpectrum(objectiveSamples); //detectorSpectrum(objectiveSamples); } double absorbanceDistortion(){ //compute the mean of the spectrum double sumSim = 0.0; for(unsigned int i=0; i* B = (complex*)malloc(sizeof(complex) * Nl * nLambda); computeBArray(B, Nl, nLambda); `````` `562` `````` QProgressDialog progress("Computing distortion map...", "Stop", 0, nSteps * nSteps); `````` `563` `````` progress.setWindowModality(Qt::WindowModal); `````` ```564 565``` `````` double D; `````` ```566 567 568 569``` `````` double e = 0.001; int i, o; for(i=0; i= cNAo || cNAi >= oNAo || oNAi >= cNAo || oNAi >= oNAo) D = -1.0; `````` ```597 598 599 600 601``` `````` else D = absorbanceDistortion(); } else { `````` ```602 603``` `````` if(cNAi >= cNAo || oNAi >= oNAo) D = -1.0; `````` ```604 605``` `````` else D = intensityDistortion(); `````` `606` `````` } `````` `607` `````` distortionMap[o * nSteps + i] = D; `````` ```608 609 610``` `````` outFile<