main.cc 26 KB
Newer Older
Phillip Lino Rall's avatar
Phillip Lino Rall committed
1
2
3
4
5
6
7
8
9
10
11
// ------------------------------------------------------------
// main.cc
//
// ------------------------------------------------------------
#define _USE_MATH_DEFINES
#include <cmath>
#include <complex>
#include <iostream>
#include <string>
#include <vector>
#include "source/ugblock2D.h"
12
#include "source/ugblock.h"
Phillip Lino Rall's avatar
Phillip Lino Rall committed
13
14
15
16
17
18
19
20
21
22
23
24
25
26


using std::complex;
using namespace ::_COLSAMM_;

#define L2NORM(vector) sqrt(product(vector,vector).real())
#define RESTART 0


double isZero(double x)
{
	return fabs(x) < 1e-10?1.0:0.0;
}

Blue Bird's avatar
Blue Bird committed
27
complex<double> I(0.0,1.0);
Phillip Lino Rall's avatar
Phillip Lino Rall committed
28

Blue Bird's avatar
Blue Bird committed
29
30
31
std::complex<double> expi(double x) {
   return exp(I*x);
}
Phillip Lino Rall's avatar
Phillip Lino Rall committed
32

Blue Bird's avatar
Blue Bird committed
33
34
35
std::complex<double> sinExp(double x) {
   return sin(x);
}
Phillip Lino Rall's avatar
Phillip Lino Rall committed
36

Blue Bird's avatar
Blue Bird committed
37
38
39
std::complex<double> cosExp(double x) {
   return cos(x);
}
Phillip Lino Rall's avatar
Phillip Lino Rall committed
40

Blue Bird's avatar
Blue Bird committed
41
double realPart(std::complex<double> x) {
42
   return x.real();
Blue Bird's avatar
Blue Bird committed
43
}
Phillip Lino Rall's avatar
Phillip Lino Rall committed
44

Blue Bird's avatar
Blue Bird committed
45
double imPart(std::complex<double> x) {
46
   return x.imag();
Blue Bird's avatar
Blue Bird committed
47
}
Phillip Lino Rall's avatar
Phillip Lino Rall committed
48

49
50
51
52
double complexAngle(std::complex<double> x) {
    return (std::arg(x)+2.0*M_PI);
 }

Blue Bird's avatar
Blue Bird committed
53
54
55
56
57
double radiusLoch;
std::complex<double> loch(double x, double y) {
   if(sqrt(x*x+y*y) < radiusLoch) return 1.0;
   return 0.0;   
}
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75

double radiusGauss;
double curvature;
double distanceFromWaist;
double lambda;
double rayleighrange;
std::complex<double> gauss(double x, double y) {
    double k = 2.0 * M_PI / lambda;
    double waist = radiusGauss * sqrt(1+pow(distanceFromWaist / rayleighrange,2));
    if (curvature == 0.0)
    {
        return exp(-(x*x+y*y)/(waist*waist));
    }
    else
    {
        return exp(-(x*x+y*y)/(waist*waist))*expi(-1.0 * k * (x*x+y*y) / (2.0 * curvature));
    }
}
76
std::complex<double> spalt(double x, double y) {
77
   if(fabs(x) < radiusLoch) return 1.0;
78
79
   return 0.0;
}
80
81
82
83
84
85
86
87
88
89
90
91
92
double myRealSqrt(std::complex<double> z) {
    return sqrt(z.real());
}

std::complex<double> myLimitToOne(std::complex<double> in)
{
    if (in.real() < 1.0)
    {
        return std::complex<double>(1,0);
    }
    return in;
}

93
void CalcFresnel(std::complex<double> (*aperture) (double x, double y),
Blue Bird's avatar
Blue Bird committed
94
95
96
97
98
99
100
101
102
                 double distance,
                 double wavenumber,
                 VariableFFT& varIn, 
                 VariableFFT& varFar) {
    Blockgrid2D* blockGridRec = varFar.Give_blockgrid();

	X_coordinate2d X(*blockGridRec);
	Y_coordinate2d Y(*blockGridRec);

103
    Function2d2<std::complex<double>,double> Aperture(aperture);
Blue Bird's avatar
Blue Bird committed
104
    Function2d1<std::complex<double>,double> Expi(expi);
105

106
    varFar = Aperture(X,Y) * Expi(wavenumber * (X*X+Y*Y) / (2.0 * distance));
Blue Bird's avatar
Blue Bird committed
107
108
109
    varIn  = varFar;
    varFar.FFT();
}
Phillip Lino Rall's avatar
Phillip Lino Rall committed
110

111
112
113
114
115

void spectrumPlaneWave(std::complex<double> (*aperture) (double x, double y),
                       double distance,
                       double wavenumber,
                       VariableFFT& varIn,
116
117
118
119
120
121
                       VariableFFT& varFar,
                       X_coordinate2d& X,
                       Y_coordinate2d& Y,
                       VariableFFT& Kx,
                       VariableFFT& Ky,
                       VariableFFT& temp) {
122
123
          Blockgrid2D* blockGridRec = varFar.Give_blockgrid();

124
125
126
127
//          X_coordinate2d X(*blockGridRec);
//          Y_coordinate2d Y(*blockGridRec);
//          VariableFFT Kx(varFar);
//          VariableFFT Ky(varFar);
128

129
130
//          double dx = varIn.getHx();
//          double dy = varIn.getHy();
131

132
133
//          double ukx = 2.0 * M_PI / varIn.getSizeX();
//          double uky = 2.0 * M_PI / varIn.getSizeY();
134

135
136
//          Kx = X / dx * ukx ;
//          Ky = Y / dy * uky ;
137
138

          Function2d2<std::complex<double>,double> Aperture(aperture);
139
140
          Function2d1<double, std::complex<double> > absolute(ABS);
          Function2d1<double, std::complex<double> > myRealFunc(myReal);
141
142
143
144
          Function2d1<std::complex<double>,double> Expi(expi);
          Function2d1<double, std::complex<double> > Sqrt(myRealSqrt);


145
146
147
148
149
150
151
152
153
154
          //VariableFFT temp(varFar);
          temp = Sqrt(wavenumber*wavenumber - Kx*Kx - Ky*Ky) * (distance) ;
          std::ofstream DATEIA;

          DATEIA.open("varWaveK.vtk");
          temp.Print_VTK(DATEIA);
          DATEIA.close();

          //varIn = varIn * Expi(myRealFunc(temp));

155
156
157
          varFar = Aperture(X,Y);
          varIn  = varFar;
          varFar.FFT();
158
          varFar = varFar * Expi( myRealFunc(temp));
159
160
161
          varFar.inversFFT();
      }

162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
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
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305

void virtualLensCorrection(double dz, double wavenumberAverage,
                           VariableFFT& varIn,
                           VariableFFT& varWavenumber,
                           VariableFFT& temp)
{





    Function2d1<std::complex<double>,double> Expi(expi);
    Function2d1<double, std::complex<double> > absolute(ABS);
    Function2d1<double, std::complex<double> > myRealFunc(myReal);

    temp = myRealFunc(varWavenumber - wavenumberAverage ) * dz;
//    std::ofstream DATEIC;
//    DATEIC.open("varTEMPAVERAGE.vtk");
//    temp.Print_VTK(DATEIC);
//    DATEIC.close();



//    temp = myRealFunc(varWavenumber - minimumWavenmumber ) * dz;
//    DATEIC.open("varTEMPMINIMA.vtk");
//    temp.Print_VTK(DATEIC);
//    DATEIC.close();
    //sampling
    double samplingTest =Maximum(myRealFunc(temp)) ;
    double samplingMax = M_PI / samplingTest;
    if (samplingMax < 1.0)
    {
        std::cout << "undersampling \n";
        std::cout << "dz = " << dz << "\n";
        std::cout << "dzMax = " << samplingMax * dz << std::endl;
    }

//    std::ofstream DATEIB;

//    DATEIB.open("varTEMPTEST.vtk");
//    temp.Print_VTK(DATEIB);
//    DATEIB.close();



    varIn = varIn * Expi(1.0*myRealFunc(temp));

//    std::ofstream DATEIA;

//    DATEIA.open("varInAfterCorrection.vtk");
//    varIn.Print_VTK(DATEIA);
//    DATEIA.close();


}

void powerTest(VariableFFT& varIn)
{
    Function2d1<double, std::complex<double> > absolute(ABS);
    double power = product(absolute(varIn),absolute(varIn));
    std::cout << "power  = " << power << "\n";

}
void spectrumPlaneWavePropagation(double distance,
                       double wavenumber,
                       VariableFFT& varIn,
                       VariableFFT& varFar,
                       VariableFFT& Kx,
                       VariableFFT& Ky,
                       VariableFFT& temp ) {
         // Blockgrid2D* blockGridRec = varFar.Give_blockgrid();

//          X_coordinate2d X(*blockGridRec);
//          Y_coordinate2d Y(*blockGridRec);
//          VariableFFT Kx(varFar);
//          VariableFFT Ky(varFar);

//          double dx = varIn.getHx();
//          double dy = varIn.getHy();

//          double ukx = 2.0 * M_PI / varIn.getSizeX();
//          double uky = 2.0 * M_PI / varIn.getSizeY();

//          Kx = X / dx * ukx ;
//          Ky = Y / dy * uky ;


                  Function2d1<double, std::complex<double> > MYREAL(myReal);

          double maxKx = Maximum(MYREAL(Kx));
          double maxKy = Maximum(MYREAL(Ky));

          Function2d1<std::complex<double>,double> Expi(expi);
          Function2d1<double, std::complex<double> > Sqrt(myRealSqrt);
          Function2d1<double, std::complex<double> > absolute(ABS);

         // VariableFFT temp(varIn);
          temp = wavenumber*wavenumber - Kx*Kx - Ky*Ky;
          temp = Sqrt(temp) * (distance);

//          std::ofstream DATEIA;
//          DATEIA.open("varTempA.vtk");
//          temp.Print_VTK(DATEIA);
//          DATEIA.close();


          if ((wavenumber*wavenumber - maxKx*maxKx - maxKy*maxKy) < 0)
          {
              std::cout << "warning : negative sqrt() ! decrease step size or increase resolution" << std::endl;
          }

         // varIn.FFTShift();

          varIn.FFT();
          //varIn.FFTShift();


//          DATEIB.open("varIn.FFTShift-FFT-FFTShift.vtk");
//          varIn.Print_VTK(DATEIB);
//          DATEIB.close();

//          std::ofstream DATEIC;
//          DATEIC.open("varTempWithOutExp.vtk");
//          temp.Print_VTK(DATEIC);
//          DATEIC.close();
          varFar = varIn;
          temp = Expi( 1.0*MYREAL(temp));
          varIn = varIn * temp;




          //varIn.FFTShift();
          varIn.inversFFT();
          //varIn.FFTShift();

//          std::ofstream DATEID;
//          DATEID.open("varTempD.vtk");
//          varIn.Print_VTK(DATEID);
//          DATEID.close();
//varFar = varFar;
         // varFar = varIn;
      }
void applyLens(VariableFFT& varIn,VariableFFT& varOPL, double wavenumber = 1)
306
{
307

308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
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
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
        Function2d1<std::complex<double>,double> Expi(expi);
        Function2d1<double, std::complex<double> > absolute(ABS);
        Function2d1<double, std::complex<double> > myRealPart(realPart);



        varIn = varIn * Expi(-1.0*myRealPart(varOPL) * wavenumber);



}

double C_4;
double focalLength;

void estimateBeamQuality(VariableFFT& varIn,
                         VariableFFT& Kx,
                         VariableFFT& Ky,
                         X_coordinate2d& X,
                         Y_coordinate2d& Y,
                         VariableFFT& temp,
                         double lambda)
{
    std::cout << "Beamquality not estimated! " << std::endl;
    return;

    Function2d1<double, std::complex<double> > absolute(ABS);
    Function2d1<double, std::complex<double> > myRealFunc(myReal);
    Function2d1<double, std::complex<double> > winkel(complexAngle);
    Local_stiffness_matrix2D<double> dx(*(varIn.Give_blockgrid()));
    dx.Calculate(d_dx(v_())*w_());
    Local_stiffness_matrix2D<double> dy(*(varIn.Give_blockgrid()));
    dy.Calculate(d_dy(v_())*w_());

    CalcContinuousArg cont(*(varIn.Give_blockgrid()));
    int gaussIterations = 100;
    Variable2D<double> f(*(varIn.Give_blockgrid()));
    Variable2D<double> TEMP(*(varIn.Give_blockgrid()));
     Variable2D<double>  ux(*(varIn.Give_blockgrid()));
     Variable2D<double>  uy(*(varIn.Give_blockgrid()));
     Variable2D<double>  px(*(varIn.Give_blockgrid()));
     Variable2D<double>  py(*(varIn.Give_blockgrid()));
     Variable2D<double>  unity(*(varIn.Give_blockgrid()));
     Variable2D<double>  Intensity(*(varIn.Give_blockgrid()));
     Variable2D<double>  IntensityFFT(*(varIn.Give_blockgrid()));
     Variable2D<double>  phase(*(varIn.Give_blockgrid()));
     Variable2D<double>  REALPART(*(varIn.Give_blockgrid()));

//     X_coordinate2d X(*(varIn.Give_blockgrid()));
//     Y_coordinate2d Y(*(varIn.Give_blockgrid()));
//    VariableFFT temp(varIn);


    unity = 1.0;
    Intensity = absolute(varIn) * absolute(varIn);

    temp = varIn;
    temp.FFT();
    IntensityFFT = absolute(temp) * absolute(temp);
    std::ofstream DATEIX;



//    phase = winkel(varIn) - 2.0*M_PI;
//    DATEIX.open("diff_phase_noncontinuous.vtk");
//    phase.Print_VTK(DATEIX);
//    DATEIX.close();

    cont.calcArg(phase,varIn);
    phase = phase * (-1.0);

//    DATEIX.open("diff_phase_from_calcArg.vtk");
//    phase.Print_VTK(DATEIX);
//    DATEIX.close();
//    DATEIX.open("diff_field.vtk");
//    varIn.Print_VTK(DATEIX);
//    DATEIX.close();


    double waveNumber = 2.0 * M_PI / lambda;
    phase = phase / waveNumber;
    //phase = phase;

     Local_stiffness_matrix2D<double> helm(*(varIn.Give_blockgrid())); // also in FEA_Def and solverFEA
    helm.Calculate(v_()*w_());


     (ux) = 0;
    Function2d1<double, std::complex<double> > myRealPart(realPart);
    REALPART = (myRealPart(varIn));
     f = (dy)(Intensity);

     for(int i=0;i<gaussIterations;++i)
     {
         TEMP = ux;
         (ux) = (ux) - ((helm)(ux) -f) / (helm).diag();
         if (L_infty(TEMP-ux) < 1e-13)
             i = gaussIterations;
     }
     f = (dx)(Intensity);
     for(int i=0;i<gaussIterations;++i)
     {
         TEMP = uy;
         (uy) = (uy) - ((helm)(uy) -f) / (helm).diag();
         if (L_infty(TEMP-uy) < 1e-13)
             i = gaussIterations;
     }
     f = (dx)(phase);
     for(int i=0;i<gaussIterations;++i)
     {
         TEMP = px;
         (px) = (px) - ((helm)(px) -f) / (helm).diag();
         if (L_infty(TEMP-px) < 1e-13)
             i = gaussIterations;
     }
     f = (dy)(phase);
     for(int i=0;i<gaussIterations;++i)
     {
         TEMP = py;
         (py) = (py) - ((helm)(py) -f) / (helm).diag();
         if (L_infty(TEMP-py) < 1e-13)
             i = gaussIterations;
     }


//         DATEIX.open("diff_phase_continuous.vtk");
//         px.Print_VTK(DATEIX);
//         DATEIX.close();
//     px = 2.0*X / focalLength / 2.0 + (X*X*X * 4.0 + 4.0 * X*Y*Y) * C_4;
//     //px = px * waveNumber;
//     DATEIX.open("diff_phase_continuous_analytic.vtk");
//     px.Print_VTK(DATEIX);
//     DATEIX.close();
//     py = 2.0*Y / focalLength / 2.0 + (Y*Y*Y * 4.0 + 4.0 * X*X*Y) * C_4;



//     VariableFFT Kx(varIn);
//     VariableFFT Ky(varIn);

//     double delx = varIn.getHx();
//     double dely = varIn.getHy();

//     double ukx = 2.0 * M_PI / varIn.getSizeX();
//     double uky = 2.0 * M_PI / varIn.getSizeY();

//     Kx = (X-0.5*delx) / delx * ukx ;
//     Ky = (Y-0.5*dely) / dely * uky ;
//     DATEIX.open("diff_Ky.vtk");
//     Ky.Print_VTK(DATEIX);
//     DATEIX.close();
//     DATEIX.open("diff_I_FFT.vtk");
//     IntensityFFT.Print_VTK(DATEIX);
//     DATEIX.close();


     double power = product(Intensity,unity);
     double powerFFT = product(IntensityFFT,unity);

 //    std::cout << "ratio of dx / ux " << delx / ukx << std::endl;
     f = myRealFunc(IntensityFFT * Kx * Kx);
     double phiX_FFT = product(f,unity) / powerFFT / waveNumber/ waveNumber;
     f = myRealFunc(IntensityFFT * Ky * Ky);
     double phiY_FFT = product(f,unity) / powerFFT / waveNumber/ waveNumber;
     double medianX = product(Intensity * X * X,unity) / power;
     double medianX4 = product(Intensity * X * X * X * X,unity) / power;
     double medianX6 = product(Intensity * X * X * X * X* X * X,unity) / power;
     double betaX = sqrt((medianX * medianX6 - medianX4 * medianX4)/(medianX4 * medianX4));
     double C4abb = 16.0 * M_PI / lambda * 0.816 * C_4 * medianX4 ;
     C4abb = 16.0 * M_PI / lambda * C_4 * medianX4 ;
     std::cout << "aberration due to C4 " << C4abb << std::endl;
     double medianY = product(Intensity * Y * Y,unity) / power;
     std::cout << "beamwaistX = " << 2.0 * sqrt(medianX) <<std::endl;
     std::cout << "beamwaistY = " << 2.0 * sqrt(medianY) <<std::endl;

     f = 1.0 / 4.0 / waveNumber / waveNumber / power * ux * ux / Intensity;
     f = f + 1.0 / power * Intensity * px * px;
     double phiX = product(f,unity);
     f = 1.0 / 4.0 / waveNumber / waveNumber / power * uy * uy / Intensity;
     f = f + 1.0 / power * Intensity * py * py;
     double phiY = product(f,unity);
     f = 1.0 / power * Intensity * X * px;
     double medianXphiX = product(f,unity);
     f = 1.0 / power * Intensity * Y * py;
     double medianYphiY = product(f,unity);
     double M2X = 2.0 * waveNumber * sqrt(fabs(medianX * phiX_FFT - medianXphiX * medianXphiX));
     double M2Y = 2.0 * waveNumber * sqrt(fabs(medianY * phiY_FFT - medianYphiY * medianYphiY));
     double M2X_direct = 2.0 * waveNumber * sqrt(fabs(medianX * phiX - medianXphiX * medianXphiX));
     double M2Y_direct = 2.0 * waveNumber * sqrt(fabs(medianY * phiY - medianYphiY * medianYphiY));

     double REffX = medianX / medianXphiX;
     double REffY = medianY / medianYphiY;

     f = medianX / power * ux * ux / Intensity;
     double M2diffX = sqrt(product(f,unity));
     f = Intensity * (px - X / REffX)*(px - X / REffX);
     double M2abbX = sqrt(product(f,unity) / power) * 2.0 * waveNumber * medianY;
     f = medianY / power * uy * uy / Intensity;
     double M2diffY = sqrt(product(f,unity));
     f = Intensity * (py - Y / REffY)*(py - Y / REffY);
     double M2abbY = sqrt(product(f,unity) / power) * 2.0 * waveNumber * medianY;

//     std::cout << "medianX = " << medianX <<std::endl;
//     std::cout << "medianX4 = " << medianX4 <<std::endl;
//     std::cout << "medianX6 = " << medianX6 <<std::endl;
//     std::cout << "medianY = " << medianY <<std::endl;
//     std::cout << "phiX = " << phiX <<std::endl;
//     std::cout << "phiY = " << phiY <<std::endl;
//     std::cout << "phiX_FFT = " << phiX_FFT <<std::endl;
//     std::cout << "phiY_FFT = " << phiY_FFT <<std::endl;


//     std::cout << "REffX = " << REffX <<std::endl;
//     std::cout << "REffY = " << REffY <<std::endl;
//     std::cout << "medianXphiX = " << medianXphiX <<std::endl;
//     std::cout << "medianYphiY = " << medianYphiY <<std::endl;


     std::cout << "M2X_with_fft " << M2X << std::endl;
     std::cout << "M2Y_with_fft " << M2Y << std::endl;
     std::cout << "M2X_direct " << M2X_direct << std::endl;
     std::cout << "M2Y_direct " << M2Y_direct << std::endl;

//     std::cout << "M2diffX " << M2diffX << std::endl;
//     std::cout << "M2abbX " << M2abbX << std::endl;
//     std::cout << "M2TotalX " << sqrt(M2abbX*M2abbX+M2diffX*M2diffX) << std::endl;
//     std::cout << "M2diffY " << M2diffY << std::endl;
//     std::cout << "M2abbY " << M2abbY << std::endl;
//     std::cout << "M2TotalY " << sqrt(M2abbY*M2abbY+M2diffY*M2diffY) << std::endl;
//     std::cout << "C4 / M2Abb " << C4abb / M2abbY << std::endl;
//     std::cout << "C4 / M2Abb " << C4abb / M2abbX << std::endl;
//     std::cout << "M2TotalY " << sqrt(M2abbY*M2abbY+M2diffY*M2diffY) << std::endl;

//         std::ofstream DATEIR;
//         DATEIR.open("diffDX.vtk");
//         ux.Print_VTK(DATEIR);
//         DATEIR.close();

    //CalcContinuousArg;
547

548
        
549
}
Phillip Lino Rall's avatar
Phillip Lino Rall committed
550

Blue Bird's avatar
Blue Bird committed
551
552
int main(int argc, char** argv) {
	std::ofstream DATEI;
Phillip Lino Rall's avatar
Phillip Lino Rall committed
553

554
    int n = 10;
Blue Bird's avatar
Blue Bird committed
555

556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
    double R1 = 100;
    double R2 = 100;

    focalLength = 100.0;

    int distanceIncrements = 15;
    double distance = 50 ;      //[mm]
    double dz = distance / double(distanceIncrements);
    lambda   = 1064e-6;  //[mm]
    radiusLoch = 0.2;   //[mm]


    distance = fabs(distanceFromWaist * 2.0);

    distance = 150 ;
    dz = distance / double(distanceIncrements);
    double geometrySize  =  5.0; //[mm]
    geometrySize =  8.0;

    radiusGauss = geometrySize / 4.0;   //[mm]
    rayleighrange = M_PI *radiusGauss*radiusGauss /lambda;
    std::cout << "rayleighrange " << rayleighrange<< std::endl;
    distanceFromWaist = -rayleighrange;
    distanceFromWaist = 0;
    curvature = distanceFromWaist * (1.0 + pow(rayleighrange / distanceFromWaist,2) );


Blue Bird's avatar
Blue Bird committed
583
584
    double wavenumber = 2.0 * M_PI / lambda;
    
585
586
587
    VTK_Reader reader()


588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
    cout << " Test CalcFresnel!!" << endl;

    std::cout << "distance = " << distance << std::endl;
    std::cout << "lambda = " << lambda << std::endl;
    std::cout << "aperture = " << radiusLoch << std::endl;
    std::cout << "geometrySize = " << geometrySize << std::endl;

    std::cout << "Fresnelnumber = " << radiusLoch*radiusLoch / distance / lambda << std::endl;
    std::cout << "Fresnelnumber >> 1 : near field "      << std::endl;
    std::cout << "Fresnelnumber ~  1 : fresnel zone"     << std::endl;
    std::cout << "Fresnelnumber << 1 : fraunhofer zone"  << std::endl;

    double deltaX = geometrySize / pow(2,n-1);
    double deltaXFourierPlane = lambda * distance / pow(2,n-1) / deltaX;
    std::cout << "deltaX initial plane (z = 0) = " << deltaX << std::endl;
    std::cout << "deltaX fourier plane (z = " <<distance<<")  = " << deltaXFourierPlane << std::endl;
    std::cout << "dxInitial / dxFourier   = " << deltaX / deltaXFourierPlane << std::endl;


Blue Bird's avatar
Blue Bird committed
607
    double radiusGeo = 0.5 * geometrySize;
608
    Rechteck geo(-radiusGeo, -radiusGeo, radiusGeo, radiusGeo);
Blue Bird's avatar
Blue Bird committed
609
    
610
611
612
613




614
    VariableFFT varE(n,n,geo);
615
    VariableFFT varIn(varE);
616
    VariableFFT varIntensity(varE);
617
    VariableFFT varPhase(varE);
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
    VariableFFT varTempX(varE);
    VariableFFT varTempY(varE);
    VariableFFT varRefr(varE);
    VariableFFT varPhaseLens(varE);
    VariableFFT temp(varE);
    VariableFFT varWavenumber(varE);
    Blockgrid2D* blockGridRec = varE.Give_blockgrid();
    X_coordinate2d X(*blockGridRec);
    Y_coordinate2d Y(*blockGridRec);
      VariableFFT Kx(varE);
      VariableFFT Ky(varE);


    double dx = varIn.getHx();
    double dy = varIn.getHy();

    double ukx = 2.0 * M_PI / varIn.getSizeX();
    double uky = 2.0 * M_PI / varIn.getSizeY();
636

637
638
639
640
    varTempX =(X  - 0.5*dx);
    varTempY =(Y  - 0.5*dy);
    Kx = varTempX / dx * ukx ;
    Ky = varTempY / dy * uky ;
641

642
643
644
    Function2d2<std::complex<double>,double> Aperture(gauss);
//    Function2d1<std::complex<double>,double> Expi(expi);
//    Function2d1<double, std::complex<double> > Sqrt(myRealSqrt);
645

646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
    //sampling criterion


    varE = Aperture(X,Y);

    C_4 = 0.00015;
    C_4 = 0.0;
//    std::ofstream DATEIX;

//    DATEIX.open("varE_bef_fft.vtk");
//    varE.Print_VTK(DATEIX);
//    DATEIX.close();

    //varE.FFT();
//    std::ofstream DATEIR;
//    DATEIR.open("varE_fft.vtk");
//    varE.Print_VTK(DATEIR);
//    DATEIR.close();

//    varE.inversFFT();
//    std::ofstream DATEIH;
//    DATEIH.open("varE_fft_ifft.vtk");
//    varE.Print_VTK(DATEIH);
//    DATEIH.close();


    varIn = varE;

    Function2d1<double, double > wurzel(sqrt);
    //varPhaseLens =(-1.83+1.0) * (X*X+Y*Y)/2.0 * (1.0 / R1 + 1.0 / R2) *wavenumber;

    varPhaseLens = (X*X + Y*Y) / 2.0 / focalLength + C_4 * ( X * X + Y * Y ) * ( X * X + Y * Y );


    //varPhaseLens = (X*X + Y*Y) / 2.0 / focalLength;// + C_4 * ( X * X + Y * Y ) * ( X * X + Y * Y );
    //    *convergenceVariable = n_0 * wurzel(absolut( 1 - alpha * alpha * ( X * X + Y * Y ) + alpha * alpha * 4.0 * ( X * X + Y * Y ) * ( X * X + Y * Y ) )); //with C4 aberration


    //varPhaseLens = wurzel(X*X + Y*Y) / focalLength;


    Function2d1<std::complex<double>, std::complex<double> > limitToOne(myLimitToOne);
    double alpha = 2.0 * M_PI /  distance / 2.0 ;//200.0;
    alpha = alpha / 2.0;

    alpha = 1.0/1000.0 * 2.0 * M_PI ;
    varRefr = wurzel( 1.5 * 1.5 * (1.0 - alpha * alpha * (X*X+Y*Y)));
   // varRefr = wurzel( 1.5 * 1.5 * (1.0 - alpha * alpha * (X*X)));
    //varRefr = wurzel(1.8 * 1.8 * (1 - alpha * (X*X)));

    varRefr = limitToOne(varRefr);
    varWavenumber = 2.0 * M_PI * varRefr / lambda;

    Function2d1<double, std::complex<double> > myRealFunc(myReal);
    std::cout << "Minimum( (myRealFunc(varRefr))) " << Minimum( (myRealFunc(varRefr))) << std::endl;
    std::cout << "Minimum( X) " << Minimum( (myRealFunc(X))) << std::endl;
    std::cout << "Maximum( X) " << Maximum( (myRealFunc(X))) << std::endl;
    std::cout << " Maximum( (myRealFunc(varRefr))" << Maximum( (myRealFunc(varRefr))) << std::endl;

    Function2d1<double, std::complex<double> > absolute(ABS);


    double refrAverage = 0.1 *  Minimum( (myRealFunc(varRefr))) + 0.9 * Maximum( (myRealFunc(varRefr)));
    double wavenumberAverage = 2.0 * M_PI * refrAverage / lambda;

    //double focalLength = 1.0 / refrAverage / alpha / sin(alpha * distance);
    double peroid = 2.0 * M_PI / alpha;
    std::cout << "focal  length approx: " << focalLength<< std::endl;
    std::cout << "peroid length approx: " << peroid<< std::endl;
    std::cout << "z-dir      increment: " << dz<< std::endl;
//    std::ofstream DATEIG;
//    DATEIG.open("varWavenumber.vtk");
//    varWavenumber.Print_VTK(DATEIG);
//    DATEIG.close();
//    DATEIG.open("varvarPhaseLens.vtk");
//    varPhaseLens.Print_VTK(DATEIG);
//    DATEIG.close();
//    DATEIG.open("varE___before.vtk");
//    varIn.Print_VTK(DATEIG);
//    DATEIG.close();

    double dzMax = M_PI / ( sqrt(wavenumberAverage*wavenumberAverage-pow((pow(2,n)/2-1) * ukx,2))
                           -sqrt(wavenumberAverage*wavenumberAverage-pow((pow(2,n)/2) * ukx,2))) ;

    if (dzMax < dz)
    {
        std::cout << "undersampling " << std::endl;
    }
    //virtualLensCorrection(distance,wavenumberAverage,varIn,varWavenumber);
    applyLens(varIn, varPhaseLens,wavenumber);
    estimateBeamQuality(varIn,Kx,Ky,X,Y,temp,lambda);

//        DATEIG.open("varE___before.vtk");
//        varIn.Print_VTK(DATEIG);
//        DATEIG.close();
//        spectrumPlaneWavePropagation(100,
//                     wavenumber,
//                     varIn,
//                     varE,Kx,Ky,X,Y,temp);
//        DATEIG.open("varE___FFT.vtk");
//        varE.Print_VTK(DATEIG);
//        DATEIG.close();
//        DATEIG.open("varE___after.vtk");
//        varIn.Print_VTK(DATEIG);
//        DATEIG.close();
    for (int iter = 0 ; iter < distanceIncrements;iter++)
    {
        std::cout << "progress : " << double(iter) / double(distanceIncrements) * 100 << "%" << std::endl;
        std::ofstream DATEIQ;
//        DATEIQ.open("varIn____befCorrection.vtk");
//        varIn.Print_VTK(DATEIQ);
//        DATEIQ.close();

//        virtualLensCorrection(dz,wavenumberAverage,varIn,varWavenumber);
//        DATEIQ.open("varIn____AftCorrection.vtk");
//        varIn.Print_VTK(DATEIQ);
//        DATEIQ.close();
        spectrumPlaneWavePropagation(dz,
                     wavenumber,
                     varIn,
                     varE,Kx,Ky,temp);
//        DATEIQ.open("varIn____AftPropagation.vtk");
//        varIn.Print_VTK(DATEIQ);
//        DATEIQ.close();
//        powerTest(varIn);


        //virtualLensCorrection(dz,wavenumberAverage,varIn,varWavenumber);



        int plotevery = 1;
        if (iter % plotevery == 0)
        {

            estimateBeamQuality(varIn,Kx,Ky,X,Y,temp,lambda);

            std::ofstream DATEIA;

            DATEIA.open("varEAtPlane_newshift"+std::to_string(iter / plotevery)+".vtk");
            varIntensity = absolute(varIn) * absolute(varIn);
            varIntensity.Print_VTK(DATEIA);
            DATEIA.close();

//            std::ofstream DATEIB;

//            DATEIB.open("varEFFT"+std::to_string(iter / plotevery)+".vtk");
//            varE.Print_VTK(DATEIB);
//            DATEIB.close();
            if (iter == 101)
                iter += 500;
        }
    }
799
800
801



Blue Bird's avatar
Blue Bird committed
802
803
804
805
806
807
808
809
810
811
//    VariableFFT varB(varA);
/*
    Blockgrid2D* blockGridRec = varE.Give_blockgrid();
    
	X_coordinate2d X(*blockGridRec);
	Y_coordinate2d Y(*blockGridRec);
*/

    

812
813


Blue Bird's avatar
Blue Bird committed
814
    
815
816
817
818
819
820
//    CalcFresnel(loch,
//                 distance,
//                 wavenumber,
//                 varIn,
//                 varE);

821
822
823
824
825
826
//    spectrumPlaneWave(loch,
//                 distance,
//                 wavenumber,
//                 varIn,
//                 varE);
    spectrumPlaneWave(gauss,
Blue Bird's avatar
Blue Bird committed
827
828
829
                 distance,
                 wavenumber,
                 varIn,
830
                 varE,X,Y,Kx,Ky,temp);
831

832
   // varE = varE / L_infty(  varE);
833

834

Blue Bird's avatar
Blue Bird committed
835
836
837
838
839
840
841
842
843
844
	std::ofstream DATEIA;     
     
    DATEIA.open("varIn.vtk");
	varIn.Print_VTK(DATEIA);
	DATEIA.close();    
    
    DATEIA.open("varE.vtk");
	varE.Print_VTK(DATEIA);
	DATEIA.close();

845
846
847
//    DATEIA.open("varEimag.vtk");
//	varE.Print_VTK(DATEIA,imPart);
//	DATEIA.close();
Blue Bird's avatar
Blue Bird committed
848
     
849
850
851
852
853
//    DATEIA.open("varEreal.vtk");
//	varE.Print_VTK(DATEIA,realPart);
//	DATEIA.close();

    cout << " Test CalcFresnel finished!!" << endl;
Phillip Lino Rall's avatar
Phillip Lino Rall committed
854
855
856

}