Skip to content

Commit

Permalink
Add Buckingham pi tests
Browse files Browse the repository at this point in the history
  • Loading branch information
YingboMa committed Aug 15, 2023
1 parent 83e5d58 commit 782ec36
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/systems/validation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -279,23 +279,23 @@ function check_units(eqs...)
end
all_dimensionless(states) = all(x -> safe_get_unit(x, "") in (unitless, nothing), states)

using DynamicQuantities: @us_str
const UNITLESS_UNIT = us"1"
using DynamicQuantities: @us_str, expand_units, DynamicQuantities, SymbolicDimensions
const UNITLESS_UNIT = DynamicQuantities.Quantity(1, SymbolicDimensions)
const UNITLESS_SI = expand_units(UNITLESS_UNIT)
const N_UNIT_DIMS = nfields(dimension(UNITLESS_SI))
const N_UNIT_DIMS = nfields(DynamicQuantities.dimension(UNITLESS_SI))
function _get_unit(x)
getmetadata(ModelingToolkit.unwrap(x), ModelingToolkit.VariableUnit, UNITLESS_UNIT)

Check warning on line 287 in src/systems/validation.jl

View check run for this annotation

Codecov / codecov/patch

src/systems/validation.jl#L286-L287

Added lines #L286 - L287 were not covered by tests
end

function buckingham_pi(vals)
us = _get_unit.(vals)
M = [convert(Rational, getfield(dimension(expand_units(u)), p))
M = [convert(Rational, getfield(DynamicQuantities.dimension(expand_units(u)), p))

Check warning on line 292 in src/systems/validation.jl

View check run for this annotation

Codecov / codecov/patch

src/systems/validation.jl#L290-L292

Added lines #L290 - L292 were not covered by tests
for u in us, p in 1:N_UNIT_DIMS]
s = lcm(denominator.(M))
Mt = convert.(Int, isone(s) ? M' : M' * s)
coeffs = ModelingToolkit.nullspace(Mt)
for c in eachcol(coeffs)
c .= c gcd(c)
end
permutedims(prod(vals .^ coeffs, dims = 1))
vec(prod(vals .^ coeffs, dims = 1))

Check warning on line 300 in src/systems/validation.jl

View check run for this annotation

Codecov / codecov/patch

src/systems/validation.jl#L294-L300

Added lines #L294 - L300 were not covered by tests
end
19 changes: 19 additions & 0 deletions test/units.jl
Original file line number Diff line number Diff line change
Expand Up @@ -197,3 +197,22 @@ maj2 = MassActionJump(γ, [I => 1], [I => -1, R => 1])
maj1 = MassActionJump(2.0, [0 => 1], [S => 1])
maj2 = MassActionJump(γ, [S => 1], [S => -1])
@named js4 = JumpSystem([maj1, maj2], t, [S], [β, γ])

using DynamicQuantities: @us_str
vals1 = @variables begin
ρ, [unit = us"g/m^3"]
μ, [unit = us"g/m/s"]
u, [unit = us"m/s"]
ℓ, [unit = us"m"]
end

vals2 = @variables begin
ℓ, [unit = us"m"]
g, [unit = 9.8us"m/s^2"]
m, [unit = us"g"]
T, [unit = us"s"]
v, [unit = us"m/s"]
θ
end
@test isequal(ModelingToolkit.buckingham_pi(vals1), [μ / (u * ρ * ℓ)])
@test isequal(ModelingToolkit.buckingham_pi(vals2), [g * (T^2) / ℓ, v^2 / (g * ℓ), θ])

0 comments on commit 782ec36

Please sign in to comment.