Skip to content

Commit 5457cd3

Browse files
committed
New function Modia.unitAsString(unitOfQuantity); see Unitful issue 412 (JuliaPhysics/Unitful.jl#412).
1 parent 2c0159f commit 5457cd3

File tree

4 files changed

+50
-4
lines changed

4 files changed

+50
-4
lines changed

src/Modia.jl

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,37 @@ The function is defined as: `stripUnit(v) = ustrip.(upreferred.(v))`.
111111
"""
112112
stripUnit(v) = ustrip.(upreferred.(v))
113113

114+
115+
"""
116+
str = unitAsString( unitOfQuantity::Unitful.FreeUnits )
117+
118+
Return a string representation of the unit of a quantity that can be used in a unit string macro
119+
(see also Unitful [issue 412](https://github.com/PainterQubits/Unitful.jl/issues/412)).
120+
121+
# Example
122+
```
123+
v1 = 2.0u"m/s"
124+
v1_unit = unitAsString( unit(v1) ) # = "m*s^-1"
125+
v2_withoutUnit = 2.0
126+
code = :( \$v2_withoutUnit@u_str(\$v1_unit) ) # = 2.0u"m*s^-1"
127+
v2 = eval(code)
128+
@show v1
129+
@show v1_unit
130+
@show v2
131+
```
132+
133+
# Notes
134+
Transforms unit to string representation that is parseable again
135+
(see also Unitful [issue 412](https://github.com/PainterQubits/Unitful.jl/issues/412)).
136+
This implementation is a hack and only works in common cases.
137+
Implementation is performed in the following way:
138+
139+
1. Transform to string and display exponents on units not as Unicode superscripts (= default on macOS).
140+
2. Replace " " by "*", since Unitful removes "*" when converting to string.
141+
"""
142+
unitAsString(unitOfQuantity::Unitful.FreeUnits) = replace(repr(unitOfQuantity,context = Pair(:fancy_exponent,false)), " " => "*")
143+
144+
114145
include("EquationAndStateInfo.jl")
115146
include("StateSelection.jl")
116147

src/ModiaLang.jl

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -552,10 +552,7 @@ function stateSelectionAndCodeGeneration(modStructure, Gexplicit, name, modelMod
552552
@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)"
553553
un = un[1]
554554
end
555-
# Transform unit to string representation that is parseable again (see also https://github.com/PainterQubits/Unitful.jl/issues/412):
556-
# - Display exponents on units not as superscripts (= default on macOS)
557-
# - Replace " " by "*", since Unitful removes "*" when converting to string
558-
return replace(repr(un,context = Pair(:fancy_exponent,false)), " " => "*")
555+
return unitAsString(un)
559556
end
560557

561558
function var_startInitFixed(v_original)

test/TestUnitAsString.jl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
module TestUnitAsString
2+
3+
using Modia
4+
using Test
5+
6+
v1 = 2.0u"m/s"
7+
unit_v1 = unit(v1)
8+
v1_unit = Modia.unitAsString( unit_v1 ) # = "m*s^-1"
9+
v2_withoutUnit = 2.0
10+
code = :( $v2_withoutUnit*@u_str($v1_unit) ) # = 2.0u"m*s^-1"
11+
v2 = eval(code)
12+
@show v1
13+
@show v1_unit
14+
@show v2
15+
16+
@test v1==v2
17+
end

test/include_all.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import Modia
22
import Modia.Test
33

44
Test.@testset "Test basic functionality" begin
5+
include("TestUnitAsString.jl")
56
include("TestVariables.jl")
67
include("TestFirstOrder.jl")
78
include("TestFirstOrder2.jl")

0 commit comments

Comments
 (0)