在 jscience 中使用转动惯量
Using moment of inertia in jscience
我正在制作一个简单的物理计算器中使用 jscience。我需要计算给定一些齿轮和旋转圆柱体的转动惯量。
我比较喜欢用jscience,但是jscience好像没有惯性矩的度量?还是惯性矩表示为其他东西?从 these formulas 我知道转动惯量可以用 kg*m^2 来描述。
查看jscience中的其他数量接口,我尝试模仿"Mass"接口并创建了我自己的数量接口,名称为"MomentOfInertia":
package jscience;
import javax.measure.quantity.Quantity;
import javax.measure.unit.Unit;
public interface MomentOfInertia extends Quantity {
public final static Unit<MomentOfInertia> UNIT =
SI.KILOGRAM.times(SI.SQUARE_METRE).asType(MomentOfInertia.class);
}
接下来我要定义惯性矩:
public static void main(String[] args) throws Exception {
Amount<MomentOfInertia> moi = Amount.valueOf(1000,
SI.KILOGRAM.times(SI.SQUARE_METRE).asType(MomentOfInertia.class));
System.out.println(moi);
}
但是,如果不抛出以下异常,这将 运行:
Exception in thread "main" java.lang.ExceptionInInitializerError
at sun.misc.Unsafe.ensureClassInitialized(Native Method)
at sun.reflect.UnsafeFieldAccessorFactory.newFieldAccessor(UnsafeFieldAccessorFactory.java:43)
at sun.reflect.ReflectionFactory.newFieldAccessor(ReflectionFactory.java:142)
at java.lang.reflect.Field.acquireFieldAccessor(Field.java:1088)
at java.lang.reflect.Field.getFieldAccessor(Field.java:1069)
at java.lang.reflect.Field.get(Field.java:393)
at javax.measure.unit.Unit.asType(Unit.java:170)
at test.Test.main(Test.java:8)
Caused by: java.lang.NullPointerException
at javax.measure.unit.Unit.asType(Unit.java:174)
at jscience.MomentOfInertia.<clinit>(MomentOfInertia.java:10)
... 8 more
TLDR:(如何)我可以在 jscience 中定义惯性矩?
我对JScience不熟悉,不过看看Torque
的定义方式:
public interface Torque extends Quantity {
public final static Unit<Torque> UNIT =
new ProductUnit<Torque>(SI.NEWTON.times(SI.METRE));
}
您在这里遇到的问题是循环初始化之一:您正在调用 asType
来获取要分配给 MomentOfInertia.UNIT
的值,但是 asType(MomentOfInertia.class)
需要 the value of MomentOfInertia.UNIT
,目前为空,因为它还没有被赋值。
因此,类似以下的方法可能有效:
public interface MomentOfInertia extends Quantity {
public final static Unit<MomentOfInertia> UNIT =
new ProductUnit<MomentOfInertia>(SI.KILOGRAM.times(SI.SQUARE_METRE));
}
我正在制作一个简单的物理计算器中使用 jscience。我需要计算给定一些齿轮和旋转圆柱体的转动惯量。
我比较喜欢用jscience,但是jscience好像没有惯性矩的度量?还是惯性矩表示为其他东西?从 these formulas 我知道转动惯量可以用 kg*m^2 来描述。
查看jscience中的其他数量接口,我尝试模仿"Mass"接口并创建了我自己的数量接口,名称为"MomentOfInertia":
package jscience;
import javax.measure.quantity.Quantity;
import javax.measure.unit.Unit;
public interface MomentOfInertia extends Quantity {
public final static Unit<MomentOfInertia> UNIT =
SI.KILOGRAM.times(SI.SQUARE_METRE).asType(MomentOfInertia.class);
}
接下来我要定义惯性矩:
public static void main(String[] args) throws Exception {
Amount<MomentOfInertia> moi = Amount.valueOf(1000,
SI.KILOGRAM.times(SI.SQUARE_METRE).asType(MomentOfInertia.class));
System.out.println(moi);
}
但是,如果不抛出以下异常,这将 运行:
Exception in thread "main" java.lang.ExceptionInInitializerError
at sun.misc.Unsafe.ensureClassInitialized(Native Method)
at sun.reflect.UnsafeFieldAccessorFactory.newFieldAccessor(UnsafeFieldAccessorFactory.java:43)
at sun.reflect.ReflectionFactory.newFieldAccessor(ReflectionFactory.java:142)
at java.lang.reflect.Field.acquireFieldAccessor(Field.java:1088)
at java.lang.reflect.Field.getFieldAccessor(Field.java:1069)
at java.lang.reflect.Field.get(Field.java:393)
at javax.measure.unit.Unit.asType(Unit.java:170)
at test.Test.main(Test.java:8)
Caused by: java.lang.NullPointerException
at javax.measure.unit.Unit.asType(Unit.java:174)
at jscience.MomentOfInertia.<clinit>(MomentOfInertia.java:10)
... 8 more
TLDR:(如何)我可以在 jscience 中定义惯性矩?
我对JScience不熟悉,不过看看Torque
的定义方式:
public interface Torque extends Quantity {
public final static Unit<Torque> UNIT =
new ProductUnit<Torque>(SI.NEWTON.times(SI.METRE));
}
您在这里遇到的问题是循环初始化之一:您正在调用 asType
来获取要分配给 MomentOfInertia.UNIT
的值,但是 asType(MomentOfInertia.class)
需要 the value of MomentOfInertia.UNIT
,目前为空,因为它还没有被赋值。
因此,类似以下的方法可能有效:
public interface MomentOfInertia extends Quantity {
public final static Unit<MomentOfInertia> UNIT =
new ProductUnit<MomentOfInertia>(SI.KILOGRAM.times(SI.SQUARE_METRE));
}