-
Notifications
You must be signed in to change notification settings - Fork 1
/
propagator.cu
executable file
·1718 lines (1375 loc) · 63.8 KB
/
propagator.cu
1
2
3
4
5
6
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
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
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
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
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
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
799
800
801
802
803
804
805
806
807
808
809
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
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#ifdef _ENABLE_CUDA
#include "cuda_extmath.h"
#endif
#include "ionization.h"
void calculate_NLresponse(f_complex* input, f_complex* output);
void calculate_Hlike_response(f_complex* input, f_complex* output, int N, float_type* Zeff, float_type* alpha);
#ifdef _ENABLE_CUDA
__device__ float_type* cuda_plasma_func;
__constant__ float_type cuda_plasma_factor_re;
__constant__ float_type cuda_plasma_factor_im;
__device__ float_type* cuda_raman_func;
__device__ float_type* cuda_wavenum;
__device__ float_type* cuda_omega;
__device__ float_type* cuda_hodisp;
__constant__ __device__ int cuda_N_T;
__constant__ float_type cuda_TMIN;
__constant__ float_type cuda_TMAX;
__constant__ float_type cuda_TSTEP;
__constant__ float_type cuda_OMEGA_MAX;
__constant__ float_type cuda_OMEGA_MIN;
__constant__ float_type cuda_IONIZATION_POTENTIAL;
__constant__ float_type cuda_RECOMBINATION_TAU;
__constant__ float_type cuda_IONIZATION_POTENIAL;
#ifdef MULTI_LEVEL_IONIZATION
__device__ float_type* cuda_IONIZATION_POTENTIALS;
__device__ int cuda_IONIZATION_LEVEL_N;
#endif
__constant__ float_type cuda_AMBIENT_CARRIER_DENSITY;
__constant__ float_type cuda_PONDEROMOTIVE_COEFFICIENT;
__constant__ float_type cuda_RAMAN_FRACTION;
__constant__ float_type cuda_N4;
__constant__ float_type cuda_N2;
#ifdef THIRD_HARMONICS
__constant__ float_type cuda_th_factor;
#endif
__constant__ float_type cuda_AVALANCHE_CROSSSECTION;
__constant__ float_type cuda_GROUP_VELOCITY;
__constant__ float_type cuda_kxstep;
__constant__ float_type cuda_kystep;
__constant__ float_type cuda_wavenum0;
__constant__ float_type cuda_omega0;
__constant__ float_type cuda_NEUTRAL_DENSITY;
#ifdef MULTIPHOTON_IONIZATION
__constant__ int cuda_K_MPI;
__constant__ float_type cuda_BETA_MPI_LN;
#else
__constant__ float_type* cuda_IONIZATION_RATE_LN;
#endif
__device__ float_type plasma_source_function_device(float_type reA, float_type imA, float_type ro);
__device__ float_type photoionization_function_device(float_type reA, float_type imA, float_type ro);
__device__ float_type photoionization_function_device2(float_type reA, float_type imA, float_type ro1, float_type ro2);
__device__ float_type photoabsorbtion_function_device(float_type reA, float_type imA, float_type ro);
__device__ float_type avalanche_ionization_function_device(float_type reA, float_type imA, float_type ro);
__device__ float_type recombination_function_device(float_type ro);
#ifdef MULTI_LEVEL_IONIZATION
__device__ void photoionization_functionsN_device(float_type reA, float_type imA, float_type* W, int stride);
#endif
__device__ void getpolar(float_type* X, float_type* ro, float_type* phi);
__device__ void calculate_plasmadensity_small_device(float_type* field, float_type* pro, float_type* buf);
__device__ void calculate_plasmadensity_small_device_strided (float_type* field, float_type* pro, int stride, float_type* buf);
__device__ void calculate_plasmadensity_small_device_strided_2float(float_type* field, float_type* pro, int stride, float_type* buf);
__device__ void calculate_plasmadensity_losses_small_device(float_type* field, float_type* pro, int stride, int rostride, float_type* loss, float_type* buf);
__device__ float_type calculate_maxplasmadensity_small_device (float_type* field, float_type* buf);
//__device__ void calculate_single_NLresponse_kernel (float_type* input, float_type* output, int N_T, float_type* bufs);
__device__ void calculate_single_NLresponse_kernel_strided(float_type* input, float_type* output, int N_T, float_type* bufs);
//__device__ void calculate_single_NLresponse_kernel_strided(float_type* field, float_type* out, float_type* tempb, int stride);
__global__ void calculate_Lresponse_kernel(float_type* field, float_type* buf1, float_type* kt2_, float_type ZSTEP, size_t N_T, size_t n);
__global__ void calculate_NLresponse_kernel (f_complex* input, f_complex* output, float_type zstep, int N_T, float_type* bufs);
__global__ void calculate_NLresponse_kernel_strided(f_complex* input, f_complex* output, float_type zstep, int N_T, float_type* bufs, float_type* maA2, float_type* maxNL2);
__global__ void calculate_plasma_2float_kernel (f_complex* input, float_type* output, f_complex* buf);
__global__ void calculate_maxplasma_kernel (f_complex* input, float_type* output, f_complex* buf);
#ifdef YUDIN_IVANOV_CORRECTION
__device__ float_type YI_Phi_device(float_type theta, float_type g);
#endif
__device__ inline float_type device_hfgaussfilter(float_type omega) {float_type domega = (omega/cuda_OMEGA_MAX-1)/ABSORBTION_LAYER_WIDTH; return (domega<0)?(float_type)1.0:exp_f(-ABSORBTION_LAYER_BETA*domega*domega);}
__device__ inline float_type device_lfgaussfilter(float_type omega) {float_type domega = (omega*(1-ABSORBTION_LAYER_WIDTH)/cuda_OMEGA_MIN-1); return (domega>0)?(float_type)1.0:exp_f(-ABSORBTION_LAYER_BETA*domega*domega);}
//__device__ inline float_type device_hfgaussfilter(float_type omega) {float_type domega = (2.0*omega/cuda_OMEGA_MAX-1)/0.99; return exp_p(-pow_p(domega, (float_type)100));}
//__device__ inline float_type device_lfgaussfilter(float_type omega) {return (float_type)1.0;}
f_complex* plasma_func_;
bool cuda_do_extra_memtransfer = true;
bool cuda_use_pinned_memory = false;
float_type* cuda_field;
float_type* cuda_buf1;
float_type* cuda_buf2;
float_type* cuda_bufs;
float_type* cuda_maxA2;
float_type* cuda_maxNL2;
size_t cuda_device_freemem=0, cuda_device_totalmem=0;
size_t cuda_pointN = 0, cuda_piece_size = 0;
size_t cuda_blocksize = 64, cuda_maxbuf_len = 0;
#endif
void calculate_NLstep(float_type* maxI, float_type* maxNL2);
void calculate_Lstep ();
void propagator()
{
#ifndef _SILENCE
#ifdef _SHOW_EVERY_STEP
if (ISMASTER) printf("\nCalculating nonlinear responses...");fflush(stdout);
#endif
#endif
while (true)
{
float_type max_NL2 = 0;
float_type max_A2 = 0;
calculate_NLstep(&max_A2, &max_NL2);
float_type max_NL2_0 = max_NL2, max_A2_0 = max_A2;
MPI_Allreduce(&max_NL2_0, &max_NL2, 1, MPI_FLOAT_TYPE, MPI_MAX, MPI_COMM_WORLD);
MPI_Allreduce(&max_A2_0, &max_A2, 1, MPI_FLOAT_TYPE, MPI_MAX, MPI_COMM_WORLD);
bool repeatstep = false;
#ifndef _ODE_EULER
if (ZSTEP*ZSTEP*max_NL2 > MAX_TOLERANCE*MAX_TOLERANCE*max_A2) {repeatstep = true; STEP_N++;}
#endif
ZSTEP = 0.5*MAX_TOLERANCE*sqrt(max_A2/max_NL2);
#ifdef _UNIAXIAL_FINITE_DIFFERENCE
ZSTEP = min(ZSTEP, LIN_ZSTEP);
#endif
#ifdef _SHOW_EVERY_STEP
if (ISMASTER) { printf("Done. (%5.2fs)Z=%e, max_A2=%e, max_NL2=%e, Zstep=%e", MPI_Wtime()-TIME_START,CURRENT_Z, max_A2, max_NL2, ZSTEP); fflush(stdout);}
#endif
if (ZSTEP < MINSTEP_RATIO*(ZNET[n_Z]-ZNET[n_Z-1]) || !(ZSTEP > 0))
{
printf("[%d]: max_A2=%e, max_NL2=%e, Zstep=%e", PROCESS_RANK, max_A2, max_NL2, ZSTEP);
throw "Minimum Z step size reached, collapse is possible!";
}
ZSTEP = min(ZSTEP, ZNET[n_Z]-CURRENT_Z);
if (!repeatstep)
{
#ifndef _UNIAXIAL_FINITE_DIFFERENCE
#ifndef NO_DIFFRACTION
#ifdef _SHOW_EVERY_STEP
if (ISMASTER) {printf("\nRunning Fourier transform for nonlinear response array... "); fflush(stdout); }
#endif
#ifdef _UNIAXIAL
#ifdef _ENABLE_CUDA
if (!cuda_do_extra_memtransfer) ht_run((f_complex*)cuda_buf1, (f_complex*)cuda_bufs);
else
#endif
ht_run(BIGBUFFER1, FIELD);
#else
fftwt_execute(FFT_FWPLAN_XY);
#endif
#ifdef _SHOW_EVERY_STEP
if (ISMASTER) {printf("Done"); fflush(stdout); }
#endif
#endif
#ifdef _SHOW_EVERY_STEP
if (ISMASTER) {printf("\nCalculating field change during propagation step... "); fflush(stdout); }
#endif
calculate_Lstep();
#ifdef _SHOW_EVERY_STEP
if (ISMASTER) {printf("Done."); fflush(stdout); }
#endif
#ifndef NO_DIFFRACTION
#ifdef _SHOW_EVERY_STEP
if (ISMASTER) {printf("\nRunning Fourier transform for field array... "); fflush(stdout); }
#endif
#ifdef _UNIAXIAL
#ifdef _ENABLE_CUDA
if (!cuda_do_extra_memtransfer)
{
ht_run((f_complex*)cuda_field, (f_complex*)cuda_bufs);
(cudaMemcpy(FIELD, cuda_field, cuda_piece_size, cudaMemcpyDeviceToHost));
}
else
#endif
ht_run(FIELD, BIGBUFFER1);
#else
fftwt_execute(FFT_BWPLAN_XY);
for (size_t i=0; i<N_T*MY_NX*MY_NY; i++) FIELD[i] /= N_X*N_Y;
#endif
#ifdef _SHOW_EVERY_STEP
if (ISMASTER) {printf("\nDone. "); fflush(stdout); }
#endif
#endif
#else
#ifdef _SHOW_EVERY_STEP
if (ISMASTER) {printf("\nCalculating field change during propagation step using finite differences... "); fflush(stdout); }
#endif
calculate_Lstep_UAFD();
#ifdef _SHOW_EVERY_STEP
if (ISMASTER) {printf("Done."); fflush(stdout); }
#endif
#endif
if (APERTURE_N > 0) if (CURRENT_Z >= APERTURE_Z[APERTURE_N - 1])
{
for (int nx=0; nx<MY_NX; nx++)
for (int ny=0; ny<MY_NY; ny++)
{
#ifdef _UNIAXIAL
#ifndef _UNIAXIAL_FINITE_DIFFERENCE
float_type R = HT_PLAN->x_n(nx+MY_NXstart)*XMAX;
#else
float_type R = XMAX*(nx+MY_NXstart)/N_X;
#endif
#else
float_type x = XMIN+nx*XSTEP, y = YMIN+(ny+MY_NYstart)*YSTEP;
float_type R = sqrt(x*x+y*y);
#endif
if (R > APERTURE_R[APERTURE_N - 1]) for (int nw=0; nw<N_T; nw++) FIELD[nw + N_T*(nx+MY_NX*ny)]=0.0;
}
#ifndef _UNIAXIAL_FINITE_DIFFERENCE
memcpy(BIGBUFFER2, FIELD, MY_NX*MY_NY*N_T*sizeof(f_complex));
#ifdef _UNIAXIAL
ht_run(BIGBUFFER2, BIGBUFFER1);
#else
fftwt_mpi_execute_dft(FFT_FWPLAN_XY, (fftwt_complex*)BIGBUFFER2, (fftwt_complex*)BIGBUFFER2);
#endif
#endif
if (ISMASTER) {printf("\n Aperture #%d of radius %g is applied", APERTURE_N, APERTURE_R[APERTURE_N-1]); fflush(stdout);}
APERTURE_N--;
}
return;
}
}
}
void calculate_NLstep(float_type* maxI, float_type* maxNL2)
{
#ifdef _ENABLE_CUDA
f_complex* cuda_field_piece;
float_type* maxA2buf, *maxNL2buf;
#ifdef _ODE_RK4
int bufs_size_factor = 10;
#endif
#ifdef _ODE_HEUN
int bufs_size_factor = 8;
#endif
#ifdef _ODE_EULER
int bufs_size_factor = 7;
#endif
if (STEP_N == 0)
{
int devicenum = 0;
(cudaGetDevice(&devicenum));
(cudaMemGetInfo(&cuda_device_freemem, &cuda_device_totalmem));
cuda_pointN = (int)pow(2.0,floor_(log2((double)(cuda_device_freemem/N_T/(bufs_size_factor+1)/sizeof(f_complex)))));
if (cuda_pointN > MY_NX*MY_NY) cuda_pointN=MY_NX*MY_NY;
if (cuda_blocksize > cuda_pointN) cuda_blocksize=cuda_pointN;
cuda_piece_size = N_T*cuda_pointN*sizeof(f_complex);
cuda_maxbuf_len = sizeof(float_type)*cuda_pointN;
printf("\n[%d]: Launching on CUDA device %d. It has %ld bytes of memory free. Allocating for %ld points", PROCESS_RANK, devicenum, cuda_device_freemem, cuda_pointN); fflush(stdout);
if (cuda_device_freemem > N_T*MY_NX*MY_NY*(bufs_size_factor+3)*sizeof(f_complex) &&false)
{
cuda_do_extra_memtransfer = false;
if (ISMASTER) printf("\n Storing all data in CUDA device memory.");
(cudaMalloc((void**)&cuda_field, cuda_piece_size));
(cudaMalloc((void**)&cuda_buf1, cuda_piece_size));
(cudaMalloc((void**)&cuda_buf2, cuda_piece_size));
(cudaMemcpy(cuda_field, FIELD, cuda_piece_size, cudaMemcpyHostToDevice));
(cudaMemcpy(cuda_buf2, BIGBUFFER2, cuda_piece_size, cudaMemcpyHostToDevice));
(cudaMalloc((void**)&cuda_bufs, bufs_size_factor*cuda_piece_size));
(cudaMalloc((void**)&cuda_maxA2, cuda_maxbuf_len));
(cudaMalloc((void**)&cuda_maxNL2, cuda_maxbuf_len));
}
if (cuda_use_pinned_memory)
{
if (ISMASTER) printf("\n Using page-locked memory. ");
(cudaHostRegister(FIELD, cuda_piece_size, 0));
(cudaHostRegister(BIGBUFFER1, cuda_piece_size, 0));
(cudaHostRegister(BIGBUFFER2, cuda_piece_size, 0));
}
}
maxA2buf = (float_type*)malloc_ch(sizeof(float_type)*cuda_pointN); maxNL2buf = (float_type*)malloc_ch(sizeof(float_type)*cuda_pointN);
if (cuda_do_extra_memtransfer)
{
(cudaMalloc((void**)&cuda_field_piece, cuda_piece_size));
(cudaMalloc((void**)&cuda_bufs, bufs_size_factor*cuda_piece_size));
(cudaMalloc((void**)&cuda_maxA2, cuda_maxbuf_len));
(cudaMalloc((void**)&cuda_maxNL2, cuda_maxbuf_len));
for (long i=0; i<MY_NX*MY_NY; i+= cuda_pointN)
{
(cudaMemcpy(cuda_field_piece, FIELD+i*N_T, cuda_piece_size, cudaMemcpyHostToDevice)) ;
calculate_NLresponse_kernel_strided<<< cuda_pointN/cuda_blocksize,cuda_blocksize >>>(cuda_field_piece, cuda_field_piece, ZSTEP, N_T, cuda_bufs, cuda_maxA2, cuda_maxNL2);
(cudaMemcpy(BIGBUFFER1+i*N_T, cuda_field_piece, cuda_piece_size, cudaMemcpyDeviceToHost)) ;
(cudaMemcpy(maxA2buf, cuda_maxA2, cuda_maxbuf_len, cudaMemcpyDeviceToHost));
(cudaMemcpy(maxNL2buf, cuda_maxNL2, cuda_maxbuf_len, cudaMemcpyDeviceToHost));
//for (int np=0; np<cuda_pointN; np++) { (*maxI) = max((*maxI), maxA2buf[np]); (*maxNL2) = max((*maxNL2), maxNL2buf[np]);}
}
for (long i=0; i<MY_NX*MY_NY*N_T; i++)
{
(*maxI) = max((*maxI), abs2(FIELD[i]));
(*maxNL2) = max((*maxNL2), abs2(BIGBUFFER1[i]));
}
}
else
{
calculate_NLresponse_kernel_strided<<< cuda_pointN/cuda_blocksize,cuda_blocksize >>>((f_complex*)cuda_field, (f_complex*)cuda_buf1, ZSTEP, N_T, cuda_bufs, cuda_maxA2, cuda_maxNL2);
(cudaMemcpy(maxA2buf, cuda_maxA2, cuda_maxbuf_len, cudaMemcpyDeviceToHost));
(cudaMemcpy(maxNL2buf, cuda_maxNL2, cuda_maxbuf_len, cudaMemcpyDeviceToHost));
for (int np=0; np<cuda_pointN; np++) { (*maxI) = max((*maxI), maxA2buf[np]); (*maxNL2) = max((*maxNL2), maxNL2buf[np]);}
}
//for (int i=0; i<MY_NX*MY_NY*N_T; i++) {(*maxI) = max((*maxI), abs2(FIELD[i])); (*maxNL2)=max((*maxNL2), abs2(BIGBUFFER1[i])); }
if (cuda_do_extra_memtransfer)
{
(cudaFree(cuda_maxA2));
(cudaFree(cuda_maxNL2));
(cudaFree(cuda_field_piece));
(cudaFree(cuda_bufs));
}
free(maxA2buf); free(maxNL2buf);
#else
float_type max_I = 0, max_NL2 = 0;
for (int ny=0; ny<MY_NY; ny++) for (int nx=0; nx<MY_NX; nx++)
{
int ofs0 = N_T*(nx+MY_NX*ny);
#ifdef NONLINEARITY_ON
#ifdef _ODE_EULER
calculate_NLresponse(FIELD+ofs0, BIGBUFFER1+ofs0);
#endif
#ifdef _ODE_RK4
calculate_NLresponse(FIELD+ofs0, NL_OUTPUT1);
for (int nt=0; nt<N_T; nt++) NL_INPUT[nt] = FIELD[ofs0+nt] + ZSTEP*(float_type)0.5*NL_OUTPUT1[nt];
calculate_NLresponse(NL_INPUT, NL_OUTPUT2);
for (int nt=0; nt<N_T; nt++) NL_INPUT[nt] = FIELD[ofs0+nt] + ZSTEP*(float_type)0.5*NL_OUTPUT2[nt];
calculate_NLresponse(NL_INPUT, NL_OUTPUT3);
for (int nt=0; nt<N_T; nt++) NL_INPUT[nt]= FIELD[ofs0+nt] + ZSTEP*NL_OUTPUT3[nt];
calculate_NLresponse(NL_INPUT, NL_OUTPUT4);
#endif
#ifdef _ODE_HEUN
calculate_NLresponse(FIELD+ofs0, NL_OUTPUT1);
for (int nt=0; nt<N_T; nt++) NL_INPUT[nt] = FIELD[ofs0+nt] + ZSTEP*NL_OUTPUT1[nt];
calculate_NLresponse(NL_INPUT, NL_OUTPUT2);
#endif
#endif
for (int nt=0; nt<N_T; nt++)
{
#ifdef NONLINEARITY_ON
#ifdef _ODE_RK4
BIGBUFFER1[ofs0+nt] = (NL_OUTPUT1[nt] + (float_type)2.0*NL_OUTPUT2[nt] + (float_type)2.0*NL_OUTPUT3[nt] + NL_OUTPUT4[nt])/(float_type)6.0;
#endif
#ifdef _ODE_HEUN
BIGBUFFER1[ofs0+nt] = (NL_OUTPUT1[nt] + NL_OUTPUT2[nt])/(float_type)2.0;
#endif
#else
BIGBUFFER1[ofs0+nt] = 0.0;
#endif
max_NL2 = max(max_NL2, abs2(BIGBUFFER1[ofs0+nt]));
max_I = max(max_I, abs2(FIELD [ofs0+nt]));
}
}
(*maxI)=max_I; (*maxNL2)=max_NL2;
#endif
}
#ifdef _UNIAXIAL_FINITE_DIFFERENCE
void calculate_Lstep_UAFD()
{
f_complex j = f_complex(0,1);
MPI_Status mpistatus;
for (int nw=0; nw<N_T; nw++)
{
f_complex* kappa = BIGBUFFER2 +nw*(MY_NX);
f_complex* khi = BIGBUFFER2+(N_T+nw)*(MY_NX);
f_complex khibuf, kappabuf, fieldbuf;
//calculate alpha, beta and gamma and, finally, khi and kappa.
if (PROCESS_RANK > 0)
{
// printf("\n[%d]:Revceiving kappa and khi with tag %d from %d...", PROCESS_RANK, nw, PROCESS_RANK-1); fflush(stdout);
MPI_Recv(&khibuf, 2, MPI_FLOAT_TYPE, PROCESS_RANK-1, nw, MPI_COMM_WORLD, &mpistatus);
MPI_Recv(&kappabuf, 2, MPI_FLOAT_TYPE, PROCESS_RANK-1, nw, MPI_COMM_WORLD, &mpistatus);
MPI_Recv(&fieldbuf, 2, MPI_FLOAT_TYPE, PROCESS_RANK-1, nw, MPI_COMM_WORLD, &mpistatus);
// printf("[%d]:Success! ", PROCESS_RANK); fflush(stdout);
float_type rm = XMAX*(MY_NXstart-1)/N_X, r = XMAX*(MY_NXstart)/N_X, rp = XMAX*(1+MY_NXstart)/N_X;
float_type hm = r-rm, hp=rp-r;
f_complex S = -j*ZSTEP/(real(WAVENUMBER[nw]))/(float_type)4.0;
f_complex A = S/hm*((float_type)2.0/(rp-rm)-(float_type)1.0/(r+rm));
f_complex C = S/hp*((float_type)2.0/(rp-rm)+(float_type)1.0/(rp+r));
f_complex B = S*((float_type)1.0/hm/(r+rm) - (float_type)1.0/hp/(rp+r) - (float_type)2.0/hm/hp) - (float_type)1.0;
f_complex D = -A*fieldbuf - (B+(float_type)2.0)*FIELD[nw] - C*FIELD[nw+N_T] - BIGBUFFER1[nw]*ZSTEP;
khi[0] = (D-A*khibuf)/(A*kappabuf+B);
kappa[0] = -C /(A*kappabuf+B);
}
else {khi[0]=0; kappa[0]=1;}
for (int nx=1; nx<MY_NX; nx++)
{
float_type rm = XMAX*(nx+MY_NXstart-1)/N_X, r = XMAX*(nx+MY_NXstart)/N_X, rp = XMAX*(nx+1+MY_NXstart)/N_X;
float_type hm = r-rm, hp=rp-r;
size_t ofs = nw+N_T*nx;
#ifndef NO_SPACE_TIME_FOCUSING
float_type k = real(WAVENUMBER[nw]);
#else
float_type k = real(WAVENUMBER0);
#endif
f_complex S = -j*ZSTEP/(real(WAVENUMBER[nw]))/(float_type)4.0;
f_complex A = S/hm*((float_type)2.0/(rp-rm)-(float_type)1.0/(r+rm));
f_complex C = S/hp*((float_type)2.0/(rp-rm)+(float_type)1.0/(rp+r));
f_complex B = S*((float_type)1.0/hm/(r+rm) - (float_type)1.0/hp/(rp+r) - (float_type)2.0/hm/hp) - (float_type)1.0;
f_complex D = -A*FIELD[ofs-N_T] - (B+(float_type)2.0)*FIELD[ofs] - C*FIELD[ofs+N_T] - BIGBUFFER1[ofs]*ZSTEP;
khi[nx] = (D-A*khi[nx-1])/(A*kappa[nx-1]+B);
kappa[nx] =-C /(A*kappa[nx-1]+B);
}
if (PROCESS_RANK < PROCESS_N-1)
{
// printf("\n[%d]:Sending kappa and khi with tag %d to %d...", PROCESS_RANK, nw, PROCESS_RANK+1); fflush(stdout);
MPI_Send(khi+MY_NX-1, 2, MPI_FLOAT_TYPE, PROCESS_RANK+1, nw, MPI_COMM_WORLD);
MPI_Send(kappa+MY_NX-1, 2, MPI_FLOAT_TYPE, PROCESS_RANK+1, nw, MPI_COMM_WORLD);
MPI_Send(FIELD+nw+N_T*(MY_NX-1), 2, MPI_FLOAT_TYPE, PROCESS_RANK+1, nw, MPI_COMM_WORLD);
// printf("[%d]:Success!", PROCESS_RANK); fflush(stdout);
}
}
for (int nw=0; nw<N_T; nw++)
{
f_complex* kappa = BIGBUFFER2 +nw*(MY_NX);
f_complex* khi = BIGBUFFER2+(N_T+nw)*(MY_NX);
if (PROCESS_RANK < PROCESS_N-1) MPI_Recv(FIELD+nw+MY_NX*N_T, 2, MPI_FLOAT_TYPE, PROCESS_RANK+1, nw, MPI_COMM_WORLD, &mpistatus);
else FIELD[nw+MY_NX*N_T] = 0;//-(khi[MY_NX-1])/(kappa[MY_NX-1]-(float_type)1.0); //Dierichlet boundary outer boundary condition
for (int nx=MY_NX-1; nx>=0; nx--)
{
size_t ofs = nw+N_T*nx;
FIELD[ofs] = FIELD[ofs+N_T]*kappa[nx]+khi[nx];
}
if (PROCESS_RANK > 0) MPI_Send(FIELD+nw, 2, MPI_FLOAT_TYPE, PROCESS_RANK-1, nw, MPI_COMM_WORLD);
}
for (int nw=0; nw<N_T; nw++) for (int nx=0; nx<=MY_NX; nx++) FIELD[nw+N_T*nx] *= exp(ZSTEP*(-j*HO_DISPERSION[nw]+imag(WAVENUMBER[nw])));
}
#endif
void calculate_Lstep()
{
#ifdef _ENABLE_CUDA
float_type* kt2 = (float_type*)malloc_ch(sizeof(float_type)*N_Y*MY_NX_FT);
float_type* cuda_kt2;
if (cuda_do_extra_memtransfer) (cudaMalloc((void**)&cuda_kt2, sizeof(float_type)*N_Y*MY_NX_FT));
else cuda_kt2 = cuda_bufs;
#ifndef _UNIAXIAL
float_type kxstep = (float_type)2.0*M_PI/(XMAX-XMIN);
float_type kystep = (float_type)2.0*M_PI/(YMAX-YMIN);
#endif
for (size_t nx=0; nx<MY_NX_FT; nx++) for (size_t ny=0; ny<N_Y; ny++)
{
#ifdef _UNIAXIAL
float_type kt = 2*M_PI*HT_PLAN->x_n(nx+MY_NXstart_FT)*HT_PLAN->getNf()/XMAX;
kt2[nx+MY_NX_FT*ny] = kt*kt;
#else
size_t nx_g = nx+MY_NXstart_FT;
float_type kx=0; if (nx_g<=N_X/2) kx=kxstep*nx_g; else kx=-kxstep*(N_X-nx_g);
float_type ky=0; if (ny<=N_Y/2) ky=kystep*ny; else ky=-kystep*(N_Y-ny);
kt2[ny+N_Y*nx] = kx*kx+ky*ky;
#endif
}
(cudaMemcpy(cuda_kt2, kt2, sizeof(float_type)*N_Y*MY_NX_FT, cudaMemcpyHostToDevice));
free(kt2);
float_type* cuda_field_piece, *cuda_buf_piece;
if (cuda_do_extra_memtransfer)
{
(cudaMalloc((void**)&cuda_field_piece, cuda_piece_size));
(cudaMalloc((void**)&cuda_buf_piece, cuda_piece_size));
for (size_t i=0; i<N_Y*MY_NX_FT; i+= cuda_pointN)
{
(cudaMemcpy(cuda_field_piece, BIGBUFFER2+i*N_T, cuda_piece_size, cudaMemcpyHostToDevice)) ;
(cudaMemcpy(cuda_buf_piece, BIGBUFFER1+i*N_T, cuda_piece_size, cudaMemcpyHostToDevice)) ;
calculate_Lresponse_kernel<<< cuda_pointN/cuda_blocksize,cuda_blocksize >>>(cuda_field_piece, cuda_buf_piece, cuda_kt2, ZSTEP, N_T, i);
(cudaMemcpy(FIELD+i*N_T, cuda_field_piece, cuda_piece_size, cudaMemcpyDeviceToHost));
(cudaMemcpy(BIGBUFFER2+i*N_T, cuda_field_piece, cuda_piece_size, cudaMemcpyDeviceToHost));
}
(cudaFree(cuda_field_piece));
(cudaFree(cuda_buf_piece));
(cudaFree(cuda_kt2));
}
else
{
calculate_Lresponse_kernel<<< cuda_pointN/cuda_blocksize,cuda_blocksize >>>(cuda_buf2, cuda_buf1, cuda_kt2, ZSTEP, N_T, 0);
(cudaMemcpy(cuda_field, cuda_buf2, cuda_piece_size, cudaMemcpyDeviceToDevice));
// (cudaMemcpyAsync(BUGBUFFER2, cuda_buf2, cuda_piece_size, cudaMemcpyDeviceToHost));
}
#else
#ifndef _UNIAXIAL
float_type kxstep = (float_type)2.0*M_PI/(XMAX-XMIN);
float_type kystep = (float_type)2.0*M_PI/(YMAX-YMIN);
#endif
f_complex j=f_complex(0,1);
for (size_t nx=0; nx<MY_NX_FT; nx++) for (size_t ny=0; ny<N_Y; ny++)
{
float_type kt2 = 0;
#ifdef _UNIAXIAL
float_type kt = 2*M_PI*HT_PLAN->x_n(nx+MY_NXstart_FT)*HT_PLAN->getNf()/XMAX;
kt2 = kt*kt;
#else
size_t nx_g = nx+MY_NXstart_FT;
float_type kx=0; if (nx_g<=N_X/2) kx=kxstep*nx_g; else kx=-kxstep*(N_X-nx_g);
float_type ky=0; if (ny<=N_Y/2) ky=kystep*ny; else ky=-kystep*(N_Y-ny);
kt2 = kx*kx+ky*ky;
#endif
for (size_t nw = 0; nw<N_T; nw++)
{
size_t ofs = (nw + N_T*(ny+N_Y*nx));
float_type w = OMEGA[nw];
#ifndef NO_SPACE_TIME_FOCUSING
f_complex k = WAVENUMBER[nw];
#else
f_complex k = WAVENUMBER0;
#endif
if (w<OMEGA_MIN || w > OMEGA_MAX) {FIELD[ofs]=0.0;continue;}
#ifndef NO_DIFFRACTION
if (kt2>MAX_KT2*real(k*k)) {FIELD[ofs]=0.0;continue;}
#endif
//float_type f = lfgaussfilter(w)*hfgaussfilter(w);
#ifndef NO_DIFFRACTION
float_type k1 = real(k), k2=imag(k);
#ifdef PARABOLICAL_DIFRACTION
BIGBUFFER2[ofs] += ZSTEP*BIGBUFFER1[ofs];
FIELD[ofs] = (BIGBUFFER2[ofs]*exp(ZSTEP*(j*(kt2/2/k1-HO_DISPERSION[nw])-k2)));// + ZSTEP*BIGBUFFER1[ofs]);
#else
//f_complex kz = sqrt(k*k - kt2);
float_type sqHO = sqrtHO(-kt2/k1/k1);
BIGBUFFER2[ofs] += ZSTEP*BIGBUFFER1[ofs]/(1+sqHO);
FIELD[ofs] = (BIGBUFFER2[ofs]*exp(ZSTEP*(j*(-HO_DISPERSION[nw]-sqHO*k1)+k2)));// + ZSTEP*BIGBUFFER1[ofs]/(1+sqHO));
#endif
#else
FIELD[ofs] = BIGBUFFER2[ofs]*exp(-ZSTEP*j*HO_DISPERSION[nw]) + ZSTEP*BIGBUFFER1[ofs];
#endif
BIGBUFFER2[ofs]=FIELD[ofs];
}
}
#endif
}
void calculate_NLresponse(f_complex* input, f_complex* output)
{
//This function calculates nonlinear response at one spatial net point.
f_complex j = f_complex(0,1);
#ifndef NONLINEARITY_ON
for (int nt=0; nt<N_T; nt++) output[nt] = 0;
return;
#endif
#ifndef H_LIKE_RESPONSE
fftwt_execute_dft(FFT_BWPLAN_T, (fftwt_complex*)input, (fftwt_complex*)FIELD_REAL_SMALL);
fftwt_Nnormalize(1, FIELD_REAL_SMALL);
#ifndef NO_PLASMARESPONSE
calculate_plasmadensity_losses_small(FIELD_REAL_SMALL, (float_type*)NL_SMALLBUFFER3, 2, NL_SMALLBUFFER1, NL_SMALLBUFFER4); //calculate plasma density at this point
//float_type tstep = (TMAX - TMIN)/N_T;
for (int nt=0; nt<N_T; nt++)
{
float_type ro = real(NL_SMALLBUFFER3[nt]);
#ifndef PLASMA_FULL_DISPERSION
f_complex E = FIELD_REAL_SMALL[nt];
#ifdef PLASMA_DISPERSION
NL_SMALLBUFFER3[nt] = ro*E; //multiply plasma density by field
#else
f_complex Fro = ro*PLASMA_FACTOR, sFro = 0;
if (fabs(real(Fro))<0.1) sFro = sqrtHO(-Fro);
else sFro = sqrt((float_type)1.0-Fro)-(float_type)1.0;
NL_SMALLBUFFER1[nt] += - j*WAVENUMBER0*sFro*E;
#endif
#else
f_complex w0 = exp((float_type)2.0*(float_type)M_PI*j*((float_type)nt)/((float_type)N_T));
f_complex M = 1.0/N_T;
for (int nw=0; nw<N_T; nw++)
{
f_complex Fro_ = PLASMA_FUNC[nw]*ro, sFro_ = 0;
if (fabs(real(Fro_))<0.1) sFro_ = sqrtHO(+Fro_);
else sFro_ = sqrt((float_type)1.0+Fro_)-(float_type)1.0;
NL_SMALLBUFFER1[nt]+=-j*WAVENUMBER[nw]*sFro_*input[nw]*M;
M*=w0;
}
#endif
}
#ifdef PLASMA_DISPERSION
fftwt_execute_dft(FFT_FWPLAN_T, (fftwt_complex*)NL_SMALLBUFFER1, (fftwt_complex*)NL_SMALLBUFFER2); //These two lines execute forward Fourier transform 1->2 and 3->1
fftwt_execute_dft(FFT_FWPLAN_T, (fftwt_complex*)NL_SMALLBUFFER3, (fftwt_complex*)NL_SMALLBUFFER1); //i.e. for ro*E and for gamma*E, where ro is plasma density, E - field and gamma - PA losses
for (int nw=0; nw<N_T; nw++)
{
NL_SMALLBUFFER1[nw] *= PLASMA_FUNC[nw];
NL_SMALLBUFFER2[nw] += NL_SMALLBUFFER1[nw]; //now NL_SMALLBUFFER2 contains all ionization responses.
}
#else
fftwt_execute_dft(FFT_FWPLAN_T, (fftwt_complex*)NL_SMALLBUFFER1, (fftwt_complex*)NL_SMALLBUFFER2);
#endif
#else
for (int nw=0; nw<N_T; nw++) {NL_SMALLBUFFER2[nw]=0; NL_SMALLBUFFER4[nw]=1.0;}
#endif
if (NONLIN_REFRINDEX != 0)
{
for (int nt=0; nt<N_T; nt++) NL_SMALLBUFFER3[nt] = abs2(FIELD_REAL_SMALL[nt]); //put intensity into NL_SMALLBUFFER2
if (RAMAN_FRACTION > 0.001)
{
//Calculate delayed nonlinearity response using intensity Fourier-transform
fftwt_execute_dft(FFT_FWPLAN_T, (fftwt_complex*)NL_SMALLBUFFER3, (fftwt_complex*)NL_SMALLBUFFER1);
for (int nw=0; nw<N_T; nw++) NL_SMALLBUFFER1[nw] *= RAMAN_FUNCTION[nw];
fftwt_execute_dft(FFT_BWPLAN_T, (fftwt_complex*)NL_SMALLBUFFER1, (fftwt_complex*)NL_SMALLBUFFER3);
fftwt_Nnormalize(1,NL_SMALLBUFFER3);
for (int nt=0; nt<N_T; nt++) { f_complex E=FIELD_REAL_SMALL[nt]; NL_SMALLBUFFER3[nt]*=E;}
}
else
for (int nt=0; nt<N_T; nt++) { f_complex E=FIELD_REAL_SMALL[nt]; NL_SMALLBUFFER3[nt]*=E;}
fftwt_execute_dft(FFT_FWPLAN_T, (fftwt_complex*)NL_SMALLBUFFER3, (fftwt_complex*)NL_SMALLBUFFER1);
for (int nw=0; nw<N_T; nw++) { NL_SMALLBUFFER2[nw] += -j*KERR_PROFILE[nw]*NL_SMALLBUFFER1[nw];}
#ifdef THIRD_HARMONICS
for (int nt=0; nt<N_T; nt++)
{
float_type carrier_phase = (OMEGA0 == OMEGA[0])?(-2.0*OMEGA0*(TMIN+nt*(TMIN-TMAX)/N_T)):0;
f_complex E=FIELD_REAL_SMALL[nt]; NL_SMALLBUFFER3[nt]=E*E*E*exp(j*carrier_phase);
}
fftwt_execute_dft(FFT_FWPLAN_T, (fftwt_complex*)NL_SMALLBUFFER3, (fftwt_complex*)NL_SMALLBUFFER1);
for (int nw=0; nw<N_T; nw++) { NL_SMALLBUFFER2[nw] += -j*KERR_TH_PROFILE[nw]*NL_SMALLBUFFER1[nw];}
#endif
}
if (NONLIN_REFRINDEX4 != 0)
{
for (int nt=0; nt<N_T; nt++)
{ f_complex E=FIELD_REAL_SMALL[nt]; float_type I = abs2(E);
NL_SMALLBUFFER3[nt]=I*I*E;
#ifdef THIRD_HARMONICS
float_type carrier_phase = (OMEGA0 == OMEGA[0])?(-2.0*OMEGA0*(TMIN+nt*(TMIN-TMAX)/N_T)):0;
NL_SMALLBUFFER3[nt] += TH_FACTOR*(I*E*E*E*exp(j*carrier_phase)/2.0 + E*E*E*E*E*exp(2.0*j*carrier_phase));
#endif
}
fftwt_execute_dft(FFT_FWPLAN_T, (fftwt_complex*)NL_SMALLBUFFER3, (fftwt_complex*)NL_SMALLBUFFER1);
#ifndef NO_SHOCK
for (int nw=0; nw<N_T; nw++) { NL_SMALLBUFFER2[nw] += -j*OMEGA[nw]/LIGHT_VELOCITY*NL_SMALLBUFFER3[nw];}
#else
for (int nw=0; nw<N_T; nw++) { NL_SMALLBUFFER2[nw] += -j*OMEGA[0]/LIGHT_VELOCITY*NL_SMALLBUFFER3[nw];}
#endif
}
/*
for (int nt=0; nt<N_T; nt++)
{
f_complex E = FIELD_REAL_SMALL[nt];
float_type I = abs2(E);
#ifndef THIRD_HARMONICS
NL_SMALLBUFFER3[nt] *= E*NL_SMALLBUFFER4[nt];
NL_SMALLBUFFER3[nt] += NONLIN_REFRINDEX4*I*I*E*NL_SMALLBUFFER3[nt];
#else
float_type carrier_phase = (OMEGA0 == OMEGA[0])?(-2.0*OMEGA0*(TMIN+nt*(TMIN-TMAX)/N_T)):0;
if (NL_SMALLBUFFER3[nt]) = NL
NL_SMALLBUFFER3[nt] = NONLIN_REFRINDEX*(NL_SMALLBUFFER3[nt] + (1-RAMAN_FRACTION)*((float_type)(1.0/3.0))*TH_FACTOR*E*E*exp(j*carrier_phase))*E*NL_SMALLBUFFER4[nt];
NL_SMALLBUFFER3[nt] += NONLIN_REFRINDEX4*(I*I + TH_FACTOR*((float_type)(1.0/2.0)*exp(j*carrier_phase)*I*E*E + (float_type)(1.0/10.0)*exp((float_type)2.0*j*carrier_phase)*E*E*E*E))*E;
#endif
}
fftwt_execute_dft(FFT_FWPLAN_T, (fftwt_complex*)NL_SMALLBUFFER3, (fftwt_complex*)NL_SMALLBUFFER1);
for (int nw=0; nw<N_T; nw++)
{
#ifndef NO_SHOCK
float_type w = OMEGA[nw];
#else
float_type w = OMEGA0;
#endif
output[nw] = -j*(KERR_PROFILE*NL_SMALLBUFFER1[nw]) + NL_SMALLBUFFER2[nw];
}
}
else*/
for (int nw=0; nw<N_T; nw++) output[nw] = NL_SMALLBUFFER2[nw];
#else
float_type Zeff[2] = {1, 1};
float_type alpha[2] = {1, 1};
calculate_Hlike_response(input, output, 2, Zeff, alpha);
#endif
// for (int nw=0; nw<N_T; nw++) {float_type w_ = OMEGA[nw]; output[nw]*=hfgaussfilter(w_)*lfgaussfilter(w_);}
}
void calculate_Hlike_response(f_complex* input, f_complex* output, int N, float_type* Zeff, float_type* alpha)
{
float_type Xmax = 20;
int Nx = 256;
float_type dt0 = 0.2;
f_complex j = f_complex(0,1);
int Nt = (TMAX-TMIN)/ATOMIC_TIME/dt0;
int tfac = (int)exp2(ceil(log2((float_type)Nt/(float_type)N_T)));
Nt = N_T*tfac;
float_type dt =(TMAX-TMIN)/Nt/ATOMIC_TIME;
f_complex* E = (f_complex*)malloc_ch(Nt*sizeof(f_complex));
f_complex* Er = (f_complex*)malloc_ch(Nt*sizeof(f_complex));
memcpy(E, input, N_T/2*sizeof(f_complex));
memcpy(E+(Nt-N_T/2), input+N_T/2, N_T/2*sizeof(f_complex));
for (int nt=N_T/2; nt<(Nt-N_T/2); nt++) E[nt]=0;
fftwt_plan plan = fftwt_plan_dft_1d(Nt, (fftwt_complex*)E, (fftwt_complex*)Er, FFTW_BACKWARD, FFTW_ESTIMATE);
fftwt_execute(plan);
fftwt_destroy_plan(plan);
free(E);
#ifndef _UNWRAP_FRQUENCIES
for (int nt=0; nt<Nt; nt++) Er[nt] *= exp(j*OMEGA0*(TMIN+dt*nt*ATOMIC_TIME))*(float_type)FIELD_DENOM/(ATOMIC_FIELD_*N_T);
#else
for (int nt=0; nt<Nt; nt++) Er[nt] *= (float_type)FIELD_DENOM/(ATOMIC_FIELD_);
#endif
float_type dx = Xmax/Nx;
f_complex* f = (f_complex*)malloc_ch(2*N*Nx*sizeof(f_complex)); for (int n=0; n<2*N; n++) for (int nx=0; nx<Nx; nx++) f[nx+n*Nx]=1.0;
f_complex* gamma = (f_complex*)malloc_ch(2*N*Nx*sizeof(f_complex));
f_complex* kappa = (f_complex*)malloc_ch(2*N*Nx*sizeof(f_complex));
float_type* Ixn = (float_type*)malloc_ch(6*N*sizeof(float_type));
float_type Eo = real(Er[0]), En = 0;
for (int nt=1; nt < Nt; nt++)
{
En = real(Er[nt]);
for (int n=0; n<2*N; n++) { gamma[n*Nx] = 0.0; kappa[n*Nx] = 1.0; }
for (int nx=1; nx<(Nx-1); nx++)
{
float_type x = (nx+1)*dx;
float_type A = dt/dx/dx - dt*(1.0/x - 1.0)/2.0/dx;
float_type C = dt/dx/dx + dt*(1.0/x - 1.0)/2.0/dx;
for (int n=0; n<2*N; n++)
{
float_type z = (n<N) ? Zeff[n] : (-Zeff[n-N]);
float_type Enc = En/z/z/z;
float_type Eoc = Eo/z/z/z;
f_complex B = f_complex(-2.0*dt/dx/dx + dt*x*Enc/2.0, 1.0);
f_complex D = f_complex(-2.0*dt/dx/dx + dt*x*Eoc/2.0,-1.0);
f_complex F = -A*f[nx-1 + n*Nx] - C*f[nx + 1 + n*Nx] - D*f[nx + n*Nx];
f_complex G = (A*kappa[nx-1+n*Nx]+B);
kappa[nx + n*Nx] = -C/G;
gamma[nx + n*Nx] = (F-A*gamma[nx -1 + n*Nx])/G;
}
}
for (int n=0; n<2*N; n++) f[Nx-1 + n*Nx] = gamma[Nx-2 + n*Nx]/((float_type)1.0-kappa[Nx-2 + n*Nx]);
for (int nx=Nx-1; nx>0; nx--) for (int n=0; n<2*N; n++) f[nx-1 + n*Nx] = kappa[nx-1 + n*Nx]*f[nx + n*Nx] + gamma[nx-1 + n*Nx];
Eo = En;
if ((nt % tfac) == 0)
{
for (int i=0; i<3; i++)
{
// float_type x1 = dx, F1 = exp(-x1)*pow(x1,i);
for (int n=0; n<2*N; n++) Ixn[i+3*n] = 0;
for (int nx=0; nx<Nx; nx++)
{
float_type x = (nx+1)*dx, F1 = exp(-x)*pow(x, i);
for (int n=0; n<2*N; n++) Ixn[i+3*n] += abs2(f[nx+n*Nx])*F1;
//F1 = F2;
}
for (int n=0; n<2*N; n++) Ixn[i+3*n] *= dx;
}
NL_SMALLBUFFER1[nt/tfac] = 0;
for (int n=0; n<N; n++) NL_SMALLBUFFER1[nt/tfac] += (Ixn[2+3*n]*Ixn[0+3*(n+N)] - Ixn[0+3*n]*Ixn[2+3*(n+N)])/(Ixn[1+3*n]*Ixn[0+3*(n+N)] + Ixn[0+3*n]*Ixn[1+3*(n+N)])*alpha[n]/Zeff[n];
NL_SMALLBUFFER1[nt/tfac] *= 0.5*ELECTRON_CHARGE*BOHR_RADIUS*NEUTRAL_DENSITY;
}
}
free(f); free(gamma); free(kappa); free(Er); free(Ixn);
#ifndef _UNWRAP_FREQUENCIES
NL_SMALLBUFFER1[0] = 0; for (int nt=1; nt<N_T; nt++) NL_SMALLBUFFER1[nt] *= exp(-j*OMEGA0*(TMIN+TSTEP*nt));
#endif
fftwt_execute_dft(FFT_FWPLAN_T, (fftwt_complex*)NL_SMALLBUFFER1, (fftwt_complex*)output);
}
void calculate_plasmadensity_2float(f_complex* input, float_type* output, size_t N)
{
//#ifndef _ENABLE_CUDA
for (size_t i=0; i<N; i++) calculate_plasmadensity_losses_small(input+N_T*i, output+N_T*i);
return;
/*#else
f_complex* cuda_field_piece, *cuda_buf; float_type *cuda_plasma_piece;
size_t device_freemem=0, device_totalmem=0;
int device_processN; MPI_Comm_size(DEVICE_COMM, &device_processN);
(cudaMemGetInfo(&device_freemem, &device_totalmem));
size_t cuda_pointN = exp2(floor(log2((double)device_freemem/N_T/sizeof(f_complex)/1.5/device_processN)));
if (N < cuda_pointN) cuda_pointN = N;
int blocksize = 64; if (blocksize > cuda_pointN) blocksize=cuda_pointN;
size_t cuda_piece_size = N_T*cuda_pointN*sizeof(f_complex);
size_t cuda_piece_sizef = N_T*cuda_pointN*sizeof(float_type);
(cudaMalloc((void**)&cuda_field_piece, cuda_piece_size ));
(cudaMalloc((void**)&cuda_buf, cuda_piece_size ));
(cudaMalloc((void**)&cuda_plasma_piece, cuda_piece_sizef));
for (long i=0; i<N; i+= cuda_pointN)
{
if ((N-i)<cuda_pointN)
{
cuda_pointN = N-i;
cuda_piece_size = N_T*cuda_pointN*sizeof(f_complex);
cuda_piece_sizef = N_T*cuda_pointN*sizeof(float_type);
}
(cudaMemcpy(cuda_field_piece, input+i*N_T, cuda_piece_size, cudaMemcpyHostToDevice)) ;
int blockN = cuda_pointN/blocksize, r = cuda_pointN % blocksize;
calculate_plasma_2float_kernel<<<blockN,blocksize>>>(cuda_field_piece, cuda_plasma_piece, cuda_buf);
if (r != 0) calculate_plasma_2float_kernel<<<1,r>>>(cuda_field_piece+N_T*blockN*blocksize, cuda_plasma_piece+N_T*blockN*blocksize, cuda_buf+N_T*blockN*blocksize);
(cudaMemcpy(output+i*N_T, cuda_plasma_piece, cuda_piece_sizef, cudaMemcpyDeviceToHost)) ;
}
(cudaFree(cuda_field_piece));
(cudaFree(cuda_buf));
(cudaFree(cuda_plasma_piece));
#endif*/
}
void calculate_maxplasmadensity_2float(f_complex* input, float_type* output, size_t N)
{
//#ifndef _ENABLE_CUDA
float_type* b = (float_type*)NL_SMALLBUFFER1;
for (size_t i=0; i<N; i++)
{
float_type ro=0;
calculate_plasmadensity_losses_small(input+N_T*i, b);
for (size_t nt=0; nt<N_T; nt++) ro=max(ro, b[nt]);
output[i]=ro;
}
/*#else
f_complex* cuda_field_piece, *cuda_buf; float_type *cuda_maxplasma_piece;
size_t device_freemem=0, device_totalmem=0;
int device_processN; MPI_Comm_size(DEVICE_COMM, &device_processN);
(cudaMemGetInfo(&device_freemem, &device_totalmem));
size_t cuda_pointN = exp2(floor(log2((double)device_freemem/(N_T+0.5)/sizeof(f_complex)/device_processN)));
if (cuda_pointN > N) cuda_pointN=N;
int blocksize = 64; if (blocksize > cuda_pointN) blocksize=cuda_pointN;
size_t cuda_piece_size = N_T*cuda_pointN*sizeof(f_complex);
size_t cuda_piece_sizef = cuda_pointN*sizeof(float_type);
(cudaMalloc((void**)&cuda_field_piece, cuda_piece_size ));
(cudaMalloc((void**)&cuda_buf, cuda_piece_size ));
(cudaMalloc((void**)&cuda_maxplasma_piece, cuda_piece_sizef));
for (long i=0; i<N; i+= cuda_pointN)
{
if ((N-i)<cuda_pointN)
{
cuda_pointN = N-i;
cuda_piece_size = N_T*cuda_pointN*sizeof(f_complex);
cuda_piece_sizef = cuda_pointN*sizeof(float_type);
}
(cudaMemcpy(cuda_field_piece, input+i*N_T, cuda_piece_size, cudaMemcpyHostToDevice)) ;
int blockN = cuda_pointN/blocksize, r = cuda_pointN % blocksize;
calculate_maxplasma_kernel<<<blockN,blocksize>>>(cuda_field_piece, cuda_maxplasma_piece, cuda_buf);
if (r != 0) calculate_maxplasma_kernel<<<1,r>>>(cuda_field_piece+N_T*blockN*blocksize, cuda_maxplasma_piece+blockN*blocksize, cuda_buf+N_T*blockN*blocksize);
(cudaMemcpy(output+i*N_T, cuda_maxplasma_piece, cuda_piece_sizef, cudaMemcpyDeviceToHost)) ;
}
(cudaFree(cuda_field_piece));
(cudaFree(cuda_maxplasma_piece));
(cudaFree(cuda_buf));
#endif*/
}
//----------------------------------------------
//------------------------------------------------
#ifdef _ENABLE_CUDA
void cuda_load_const()
{