mstm_parameters.py
4.16 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
class ParameterClass:
#minimum and maximum wavelengths for the simulation
minLambda = 0.300
maxLambda = 0.700
snapshotLambda = 0.300
#number of spectral samples
nSamples = 40
#spatial samples
nSteps = 100
#sphere size and separation
a = 0.025
d = 0.005
#material file name
matFilename = 'etaSilver.txt'
#are the sphere's in water?
inWater = False
#show console output from the MS-TM FORTRAN program
showOutput = False
paramDict = {}
sphereList = []
sphereParamNames = ['radius', 'X', 'Y', 'Z', 'n', 'k', 'Xr', 'Xi']
def __init__(self, fileName):
self.loadFile(fileName)
def __getitem__(self, key):
return self.paramDict[key];
def __setitem__(self, key, value):
self.paramDict[key] = value;
def clearSpheres(self):
self.sphereList = []
def addSphere(self, a, x, y, z, n = 1.0, k=1.0):
s = [a, x, y, z, n, k]
self.sphereList.append(s)
def loadFile(self, fileName):
inpFID = open(fileName, 'r')
selfparamDict = {}
while 1:
key = inpFID.readline().strip()
#deal with sphere sizes and positions
if key == 'sphere_sizes_and_positions':
while True:
#load the parameters for a sphere
value = inpFID.readline().strip()
if value == 'end_of_options':
break
self.sphereList.append(value.split(' '))
elif not key:
break
elif key == 'end_of_options':
break
#deal with the near field plane
elif key == 'near_field_plane_vertices':
value = inpFID.readline().strip()
#self.paramDict[key] = list(map(float, value.split(',')))
self.paramDict[key] = [-1, -1, 1, 1]
else:
value = inpFID.readline().strip()
self.paramDict[key] = value
#update the length scale factor to deal with the UI
self.paramDict['length_scale_factor'] = (2.0 * 3.14159)/self.snapshotLambda
inpFID.close()
def saveFile(self, l, fileName):
#print(self)
#open the output file
outFID = open(fileName, 'w')
#write the parameters
for key in self.paramDict.keys():
outFID.write(key + '\n')
#deal with the near field plane
if key == 'near_field_plane_vertices':
#these have to be scaled by the length scale factor
ls = (2 * 3.14159)/l
v = self.paramDict[key]
outFID.write(str(v[0]*ls) + ',' + str(v[1]*ls) + ',' + str(v[2]*ls) + ',' + str(v[3]*ls) + '\n')
elif key == 'spacial_step_size':
ls = (2 * 3.14159)/l
dx = self.paramDict[key] * ls
outFID.write(str(dx) + '\n')
else:
outFID.write(str(self.paramDict[key]) + '\n')
#write the spheres
outFID.write("sphere_sizes_and_positions\n")
for s in self.sphereList:
for p in s:
outFID.write(str(p) + ' ')
outFID.write('\n')
outFID.write("end_of_options")
def __str__(self):
#print(self.paramDict)
result = ""
for key in self.paramDict.keys():
#deal with the near field plane
if key == 'near_field_plane_vertices':
v = map(str, self.paramDict[key])
result += key + ": " + v[0] + ',' + v[1] + ',' + v[2] + ',' + v[3] + '\n'
else:
result += key + ": " + str(self.paramDict[key]) + '\n'
result += "\n"
result += "Spheres:\n"
#iterate through each sphere
for s in self.sphereList:
result += "------------------\n"
for i in range(len(s)):
result += self.sphereParamNames[i] + ": " + str(s[i]) + '\n'
return result