-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathShockleyQueisserCore.py
1275 lines (1096 loc) · 54.6 KB
/
ShockleyQueisserCore.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# ======================================================================================================
# Solar Cell Shockley-Queisser Limit Calculator
# Code written by:
# Pr. Sidi Hamady
# Université de Lorraine, France
# sidi.hamady@univ-lorraine.fr
# See Copyright Notice in COPYRIGHT
# HowTo in README.md and README.pdf
# https://github.com/sidihamady/Shockley-Queisser
# http://www.hamady.org/photovoltaics/ShockleyQueisser.zip
# ======================================================================================================
# ShockleyQueisserCore.py
# the class ShockleyQueisserCore implements the program core functionality
# only the constructor and the calculate function are to be called from outside the class
# example (to put in a test.py file, for instance):
#
# #!/usr/bin/env python
# #-*- coding: utf-8 -*-
#
# from ShockleyQueisserCore import *
#
# SCC = ShockleyQueisserCore(verbose = False)
#
# SCC.calculate(
# TargetBandgap = 1.1,
# TargetBandgapTop = 0.0,
# Temperature = 300.0,
# SolarConcentration = 1.0,
# OutputFilename = './ShockleyQueisserOutput'
# )
#
# import as usual
import math
import numpy as np
import scipy as sp
import sys, os, time
import threading
TkFound = False
TkRet = ''
# try to load the tkinter and matplotlib modules
# should be always installed in any Linux distribution
# (for Windows, just use some ready-to-use packages such as anaconda (https://www.anaconda.com/distribution/))
try:
import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot as pl
from matplotlib.backends.backend_pdf import PdfPages
import matplotlib.backends.backend_tkagg
from matplotlib.font_manager import FontProperties
if sys.version_info[0] < 3:
# Python 2.7.x
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.backends.backend_tkagg import NavigationToolbar2Tk as NavigationToolbar2TkAgg
import Tkinter as Tk
import ttk
import tkFileDialog
import tkFont
import tkMessageBox
else:
# Python 3.x
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.backends.backend_tkagg import NavigationToolbar2Tk as NavigationToolbar2TkAgg
import tkinter as Tk
import tkinter.ttk as ttk
import tkinter.filedialog as tkFileDialog
import tkinter.font as tkFont
import tkinter.messagebox as tkMessageBox
# end if
class NavigationToolbar(NavigationToolbar2TkAgg):
""" custom Tk toolbar """
def __init__(self, chart):
NavigationToolbar2TkAgg.__init__(self, chart.canvas, chart.root)
self.chart = chart
# end __init__
try:
toolitems = [tt for tt in NavigationToolbar2TkAgg.toolitems if tt[0] in ('Home', 'Zoom')]
toolitems.append(('AutoScale', 'Auto scale the plot', 'hand', 'onAutoScale'))
toolitems.append(('Save', 'Save the plot', 'filesave', 'onSave'))
except:
pass
# end try
def onAutoScale(self):
self.chart.onAutoScale()
# end onAutoScale
def onSave(self):
self.chart.onSave()
# end onSave
# end NavigationToolbar
TkFound = True
except ImportError as ierr:
# if Tkinter is not found, just install or update python/numpy/scipy/matplotlib/tk modules
TkRet = "\n! cannot load Tkinter:\n " + ("{0}".format(ierr)) + "\n"
pass
except Exception as excT:
TkRet = "\n! cannot load Tkinter:\n %s\n" % str(excT)
pass
# end try
# suppress a nonrelevant warning from matplotlib
import warnings
warnings.simplefilter(action='ignore', category=FutureWarning)
# calculations done in a secondary thread, not on UI
class CalculationThread(threading.Thread):
def __init__(self, id, func):
threading.Thread.__init__(self)
self.id = id
self.func = func
# end __init__
def run(self):
self.func()
# end run
# end CalculationThread
# the core class
class ShockleyQueisserCore(object):
""" the Shockley-Queisser calculator core class """
def __init__(self, verbose = True, useTkinterGUI = True):
""" the Shockley-Queisser calculator class constructor """
self.name = "Solar Cell Shockley-Queisser Limit Calculator"
self.__version__ = "Version 1.0 Build 2205"
# Basic constants
self.pi = 3.14159265358979 #
self.q = 1.602176e-19 # elementary charge
self.h = 6.626068e-34 # Planck constant
self.c = 2.99792458e+8 # light speed in vacuum
self.hc = 1.986445213e-25 # h c
self.nmeV = 1239.84207 # nm to eV
# AM1.5 solar spectrum file to put here (usually ASTM AM1.5 G-173)
# Solar spectrum file name (SolarSpectrum_AM15G.txt included):
# two columns (wavelength in nm and irrandiance in W/m2/nm):
# 280.0 4.7309E-23
# 280.5 1.2307E-21
# 281.0 5.6895E-21
# 281.5 1.5662E-19
# ...
# 4000.0 7.1043E-03
# the first two rows are skipped
self.SolarSpectrumAMX = './SolarSpectrum_AM15G.txt'
self.WavelengthMin = 300.0 # nm
self.WavelengthMax = 4000.0 # nm
self.BandgapMin = self.nmeV / self.WavelengthMax
self.BandgapMax = self.nmeV / self.WavelengthMin
# the bandgap range in eV
self.BandgapRange = None
# the used data delimiter (usually TAB) in the AM1.5 ASCII file
self.DataDelimiter = '\t'
self.Bandgap = None
self.Efficiency = None
# Shockley-Queisser calculated parameters
self.SQ_Efficiency = None
self.SQ_JSC = None
self.SQ_VOC = None
self.SQ_FF = None
self.SQ_Vm = None
self.SQ_Jm = None
self.SQ_Bandgap = None
self.SQ_Voltage = None
self.SQ_Current = None
self.SQ_Done = False
#
# Target calculated parameters
self.Target_Efficiency = None
self.Target_VOC = None
self.Target_JSC = None
self.Target_FF = None
self.Target_Vm = None
self.Target_Jm = None
self.Target_Voltage = None
self.Target_Current = None
#
self.running = False
self.threadfinish = None
self.thread = None
self.actionbutton = None
self.timerduration = 200 # in milliseconds
self.report = None
self.root = None
self.GUIstarted = False
self.PlotInitialized = False
self.SpectrumLoaded = False
self.tic = 0.0
self.Counter = 0
self.CounterMax = 0
# one can set verbose to False to disable printing output
self.verbose = verbose
if not self.verbose:
print("\nverbose set to False: printing output disabled")
# end if
# useTkinterGUI: the calculator can be used in graphical (GUI) mode or command-line only mode.
# in command-line mode (useTkinterGUI = False) the results are printed out and saved in text files.
# the command-line mode is useful to perform specific calculations such as multijunction solar cell efficiency
if useTkinterGUI and (not TkFound):
# if Tkinter is not found, just install or update python/numpy/scipy/matplotlib/tk modules
print(TkRet)
# end if
self.useTkinterGUI = useTkinterGUI if TkFound else False
return
# end __init__
def calculate(self,
TargetBandgap = 1.1,
TargetBandgapTop = 0.0,
Temperature = 300.0,
SolarConcentration = 1.0,
OutputFilename = './ShockleyQueisserOutput'):
""" the Shockley-Queisser calculator main function """
# recalculate the Shockley-Queisser curve if changing temperature or solar concentration
if self.SQ_Done and ((Temperature != self.Temperature) or (SolarConcentration != self.SolarConcentration)):
self.SQ_Done = False
# end if
# Temperature: in Kelvin (from 100 K to 700 K)
self.Temperature = Temperature if ((Temperature >= 100.0) and (TargetBandgap <= 700.0)) else 300.0
self.kTeV = 0.02585202874091 * self.Temperature / 300.0 # in eV
self.kTJ = self.kTeV * self.q # in J
# TargetBandgap: Target bandgap in eV (from 0.2 eV to 6 eV): to compare with the Shockley-Queisser Limit
self.Target_Bandgap = TargetBandgap if ((TargetBandgap >= 0.2) and (TargetBandgap <= 6.0)) else 1.1
# Target top bandgap: used to take into account the part of solar spectrum already absorbed (for example in a top cell).
# useful to calculate the overall efficiency in a multijunction solar cell.
# For example for double junction solar cell, follow the steps below:
# 1. set TargetBandgap to 1.65 eV and the TargetBandgapTop to 0, and calculate the corresponding efficiency and current-voltage characteristic.
# 2. set TargetBandgap to 0.95 eV and the TargetBandgapTop to 1.65, and calculate the corresponding efficiency and current-voltage characteristic.
# deduce from the previous data the overall double junction solar cell efficiency.
# examples are given in ShockleyQueisserTJ.py and ShockleyQueisserDJ.py.
self.Target_Bandgap_Top = TargetBandgapTop if ((TargetBandgapTop >= 0.2) and (TargetBandgapTop <= 6.0) and (TargetBandgapTop > TargetBandgap)) else 0.0
# Solar concentration (1 sun to 1000 suns)
self.SolarConcentration = SolarConcentration if ((SolarConcentration >= 1.0) and (SolarConcentration <= 1000.0)) else 1.0
# OutputFilename: Output file name without extension (used to save figure in PDF format if in GUI mode, and the text output data).
# set to None to disable.
self.OutputFilename = OutputFilename
if self.OutputFilename and (not self.OutputFilename.endswith('.pdf')):
self.OutputFilename = self.OutputFilename + '.pdf'
# end if
if self.useTkinterGUI:
# GUI mode: calculation done in a working thread
self.startGUI()
else:
# command-line mode
self.start()
# end if
return
# end calculate
def isRunning(self):
if (self.thread is None):
return self.running
# end if
threadalive = self.thread.isAlive() if (sys.version_info[0] < 3) else self.thread.is_alive()
if (not threadalive):
self.thread = None
self.running = False
# end if
return self.running
# end isRunning
def setRunning(self, running = True):
self.running = running
if self.actionbutton is not None:
self.actionbutton["text"] = "Calculate"
if self.running:
self.actionbutton.configure(style='Red.TButton')
else:
self.actionbutton.configure(style='Black.TButton')
self.actionbutton = None
# end if
# end if
# end setRunning
# init the Tkinter GUI
def startGUI(self):
if self.GUIstarted or (not self.useTkinterGUI):
return
# end if
try:
self.plotcount = 3
self.curvecount = 4
self.xLabel = {}
self.yLabel = {}
self.xLabel[0] = '$Wavelength\ (nm)$'
self.xLabel[1] = '$Bandgap\ (eV)$'
self.xLabel[2] = '$Voltage\ (V)$'
self.yLabel[0] = '$Irradiance\ (W/m^2/nm)$'
self.yLabel[1] = '$Efficiency\ (\%)$'
self.yLabel[2] = '$Current\ (mA/cm^2)$'
self.root = Tk.Tk()
self.root.bind_class("Entry","<Control-a>", self.onEntrySelectAll)
self.root.bind_class("Entry","<Control-z>", self.onEntryUndo)
self.root.bind_class("Entry","<Control-y>", self.onEntryRedo)
self.root.withdraw()
self.root.wm_title(self.name)
self.root.option_add('*Dialog.msg.font', 'Helvetica 11')
self.figure = matplotlib.figure.Figure(figsize=(10,8), dpi=100, facecolor='#FFFFFF', linewidth=1.0, frameon=True)
self.figure.subplots_adjust(top = 0.9, bottom = 0.1, left = 0.09, right = 0.95, wspace = 0.25, hspace = 0.25)
self.plot = {}
# figure top-left part: solar spetrum
self.plot[0] = self.figure.add_subplot(221)
self.plot[0].set_xlim(0.0, 4000.0)
self.plot[0].set_ylim(0.0, 1.8)
# figure top-right part: efficiency vs bandgap
self.plot[1] = self.figure.add_subplot(222)
self.plot[1].set_xlim(0.0, 4.5)
self.plot[1].set_ylim(0.0, 35.0)
# figure bottom-left part: current-voltage characteristic
self.plot[2] = self.figure.add_subplot(223)
self.plot[2].set_xlim(0.0, 1.2)
self.plot[2].set_ylim(-50.0, 10.0)
# figure bottom-right part: report
self.plot[4] = self.figure.add_subplot(224)
self.plot[4].set_xticks([])
self.plot[4].set_yticks([])
self.line0a = None
self.line0b = None
self.line0c = None
self.line2a = None
self.line2b = None
spx = 6
spy = 12
spxm = 1
parFrame = Tk.Frame(self.root)
parFrame.pack(fill=Tk.X, side=Tk.TOP, padx=spx, pady=spx)
self.LLabel = Tk.Label(parFrame, text=" ")
self.LLabel.pack(fill=Tk.X, side=Tk.LEFT, expand=True, padx=(spxm, spxm), pady=spy)
self.SolarConcentrationLabel = Tk.Label(parFrame, text="Solar Concentration: ")
self.SolarConcentrationLabel.pack(side=Tk.LEFT, padx=(spxm, spxm), pady=spy)
SolarConcentrationValidate = (parFrame.register(self.onFloatValidate), '%P')
self.SolarConcentrationEdit = Tk.Entry(parFrame, width=7, validate="key", vcmd=SolarConcentrationValidate)
self.SolarConcentrationEdit.pack(side=Tk.LEFT, padx=(spxm, spx), pady=spy)
self.SolarConcentrationEdit.insert(0, ("%.1f" % self.SolarConcentration) if ((self.SolarConcentration is not None) and (self.SolarConcentration >= 1.0) and (self.SolarConcentration <= 1000.0)) else "")
self.SolarConcentrationEdit.prev = None
self.SolarConcentrationEdit.next = None
self.TargetLabel = Tk.Label(parFrame, text="Target bandgap (eV): ")
self.TargetLabel.pack(side=Tk.LEFT, padx=(spx, spxm), pady=spy)
TargetValidate = (parFrame.register(self.onFloatValidate), '%P')
self.TargetEdit = Tk.Entry(parFrame, width=7, validate="key", vcmd=TargetValidate)
self.TargetEdit.pack(side=Tk.LEFT, padx=(spxm, spx), pady=spy)
self.TargetEdit.insert(0, ("%.3f" % self.Target_Bandgap) if (self.Target_Bandgap is not None) else "")
self.TargetEdit.prev = None
self.TargetEdit.next = None
self.TargetTopLabel = Tk.Label(parFrame, text="Top bandgap (> target): ")
self.TargetTopLabel.pack(side=Tk.LEFT, padx=(spx, spxm), pady=spy)
self.TargetTopEdit = Tk.Entry(parFrame, width=7, validate="key", vcmd=TargetValidate)
self.TargetTopEdit.pack(side=Tk.LEFT, padx=(spxm, spx), pady=spy)
self.TargetTopEdit.insert(0, ("%.3f" % self.Target_Bandgap_Top) if ((self.Target_Bandgap_Top is not None) and (self.Target_Bandgap_Top > 0.0) and (self.Target_Bandgap_Top < self.Target_Bandgap)) else "")
self.TargetTopEdit.prev = None
self.TargetTopEdit.next = None
self.TemperatureLabel = Tk.Label(parFrame, text="Temperature (K): ")
self.TemperatureLabel.pack(side=Tk.LEFT, padx=(spx, spxm), pady=spy)
TemperatureValidate = (parFrame.register(self.onFloatValidate), '%P')
self.TemperatureEdit = Tk.Entry(parFrame, width=7, validate="key", vcmd=TemperatureValidate)
self.TemperatureEdit.pack(side=Tk.LEFT, padx=(spxm, spx), pady=spy)
self.TemperatureEdit.insert(0, ("%.1f" % self.Temperature) if (self.Temperature is not None) else "")
self.TemperatureEdit.prev = None
self.TemperatureEdit.next = None
self.btnstyle_red = ttk.Style()
self.btnstyle_red.configure("Red.TButton", foreground="#DE0015")
self.btnstyle_black = ttk.Style()
self.btnstyle_black.configure("Black.TButton", foreground="black")
self.btnCalculate = ttk.Button(parFrame, width=18, text="Calculate", compound=Tk.LEFT, command=self.onStart)
self.btnCalculate.pack(side=Tk.LEFT, padx=spx, pady=spy)
self.btnCalculate.configure(style="Black.TButton")
self.root.bind('<Return>', self.onEnter)
self.RLabel = Tk.Label(parFrame, text=" ")
self.RLabel.pack(fill=Tk.X, side=Tk.LEFT, expand=True, padx=(spxm, spxm), pady=spy)
self.canvas = FigureCanvasTkAgg(self.figure, master=self.root)
self.canvas._tkcanvas.config(highlightthickness=0)
try:
self.toolbar = NavigationToolbar(self)
except:
self.toolbar = None
pass
# end try
self.toolbar.pack(side=Tk.BOTTOM, fill=Tk.X)
self.toolbar.update()
self.canvas._tkcanvas.pack(side=Tk.LEFT, fill=Tk.BOTH, expand=1)
self.canvas.draw()
self.root.protocol('WM_DELETE_WINDOW', self.onClose)
self.linecolor = ['g', 'm', 'b', 'r']
self.linestyle = ['-', '-', '-', '-']
self.linesize = [1.5, 1.5, 1.5, 1.5]
self.line = {}
self.scatter = {}
for idc in range(0, self.curvecount):
self.line[idc] = None
self.scatter[idc] = None
# end for
# center the window
iw = self.root.winfo_screenwidth()
ih = self.root.winfo_screenheight()
isize = (1152, 720)
ix = (iw - isize[0]) / 2
iy = (ih - isize[1]) / 2
self.root.geometry("%dx%d+%d+%d" % (isize + (ix, iy)))
self.root.minsize(800, 600)
self.fontsize = 10
for idp in range(0, self.plotcount):
try:
self.plot[idp].tick_params(axis='x', labelsize=self.fontsize)
self.plot[idp].tick_params(axis='y', labelsize=self.fontsize)
except:
[tx.label.set_fontsize(self.fontsize) for tx in self.plot[idp].xaxis.get_major_ticks()]
[ty.label.set_fontsize(self.fontsize) for ty in self.plot[idp].yaxis.get_major_ticks()]
pass
# end try
self.plot[idp].set_xlabel(self.xLabel[idp], fontsize=self.fontsize)
self.plot[idp].set_ylabel(self.yLabel[idp], fontsize=self.fontsize)
# end for
if (os.name == "nt"):
self.root.iconbitmap(r'iconmain.ico')
else:
iconmain = Tk.PhotoImage(file='iconmain.gif')
self.root.tk.call('wm', 'iconphoto', self.root._w, iconmain)
# end if
self.popmenu = Tk.Menu(self.root, tearoff=0)
self.popmenu.add_command(label="Calculate", command=self.onStart)
self.popmenu.add_separator()
self.popmenu.add_command(label="Auto scale", command=self.onAutoScale)
self.popmenu.add_separator()
self.popmenu.add_command(label="Close", command=self.onClose)
self.popmenu.add_separator()
self.popmenu.add_command(label="About...", command=self.onAbout)
self.root.bind("<Button-3>", self.onPopmenu)
self.root.deiconify()
self.setFocus()
self.GUIstarted = True
self.root.mainloop()
except Exception as excT:
excType, excObj, excTb = sys.exc_info()
excFile = os.path.split(excTb.tb_frame.f_code.co_filename)[1]
strErr = "\n! cannot initialize GUI:\n %s\n in %s (line %d)\n" % (str(excT), excFile, excTb.tb_lineno)
print(strErr)
if self.GUIstarted and self.root:
self.root.quit()
self.root.destroy()
# end if
os._exit(1)
# never reached
pass
# end try
# end startGUI
def isIncSorted(self, arr):
for ii in range(arr.size - 1):
if arr[ii + 1] <= arr[ii] :
return False
# end if
# end for
return True
# end isIncSorted
# load the solar spectrum file
def loadSpectrum(self):
if self.SpectrumLoaded:
return True
# end if
try:
# load the spectral data from the input file and calculate the total power
self.SolarSpectrumData = np.loadtxt(self.SolarSpectrumAMX, delimiter=self.DataDelimiter, skiprows=2, usecols=(0,1))
self.Wavelength = self.SolarSpectrumData[:,0] # nm
self.Irradiance = self.SolarSpectrumData[:,1] # W/m2/nm
# check the data consistency
if ((len (self.Wavelength) < 100) or
(len (self.Irradiance) < 100) or
(len (self.Irradiance) != len(self.Wavelength)) or
(not (self.Wavelength >= 200.0).all()) or
(not (self.Wavelength <= 10000.0).all()) or
(not (self.Irradiance >= 0.0).all()) or
(not (self.Irradiance <= 10.0).all()) or
(not self.isIncSorted(self.Wavelength))):
raise Exception('invalid wavelength/irradiance')
# end if
self.Energy = self.nmeV / self.Wavelength # eV
self.SolarPower = sp.trapz(self.Irradiance, x=self.Wavelength) # for AM1.5 solar spectrum, the total power is close to 1000 W/m2 or 100 mW/cm2
if (self.SolarPower < 1.0) or (self.SolarPower > 10000.0):
raise Exception('invalid total power density')
# end if
self.WavelengthMin = self.Wavelength[0] + 10.0
self.WavelengthMax = self.Wavelength[len(self.Wavelength) - 1] - 10.0
self.BandgapMin = self.nmeV / self.WavelengthMax # in eV
self.BandgapMax = self.nmeV / self.WavelengthMin # in eV
# the bandgap range in eV
self.BandgapRange = np.arange(self.BandgapMin, self.BandgapMax, (self.BandgapMax - self.BandgapMin) / 500.0)
self.CounterMax = len(self.BandgapRange)
self.SpectrumLoaded = True
self.SQ_Done = False
return True
except Exception as excT:
excType, excObj, excTb = sys.exc_info()
excFile = os.path.split(excTb.tb_frame.f_code.co_filename)[1]
strErr = "\n! cannot load the solar spectrum data:\n %s\n in %s (line %d)\n" % (str(excT), excFile, excTb.tb_lineno)
print(strErr)
if self.GUIstarted and self.root:
self.root.quit()
self.root.destroy()
# end if
os._exit(1)
# never reached
pass
# end try
# end loadSpectrum
# Planck distribution
def PlanckDistribution(self, Energy):
aPlanck = np.array([])
for aE in Energy:
tE = aE * self.q # convert E from eV to J
tP = (2.0 * self.pi / ((self.h ** 3.0) * (self.c ** 2.0))) * (tE ** 2) / (math.exp(tE / self.kTJ) - 1.0)
aPlanck = np.append(aPlanck, -self.q * tP)
# end for
return aPlanck
# end PlanckDistribution
# calculate the efficiency (and other photovoltaic parameters) for a given bandgap
# Theory by W. Shockley and H. J. Queisser in Journal of Applied Physics 32 (1961)
def calculateEfficiency(self, Bandgap, BandgapTop = 0.0):
try:
if (Bandgap < self.BandgapMin) or (Bandgap > self.BandgapMax):
raise Exception("invalid bandgap: %.3f" % Bandgap)
# end if
aLambda = self.nmeV / Bandgap
aLambdaLow = 0.0
CutSpectrum = (BandgapTop > Bandgap) and (BandgapTop >= self.BandgapMin) and (BandgapTop <= self.BandgapMax)
if CutSpectrum:
aLambdaLow = self.nmeV / BandgapTop
# end if
aWavelength = np.copy(self.SolarSpectrumData[:,0])
aWavelength = aWavelength[(aWavelength >= aLambdaLow) & (aWavelength <= aLambda)]
aEnergy = self.nmeV / aWavelength
indexT = np.where(np.logical_and(self.SolarSpectrumData[:,0] >= aLambdaLow, self.SolarSpectrumData[:,0] <= aLambda))
aIrradiance = self.SolarConcentration * np.take(self.SolarSpectrumData[:,1], indexT, axis=0)
aFlux = aIrradiance * aWavelength * 1e-9 / self.hc
aJSC = self.q * sp.trapz(aFlux, x=aWavelength) # Short-Circuit Current in A/m2
aPlanck = self.PlanckDistribution(aEnergy)
aJ0 = self.q * sp.trapz(aPlanck, x=aEnergy) # Dark Current in A/m2
aVOC = self.kTeV * math.log((aJSC / aJ0) + 1.0) # Open-Circuit Voltage in V
aVstep = aVOC / 500.0
aVoltage = np.arange(0.0, aVOC + aVstep, aVstep)
aCurrent = np.array([])
aVm = 0.0
aJm = 0.0
aPm = 0.0
for aV in aVoltage:
aJ = -aJSC + (aJ0 * (math.exp(aV / self.kTeV) - 1.0))
aCurrent = np.append(aCurrent, aJ)
if (aJ < 0.0) and (math.fabs(aJ * aV) > aPm):
aPm = math.fabs(aJ * aV)
aVm = aV
aJm = aJ
aFF = aPm / (aJSC * aVOC)
# end if
# end for
aEff = -100.0 * np.min(aCurrent * aVoltage) / (self.SolarPower * self.SolarConcentration)
return (aEff, aVOC, aJSC, aFF, aVm, aJm, aVoltage, aCurrent, None)
except Exception as excT:
excType, excObj, excTb = sys.exc_info()
excFile = os.path.split(excTb.tb_frame.f_code.co_filename)[1]
strErr = "\n! %s\n in %s (line %d)\n" % (str(excT), excFile, excTb.tb_lineno)
return (0.0, 0.0, 0.0, 0.0, 0.0, 0.0, np.array([]), np.array([]), strErr)
# never reached
pass
# end try
# end calculateEfficiency
def monitorCalculation(self):
running = self.isRunning()
try:
if not running:
self.setRunning(running = False)
if self.threadfinish is not None:
self.threadfinish()
self.threadfinish = None
# end if
if self.GUIstarted:
self.btnCalculate["text"] = "Calculate"
# end if
return
# end if
if self.root:
if self.GUIstarted and (self.CounterMax > 0) and (not self.SQ_Done):
tPC = (100 * self.Counter) / self.CounterMax
self.btnCalculate["text"] = "Calculate" if (tPC > 99) else ("Calculate (%.0f %%)" % tPC)
# end if
self.root.after(self.timerduration if ((self.timerduration >= 100) and (self.timerduration <= 1000)) else 200, self.monitorCalculation)
# end if
except Exception as excT:
pass
# end try
# end monitorCalculation
# start calculations of the efficiency vs bandgap curve
def start(self):
if self.isRunning():
return False
# end if
if self.useTkinterGUI and self.GUIstarted:
# GUI mode
# get the input parameters
strT = self.SolarConcentrationEdit.get().strip("\r\n\t")
try:
fT = float(strT)
if (fT >= 1.0) and (fT <= 1000.0):
# recalculate the Shockley-Queisser curve if changing temperature or solar concentration
if self.SQ_Done and (fT != self.SolarConcentration):
self.SQ_Done = False
# end if
self.SolarConcentration = fT
else:
self.SolarConcentrationEdit.delete(0, Tk.END)
self.SolarConcentrationEdit.insert(0, "%.1f" % self.SolarConcentration)
# end if
except ValueError:
pass
# end try
strT = self.TargetEdit.get().strip("\r\n\t")
try:
fT = float(strT)
if (fT >= 0.2) and (fT <= 6.0):
self.Target_Bandgap = fT
else:
self.TargetEdit.delete(0, Tk.END)
self.TargetEdit.insert(0, "%.3f" % self.Target_Bandgap)
# end if
except ValueError:
pass
# end try
strT = self.TargetTopEdit.get().strip("\r\n\t")
try:
fT = float(strT)
if (fT >= 0.2) and (fT <= 6.0) and (fT > self.Target_Bandgap):
self.Target_Bandgap_Top = fT
else:
self.TargetTopEdit.delete(0, Tk.END)
self.TargetTopEdit.insert(0, "%.3f" % self.Target_Bandgap_Top)
# end if
except ValueError:
pass
# end try
strT = self.TemperatureEdit.get().strip("\r\n\t")
try:
fT = float(strT)
if (fT >= 100.0) and (fT <= 700.0):
# recalculate the Shockley-Queisser curve if changing temperature or solar concentration
if self.SQ_Done and (fT != self.Temperature):
self.SQ_Done = False
# end if
self.Temperature = fT # Temperature in K
self.kTeV = 0.02585202874091 * self.Temperature / 300.0 # kT in eV
self.kTJ = self.kTeV * self.q # kT in J
else:
self.TemperatureEdit.delete(0, Tk.END)
self.TemperatureEdit.insert(0, "%.1f" % self.Temperature)
# end if
except ValueError:
pass
# end try
# start calculations
self.actionbutton = self.btnCalculate
self.setRunning(running = True)
self.thread = CalculationThread(id=1, func=self.run)
self.threadfinish = self.updatePlot
self.thread.start()
self.monitorCalculation()
else:
# Command-line mode (or GUI initialization step)
if ((self.Target_Bandgap < 0.2) or
(self.Target_Bandgap > 6.0) or
(self.Temperature < 100.0) or
(self.Temperature > 700.0) or
(self.SolarConcentration < 1.0) or
(self.SolarConcentration > 1000.0)):
strErr = "\n! cannot perform calculations:\n invalid input parameters\n"
if self.useTkinterGUI and (self.report is not None):
self.report.set_color('red')
self.report.set_text(strErr)
self.canvas.draw()
# end if
print(strErr)
else:
if self.useTkinterGUI:
self.actionbutton = self.btnCalculate
self.setRunning(running = True)
self.thread = CalculationThread(id=1, func=self.run)
self.threadfinish = self.updatePlot
self.thread.start()
self.monitorCalculation()
else:
SQ_Done = self.SQ_Done
self.setRunning(running = True)
self.run()
self.setRunning(running = False)
self.doSave(self.OutputFilename)
if self.verbose:
if not SQ_Done:
treport = ("Shockley-Queisser limit for T = %.1f K and SC = %.1f sun%s\nBandgap ; Efficiency ; JSC ; VOC ; Fill Factor" % (self.Temperature, self.SolarConcentration, "" if (self.SolarConcentration <= 1.0) else "s")) + ("\n%.3f eV ; %05.2f %% ; %06.3f mA/cm2 ; %05.3f V ; %05.3f %%" % (self.SQ_Bandgap, self.SQ_Efficiency, 0.1 * self.SQ_JSC, self.SQ_VOC, 100.0 * self.SQ_FF))
print("\n======================================================================\n" + treport + "\n======================================================================\n")
# end if
treport = ("Target for T = %.1f K and SC = %.1f sun%s\nBandgap ; Efficiency ; JSC ; VOC ; Fill Factor" % (self.Temperature, self.SolarConcentration, "" if (self.SolarConcentration <= 1.0) else "s")) + ("\n%.3f eV ; %05.2f %% ; %06.3f mA/cm2 ; %05.3f V ; %05.3f %%" % (self.Target_Bandgap, self.Target_Efficiency, 0.1 * self.Target_JSC, self.Target_VOC, 100.0 * self.Target_FF))
print("\n----------------------------------------------------------------------\n" + treport + "\n----------------------------------------------------------------------\n")
# end if
# end if
# end if
# end if
# end start
# calculate the efficiency vs bandgap curve
def run(self):
try:
if self.verbose:
print("\ncalculating...")
# end if
self.Counter = 0
# load the spectral data
self.loadSpectrum()
if not self.SpectrumLoaded:
if self.verbose:
print("\ndone.")
# end if
return False
# end if
# to determine the calculation duration
ticT = time.time()
# Shockley-Queisser calculated parameters
if not self.SQ_Done:
self.Bandgap = np.array([])
self.Efficiency = np.array([])
self.SQ_Efficiency = 0.0
self.SQ_JSC = 0.0
self.SQ_VOC = 0.0
self.SQ_FF = 0.0
self.SQ_Bandgap = 0.0
self.SQ_Voltage = np.array([])
self.SQ_Current = np.array([])
for aGap in self.BandgapRange:
(aEff, aVOC, aJSC, aFF, aVm, aJm, aVoltage, aCurrent, strRet) = self.calculateEfficiency(aGap, 0.0)
if strRet is not None:
if self.useTkinterGUI and self.GUIstarted and (self.report is not None):
self.report.set_color('red')
self.report.set_text(strRet)
self.canvas.draw()
# end if
if self.verbose:
print(strRet)
print("\ndone.")
# end if
return False
# endif
if aEff > self.SQ_Efficiency:
self.SQ_Efficiency = aEff
self.SQ_JSC = aJSC
self.SQ_VOC = aVOC
self.SQ_FF = aFF
self.SQ_Vm = aVm
self.SQ_Jm = aJm
self.SQ_Bandgap = aGap
self.SQ_Voltage = np.copy(aVoltage) # in V
self.SQ_Current = 0.1 * np.copy(aCurrent) # in mA/cm2
# end if
self.Bandgap = np.append(self.Bandgap, aGap)
self.Efficiency = np.append(self.Efficiency, aEff)
self.tic = float(time.time() - ticT)
self.Counter += 1
# end for
self.SQ_Done = True
# end if
# calculate the efficiency for the target bandgap value
(self.Target_Efficiency, self.Target_VOC, self.Target_JSC, self.Target_FF, self.Target_Vm, self.Target_Jm, self.Target_Voltage, self.Target_Current, strRet) = self.calculateEfficiency(self.Target_Bandgap, self.Target_Bandgap_Top)
if strRet is not None:
if self.verbose:
print (strRet)
print("\ndone.")
# end if
return False
# endif
self.Target_Current = 0.1 * np.copy(self.Target_Current) # convert from A/m2 to mA/cm2
self.datax = {}
self.datax[0] = self.Wavelength
self.datax[1] = self.Bandgap
self.datax[2] = self.SQ_Voltage
self.datax[3] = self.Target_Voltage
self.datay = {}
self.datay[0] = self.Irradiance
self.datay[1] = self.Efficiency
self.datay[2] = self.SQ_Current
self.datay[3] = self.Target_Current
self.tic = float(time.time() - ticT)
if self.verbose:
print("\ndone. elapsed time = %.3f sec." % self.tic)
# end if
return True
except Exception as excT:
excType, excObj, excTb = sys.exc_info()
excFile = os.path.split(excTb.tb_frame.f_code.co_filename)[1]
strErr = "\n! %s\n in %s (line %d)\n" % (str(excT), excFile, excTb.tb_lineno)
if self.useTkinterGUI and (self.report is not None):
self.report.set_color('red')
self.report.set_text(strErr)
self.canvas.draw()
# end if
if self.verbose:
print(strErr)
# end if
return False
# never reached
pass
# end try
# end run
def setFocus(self):
if (not self.useTkinterGUI) or (not self.root):
return
# end if
self.root.attributes('-topmost', 1)
self.root.attributes('-topmost', 0)
self.root.after(10, lambda: self.root.focus_force())
# end setFocus
# plot the efficiency vs bandgap curve
def updatePlot(self):
if (not self.useTkinterGUI):
return
# end if
try:
if not self.PlotInitialized:
idp = 0
for idc in range(0, self.curvecount):
self.line[idc], = self.plot[idp].plot(np.array([]), np.array([]), self.linestyle[idc], linewidth=self.linesize[idc], zorder=4)
self.line[idc].set_color(self.linecolor[idc])
if (idp < (self.plotcount - 1)):
idp += 1
# end if
# end for
self.scatter[0], = self.plot[1].plot(np.array([]), np.array([]), 'b^', zorder=4, label=" ")
self.scatter[0].set_markerfacecolor('b')
self.scatter[0].set_markeredgecolor('b')
self.scatter[0].set_markersize(5)
self.scatter[1], = self.plot[1].plot(np.array([]), np.array([]), 'ro', zorder=4, label=" ")
self.scatter[1].set_markerfacecolor('r')
self.scatter[1].set_markeredgecolor('r')
self.scatter[1].set_markersize(5)
self.scatter[2], = self.plot[2].plot(np.array([]), np.array([]), 'b^', zorder=4, label=" ")
self.scatter[2].set_markerfacecolor('b')
self.scatter[2].set_markeredgecolor('b')
self.scatter[2].set_markersize(5)
self.scatter[3], = self.plot[2].plot(np.array([]), np.array([]), 'ro', zorder=4, label=" ")
self.scatter[3].set_markerfacecolor('r')
self.scatter[3].set_markeredgecolor('r')
self.scatter[3].set_markersize(5)
self.plot[0].legend(['ASTM AM1.5 G-173 (1 sun)'], loc='upper right', fontsize='x-small')
self.plot[1].legend(numpoints=1, fontsize='x-small', loc='best')
self.plot[2].legend(numpoints=1, fontsize='x-small', loc='best')
self.line0a = self.plot[0].axvline(x=0, ymin=0, ymax=1, linewidth=1, color='b', linestyle='-.')
self.line0b = self.plot[0].axvline(x=0, ymin=0, ymax=1, linewidth=1, color='r', linestyle='-.')
self.line0c = self.plot[0].axvline(x=0, ymin=0, ymax=1, linewidth=1, color='olive', linestyle='-.')
self.line2a = self.plot[2].axhline(y=0, xmin=0, xmax=1, linewidth=2, color='k')
self.line2b = self.plot[2].axvline(x=0, ymin=0, ymax=1, linewidth=2, color='k')
self.plot[4].set_xticks([])
self.plot[4].set_yticks([])
(tleft, tright) = self.plot[4].get_xlim()
(tbottom, ttop) = self.plot[4].get_ylim()
afont = FontProperties()
tfont = afont.copy()
tfont.set_style('normal')
tfont.set_weight('bold')
tfont.set_size('x-small')
self.report = self.plot[4].text(0.5 * (tleft + tright), 0.5 * (tbottom + ttop), "", horizontalalignment='center', verticalalignment='center', clip_on=True, fontproperties=tfont, color='black', transform=self.plot[4].transAxes)
for idp in range(0, self.plotcount):
self.plot[idp].get_xaxis().set_visible(True)
self.plot[idp].get_yaxis().set_visible(True)
# end for
self.PlotInitialized = True
# end if