Skip to content

Commit

Permalink
Add tests for MorsePotential and PoschlTeller, Close #43
Browse files Browse the repository at this point in the history
  • Loading branch information
ohno committed Jun 9, 2024
1 parent d86080a commit b33a273
Show file tree
Hide file tree
Showing 7 changed files with 310 additions and 67 deletions.
12 changes: 5 additions & 7 deletions src/MorsePotential.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,9 @@ function V(model::MorsePotential, r)
end

# eigenvalues
function E(model::MorsePotential; n::Int=0)
n_max = nₘₐₓ(model)
if !(0 n n_max)
throw(DomainError("n = $n, n_max = $n_max", "n must be non-negative and smaller than n_max: 0 ≤ n ≤ n_max."))
function E(model::MorsePotential; n::Int=0, nocheck=false)
if !(0 n nₘₐₓ(model) || nocheck)
throw(DomainError("(n,nₘₐₓ(model)) = ($n,$(nₘₐₓ(model)))", "This function is defined for 0 ≤ n ≤ nₘₐₓ(model)."))
end
Dₑ = model.Dₑ
k = model.k
Expand All @@ -52,9 +51,8 @@ end

# eigenfunctions
function ψ(model::MorsePotential, r; n::Int=0)
n_max = nₘₐₓ(model)
if !(0 n n_max)
throw(DomainError("n = $n, n_max = $n_max", "n must be non-negative and smaller than n_max: 0 ≤ n ≤ n_max."))
if !(0 n nₘₐₓ(model))
throw(DomainError("(n,nₘₐₓ(model)) = ($n,$(nₘₐₓ(model)))", "This function is defined for 0 ≤ n ≤ nₘₐₓ(model)."))
end
if !(0 r)
throw(DomainError("r = $r", "r must be non-negative: 0 ≤ r."))
Expand Down
10 changes: 5 additions & 5 deletions src/PoschlTeller.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ function nₘₐₓ(model::PoschlTeller)
end

# eigenvalues
function E(model::PoschlTeller; n::Int=0)
function E(model::PoschlTeller; n::Int=0, nocheck=false)
n_max = nₘₐₓ(model)
if !(0 n n_max)
throw(DomainError("(n,n_max) = ($n,$n_max)", "This function is defined for 0 ≤ n ≤ n_max."))
if !(0 n nₘₐₓ(model) || nocheck)
throw(DomainError("(n,nₘₐₓ(model)) = ($n,$(nₘₐₓ(model)))", "This function is defined for 0 ≤ n ≤ nₘₐₓ(model)."))
end
λ = model.λ
m = model.m
Expand All @@ -37,8 +37,8 @@ end
# eigenfunctions
function ψ(model::PoschlTeller, x; n::Int=0)
n_max = nₘₐₓ(model)
if !(0 n n_max)
throw(DomainError("(n,n_max) = ($n,$n_max)", "This function is defined for 0 ≤ n ≤ n_max."))
if !(0 n nₘₐₓ(model))
throw(DomainError("(n,nₘₐₓ(model)) = ($n,$(nₘₐₓ(model)))", "This function is defined for 0 ≤ n ≤ nₘₐₓ(model)."))
end
λ = model.λ
mu = n_max - n + 1
Expand Down
54 changes: 54 additions & 0 deletions test/MorsePotential.jl
Original file line number Diff line number Diff line change
Expand Up @@ -199,3 +199,57 @@ end

println("""```
""")


# 0 < Eₙ₊₁ - Eₙ for 0 ≤ n ≤ nₘₐₓ


println(raw"""
#### Recurrence Relation between $E_{n+1}$ and $E_n$
```math
\begin{equation}
\left\{ \,
\begin{aligned}
0 < \Delta E && 0 \leq n \leq n_\mathrm{max} \\
\Delta E < 0 && \mathrm{otherwise}
\end{aligned}
\right.
\end{equation}
```
```math
\Delta E = E_{n+1} - E_n
```
```math
n_\mathrm{max} = \left\lfloor\frac{2 D_{\mathrm{e}}-h \nu_0}{h \nu_0}\right\rfloor
```
```""")

@testset "0 < Eₙ₊₁ - Eₙ for 0 ≤ n ≤ nₘₐₓ" begin
println(" n Eₙ ΔE")
for n in 0:nₘₐₓ(MP)+5
Eₙ₊₁ = E(MP, n=n+1, nocheck=true)
Eₙ = E(MP, n=n, nocheck=true)
ΔE = Eₙ₊₁ - Eₙ
@printf("%2d %+9.6f %+9.6f ", n, Eₙ, ΔE)
if n nₘₐₓ(MP)
print("0 < ΔE ")
acceptance = 0 < ΔE
else
print("ΔE < 0 ")
acceptance = ΔE < 0
end
@test acceptance
println(acceptance ? "" : "")
if nₘₐₓ(MP) == n
println("----------------------------------- nₘₐₓ(MP) = ", nₘₐₓ(MP))
end
end
end

println("""```
""")

70 changes: 66 additions & 4 deletions test/PoschlTeller.jl
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,8 @@ are given by the sum of 2 Taylor series:

@testset "∫ψₙ*Hψₙdx = <ψₙ|H|ψₙ> = Eₙ" begin
ψHψ(PT, x; n=0, Δx=0.005) = V(PT,x)*ψ(PT,x,n=n)^2 - PT.^2/(2*PT.m)*conj(ψ(PT,x,n=n))*(ψ(PT,x+Δx,n=n)-2*ψ(PT,x,n=n)+ψ(PT,x-Δx,n=n))/Δx^2
println(" λ | n | analytical | numerical ")
println("--- | -- | ----------------- | ----------------- ")
println(" λ | n | m | ℏ | x₀ | analytical | numerical ")
println("--- | -- | --- | --- | --- | ----------------- | ----------------- ")
for λ in [1,2,3]
for n in 0:λ-1
for m in [1.0,exp(1)]
Expand All @@ -164,7 +164,7 @@ are given by the sum of 2 Taylor series:
numerical = quadgk(x -> ψHψ(PT, x, n=n, Δx=0.001), -Inf, Inf, atol=1e-4)[1]
acceptance = iszero(analytical) ? isapprox(analytical, numerical, atol=1e-3) : isapprox(analytical, numerical, rtol=1e-5)
@test acceptance
@printf("%.1f | %2d | %.1f |%.1f |%.1f |%17.12f | %17.12f %s\n", λ, n, m, ℏ, x₀, analytical, numerical, acceptance ? "" : "")
@printf("%.1f | %2d | %.1f | %.1f | %.1f |%17.12f | %17.12f %s\n", λ, n, m, ℏ, x₀, analytical, numerical, acceptance ? "" : "")
end
end
end
Expand All @@ -174,4 +174,66 @@ end
PT = PoschlTeller=1.0, m=1.0, ℏ=1.0)

println("""```
""")
""")


# 0 < Eₙ₊₁ - Eₙ for 0 ≤ n ≤ nₘₐₓ


println(raw"""
#### Recurrence Relation between $E_{n+1}$ and $E_n$
```math
\begin{equation}
\left\{ \,
\begin{aligned}
0 < \Delta E && 0 \leq n \leq n_\mathrm{max} \\
\Delta E < 0 && \mathrm{otherwise}
\end{aligned}
\right.
\end{equation}
```
```math
\Delta E = E_{n+1} - E_n
```
```math
n_\mathrm{max} = \left\lfloor \lambda \right\rfloor - 1
```
```""")

@testset "0 < Eₙ₊₁ - Eₙ for 0 ≤ n ≤ nₘₐₓ" begin
for PT in [
PoschlTeller=2, m=1.0, ℏ=1.0, x₀=1.0),
PoschlTeller=3, m=1.0, ℏ=1.0, x₀=1.0),
PoschlTeller=4, m=1.0, ℏ=1.0, x₀=1.0),
PoschlTeller=10, m=1.0, ℏ=1.0, x₀=1.0),
]
@show PT
println(" n Eₙ ΔE")
for n in 0:nₘₐₓ(PT)+5
Eₙ₊₁ = E(PT, n=n+1, nocheck=true)
Eₙ = E(PT, n=n, nocheck=true)
ΔE = Eₙ₊₁ - Eₙ
@printf("%2d %+9.6f %+9.6f ", n, Eₙ, ΔE)
if n nₘₐₓ(PT)
print("0 < ΔE ")
acceptance = 0 < ΔE
else
print("ΔE < 0 ")
acceptance = ΔE < 0
end
@test acceptance
println(acceptance ? "" : "")
if n == nₘₐₓ(PT)
println("----------------------------------- nₘₐₓ(PT) = ", nₘₐₓ(PT))
end
end
println()
end
end

println("""```
""")
50 changes: 50 additions & 0 deletions test/result/MorsePotential.log
Original file line number Diff line number Diff line change
Expand Up @@ -887,4 +887,54 @@ are given by the sum of 2 Taylor series:
0.1 | 9 | -0.026741858566 | -0.026742018376 ✔
```

#### Recurrence Relation between $E_{n+1}$ and $E_n$

```math
\begin{equation}
\left\{ \,
\begin{aligned}
0 < \Delta E && 0 \leq n \leq n_\mathrm{max} \\
\Delta E < 0 && \mathrm{otherwise}
\end{aligned}
\right.
\end{equation}
```

```math
\Delta E = E_{n+1} - E_n
```

```math
n_\mathrm{max} = \left\lfloor\frac{2 D_{\mathrm{e}}-h \nu_0}{h \nu_0}\right\rfloor
```

```
n Eₙ ΔE
0 -0.097414 +0.010033 0 < ΔE ✔
1 -0.087381 +0.009488 0 < ΔE ✔
2 -0.077893 +0.008943 0 < ΔE ✔
3 -0.068951 +0.008398 0 < ΔE ✔
4 -0.060553 +0.007852 0 < ΔE ✔
5 -0.052701 +0.007307 0 < ΔE ✔
6 -0.045393 +0.006762 0 < ΔE ✔
7 -0.038631 +0.006217 0 < ΔE ✔
8 -0.032414 +0.005672 0 < ΔE ✔
9 -0.026742 +0.005127 0 < ΔE ✔
10 -0.021615 +0.004582 0 < ΔE ✔
11 -0.017033 +0.004037 0 < ΔE ✔
12 -0.012996 +0.003492 0 < ΔE ✔
13 -0.009505 +0.002946 0 < ΔE ✔
14 -0.006558 +0.002401 0 < ΔE ✔
15 -0.004157 +0.001856 0 < ΔE ✔
16 -0.002301 +0.001311 0 < ΔE ✔
17 -0.000989 +0.000766 0 < ΔE ✔
18 -0.000223 +0.000221 0 < ΔE ✔
----------------------------------- nₘₐₓ(MP) = 18
19 -0.000002 -0.000324 ΔE < 0 ✔
20 -0.000327 -0.000869 ΔE < 0 ✔
21 -0.001196 -0.001414 ΔE < 0 ✔
22 -0.002610 -0.001959 ΔE < 0 ✔
23 -0.004570 -0.002505 ΔE < 0 ✔
```


Loading

0 comments on commit b33a273

Please sign in to comment.