I discovered JScience a couple months ago and it has been a huge help for my project, though I'm struggling with one thing.
I'm trying to create a PressureHead
(aka water column) unit that can be converted directly with Length
and indirectly with Pressure
given a VolumetricDensity
value.
To find pressure: Density × Gravity × Head = Pressure
Here is an example conversion from Wikipedia:
1 cmH2O (4°C) = 999.9720 kg/m3 × 9.80665 m/s2 × 1 cm = 98.063754138 Pa
1 cmH2O can be directly converted to 1 cm.
Let's say I know the pressure in Pa and want to find the pressure head in mH2O, which is the conversion I would be doing most often in my project. I'll also need to know the density of the fluid. The pressure and density are variable inputs. Gravity must also be known for the formula, but for my purposes it can be fixed to standard gravity.
To find pressure head: Pressure / (Density × Gravity) = Head
For sake of simplicity, I've just repurposed the values from the above example, multiplying pressure by 100 to get 1 mH2O instead of 1 cmH2O.
9806.3754138 Pa / (999.9720 kg/m3 × 9.80665 m/s2) = 1 mH2O
It looks like JScience may be flexible enough to allow such a unit, but I haven't seen any examples to help me create it. Worst case, I'll probably just settle for converting between them with util methods.
Edit
Some examples of the ideal usage I would like to see:
// Pressure to PressureHead
Amount<Pressure> pressure = Amount.valueOf(9806.3754138, PASCAL);
Amount<VolumetricDensity> density = Amount.valueOf(999.9720, KG_M3);
Amount<PressureHead> head = pressure.to(M_H2O, density);
System.out.println(pressure.doubleValue(M_H2O, density)); // 1.0
// PressureHead <-> Length
Amount<Length> length = head.to(METER);
System.out.println(head.doubleValue(METER)); // 1.0
head = length.to(M_H2O);
System.out.println(length.doubleValue(M_H2O)); // 1.0
// PressureHead to Pressure
pressure = head.to(PASCAL, density);
System.out.println(head.doubleValue(PASCAL, density)); // 9806.3754138
Converting between PressureHead
units is easy. I can define additional units like so:
Unit<PressureHead> FT_H2O = M_H2O.transform(FOOT.getConverterTo(METER));
For the ideal usage above, I would need to subclass Amount
and overload to()
and doubleValue()
. I suspect if there's a more proper way of doing the conversions (albeit not as pretty usage), it involves subclassing UnitConverter
and/or one of the DerivedUnit
-based classes.
Part of me wants to give up and just go the quick and easy (and ugly) route of util methods so I can move on to more important things, the other part of me wants to find a solution that makes me love JScience even more.