-
Notifications
You must be signed in to change notification settings - Fork 0
/
BoundStatePotentials.py
944 lines (750 loc) · 43.6 KB
/
BoundStatePotentials.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
# -*- coding: utf-8 -*-
"""
Created on Sun Sep 29 15:49:30 2019
@author: lawre
"""
import Operators as op
import numpy as np
import scipy.special as sp
import scipy.optimize as opt
import matplotlib.pyplot as plt
class InfSquareWell:
"""
Simulates the Infinite Square Well potential. Also known as the Particle in a Box.
Parameters
--------------------
x : array-like
The spatial coordinates used to define the potential, V(x)
L : `float`, `int`
The width of the well/the size of the box. Note that the well is centered at 0 meaning L = 1 will produce
a well with with boundaries at x = -0.5 and x = 0.5
Attributes
---------------------------
Same as Parameters
"""
def __init__(self, x, L):
self.x = x
self.L = L
def potential(self, ylim = np.inf):
"""
Generates the potential, V(x), for the Infinite Square Well/Particle in a Box.
Parameters
--------------------
ylim : number, optional
The value at the boundaries of the Infinite Square Well/Particle in a Box. Conventionally, the value is infinity but
if real numbers are needed, the value can be specified. Defaults to np.inf
Returns
--------------------
out : `ndarray`
The output is an array representing an infinite square well of size L.
Examples
----------------------
Return V(x) for a well of size 4 with boundaries at x = -2 and x = 2.
>>> x = np.linspace(-5, 5, 11)
>>> infsquarewell = InfSquareWell(x, 4)
>>> infsquarewell.potential(), x
(array([inf, inf, inf, inf, 0., 0., 0., inf, inf, inf, inf]),
array([-5., -4., -3., -2., -1., 0., 1., 2., 3., 4., 5.]))
Return V(x) for a well of size 4 with boundaries at x = -2 and x = 2. Let infinity be replaced by 10.
>>> x = np.linspace(-5, 5, 11)
>>> infsquarewell = InfSquareWell(x, 4)
>>> infsquarewell.potential(ylim = 10), x
(array([10., 10., 10., 10., 0., 0., 0., 10., 10., 10., 10.]),
array([-5., -4., -3., -2., -1., 0., 1., 2., 3., 4., 5.]))
"""
potential = np.piecewise(self.x,
[np.any([self.x <= -self.L / 2,
self.x >= self.L / 2], axis = 0),
np.all([self.x > -self.L / 2,
self.x < self.L / 2], axis = 0)],
[ylim,
0])
return potential
def hamiltonian(self, wfunc, particle, finitediff_scheme = 'central', h_bar = 6.626e-34/(2*np.pi)):
"""
Returns the result of the Infinite Square Well/Particle in a Box Hamiltonian operator acting on a wavefunction.
Parameters
-----------------------------------
wfunc : array-like
Wavefunction for the operator to act on. Should be the same size as x
particle : Particle class (see Particle.py for more details)
Particle whose mass will be used in the kinetic energy portion of the hamiltonian
finitediff_scheme : {'central', 'five point stencil'}, optional
Method of finite difference approximation for the second order derivative in the kinetic energy operator. Options are
'central' for the central differences method or 'five point stencil' for the five point stencil method.
Defaults to 'central'.
h_bar : number, optional
The reduced planck constant. Defaults to 6.626e-34/2pi. For natural units, set to 1.
Returns
--------------------------------------
out : `ndarray`
The output is an array representing the result of the Infinite Square Well/Particle in a Box Hamiltonian acting on
the input wavefunction.
Notes
------------------------------------------
Since the function uses a finite difference method to approximate the second order derivative present in the kinetic energy
operator, a large array shoud be used to yield the most accurate results.
"""
if len(wfunc) < 100:
print ('WARNING: size of array may be too small to yield accurate results from finite difference approximation')
kinetic = op.kinetic_op(self.x,
wfunc,
particle,
h_bar = h_bar,
finitediff_scheme = finitediff_scheme)
potential = self.potential() * wfunc
new_wfunc = kinetic + potential
new_wfunc[new_wfunc == np.inf] = 0 #converts all infinity values to zero
new_wfunc = np.nan_to_num(new_wfunc) #converts all nan values to zero
return new_wfunc
def eigenfunc(self, n):
"""
Returns the normalized eigenfunction for the Infinite Square Well/Particle in a Box. Does not include time dependance.
This is equivalent to the eigenfunction at time t = 0. For the eigenfunction with the time dependance included,
see timedep_eigenfunc().
Parameters
---------------------------------------------
n : `int` starting from 1
The order of the eigenfunction. Note that for the Infinite Square Well/Particle in a Box, n = 1, 2, 3.... and does
not begin at 0.
Returns
---------------------------------------------
out : `ndarray`
The output is an array representing the nth normalized eigenfunction for the Infinite Square Well/Particle in a Box.
Examples
--------------------------------------------
Return the ground state eigenfunction for a Particle in a Box of length L = 3.
>>> x = np.linspace(-5, 5, 11)
>>> infsquarewell = InfSquareWell(x, 3)
>>> infsquarewell.eigenfunc(1)
array([0. , 0. , 0. , 0. , 0.40824829,
0.81649658, 0.40824829, 0. , 0. , 0. ,
0. ])
"""
if n <= 0 or n%1 != 0:
raise Exception('for the infinite square well, n must be a positive integer starting from 1')
if n%2 == 0:
return np.piecewise(self.x,
[np.any([self.x <= -self.L / 2,
self.x >= self.L / 2], axis = 0),
np.all([self.x > -self.L / 2,
self.x < self.L / 2], axis = 0)],
[0,
lambda x : np.sqrt(2 / self.L) * np.sin(n * np.pi * x / self.L)])
else:
return np.piecewise(self.x,
[np.any([self.x <= -self.L / 2,
self.x >= self.L / 2], axis = 0),
np.all([self.x > -self.L / 2,
self.x < self.L / 2], axis = 0)],
[0,
lambda x : np.sqrt(2 / self.L) * np.cos(n * np.pi * x / self.L)])
def timedep_eigenfunc(self, t, n, particle, h_bar = 6.626e-34/(2*np.pi)):
"""
Returns the normalized eigenfunction for the Infinite Square Well/Particle in a Box with time dependance included.
Parameters
-------------------------------------------------------
t : number
The time at which the eigenfunction is to be evaluated.
n : `int` starting from 1
The order of the eigenfunction. Note that for the Infinite Square Well/Particle in a Box, n = 1, 2, 3.... and does
not begin at 0.
particle : Particle class (see Particle.py for more details)
Particle whose mass will determine the energy and therefore the time dependance of the eigenfunction.
h_bar : number, optional
The reduced planck constant. Defaults to 6.626e-34/2pi. For natural units, set to 1.
Returns
------------------------------------------------------
out : `complex ndarray`
The output is a complex array representing the nth normalized eigenfunction at time t for the
Infinite Square Well/Particle in a Box.
Examples
------------------------------------------------------
Return the ground state eigenfunction for a Particle in a Box of length L = 3 at time t = 3. Units are natural units
such that the electron rest mass and the reduced planck constant is 1.
>>> import Particle as p
>>> x = np.linspace(-5, 5, 11)
>>> electron = p.Particle(1)
>>> infsquarewell = InfSquareWell(x, 3)
>>> infsquarewell.timedep_eigenfunc(3, 1, electron, h_bar = 1)
array([ 0. +0.j , 0. +0.j ,
0. +0.j , 0. +0.j ,
-0.03023889-0.40712686j, -0.06047777-0.81425371j,
-0.03023889-0.40712686j, 0. +0.j ,
0. +0.j , 0. +0.j ,
0. +0.j ])
"""
complex_x = np.array(self.x) + 0j #ensures input array is complex as np.piecewise returns an array of the same type as the input array
if n <= 0 or n%1 != 0:
raise Exception('for the infinite square well, n must be a positive integer starting from 1')
if n%2 == 0:
return np.piecewise(complex_x,
[np.any([self.x <= -self.L / 2,
self.x >= self.L / 2], axis = 0),
np.all([self.x > -self.L / 2,
self.x < self.L / 2], axis = 0)],
[0,
lambda x : np.sqrt(2 / self.L)
* np.sin(n * np.pi * x / self.L)
* np.exp(-1j * self.eigenvalue(n, particle, h_bar = h_bar) * t / h_bar)])
else:
return np.piecewise(complex_x,
[np.any([self.x <= -self.L / 2,
self.x >= self.L / 2], axis = 0),
np.all([self.x > -self.L / 2,
self.x < self.L / 2], axis = 0)],
[0,
lambda x : np.sqrt(2 / self.L)
* np.cos(n * np.pi * x / self.L)
* np.exp(-1j * self.eigenvalue(n, particle, h_bar = h_bar) * t / h_bar)])
def eigenvalue(self, n, particle, h_bar = 6.626e-34/(2*np.pi)):
"""
Returns the eigenvalue or energy of the Infinite Square Well/Particle in a Box.
Parameters
----------------------------------------------
n : `int` starting from 1
The order of the eigenvalue. Note that for the Infinite Square Well/Particle in a Box, n = 1, 2, 3.... and does
not begin at 0.
particle : Particle class (see Particle.py for more details)
The particle whose mass determines the energy of the nth eigenstate.
h_bar : number, optional
The reduced planck constant. Defaults to 6.626e-34/2pi. For natural units, set to 1.
Returns
----------------------------------------------
out : `float`
The output is the eigenvalue or the energy of the nth eigenstate for a particle trapped in the Infinite Square Well.
Examples
------------------------------------------------
Return the ground state energy of an electron trapped in an Infinite Square Well of length L = 1. Units are natural units
such that the electron rest mass and reduced planck constant is 1.
>>> import Particle as p
>>> x = np.linspace(-2, 2, 1000)
>>> electron = p.Particle(1)
>>> infsquarewell = InfSquareWell(x, 1)
>>> infsquarewell.eigenvalue(1, electron, h_bar = 1)
4.934802200544679
"""
return (h_bar * np.pi * n) ** 2 / (2 * particle.m * self.L ** 2)
class HarmonicOscillator:
"""
Simulates the Harmonic Oscillator potential.
Parameters
-------------------
x : array-like
The spatial coordinates used to define the potential, V(x)
k : `float`, `int`
The force constant of the harmonic oscillator
Attributes
--------------------
Same as Parameters
"""
def __init__(self, x, k):
self.k = k
self.x = x
def potential(self):
"""
Generates the potential, V(x), for the Harmonic Oscillator.
"""
return (self.k * self.x ** 2) / 2
def hamiltonian(self, wfunc, particle, finitediff_scheme = 'central', h_bar = 6.626e-34/(2*np.pi)):
"""
Returns the result of the Harmonic Oscillator Hamiltonian operator acting on a wavefunction.
Parameters
---------------------------------------------------
wfunc : array-like
Wavefunction for the operator to act on. Should be the same size as x
particle : Particle class (see Particle.py for more details)
Particle whose mass will be used in the kinetic energy portion of the hamiltonian
finitediff_scheme : {'central', 'five point stencil'}, optional
Method of finite difference approximation for the second order derivative in the kinetic energy operator. Options are
'central' for the central differences method or 'five point stencil' for the five point stencil method.
Defaults to 'central'.
h_bar : number, optional
The reduced planck constant. Defaults to 6.626e-34/2pi. For natural units, set to 1.
Returns
--------------------------------------
out : `ndarray`
The output is an array representing the result of the Harmonic Oscillator Hamiltonian acting on
the input wavefunction.
Notes
------------------------------------------
Since the function uses a finite difference method to approximate the second order derivative present in the kinetic energy
operator, a large array shoud be used to yield the most accurate results.
"""
if len(wfunc) < 100:
print ('WARNING: size of array may be too small to yield accurate results from finite difference approximation')
kinetic = op.kinetic_op(self.x,
wfunc,
particle,
h_bar = h_bar,
finitediff_scheme = finitediff_scheme)
potential = self.potential() * wfunc
return kinetic + potential
def ladderup_op(self, wfunc, particle, finitediff_scheme = 'central', h_bar = 6.626e-34/(2*np.pi)):
"""
Returns the result of the raising ladder operator acting on a wavefunction. The raising ladder operator increases the
order of the Harmonic Oscillator eigenfunction by one (up to a normalization constant).
Parameters
---------------------------------------------------------------------------------------------
wfunc : array-like
Wavefunction for the operator to act on. Should be the same size as x
particle : Particle class (see Particle.py for more details)
Particle whose mass will be used with the force constant to determine the angular frequency
finitediff_scheme : {'central', 'five point stencil'}, optional
Method of finite difference approximation for the second order derivative in the kinetic energy operator. Options are
'central' for the central differences method or 'five point stencil' for the five point stencil method.
Defaults to 'central'.
h_bar : number, optional
The reduced planck constant. Defaults to 6.626e-34/2pi. For natural units, set to 1.
Returns
--------------------------------------
out : `complex ndarray`
The output is a complex array representing the result of the raising ladder operator acting on
the input wavefunction. Note that the resulting wavefunction has NOT been normalized.
Notes
------------------------------------------
Since the function uses a finite difference method to approximate the first order derivative present in the momentum
operator, a large array shoud be used to yield the most accurate results. The output of the function is also NOT normalized.
"""
if len(wfunc) < 100:
print ('WARNING: size of array may be too small to yield accurate results from finite difference approximation')
w = np.sqrt(self.k / particle.m)
constant = 1 / np.sqrt(2 * h_bar * particle.m * w)
return constant*(particle.m * w * op.position_op(self.x, wfunc)
- 1j * op.momentum_op(self.x,
wfunc,
h_bar = h_bar,
finitediff_scheme = finitediff_scheme))
def ladderdown_op(self, wfunc, particle, h_bar = 6.626e-34/(2*np.pi), finitediff_scheme = 'central'):
"""
Returns the result of the lowering ladder operator acting on a wavefunction. The lowering ladder operator decreases the
order of the Harmonic Oscillator eigenfunction by one (up to a normalization constant).
Parameters
---------------------------------------------------------------------------------------------
wfunc : array-like
Wavefunction for the operator to act on. Should be the same size as x
particle : Particle class (see Particle.py for more details)
Particle whose mass will be used with the force constant to determine the angular frequency
finitediff_scheme : {'central', 'five point stencil'}, optional
Method of finite difference approximation for the second order derivative in the kinetic energy operator. Options are
'central' for the central differences method or 'five point stencil' for the five point stencil method.
Defaults to 'central'.
h_bar : number, optional
The reduced planck constant. Defaults to 6.626e-34/2pi. For natural units, set to 1.
Returns
--------------------------------------
out : `complex ndarray`
The output is a complex array representing the result of the lowering ladder operator acting on
the input wavefunction. Note that the resulting wavefunction has NOT been normalized.
Notes
------------------------------------------
Since the function uses a finite difference method to approximate the first order derivative present in the momentum
operator, a large array shoud be used to yield the most accurate results. The output of the function is also NOT normalized.
"""
if len(wfunc) < 100:
print ('WARNING: size of array may be too small to yield accurate results from finite difference approximation')
w = np.sqrt(self.k / particle.m)
constant = 1 / np.sqrt(2 * h_bar * particle.m * w)
return constant*(particle.m * w * op.position_op(self.x, wfunc)
+ 1j*op.momentum_op(self.x,
wfunc,
h_bar = h_bar,
finitediff_scheme = finitediff_scheme))
def eigenfunc(self, n, particle, h_bar = 6.626e-34/(2*np.pi)):
"""
Returns the normalized eigenfunction for the Harmonic Oscillator. Does not include time dependance.
This is equivalent to the eigenfunction at time t = 0. For the eigenfunction with the time dependance included,
see timedep_eigenfunc().
Parameters
---------------------------------------------
n : `int`
The order of the eigenfunction.
particle : Particle class (see Particle.py for more details)
Particle whose mass will determine the angular frequency from the force constant
h_bar : number, optional
The reduced planck constant. Defaults to 6.626e-34/2pi. For natural units, set to 1.
Returns
---------------------------------------------
out : `ndarray`
The output is an array representing the nth normalized eigenfunction for the Harmonic Oscillator.
Examples
--------------------------------------------
Return the ground state eigenfunction for a Harmonic Oscillator with force constant k = 1. Units are natural units such
that the electron rest mass and reduced Planck constant is 1.
>>> import Particle as p
>>> x = np.linspace(-5, 5, 10)
>>> ho = HarmonicOscillator(x, 1)
>>> electron = p.Particle(1)
>>> ho.eigenfunc(0, electron, h_bar = 1)
array([2.79918439e-06, 3.90567063e-04, 1.58560022e-02, 1.87294814e-01,
6.43712257e-01, 6.43712257e-01, 1.87294814e-01, 1.58560022e-02,
3.90567063e-04, 2.79918439e-06])
"""
if n < 0 or n%1 != 0:
raise Exception('for the harmonic oscillator, n must be a positive integer starting from 0')
n = int(n) #coverts n datatype to the native Python int type to ensure number of bits is enough for calculating the square root of large numbers
w = np.sqrt(self.k / particle.m)
alpha = particle.m * w / h_bar
y = np.sqrt(alpha) * self.x
Hermite = sp.eval_hermite(n, y)
C = (1 / np.sqrt(float(2 ** n) * np.math.factorial(n))) * (alpha / np.pi) ** (1 / 4)
return C * np.exp(-y ** 2 / 2) * Hermite
def timedep_eigenfunc(self, t, n, particle, h_bar = 6.626e-34/(2*np.pi)):
"""
Returns the normalized eigenfunction for the Harmonic Oscillator with time dependance included.
Parameters
-------------------------------------------------------
t : number
The time at which the eigenfunction is to be evaluated.
n : `int`
The order of the eigenfunction.
particle : Particle class (see Particle.py for more details)
Particle whose mass will determine the energy and therefore the time dependance of the eigenfunction.
h_bar : number, optional
The reduced planck constant. Defaults to 6.626e-34/2pi. For natural units, set to 1.
Returns
------------------------------------------------------
out : `complex ndarray`
The output is a complex array representing the nth normalized eigenfunction at time t for the
Harmonic Oscillator
Examples
------------------------------------------------------
Return the first excited state for a Harmonic Oscillator with k = 1 at time t = 3. Units are natural units
such that the electron rest mass and the reduced planck constant is 1.
>>> import Particle as p
>>> x = np.linspace(-5, 5, 10)
>>> electron = p.Particle(1)
>>> ho = HarmonicOscillator(x, 1)
>>> ho.timedep_eigenfunc(3, 0, electron, h_bar = 1)
array([2.79918439e-06+0.j, 3.90567063e-04+0.j, 1.58560022e-02+0.j,
1.87294814e-01+0.j, 6.43712257e-01+0.j, 6.43712257e-01+0.j,
1.87294814e-01+0.j, 1.58560022e-02+0.j, 3.90567063e-04+0.j,
2.79918439e-06+0.j])
"""
if n < 0 or n%1 != 0:
raise Exception('for the harmonic oscillator, n must be a positive integer starting from 0')
n = int(n) #coverts n datatype to the native Python int type to ensure number of bits is enough for calculating the square root of large numbers
w = np.sqrt(self.k / particle.m)
alpha = particle.m * w / h_bar
y = np.sqrt(alpha) * self.x
Hermite = sp.eval_hermite(n, y)
C = (1 / np.sqrt(float(2 ** n) * np.math.factorial(n))) * (alpha / np.pi) ** (1 / 4)
return C * np.exp(-y ** 2 / 2) * Hermite * np.exp(-1j * self.eigenvalue(n, particle, h_bar = h_bar) * t / h_bar)
def eigenvalue(self, n, particle, h_bar = 6.626e-34/(2*np.pi)):
"""
Returns the eigenvalue or energy of the Harmonic Oscillator.
Parameters
----------------------------------------------
n : `int`
The order of the eigenvalue.
particle : Particle class (see Particle.py for more details)
The particle whose mass determines the energy of the nth eigenstate.
h_bar : number, optional
The reduced planck constant. Defaults to 6.626e-34/2pi. For natural units, set to 1.
Returns
----------------------------------------------
out : `float`
The output is the eigenvalue or the energy of the nth eigenstate for a particle trapped in the Harmonic Oscillator.
Examples
------------------------------------------------
Return the ground state energy of an electron trapped in a Harmonic Oscillator with force constant k = 1. Units are natural
units such that the electron rest mass and reduced planck constant is 1.
>>> import Particle as p
>>> x = np.linspace(-2, 2, 1000)
>>> electron = p.Particle(1)
>>> ho = HarmonicOscillator(x, 1)
>>> ho.eigenvalue(0, electron, h_bar = 1)
0.5
"""
w = np.sqrt(self.k / particle.m)
return h_bar * w * (n + 1/2)
class FiniteSquareWell:
"""
Simulates the finite square well potential.
Parameters
-------------------
x : array-like
The spatial coordinates used to define the potential, V(x)
L : `float`, `int`
The length of the box
V0 : `float`, `int`
The 'depth' of the box
Attributes
--------------------
Same as Parameters
"""
def __init__(self, x, L, V0):
self.x = x
self.L = L
self.V0 = V0
def potential(self):
"""
Generates the potential, V(x), of the finite square well
"""
potential = np.piecewise(self.x,
[np.any([self.x <= -self.L / 2,
self.x >= self.L / 2], axis = 0),
np.all([self.x > -self.L / 2,
self.x < self.L / 2], axis = 0)],
[0,
-self.V0])
return potential
def hamiltonian(self, wfunc, particle, finitediff_scheme = 'central', h_bar = 6.626e-34/(2*np.pi)):
"""
Returns the result of the Finite Square Potential Hamiltonian operator acting on a wavefunction.
Parameters
---------------------------------------------------
wfunc : array-like
Wavefunction for the operator to act on. Should be the same size as x
particle : Particle class (see Particle.py for more details)
Particle whose mass will be used in the kinetic energy portion of the hamiltonian
finitediff_scheme : {'central', 'five point stencil'}, optional
Method of finite difference approximation for the second order derivative in the kinetic energy operator. Options are
'central' for the central differences method or 'five point stencil' for the five point stencil method.
Defaults to 'central'.
h_bar : number, optional
The reduced planck constant. Defaults to 6.626e-34/2pi. For natural units, set to 1.
Returns
--------------------------------------
out : `ndarray`
The output is an array representing the result of the Finite Square Potential Hamiltonian acting on
the input wavefunction.
Notes
------------------------------------------
Since the function uses a finite difference method to approximate the second order derivative present in the kinetic energy
operator, a large array shoud be used to yield the most accurate results.
"""
kinetic = op.kinetic_op(self.x,
wfunc,
particle,
h_bar = h_bar,
finitediff_scheme = finitediff_scheme)
potential = self.potential() * wfunc
new_wfunc = kinetic + potential
return new_wfunc
def eigenfunc(self, n, particle, h_bar = 6.626e-34/(2*np.pi)):
"""
Returns the normalized eigenfunction for the Finite Square Potential. Does not include time dependance.
This is equivalent to the eigenfunction at time t = 0. For the eigenfunction with the time dependance included,
see timedep_eigenfunc().
Parameters
---------------------------------------------
n : `int`
The order of the eigenfunction.
particle : Particle class (see Particle.py for more details)
Particle whose mass will determine the Energy levels of the potential
h_bar : number, optional
The reduced planck constant. Defaults to 6.626e-34/2pi. For natural units, set to 1.
Returns
---------------------------------------------
out : `ndarray`
The output is an array representing the nth normalized eigenfunction for the Finite Square Potential.
Examples
--------------------------------------------
Return the ground state eigenfunction for a Finite Square Well with length, L = 1 and depth, V0 = 10. Units are natural units
that the electron rest mass and reduced Planck constant is 1.
>>> import Particle as p
>>> x = np.linspace(-1, 1, 10)
>>> fsw = FiniteSquareWell(x, 1, 10)
>>> electron = p.Particle(1)
>>> fsw.eigenfunc(0, electron, h_bar = 1)
array([0.07759989 0.18565918 0.44419303 0.87140321 1.12065552 1.12065552
0.87140321 0.44419303 0.18565918 0.07759989])
"""
#Finding roots of the transcendental equation: tan(z) = np.sqrt((z/z0) ** 2 - 1) for even functions or -cot(z) = np.sqrt((z/z0) ** 2 -1) for odd functions
z0 = self.L * np.sqrt(2 * particle.m * self.V0) / (2 * h_bar)
z = opt.brentq(self.transcendental_root, #uses brentq method of root finding
n * np.pi / 2, #in the given region
(n + 1) * np.pi / 2,
args = (z0, n))
#catch any errors from root finding
if z == 0 or (z0 ** 2 - z ** 2) < 0:
raise Exception('Dimensions of the well do not permit bound states with this value of n. Please reconsider your parameters')
#finds "Energy" constants from root, z
l = 2 * z / self.L
k = 2 * np.sqrt(z0 ** 2 - z ** 2) / self.L
#construct bound state eigenfunctions
#Even functions
if n%2 == 0:
const_factor = np.cos(z) / np.exp(-np.sqrt(z0 ** 2 - z ** 2)) #finds constant such that piecewise function is continuous at boundaries
eigenfunction = np.piecewise(self.x,
[self.x <= -self.L / 2,
np.all([self.x > -self.L / 2,
self.x < self.L / 2], axis = 0),
self.x >= self.L / 2],
[lambda x : const_factor * np.exp(k * x),
lambda x : np.cos(l * x),
lambda x : const_factor * np.exp(-k * x)])
eigenfunction = op.normalize(self.x, eigenfunction) #normalize eigenfunction
#Odd functions
else:
const_factor = np.sin(z) / np.exp(-np.sqrt(z0 ** 2 - z ** 2)) #finds constant such that piecewise function is continuous at boundaries
eigenfunction = np.piecewise(self.x,
[self.x <= -self.L / 2,
np.all([self.x > -self.L / 2,
self.x < self.L / 2], axis = 0),
self.x >= self.L / 2],
[lambda x : -const_factor * np.exp(k * x),
lambda x : np.sin(l * x),
lambda x: const_factor * np.exp(-k * x)])
eigenfunction = op.normalize(self.x, eigenfunction) #normalize eigenfunctions
return eigenfunction
def timedep_eigenfunc(self, t, n, particle, h_bar = 6.626e-34/(2*np.pi)):
"""
Returns the normalized eigenfunction for the Finite Square Well with time dependance included.
Parameters
-------------------------------------------------------
t : number
The time at which the eigenfunction is to be evaluated.
n : `int`
The order of the eigenfunction.
particle : Particle class (see Particle.py for more details)
Particle whose mass will determine the energy and therefore the time dependance of the eigenfunction.
h_bar : number, optional
The reduced planck constant. Defaults to 6.626e-34/2pi. For natural units, set to 1.
Returns
------------------------------------------------------
out : `complex ndarray`
The output is a complex array representing the nth normalized eigenfunction at time t for the
Finite Square Well
Examples
------------------------------------------------------
Return the ground state for a Finite Square Well with length, L = 1 and depth, V0 = 10, at time t = 3.
Units are natural units such that the electron rest mass and the reduced planck constant is 1.
>>> import Particle as p
>>> x = np.linspace(-1, 1, 10)
>>> electron = p.Particle(1)
>>> fsw = FiniteSquareWell(x, 1, 10)
>>> fsw.timedep_eigenfunc(3, 0, electron, h_bar = 1)
array([-0.07039142+0.03266177j -0.16841278+0.07814389j -0.40293069+0.18696071j
-0.79045612+0.36677335j -1.01655467+0.47168357j -1.01655467+0.47168357j
-0.79045612+0.36677335j -0.40293069+0.18696071j -0.16841278+0.07814389j
-0.07039142+0.03266177j])
"""
complex_x = np.array(self.x) + 0j #ensure complex array
#Finding roots of the transcendental equation: tan(z) = np.sqrt((z/z0) ** 2 - 1) for even functions or -cot(z) = np.sqrt((z/z0) ** 2 -1) for odd functions
z0 = self.L * np.sqrt(2 * particle.m * self.V0) / (2 * h_bar)
z = opt.brentq(self.transcendental_root, #uses brentq method of root finding
n * np.pi / 2, #in the given region
(n + 1) * np.pi / 2,
args = (z0, n))
#catch any errors from root finding
if z == 0 or (z0 ** 2 - z ** 2) < 0:
raise Exception('Dimensions of the well do not permit this value of n. Please reconsider your parameters')
#finds "Energy" constants from root, z
l = 2 * z / self.L
k = 2 * np.sqrt(z0 ** 2 - z ** 2) / self.L
#constructs bound state eigenfunctions with time dependance
timefactor = np.exp(-1j * self.eigenvalue(n, particle, h_bar = h_bar) * t / h_bar) #time dependance factor
#even functions
if n%2 == 0:
const_factor = np.cos(z) / np.exp(-np.sqrt(z0 ** 2 - z ** 2)) #finds constant such that piecewise function is continuous
eigenfunction = np.piecewise(complex_x,
[self.x <= -self.L / 2,
np.all([self.x > -self.L / 2,
self.x < self.L / 2], axis = 0),
self.x >= self.L / 2],
[lambda x : const_factor * np.exp(k * x),
lambda x : np.cos(l * x),
lambda x : const_factor * np.exp(-k * x)])
#odd functions
else:
const_factor = np.sin(z) / np.exp(-np.sqrt(z0 ** 2 - z ** 2)) #finds constant such that piecewise function is continuous
eigenfunction = np.piecewise(complex_x,
[self.x <= -self.L / 2,
np.all([self.x > -self.L / 2,
self.x < self.L / 2], axis = 0),
self.x >= self.L / 2],
[lambda x : -const_factor * np.exp(k * x),
lambda x : np.sin(l * x),
lambda x : const_factor * np.exp(-k * x)])
eigenfunction = op.normalize(self.x, eigenfunction)*timefactor #normalize eigenfunction and multiply it by time dependance
return eigenfunction
def eigenvalue(self, n, particle, h_bar = 6.626e-34/(2*np.pi)):
"""
Returns the eigenvalue or energy of the Finite Square Well.
Parameters
----------------------------------------------
n : `int`
The order of the eigenvalue.
particle : Particle class (see Particle.py for more details)
The particle whose mass determines the energy of the nth eigenstate.
h_bar : number, optional
The reduced planck constant. Defaults to 6.626e-34/2pi. For natural units, set to 1.
Returns
----------------------------------------------
out : `float`
The output is the eigenvalue or the energy of the nth eigenstate for a particle trapped in the Harmonic Oscillator.
Examples
------------------------------------------------
Return the ground state energy of an electron trapped in a Finite Square Well with length, L = 1 and depth V0 = 10.
Units are natural units such that the electron rest mass and reduced planck constant is 1.
>>> import Particle as p
>>> x = np.linspace(-1, 1, 1000)
>>> electron = p.Particle(1)
>>> fsw = FiniteSquareWell(x, 1, 10)
>>> fsw.eigenvalue(0, electron, h_bar = 1)
-7.185570550459651
"""
#Finding roots of the transcendental equation: tan(z) = np.sqrt((z/z0) ** 2 - 1) for even functions or -cot(z) = np.sqrt((z/z0) ** 2 -1) for odd functions
z0 = self.L * np.sqrt(2 * particle.m * self.V0 / (2 * h_bar))
z = opt.brentq(self.transcendental_root, #uses brentq method of root finding
n * np.pi / 2, (n + 1) * np.pi / 2, #in the given region
args = (z0, n))
#catch any errors from root finding
if z == 0 or (z0 ** 2 - z ** 2) < 0:
raise Exception('Dimensions of the well do not permit this value of n. Please reconsider your parameters')
#Find energy from root, z
l = 2 * z / self.L
E = (l * h_bar) ** 2 / (2 * particle.m) - self.V0
return E
def transcendental_root(self, z, z0, n):
"""
Returns transcendental equations to find energies for the Finite Square Well potential. For even n, equations are of the form
tan(z) - sqrt((z / z0) ** 2 - 1) = 0 and for odd n, equations are of the form -cot(z) - sqrt((z / z0) ** 2 - 1) = 0.
Used in the eigenfunc, timedep_eigenfunc, and eigenvalue methods.
Parameters
---------------------------------------------------
z : ndarray
Array containing z-values that can be used to find eigenvalues for the Finite Square Well potential
z0 : number
Constant that is defined by length and depth of the Finite Square Well. Determines root of equation and therefore the
eigenvalue.
n : `int`
Order of the eigenvalue
Returns
-----------------------------------------------------
out : ndarray
Returns array representing transcendental equations that can be used to find the energy of the Finite Square Well
"""
if n%2 == 0:
result = np.tan(z) - np.sqrt((z0 / z) ** 2 - 1)
else:
result = -1 / np.tan(z) - np.sqrt((z0 / z) ** 2 - 1)
return result
def plot_transcendental(self, n, particle, h_bar = 6.626e-34/(2*np.pi), y_lim = 30):
"""
Plots transcendental equation that can be used to find energies for the Finite Square Well potential. The points
at which the curves meet are the points at which the energy can be evaluated.
Parameters
-------------------------------------------------------------
n : `int`
Order of the largest eigenfunction
particle : Particle class (see Particle.py for more details)
The particle whose mass determines the energy of the nth eigenstate.
h_bar : number, optional
The reduced planck constant. Defaults to 6.626e-34/2pi. For natural units, set to 1.
ylim : number, optional
The maximum y-value on the y-axis. Defaults to 30.
"""
z0 = self.L * np.sqrt(2 * particle.m * self.V0 / (2 * h_bar))
z = np.linspace(0, (n + 1) * np.pi / 2, 1000)
dz = z[1] - z[0]
even = np.tan(z)
even[z % (np.pi / 2) < dz * 3] = np.nan #prevents matplotlib from plotting "jumps" and makes the graph look nicer
odd = -1/np.tan(z)
odd[z % (np.pi / 2) < dz * 3] = np.nan #prevents matplotlib from plotting "jumps" and makes the graph look nicer
plt.plot(z, np.sqrt((z0 / z) ** 2 - 1))
plt.plot(z, even, 'r')
plt.plot(z, odd, 'r')
plt.ylim(0, y_lim)