-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUTILITY_modifyAndSaveInputBeam.py
55 lines (45 loc) · 1.62 KB
/
UTILITY_modifyAndSaveInputBeam.py
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
from pmd_beamphysics import ParticleGroup
import numpy as np
import os
import random
def modifyAndSaveInputBeam(
inputBeamFilePath,
betaX = None,
alphaX = None,
betaY = None,
alphaY = None,
numMacroParticles = None,
timeCenterTF = True):
#Import
P = ParticleGroup(inputBeamFilePath)
#Downsample
#if numMacroParticles:
# P.data.update(resample_particles(P, n=numMacroParticles))
#PROBLEM! Built-in resampling smushes everything down to a single particle weight. No good for me since I'm using those to keep track of driver/witness
#Instead, since the weights are ~equal, just pick a random subset then rescale their weights
initialImportSize = np.size(P.x)
if numMacroParticles:
numMacroParticles = int(numMacroParticles)
P = P[random.sample(range(initialImportSize), numMacroParticles)]
P.weight = P.weight * (initialImportSize / numMacroParticles)
#Time center
if timeCenterTF:
P.t=P.t-np.mean(P.t) #This is OK because present beam doesn't have different weights; np.unique(P.weight)
#Apply linear matching
if (betaX is not None) and (alphaX is not None):
P.twiss_match(
plane='x',
beta = betaX,
alpha = alphaX,
inplace=True)
if (betaY is not None) and (alphaY is not None):
P.twiss_match(
plane='y',
beta = betaY,
alpha = alphaY,
inplace=True)
filePath = os.getcwd()
#Write as the active file
P.write(f'{filePath}/beams/activeBeamFile.h5')
#Also return the beam object
return P