Skip to content

Commit

Permalink
Merge pull request #976 from JonWel/patch-1
Browse files Browse the repository at this point in the history
Add high-level functions to Julia wrapper
  • Loading branch information
ibell committed Feb 18, 2016
2 parents 789f67f + 6570efd commit 092e93b
Showing 1 changed file with 66 additions and 24 deletions.
90 changes: 66 additions & 24 deletions wrappers/Julia/CoolProp.jl
@@ -1,12 +1,11 @@
module CoolProp

export F2K, K2F, HAPropsSI, PropsSI, PhaseSI, get_global_param_string, get_param_index, get_input_pair_index, AbstractState_factory, AbstractState_free, AbstractState_set_fractions, AbstractState_update, AbstractState_keyed_output
export PropsSI, PhaseSI, get_global_param_string, get_parameter_information_string,get_fluid_param_string,set_reference_stateS, get_param_index, get_input_pair_index, F2K, K2F, HAPropsSI, AbstractState_factory, AbstractState_free, AbstractState_set_fractions, AbstractState_update, AbstractState_keyed_output

# Check the current Julia version to make this Julia 0.4 code compatible with older version
if VERSION <= VersionNumber(0,4)
typealias UInt8 Uint8
typealias Ref Ptr
typealias RefValue Array
errcode = Array(Clong, 1)
typealias AbstractString String
else
Expand All @@ -16,22 +15,20 @@ end
const buffer_length = 255
message_buffer = Array(UInt8, buffer_length)

function F2K(TF::Number)
return ccall( (:F2K, "CoolProp"), Cdouble, (Cdouble,), TF)
end

function K2F(TK::Number)
return ccall( (:K2F, "CoolProp"), Cdouble, (Cdouble,), TK)
end
# ---------------------------------
# High-level functions
# ---------------------------------

function HAPropsSI(Output::AbstractString, Name1::AbstractString, Value1::Number, Name2::AbstractString, Value2::Number, Name3::AbstractString, Value3::Number)
val = ccall( (:HAPropsSI, "CoolProp"), Cdouble, (Ptr{UInt8},Ptr{UInt8},Float64,Ptr{UInt8},Float64,Ptr{UInt8},Float64), Output,Name1,Value1,Name2,Value2,Name3,Value3)
# CoolProp::Props1SI(std::string, std::string)
function PropsSI(FluidName::AbstractString, Output::AbstractString)
val = ccall( (:Props1SI, "CoolProp"), Cdouble, (Ptr{UInt8},Ptr{UInt8}), FluidName,Output)
if val == Inf
error("CoolProp: ", get_global_param_string("errstring"))
end
return val
end

# CoolProp::PropsSI(const std::string &, const std::string &, double, const std::string &, double, const std::string&)
function PropsSI(Output::AbstractString, Name1::AbstractString, Value1::Number, Name2::AbstractString, Value2::Number, Fluid::AbstractString)
val = ccall( (:PropsSI, "CoolProp"), Cdouble, (Ptr{UInt8},Ptr{UInt8},Float64,Ptr{UInt8},Float64,Ptr{UInt8}), Output,Name1,Value1,Name2,Value2,Fluid)
if val == Inf
Expand All @@ -40,25 +37,50 @@ function PropsSI(Output::AbstractString, Name1::AbstractString, Value1::Number,
return val
end

function PropsSI(FluidName::AbstractString, Output::AbstractString)
val = ccall( (:Props1SI, "CoolProp"), Cdouble, (Ptr{UInt8},Ptr{UInt8}), FluidName,Output)
if val == Inf
# CoolProp::PhaseSI(const std::string &, double, const std::string &, double, const std::string&)
function PhaseSI(Name1::AbstractString, Value1::Number, Name2::AbstractString, Value2::Number, Fluid::AbstractString)
val = ccall( (:PhaseSI, "CoolProp"), Int32, (Ptr{UInt8},Float64,Ptr{UInt8},Float64,Ptr{UInt8}, Ptr{UInt8}, Int), Name1,Value1,Name2,Value2,Fluid,message_buffer::Array{UInt8,1},buffer_length)
val = bytestring(convert(Ptr{UInt8}, pointer(message_buffer::Array{UInt8,1})))
if val == ""
error("CoolProp: ", get_global_param_string("errstring"))
end
return val
end

function PhaseSI(Name1::AbstractString, Value1::Number, Name2::AbstractString, Value2::Number, Fluid::AbstractString)
val = ccall( (:PhaseSI, "CoolProp"), Int32, (Ptr{UInt8},Float64,Ptr{UInt8},Float64,Ptr{UInt8}, Ptr{UInt8}, Int), Name1,Value1,Name2,Value2,Fluid,message_buffer::Array{UInt8,1},buffer_length)
# CoolProp::get_global_param_string
function get_global_param_string(Key::AbstractString)
val = ccall( (:get_global_param_string, "CoolProp"), Clong, (Ptr{UInt8},Ptr{UInt8},Int), Key,message_buffer::Array{UInt8,1},buffer_length)
return bytestring(convert(Ptr{UInt8}, pointer(message_buffer::Array{UInt8,1})))
end

# This function returns the output string in pre-allocated char buffer. If buffer is not large enough, no copy is made
function get_global_param_string(Key::AbstractString)
val = ccall( (:get_global_param_string, "CoolProp"), Clong, (Ptr{UInt8},Ptr{UInt8},Int), Key,message_buffer::Array{UInt8,1},buffer_length)
# CoolProp::get_parameter_information_string
function get_parameter_information_string(Key::AbstractString)
val = ccall( (:get_parameter_information_string, "CoolProp"), Clong, (Ptr{UInt8},Ptr{UInt8},Int), Key,message_buffer::Array{UInt8,1},buffer_length)
return bytestring(convert(Ptr{UInt8}, pointer(message_buffer::Array{UInt8,1})))
end

# CoolProp::get_mixture_binary_pair_data
# Not valid yet
#function get_mixture_binary_pair_data(CAS1::AbstractString,CAS2::AbstractString,Key::AbstractString)
# val = ccall( (:get_mixture_binary_pair_data, "CoolProp"), Clong, (Ptr{UInt8},Ptr{UInt8},Ptr{UInt8}), CAS1,CAS2,Key)
# return val
#end

# CoolProp::get_fluid_param_string
function get_fluid_param_string(fluid::AbstractString,param::AbstractString)
val = ccall( (:get_fluid_param_string, "CoolProp"), Clong, (Ptr{UInt8},Ptr{UInt8},Ptr{UInt8},Int), fluid,param,message_buffer::Array{UInt8,1},buffer_length)
return bytestring(convert(Ptr{UInt8}, pointer(message_buffer::Array{UInt8,1})))
end

# CoolProp::set_reference_stateS
function set_reference_stateS(Ref::AbstractString, reference_state::AbstractString)
val = ccall( (:set_reference_stateS, "CoolProp"), Cint, (Ptr{UInt8},Ptr{UInt8}), Ref,reference_state)
if val == 0
error("CoolProp: ", get_global_param_string("errstring"))
end
return val
end

# Get the index for a parameter "T", "P", etc.
# returns the index as a long.
function get_param_index(Param::AbstractString)
Expand Down Expand Up @@ -91,6 +113,26 @@ function set_debug_level(level::Int)
ccall( (:set_debug_level, "CoolProp"), Void, (Cint,), level)
end

function F2K(TF::Number)
return ccall( (:F2K, "CoolProp"), Cdouble, (Cdouble,), TF)
end

function K2F(TK::Number)
return ccall( (:K2F, "CoolProp"), Cdouble, (Cdouble,), TK)
end

# ---------------------------------
# Humid Air Properties
# ---------------------------------

function HAPropsSI(Output::AbstractString, Name1::AbstractString, Value1::Number, Name2::AbstractString, Value2::Number, Name3::AbstractString, Value3::Number)
val = ccall( (:HAPropsSI, "CoolProp"), Cdouble, (Ptr{UInt8},Ptr{UInt8},Float64,Ptr{UInt8},Float64,Ptr{UInt8},Float64), Output,Name1,Value1,Name2,Value2,Name3,Value3)
if val == Inf
error("CoolProp: ", get_global_param_string("errstring"))
end
return val
end

# ---------------------------------
# Low-level access
# ---------------------------------
Expand All @@ -100,7 +142,7 @@ end
# param fluids '&' delimited list of fluids
# return A handle to the state class generated
function AbstractState_factory(backend::AbstractString, fluids::AbstractString)
AbstractState = ccall( (:AbstractState_factory, "CoolProp"), Clong, (Ptr{UInt8},Ptr{UInt8},Ref{Clong},Ptr{UInt8},Clong), backend,fluids,errcode::RefValue{Clong,1},message_buffer::Array{UInt8,1},buffer_length)
AbstractState = ccall( (:AbstractState_factory, "CoolProp"), Clong, (Ptr{UInt8},Ptr{UInt8},Ref{Clong},Ptr{UInt8},Clong), backend,fluids,errcode,message_buffer::Array{UInt8,1},buffer_length)
if errcode[] != 0
if errcode[] == 1
error("CoolProp: ", bytestring(convert(Ptr{UInt8}, pointer(message_buffer))))
Expand All @@ -116,7 +158,7 @@ end
# Release a state class generated by the low-level interface wrapper
# param handle The integer handle for the state class stored in memory
function AbstractState_free(handle::Clong)
ccall( (:AbstractState_free, "CoolProp"), Void, (Clong,Ref{Clong},Ptr{UInt8},Clong), handle,errcode::RefValue{Clong,1},message_buffer::Array{UInt8,1},buffer_length)
ccall( (:AbstractState_free, "CoolProp"), Void, (Clong,Ref{Clong},Ptr{UInt8},Clong), handle,errcode,message_buffer::Array{UInt8,1},buffer_length)
if errcode[] != 0
if errcode[] == 1
error("CoolProp: ", bytestring(convert(Ptr{UInt8}, pointer(message_buffer))))
Expand All @@ -133,7 +175,7 @@ end
# param handle The integer handle for the state class stored in memory
# param fractions The array of fractions
function AbstractState_set_fractions(handle::Clong,fractions::Array)
ccall( (:AbstractState_set_fractions, "CoolProp"), Void, (Clong,Ptr{Cdouble},Clong,Ref{Clong},Ptr{UInt8},Clong), handle,fractions,length(fractions),errcode::RefValue{Clong,1},message_buffer::Array{UInt8,1},buffer_length)
ccall( (:AbstractState_set_fractions, "CoolProp"), Void, (Clong,Ptr{Cdouble},Clong,Ref{Clong},Ptr{UInt8},Clong), handle,fractions,length(fractions),errcode,message_buffer::Array{UInt8,1},buffer_length)
if errcode[] != 0
if errcode[] == 1
error("CoolProp: ", bytestring(convert(Ptr{UInt8}, pointer(message_buffer))))
Expand All @@ -152,7 +194,7 @@ end
# param value1 The first input value
# param value2 The second input value
function AbstractState_update(handle::Clong,input_pair::Clong,value1::Number,value2::Number)
ccall( (:AbstractState_update, "CoolProp"), Void, (Clong,Clong,Cdouble,Cdouble,Ref{Clong},Ptr{UInt8},Clong), handle,input_pair,value1,value2,errcode::RefValue{Clong,1},message_buffer::Array{UInt8,1},buffer_length)
ccall( (:AbstractState_update, "CoolProp"), Void, (Clong,Clong,Cdouble,Cdouble,Ref{Clong},Ptr{UInt8},Clong), handle,input_pair,value1,value2,errcode,message_buffer::Array{UInt8,1},buffer_length)
if errcode[] != 0
if errcode[] == 1
error("CoolProp: ", bytestring(convert(Ptr{UInt8}, pointer(message_buffer))))
Expand All @@ -169,7 +211,7 @@ end
# param handle The integer handle for the state class stored in memory
# param param The integer value for the parameter you want
function AbstractState_keyed_output(handle::Clong, param::Clong)
output = ccall( (:AbstractState_keyed_output, "CoolProp"), Cdouble, (Clong,Clong,Ref{Clong},Ptr{UInt8},Clong), handle,param,errcode::RefValue{Clong,1},message_buffer::Array{UInt8,1},buffer_length)
output = ccall( (:AbstractState_keyed_output, "CoolProp"), Cdouble, (Clong,Clong,Ref{Clong},Ptr{UInt8},Clong), handle,param,errcode,message_buffer::Array{UInt8,1},buffer_length)
if errcode[] != 0
if errcode[] == 1
error("CoolProp: ", bytestring(convert(Ptr{UInt8}, pointer(message_buffer))))
Expand Down

0 comments on commit 092e93b

Please sign in to comment.