ruby-units icon indicating copy to clipboard operation
ruby-units copied to clipboard

Support for fractional dimensions

Open andreasgebhard7 opened this issue 3 years ago • 1 comments

In fracture mechanics, the critical stress intensity factor is given in MPa * m^0.5 wikipedia. However, I cannot make RubyUnits to create this unit or any other unit with a non-integer dimension. RubyUnits::Unit#power seems to support only integer dimensions. I tried to work around this with U('1 m').root(2), however, this yields an illegal root error. Is there any support for fractional powers that I am missing?

The use case is an application in which the user can enter physical quantities. The app's classes provide two separate fields for each quantity: a numeric field and a string field. From this, the application generates a compound unit using RubyUnits::Unit("#{number} #{unit_string}"). Using this, the application is checking whether user input is within a given "expected range". Since the user can potentially enter quantities in different units, I cannot simply compare the numbers.

Example:

expected_range = Range.new U("0.01 m"), U("1 m")
# user_input
number = 95
unit_string = 'mm'
user_length = U("#{number} #{unit_string}") # 95 mm
expected_range.include? user_length # true

I need to do this with MPa * m^0.5 too, so that if a user enters his quantities in, lets say Pa * cm^0.5, I still can use the above example. When I had to add support for gram-force and gram-force-cm, I was able to do this:

RubyUnits::Unit.define('gram-force') do |gf|
 gf.definition  = RubyUnits::Unit.new('1 g') * RubyUnits::Unit.new('1 gee')
 gf.aliases     = %w[gf gram-force]
end

RubyUnits::Unit.define('gram-force-centimeter') do |gf_cm|
 gf_cm.definition  = RubyUnits::Unit.new('1 gf') * RubyUnits::Unit.new('1 cm')
 gf_cm.aliases     = %w[gf-cm g-cm]
end

However, I am completely at a loss with this:

RubyUnits::Unit.define('MPa*m^0.5') do |u|
  #  u.definition  = ?
end

or even this:

RubyUnits::Unit.define('m^0.5') do |u|
  #  u.definition  = ?
end

By the way, your great work not only enables a great app on my side, it has also found academic attention: https://www.sciencedirect.com/science/article/abs/pii/S0950584919301156

Any support would be greatly appreciated!

andreasgebhard7 avatar Dec 03 '21 08:12 andreasgebhard7

Handling arbitrary powers on dimensions is going to require some significant modifications to the internals of ruby-units and seems to only serve a very small subset of the use cases.

Maybe you can re-formulate your equations so that the quantity in question is the square of the one you need so that the powers are still integers?

olbrich avatar May 29 '22 12:05 olbrich