Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multi-dispatch version #14

Merged
merged 6 commits into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "Antique"
uuid = "be6e5d0e-34a5-4c8f-af83-e1b5389203d8"
authors = ["Shuhei Ohno"]
version = "0.1.2"
version = "0.2.0"

[deps]
SpecialFunctions = "276daf66-3868-5448-9aa4-cd146d93841b"
Expand Down
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ using Pkg; Pkg.add("Antique")

## Usage & Examples

Install Antique.jl for the first use and run `using Antique` before each use. The function `antique(model, parameters...)` returns a module that has `E`, `ψ`, `V` and some other functions. Here are examples in hydrogen-like atom. The analytical notation of energy (eigen value of the Hamiltonian) is written as
Install Antique.jl for the first use and run `using Antique` before each use. The energy `E()`, wavefunction `ψ()`, potential `V()` and some other functions are suppoted. Here are examples in hydrogen-like atom. The analytical notation of energy (eigen value of the Hamiltonian) is written as

```math
E_n = -\frac{Z^2}{2n^2} E_\mathrm{h}.
Expand All @@ -26,28 +26,28 @@ Hydrogen atom has symbol $\mathrm{H}$ and atomic number 1 ($Z=1$). Therefore the

```julia
using Antique
H = antique(:HydrogenAtom, Z=1)
H.E(n=1)
H = HydrogenAtom(Z=1)
E(H)
# output> -0.5
```

Helium cation has symbol $\mathrm{He}^+$ and atomic number 2 ($Z=2$). Therefore the ground state ($n=1$) energy is $-2 E_\mathrm{h}$.

```julia
using Antique
He⁺ = antique(:HydrogenAtom, Z=2)
He⁺.E(n=1)
He⁺ = HydrogenAtom(Z=2)
E(He⁺)
# output> -2.0
```

There are more examples on each model page.

## Supported Models

- [Infinite Potential Well](https://ohno.github.io/Antique.jl/dev/InfinitePotentialWell/) `:InfinitePotentialWell`
- [Harmonic Oscillator](https://ohno.github.io/Antique.jl/dev/HarmonicOscillator/) `:HarmonicOscillator`
- [Morse Potential](https://ohno.github.io/Antique.jl/dev/MorsePotential/) `:MorsePotential`
- [Hydrogen Atom](https://ohno.github.io/Antique.jl/dev/HydrogenAtom/) `:HydrogenAtom`
- [Infinite Potential Well](https://ohno.github.io/Antique.jl/dev/InfinitePotentialWell/) `InfinitePotentialWell`
- [Harmonic Oscillator](https://ohno.github.io/Antique.jl/dev/HarmonicOscillator/) `HarmonicOscillator`
- [Morse Potential](https://ohno.github.io/Antique.jl/dev/MorsePotential/) `MorsePotential`
- [Hydrogen Atom](https://ohno.github.io/Antique.jl/dev/HydrogenAtom/) `HydrogenAtom`

## Future Works

Expand Down
57 changes: 51 additions & 6 deletions developer/dev.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,66 @@
"name": "stderr",
"output_type": "stream",
"text": [
"\u001b[32m\u001b[1m Activating\u001b[22m\u001b[39m new project at `C:\\Users\\user\\Desktop\\GitHub\\Antique.jl\\developer`\n"
"\u001b[32m\u001b[1m Activating\u001b[22m\u001b[39m project at `C:\\Users\\user\\Desktop\\Antique.jl`\n",
"\u001b[36m\u001b[1m[ \u001b[22m\u001b[39m\u001b[36m\u001b[1mInfo: \u001b[22m\u001b[39mPrecompiling Antique [be6e5d0e-34a5-4c8f-af83-e1b5389203d8]\n"
]
}
],
"source": [
"include(\"revice.jl\")"
"# run `include(\"./developer/revice.jl\")`\n",
"using Pkg\n",
"Pkg.instantiate()\n",
"# Pkg.add(\"Revise\")\n",
"using Revise\n",
"Pkg.activate(\"../\")\n",
"using Antique"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "97a634d3",
"execution_count": 2,
"id": "526ca10f",
"metadata": {},
"outputs": [],
"source": []
"outputs": [
{
"data": {
"text/plain": [
"-0.5"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"using Antique\n",
"H = HydrogenAtom(Z=1)\n",
"E(H)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "4e1782eb",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"-2.0"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"using Antique\n",
"He⁺ = HydrogenAtom(Z=2)\n",
"E(He⁺)"
]
}
],
"metadata": {
Expand Down
2 changes: 1 addition & 1 deletion docs/jmd2md.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Antique
using Weave

for file in Antique.models # [:InfinitePotentialWell]
for file in Antique.models # [:InfinitePotentialWell :HarmonicOscillator :MorsePotential :HydrogenAtom]
weave("./src/jmd/$file.jmd", doctype="github", out_path="./src/", fig_path="./assets/fig/")
text = Antique.load("./src/$file.md")
# remove ``` after include(...s)
Expand Down
36 changes: 18 additions & 18 deletions docs/src/HarmonicOscillator.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ The harmonic oscillator is the most frequently used model in quantum physics.
```

#### Potential
`V(x; k=k, m=m, ω=sqrt(k/m))`
`V(model::HarmonicOscillator; x)`
```math
V(x)
= \frac{1}{2} k x^2
Expand All @@ -34,19 +34,19 @@ The harmonic oscillator is the most frequently used model in quantum physics.
```

#### Eigen Values
`E(n; k, m, ω=sqrt(k/m), ℏ=ℏ)`
`E(model::HarmonicOscillator; n=0)`
```math
E_n = \hbar \omega \left( n + \frac{1}{2} \right)
```

#### Eigen Functions
`ψ(n, x; k=k, m=m, ω=sqrt(k/m), ℏ=ℏ)`
`ψ(model::HarmonicOscillator, x; n=0)`
```math
\psi_n(x) = A_n H_n(\xi) \exp{\left( -\frac{\xi^2}{2} \right)}
```

#### Hermite Polynomials
`H(x; n=0)`
`H(model::HarmonicOscillator, x; n=0)`

Rodrigues' formula & closed-form:
```math
Expand Down Expand Up @@ -88,11 +88,11 @@ Examples:

## Usage & Examples

[Install Antique.jl](@ref Install) for the first use and run `using Antique` before each use. The function `antique(model, parameters...)` returns a module that has `E()`, `ψ(x)`, `V(x)` and some other functions. In this system, the model name is specified by `:HarmonicOscillator` and several parameters `k`, `m` and `ℏ` are set as optional arguments.
[Install Antique.jl](@ref Install) for the first use and run `using Antique` before each use. The energy `E()`, wavefunction `ψ()`, potential `V()` and some other functions are suppoted. In this system, the model is generated by `HarmonicOscillator` and several parameters `k`, `m` and `ℏ` are set as optional arguments.

```julia
using Antique
HO = antique(:HarmonicOscillator, k=1.0, m=1.0, ℏ=1.0)
HO = HarmonicOscillator(k=1.0, m=1.0, ℏ=1.0)
```


Expand All @@ -116,10 +116,10 @@ julia> HO.ℏ
Eigen values:

```julia
julia> HO.E(n=0)
julia> E(HO, n=0)
0.5

julia> HO.E(n=1)
julia> E(HO, n=1)
1.5
```

Expand All @@ -129,7 +129,7 @@ Potential energy curve:

```julia
using Plots
plot(-5:0.1:5, x -> HO.V(x), lw=2, label="", xlabel="x", ylabel="V(x)")
plot(-5:0.1:5, x -> V(HO, x), lw=2, label="", xlabel="x", ylabel="V(x)")
```

![](./assets/fig//HarmonicOscillator_4_1.png)
Expand All @@ -141,11 +141,11 @@ Wave functions:
```julia
using Plots
plot(xlim=(-5,5), xlabel="x", ylabel="ψ(x)")
plot!(x -> HO.ψ(x, n=0), label="n=0", lw=2)
plot!(x -> HO.ψ(x, n=1), label="n=1", lw=2)
plot!(x -> HO.ψ(x, n=2), label="n=2", lw=2)
plot!(x -> HO.ψ(x, n=3), label="n=3", lw=2)
plot!(x -> HO.ψ(x, n=4), label="n=4", lw=2)
plot!(x -> ψ(HO, x, n=0), label="n=0", lw=2)
plot!(x -> ψ(HO, x, n=1), label="n=1", lw=2)
plot!(x -> ψ(HO, x, n=2), label="n=2", lw=2)
plot!(x -> ψ(HO, x, n=3), label="n=3", lw=2)
plot!(x -> ψ(HO, x, n=4), label="n=4", lw=2)
```

![](./assets/fig//HarmonicOscillator_5_1.png)
Expand All @@ -159,13 +159,13 @@ using Plots
plot(xlim=(-5.5,5.5), ylim=(-0.2,5.4), xlabel="\$x\$", ylabel="\$V(x),~E_n,~\\psi_n(x)\\times0.5+E_n\$", size=(480,400), dpi=300)
for n in 0:4
# energy
hline!([HO.E(n=n)], lc=:black, ls=:dash, label="")
plot!([-sqrt(2*HO.k*HO.E(n=n)),sqrt(2*HO.k*HO.E(n=n))], fill(HO.E(n=n),2), lc=:black, lw=2, label="")
hline!([E(HO, n=n)], lc=:black, ls=:dash, label="")
plot!([-sqrt(2*HO.k*E(HO, n=n)),sqrt(2*HO.k*E(HO, n=n))], fill(E(HO, n=n),2), lc=:black, lw=2, label="")
# wave function
plot!(x -> HO.E(n=n) + 0.5*HO.ψ(x,n=n), lc=n+1, lw=2, label="")
plot!(x -> E(HO, n=n) + 0.5*ψ(HO, x,n=n), lc=n+1, lw=2, label="")
end
# potential
plot!(x -> HO.V(x), lc=:black, lw=2, label="")
plot!(x -> V(HO, x), lc=:black, lw=2, label="")
```

![](./assets/fig//HarmonicOscillator_6_1.png)
Expand Down
36 changes: 18 additions & 18 deletions docs/src/HydrogenAtom.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ The hydrogen atom is the simplest 2-body Coulomb system.
Where, $\mu=\left(\frac{1}{m_\mathrm{e}}+\frac{1}{m_\mathrm{p}}\right)^{-1}$ is the reduced mass of electron $\mathrm{e}$ and proton $\mathrm{p}$. $\mu = m_\mathrm{e}$ holds in the limit $m_\mathrm{p}\rightarrow\infty$.

#### Potential
`V(r; Z=Z, a₀=a₀)`
`V(model::HydrogenAtom, r)`
```math
\begin{aligned}
V(r)
Expand All @@ -33,27 +33,27 @@ Where, $\mu=\left(\frac{1}{m_\mathrm{e}}+\frac{1}{m_\mathrm{p}}\right)^{-1}$ is
```

#### Eigen Values
`E(; n=1, Z=Z, Eₕ=Eₕ)`
`E(model::HydrogenAtom; n=1)`
```math
E_n = -\frac{m_\mathrm{e} e^4 Z^2}{2n^2(4\pi\varepsilon_0)^2\hbar^2} = -\frac{Z^2}{2n^2} E_\mathrm{h}
```
Where, ``E_\mathrm{h}`` is the Hartree energy, one of atomic unit. About atomic units, see section 3.9.2 of the [IUPAC GreenBook](https://iupac.org/what-we-do/books/greenbook/). In other units, ``E_\mathrm{h} = 27.211~386~245~988(53)~\mathrm{eV}`` from [here](https://physics.nist.gov/cgi-bin/cuu/Value?hrev).

#### Eigen Functions
`ψ(r, θ, φ; n=1, l=0, m=0, Z=Z, a₀=a₀)`
`ψ(model::HydrogenAtom, r, θ, φ; n=1, l=0, m=0)`
```math
\psi_{nlm}(\pmb{r}) = R_{nl}(r) Y_{lm}(\theta,\varphi)
```

#### Radial Functions
`R(r; n=1, l=0, Z=Z, a₀=a₀)`
`R(model::HydrogenAtom, r; n=1, l=0)`
```math
R_{nl}(r) = -\sqrt{\frac{(n-l-1)!}{2n(n+l)!} \left(\frac{2Z}{n a_0}\right)^3} \left(\frac{2Zr}{n a_0}\right)^l \exp \left(-\frac{Zr}{n a_0}\right) L_{n+l}^{2l+1} \left(\frac{2Zr}{n a_0}\right)
```
Where, Laguerre polynomials are defined as ``L_n(x) = \frac{1}{n!} \mathrm{e}^x \frac{\mathrm{d}^n}{\mathrm{d}x ^n} \left( \mathrm{e}^{-x} x^n \right)``, and associated Laguerre polynomials are defined as ``L_n^{k}(x) = \frac{\mathrm{d}^k}{\mathrm{d}x^k} L_n(x)``. Note that, replace ``2n(n+l)!`` with ``2n[(n+l)!]^3`` if Laguerre polynomials are defined as ``L_n(x) = \mathrm{e}^x \frac{\mathrm{d}^n}{\mathrm{d}x ^n} \left( \mathrm{e}^{-x} x^n \right)``.

#### Associated Laguerre Polynomials
`L(x; n=0, k=0)`
`L(model::HydrogenAtom, x; n=0, k=0)`

Rodrigues' formula & closed-form:
```math
Expand Down Expand Up @@ -91,7 +91,7 @@ Examples:
```

#### Spherical Harmonics
`Y(θ, φ; l=0, m=0)`
`Y(model::HydrogenAtom, θ, φ; l=0, m=0)`
```math
Y_{lm}(\theta,\varphi) = (-1)^{\frac{|m|+m}{2}} \sqrt{\frac{2l+1}{4\pi} \frac{(l-|m|)!}{(l+|m|)!}} P_l^{|m|} (\cos\theta) \mathrm{e}^{im\varphi}
```
Expand All @@ -101,7 +101,7 @@ i^{|m|+m} \sqrt{\frac{(l-|m|)!}{(l+|m|)!}} P_l^{|m|} = (-1)^{\frac{|m|+m}{2}} \s
```

#### Associated Legendre Polynomials
`P(x; n=0, m=0)`
`P(model::HydrogenAtom, x; n=0, m=0)`

Rodrigues' formula & closed-form:
```math
Expand Down Expand Up @@ -151,11 +151,11 @@ Examples:

## Usage & Examples

[Install Antique.jl](@ref Install) for the first use and run `using Antique` before each use. The function `antique(model, parameters...)` returns a module that has `E()`, `ψ(r)`, `V(r)` and some other functions. In this system, the model name is specified by `:HydrogenAtom` and several parameters `Z`, `Eₕ`, `mₑ`, `a₀` and `ℏ` are set as optional arguments.
[Install Antique.jl](@ref Install) for the first use and run `using Antique` before each use. The energy `E()`, wavefunction `ψ()`, potential `V()` and some other functions are suppoted. In this system, the model is generated by `HydrogenAtom` and several parameters `Z`, `Eₕ`, `mₑ`, `a₀` and `ℏ` are set as optional arguments.

```julia
using Antique
H = antique(:HydrogenAtom, Z=1, Eₕ=1.0, a₀=1.0, mₑ=1.0, ℏ=1.0)
H = HydrogenAtom(Z=1, Eₕ=1.0, a₀=1.0, mₑ=1.0, ℏ=1.0)
```


Expand Down Expand Up @@ -185,10 +185,10 @@ julia> H.ℏ
Eigen values:

```julia
julia> H.E(n=1)
julia> E(H, n=1)
-0.5

julia> H.E(n=2)
julia> E(H, n=2)
-0.125
```

Expand All @@ -198,8 +198,8 @@ Wave length ($n=2\rightarrow1$, the first line of the Lyman series):

```julia
Eₕ2nm⁻¹ = 2.1947463136320e-2 # https://physics.nist.gov/cgi-bin/cuu/CCValue?hrminv
println("ΔE = ", H.E(n=2) - H.E(n=1), " Eₕ")
println("λ = ", ((H.E(n=2)-H.E(n=1))*Eₕ2nm⁻¹)^-1, " nm")
println("ΔE = ", E(H,n=2) - E(H,n=1), " Eₕ")
println("λ = ", ((E(H,n=2)-E(H,n=1))*Eₕ2nm⁻¹)^-1, " nm")
```

```
Expand All @@ -226,7 +226,7 @@ ge = 2.00231930436256 # https://physics.nist.gov/cgi-bin/cuu/Value?gem
gp = 5.5856946893 # https://physics.nist.gov/cgi-bin/cuu/Value?gp

# calculation: https://doi.org/10.1119/1.12733
δ = abs(H.ψ(0,0,0))^2
δ = abs(ψ(H,0,0,0))^2
ΔE = 2 / 3 * µ0 * µN * µB * gp * ge * δ * a0^(-3)
println("1/π = ", 1/π)
println("<δ(r)> = ", δ, " a₀⁻³")
Expand Down Expand Up @@ -254,7 +254,7 @@ Potential energy curve:
```julia
using Plots
plot(xlims=(0.0,15.0), ylims=(-0.6,0.05), xlabel="\$r~/~a_0\$", ylabel="\$V(r)/E_\\mathrm{h},~E_n/E_\\mathrm{h}\$", legend=:bottomright, size=(480,400))
plot!(0.1:0.01:15, r -> H.V(r), lc=:black, lw=2, label="") # potential
plot!(0.1:0.01:15, r -> V(H,r), lc=:black, lw=2, label="") # potential
```

![](./assets/fig//HydrogenAtom_6_1.png)
Expand All @@ -267,9 +267,9 @@ Potential energy curve, Energy levels:
using Plots
plot(xlims=(0.0,15.0), ylims=(-0.6,0.05), xlabel="\$r~/~a_0\$", ylabel="\$V(r)/E_\\mathrm{h}\$", legend=:bottomright, size=(480,400))
for n in 0:10
plot!(0.0:0.01:15, r -> H.E(n=n) > H.V(r) ? H.E(n=n) : NaN, lc=n, lw=1, label="") # energy level
plot!(0.0:0.01:15, r -> E(H,n=n) > V(H,r) ? E(H,n=n) : NaN, lc=n, lw=1, label="") # energy level
end
plot!(0.1:0.01:15, r -> H.V(r), lc=:black, lw=2, label="") # potential
plot!(0.1:0.01:15, r -> V(H,r), lc=:black, lw=2, label="") # potential
```

![](./assets/fig//HydrogenAtom_7_1.png)
Expand All @@ -283,7 +283,7 @@ using Plots
plot(xlabel="\$r~/~a_0\$", ylabel="\$r^2|R_{nl}(r)|^2~/~a_0^{-1}\$", ylims=(-0.01,0.55), xticks=0:1:20, size=(480,400), dpi=300)
for n in 1:3
for l in 0:n-1
plot!(0:0.01:20, r->r^2*H.R(r,n=n,l=l)^2, lc=n, lw=2, ls=[:solid,:dash,:dot,:dashdot,:dashdotdot][l+1], label="\$n = $n, l=$l\$")
plot!(0:0.01:20, r->r^2*R(H,r,n=n,l=l)^2, lc=n, lw=2, ls=[:solid,:dash,:dot,:dashdot,:dashdotdot][l+1], label="\$n = $n, l=$l\$")
end
end
plot!()
Expand Down
Loading
Loading