APS Excellence in Physics Education Award
November 2019

Education Prize Logo
Science SPORE Prize
November 2011

NSF Logo
The Open Source Physics Project is supported by NSF DUE-0442581.

Cannot integrate OSP AbstractCalculation, AbstractSimulation to python jpype

Return to the OSP Programming forum
Login to post to this thread

OSP Display3DFrame and 3D elements work from Python jpype but not AbstractSimulation
ALFONSO REYES
9 Posts

Hello,
I am having some difficulties calling OSP Java libraries from Python using jpype. Specifically, I am not able to properly call AbstractSimulation or AbstractCalculation which are the core and butter of java physics simulation.

Could anybody help me?

This simple example, for instance, works for me. But have tried for hours to call AbstractSimulation or AbstractCalculation without success.

Thanks,
Alfonso


# Display3DFrameApp
import jpype

def init_jvm(jvmpath=None):
    # indicate the java JAR file
    jar_path = 'osp.jar'
    if jpype.isJVMStarted():
        return

    jpype.startJVM(jpype.getDefaultJVMPath(), "-Djava.class.path=%s" % jar_path)
    


init_jvm()
jpype.java.lang.System.out.println("Hello World")

# get the whole OSP package
javaPackage = jpype.JPackage("org").opensourcephysics
Display3DFrame = javaPackage.frames.Display3DFrame
ElementCircle = javaPackage.display3d.simple3d.ElementCircle

import math
import numpy as np
frame = Display3DFrame("Random Circles")

for i in range(0,25):
    ball = ElementCircle()
    ball.setSizeXYZ(0.1, 0.1, 0.1)
    ball.setXYZ(-1.0+2*np.random.random(), -1.0+2*np.random.random(), -1.0+2*np.random.random())
    frame.addElement(ball)
    
frame.setVisible(True)


Python jpype can access simple OSP libraries but ...
ALFONSO REYES
9 Posts

I have been succesful opening and executing OSP Java libraries but still cannot get AbstractCalculation and AbstractSimulation to work.

Has anyone used Python jpype before?
Any ides of how I should call AbstractCalculation and AbstractSimulation?

This simple example works for me:

import jpype

def init_jvm(jvmpath=None):
    if jpype.isJVMStarted():
        return
    jpype.startJVM(jpype.getDefaultJVMPath())
    
init_jvm()
jpype.java.lang.System.out.println("Hello World")

JFrame = jpype.JClass("javax.swing.JFrame")
PlotFrame = jpype.JClass("org.opensourcephysics.frames.PlotFrame")
frame = PlotFrame("position", "amplitude", "First Plot")
frame.setSize(400, 400)

import numpy as np
import math
for x in np.arange(-10, 10, 0.1):
    print x,
    frame.append(0, x, math.sin(x))
    
frame.setVisible(True)

Thanks,
Alfonso R. Reyes


OSP Projects:
Open Source Physics - EJS Modeling
Tracker
Physlet Physics
Physlet Quantum Physics
STP Book