Skip to content

Commit

Permalink
New function Modia.unitAsString(unitOfQuantity); see Unitful issue 412 (
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinOtter committed Mar 6, 2022
1 parent 2c0159f commit 5457cd3
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 4 deletions.
31 changes: 31 additions & 0 deletions src/Modia.jl
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,37 @@ The function is defined as: `stripUnit(v) = ustrip.(upreferred.(v))`.
"""
stripUnit(v) = ustrip.(upreferred.(v))


"""
str = unitAsString( unitOfQuantity::Unitful.FreeUnits )
Return a string representation of the unit of a quantity that can be used in a unit string macro
(see also Unitful [issue 412](https://github.com/PainterQubits/Unitful.jl/issues/412)).
# Example
```
v1 = 2.0u"m/s"
v1_unit = unitAsString( unit(v1) ) # = "m*s^-1"
v2_withoutUnit = 2.0
code = :( \$v2_withoutUnit@u_str(\$v1_unit) ) # = 2.0u"m*s^-1"
v2 = eval(code)
@show v1
@show v1_unit
@show v2
```
# Notes
Transforms unit to string representation that is parseable again
(see also Unitful [issue 412](https://github.com/PainterQubits/Unitful.jl/issues/412)).
This implementation is a hack and only works in common cases.
Implementation is performed in the following way:
1. Transform to string and display exponents on units not as Unicode superscripts (= default on macOS).
2. Replace " " by "*", since Unitful removes "*" when converting to string.
"""
unitAsString(unitOfQuantity::Unitful.FreeUnits) = replace(repr(unitOfQuantity,context = Pair(:fancy_exponent,false)), " " => "*")


include("EquationAndStateInfo.jl")
include("StateSelection.jl")

Expand Down
5 changes: 1 addition & 4 deletions src/ModiaLang.jl
Original file line number Diff line number Diff line change
Expand Up @@ -552,10 +552,7 @@ function stateSelectionAndCodeGeneration(modStructure, Gexplicit, name, modelMod
@assert all([un[i] == un[1] for i in 2:length(un)]) "The unit of all elements of state vector must be equal: $var::$(value)"
un = un[1]
end
# Transform unit to string representation that is parseable again (see also https://github.com/PainterQubits/Unitful.jl/issues/412):
# - Display exponents on units not as superscripts (= default on macOS)
# - Replace " " by "*", since Unitful removes "*" when converting to string
return replace(repr(un,context = Pair(:fancy_exponent,false)), " " => "*")
return unitAsString(un)
end

function var_startInitFixed(v_original)
Expand Down
17 changes: 17 additions & 0 deletions test/TestUnitAsString.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module TestUnitAsString

using Modia
using Test

v1 = 2.0u"m/s"
unit_v1 = unit(v1)
v1_unit = Modia.unitAsString( unit_v1 ) # = "m*s^-1"
v2_withoutUnit = 2.0
code = :( $v2_withoutUnit*@u_str($v1_unit) ) # = 2.0u"m*s^-1"
v2 = eval(code)
@show v1
@show v1_unit
@show v2

@test v1==v2
end
1 change: 1 addition & 0 deletions test/include_all.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Modia
import Modia.Test

Test.@testset "Test basic functionality" begin
include("TestUnitAsString.jl")
include("TestVariables.jl")
include("TestFirstOrder.jl")
include("TestFirstOrder2.jl")
Expand Down

0 comments on commit 5457cd3

Please # to comment.