Blame view

mstm_materials.py 2.79 KB
de89d3bc   dmayerich   Initial commit.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
  class MaterialSampleClass:
      
      #constructor      
      def __init__(self, l, n):
          self.l = l
          self.n = n
  
      #string conversion
      def __str__(self):
          result = ""
          result += str(self.l) + 'um: ' + str(self.n)
          return result
          
  class MaterialClass:
      materialList = []
  
      def __init__(self, fileName=""):
6f131540   dmayerich   Additional UI cha...
18
19
20
21
          
          self.materialList = []
          
          if fileName != "":
de89d3bc   dmayerich   Initial commit.
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
              self.loadFile(fileName)
  
      #when the material is cast to a string, create the list of refractive indices
      def __str__(self):
          nSamples = len(self.materialList)
          result = ""
          for i in range(nSamples):
              result += str(self.materialList[i]) + '\n'
          return result
  
      def __len__(self):
          return len(self.materialList)
  
      def __getitem__(self, l):
          bigI = smallI = 0;
          bigV = 999;
          smallV = 0;
          #find the smallest sample larger than l
          for i in range(len(self.materialList)):
              if self.materialList[i].l > l and self.materialList[i].l < bigV:
                  bigI = i
                  bigV = self.materialList[i].l
              if self.materialList[i].l < l and self.materialList[i].l > smallV:
                  smallI = i
                  smallV = self.materialList[i].l
  
          a = (l - smallV)/(bigV - smallV)
  
          bigN = self.materialList[bigI].n
          smallN = self.materialList[smallI].n
  
          n = a * bigN + (1 - a) * smallN
  
          return MaterialSampleClass(l, n)
          
  
          #print(str(self.materialList[smallI].l) + "---" + str(self.materialList[bigI].l))
          
          return self.materialList[smallI]
  
      def add(self, l, n):
          m = MaterialSampleClass(l, n)
          self.materialList.append(m)
  
      def clip(self, minLambda, maxLambda):
          #this function clips all material samples to the range [minLambda, maxLambda]
          self.materialList = list(filter(lambda m: m.l > minLambda, self.materialList))
          self.materialList = list(filter(lambda m: m.l < maxLambda, self.materialList))
  
  
      def addSolution(self, n):
          #places the material in a solution (divide by the solution's n)
          for i in range(len(self.materialList)):
              self.materialList[i].n = self.materialList[i].n / n
  
          
  
      def loadFile(self, fileName):
          #open the real refractive index file
          irFID = open(fileName, 'r')
          #read the first line to get the units (wavelength (um) or wavenumber (cm^2))
edf1d940   dmayerich   UI modifications ...
83
          lightUnits = irFID.readline().split()[0]
de89d3bc   dmayerich   Initial commit.
84
85
86
  
          #load the material
          for line in irFID:
edf1d940   dmayerich   UI modifications ...
87
              l, n, k = map(float, line.split())
de89d3bc   dmayerich   Initial commit.
88
89
90
91
92
93
94
95
96
  
              #if units are in wavenumber, convert to wavelength
              if lightUnits == "nu":
                  l = l/10000
  
              self.add(l, complex(n, k))
  
          #close the file
          irFID.close()