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

Dimensionful quantities with Unitful.jl #19

Merged
merged 1 commit into from
Sep 15, 2018
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
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ via `elements["oxygen"]`, by symbol via `elements[:O]`, or by number via

Each element has fields `name`, `appearance`, `atomic_mass`, `boil`, `category`, `color`, `density`, `discovered_by`, `melt`, `molar_heat`, `named_by`, `number`, `period`, `phase`, `source`, `spectral_img`, `summary`, `symbol`, `xpos`, `ypos`, `shells`.

This data is pretty-printed when you look up an element in the Julia REPL.
All physical quantities are [unitful](https://github.com/ajkeller34/Unitful.jl).

The data is pretty-printed when you look up an element in the Julia REPL.
For example:
```jl
julia> elements["oxygen"]
Expand Down
1 change: 1 addition & 0 deletions REQUIRE
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
julia 0.6
Compat 0.33.0
Unitful
27 changes: 14 additions & 13 deletions src/PeriodicTable.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,23 @@ module PeriodicTable
export Element, elements

using Compat: replace
import Unitful: u, g, cm, K, J, mol, Quantity

"""
Element composite type
"""
mutable struct Element
name::String
appearance::String
atomic_mass::Float64
boil::Float64
atomic_mass::typeof(1.0u)
boil::typeof(1.0K)
category::String
color::String
density::Float64
density::typeof(1.0g/cm^3)
discovered_by::String
el_config::String
melt::Float64
molar_heat::Float64
melt::typeof(1.0K)
molar_heat::typeof(1.0J/(mol*K))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this reads nice, did not know this works

named_by::String
number::Int
period::Int
Expand All @@ -43,15 +44,15 @@ end

Element(; name::AbstractString="",
appearance::AbstractString="",
atomic_mass::Real=NaN,
boil::Real=NaN,
atomic_mass::typeof(1.0u)=NaN*u,
boil::typeof(1.0K)=NaN*K,
category::AbstractString="",
color::AbstractString="",
density::Real=NaN,
density::typeof(1.0g/cm^3)=NaN*g/cm^3,
discovered_by::AbstractString="",
el_config::AbstractString="",
melt::Real=NaN,
molar_heat::Real=NaN,
melt::typeof(1.0K)=NaN*K,
molar_heat::typeof(1.0J/(mol*K))=NaN*J/(mol*K),
named_by::AbstractString="",
number::Integer=-1,
period::Integer=-1,
Expand All @@ -70,11 +71,11 @@ Element(; name::AbstractString="",
Base.show(io::IO, el::Element) = print(io, "Element(", el.name, ')')

ispresent(s) = !isempty(s)
ispresent(x::Float64) = !isnan(x)
ispresent(x::Union{Float64, Quantity}) = !isnan(x)
ispresent(n::Int) = n 0
function printpresent(io::IO, name, val, suffix=""; pad=16)
if ispresent(val)
println(io, lpad(name, pad), ": ", val, suffix)
println(io, lpad(name, pad), ": ", typeof(val) <: Quantity ? val.val : val, suffix)
end
end

Expand All @@ -100,7 +101,7 @@ end

function printpresenthtml(io::IO, name, val, suffix="")
if ispresent(val)
println(io, "<tr><th>", name, "</th><td>", val, suffix, "</td></tr>")
println(io, "<tr><th>", name, "</th><td>", typeof(val) <: Quantity ? val.val : val, suffix, "</td></tr>")
end
end

Expand Down
Loading