-
Notifications
You must be signed in to change notification settings - Fork 0
/
src_in_one_file.f90
14224 lines (12308 loc) · 524 KB
/
src_in_one_file.f90
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
module constants
implicit none
integer,parameter :: p_=kind(1.0d0) !precision for real number
character(len=20), parameter :: plasma_type='DT' !Deuterium-Trinuem plasma
!character(len=20), parameter :: plasma_type='P11B'!proton-Boron plasma
!character(len=20), parameter :: plasma_type='DC' !assume Deuterimum plasma with Carbon impurity
logical,parameter :: with_rmp = .true. !3D perturnbation
logical,parameter :: FLR_push = .true. !finite Larmor radius effect
logical,parameter :: FLR_loss = .true. !
logical,parameter :: FLR_deposition = .true.
integer, parameter :: collision_steps=10 !number of steps when a collision operation is imposed
integer, parameter :: injection_interval=100 !source birth at t=0+injection_interval*dtao
logical,parameter :: toroidal_analytical = .true.
real(p_), parameter :: Ln=1.d0 !a characteristic length (in unit of meter) !fixed, do not change
real(p_), parameter :: bn=1.d0 !a characteristic magnetic field strength (in unit of Tesla), !fixed, do not change
integer, parameter :: mpoloidal=150, nflux=50 !poloidal and radial grid numbers
!real(p_),parameter:: coulomb_log=15._p_ !assumed to be a constant
real(p_),parameter:: kev=1.6022d-16 !in S.I units
real(p_),parameter:: Mev=1.6022d-13 !in S.I units
real(p_),parameter:: MW=10**6 !in S.I units
real(p_),parameter:: kj=1000_p_ !in S.I units
real(p_),parameter:: elementary_charge=1.6022d-19 !unit C
real(p_),parameter:: electron_mass=9.1094d-31 !in unit of kg
real(p_),parameter:: epsilon0=8.8542d-12 !Permittivity of free space
real(p_),parameter:: atom_mass_unit=1.660539066d-27 !kg
real(p_),parameter:: pi=4*ATAN(1.d0) !3.1415926_p_
real(p_),parameter:: twopi=pi*2.0_p_
real(p_),parameter:: fourpi=pi*4.0_p_
real(p_),parameter:: half_pi=pi*0.5_p_
real(p_),parameter:: mu0=fourpi*1.0d-7 !permeability in SI unit
real(p_),parameter:: zero=0.0_p_
real(p_),parameter:: one=1.0_p_
real(p_),parameter:: two=2.0_p_
real(p_),parameter:: three=3.0_p_
real(p_),parameter:: four=4.0_p_
real(p_),parameter:: five=5.0_p_
real(p_),parameter:: six=6.0_p_
real(p_),parameter:: seven=7.0_p_
real(p_),parameter:: eight=8.0_p_
real(p_),parameter:: nine=9.0_p_
real(p_),parameter:: ten=10.0_p_
real(p_),parameter:: eleven=11.0_p_
real(p_),parameter:: twelve=12.0_p_
real(p_),parameter:: thirteen=13.0_p_
real(p_),parameter:: fourteen=14.0_p_
real(p_),parameter:: fifteen=15.0_p_
real(p_),parameter:: one_half=0.5_p_
real(p_),parameter:: one_third=one/three
real(p_),parameter:: one_fifth=0.2_p_
real(p_),parameter:: three_halfs=1.5_p_
real(p_) :: dtao !time step used in integrating the equation of the guiding center motion, in unit of 2pi/Omegan, where omegan=bn*charge/mass
real(p_) :: dt_omega_axis
character(50) :: fusion_type
integer :: n_alpha_per_fusion
real(p_) :: ti_axis
integer :: myid, np !number of MPI processors
end module constants
module nbi_source_parameters_module
use constants,only:p_
use constants,only:kev
implicit none
real(p_)::rtan_central_beam !in unit of meter, !rtan is the tangent radius of a beam line
real(p_):: r0_source_center, z0_source_center !in unit of meter, cylindrical coordinates of NBI exit grid center
real(p_):: phi0_source_center !in unit of radius
real(p_):: source_width !in unit of meter, of the NBI exit grid
real(p_)::source_height !in unit of meter, of the NBI exit grid
real(p_):: dist_grid_aperture !the distance between the exit grids and the aperture
real(p_):: aperture_half_width,aperture_half_height
real(p_):: full_energy !in uint of keV
real(p_):: full_energy_fraction
real(p_):: half_energy_fraction
real(p_):: vertical_focal_length, horizontal_focal_length
real(p_):: horizontal_divergence_angle,vertical_divergence_angle
real(p_)::nbi_power !in unit of watta, beam power after neutralization
real(p_) :: plate_angle
logical :: ionization_outside_lcfs
real(p_) :: phi_direction !+1 for injection in +phi direction, and -1 for injection in -phi direction
end module nbi_source_parameters_module
module poloidal_flux_2d
!poloidal flux and its partial derivatives on (R,Z) plane, where (R,fai,Z) is the cylindrical coordinate system
use constants,only:p_
implicit none
integer:: nx,nz !nx,nz are respectively the numbers of grids in R and Z directions (specified in G-file)
real(p_),dimension(:),allocatable ::xarray,zarray ! R and Z array
real(p_),dimension(:,:),allocatable ::psi,y2a_psi !psi, 2D array on (R,Z) plane, is the poloidal flux function appearing in Grad-Shafranov equation.
!psi is related to poloidal magnetic field by Bp=\nabal{psi}\times\nabla{fai},where fai is the usual toroidal angle; y2a_psi is the 2nd order erivative array used in the 2D spline interpolation of psi
real(p_),dimension(:,:),allocatable ::psi_x,psi_z,psi_xx,psi_zz,psi_xz,psi_zx !psi_x is the partial derivative with respect to x, similar meaning for others
real(p_),dimension(:,:),allocatable ::psi_gradient,y2a_gradient !strength of the gradient of the poloidal flux, y2a_gradient is the second derivative array used in 2D spline interpolation
real(p_),dimension(:,:),allocatable ::y2a_psi_x,y2a_psi_z,y2a_psi_xx,y2a_psi_zz,y2a_psi_xz,y2a_psi_zx ! y2a_* is the second derivative array used in 2D spline interpolation
real(p_),dimension(:,:),allocatable :: bphi, bphi_r, bphi_z
real(p_),dimension(:,:), allocatable :: equ_b, equ_b_r, equ_b_z
real(p_),dimension(:,:,:,:), allocatable :: cubic_coef
real(p_) :: current !total plasma current in Ampere
end module poloidal_flux_2d
module rmp_coils
use constants,only:p_
use constants,only: twopi,pi
implicit none
integer,parameter::n_wires=16 !one coil has 2 poloida wires and 2 toroidal wires. 16 coils has 32 poloidal wires and 32 toroidal wires
integer,parameter:: m_dl_pol=160 !number of points on each poloidal wire (discretizing the poloidal wire)
integer,parameter:: m_dl_tor=150 !number of points on each toroidal wire (discretizing the toroidal wire)
! real(p_),parameter:: rleft=2.1_p_, rright=2.35_p_ !These engineering parameters need to be verifid
! real(p_),parameter:: zlow1=-0.75_p_,zupp1=-0.57_p_ !need to be verifid
real(p_),parameter:: rleft=2.0925_p_, rright=2.2783_p_ !These engineering parameters are verifid by W.F Guo
real(p_),parameter:: zlow1=-0.7585_p_, zupp1=-0.5767_p_ !verifid
real(p_),parameter:: zlow2=-zupp1, zupp2=-zlow1
real(p_),parameter:: phi_gap=pi/180._p_*8._p_ ! toroidal angle range of the gap between two coils: 8degree. coil toroidal span: 37degree
real(p_),parameter:: phi_span=twopi/8._p_-phi_gap !coil toroidal span: 37degree
! real(p_),parameter:: phi0_rmp= 0.0_p_ !toroidal location of the first side of the first rmp coil
real(p_),parameter:: phi0_rmp= pi/180._p_*4._p_ !toroidal location of the first side of the first rmp coil
! real(p_),parameter:: upp_down_phase= 0*twopi/6 !the phase difference between current in the upper coils and lower coils, =phase_upp-phase_down
integer :: upp_down_phase !chosen among values: 0,1,2,3,4,5,6,7
real(p_):: rmp_current_amplitude
integer:: rmp_nh
logical, parameter :: is_ripple=.true.
integer,parameter :: nripple=16
end module rmp_coils
module rmp_3d_field
!rmp 3d field and its partial derivatives in (R,phi,Z) coordinates, where (R,fai,Z) are the cylindrical coordinates.
use constants,only:p_
implicit none
integer:: nphi
real(p_),dimension(:),allocatable :: phiarray
real(p_),dimension(:,:,:),allocatable :: rmp_br,rmp_bz,rmp_bphi, rmp_b
real(p_),dimension(:,:,:),allocatable :: rmp_br_r,rmp_br_z,rmp_br_phi
real(p_),dimension(:,:,:),allocatable :: rmp_bz_r,rmp_bz_z,rmp_bz_phi
real(p_),dimension(:,:,:),allocatable :: rmp_bphi_r,rmp_bphi_z,rmp_bphi_phi
real(p_),dimension(:,:,:),allocatable :: rmp_b_r,rmp_b_z,rmp_b_phi
real(p_),dimension(:,:,:),allocatable :: rmp_Ar,rmp_Az,rmp_Aphi
real(p_),dimension(:,:),allocatable :: rmp_br_amp, rmp_bz_amp, rmp_bphi_amp
real(p_),dimension(:,:),allocatable :: rmp_br_r_amp, rmp_br_z_amp
real(p_),dimension(:,:),allocatable :: rmp_bz_r_amp, rmp_bz_z_amp
real(p_),dimension(:,:),allocatable :: rmp_bphi_r_amp, rmp_bphi_z_amp
end module rmp_3d_field
module ne_module
use constants,only:p_
implicit none
integer:: ndata
real(p_),dimension(:),allocatable:: pfn_ndata,ne_ndata,tmp_y2 !tmp_y2 stores the interpolating coefficients
real(p_),dimension(:),allocatable:: ne_npsi
end module ne_module
module te_module
use constants,only:p_
implicit none
integer:: ndata
real(p_),dimension(:),allocatable:: pfn_ndata,te_ndata, te_npsi, tmp_y2 !tmp_y2 stores the interpolating coefficients
end module te_module
!!$module ti_module
!!$ use constants,only:p_
!!$ implicit none
!!$ integer:: ndata
!!$ real(p_),dimension(:),allocatable:: pfn_ndata,ti_ndata, ti_npsi, tmp_y2 !tmp_y2 stores the interpolating coefficients
!!$
!!$
!!$end module ti_module
module radial_module
use constants,only:p_, nflux
implicit none
integer:: npsi
real(p_),dimension(:),allocatable:: psi_1d,fpsi,ffprime,fprime,qpsi
!real(p_),dimension(:),allocatable ::y2_fpsi,y2_fprime
real(p_),dimension(:),allocatable:: pfn_npsi,tfn_npsi
real(p_):: r_axis,z_axis,baxis
real(p_):: psi_axis,psi_lcfs
integer:: sign_bphi !sign of the toroidal component of the magnetic field
!real(p_),parameter:: pfn_inner=0.005_p_, pfn_bdry=0.99_p_
real(p_),parameter:: pfn_inner=0.0_p_, pfn_bdry=1.0_p_
real(p_):: psi_array(nflux), pfn(nflux), circumference(nflux)
real(p_) :: vol(nflux-1), pol_area(nflux-1) !v(j) is the volume between surface j and j+1
real(p_) :: vol_int(nflux), radial_coor_vol(nflux) !vol_int(j) is the volume enclosed by surface j
real(p_) :: total_volume !volume enclosed by the LCFS, uint: m^3
real(p_),allocatable :: pressure(:), pprime(:) !pressure and its gradient
contains
subroutine create_psi_array() !radial coordinate in magnetic coordinates
! real(p_):: pfn_sqrt(nflux)
integer:: j
do j=1,nflux
pfn(j)=pfn_inner+(pfn_bdry-pfn_inner)/(nflux-1)*(j-1)
enddo
!!$
!!$ do j=1,nflux
!!$ psi_array(j)=psi_axis+(psi_lcfs-psi_axis)*pfn_sqrt(j)
!!$ enddo
!!$ do j=1,nflux
!!$ psi_array(j)=psi_axis+(psi_lcfs-psi_axis)/(nflux-1)*(j-1)
!!$ enddo
do j=1,nflux
psi_array(j)=psi_axis+pfn(j)*(psi_lcfs-psi_axis)
enddo
end subroutine create_psi_array
end module radial_module
module magnetic_coordinates
use constants,only:p_, pi, twopi, mpoloidal, nflux
use radial_module, only : pfn
implicit none
real(p_),dimension(:,:),allocatable :: r_mag_surf, z_mag_surf
real(p_),dimension(:,:),allocatable :: jacobian
real(p_), dimension(:), allocatable :: theta
real(p_) :: dtheta
contains
subroutine poloidal_angle()
integer :: i
allocate(theta(mpoloidal))
do i=1,mpoloidal
theta(i)= -pi + twopi/(mpoloidal-1)*(i-1)
enddo
dtheta=(theta(mpoloidal)-theta(1))/(mpoloidal-1) !grid interval, uniform theta grid is assumed
end subroutine poloidal_angle
end module magnetic_coordinates
module boundary
use constants,only:p_, twopi
implicit none
integer:: nlim, np_lcfs, nbdry, nbdry_phi
real(p_),allocatable :: rlim(:), zlim(:), x_lcfs(:), z_lcfs(:)
real(p_),allocatable :: rbdry(:,:), zbdry(:,:), phibdry(:)
real(p_) :: dphi_bdry
character(100):: loss_bdry, loss_bdry_fn
logical :: loss_bdry_axisymmetry
contains
subroutine set_loss_boundary()
integer :: i,j,u
select case(trim(loss_bdry))
case ('lcfs')
nbdry=np_lcfs
nbdry_phi=50
allocate(rbdry(nbdry,nbdry_phi))
allocate(zbdry(nbdry,nbdry_phi))
do j=1,nbdry
rbdry(j,:)=x_lcfs(j)
zbdry(j,:)=z_lcfs(j)
enddo
allocate(phibdry(nbdry_phi))
dphi_bdry = twopi/(nbdry_phi-1)
phibdry= [ (0.+dphi_bdry*(j-1), j=1, nbdry_phi) ]
case ('limiter')
nbdry=nlim
nbdry_phi=50
allocate(rbdry(nlim,nbdry_phi))
allocate(zbdry(nlim,nbdry_phi))
do j=1,nbdry
rbdry(j,:)=rlim(j)
zbdry(j,:)=zlim(j)
enddo
allocate(phibdry(nbdry_phi))
dphi_bdry = twopi/(nbdry_phi-1)
phibdry= [ (0.+dphi_bdry*(j-1), j=1, nbdry_phi) ]
case ('user_provide')
open(newunit=u,file=loss_bdry_fn)
if(loss_bdry_axisymmetry .eqv. .true.) then
nbdry_phi=50
allocate(rbdry(nbdry, nbdry_phi))
allocate(zbdry(nbdry, nbdry_phi))
do i=1,nbdry
read(u,*) rbdry(i,1), zbdry(i,1)
enddo
do j=2, nbdry_phi
rbdry(:,j)=rbdry(:,1)
zbdry(:,j)=zbdry(:,1)
enddo
allocate(phibdry(nbdry_phi))
dphi_bdry = twopi/(nbdry_phi-1)
phibdry= [ (0.+dphi_bdry*(j-1), j=1, nbdry_phi) ]
else !boundary surface is not axisymmetircal
allocate(rbdry(nbdry, nbdry_phi))
allocate(zbdry(nbdry, nbdry_phi))
allocate(phibdry(nbdry_phi))
do j=1,nbdry_phi
do i=1,nbdry
read(u,*) rbdry(i,j), zbdry(i,j), phibdry(j)
enddo
enddo
dphi_bdry=phibdry(2)-phibdry(1)
endif
close(u)
case default
stop "please choose loss boundary among 'lcfs', 'limiter', 'user_provide'"
end select
open(newunit=u,file='tmp_loss_bdry.txt')
do i=1,nbdry
write(u,*) rbdry(i,1), zbdry(i,1)
enddo
close(u)
end subroutine set_loss_boundary
end module boundary
module trapped_particles
integer:: ntrapped=0
end module trapped_particles
module ep_parameters
use constants,only:p_, kev
use constants,only: bn
implicit none
!real(p_),parameter:: mass=2._p_*1.6726d-27 !particle mass in kg (2._p_*1.6726d-27 is for Deuterium)
real(p_):: mass !particle mass in kg (2._p_*1.6726d-27 is for Deuterium)
!real(p_),parameter:: mass=9.1094d-31 !particle mass in kg (9.1094d-31 is for electron)
!real(p_),parameter:: charge=1._p_*1.6022d-19 !element charge in C
real(p_):: charge !element charge in C
character(100):: ep_distribution_type !type of fast ion distribution, from nbi/fusion/icrf
real(p_) :: zf !charge number
real(p_) :: weight0
real(p_):: omegan,tn,vn,mun !omegan is the cyclotron angular frequency in Hz
integer :: profile_reporting_interval
real(p_),parameter:: vbirth=2.35d6,deltav=0.21d6
real(p_),parameter:: mu_max=4.64d-15 !in SI unit
real(p_) :: cutoff_energy
real(p_) :: total_energy, source_power
end module ep_parameters
module background_plasma
use constants,only:p_
use constants,only: two,kev,elementary_charge
implicit none
! real(p_),parameter:: ti=2._p_*kev
! real(p_),parameter:: mi=2.0_p_*1.6726d-27 !particle mass in kg (mass=2._p_*1.6726d-27 is for Deuterium)
! real(p_),parameter:: vti=sqrt(two*ti/mi)
real(p_) :: zeff
! real(p_),parameter:: zi=1._p_
! real(p_),parameter:: qi=zi*elementary_charge
real(p_) :: coulomb_log !Coulomb logarithm
end module background_plasma
module cross_section_interpolate
use constants,only:p_
implicit none
integer,parameter:: ndata_cx=25, ndata_ii=13, ndata_te=24
real(p_):: energy_cx_log(ndata_cx),sigma_cx_log(ndata_cx),coeff_cx(ndata_cx)
real(p_):: energy_ii_log(ndata_ii),sigma_ii_log(ndata_ii),coeff_ii(ndata_ii)
real(p_):: te_array(ndata_te),sigma_ve_averaged(ndata_te),coeff_electron_impact(ndata_te)
end module cross_section_interpolate
module rzphi_grid
use constants,only:p_, twopi
implicit none
integer,parameter:: mr=90,mz=80, mphi=50
real(p_):: rgrid(mr),zgrid(mz),phigrid(mphi),dr,dz,dphi
real(p_) :: dvol(mr,mz), dvol_rphi(mr,mphi)
real(p_) :: target_density(mr,mz), target_temperature(mr,mz) !used in computing beam-target fusion rate
contains
subroutine set_rzphi_grid()
use boundary, only : rlim, zlim
integer:: i,j,k, u
real(p_):: r_left,r_right,z_bot,z_top
r_left=minval(rlim)
r_right=maxval(rlim)
z_bot=minval(zlim)
z_top=maxval(zlim)
dr=(r_right-r_left)/(mr-1)
do i=1,mr
rgrid(i)=r_left+dr*(i-1)
enddo
dz=(z_top-z_bot)/(mz-1)
do j=1,mz
zgrid(j)=z_bot+dz*(j-1)
enddo
dphi=twopi/(mphi-1)
do j=1, mphi
phigrid(j)=0+dphi*(j-1)
enddo
do i=1,mr
do j=1,mz
dvol(i,j)=rgrid(i)*dr*dz*twopi
enddo
enddo
do i=1,mr
do j=1,mphi
dvol_rphi(i,j)=rgrid(i)*dphi*dr*(z_top-z_bot)
enddo
enddo
end subroutine set_rzphi_grid
end module rzphi_grid
module energy_pitch_angle_grid
use constants, only : p_
implicit none
real(p_) :: max_energy !in Joule, Used for the energy grid
integer, parameter:: m=100, n=100
real(p_) :: delta_e, delta_pitch_angle
real(p_) :: e(m), pitch_angle(n)
real(p_) :: energy_dist(m), pitch_angle_dist(n), twod_dist(m, n)
real(p_) :: trapped_energy_dist(m), trapped_pitch_angle_dist(n), trapped_twod_dist(m, n)
contains
subroutine set_energy_pitch_angle_grid(max_value)
use constants, only : two, kev
real(p_), intent(in) :: max_value
integer :: j, j2
max_energy = max_value
delta_e=max_energy/kev/(m-1)
do j=1,m !create energy grids
e(j)=delta_e*(j-1)
enddo
delta_pitch_angle= two/(n-1)
do j2=1,n !create pitch_angle grids
pitch_angle(j2) = -1.0 + delta_pitch_angle*(j2-1)
enddo
end subroutine set_energy_pitch_angle_grid
end module energy_pitch_angle_grid
!!$module pphi_lambda_grid
!!$ use constants, only : p_
!!$ implicit none
!!$ integer,parameter:: mpphi=50,mlambda=50
!!$ real(p_):: pphi_grid(mpphi),lambda_grid(mlambda)
!!$
!!$contains
!!$ subroutine set_pphi_lambda_grid()
!!$ integer:: i,j
!!$ real(p_):: xleft, xright, yleft, yright
!!$
!!$
!!$
!!$ end subroutine set_pphi_lambda_grid
!!$
!!$
!!$end module pphi_lambda_grid
!>>>PNP1
! http://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html
! ..................................................................
!
! SUBROUTINE PNPOLY
!
! PURPOSE
! TO DETERMINE WHETHER A POINT IS INSIDE A POLYGON
!
! USAGE
! CALL PNPOLY (PX, PY, XX, YY, N, INOUT )
!
! DESCRIPTION OF THE PARAMETERS
! PX - X-COORDINATE OF POINT IN QUESTION.
! PY - Y-COORDINATE OF POINT IN QUESTION.
! XX - N LONG VECTOR CONTAINING X-COORDINATES OF
! VERTICES OF POLYGON.
! YY - N LONG VECTOR CONTAING Y-COORDINATES OF
! VERTICES OF POLYGON.
! N - NUMBER OF VERTICES IN THE POLYGON.
! INOUT - THE SIGNAL RETURNED:
! -1 IF THE POINT IS OUTSIDE OF THE POLYGON,
! 0 IF THE POINT IS ON AN EDGE OR AT A VERTEX,
! 1 IF THE POINT IS INSIDE OF THE POLYGON.
!
! REMARKS
! THE VERTICES MAY BE LISTED CLOCKWISE OR ANTICLOCKWISE.
! THE FIRST MAY OPTIONALLY BE REPEATED, IF SO N MAY
! OPTIONALLY BE INCREASED BY 1.
! THE INPUT POLYGON MAY BE A COMPOUND POLYGON CONSISTING
! OF SEVERAL SEPARATE SUBPOLYGONS. IF SO, THE FIRST VERTEX
! OF EACH SUBPOLYGON MUST BE REPEATED, AND WHEN CALCULATING
! N, THESE FIRST VERTICES MUST BE COUNTED TWICE.
! INOUT IS THE ONLY PARAMETER WHOSE VALUE IS CHANGED.
! THE SIZE OF THE ARRAYS MUST BE INCREASED IF N > MAXDIM
! WRITTEN BY RANDOLPH FRANKLIN, UNIVERSITY OF OTTAWA, 7/70.
!
! SUBROUTINES AND FUNCTION SUBPROGRAMS REQUIRED
! NONE
!
! METHOD
! A VERTICAL LINE IS DRAWN THRU THE POINT IN QUESTION. IF IT
! CROSSES THE POLYGON AN ODD NUMBER OF TIMES, THEN THE
! POINT IS INSIDE OF THE POLYGON.
!
! ..................................................................
!
module pnpoly_mod
contains
pure SUBROUTINE PNPOLY(PX,PY,XX,YY,N,INOUT)
use constants,only:p_
implicit none
integer,intent(in):: n
integer,intent(inout):: inout
real(p_),intent(in):: px,py
REAL(p_),intent(in):: XX(N),YY(N)
integer:: i,j,maxdim
REAL(p_):: X(n),Y(n), tmp
LOGICAL MX,MY,NX,NY
DO I=1,N
X(I)=XX(I)-PX
Y(I)=YY(I)-PY
enddo
INOUT=-1
DO 2 I=1,N
J=1+MOD(I,N)
MX=X(I).GE.0.0
NX=X(J).GE.0.0
MY=Y(I).GE.0.0
NY=Y(J).GE.0.0
IF(.NOT.((MY.OR.NY).AND.(MX.OR.NX)).OR.(MX.AND.NX)) GO TO 2
IF(.NOT.(MY.AND.NY.AND.(MX.OR.NX).AND..NOT.(MX.AND.NX))) GO TO 3
INOUT=-INOUT
GO TO 2
!!$3 IF((Y(I)*X(J)-X(I)*Y(J))/(X(J)-X(I))) 2,4,5
!!$4 INOUT=0
!!$ RETURN
!!$5 INOUT=-INOUT
!!$2 CONTINUE
3 tmp=(Y(I)*X(J)-X(I)*Y(J))/(X(J)-X(I))
IF(tmp<0) then
goto 2
elseif(tmp==0) then
goto 4
else
goto 5
endif
4 INOUT=0
RETURN
5 INOUT=-INOUT
2 CONTINUE
END SUBROUTINE
end module
module interpolate_mod
contains
pure subroutine linear_1d_interpolate(n,x,y,xval,yval)
use constants,only:p_
use constants,only:one
implicit none
integer,intent(in):: n
real(p_),intent(in):: x(n),y(n)
real(p_),intent(in):: xval
real(p_),intent(out):: yval
real(p_):: dx,slope
integer:: i
dx=x(2)-x(1)
i=floor(one+(xval-x(1))/dx) !uniform xarray is assumed, otherwise we need to call location() subroutine to locate xval
!call location(n,x,xval,i)
if(i.ge.n) i=n-1
if(i.lt.1) i=1
slope=(y(i+1)-y(i))/(x(i+1)-x(i))
yval=y(i)+slope*(xval-x(i))
end subroutine linear_1d_interpolate
pure subroutine linear_1d_interpolate_extrapolate(n,x,y,xval,yval)
use constants,only:p_
use constants,only:one
implicit none
integer,intent(in):: n
real(p_),intent(in):: x(n),y(n)
real(p_),intent(in):: xval
real(p_),intent(out):: yval
real(p_):: dx,slope
integer:: i
dx=x(2)-x(1)
i=floor(one+(xval-x(1))/dx) !uniform xarray is assumed, otherwise we need to call location() subroutine to locate xval
!call location(n,x,xval,i)
if(i.ge.n) then !assume constant beyond the ending points
yval=y(n)
elseif(i.le.1) then
yval=y(1)
else
slope=(y(i+1)-y(i))/(x(i+1)-x(i))
yval=y(i)+slope*(xval-x(i))
endif
end subroutine linear_1d_interpolate_extrapolate
pure subroutine linear_1d_interpolate_nonuniform(n,x,y,xval,yval)
use constants,only:p_
use constants,only:one
implicit none
integer,intent(in):: n
real(p_),intent(in):: x(n),y(n)
real(p_),intent(in):: xval
real(p_),intent(out):: yval
real(p_):: dx,slope
integer:: i
dx=x(2)-x(1)
!i=floor(one+(xval-x(1))/dx) !uniform xarray is assumed, otherwise we need to call location() subroutine to locate xval
call location(n,x,xval,i)
if(i.ge.n) i=n-1
slope=(y(i+1)-y(i))/(x(i+1)-x(i))
yval=y(i)+slope*(xval-x(i))
end subroutine linear_1d_interpolate_nonuniform
pure subroutine location(n,x,xval,k) !use bisection method to locate xval in an array
use constants,only:p_
implicit none
integer,intent(in):: n
real(p_),intent(in):: x(n),xval
integer,intent(out)::k
integer:: kl,ku,km
kl=1
ku=n
!!$30 if(ku-kl .gt. 1) then !use bisection method to search location of theta
!!$ km=(ku+kl)/2
!!$ if((x(n).ge.x(1)).eqv.(xval.ge.x(km))) then
!!$ kl=km
!!$ else
!!$ ku=km
!!$ endif
!!$ goto 30
!!$ endif
do while((ku-kl) .gt. 1) !use bisection method to search location of theta
km=(ku+kl)/2
if((x(n).ge.x(1)).eqv.(xval.ge.x(km))) then
kl=km
else
ku=km
endif
enddo
k=kl
end subroutine location
subroutine linear_1d_interpolate_tmp(n,x,y,xval,yval)
use constants,only:p_
use constants,only:one
implicit none
integer,intent(in):: n
real(p_),intent(in):: x(n),y(n)
real(p_),intent(in):: xval
real(p_),intent(out):: yval
real(p_):: slope
integer:: i
!dx=x(2)-x(1)
!i=floor(one+(xval-x(1))/dx) !this for uniform x, otherwise we need to call location() subroutine to locate xval
call location(n,x,xval,i)
if(i.ge.n) i=n-1
slope=(y(i+1)-y(i))/(x(i+1)-x(i))
yval=y(i)+slope*(xval-x(i))
end subroutine linear_1d_interpolate_tmp
!!$ pure subroutine linear_2d_interpolate_kernel(x1a,x2a,ya,x1,x2,y)
!!$ use constants,only:p_
!!$ implicit none
!!$ real(p_), intent(in)::x1a(2),x2a(2),ya(2,2),x1,x2
!!$ real(p_), intent(out) :: y
!!$ real(p_):: ytmp(2),slope
!!$ integer:: j
!!$
!!$ do j=1,2
!!$ slope=(ya(2,j)-ya(1,j))/(x1a(2)-x1a(1))
!!$ ytmp(j)=ya(1,j)+slope*(x1-x1a(1))
!!$ enddo
!!$ slope=(ytmp(2)-ytmp(1))/(x2a(2)-x2a(1))
!!$ y=ytmp(1)+slope*(x2-x2a(1))
!!$
!!$ end subroutine linear_2d_interpolate_kernel
pure subroutine linear_2d_interpolate(nx,nz,xarray,zarray,psi,x,z,psival) !uniform xarray and zarray are assumed
use constants,only:p_
use constants,only:one
! use interpolate_mod, only: linear_2d_interpolate_kernel
implicit none
integer,intent(in):: nx,nz
real(p_),intent(in):: xarray(nx),zarray(nz),psi(nx,nz)
real(p_),intent(in):: x,z
real(p_),intent(out)::psival
real(p_):: dx,dz,t1,t2,slope
integer:: i,j ,ii,jj
real(p_):: psi_tmp(2,2)
!if(z>zarray(nz) .or. z<zarray(1)) write(*,*) ' z=, in interplation',z
dx=xarray(2)-xarray(1)
i=floor(one+(x-xarray(1))/dx) !uniform xarray is assumed, otherwise we need to call location() subroutine to locate xval
!call location(nx,xarray,x,i)
dz=zarray(2)-zarray(1)
j=floor(one+(z-zarray(1))/dz)
if(i.ge.nx) i=nx-1
if(j.ge.nz) j=nz-1
if(i.le.1) i=1
if(j.le.1) j=1
!!$ if(i.lt.1) i=1
!!$ if(j.lt.1) j=1
! if(j.gt.nz) write(*,*) 'j=',j,x,z
!call location(nz,zarray,z,j)
!define a 2x2 array near (x,z)
!!$ do ii=1,2
!!$ do jj=1,2
!!$ psi_tmp(ii,jj)=psi(i+ii-1,j+jj-1)
!!$ enddo
!!$ enddo
!!$ call linear_2d_interpolate_kernel(xarray(i),zarray(j),psi_tmp,x,z,psival)
!!$ associate (psi_tmp=>psi(i:i+1,j:j+1))
!!$ call linear_2d_interpolate_kernel(xarray(i),zarray(j),psi_tmp,x,z,psival)
!!$ end associate
slope=(psi(i+1,j)-psi(i,j))/dx
t1=psi(i,j)+slope*(x-xarray(i))
slope=(psi(i+1,j+1)-psi(i,j+1))/dx
t2=psi(i,j+1)+slope*(x-xarray(i))
slope=(t2-t1)/dz
psival=t1+slope*(z-zarray(j))
end subroutine linear_2d_interpolate
pure subroutine linear_3d_interpolate(nx,nz,nphi,rarray,zarray,phiarray,b,x,z,phi,bval) !uniform rarray ,zarray, phiarray are assumed
use constants,only:p_
use constants,only:one,twopi
implicit none
integer,intent(in):: nx,nz,nphi
real(p_),intent(in):: rarray(nx),zarray(nz),phiarray(nphi),b(nx,nz,nphi)
real(p_),intent(in):: x,z,phi
real(p_),intent(out)::bval
real(p_):: dx,dz,dphi,phitmp, t1, t2, ta, tb, slope
integer:: i,j,k ,ii,jj,kk
real(p_):: b_tmp(2,2,2)
! write(*,*) 'nx=',nx,'nz=',nz,'nphi=',nphi
dx=rarray(2)-rarray(1)
i=floor(one+(x-rarray(1))/dx) !uniform rarray is assumed, otherwise we need to call location() subroutine to locate xval
!call location(nx,rarray,x,i)
dz=zarray(2)-zarray(1)
j=floor(one+(z-zarray(1))/dz)
dphi=phiarray(2)-phiarray(1)
!!$ phitmp=mod(phi,twopi)
!!$ if (phitmp<0) phitmp=phitmp+twopi
! phitmp=phi
! if((phi<0) .or. (phi>=twopi)) call shift_to_zero_twopi_range(phitmp)
phitmp=phi-floor(phi/twopi)*twopi !shift to the range [0:twopi]
k=floor(one+(phitmp-phiarray(1))/dphi)
if(i.ge.nx) i=nx-1
if(j.ge.nz) j=nz-1
if(k.ge.nphi) k=nphi-1
if(i.le.1) i=1
if(j.le.1) j=1
if(k.le.1) k=1
!define a 2x2x2 array near (x,z,phi)
!!$ do ii=1,2
!!$ do jj=1,2
!!$ do kk=1,2
!!$ b_tmp(ii,jj,kk)=b(i+ii-1,j+jj-1,k+kk-1)
!!$ enddo
!!$ enddo
!!$ enddo
!!$ call linear_3d_interpolate_kernel(rarray(i),zarray(j),phiarray(k),b_tmp,x,z,phitmp,bval)
!!$associate (b_tmp=>b(i:i+1,j:j+1,k:k+1))
!!$ call linear_3d_interpolate_kernel(rarray(i),zarray(j),phiarray(k),b_tmp,x,z,phitmp,bval)
!!$end associate
!2D interpolation at fixed phi
slope=(b(i+1,j,k)-b(i,j,k))/dx
t1=b(i,j,k)+slope*(x-rarray(i))
slope=(b(i+1,j+1,k)-b(i,j+1,k))/dx
t2=b(i,j+1,k)+slope*(x-rarray(i))
slope=(t2-t1)/dz
ta=t1+slope*(z-zarray(j))
!2D interpolation at fixed phi
slope=(b(i+1,j,k+1)-b(i,j,k+1))/dx
t1=b(i,j,k+1)+slope*(x-rarray(i))
slope=(b(i+1,j+1,k+1)-b(i,j+1,k+1))/dx
t2=b(i,j+1,k+1)+slope*(x-rarray(i))
slope=(t2-t1)/dz
tb=t1+slope*(z-zarray(j))
slope=(tb-ta)/dphi
bval=ta+slope*(phitmp-phiarray(k))
end subroutine linear_3d_interpolate
!!$ pure subroutine linear_3d_interpolate_kernel(x1a,x2a,x3a,ya,x1,x2,x3,y)
!!$ use constants,only:p_
!!$ ! use interpolate_mod, only: linear_2d_interpolate_kernel
!!$ implicit none
!!$ real(p_),intent(in)::x1a(2),x2a(2),x3a(2),ya(2,2,2)
!!$ real(p_),intent(in)::x1,x2,x3
!!$ real(p_),intent(out):: y
!!$ real(p_):: yatmp(2,2),slope(2,2)
!!$ integer:: i,j
!!$
!!$ do i=1,2
!!$ do j=1,2
!!$ slope(i,j)=(ya(i,j,2)-ya(i,j,1))/(x3a(2)-x3a(1))
!!$ yatmp(i,j)= ya(i,j,1)+slope(i,j)*(x3-x3a(1))
!!$ enddo
!!$ enddo
!!$
!!$ call linear_2d_interpolate_kernel(x1a,x2a,yatmp,x1,x2,y)
!!$
!!$ end subroutine linear_3d_interpolate_kernel
end module interpolate_mod
module math
contains
pure subroutine shift_to_specified_toroidal_range(a) !shift "a" into the range [0:toroidal_range]
use constants,only: p_, twopi
implicit none
real(p_),intent(inout):: a
integer:: ishift
real(p_),parameter :: toroidal_range=twopi
ishift=floor(a/toroidal_range)
a=a-ishift*toroidal_range
end subroutine shift_to_specified_toroidal_range
pure function phi_principal(phi) result(z)
use constants,only: p_, twopi
implicit none
real(p_),intent(in):: phi
real(p_) :: z
real(p_),parameter :: toroidal_range=twopi
z = mod(phi,toroidal_range)
if(z<0) z = z + toroidal_range
end function phi_principal
subroutine grow_array(a, n) !keep the data in a untouched and grow its size
use constants, only : p_
implicit none
real(p_), intent(inout), allocatable :: a(:)
integer, intent(in) :: n
real(p_), allocatable :: tmp(:)
allocate(tmp(n))
tmp(1:size(a)) = a(:)
call move_alloc(tmp, a) !equivalent to `a=tmp; deallocate(tmp)`, but is more efficient since no data copying is involved (only descriptor copying is involved)
end subroutine grow_array
subroutine partial_derivatives_2d(nx,nz,rarray,zarray,b,b_x,b_z)
use constants,only:p_
implicit none
integer,intent(in):: nx,nz
real(p_),intent(in):: rarray(nx),zarray(nz),b(nx,nz)
real(p_),intent(out):: b_x(nx,nz),b_z(nx,nz)
integer:: i,j,i1,j1,i2,j2
do i=1,nx
do j=1,nz
i2=i+1
i1=i-1
j2=j+1
j1=j-1
if(i.eq.1) i1=i
if(j.eq.1) j1=j
if(i.eq.nx) i2=i
if(j.eq.nz) j2=j
b_x(i,j)=(b(i2,j)-b(i1,j))/(rarray(i2)-rarray(i1))
b_z(i,j)=(b(i,j2)-b(i,j1))/(zarray(j2)-zarray(j1))
enddo
end do
end subroutine partial_derivatives_2d
subroutine laplace_cylindrical2d(psi, x, z, nx, nz, jphi)
! use math, only : partial_derivatives_2d
use constants, only : p_, mu0
implicit none
integer, intent(in) :: nx, nz
real(p_), intent(in) :: psi(nx,nz), x(nx), z(nz)
real(p_), intent(out) :: jphi(nx,nz)
real(p_) :: psi_x(nx,nz), psi_z(nx,nz), psi_xx(nx,nz), psi_xz(nx,nz), psi_zx(nx,nz),psi_zz(nx,nz)
real(p_) :: dx, dz
integer :: i, j
dx = x(2) - x(1)
dz = z(2) - z(1)
call partial_derivatives_2d(nx,nz,x,z, psi, psi_x, psi_z)
call partial_derivatives_2d(nx,nz,x,z, psi_x, psi_xx, psi_xz)
call partial_derivatives_2d(nx,nz,x,z, psi_z, psi_zx, psi_zz)
do i= 1, nx
do j=1,nz
jphi(i,j) = psi_zz(i,j) + psi_xx(i,j) - 1/x(i)*psi_x(i,j)
jphi(i,j) = -jphi(i,j)/(mu0*x(i)) !to toroidal current density
enddo
enddo
end subroutine laplace_cylindrical2d
pure subroutine shift_to_zero_twopi_range(a) !shift a into the range [0:twopi]
use constants,only:p_
use constants,only: twopi
implicit none
real(p_),intent(inout):: a
integer:: ishift
!!$ a=a-int(a/twopi)*twopi !shift into the range [0:twopi]
!!$ if(a.lt.0) a=a+twopi !shift into the range [0:twopi]
ishift=floor(a/twopi)
a=a-ishift*twopi
end subroutine shift_to_zero_twopi_range
real(p_) FUNCTION rtbis(func,x1,x2,xacc,xmaxis,zmaxis,slope,psival) !binary-section mehtod of root searching
use constants,only: p_
implicit none
REAL(p_) :: x1,x2,xacc,xmaxis,zmaxis,slope,psival
! real(p_), EXTERNAL :: func
integer, PARAMETER :: JMAX=40
INTEGER j
REAL(p_) dx,f,fmid,xmid
interface
pure real(p_) function func(x_axis,z_axis,slope,psival,x)
use constants,only: p_
implicit none
real(p_),intent(in):: x_axis,z_axis,slope,psival,x
end function func
end interface
fmid=func(xmaxis,zmaxis,slope,psival,x2)
f= func(xmaxis,zmaxis,slope,psival,x1)
! write(*,*) 'f1=', f, 'f2=',fmid
if(f*fmid.ge.0.) stop 'root must be bracketed in rtbis'
if(f.lt.0.)then
rtbis=x1
dx=x2-x1
else
rtbis=x2
dx=x1-x2
endif
do j=1,JMAX
dx=dx*.5
xmid=rtbis+dx
fmid=func(xmaxis,zmaxis,slope,psival,xmid)
if(fmid.le.0.)rtbis=xmid
if(abs(dx).lt.xacc .or. fmid.eq.0.) then
!write(*,*) 'in rtbis j=',j
return
endif
enddo
stop 'too many bisections in rtbis'
END FUNCTION rtbis
FUNCTION rtbis2(func,x1,x2,xacc,nx,psival_nx,qpsi_nx,tmp_y2)
use constants,only:p_
implicit none