This repository has been archived by the owner on Jul 28, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmaterials.py
2735 lines (2129 loc) · 103 KB
/
materials.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# materials.py
#
# Classes for defining the optical properties of materials and a
# function to read the properties of a material in a material_file.
#
# Copyright (c) 2000-2003,2005-2008,2010-2015 Stephane Larouche.
#
# This file is part of OpenFilters.
#
# OpenFilters is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or (at
# your option) any later version.
#
# OpenFilters is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
# USA
import os
import os.path
import re
import math
import cmath
import copy
import config
from definitions import *
import abeles
import main_directory
import simple_parser
import units
# The various models for the optical properties.
CONSTANT = 0
CAUCHY = 1
TABLE = 2
SELLMEIER = 3
# A few useful dictionaries.
KINDS = {MATERIAL_REGULAR: "Regular",
MATERIAL_MIXTURE: "Mixture"}
MODELS = {CONSTANT: "Constant",
CAUCHY: "Cauchy",
TABLE: "Table",
SELLMEIER: "Sellmeier"}
# The material directory is relative to the filters directory.
base_directory = main_directory.get_main_directory()
default_material_directory = os.path.join(base_directory, *config.MATERIALS_DIRECTORY)
########################################################################
# #
# material_error #
# #
########################################################################
class material_error(Exception):
"""An exception derived class for material errors."""
def __init__(self, name, value = ""):
self.name = name
self.value = value
def __str__(self):
if self.value:
return "Material error (%s): %s." % (self.name, self.value)
else:
return "Material error (%s)." % self.name
########################################################################
# #
# material_does_not_exist_error #
# material_parsing_error #
# #
########################################################################
class material_does_not_exist_error(material_error):
"""An exception derived class for materials that do not exist."""
def __init__(self, name):
self.name = name
self.value = "Material does not exist"
class material_parsing_error(material_error):
"""An exception derived class for material with malformed files."""
########################################################################
# #
# material #
# #
########################################################################
class material(object):
"""A generic class to define materials
All material class derive from this class."""
kind = MATERIAL_REGULAR
######################################################################
# #
# __init__ #
# #
######################################################################
def __init__(self):
"""Initialize the material instance"""
# The name and description of the material
self.name = ""
self.description = ""
# An object for the dispersion.
self.dispersion = None
# Deposition rate.
self.rate = None
# Parameters used during deposition, if specified. constants
# contain names of these parameters while constant_values contain
# values of these parameters.
self.constants = []
self.constant_values = []
######################################################################
# #
# clone #
# #
######################################################################
def clone(self):
"""Get a copy of the material
This method returns a clone of the material."""
clone = self.__class__()
clone.__init__()
clone.name = self.name
clone.description = self.description
clone.rate = copy.copy(self.rate)
clone.constants = self.constants[:]
clone.constant_values = self.constant_values[:]
clone.set_properties(*copy.deepcopy(self.get_properties()))
return clone
######################################################################
# #
# set_name #
# #
######################################################################
def set_name(self, name):
"""Set the name of the material
This method takes a single argument:
name the name of the material."""
self.name = name
######################################################################
# #
# get_name #
# #
######################################################################
def get_name(self):
"""Get the name of the material
This method returns the name of the material."""
return self.name
######################################################################
# #
# set_description #
# #
######################################################################
def set_description(self, description):
"""Set the description of the material
This method takes a single argument:
description the description of the material."""
self.description = description
######################################################################
# #
# get_description #
# #
######################################################################
def get_description(self):
"""Get the description of the material
This method returns the description of the material."""
return self.description
######################################################################
# #
# get_kind #
# #
######################################################################
def get_kind(self):
"""Get the kind of material
This method returns the kind of material (MATERIAL_REGULAR or
MATERIAL_MIXTURE)."""
return self.kind
######################################################################
# #
# is_mixture #
# #
######################################################################
def is_mixture(self):
"""Get if the material is a mixture
This method returns a boolean indicating if the material is a
mixture."""
if self.kind == MATERIAL_MIXTURE:
return True
else:
return False
######################################################################
# #
# get_model #
# #
######################################################################
def get_model(self):
"""Get the model of the material
This method returns the model used to define the dispersion curve
of the material."""
return self.model
######################################################################
# #
# set_properties #
# #
######################################################################
def set_properties(self):
"""Set the properties of the material
The derived class implement this method according to the
dispersion model used."""
raise NotImplementedError("Subclass must implement this method")
######################################################################
# #
# get_properties #
# #
######################################################################
def get_properties(self):
"""Get the properties of the material
The derived class implement this method according to the
dispersion model used."""
raise NotImplementedError("Subclass must implement this method")
######################################################################
# #
# get_index #
# #
######################################################################
def get_index(self, wvl):
"""Get the index of refraction of the material
This method takes a single argument:
wvl a wavelength
and returns the real part of the index of refraction at this
wavelength. This method is not implement for mixtures.
The derived class implement this method according to the
dispersion model used."""
raise NotImplementedError("Subclass must implement this method")
######################################################################
# #
# get_N #
# #
######################################################################
def get_N(self, wvls):
"""Get the dispersion curve of the material
This method takes a single argument:
wvls a wavelengths structure
and returns an index structure with the dispersion curve of the
material at those wavelengths.
The derived class implement this method according to the
dispersion model used."""
raise NotImplementedError("Subclass must implement this method")
######################################################################
# #
# set_deposition_rate #
# #
######################################################################
def set_deposition_rate(self, rate):
"""Set the deposition rate of the material
This method takes a single argument:
rate the deposition rate of the material."""
self.rate = rate
######################################################################
# #
# get_deposition_rate #
# #
######################################################################
def get_deposition_rate(self):
"""Get the deposition rate of the material
This method returns the deposition rate of the material."""
return self.rate
######################################################################
# #
# set_deposition_constants #
# #
######################################################################
def set_deposition_constants(self, constants, constant_values):
"""Set the constant deposition conditions of the material
This method takes 2 arguments:
constants the name of the deposition properties;
constant_values their values.
Regular materials only have constant deposition conditions.
Mixtures have variable deposition conditions and might also have
constant deposition conditions."""
self.constants = constants
self.constant_values = constant_values
######################################################################
# #
# get_deposition_constants #
# #
######################################################################
def get_deposition_constants(self):
"""Get the deposition conditions of the material
This method returns the deposition condition of the material:
constants the name of the deposition properties;
constant_values their values."""
return self.constants, self.constant_values
########################################################################
# #
# material_mixture #
# #
########################################################################
class material_mixture(material):
"""A generic class to define material mixtures
All mixture materials class are derived from this class.
Mixtures class are implemented with the deposition process in mind.
The deposition systems have a limited resolution and cannot deposit
any arbitrary index of refraction, but rather a large, but finite,
number of mixtures. The mixtures are therefore represented by integer
mixture numbers called x values.
When the dispersion of the mixtures is defined in set_properties, it
is not possible to define it for all x values. They are rather
defined by at least 3 x values between 0 and the number of
depositable mixtures minus one. The dispersion of the other mixtures
is interpolated using cubic splines."""
kind = MATERIAL_MIXTURE
######################################################################
# #
# __init__ #
# #
######################################################################
def __init__(self):
"""Initialize the material instance"""
material.__init__(self)
# Deposition conditions are described by a number.
self.X = []
# Parameters used during deposition, if specified. constants and
# variables contain names of these parameters while constants_values
# and variables_values contain values of these parameters.
self.variables = []
self.variable_values = []
######################################################################
# #
# clone #
# #
######################################################################
def clone(self):
"""Get a copy of the material
This method returns a clone of the material."""
clone = material.clone(self)
clone.variables = copy.deepcopy(self.variables)
clone.variable_values = copy.deepcopy(self.variable_values)
return clone
######################################################################
# #
# set_deposition_variables #
# #
######################################################################
def set_deposition_variables(self, variables, variable_values):
"""Set the variable deposition conditions of the material
This method takes 2 arguments:
variables the name of the deposition properties;
variable_values a list of lists of their values."""
self.variables = variables
self.variable_values = variable_values
######################################################################
# #
# get_deposition_variables #
# #
######################################################################
def get_deposition_variables(self):
"""Get the variable deposition conditions of the material
This method returns the variable deposition conditions:
variables the name of the deposition properties;
variable_values a list of lists of their values."""
return self.variables, self.variable_values
######################################################################
# #
# get_deposition_steps #
# #
######################################################################
def get_deposition_steps(self, wvl):
"""Get the deposition steps
This method takes a single argument:
wvl a wavelength;
and returns a list of the index of refraction of all the mixtures
that can be deposited at this wavelength.
The derived class must implement this method according to the
dispersion model."""
raise NotImplementedError("Subclass must implement this method")
######################################################################
# #
# get_index_range #
# #
######################################################################
def get_index_range(self, wvl):
"""Get the range of available index of refraction
This method takes a single argument:
wvl a wavelength;
and returns the range of indices of refraction.
The derived class must implement this method according to the
dispersion model."""
raise NotImplementedError("Subclass must implement this method")
######################################################################
# #
# change_index_wavelength #
# #
######################################################################
def change_index_wavelength(self, old_n, old_wvl, new_wvl):
"""Convert the index of refraction to another wavelength
This method takes 3 argument:
old_n an index of refraction;
old_wvl the wavelength at which old_n is defined;
new_wvl the wavelength at which the index is desired;
and returns the index of refraction at new_wvl using the dispersion
relations of the mixture.
The derived class must implement this method according to the
dispersion model."""
raise NotImplementedError("Subclass must implement this method")
######################################################################
# #
# change_index_profile_wavelength #
# #
######################################################################
def change_index_profile_wavelength(self, index_profile, old_wvl, new_wvl):
"""Change the wavelength at which an index profile is defined
This method takes 3 arguments:
index_profile a list containing the index profile;
old_wvl the wavelength at which the index profile is
defined;
new_wvl the wavelength at which the index profile
must be defined;
and modifies index_profile to the new_wvl.
The wavelength of the index profile is changed by repeatadly
calling change_index_wavelength on every element of the index
profile. It is possible to accelerate the execution of this
method by making a dispersion model specific method that does not
call change_index_wavelength."""
if new_wvl != old_wvl:
for i in range(len(index_profile)):
index_profile[i] = self.change_index_wavelength(index_profile[i], old_wvl, new_wvl)
######################################################################
# #
# check_monotonicity #
# #
######################################################################
def check_monotonicity(self, wvl):
"""Check if the refractive index is monotonic at a wavelength
This method takes 1 argument:
wvl the wavelength;
and returns a boolean value.
This method allows one to make sure that the refractive index is
monotonic at a given wavelength. This monotonicity is necessary for
the interpolation methods to work correctly."""
raise NotImplementedError("Subclass must implement this method")
########################################################################
# #
# material_constant #
# #
########################################################################
class material_constant(material):
"""A class for dispersion-less materials"""
model = CONSTANT
######################################################################
# #
# __init__ #
# #
######################################################################
def __init__(self):
"""Initialize the material instance"""
material.__init__(self)
# Value of the index.
self.N = 0.0+0.0j
# The dispersion object.
self.dispersion = abeles.constant()
######################################################################
# #
# set_properties #
# #
######################################################################
def set_properties(self, N):
"""Set the properties of the material
This method takes a single argument:
N the complex index of refraction of the
material."""
self.N = N
# Set the dispersion object.
self.dispersion.set_constant(self.N)
######################################################################
# #
# get_properties #
# #
######################################################################
def get_properties(self):
"""Get the properties of the material
This method returns the complex index of refraction of the
material."""
return self.N,
######################################################################
# #
# get_index #
# #
######################################################################
def get_index(self, wvl):
"""Get the index of refraction of the material
This method takes a single argument:
wvl a wavelength
and returns the real part of the index of refraction at this
wavelength."""
return self.N.real
######################################################################
# #
# get_N #
# #
######################################################################
def get_N(self, wvls):
"""Get the dispersion curve of the material
This method takes a single argument:
wvls a wavelengths structure
and returns an index structure with the dispersion curve of the
material at those wavelengths."""
N = abeles.N(wvls)
self.dispersion.set_N_constant(N)
return N
########################################################################
# #
# material_table #
# #
########################################################################
class material_table(material):
"""A class to define the dispersion curve of material with a table of
data"""
model = TABLE
######################################################################
# #
# __init__ #
# #
######################################################################
def __init__(self):
"""Initialize the material instance"""
material.__init__(self)
# Wavelengths and index where the dispersion is defined
self.wvls = []
self.N = []
######################################################################
# #
# set_properties #
# #
######################################################################
def set_properties(self, wvls, N):
"""Set the properties of the material
This method takes 2 arguments:
wvls a list of wavelengths;
N a list of the complex index of refraction at
those wavelengths."""
self.wvls = wvls
self.N = N
length = len(self.wvls)
# Set the dispersion object.
self.dispersion = abeles.table(length)
for i in range(length):
self.dispersion.set_table(i, self.wvls[i], self.N[i])
######################################################################
# #
# get_properties #
# #
######################################################################
def get_properties(self):
"""Get the properties of the material
This method returns the properties of the material:
wvls a list of wavelengths;
N a list of the complex index of refraction at
those wavelengths."""
return self.wvls, self.N
######################################################################
# #
# get_index #
# #
######################################################################
def get_index(self, wvl):
"""Get the index of refraction of the material
This method takes a single argument:
wvl a wavelength
and returns the real part of the index of refraction at this
wavelength."""
n = self.dispersion.get_table_index(wvl)
return n
######################################################################
# #
# get_N #
# #
######################################################################
def get_N(self, wvls):
"""Get the dispersion curve of the material
This method takes a single argument:
wvls a wavelengths structure
and returns an index structure with the dispersion curve of the
material at those wavelengths."""
N = abeles.N(wvls)
self.dispersion.set_N_table(N)
return N
########################################################################
# #
# material_Cauchy #
# #
########################################################################
class material_Cauchy(material):
"""A class to define the dispersion curve of material with a Cauchy
dispersion formula and an Urbach absorption tail"""
model = CAUCHY
######################################################################
# #
# __init__ #
# #
######################################################################
def __init__(self):
"""Initialize the material instance"""
material.__init__(self)
# Values of A, B and C in the Cauchy model if it is used. And,
# eventually the values of A_k, exponent and edge for the Urbach
# absorbtion tail. The exponent and the edge are not set to 0.0
# to avoit math range errors.
self.A = 0.0
self.B = 0.0
self.C = 0.0
self.A_k = 0.0
self.exponent = 1.0
self.edge = 4000.0
# Create the dispersion object.
self.dispersion = abeles.Cauchy()
######################################################################
# #
# set_properties #
# #
######################################################################
def set_properties(self, A, B, C, A_k, exponent, edge):
"""Set the properties of the material
This method takes 6 arguments:
A, B, C the parameters of the Cauchy model;
A_k, exponent, edge the parameters of the Urbach absorption
tail."""
self.A = A
self.B = B
self.C = C
self.A_k = A_k
self.exponent = exponent
self.edge = edge
# Set the dispersion object.
self.dispersion.set_Cauchy(self.A, self.B, self.C, self.A_k, self.exponent, self.edge)
######################################################################
# #
# get_properties #
# #
######################################################################
def get_properties(self):
"""Get the properties of the material
This method returns the properties of the material:
A, B, C the parameters of the Cauchy model;
A_k, exponent, edge the parameters of the Urbach absorption
tail."""
return self.A, self.B, self.C, self.A_k, self.exponent, self.edge
######################################################################
# #
# get_index #
# #
######################################################################
def get_index(self, wvl):
"""Get the index of refraction of the material
This method takes a single argument:
wvl a wavelength
and returns the real part of the index of refraction at this
wavelength."""
wvl_micron = wvl/1000.0
wvl_micron_square = wvl_micron*wvl_micron
n = self.A + self.B/wvl_micron_square + self.C/(wvl_micron_square*wvl_micron_square)
return n
######################################################################
# #
# get_N #
# #
######################################################################
def get_N(self, wvls):
"""Get the dispersion curve of the material
This method takes a single argument:
wvls a wavelengths structure
and returns an index structure with the dispersion curve of the
material at those wavelengths."""
N = abeles.N(wvls)
self.dispersion.set_N_Cauchy(N)
return N
########################################################################
# #
# material_Sellmeier #
# #
########################################################################
class material_Sellmeier(material):
"""A class to define the dispersion curve of material with a Sellmeier
dispersion formula"""
model = SELLMEIER
######################################################################
# #
# __init__ #
# #
######################################################################
def __init__(self):
"""Initialize the material instance"""
material.__init__(self)
# Values of Bs and Cs in the Sellmeier model.
self.B1 = 0.0
self.C1 = 0.0
self.B2 = 0.0
self.C2 = 0.0
self.B3 = 0.0
self.C3 = 0.0
self.A_k = 0.0
self.exponent = 1.0
self.edge = 4000.0
# Create the dispersion object.
self.dispersion = abeles.Sellmeier()
######################################################################
# #
# set_properties #
# #
######################################################################
def set_properties(self, B1, C1, B2, C2, B3, C3, A_k, exponent, edge):
"""Set the properties of the material
This method takes 6 arguments:
B1, C1, B2, C2, B3, C3 the parameters of the Sellmeier model;
A_k, exponent, edge the parameters of the Urbach absorption
tail."""
self.B1 = B1
self.C1 = C1
self.B2 = B2
self.C2 = C2
self.B3 = B3
self.C3 = C3
self.A_k = A_k
self.exponent = exponent
self.edge = edge
# Set the dispersion object.
self.dispersion.set_Sellmeier(self.B1, self.C1, self.B2, self.C2, self.B3, self.C3, self.A_k, self.exponent, self.edge)
######################################################################
# #
# get_properties #
# #
######################################################################
def get_properties(self):
"""Get the properties of the material
This method returns the properties of the material:
B1, C1, B2, C2, B3, C3 the parameters of the Sellmeier model;
A_k, exponent, edge the parameters of the Urbach absorption."""
return self.B1, self.C1, self.B2, self.C2, self.B3, self.C3, self.A_k, self.exponent, self.edge
######################################################################
# #
# get_index #
# #
######################################################################
def get_index(self, wvl):
"""Get the index of refraction of the material