-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest00.f90
executable file
·376 lines (294 loc) · 11.7 KB
/
test00.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
program test
!implicit none
implicit real*8 (a-h,o-z)
integer :: ierr
integer, parameter :: Npt = 1000
real*8, parameter :: emass = 0.511d6, gamma0 = 100d6/emass
real*8 :: beammat(Npt,6), k, freq, len, accgrad
real*8, parameter :: realnumptl = 200d-12/1.602d-19, q = realnumptl/Npt, m = 9.109d-31, c = 2.997d8
!!realnumptl is the real number particle
!!q is the charge of each macroparticle and also the number of e charge per macroparticle, unit is [e],
!use beam
!---------above shows the usage of module in other file
!call data_passing('me')
!!! input initial conditions:
!current = 100e-12
!energy = 100e6
!bunchlength = 3e-3
!energystandeviate = 2e3
!energyspread = 0
!rmssize = 0.2e-3
!call initial_distribution(Npt,beammat) !generate distributions and transfer matrix
call read_data(Npt, beammat, ierr)
!print*, '***', beammat(3,3)
!call linac(Npt, beammat, q, gamma0, 0.61685d0, 1.3d9, 20d0, 16d6)
!call linac(Npt, beammat, q, gamma0, 0.61685d0, 3.9d9, 5d0, 10d6)
!call compressor(Npt, beammat, 5d-2, 10d-2, 0.27416d0) !BC1
!call linac(Npt, beammat, q, gamma0, 0.61685d0, 1.3d6, 200d0, 16d6)
!call compressor(Npt, beammat, 5d-2, 20d-2, 0.27416d0)
!call linac(Npt, beammat, q, gamma0, 0.61685d0, 1.3d6, 400d0, 16d6)
call plothistogram(Npt, q, gamma0,beammat) !plot the figures and print out the parameters
!call felpara()
end program
!!! -----------------------------------------------------------------------
subroutine data_passing(name)
character(len=*), intent(in) :: name
print*, 'hi',' ', name
end subroutine
!!! ------------------------------------------------------------------------
subroutine read_data(Npt, beammat, ierr)
implicit none
integer, intent(in) :: Npt
real*8, intent(out) :: beammat
integer, intent(out) :: ierr
integer :: i
character(len=100) :: line
real*8 :: x, px, y, py, z, dg
ierr = 0
! Open the file fort.10 for reading
open(unit=10, file='fort.10', status='old', action='read', iostat=ierr)
if (ierr /= 0) then
print *, "Error opening the file"
return
endif
! Read the data line by line
do i = 1, Npt
read(10,'(A)', iostat=ierr) line
if (ierr /= 0) then
print *, "Error reading the file or end of file reached"
exit
endif
read(line, *) x, px, y, py, z, dg
beammat(i,1) = x
beammat(i,2) = px
beammat(i,3) = y
beammat(i,4) = py
beammat(i,5) = z
beammat(i,6) = dg
end do
! Close the file
close(10)
end subroutine read_data
!!! -----------------------------------------------------------------------
subroutine linac(Npt, beammat, q, gamma0, k, freq, len, accgrad)
implicit real*8 (a-h,o-z)
integer, intent(in) :: Npt
real*8, intent(inout) :: beammat(Npt,6)
real*8, intent(in) :: q, gamma0, k, freq, len, accgrad
!real*8, parameter :: m = 9.109d-31, c = 2.997d8
!for macroparticle
real*8, parameter :: mc2 = 0.511d6 * q
!need to seperate the length into two half
!the mutval is done by linactrfmat subroutine
!output the beammat
halflen = len/2d0
call linactrfmat(Npt, beammat, gamma0, k, freq, halflen, accgrad)
beammat(:,6) = beammat(:,6) + (halflen * q * accgrad) / mc2
call linactrfmat(Npt, beammat, gamma0, k, freq, halflen, accgrad)
beammat(:,6) = beammat(:,6) + (halflen * q * accgrad) / mc2
end subroutine
!!! ----------------------------------------------------------------------- !# only do the matrix part, the energy gain is done in the main linac subroutine
subroutine linactrfmat(Npt, beammat, gamma0, k, freq, z, accgrad)
implicit real*8 (a-h,o-z)
integer, intent(in) :: Npt
real*8, intent(in) :: gamma0, k, freq, z, accgrad
real*8, intent(inout) :: beammat(Npt,6)
real*8 :: trfmat(6,6), gamma, beta, thisptl(6), pp
real*8, parameter :: m = 9.109d-31, c = 2.997d8
do i = 1,Npt
!construction of the linac matrix
!\\\\\ '/q'
gamma_square = (beammat(i,6)/q + gamma0) * (beammat(i,6)/q + gamma0)
beta_square = 1 - ( 1 / gamma_square )
a = sqrt(k)
trfmat = 0d0
trfmat(1,1) = cos(a*z)
trfmat(1,2) = 1/a * (sin(a*z))
trfmat(2,1) = -a*sin((a*a)*z)
trfmat(2,2) = cos(a*z)
trfmat(3,3) = cos(a*z)
trfmat(3,4) = 1/a * (sin(a*z))
trfmat(4,3) = -a*sin(a*a*z)
trfmat(4,4) = cos(a*z)
trfmat(5,5) = 1
trfmat(5,6) = (1/(gamma_square * beta_square) ) * z
trfmat(6,5) = 0
trfmat(6,6) = 1
!*** see the content of matrix---------------------
!print*, '***trfmat',trfmat(5,6)
!do i = 1, 6
! do j = 1, 6
! print *, trfmat(i, j)
! end do
! print *
!end do
!print*, shape(trfmat)
!----------------------------------------------
!perform the multval
thisptl = beammat(i,:)
!print*, shape(thisptl)
beammat(i,:) = matmul(trfmat,thisptl)
!print*, beammat(i,5)
end do
end subroutine
!!! -----------------------------------------------------------------------
subroutine compressor(Npt, beammat, R56, z, k)
implicit real*8 (a-h,o-z)
integer, intent(in) :: Npt
real*8, intent(inout) :: beammat(Npt,6)
real*8, intent(in) :: R56, z, k
real*8 :: trfmat(6,6), a, thisptl(6)
!match the transfer matrix
a = sqrt(k)
trfmat = 0d0
trfmat(1,1) = cos(a*z)
trfmat(1,2) = 1/a * (sin(a*z))
trfmat(2,1) = -a*sin((a*a)*z)
trfmat(2,2) = cos(a*z)
trfmat(3,3) = cos(a*z)
trfmat(3,4) = 1/a * (sin(a*z))
trfmat(4,3) = -a*sin(a*a*z)
trfmat(4,4) = cos(a*z)
trfmat(5,5) = 1
trfmat(5,6) = R56
trfmat(6,5) = 0
trfmat(6,6) = 1
do i = 1, Npt
thisptl = beammat(i,:)
!print*, shape(thisptl)
beammat(i,:) = matmul(trfmat,thisptl)
end do
end subroutine
!!! -----------------------------------------------------------------------
!!! for transferring the read-in bunch data into matrix
subroutine initial_distribution(Npt,beammat)
implicit real*8 (a-h,o-z)
integer, intent(in) :: Npt
real*8, dimension(Npt) :: x,px,y,py,z,dg
real*8, intent(out) :: beammat(Npt,6)
twopi = 4*asin(1.0)
emass = 0.511d6
!energy spread distribution in a standard deviation (unit: gamma)
sigdg = 2e3/emass
!rms size
sig = 0.2e-3
!reference energy (unit: gamma)
gamma0 = 100d6/emass
!emittance
emitun= 0.5e-6/gamma0
!square root of mean transverse energy (emitun/sig = \sqrt(MTE)/mc^2) (unit: gamma)
sigxp = emitun/sig
print*, 'initial conditions ------------------------------'
print*, 'bunch length', 3e-3
print*, 'reference energy', gamma0
print*, 'emittance', emitun
print*, 'EMS size', sig
print*, 'square root transverse energy', sigxp
do i = 1, Npt
call random_number(r0)
z(i) = 3e-3*r0-1.5e-3
call gaussian(rg)
dg(i) = sigdg*rg
call gaussian(rg)
x(i) = sig*rg
call gaussian(rg)
px(i) = sigxp*rg
call gaussian(rg)
y(i) = sig*rg
call gaussian(rg)
py(i) = sigxp*rg
write(10,110)x(i),px(i),y(i),py(i),z(i),dg(i)
enddo
110 format(6(1x,e15.7))
do i = 1, Npt
beammat(i,1) = x(i)
beammat(i,2) = px(i)
beammat(i,3) = y(i)
beammat(i,4) = py(i)
beammat(i,5) = z(i)
beammat(i,6) = dg(i)
enddo
!print*, '***', beammat(3,3)
end subroutine initial_distribution
!!! -----------------------------------------------------------------------
subroutine gaussian(rg)
real*8 :: r1,r2,rr,rg,phi
twopi = 4*asin(1.0)
call random_number(r1)
call random_number(r2)
phi = twopi*r1
rr = sqrt(-2*log(r2))
rg = rr*cos(phi)
end subroutine
!!! -----------------------------------------------------------------------
subroutine plothistogram(Npt, q, gamma0,mat)
implicit real*8 (a-h,o-z)
integer, intent(in) :: Npt
real*8, intent(in) :: mat(Npt,6), q, gamma0
integer, parameter :: numbins = 10
integer :: i, hist(numbins)
real*8 :: bunchlength, energy, emitun, meanloc, sumdz, mean_pp, mean_xx, mean_px, tlength
real*8 :: x(Npt), px(Npt), y(Npt), py(Npt), z(Npt), dg(Npt), dz(Npt), pp(Npt), xx(Npt), xp(Npt)
!print*, '***', mat(3,3)
!# define the bin number
!# define the value range
do i = 1, Npt
x(i) = mat(i,1)
px(i) = mat(i,2)
y(i) = mat(i,3)
py(i) = mat(i,4)
z(i) = mat(i,5)
dg(i) = mat(i,6)
end do
!# printout the parameters
meanloc = sum(z) / Npt
dz = z - meanloc
sumdz = sum(dz)
!print*, 'meanloc', meanloc
!print*, 'sumdz', sumdz
rms_bunchlength = 2d0 * sqrt(sumdz/Npt)
energy = maxval(dg) / q + gamma0
do i = 1, Npt
pp(i) = px(i)*px(i)
xx(i) = x(i)*x(i)
xp(i) = px(i)*x(i)
end do
mean_pp = sum(pp) / Npt
mean_xx = sum(xx) / Npt
mean_px = sum(xp) / Npt
emitun = sqrt(mean_pp * mean_xx - mean_px * mean_px)
print*, 'output parameters ------------------------------'
print*, 'rms bunch length', rms_bunchlength
print*, 'peak energy', energy
print*, 'emittance', emitun
!!!plot histogram and printout data
!# classificate the particles
!## bunch length histogram
!tlength = maxval(z) - minval(z)
!binwidth = tlength / numbins
!minz = minval(z)
!do i = 1, Npt
! index = int( (z(i)-minz) / tlength ) + 1
! if (index > 0 .and. index <= numbins) then
! hist(index) = hist(index) + 1
! end if
!enddo
!# output the file for displaying histograms
!## bunch length
!do i = 1, numbins
! write(11,*) (minz + binwidth * i), hist(i)
!enddo
do i = 1, Npt
write(11,*) z(i)
enddo
!## energy distribution
do i = 1, Npt
write(12,110) z(i), (dg(i)/q + gamma0)
enddo
!## phase space emittance
do i = 1, Npt
write(13,110) x(i), px(i), y(i), py(i)
enddo
110 format(6(1x,e15.7))
end subroutine
!!! ------------------------------------------------------------------------