Skip to content

Commit

Permalink
Merge pull request #19 from crstnbr/units
Browse files Browse the repository at this point in the history
Dimensionful quantities with Unitful.jl
  • Loading branch information
carstenbauer authored Sep 15, 2018
2 parents f9c6503 + 3db549d commit 6084c6b
Show file tree
Hide file tree
Showing 5 changed files with 551 additions and 538 deletions.
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))
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

0 comments on commit 6084c6b

Please sign in to comment.