Skip to content

Commit

Permalink
Add a faster unit converter for large vectors
Browse files Browse the repository at this point in the history
Won't handle temperature
moyner committed Jan 29, 2024
1 parent badf23c commit 3bd5e92
Showing 2 changed files with 23 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/InputParser/keywords/grid.jl
Original file line number Diff line number Diff line change
@@ -97,13 +97,13 @@ end

function parse_keyword!(data, outer_data, units, cfg, f, ::Val{:COORD})
coord = parse_deck_vector(f, Float64)
coord = swap_unit_system!(coord, units, Val(:length))
coord = swap_unit_system_fast!(coord, units, Val(:length))
data["COORD"] = coord
end

function parse_keyword!(data, outer_data, units, cfg, f, ::Val{:ZCORN})
zcorn = parse_deck_vector(f, Float64)
zcorn = swap_unit_system!(zcorn, units, Val(:length))
zcorn = swap_unit_system_fast!(zcorn, units, Val(:length))
data["ZCORN"] = zcorn
end

21 changes: 21 additions & 0 deletions src/InputParser/units.jl
Original file line number Diff line number Diff line change
@@ -206,6 +206,27 @@ function swap_unit_system!(x::AbstractArray, systems, k)
return swap_unit_system!(x, systems, Val(k))
end

function swap_unit_system_fast!(v, systems::Union{Nothing, Missing}, k::Val)
# No systems - trivial conversion
return v
end

function swap_unit_system_fast!(x::AbstractArray, systems, k::Val)
(; to, from) = systems

to_unit = deck_unit(to, k)
from_unit = deck_unit(from, k)

val_si = convert_to_si(1.0, from_unit)
val_final = convert_from_si(val_si, to_unit)

val_final::Float64
for i in eachindex(x)
x[i] *= val_final
end
return x
end

function swap_unit_system!(x::AbstractArray, systems, k::Val)
for i in eachindex(x)
x[i] = swap_unit_system(x[i], systems, k)

0 comments on commit 3bd5e92

Please sign in to comment.