Skip to content

Commit

Permalink
Merge pull request #6 from stillyslalom/overhaul
Browse files Browse the repository at this point in the history
Switch from PyCall/Conda to PythonCall/CondaPkg
  • Loading branch information
stillyslalom authored Sep 3, 2022
2 parents 4f5e47b + 11a0b65 commit 470fced
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 40 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
/docs/build/
/docs/Manifest.toml
/deps/build.log
.CondaPkg/
4 changes: 4 additions & 0 deletions CondaPkg.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

[deps.thermo]
channel = "conda-forge"
version = "0.1.40"
6 changes: 2 additions & 4 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@ authors = ["Alex Ames <[email protected]> and contributors"]
version = "0.2.0"

[deps]
Conda = "8f4d0f93-b110-5947-807f-2305c1781a2d"
CondaPkg = "992eb4ea-22a4-4c89-a5bb-47a3300528ab"
Markdown = "d6f4376e-aef5-505a-96c1-9c027394607a"
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"
PyCall = "438e738f-606a-5dbb-bf0a-cddfbfd45ab0"
PythonCall = "6099a3de-0909-46bc-b1f4-468b9a2dfc0d"
Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d"

[compat]
Conda = "1.4"
PyCall = "1.92"
Unitful = "1.6"
julia = "1.6"

Expand Down
4 changes: 0 additions & 4 deletions deps/build.jl

This file was deleted.

46 changes: 23 additions & 23 deletions src/PyThermo.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module PyThermo

using PyCall
using PythonCall
using Printf
using Unitful

Expand Down Expand Up @@ -50,7 +50,7 @@ P : pressure of the chemical (default 101325 Pa)
Examples
--------
```jldoctest
```jldoctest pythermo; setup = :(using PyThermo)
julia> He = Species("He")
Species(He, 298.1 K, 1.013e+05 Pa)
Expand All @@ -63,7 +63,7 @@ julia> He.T = 30u"K"
30 K
julia> density(He)
1.6235030074934973 kg m^-3
1.623503007493497 kg m^-3
```
A wide variety of unexported properties can be accessed from the underlying Python object:
Expand All @@ -89,7 +89,7 @@ Carcinogen_sources S_int_l_Tm_to_Tb __setattr__
```
"""
struct Species <: Chemical
o::PyObject
o::Py
end
Species(chemname::String; kwargs...) = Species(PY_CHEM.Chemical(chemname; _SI_TP(kwargs)...))

Expand Down Expand Up @@ -117,16 +117,16 @@ The composition can also be specified by providing a vector of `"ID" => molefrac
Examples
--------
```jldoctest
```jldoctest; setup = :(using PyThermo)
julia> air = Mixture(["N2" => 0.78, "O2" => 0.21, "Ar" => 0.01])
Mixture({N2: 0.78, O2: 0.21, Ar: 0.01}, 298.1 K, 1.013e+05 Pa)
Mixture(78% nitrogen, 21% oxygen, 1% argon, 298.1 K, 1.013e+05 Pa)
julia> soundspeed(air)
346.1466532754559 m s^-1
346.14659461295173 m s^-1
```
"""
struct Mixture <: Chemical
o::PyObject
o::Py
end
Mixture(chemnames::Vector{String}; kwargs...) = Mixture(PY_CHEM.Mixture(chemnames; _SI_TP(kwargs)...))

Expand Down Expand Up @@ -157,24 +157,24 @@ composition_string(s::Species) = s.name

Base.show(io::IO, mix::Mixture) = @printf(io, "Mixture(%s, %0.1f K, %0.3e Pa)", composition_string(mix), mix.T, mix.P)

PyObject(c::Chemical) = getfield(c, :o)
convert(::Type{T}, o::PyCall.PyObject) where {T <: Chemical} = T(o)
==(c1::Chemical, c2::Chemical) = PyObject(c1) == PyObject(c2)
hash(c::Chemical) = hash(PyObject(c))
Base.copy(c::T) where {T <: Chemical} = T(PY_COPY.copy(PyObject(c)))
Py(c::Chemical) = getfield(c, :o)
convert(::Type{T}, o::PythonCall.Py) where {T <: Chemical} = T(o)
==(c1::Chemical, c2::Chemical) = Py(c1) == Py(c2)
hash(c::Chemical) = hash(Py(c))
Base.copy(c::T) where {T <: Chemical} = T(PY_COPY.copy(Py(c)))

Base.Docs.doc(c::Chemical) = Base.Docs.doc(PyObject(c))
Base.Docs.Binding(c::Chemical, s::Symbol) = Base.Docs.Binding(PyObject(c), s)
Base.Docs.doc(c::Chemical) = Base.Docs.doc(Py(c))
Base.Docs.Binding(c::Chemical, s::Symbol) = Base.Docs.Binding(Py(c), s)

Base.getproperty(c::Chemical, s::Symbol) = getproperty(PyObject(c), s)
Base.setproperty!(c::Chemical, s::Symbol, x) = setproperty!(PyObject(c), s, x)
Base.hasproperty(c::Chemical, s::Symbol) = hasproperty(PyObject(c), s)
Base.propertynames(c::Chemical) = propertynames(PyObject(c))
haskey(c::Chemical, x) = haskey(PyObject(c), x)
Base.getproperty(c::Chemical, s::Symbol) = pyconvert(Any, getproperty(Py(c), s))
Base.setproperty!(c::Chemical, s::Symbol, x) = setproperty!(Py(c), s, x)
Base.hasproperty(c::Chemical, s::Symbol) = hasproperty(Py(c), s)
Base.propertynames(c::Chemical) = propertynames(Py(c))
haskey(c::Chemical, x) = haskey(Py(c), x)

# Strip units for unitful `setproperty`
Base.setproperty!(c::Chemical, s::Symbol, T::Unitful.Temperature) = setproperty!(PyObject(c), s, _unit(T, u"K"))
Base.setproperty!(c::Chemical, s::Symbol, T::Unitful.Pressure) = setproperty!(PyObject(c), s, _unit(T, u"Pa"))
Base.setproperty!(c::Chemical, s::Symbol, T::Unitful.Temperature) = setproperty!(Py(c), s, _unit(T, u"K"))
Base.setproperty!(c::Chemical, s::Symbol, T::Unitful.Pressure) = setproperty!(Py(c), s, _unit(T, u"Pa"))


# Thermodynamic property accessors
Expand All @@ -190,7 +190,7 @@ include("ShockTube.jl")

if ccall(:jl_generating_output, Cint, ()) == 1
__init__()
ShockTube.shockcalc(Species("N2"), Mixture(["N2" => 0.78, "O2" => 0.21, "Ar" => 0.01]), 1.8)
# ShockTube.shockcalc(Species("N2"), Mixture(["N2" => 0.78, "O2" => 0.21, "Ar" => 0.01]), 1.8)
end

end # module
12 changes: 6 additions & 6 deletions src/init.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
# initialize them here (rather than via "global foo = ..." in __init__)
# so that their type is known at compile-time.

const PY_THERMO = PyNULL()
const PY_CHEM = PyNULL()
const PY_COPY = PyNULL()
const PY_THERMO = PythonCall.pynew()
const PY_CHEM = PythonCall.pynew()
const PY_COPY = PythonCall.pynew()

function __init__()
copy!(PY_THERMO, pyimport_conda("thermo", "thermo", "conda-forge"))
copy!(PY_CHEM, pyimport("thermo.chemical"))
copy!(PY_COPY, pyimport("copy"))
PythonCall.pycopy!(PY_THERMO, pyimport("thermo"))
PythonCall.pycopy!(PY_CHEM, PY_THERMO.chemical)
PythonCall.pycopy!(PY_COPY, pyimport("copy"))
end
4 changes: 1 addition & 3 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ using PyThermo
using Test
using PyThermo.ShockTube: shockcalc
using Unitful
using Conda

@testset "PyThermo.jl" begin
@test Conda.version("thermo") <= v"0.1.40"

@testset "Species" begin
SF6 = Species("SF6")
Expand All @@ -17,7 +15,7 @@ using Conda
HeAce = Mixture(["Helium" => 0.95, "Acetone" => 0.05])
ρ_HeAce = HeAce.rho
@test !isnothing(ρ_HeAce)
@test isapprox(ρ_HeAce, 0.2747138795604815, rtol=2e-3)
@test isapprox(ρ_HeAce, 0.2747138795604815, rtol=3e-3)
end
end

Expand Down

2 comments on commit 470fced

@stillyslalom
Copy link
Owner Author

Choose a reason for hiding this comment

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

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

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

Registration pull request created: JuliaRegistries/General/67657

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.2.0 -m "<description of version>" 470fceda511ee6b44db9d88913b8c4e312a5eeb3
git push origin v0.2.0

Please sign in to comment.