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


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

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


double isZero(double x)
{
25
    return fabs(x) < 1e-10?1.0:0.0;
Phillip Lino Rall's avatar
Phillip Lino Rall committed
26
27
}

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

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

34
35
36
37
38
std::complex<double> expComplex(std::complex<double>  x) {
   return exp(x.real());
}


Blue Bird's avatar
Blue Bird committed
39
40
41
std::complex<double> sinExp(double x) {
   return sin(x);
}
Phillip Lino Rall's avatar
Phillip Lino Rall committed
42

Blue Bird's avatar
Blue Bird committed
43
44
45
std::complex<double> cosExp(double x) {
   return cos(x);
}
Phillip Lino Rall's avatar
Phillip Lino Rall committed
46

Blue Bird's avatar
Blue Bird committed
47
double realPart(std::complex<double> x) {
48
   return x.real();
Blue Bird's avatar
Blue Bird committed
49
}
Phillip Lino Rall's avatar
Phillip Lino Rall committed
50

Blue Bird's avatar
Blue Bird committed
51
double imPart(std::complex<double> x) {
52
   return x.imag();
Blue Bird's avatar
Blue Bird committed
53
}
Phillip Lino Rall's avatar
Phillip Lino Rall committed
54

55
56
57
58
double complexAngle(std::complex<double> x) {
    return (std::arg(x)+2.0*M_PI);
 }

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

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));
73
    if (curvature == 0.0 || std::isnan(curvature))
74
75
76
77
78
79
80
81
    {
        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));
    }
}
82
std::complex<double> spalt(double x, double y) {
83
   if(fabs(x) < radiusLoch) return 1.0;
84
85
   return 0.0;
}
86
87
88
89
90
91
92
93
94
95
96
97
98
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;
}

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

106
107
    X_coordinate2d X(*blockGridRec);
    Y_coordinate2d Y(*blockGridRec);
Blue Bird's avatar
Blue Bird committed
108

109
    Function2d2<std::complex<double>,double> Aperture(aperture);
Blue Bird's avatar
Blue Bird committed
110
    Function2d1<std::complex<double>,double> Expi(expi);
111

112
    varFar = Aperture(X,Y) * Expi(wavenumber * (X*X+Y*Y) / (2.0 * distance));
Blue Bird's avatar
Blue Bird committed
113
114
115
    varIn  = varFar;
    varFar.FFT();
}
Phillip Lino Rall's avatar
Phillip Lino Rall committed
116

117
118
119
120
121

void spectrumPlaneWave(std::complex<double> (*aperture) (double x, double y),
                       double distance,
                       double wavenumber,
                       VariableFFT& varIn,
122
123
124
125
126
127
                       VariableFFT& varFar,
                       X_coordinate2d& X,
                       Y_coordinate2d& Y,
                       VariableFFT& Kx,
                       VariableFFT& Ky,
                       VariableFFT& temp) {
128
129
          Blockgrid2D* blockGridRec = varFar.Give_blockgrid();

130
131
132
133
//          X_coordinate2d X(*blockGridRec);
//          Y_coordinate2d Y(*blockGridRec);
//          VariableFFT Kx(varFar);
//          VariableFFT Ky(varFar);
134

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

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

141
142
//          Kx = X / dx * ukx ;
//          Ky = Y / dy * uky ;
143
144

          Function2d2<std::complex<double>,double> Aperture(aperture);
145
146
          Function2d1<double, std::complex<double> > absolute(ABS);
          Function2d1<double, std::complex<double> > myRealFunc(myReal);
147
148
149
150
          Function2d1<std::complex<double>,double> Expi(expi);
          Function2d1<double, std::complex<double> > Sqrt(myRealSqrt);


151
152
153
154
155
156
157
158
159
160
          //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));

161
162
163
          varFar = Aperture(X,Y);
          varIn  = varFar;
          varFar.FFT();
164
          varFar = varFar * Expi( myRealFunc(temp));
165
166
167
          varFar.inversFFT();
      }

168
169
170
171
172
173
void amplification(double dz,
                   VariableFFT& pumppower,
                   VariableFFT& varIn,
                   VariableFFT& varWavenumber,
                   VariableFFT& temp)
{
Phillip Lino Rall's avatar
Phillip Lino Rall committed
174
175
    double c = 3e11;// mm / s
    //c = 3e8;// m / s
176
177
    double planck = 6.626e-34;
    double emissionCrosssection = 7.7* 1e-18;//mm²
Phillip Lino Rall's avatar
Phillip Lino Rall committed
178
179
180
181
182
183
184
//    emissionCrosssection = 7.7* 1e-20;//cm²
//    emissionCrosssection = 7.7* 1e-24;//m²
    double upperLevelLifetime = 230e-6;
    double Ntot = 1.3e+17; //( 1 / mm^3)
    //Ntot = 1.3e+26; //( 1 / m^3)

    //pumppower : W / mm³
185
186

    VariableFFT photonDensity(temp);
Phillip Lino Rall's avatar
Phillip Lino Rall committed
187
188
    VariableFFT inversion(temp);
    VariableFFT pumpPhotons(temp);
189
190
    Function2d1<double, std::complex<double> > absolute(ABS);
    photonDensity = absolute(varIn) * absolute(varIn)/ (planck * c / lambda) / dz;
Phillip Lino Rall's avatar
Phillip Lino Rall committed
191
    pumpPhotons = pumppower / (planck * c / lambda); // N / s mm³
192
193
194
195
196
197
    // falsch : alle pumpphotonen werden zur verstärkung genutzt -> anpassen

    std::cout << "pumppower "<< L_infty(pumppower) << std::endl;
    std::cout << "inversion "<< L_infty(temp) << std::endl;
    std::cout << "photonDensity "<< L_infty(photonDensity) << std::endl;
    Function2d1<std::complex<double>,std::complex<double>> Exp(expComplex);
Phillip Lino Rall's avatar
Phillip Lino Rall committed
198
199
200
201
202
203
204
205
206


    inversion = pumpPhotons / (emissionCrosssection * pumpPhotons * photonDensity * c + 1.0 / upperLevelLifetime + pumpPhotons / Ntot);

    std::cout << "pumppower "<< L_infty(pumppower) << std::endl;
    std::cout << "photonDensity "<< L_infty(photonDensity) << std::endl;
    std::cout << "inversion "<< L_infty(inversion) << std::endl;
    std::cout << "pumpPhotons "<< L_infty(pumpPhotons) << std::endl;
    temp= emissionCrosssection * inversion * photonDensity * dz   ;
207
208
209
210
211
212
213
214
215
216
217
218
219
    std::cout << "arg exp temp "<< L_infty(temp) << std::endl;
    temp = Exp(temp);
    std::cout << "exp temp "<< L_infty(temp) << std::endl;

        std::ofstream DATEIC;
        DATEIC.open("varGAIN.vtk");
        temp.Print_VTK(DATEIC);
        DATEIC.close();

    varIn = varIn * temp;


}
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
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

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;

349
350
          temp = varIn;

351
352
353
354
355
356
357
358
359
360
361
362
363
364
365



          //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)
366
{
367

368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
        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;

383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
void estimateBeamWaist(VariableFFT& varIn,
                       X_coordinate2d& X,
                       Y_coordinate2d& Y)
{

    Variable2D<double>  unity(*(varIn.Give_blockgrid()));
    Variable2D<double>  Intensity(*(varIn.Give_blockgrid()));
   unity = 1.0;
   Function2d1<double, std::complex<double> > absolute(ABS);
   Intensity = absolute(varIn) * absolute(varIn);


    double power = product(Intensity,unity);
    double medianX = product(Intensity * X * X,unity) / power;
    double medianY = product(Intensity * Y * Y,unity) / power;
398
    std::cout << "power = " << power <<std::endl;
399
400
401
402
    std::cout << "beamwaistX = " << 2.0 * sqrt(medianX) <<std::endl;
    std::cout << "beamwaistY = " << 2.0 * sqrt(medianY) <<std::endl;
}

403
404
405
406
407
408
409
410
void estimateBeamQuality(VariableFFT& varIn,
                         VariableFFT& Kx,
                         VariableFFT& Ky,
                         X_coordinate2d& X,
                         Y_coordinate2d& Y,
                         VariableFFT& temp,
                         double lambda)
{
411
412
//    std::cout << "Beamquality not estimated! " << std::endl;
//    return;
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
547
548
549
550
551
552
553
554
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
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610

    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;

611
612
613
614
     std::cout << "M2diffX " << M2diffX << std::endl;
     std::cout << "M2abbX " << M2abbX << std::endl;
     std::cout << "M2diffY " << M2diffY << std::endl;
     std::cout << "M2abbY " << M2abbY << std::endl;
615
616
617
618
619
620
621
622
623
624
625
//     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;
626

627

628
}
Phillip Lino Rall's avatar
Phillip Lino Rall committed
629

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

633
    int n = 8;
Blue Bird's avatar
Blue Bird committed
634

635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
    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]
652
    geometrySize =  1.0;
653
654

    radiusGauss = geometrySize / 4.0;   //[mm]
655
    radiusGauss = 0.2;
656
657
658
659
660
661
    rayleighrange = M_PI *radiusGauss*radiusGauss /lambda;
    std::cout << "rayleighrange " << rayleighrange<< std::endl;
    distanceFromWaist = -rayleighrange;
    distanceFromWaist = 0;
    curvature = distanceFromWaist * (1.0 + pow(rayleighrange / distanceFromWaist,2) );

Phillip Lino Rall's avatar
Phillip Lino Rall committed
662
663
    double radiusGeo = 0.5 * geometrySize;
    Rechteck geo(-radiusGeo, -radiusGeo, radiusGeo, radiusGeo);
664

Blue Bird's avatar
Blue Bird committed
665
    double wavenumber = 2.0 * M_PI / lambda;
666
667


668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
    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;
Phillip Lino Rall's avatar
Phillip Lino Rall committed
685
    std::cout << "curvature " << curvature << std::endl;
686
687


Phillip Lino Rall's avatar
Phillip Lino Rall committed
688
689
    VTK_Reader reader(         QString("C:/Users/rall/FAUbox/Promotion/Vectorial_BPM/fibercryst_exmaple/RefractionIndexTherm_last.vtk"));
    VTK_Reader readerPumppower(QString("C:/Users/rall/FAUbox/Promotion/Vectorial_BPM/fibercryst_exmaple/abspower.vtk"));
690
    Variable<double> * thermalRefractiveIndex3D = reader.give_first_variable();
691
    Variable<double> * pumpPowerRaytracing = readerPumppower.give_first_variable();
692

693

694
695
696
//    Unstructured_grid *ug = new Cylinder(2,1,20);
//    Blockgrid *bg = new Blockgrid(ug,10,10,20);
//    Variable<double> * thermalRefractiveIndex3D = new Variable<double>(*bg);
Phillip Lino Rall's avatar
Phillip Lino Rall committed
697

698
699


700
    UGFrom3DSlice slice(thermalRefractiveIndex3D->Give_Ug(),0.0);
701
    UGFrom3DSlice slicePumppower(pumpPowerRaytracing->Give_Ug(),0.0);
702
703

    Blockgrid2DFrom3D D2block(&slice,thermalRefractiveIndex3D->Give_blockgrid());
704
    Blockgrid2DFrom3D D2blockPumppower(&slicePumppower,pumpPowerRaytracing->Give_blockgrid());
705
    Variable2D<std::complex<double> > vardDeltaNComplex(D2block);
706
707
    Variable2D<std::complex<double> > varPumppowerComplex(D2blockPumppower);

708
    Variable2D<double > varDeltaN(D2block);
709
    Variable2D<double > varPumppower(D2blockPumppower);
Phillip Lino Rall's avatar
Phillip Lino Rall committed
710
    IteratorZDirection zIterator(thermalRefractiveIndex3D->Give_blockgrid());
711
712
713
    zIterator.next();
   varDeltaN.interpolateSlizeZ(thermalRefractiveIndex3D,0.0);

714

715
    vardDeltaNComplex = varDeltaN;
716
    VariableFFT varE(n,n,geo);
717
718

    VariableFFT deltaN(varE);
719
    VariableFFT pumppower(varE);
720
721
722

    deltaN.interpolate(vardDeltaNComplex,0.0);

723
    VariableFFT varIn(varE);
724
    VariableFFT varIntensity(varE);
725
    VariableFFT varPhase(varE);
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
    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();
744

745
746
747
748
    varTempX =(X  - 0.5*dx);
    varTempY =(Y  - 0.5*dy);
    Kx = varTempX / dx * ukx ;
    Ky = varTempY / dy * uky ;
749

750
751
752
    Function2d2<std::complex<double>,double> Aperture(gauss);
//    Function2d1<std::complex<double>,double> Expi(expi);
//    Function2d1<double, std::complex<double> > Sqrt(myRealSqrt);
753

754
755
756
757
758
    //sampling criterion


    varE = Aperture(X,Y);

Phillip Lino Rall's avatar
Phillip Lino Rall committed
759
760
761
762
763
    double power = 1.0;

    double amp = sqrt(2.0 * power / M_PI / radiusGauss / radiusGauss);
    varE = varE * amp;

764
765
766
767
768
    std::ofstream DATEIG;
    DATEIG.open("/local/er96apow/FFT_results/varE___before.vtk");
    varE.Print_VTK(DATEIG);
    DATEIG.close();

769
    C_4 = 0.00015;
770
    //C_4 = 0.0;
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
799
800
801
802
803
804
805
806
807
808
//    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;

809
    double n0 = 1.83;
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
    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;
835

836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
//    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);
854
855
//    applyLens(varIn, varPhaseLens,wavenumber);
//    estimateBeamQuality(varIn,Kx,Ky,X,Y,temp,lambda);
856

857

858
859
860
861
//        spectrumPlaneWavePropagation(100,
//                     wavenumber,
//                     varIn,
//                     varE,Kx,Ky,X,Y,temp);
862
863

    for (int iter = 0 ; iter < zIterator.getSize();iter++)
864
    {
865
866

        std::cout << "progress : " << double(iter) / double(zIterator.getSize()) * 100 << "%" << std::endl;
867
        std::ofstream DATEIQ;
868
        varDeltaN.interpolateSlizeZ(thermalRefractiveIndex3D,zIterator.get_z());
869
870

        varPumppower.interpolateSlizeZ(pumpPowerRaytracing, zIterator.get_z());
871
         vardDeltaNComplex = varDeltaN;
872
         varPumppowerComplex = varPumppower;
873
         deltaN.interpolate(vardDeltaNComplex,0.0);
874
875
         pumppower.interpolate(varPumppowerComplex,0.0);

876
877
878
879
880
881
882
883
884
885
886



//         deltaN = 0.0;
//         n0 = 1.0;

        varWavenumber = (n0 + deltaN) * 2.0 * M_PI / lambda;
        double wavenumberAverage_ = 0.1 *  Minimum( (myRealFunc(varWavenumber))) + 0.9 * Maximum( (myRealFunc(varWavenumber)));


        virtualLensCorrection(zIterator.get_hz(),wavenumberAverage_,varIn,varWavenumber, temp);
887
        amplification(zIterator.get_hz(),pumppower,varIn,varWavenumber, temp);
888
        spectrumPlaneWavePropagation( zIterator.get_hz(),
889
890
891
892
893
894
                     wavenumber,
                     varIn,
                     varE,Kx,Ky,temp);



895
        int plotevery = 10;
896
897
898
        if (iter % plotevery == 0)
        {

899
            //estimateBeamQuality(varIn,Kx,Ky,X,Y,temp,lambda);
900
            estimateBeamWaist(varIn,X,Y);
901
902


903
904
905

            std::ofstream DATEIA;

906
            DATEIA.open("/local/er96apow/FFT_results/fibercryst_lowres_"+std::to_string(iter)+".vtk");
907
908
909
910
            varIntensity = absolute(varIn) * absolute(varIn);
            varIntensity.Print_VTK(DATEIA);
            DATEIA.close();

911
912
913
//            DATEIA.open("/local/er96apow/FFT_results/fibercryst_fftspace_lowres_"+std::to_string(iter)+".vtk");
//            temp.Print_VTK(DATEIA);
//            DATEIA.close();
914
915

//            vardDeltaNComplex.interpolateSlizeZ(varIntensity,0.0);
916

917
918
919
920
921
922
//            DATEIA.open("/local/er96apow/FFT_results/interpolated_"+std::to_string(iter)+".vtk");
//            //varIntensity = absolute(varIn) * absolute(varIn);
//            vardDeltaNComplex.Print_VTK(DATEIA);
//            DATEIA.close();
//            if (iter == 101)
//                iter += 500;
923
        }
924
925

        zIterator.next();
926
    }
927
928
929



Blue Bird's avatar
Blue Bird committed
930
931
932
//    VariableFFT varB(varA);
/*
    Blockgrid2D* blockGridRec = varE.Give_blockgrid();
933
934
935

    X_coordinate2d X(*blockGridRec);
    Y_coordinate2d Y(*blockGridRec);
Blue Bird's avatar
Blue Bird committed
936
937
938
*/


939
940


941
942


943
944
945
946
947
948
//    CalcFresnel(loch,
//                 distance,
//                 wavenumber,
//                 varIn,
//                 varE);

949
950
951
952
953
//    spectrumPlaneWave(loch,
//                 distance,
//                 wavenumber,
//                 varIn,
//                 varE);
954
955
956
957
958
//    spectrumPlaneWave(gauss,
//                 distance,
//                 wavenumber,
//                 varIn,
//                 varE,X,Y,Kx,Ky,temp);
959

960
//   // varE = varE / L_infty(  varE);
961

962

963
//    std::ofstream DATEIA;
964

965
966
967
//    DATEIA.open("varIn.vtk");
//    varIn.Print_VTK(DATEIA);
//    DATEIA.close();
968

969
970
971
//    DATEIA.open("varE.vtk");
//    varE.Print_VTK(DATEIA);
//    DATEIA.close();
Blue Bird's avatar
Blue Bird committed
972

973
974
975
//    DATEIA.open("varEimag.vtk");
//	varE.Print_VTK(DATEIA,imPart);
//	DATEIA.close();
976

977
978
979
980
981
//    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
982
983
984

}