Skip to content

Commit

Permalink
file for low level api
Browse files Browse the repository at this point in the history
  • Loading branch information
DANA-Laboratory committed Dec 29, 2016
1 parent 36fb1ab commit 0f428d2
Show file tree
Hide file tree
Showing 5 changed files with 203 additions and 129 deletions.
136 changes: 8 additions & 128 deletions test/CoolProp.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module CoolProp
using Compat
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, set_config, F2K, K2F, HAPropsSI, AbstractState_factory, AbstractState_free, AbstractState_set_fractions, AbstractState_update, AbstractState_specify_phase, AbstractState_unspecify_phase, AbstractState_keyed_output, AbstractState_output, AbstractState_update_and_common_out, AbstractState_update_and_1_out, AbstractState_update_and_5_out, AbstractState_set_binary_interaction_double, AbstractState_set_cubic_alpha_C, AbstractState_set_fluid_parameter_double
export propssi, phasesi, k2f, f2k, hapropssi, cair_sat
export propssi, phasesi, k2f, f2k, hapropssi, cair_sat, abstractstate_factory, abstractstate_free, abstractstate_set_fractions, abstractstate_update, abstractstate_keyed_output
errcode = Ref{Clong}(0)

const buffer_length = 20000
Expand Down Expand Up @@ -54,106 +54,7 @@ end
###

include("CoolPropHA.jl");

# ---------------------------------
# Low-level access
# ---------------------------------

"""
AbstractState_factory(backend::AbstractString, fluids::AbstractString)
Generate an AbstractState instance, return an integer handle to the state class generated to be used in the other low-level accessor functions.
param backend The backend you will use, "HEOS", "REFPROP", etc.
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,message_buffer::Array{UInt8,1},buffer_length)
if errcode[] != 0
if errcode[] == 1
error("CoolProp: ", unsafe_string(convert(Ptr{UInt8}, pointer(message_buffer))))
elseif errcode[] == 2
error("CoolProp: message buffer too small")
else # == 3
error("CoolProp: unknown error")
end
end
return AbstractState
end

"""
AbstractState_free(handle::Clong)
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,message_buffer::Array{UInt8,1},buffer_length)
if errcode[] != 0
if errcode[] == 1
error("CoolProp: ", unsafe_string(convert(Ptr{UInt8}, pointer(message_buffer))))
elseif errcode[] == 2
error("CoolProp: message buffer too small")
else # == 3
error("CoolProp: unknown error")
end
end
return nothing
end

"""
AbstractState_set_fractions(handle::Clong,fractions::Array)
Set the fractions (mole, mass, volume) for the AbstractState.
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,message_buffer::Array{UInt8,1},buffer_length)
if errcode[] != 0
if errcode[] == 1
error("CoolProp: ", unsafe_string(convert(Ptr{UInt8}, pointer(message_buffer))))
elseif errcode[] == 2
error("CoolProp: message buffer too small")
else # == 3
error("CoolProp: unknown error")
end
end
return nothing
end

"""
AbstractState_update(handle::Clong,input_pair::Clong,value1::Real,value2::Real)
AbstractState_update(handle::Clong,input_pair::AbstractString,value1::Real,value2::Real)
Update the state of the AbstractState.
param handle The integer handle for the state class stored in memory
param input_pair The integer value for the input pair obtained from get_input_pair_index(param::AbstractString)
param value1 The first input value
param value2 The second input value
"""
function AbstractState_update(handle::Clong,input_pair::Clong,value1::Real,value2::Real)
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: ", unsafe_string(convert(Ptr{UInt8}, pointer(message_buffer))))
elseif errcode[] == 2
error("CoolProp: message buffer too small")
else # == 3
error("CoolProp: unknown error")
end
end
return nothing
end

function AbstractState_update(handle::Clong,input_pair::AbstractString,value1::Real,value2::Real)
AbstractState_update(handle::Clong,get_input_pair_index(input_pair),value1::Real,value2::Real)
return nothing
end
include("CoolPropLowLevel.jl");

"""
AbstractState_specify_phase(handle::Clong,phase::AbstractString)
Expand Down Expand Up @@ -204,33 +105,6 @@ function AbstractState_unspecify_phase(handle::Clong)
return nothing
end

"""
AbstractState_keyed_output(handle::Clong, param::Clong)
AbstractState_output(handle::Clong, param::AbstractString)
Get an output value from the AbstractState using an integer value for the desired output value.
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,message_buffer::Array{UInt8,1},buffer_length)
if errcode[] != 0
if errcode[] == 1
error("CoolProp: ", unsafe_string(convert(Ptr{UInt8}, pointer(message_buffer))))
elseif errcode[] == 2
error("CoolProp: message buffer too small")
else # == 3
error("CoolProp: unknown error")
end
elseif output == -Inf
error("CoolProp: no correct state has been set with AbstractState_update")
end
return output
end
function AbstractState_output(handle::Clong, param::AbstractString)
return AbstractState_keyed_output(handle, get_param_index(param))
end

# /**
# * @brief Calculate a saturation derivative from the AbstractState using integer values for the desired parameters
Expand Down Expand Up @@ -520,4 +394,10 @@ const PhaseSI = phasesi;
const K2F = k2f;
const F2K = f2k;
const HAPropsSI = hapropssi;
const AbstractState_factory = abstractstate_factory;
const AbstractState_free = abstractstate_free;
const AbstractState_set_fractions = abstractstate_set_fractions;
const AbstractState_update = abstractstate_update;
const AbstractState_keyed_output = abstractstate_keyed_output;
const AbstractState_output = abstractstate_output;
end #module
2 changes: 1 addition & 1 deletion test/CoolPropHighLevel.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
Return a value that does not depend on the thermodynamic state - this is a convenience function that does the call `PropsSI(output, "", 0, "", 0, fluid)`.
# Arguments
* `fluid::AbstractString`: The name of the fluid that is part of CoolProp, for instance "n-Propane", to get a list of different passible fulid types call `get_global_param_string(key)` with `key` one of the following: `["FluidsList", "incompressible_list_pure", "incompressible_list_solution", "mixture_binary_pairs_list", "predefined_mixtures"]`, also there is a list in CoolProp online documentation [List of Fluids](http://www.coolprop.org/fluid_properties/PurePseudoPure.html#list-of-fluids), or simply type `?fluids`
* `fluid::AbstractString`: The name of the fluid that is part of CoolProp, for instance "n-Propane", to get a list of possible values types call `get_global_param_string(key)` with `key` one of the following: `["FluidsList", "incompressible_list_pure", "incompressible_list_solution", "mixture_binary_pairs_list", "predefined_mixtures"]`, also there is a list in CoolProp online documentation [List of Fluids](http://www.coolprop.org/fluid_properties/PurePseudoPure.html#list-of-fluids), or simply type `?fluids`
* `output::AbstractString`: The name of parameter to evaluate. to see a list type `?parameters`
# Example
Expand Down
3 changes: 3 additions & 0 deletions test/CoolPropInformation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@ end
get_param_index(param::AbstractString)
Get the index as a long for a parameter "T", "P", etc, for `abstractstate_keyed_output()` function.
# Arguments
* `param`: A string represents parameter name, to see full list check "Table of string inputs to PropsSI function": http://www.coolprop.org/coolprop/HighLevelAPI.html#parameter-table, or simply type `get_global_param_string("parameter_list")`
"""
function get_param_index(param::AbstractString)
val = ccall( (:get_param_index, "CoolProp"), Clong, (Ptr{UInt8},), param)
Expand Down
177 changes: 177 additions & 0 deletions test/CoolPropLowLevel.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
# ---------------------------------
# Low-level access
# ---------------------------------

"""
abstractstate_factory(backend::AbstractString, fluids::AbstractString)
Generate an AbstractState instance, return an integer handle to the state class generated to be used in the other low-level accessor functions.
# Arguments
* `backend`: The backend you will use, `["HEOS", "REFPROP", "INCOMP", "IF97", "TREND", "HEOS&TTSE", "HEOS&BICUBIC", "SRK", "PR", "VTPR"]` etc.
* `fluids`: '&' delimited list of fluids, to get a list of possible values call `get_global_param_string(key)` with `key` one of the following: `["FluidsList", "incompressible_list_pure", "incompressible_list_solution", "mixture_binary_pairs_list", "predefined_mixtures"]`, also there is a list in CoolProp online documentation [List of Fluids](http://www.coolprop.org/fluid_properties/PurePseudoPure.html#list-of-fluids), or simply type `?fluids`
# Example
```julia
julia> HEOS = abstractstate_factory("HEOS", "R245fa");
julia> TTSE = abstractstate_factory("HEOS&TTSE", "R245fa");
julia> BICU = abstractstate_factory("HEOS&BICUBIC", "R245fa");
julia> SRK = abstractstate_factory("SRK", "R245fa");
julia> PR = abstractstate_factory("PR", "R245fa");
```
"""
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,message_buffer::Array{UInt8,1},buffer_length)
if errcode[] != 0
if errcode[] == 1
error("CoolProp: ", unsafe_string(convert(Ptr{UInt8}, pointer(message_buffer))))
elseif errcode[] == 2
error("CoolProp: message buffer too small")
else # == 3
error("CoolProp: unknown error")
end
end
return AbstractState
end

"""
abstractstate_free(handle::Clong)
Release a state class generated by the low-level interface wrapper.
# Arguments
* `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,message_buffer::Array{UInt8,1},buffer_length)
if errcode[] != 0
if errcode[] == 1
error("CoolProp: ", unsafe_string(convert(Ptr{UInt8}, pointer(message_buffer))))
elseif errcode[] == 2
error("CoolProp: message buffer too small")
else # == 3
error("CoolProp: unknown error")
end
end
return nothing
end

"""
abstractstate_set_fractions(handle::Clong,fractions::Array)
Set the fractions (mole, mass, volume) for the AbstractState.
# Arguments
* `handle`: The integer handle for the state class stored in memory
* `fractions`: The array of fractions
# Example
```julia
julia> handle = abstractstate_factory("HEOS", "Water&Ethanol");
julia> pq_inputs = get_input_pair_index("PQ_INPUTS");
julia> t = get_param_index("T");
julia> abstractstate_set_fractions(handle, [0.4, 0.6]);
julia> abstractstate_update(handle,pq_inputs,101325, 0);
julia> abstractstate_keyed_output(handle,t)
352.3522212991724
julia> abstractstate_free(handle);
```
"""
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,message_buffer::Array{UInt8,1},buffer_length)
if errcode[] != 0
if errcode[] == 1
error("CoolProp: ", unsafe_string(convert(Ptr{UInt8}, pointer(message_buffer))))
elseif errcode[] == 2
error("CoolProp: message buffer too small")
else # == 3
error("CoolProp: unknown error")
end
end
return nothing
end

"""
abstractstate_update(handle::Clong,input_pair::Clong,value1::Real,value2::Real)
abstractstate_update(handle::Clong,input_pair::AbstractString,value1::Real,value2::Real)
Update the state of the AbstractState.
# Arguments
* `handle`: The integer handle for the state class stored in memory
* `input_pair::Clong`: The integer value for the input pair obtained from get_input_pair_index(param::AbstractString)
* `input_pair::AbstractString`: The name of an input pair
* `value1`: The first input value
* `value2`: The second input value
# Example
```julia
julia> handle = abstractstate_factory("HEOS", "Water&Ethanol");
julia> pq_inputs = get_input_pair_index("PQ_INPUTS");
julia> t = get_param_index("T");
julia> abstractstate_set_fractions(handle, [0.4, 0.6]);
julia> abstractstate_update(handle,pq_inputs,101325, 0);
julia> abstractstate_keyed_output(handle,t)
352.3522212991724
julia> abstractstate_free(handle);
```
"""
function abstractstate_update(handle::Clong,input_pair::Clong,value1::Real,value2::Real)
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: ", unsafe_string(convert(Ptr{UInt8}, pointer(message_buffer))))
elseif errcode[] == 2
error("CoolProp: message buffer too small")
else # == 3
error("CoolProp: unknown error")
end
end
return nothing
end

function abstractstate_update(handle::Clong,input_pair::AbstractString,value1::Real,value2::Real)
abstractstate_update(handle::Clong,get_input_pair_index(input_pair),value1::Real,value2::Real)
return nothing
end

"""
abstractstate_keyed_output(handle::Clong, param::Clong)
Get an output value from the `abstractstate` using an integer value for the desired output value.
# Arguments
* `handle`: The integer handle for the state class stored in memory
* `param::Clong`: param The integer value for the parameter you want
# Note
See `abstractstate_output`
"""
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,message_buffer::Array{UInt8,1},buffer_length)
if errcode[] != 0
if errcode[] == 1
error("CoolProp: ", unsafe_string(convert(Ptr{UInt8}, pointer(message_buffer))))
elseif errcode[] == 2
error("CoolProp: message buffer too small")
else # == 3
error("CoolProp: unknown error")
end
elseif output == -Inf
error("CoolProp: no correct state has been set with AbstractState_update")
end
return output
end

"""
abstractstate_output(handle::Clong, param::AbstractString)
Get an output value from the `abstractstate` using an integer value for the desired output value. It is a convenience function that call `abstractstate_keyed_output`
# Arguments
* `handle`: The integer handle for the state class stored in memory
* `param::AbstractString`: The name for the parameter you want
"""
function abstractstate_output(handle::Clong, param::AbstractString)
return abstractstate_keyed_output(handle, get_param_index(param))
end
14 changes: 14 additions & 0 deletions test/testLow.jl
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,17 @@ catch
warn("QT_INPUTS not ready");
end
AbstractState_free(IF97);
#AbstractState_output AbstractState_set_fractions
handle = AbstractState_factory("HEOS", "Water&Ethanol");
pq_inputs = get_input_pair_index("PQ_INPUTS");
t = get_param_index("T");
AbstractState_set_fractions(handle, [0.4, 0.6]);
AbstractState_update(handle,pq_inputs,101325, 0);
if is_apple()
@test_approx_eq AbstractState_keyed_output(handle,t) 352.3522212978604
@test_approx_eq AbstractState_output(handle,"T") 352.3522212978604
else
@test_approx_eq AbstractState_keyed_output(handle,t) 352.3522212991724
@test_approx_eq AbstractState_output(handle,"T") 352.3522212991724
end
AbstractState_free(handle);

0 comments on commit 0f428d2

Please sign in to comment.