Modeling Physics with Easy Java Simulations Wolfgang Christian,
Davidson College, Davidson, NC
Francisco Esquembre,
Universidad de Murcia, Murcia, Spain
M
odeling has been shown to correct weaknesses of traditional instruction by engaging students in the design of physical models to describe, explain, and predict phenomena.1 Although the modeling method can be used without computers, the use of computers allows students to study problems that are difficult and time consuming, to visualize their results, and to communicate their results with others. This combination of computer modeling with theory and experiment can achieve insight and understanding that cannot be achieved with only one approach. This paper describes the Easy Java Simulations (Ejs) modeling and authoring tool and shows how it can be used to teach mechanics concepts with computer modeling. A model is a surrogate object or a conceptual representation of a physical system and/or its properties, and modeling is the process whereby we construct this representation. Computer modeling requires (1) a description and an analysis of the problem, (2) the identification of the variables and the algorithms, (3) the implementation on a specific hardware-software platform, (4) the execution of the implementation and analysis of the results, (5) refinement and generalization, and (6) the presentation of results. A computer simulation is an implementation of a model that allows us to test the model with different initial conditions with the objective of learning about the model’s behavior. The applicability of the results of the simulation to those of the real (physical) system depends on how well the model describes reality. The process of devising more general and more accurate models is The Physics Teacher ◆ Vol. 45, November 2007
what science is about.2 Wells, Hestenes, and Swackhamer point out that introductory mechanics courses are organized around a small number of basic models that are adapted to various contexts. Constant acceleration, simple harmonic motion, Newton’s universal law of gravitation, and energy/momentum conservation form the core of this course. Ready-to-use models and associated curricular material described for these topics are available on the Open Source Physics (OSP) website in the ejs_tpt_modeling.jar archive.3 The OSP Launcher shown in Fig. 1 is a Java program that contains mechanics models and documentation in a single archive known as a Launcher package.4 Clicking on an item in the table of contents
Fig. 1. An Ejs Launcher package contains compiled readyto-use models. Clicking on a Launch Node runs the model. Right-clicking on a Launch Node passes the model to the Ejs authoring tool.
DOI: 10.1119/1.2798358
475
tree (left-hand side of the Launcher window) loads an html page that describes the model. Users execute (run) models in our package by double clicking on the “Launch Nodes” (green arrows) in the tree. Users without vendor-installed Java must, of course, first download and install Java in order to run our models.5 Running a Java program on a modern operating system is straightforward because a typical (default) Java installation recognizes a Java archive (jar) file as a compiled Java program. Users need only double-click on the jar file to run our models. Although the models in our mechanics package are compiled and ready-to-use Java applications, an important pedagogic feature is that our package is created in such a way that users can return to Ejs at any time to examine, modify, and adapt the models. The Launcher package contains a small Extensible Markup Language (XML) description of each model, and right-clicking on a Launch Node passes this XML document to the modeling tool, thereby allowing users to examine and modify the model. Ejs can repackage the modified model in a new jar file for redistribution or submission to an instructor as a completed exercise.
Ejs Modeling Easy Java Simulations is a Java program that enables both programmers and novices to quickly and easily prototype, test, and distribute packages of Java simulations. It can be downloaded from the Ejs website and installed (unzipped) into a directory of their
choice.6 Executing the EjsConsole.jar file in the installation directory creates an empty Ejs workspace, as shown in Fig. 2. Easy Java Simulations is well-suited for education because it is simple to use and combines authoring with powerful modeling tools. Its dynamic and highly interactive user interface greatly reduces the amount of programming required to implement an idea. Even experienced programmers find Ejs useful, because it is faster and easier to: • Develop a prototype of an application in order to test an idea or algorithm. • Create user interfaces without programming. • Create models whose structure and algorithms nonprogrammers can inspect and understand. • Encourage students or colleagues (who may be new to Java) to create their own simulations. • Quickly prepare simulations to be distributed as applets or as stand-alone programs. • Create a package containing multiple programs and the associated curricular material. Easy Java Simulations simplifies the modeling process by breaking it into activities that are selected using radio buttons: (1) documentation, (2) modeling, and (3) interface design. The model’s html-based documentation is accessed by selecting “Description.” The physics is accessed by selecting “Model,” which provides access to the data and the methods (Java code) by which the model can change. A plot is a visual representation of data and is an example of a “View.” Because it is possible to have multiple views of the same data and because the model and its views are controlled by buttons and other on-screen components, Ejs provides a graphical drag-and-drop editor for the user interface, which eliminates much coding. The Ejs model-view-controller paradigm is well known in computer science and provides a solid foundation for software development.
Pendulum Model
Fig. 2. The Easy Java Simulations user interface (with annotations).
476
Screen shots are no substitute for a live demonstration, and we encourage readers to load, inspect, and run the Pendulum model described in this paper. Execute the EjsConsole.jar program in the directory The Physics Teacher ◆ Vol. 45, November 2007
Fig. 4. A table of variables that describes the state of a simple pendulum. Fig. 3. A description of the Pendulum model. Descriptions are html pages that can be created within or imported into Ejs.
where Ejs was installed to start Ejs. Then click on the file-folder icon on the Ejs taskbar to open a file-browser to load the Pendulum model. The Pendulum.xml file is located in the users/davidson/wochristian/ tpt_modeling subdirectory in the “Simulations” directory. Use the “Save As” icon on the Ejs taskbar to create a copy of this file in your own working directory. Narratives, such as the pendulum description shown in Fig. 3, are an essential component of modeling, and Ejs makes it straightforward for modelers to include html-based documentation. Right-clicking within the workpanel allows users to edit existing html pages and to create additional pages. Teachers can, for example, author a student exercise that uses our existing Pendulum model. Selecting “Model” reveals a series of subpanels specifying the different parts of the model: definition of variables, initialization, evolution of the system, constraints or relationships among variables, and custom methods. Each panel provides tools that facilitate the creation of these parts. For example, if we ignore friction, a simple pendulum obeys the differential equation
d 2q g = − sin (q ), 2 dt l where q corresponds to the pendulum’s angular displacement from the vertical, l is the pendulum’s length, g is the acceleration due to gravity, and t is the time. The Physics Teacher ◆ Vol. 45, November 2007
Corresponding program variables are defined in “Variables” tables as shown in Fig. 4. Because the Pendulum model uses 10 variables, we have chosen to create three tables. The table of dynamic variables contains values that evolve in time while the constants table contains parameters, such as g and L, which remain fixed. The x, y, vx, and vy auxiliary variables store Cartesian position and velocity vector components of the pendulum bob. These variables are computed from the dynamical (polar) variables by entering a small amount of Java code into the model’s “Constraints” page. x= L*Math.sin(theta); y = -L*Math.cos(theta); vx = omega*L*Math.cos(theta); vy = omega*L*Math.sin(theta);
The “Constraints” panel contains additional code, such as an energy computation, that is executed at the end of every evolution step or when the user drags an on-screen object. The most difficult aspect to remember when entering these code fragments is to use the multiplication character * and to place a semicolon at the end of each statement. Creating separate tables to group variables is an effective way to organize data in Ejs models. Although the Pendulum model uses only floating point (decimal) numbers, it is also possible to define integer and Boolean variables and arrays of other variables. (See the examples in the Launcher package for this article.) Newton’s second law can be converted to two firstorder equations by adding an angular velocity variable w to the model. Both angular position q and veloc477
Fig. 5. The Ejs equation editor showing the pendulum’s system of ODEs.
Fig. 6. The “Tree of Elements” for the simulation (left) shows the graphical elements (right) of the pendulum simulation. Double-clicking on an element in the tree shows a table of element properties.
ity w are now independent functions of time and have their own rate of change:
dq =w dt dw g = − sin(q ). dt l The process of defining velocity variables, such as w, to reduce the order of a differential equation (but increase the number of equations) can be extended to higher-order derivatives as needed. Because higherorder differential equations can be converted into a system of coupled first-order equations, we need only consider general methods of solving systems of first-
478
order equations. The “Evolution” page shown in Fig. 5 contains the differential equations for the pendulum using the model’s variables. Ejs will solve the given system of differential equations using the numerical algorithm specified in the field below the equation. The initial position q0 and initial velocity w0 are advanced by a time step Δt to obtain new values q1 and w1, respectively, and this process is repeated 10 times per second. Solving a system of first-order differential equations efficiently using numerical algorithms is not a trivial task, but Ejs removes many of the barriers. Pressing the run icon (green triangle) on the Ejs taskbar generates the necessary Java code so that students and instructors can focus their attention on the physical model. But the numerical method is not a “black box” either. Modelers can select both the algorithm and the algorithm’s time step, and advanced users can download and examine the algorithm’s Java code from the Open Source Physics website.3 Simple tests such as comparing solutions with different algorithms or with different time steps are usually all that are needed to determine the validity of the algorithm. Testing the validity of a solution is an important but often overlooked step in computational modeling. Selecting “View” in the Ejs workspace displays the structure of the simulation’s user interface, as shown in Fig. 6. The “Tree of Elements” shows that this model has a frame (window) with two buttons and a plotting panel. The plotting panel contains a line representing the pendulum arm and a circle representing the pendulum bob. There is a second frame containing another plotting panel and a trace. This trace will display the bob’s angle q with respect to time t. High-level data visualizations, such as the q plot in the pendulum model, are created in Ejs without programming by dragging objects from the “Elements” pallet into the “Tree of Elements.” Element properties are then associated with variables from the model (or expressions that use them) in order to create a true dynamic and interactive visualization of the phenomenon under study. If you right-click on the “trace” element you will obtain the properties table for that element. Observe that the angle and time variables in Fig. 4 are assigned to the trace’s ordinate and abscissa inputs on Fig. 7. The “Evolution” page (Fig. 5) conThe Physics Teacher ◆ Vol. 45, November 2007
trols the temporal evolution of the Pendulum model and the trace automatically collects q(t) as the model evolves. In order to position the bob on the screen, we associate the model’s x and y auxiliary variables with the pendulum bob’s (blue circle) position properties. Right-clicking on the bob and selecting the properties menu item displays the table shown in Fig. 8. The “Visibility and Interaction” section of the table specifies how the bob reacts to mouse actions. Pressing on the bob stops the animation using an Ejs-defined _pause method. (Clicking on an icon next to the text field displays a list of methods.) Dragging the bob computes new q and w values using a user-created newPosition method in the model. The newPosition method that is invoked in response to a mouse-drag is defined on the model’s “Custom” code page. This method starts by computing the bob’s angle from the Cartesian mouse coordinates (x, y). The (x, y) values are adjusted to ensure that the pendulum length remains fixed, and the velocity variables are set to zero. Finally, data in the q plot are cleared. Users can compare different plots by right-clicking within the plot to create a copy before dragging the bob to a new position. public void newPosition () // length is constant theta = Math.atan2(x,-y); x = L*Math.sin(theta); y = -L*Math.cos(theta); omega = 0.0; vx = vy = 0.0; t = 0.0; _view.resetTraces();
{ // invoked during a drag action // computes the angle // computes x given a length // computes y given a length // zero angular velocity // zeros velocity components // resets time // clears the plot
It is important to note that the newPosition method on the “Custom” page and the conversion from polar to Cartesian coordinates on the “Constraints” page are the only Java code within the pendulum model. Ejs creates a complete and ready-to-distribute Java application and a complete Java applet with an associated html page from the XML-based pendulum model in the _apps directory when the run icon is pressed.
Conclusion The Pendulum model illustrates many concepts in introductory mechanics and is easy to extend to include damping and forcing and to improve the analysis with phase-space and energy graphs. This model The Physics Teacher ◆ Vol. 45, November 2007
Fig. 7. Table of properties for the trace element in the view.
Fig. 8. Table of properties for the bob element in the view.
is packaged with other mechanics examples in the ejs_tpt_modeling.jar archive. The first example in the package models Newton’s first law without differential equations. The second example models one-dimensional free fall with first-order differential equations for position and velocity. The models in the archive increase in complexity and show additional Ejs features, but even advanced models, such as Keplerian orbits from an inverse-square-law force, require almost no Java programming. Teachers who customize our examples or who develop their own models and curricular material can use the “Package” icon on the right-hand side of the Ejs workspace to create readyto-use Launcher packages containing collections of models for distribution to students and colleagues. We are in the process of creating a database of Ejs models and of related curricular material on the BQ Learning website that in-service physics teachers can use to implement computer-based interactive engagement methods in their classrooms.7 The database is free and open and is based on XML standards for easy indexing in digital libraries. Ejs promotes an integrated understanding of the modeling process by exposing and providing access to the underlying assumptions in the physical model. It encourages revision and testing of the model and the distribution of the model including narrative and 479
documentation. It is both simple to use and sufficiently advanced to handle complex models that occur in professional practice. Finally, the BQ database enables teachers to quickly access, revise, and adapt Ejs models for their own teaching and to distribute their revisions. Acknowledgments
We would like to thank Mario Belloni, Anne Cox, and Fu-Kwun Hwang for useful conversations regarding the development of Ejs models. The Launcher program used to distribute ready-to-use Ejs models was developed by Douglas Brown. The BQ database is maintained by William Junkin and Anne Cox. Easy Java Simulations is an Open Source Physics project and this project is supported by the National Science Foundation (DUE-0442581). References 1. M. Wells, D. Hestenes, and G. Swackhamer, “A modeling method for high school physics instruction,” Am. J. Phys. 63, 606–619 (July 1995). 2. F.E. Cellier and J. Greifeneder, Continuous System Modeling (Springer, New York, 2006). 3. W. Christian, Open Source Physics: A User’s Guide with Examples (Addison Wesley, New York, 2007). Simulations developed using the OSP library and OSP tools are available at http://www.opensourcephysics.org. Resources for Ejs modeling are located in the modeling subdirectory; http://www.opensourcephysics.org/ modeling. 4. W. Christian, M. Belloni, and D. Brown, “An open source XML framework for authoring curricular material,” Comp. Sci. Eng. 8 (5), 51-58 (Sept./Oct. 2006). 5. Java is freely available under the GNU open source GPL license and installing it is no more difficult than installing other third-party software such as Acrobat or Flash readers. The most popular Java implementations
480
are available from Sun Microsystems at http://java.sun. com/javase. The Apple Computer OS X operating system ships with Java installed. 6. F. Esquembre, Creación de Simulaciones Interactivas en Java (Prentice Hall, Madrid, 2005). The Ejs program, documentation, and examples can be downloaded from http://www.um.es/fem/Ejs. 7. The BQ Learning database contains many Ejs models. These models can be accessed at http://www. bqlearning.org. PACS codes: 01.50.ht, 45.00.00 Wolfgang Christian is the Brown Professor of Physics at Davidson College, where he has taught since 1983. He received his BS and PhD in physics from North Carolina State University at Raleigh. He is author or coauthor of nine books, including: Open Source Physics: A User’s Guide with Examples, An Introduction to Computer Simulation Methods, Physlet Quantum Physics, Physlet Physics, Physlets: Teaching Physics with Interactive Curricular Material, Just-in-Time Teaching, and Waves and Optics: Volume 9 of the Computational Physics Upper Level Software series. He has been the books editor of the APS journal Computers in Physics. He is a fellow of the American Physical Society and past chair of the Forum on Education. His research interests are in the areas of computational physics and instructional software design. Department of Physics, Davidson College, Davidson, NC 28035-6910, USA;
[email protected] Francisco Esquembre received a PhD in mathematics in 1991 from the Universidad de Murcia, Spain, where he is currently Assistant Professor of Mathematics. From 1995 until 1999 he was General Director for Universities and Research for the Regional Ministry of Education, and from 2002 to 2006 he was head of the Transfer Research Office of the University of Murcia. He is the author of Creación de Simulaciones Interactivas en Java and coauthor of Fislets. His academic expertise includes differential equations, dynamical systems, and numerical analysis. His teaching interests are computer assisted teaching and learning as well as simulation of scientific processes for didactical purposes. Departamento de Matemáticas, Universidad de Murcia, 30071 Murcia, Spain;
[email protected]
The Physics Teacher ◆ Vol. 45, November 2007