From 4b808de50b1070e4fabfae6c904fef17aef8bf64 Mon Sep 17 00:00:00 2001 From: Qi Zhang Date: Fri, 27 Sep 2019 02:58:22 -0400 Subject: [PATCH 01/61] Implement `fieldvalues` & `eltype` for `EquationOfState` --- src/Collections.jl | 64 ++++++++++++++++++----------------------- src/NonlinearFitting.jl | 2 +- 2 files changed, 29 insertions(+), 37 deletions(-) diff --git a/src/Collections.jl b/src/Collections.jl index 062fc38..b7b1b62 100644 --- a/src/Collections.jl +++ b/src/Collections.jl @@ -304,7 +304,7 @@ apply(form::EnergyForm, eos::EquationOfState) = v -> apply(form, eos, v) Return the energy of a `Murnaghan` equation of state on volume `v`. """ function apply(::EnergyForm, eos::Murnaghan, v::Real) - v0, b0, bp0, e0 = collect(eos) + v0, b0, bp0, e0 = fieldvalues(eos) x = bp0 - 1 y = (v0 / v)^bp0 @@ -316,7 +316,7 @@ end Return the energy of a `BirchMurnaghan2nd` equation of state on volume `v`. """ function apply(::EnergyForm, eos::BirchMurnaghan2nd, v::Real) - v0, b0, e0 = collect(eos) + v0, b0, e0 = fieldvalues(eos) f = (cbrt(v0 / v)^2 - 1) / 2 return e0 + 9 / 2 * b0 * v0 * f^2 @@ -327,7 +327,7 @@ end Return the energy of a `BirchMurnaghan3rd` equation of state on volume `v`. """ function apply(::EnergyForm, eos::BirchMurnaghan3rd, v::Real) - v0, b0, bp0, e0 = collect(eos) + v0, b0, bp0, e0 = fieldvalues(eos) eta = cbrt(v0 / v) xi = eta^2 - 1 @@ -339,7 +339,7 @@ end Return the energy of a `BirchMurnaghan4th` equation of state on volume `v`. """ function apply(::EnergyForm, eos::BirchMurnaghan4th, v::Real) - v0, b0, bp0, bpp0, e0 = collect(eos) + v0, b0, bp0, bpp0, e0 = fieldvalues(eos) f = (cbrt(v0 / v)^2 - 1) / 2 h = b0 * bpp0 + bp0^2 @@ -351,7 +351,7 @@ end Return the energy of a `PoirierTarantola2nd` equation of state on volume `v`. """ function apply(::EnergyForm, eos::PoirierTarantola2nd, v::Real) - v0, b0, e0 = collect(eos) + v0, b0, e0 = fieldvalues(eos) return e0 + b0 / 2 * v0 * log(v / v0)^(2 / 3) end @@ -361,7 +361,7 @@ end Return the energy of a `PoirierTarantola3rd` equation of state on volume `v`. """ function apply(::EnergyForm, eos::PoirierTarantola3rd, v::Real) - v0, b0, bp0, e0 = collect(eos) + v0, b0, bp0, e0 = fieldvalues(eos) x = cbrt(v / v0) xi = -3 * log(x) @@ -373,7 +373,7 @@ end Return the energy of a `PoirierTarantola4th` equation of state on volume `v`. """ function apply(::EnergyForm, eos::PoirierTarantola4th, v::Real) - v0, b0, bp0, bpp0, e0 = collect(eos) + v0, b0, bp0, bpp0, e0 = fieldvalues(eos) x = cbrt(v / v0) xi = log(x) @@ -386,7 +386,7 @@ end Return the energy of a `Vinet` equation of state on volume `v`. """ function apply(::EnergyForm, eos::Vinet, v::Real) - v0, b0, bp0, e0 = collect(eos) + v0, b0, bp0, e0 = fieldvalues(eos) x = cbrt(v / v0) xi = 3 / 2 * (bp0 - 1) @@ -398,7 +398,7 @@ end Return the energy of a `AntonSchmidt` equation of state on volume `v`. """ function apply(::EnergyForm, eos::AntonSchmidt, v::Real) - v0, β, n, e∞ = collect(eos) + v0, β, n, e∞ = fieldvalues(eos) x = v / v0 η = n + 1 @@ -442,7 +442,7 @@ apply(::PressureForm, eos::EquationOfState) = v -> apply(PressureForm(), eos, v) Return the pressure of a `Murnaghan` equation of state on volume `v`. """ function apply(::PressureForm, eos::Murnaghan, v::Real) - v0, b0, bp0 = collect(eos) + v0, b0, bp0 = fieldvalues(eos) return b0 / bp0 * ((v0 / v)^bp0 - 1) end @@ -452,7 +452,7 @@ end Return the pressure of a `BirchMurnaghan2nd` equation of state on volume `v`. """ function apply(::PressureForm, eos::BirchMurnaghan2nd, v::Real) - v0, b0 = collect(eos) + v0, b0 = fieldvalues(eos) f = ((v0 / v)^(2 / 3) - 1) / 2 return 3b0 * f * (1 + 2f)^(5 / 2) @@ -463,7 +463,7 @@ end Return the pressure of a `BirchMurnaghan3rd` equation of state on volume `v`. """ function apply(::PressureForm, eos::BirchMurnaghan3rd, v::Real) - v0, b0, bp0 = collect(eos) + v0, b0, bp0 = fieldvalues(eos) eta = (v0 / v)^(1 / 3) return 3 / 2 * b0 * (eta^7 - eta^5) * (1 + 3 / 4 * (bp0 - 4) * (eta^2 - 1)) @@ -474,7 +474,7 @@ end Return the pressure of a `BirchMurnaghan4th` equation of state on volume `v`. """ function apply(::PressureForm, eos::BirchMurnaghan4th, v::Real) - v0, b0, bp0, bpp0 = collect(eos) + v0, b0, bp0, bpp0 = fieldvalues(eos) f = ((v0 / v)^(2 / 3) - 1) / 2 h = b0 * bpp0 + bp0^2 @@ -486,7 +486,7 @@ end Return the pressure of a `PoirierTarantola2nd` equation of state on volume `v`. """ function apply(::PressureForm, eos::PoirierTarantola2nd, v::Real) - v0, b0 = collect(eos) + v0, b0 = fieldvalues(eos) x = (v / v0)^(1 / 3) return -b0 / x * log(x) @@ -497,7 +497,7 @@ end Return the pressure of a `PoirierTarantola3rd` equation of state on volume `v`. """ function apply(::PressureForm, eos::PoirierTarantola3rd, v::Real) - v0, b0, bp0 = collect(eos) + v0, b0, bp0 = fieldvalues(eos) x = v / v0 xi = log(x) @@ -509,7 +509,7 @@ end Return the pressure of a `PoirierTarantola4th` equation of state on volume `v`. """ function apply(::PressureForm, eos::PoirierTarantola4th, v::Real) - v0, b0, bp0, bpp0 = collect(eos) + v0, b0, bp0, bpp0 = fieldvalues(eos) x = (v / v0)^(1 / 3) xi = log(x) @@ -522,7 +522,7 @@ end Return the pressure of a `Vinet` equation of state on volume `v`. """ function apply(::PressureForm, eos::Vinet, v::Real) - v0, b0, bp0 = collect(eos) + v0, b0, bp0 = fieldvalues(eos) x = (v / v0)^(1 / 3) xi = 3 / 2 * (bp0 - 1) @@ -534,7 +534,7 @@ end Return the pressure of a `AntonSchmidt` equation of state on volume `v`. """ function apply(::PressureForm, eos::AntonSchmidt, v::Real) - v0, β, n = collect(eos) + v0, β, n = fieldvalues(eos) x = v / v0 return -β * x^n * log(x) @@ -545,7 +545,7 @@ end Return the pressure of a `BreenanStacey` equation of state on volume `v`. """ function apply(::PressureForm, eos::BreenanStacey, v::Real) - v0, b0, γ0 = collect(eos) + v0, b0, γ0 = fieldvalues(eos) x = v0 / v return b0 / 2 / γ0 * x^(4 / 3) * (exp(2γ0 * (1 - x)) - 1) @@ -588,7 +588,7 @@ apply(::BulkModulusForm, eos::EquationOfState) = v -> apply(BulkModulusForm(), e Return the bulk modulus of a `BirchMurnaghan2nd` equation of state on volume `v`. """ function apply(::BulkModulusForm, eos::BirchMurnaghan2nd, v::Real) - v0, b0 = collect(eos) + v0, b0 = fieldvalues(eos) f = ((v0 / v)^(2 / 3) - 1) / 2 return b0 * (7f + 1) * (2f + 1)^(5 / 2) @@ -599,7 +599,7 @@ end Return the bulk modulus of a `BirchMurnaghan3rd` equation of state on volume `v`. """ function apply(::BulkModulusForm, eos::BirchMurnaghan3rd, v::Real) - v0, b0, bp0 = collect(eos) + v0, b0, bp0 = fieldvalues(eos) f = ((v0 / v)^(2 / 3) - 1) / 2 return b0 / 2 * (2f + 1)^(5 / 2) * ((27 * f^2 + 6f) * (bp0 - 4) - 4f + 2) @@ -610,7 +610,7 @@ end Return the bulk modulus of a `BirchMurnaghan4th` equation of state on volume `v`. """ function apply(::BulkModulusForm, eos::BirchMurnaghan4th, v::Real) - v0, b0, bp0, bpp0 = collect(eos) + v0, b0, bp0, bpp0 = fieldvalues(eos) f = ((v0 / v)^(2 / 3) - 1) / 2 h = b0 * bpp0 + bp0^2 @@ -623,7 +623,7 @@ end Return the bulk modulus of a `PoirierTarantola2nd` equation of state on volume `v`. """ function apply(::BulkModulusForm, eos::PoirierTarantola2nd, v::Real) - v0, b0 = collect(eos) + v0, b0 = fieldvalues(eos) x = (v / v0)^(1 / 3) return b0 / x * (1 - log(x)) @@ -634,7 +634,7 @@ end Return the bulk modulus of a `PoirierTarantola3rd` equation of state on volume `v`. """ function apply(::BulkModulusForm, eos::PoirierTarantola3rd, v::Real) - v0, b0, bp0 = collect(eos) + v0, b0, bp0 = fieldvalues(eos) x = v / v0 xi = log(x) @@ -646,7 +646,7 @@ end Return the bulk modulus of a `PoirierTarantola4th` equation of state on volume `v`. """ function apply(::BulkModulusForm, eos::PoirierTarantola4th, v::Real) - v0, b0, bp0, bpp0 = collect(eos) + v0, b0, bp0, bpp0 = fieldvalues(eos) x = (v / v0)^(1 / 3) xi = log(x) @@ -660,7 +660,7 @@ end Return the bulk modulus of a `Vinet` equation of state on volume `v`. """ function apply(::BulkModulusForm, eos::Vinet, v::Real) - v0, b0, bp0 = collect(eos) + v0, b0, bp0 = fieldvalues(eos) x = (v / v0)^(1 / 3) xi = 3 / 2 * (bp0 - 1) @@ -672,7 +672,7 @@ end Return the bulk modulus of a `AntonSchmidt` equation of state on volume `v`. """ function apply(::BulkModulusForm, eos::AntonSchmidt, v::Real) - v0, β, n = collect(eos) + v0, β, n = fieldvalues(eos) x = v / v0 return β * x^n * (1 + n * log(x)) @@ -698,16 +698,8 @@ for E in nonabstract(EquationOfState) end) end -Base.iterate(eos::EquationOfState) = getfield(eos, 1), 2 -function Base.iterate(eos::EquationOfState, state::Integer) - state > length(eos) && return nothing - return getfield(eos, state), state + 1 -end # function Base.iterate -Base.length(eos::EquationOfState) = nfields(eos) -Base.size(eos::EquationOfState) = (length(eos),) +fieldvalues(eos::EquationOfState) = [getfield(eos, i) for i in 1:nfields(eos)] Base.eltype(::Type{<:EquationOfState{T}}) where {T} = T - -Base.isapprox(a::T, b::T; kwargs...) where {T<:EquationOfState} = isapprox(collect(a), collect(b); kwargs...) # Base.getindex(eos::PolynomialEquationOfState{T,N}, index::Int64) where {T,N} = getindex(eos.data, index) # =============================== Miscellaneous ============================== # diff --git a/src/NonlinearFitting.jl b/src/NonlinearFitting.jl index 260a434..75bf036 100644 --- a/src/NonlinearFitting.jl +++ b/src/NonlinearFitting.jl @@ -42,7 +42,7 @@ function lsqfit( T = promote_type(eltype(eos), eltype(xdata), eltype(ydata), Float64) P = Collections.similar_type(E, T) model(x, p) = map(apply(form, P(p...)), x) - fitted = curve_fit(model, T.(xdata), T.(ydata), T.(collect(eos)); kwargs...) + fitted = curve_fit(model, T.(xdata), T.(ydata), T.(Collections.fieldvalues(eos)); kwargs...) return debug ? fitted : P(fitted.param...) end # function lsqfit From 4c04ec011cfac8dab4e1a958008394096e5c25fa Mon Sep 17 00:00:00 2001 From: Qi Zhang Date: Fri, 27 Sep 2019 03:06:19 -0400 Subject: [PATCH 02/61] test: Fix `isapprox` in test/NonlinearFitting.jl when deprecating iteration interface, `isapprox` cannot be applied onto `EquationOfState`s --- test/NonlinearFitting.jl | 72 ++++++++++++++++++++-------------------- 1 file changed, 36 insertions(+), 36 deletions(-) diff --git a/test/NonlinearFitting.jl b/test/NonlinearFitting.jl index 9b4861f..1c6f01f 100644 --- a/test/NonlinearFitting.jl +++ b/test/NonlinearFitting.jl @@ -10,14 +10,14 @@ using EquationsOfState.NonlinearFitting 103.58772269057364, -144.45152457521132, -40.31992619868024, - ) + ) |> Collections.fieldvalues @test isapprox( lsqfit( EnergyForm(), BirchMurnaghan3rd(1, 2, 3.0, 0), [1, 2, 3, 4, 5], [5, 6, 9, 8, 7], - ), + ) |> Collections.fieldvalues, result; atol = 1e-5, ) @@ -27,7 +27,7 @@ using EquationsOfState.NonlinearFitting BirchMurnaghan3rd(1, 2, 3, 0), [1, 2, 3, 4, 5.0], [5, 6, 9, 8, 7], - ), + ) |> Collections.fieldvalues, result; atol = 1e-5, ) @@ -37,7 +37,7 @@ using EquationsOfState.NonlinearFitting BirchMurnaghan3rd(1, 2, 3.0, 0), [1, 2, 3, 4, 5], [5, 6, 9, 8, 7.0], - ), + ) |> Collections.fieldvalues, result; atol = 1e-5, ) @@ -47,7 +47,7 @@ using EquationsOfState.NonlinearFitting BirchMurnaghan3rd(1, 2, 3, 0), [1, 2, 3, 4, 5], [5, 6, 9, 8, 7], - ), + ) |> Collections.fieldvalues, result; atol = 1e-5, ) @@ -59,14 +59,14 @@ end 29.30861698140365, 12.689089871112746, 0.0, - ) + ) |> Collections.fieldvalues @test isapprox( lsqfit( PressureForm(), BirchMurnaghan3rd(1, 2, 3.0, 0), [1, 2, 3, 4, 5], [5, 6, 9, 8, 7], - ), + ) |> Collections.fieldvalues, result; atol = 1e-6, ) @@ -76,7 +76,7 @@ end BirchMurnaghan3rd(1, 2, 3, 0), [1, 2, 3, 4, 5.0], [5, 6, 9, 8, 7], - ), + ) |> Collections.fieldvalues, result; atol = 1e-6, ) @@ -86,7 +86,7 @@ end BirchMurnaghan3rd(1, 2, 3.0, 0), [1, 2, 3, 4, 5], [5, 6, 9, 8, 7.0], - ), + ) |> Collections.fieldvalues, result; atol = 1e-6, ) @@ -96,21 +96,21 @@ end BirchMurnaghan3rd(1, 2, 3, 0), [1, 2, 3, 4, 5], [5, 6, 9, 8, 7], - ), + ) |> Collections.fieldvalues, result; atol = 1e-6, ) end @testset "Test fitting bulk modulus with different element types" begin - result = BirchMurnaghan3rd(7.218928431312577, 5.007900469653902, 4.06037725509478, 0.0) + result = BirchMurnaghan3rd(7.218928431312577, 5.007900469653902, 4.06037725509478, 0.0) |> Collections.fieldvalues @test isapprox( lsqfit( BulkModulusForm(), BirchMurnaghan3rd(1, 2, 3.0, 0), [1, 2, 3, 4, 5], [5, 6, 9, 8, 7], - ), + ) |> Collections.fieldvalues, result; atol = 1e-5, ) @@ -120,7 +120,7 @@ end BirchMurnaghan3rd(1, 2, 3, 0), [1, 2, 3, 4, 5.0], [5, 6, 9, 8, 7], - ), + ) |> Collections.fieldvalues, result; atol = 1e-5, ) @@ -130,7 +130,7 @@ end BirchMurnaghan3rd(1, 2, 3.0, 0), [1, 2, 3, 4, 5], [5, 6, 9, 8, 7.0], - ), + ) |> Collections.fieldvalues, result; atol = 1e-5, ) @@ -140,7 +140,7 @@ end BirchMurnaghan3rd(1, 2, 3, 0), [1, 2, 3, 4, 5], [5, 6, 9, 8, 7], - ), + ) |> Collections.fieldvalues, result; atol = 1e-5, ) @@ -210,40 +210,40 @@ end -9.73155247952, ] @test isapprox( - lsqfit(EnergyForm(), BirchMurnaghan3rd(40, 0.5, 4, 0), volumes, energies), + lsqfit(EnergyForm(), BirchMurnaghan3rd(40, 0.5, 4, 0), volumes, energies) |> Collections.fieldvalues, BirchMurnaghan3rd( 40.98926572528106, 0.5369258245417454, 4.178644235500821, -10.842803908240892, - ), + ) |> Collections.fieldvalues, ) @test isapprox( - lsqfit(EnergyForm(), Murnaghan(41, 0.5, 4, 0), volumes, energies), + lsqfit(EnergyForm(), Murnaghan(41, 0.5, 4, 0), volumes, energies) |> Collections.fieldvalues, Murnaghan( 41.13757930387086, 0.5144967693786603, 3.9123862262572264, -10.836794514626673, - ), + ) |> Collections.fieldvalues, ) @test isapprox( - lsqfit(EnergyForm(), PoirierTarantola3rd(41, 0.5, 4, 0), volumes, energies), + lsqfit(EnergyForm(), PoirierTarantola3rd(41, 0.5, 4, 0), volumes, energies) |> Collections.fieldvalues, PoirierTarantola3rd( 40.86770643373908, 0.5667729960804602, 4.331688936974368, -10.851486685041658, - ), + ) |> Collections.fieldvalues, ) @test isapprox( - lsqfit(EnergyForm(), Vinet(41, 0.5, 4, 0), volumes, energies), + lsqfit(EnergyForm(), Vinet(41, 0.5, 4, 0), volumes, energies) |> Collections.fieldvalues, Vinet( 40.916875663779784, 0.5493839425156859, 4.3051929654936885, -10.846160810560756, - ), + ) |> Collections.fieldvalues, ) # 'deltafactor': {'b0': 0.5369258245611414, # 'b1': 4.178644231924639, @@ -331,13 +331,13 @@ end fitted_eos = lsqfit(EnergyForm(), Vinet(23, 0.5, 4, -2), mp153_volumes, mp153_energies) @test isapprox( - fitted_eos, + fitted_eos |> Collections.fieldvalues, Vinet( 22.95764559358769, 0.2257091141420788, 4.060543387224629, -1.5944292606251582, - ), + ) |> Collections.fieldvalues, ) @test isapprox( map(apply(EnergyForm(), fitted_eos), mp153_volumes), @@ -421,13 +421,13 @@ end fitted_eos = lsqfit(EnergyForm(), Vinet(20, 0.5, 4, -5), mp149_volumes, mp149_energies) @test isapprox( - fitted_eos, + fitted_eos |> Collections.fieldvalues, Vinet( 20.446696754873944, 0.5516638521306302, 4.324373909783161, -5.424963389876503, - ), + ) |> Collections.fieldvalues, ) @test isapprox( map(apply(EnergyForm(), fitted_eos), mp149_volumes), @@ -511,13 +511,13 @@ end fitted_eos = lsqfit(EnergyForm(), Vinet(17, 0.5, 4, -7), mp72_volumes, mp72_energies) @test isapprox( - fitted_eos, + fitted_eos |> Collections.fieldvalues, Vinet( 17.13223026131245, 0.7029766224730147, 3.6388077563621812, -7.897414959124461, - ), + ) |> Collections.fieldvalues, ) @test isapprox( map(apply(EnergyForm(), fitted_eos), mp72_volumes), @@ -597,26 +597,26 @@ end BirchMurnaghan3rd(224, 0.0006, 4, -323), volumes, energies, - ) ≈ BirchMurnaghan3rd(224.444565, 0.00062506191050572675, 3.740369, -323.417714) + ) |> Collections.fieldvalues ≈ BirchMurnaghan3rd(224.444565, 0.00062506191050572675, 3.740369, -323.417714) |> Collections.fieldvalues @test isapprox( lsqfit( EnergyForm(), BirchMurnaghan4th(224, 0.0006, 4, -5460, -323), volumes, energies, - ), + ) |> Collections.fieldvalues, BirchMurnaghan4th( 224.45756247137314, 0.0006229382259822287, 3.730991473426449, -5322.693307704408, -323.4177113158418, - ); + ) |> Collections.fieldvalues; atol = 1e-3, ) @test isapprox( - lsqfit(EnergyForm(), Murnaghan(224, 0.006, 4, -323), volumes, energies), - Murnaghan(224.501825, 0.00060479524074699499, 3.723835, -323.417686); + lsqfit(EnergyForm(), Murnaghan(224, 0.006, 4, -323), volumes, energies) |> Collections.fieldvalues, + Murnaghan(224.501825, 0.00060479524074699499, 3.723835, -323.417686) |> Collections.fieldvalues; atol = 1e-5, ) @test isapprox( @@ -625,8 +625,8 @@ end PoirierTarantola3rd(100, 0.0006, 3.7, -323), volumes, energies, - ), - PoirierTarantola3rd(224.509208, 0.000635892264159838, 3.690448, -323.41773); + ) |> Collections.fieldvalues, + PoirierTarantola3rd(224.509208, 0.000635892264159838, 3.690448, -323.41773) |> Collections.fieldvalues; atol = 1e-5, ) # @test lsqfit(EnergyForm(), PoirierTarantola4th(220, 0.0006, 3.7, -5500, -323), volumes, energies; lower = Float64[220, 0, 3, -6000, -400], upper = Float64[300, 0.01, 5, -5000, -300]) ≈ PoirierTarantola4th(224.430182, 0.0006232241765069493, 3.758360, -5493.859729817176, -323.417712) From 8b2a77a24802c8895011b927ec81cbb9f6b56095 Mon Sep 17 00:00:00 2001 From: Qi Zhang Date: Fri, 27 Sep 2019 03:07:44 -0400 Subject: [PATCH 03/61] Make a comment on `fieldvalues` --- src/Collections.jl | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Collections.jl b/src/Collections.jl index b7b1b62..5ffcec1 100644 --- a/src/Collections.jl +++ b/src/Collections.jl @@ -698,7 +698,9 @@ for E in nonabstract(EquationOfState) end) end +# This is a helper function and should not be exported. fieldvalues(eos::EquationOfState) = [getfield(eos, i) for i in 1:nfields(eos)] + Base.eltype(::Type{<:EquationOfState{T}}) where {T} = T # Base.getindex(eos::PolynomialEquationOfState{T,N}, index::Int64) where {T,N} = getindex(eos.data, index) # =============================== Miscellaneous ============================== # From 2a8b2e532d8831a1919f49a46c3d4a1310512da5 Mon Sep 17 00:00:00 2001 From: Qi Zhang Date: Fri, 27 Sep 2019 03:31:14 -0400 Subject: [PATCH 04/61] Define `length` for `EquationOfState` --- src/Collections.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Collections.jl b/src/Collections.jl index 5ffcec1..3ef928c 100644 --- a/src/Collections.jl +++ b/src/Collections.jl @@ -702,6 +702,7 @@ end fieldvalues(eos::EquationOfState) = [getfield(eos, i) for i in 1:nfields(eos)] Base.eltype(::Type{<:EquationOfState{T}}) where {T} = T +Base.length(eos::EquationOfState) = nfields(eos) # Base.getindex(eos::PolynomialEquationOfState{T,N}, index::Int64) where {T,N} = getindex(eos.data, index) # =============================== Miscellaneous ============================== # From 921e68ff7a138f27328b3906b8b6890af0ebef12 Mon Sep 17 00:00:00 2001 From: Qi Zhang Date: Fri, 27 Sep 2019 03:34:14 -0400 Subject: [PATCH 05/61] Deprecate `length` for `EquationOfState` this cannot fix #31, an iteration interface still need to be defined. And once it is defined, the error emerges. --- src/Collections.jl | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Collections.jl b/src/Collections.jl index 3ef928c..5ffcec1 100644 --- a/src/Collections.jl +++ b/src/Collections.jl @@ -702,7 +702,6 @@ end fieldvalues(eos::EquationOfState) = [getfield(eos, i) for i in 1:nfields(eos)] Base.eltype(::Type{<:EquationOfState{T}}) where {T} = T -Base.length(eos::EquationOfState) = nfields(eos) # Base.getindex(eos::PolynomialEquationOfState{T,N}, index::Int64) where {T,N} = getindex(eos.data, index) # =============================== Miscellaneous ============================== # From 5cb65edb637eb129f5a1d970981578e26894b0ae Mon Sep 17 00:00:00 2001 From: Qi Zhang Date: Sun, 29 Sep 2019 16:13:56 -0400 Subject: [PATCH 06/61] Remove type promotions in src/Collections.jl --- src/Collections.jl | 258 +++++++++++++++++++-------------------------- 1 file changed, 109 insertions(+), 149 deletions(-) diff --git a/src/Collections.jl b/src/Collections.jl index 5ffcec1..e9d5595 100644 --- a/src/Collections.jl +++ b/src/Collections.jl @@ -34,18 +34,18 @@ export apply, # Types # # ============================================================================ # """ - EquationOfState{T} + EquationOfState An abstraction of equations of state, where `T` specifies the elements' type. """ -abstract type EquationOfState{T} end +abstract type EquationOfState end """ - FiniteStrainEquationOfState{T} <: EquationOfState{T} + FiniteStrainEquationOfState <: EquationOfState An abstraction of finite strain equations of state. """ -abstract type FiniteStrainEquationOfState{T} <: EquationOfState{T} end +abstract type FiniteStrainEquationOfState <: EquationOfState end # struct PolynomialEquationOfState{T<:Real,N} <: EquationOfState{T,N} # data::NTuple{N,T} @@ -70,15 +70,11 @@ Create a Murnaghan equation of state. The elements' type will be handled automat - `bp0`: the first-order pressure-derivative bulk modulus of solid at zero pressure. - `e0=0`: the energy of solid at zero pressure. By default is `0`. """ -struct Murnaghan{T<:Real} <: EquationOfState{T} - v0::T - b0::T - bp0::T - e0::T -end -function Murnaghan(v0::Real, b0::Real, bp0::Real, e0::Real) - T = Base.promote_typeof(v0, b0, bp0, e0) - Murnaghan{T}(v0, b0, bp0, e0) +struct Murnaghan{A,B,C,D} <: EquationOfState + v0::A + b0::B + bp0::C + e0::D end Murnaghan(v0, b0, bp0) = Murnaghan(v0, b0, bp0, 0) @@ -92,14 +88,10 @@ Create a Birch–Murnaghan 2nd order equation of state. The elements' type will - `b0`: the bulk modulus of solid at zero pressure. - `e0=0`: the energy of solid at zero pressure. By default is `0`. """ -struct BirchMurnaghan2nd{T<:Real} <: FiniteStrainEquationOfState{T} - v0::T - b0::T - e0::T -end -function BirchMurnaghan2nd(v0::Real, b0::Real, e0::Real) - T = Base.promote_typeof(v0, b0, e0) - BirchMurnaghan2nd{T}(v0, b0, e0) +struct BirchMurnaghan2nd{A,B,C} <: FiniteStrainEquationOfState + v0::A + b0::B + e0::C end BirchMurnaghan2nd(v0, b0) = BirchMurnaghan2nd(v0, b0, 0) @@ -114,15 +106,11 @@ Create a Birch–Murnaghan 3rd order equation of state. The elements' type will - `bp0`: the first-order pressure-derivative bulk modulus of solid at zero pressure. - `e0=0`: the energy of solid at zero pressure. By default is `0`. """ -struct BirchMurnaghan3rd{T<:Real} <: FiniteStrainEquationOfState{T} - v0::T - b0::T - bp0::T - e0::T -end -function BirchMurnaghan3rd(v0::Real, b0::Real, bp0::Real, e0::Real) - T = Base.promote_typeof(v0, b0, bp0, e0) - BirchMurnaghan3rd{T}(v0, b0, bp0, e0) +struct BirchMurnaghan3rd{A,B,C,D} <: FiniteStrainEquationOfState + v0::A + b0::B + bp0::C + e0::D end BirchMurnaghan3rd(v0, b0, bp0) = BirchMurnaghan3rd(v0, b0, bp0, 0) @@ -138,16 +126,12 @@ Create a Birch–Murnaghan 4th order equation of state. The elements' type will - `bpp0`: the second-order pressure-derivative bulk modulus of solid at zero pressure. - `e0=0`: the energy of solid at zero pressure. By default is `0`. """ -struct BirchMurnaghan4th{T<:Real} <: FiniteStrainEquationOfState{T} - v0::T - b0::T - bp0::T - bpp0::T - e0::T -end -function BirchMurnaghan4th(v0::Real, b0::Real, bp0::Real, bpp0::Real, e0::Real) - T = Base.promote_typeof(v0, b0, bp0, bpp0, e0) - BirchMurnaghan4th{T}(v0, b0, bp0, bpp0, e0) +struct BirchMurnaghan4th{A,B,C,D,E} <: FiniteStrainEquationOfState + v0::A + b0::B + bp0::C + bpp0::D + e0::E end BirchMurnaghan4th(v0, b0, bp0, bpp0) = BirchMurnaghan4th(v0, b0, bp0, bpp0, 0) @@ -161,14 +145,10 @@ Create a Poirier–Tarantola order equation of state. The elements' type will be - `b0`: the bulk modulus of solid at zero pressure. - `e0=0`: the energy of solid at zero pressure. By default is `0`. """ -struct PoirierTarantola2nd{T<:Real} <: FiniteStrainEquationOfState{T} - v0::T - b0::T - e0::T -end -function PoirierTarantola2nd(v0::Real, b0::Real, e0::Real) - T = Base.promote_typeof(v0, b0, e0) - PoirierTarantola2nd{T}(v0, b0, e0) +struct PoirierTarantola2nd{A,B,C} <: FiniteStrainEquationOfState + v0::A + b0::B + e0::C end PoirierTarantola2nd(v0, b0) = PoirierTarantola2nd(v0, b0, 0) @@ -183,15 +163,11 @@ Create a Poirier–Tarantola 3rd order equation of state. The elements' type wil - `bp0`: the first-order pressure-derivative bulk modulus of solid at zero pressure. - `e0=0`: the energy of solid at zero pressure. By default is `0`. """ -struct PoirierTarantola3rd{T<:Real} <: FiniteStrainEquationOfState{T} - v0::T - b0::T - bp0::T - e0::T -end -function PoirierTarantola3rd(v0::Real, b0::Real, bp0::Real, e0::Real) - T = Base.promote_typeof(v0, b0, bp0, e0) - PoirierTarantola3rd{T}(v0, b0, bp0, e0) +struct PoirierTarantola3rd{A,B,C,D} <: FiniteStrainEquationOfState + v0::A + b0::B + bp0::C + e0::D end PoirierTarantola3rd(v0, b0, bp0) = PoirierTarantola3rd(v0, b0, bp0, 0) @@ -207,16 +183,12 @@ Create a Birch–Murnaghan 4th order equation of state. The elements' type will - `bpp0`: the second-order pressure-derivative bulk modulus of solid at zero pressure. - `e0=0`: the energy of solid at zero pressure. By default is `0`. """ -struct PoirierTarantola4th{T<:Real} <: FiniteStrainEquationOfState{T} - v0::T - b0::T - bp0::T - bpp0::T - e0::T -end -function PoirierTarantola4th(v0::Real, b0::Real, bp0::Real, bpp0::Real, e0::Real) - T = Base.promote_typeof(v0, b0, bp0, bpp0, e0) - PoirierTarantola4th{T}(v0, b0, bp0, bpp0, e0) +struct PoirierTarantola4th{A,B,C,D,E} <: FiniteStrainEquationOfState + v0::A + b0::B + bp0::C + bpp0::D + e0::E end PoirierTarantola4th(v0, b0, bp0, bpp0) = PoirierTarantola4th(v0, b0, bp0, bpp0, 0) @@ -231,39 +203,27 @@ Create a Vinet equation of state. The elements' type will be handled automatical - `bp0`: the first-order pressure-derivative bulk modulus of solid at zero pressure. - `e0=0`: the energy of solid at zero pressure. By default is `0`. """ -struct Vinet{T<:Real} <: EquationOfState{T} - v0::T - b0::T - bp0::T - e0::T -end -function Vinet(v0::Real, b0::Real, bp0::Real, e0::Real) - T = Base.promote_typeof(v0, b0, bp0, e0) - Vinet{T}(v0, b0, bp0, e0) +struct Vinet{A,B,C,D} <: EquationOfState + v0::A + b0::B + bp0::C + e0::D end Vinet(v0, b0, bp0) = Vinet(v0, b0, bp0, 0) -struct AntonSchmidt{T<:Real} <: EquationOfState{T} - v0::T - β::T - n::T - e∞::T -end -function AntonSchmidt(v0::Real, β::Real, n::Real, e∞::Real) - T = Base.promote_typeof(v0, β, n, e∞) - AntonSchmidt{T}(v0, β, n, e∞) +struct AntonSchmidt{A,B,C,D} <: EquationOfState + v0::A + β::B + n::C + e∞::D end AntonSchmidt(v0, β, n) = AntonSchmidt(v0, β, n, 0) -struct BreenanStacey{T<:Real} <: EquationOfState{T} - v0::T - b0::T - γ0::T - e0::T -end -function BreenanStacey(v0::Real, b0::Real, γ0::Real, e0::Real) - T = Base.promote_typeof(v0, b0, γ0, e0) - BreenanStacey{T}(v0, b0, γ0, e0) +struct BreenanStacey{A,B,C,D} <: EquationOfState + v0::A + b0::B + γ0::C + e0::D end BreenanStacey(v0, b0, γ0) = BreenanStacey(v0, b0, γ0, 0) # =================================== Types ================================== # @@ -299,11 +259,11 @@ julia> map(f, 1:1:10) """ apply(form::EnergyForm, eos::EquationOfState) = v -> apply(form, eos, v) """ - apply(EnergyForm(), eos::Murnaghan, v::Real) + apply(EnergyForm(), eos::Murnaghan, v) Return the energy of a `Murnaghan` equation of state on volume `v`. """ -function apply(::EnergyForm, eos::Murnaghan, v::Real) +function apply(::EnergyForm, eos::Murnaghan, v) v0, b0, bp0, e0 = fieldvalues(eos) x = bp0 - 1 @@ -311,22 +271,22 @@ function apply(::EnergyForm, eos::Murnaghan, v::Real) return e0 + b0 / bp0 * v * (y / x + 1) - v0 * b0 / x end """ - apply(EnergyForm(), eos::BirchMurnaghan2nd, v::Real) + apply(EnergyForm(), eos::BirchMurnaghan2nd, v) Return the energy of a `BirchMurnaghan2nd` equation of state on volume `v`. """ -function apply(::EnergyForm, eos::BirchMurnaghan2nd, v::Real) +function apply(::EnergyForm, eos::BirchMurnaghan2nd, v) v0, b0, e0 = fieldvalues(eos) f = (cbrt(v0 / v)^2 - 1) / 2 return e0 + 9 / 2 * b0 * v0 * f^2 end """ - apply(EnergyForm(), eos::BirchMurnaghan3rd, v::Real) + apply(EnergyForm(), eos::BirchMurnaghan3rd, v) Return the energy of a `BirchMurnaghan3rd` equation of state on volume `v`. """ -function apply(::EnergyForm, eos::BirchMurnaghan3rd, v::Real) +function apply(::EnergyForm, eos::BirchMurnaghan3rd, v) v0, b0, bp0, e0 = fieldvalues(eos) eta = cbrt(v0 / v) @@ -334,11 +294,11 @@ function apply(::EnergyForm, eos::BirchMurnaghan3rd, v::Real) return e0 + 9 / 16 * b0 * v0 * xi^2 * (6 + bp0 * xi - 4 * eta^2) end """ - apply(EnergyForm(), eos::BirchMurnaghan4th, v::Real) + apply(EnergyForm(), eos::BirchMurnaghan4th, v) Return the energy of a `BirchMurnaghan4th` equation of state on volume `v`. """ -function apply(::EnergyForm, eos::BirchMurnaghan4th, v::Real) +function apply(::EnergyForm, eos::BirchMurnaghan4th, v) v0, b0, bp0, bpp0, e0 = fieldvalues(eos) f = (cbrt(v0 / v)^2 - 1) / 2 @@ -346,21 +306,21 @@ function apply(::EnergyForm, eos::BirchMurnaghan4th, v::Real) return e0 + 3 / 8 * v0 * b0 * f^2 * ((9h - 63bp0 + 143) * f^2 + 12 * (bp0 - 4) * f + 12) end """ - apply(EnergyForm(), eos::PoirierTarantola2nd, v::Real) + apply(EnergyForm(), eos::PoirierTarantola2nd, v) Return the energy of a `PoirierTarantola2nd` equation of state on volume `v`. """ -function apply(::EnergyForm, eos::PoirierTarantola2nd, v::Real) +function apply(::EnergyForm, eos::PoirierTarantola2nd, v) v0, b0, e0 = fieldvalues(eos) return e0 + b0 / 2 * v0 * log(v / v0)^(2 / 3) end """ - apply(EnergyForm(), eos::PoirierTarantola3rd, v::Real) + apply(EnergyForm(), eos::PoirierTarantola3rd, v) Return the energy of a `PoirierTarantola3rd` equation of state on volume `v`. """ -function apply(::EnergyForm, eos::PoirierTarantola3rd, v::Real) +function apply(::EnergyForm, eos::PoirierTarantola3rd, v) v0, b0, bp0, e0 = fieldvalues(eos) x = cbrt(v / v0) @@ -368,11 +328,11 @@ function apply(::EnergyForm, eos::PoirierTarantola3rd, v::Real) return e0 + b0 / 6 * v0 * xi^2 * ((bp0 - 2) * xi + 3) end """ - apply(EnergyForm(), eos::PoirierTarantola4th, v::Real) + apply(EnergyForm(), eos::PoirierTarantola4th, v) Return the energy of a `PoirierTarantola4th` equation of state on volume `v`. """ -function apply(::EnergyForm, eos::PoirierTarantola4th, v::Real) +function apply(::EnergyForm, eos::PoirierTarantola4th, v) v0, b0, bp0, bpp0, e0 = fieldvalues(eos) x = cbrt(v / v0) @@ -381,11 +341,11 @@ function apply(::EnergyForm, eos::PoirierTarantola4th, v::Real) return e0 + b0 / 24v0 * xi^2 * ((h + 3bp0 + 3) * xi^2 + 4 * (bp0 + 2) * xi + 12) end """ - apply(EnergyForm(), eos::Vinet, v::Real) + apply(EnergyForm(), eos::Vinet, v) Return the energy of a `Vinet` equation of state on volume `v`. """ -function apply(::EnergyForm, eos::Vinet, v::Real) +function apply(::EnergyForm, eos::Vinet, v) v0, b0, bp0, e0 = fieldvalues(eos) x = cbrt(v / v0) @@ -393,11 +353,11 @@ function apply(::EnergyForm, eos::Vinet, v::Real) return e0 + 9b0 * v0 / xi^2 * (1 + (xi * (1 - x) - 1) * exp(xi * (1 - x))) end """ - apply(EnergyForm(), eos::AntonSchmidt, v::Real) + apply(EnergyForm(), eos::AntonSchmidt, v) Return the energy of a `AntonSchmidt` equation of state on volume `v`. """ -function apply(::EnergyForm, eos::AntonSchmidt, v::Real) +function apply(::EnergyForm, eos::AntonSchmidt, v) v0, β, n, e∞ = fieldvalues(eos) x = v / v0 @@ -437,43 +397,43 @@ julia> map(f, 1:1:10) """ apply(::PressureForm, eos::EquationOfState) = v -> apply(PressureForm(), eos, v) """ - apply(PressureForm(), eos::Murnaghan, v::Real) + apply(PressureForm(), eos::Murnaghan, v) Return the pressure of a `Murnaghan` equation of state on volume `v`. """ -function apply(::PressureForm, eos::Murnaghan, v::Real) +function apply(::PressureForm, eos::Murnaghan, v) v0, b0, bp0 = fieldvalues(eos) return b0 / bp0 * ((v0 / v)^bp0 - 1) end """ - apply(PressureForm(), eos::BirchMurnaghan2nd, v::Real) + apply(PressureForm(), eos::BirchMurnaghan2nd, v) Return the pressure of a `BirchMurnaghan2nd` equation of state on volume `v`. """ -function apply(::PressureForm, eos::BirchMurnaghan2nd, v::Real) +function apply(::PressureForm, eos::BirchMurnaghan2nd, v) v0, b0 = fieldvalues(eos) f = ((v0 / v)^(2 / 3) - 1) / 2 return 3b0 * f * (1 + 2f)^(5 / 2) end """ - apply(PressureForm(), eos::BirchMurnaghan3rd, v::Real) + apply(PressureForm(), eos::BirchMurnaghan3rd, v) Return the pressure of a `BirchMurnaghan3rd` equation of state on volume `v`. """ -function apply(::PressureForm, eos::BirchMurnaghan3rd, v::Real) +function apply(::PressureForm, eos::BirchMurnaghan3rd, v) v0, b0, bp0 = fieldvalues(eos) eta = (v0 / v)^(1 / 3) return 3 / 2 * b0 * (eta^7 - eta^5) * (1 + 3 / 4 * (bp0 - 4) * (eta^2 - 1)) end """ - apply(PressureForm(), eos::BirchMurnaghan4th, v::Real) + apply(PressureForm(), eos::BirchMurnaghan4th, v) Return the pressure of a `BirchMurnaghan4th` equation of state on volume `v`. """ -function apply(::PressureForm, eos::BirchMurnaghan4th, v::Real) +function apply(::PressureForm, eos::BirchMurnaghan4th, v) v0, b0, bp0, bpp0 = fieldvalues(eos) f = ((v0 / v)^(2 / 3) - 1) / 2 @@ -481,22 +441,22 @@ function apply(::PressureForm, eos::BirchMurnaghan4th, v::Real) return b0 / 2 * (2f + 1)^(5 / 2) * ((9h - 63bp0 + 143) * f^2 + 9 * (bp0 - 4) * f + 6) end """ - apply(PressureForm(), eos::PoirierTarantola2nd, v::Real) + apply(PressureForm(), eos::PoirierTarantola2nd, v) Return the pressure of a `PoirierTarantola2nd` equation of state on volume `v`. """ -function apply(::PressureForm, eos::PoirierTarantola2nd, v::Real) +function apply(::PressureForm, eos::PoirierTarantola2nd, v) v0, b0 = fieldvalues(eos) x = (v / v0)^(1 / 3) return -b0 / x * log(x) end """ - apply(PressureForm(), eos::PoirierTarantola3rd, v::Real) + apply(PressureForm(), eos::PoirierTarantola3rd, v) Return the pressure of a `PoirierTarantola3rd` equation of state on volume `v`. """ -function apply(::PressureForm, eos::PoirierTarantola3rd, v::Real) +function apply(::PressureForm, eos::PoirierTarantola3rd, v) v0, b0, bp0 = fieldvalues(eos) x = v / v0 @@ -504,11 +464,11 @@ function apply(::PressureForm, eos::PoirierTarantola3rd, v::Real) return -b0 * xi / 2x * ((bp0 - 2) * xi - 2) end """ - apply(PressureForm(), eos::PoirierTarantola4th, v::Real) + apply(PressureForm(), eos::PoirierTarantola4th, v) Return the pressure of a `PoirierTarantola4th` equation of state on volume `v`. """ -function apply(::PressureForm, eos::PoirierTarantola4th, v::Real) +function apply(::PressureForm, eos::PoirierTarantola4th, v) v0, b0, bp0, bpp0 = fieldvalues(eos) x = (v / v0)^(1 / 3) @@ -517,11 +477,11 @@ function apply(::PressureForm, eos::PoirierTarantola4th, v::Real) return -b0 * xi / 6 / x * ((h + 3bp0 + 3) * xi^2 + 3 * (bp0 + 6) * xi + 6) end """ - apply(PressureForm(), eos::Vinet, v::Real) + apply(PressureForm(), eos::Vinet, v) Return the pressure of a `Vinet` equation of state on volume `v`. """ -function apply(::PressureForm, eos::Vinet, v::Real) +function apply(::PressureForm, eos::Vinet, v) v0, b0, bp0 = fieldvalues(eos) x = (v / v0)^(1 / 3) @@ -529,22 +489,22 @@ function apply(::PressureForm, eos::Vinet, v::Real) return 3b0 / x^2 * (1 - x) * exp(xi * (1 - x)) end """ - apply(PressureForm(), eos::AntonSchmidt, v::Real) + apply(PressureForm(), eos::AntonSchmidt, v) Return the pressure of a `AntonSchmidt` equation of state on volume `v`. """ -function apply(::PressureForm, eos::AntonSchmidt, v::Real) +function apply(::PressureForm, eos::AntonSchmidt, v) v0, β, n = fieldvalues(eos) x = v / v0 return -β * x^n * log(x) end """ - apply(PressureForm(), eos::BreenanStacey, v::Real) + apply(PressureForm(), eos::BreenanStacey, v) Return the pressure of a `BreenanStacey` equation of state on volume `v`. """ -function apply(::PressureForm, eos::BreenanStacey, v::Real) +function apply(::PressureForm, eos::BreenanStacey, v) v0, b0, γ0 = fieldvalues(eos) x = v0 / v @@ -583,33 +543,33 @@ julia> map(f, 1:1:10) """ apply(::BulkModulusForm, eos::EquationOfState) = v -> apply(BulkModulusForm(), eos, v) """ - apply(BulkModulusForm(), eos::BirchMurnaghan2nd, v::Real) + apply(BulkModulusForm(), eos::BirchMurnaghan2nd, v) Return the bulk modulus of a `BirchMurnaghan2nd` equation of state on volume `v`. """ -function apply(::BulkModulusForm, eos::BirchMurnaghan2nd, v::Real) +function apply(::BulkModulusForm, eos::BirchMurnaghan2nd, v) v0, b0 = fieldvalues(eos) f = ((v0 / v)^(2 / 3) - 1) / 2 return b0 * (7f + 1) * (2f + 1)^(5 / 2) end """ - apply(BulkModulusForm(), eos::BirchMurnaghan3rd, v::Real) + apply(BulkModulusForm(), eos::BirchMurnaghan3rd, v) Return the bulk modulus of a `BirchMurnaghan3rd` equation of state on volume `v`. """ -function apply(::BulkModulusForm, eos::BirchMurnaghan3rd, v::Real) +function apply(::BulkModulusForm, eos::BirchMurnaghan3rd, v) v0, b0, bp0 = fieldvalues(eos) f = ((v0 / v)^(2 / 3) - 1) / 2 return b0 / 2 * (2f + 1)^(5 / 2) * ((27 * f^2 + 6f) * (bp0 - 4) - 4f + 2) end """ - apply(BulkModulusForm(), eos::BirchMurnaghan4th, v::Real) + apply(BulkModulusForm(), eos::BirchMurnaghan4th, v) Return the bulk modulus of a `BirchMurnaghan4th` equation of state on volume `v`. """ -function apply(::BulkModulusForm, eos::BirchMurnaghan4th, v::Real) +function apply(::BulkModulusForm, eos::BirchMurnaghan4th, v) v0, b0, bp0, bpp0 = fieldvalues(eos) f = ((v0 / v)^(2 / 3) - 1) / 2 @@ -618,22 +578,22 @@ function apply(::BulkModulusForm, eos::BirchMurnaghan4th, v::Real) ((99h - 693bp0 + 1573) * f^3 + (27h - 108bp0 + 105) * f^2 + 6f * (3bp0 - 5) + 6) end """ - apply(BulkModulusForm(), eos::PoirierTarantola2nd, v::Real) + apply(BulkModulusForm(), eos::PoirierTarantola2nd, v) Return the bulk modulus of a `PoirierTarantola2nd` equation of state on volume `v`. """ -function apply(::BulkModulusForm, eos::PoirierTarantola2nd, v::Real) +function apply(::BulkModulusForm, eos::PoirierTarantola2nd, v) v0, b0 = fieldvalues(eos) x = (v / v0)^(1 / 3) return b0 / x * (1 - log(x)) end """ - apply(BulkModulusForm(), eos::PoirierTarantola3rd, v::Real) + apply(BulkModulusForm(), eos::PoirierTarantola3rd, v) Return the bulk modulus of a `PoirierTarantola3rd` equation of state on volume `v`. """ -function apply(::BulkModulusForm, eos::PoirierTarantola3rd, v::Real) +function apply(::BulkModulusForm, eos::PoirierTarantola3rd, v) v0, b0, bp0 = fieldvalues(eos) x = v / v0 @@ -641,11 +601,11 @@ function apply(::BulkModulusForm, eos::PoirierTarantola3rd, v::Real) return -b0 / 2x * (((bp0 - 2) * xi + 2 - 2bp0) * xi + 2) end """ - apply(BulkModulusForm(), eos::PoirierTarantola4th, v::Real) + apply(BulkModulusForm(), eos::PoirierTarantola4th, v) Return the bulk modulus of a `PoirierTarantola4th` equation of state on volume `v`. """ -function apply(::BulkModulusForm, eos::PoirierTarantola4th, v::Real) +function apply(::BulkModulusForm, eos::PoirierTarantola4th, v) v0, b0, bp0, bpp0 = fieldvalues(eos) x = (v / v0)^(1 / 3) @@ -655,11 +615,11 @@ function apply(::BulkModulusForm, eos::PoirierTarantola4th, v::Real) ((h + 3bp0 + 3) * xi^3 - 3 * xi^2 * (h + 2bp0 + 1) - 6xi * (bp0 + 1) - 6) end """ - apply(BulkModulusForm(), eos::Vinet, v::Real) + apply(BulkModulusForm(), eos::Vinet, v) Return the bulk modulus of a `Vinet` equation of state on volume `v`. """ -function apply(::BulkModulusForm, eos::Vinet, v::Real) +function apply(::BulkModulusForm, eos::Vinet, v) v0, b0, bp0 = fieldvalues(eos) x = (v / v0)^(1 / 3) @@ -667,11 +627,11 @@ function apply(::BulkModulusForm, eos::Vinet, v::Real) return -b0 / (2 * x^2) * (3x * (x - 1) * (bp0 - 1) + 2 * (x - 2)) * exp(-xi * (x - 1)) end """ - apply(BulkModulusForm(), eos::AntonSchmidt, v::Real) + apply(BulkModulusForm(), eos::AntonSchmidt, v) Return the bulk modulus of a `AntonSchmidt` equation of state on volume `v`. """ -function apply(::BulkModulusForm, eos::AntonSchmidt, v::Real) +function apply(::BulkModulusForm, eos::AntonSchmidt, v) v0, β, n = fieldvalues(eos) x = v / v0 @@ -701,7 +661,7 @@ end # This is a helper function and should not be exported. fieldvalues(eos::EquationOfState) = [getfield(eos, i) for i in 1:nfields(eos)] -Base.eltype(::Type{<:EquationOfState{T}}) where {T} = T +Base.eltype(T::Type{<:EquationOfState}) = promote_type(T.types...) # Base.getindex(eos::PolynomialEquationOfState{T,N}, index::Int64) where {T,N} = getindex(eos.data, index) # =============================== Miscellaneous ============================== # From c73d445fe6181a2cc3c862b2a3f32346d6003882 Mon Sep 17 00:00:00 2001 From: Qi Zhang Date: Sun, 29 Sep 2019 16:14:16 -0400 Subject: [PATCH 07/61] Add Unitful.jl to deps --- Project.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/Project.toml b/Project.toml index 6b9b24e..04c0ee5 100644 --- a/Project.toml +++ b/Project.toml @@ -11,6 +11,7 @@ MLStyle = "d8e11817-5142-5d16-987a-aa16d5891078" Polynomials = "f27b6e38-b328-58d1-80ce-0feddd5e7a45" Roots = "f2b01f46-fcfa-551c-844a-d8ac1e96c665" Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2" +Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d" [compat] julia = "1" From cb4c64322f3fed19e87daa5cf47b01418a4bd3c3 Mon Sep 17 00:00:00 2001 From: Qi Zhang Date: Sun, 29 Sep 2019 16:14:32 -0400 Subject: [PATCH 08/61] test: Deprecate test/Collections.jl --- test/Collections.jl | 29 ----------------------------- test/runtests.jl | 1 - 2 files changed, 30 deletions(-) delete mode 100644 test/Collections.jl diff --git a/test/Collections.jl b/test/Collections.jl deleted file mode 100644 index 51eabb9..0000000 --- a/test/Collections.jl +++ /dev/null @@ -1,29 +0,0 @@ -using Test - -using EquationsOfState.Collections - -@testset "Test EOS promotion" begin - @test typeof(Murnaghan(1, 2, 3.0, 0)) == Murnaghan{Float64} - @test typeof(BirchMurnaghan2nd(1, 2.0, 0)) == BirchMurnaghan2nd{Float64} - @test typeof(BirchMurnaghan3rd(1, 2, 3.0, 0)) == BirchMurnaghan3rd{Float64} - @test typeof(BirchMurnaghan4th(1, 2.0, 3, 4, 0)) == BirchMurnaghan4th{Float64} - @test typeof(Vinet(1, 2, 3.0, 0)) == Vinet{Float64} - @test typeof(PoirierTarantola2nd(1, 2.0, 0)) == PoirierTarantola2nd{Float64} - @test typeof(PoirierTarantola3rd(1, 2, 3.0, 0)) == PoirierTarantola3rd{Float64} - @test typeof(PoirierTarantola4th(1, 2, 3, 4, 0)) == PoirierTarantola4th{Int} - @test typeof(AntonSchmidt(1, 2, 3.0, 0)) == AntonSchmidt{Float64} - @test typeof(BreenanStacey(1, 2, 3.0, 0)) == BreenanStacey{Float64} -end - -@testset "Test default EOS parameter `e0` and promotion" begin - @test Murnaghan(1, 2, 3.0) == Murnaghan(1.0, 2.0, 3.0, 0.0) - @test BirchMurnaghan2nd(1, 2.0) == BirchMurnaghan2nd(1.0, 2.0, 0.0) - @test BirchMurnaghan3rd(1, 2, 3.0) == BirchMurnaghan3rd(1.0, 2.0, 3.0, 0.0) - @test BirchMurnaghan4th(1, 2.0, 3, 4) == BirchMurnaghan4th(1.0, 2.0, 3.0, 4.0, 0.0) - @test Vinet(1, 2, 3.0) == Vinet(1.0, 2.0, 3.0, 0.0) - @test PoirierTarantola2nd(1, 2.0) == PoirierTarantola2nd(1.0, 2.0, 0.0) - @test PoirierTarantola3rd(1, 2, 3.0) == PoirierTarantola3rd(1.0, 2.0, 3.0, 0.0) - @test PoirierTarantola4th(1, 2, 3, 4) == PoirierTarantola4th(1, 2, 3, 4, 0) - @test AntonSchmidt(1, 2, 3.0) == AntonSchmidt(1.0, 2.0, 3.0, 0.0) - @test BreenanStacey(1, 2, 3.0) == BreenanStacey(1.0, 2.0, 3.0, 0.0) -end diff --git a/test/runtests.jl b/test/runtests.jl index e082a3e..e64bb2c 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -2,7 +2,6 @@ using EquationsOfState using Test @testset "EquationsOfState.jl" begin - include("Collections.jl") include("FiniteStrains.jl") include("NonlinearFitting.jl") include("LinearFitting.jl") From 5b3ebdbd8215ca8fb45cfaba9d5180e309186994 Mon Sep 17 00:00:00 2001 From: Qi Zhang Date: Sun, 29 Sep 2019 16:15:51 -0400 Subject: [PATCH 09/61] Deprecate `similar_type` in src/Collections.jl --- src/Collections.jl | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/src/Collections.jl b/src/Collections.jl index e9d5595..19e72c9 100644 --- a/src/Collections.jl +++ b/src/Collections.jl @@ -643,21 +643,6 @@ end # ============================================================================ # # Miscellaneous # # ============================================================================ # -function allsubtypes(T::Type, types = Type[])::Vector{Type} - for S in subtypes(T) - types = allsubtypes(S, push!(types, S)) - end - types -end - -nonabstract(T::Type)::Vector{Type} = filter(!isabstracttype, allsubtypes(T)) - -for E in nonabstract(EquationOfState) - eval(quote - similar_type(::Type{A}, ::Type{T}) where {A<:$E,T} = $E{T} - end) -end - # This is a helper function and should not be exported. fieldvalues(eos::EquationOfState) = [getfield(eos, i) for i in 1:nfields(eos)] From c9b3d29977838d876d58885d43411c6295f4f42e Mon Sep 17 00:00:00 2001 From: Qi Zhang Date: Sun, 29 Sep 2019 19:10:39 -0400 Subject: [PATCH 10/61] Before making them abstract --- src/Collections.jl | 75 +++++++++++++++++++++++++++++++++++------ src/EquationsOfState.jl | 2 +- src/NonlinearFitting.jl | 34 +++++++++++++++++-- 3 files changed, 97 insertions(+), 14 deletions(-) diff --git a/src/Collections.jl b/src/Collections.jl index 19e72c9..9e17892 100644 --- a/src/Collections.jl +++ b/src/Collections.jl @@ -12,6 +12,8 @@ julia> module Collections using InteractiveUtils +using Unitful: AbstractQuantity, @u_str +import Unitful using EquationsOfState @@ -76,7 +78,14 @@ struct Murnaghan{A,B,C,D} <: EquationOfState bp0::C e0::D end -Murnaghan(v0, b0, bp0) = Murnaghan(v0, b0, bp0, 0) +Murnaghan(v0::Real, b0::Real, bp0::Real) = Murnaghan(v0, b0, bp0, 0) +Murnaghan(v0::AbstractQuantity, b0::AbstractQuantity, bp0::AbstractQuantity) = Murnaghan(v0, b0, bp0, 0u"eV") +Unitful.upreferred(eos::Murnaghan) = Murnaghan( + uconvert(u"angstrom^3", eos.v0), + uconvert(u"eV/angstrom^3", eos.b0), + uconvert(NoUnits, eos.bp0), + uconvert(u"eV", eos.e0) +) """ BirchMurnaghan2nd(v0, b0, e0=0) @@ -93,7 +102,13 @@ struct BirchMurnaghan2nd{A,B,C} <: FiniteStrainEquationOfState b0::B e0::C end -BirchMurnaghan2nd(v0, b0) = BirchMurnaghan2nd(v0, b0, 0) +BirchMurnaghan2nd(v0::Real, b0::Real) = BirchMurnaghan2nd(v0, b0, 0) +BirchMurnaghan2nd(v0::AbstractQuantity, b0::AbstractQuantity) = BirchMurnaghan2nd(v0, b0, 0u"eV") +Unitful.upreferred(eos::BirchMurnaghan2nd) = BirchMurnaghan2nd( + uconvert(u"angstrom^3", eos.v0), + uconvert(u"eV/angstrom^3", eos.b0), + uconvert(u"eV", eos.e0) +) """ BirchMurnaghan3rd(v0, b0, bp0, e0=0) @@ -112,7 +127,14 @@ struct BirchMurnaghan3rd{A,B,C,D} <: FiniteStrainEquationOfState bp0::C e0::D end -BirchMurnaghan3rd(v0, b0, bp0) = BirchMurnaghan3rd(v0, b0, bp0, 0) +BirchMurnaghan3rd(v0::Real, b0::Real, bp0::Real) = BirchMurnaghan3rd(v0, b0, bp0, 0) +BirchMurnaghan3rd(v0::AbstractQuantity, b0::AbstractQuantity, bp0::AbstractQuantity) = BirchMurnaghan3rd(v0, b0, bp0, 0u"eV") +Unitful.upreferred(eos::BirchMurnaghan3rd) = BirchMurnaghan3rd( + uconvert(u"angstrom^3", eos.v0), + uconvert(u"eV/angstrom^3", eos.b0), + uconvert(NoUnits, eos.bp0), + uconvert(u"eV", eos.e0) +) """ BirchMurnaghan4th(v0, b0, bp0, bpp0, e0=0) @@ -133,7 +155,15 @@ struct BirchMurnaghan4th{A,B,C,D,E} <: FiniteStrainEquationOfState bpp0::D e0::E end -BirchMurnaghan4th(v0, b0, bp0, bpp0) = BirchMurnaghan4th(v0, b0, bp0, bpp0, 0) +BirchMurnaghan4th(v0::Real, b0::Real, bp0::Real, bpp0::Real) = BirchMurnaghan4th(v0, b0, bp0, bpp0, 0) +BirchMurnaghan4th(v0::AbstractQuantity, b0::AbstractQuantity, bp0::AbstractQuantity, bpp0::AbstractQuantity) = BirchMurnaghan4th(v0, b0, bp0, bpp0, 0u"eV") +Unitful.upreferred(eos::BirchMurnaghan3rd) = BirchMurnaghan3rd( + uconvert(u"angstrom^3", eos.v0), + uconvert(u"eV/angstrom^3", eos.b0), + uconvert(NoUnits, eos.bp0), + uconvert(u"angstrom^3/eV", eos.bpp0) + uconvert(u"eV", eos.e0) +) """ PoirierTarantola2nd(v0, b0, e0=0) @@ -150,7 +180,13 @@ struct PoirierTarantola2nd{A,B,C} <: FiniteStrainEquationOfState b0::B e0::C end -PoirierTarantola2nd(v0, b0) = PoirierTarantola2nd(v0, b0, 0) +PoirierTarantola2nd(v0::Real, b0::Real) = PoirierTarantola2nd(v0, b0, 0) +PoirierTarantola2nd(v0::AbstractQuantity, b0::AbstractQuantity) = PoirierTarantola2nd(v0, b0, 0u"eV") +Unitful.upreferred(eos::PoirierTarantola2nd) = PoirierTarantola2nd( + uconvert(u"angstrom^3", eos.v0), + uconvert(u"eV/angstrom^3", eos.b0), + uconvert(u"eV", eos.e0) +) """ PoirierTarantola3rd(v0, b0, bp0, e0=0) @@ -169,7 +205,14 @@ struct PoirierTarantola3rd{A,B,C,D} <: FiniteStrainEquationOfState bp0::C e0::D end -PoirierTarantola3rd(v0, b0, bp0) = PoirierTarantola3rd(v0, b0, bp0, 0) +PoirierTarantola3rd(v0::Real, b0::Real, bp0::Real) = PoirierTarantola3rd(v0, b0, bp0, 0) +PoirierTarantola3rd(v0::AbstractQuantity, b0::AbstractQuantity, bp0::AbstractQuantity) = PoirierTarantola3rd(v0, b0, bp0, 0u"eV") +Unitful.upreferred(eos::PoirierTarantola3rd) = PoirierTarantola3rd( + uconvert(u"angstrom^3", eos.v0), + uconvert(u"eV/angstrom^3", eos.b0), + uconvert(NoUnits, eos.bp0), + uconvert(u"eV", eos.e0) +) """ PoirierTarantola4th(v0, b0, bp0, bpp0, e0=0) @@ -190,7 +233,15 @@ struct PoirierTarantola4th{A,B,C,D,E} <: FiniteStrainEquationOfState bpp0::D e0::E end -PoirierTarantola4th(v0, b0, bp0, bpp0) = PoirierTarantola4th(v0, b0, bp0, bpp0, 0) +PoirierTarantola4th(v0::Real, b0::Real, bp0::Real, bpp0::Real) = PoirierTarantola4th(v0, b0, bp0, bpp0, 0) +PoirierTarantola4th(v0::AbstractQuantity, b0::AbstractQuantity, bp0::AbstractQuantity, bpp0::AbstractQuantity) = PoirierTarantola4th(v0, b0, bp0, bpp0, 0u"eV") +Unitful.upreferred(eos::BirchMurnaghan3rd) = BirchMurnaghan3rd( + uconvert(u"angstrom^3", eos.v0), + uconvert(u"eV/angstrom^3", eos.b0), + uconvert(NoUnits, eos.bp0), + uconvert(u"angstrom^3/eV", eos.bpp0) + uconvert(u"eV", eos.e0) +) """ Vinet(v0, b0, bp0, e0=0) @@ -209,7 +260,8 @@ struct Vinet{A,B,C,D} <: EquationOfState bp0::C e0::D end -Vinet(v0, b0, bp0) = Vinet(v0, b0, bp0, 0) +Vinet(v0::Real, b0::Real, bp0::Real) = Vinet(v0, b0, bp0, 0) +Vinet(v0::AbstractQuantity, b0::AbstractQuantity, bp0::AbstractQuantity) = Vinet(v0, b0, bp0, 0u"eV") struct AntonSchmidt{A,B,C,D} <: EquationOfState v0::A @@ -217,7 +269,7 @@ struct AntonSchmidt{A,B,C,D} <: EquationOfState n::C e∞::D end -AntonSchmidt(v0, β, n) = AntonSchmidt(v0, β, n, 0) +AntonSchmidt(v0::Real, β::Real, n::Real) = AntonSchmidt(v0, β, n, 0) struct BreenanStacey{A,B,C,D} <: EquationOfState v0::A @@ -225,7 +277,7 @@ struct BreenanStacey{A,B,C,D} <: EquationOfState γ0::C e0::D end -BreenanStacey(v0, b0, γ0) = BreenanStacey(v0, b0, γ0, 0) +BreenanStacey(v0::Real, b0::Real, γ0::Real) = BreenanStacey(v0, b0, γ0, 0) # =================================== Types ================================== # @@ -648,6 +700,9 @@ fieldvalues(eos::EquationOfState) = [getfield(eos, i) for i in 1:nfields(eos)] Base.eltype(T::Type{<:EquationOfState}) = promote_type(T.types...) # Base.getindex(eos::PolynomialEquationOfState{T,N}, index::Int64) where {T,N} = getindex(eos.data, index) + +Unitful.promote_unit(::S, ::T) where {S<:Unitful.EnergyUnits,T<:Unitful.EnergyUnits} = u"eV" +Unitful.promote_unit(::S, ::T) where {S<:Unitful.LengthUnits,T<:Unitful.LengthUnits} = u"angstrom" # =============================== Miscellaneous ============================== # end diff --git a/src/EquationsOfState.jl b/src/EquationsOfState.jl index 4f8a386..7968549 100644 --- a/src/EquationsOfState.jl +++ b/src/EquationsOfState.jl @@ -2,7 +2,7 @@ module EquationsOfState include("prelude.jl") include("Collections.jl") -include("NonlinearFitting.jl") +# include("NonlinearFitting.jl") include("FiniteStrains.jl") include("LinearFitting.jl") include("Find.jl") diff --git a/src/NonlinearFitting.jl b/src/NonlinearFitting.jl index 75bf036..6051053 100644 --- a/src/NonlinearFitting.jl +++ b/src/NonlinearFitting.jl @@ -12,6 +12,8 @@ julia> module NonlinearFitting using LsqFit: curve_fit +using Unitful +import Unitful: AbstractQuantity, 𝐋, 𝐌, 𝐓 import ..EquationForm using ..Collections @@ -39,11 +41,37 @@ function lsqfit( debug = false, kwargs... ) where {E<:EquationOfState} - T = promote_type(eltype(eos), eltype(xdata), eltype(ydata), Float64) + + # T = promote_type(eltype(eos), eltype(xdata), eltype(ydata)) + # T <: P = Collections.similar_type(E, T) model(x, p) = map(apply(form, P(p...)), x) - fitted = curve_fit(model, T.(xdata), T.(ydata), T.(Collections.fieldvalues(eos)); kwargs...) - return debug ? fitted : P(fitted.param...) + return lsqfit(model, xdata, ydata, debug = debug, kwargs...) +end # function lsqfit +function lsqfit( + form::EnergyForm, + eos::EquationOfState, + xdata::AbstractVector{A}, + ydata::AbstractVector{B}; + debug = false, + kwargs... +) where {T<:Real,A<:AbstractQuantity{T,𝐋^3},B<:AbstractQuantity{T,𝐋^2*𝐌*𝐓^-2}} + @assert(eltype(eos) <: AbstractQuantity, "The equation of state must have units!") + xdata, ydata = uconvert.(u"angstrom^3", xdata), uconvert.(u"eV", ydata) + E = typeof(eos).name.wrapper + model(x, p) = map(apply(form, E(p...)), x) + return lsqfit(model, xdata, ydata, debug = debug, kwargs...) +end # function lsqfit +function lsqfit( + model::Function, + xdata::AbstractVector{A}, + ydata::AbstractVector{B}, + trial_params::AbstractVector{B}; + debug = false, + kwargs... +) where {A<:AbstractFloat,B<:AbstractFloat} + fitted = curve_fit(model, xdata, ydata, trial_params; kwargs...) + return debug ? fitted : fitted.param end # function lsqfit end From 077018bf8686098eca77653c5f392e6f328617f0 Mon Sep 17 00:00:00 2001 From: Qi Zhang Date: Sun, 29 Sep 2019 19:11:46 -0400 Subject: [PATCH 11/61] Deprecate `PolynomialEquationOfState` should be replaced in linear fitting --- src/Collections.jl | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/src/Collections.jl b/src/Collections.jl index 9e17892..404133a 100644 --- a/src/Collections.jl +++ b/src/Collections.jl @@ -20,7 +20,6 @@ using EquationsOfState export apply, EquationOfState, FiniteStrainEquationOfState, - # PolynomialEquationOfState, Murnaghan, BirchMurnaghan2nd, BirchMurnaghan3rd, @@ -49,18 +48,6 @@ An abstraction of finite strain equations of state. """ abstract type FiniteStrainEquationOfState <: EquationOfState end -# struct PolynomialEquationOfState{T<:Real,N} <: EquationOfState{T,N} -# data::NTuple{N,T} -# function PolynomialEquationOfState{T,N}(args::NTuple{N,T}) where {T,N} -# @assert N ≤ 10 -# new(args) -# end -# end -# function PolynomialEquationOfState(args...) -# T = Base.promote_typeof(args...) -# PolynomialEquationOfState{T,length(args)}(args) -# end - """ Murnaghan(v0, b0, bp0, e0=0) @@ -699,7 +686,6 @@ end fieldvalues(eos::EquationOfState) = [getfield(eos, i) for i in 1:nfields(eos)] Base.eltype(T::Type{<:EquationOfState}) = promote_type(T.types...) -# Base.getindex(eos::PolynomialEquationOfState{T,N}, index::Int64) where {T,N} = getindex(eos.data, index) Unitful.promote_unit(::S, ::T) where {S<:Unitful.EnergyUnits,T<:Unitful.EnergyUnits} = u"eV" Unitful.promote_unit(::S, ::T) where {S<:Unitful.LengthUnits,T<:Unitful.LengthUnits} = u"angstrom" From f409602a6aa1399edbe95f474585999ec4db6e89 Mon Sep 17 00:00:00 2001 From: Qi Zhang Date: Sun, 29 Sep 2019 19:44:56 -0400 Subject: [PATCH 12/61] Move all `upreferred` together --- src/Collections.jl | 91 +++++++++++++++++++++++++--------------------- 1 file changed, 49 insertions(+), 42 deletions(-) diff --git a/src/Collections.jl b/src/Collections.jl index 404133a..8908ee6 100644 --- a/src/Collections.jl +++ b/src/Collections.jl @@ -67,12 +67,6 @@ struct Murnaghan{A,B,C,D} <: EquationOfState end Murnaghan(v0::Real, b0::Real, bp0::Real) = Murnaghan(v0, b0, bp0, 0) Murnaghan(v0::AbstractQuantity, b0::AbstractQuantity, bp0::AbstractQuantity) = Murnaghan(v0, b0, bp0, 0u"eV") -Unitful.upreferred(eos::Murnaghan) = Murnaghan( - uconvert(u"angstrom^3", eos.v0), - uconvert(u"eV/angstrom^3", eos.b0), - uconvert(NoUnits, eos.bp0), - uconvert(u"eV", eos.e0) -) """ BirchMurnaghan2nd(v0, b0, e0=0) @@ -91,11 +85,6 @@ struct BirchMurnaghan2nd{A,B,C} <: FiniteStrainEquationOfState end BirchMurnaghan2nd(v0::Real, b0::Real) = BirchMurnaghan2nd(v0, b0, 0) BirchMurnaghan2nd(v0::AbstractQuantity, b0::AbstractQuantity) = BirchMurnaghan2nd(v0, b0, 0u"eV") -Unitful.upreferred(eos::BirchMurnaghan2nd) = BirchMurnaghan2nd( - uconvert(u"angstrom^3", eos.v0), - uconvert(u"eV/angstrom^3", eos.b0), - uconvert(u"eV", eos.e0) -) """ BirchMurnaghan3rd(v0, b0, bp0, e0=0) @@ -116,12 +105,6 @@ struct BirchMurnaghan3rd{A,B,C,D} <: FiniteStrainEquationOfState end BirchMurnaghan3rd(v0::Real, b0::Real, bp0::Real) = BirchMurnaghan3rd(v0, b0, bp0, 0) BirchMurnaghan3rd(v0::AbstractQuantity, b0::AbstractQuantity, bp0::AbstractQuantity) = BirchMurnaghan3rd(v0, b0, bp0, 0u"eV") -Unitful.upreferred(eos::BirchMurnaghan3rd) = BirchMurnaghan3rd( - uconvert(u"angstrom^3", eos.v0), - uconvert(u"eV/angstrom^3", eos.b0), - uconvert(NoUnits, eos.bp0), - uconvert(u"eV", eos.e0) -) """ BirchMurnaghan4th(v0, b0, bp0, bpp0, e0=0) @@ -144,13 +127,6 @@ struct BirchMurnaghan4th{A,B,C,D,E} <: FiniteStrainEquationOfState end BirchMurnaghan4th(v0::Real, b0::Real, bp0::Real, bpp0::Real) = BirchMurnaghan4th(v0, b0, bp0, bpp0, 0) BirchMurnaghan4th(v0::AbstractQuantity, b0::AbstractQuantity, bp0::AbstractQuantity, bpp0::AbstractQuantity) = BirchMurnaghan4th(v0, b0, bp0, bpp0, 0u"eV") -Unitful.upreferred(eos::BirchMurnaghan3rd) = BirchMurnaghan3rd( - uconvert(u"angstrom^3", eos.v0), - uconvert(u"eV/angstrom^3", eos.b0), - uconvert(NoUnits, eos.bp0), - uconvert(u"angstrom^3/eV", eos.bpp0) - uconvert(u"eV", eos.e0) -) """ PoirierTarantola2nd(v0, b0, e0=0) @@ -169,11 +145,6 @@ struct PoirierTarantola2nd{A,B,C} <: FiniteStrainEquationOfState end PoirierTarantola2nd(v0::Real, b0::Real) = PoirierTarantola2nd(v0, b0, 0) PoirierTarantola2nd(v0::AbstractQuantity, b0::AbstractQuantity) = PoirierTarantola2nd(v0, b0, 0u"eV") -Unitful.upreferred(eos::PoirierTarantola2nd) = PoirierTarantola2nd( - uconvert(u"angstrom^3", eos.v0), - uconvert(u"eV/angstrom^3", eos.b0), - uconvert(u"eV", eos.e0) -) """ PoirierTarantola3rd(v0, b0, bp0, e0=0) @@ -194,12 +165,6 @@ struct PoirierTarantola3rd{A,B,C,D} <: FiniteStrainEquationOfState end PoirierTarantola3rd(v0::Real, b0::Real, bp0::Real) = PoirierTarantola3rd(v0, b0, bp0, 0) PoirierTarantola3rd(v0::AbstractQuantity, b0::AbstractQuantity, bp0::AbstractQuantity) = PoirierTarantola3rd(v0, b0, bp0, 0u"eV") -Unitful.upreferred(eos::PoirierTarantola3rd) = PoirierTarantola3rd( - uconvert(u"angstrom^3", eos.v0), - uconvert(u"eV/angstrom^3", eos.b0), - uconvert(NoUnits, eos.bp0), - uconvert(u"eV", eos.e0) -) """ PoirierTarantola4th(v0, b0, bp0, bpp0, e0=0) @@ -222,13 +187,6 @@ struct PoirierTarantola4th{A,B,C,D,E} <: FiniteStrainEquationOfState end PoirierTarantola4th(v0::Real, b0::Real, bp0::Real, bpp0::Real) = PoirierTarantola4th(v0, b0, bp0, bpp0, 0) PoirierTarantola4th(v0::AbstractQuantity, b0::AbstractQuantity, bp0::AbstractQuantity, bpp0::AbstractQuantity) = PoirierTarantola4th(v0, b0, bp0, bpp0, 0u"eV") -Unitful.upreferred(eos::BirchMurnaghan3rd) = BirchMurnaghan3rd( - uconvert(u"angstrom^3", eos.v0), - uconvert(u"eV/angstrom^3", eos.b0), - uconvert(NoUnits, eos.bp0), - uconvert(u"angstrom^3/eV", eos.bpp0) - uconvert(u"eV", eos.e0) -) """ Vinet(v0, b0, bp0, e0=0) @@ -689,6 +647,55 @@ Base.eltype(T::Type{<:EquationOfState}) = promote_type(T.types...) Unitful.promote_unit(::S, ::T) where {S<:Unitful.EnergyUnits,T<:Unitful.EnergyUnits} = u"eV" Unitful.promote_unit(::S, ::T) where {S<:Unitful.LengthUnits,T<:Unitful.LengthUnits} = u"angstrom" + +Unitful.upreferred(eos::Murnaghan{<:AbstractQuantity,<:AbstractQuantity,<:AbstractQuantity,<:AbstractQuantity}) = Murnaghan( + uconvert(u"angstrom^3", eos.v0), + uconvert(u"eV/angstrom^3", eos.b0), + uconvert(NoUnits, eos.bp0), + uconvert(u"eV", eos.e0) +) +Unitful.upreferred(eos::BirchMurnaghan2nd{<:AbstractQuantity,<:AbstractQuantity,<:AbstractQuantity}) = BirchMurnaghan2nd( + uconvert(u"angstrom^3", eos.v0), + uconvert(u"eV/angstrom^3", eos.b0), + uconvert(u"eV", eos.e0) +) +Unitful.upreferred(eos::BirchMurnaghan3rd{<:AbstractQuantity,<:AbstractQuantity,<:AbstractQuantity,<:AbstractQuantity}) = BirchMurnaghan3rd( + uconvert(u"angstrom^3", eos.v0), + uconvert(u"eV/angstrom^3", eos.b0), + uconvert(NoUnits, eos.bp0), + uconvert(u"eV", eos.e0) +) +Unitful.upreferred(eos::BirchMurnaghan4th{<:AbstractQuantity,<:AbstractQuantity,<:AbstractQuantity,<:AbstractQuantity,<:AbstractQuantity}) = BirchMurnaghan4th( + uconvert(u"angstrom^3", eos.v0), + uconvert(u"eV/angstrom^3", eos.b0), + uconvert(NoUnits, eos.bp0), + uconvert(u"angstrom^3/eV", eos.bpp0) + uconvert(u"eV", eos.e0) +) +Unitful.upreferred(eos::PoirierTarantola2nd{<:AbstractQuantity,<:AbstractQuantity,<:AbstractQuantity}) = PoirierTarantola2nd( + uconvert(u"angstrom^3", eos.v0), + uconvert(u"eV/angstrom^3", eos.b0), + uconvert(u"eV", eos.e0) +) +Unitful.upreferred(eos::PoirierTarantola3rd{<:AbstractQuantity,<:AbstractQuantity,<:AbstractQuantity,<:AbstractQuantity}) = PoirierTarantola3rd( + uconvert(u"angstrom^3", eos.v0), + uconvert(u"eV/angstrom^3", eos.b0), + uconvert(NoUnits, eos.bp0), + uconvert(u"eV", eos.e0) +) +Unitful.upreferred(eos::PoirierTarantola4th{<:AbstractQuantity,<:AbstractQuantity,<:AbstractQuantity,<:AbstractQuantity,<:AbstractQuantity}) = PoirierTarantola4th( + uconvert(u"angstrom^3", eos.v0), + uconvert(u"eV/angstrom^3", eos.b0), + uconvert(NoUnits, eos.bp0), + uconvert(u"angstrom^3/eV", eos.bpp0) + uconvert(u"eV", eos.e0) +) +Unitful.upreferred(eos::Vinet{<:AbstractQuantity,<:AbstractQuantity,<:AbstractQuantity,<:AbstractQuantity}) = Vinet( + uconvert(u"angstrom^3", eos.v0), + uconvert(u"eV/angstrom^3", eos.b0), + uconvert(NoUnits, eos.bp0), + uconvert(u"eV", eos.e0) +) # =============================== Miscellaneous ============================== # end From e826f683dedaf25d422fd4d3e389a229f6d69f6b Mon Sep 17 00:00:00 2001 From: Qi Zhang Date: Sun, 29 Sep 2019 19:47:25 -0400 Subject: [PATCH 13/61] style: Reformat code --- src/Collections.jl | 180 +++++++++++++++++++++++++++------------- src/NonlinearFitting.jl | 8 +- 2 files changed, 126 insertions(+), 62 deletions(-) diff --git a/src/Collections.jl b/src/Collections.jl index 8908ee6..81f3cfe 100644 --- a/src/Collections.jl +++ b/src/Collections.jl @@ -66,7 +66,8 @@ struct Murnaghan{A,B,C,D} <: EquationOfState e0::D end Murnaghan(v0::Real, b0::Real, bp0::Real) = Murnaghan(v0, b0, bp0, 0) -Murnaghan(v0::AbstractQuantity, b0::AbstractQuantity, bp0::AbstractQuantity) = Murnaghan(v0, b0, bp0, 0u"eV") +Murnaghan(v0::AbstractQuantity, b0::AbstractQuantity, bp0::AbstractQuantity) = + Murnaghan(v0, b0, bp0, 0 * u"eV") """ BirchMurnaghan2nd(v0, b0, e0=0) @@ -84,7 +85,8 @@ struct BirchMurnaghan2nd{A,B,C} <: FiniteStrainEquationOfState e0::C end BirchMurnaghan2nd(v0::Real, b0::Real) = BirchMurnaghan2nd(v0, b0, 0) -BirchMurnaghan2nd(v0::AbstractQuantity, b0::AbstractQuantity) = BirchMurnaghan2nd(v0, b0, 0u"eV") +BirchMurnaghan2nd(v0::AbstractQuantity, b0::AbstractQuantity) = + BirchMurnaghan2nd(v0, b0, 0 * u"eV") """ BirchMurnaghan3rd(v0, b0, bp0, e0=0) @@ -104,7 +106,8 @@ struct BirchMurnaghan3rd{A,B,C,D} <: FiniteStrainEquationOfState e0::D end BirchMurnaghan3rd(v0::Real, b0::Real, bp0::Real) = BirchMurnaghan3rd(v0, b0, bp0, 0) -BirchMurnaghan3rd(v0::AbstractQuantity, b0::AbstractQuantity, bp0::AbstractQuantity) = BirchMurnaghan3rd(v0, b0, bp0, 0u"eV") +BirchMurnaghan3rd(v0::AbstractQuantity, b0::AbstractQuantity, bp0::AbstractQuantity) = + BirchMurnaghan3rd(v0, b0, bp0, 0 * u"eV") """ BirchMurnaghan4th(v0, b0, bp0, bpp0, e0=0) @@ -125,8 +128,14 @@ struct BirchMurnaghan4th{A,B,C,D,E} <: FiniteStrainEquationOfState bpp0::D e0::E end -BirchMurnaghan4th(v0::Real, b0::Real, bp0::Real, bpp0::Real) = BirchMurnaghan4th(v0, b0, bp0, bpp0, 0) -BirchMurnaghan4th(v0::AbstractQuantity, b0::AbstractQuantity, bp0::AbstractQuantity, bpp0::AbstractQuantity) = BirchMurnaghan4th(v0, b0, bp0, bpp0, 0u"eV") +BirchMurnaghan4th(v0::Real, b0::Real, bp0::Real, bpp0::Real) = + BirchMurnaghan4th(v0, b0, bp0, bpp0, 0) +BirchMurnaghan4th( + v0::AbstractQuantity, + b0::AbstractQuantity, + bp0::AbstractQuantity, + bpp0::AbstractQuantity, +) = BirchMurnaghan4th(v0, b0, bp0, bpp0, 0 * u"eV") """ PoirierTarantola2nd(v0, b0, e0=0) @@ -144,7 +153,8 @@ struct PoirierTarantola2nd{A,B,C} <: FiniteStrainEquationOfState e0::C end PoirierTarantola2nd(v0::Real, b0::Real) = PoirierTarantola2nd(v0, b0, 0) -PoirierTarantola2nd(v0::AbstractQuantity, b0::AbstractQuantity) = PoirierTarantola2nd(v0, b0, 0u"eV") +PoirierTarantola2nd(v0::AbstractQuantity, b0::AbstractQuantity) = + PoirierTarantola2nd(v0, b0, 0 * u"eV") """ PoirierTarantola3rd(v0, b0, bp0, e0=0) @@ -164,7 +174,8 @@ struct PoirierTarantola3rd{A,B,C,D} <: FiniteStrainEquationOfState e0::D end PoirierTarantola3rd(v0::Real, b0::Real, bp0::Real) = PoirierTarantola3rd(v0, b0, bp0, 0) -PoirierTarantola3rd(v0::AbstractQuantity, b0::AbstractQuantity, bp0::AbstractQuantity) = PoirierTarantola3rd(v0, b0, bp0, 0u"eV") +PoirierTarantola3rd(v0::AbstractQuantity, b0::AbstractQuantity, bp0::AbstractQuantity) = + PoirierTarantola3rd(v0, b0, bp0, 0 * u"eV") """ PoirierTarantola4th(v0, b0, bp0, bpp0, e0=0) @@ -185,8 +196,14 @@ struct PoirierTarantola4th{A,B,C,D,E} <: FiniteStrainEquationOfState bpp0::D e0::E end -PoirierTarantola4th(v0::Real, b0::Real, bp0::Real, bpp0::Real) = PoirierTarantola4th(v0, b0, bp0, bpp0, 0) -PoirierTarantola4th(v0::AbstractQuantity, b0::AbstractQuantity, bp0::AbstractQuantity, bpp0::AbstractQuantity) = PoirierTarantola4th(v0, b0, bp0, bpp0, 0u"eV") +PoirierTarantola4th(v0::Real, b0::Real, bp0::Real, bpp0::Real) = + PoirierTarantola4th(v0, b0, bp0, bpp0, 0) +PoirierTarantola4th( + v0::AbstractQuantity, + b0::AbstractQuantity, + bp0::AbstractQuantity, + bpp0::AbstractQuantity, +) = PoirierTarantola4th(v0, b0, bp0, bpp0, 0 * u"eV") """ Vinet(v0, b0, bp0, e0=0) @@ -206,7 +223,8 @@ struct Vinet{A,B,C,D} <: EquationOfState e0::D end Vinet(v0::Real, b0::Real, bp0::Real) = Vinet(v0, b0, bp0, 0) -Vinet(v0::AbstractQuantity, b0::AbstractQuantity, bp0::AbstractQuantity) = Vinet(v0, b0, bp0, 0u"eV") +Vinet(v0::AbstractQuantity, b0::AbstractQuantity, bp0::AbstractQuantity) = + Vinet(v0, b0, bp0, 0 * u"eV") struct AntonSchmidt{A,B,C,D} <: EquationOfState v0::A @@ -648,54 +666,100 @@ Base.eltype(T::Type{<:EquationOfState}) = promote_type(T.types...) Unitful.promote_unit(::S, ::T) where {S<:Unitful.EnergyUnits,T<:Unitful.EnergyUnits} = u"eV" Unitful.promote_unit(::S, ::T) where {S<:Unitful.LengthUnits,T<:Unitful.LengthUnits} = u"angstrom" -Unitful.upreferred(eos::Murnaghan{<:AbstractQuantity,<:AbstractQuantity,<:AbstractQuantity,<:AbstractQuantity}) = Murnaghan( - uconvert(u"angstrom^3", eos.v0), - uconvert(u"eV/angstrom^3", eos.b0), - uconvert(NoUnits, eos.bp0), - uconvert(u"eV", eos.e0) -) -Unitful.upreferred(eos::BirchMurnaghan2nd{<:AbstractQuantity,<:AbstractQuantity,<:AbstractQuantity}) = BirchMurnaghan2nd( - uconvert(u"angstrom^3", eos.v0), - uconvert(u"eV/angstrom^3", eos.b0), - uconvert(u"eV", eos.e0) -) -Unitful.upreferred(eos::BirchMurnaghan3rd{<:AbstractQuantity,<:AbstractQuantity,<:AbstractQuantity,<:AbstractQuantity}) = BirchMurnaghan3rd( - uconvert(u"angstrom^3", eos.v0), - uconvert(u"eV/angstrom^3", eos.b0), - uconvert(NoUnits, eos.bp0), - uconvert(u"eV", eos.e0) -) -Unitful.upreferred(eos::BirchMurnaghan4th{<:AbstractQuantity,<:AbstractQuantity,<:AbstractQuantity,<:AbstractQuantity,<:AbstractQuantity}) = BirchMurnaghan4th( - uconvert(u"angstrom^3", eos.v0), - uconvert(u"eV/angstrom^3", eos.b0), - uconvert(NoUnits, eos.bp0), - uconvert(u"angstrom^3/eV", eos.bpp0) - uconvert(u"eV", eos.e0) -) -Unitful.upreferred(eos::PoirierTarantola2nd{<:AbstractQuantity,<:AbstractQuantity,<:AbstractQuantity}) = PoirierTarantola2nd( - uconvert(u"angstrom^3", eos.v0), - uconvert(u"eV/angstrom^3", eos.b0), - uconvert(u"eV", eos.e0) -) -Unitful.upreferred(eos::PoirierTarantola3rd{<:AbstractQuantity,<:AbstractQuantity,<:AbstractQuantity,<:AbstractQuantity}) = PoirierTarantola3rd( - uconvert(u"angstrom^3", eos.v0), - uconvert(u"eV/angstrom^3", eos.b0), - uconvert(NoUnits, eos.bp0), - uconvert(u"eV", eos.e0) -) -Unitful.upreferred(eos::PoirierTarantola4th{<:AbstractQuantity,<:AbstractQuantity,<:AbstractQuantity,<:AbstractQuantity,<:AbstractQuantity}) = PoirierTarantola4th( - uconvert(u"angstrom^3", eos.v0), - uconvert(u"eV/angstrom^3", eos.b0), - uconvert(NoUnits, eos.bp0), - uconvert(u"angstrom^3/eV", eos.bpp0) - uconvert(u"eV", eos.e0) -) -Unitful.upreferred(eos::Vinet{<:AbstractQuantity,<:AbstractQuantity,<:AbstractQuantity,<:AbstractQuantity}) = Vinet( - uconvert(u"angstrom^3", eos.v0), - uconvert(u"eV/angstrom^3", eos.b0), - uconvert(NoUnits, eos.bp0), - uconvert(u"eV", eos.e0) -) +Unitful.upreferred(eos::Murnaghan{ + <:AbstractQuantity, + <:AbstractQuantity, + <:AbstractQuantity, + <:AbstractQuantity, +}) = + Murnaghan( + uconvert(u"angstrom^3", eos.v0), + uconvert(u"eV/angstrom^3", eos.b0), + uconvert(NoUnits, eos.bp0), + uconvert(u"eV", eos.e0), + ) +Unitful.upreferred(eos::BirchMurnaghan2nd{ + <:AbstractQuantity, + <:AbstractQuantity, + <:AbstractQuantity, +}) = + BirchMurnaghan2nd( + uconvert(u"angstrom^3", eos.v0), + uconvert(u"eV/angstrom^3", eos.b0), + uconvert(u"eV", eos.e0), + ) +Unitful.upreferred(eos::BirchMurnaghan3rd{ + <:AbstractQuantity, + <:AbstractQuantity, + <:AbstractQuantity, + <:AbstractQuantity, +}) = + BirchMurnaghan3rd( + uconvert(u"angstrom^3", eos.v0), + uconvert(u"eV/angstrom^3", eos.b0), + uconvert(NoUnits, eos.bp0), + uconvert(u"eV", eos.e0), + ) +Unitful.upreferred(eos::BirchMurnaghan4th{ + <:AbstractQuantity, + <:AbstractQuantity, + <:AbstractQuantity, + <:AbstractQuantity, + <:AbstractQuantity, +}) = + BirchMurnaghan4th( + uconvert(u"angstrom^3", eos.v0), + uconvert(u"eV/angstrom^3", eos.b0), + uconvert(NoUnits, eos.bp0), + uconvert(u"angstrom^3/eV", eos.bpp0) * uconvert(u"eV", eos.e0), + ) +Unitful.upreferred(eos::PoirierTarantola2nd{ + <:AbstractQuantity, + <:AbstractQuantity, + <:AbstractQuantity, +}) = + PoirierTarantola2nd( + uconvert(u"angstrom^3", eos.v0), + uconvert(u"eV/angstrom^3", eos.b0), + uconvert(u"eV", eos.e0), + ) +Unitful.upreferred(eos::PoirierTarantola3rd{ + <:AbstractQuantity, + <:AbstractQuantity, + <:AbstractQuantity, + <:AbstractQuantity, +}) = + PoirierTarantola3rd( + uconvert(u"angstrom^3", eos.v0), + uconvert(u"eV/angstrom^3", eos.b0), + uconvert(NoUnits, eos.bp0), + uconvert(u"eV", eos.e0), + ) +Unitful.upreferred(eos::PoirierTarantola4th{ + <:AbstractQuantity, + <:AbstractQuantity, + <:AbstractQuantity, + <:AbstractQuantity, + <:AbstractQuantity, +}) = + PoirierTarantola4th( + uconvert(u"angstrom^3", eos.v0), + uconvert(u"eV/angstrom^3", eos.b0), + uconvert(NoUnits, eos.bp0), + uconvert(u"angstrom^3/eV", eos.bpp0) * uconvert(u"eV", eos.e0), + ) +Unitful.upreferred(eos::Vinet{ + <:AbstractQuantity, + <:AbstractQuantity, + <:AbstractQuantity, + <:AbstractQuantity, +}) = + Vinet( + uconvert(u"angstrom^3", eos.v0), + uconvert(u"eV/angstrom^3", eos.b0), + uconvert(NoUnits, eos.bp0), + uconvert(u"eV", eos.e0), + ) # =============================== Miscellaneous ============================== # end diff --git a/src/NonlinearFitting.jl b/src/NonlinearFitting.jl index 6051053..aa6317c 100644 --- a/src/NonlinearFitting.jl +++ b/src/NonlinearFitting.jl @@ -39,7 +39,7 @@ function lsqfit( xdata::AbstractVector, ydata::AbstractVector; debug = false, - kwargs... + kwargs..., ) where {E<:EquationOfState} # T = promote_type(eltype(eos), eltype(xdata), eltype(ydata)) @@ -54,8 +54,8 @@ function lsqfit( xdata::AbstractVector{A}, ydata::AbstractVector{B}; debug = false, - kwargs... -) where {T<:Real,A<:AbstractQuantity{T,𝐋^3},B<:AbstractQuantity{T,𝐋^2*𝐌*𝐓^-2}} + kwargs..., +) where {T<:Real,A<:AbstractQuantity{T,𝐋^3},B<:AbstractQuantity{T,𝐋^2 * 𝐌 * 𝐓^-2}} @assert(eltype(eos) <: AbstractQuantity, "The equation of state must have units!") xdata, ydata = uconvert.(u"angstrom^3", xdata), uconvert.(u"eV", ydata) E = typeof(eos).name.wrapper @@ -68,7 +68,7 @@ function lsqfit( ydata::AbstractVector{B}, trial_params::AbstractVector{B}; debug = false, - kwargs... + kwargs..., ) where {A<:AbstractFloat,B<:AbstractFloat} fitted = curve_fit(model, xdata, ydata, trial_params; kwargs...) return debug ? fitted : fitted.param From 5b7c1a5a59ded63e8924ba330e99a3489f98c2d3 Mon Sep 17 00:00:00 2001 From: Qi Zhang Date: Sun, 29 Sep 2019 20:29:51 -0400 Subject: [PATCH 14/61] feat: Rewrite NonlinearFitting.jl with units --- src/Collections.jl | 1 + src/EquationsOfState.jl | 2 +- src/NonlinearFitting.jl | 54 +++++++++++++++++++++++------------------ 3 files changed, 32 insertions(+), 25 deletions(-) diff --git a/src/Collections.jl b/src/Collections.jl index 81f3cfe..cf03f12 100644 --- a/src/Collections.jl +++ b/src/Collections.jl @@ -665,6 +665,7 @@ Base.eltype(T::Type{<:EquationOfState}) = promote_type(T.types...) Unitful.promote_unit(::S, ::T) where {S<:Unitful.EnergyUnits,T<:Unitful.EnergyUnits} = u"eV" Unitful.promote_unit(::S, ::T) where {S<:Unitful.LengthUnits,T<:Unitful.LengthUnits} = u"angstrom" +Unitful.promote_unit(::S, ::T) where {S<:Unitful.PressureUnits,T<:Unitful.PressureUnits} = u"eV/angstrom^3" Unitful.upreferred(eos::Murnaghan{ <:AbstractQuantity, diff --git a/src/EquationsOfState.jl b/src/EquationsOfState.jl index 7968549..4f8a386 100644 --- a/src/EquationsOfState.jl +++ b/src/EquationsOfState.jl @@ -2,7 +2,7 @@ module EquationsOfState include("prelude.jl") include("Collections.jl") -# include("NonlinearFitting.jl") +include("NonlinearFitting.jl") include("FiniteStrains.jl") include("LinearFitting.jl") include("Find.jl") diff --git a/src/NonlinearFitting.jl b/src/NonlinearFitting.jl index aa6317c..a1c2b95 100644 --- a/src/NonlinearFitting.jl +++ b/src/NonlinearFitting.jl @@ -20,6 +20,13 @@ using ..Collections export lsqfit +abstract type UnitTrait end +struct NoUnit <: UnitTrait end +struct HasUnit <: UnitTrait end + +_traitfn(T::Type{<:Number}) = NoUnit +_traitfn(T::Type{<:AbstractQuantity}) = HasUnit + """ lsqfit(form, eos, xdata, ydata; debug = false, kwargs...) @@ -35,43 +42,42 @@ Fit an equation of state using least-squares fitting method (with the Levenberg- """ function lsqfit( form::EquationForm, - eos::E, + eos::EquationOfState, xdata::AbstractVector, ydata::AbstractVector; - debug = false, kwargs..., -) where {E<:EquationOfState} - - # T = promote_type(eltype(eos), eltype(xdata), eltype(ydata)) - # T <: - P = Collections.similar_type(E, T) - model(x, p) = map(apply(form, P(p...)), x) - return lsqfit(model, xdata, ydata, debug = debug, kwargs...) -end # function lsqfit +) + T = eltype(eos) + return lsqfit(form, eos, xdata, ydata, _traitfn(T), kwargs...) +end # function lsqfit function lsqfit( - form::EnergyForm, + form::EquationForm, eos::EquationOfState, - xdata::AbstractVector{A}, - ydata::AbstractVector{B}; + xdata::AbstractVector, + ydata::AbstractVector, + trait::Type{NoUnit}; debug = false, kwargs..., -) where {T<:Real,A<:AbstractQuantity{T,𝐋^3},B<:AbstractQuantity{T,𝐋^2 * 𝐌 * 𝐓^-2}} - @assert(eltype(eos) <: AbstractQuantity, "The equation of state must have units!") - xdata, ydata = uconvert.(u"angstrom^3", xdata), uconvert.(u"eV", ydata) +) + T = promote_type(eltype(eos), eltype(xdata), eltype(ydata), Float64) E = typeof(eos).name.wrapper model(x, p) = map(apply(form, E(p...)), x) - return lsqfit(model, xdata, ydata, debug = debug, kwargs...) + fitted = curve_fit(model, T.(xdata), T.(ydata), T.(Collections.fieldvalues(eos)); kwargs...) + return debug ? fitted : E(fitted.param...) end # function lsqfit function lsqfit( - model::Function, - xdata::AbstractVector{A}, - ydata::AbstractVector{B}, - trial_params::AbstractVector{B}; + form::EquationForm, + eos::EquationOfState, + xdata::AbstractVector, + ydata::AbstractVector, + trait::Type{HasUnit}; debug = false, kwargs..., -) where {A<:AbstractFloat,B<:AbstractFloat} - fitted = curve_fit(model, xdata, ydata, trial_params; kwargs...) - return debug ? fitted : fitted.param +) + E = typeof(eos).name.wrapper + trial_params = map(ustrip, Collections.fieldvalues(upreferred(eos))) + xdata, ydata = ustrip.(promote(xdata...)), ustrip.(promote(ydata...)) + return lsqfit(form, E(trial_params...), xdata, ydata, debug = debug, kwargs...) end # function lsqfit end From 0290ea9eddc37faf5f78152f3d272d5b0c974386 Mon Sep 17 00:00:00 2001 From: Qi Zhang Date: Sun, 29 Sep 2019 22:56:19 -0400 Subject: [PATCH 15/61] Make nonlinear fitting with unit work --- src/Collections.jl | 5 ++++- src/NonlinearFitting.jl | 5 ++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/Collections.jl b/src/Collections.jl index cf03f12..1c27186 100644 --- a/src/Collections.jl +++ b/src/Collections.jl @@ -12,7 +12,7 @@ julia> module Collections using InteractiveUtils -using Unitful: AbstractQuantity, @u_str +using Unitful: AbstractQuantity, @u_str, uconvert, NoUnits, 𝐋, 𝐌, 𝐓, Dimension, Dimensions import Unitful using EquationsOfState @@ -667,6 +667,9 @@ Unitful.promote_unit(::S, ::T) where {S<:Unitful.EnergyUnits,T<:Unitful.EnergyUn Unitful.promote_unit(::S, ::T) where {S<:Unitful.LengthUnits,T<:Unitful.LengthUnits} = u"angstrom" Unitful.promote_unit(::S, ::T) where {S<:Unitful.PressureUnits,T<:Unitful.PressureUnits} = u"eV/angstrom^3" +Unitful.upreferred(::Dimensions{(Dimension{:Length}(2//1),Dimension{:Mass}(1//1),Dimension{:Time}(-2//1))}) = u"eV" +Unitful.upreferred(::Dimensions{(Dimension{:Length}(3//1),)}) = u"angstrom^3" +Unitful.upreferred(::Dimensions{(Dimension{:Length}(-1//1),Dimension{:Mass}(1//1),Dimension{:Time}(-2//1))}) = u"eV/angstrom^3" Unitful.upreferred(eos::Murnaghan{ <:AbstractQuantity, <:AbstractQuantity, diff --git a/src/NonlinearFitting.jl b/src/NonlinearFitting.jl index a1c2b95..a9bc2c8 100644 --- a/src/NonlinearFitting.jl +++ b/src/NonlinearFitting.jl @@ -71,13 +71,12 @@ function lsqfit( xdata::AbstractVector, ydata::AbstractVector, trait::Type{HasUnit}; - debug = false, kwargs..., ) E = typeof(eos).name.wrapper trial_params = map(ustrip, Collections.fieldvalues(upreferred(eos))) - xdata, ydata = ustrip.(promote(xdata...)), ustrip.(promote(ydata...)) - return lsqfit(form, E(trial_params...), xdata, ydata, debug = debug, kwargs...) + xdata, ydata = map(ustrip ∘ upreferred, xdata), map(ustrip ∘ upreferred, ydata) + return lsqfit(form, E(trial_params...), xdata, ydata, kwargs...) end # function lsqfit end From 9f61a51f45e4479f97b9887ddc9d74c6622413e4 Mon Sep 17 00:00:00 2001 From: Qi Zhang Date: Mon, 30 Sep 2019 00:42:51 -0400 Subject: [PATCH 16/61] Keep track of the unit when fitting --- src/NonlinearFitting.jl | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/NonlinearFitting.jl b/src/NonlinearFitting.jl index a9bc2c8..ca390dc 100644 --- a/src/NonlinearFitting.jl +++ b/src/NonlinearFitting.jl @@ -74,9 +74,15 @@ function lsqfit( kwargs..., ) E = typeof(eos).name.wrapper + units = unit.(Collections.fieldvalues(eos)) trial_params = map(ustrip, Collections.fieldvalues(upreferred(eos))) xdata, ydata = map(ustrip ∘ upreferred, xdata), map(ustrip ∘ upreferred, ydata) - return lsqfit(form, E(trial_params...), xdata, ydata, kwargs...) + result = lsqfit(form, E(trial_params...), xdata, ydata, kwargs...) + if result isa EquationOfState + E( + [uconvert(u, Collections.fieldvalues(result)[i] * upreferred(u)) for (i, u) in enumerate(units)]... + ) + end end # function lsqfit end From f88aeb0acba559f662fbccb8747f2d8d8aba9872 Mon Sep 17 00:00:00 2001 From: Qi Zhang Date: Mon, 30 Sep 2019 01:03:10 -0400 Subject: [PATCH 17/61] Add UnitfulAstro.jl to deps temporarily for unit "angstrom" --- Project.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/Project.toml b/Project.toml index 04c0ee5..2782390 100644 --- a/Project.toml +++ b/Project.toml @@ -12,6 +12,7 @@ Polynomials = "f27b6e38-b328-58d1-80ce-0feddd5e7a45" Roots = "f2b01f46-fcfa-551c-844a-d8ac1e96c665" Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2" Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d" +UnitfulAstro = "6112ee07-acf9-5e0f-b108-d242c714bf9f" [compat] julia = "1" From 44de5dbf5712c57a9cba9162f0056477e1493a75 Mon Sep 17 00:00:00 2001 From: Qi Zhang Date: Mon, 30 Sep 2019 01:08:15 -0400 Subject: [PATCH 18/61] Fix `LoadError: Symbol angstrom could not be found in registered unit modules` --- src/Collections.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Collections.jl b/src/Collections.jl index 1c27186..e76bbb2 100644 --- a/src/Collections.jl +++ b/src/Collections.jl @@ -14,6 +14,7 @@ module Collections using InteractiveUtils using Unitful: AbstractQuantity, @u_str, uconvert, NoUnits, 𝐋, 𝐌, 𝐓, Dimension, Dimensions import Unitful +using UnitfulAstro using EquationsOfState From e3dfd4a9bcf88b3f097685e9b6d1a8eb5d6d5b34 Mon Sep 17 00:00:00 2001 From: Qi Zhang Date: Mon, 30 Sep 2019 02:36:16 -0400 Subject: [PATCH 19/61] Implement trait `HasUnit` following SimpleTraits.jl --- src/NonlinearFitting.jl | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/src/NonlinearFitting.jl b/src/NonlinearFitting.jl index ca390dc..9c1f712 100644 --- a/src/NonlinearFitting.jl +++ b/src/NonlinearFitting.jl @@ -20,12 +20,13 @@ using ..Collections export lsqfit -abstract type UnitTrait end -struct NoUnit <: UnitTrait end -struct HasUnit <: UnitTrait end +# This idea is borrowed from [SimpleTraits.jl](https://github.com/mauro3/SimpleTraits.jl/blob/master/src/SimpleTraits.jl). +abstract type Trait end +abstract type Not{T<:Trait} <: Trait end +struct HasUnit <: Trait end -_traitfn(T::Type{<:Number}) = NoUnit -_traitfn(T::Type{<:AbstractQuantity}) = HasUnit +_unit_trait(T::Type{<:Real}) = Not{HasUnit} +_unit_trait(T::Type{<:AbstractQuantity}) = HasUnit """ lsqfit(form, eos, xdata, ydata; debug = false, kwargs...) @@ -48,29 +49,29 @@ function lsqfit( kwargs..., ) T = eltype(eos) - return lsqfit(form, eos, xdata, ydata, _traitfn(T), kwargs...) + return lsqfit(_unit_trait(T), form, eos, xdata, ydata, kwargs...) end # function lsqfit function lsqfit( + ::Type{Not{HasUnit}}, form::EquationForm, eos::EquationOfState, xdata::AbstractVector, - ydata::AbstractVector, - trait::Type{NoUnit}; + ydata::AbstractVector; debug = false, kwargs..., ) T = promote_type(eltype(eos), eltype(xdata), eltype(ydata), Float64) E = typeof(eos).name.wrapper model(x, p) = map(apply(form, E(p...)), x) - fitted = curve_fit(model, T.(xdata), T.(ydata), T.(Collections.fieldvalues(eos)); kwargs...) + fitted = curve_fit(model, T.(xdata), T.(ydata), T.(Collections.fieldvalues(eos)), kwargs...) return debug ? fitted : E(fitted.param...) end # function lsqfit function lsqfit( + ::Type{HasUnit}, form::EquationForm, eos::EquationOfState, xdata::AbstractVector, - ydata::AbstractVector, - trait::Type{HasUnit}; + ydata::AbstractVector; kwargs..., ) E = typeof(eos).name.wrapper From fcc63d9c502e358c77d64caaf1ebc0a4ba73dcad Mon Sep 17 00:00:00 2001 From: Qi Zhang Date: Mon, 30 Sep 2019 03:02:18 -0400 Subject: [PATCH 20/61] refactor: Simplify `lsqfit` --- src/NonlinearFitting.jl | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/NonlinearFitting.jl b/src/NonlinearFitting.jl index 9c1f712..d54182c 100644 --- a/src/NonlinearFitting.jl +++ b/src/NonlinearFitting.jl @@ -75,15 +75,17 @@ function lsqfit( kwargs..., ) E = typeof(eos).name.wrapper - units = unit.(Collections.fieldvalues(eos)) - trial_params = map(ustrip, Collections.fieldvalues(upreferred(eos))) - xdata, ydata = map(ustrip ∘ upreferred, xdata), map(ustrip ∘ upreferred, ydata) + values = Collections.fieldvalues(eos) + original_units = map(unit, values) + trial_params, xdata, ydata = [map(ustrip ∘ upreferred, x) for x in (values, xdata, ydata)] result = lsqfit(form, E(trial_params...), xdata, ydata, kwargs...) if result isa EquationOfState - E( - [uconvert(u, Collections.fieldvalues(result)[i] * upreferred(u)) for (i, u) in enumerate(units)]... + data = Collections.fieldvalues(result) + return E( + [uconvert(u, data[i] * upreferred(u)) for (i, u) in enumerate(original_units)]... ) end + return result end # function lsqfit end From 0e70bf01fc3653bfdd13bc87162396b74cbfd421 Mon Sep 17 00:00:00 2001 From: Qi Zhang Date: Mon, 30 Sep 2019 03:03:29 -0400 Subject: [PATCH 21/61] Remove unneeded `promote_unit` & `upreferred` --- src/Collections.jl | 98 ---------------------------------------------- 1 file changed, 98 deletions(-) diff --git a/src/Collections.jl b/src/Collections.jl index e76bbb2..325c71f 100644 --- a/src/Collections.jl +++ b/src/Collections.jl @@ -664,107 +664,9 @@ fieldvalues(eos::EquationOfState) = [getfield(eos, i) for i in 1:nfields(eos)] Base.eltype(T::Type{<:EquationOfState}) = promote_type(T.types...) -Unitful.promote_unit(::S, ::T) where {S<:Unitful.EnergyUnits,T<:Unitful.EnergyUnits} = u"eV" -Unitful.promote_unit(::S, ::T) where {S<:Unitful.LengthUnits,T<:Unitful.LengthUnits} = u"angstrom" -Unitful.promote_unit(::S, ::T) where {S<:Unitful.PressureUnits,T<:Unitful.PressureUnits} = u"eV/angstrom^3" - Unitful.upreferred(::Dimensions{(Dimension{:Length}(2//1),Dimension{:Mass}(1//1),Dimension{:Time}(-2//1))}) = u"eV" Unitful.upreferred(::Dimensions{(Dimension{:Length}(3//1),)}) = u"angstrom^3" Unitful.upreferred(::Dimensions{(Dimension{:Length}(-1//1),Dimension{:Mass}(1//1),Dimension{:Time}(-2//1))}) = u"eV/angstrom^3" -Unitful.upreferred(eos::Murnaghan{ - <:AbstractQuantity, - <:AbstractQuantity, - <:AbstractQuantity, - <:AbstractQuantity, -}) = - Murnaghan( - uconvert(u"angstrom^3", eos.v0), - uconvert(u"eV/angstrom^3", eos.b0), - uconvert(NoUnits, eos.bp0), - uconvert(u"eV", eos.e0), - ) -Unitful.upreferred(eos::BirchMurnaghan2nd{ - <:AbstractQuantity, - <:AbstractQuantity, - <:AbstractQuantity, -}) = - BirchMurnaghan2nd( - uconvert(u"angstrom^3", eos.v0), - uconvert(u"eV/angstrom^3", eos.b0), - uconvert(u"eV", eos.e0), - ) -Unitful.upreferred(eos::BirchMurnaghan3rd{ - <:AbstractQuantity, - <:AbstractQuantity, - <:AbstractQuantity, - <:AbstractQuantity, -}) = - BirchMurnaghan3rd( - uconvert(u"angstrom^3", eos.v0), - uconvert(u"eV/angstrom^3", eos.b0), - uconvert(NoUnits, eos.bp0), - uconvert(u"eV", eos.e0), - ) -Unitful.upreferred(eos::BirchMurnaghan4th{ - <:AbstractQuantity, - <:AbstractQuantity, - <:AbstractQuantity, - <:AbstractQuantity, - <:AbstractQuantity, -}) = - BirchMurnaghan4th( - uconvert(u"angstrom^3", eos.v0), - uconvert(u"eV/angstrom^3", eos.b0), - uconvert(NoUnits, eos.bp0), - uconvert(u"angstrom^3/eV", eos.bpp0) * uconvert(u"eV", eos.e0), - ) -Unitful.upreferred(eos::PoirierTarantola2nd{ - <:AbstractQuantity, - <:AbstractQuantity, - <:AbstractQuantity, -}) = - PoirierTarantola2nd( - uconvert(u"angstrom^3", eos.v0), - uconvert(u"eV/angstrom^3", eos.b0), - uconvert(u"eV", eos.e0), - ) -Unitful.upreferred(eos::PoirierTarantola3rd{ - <:AbstractQuantity, - <:AbstractQuantity, - <:AbstractQuantity, - <:AbstractQuantity, -}) = - PoirierTarantola3rd( - uconvert(u"angstrom^3", eos.v0), - uconvert(u"eV/angstrom^3", eos.b0), - uconvert(NoUnits, eos.bp0), - uconvert(u"eV", eos.e0), - ) -Unitful.upreferred(eos::PoirierTarantola4th{ - <:AbstractQuantity, - <:AbstractQuantity, - <:AbstractQuantity, - <:AbstractQuantity, - <:AbstractQuantity, -}) = - PoirierTarantola4th( - uconvert(u"angstrom^3", eos.v0), - uconvert(u"eV/angstrom^3", eos.b0), - uconvert(NoUnits, eos.bp0), - uconvert(u"angstrom^3/eV", eos.bpp0) * uconvert(u"eV", eos.e0), - ) -Unitful.upreferred(eos::Vinet{ - <:AbstractQuantity, - <:AbstractQuantity, - <:AbstractQuantity, - <:AbstractQuantity, -}) = - Vinet( - uconvert(u"angstrom^3", eos.v0), - uconvert(u"eV/angstrom^3", eos.b0), - uconvert(NoUnits, eos.bp0), - uconvert(u"eV", eos.e0), - ) # =============================== Miscellaneous ============================== # end From 6a93cd8e2d0b0e241227f4dea4cd1b1f7c13ab7a Mon Sep 17 00:00:00 2001 From: Qi Zhang Date: Mon, 30 Sep 2019 03:08:58 -0400 Subject: [PATCH 22/61] Fix imports --- src/Collections.jl | 2 +- src/NonlinearFitting.jl | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/Collections.jl b/src/Collections.jl index 325c71f..77dc7ea 100644 --- a/src/Collections.jl +++ b/src/Collections.jl @@ -12,7 +12,7 @@ julia> module Collections using InteractiveUtils -using Unitful: AbstractQuantity, @u_str, uconvert, NoUnits, 𝐋, 𝐌, 𝐓, Dimension, Dimensions +using Unitful: AbstractQuantity, @u_str, Dimension, Dimensions import Unitful using UnitfulAstro diff --git a/src/NonlinearFitting.jl b/src/NonlinearFitting.jl index d54182c..cf2eabe 100644 --- a/src/NonlinearFitting.jl +++ b/src/NonlinearFitting.jl @@ -12,8 +12,7 @@ julia> module NonlinearFitting using LsqFit: curve_fit -using Unitful -import Unitful: AbstractQuantity, 𝐋, 𝐌, 𝐓 +using Unitful: AbstractQuantity, upreferred, ustrip, unit, uconvert import ..EquationForm using ..Collections From 27cc8925f80018bd62d7a546d5f65790886eb81f Mon Sep 17 00:00:00 2001 From: Qi Zhang Date: Mon, 30 Sep 2019 03:14:02 -0400 Subject: [PATCH 23/61] style: Restyle code --- src/Collections.jl | 17 +++++++++++++---- src/Find.jl | 6 +++++- src/NonlinearFitting.jl | 10 ++++++++-- 3 files changed, 26 insertions(+), 7 deletions(-) diff --git a/src/Collections.jl b/src/Collections.jl index 77dc7ea..a80acbb 100644 --- a/src/Collections.jl +++ b/src/Collections.jl @@ -319,7 +319,8 @@ function apply(::EnergyForm, eos::BirchMurnaghan4th, v) f = (cbrt(v0 / v)^2 - 1) / 2 h = b0 * bpp0 + bp0^2 - return e0 + 3 / 8 * v0 * b0 * f^2 * ((9h - 63bp0 + 143) * f^2 + 12 * (bp0 - 4) * f + 12) + return e0 + + 3 / 8 * v0 * b0 * f^2 * ((9h - 63bp0 + 143) * f^2 + 12 * (bp0 - 4) * f + 12) end """ apply(EnergyForm(), eos::PoirierTarantola2nd, v) @@ -664,9 +665,17 @@ fieldvalues(eos::EquationOfState) = [getfield(eos, i) for i in 1:nfields(eos)] Base.eltype(T::Type{<:EquationOfState}) = promote_type(T.types...) -Unitful.upreferred(::Dimensions{(Dimension{:Length}(2//1),Dimension{:Mass}(1//1),Dimension{:Time}(-2//1))}) = u"eV" -Unitful.upreferred(::Dimensions{(Dimension{:Length}(3//1),)}) = u"angstrom^3" -Unitful.upreferred(::Dimensions{(Dimension{:Length}(-1//1),Dimension{:Mass}(1//1),Dimension{:Time}(-2//1))}) = u"eV/angstrom^3" +Unitful.upreferred(::Dimensions{( + Dimension{:Length}(2 // 1), + Dimension{:Mass}(1 // 1), + Dimension{:Time}(-2 // 1), +)}) = u"eV" +Unitful.upreferred(::Dimensions{(Dimension{:Length}(3 // 1),)}) = u"angstrom^3" +Unitful.upreferred(::Dimensions{( + Dimension{:Length}(-1 // 1), + Dimension{:Mass}(1 // 1), + Dimension{:Time}(-2 // 1), +)}) = u"eV/angstrom^3" # =============================== Miscellaneous ============================== # end diff --git a/src/Find.jl b/src/Find.jl index dfd2317..2e6993f 100644 --- a/src/Find.jl +++ b/src/Find.jl @@ -46,7 +46,11 @@ function findvolume( eos::EquationOfState, y::Real, domain::Union{AbstractVector,Tuple}, - method::Union{AbstractNonBracketing,AbstractHalleyLikeMethod,AbstractNewtonLikeMethod}, + method::Union{ + AbstractNonBracketing, + AbstractHalleyLikeMethod, + AbstractNewtonLikeMethod, + }, ) f(v) = apply(form, eos, v) - y return find_zero(f, median(domain), method) diff --git a/src/NonlinearFitting.jl b/src/NonlinearFitting.jl index cf2eabe..fa4df01 100644 --- a/src/NonlinearFitting.jl +++ b/src/NonlinearFitting.jl @@ -61,8 +61,14 @@ function lsqfit( ) T = promote_type(eltype(eos), eltype(xdata), eltype(ydata), Float64) E = typeof(eos).name.wrapper - model(x, p) = map(apply(form, E(p...)), x) - fitted = curve_fit(model, T.(xdata), T.(ydata), T.(Collections.fieldvalues(eos)), kwargs...) + model = (x, p) -> map(apply(form, E(p...)), x) + fitted = curve_fit( + model, + T.(xdata), + T.(ydata), + T.(Collections.fieldvalues(eos)), + kwargs..., + ) return debug ? fitted : E(fitted.param...) end # function lsqfit function lsqfit( From 217a4d867be3cd62ebb6d78743625aa1f9cb0dd0 Mon Sep 17 00:00:00 2001 From: Qi Zhang Date: Mon, 30 Sep 2019 19:38:48 -0400 Subject: [PATCH 24/61] test: Relax a test tolerance or else it fails on CIs --- test/NonlinearFitting.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/NonlinearFitting.jl b/test/NonlinearFitting.jl index 1c6f01f..3d86801 100644 --- a/test/NonlinearFitting.jl +++ b/test/NonlinearFitting.jl @@ -612,7 +612,7 @@ end -5322.693307704408, -323.4177113158418, ) |> Collections.fieldvalues; - atol = 1e-3, + rtol = 1e-3, ) @test isapprox( lsqfit(EnergyForm(), Murnaghan(224, 0.006, 4, -323), volumes, energies) |> Collections.fieldvalues, From ddaf3275111b08ad9836e4d2774b71f9df1fb062 Mon Sep 17 00:00:00 2001 From: Qi Zhang Date: Tue, 1 Oct 2019 01:08:59 -0400 Subject: [PATCH 25/61] Simplify `findvolume` by `_adapt_domain` --- src/Find.jl | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/src/Find.jl b/src/Find.jl index 2e6993f..1b56870 100644 --- a/src/Find.jl +++ b/src/Find.jl @@ -31,29 +31,29 @@ using ..Collections: EquationOfState, apply export findvolume -function findvolume( - form::EquationForm, - eos::EquationOfState, - y::Real, +function _adapt_domain(domain::Union{AbstractVector,Tuple}, method::AbstractBracketing) + return minimum(domain), maximum(domain) +end # function _adapt_domain +function _adapt_domain( domain::Union{AbstractVector,Tuple}, - method::AbstractBracketing, + method::Union{ + AbstractNonBracketing, + AbstractHalleyLikeMethod, + AbstractNewtonLikeMethod, + }, ) - f(v) = apply(form, eos, v) - y - return find_zero(f, (minimum(domain), maximum(domain)), method) -end # function findvolume + return median(domain) +end # function _adapt_domain + function findvolume( form::EquationForm, eos::EquationOfState, y::Real, domain::Union{AbstractVector,Tuple}, - method::Union{ - AbstractNonBracketing, - AbstractHalleyLikeMethod, - AbstractNewtonLikeMethod, - }, + method, ) f(v) = apply(form, eos, v) - y - return find_zero(f, median(domain), method) + return find_zero(f, _adapt_domain(domain), method) end # function findvolume function findvolume( form::EquationForm, From 1b4f03c31352429cba8f8e5658cab5cb04a8dfee Mon Sep 17 00:00:00 2001 From: Qi Zhang Date: Tue, 1 Oct 2019 01:45:45 -0400 Subject: [PATCH 26/61] Implement `_whose_zero` for dealing with units in src/Find.jl --- src/Find.jl | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/src/Find.jl b/src/Find.jl index 1b56870..5f760ff 100644 --- a/src/Find.jl +++ b/src/Find.jl @@ -13,6 +13,7 @@ module Find using InteractiveUtils: subtypes using Statistics: median +using Unitful: AbstractQuantity, ustrip using Roots: find_zero, AbstractBracketing, @@ -31,6 +32,23 @@ using ..Collections: EquationOfState, apply export findvolume +function _whose_zero( + form::EquationForm, + eos::EquationOfState, + y::AbstractQuantity, +) + @assert(eltype(eos) <: AbstractQuantity, "The elements type mismatched!") + return v::AbstractQuantity -> ustrip(apply(form, eos, v) - y) +end # function _whose_zero +function _whose_zero( + form::EquationForm, + eos::EquationOfState, + y::Real, +) + @assert(eltype(eos) <: Real, "The elements type mismatched!") + return v::Real -> apply(form, eos, v) - y +end # function _whose_zero + function _adapt_domain(domain::Union{AbstractVector,Tuple}, method::AbstractBracketing) return minimum(domain), maximum(domain) end # function _adapt_domain @@ -48,17 +66,17 @@ end # function _adapt_domain function findvolume( form::EquationForm, eos::EquationOfState, - y::Real, + y, domain::Union{AbstractVector,Tuple}, method, ) - f(v) = apply(form, eos, v) - y + f = _whose_zero(form, eos, y) return find_zero(f, _adapt_domain(domain), method) end # function findvolume function findvolume( form::EquationForm, eos::EquationOfState, - y::Real, + y, domain::Union{AbstractVector,Tuple}, ) for T in [ From 25b39d7d5f84b62fc2e87cbeb4a98a6aca580d5f Mon Sep 17 00:00:00 2001 From: Qi Zhang Date: Tue, 1 Oct 2019 02:20:49 -0400 Subject: [PATCH 27/61] Implement `Statistics.middle` for `AbstractQuantity` to let `median` works --- src/Find.jl | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/Find.jl b/src/Find.jl index 5f760ff..9ac63c7 100644 --- a/src/Find.jl +++ b/src/Find.jl @@ -12,8 +12,8 @@ julia> module Find using InteractiveUtils: subtypes -using Statistics: median -using Unitful: AbstractQuantity, ustrip +import Statistics +using Unitful: AbstractQuantity, ustrip, upreferred using Roots: find_zero, AbstractBracketing, @@ -60,7 +60,7 @@ function _adapt_domain( AbstractNewtonLikeMethod, }, ) - return median(domain) + return Statistics.median(domain) end # function _adapt_domain function findvolume( @@ -97,4 +97,14 @@ function findvolume( end end # function findvolume +Statistics.middle(x::AbstractQuantity) = (x + zero(x)) / 1 +Statistics.middle(a::T, b::T) where {T<:AbstractQuantity} = middle(ustrip(a), ustrip(b)) * unit(a) +function Statistics.middle(a::AbstractQuantity, b::AbstractQuantity) + @assert(dimension(a) == dimension(b)) + a0, b0 = promote(map(ustrip, (a, b))...) + a, b = a0 * unit(a), b0 * unit(b) + a, b = map(upreferred, a, b) + return middle(a, b) +end # Statistics.middle + end From bc13999a95d49571d3ddb22ca01ce651f023fe9f Mon Sep 17 00:00:00 2001 From: Qi Zhang Date: Tue, 1 Oct 2019 04:50:09 -0400 Subject: [PATCH 28/61] Deprecate `_adapt_domain` & domain annotation Abstraction: let users choose --- src/Find.jl | 22 ++++------------------ 1 file changed, 4 insertions(+), 18 deletions(-) diff --git a/src/Find.jl b/src/Find.jl index 9ac63c7..e00b6e2 100644 --- a/src/Find.jl +++ b/src/Find.jl @@ -49,35 +49,21 @@ function _whose_zero( return v::Real -> apply(form, eos, v) - y end # function _whose_zero -function _adapt_domain(domain::Union{AbstractVector,Tuple}, method::AbstractBracketing) - return minimum(domain), maximum(domain) -end # function _adapt_domain -function _adapt_domain( - domain::Union{AbstractVector,Tuple}, - method::Union{ - AbstractNonBracketing, - AbstractHalleyLikeMethod, - AbstractNewtonLikeMethod, - }, -) - return Statistics.median(domain) -end # function _adapt_domain - function findvolume( form::EquationForm, eos::EquationOfState, y, - domain::Union{AbstractVector,Tuple}, + x0, method, ) f = _whose_zero(form, eos, y) - return find_zero(f, _adapt_domain(domain), method) + return find_zero(f, x0, method) end # function findvolume function findvolume( form::EquationForm, eos::EquationOfState, y, - domain::Union{AbstractVector,Tuple}, + x0, ) for T in [ subtypes(AbstractAlefeldPotraShi) @@ -89,7 +75,7 @@ function findvolume( ] @info("Using method \"$T\"...") try - return findvolume(form, eos, y, domain, T()) + return findvolume(form, eos, y, x0, T()) catch e @info("Method \"$T\" failed because of $e.") continue From 8635c0d436aa6018345b1178c6b18e9dc7d0c42d Mon Sep 17 00:00:00 2001 From: Qi Zhang Date: Tue, 1 Oct 2019 04:51:18 -0400 Subject: [PATCH 29/61] style: Reformat code --- src/Find.jl | 30 ++++++------------------------ 1 file changed, 6 insertions(+), 24 deletions(-) diff --git a/src/Find.jl b/src/Find.jl index e00b6e2..6064f26 100644 --- a/src/Find.jl +++ b/src/Find.jl @@ -32,39 +32,20 @@ using ..Collections: EquationOfState, apply export findvolume -function _whose_zero( - form::EquationForm, - eos::EquationOfState, - y::AbstractQuantity, -) +function _whose_zero(form::EquationForm, eos::EquationOfState, y::AbstractQuantity) @assert(eltype(eos) <: AbstractQuantity, "The elements type mismatched!") return v::AbstractQuantity -> ustrip(apply(form, eos, v) - y) end # function _whose_zero -function _whose_zero( - form::EquationForm, - eos::EquationOfState, - y::Real, -) +function _whose_zero(form::EquationForm, eos::EquationOfState, y::Real) @assert(eltype(eos) <: Real, "The elements type mismatched!") return v::Real -> apply(form, eos, v) - y end # function _whose_zero -function findvolume( - form::EquationForm, - eos::EquationOfState, - y, - x0, - method, -) +function findvolume(form::EquationForm, eos::EquationOfState, y, x0, method) f = _whose_zero(form, eos, y) return find_zero(f, x0, method) end # function findvolume -function findvolume( - form::EquationForm, - eos::EquationOfState, - y, - x0, -) +function findvolume(form::EquationForm, eos::EquationOfState, y, x0) for T in [ subtypes(AbstractAlefeldPotraShi) subtypes(AbstractBisection) @@ -84,7 +65,8 @@ function findvolume( end # function findvolume Statistics.middle(x::AbstractQuantity) = (x + zero(x)) / 1 -Statistics.middle(a::T, b::T) where {T<:AbstractQuantity} = middle(ustrip(a), ustrip(b)) * unit(a) +Statistics.middle(a::T, b::T) where {T<:AbstractQuantity} = + middle(ustrip(a), ustrip(b)) * unit(a) function Statistics.middle(a::AbstractQuantity, b::AbstractQuantity) @assert(dimension(a) == dimension(b)) a0, b0 = promote(map(ustrip, (a, b))...) From ef78245f2f54cc911d7a0bc596525d4d1209fb7e Mon Sep 17 00:00:00 2001 From: Qi Zhang Date: Tue, 1 Oct 2019 04:52:06 -0400 Subject: [PATCH 30/61] Deprecate the use of `Statistics` module --- src/Find.jl | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/src/Find.jl b/src/Find.jl index 6064f26..b8580f5 100644 --- a/src/Find.jl +++ b/src/Find.jl @@ -12,7 +12,6 @@ julia> module Find using InteractiveUtils: subtypes -import Statistics using Unitful: AbstractQuantity, ustrip, upreferred using Roots: find_zero, @@ -64,15 +63,4 @@ function findvolume(form::EquationForm, eos::EquationOfState, y, x0) end end # function findvolume -Statistics.middle(x::AbstractQuantity) = (x + zero(x)) / 1 -Statistics.middle(a::T, b::T) where {T<:AbstractQuantity} = - middle(ustrip(a), ustrip(b)) * unit(a) -function Statistics.middle(a::AbstractQuantity, b::AbstractQuantity) - @assert(dimension(a) == dimension(b)) - a0, b0 = promote(map(ustrip, (a, b))...) - a, b = a0 * unit(a), b0 * unit(b) - a, b = map(upreferred, a, b) - return middle(a, b) -end # Statistics.middle - end From 2ebb4797b4107ef3eb92e2577195684b2a7ea42d Mon Sep 17 00:00:00 2001 From: Qi Zhang Date: Tue, 1 Oct 2019 04:52:40 -0400 Subject: [PATCH 31/61] Remove `Statistics` module from deps --- Project.toml | 1 - 1 file changed, 1 deletion(-) diff --git a/Project.toml b/Project.toml index 2782390..712c4ec 100644 --- a/Project.toml +++ b/Project.toml @@ -10,7 +10,6 @@ LsqFit = "2fda8390-95c7-5789-9bda-21331edee243" MLStyle = "d8e11817-5142-5d16-987a-aa16d5891078" Polynomials = "f27b6e38-b328-58d1-80ce-0feddd5e7a45" Roots = "f2b01f46-fcfa-551c-844a-d8ac1e96c665" -Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2" Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d" UnitfulAstro = "6112ee07-acf9-5e0f-b108-d242c714bf9f" From 6cedfbca36d0563e7dfce64e4e70fee4c55d0352 Mon Sep 17 00:00:00 2001 From: Qi Zhang Date: Tue, 1 Oct 2019 04:53:56 -0400 Subject: [PATCH 32/61] Fix import --- src/Find.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Find.jl b/src/Find.jl index b8580f5..0758cc3 100644 --- a/src/Find.jl +++ b/src/Find.jl @@ -12,7 +12,7 @@ julia> module Find using InteractiveUtils: subtypes -using Unitful: AbstractQuantity, ustrip, upreferred +using Unitful: AbstractQuantity, ustrip using Roots: find_zero, AbstractBracketing, From b90cc58121226eb131cb1008ddc69c81af672539 Mon Sep 17 00:00:00 2001 From: Qi Zhang Date: Tue, 1 Oct 2019 16:55:56 -0400 Subject: [PATCH 33/61] Fix a possible bug in `findvolume` --- src/Find.jl | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/Find.jl b/src/Find.jl index 0758cc3..f67b57b 100644 --- a/src/Find.jl +++ b/src/Find.jl @@ -44,10 +44,17 @@ function findvolume(form::EquationForm, eos::EquationOfState, y, x0, method) f = _whose_zero(form, eos, y) return find_zero(f, x0, method) end # function findvolume -function findvolume(form::EquationForm, eos::EquationOfState, y, x0) +function findvolume(form::EquationForm, eos::EquationOfState, y, x0::Union{AbstractVector,Tuple}) + for T in [subtypes(AbstractBisection); subtypes(AbstractAlefeldPotraShi)] + @info("Using method \"$T\"...") + try + return findvolume(form, eos, y, (minimum(x0), maximum(x0)), T()) + catch e + @info("Method \"$T\" failed because of $e.") + continue + end + end for T in [ - subtypes(AbstractAlefeldPotraShi) - subtypes(AbstractBisection) Brent subtypes(AbstractHalleyLikeMethod) Newton @@ -55,7 +62,7 @@ function findvolume(form::EquationForm, eos::EquationOfState, y, x0) ] @info("Using method \"$T\"...") try - return findvolume(form, eos, y, x0, T()) + return findvolume(form, eos, y, (minimum(x0) + maximum(x0)) / 2, T()) catch e @info("Method \"$T\" failed because of $e.") continue From f4aac2a9edeb7843cc9298d44411cba401b03489 Mon Sep 17 00:00:00 2001 From: Qi Zhang Date: Tue, 1 Oct 2019 17:05:45 -0400 Subject: [PATCH 34/61] Add a comment in src/Find.jl --- src/Find.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Find.jl b/src/Find.jl index f67b57b..b23bbde 100644 --- a/src/Find.jl +++ b/src/Find.jl @@ -48,6 +48,7 @@ function findvolume(form::EquationForm, eos::EquationOfState, y, x0::Union{Abstr for T in [subtypes(AbstractBisection); subtypes(AbstractAlefeldPotraShi)] @info("Using method \"$T\"...") try + # `maximum` and `minimum` also works with `AbstractQuantity`s. return findvolume(form, eos, y, (minimum(x0), maximum(x0)), T()) catch e @info("Method \"$T\" failed because of $e.") From ee7cfb2f1dc1ad03db1a1cab3822164156703f8a Mon Sep 17 00:00:00 2001 From: Qi Zhang Date: Wed, 2 Oct 2019 04:52:46 -0400 Subject: [PATCH 35/61] Add back `T` in src/Collections.jl deprecate separate type parameters --- src/Collections.jl | 119 +++++++++++++++++++++++---------------------- 1 file changed, 62 insertions(+), 57 deletions(-) diff --git a/src/Collections.jl b/src/Collections.jl index a80acbb..aa49876 100644 --- a/src/Collections.jl +++ b/src/Collections.jl @@ -36,18 +36,18 @@ export apply, # Types # # ============================================================================ # """ - EquationOfState + EquationOfState{T} An abstraction of equations of state, where `T` specifies the elements' type. """ -abstract type EquationOfState end +abstract type EquationOfState{T} end """ - FiniteStrainEquationOfState <: EquationOfState + FiniteStrainEquationOfState{T} <: EquationOfState{T} An abstraction of finite strain equations of state. """ -abstract type FiniteStrainEquationOfState <: EquationOfState end +abstract type FiniteStrainEquationOfState{T} <: EquationOfState{T} end """ Murnaghan(v0, b0, bp0, e0=0) @@ -60,15 +60,20 @@ Create a Murnaghan equation of state. The elements' type will be handled automat - `bp0`: the first-order pressure-derivative bulk modulus of solid at zero pressure. - `e0=0`: the energy of solid at zero pressure. By default is `0`. """ -struct Murnaghan{A,B,C,D} <: EquationOfState - v0::A - b0::B - bp0::C - e0::D +struct Murnaghan{T} <: EquationOfState{T} + v0::T + b0::T + bp0::T + e0::T end -Murnaghan(v0::Real, b0::Real, bp0::Real) = Murnaghan(v0, b0, bp0, 0) -Murnaghan(v0::AbstractQuantity, b0::AbstractQuantity, bp0::AbstractQuantity) = - Murnaghan(v0, b0, bp0, 0 * u"eV") +function Murnaghan(v0, b0, bp0, e0) + T = Base.promote_typeof(v0, b0, bp0, e0) + return Murnaghan{T}(map(x -> convert(T, x), (v0, b0, bp0, e0))...) +end +Murnaghan(v0::Real, b0::Real, bp0::Real) = + Murnaghan(v0, b0, bp0, zero(Base.promote_typeof(v0, b0, bp0))) +Murnaghan(v0::AbstractQuantity{A}, b0::AbstractQuantity{B}, bp0::AbstractQuantity{C}) where {A,B,C} = + Murnaghan(v0, b0, bp0, zero(promote_type(A, B, C)) * u"eV") """ BirchMurnaghan2nd(v0, b0, e0=0) @@ -80,10 +85,10 @@ Create a Birch–Murnaghan 2nd order equation of state. The elements' type will - `b0`: the bulk modulus of solid at zero pressure. - `e0=0`: the energy of solid at zero pressure. By default is `0`. """ -struct BirchMurnaghan2nd{A,B,C} <: FiniteStrainEquationOfState - v0::A - b0::B - e0::C +struct BirchMurnaghan2nd{T} <: FiniteStrainEquationOfState{T} + v0::T + b0::T + e0::T end BirchMurnaghan2nd(v0::Real, b0::Real) = BirchMurnaghan2nd(v0, b0, 0) BirchMurnaghan2nd(v0::AbstractQuantity, b0::AbstractQuantity) = @@ -100,11 +105,11 @@ Create a Birch–Murnaghan 3rd order equation of state. The elements' type will - `bp0`: the first-order pressure-derivative bulk modulus of solid at zero pressure. - `e0=0`: the energy of solid at zero pressure. By default is `0`. """ -struct BirchMurnaghan3rd{A,B,C,D} <: FiniteStrainEquationOfState - v0::A - b0::B - bp0::C - e0::D +struct BirchMurnaghan3rd{T} <: FiniteStrainEquationOfState{T} + v0::T + b0::T + bp0::T + e0::T end BirchMurnaghan3rd(v0::Real, b0::Real, bp0::Real) = BirchMurnaghan3rd(v0, b0, bp0, 0) BirchMurnaghan3rd(v0::AbstractQuantity, b0::AbstractQuantity, bp0::AbstractQuantity) = @@ -122,12 +127,12 @@ Create a Birch–Murnaghan 4th order equation of state. The elements' type will - `bpp0`: the second-order pressure-derivative bulk modulus of solid at zero pressure. - `e0=0`: the energy of solid at zero pressure. By default is `0`. """ -struct BirchMurnaghan4th{A,B,C,D,E} <: FiniteStrainEquationOfState - v0::A - b0::B - bp0::C - bpp0::D - e0::E +struct BirchMurnaghan4th{T} <: FiniteStrainEquationOfState{T} + v0::T + b0::T + bp0::T + bpp0::T + e0::T end BirchMurnaghan4th(v0::Real, b0::Real, bp0::Real, bpp0::Real) = BirchMurnaghan4th(v0, b0, bp0, bpp0, 0) @@ -148,10 +153,10 @@ Create a Poirier–Tarantola order equation of state. The elements' type will be - `b0`: the bulk modulus of solid at zero pressure. - `e0=0`: the energy of solid at zero pressure. By default is `0`. """ -struct PoirierTarantola2nd{A,B,C} <: FiniteStrainEquationOfState - v0::A - b0::B - e0::C +struct PoirierTarantola2nd{T} <: FiniteStrainEquationOfState{T} + v0::T + b0::T + e0::T end PoirierTarantola2nd(v0::Real, b0::Real) = PoirierTarantola2nd(v0, b0, 0) PoirierTarantola2nd(v0::AbstractQuantity, b0::AbstractQuantity) = @@ -168,11 +173,11 @@ Create a Poirier–Tarantola 3rd order equation of state. The elements' type wil - `bp0`: the first-order pressure-derivative bulk modulus of solid at zero pressure. - `e0=0`: the energy of solid at zero pressure. By default is `0`. """ -struct PoirierTarantola3rd{A,B,C,D} <: FiniteStrainEquationOfState - v0::A - b0::B - bp0::C - e0::D +struct PoirierTarantola3rd{T} <: FiniteStrainEquationOfState{T} + v0::T + b0::T + bp0::T + e0::T end PoirierTarantola3rd(v0::Real, b0::Real, bp0::Real) = PoirierTarantola3rd(v0, b0, bp0, 0) PoirierTarantola3rd(v0::AbstractQuantity, b0::AbstractQuantity, bp0::AbstractQuantity) = @@ -190,12 +195,12 @@ Create a Birch–Murnaghan 4th order equation of state. The elements' type will - `bpp0`: the second-order pressure-derivative bulk modulus of solid at zero pressure. - `e0=0`: the energy of solid at zero pressure. By default is `0`. """ -struct PoirierTarantola4th{A,B,C,D,E} <: FiniteStrainEquationOfState - v0::A - b0::B - bp0::C - bpp0::D - e0::E +struct PoirierTarantola4th{T} <: FiniteStrainEquationOfState{T} + v0::T + b0::T + bp0::T + bpp0::T + e0::T end PoirierTarantola4th(v0::Real, b0::Real, bp0::Real, bpp0::Real) = PoirierTarantola4th(v0, b0, bp0, bpp0, 0) @@ -217,29 +222,29 @@ Create a Vinet equation of state. The elements' type will be handled automatical - `bp0`: the first-order pressure-derivative bulk modulus of solid at zero pressure. - `e0=0`: the energy of solid at zero pressure. By default is `0`. """ -struct Vinet{A,B,C,D} <: EquationOfState - v0::A - b0::B - bp0::C - e0::D +struct Vinet{T} <: EquationOfState{T} + v0::T + b0::T + bp0::T + e0::T end Vinet(v0::Real, b0::Real, bp0::Real) = Vinet(v0, b0, bp0, 0) Vinet(v0::AbstractQuantity, b0::AbstractQuantity, bp0::AbstractQuantity) = Vinet(v0, b0, bp0, 0 * u"eV") -struct AntonSchmidt{A,B,C,D} <: EquationOfState - v0::A - β::B - n::C - e∞::D +struct AntonSchmidt{T} <: EquationOfState{T} + v0::T + β::T + n::T + e∞::T end AntonSchmidt(v0::Real, β::Real, n::Real) = AntonSchmidt(v0, β, n, 0) -struct BreenanStacey{A,B,C,D} <: EquationOfState - v0::A - b0::B - γ0::C - e0::D +struct BreenanStacey{T} <: EquationOfState{T} + v0::T + b0::T + γ0::T + e0::T end BreenanStacey(v0::Real, b0::Real, γ0::Real) = BreenanStacey(v0, b0, γ0, 0) # =================================== Types ================================== # From 41e9aa5bb7db816b98d8d2c8eb60f4144db29bdd Mon Sep 17 00:00:00 2001 From: Qi Zhang Date: Thu, 3 Oct 2019 04:51:21 -0400 Subject: [PATCH 36/61] test: Add UnitfulAtomic.jl to test deps referenced from https://discourse.julialang.org/t/how-to-add-a-package-as-a-test-dependency-from-the-repl/14313/2 --- Project.toml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 712c4ec..e8365c5 100644 --- a/Project.toml +++ b/Project.toml @@ -18,6 +18,7 @@ julia = "1" [extras] Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" +UnitfulAtomic = "a7773ee8-282e-5fa2-be4e-bd808c38a91a" [targets] -test = ["Test"] +test = ["Test", "UnitfulAtomic"] From 1a9321426166180c365ab8ae1f44c51465b3e626 Mon Sep 17 00:00:00 2001 From: Qi Zhang Date: Thu, 3 Oct 2019 04:55:29 -0400 Subject: [PATCH 37/61] test: Add "Test `findvolume` with random unit" --- test/Find.jl | 182 +++++++++++++++++++++++++++++---------------------- 1 file changed, 102 insertions(+), 80 deletions(-) diff --git a/test/Find.jl b/test/Find.jl index 567109e..74faf78 100644 --- a/test/Find.jl +++ b/test/Find.jl @@ -1,90 +1,112 @@ using Test using Roots +using Unitful +using UnitfulAstro +using UnitfulAtomic using EquationsOfState using EquationsOfState.Collections using EquationsOfState.Find -# Data in the following tests are from -# https://github.com/materialsproject/pymatgen/blob/1f0957b8525ddc7d12ea348a19caecebe6c7ff34/pymatgen/analysis/tests/test_eos.py -@testset "Test data from Pymatgen" begin - results = [ - 25.987454833, - 26.9045702104, - 27.8430241908, - 28.8029649591, - 29.7848370694, - 30.7887887064, - 31.814968055, - 32.8638196693, - 33.9353435494, - 35.0299842495, - 36.1477417695, - 37.2892088485, - 38.4543854865, - 39.6437162376, - 40.857201102, - 42.095136449, - 43.3579668329, - 44.6456922537, - 45.9587572656, - 47.2973100535, - 48.6614988019, - 50.0517680652, - 51.4682660281, - 52.9112890601, - 54.3808371612, - 55.8775030703, - 57.4014349722, - 58.9526328669, - ] - energies = [ - -7.63622156576, - -8.16831294894, - -8.63871612686, - -9.05181213218, - -9.41170988374, - -9.72238224345, - -9.98744832526, - -10.210309552, - -10.3943401353, - -10.5427238068, - -10.6584266073, - -10.7442240979, - -10.8027285713, - -10.8363890521, - -10.8474912964, - -10.838157792, - -10.8103477586, - -10.7659387815, - -10.7066179666, - -10.6339907853, - -10.5495538639, - -10.4546677714, - -10.3506386542, - -10.2386366017, - -10.1197772808, - -9.99504030111, - -9.86535084973, - -9.73155247952, - ] - @test isapprox( - map( - e -> findvolume( - EnergyForm(), - BirchMurnaghan3rd( - 40.98926572528106, - 0.5369258245417454, - 4.178644235500821, - -10.842803908240892, - ), - e, - (25, 60), - Order16(), - ), - energies, +# # Data in the following tests are from +# # https://github.com/materialsproject/pymatgen/blob/1f0957b8525ddc7d12ea348a19caecebe6c7ff34/pymatgen/analysis/tests/test_eos.py +# @testset "Test data from Pymatgen" begin +# results = [ +# 25.987454833, +# 26.9045702104, +# 27.8430241908, +# 28.8029649591, +# 29.7848370694, +# 30.7887887064, +# 31.814968055, +# 32.8638196693, +# 33.9353435494, +# 35.0299842495, +# 36.1477417695, +# 37.2892088485, +# 38.4543854865, +# 39.6437162376, +# 40.857201102, +# 42.095136449, +# 43.3579668329, +# 44.6456922537, +# 45.9587572656, +# 47.2973100535, +# 48.6614988019, +# 50.0517680652, +# 51.4682660281, +# 52.9112890601, +# 54.3808371612, +# 55.8775030703, +# 57.4014349722, +# 58.9526328669, +# ] +# energies = [ +# -7.63622156576, +# -8.16831294894, +# -8.63871612686, +# -9.05181213218, +# -9.41170988374, +# -9.72238224345, +# -9.98744832526, +# -10.210309552, +# -10.3943401353, +# -10.5427238068, +# -10.6584266073, +# -10.7442240979, +# -10.8027285713, +# -10.8363890521, +# -10.8474912964, +# -10.838157792, +# -10.8103477586, +# -10.7659387815, +# -10.7066179666, +# -10.6339907853, +# -10.5495538639, +# -10.4546677714, +# -10.3506386542, +# -10.2386366017, +# -10.1197772808, +# -9.99504030111, +# -9.86535084973, +# -9.73155247952, +# ] +# @test isapprox( +# map( +# e -> findvolume( +# EnergyForm(), +# BirchMurnaghan3rd( +# 40.98926572528106, +# 0.5369258245417454, +# 4.178644235500821, +# -10.842803908240892, +# ), +# e, +# (25, 60), +# Order16(), +# ), +# energies, +# ), +# results, +# ) +# end + +@testset "Test `findvolume` with random unit" begin + pressures = collect(0:20:200) .* u"GPa" + eos = BirchMurnaghan3rd(167 * u"angstrom^3", 2600 * u"kbar", 4.0 * u"1000mm/m") + volumes = map( + p -> findvolume( + PressureForm(), + eos, + p, + (eps() * u"bohr^3", eos.v0 * 1.3), ), - results, + pressures, + ) + @test isapprox( + ustrip.(map(apply(PressureForm(), eos), volumes) - pressures), + zeros(11), + atol = 1e-5 ) -end +end # testset From 6ba985450e2bb6a280ba184d5204fba0d0f5330b Mon Sep 17 00:00:00 2001 From: Qi Zhang Date: Thu, 3 Oct 2019 04:56:39 -0400 Subject: [PATCH 38/61] Deprecate `_whose_zero` in src/Find.jl It is not necessary to distinguish `AbstractQuantity` or `Real` for `v`. `find_zero` is duck-typing. --- src/Find.jl | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/src/Find.jl b/src/Find.jl index b23bbde..b5f8579 100644 --- a/src/Find.jl +++ b/src/Find.jl @@ -31,17 +31,8 @@ using ..Collections: EquationOfState, apply export findvolume -function _whose_zero(form::EquationForm, eos::EquationOfState, y::AbstractQuantity) - @assert(eltype(eos) <: AbstractQuantity, "The elements type mismatched!") - return v::AbstractQuantity -> ustrip(apply(form, eos, v) - y) -end # function _whose_zero -function _whose_zero(form::EquationForm, eos::EquationOfState, y::Real) - @assert(eltype(eos) <: Real, "The elements type mismatched!") - return v::Real -> apply(form, eos, v) - y -end # function _whose_zero - function findvolume(form::EquationForm, eos::EquationOfState, y, x0, method) - f = _whose_zero(form, eos, y) + f = v -> apply(form, eos, v) - y return find_zero(f, x0, method) end # function findvolume function findvolume(form::EquationForm, eos::EquationOfState, y, x0::Union{AbstractVector,Tuple}) From 05423f45dac7aca0780ab7f7a07e39958e0ada00 Mon Sep 17 00:00:00 2001 From: Qi Zhang Date: Thu, 3 Oct 2019 04:58:44 -0400 Subject: [PATCH 39/61] test: Uncomment another test in test/Find.jl --- test/Find.jl | 164 +++++++++++++++++++++++++-------------------------- 1 file changed, 82 insertions(+), 82 deletions(-) diff --git a/test/Find.jl b/test/Find.jl index 74faf78..60dd486 100644 --- a/test/Find.jl +++ b/test/Find.jl @@ -9,88 +9,88 @@ using EquationsOfState using EquationsOfState.Collections using EquationsOfState.Find -# # Data in the following tests are from -# # https://github.com/materialsproject/pymatgen/blob/1f0957b8525ddc7d12ea348a19caecebe6c7ff34/pymatgen/analysis/tests/test_eos.py -# @testset "Test data from Pymatgen" begin -# results = [ -# 25.987454833, -# 26.9045702104, -# 27.8430241908, -# 28.8029649591, -# 29.7848370694, -# 30.7887887064, -# 31.814968055, -# 32.8638196693, -# 33.9353435494, -# 35.0299842495, -# 36.1477417695, -# 37.2892088485, -# 38.4543854865, -# 39.6437162376, -# 40.857201102, -# 42.095136449, -# 43.3579668329, -# 44.6456922537, -# 45.9587572656, -# 47.2973100535, -# 48.6614988019, -# 50.0517680652, -# 51.4682660281, -# 52.9112890601, -# 54.3808371612, -# 55.8775030703, -# 57.4014349722, -# 58.9526328669, -# ] -# energies = [ -# -7.63622156576, -# -8.16831294894, -# -8.63871612686, -# -9.05181213218, -# -9.41170988374, -# -9.72238224345, -# -9.98744832526, -# -10.210309552, -# -10.3943401353, -# -10.5427238068, -# -10.6584266073, -# -10.7442240979, -# -10.8027285713, -# -10.8363890521, -# -10.8474912964, -# -10.838157792, -# -10.8103477586, -# -10.7659387815, -# -10.7066179666, -# -10.6339907853, -# -10.5495538639, -# -10.4546677714, -# -10.3506386542, -# -10.2386366017, -# -10.1197772808, -# -9.99504030111, -# -9.86535084973, -# -9.73155247952, -# ] -# @test isapprox( -# map( -# e -> findvolume( -# EnergyForm(), -# BirchMurnaghan3rd( -# 40.98926572528106, -# 0.5369258245417454, -# 4.178644235500821, -# -10.842803908240892, -# ), -# e, -# (25, 60), -# Order16(), -# ), -# energies, -# ), -# results, -# ) -# end +# Data in the following tests are from +# https://github.com/materialsproject/pymatgen/blob/1f0957b8525ddc7d12ea348a19caecebe6c7ff34/pymatgen/analysis/tests/test_eos.py +@testset "Test data from Pymatgen" begin + results = [ + 25.987454833, + 26.9045702104, + 27.8430241908, + 28.8029649591, + 29.7848370694, + 30.7887887064, + 31.814968055, + 32.8638196693, + 33.9353435494, + 35.0299842495, + 36.1477417695, + 37.2892088485, + 38.4543854865, + 39.6437162376, + 40.857201102, + 42.095136449, + 43.3579668329, + 44.6456922537, + 45.9587572656, + 47.2973100535, + 48.6614988019, + 50.0517680652, + 51.4682660281, + 52.9112890601, + 54.3808371612, + 55.8775030703, + 57.4014349722, + 58.9526328669, + ] + energies = [ + -7.63622156576, + -8.16831294894, + -8.63871612686, + -9.05181213218, + -9.41170988374, + -9.72238224345, + -9.98744832526, + -10.210309552, + -10.3943401353, + -10.5427238068, + -10.6584266073, + -10.7442240979, + -10.8027285713, + -10.8363890521, + -10.8474912964, + -10.838157792, + -10.8103477586, + -10.7659387815, + -10.7066179666, + -10.6339907853, + -10.5495538639, + -10.4546677714, + -10.3506386542, + -10.2386366017, + -10.1197772808, + -9.99504030111, + -9.86535084973, + -9.73155247952, + ] + @test isapprox( + map( + e -> findvolume( + EnergyForm(), + BirchMurnaghan3rd( + 40.98926572528106, + 0.5369258245417454, + 4.178644235500821, + -10.842803908240892, + ), + e, + (25, 60), + Order16(), + ), + energies, + ), + results, + ) +end @testset "Test `findvolume` with random unit" begin pressures = collect(0:20:200) .* u"GPa" From 5915df8b9a694baa8b834a99d4245b5f987ab02b Mon Sep 17 00:00:00 2001 From: Qi Zhang Date: Thu, 3 Oct 2019 05:00:52 -0400 Subject: [PATCH 40/61] test: Make "Test data from Pymatgen" runnable --- test/Find.jl | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/test/Find.jl b/test/Find.jl index 60dd486..cca2614 100644 --- a/test/Find.jl +++ b/test/Find.jl @@ -41,7 +41,7 @@ using EquationsOfState.Find 55.8775030703, 57.4014349722, 58.9526328669, - ] + ] .* u"angstrom^3" energies = [ -7.63622156576, -8.16831294894, @@ -71,20 +71,19 @@ using EquationsOfState.Find -9.99504030111, -9.86535084973, -9.73155247952, - ] - @test isapprox( + ] .* u"eV" + isapprox( map( e -> findvolume( EnergyForm(), BirchMurnaghan3rd( - 40.98926572528106, - 0.5369258245417454, - 4.178644235500821, - -10.842803908240892, + 40.98926572528106u"angstrom^3", + 0.5369258245417454u"eV/angstrom^3", + 4.178644235500821u"1000mm/m", + -10.842803908240892u"eV", ), e, - (25, 60), - Order16(), + (eps(), 100) .* u"angstrom^3", ), energies, ), From 68270ff4bf761d488def5ba5f2d0a2a5f4ad01f3 Mon Sep 17 00:00:00 2001 From: Qi Zhang Date: Fri, 4 Oct 2019 23:10:16 -0400 Subject: [PATCH 41/61] style: Reformat test/Find.jl --- test/Find.jl | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/test/Find.jl b/test/Find.jl index cca2614..0831449 100644 --- a/test/Find.jl +++ b/test/Find.jl @@ -93,19 +93,14 @@ end @testset "Test `findvolume` with random unit" begin pressures = collect(0:20:200) .* u"GPa" - eos = BirchMurnaghan3rd(167 * u"angstrom^3", 2600 * u"kbar", 4.0 * u"1000mm/m") + eos = BirchMurnaghan3rd(167u"angstrom^3", 2600u"kbar", 4.0u"1000mm/m") volumes = map( - p -> findvolume( - PressureForm(), - eos, - p, - (eps() * u"bohr^3", eos.v0 * 1.3), - ), + p -> findvolume(PressureForm(), eos, p, (eps() * u"bohr^3", eos.v0 * 1.3)), pressures, ) @test isapprox( ustrip.(map(apply(PressureForm(), eos), volumes) - pressures), zeros(11), - atol = 1e-5 + atol = 1e-5, ) end # testset From 0b27e1432e05c61252903cc498afe461dbac8429 Mon Sep 17 00:00:00 2001 From: Qi Zhang Date: Sat, 5 Oct 2019 02:52:06 -0400 Subject: [PATCH 42/61] Remove `uconvert` in favor of function chaining operator `|>`. See https://painterqubits.github.io/Unitful.jl/latest/conversion/ --- src/NonlinearFitting.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/NonlinearFitting.jl b/src/NonlinearFitting.jl index fa4df01..d4a5e40 100644 --- a/src/NonlinearFitting.jl +++ b/src/NonlinearFitting.jl @@ -12,7 +12,7 @@ julia> module NonlinearFitting using LsqFit: curve_fit -using Unitful: AbstractQuantity, upreferred, ustrip, unit, uconvert +using Unitful: AbstractQuantity, upreferred, ustrip, unit import ..EquationForm using ..Collections @@ -87,7 +87,7 @@ function lsqfit( if result isa EquationOfState data = Collections.fieldvalues(result) return E( - [uconvert(u, data[i] * upreferred(u)) for (i, u) in enumerate(original_units)]... + [data[i] * upreferred(u) |> u for (i, u) in enumerate(original_units)]... ) end return result From 064d99390227dbe30970926e0fcbe645af329693 Mon Sep 17 00:00:00 2001 From: Qi Zhang Date: Thu, 10 Oct 2019 02:11:41 -0400 Subject: [PATCH 43/61] Remove UnitfulAstro.jl from deps Unitful.jl has merged my PR https://github.com/PainterQubits/Unitful.jl/pull/271 --- Project.toml | 1 - 1 file changed, 1 deletion(-) diff --git a/Project.toml b/Project.toml index e8365c5..7553376 100644 --- a/Project.toml +++ b/Project.toml @@ -11,7 +11,6 @@ MLStyle = "d8e11817-5142-5d16-987a-aa16d5891078" Polynomials = "f27b6e38-b328-58d1-80ce-0feddd5e7a45" Roots = "f2b01f46-fcfa-551c-844a-d8ac1e96c665" Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d" -UnitfulAstro = "6112ee07-acf9-5e0f-b108-d242c714bf9f" [compat] julia = "1" From c7a6bf446b99bc82737c38044193703532d6f162 Mon Sep 17 00:00:00 2001 From: Qi Zhang Date: Thu, 10 Oct 2019 02:12:51 -0400 Subject: [PATCH 44/61] Add ConstructionBase.jl to deps --- Project.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/Project.toml b/Project.toml index 7553376..6af4428 100644 --- a/Project.toml +++ b/Project.toml @@ -4,6 +4,7 @@ authors = ["Qi Zhang "] version = "2.0.0" [deps] +ConstructionBase = "187b0558-2788-49d3-abe0-74a17ed4e7c9" InteractiveUtils = "b77e0a4c-d291-57a0-90e8-8db25a27a240" LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" LsqFit = "2fda8390-95c7-5789-9bda-21331edee243" From 71e7e81280a762bb04705dcbdbd1363eff0c4be1 Mon Sep 17 00:00:00 2001 From: Qi Zhang Date: Thu, 10 Oct 2019 02:15:00 -0400 Subject: [PATCH 45/61] Replace `typeof(eos).name.wrapper` with `constructorof` --- src/NonlinearFitting.jl | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/NonlinearFitting.jl b/src/NonlinearFitting.jl index d4a5e40..86d55da 100644 --- a/src/NonlinearFitting.jl +++ b/src/NonlinearFitting.jl @@ -11,6 +11,7 @@ julia> """ module NonlinearFitting +using ConstructionBase: constructorof using LsqFit: curve_fit using Unitful: AbstractQuantity, upreferred, ustrip, unit @@ -60,7 +61,7 @@ function lsqfit( kwargs..., ) T = promote_type(eltype(eos), eltype(xdata), eltype(ydata), Float64) - E = typeof(eos).name.wrapper + E = constructorof(typeof(eos)) model = (x, p) -> map(apply(form, E(p...)), x) fitted = curve_fit( model, @@ -79,7 +80,7 @@ function lsqfit( ydata::AbstractVector; kwargs..., ) - E = typeof(eos).name.wrapper + E = constructorof(typeof(eos)) values = Collections.fieldvalues(eos) original_units = map(unit, values) trial_params, xdata, ydata = [map(ustrip ∘ upreferred, x) for x in (values, xdata, ydata)] From dc2d0aa78fcf054a1263a1288dafbf73a8bbf617 Mon Sep 17 00:00:00 2001 From: Qi Zhang Date: Thu, 10 Oct 2019 02:17:12 -0400 Subject: [PATCH 46/61] Fix imports of `UnitfulAstro` --- src/Collections.jl | 1 - test/Find.jl | 1 - 2 files changed, 2 deletions(-) diff --git a/src/Collections.jl b/src/Collections.jl index a80acbb..bab2f53 100644 --- a/src/Collections.jl +++ b/src/Collections.jl @@ -14,7 +14,6 @@ module Collections using InteractiveUtils using Unitful: AbstractQuantity, @u_str, Dimension, Dimensions import Unitful -using UnitfulAstro using EquationsOfState diff --git a/test/Find.jl b/test/Find.jl index 0831449..ac99aca 100644 --- a/test/Find.jl +++ b/test/Find.jl @@ -2,7 +2,6 @@ using Test using Roots using Unitful -using UnitfulAstro using UnitfulAtomic using EquationsOfState From 183e3934ba348611892eee6f0bc13b1226f6d804 Mon Sep 17 00:00:00 2001 From: Qi Zhang Date: Thu, 10 Oct 2019 02:17:31 -0400 Subject: [PATCH 47/61] Fix `promote_type` in src/NonlinearFitting.jl --- src/NonlinearFitting.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/NonlinearFitting.jl b/src/NonlinearFitting.jl index 86d55da..1a9a9a4 100644 --- a/src/NonlinearFitting.jl +++ b/src/NonlinearFitting.jl @@ -48,7 +48,7 @@ function lsqfit( ydata::AbstractVector; kwargs..., ) - T = eltype(eos) + T = promote_type(eltype(eos), eltype(xdata), eltype(ydata)) return lsqfit(_unit_trait(T), form, eos, xdata, ydata, kwargs...) end # function lsqfit function lsqfit( From 9e55afa11492b0ceeca01cecba38c2148c589d1f Mon Sep 17 00:00:00 2001 From: Qi Zhang Date: Thu, 10 Oct 2019 02:25:11 -0400 Subject: [PATCH 48/61] Track Manifest.toml to use Unitful.jl `master` branch --- Manifest.toml | 307 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 307 insertions(+) create mode 100644 Manifest.toml diff --git a/Manifest.toml b/Manifest.toml new file mode 100644 index 0000000..d4b4bec --- /dev/null +++ b/Manifest.toml @@ -0,0 +1,307 @@ +# This file is machine-generated - editing it directly is not advised + +[[Arpack]] +deps = ["BinaryProvider", "Libdl", "LinearAlgebra"] +git-tree-sha1 = "07a2c077bdd4b6d23a40342a8a108e2ee5e58ab6" +uuid = "7d9fca2a-8960-54d3-9f78-7d1dccf2cb97" +version = "0.3.1" + +[[ArrayInterface]] +deps = ["LinearAlgebra", "Requires", "SparseArrays"] +git-tree-sha1 = "981354dab938901c2b607a213e62d9defa50b698" +uuid = "4fba245c-0d91-5ea0-9b3e-6abc04ee57a9" +version = "1.2.1" + +[[Base64]] +uuid = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f" + +[[BinDeps]] +deps = ["Compat", "Libdl", "SHA", "URIParser"] +git-tree-sha1 = "12093ca6cdd0ee547c39b1870e0c9c3f154d9ca9" +uuid = "9e28174c-4ba2-5203-b857-d8d62c4213ee" +version = "0.8.10" + +[[BinaryProvider]] +deps = ["Libdl", "Logging", "SHA"] +git-tree-sha1 = "c7361ce8a2129f20b0e05a89f7070820cfed6648" +uuid = "b99e7846-7c00-51b0-8f62-c81ae34c0232" +version = "0.5.6" + +[[Calculus]] +deps = ["Compat"] +git-tree-sha1 = "bd8bbd105ba583a42385bd6dc4a20dad8ab3dc11" +uuid = "49dc2e85-a5d0-5ad3-a950-438e2897f1b9" +version = "0.5.0" + +[[CommonSubexpressions]] +deps = ["Test"] +git-tree-sha1 = "efdaf19ab11c7889334ca247ff4c9f7c322817b0" +uuid = "bbf7d656-a473-5ed7-a52c-81e309532950" +version = "0.2.0" + +[[Compat]] +deps = ["Base64", "Dates", "DelimitedFiles", "Distributed", "InteractiveUtils", "LibGit2", "Libdl", "LinearAlgebra", "Markdown", "Mmap", "Pkg", "Printf", "REPL", "Random", "Serialization", "SharedArrays", "Sockets", "SparseArrays", "Statistics", "Test", "UUIDs", "Unicode"] +git-tree-sha1 = "84aa74986c5b9b898b0d1acaf3258741ee64754f" +uuid = "34da2185-b29b-5c13-b0c7-acf172513d20" +version = "2.1.0" + +[[ConstructionBase]] +git-tree-sha1 = "e3efe0a0f49dcd294c8c73e897b4fdf891f0fbd3" +uuid = "187b0558-2788-49d3-abe0-74a17ed4e7c9" +version = "0.1.0" + +[[DataAPI]] +git-tree-sha1 = "674b67f344687a88310213ddfa8a2b3c76cc4252" +uuid = "9a962f9c-6df0-11e9-0e5d-c546b8b5ee8a" +version = "1.1.0" + +[[DataStructures]] +deps = ["InteractiveUtils", "OrderedCollections"] +git-tree-sha1 = "f94423c68f2e47db0d6f626a26d4872266e0ec3d" +uuid = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8" +version = "0.17.2" + +[[Dates]] +deps = ["Printf"] +uuid = "ade2ca70-3891-5945-98fb-dc099432e06a" + +[[DelimitedFiles]] +deps = ["Mmap"] +uuid = "8bb1440f-4735-579b-a4ab-409b98df4dab" + +[[DiffEqDiffTools]] +deps = ["ArrayInterface", "LinearAlgebra", "Requires", "SparseArrays", "StaticArrays"] +git-tree-sha1 = "21b855cb29ec4594f9651e0e9bdc0cdcfdcd52c1" +uuid = "01453d9d-ee7c-5054-8395-0335cb756afa" +version = "1.3.0" + +[[DiffResults]] +deps = ["Compat", "StaticArrays"] +git-tree-sha1 = "34a4a1e8be7bc99bc9c611b895b5baf37a80584c" +uuid = "163ba53b-c6d8-5494-b064-1a9d43ac40c5" +version = "0.0.4" + +[[DiffRules]] +deps = ["Random", "Test"] +git-tree-sha1 = "dc0869fb2f5b23466b32ea799bd82c76480167f7" +uuid = "b552c78f-8df3-52c6-915a-8e097449b14b" +version = "0.0.10" + +[[Distributed]] +deps = ["Random", "Serialization", "Sockets"] +uuid = "8ba89e20-285c-5b6f-9357-94700520ee1b" + +[[Distributions]] +deps = ["LinearAlgebra", "PDMats", "Printf", "QuadGK", "Random", "SpecialFunctions", "Statistics", "StatsBase", "StatsFuns"] +git-tree-sha1 = "b419fcf95ef9c8cf4d6610cd323890ad66d64240" +uuid = "31c24e10-a181-5473-b8eb-7969acd0382f" +version = "0.21.3" + +[[ForwardDiff]] +deps = ["CommonSubexpressions", "DiffResults", "DiffRules", "InteractiveUtils", "LinearAlgebra", "NaNMath", "Random", "SparseArrays", "SpecialFunctions", "StaticArrays", "Test"] +git-tree-sha1 = "4c4d727f1b7e0092134fabfab6396b8945c1ea5b" +uuid = "f6369f11-7733-5829-9624-2563aa707210" +version = "0.10.3" + +[[InteractiveUtils]] +deps = ["Markdown"] +uuid = "b77e0a4c-d291-57a0-90e8-8db25a27a240" + +[[LibGit2]] +uuid = "76f85450-5226-5b5a-8eaa-529ad045b433" + +[[Libdl]] +uuid = "8f399da3-3557-5675-b5ff-fb832c97cbdb" + +[[LinearAlgebra]] +deps = ["Libdl"] +uuid = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" + +[[Logging]] +uuid = "56ddb016-857b-54e1-b83d-db4d58db5568" + +[[LsqFit]] +deps = ["Distributions", "LinearAlgebra", "NLSolversBase", "OptimBase", "Random", "StatsBase", "Test"] +git-tree-sha1 = "186c2afbdb3cd52191078cfc6176f7084ed9dfb7" +uuid = "2fda8390-95c7-5789-9bda-21331edee243" +version = "0.8.1" + +[[MLStyle]] +git-tree-sha1 = "67f9a88611bc79f992aa705d9bbc833a2547dec7" +uuid = "d8e11817-5142-5d16-987a-aa16d5891078" +version = "0.3.1" + +[[Markdown]] +deps = ["Base64"] +uuid = "d6f4376e-aef5-505a-96c1-9c027394607a" + +[[Missings]] +deps = ["DataAPI"] +git-tree-sha1 = "de0a5ce9e5289f27df672ffabef4d1e5861247d5" +uuid = "e1d29d7a-bbdc-5cf2-9ac0-f12de2c33e28" +version = "0.4.3" + +[[Mmap]] +uuid = "a63ad114-7e13-5084-954f-fe012c677804" + +[[NLSolversBase]] +deps = ["Calculus", "DiffEqDiffTools", "DiffResults", "Distributed", "ForwardDiff"] +git-tree-sha1 = "f1b8ed89fa332f410cfc7c937682eb4d0b361521" +uuid = "d41bc354-129a-5804-8e4c-c37616107c6c" +version = "7.5.0" + +[[NaNMath]] +deps = ["Compat"] +git-tree-sha1 = "ce3b85e484a5d4c71dd5316215069311135fa9f2" +uuid = "77ba4419-2d1f-58cd-9bb1-8ffee604a2e3" +version = "0.3.2" + +[[OptimBase]] +deps = ["Compat", "NLSolversBase", "Printf", "Reexport", "Test"] +git-tree-sha1 = "92667ab46a66ad502ec3044f65c41ea68b2e0e9c" +uuid = "87e2bd06-a317-5318-96d9-3ecbac512eee" +version = "2.0.0" + +[[OrderedCollections]] +deps = ["Random", "Serialization", "Test"] +git-tree-sha1 = "c4c13474d23c60d20a67b217f1d7f22a40edf8f1" +uuid = "bac558e1-5e72-5ebc-8fee-abe8a469f55d" +version = "1.1.0" + +[[PDMats]] +deps = ["Arpack", "LinearAlgebra", "SparseArrays", "SuiteSparse", "Test"] +git-tree-sha1 = "035f8d60ba2a22cb1d2580b1e0e5ce0cb05e4563" +uuid = "90014a1f-27ba-587c-ab20-58faa44d9150" +version = "0.9.10" + +[[Pkg]] +deps = ["Dates", "LibGit2", "Markdown", "Printf", "REPL", "Random", "SHA", "UUIDs"] +uuid = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f" + +[[Polynomials]] +deps = ["LinearAlgebra", "SparseArrays", "Test"] +git-tree-sha1 = "62142bd65d3f8aeb2226ec64dd8493349147df94" +uuid = "f27b6e38-b328-58d1-80ce-0feddd5e7a45" +version = "0.5.2" + +[[Printf]] +deps = ["Unicode"] +uuid = "de0858da-6303-5e67-8744-51eddeeeb8d7" + +[[QuadGK]] +deps = ["DataStructures", "LinearAlgebra", "Test"] +git-tree-sha1 = "3ce467a8e76c6030d4c3786e7d3a73442017cdc0" +uuid = "1fd47b50-473d-5c70-9696-f719f8f3bcdc" +version = "2.0.3" + +[[REPL]] +deps = ["InteractiveUtils", "Markdown", "Sockets"] +uuid = "3fa0cd96-eef1-5676-8a61-b3b8758bbffb" + +[[Random]] +deps = ["Serialization"] +uuid = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c" + +[[Reexport]] +deps = ["Pkg"] +git-tree-sha1 = "7b1d07f411bc8ddb7977ec7f377b97b158514fe0" +uuid = "189a3867-3050-52da-a836-e630ba90ab69" +version = "0.2.0" + +[[Requires]] +deps = ["Test"] +git-tree-sha1 = "f6fbf4ba64d295e146e49e021207993b6b48c7d1" +uuid = "ae029012-a4dd-5104-9daa-d747884805df" +version = "0.5.2" + +[[Rmath]] +deps = ["BinaryProvider", "Libdl", "Random", "Statistics", "Test"] +git-tree-sha1 = "9a6c758cdf73036c3239b0afbea790def1dabff9" +uuid = "79098fc4-a85e-5d69-aa6a-4863f24498fa" +version = "0.5.0" + +[[Roots]] +deps = ["Printf"] +git-tree-sha1 = "9cc4b586c71f9aea25312b94be8c195f119b0ec3" +uuid = "f2b01f46-fcfa-551c-844a-d8ac1e96c665" +version = "0.8.3" + +[[SHA]] +uuid = "ea8e919c-243c-51af-8825-aaa63cd721ce" + +[[Serialization]] +uuid = "9e88b42a-f829-5b0c-bbe9-9e923198166b" + +[[SharedArrays]] +deps = ["Distributed", "Mmap", "Random", "Serialization"] +uuid = "1a1011a3-84de-559e-8e89-a11a2f7dc383" + +[[Sockets]] +uuid = "6462fe0b-24de-5631-8697-dd941f90decc" + +[[SortingAlgorithms]] +deps = ["DataStructures", "Random", "Test"] +git-tree-sha1 = "03f5898c9959f8115e30bc7226ada7d0df554ddd" +uuid = "a2af1166-a08f-5f64-846c-94a0d3cef48c" +version = "0.3.1" + +[[SparseArrays]] +deps = ["LinearAlgebra", "Random"] +uuid = "2f01184e-e22b-5df5-ae63-d93ebab69eaf" + +[[SpecialFunctions]] +deps = ["BinDeps", "BinaryProvider", "Libdl", "Test"] +git-tree-sha1 = "0b45dc2e45ed77f445617b99ff2adf0f5b0f23ea" +uuid = "276daf66-3868-5448-9aa4-cd146d93841b" +version = "0.7.2" + +[[StaticArrays]] +deps = ["LinearAlgebra", "Random", "Statistics"] +git-tree-sha1 = "db23bbf50064c582b6f2b9b043c8e7e98ea8c0c6" +uuid = "90137ffa-7385-5640-81b9-e52037218182" +version = "0.11.0" + +[[Statistics]] +deps = ["LinearAlgebra", "SparseArrays"] +uuid = "10745b16-79ce-11e8-11f9-7d13ad32a3b2" + +[[StatsBase]] +deps = ["DataAPI", "DataStructures", "LinearAlgebra", "Missings", "Printf", "Random", "SortingAlgorithms", "SparseArrays", "Statistics"] +git-tree-sha1 = "c53e809e63fe5cf5de13632090bc3520649c9950" +uuid = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91" +version = "0.32.0" + +[[StatsFuns]] +deps = ["Rmath", "SpecialFunctions", "Test"] +git-tree-sha1 = "b3a4e86aa13c732b8a8c0ba0c3d3264f55e6bb3e" +uuid = "4c63d2b9-4356-54db-8cca-17b64c39e42c" +version = "0.8.0" + +[[SuiteSparse]] +deps = ["Libdl", "LinearAlgebra", "Serialization", "SparseArrays"] +uuid = "4607b0f0-06f3-5cda-b6b1-a6196a1729e9" + +[[Test]] +deps = ["Distributed", "InteractiveUtils", "Logging", "Random"] +uuid = "8dfed614-e22c-5e08-85e1-65c5234f0b40" + +[[URIParser]] +deps = ["Test", "Unicode"] +git-tree-sha1 = "6ddf8244220dfda2f17539fa8c9de20d6c575b69" +uuid = "30578b45-9adc-5946-b283-645ec420af67" +version = "0.4.0" + +[[UUIDs]] +deps = ["Random", "SHA"] +uuid = "cf7118a7-6976-5b1a-9a39-7adc72f591a4" + +[[Unicode]] +uuid = "4ec0a83e-493e-50e2-b9ac-8f72acf5a8f5" + +[[Unitful]] +deps = ["LinearAlgebra", "Random"] +git-tree-sha1 = "0e9ad8a9621a9e1ec48f7cb48a5d6ded9e2c68f3" +repo-rev = "master" +repo-url = "https://github.com/PainterQubits/Unitful.jl.git" +uuid = "1986cc42-f94f-5a68-af5c-568840ba703d" +version = "0.17.0" From 7907fc55a882c16313e78b607aa2ab5077f5d611 Mon Sep 17 00:00:00 2001 From: Qi Zhang Date: Thu, 10 Oct 2019 02:36:11 -0400 Subject: [PATCH 49/61] Update README.md --- README.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/README.md b/README.md index ce7c6a1..c5a5581 100644 --- a/README.md +++ b/README.md @@ -38,7 +38,7 @@ This package also includes linear and nonlinear fitting methods, also referenced ## Compatibility - Julia version: `v1.0.0` and above -- Dependencies: see [`Project.toml`]([Project.toml](https://github.com/MineralsCloud/EquationsOfState.jl/blob/master/Project.toml)) +- Dependencies: see [`Project.toml`](https://github.com/MineralsCloud/EquationsOfState.jl/blob/master/Project.toml) - OS versions: `macOS`, `Linux`, and `Windows` ## Installation @@ -73,7 +73,6 @@ This package also includes linear and nonlinear fitting methods, also referenced ## Related packages 1. [CommandLineEquationsOfState.jl](https://github.com/MineralsCloud/CommandLineEquationsOfState.jl) -2. [ExtendedEquationsOfState.jl](https://github.com/MineralsCloud/ExtendedEquationsOfState.jl) ## References From 9ad9ef059eec81b6c6f0711a4bb5a35cb40eae88 Mon Sep 17 00:00:00 2001 From: "Restyled.io" Date: Thu, 10 Oct 2019 06:37:54 +0000 Subject: [PATCH 50/61] Restyled by prettier-markdown --- README.md | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index c5a5581..0109a99 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@
------- +--- # EquationsOfState.jl @@ -16,29 +16,32 @@ [![GitHub license](https://img.shields.io/github/license/MineralsCloud/EquationsOfState.jl)](https://github.com/MineralsCloud/EquationsOfState.jl/blob/master/LICENSE) ![GitHub release (latest by date including pre-releases)](https://img.shields.io/github/v/release/MineralsCloud/EquationsOfState.jl?include_prereleases) -This package implements some _equations of state_ (EOS) of solids which are useful in research. It currently includes: +This package implements some _equations of state_ (EOS) of solids which are +useful in research. It currently includes: 1. `Murnaghan` EOS 2. Birch–Murnaghan EOS family: - 1. `BirchMurnaghan2nd` - 2. `BirchMurnaghan3rd` - 3. `BirchMurnaghan4th` + 1. `BirchMurnaghan2nd` + 2. `BirchMurnaghan3rd` + 3. `BirchMurnaghan4th` 3. `Vinet` EOS 4. Poirier–Tarantola EOS family: - 1. `PoirierTarantola2nd` - 2. `PoirierTarantola3rd` - 3. `PoirierTarantola4th` + 1. `PoirierTarantola2nd` + 2. `PoirierTarantola3rd` + 3. `PoirierTarantola4th` 5. `AntonSchmidt` EOS (experimental) 6. `BreenanStacey` EOS (experimental) The formula are referenced from Ref. 1. -This package also includes linear and nonlinear fitting methods, also referenced from Ref. 1. +This package also includes linear and nonlinear fitting methods, also referenced +from Ref. 1. ## Compatibility - Julia version: `v1.0.0` and above -- Dependencies: see [`Project.toml`](https://github.com/MineralsCloud/EquationsOfState.jl/blob/master/Project.toml) +- Dependencies: see + [`Project.toml`](https://github.com/MineralsCloud/EquationsOfState.jl/blob/master/Project.toml) - OS versions: `macOS`, `Linux`, and `Windows` ## Installation @@ -51,7 +54,7 @@ This package also includes linear and nonlinear fitting methods, also referenced ```julia julia> using Pkg - + julia> Pkg.add("https://github.com/MineralsCloud/EquationsOfState.jl") ``` @@ -59,7 +62,7 @@ This package also includes linear and nonlinear fitting methods, also referenced ```julia julia> using Pkg - + julia> Pkg.add("EquationsOfState") ``` @@ -67,7 +70,8 @@ This package also includes linear and nonlinear fitting methods, also referenced ## TODOs -- [ ] Implement nonlinear fitting using [CMPFit.jl](https://github.com/gcalderone/CMPFit.jl). +- [ ] Implement nonlinear fitting using + [CMPFit.jl](https://github.com/gcalderone/CMPFit.jl). - [ ] Finish [docs](https://mineralscloud.github.io/EquationsOfState.jl/) ## Related packages @@ -76,4 +80,5 @@ This package also includes linear and nonlinear fitting methods, also referenced ## References -1. A. Otero-De-La-Roza, V. Luaña, *Comput. Phys. Commun.* **182**, 1708–1720 (2011). +1. A. Otero-De-La-Roza, V. Luaña, _Comput. Phys. Commun._ **182**, 1708–1720 + (2011). From a4bb485b9fbdb5100f8462bde96e9ee5855ae126 Mon Sep 17 00:00:00 2001 From: Qi Zhang Date: Thu, 10 Oct 2019 02:41:54 -0400 Subject: [PATCH 51/61] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 0109a99..b60b3d1 100644 --- a/README.md +++ b/README.md @@ -42,7 +42,7 @@ from Ref. 1. - Julia version: `v1.0.0` and above - Dependencies: see [`Project.toml`](https://github.com/MineralsCloud/EquationsOfState.jl/blob/master/Project.toml) -- OS versions: `macOS`, `Linux`, and `Windows` +- OS versions: macOS, Linux, and Windows ## Installation From add6dc6f5dacaa8d7a35cef55ea59bac30e4af51 Mon Sep 17 00:00:00 2001 From: Qi Zhang Date: Thu, 10 Oct 2019 02:46:37 -0400 Subject: [PATCH 52/61] Fix .github/PULL_REQUEST_TEMPLATE.md --- ...ersational.md => PULL_REQUEST_TEMPLATE.md} | 0 .github/PULL_REQUEST_TEMPLATE/bugs-only.md | 23 ------------------- 2 files changed, 23 deletions(-) rename .github/{PULL_REQUEST_TEMPLATE/conversational.md => PULL_REQUEST_TEMPLATE.md} (100%) mode change 100755 => 100644 delete mode 100755 .github/PULL_REQUEST_TEMPLATE/bugs-only.md diff --git a/.github/PULL_REQUEST_TEMPLATE/conversational.md b/.github/PULL_REQUEST_TEMPLATE.md old mode 100755 new mode 100644 similarity index 100% rename from .github/PULL_REQUEST_TEMPLATE/conversational.md rename to .github/PULL_REQUEST_TEMPLATE.md diff --git a/.github/PULL_REQUEST_TEMPLATE/bugs-only.md b/.github/PULL_REQUEST_TEMPLATE/bugs-only.md deleted file mode 100755 index 61262fa..0000000 --- a/.github/PULL_REQUEST_TEMPLATE/bugs-only.md +++ /dev/null @@ -1,23 +0,0 @@ -Bug Fixes **ONLY**. NO NEW FEATURE ACCEPTED! - - - -## Description - - -## Related Issue - - - - - -## Motivation and Context - - - -## How Has This Been Tested? - - - - -## Screenshots (if appropriate): From ae6d875912451378c925110b8e1230cc0a7382cb Mon Sep 17 00:00:00 2001 From: Qi Zhang Date: Thu, 10 Oct 2019 05:34:36 -0400 Subject: [PATCH 53/61] Deprecate `Base.eltype` of `EquationOfState`s --- src/Collections.jl | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/Collections.jl b/src/Collections.jl index b388450..5568833 100644 --- a/src/Collections.jl +++ b/src/Collections.jl @@ -67,7 +67,7 @@ struct Murnaghan{T} <: EquationOfState{T} end function Murnaghan(v0, b0, bp0, e0) T = Base.promote_typeof(v0, b0, bp0, e0) - return Murnaghan{T}(map(x -> convert(T, x), (v0, b0, bp0, e0))...) + return Murnaghan{T}(convert.(T, [v0, b0, bp0, e0])...) end Murnaghan(v0::Real, b0::Real, bp0::Real) = Murnaghan(v0, b0, bp0, zero(Base.promote_typeof(v0, b0, bp0))) @@ -667,8 +667,6 @@ end # This is a helper function and should not be exported. fieldvalues(eos::EquationOfState) = [getfield(eos, i) for i in 1:nfields(eos)] -Base.eltype(T::Type{<:EquationOfState}) = promote_type(T.types...) - Unitful.upreferred(::Dimensions{( Dimension{:Length}(2 // 1), Dimension{:Mass}(1 // 1), From 9ae4fd9a155bca3488e7bb407d19576689e0eacf Mon Sep 17 00:00:00 2001 From: Qi Zhang Date: Thu, 10 Oct 2019 06:06:53 -0400 Subject: [PATCH 54/61] Fix promotion problem --- src/Collections.jl | 61 +++++++++++++++++++++++++++++++++------------- 1 file changed, 44 insertions(+), 17 deletions(-) diff --git a/src/Collections.jl b/src/Collections.jl index 5568833..92fbc99 100644 --- a/src/Collections.jl +++ b/src/Collections.jl @@ -12,7 +12,7 @@ julia> module Collections using InteractiveUtils -using Unitful: AbstractQuantity, @u_str, Dimension, Dimensions +using Unitful: AbstractQuantity, @u_str, Dimension, Dimensions, upreferred import Unitful using EquationsOfState @@ -69,10 +69,9 @@ function Murnaghan(v0, b0, bp0, e0) T = Base.promote_typeof(v0, b0, bp0, e0) return Murnaghan{T}(convert.(T, [v0, b0, bp0, e0])...) end -Murnaghan(v0::Real, b0::Real, bp0::Real) = - Murnaghan(v0, b0, bp0, zero(Base.promote_typeof(v0, b0, bp0))) -Murnaghan(v0::AbstractQuantity{A}, b0::AbstractQuantity{B}, bp0::AbstractQuantity{C}) where {A,B,C} = - Murnaghan(v0, b0, bp0, zero(promote_type(A, B, C)) * u"eV") +Murnaghan(v0::Real, b0::Real, bp0::Real) = Murnaghan(v0, b0, bp0, 0) +Murnaghan(v0::AbstractQuantity, b0::AbstractQuantity, bp0) = + Murnaghan(v0, b0, bp0, 0 * upreferred(Unitful.J)) """ BirchMurnaghan2nd(v0, b0, e0=0) @@ -89,9 +88,13 @@ struct BirchMurnaghan2nd{T} <: FiniteStrainEquationOfState{T} b0::T e0::T end +function BirchMurnaghan2nd(v0, b0, e0) + T = Base.promote_typeof(v0, b0, e0) + return BirchMurnaghan2nd{T}(convert.(T, [v0, b0, e0])...) +end BirchMurnaghan2nd(v0::Real, b0::Real) = BirchMurnaghan2nd(v0, b0, 0) BirchMurnaghan2nd(v0::AbstractQuantity, b0::AbstractQuantity) = - BirchMurnaghan2nd(v0, b0, 0 * u"eV") + BirchMurnaghan2nd(v0, b0, 0 * upreferred(Unitful.J)) """ BirchMurnaghan3rd(v0, b0, bp0, e0=0) @@ -110,9 +113,13 @@ struct BirchMurnaghan3rd{T} <: FiniteStrainEquationOfState{T} bp0::T e0::T end +function BirchMurnaghan3rd(v0, b0, bp0, e0) + T = Base.promote_typeof(v0, b0, bp0, e0) + return BirchMurnaghan3rd{T}(convert.(T, [v0, b0, bp0, e0])...) +end BirchMurnaghan3rd(v0::Real, b0::Real, bp0::Real) = BirchMurnaghan3rd(v0, b0, bp0, 0) -BirchMurnaghan3rd(v0::AbstractQuantity, b0::AbstractQuantity, bp0::AbstractQuantity) = - BirchMurnaghan3rd(v0, b0, bp0, 0 * u"eV") +BirchMurnaghan3rd(v0::AbstractQuantity, b0::AbstractQuantity, bp0) = + BirchMurnaghan3rd(v0, b0, bp0, 0 * upreferred(Unitful.J)) """ BirchMurnaghan4th(v0, b0, bp0, bpp0, e0=0) @@ -133,14 +140,18 @@ struct BirchMurnaghan4th{T} <: FiniteStrainEquationOfState{T} bpp0::T e0::T end +function BirchMurnaghan4th(v0, b0, bp0, bpp0, e0) + T = Base.promote_typeof(v0, b0, bp0, bpp0, e0) + return BirchMurnaghan4th{T}(convert.(T, [v0, b0, bp0, bpp0, e0])...) +end BirchMurnaghan4th(v0::Real, b0::Real, bp0::Real, bpp0::Real) = BirchMurnaghan4th(v0, b0, bp0, bpp0, 0) BirchMurnaghan4th( v0::AbstractQuantity, b0::AbstractQuantity, - bp0::AbstractQuantity, + bp0, bpp0::AbstractQuantity, -) = BirchMurnaghan4th(v0, b0, bp0, bpp0, 0 * u"eV") +) = BirchMurnaghan4th(v0, b0, bp0, bpp0, 0 * upreferred(Unitful.J)) """ PoirierTarantola2nd(v0, b0, e0=0) @@ -157,9 +168,13 @@ struct PoirierTarantola2nd{T} <: FiniteStrainEquationOfState{T} b0::T e0::T end +function PoirierTarantola2nd(v0, b0, e0) + T = Base.promote_typeof(v0, b0, e0) + return PoirierTarantola2nd{T}(convert.(T, [v0, b0, e0])...) +end PoirierTarantola2nd(v0::Real, b0::Real) = PoirierTarantola2nd(v0, b0, 0) PoirierTarantola2nd(v0::AbstractQuantity, b0::AbstractQuantity) = - PoirierTarantola2nd(v0, b0, 0 * u"eV") + PoirierTarantola2nd(v0, b0, 0 * upreferred(Unitful.J)) """ PoirierTarantola3rd(v0, b0, bp0, e0=0) @@ -178,9 +193,13 @@ struct PoirierTarantola3rd{T} <: FiniteStrainEquationOfState{T} bp0::T e0::T end +function PoirierTarantola3rd(v0, b0, bp0, e0) + T = Base.promote_typeof(v0, b0, bp0, e0) + return PoirierTarantola3rd{T}(convert.(T, [v0, b0, bp0, e0])...) +end PoirierTarantola3rd(v0::Real, b0::Real, bp0::Real) = PoirierTarantola3rd(v0, b0, bp0, 0) -PoirierTarantola3rd(v0::AbstractQuantity, b0::AbstractQuantity, bp0::AbstractQuantity) = - PoirierTarantola3rd(v0, b0, bp0, 0 * u"eV") +PoirierTarantola3rd(v0::AbstractQuantity, b0::AbstractQuantity, bp0) = + PoirierTarantola3rd(v0, b0, bp0, 0 * upreferred(Unitful.J)) """ PoirierTarantola4th(v0, b0, bp0, bpp0, e0=0) @@ -201,14 +220,18 @@ struct PoirierTarantola4th{T} <: FiniteStrainEquationOfState{T} bpp0::T e0::T end +function PoirierTarantola4th(v0, b0, bp0, bpp0, e0) + T = Base.promote_typeof(v0, b0, bp0, bpp0, e0) + return PoirierTarantola4th{T}(convert.(T, [v0, b0, bp0, bpp0, e0])...) +end PoirierTarantola4th(v0::Real, b0::Real, bp0::Real, bpp0::Real) = PoirierTarantola4th(v0, b0, bp0, bpp0, 0) PoirierTarantola4th( v0::AbstractQuantity, b0::AbstractQuantity, - bp0::AbstractQuantity, + bp0, bpp0::AbstractQuantity, -) = PoirierTarantola4th(v0, b0, bp0, bpp0, 0 * u"eV") +) = PoirierTarantola4th(v0, b0, bp0, bpp0, 0 * upreferred(Unitful.J)) """ Vinet(v0, b0, bp0, e0=0) @@ -227,9 +250,13 @@ struct Vinet{T} <: EquationOfState{T} bp0::T e0::T end +function Vinet(v0, b0, bp0, e0) + T = Base.promote_typeof(v0, b0, bp0, e0) + return Vinet{T}(convert.(T, [v0, b0, bp0, e0])...) +end Vinet(v0::Real, b0::Real, bp0::Real) = Vinet(v0, b0, bp0, 0) -Vinet(v0::AbstractQuantity, b0::AbstractQuantity, bp0::AbstractQuantity) = - Vinet(v0, b0, bp0, 0 * u"eV") +Vinet(v0::AbstractQuantity, b0::AbstractQuantity, bp0) = + Vinet(v0, b0, bp0, 0 * upreferred(Unitful.J)) struct AntonSchmidt{T} <: EquationOfState{T} v0::T From 84e704a523b0b95a939ebaa21e5cbd9d9efa7378 Mon Sep 17 00:00:00 2001 From: Qi Zhang Date: Thu, 10 Oct 2019 06:08:37 -0400 Subject: [PATCH 55/61] Specify imports explicitly --- src/Collections.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Collections.jl b/src/Collections.jl index 92fbc99..e1146ed 100644 --- a/src/Collections.jl +++ b/src/Collections.jl @@ -15,7 +15,7 @@ using InteractiveUtils using Unitful: AbstractQuantity, @u_str, Dimension, Dimensions, upreferred import Unitful -using EquationsOfState +using EquationsOfState: EnergyForm, PressureForm, BulkModulusForm export apply, EquationOfState, From 97de36f5fcf8a95c3babaf4bc13e29d61cbcfd38 Mon Sep 17 00:00:00 2001 From: Qi Zhang Date: Thu, 10 Oct 2019 06:14:06 -0400 Subject: [PATCH 56/61] Deprecate `HasUnit` trait --- src/NonlinearFitting.jl | 32 ++++++-------------------------- 1 file changed, 6 insertions(+), 26 deletions(-) diff --git a/src/NonlinearFitting.jl b/src/NonlinearFitting.jl index 1a9a9a4..bbba5ab 100644 --- a/src/NonlinearFitting.jl +++ b/src/NonlinearFitting.jl @@ -20,14 +20,6 @@ using ..Collections export lsqfit -# This idea is borrowed from [SimpleTraits.jl](https://github.com/mauro3/SimpleTraits.jl/blob/master/src/SimpleTraits.jl). -abstract type Trait end -abstract type Not{T<:Trait} <: Trait end -struct HasUnit <: Trait end - -_unit_trait(T::Type{<:Real}) = Not{HasUnit} -_unit_trait(T::Type{<:AbstractQuantity}) = HasUnit - """ lsqfit(form, eos, xdata, ydata; debug = false, kwargs...) @@ -43,20 +35,9 @@ Fit an equation of state using least-squares fitting method (with the Levenberg- """ function lsqfit( form::EquationForm, - eos::EquationOfState, - xdata::AbstractVector, - ydata::AbstractVector; - kwargs..., -) - T = promote_type(eltype(eos), eltype(xdata), eltype(ydata)) - return lsqfit(_unit_trait(T), form, eos, xdata, ydata, kwargs...) -end # function lsqfit -function lsqfit( - ::Type{Not{HasUnit}}, - form::EquationForm, - eos::EquationOfState, - xdata::AbstractVector, - ydata::AbstractVector; + eos::EquationOfState{<:Real}, + xdata::AbstractVector{<:Real}, + ydata::AbstractVector{<:Real}; debug = false, kwargs..., ) @@ -73,11 +54,10 @@ function lsqfit( return debug ? fitted : E(fitted.param...) end # function lsqfit function lsqfit( - ::Type{HasUnit}, form::EquationForm, - eos::EquationOfState, - xdata::AbstractVector, - ydata::AbstractVector; + eos::EquationOfState{<:AbstractQuantity}, + xdata::AbstractVector{<:AbstractQuantity}, + ydata::AbstractVector{<:AbstractQuantity}; kwargs..., ) E = constructorof(typeof(eos)) From 14c7d86095319494fb28d50b202083adbed7e0f3 Mon Sep 17 00:00:00 2001 From: Qi Zhang Date: Thu, 10 Oct 2019 06:16:31 -0400 Subject: [PATCH 57/61] Fix `MethodError: no constructors have been defined for Any` `eltype(eos)` is `Any`, which results in the `promote_type` returns an `Any`. --- src/NonlinearFitting.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/NonlinearFitting.jl b/src/NonlinearFitting.jl index bbba5ab..3e7131b 100644 --- a/src/NonlinearFitting.jl +++ b/src/NonlinearFitting.jl @@ -41,7 +41,7 @@ function lsqfit( debug = false, kwargs..., ) - T = promote_type(eltype(eos), eltype(xdata), eltype(ydata), Float64) + T = promote_type(eltype(xdata), eltype(ydata), Float64) E = constructorof(typeof(eos)) model = (x, p) -> map(apply(form, E(p...)), x) fitted = curve_fit( From 4b2ec524140409e623bb68840ddf4dfa4ba61d13 Mon Sep 17 00:00:00 2001 From: Qi Zhang Date: Thu, 10 Oct 2019 06:17:53 -0400 Subject: [PATCH 58/61] style: Use `;` to separate `kwargs` --- src/NonlinearFitting.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/NonlinearFitting.jl b/src/NonlinearFitting.jl index 3e7131b..2acbb24 100644 --- a/src/NonlinearFitting.jl +++ b/src/NonlinearFitting.jl @@ -64,7 +64,7 @@ function lsqfit( values = Collections.fieldvalues(eos) original_units = map(unit, values) trial_params, xdata, ydata = [map(ustrip ∘ upreferred, x) for x in (values, xdata, ydata)] - result = lsqfit(form, E(trial_params...), xdata, ydata, kwargs...) + result = lsqfit(form, E(trial_params...), xdata, ydata; kwargs...) if result isa EquationOfState data = Collections.fieldvalues(result) return E( From 6e5f28732ddf5a4c086f87ac7a0bed6da16edfb5 Mon Sep 17 00:00:00 2001 From: Qi Zhang Date: Thu, 10 Oct 2019 06:23:42 -0400 Subject: [PATCH 59/61] Add promotion for `AntonSchmidt` & `BreenanStacey` --- src/Collections.jl | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/Collections.jl b/src/Collections.jl index e1146ed..6af09b2 100644 --- a/src/Collections.jl +++ b/src/Collections.jl @@ -264,6 +264,10 @@ struct AntonSchmidt{T} <: EquationOfState{T} n::T e∞::T end +function AntonSchmidt(v0, β, n, e∞) + T = Base.promote_typeof(v0, β, n, e∞) + return AntonSchmidt{T}(convert.(T, [v0, β, n, e∞])...) +end AntonSchmidt(v0::Real, β::Real, n::Real) = AntonSchmidt(v0, β, n, 0) struct BreenanStacey{T} <: EquationOfState{T} @@ -272,6 +276,10 @@ struct BreenanStacey{T} <: EquationOfState{T} γ0::T e0::T end +function BreenanStacey(v0, b0, γ0, e0) + T = Base.promote_typeof(v0, b0, γ0, e0) + return BreenanStacey{T}(convert.(T, [v0, b0, γ0, e0])...) +end BreenanStacey(v0::Real, b0::Real, γ0::Real) = BreenanStacey(v0, b0, γ0, 0) # =================================== Types ================================== # From 8f3ca0e7939e37df87e3deda97d6f2920084de8e Mon Sep 17 00:00:00 2001 From: Qi Zhang Date: Thu, 10 Oct 2019 06:24:04 -0400 Subject: [PATCH 60/61] test: Add back Collections.jl which was deprecated before. --- test/Collections.jl | 29 +++++++++++++++++++++++++++++ test/runtests.jl | 1 + 2 files changed, 30 insertions(+) create mode 100644 test/Collections.jl diff --git a/test/Collections.jl b/test/Collections.jl new file mode 100644 index 0000000..51eabb9 --- /dev/null +++ b/test/Collections.jl @@ -0,0 +1,29 @@ +using Test + +using EquationsOfState.Collections + +@testset "Test EOS promotion" begin + @test typeof(Murnaghan(1, 2, 3.0, 0)) == Murnaghan{Float64} + @test typeof(BirchMurnaghan2nd(1, 2.0, 0)) == BirchMurnaghan2nd{Float64} + @test typeof(BirchMurnaghan3rd(1, 2, 3.0, 0)) == BirchMurnaghan3rd{Float64} + @test typeof(BirchMurnaghan4th(1, 2.0, 3, 4, 0)) == BirchMurnaghan4th{Float64} + @test typeof(Vinet(1, 2, 3.0, 0)) == Vinet{Float64} + @test typeof(PoirierTarantola2nd(1, 2.0, 0)) == PoirierTarantola2nd{Float64} + @test typeof(PoirierTarantola3rd(1, 2, 3.0, 0)) == PoirierTarantola3rd{Float64} + @test typeof(PoirierTarantola4th(1, 2, 3, 4, 0)) == PoirierTarantola4th{Int} + @test typeof(AntonSchmidt(1, 2, 3.0, 0)) == AntonSchmidt{Float64} + @test typeof(BreenanStacey(1, 2, 3.0, 0)) == BreenanStacey{Float64} +end + +@testset "Test default EOS parameter `e0` and promotion" begin + @test Murnaghan(1, 2, 3.0) == Murnaghan(1.0, 2.0, 3.0, 0.0) + @test BirchMurnaghan2nd(1, 2.0) == BirchMurnaghan2nd(1.0, 2.0, 0.0) + @test BirchMurnaghan3rd(1, 2, 3.0) == BirchMurnaghan3rd(1.0, 2.0, 3.0, 0.0) + @test BirchMurnaghan4th(1, 2.0, 3, 4) == BirchMurnaghan4th(1.0, 2.0, 3.0, 4.0, 0.0) + @test Vinet(1, 2, 3.0) == Vinet(1.0, 2.0, 3.0, 0.0) + @test PoirierTarantola2nd(1, 2.0) == PoirierTarantola2nd(1.0, 2.0, 0.0) + @test PoirierTarantola3rd(1, 2, 3.0) == PoirierTarantola3rd(1.0, 2.0, 3.0, 0.0) + @test PoirierTarantola4th(1, 2, 3, 4) == PoirierTarantola4th(1, 2, 3, 4, 0) + @test AntonSchmidt(1, 2, 3.0) == AntonSchmidt(1.0, 2.0, 3.0, 0.0) + @test BreenanStacey(1, 2, 3.0) == BreenanStacey(1.0, 2.0, 3.0, 0.0) +end diff --git a/test/runtests.jl b/test/runtests.jl index e64bb2c..e082a3e 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -2,6 +2,7 @@ using EquationsOfState using Test @testset "EquationsOfState.jl" begin + include("Collections.jl") include("FiniteStrains.jl") include("NonlinearFitting.jl") include("LinearFitting.jl") From 6b8c53bd8167d13cdfc81ecbc669017052e2ce3b Mon Sep 17 00:00:00 2001 From: Qi Zhang Date: Thu, 10 Oct 2019 06:35:01 -0400 Subject: [PATCH 61/61] test: Add more tests to `Collections` module with `Unitful` --- test/Collections.jl | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/test/Collections.jl b/test/Collections.jl index 51eabb9..9d09396 100644 --- a/test/Collections.jl +++ b/test/Collections.jl @@ -1,5 +1,7 @@ using Test +using Unitful + using EquationsOfState.Collections @testset "Test EOS promotion" begin @@ -13,6 +15,10 @@ using EquationsOfState.Collections @test typeof(PoirierTarantola4th(1, 2, 3, 4, 0)) == PoirierTarantola4th{Int} @test typeof(AntonSchmidt(1, 2, 3.0, 0)) == AntonSchmidt{Float64} @test typeof(BreenanStacey(1, 2, 3.0, 0)) == BreenanStacey{Float64} + @test Murnaghan(1, Int32(2), Int8(3), 0) == Murnaghan{Int64}(1, 2, 3, 0) + @test Murnaghan(1, 2//1, Int8(3), 0) == Murnaghan{Rational{Int64}}(1//1, 2//1, 3//1, 0//1) + @test typeof(Murnaghan(1u"angstrom^3", 2u"eV/angstrom^3", 3.0, 4u"eV")) == Murnaghan{Quantity{Float64}} + @test typeof(Murnaghan(1u"angstrom^3", 2u"eV/angstrom^3", 3//2, 4u"eV")) == Murnaghan{Quantity{Rational{Int64}}} end @testset "Test default EOS parameter `e0` and promotion" begin @@ -26,4 +32,7 @@ end @test PoirierTarantola4th(1, 2, 3, 4) == PoirierTarantola4th(1, 2, 3, 4, 0) @test AntonSchmidt(1, 2, 3.0) == AntonSchmidt(1.0, 2.0, 3.0, 0.0) @test BreenanStacey(1, 2, 3.0) == BreenanStacey(1.0, 2.0, 3.0, 0.0) + @test typeof(Murnaghan(1u"angstrom^3", 2u"eV/angstrom^3", 3)) == Murnaghan{Quantity{Int64}} + @test typeof(Murnaghan(1u"angstrom^3", 2u"eV/angstrom^3", 3.0)) == Murnaghan{Quantity{Float64}} + @test typeof(Murnaghan(1.0u"angstrom^3", 2u"eV/angstrom^3", 3.0)) == Murnaghan{Quantity{Float64}} end