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.

Using the 'Custom' panel to centralize code editing post and replies

Return to the Using the 'Custom' panel to centralize code editing thread
Login to post to this thread

Using the 'Custom' panel to centralize code editing
Cleon Teunissen
27 Posts

To describe my question, let me explain how I work. View elements such as buttons, checkboxes and numberfields have among their properties that you can define an action. I write that action handling code in a method in the Custom panel. In the view element properties box, in the field 'Action' I only write the call to that method. That way, when I want to edit the code for that action I don't have to dig down into the view element tree, all the code is directly accessible in the Custom panel.

I now have two simulations that use the 'evolution event' capability of EJS. Can I do the same thing with the code in the 'Events for ODE evolution' box?

The reason I ask is that in the upper section, with the code for the zero condition, the code returns a value.

Of course, for one thing the method in the Custom panel must then not be declared as a 'void' method; void methods are methods that do not return a value. Java being a type-aware language I assume that when the returned value is type double you declare 'double' instead of 'void'.

Yeah, I could just try that and see if it works, but I decided to ask anyway.

Replies to Using the 'Custom' panel to centralize code editing

Re: Using the 'Custom' panel to centralize code editing -
Francisco Esquembre
228 Posts

You can write:

return myCustomFunction(t,x,vx);

But you must pass all variables used by your function. The reason is that the event receives intermediate values of the variables in order to compute the zero function at many points inside the step. If you pass to the function all the affected variable, it should work.

Your function should return a double value.


Post edited March 5, 2017 at 2:51 AM EST.

Re: Using the 'Custom' panel to centralize code editing -
Cleon Teunissen
27 Posts

Ah, I see, if the box for the zero condition code contains only a single line of code it must be the return statement. The content of that return statement can itself be a call to a method (with the variables used being passed to that method.)

What I would have tried was just calling the method (same as I do with the Action fields), and that would have failed.  


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