diff --git a/src/Modia.jl b/src/Modia.jl index 70ad4ea..7e6e62f 100644 --- a/src/Modia.jl +++ b/src/Modia.jl @@ -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") diff --git a/src/ModiaLang.jl b/src/ModiaLang.jl index 16b8129..1e48db3 100644 --- a/src/ModiaLang.jl +++ b/src/ModiaLang.jl @@ -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) diff --git a/test/TestUnitAsString.jl b/test/TestUnitAsString.jl new file mode 100644 index 0000000..5e5fd1b --- /dev/null +++ b/test/TestUnitAsString.jl @@ -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 \ No newline at end of file diff --git a/test/include_all.jl b/test/include_all.jl index 429ef40..e004bc6 100644 --- a/test/include_all.jl +++ b/test/include_all.jl @@ -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")