forked from ThorstenHellert/SC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SCgenBunches.m
87 lines (79 loc) · 2.33 KB
/
SCgenBunches.m
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
function Z = SCgenBunches(SC)
% SCgenBunches
% ============
%
% NAME
% ----
% SCgenBunches - Generate injected beam particle coordinates.
%
% SYNOPSIS
% --------
% `Z = SCgenBunches(SC)`
%
%
% DESCRIPTION
% -----------
% Generates bunches according to the current injection setup as defined in
% `SC.INJ`. The injection setup includes the fields:
%
% `nParticles`::
% Number of particles for tracking.
% `nTurns`::
% Number of turns for tracking.
% `nShots`::
% Number of injections used for averaging the BPM readings.
% `Z0ideal`::
% [6 x 1] array defining the ideal injection vector.
% `Z0`::
% [6 x 1] array defining the current injection vector.
% `beamSize`::
% [6 x 6] array defining the beam sigma matrix.
% `randomInjectionZ`::
% [6 x 1] array defining the shot-to-shot injection jitter.
% `trackMode`::
% String defining the tracking mode.
%
% For each bunch the random injection error is calculated and added to the mean
% injected beam trajectory. If the number of particles per bunch is larger than
% one, individual particle launch conditions are randomly distributed around
% the bunch centroid using the beam sigma matrix. Otherwise the single
% macro-particle per bunch is launched at the bunch centroid trajectory. The
% global injection count `SCinjections` is increased appropriately. If a
% post-injection function is defined in `SC.INJ.postFun`, it is applied on the
% generated particles.
%
% INPUTS
% ------
%
% `SC`::
% SC base structure
%
%
% RETURN VALUES
% -------------
% `Z`::
% [6 x (#Bunches x #Shots x #Particles)] array of injected beam particle coordinates
%
%
% SEE ALSO
% --------
% *SCgetBPMreading*
global SCinjections
% Calculate bunch centroid trajectory
Z = repmat(SC.INJ.randomInjectionZ.*SCrandnc(2,6,1) + SC.INJ.Z0 ,1,SC.INJ.nParticles);
% If not only single macro particle is used
if SC.INJ.nParticles~=1
% Perform eigenvalue decomposition -> chol() doesn't work if sigma is not positive definite
[V,L] = eig(SC.INJ.beamSize);
% Generate particles from sigma matrix
particles = V*sqrt(L) * SCrandnc(3,6,SC.INJ.nParticles);
% Add to bunch centroid coordinates
Z = Z + particles;
end
% Apply post injection function, if defined
if isfield(SC.INJ,'postFun') && isa(SC.INJ.postFun, 'function_handle')
Z = SC.INJ.postFun(Z);
end
% Increase total number of injected beams
SCinjections = SCinjections + 1;
end