2

In an application I'm making, I need some physics calculations (power, forces, torques etc.). I want to use JScience, because it seems really useful when it comes to keeping track of units. For instance, to assign a velocity I would do this:

Amount<Velocity> vel = Amount.valueOf(100, SI.METERS_PER_SECOND);

My problem is that I want to assign some angular velocity in a class, but I can't wrap my head around how I am supposed to do this. Say I want to specify an angular velocity of 100 rad/s.

Amount<AngularVelocity> angVel = Amount.valueOf(100, {SOME UNIT});

I can't find any unit in JScience to use for angular velocity. The documentation says "The system unit for this quantity is "rad/s" (radian per second).", but there is no such thing as a an "SI.RAD_PER_SECOND" or any enum value like that I think...

So the question is: What do I write as {SOME UNIT} in the latter code sample?

Cheers from Birger

---EDIT--

Got suggestions to use "SI.RADIAN.divide(SI.SECOND)" as unit, but then I have to assing the variable to a different type.

// This will not compile, due to type mismatch
Amount<AngularVelocity> angVel = Amount.valueOf(100, SI.RADIAN.divide(SI.SECOND));

// This will compile, but I want to assign the amount as a AngularVelocity ...
Amount<?> angVel = Amount.valueOf(100, SI.RADIAN.divide(SI.SECOND));

// Another, working alternative. But, surely, there is a way to assign an angular velocity using rad/s ... ?
Amount<Frequency> angVel = Amount.valueOf(100 * Math.PI, SI.HERTZ);
birgersp
  • 3,909
  • 8
  • 39
  • 79
  • `SI.RADIAN.divide (SI.SECOND)` maybe? –  Oct 14 '15 at 11:49
  • 2
    http://jscience.org/api/javax/measure/unit/Unit.html#getStandardUnit() suggests `RADIAN.divide(SECOND)` – Andy Turner Oct 14 '15 at 11:49
  • Amount angVel = Amount.valueOf(100, SI.RADIAN.divide(SI.SECOND)); \\ gives type mismatch – birgersp Oct 14 '15 at 11:59
  • 3
    The unit is 1/s, which is more commonly known as Hz. One could argue that as inappropriate since Hertz usually is understood as events/second, but from a purely SI unit standpoint that does not matter. – Lutz Lehmann Oct 14 '15 at 12:06
  • Yes, hertz seems to be an option for me. But then I have to store the angular velocity as an Amount, I think ... I could do that but I guess there is a way to store it as Amount. – birgersp Oct 14 '15 at 12:14
  • About radian, radian is the norm (instead of degree needing `*2pi/360`) hence Hz is fine. – Joop Eggen Oct 14 '15 at 13:57

1 Answers1

2

I'm no expert with this particular library, but some quick tests in eclipse led me to this :

Amount<AngularVelocity> angVel = Amount.valueOf(100, AngularVelocity.UNIT);

The problem with that is that if AngularVelocity.UNIT changes, your quntity will change too. But that's unlikely I'd say.

Otherwise, giving a less beautiful line of code :

Amount<AngularVelocity> angVel = Amount.valueOf(100, SI.RADIAN.divide(SI.SECOND).asType(AngularVelocity.class));

This leads me to a good hint when programming in Java: Use control-space. A lot. I never used this library and I was still able to find this with just a quick check in the doc for the AngularVelocity.UNIT version. The other is just control-space after your own solution.

Kaidjin
  • 1,433
  • 1
  • 12
  • 18
  • Amount angVel = Amount.valueOf(100, SI.RADIAN.divide(SI.SECOND).asType(AngularVelocity.class)); // This is what I was looking for. Failed to see the "asType" method. Thanks – birgersp Oct 14 '15 at 14:15
  • Also thanks for the control-space tip. I got blind looking for a "SI.RAD_PER_SECOND". I'm a little puzzled that they included the "SI.METER_PER_SECOND". Shouldn't it be the same principle for angular velocity as it is for a velocity? – birgersp Oct 14 '15 at 14:23
  • 1
    I agree, it isn't very consistent. Maybe they only propose combinations of fundamental SI units ? Actually it seems than meter per seconds and meter per square seconds are the only combinations that are present (except all of those that have a name). – Kaidjin Oct 14 '15 at 15:08