-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMag3to2.py
More file actions
executable file
·123 lines (106 loc) · 4.74 KB
/
Mag3to2.py
File metadata and controls
executable file
·123 lines (106 loc) · 4.74 KB
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
#!/usr/bin/python3
'''
A package to convert magfield3 style inputs to those for magfield2. If called from the shell as:
python3 Mag3to2.py </path/to/input/> </path/to/output>
Then the conversion is done, creating a new file. The package will also be importable so that it can be used in some larger context or so that pieces can be used.
'''
import os, sys, math
class Mag3to2:
'''
Converter from magfield3 input to magfield2 input
'''
def __init__(self,mf3file=False,mf2file=False):
'''
If input and output files are given then does a "default" magfield3 to magfield2 conversion.
mf3file: (optional) the filename or path to the magfield3 input file
mf2file: (optional) the file to create with the magfield2 output
'''
if (mf3file and mf2file):
self.ReadInputFile(mf3file)
self.EnsureColinear()
self.ConvertArray()
self.WriteOutput(mf2file)
def ReadInputFile(self,filename):
'''
Grab the input magfield3 data from a file on disk and store as an attribute
No explicit return values.
'''
filecontent = open(filename,'r').readlines()[1:]
setattr(self,'mf3array',[list(map(float,line.split())) for line in filecontent])
def EnsureColinear(self):
'''
Ensure that all of the magfield3 input coils are colinear.
Returns True if colinear, False otherwise.
'''
colinear = True
#get a vector for the first coil.
coil0vec = [B-A for A,B in zip(self.mf3array[0][1:4],self.mf3array[0][4:7])]
#normalize to get a unit vector.
norm = 0
for val in coil0vec: norm = norm + val**2
norm=math.sqrt(norm)
coil0dir = [val/norm for val in coil0vec]
for coil in self.mf3array:
vecA = [B-A for A,B in zip(coil[1:4],self.mf3array[0][1:4])]
vecB = [B-A for A,B in zip(coil[4:7],self.mf3array[0][1:4])]
normA = 0
normB = 0
for val in vecA: normA = normA + val**2
for val in vecB: normB = normB + val**2
normA = math.sqrt(normA)
normB = math.sqrt(normB)
if normA !=0:
dirAfwd = [val/normA for val in vecA]
dirAbwd = [-val/normA for val in vecA]
else:
dirAfwd = coil0dir
dirAbwd = coil0dir
if normB !=0:
dirBfwd = [val/normB for val in vecB]
dirBbwd = [-val/normB for val in vecB]
else:
dirBfwd = coil0dir
dirBbwd = coil0dir
colinear = colinear and ((coil0dir == dirAfwd) or (coil0dir == dirAbwd))
colinear = colinear and ((coil0dir == dirBfwd) or (coil0dir == dirBbwd))
return colinear
def ConvertArray(self):
'''
Take a list of lists, each of which is a magfield3 coil and produce a corresponding list of lists, each of which is a magfield2 coil.
The magfield3 data is in self.mf3array
The produced magfield2 data gets stored as self.mf2array
'''
mf2array = []
for coil in self.mf3array:
zmid = math.sqrt(((coil[4] + coil[1]) / 2)**2 + ((coil[5] + coil[2])/2)**2 + ((coil[6] + coil[3])/2)**2)
if (((coil[4] + coil[1]) < 0) or ((coil[5] + coil[2]) < 0) or ((coil[6] + coil[3]) < 0)):
zmid=-zmid
rin = coil[7]
thick = coil[8] - coil[7]
length = math.sqrt((coil[4]-coil[1])**2+(coil[5]-coil[2])**2+(coil[6]-coil[3])**2)
cur = coil[0]*(thick)*(length)
mf2array.append([zmid, rin, thick, length, cur])
setattr(self,'mf2array',mf2array)
def WriteOutput(self,filename):
'''
Write the computed magfield2 coils to a file in the magfield2 input format.
The magfield2 coil data comes from self.mf2array
The output is written to filename.
'''
outputfile = open(filename, 'w')
outputfile.write(str(len(self.mf2array))+'\n')
for coil in self.mf2array:
outputfile.write('%f %f %f %f %f' % tuple(coil) + '\n')
outputfile.close()
# If called from the terminal, fully convert input file and create an output:
if __name__=='__main__':
try:
if len(sys.argv) != 3:
raise IndexError('The number of inputs is not correct')
if not os.path.exists(sys.argv[1]):
raise NameError('file named %s does not exist' % sys.argv[1])
if os.path.exists(sys.argv[2]):
raise NameError('file named %s already exists' % sys.argv[2])
MagConv = Mag3to2(sys.argv[1],sys.argv[2])
except IndexError:
print('There is a problem with the inputs provided')