### 11.2 A short introduction to EJS (Easy Java Simulation)

EJS

You reach the up-to-date description of the EJS program via the homepage given on the right. Here we will give a very short overview as suggestion to have a close look at this program. When calling the EJS program on the lower boundary of the screen the EJS console as shown in the following figure appears.

In the first line of the main window one enters the directory where the Java-jre(java runtime environemt) is located, if the program does not find it by itself. In the second line an arbitrary directory can be defined as workspace for EJS; the program then automatically creates two directories in this directory:

• Source: for *.xlm files
• Export: for compressed *.jar files

The program stores new or changed files automatically in these directories, unless other paths for saving are specified in the individual case. The two directories can contain a hierarchy of further directories. Files that have been automatically copied can later be copied of moved to other places.

With Launch Easy Java Simulation the editing window in Fig.11.2 is created. The console can be configured in such a way, that this step happens automatically whenever it is called.

The Figure above shows it visual interface. the main menu in the top line contains three sections, each of which can consist of several pages.

• Description as text
• Model (Code)
• View (optical surface for the creation of the visualization tree)

For many simulations only a few pages are active. Simple function plotters for example do not require any special code and can be realized using view alone.

In the following line of the menu model in Fig.11.2 the sub-menu Variables is highlighted. The page Hilfsgroessen for the simulaion Doppelpendel Antrieb is shown.

The individual pages of Model have the following meaning:

Variables:

Global variables, that do not appear only locally in individual code methods. The are designated as decimal number (double), integers (int), symbolic text (string), logical variable (boolean) and are also classified according to their dimensions ( for example number of computation loops with indices i,j,j:[i] or [i][j][k] …). It is important to note, that the decimal point appears as point, as is the standard in the U.S. and not as a comma as in German.

Initialization:

Here is entered, what applies when starting, for example special values for variables, equations involving the variables of calls to methods listed on other pages, that have to take place at start. This also includes logical equations, that select from different possibilities. Help is provided via the context menu, that can be called with the right mouse button. The following example puts the two initial velocities $Math content$ of the pendulum bobs and the time $Math content$ at the start of the simulation to zero.

$Math content$

Evolution:

Controls the succession of events, for example for an animation. It is particularly important, that differential equations can be entered here, that are then automatically solved via a choice of different methods . Typical example:

$Math content$

Fixed Relations:

Here relationships between the variables can be entered, that are always valid and provide input to the calculation. The following example from the simulation connects variables with trigonometric functions of other variables. (Please note, that the function name must start with Math. in Java code).

x1=L1*Math.sin(a1);

y1=-L1*Math.cos(a1);

x2=L2*Math.sin(a2);

y2=-L2*Math.cos(a2);

Custom:

Here special methods are formulated in Java code, that are called for example from the initialization page or by the control elements. The following example is used by button of the visualization. It deletes all lines and resets the variables to their initial values.

public void clear(){_resetView();_initialize();}

Here the methods _resetView() and _initialize() can be selected from a large number of prepared subroutines and one does not have to program these.

We now describe the function of the icons, that are on the right hand side. The shorthand *. stands , as usual for files, for an arbitrary filename before the file type like xml or jar.

• On line 2, on the right: information about author and file
• line 3 from the top: Open new file
• line 4 from the top : Opening of *.xml-files
• line 5 from the top : ( as well as Opening with screen) Leads to the home page of EJS and a current library of EJS simulations.
• line 6 from the top Saving at the original location and under the same name, but as *.xml file, which can be opened from the console. *.xml files cannot be activated by themselves, but very compact.
• line 7 from the top: Saving the file at another location or under a different name as *.xml file.
• line 8 from the top: Searching help. After entering the keyword it is shown , where it appears in the file.
• line 9 from the top (green triangle): Creates the active simulation or gives an error message with hints.
• line 10 from the top: compressing the EJS file as *.jar file. Such files can be called as stand-alone application and contain all required codes except for JAVA, which has to be installed on the computer. Alternatively html-pages or applets can be created.
• line 11 from the top: Opens general editing options, which are not required for creating files.
• line 12 from the top : Calling the internet help page of the EJS program.

View

On the right there are three menus below each other with a few pages each. They contain many icons, that can be put together with drag and drop on the visualization tree.

The top menu, which is called Interface, includes Containers as superior parents of the Java hierarchy, and pre-assembled control elements as children to be contained inside of it.

The second menu 2D Drawables contains icons, that can be inserted for two-dimensional visualizations. the third 3D Drawables icons for three-dimensional visualizations. In addition to icons, that symbolize a single element, there are some, that represent whole families of elements, for example arrows, points or curves.

Every icon used on the tree shows after double clicking a large menu for formatting and for connecting to variables and methods. Fig.11.4 shows this for the relatively simple icon P1, that represents the blob of the main pendulum in the double pendulum simulation. The coordinates $Math content$ and $Math content$ are connected to the element Pos. In the element Size the same dimensions are fixed in both directions. Draggable True means, that the point can be pulled with the mouse, which automatically gives a new value to the variable. On Drag Pendel() calls the proceudre Pendel() when the mouse is pulled. (Deleting of previous traces and restarting the calculations). Many open positions can be used for further formatting.

The exact definition of individual elements appears, if one holds the mouse pointer on the designation on the left. If one points at the label Visible, the message The visibility of the element (boolean) appears. If one double clicks on the first icon at the right of an element either the existing choices ( for visible true, false are shown, or assistance for entering information is given. The second icon contains a list of permitted quantities or methods, one of which can be selected with a click.

The next figure 11.5 finally shows the appearance of the main window in the active simulation. You can certainly identify elements of the visualization tree, especially on the left hand side (L1 and L2 are the pendulum bars, P1 and P2 are the pendulum bobs. Pfad_P2, the red orbit of the secondary pendulum, and so on ).

current EJS

You actually do not need to know more to start with these simulations, that have been developed for this book ,and to use and change them. The same applies to other simulations that have been created with EJS. The EJS console require for this purpose is contained in the data carrier for this book. You can download a possibly newer version from the EJS homepage.

Start with something simple, for example with the calculation of the geometric series. Pressing the Ctrl-key and clicking on the following interactive picture opens the working EJS simulation as independent *.jar file.

Now click with the right mouse button on the simulation and choose OPEN EJS Model in the context menu, that appears. A menu will pop up , that shows how a *.xml file is extracted and stored. The standard storage location is the directory source in the EJS workspace; you may also choose a different one.

After confirming your input, the EJS console appears with the editing window as in Fig.11.3. The previously active simulation vanishes into the background, and passive EJS windows appears. You may see the configuration of the elements, but the simulation cannot run in these windows.

Now save under another name ( the *.xml file is saved). Pressing the icon in the shape of a green triangle creates an active simulation under a new name.

Now change individual elements in the pages of the editing console and then save the *.xml file under a new name. If you close the old version and click on the green triangle, your version becomes active, or you get an error message with solution hints, if you have build a bug into the program. Initially only go into View and change the colours or the thickness of lines. This way nothing can really break.

With some more insight you can also change the simple formulas in Custom and Fixed Relations and thus calculate another series then the geometric one. Frequent saving under different names allows the finding of working versions when encountering errors.

The help function, that can be called with the link on the margin or directly from the console contains details on all individual elements. At this point you also find extensive documentation for example for a introductory course. help-doc