# Developing Computer Simulations Using Object Oriented Programming. The Three Body Problem: A Case Study Mike O’Leary & Shiva Azadegan Towson University.

## Presentation on theme: "Developing Computer Simulations Using Object Oriented Programming. The Three Body Problem: A Case Study Mike O’Leary & Shiva Azadegan Towson University."— Presentation transcript:

Developing Computer Simulations Using Object Oriented Programming. The Three Body Problem: A Case Study Mike O’Leary & Shiva Azadegan Towson University Supported by the National Science Foundation under grant DUE 9952625

What is the Course? We teach an interdisciplinary course in modeling, numerical methods, and computer programming. Mixed audience primarily of computer science and mathematics majors. Prerequisites: –Calculus 1, Calculus 2 –Introduction to Computer Science 1 (C++)

The Goal To teach students how to take a realistic problem –Model it –Choose an appropriate numerical method –Implement that method using modern object- oriented programming and using modern computer graphics –Interpret the results –Write a report summarizing their investigation

Methodology Team taught –One mathematics & one computer science instructor Course is driven by a sequence of three realistic project problems –Each project requires modeling, numerical methods, programming, and a report –Students are evaluated based on their report and on their program.

The Problems Realistic problems from the sciences Examples: –Three body problem –Heat flow –Motion under air resistance –Spread of HIV virus through the body –Traffic flow

Numerical Methods Evaluating integrals –Trapezoidal Rule, Simpson’s Rule Ordinary differential equations and systems –Euler’s method, Implicit Euler, Heun’s method, Runge-Kutta methods, Runge-Kutta-Fehberg methods –Error analysis and control Partial differential equations. –Finite difference methods –Error and stability analysis using von Neumann methods

Programming C++ Microsoft Visual Studio Microsoft Foundation Classes (MFC)

Object–Oriented Programming Extensive use of classes –What is a class? A structure containing data and methods for manipulating the data Examples: dialog, simulation, graph, vector –How are they useful? Organization Encapsulation (Data Hiding) Inheritance Reusability

Student Example

Event-Driven Programming Program initializes, then waits for events –Button is pressed –Key is pressed –Mouse is moved When an event occurs, a certain piece of code is executed

Student Example

Programming: Graphics Full use is made of the computer’s ability to represent information graphically Use the MFC library of Microsoft Visual C++ to access graphics routines –In common use Student example

Implementation How can such a course be taught in practice? What are the details?!

How is it Taught? Team taught Mathematics and computer science are closely integrated. Ideas are introduced in the order that they are needed.

Worksheets Because no text is available for the course, we provide students with a sequence of worksheets. These summarize the material taught in class and contain exercises. Examples

Worksheet 1 Mathematics –Trapezoidal Rule –Simpson’s Rule Programming –Review of functions –Function prototypes –Passing functions Assignment –Write a console program to implement Trapezoidal Rule & Simpson’s Rule –Implement and analyze Simpson’s 3/8 Rule

Worksheet 2 Programming –Classes, objects & instances –Private & public data & methods –Constructors & destructors; setters & getters –Overloading Assignment –Create a class for points, vectors, and line segments. –Use inheritance.

Worksheet 3 Programming –Classes, continued –Pointers to functions Assignment –Write a program with a class called Integral. –It should have a public method that take as input the number of subintervals and return the trapezoidal rule approximation of the integral. –It should have another method that return’s the Simpson’s rule approximation

Worksheet 3 Results

Worksheet 3 Results

Worksheet 4 Programming –Introduction to dialog-based programming –Introduction to event-driven programming Assignment –Write a dialog-based version of the integral program

Worksheet 4 Results

Worksheet 5 Programming –Create a dialog based program with graphics. –Classes for balls, for control dialog and for graphics dialog.

Worksheet 6 Modeling –Newton’s law of gravity. –Model for the three body problem where for i = 1, 2, 3. –12 equations in 12 variables plus time

Worksheet 6 Mathematics –Euler’s method –Implicit Euler –Heun’s method (Improved Euler) Applications to single equations Assignment –Write a dialog based program to solve a particular initial-value problem.

Worksheet 6 Results

Worksheet 7 Mathematics –Fourth order Runge-Kutta method –Applications to systems of differential equations Assignment –Modify the previous program to also implement the Runge-Kutta method –Write a program that solves a system using the Runge-Kutta method

Worksheet 7 Results

Project Write a C++ program that simulates the motion of three bodies under the influence of gravity As input the program should take the initial positions, velocities, and masses of all of the bodies, as well as the gravitational constant, the time step, and the ending time.

Project The program should use a Runge-Kutta method to calculate the resulting motion of the bodies. The program should graphically display the results.

Project You are then to write up a technical report that answers the following questions: 1.What is the mathematical model of the problem? 2.What is the numerical method used to solve the problem? 3.What is the structure of your program? 4.Describe the range of possible behaviors shown by the system.

Project In particular, for question 4, consider the following questions: –Is it possible for the system to eject one of the bodies to infinity? –Is it possible for the system to return to its initial state? –Is it possible for the system to return to a state close to, but not the same as its initial state?

Results Student programStudent program Student code

Where did we go from here? Double pendulum Heat flow

Double pendulum Model Mathematics –Lagrangian dynamics –Calculus of variations Programming –Use of the mouse

Double pendulum Results –Sample programSample program –Sample codeSample code

Heat equation Model –As a system of ordinary differential equations –Probabilistically –As a partial differential equation Mathematics –Finite difference methods –Implicit and explicit –Von Neumann analysis of stability Programming –Menus

Heat equation Results –Student programStudent program –Student codeStudent code

Other projects Motion of a baseball under air resistance Wave motion Traffic jam formation Double spring Resonant filter (http://www.cs/gasou.edu/faculty/demos/filterdemo/)Resonant filterhttp://www.cs/gasou.edu/faculty/demos/filterdemo/ Dynamics of HIV

Evaluation & Lessons What did we learn?

Project Due Dates Same due date for program and report –Students would not finish the program until the due date. –Student report quality suffered. Now we have two due dates –One for the program –One for the report. –These are graded separately

Open Ended Questions Originally we used open-ended questions for the project reports –Without focus, students had difficulty deciding what was important Now we ask the students detailed and specific questions which they must answer in their report

Student reaction Positive reaction –Students enjoyed the way the programming and the mathematics were integrated –Students also enjoyed working on problems with a purpose Negative reaction –Math students have difficulty with the programming, and computer science students have difficulty with the mathematics –Encourage teamwork!

Resources http://www.towson.edu/~moleary/Simulation.htm

Download ppt "Developing Computer Simulations Using Object Oriented Programming. The Three Body Problem: A Case Study Mike O’Leary & Shiva Azadegan Towson University."

Similar presentations