Science SPORE Prize
November 2011

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

## Modeling Tips & Help post and replies

Iterating with ODE solving - Oct 18, 2012 at 5:28PM
Ioannis Vourkas
4 Posts

Hi again,

I am writing to ask if you have any recommendation regarding, how one might manage to use a specific ODE for multiple types of systems whose functionality is described in part by the same ODE.

i.e. In case of having a mesh grid of nodes, where at each node the same ODE applies, though the current values of the state variables are different. Thus, instead of defining as many ODEs as the nodes, should I iterate and step the solver for each node or is there any other possible way to do it?

A student of mine recently defined as state variable a cell of a table, e.g. as follows: d(w[i]) / dt
and thinks that the solver tries to iterate within all cells of table w[] ...

Is this some kind of a bug or is it really what it does in case of having such a state variable declaration?

### Replies to Iterating with ODE solving

Re: Iterating with ODE solving - Oct 18 2012 5:41PM
Francisco Esquembre
222 Posts

Ioannis,

The ODE editor of EJS can very well handle differential equations where one or more of the states are on-dimensional double arrays.

Hence, what your student did is perfectly correct.

In your case, define state arrays where each entry is the state of a given node in th grid, and define the corrresponding ODE. It will work.

As an example, see how array states are used to solve the motion of several balls in a box in the model at http://www.compadre.org/osp/items/detail.cfm?ID=8307

Paco

Re: Re: Iterating with ODE solving - Oct 18 2012 5:53PM
Wolfgang
113 Posts

The chain of coupled oscillators is another model that solved an array of ODEs.

Page of Code Vs Page of ODEs in EJS Evolution - Jun 8, 2012 at 8:34AM
Ioannis Vourkas
4 Posts

Hi! I 've been using EJS ever since 2009 when I was in Spain, and I'd like to encourage you to keep up doing such a briliant work. Nevertheless, this is the first time I use this forum...

I 'd like to make a question regarding the Evolution page in EJS. First of all, is there any problem if someone uses both pages of code and ODEs in the same simulation? In most of the cases I use a page sequence like...

Page of Code--Page of ODEs--Page of Code--Page of ODEs ... etc

so I organize my preliminary code before specific ODEs whenever it is necessary. Is this OK? (I guess I could use the preliminary code section of ODE pages instead...).

Moreover, let's say that I want to simulate a 2D grid of nxm devices and need a DE to describe the response of each device, in every time step. Is there any way to do this without having to declare nxm different DEs? Is there any kind of iteration I could use instead? (Maybe PJ could be helpful?) The devices are identical, so the DEs are all the same too, except for some different coefficient values that I pass as parameters to the method I use in the ODEs page.

Thank you very much in advance.

Ioannis

### Replies to Page of Code Vs Page of ODEs in EJS Evolution

Re: Page of Code Vs Page of ODEs in EJS Evolution - Jun 08 2012 12:19PM
Francisco Esquembre
222 Posts

Ioannis,

You can (and it certainly makes sense) use pages of code and ODEs in the same simulation. However, there are a couple of precautions that you need to observe:
- do not use two pages of ODEs with the same independent variable, or you will have this variable increased twice at each evolution step, which I doubt you want to.
- do not use the pages of code as preliminary code but rather a 'preparatory code'. The difference is that preliminary code can be executed several times with intermediate values of the variables within the process of solving the equations (actually, this is the case for all provided solvers which -except Euler-  evaluate the rate more than once to step the solver). On the contrary, pages of code previous to the ODE page will be executed only once with the values of the variables before entering the solving process.

With respect to the second question, if I understood it correctly, I see two possibilities:
- use a one-dimensional array of states, one for each of the devices (recommended)
- declare one ODE and do not let the evolution run, but manage the ODE explicitly by using object-oriented programming. This is advanced, but you can see an example of how to do this in the Lorenz attractor example at http://www.compadre.org/osp/items/detail.cfm?ID=8986
Notice how the author runs the ODEs in the drawAttractor() custom method to display the attractor before actually playing the evolution.

You are welcomed. And thanks for your kind words on EJS.

Paco

Re: Re: Page of Code Vs Page of ODEs in EJS Evolution - Jun 09 2012 3:59AM
Ioannis Vourkas
4 Posts

Hi again and thanks for the usefull tips, though I have a few more doubts...

In the EJS model section, all declared pages run sequentially depending on the order they are found,? That is...if I have some pages declared in the following manner..

Page of Code 1 -- Page of ODEs 1 -- Page of Code 2 -- Page of ODEs 2 -- Page of Code 3 etc...

what is exactly the execution order?

Furthermore, regarding my second question in the previous post, I checked out the recomended code example...where only one page of ODEs is declared. So the author uses the _step() to make a preparatory simulation and then resets the variables of interest...but what if I have both pages of code and pages of ODEs? The _step() executes everything that is found inside the EJS Model, right? Nevertheless, I seem to understand that I could appropriately update the independent variable and coefficients of an ODE and use the _step() to control simulation execution as many times as I like, without having to declare a bunch of ODEs describing all of my devices...

Re: Re: Re: Page of Code Vs Page of ODEs in EJS Evolution - Jun 09 2012 7:17AM
Francisco Esquembre
222 Posts

Ioannis,

The order is always from left to right. (Except for Custom pages, of course, whose methods must be invoked explicitly.)

If you want to only step the solver, then you must enter secret/advanced/adventurous land :-).
Use the method:
_getODE("Lorenz ODE").step();
where you must replace "Lorenz ODE" by the name of your page of ODEs that you want to step.
The _getODE(String name) gives you direct access to the ODE. This is an object of the class EJSODE.
You can find a complete JavaDoc set of pages for classes used internally by EJS at:

Finally, yes. I assume that used carefully you can tweak EJS to do what you want.

Paco

automatically stop a simulation - Feb 2, 2012 at 11:10PM
mos
1 Posts

Hi,

Is there any way to define a final time for automatically stop a simulation when the simulation time reaches it?

### Replies to automatically stop a simulation

Re: automatically stop a simulation - Feb 03 2012 6:45AM
Francisco Esquembre
222 Posts

Hi Mos,

If the model is based on an differential equation, then add an event with Zero condition:
return t - yourLimit;

where t is the time variable and yourLimit the maximum you want.

As action, enter:
_pause();

This will stop (_pause()) your simulation exactly at that time.

Notice that, if the event is of the "State event" type, you should not continue playing it. If you want to allow the user to continue playing, set the event type to be "Zero crossing".

If the model is not based on an ODE, then you need to add to your Evolution page the code
if (t>=yourLimit) _pause();

This will pause your simulation whenever t reaches or exceeds yourLimit. (numerical round errors permit).

Cheers,

Paco

Input from user through textbox on frame - Jan 26, 2012 at 3:12PM
SUNIL KUMAR
1 Posts

i want to get input from the user through text box and then again restart the simulation with updated valuse

can anyone help me in doing this

### Replies to Input from user through textbox on frame

Re: Input from user through textbox on frame - Mar 08 2012 3:34PM
Francisco Esquembre
222 Posts

You have to call _initialize() from the text box Action property.

_initialize() will make the simulation execute whatever you entered in the Initialization pages. Place there the code you need to 'restart' your simulation. This typically includes resetting time to 0, but it may necessary to add other initialization sentences that only you know, since they depend on your model.

You should not call _reset(), because this makes the simulation go through the Variables pages and this most likely overwrites whatever change th user input through your text box.

Paco

Fixed Function Plotter - Nov 14, 2011 at 3:47PM
Ronald Modesitt
2 Posts

I am following the Fixed Function Plotter example in chapter three of Modeling Science: From Free Fall to Chaos.

My graphs appear correctly, however, the trace function connects the end points of the graph (straight line) for both sin(x)/x and sin(x). What parameter do I change to eliminate this effect?

Many thanks for EJS. What a fun environment to re-learn physics.

Ron

### Replies to Fixed Function Plotter

Re: Fixed Function Plotter - Nov 14 2011 4:13PM
Francisco Esquembre
222 Posts

Ron,

When you set arrays as inputs to a Trail, each time the view is updated, a whole new set of points (x[i],y[i]) is added to the trail, connecting them to whatever previous points the trail had.

Because at start time the view is updated a couple of times (to make sure it has the very last state after start-up), you have not 500, but at least 1000 points displayed. The point 500 (the end of the first complete plot) is connected to the point 501 (beginning of the second copy of the plot) and this causes the ugly line you complain about.

You want to set the "Clear at input" property of the trail to "true". This clears any existing data before adding  the (x[i],y[i]) pairs to the trail. This way, you will have exactly 500 points displayed each time.

That should fix the problem.

Paco

Re: Re: Fixed Function Plotter - Nov 14 2011 5:03PM
Ronald Modesitt
2 Posts

Paco,
Thank you for the help. The solution is obvious now that I re-read the help file.

Ron

Parallelization - May 23, 2011 at 11:42AM
Zach Wilson
1 Posts

Does anyone know if there is a way to utilize parallel computing or if there is going to be a version dedicated to this?

### Replies to Parallelization

Re: Parallelization - May 24 2011 3:36AM
Francisco Esquembre
222 Posts

The Parallel Java library (see http://www.cs.rit.edu/~ark/pj.shtml) is a very nice 100% Java library that can be used for this. The book from the author is also a nice text for Java programmers.

I am so fond of this, that I plan to add Model Elements to EJS that will make it easy for an EJS author to parallelize a model in a shared memory multiprocessor computer. (The library also supports small clusters.)

I also plan to use the examples from Kaminsky's book (see the Web page above) to create the corresponding EJS examples.

When will this be ready? I can't promise it, because the term hasn't finished yet in Spain. But I want to work on this before the summer and maybe finish it during August.

Paco

Post edited May 24, 2011 at 3:35 AM EST.

Re: Parallelization - Oct 20 2011 5:46PM
Francisco Esquembre
222 Posts

Ok. Here they are.
The latest EJS has brand new Model elements based on the Parallel Java library that allow you to create nice parallel algorithms. The source code examples contain a number of examples taken from the book "Building Parallel Programs" by Alan Kaminsky.
These are tools for multicore shared-memory computers. I have tried the examples with a 12 cores computer and the result is superb!
The examples are in users/murcia/fem/demos/ParallelElements

Post edited October 20, 2011 at 5:44 PM EST.

Mathematical Function Use in ODEs??? - Jun 22, 2010 at 2:35PM
Nolan Nicholas
1 Posts

Does anyone know if it is possible to use named functions (e.g. sine) in the ODE "evolution"?

[I want to simulate a "particle on a hoop" under the influence of a separate external force field.]

### Replies to Mathematical Function Use in ODEs???

Re: Mathematical Function Use in ODEs??? - Jul 03 2010 12:33PM
Vasudeva Rao Aravind
3 Posts

Yes. For example if you want to say that dx/dt is sin(w*t), your command should read

dx/dt=Math.sin(w*t)

Hope it helps.

Re: Mathematical Function Use in ODEs??? - Jul 23 2010 4:17PM
Wolfgang
113 Posts

The OSP parser within EJS can be used used to compute the ODE rate.  See how this is done in the Barrier Scattering model.