-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCSTtoOpal3DFiles.py
214 lines (167 loc) · 6.85 KB
/
CSTtoOpal3DFiles.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
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
# -*- coding: utf-8 -*-
"""
Created on Mon Feb 13 13:50:10 2017
@author: nneveu
ombine 3D linac files from CST
"""
import linecache
import numpy as np
import matplotlib.pyplot as plt
#==============================================================================
# This function combines E and H CST files
#==============================================================================
def CSTcombo(efilein, hfilein, ehfileout):
num_lines = sum(1 for line in open(efilein))
#efin = open(efilein, 'r')
ehout = open(ehfileout, 'w')
#Starting at line #3 because CST file has 2 lines of header
for i in range(3, num_lines+1):
lineE = linecache.getline(efilein, i)
lineH = linecache.getline(hfilein, i)
#print lineE.split()[0]
#print lineH.split()[0]
if lineE.split()[0] == lineH.split()[0]:
#print 'yes'
if lineE.split()[1]== lineH.split()[1]:
#print 'yes'
if lineE.split()[2]== lineH.split()[2]:
#print 'yes'
exreal = float(lineE.split()[3])*10**-6
eyreal = float(lineE.split()[4])*10**-6
ezreal = float(lineE.split()[5])*10**-6
hximag = lineH.split()[6]
hyimag = lineH.split()[7]
hzimag = lineH.split()[8]
ehout.write('%-20s %-20s %-20s %-20s %-20s %-20s\n' % (str(exreal), str(eyreal), str(ezreal), hximag, hyimag, hzimag))
return num_lines
#==============================================================================
# This function combines E and H CST files, and keeps xyz info
#==============================================================================
def CSTcomboxyz(efilein, hfilein, ehfileout):
num_lines = sum(1 for line in open(efilein))
print (num_lines)
#efin = open(efilein, 'r')
ehout = open(ehfileout, 'w')
for i in range(3, num_lines+1): #smaller file 2539203):
lineE = linecache.getline(efilein, i)
lineH = linecache.getline(hfilein, i)
#print lineE.split()[0]
#print lineH.split()[0]
if lineE.split()[0] == lineH.split()[0]:
#print 'yes'
if lineE.split()[1]== lineH.split()[1]:
#print 'yes'
if lineE.split()[2]== lineH.split()[2]:
#print 'yes'
x = lineE.split()[0]
y = lineE.split()[1]
z = lineE.split()[2]
exreal = lineE.split()[3]
eyreal = lineE.split()[4]
ezreal = lineE.split()[5]
hximag = lineH.split()[6]
hyimag = lineH.split()[7]
hzimag = lineH.split()[8]
ehout.write('%-10s %-10s %-10s %-20s %-20s %-20s %-20s %-20s %-20s\n' % (x, y, z, exreal, eyreal, ezreal, hximag, hyimag, hzimag))
return num_lines
def reorderCST(EHcombofile, reorderedFile, freq, gridarray, dimarray):
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Step 2:
# Write Header info
# This info needs to be adjusted by hand
freq = str(freq)
snx = str(gridarray[0])
sny = str(gridarray[1])
snz = str(gridarray[2])
reorder = open(reorderedFile, 'w')
reorder.write('3DDynamic XYZ\n')
reorder.write(freq+'\n')
reorder.write(dimarray[0]+' '+dimarray[1]+' '+snx+'\n')
reorder.write(dimarray[2]+' '+dimarray[3]+' '+sny+'\n')
reorder.write(dimarray[4]+' '+dimarray[5]+' '+snz+'\n')
#Number of grid points in each dimension
nx = gridarray[0] +1
ny = gridarray[1] +1
nz = gridarray[2] +1
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Step 3:
# Reorder E and H values
numlines = sum(1 for line in open(EHcombofile))
#Checking file size is correct
if numlines != nx*ny*nz :
print('Not the right number of lines!',numlines, nx*ny*nz )
# This is the right order to convert from XYZ to ZYX format
print(nx, ny, nz)
dx = nx*ny
dy = ny
dz = 1
#==============================================================================
# for k in range(0,nx):
# dlz = k*dz
#
# for j in range(0, ny):
# dly = j*dy
#
# for i in range(0,nz):
# dlx = i*dx
#==============================================================================
for k in range(0,nx):
dlz = k*dz
for j in range(0, ny):
dly = j*dy
for i in range(0,nz):
dlx = i*dx
zline = dlx + dly + dlz + 1
reorder.write(linecache.getline(EHcombofile, zline))
reorder.close()
def plotEz(efile, nx):
#plot Ez on axis from CST E file
ez = np.zeros(nx+1)
n = 0
for line in open(efile):
sl = line.split()
try:
if sl[0] == sl[1] =='0':
#print(line)
ez[n] = sl[5]
n = n+1
except:
print(line)
#print(ez)
plt.plot(ez)
plt.show()
return(ez)
#==============================================================================
# testing functions
#==============================================================================
#==============================================================================
# num_lines_gun = CSTcombo('field_files/e3D_gun_Zheng.txt', 'field_files/h3D_gun_Zheng.txt', 'hold1.txt')
# #num_lines = CSTcomboxyz('field_files/e3D_gun_Zheng.txt', 'field_files/h3D_gun_Zheng.txt', 'test.txt')
# reorderCST('hold.txt', 'DriveGun_3D.txt', 'gun')
#==============================================================================
#old Gun info
#nx = 80 +1
#ny = 80 +1
#nz = 376 +1
#reorder.write('1300.151204\n')
#reorder.write('-2.5 2.5 80\n')
#reorder.write('-2.5 2.5 80\n')
#reorder.write('0.0 23.271 376\n')
#old Linac info
#nx = 46 +1
#ny = 46 +1
#nz = 1200 +1
#reorder.write('1300.341684\n')
#reorder.write('-4.6 4.6 46\n')
#reorder.write('-4.6 4.6 46\n')
#reorder.write('0.0 120.0 1200\n')
#num_lines = CSTcomboxyz('field_files/e3d_LinacLargeMeshZheng.txt', 'field_files/h3d_LinacLargeMeshZheng.txt', 'test.txt')
#num_lines_linac = CSTcombo('field_files/e3d_LinacLargeMeshZheng.txt', 'field_files/h3d_LinacLargeMeshZheng.txt', 'hold2.txt')
#reorderCST('hold2.txt', 'DriveLinac_3D_2.txt', 'linac')
#num_lines = sum(1 for line in open('DriveLinac3D_CosMinusSin.txt'))
#num_lines_linac = CSTcombo('6_SYMM_cav_WGshort_holes_Eigen_E.txt', '6_SYMM_cav_WGshort_holes_Eigen_H.txt', 'hold.txt')
freq = 9410
gridarray = np.array([20,20,1000])
dimarray = ['-0.2','0.2','-0.2','0.2','-0.51','32.3']
#reorderCST('hold.txt', 'EuclidGun_3D.txt', freq, gridarray, dimarray)
plotEz('6_SYMM_cav_WGshort_holes_Eigen_E.txt', 1000)