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

Override display format #69

Open
ohno opened this issue Jul 2, 2024 · 2 comments
Open

Override display format #69

ohno opened this issue Jul 2, 2024 · 2 comments
Labels
enhancement New feature or request good first issue Good for newcomers

Comments

@ohno
Copy link
Owner

ohno commented Jul 2, 2024

I want to display variable names when displaying struct. Please refer to the discourse.

Current:

julia> HO = HarmonicOscillator(k=1.0, m=1.0, ℏ=1.0)
HarmonicOscillator(1.0, 1.0, 1.0)

Goal:

julia> HO = HarmonicOscillator(k=1.0, m=1.0, ℏ=1.0)
HarmonicOscillator(k=1.0, m=1.0, ℏ=1.0)

Overwriting dump() to Base.show() is enough if we ignore executability:

julia> Base.show(io::IO,model::HarmonicOscillator) = dump(model)

julia> HO = HarmonicOscillator(k=1.0, m=1.0, ℏ=1.0)
HarmonicOscillator
  k: Float64 1.0
  m: Float64 1.0
  ℏ: Float64 1.0

In any case, this struct contains information about the variable names and their values.

@ohno ohno added enhancement New feature or request good first issue Good for newcomers labels Jul 2, 2024
@ohno
Copy link
Owner Author

ohno commented Jul 6, 2024

Here is an example for the harmonic oscillator.

julia> Base.show(io::IO, model::HarmonicOscillator) = print(io, "HarmonicOscillator(" * join([ "$(symbol)=$(getproperty(model,symbol))" for symbol in fieldnames(HarmonicOscillator)], ", ") * ")")

julia> HO = HarmonicOscillator(k=1.0, m=1.0, ℏ=1.0)
HarmonicOscillator(k=1.0, m=1.0, ℏ=1.0)

We should add 2 lines to src/HarmonicOscillator.jl.

export HarmonicOscillator, V, E, ψ

+ # formatter
+ Base.show(io::IO, model::HarmonicOscillator) = print(io, "HarmonicOscillator(" * join([ "$(symbol)=$(getproperty(model,symbol))" for symbol in fieldnames(HarmonicOscillator)], ", ") * ")")

# parameters
@kwdef struct HarmonicOscillator
  k = 1.0
  m = 1.0
  ℏ = 1.0
end

@ohno
Copy link
Owner Author

ohno commented Jul 6, 2024

We may be able to avoid rewriting each file through metaprogramming. Add the following code to src/Antique.jl:

for model in Antique.models
    code = """
    Base.show(io::IO, model::$(model)) = print(io, \"$(model)(\" * join([ \"\$(symbol)=\$(getproperty(model,symbol))\" for symbol in fieldnames($(model))], \", \") * \")\")
    """
    code |> Meta.parse |> eval
    print(code)
end
Base.show(io::IO, model::InfinitePotentialWell) = print(io, "InfinitePotentialWell(" * join([ "$(symbol)=$(getproperty(model,symbol))" for symbol in fieldnames(InfinitePotentialWell)], ", ") * ")")
Base.show(io::IO, model::HarmonicOscillator) = print(io, "HarmonicOscillator(" * join([ "$(symbol)=$(getproperty(model,symbol))" for symbol in fieldnames(HarmonicOscillator)], ", ") * ")")
Base.show(io::IO, model::MorsePotential) = print(io, "MorsePotential(" * join([ "$(symbol)=$(getproperty(model,symbol))" for symbol in fieldnames(MorsePotential)], ", ") * ")")
Base.show(io::IO, model::HydrogenAtom) = print(io, "HydrogenAtom(" * join([ "$(symbol)=$(getproperty(model,symbol))" for symbol in fieldnames(HydrogenAtom)], ", ") * ")")
Base.show(io::IO, model::DeltaPotential) = print(io, "DeltaPotential(" * join([ "$(symbol)=$(getproperty(model,symbol))" for symbol in fieldnames(DeltaPotential)], ", ") * ")")
Base.show(io::IO, model::PoschlTeller) = print(io, "PoschlTeller(" * join([ "$(symbol)=$(getproperty(model,symbol))" for symbol in fieldnames(PoschlTeller)], ", ") * ")")
Base.show(io::IO, model::SphericalOscillator) = print(io, "SphericalOscillator(" * join([ "$(symbol)=$(getproperty(model,symbol))" for symbol in fieldnames(SphericalOscillator)], ", ") * ")")
Base.show(io::IO, model::RigidRotor) = print(io, "RigidRotor(" * join([ "$(symbol)=$(getproperty(model,symbol))" for symbol in fieldnames(RigidRotor)], ", ") * ")")
Base.show(io::IO, model::InfinitePotentialWell3D) = print(io, "InfinitePotentialWell3D(" * join([ "$(symbol)=$(getproperty(model,symbol))" for symbol in fieldnames(InfinitePotentialWell3D)], ", ") * ")")
Base.show(io::IO, model::CoulombTwoBody) = print(io, "CoulombTwoBody(" * join([ "$(symbol)=$(getproperty(model,symbol))" for symbol in fieldnames(CoulombTwoBody)], ", ") * ")")

These codes are working:

for model in Antique.models
    code = "$(model)()"
    println("julia> ", code)
    code |> Meta.parse |> eval |> println
    println()
end
julia> InfinitePotentialWell()
InfinitePotentialWell(L=1.0, m=1.0, ℏ=1.0)

julia> HarmonicOscillator()
HarmonicOscillator(k=1.0, m=1.0, ℏ=1.0)

julia> MorsePotential()
MorsePotential(rₑ=1.997193319969992, Dₑ=0.10263461910653993, k=0.1027265041900817, μ=918.076336715, ℏ=1.0)

julia> HydrogenAtom()
HydrogenAtom(Z=1, mₑ=1.0, a₀=1.0, Eₕ=1.0, ℏ=1.0)

julia> DeltaPotential()
DeltaPotential=1.0, m=1.0, ℏ=1.0)

julia> PoschlTeller()
PoschlTeller=1, m=1.0, ℏ=1.0, x₀=1.0)

julia> SphericalOscillator()
SphericalOscillator(k=1.0, μ=1.0, ℏ=1.0)

julia> RigidRotor()
RigidRotor(m₁=1.0, m₂=1.0, R=1.0, ℏ=1.0)

julia> InfinitePotentialWell3D()
InfinitePotentialWell3D(L=[1.0, 1.0, 1.0], m=1.0, ℏ=1.0)

julia> CoulombTwoBody()
CoulombTwoBody(z₁=-1, z₂=1, m₁=1.0, m₂=1.0, mₑ=1.0, a₀=1.0, Eₕ=1.0, ℏ=1.0)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

1 participant