Skip to content

Commit 2aeb850

Browse files
committed
Update BasisSetSchrodinger.wl
1 parent 078f45b commit 2aeb850

File tree

1 file changed

+123
-62
lines changed

1 file changed

+123
-62
lines changed

Diff for: BasisSetSchrodinger.wl

+123-62
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ shiftScaledWfns::usage="";
4141
$defaults::usage="";
4242

4343

44+
iWavefunctionEigensystem::usage="";
45+
iWavefunctions::usage="";
46+
iWavefunctionPlot::usage="";
47+
48+
4449
EndPackage[]
4550

4651

@@ -70,38 +75,43 @@ pib[{l_, min_}]:=
7075
pib~SetAttributes~HoldRest;
7176

7277

73-
(* ::Subsubsection::Closed:: *)
78+
(* ::Subsubsection:: *)
7479
(*ho*)
7580

7681

77-
ho[{omega_, m_, hb_}, x_][n_]:=
78-
With[{a=m*omega/hb},
79-
(1/Sqrt[(2^n)n!])*Power[a/Pi, 1/4]*Exp[-a/2*x^2]*HermiteH[n, Sqrt[a]*x]
82+
ho[{omega_, m_, hb_, x0_}, x_][nn_]:=
83+
With[{a=m*omega/hb, n=nn-1},
84+
(1/Sqrt[(2^n)n!])*Power[a/Pi, 1/4]*Exp[-a/2*(x-x0)^2]*HermiteH[n, Sqrt[a]*(x-x0)]
8085
];
81-
ho[{omega_, m_, hb_}]:=
82-
Function[Null, ho[{omega, m, hb}, #], HoldFirst];
86+
ho[{omega_, m_, hb_, x0_}]:=
87+
Function[Null, ho[{omega, m, hb, x0}, #], HoldFirst];
8388
ho~SetAttributes~HoldRest;
8489

8590

86-
(* ::Subsubsection::Closed:: *)
91+
(* ::Subsubsection:: *)
8792
(*getBasisFunction*)
8893

8994

9095
getBasisFunction//Clear;
9196
getBasisFunction["ParticleInABox", ops___]:=
92-
pib[
93-
{
94-
Lookup[{ops}, "Length", 1.],
95-
Lookup[{ops}, "Minimum", 0.]
96-
}
97+
With[{oop=Select[{ops}, OptionQ]},
98+
pib[
99+
{
100+
Lookup[oop, "Length", 1.],
101+
Lookup[oop, "Minimum", 0.]
102+
}
103+
]
97104
];
98105
getBasisFunction["HarmonicOscillator", ops___]:=
99-
ho[
100-
{
101-
Lookup[{ops}, "Frequency", 1.],
102-
Lookup[{ops}, "Mass", 1],
103-
Lookup[{ops}, "PlanckConstant", 1]
104-
}
106+
With[{oop=Select[{ops}, OptionQ]},
107+
ho[
108+
{
109+
Lookup[oop, "Frequency", 1.],
110+
Lookup[oop, "Mass", 1],
111+
Lookup[oop, "PlanckConstant", 1],
112+
Lookup[oop, "Center", 0.]
113+
}
114+
]
105115
];
106116
getBasisFunction[l_List]:=
107117
getBasisFunction@@l;
@@ -117,30 +127,53 @@ $integrator=NIntegrate;
117127
(* I put this here so people can hack it later *)
118128

119129

120-
hel[{n_, m_}, bf_, pot_, {hb_, mu_}, {min_, max_}, x_Symbol]:=
121-
hel[{n, m}, bf, pot, {hb, mu},{min, max}, x]=
122-
With[{psin=bf[x][n], psim=bf[x][m], p=pot[x]},
123-
Quiet[
124-
$integrator[
125-
psin*(
126-
-hb/(2mu)*D[psim, {x, 2}]+p*psim
127-
),
128-
{x, min, max}
129-
],
130-
{
131-
NIntegrate::ncvb,
132-
NIntegrate::slwcon
133-
}
134-
]
135-
];
130+
ihel[{n_, m_}, bf_, pot_, {hb_, mu_}, {min_, max_}, x_Symbol]:=
131+
With[{psin=bf[x][n], psim=bf[x][m], p=pot[x]},
132+
Quiet[
133+
$integrator[
134+
psin*(
135+
-hb/(2mu)*D[psim, {x, 2}]+p*psim
136+
),
137+
{x, min, max}
138+
],
139+
{
140+
NIntegrate::ncvb,
141+
NIntegrate::slwcon
142+
}
143+
]
144+
]
136145

137146

138-
(* ::Subsubsection::Closed:: *)
147+
hel[{n_, m_}, bf_, pot_, {hb_, mu_}, {min_, max_}, x_Symbol, useCached_]:=
148+
Module[{res},
149+
res=ihel[{n, m}, bf, pot, {hb, mu},{min, max}, x];
150+
If[NumericQ@res,
151+
If[useCached,
152+
hel[{n, m}, bf, pot, {hb, mu},{min, max}, x, True]=res,
153+
res
154+
],
155+
Throw@
156+
Failure["BadHam",
157+
<|
158+
"MessageTemplate"->"Non-numeric Hamiltonian element ``",
159+
"MessageParameter"->{res}
160+
|>
161+
]
162+
]
163+
]
164+
165+
166+
(* ::Subsubsection:: *)
139167
(*ham*)
140168

141169

142-
ham[nT_, bf_, pot_,{hb_, mu_}, {min_, max_}, x_]:=
143-
Table[hel[{n ,m}, bf, pot,{hb, mu}, {min, max}, x], {n, 1, nT}, {m, 1, nT}];
170+
ham//Clear
171+
ham[nT_, bf_, pot_,{hb_, mu_}, {min_, max_}, x_, useCached_]:=
172+
Table[
173+
hel[{n ,m}, bf, pot,{hb, mu}, {min, max}, x, useCached],
174+
{n, 1, nT},
175+
{m, 1, nT}
176+
];
144177

145178

146179
(* ::Subsubsection::Closed:: *)
@@ -189,9 +222,9 @@ $defaults=
189222
"PotentialFunction"->
190223
(
191224
Piecewise[{
192-
{10^4, #<=0},
225+
{10^6, #<=0},
193226
{0, 0<#<1},
194-
{10^4, #>=1}
227+
{10^6, #>=1}
195228
}]&
196229
),
197230
"Range"->{0., 1.}
@@ -202,16 +235,17 @@ $defaults=
202235
(*WavefunctionEigensystem*)
203236

204237

205-
Options[WavefunctionEigensystem]=
238+
Options[iWavefunctionEigensystem]=
206239
{
207240
"BasisFunction"->Automatic,
208241
"PotentialFunction"->Automatic,
209242
"Range"->Automatic,
210243
"BasisSize"->Automatic,
211244
"Mass"->Automatic,
212-
"PlanckConstant"->Automatic
245+
"PlanckConstant"->Automatic,
246+
"UseCachedElements"->False
213247
};
214-
WavefunctionEigensystem[ops:OptionsPattern[]]:=
248+
iWavefunctionEigensystem[ops:OptionsPattern[]]:=
215249
Module[
216250
{
217251
nT=
@@ -236,49 +270,67 @@ WavefunctionEigensystem[ops:OptionsPattern[]]:=
236270
es
237271
},
238272
bf=getBasisFunction@bf;
239-
h=ham[nT, bf, pot, {hb, m}, r, \[FormalX]];
273+
h=ham[nT, bf, pot, {hb, m}, r, \[FormalX], TrueQ@OptionValue["UseCachedElements"]];
240274
es=getSolns@h;
241275
es[[2]]=Threshold[es[[2]], 10^-6];
242276
es
243277
]
244278

245279

280+
WavefunctionEigensystem//ClearAll
281+
Options[WavefunctionEigensystem]=
282+
Options[iWavefunctionEigensystem];
283+
WavefunctionEigensystem[e___]:=
284+
Module[{res=Catch@iWavefunctionEigensystem[e]},
285+
res/;Head[res]=!=iWavefunctionEigensystem
286+
]
287+
288+
246289
(* ::Subsubsection::Closed:: *)
247290
(*Wavefunctions*)
248291

249292

250-
Options[Wavefunctions]=
251-
Options[WavefunctionEigensystem];
252-
Wavefunctions[es:{_List, _List}, bf:Except[_?OptionQ]]:=
293+
Options[iWavefunctions]=
294+
Options[iWavefunctionEigensystem];
295+
iWavefunctions[es:{_List, _List}, bf:Except[_?OptionQ]]:=
253296
{es[[1]], expandSoln[{#, bf}, \[FormalX]]&/@es[[2]]};
254-
Wavefunctions[es:{_List, _List}, ops:OptionsPattern[]]:=
297+
iWavefunctions[es:{_List, _List}, ops:OptionsPattern[]]:=
255298
Module[
256299
{
257300
bf=
258301
Replace[OptionValue["BasisFunction"],
259302
Automatic:>$defaults["BasisFunction"]]
260303
},
261304
bf=getBasisFunction@bf;
262-
Wavefunctions[es, bf]
305+
iWavefunctions[es, bf]
263306
];
264-
Wavefunctions[ops:OptionsPattern[]]:=
265-
Wavefunctions[WavefunctionEigensystem[ops], ops]
307+
iWavefunctions[ops:OptionsPattern[]]:=
308+
iWavefunctions[iWavefunctionEigensystem[ops], ops]
266309

267310

268-
(* ::Subsubsection:: *)
311+
Wavefunctions//ClearAll
312+
Options[Wavefunctions]=
313+
Options[iWavefunctions];
314+
Wavefunctions[e___]:=
315+
Module[{res=Catch@iWavefunctions[e]},
316+
res/;Head[res]=!=iWavefunctions
317+
]
318+
319+
320+
(* ::Subsubsection::Closed:: *)
269321
(*WavefunctionPlot*)
270322

271323

272-
WavefunctionPlot//Clear
273-
Options[WavefunctionPlot]=
324+
iWavefunctionPlot//Clear
325+
Options[iWavefunctionPlot]=
274326
Join[
275327
Options[Wavefunctions],
276328
Options[Plot],
277329
{
278330
"WavefunctionScaling"->1
279331
}
280332
];
281-
WavefunctionPlot[
333+
iWavefunctionPlot[
282334
wfs:{_List, _List?(Not@MatrixQ[#, Internal`RealValuedNumberQ]&)},
283335
pot_,
284336
{min_?NumericQ, max_?NumericQ},
@@ -297,7 +349,7 @@ WavefunctionPlot[
297349
Options[Plot]
298350
]
299351
];
300-
WavefunctionPlot[
352+
iWavefunctionPlot[
301353
wfs:{_List, _List?(Not@MatrixQ[#, Internal`RealValuedNumberQ]&)},
302354
ops:OptionsPattern[]
303355
]:=
@@ -315,23 +367,32 @@ WavefunctionPlot[
315367
)
316368
]
317369
},
318-
WavefunctionPlot[wfs, pot, range, ops]
370+
iWavefunctionPlot[wfs, pot, range, ops]
319371
];
320-
WavefunctionPlot[
372+
iWavefunctionPlot[
321373
es:{_List, _List?(MatrixQ[#, Internal`RealValuedNumberQ]&)},
322374
ops:OptionsPattern[]
323375
]:=
324-
WavefunctionPlot[
325-
Wavefunctions[es, FilterRules[{ops}, Options[Wavefunctions]]],
376+
iWavefunctionPlot[
377+
iWavefunctions[es, FilterRules[{ops}, Options[iWavefunctions]]],
326378
ops
327379
];
328-
WavefunctionPlot[ops:OptionsPattern[]]:=
329-
WavefunctionPlot[
330-
Wavefunctions[FilterRules[{ops}, Options[Wavefunctions]]],
380+
iWavefunctionPlot[ops:OptionsPattern[]]:=
381+
iWavefunctionPlot[
382+
iWavefunctions[FilterRules[{ops}, Options[iWavefunctions]]],
331383
ops
332384
]
333385

334386

387+
WavefunctionPlot//ClearAll
388+
Options[WavefunctionPlot]=
389+
Options[iWavefunctionPlot];
390+
WavefunctionPlot[e___]:=
391+
Module[{res=Catch@iWavefunctionPlot[e]},
392+
res/;Head[res]=!=iWavefunctionPlot
393+
]
394+
395+
335396
(* ::Subsubsection:: *)
336397
(*End*)
337398

0 commit comments

Comments
 (0)