From 0b6594f0b1a449e97d795cf1057638b991ee2382 Mon Sep 17 00:00:00 2001 From: Bodo Kaiser Date: Wed, 11 Sep 2024 15:54:36 +0200 Subject: [PATCH] replaced Unitful with DynamicQuantities --- .github/workflows/CI.yml | 3 +- Project.toml | 6 +-- src/HeatTransferFluids.jl | 6 +-- src/flow.jl | 6 +-- src/fluid.jl | 105 +++++++++++++++--------------------- src/heat_transfer.jl | 4 +- src/pressure_drop.jl | 26 ++++----- src/structure.jl | 111 ++++++++++++++++---------------------- test/flow.jl | 4 +- test/fluid.jl | 8 +-- test/heat_transfer.jl | 4 +- test/pressure_drop.jl | 6 +-- test/runtests.jl | 4 +- test/structure.jl | 25 ++++++--- 14 files changed, 141 insertions(+), 177 deletions(-) diff --git a/.github/workflows/CI.yml b/.github/workflows/CI.yml index c1ba556..f184755 100644 --- a/.github/workflows/CI.yml +++ b/.github/workflows/CI.yml @@ -18,9 +18,8 @@ jobs: fail-fast: false matrix: version: - - "1.0" - "1.8" - - "nightly" + - "1.10" os: - ubuntu-latest arch: diff --git a/Project.toml b/Project.toml index 232f550..6359acc 100644 --- a/Project.toml +++ b/Project.toml @@ -1,14 +1,14 @@ name = "HeatTransferFluids" uuid = "44505bb3-94ea-493c-a20c-f0ca548ab76b" authors = ["Bodo Kaiser"] -version = "0.0.1" +version = "0.1.0" [deps] -Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d" +DynamicQuantities = "06fc5a27-2a28-4c7c-a15d-362465fb6821" [compat] julia = "1" -Unitful = "1.12" +DynamicQuantities = "1" [extras] Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" diff --git a/src/HeatTransferFluids.jl b/src/HeatTransferFluids.jl index ef359dc..1b4ef88 100644 --- a/src/HeatTransferFluids.jl +++ b/src/HeatTransferFluids.jl @@ -1,10 +1,6 @@ module HeatTransferFluids -using Unitful - -@derived_dimension SpecificHeatCapacity dimension(u"J/(kg*K)") -@derived_dimension ThermalConductivity dimension(u"W/(m*K)") -@derived_dimension VolumeFlow dimension(u"m^3/s") +using DynamicQuantities include("fluid.jl") include("structure.jl") diff --git a/src/flow.jl b/src/flow.jl index 242324e..0d379cf 100644 --- a/src/flow.jl +++ b/src/flow.jl @@ -18,7 +18,7 @@ function reynolds_number(f::Fluid, t::Tube) μ = f.viscosity D = t.diameter - return upreferred(ϱ * v * D / μ) + return ustrip(ϱ * v * D / μ) end """ @@ -37,7 +37,7 @@ function prandtl_number(f::Fluid) μ = f.viscosity k = f.thermal_conductivity - return c * μ / k + return ustrip(c * μ / k) end """ @@ -85,4 +85,4 @@ function nusselt_number_turbulent(f::Fluid, t::Tube) ξ = (1.8log10(Re) - 1.5)^(-2) return ((ξ / 8)Re * Pr) * (1 + λ^(2 / 3)) / (1 + 12.7(ξ / 8)^(1 / 2) * (Pr^(2 / 3) - 1)) -end \ No newline at end of file +end diff --git a/src/fluid.jl b/src/fluid.jl index 08c2993..74989eb 100644 --- a/src/fluid.jl +++ b/src/fluid.jl @@ -1,72 +1,51 @@ export Fluid, Water """ - Fluid{ - T1<:Unitful.Density, - T2<:Unitful.Velocity, - T3<:Unitful.DynamicViscosity, - T4<:SpecificHeatCapacity, - T5<:ThermalConductivity, - T6<:Unitful.Temperature, - } + Fluid{T<:AbstractQuantity} A fluid with the important physical properties to be used in heat transfer calculations. # Fields -- `density::T1`: the density of the fluid -- `velocity::T2`: the velocity of the fluid -- `viscosity::T3`: the viscosity of the fluid -- `heat_capacity::T4`: the specific heat capacity of the fluid -- `thermal_conductivity::T5`: the thermal conductivity of the fluid -- `temperature::T6`: the temperature of the fluid +- `density::T`: the density of the fluid +- `velocity::T`: the velocity of the fluid +- `viscosity::T`: the viscosity of the fluid +- `heat_capacity::T`: the specific heat capacity of the fluid +- `thermal_conductivity::T`: the thermal conductivity of the fluid +- `temperature::T`: the temperature of the fluid """ -struct Fluid{ - T1<:Unitful.Density, - T2<:Unitful.Velocity, - T3<:Unitful.DynamicViscosity, - T4<:SpecificHeatCapacity, - T5<:ThermalConductivity, - T6<:Unitful.Temperature, -} - density::T1 - velocity::T2 - viscosity::T3 - heat_capacity::T4 - thermal_conductivity::T5 - temperature::T6 -end - -""" - Fluid( - density::Unitful.Density, - velocity::Unitful.Velocity, - viscosity::Unitful.DynamicViscosity, - heat_capacity::SpecificHeatCapacity, - thermal_conductivity::ThermalConductivity, - temperature::Unitful.Temperature, - ) +struct Fluid{T<:AbstractQuantity} + density::T + velocity::T + viscosity::T + heat_capacity::T + thermal_conductivity::T + temperature::T -Returns a Fluid with the given properties. + function Fluid(; + density::T, + velocity::T, + viscosity::T, + heat_capacity::T, + thermal_conductivity::T, + temperature::T, + ) where {T} + @assert dimension(density) == dimension(u"kg/m^3") "density must have units of mass per volume" + @assert dimension(velocity) == dimension(u"m/s") "velocity must have units of length per time" + @assert dimension(viscosity) == dimension(u"Pa*s") "viscosity must have units of pressure times time" + @assert dimension(heat_capacity) == dimension(u"J/(kg*K)") "heat_capacity must have units of energy per mass per temperature" + @assert dimension(thermal_conductivity) == dimension(u"W/(m*K)") "thermal_conductivity must have units of power per length per temperature" + @assert dimension(temperature) == dimension(u"K") "temperature must have units of temperature" + @assert ustrip(density) > 0 "density must be positive" + @assert ustrip(velocity) > 0 "velocity must be positive" + @assert ustrip(viscosity) > 0 "viscosity must be positive" + @assert ustrip(heat_capacity) > 0 "heat_capacity must be positive" + @assert ustrip(thermal_conductivity) > 0 "thermal_conductivity must be positive" + @assert ustrip(temperature) > 0 "temperature must be positive" -# Keywords -- `density::Unitful.Density`: the density of the fluid -- `velocity::Unitful.Velocity`: the velocity of the fluid -- `viscosity::Unitful.DynamicViscosity`: the viscosity of the fluid -- `heat_capacity::SpecificHeatCapacity`: the specific heat capacity of the fluid -- `thermal_conductivity::ThermalConductivity`: the thermal conductivity of the fluid -- `temperature::Unitful.Temperature`: the temperature of the fluid + new{T}(density, velocity, viscosity, heat_capacity, thermal_conductivity, temperature) + end -# Returns -- `Fluid`: the fluid with the given properties -""" -Fluid(; - density::Unitful.Density, - velocity::Unitful.Velocity, - viscosity::Unitful.DynamicViscosity, - heat_capacity::SpecificHeatCapacity, - thermal_conductivity::ThermalConductivity, - temperature::Unitful.Temperature, -) = Fluid(density, velocity, viscosity, heat_capacity, thermal_conductivity, temperature) +end """ Water @@ -74,17 +53,17 @@ Fluid(; Returns a Fluid with the properties of water close to room temperature. # Keywords -- `velocity::Unitful.Velocity`: the velocity of the water -- `temperature::Unitful.Temperature`: the temperature of the water +- `velocity::AbstractQuantity`: the velocity of the water +- `temperature::AbstractQuantity`: the temperature of the water # Returns - `Fluid`: the fluid with the properties of water close to room temperature at the given velocity and temperature """ -Water(; velocity::Unitful.Velocity, temperature::Unitful.Temperature) = Fluid( +Water(; velocity::AbstractQuantity, temperature::AbstractQuantity) = Fluid( density = 997u"kg/m^3", velocity = velocity, - viscosity = 1u"mPa*s", + viscosity = 1e-3u"Pa*s", heat_capacity = 4186u"J/(kg*K)", thermal_conductivity = 0.591u"W/(m*K)", temperature = temperature, -) \ No newline at end of file +) diff --git a/src/heat_transfer.jl b/src/heat_transfer.jl index 382f458..c9d4e18 100644 --- a/src/heat_transfer.jl +++ b/src/heat_transfer.jl @@ -10,7 +10,7 @@ Computes the heat transfer coefficient of a `fluid`` flowing through a `tube`. - `t::Tube`: the tube through which the fluid flows # Returns -- `Unitful.Quantity`: the heat transfer coefficient of the fluid flowing through the tube +- `DynamicQuantity.AbstractQuantity`: the heat transfer coefficient of the fluid flowing through the tube """ function heat_transfer(f::Fluid, t::Tube) k = f.thermal_conductivity @@ -18,4 +18,4 @@ function heat_transfer(f::Fluid, t::Tube) Nu = nusselt_number(f, t) return Nu * (k / D) -end \ No newline at end of file +end diff --git a/src/pressure_drop.jl b/src/pressure_drop.jl index bca4973..7c731e8 100644 --- a/src/pressure_drop.jl +++ b/src/pressure_drop.jl @@ -15,7 +15,7 @@ See VDI Heat Atlas, p. 1057 for details. - `friction::Float`: the friction factor of the fluid flowing through the tube # Returns -- `Unitful.Quantity`: the pressure drop of the fluid flowing through the tube +- `DynamicQuantities.AbstractQuantity`: the pressure drop of the fluid flowing through the tube """ function pressure_drop(f::Fluid, t::Tube; friction = nothing) Re = reynolds_number(f, t) @@ -30,7 +30,7 @@ function pressure_drop(f::Fluid, t::Tube; friction = nothing) ξ = friction end - return uconvert(u"bar", ξ * (L / d) * (ρ * u^2 / 2)) + return uconvert(us"bar", ξ * (L / d) * (ρ * u^2 / 2)) end """ @@ -45,14 +45,14 @@ See VDI Heat Atlas, p. 1062 to 1063 for details. - `c::Coil`: the coiled tube through which the fluid flows # Returns -- `Unitful.Quantity`: the pressure drop of the fluid flowing through the tube +- `DynamicQuantity.AbstractQuantity`: the pressure drop of the fluid flowing through the tube """ -function pressure_drop(f::Fluid, c::Coil) - Dw = c.diameter - H = c.pitch +function pressure_drop(f::Fluid, h::Helix) + Dw = h.diameter + H = h.pitch D = Dw * (1 + (H / (π * Dw))^2) - d = c.tube.diameter - Re = reynolds_number(f, c.tube) + d = h.tube.diameter + Re = reynolds_number(f, h.tube) if (d / D)^(-2) < Re && Re < 2300 ξ = (64 / Re) * (1 + 0.033(log10(Re * sqrt(d / D)))) @@ -60,7 +60,7 @@ function pressure_drop(f::Fluid, c::Coil) ξ = (0.3164 / Re^(1 / 4)) * (1 + 0.095sqrt(d / D) * Re^(1 / 4)) end - return pressure_drop(f, c.tube, friction = ξ) + return pressure_drop(f, h.tube, friction = ξ) end """ @@ -73,16 +73,16 @@ Computes the pressure drop of a `fluid` flowing through a `valve`. - `v::Valve`: the valve through which the fluid flows # Returns -- `Unitful.Quantity`: the pressure drop of the fluid flowing through the tube +- `DynamicQuantity.AbstractQuantity`: the pressure drop of the fluid flowing through the tube """ # https://en.wikipedia.org/wiki/Flow_coefficient function pressure_drop(f::Fluid, v::Valve) - Q = uconvert(u"m^3/hr", v.flow_rate) - Kv = uconvert(u"m^3/hr", v.flow_factor) + Q = uconvert(us"m^3/hr", v.flow_rate) + Kv = uconvert(us"m^3/hr", v.flow_factor) ρ = f.density ρ₀ = 1000u"kg/m^3" p₀ = 1u"bar" return p₀ * (Q / Kv)^2 * ρ / ρ₀ -end \ No newline at end of file +end diff --git a/src/structure.jl b/src/structure.jl index 2ba94e1..6b6994b 100644 --- a/src/structure.jl +++ b/src/structure.jl @@ -1,4 +1,4 @@ -export Structure, Tube, Coil, Valve +export Structure, Tube, Helix, Valve """ Structure @@ -8,90 +8,69 @@ A structure is an element through which a fluid flows. abstract type Structure end """ - Tube + Tube{T<:AbstractQuantity} A tube with a diameter and length through which a fluid flows. # Fields -- `diameter::Unitful.Length`: the diameter of the tube -- `length::Unitful.Length`: the length of the tube +- `diameter::T`: the diameter of the tube +- `length::T`: the length of the tube """ -struct Tube{T1<:Unitful.Length,T2<:Unitful.Length} <: Structure - diameter::T1 - length::T2 +struct Tube{T<:AbstractQuantity} <: Structure + diameter::T + length::T + + function Tube(; diameter::T, length::T) where {T} + @assert dimension(diameter) == dimension(u"m") "diameter must have units of length" + @assert dimension(length) == dimension(u"m") "length must have units of length" + @assert ustrip(diameter) > 0 "diameter must be positive" + @assert ustrip(length) > 0 "length must be positive" + new{T}(diameter, length) + end end """ - Tube(; diameter::Unitful.Length, length::Unitful.Length) + Helix{T<:Tube,V<:AbstractQuantity} -Returns a Tube with the given tube diameter and length. - -# Keywords -- `diameter::Unitful.Length`: the diameter of the tube -- `length::Unitful.Length`: the length of the tube - -# Returns -- `Tube`: the tube with the given diameter and length -""" -Tube(; diameter::Unitful.Length, length::Unitful.Length) = Tube(diameter, length) - -""" - Coil - -A tube in the shape of a coil with a diameter and pitch. +A tube in the shape of a helix with a diameter and pitch. # Fields -- `tube::Tube`: the tube that is coiled -- `diameter::Unitful.Length`: the diameter of the coil -- `pitch::Unitful.Length`: the pitch of the coil +- `tube::T`: the tube that is coiled +- `diameter::V`: the diameter of the coil +- `pitch::V`: the pitch of the coil """ -struct Coil{T<:Tube,T1<:Unitful.Length,T2<:Unitful.Length} <: Structure +struct Helix{T<:Tube,V<:AbstractQuantity} <: Structure tube::T - diameter::T1 - pitch::T2 + diameter::V + pitch::V + + function Helix(t::T; diameter::V, pitch::V) where {T,V} + @assert dimension(diameter) == dimension(u"m") "diameter must have units of length" + @assert dimension(pitch) == dimension(u"m") "pitch must have units of length" + @assert ustrip(diameter) > 0 "diameter must be positive" + @assert ustrip(pitch) > 0 "pitch must be positive" + new{T,V}(t, diameter, pitch) + end end """ - Coil(t::Tube; diameter::Unitful.Length, pitch::Unitful.Length) - -Returns a Coil with the given tube, diameter, and pitch. - -# Arguments -- `t::Tube`: the tube that is coiled - -# Keywords -- `diameter::Unitful.Length`: the diameter of the coil -- `pitch::Unitful.Length`: the pitch of the coil - -# Returns -- `Coil`: the coil with the given tube, diameter, and pitch -""" -Coil(t::Tube; diameter::Unitful.Length, pitch::Unitful.Length) = Coil(t, diameter, pitch) - -""" - Valve + Valve{T<:AbstractQuantity} A valve parametrized by a flow rate and flow factor. # Fields -- `flow_rate::VolumeFlow`: the flow rate of the valve -- `flow_factor::VolumeFlow`: the flow factor of the valve +- `flow_rate::T`: the flow rate of the valve +- `flow_factor::T`: the flow factor of the valve """ -struct Valve{T1<:VolumeFlow,T2<:VolumeFlow} <: Structure - flow_rate::T1 - flow_factor::T2 +struct Valve{T<:AbstractQuantity} <: Structure + flow_rate::T + flow_factor::T + + function Valve(; flow_rate::T, flow_factor::T) where {T} + @assert dimension(flow_rate) == dimension(u"m^3/s") "flow_rate must have units of volume per time" + @assert dimension(flow_factor) == dimension(u"m^3/s") "flow_factor must have units of volume per time" + @assert ustrip(flow_rate) > 0 "flow_rate must be positive" + @assert ustrip(flow_factor) > 0 "flow_factor must be positive" + new{T}(flow_rate, flow_factor) + end end - -""" - Valve(; flow_rate::VolumeFlow, flow_factor::VolumeFlow) - -Returns a Valve with the given flow rate and flow factor. - -# Keywords -- `flow_rate::VolumeFlow`: the flow rate of the valve -- `flow_factor::VolumeFlow`: the flow factor of the valve - -# Returns -- `Valve`: the valve with the given flow rate and flow factor -""" -Valve(; flow_rate::VolumeFlow, flow_factor::VolumeFlow) = Valve(flow_rate, flow_factor) \ No newline at end of file diff --git a/test/flow.jl b/test/flow.jl index 05acff5..9dac6d1 100644 --- a/test/flow.jl +++ b/test/flow.jl @@ -1,6 +1,6 @@ @testset "Flow" begin - fluid = Water(velocity = 0.58u"m/s", temperature = 20u"°C") + fluid = Water(velocity = 0.58u"m/s", temperature = 293.15u"K") tube = Tube(diameter = 2.7u"mm", length = 9.5153u"m") @testset "reynolds_number" begin @@ -15,4 +15,4 @@ @test nusselt_number(fluid, tube) ≈ 5 atol = 1 end -end \ No newline at end of file +end diff --git a/test/fluid.jl b/test/fluid.jl index 58338a5..910cb0c 100644 --- a/test/fluid.jl +++ b/test/fluid.jl @@ -1,12 +1,12 @@ @testset "Fluid" begin - @test Water(velocity = 1u"m/s", temperature = 20u"°C") == Fluid( + @test Water(velocity = 1u"m/s", temperature = 293.15u"K") == Fluid( density = 997u"kg/m^3", velocity = 1u"m/s", - viscosity = 1u"mPa*s", + viscosity = 1e-3u"Pa*s", heat_capacity = 4186u"J/(kg*K)", thermal_conductivity = 0.591u"W/(m*K)", - temperature = 20u"°C", + temperature = 293.15u"K", ) -end \ No newline at end of file +end diff --git a/test/heat_transfer.jl b/test/heat_transfer.jl index 0d1c0e2..2988bd5 100644 --- a/test/heat_transfer.jl +++ b/test/heat_transfer.jl @@ -1,9 +1,9 @@ @testset "heat_transfer" begin - fluid = Water(velocity = 3u"m/s", temperature = 20u"°C") + fluid = Water(velocity = 3u"m/s", temperature = 293.15u"K") tube = Tube(diameter = 2.7u"mm", length = 14u"m") #coil = Coil(tube; diameter = 95u"mm", pitch = 5.2u"mm") @test heat_transfer(fluid, tube) ≈ 12u"kW/(K*m^2)" atol = 1u"kW/(K*m^2)" -end \ No newline at end of file +end diff --git a/test/pressure_drop.jl b/test/pressure_drop.jl index 5e0ed9e..3bc8d61 100644 --- a/test/pressure_drop.jl +++ b/test/pressure_drop.jl @@ -1,6 +1,6 @@ @testset "pressure_drop" begin - fluid = Water(velocity = 0.58u"m/s", temperature = 20u"°C") + fluid = Water(velocity = 0.58u"m/s", temperature = 293.15u"K") tube = Tube(diameter = 2.7u"mm", length = 9.5153u"m") @testset "tube" begin @@ -8,7 +8,7 @@ end @testset "coil" begin - @test pressure_drop(fluid, Coil(tube, diameter = 63.1u"mm", pitch = 2.6u"mm")) ≈ 0.26u"bar" atol = + @test pressure_drop(fluid, Helix(tube, diameter = 63.1u"mm", pitch = 2.6u"mm")) ≈ 0.26u"bar" atol = 0.01u"bar" end @@ -17,4 +17,4 @@ 0.0017u"bar" atol = 0.0001u"bar" end -end \ No newline at end of file +end diff --git a/test/runtests.jl b/test/runtests.jl index 49b7e2d..621d4de 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1,9 +1,9 @@ using HeatTransferFluids using Test -using Unitful +using DynamicQuantities include("fluid.jl") include("structure.jl") include("flow.jl") include("pressure_drop.jl") -include("heat_transfer.jl") \ No newline at end of file +include("heat_transfer.jl") diff --git a/test/structure.jl b/test/structure.jl index 0dc4c88..0f2b9f4 100644 --- a/test/structure.jl +++ b/test/structure.jl @@ -1,17 +1,28 @@ @testset "Structure" begin + tube = Tube(diameter = 2.7u"mm", length = 9u"m") + @testset "Tube" begin - @test Tube(diameter = 2.7u"mm", length = 9u"m") == Tube(2.7u"mm", 9u"m") + @test tube.diameter == 2.7u"mm" + @test tube.length == 9u"m" end - @testset "Coil" begin - @test Coil(Tube(diameter = 2.7u"mm", length = 9u"m"), diameter = 2.7u"mm", pitch = 9u"m") == - Coil(Tube(2.7u"mm", 9u"m"), 2.7u"mm", 9u"m") + @testset "Helix" begin + h = Helix(tube; diameter = 2.7u"mm", pitch = 9u"m") + @test h.tube == tube + @test h.diameter == 2.7u"mm" + @test h.pitch == 9u"m" end @testset "Valve" begin - @test Valve(flow_rate = 62u"L/hr", flow_factor = 1.5u"m^3/hr") == - Valve(62u"L/hr", 1.5u"m^3/hr") + v = Valve(flow_rate = 62u"L/hr", flow_factor = 1.5u"m^3/hr") + @test v.flow_rate == 62u"L/hr" + @test v.flow_factor == 1.5u"m^3/hr" + + @test_throws AssertionError Valve(flow_rate = 0u"L/hr", flow_factor = 1.5u"m^3/hr") + @test_throws AssertionError Valve(flow_rate = 10u"L/hr", flow_factor = 0u"m^3/hr") + @test_throws AssertionError Valve(flow_rate = 10u"m/hr", flow_factor = 1.5u"m^3/hr") + @test_throws AssertionError Valve(flow_rate = 10u"L/hr", flow_factor = 1.5u"m^2/hr") end -end \ No newline at end of file +end