-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPyEOC.py
1646 lines (1381 loc) · 71.9 KB
/
PyEOC.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
# -*- coding: utf-8 -*-
# Electro-optic coefficients calculation (PyEOC)
# Implementation of the method published by Cuniot-Ponsard et al. in JAP 109, 014107 (2011) -- https://doi.org/10.1063/1.3514083
# Code written by:
# Pr. Sidi Hamady
# Université de Lorraine, France
# Released under the MIT license (https://opensource.org/licenses/MIT)
# See Copyright Notice in COPYRIGHT
# https://github.com/sidihamady/PyEOC
# Sidi Ould Saad Hamady, "PyEOC: a Python Code for Determining Electro-Optic Coefficients of Thin-Film Materials", 2022.
# -----------------------------------------------------------------------------------------------
# import S J Byrnes' code -- https://pypi.python.org/pypi/tmm -- https://arxiv.org/abs/1603.02720
import tmmCore as tmm
# -----------------------------------------------------------------------------------------------
# import standard modules
import sys, os, time
import math
import numpy as np
from scipy.optimize import curve_fit
from scipy.interpolate import interp1d
from scipy.signal import savgol_filter
import matplotlib.pyplot as pl
try:
if sys.version_info[0] < 3:
import Tkinter as Tk
else:
import tkinter as Tk
# end if
except:
pass
# -----------------------------------------------------------------------------------------------
class PyEOC(object):
""" the PyEOC core class """
# -------------------------------------------------------------------------------------------
def __init__(self,
structure = 'SBN',
filename_RTE = '', filename_RTM = '',
filename_RTEdyn = '', filename_RTMdyn = ''):
""" PyEOC constructor """
self.verbose = False
self.knownstructures = ['SBN',] # to complete by your own structures
# theta angle range
self.toradian = np.pi / 180.0
self.todegree = 1.0 / self.toradian
self.thetaStart = 20.0 * self.toradian
self.thetaEnd = 80.0 * self.toradian
self.thetaDelta = 5.0 * self.toradian
self.reset(structure, filename_RTE, filename_RTM, filename_RTEdyn, filename_RTMdyn)
# number of lines to skip in each file (title and label lines for example)
self.linestoskip = 2
# data separator: '\t' for tabulation, by default
self.separator = '\t'
self.fontsize = 12
self.parameters_count = len(self.layers) * 3
self.parameters_maxcount = self.layers_maxcount * 3
# laser wavelength in nm
self.wavelength = 633.0
# applied voltage amplitude in volts
self.voltage = 1.0
# set self.plotderiv to plot delta_R/delta_d, delta_R/delta_n and delta_R/delta_k
self.plotderiv = True
self.theta_manual = None
self.theta_pos = [None, None]
self.theta_val = [None, None]
# variations in thickness (d) and refractive index (n and k)
# TE ('s') polarization
self.Ddo = None
self.Dno = None
self.Dko = None
# TM ('p') polarization
self.Dde = None
self.Dne = None
self.Dke = None
self.d = [None] * self.layers_maxcount
self.no = [None] * self.layers_maxcount
self.ko = [None] * self.layers_maxcount
self.ne = [None] * self.layers_maxcount
self.ke = [None] * self.layers_maxcount
# p = TM, s = TE
self.polarization = 'p'
self.polarizationall = ['s', 'p']
self.loaded = False
self.totald = 0.0
self.totalda = 0.0
self.fit_initTE = None
self.fit_initTM = None
self.fit_init = [self.fit_initTE, self.fit_initTM]
self.fitting = False
self.fittingdyn = False
self.evalcount = 0
self.evalcountdyn = 0
self.fitted = False
self.fit_TE = None
self.fit_TE_deriv = None
self.fit_TE_derivd = None
self.fit_TE_derivn = None
self.fit_TE_derivk = None
self.fit_TEdyn = None
self.fit_TM = None
self.fit_TM_deriv = None
self.fit_TM_derivd = None
self.fit_TM_derivn = None
self.fit_TM_derivk = None
self.fit_TMdyn = None
self.fit_result = [self.fit_TE, self.fit_TM]
self.fit_deriv = [self.fit_TE_deriv, self.fit_TM_deriv]
self.fit_derivd = [self.fit_TE_derivd, self.fit_TM_derivd]
self.fit_derivn = [self.fit_TE_derivn, self.fit_TM_derivn]
self.fit_derivk = [self.fit_TE_derivk, self.fit_TM_derivk]
self.fit_dyn = [self.fit_TEdyn, self.fit_TMdyn]
# Fitting parameters
self.fit_dynamic = True
self.fit_derivtol = 1e-5
self.fit_derivtol_min = 1e-12
self.fit_tol = 1e-6
self.fit_reltol = 1e-6
self.fitdyn_tol = 1e-10
self.fit_bounded = True
self.fit_paramTE = None
self.fit_paramTM = None
self.fit_param = [self.fit_paramTE, self.fit_paramTM]
self.fit_paramall = []
self.fit_stddev = [1.0, 1.0]
self.fit_mean = [1.0, 1.0]
self.fitdyn_stddev = [1.0, 1.0]
self.fitdyn_mean = [1.0, 1.0]
self.inJac = False
self.stopfilename = 'stop.txt'
# end __init__
# -------------------------------------------------------------------------------------------
def reset(self, structure = 'SBN',
filename_RTE = '', filename_RTM = '',
filename_RTEdyn = '', filename_RTMdyn = ''):
""" PyEOC constructor """
self.structure = structure
self.checked = False
self.error = False
self.message = None
self.r13 = None
self.d33 = None
self.DkDV = None
# to complete by your own structures
if self.structure == 'SBN':
# -----------------------------------------------------------------------------------
# multilayers structure data
# the default structure is the one published by Cuniot-Ponsard et al. in JAP 109, 014107 (2011)
# complete and modify to suit your needs. In theory, the number of layers in not limited but in practice the fitting...
# ... algorithm usually diverges for more than about seven layers (more or less, depending on the structure)
self.title = 'SBN (reference)'
self.layers = ['Pt', 'SBN', 'Pt', 'MgO']
self.layers_count = len(self.layers)
self.layers_maxcount = 7
# the active layer position (beginning with zero, without counting air)
self.active_layer = 1
# -----------------------------------------------------------------------------------
# the thickness and refractive index data are used as a starting point for the fitting algorithm
self.thickness = [
np.inf, # air
22.6, # layer 1 (platinum)
754.5, # layer 2 (SBN)
70, # layer 3 (platinum)
500000, # layer 4 (substrate)
np.inf # air
]
self.refractiveindexo = [
1.0, # air
2.33 + 4.14j, # layer 1 (platinum)
2.3 + 0.0515j, # layer 2 (SBN)
2.33 + 4.14j, # layer 3 (same than layer 1, platinum)
1.7346 + 0.0j, # layer 4 (substrate)
1.0 # air
]
self.refractiveindexe = [
1.0, # air
2.33 + 4.14j, # layer 1 (platinum)
2.26 + 0.0515j, # layer 2 (SBN)
2.33 + 4.14j, # layer 3 (same than layer 1, platinum)
1.7346 + 0.0j, # layer 4 (substrate)
1.0 # air
]
# -----------------------------------------------------------------------------------
# Parameters to include in the fitting procedure
# set to False for parameters already fitted for a previous identical layer
# (case of two layers of the same material)
self.fit_includeparam = [
True, # layer 1 thickness (d), platinum
True, # layer 1 refractive index real part (n), platinum
True, # layer 1 refractive index real part (k), platinum
True, # layer 2 d -- active layer, SBN
True, # layer 2 n -- active layer, SBN
True, # layer 2 k -- active layer, SBN
True, # layer 3 d, platinum
False, # layer 3 n (same than layer 1, platinum)
False, # layer 3 k (same than layer 1, platinum)
False, # layer 4 d
False, # layer 4 n
False # layer 4 k
]
# -----------------------------------------------------------------------------------
# layers parameters upper and lower limits
self.bounds = (
[
21.0, # layer 1 lower thickness (d, in nm) limit, platinum
2.30, # layer 1 lower refractive index real part (n) limit, platinum
4.0, # layer 1 lower refractive index real part (k) limit, platinum
750.0, # layer 2 lower d limit -- active layer, SBN
2.24, # layer 2 lower n limit -- active layer, SBN
0.050, # layer 2 lower k limit -- active layer, SBN
68.0, # layer 3 lower d limit, platinum
2.30, # layer 3 lower n limit (same than layer 1, platinum)
4.0, # layer 3 lower k limit (same than layer 1, platinum)
490000.0, # layer 4 lower d limit, MgO
1.70, # layer 4 lower n limit, MgO
0.00000 # layer 4 lower k limit, MgO
],
[
23.0, # layer 1 upper thickness (d, in nm) limit, platinum
2.35, # layer 1 upper refractive index real part (n) limit, platinum
4.2, # layer 1 upper refractive index real part (k) limit, platinum
760.0, # layer 2 upper d limit -- active layer, SBN
2.32, # layer 2 upper n limit -- active layer, SBN
0.052, # layer 2 upper k limit -- active layer, SBN
72.0, # layer 3 upper d limit, platinum
2.35, # layer 3 upper n limit (same than layer 1, platinum)
4.2, # layer 3 upper k limit (same than layer 1, platinum)
510000.0, # layer 4 upper d limit, MgO
1.75, # layer 4 upper n limit, MgO
0.00001 # layer 4 upper k limit, MgO
]
)
# -----------------------------------------------------------------------------------
# coherency option (usually it is not necessay to switch to incoherent case)
# up and bottom medium (usually air) should be set as incoherent
self.coherency = [
'i', # air
'c', # layer 1, platinum
'c', # layer 2, SBN
'c', # layer 3, platinum
'c', # layer 4, MgO
'i' # air
]
# -----------------------------------------------------------------------------------
# the incident angle theta starting three values and range
# the choosen theta values should correspond to a "smooth" and "different" part of the DR and delta_R / delta_? data
self.theta_manual = [35.0 * self.toradian, 40.0 * self.toradian, 45.0 * self.toradian]
self.thetaDelta = 2.0 * self.toradian
self.thetaStart = 30.0 * self.toradian
self.thetaEnd = 70.0 * self.toradian
# end if
# measurement data: static reflectivity vs angle (TE and TM)
# dynamic reflectivity vs angle (TE and TM)
# four files with the two tab-separated columns each
self.filenameTE = filename_RTE
self.filenameTM = filename_RTM
self.filenameTEdyn = filename_RTEdyn
self.filenameTMdyn = filename_RTMdyn
self.filter = False
# end reset
# ---------------------------------------------------------------------------------------
def check(self):
if not (self.structure in self.knownstructures):
self.error = True
self.message = "------> unknown structure '%s'" % str(self.structure)
self.disp("\n" + self.message)
return False
# end if
if (self.layers_count > self.layers_maxcount):
self.error = True
self.message = "------> the number of layers exceeds the limit (%d)" % self.layers_maxcount
self.disp("\n" + self.message)
return False
# end if
if (self.layers_count < 2) \
or (len(self.thickness) != len(self.refractiveindexo)) \
or (len(self.thickness) != len(self.refractiveindexe)) \
or (len(self.thickness) != (self.layers_count + 2)) \
or (len(self.bounds[0]) != len(self.bounds[1])) \
or (len(self.bounds[0]) != (self.layers_count * 3)) \
or (len(self.coherency) != (self.layers_count + 2)):
self.error = True
self.message = "------> layers parameters not consistent"
self.disp("\n" + self.message)
return False
# end if
# for the default structure, get the embedded data if necessary
if (self.structure == 'SBN') and (not os.path.isfile(self.filenameTE) or not os.path.isfile(self.filenameTEdyn) or not os.path.isfile(self.filenameTM) or not os.path.isfile(self.filenameTMdyn)):
try:
eocdir = os.path.dirname(os.path.realpath(__file__))
# measurement data: static reflectivity vs angle (TE and TM)
# dynamic reflectivity vs angle (TE and TM)
# four files with the two tab-separated columns each
self.filenameTE = os.path.join(eocdir, 'SBN_Reflectivity_TE.txt')
self.filenameTM = os.path.join(eocdir, 'SBN_Reflectivity_TM.txt')
self.filenameTEdyn = os.path.join(eocdir, 'SBN_Reflectivity_Dyn_TE.txt')
self.filenameTMdyn = os.path.join(eocdir, 'SBN_Reflectivity_Dyn_TM.txt')
except:
self.filenameTE = ''
self.filenameTM = ''
self.filenameTEdyn = ''
self.filenameTMdyn = ''
pass
# end try
# end if
for fname in [self.filenameTE, self.filenameTM, self.filenameTEdyn, self.filenameTMdyn]:
if not os.path.isfile(fname):
self.error = True
self.message = "------> data file '%s' not found" % ('?' if (fname == '') else fname)
self.disp("\n" + self.message)
return False
# end if
# end for
self.min_indexn = 0.001
self.max_indexn = 10.0
self.min_indexk = 0.0
self.max_indexk = 10.0
self.min_thickness = 1.0
self.max_thickness = 1000000.0
for ii in range(0, self.layers_count - 1):
# check layer parameters consistency
ll = 3 * ii
if (self.thickness[ii+1] < self.min_thickness) or (self.thickness[ii+1] > self.max_thickness) \
or (self.bounds[0][ll] < self.min_thickness) or (self.bounds[1][ll] > self.max_thickness) \
or (self.thickness[ii+1] < self.bounds[0][ll]) or (self.thickness[ii+1] > self.bounds[1][ll]):
self.error = True
self.message = "------> layer %d invalid thickness value / bounds: %g / [%g, %g]" % (ii + 1, self.thickness[ii+1], self.bounds[0][ll], self.bounds[1][ll])
self.disp("\n" + self.message)
return False
# end if
if (self.bounds[0][ll+1] < self.min_indexn) or (self.bounds[1][ll+1] > self.max_indexn) \
or (self.bounds[0][ll+2] < self.min_indexk) or (self.bounds[1][ll+2] > self.max_indexk) \
or (self.refractiveindexo[ii+1].real < self.min_indexn) or (self.refractiveindexo[ii+1].real > self.max_indexn) \
or (self.refractiveindexo[ii+1].real < self.bounds[0][ll+1]) or (self.refractiveindexo[ii+1].real > self.bounds[1][ll+1]) \
or (self.refractiveindexo[ii+1].imag < self.min_indexk) or (self.refractiveindexo[ii+1].imag > self.max_indexk) \
or (self.refractiveindexo[ii+1].imag < self.bounds[0][ll+2]) or (self.refractiveindexo[ii+1].imag > self.bounds[1][ll+2]) \
or (self.refractiveindexe[ii+1].real < self.min_indexn) or (self.refractiveindexe[ii+1].real > self.max_indexn) \
or (self.refractiveindexe[ii+1].real < self.bounds[0][ll+1]) or (self.refractiveindexe[ii+1].real > self.bounds[1][ll+1]) \
or (self.refractiveindexe[ii+1].imag < self.min_indexk) or (self.refractiveindexe[ii+1].imag > self.max_indexk) \
or (self.refractiveindexe[ii+1].imag < self.bounds[0][ll+2]) or (self.refractiveindexe[ii+1].imag > self.bounds[1][ll+2]):
self.error = True
self.message = "------> layer %d invalid index value / bounds" % (ii + 1)
self.disp("\n" + self.message)
return False
# end if
# end for
self.update()
self.checked = True
return self.checked
# end check
# ---------------------------------------------------------------------------------------
def update(self):
for ii in range(0, self.layers_count - 1):
# layers with same name should have the same refractive index
for rr in range(ii+1, self.layers_count):
if (self.layers[ii] == self.layers[rr]):
self.refractiveindexo[1+rr] = self.refractiveindexo[1+ii]
self.refractiveindexe[1+rr] = self.refractiveindexe[1+ii]
# end if
# end for
# end for
# update layers parameters (d, n, k)
self.totald = 0.0
self.totalda = 0.0
for ii in range(0, self.layers_count):
self.d[ii] = self.thickness[1+ii]
self.no[ii] = self.refractiveindexo[1+ii].real
self.ko[ii] = self.refractiveindexo[1+ii].imag
self.ne[ii] = self.refractiveindexe[1+ii].real
self.ke[ii] = self.refractiveindexe[1+ii].imag
self.totald += self.d[ii]
if (ii <= self.active_layer):
self.totalda += self.d[ii]
# end if
# end for
# set the initial parameters for the fitting algorithm
self.fit_init[0] = []
self.fit_init[1] = []
for rr in range(0, len(self.d)):
if self.d[rr] == None:
break
# end if
self.fit_init[0].append(self.d[rr])
self.fit_init[0].append(self.no[rr])
self.fit_init[0].append(self.ko[rr])
self.fit_init[1].append(self.d[rr])
self.fit_init[1].append(self.ne[rr])
self.fit_init[1].append(self.ke[rr])
# end for
# end update
# ---------------------------------------------------------------------------------------
def disp(self, strT):
if (self.verbose or self.error) and (not self.inJac):
print(strT)
# end if
# end disp
# ---------------------------------------------------------------------------------------
# utility function : update the parameters (d, n, k)
def updateParametersArray(self, dnk_array):
ipol = 0 if (self.polarization == 's') else 1
# some basic check
argc = len(dnk_array)
if argc > self.parameters_maxcount:
self.error = True
self.message = "------> invalid number of arguments given to 'updateParameters'"
self.disp("\n" + self.message)
return
# end if
# construct the parameters list
params_tmp = []
ll = 0
for rr in range(0, len(self.fit_init[ipol])):
if self.fit_includeparam[rr]:
params_tmp.append(dnk_array[ll])
ll += 1
else:
params_tmp.append(self.fit_init[ipol][rr])
# end if
# end for
# construct the thickness and refractive index list
ll = 0
for rr in range(0, len(params_tmp), 3):
self.thickness[1+ll] = params_tmp[rr]
if ipol == 0:
self.refractiveindexo[1+ll] = params_tmp[rr+1] + 1j * params_tmp[rr+2]
else:
self.refractiveindexe[1+ll] = params_tmp[rr+1] + 1j * params_tmp[rr+2]
# end if
ll += 1
# end for
# layers with same name should have the same refractive index...
# ... example : top and bottom Pt contact
for ii in range(0, self.layers_count - 1):
for rr in range(ii+1, self.layers_count):
if (self.layers[ii] == self.layers[rr]):
if ipol == 0:
self.refractiveindexo[1+rr] = self.refractiveindexo[1+ii]
else:
self.refractiveindexe[1+rr] = self.refractiveindexe[1+ii]
# end if
# end if
# end for
# end for
# end updateParametersArray
# ---------------------------------------------------------------------------------------
# utility function : update the parameters (d, n, k)
def updateParameters(self, *dnk_args):
self.updateParametersArray(dnk_args)
# end updateParameters
# ---------------------------------------------------------------------------------------
# calculate the reflectivity vs angle, using the S J byrnes' code
def fitfunc_static_array(self, theta, dnk_array):
if self.fitting:
self.evalcount += 1
# end if
if os.path.isfile(self.stopfilename):
try:
os.unlink(self.stopfilename)
except:
pass
# end try
print("\n------> fitting stopped by the user after %d iterations" % (self.evalcount))
exit(1)
# end if
isArray = isinstance(theta, (list, tuple, np.ndarray))
self.updateParametersArray(dnk_array)
n_list = np.array(self.refractiveindexo if (self.polarization == 's') else self.refractiveindexe, dtype=complex)
d_list = np.array(self.thickness, dtype=float)
if not isArray:
return tmm.inc_tmm(self.polarization, n_list, d_list, self.coherency, theta, self.wavelength)['R'] # end if
reflectivity = []
for th in theta:
reflectivity.append(tmm.inc_tmm(self.polarization, n_list, d_list, self.coherency, th, self.wavelength)['R'])
# end for
return reflectivity
# end self.fitfunc_static_array
# ---------------------------------------------------------------------------------------
# calculate the reflectivity vs angle, using the S J byrnes' code
def fitfunc_static(self, theta, *dnk_args):
return self.fitfunc_static_array(theta, dnk_args)
# end fitfunc_static
# ---------------------------------------------------------------------------------------
def fitfunc_static_jac(self, theta, *dnk_args):
self.inJac = True
R0 = np.array(self.fitfunc_static_array(theta, dnk_args))
dnk = []
parcount = len(dnk_args)
for ii in range(0, parcount):
dnk.append(dnk_args[ii])
# end for
jac = np.array([[]])
for ii in range(0, parcount):
dnk0 = dnk[ii]
dpar = (self.fit_derivtol * math.fabs(dnk0))
if dpar < self.fit_derivtol_min:
dpar = self.fit_derivtol_min
# end if
dnk[ii] = dnk0 + dpar
RP = np.array(self.fitfunc_static_array(theta, dnk))
if ii == 0:
jac = (RP - R0) / dpar
else:
jac = np.vstack((jac, (RP - R0) / dpar))
# end if
dnk[ii] = dnk0
# end for
self.inJac = False
return jac.transpose()
# end fitfunc_static_jac
# ---------------------------------------------------------------------------------------
# calculate the intensity vs position in the structure, using the S J byrnes' code
def poynting(self, theta, position, *dnk_args):
self.updateParameters(*dnk_args)
n_list = np.array(self.refractiveindexo if (self.polarization == 's') else self.refractiveindexe, dtype=complex)
d_list = np.array(self.thickness, dtype=float)
tmmdata = tmm.coh_tmm(self.polarization, n_list, d_list, theta, self.wavelength)
poyn = []
for pos in position:
layer, pos_in_layer = tmm.find_in_structure_with_inf(d_list, pos)
data = tmm.position_resolved(layer, pos_in_layer, tmmdata)
poyn.append(data['poyn'])
# end for
poyn = np.array(poyn)
return poyn
# end poynting
# ---------------------------------------------------------------------------------------
# calculate the reflectivity vs angle...
# ... for the two polarizations 's' and 'p'
def calculate(self, report = False, plot = False):
if not self.checked:
self.check()
# end if
# load the experimental data and set the angle range
self.load()
if self.error:
return
# end if
# setlayers (d,n,k)
self.fit_param[0] = []
self.fit_param[1] = []
for rr in range(0, len(self.fit_init[0])):
self.fit_param[0].append(self.fit_init[0][rr])
self.fit_param[1].append(self.fit_init[1][rr])
# end for
self.calc_reflectivity(ipol = 0)
self.calc_reflectivity(ipol = 1)
if self.error:
return
# end if
# calculate the derivatives delta_R/delta_d, delta_R/delta_n, delta_R/delta_k
self.calc_deriv(ipol = 0)
self.calc_deriv(ipol = 1)
# calculate DR
self.calc_dynamic(ipol = 0)
self.calc_dynamic(ipol = 1)
self.disp("\n------> parameters (d en nm) :")
ll = 0
for rr in range(0, self.parameters_count, 3):
self.disp("\n------> %7s (Layer %d): d, (n, k) : %g, (%g + j%g, %g + j%g)" % (self.layers[ll], ll+1, self.fit_init[0][rr], self.fit_init[0][rr+1], self.fit_init[0][rr+2], self.fit_init[1][rr+1], self.fit_init[1][rr+2]))
ll += 1
# end if
if self.r13 and self.d33:
self.disp("\n------> r13 = %g pm/V d33 = %g pm/V Dk/DV = %g 1/V" % (self.r13, self.d33, self.DkDV))
# end if
if report:
self.report()
# end if
if plot:
self.plot()
# end if
# end calculate
# ---------------------------------------------------------------------------------------
# plot the intensity profile in the structure
def plotpoynting(self):
if not self.checked:
self.check()
# end if
# plot intensity vs position
figT = pl.figure(num=1, figsize=(12,6), facecolor='#FFFFFF', linewidth=1.0, frameon=True)
figT.canvas.set_window_title(self.structure + ': Poynting intensity profile in the structure')
tManager = pl.get_current_fig_manager()
img = Tk.PhotoImage(file = os.path.dirname(os.path.abspath(__file__)) + '/iconmain.gif')
tManager.window.tk.call('wm', 'iconphoto', tManager.window._w, img)
ylim = [None, None]
for polarization in ['s', 'p']:
# calculate intensity
self.polarization = polarization
position = np.linspace(-100, self.totalda + 100, num=1001)
nx = self.no if (self.polarization == 's') else self.ne
kx = self.ko if (self.polarization == 's') else self.ke
poyn = self.poynting(0.0, position,
self.d[0], nx[0], kx[0],
self.d[1], nx[1], kx[1],
self.d[2], nx[2], kx[2],
self.d[3], nx[3], kx[3],
self.d[4], nx[4], kx[4],
self.d[5], nx[5], kx[5],
self.d[6], nx[6], kx[6])
psp = [221, 222] if polarization == 's' else [223, 224]
for pp in range(0,2):
splot = pl.subplot(psp[pp])
TX = 'TE' if polarization == 's' else 'TM'
if pp == 1:
iT = np.where(position >= self.thickness[1])
positionT = np.take(position, iT, axis=0).reshape(-1)
poynT = np.take(poyn, iT, axis=0).reshape(-1)
else:
positionT = position
poynT = poyn
# end if
pl.plot(positionT, poynT, 'b-' if polarization == 'p' else 'r-', linewidth=2.5, zorder=3)
pl.tick_params(axis='x', which='major', labelsize=self.fontsize+2)
pl.tick_params(axis='y', which='major', labelsize=self.fontsize+2)
if pp == 1:
pl.ylim(0., 10. ** math.ceil(math.log10(np.max(poynT))))
# end if
tgca = pl.gca()
if polarization == 's':
tgca.set_xticklabels([])
# end if
if polarization == 'p':
pl.xlabel('Position in the structure (nm)', fontsize=self.fontsize+2)
if pp == 0:
pl.ylabel('%s intensity profile' % TX, fontsize=self.fontsize+2)
# end if
Nlayers = len(self.thickness) - 3 # exclude top and bottom air, and the thick substrate
if (Nlayers >= 2):
xlp = 0.
xl = self.thickness[1]
layercolors = ['#F7CD72', '#B1E8F0', '#F2BB44']
for ii in range(2, Nlayers + 2):
pl.axvline(xl, 0., 1., color='#BA9634', linestyle=':', zorder=2)
pl.axvspan(xlp, xl, alpha=0.5, color=layercolors[ii-2] if (ii - 2) < 3 else layercolors[2])
xlp = xl
xl += self.thickness[ii]
# end for
# end if
pl.xlim(0., self.totalda + self.thickness[-3])
# end if
if polarization == 's':
ylim[pp] = pl.ylim()
else:
pl.ylim(ylim[pp])
# end if
splot.set_ylim(ymin=0.)
pl.grid()
# end for
# end for
pl.subplots_adjust(left=0.1, bottom=0.1, right=0.96, top=0.94, wspace=0.4, hspace=0.4)
figT.tight_layout(h_pad=2, w_pad=4)
pl.show()
# end plotpoynting
# ---------------------------------------------------------------------------------------
# load the experimental data, filter and interpolate
def load(self):
if not self.checked:
self.check()
# end
if self.verbose:
self.message = "------> loading the experimental data..."
self.disp("\n" + self.message)
# end if
try:
# load the experimental data (reflectivity vs angle (in degree))
dataTE = np.loadtxt(self.filenameTE, delimiter=self.separator, skiprows=self.linestoskip, usecols=(0,1))
dataTEdyn = np.loadtxt(self.filenameTEdyn, delimiter=self.separator, skiprows=self.linestoskip, usecols=(0,1))
dataTM = np.loadtxt(self.filenameTM, delimiter=self.separator, skiprows=self.linestoskip, usecols=(0,1))
dataTMdyn = np.loadtxt(self.filenameTMdyn, delimiter=self.separator, skiprows=self.linestoskip, usecols=(0,1))
except Exception as excT:
excType, excObj, excTb = sys.exc_info()
excFile = os.path.split(excTb.tb_frame.f_code.co_filename)[1]
self.message = "------> error: the experimental data cannot be loaded:\n %s\n in %s (line %d)\n" % (str(excT), excFile, excTb.tb_lineno)
self.error = True
self.disp("\n" + self.message)
return
# end try
self.dataTEX = dataTE[:,0]*self.toradian
self.dataTEY = dataTE[:,1]
self.dataTEXdyn = dataTEdyn[:,0]*self.toradian
self.dataTEYdyn = dataTEdyn[:,1]
dataTEXpoints = len(self.dataTEX)
dataTEYpoints = len(self.dataTEY)
dataTEXdynpoints = len(self.dataTEX)
dataTEYdynpoints = len(self.dataTEY)
if ((dataTEXpoints < 20) or (dataTEYpoints < 20) or (dataTEXpoints > 10000) or (dataTEYpoints > 10000)
or (dataTEXdynpoints < 20) or (dataTEYdynpoints < 20) or (dataTEXdynpoints > 10000) or (dataTEYdynpoints > 10000)
or (dataTEXpoints != dataTEYpoints) or (dataTEXdynpoints != dataTEYdynpoints)):
self.error = True
self.message = "------> error: data size not consistent : [%d %d %d %d]" % (dataTEXpoints, dataTEYpoints, dataTEXdynpoints, dataTEYdynpoints)
self.disp("\n" + self.message)
return
# end if
self.dataTMX = dataTM[:,0]*self.toradian
self.dataTMY = dataTM[:,1]
self.dataTMXdyn = dataTMdyn[:,0]*self.toradian
self.dataTMYdyn = dataTMdyn[:,1]
arThetaStart = [self.dataTEX[0], self.dataTEXdyn[0], self.dataTMX[0], self.dataTMXdyn[0]]
arThetaEnd = [self.dataTEX[len(self.dataTEX) - 1], self.dataTEXdyn[len(self.dataTEXdyn) - 1], self.dataTMX[len(self.dataTMX) - 1], self.dataTMXdyn[len(self.dataTMXdyn) - 1]]
for ii in range(0, len(arThetaStart)):
if (self.thetaStart < arThetaStart[ii]):
self.thetaStart = arThetaStart[ii]
# end if
if (self.thetaEnd > arThetaEnd[ii]):
self.thetaEnd = arThetaEnd[ii]
# end if
# end for
if self.thetaStart >= (self.thetaEnd - 3.0*self.thetaDelta):
self.error = True
self.message = "------> error: angle values not consistent : [%g %g]" % (self.thetaStart*self.todegree, self.thetaEnd*self.todegree)
self.disp("\n" + self.message)
return
# end if
if (self.theta_manual) and (isinstance(self.theta_manual, (list, tuple, np.ndarray))) and (len(self.theta_manual) == 3):
self.theta_val[0] = []
self.theta_val[1] = []
for rr in range(0, len(self.theta_manual)):
self.theta_val[0].append(self.theta_manual[rr])
self.theta_val[1].append(self.theta_manual[rr])
# end for
# end if
if self.filter:
# filter the experimental data using the Savitzky-Golay algorithm
# https://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.savgol_filter.html
# the window length (savgol_window) and other parameters should be chosen carefully to preserve the experimental data
savgol_window = 5
if (savgol_window % 2) == 0:
savgol_window += 1
# end if
if savgol_window > 51:
savgol_window = 51
savgol_order = 2
self.dataTEY = savgol_filter(self.dataTEY, savgol_window, savgol_order)
self.dataTEYdyn = savgol_filter(self.dataTEYdyn, savgol_window, savgol_order)
self.dataTMY = savgol_filter(self.dataTMY, savgol_window, savgol_order)
self.dataTMYdyn = savgol_filter(self.dataTMYdyn, savgol_window, savgol_order)
# end if self.filter
self.dataTX = [self.dataTEX, self.dataTMX]
self.dataTY = [self.dataTEY, self.dataTMY]
self.rchisq = np.zeros(2)
# the measurement error for the static reflectivity (0.5% of the measured value),
# to adapt considering the uncertainty on the measured data
self.measurerr = np.array([0.005, 0.005])
self.dataTXdyn = [self.dataTEXdyn, self.dataTMXdyn]
self.dataTYdyn = [self.dataTEYdyn, self.dataTMYdyn]
self.rchisqDyn = np.zeros(2)
# the measurement error for the dynamic reflectivity (10% of the measured value),
# to adapt considering the uncertainty on the measured data
self.measurerrDyn = np.array([0.10, 0.10])
if self.verbose:
self.message = "------> data loaded: [%d %d %d %d] points" % (dataTEXpoints, dataTEYpoints, dataTEXdynpoints, dataTEYdynpoints)
self.disp("\n" + self.message)
# end if
self.loaded = True
# end load
def reducedChiSquared(self, dataY, dataYmodel, dataYerr, Nparams):
try:
rchis = np.sum(((dataY - dataYmodel) / dataYerr) ** 2.) / (len(dataY) - Nparams)
return rchis
except:
return 0.
# end try
# end reducedChiSquared
# ---------------------------------------------------------------------------------------
# fit the experimental static reflectivity to get (d,n,k) for each layer
def fit_static(self, ipol):
if not self.checked:
self.check()
# end if
if not self.loaded:
self.load()
# end if
if self.error:
return
# end if
# perform the fitting for static reflectivity for s and p polarizations
self.polarization = self.polarizationall[ipol]
init_tmp = []
bounds_tmp = ([], [])
for rr in range(0, len(self.fit_init[ipol])):
if self.fit_includeparam[rr]:
init_tmp.append(self.fit_init[ipol][rr])
bounds_tmp[0].append(self.bounds[0][rr])
bounds_tmp[1].append(self.bounds[1][rr])
# end if
# end for
self.fitting = True
self.evalcount = 0
starttime = self.getclock()
self.disp("\n------> static fitting ('%s' polarization) in progress..." % (self.polarization))
try:
self.fit_param[ipol] = np.array(self.fit_init[ipol])
result_tmp,covar = curve_fit(self.fitfunc_static, self.dataTX[ipol], self.dataTY[ipol], p0=init_tmp, bounds=bounds_tmp, check_finite=True, method='trf', xtol=self.fit_tol, ftol=self.fit_tol, gtol=self.fit_tol, jac=self.fitfunc_static_jac)
self.rchisq[ipol] = self.reducedChiSquared(self.dataTY[ipol], self.fitfunc_static(self.dataTX[ipol], *result_tmp), self.measurerr[ipol] * self.dataTY[ipol], len(result_tmp))
self.fit_param[ipol] = []
ll = 0
for rr in range(0, len(self.fit_init[ipol])):
if self.fit_includeparam[rr]:
self.fit_param[ipol].append(result_tmp[ll])
ll += 1
else:
self.fit_param[ipol].append(self.fit_init[ipol][rr])
# end if
# end for rr
# layers with same name should have the same refractive index
ll = 0
mm = 0
for nn in range(0, self.layers_count - 1):
mm = ll + 3
for rr in range(nn+1, self.layers_count):
if (self.layers[nn] == self.layers[rr]):
self.fit_param[ipol][mm+1] = self.fit_param[ipol][ll+1]
self.fit_param[ipol][mm+2] = self.fit_param[ipol][ll+2]
# end if
mm += 3
# end for rr
ll += 3
# end for nn
self.fit_param[ipol] = np.array(self.fit_param[ipol])
self.fitted = True
self.fitting = False
except ValueError:
self.error = True
self.message = "------> static fitting ('%s' polarization) ended (duration : %g seconds)\ndata not valid" % (self.polarization, self.getclock() - starttime)
self.disp("\n" + self.message)
self.fit_param[ipol] = []
for rr in range(0, len(self.fit_init[ipol])):
self.fit_param[ipol].append(self.fit_init[ipol][rr])
# end for rr
self.fitting = False
except RuntimeError:
self.error = True
self.message = "------> static fitting ('%s' polarization) ended (duration : %g seconds)\nconvergence not achieved" % (self.polarization, self.getclock() - starttime)
self.disp("\n" + self.message)
self.fit_param[ipol] = []
for rr in range(0, len(self.fit_init[ipol])):
self.fit_param[ipol].append(self.fit_init[ipol][rr])
# end for rr
self.fitting = False
else:
self.disp("\n------> static fitting ('%s' polarization) ended (duration : %g seconds)\nnumber of evaluations: %d" % (self.polarization, self.getclock() - starttime, self.evalcount))