diff --git a/src/Antique.jl b/src/Antique.jl index 335cae1..c63423e 100644 --- a/src/Antique.jl +++ b/src/Antique.jl @@ -1,5 +1,8 @@ module Antique + # for Julia 1.1 + import Base:@kwdef + # Export public functions export energy export potential diff --git a/src/CoulombTwoBody.jl b/src/CoulombTwoBody.jl index 28018f3..82770c3 100644 --- a/src/CoulombTwoBody.jl +++ b/src/CoulombTwoBody.jl @@ -1,45 +1,23 @@ module CoulombTwoBodies +# for Julia 1.1 +import Base:@kwdef + import ..AbstractModel import ..energy, ..potential, ..wavefunction, ..radial_function, ..laguerre_polynomial, ..spherical_harmonic, ..legendre_polynomial export CoulombTwoBody, energy, potential, wavefunction, radial_function, laguerre_polynomial, spherical_harmonic, legendre_polynomial # parameters -struct CoulombTwoBody <: AbstractModel - z_1::Integer - z_2::Integer - m_1::Real - m_2::Real - m_e::Real - a_0::Real - E_h::Real - hbar::Real -end - -function CoulombTwoBody(; - z_1::Integer=-1, z₁=z_1, - z_2::Integer=1, z₂=z_2, - m_1=1.0, m₁=m_1, - m_2=1.0, m₂=m_2, - m_e=1.0, mₑ=m_e, - a_0=1.0, a₀=a_0, - E_h=1.0, Eₕ=E_h, - hbar=1.0, ℏ=hbar, -) - return CoulombTwoBody(z₁, z₂, m₁, m₂, mₑ, a₀, Eₕ, ℏ) -end - -function Base.getproperty(model::CoulombTwoBody, sym::Symbol) - sym === :z₁ && return getfield(model, :z_1) - sym === :z₂ && return getfield(model, :z_2) - sym === :m₁ && return getfield(model, :m_1) - sym === :m₂ && return getfield(model, :m_2) - sym === :mₑ && return getfield(model, :m_e) - sym === :a₀ && return getfield(model, :a_0) - sym === :Eₕ && return getfield(model, :E_h) - sym === :ℏ && return getfield(model, :hbar) - return getfield(model, sym) +@kwdef struct CoulombTwoBody <: AbstractModel + z_1::Integer = -1 + z_2::Integer = 1 + m_1::Real = 1.0 + m_2::Real = 1.0 + m_e::Real = 1.0 + a_0::Real = 1.0 + E_h::Real = 1.0 + hbar::Real = 1.0 end # potential diff --git a/src/DeltaPotential.jl b/src/DeltaPotential.jl index 8d58299..b589c3b 100644 --- a/src/DeltaPotential.jl +++ b/src/DeltaPotential.jl @@ -1,29 +1,18 @@ module DeltaPotentials +# for Julia 1.1 +import Base:@kwdef + import ..AbstractModel import ..energy, ..potential, ..wavefunction export DeltaPotential, energy, potential, wavefunction # parameters -struct DeltaPotential <: AbstractModel - alpha::Real - m::Real - hbar::Real -end - -function DeltaPotential(; - alpha=1.0, α=alpha, - m=1.0, - hbar=1.0, ℏ=hbar, -) - return DeltaPotential(α, m, ℏ) -end - -function Base.getproperty(model::DeltaPotential, sym::Symbol) - sym === :α && return getfield(model, :alpha) - sym === :ℏ && return getfield(model, :hbar) - return getfield(model, sym) +@kwdef struct DeltaPotential <: AbstractModel + alpha::Real = 1.0 + m::Real = 1.0 + hbar::Real = 1.0 end # potential diff --git a/src/HarmonicOscillator.jl b/src/HarmonicOscillator.jl index 54fc8e5..4fa4b38 100644 --- a/src/HarmonicOscillator.jl +++ b/src/HarmonicOscillator.jl @@ -1,28 +1,18 @@ module HarmonicOscillators +# for Julia 1.1 +import Base:@kwdef + import ..AbstractModel import ..energy, ..potential, ..wavefunction, ..laguerre_polynomial export HarmonicOscillator, energy, potential, wavefunction, laguerre_polynomial # parameters -struct HarmonicOscillator <: AbstractModel - k::Real - m::Real - hbar::Real -end - -function HarmonicOscillator(; - k=1.0, - m=1.0, - hbar=1.0, ℏ=hbar, -) - return HarmonicOscillator(k, m, ℏ) -end - -function Base.getproperty(model::HarmonicOscillator, sym::Symbol) - sym === :ℏ && return getfield(model, :hbar) - return getfield(model, sym) +@kwdef struct HarmonicOscillator <: AbstractModel + k::Real = 1.0 + m::Real = 1.0 + hbar::Real = 1.0 end # potential diff --git a/src/HydrogenAtom.jl b/src/HydrogenAtom.jl index 824ea31..ebe522c 100644 --- a/src/HydrogenAtom.jl +++ b/src/HydrogenAtom.jl @@ -1,35 +1,20 @@ module HydrogenAtoms +# for Julia 1.1 +import Base:@kwdef + import ..AbstractModel import ..energy, ..potential, ..wavefunction, ..radial_function, ..laguerre_polynomial, ..spherical_harmonic, ..legendre_polynomial export HydrogenAtom, energy, potential, wavefunction, radial_function, laguerre_polynomial, spherical_harmonic, legendre_polynomial # parameters -struct HydrogenAtom <: AbstractModel - Z::Integer - m_e::Real - a_0::Real - E_h::Real - hbar::Real -end - -function HydrogenAtom(; - Z::Integer=1, - m_e=1.0, mₑ=m_e, - a_0=1.0, a₀=a_0, - E_h=1.0, Eₕ=E_h, - hbar=1.0, ℏ=hbar, -) - return HydrogenAtom(Z, mₑ, a₀, Eₕ, ℏ) -end - -function Base.getproperty(model::HydrogenAtom, sym::Symbol) - sym === :mₑ && return getfield(model, :m_e) - sym === :a₀ && return getfield(model, :a_0) - sym === :Eₕ && return getfield(model, :E_h) - sym === :ℏ && return getfield(model, :hbar) - return getfield(model, sym) +@kwdef struct HydrogenAtom <: AbstractModel + Z::Integer = 1 + m_e::Real = 1.0 + a_0::Real = 1.0 + E_h::Real = 1.0 + hbar::Real = 1.0 end # potential diff --git a/src/InfinitePotentialWell.jl b/src/InfinitePotentialWell.jl index ebf0ede..5fafdda 100644 --- a/src/InfinitePotentialWell.jl +++ b/src/InfinitePotentialWell.jl @@ -1,28 +1,18 @@ module InfinitePotentialWells +# for Julia 1.1 +import Base:@kwdef + import ..AbstractModel import ..energy, ..potential, ..wavefunction export InfinitePotentialWell, energy, potential, wavefunction # parameters -struct InfinitePotentialWell <: AbstractModel - L::Real - m::Real - hbar::Real -end - -function InfinitePotentialWell(; - L=1.0, - m=1.0, - hbar=1.0, ℏ=hbar, -) - return InfinitePotentialWell(L, m, ℏ) -end - -function Base.getproperty(model::InfinitePotentialWell, sym::Symbol) - sym === :ℏ && return getfield(model, :hbar) - return getfield(model, sym) +@kwdef struct InfinitePotentialWell <: AbstractModel + L::Real = 1.0 + m::Real = 1.0 + hbar::Real = 1.0 end # potential diff --git a/src/InfinitePotentialWell3D.jl b/src/InfinitePotentialWell3D.jl index fa2cf7a..9d97966 100644 --- a/src/InfinitePotentialWell3D.jl +++ b/src/InfinitePotentialWell3D.jl @@ -1,28 +1,18 @@ module InfinitePotentialWell3Ds +# for Julia 1.1 +import Base:@kwdef + import ..AbstractModel import ..energy, ..potential, ..wavefunction export InfinitePotentialWell3D, energy, potential, wavefunction # parameters -struct InfinitePotentialWell3D <: AbstractModel - L::Vector{Real} - m::Real - hbar::Real -end - -function InfinitePotentialWell3D(; - L=[1.0, 1.0, 1.0], - m=1.0, - hbar=1.0, ℏ=hbar, -) - return InfinitePotentialWell3D(Float64.(L), m, ℏ) -end - -function Base.getproperty(model::InfinitePotentialWell3D, sym::Symbol) - sym === :ℏ && return getfield(model, :hbar) - return getfield(model, sym) +@kwdef struct InfinitePotentialWell3D <: AbstractModel + L::Vector{Real} = [1.0, 1.0, 1.0] + m::Real = 1.0 + hbar::Real = 1.0 end # potential diff --git a/src/MorsePotential.jl b/src/MorsePotential.jl index b479223..0eaf53c 100644 --- a/src/MorsePotential.jl +++ b/src/MorsePotential.jl @@ -1,5 +1,8 @@ module MorsePotentials +# for Julia 1.1 +import Base:@kwdef + import ..AbstractModel import ..energy, ..potential, ..wavefunction, ..n_max, ..laguerre_polynomial @@ -9,33 +12,12 @@ export MorsePotential, energy, potential, wavefunction, n_max, laguerre_polynomi using SpecialFunctions # parameters -struct MorsePotential <: AbstractModel - r_e::Real - D_e::Real - k::Real - mu::Real - hbar::Real -end - -function MorsePotential(; - # The simplified parameters for H2+ - # F. M. Fernandez, J. Garcia, ChemistrySelect, 6, 9527-9534(2021) https://doi.org/10.1002/slct.202102509 - # CODATA recommended values of the fundamental physical constants: 2018 https://physics.nist.gov/cgi-bin/cuu/Value?mpsme - r_e=2.0, rₑ=r_e, - D_e=0.1, Dₑ=D_e, - k=0.1, - mu=918.1, μ=mu, - hbar=1.0, ℏ=hbar, -) - return MorsePotential(rₑ, Dₑ, k, μ, ℏ) -end - -function Base.getproperty(model::MorsePotential, sym::Symbol) - sym === :rₑ && return getfield(model, :r_e) - sym === :Dₑ && return getfield(model, :D_e) - sym === :μ && return getfield(model, :mu) - sym === :ℏ && return getfield(model, :hbar) - return getfield(model, sym) +@kwdef struct MorsePotential <: AbstractModel + r_e::Real = 2.0 + D_e::Real = 0.1 + k::Real = 0.1 + mu::Real = 918.1 + hbar::Real = 1.0 end # potential diff --git a/src/PoschlTeller.jl b/src/PoschlTeller.jl index 26d0646..7a56ed6 100644 --- a/src/PoschlTeller.jl +++ b/src/PoschlTeller.jl @@ -1,5 +1,8 @@ module PoschlTellers +# for Julia 1.1 +import Base:@kwdef + import ..AbstractModel import ..energy, ..potential, ..wavefunction, ..n_max, ..legendre_polynomial @@ -9,30 +12,11 @@ export PoschlTeller, energy, potential, wavefunction, n_max, legendre_polynomial using SpecialFunctions # parameters -struct PoschlTeller <: AbstractModel - lambda::Integer - m::Real - hbar::Real - x_0::Real -end - -function PoschlTeller(; - lambda=1, λ=lambda, - m=1.0, - hbar=1.0, ℏ=hbar, - x_0=1.0, x₀=x_0, -) - if !isinteger(λ) - throw(DomainError("λ = $λ", "λ must be an integer.")) - end - return PoschlTeller(Int(λ), m, ℏ, x₀) -end - -function Base.getproperty(model::PoschlTeller, sym::Symbol) - sym === :λ && return getfield(model, :lambda) - sym === :x₀ && return getfield(model, :x_0) - sym === :ℏ && return getfield(model, :hbar) - return getfield(model, sym) +@kwdef struct PoschlTeller <: AbstractModel + lambda::Integer = 1 + m::Real = 1.0 + hbar::Real = 1.0 + x_0::Real = 1.0 end # potential diff --git a/src/RigidRotor.jl b/src/RigidRotor.jl index aec7f48..46b784a 100644 --- a/src/RigidRotor.jl +++ b/src/RigidRotor.jl @@ -1,32 +1,19 @@ module RigidRotors +# for Julia 1.1 +import Base:@kwdef + import ..AbstractModel import ..energy, ..potential, ..wavefunction, ..spherical_harmonic, ..legendre_polynomial export RigidRotor, energy, potential, wavefunction, spherical_harmonic, legendre_polynomial # parameters -struct RigidRotor <: AbstractModel - m_1::Real - m_2::Real - R::Real - hbar::Real -end - -function RigidRotor(; - m_1=1.0, m₁=m_1, - m_2=1.0, m₂=m_2, - R=1.0, - hbar=1.0, ℏ=hbar, -) - return RigidRotor(m₁, m₂, R, ℏ) -end - -function Base.getproperty(model::RigidRotor, sym::Symbol) - sym === :m₁ && return getfield(model, :m_1) - sym === :m₂ && return getfield(model, :m_2) - sym === :ℏ && return getfield(model, :hbar) - return getfield(model, sym) +@kwdef struct RigidRotor <: AbstractModel + m_1::Real = 1.0 + m_2::Real = 1.0 + R::Real = 1.0 + hbar::Real = 1.0 end # potential diff --git a/src/SphericalOscillator.jl b/src/SphericalOscillator.jl index b9b0c8e..f86a5fd 100644 --- a/src/SphericalOscillator.jl +++ b/src/SphericalOscillator.jl @@ -1,5 +1,8 @@ module SphericalOscillators +# for Julia 1.1 +import Base:@kwdef + import ..AbstractModel import ..energy, ..potential, ..wavefunction, ..radial_function, ..laguerre_polynomial, ..spherical_harmonic, ..legendre_polynomial @@ -9,24 +12,10 @@ export SphericalOscillator, energy, potential, wavefunction, radial_function, la using SpecialFunctions # parameters -struct SphericalOscillator <: AbstractModel - k::Real - mu::Real - hbar::Real -end - -function SphericalOscillator(; - k=1.0, - mu=1.0, μ=mu, - hbar=1.0, ℏ=hbar, -) - return SphericalOscillator(k, μ, ℏ) -end - -function Base.getproperty(model::SphericalOscillator, sym::Symbol) - sym === :μ && return getfield(model, :mu) - sym === :ℏ && return getfield(model, :hbar) - return getfield(model, sym) +@kwdef struct SphericalOscillator <: AbstractModel + k::Real = 1.0 + mu::Real = 1.0 + hbar::Real = 1.0 end # potential diff --git a/test/result/InfinitePotentialWell3D.md b/test/result/InfinitePotentialWell3D.md index f50c4c2..8181fd1 100644 --- a/test/result/InfinitePotentialWell3D.md +++ b/test/result/InfinitePotentialWell3D.md @@ -5,7 +5,7 @@ ``` ``` -IPW3D = Antique.InfinitePotentialWell3D(L=[1.0, 1.0, 1.0], m=1.0, hbar=1.0) +IPW3D = Antique.InfinitePotentialWell3D(L=Real[1.0, 1.0, 1.0], m=1.0, hbar=1.0) ix | iy | iz | jx | jy | jz | analytical | numerical -- | -- | -- | -- | -- | -- | -------------- | -------------- 1 | 1 | 1 | 1 | 1 | 1 | 1.000000000 | 0.999960857 ✔ @@ -73,7 +73,7 @@ ix | iy | iz | jx | jy | jz | analytical | numerical 2 | 2 | 2 | 2 | 2 | 1 | 0.000000000 | -0.000001072 ✔ 2 | 2 | 2 | 2 | 2 | 2 | 1.000000000 | 1.003329100 ✔ -IPW3D = Antique.InfinitePotentialWell3D(L=[1.2, 3.4, 4.5], m=2.0, hbar=3.0) +IPW3D = Antique.InfinitePotentialWell3D(L=Real[1.2, 3.4, 4.5], m=2.0, hbar=3.0) ix | iy | iz | jx | jy | jz | analytical | numerical -- | -- | -- | -- | -- | -- | -------------- | -------------- 1 | 1 | 1 | 1 | 1 | 1 | 1.000000000 | 0.999991924 ✔ @@ -205,7 +205,7 @@ are given by the sum of 2 Taylor series: \end{aligned} ``` ``` -IPW3D = Antique.InfinitePotentialWell3D(L=[1.0, 1.0, 1.0], m=1.0, hbar=1.0) +IPW3D = Antique.InfinitePotentialWell3D(L=Real[1.0, 1.0, 1.0], m=1.0, hbar=1.0) nx | ny | nz | analytical | numerical -- | -- | -- | -------------- | -------------- 1 | 1 | 1 | 14.803827112 | 14.804406602 ✔ @@ -217,7 +217,7 @@ IPW3D = Antique.InfinitePotentialWell3D(L=[1.0, 1.0, 1.0], m=1.0, hbar=1.0) 2 | 2 | 1 | 44.411710033 | 44.413219805 ✔ 2 | 2 | 2 | 59.414767779 | 59.217626407 ✔ -IPW3D = Antique.InfinitePotentialWell3D(L=[1.2, 3.4, 4.5], m=2.0, hbar=3.0) +IPW3D = Antique.InfinitePotentialWell3D(L=Real[1.2, 3.4, 4.5], m=2.0, hbar=3.0) nx | ny | nz | analytical | numerical -- | -- | -- | -------------- | -------------- 1 | 1 | 1 | 18.438717690 | 18.438866604 ✔ diff --git a/test/runtests.jl b/test/runtests.jl index ceb6599..6ba2c82 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -12,6 +12,7 @@ using QuadGK using Zygote @testset verbose = true "Antique.jl" begin + @testset "Base.string" begin @test string(HarmonicOscillator(k=2.0, m=3.0, hbar=4.0)) == "Antique.HarmonicOscillator(k=2.0, m=3.0, hbar=4.0)" for model_name in Antique.models @@ -23,62 +24,6 @@ using Zygote end end - @testset "Field alias compatibility" begin - ha_ascii = HydrogenAtom(m_e=2.0, a_0=3.0, E_h=4.0, hbar=5.0) - @test ha_ascii.mₑ == 2.0 - @test ha_ascii.a₀ == 3.0 - @test ha_ascii.Eₕ == 4.0 - @test ha_ascii.ℏ == 5.0 - ha_unicode = HydrogenAtom(mₑ=6.0, a₀=7.0, Eₕ=8.0, ℏ=9.0) - @test ha_unicode.m_e == 6.0 - @test ha_unicode.a_0 == 7.0 - @test ha_unicode.E_h == 8.0 - @test ha_unicode.hbar == 9.0 - - harm = HarmonicOscillator(hbar=2.0) - @test harm.ℏ == 2.0 - - ctb = CoulombTwoBody(z_1=-2, z_2=3, m_1=1.1, m_2=2.2, m_e=3.3, a_0=4.4, E_h=5.5, hbar=6.6) - @test ctb.z₁ == -2 - @test ctb.z₂ == 3 - @test ctb.m₁ == 1.1 - @test ctb.m₂ == 2.2 - @test ctb.mₑ == 3.3 - @test ctb.a₀ == 4.4 - @test ctb.Eₕ == 5.5 - @test ctb.ℏ == 6.6 - - dp = DeltaPotential(alpha=2.5, hbar=3.5) - @test dp.α == 2.5 - @test dp.ℏ == 3.5 - - ipw = InfinitePotentialWell(hbar=2.5) - @test ipw.ℏ == 2.5 - - ipw3d = InfinitePotentialWell3D(hbar=2.5) - @test ipw3d.ℏ == 2.5 - - mp = MorsePotential(r_e=1.1, D_e=2.2, mu=3.3, hbar=4.4) - @test mp.rₑ == 1.1 - @test mp.Dₑ == 2.2 - @test mp.μ == 3.3 - @test mp.ℏ == 4.4 - - pt = PoschlTeller(lambda=4, x_0=2.5, hbar=3.5) - @test pt.λ == 4 - @test pt.x₀ == 2.5 - @test pt.ℏ == 3.5 - - rr = RigidRotor(m_1=1.2, m_2=2.3, hbar=3.4) - @test rr.m₁ == 1.2 - @test rr.m₂ == 2.3 - @test rr.ℏ == 3.4 - - so = SphericalOscillator(mu=2.2, hbar=3.3) - @test so.μ == 2.2 - @test so.ℏ == 3.3 - end - for model in Antique.models # [:InfinitePotentialWell3D, :SphericalOscillator, :HydrogenAtom, :CoulombTwoBody] include("./$(model).jl") end