Scientific Applications using MVC

Christopher Roger Mangundjaja S2178403E

I. MVC OVERVIEW

MVC is one of many programming methods that is used to make a program more extendable, flexible, easy to modify.
Model View Controller design allows us to separate the program into three parts. The Model, the Controller and the View.
The Model is the core part of the program. This is where all the data associated with the program goes. The controller is the one controlling the operations of the program (e.g. when the user clicks a button the program will draw a circle). The View is what the user see, based on the data (Model).

As you can see, by doing these separations we can easily modify certain parts of the program without having to change the other parts of the program. An example of this is if the program already has a gui, but the gui only lets the user to see the data in numbers and we want the user to view the graph as well. Then we can easily add another View class, connect it to the same model and that's it. We do not have to modify other parts of the program.

MVC method that I will discuss next and onwards in this report is in Java.

Model

A Model can be one Java class or more. Depending on the design. In Scientific applications, the Model class has all the formulaes, numbers, all the calculations results related to the formulaes are stored here. By doing this, if later we want to change the formulaes and numbers used we only have to modify the Model class without worrying the other classes. This is one of the usefullness of this design. This will also allow error tracing associated with the formulaes and numbers a lot easier, because we know all that is related with formulaes and number is located only in the Model class and it is not scattered across other parts of the program.

In designing the Model class we have to makesure that it has all the necessary public methods to access all the data and do the calculations. Because these public methods are the tools for the other classes to get the data.

After we finished designing the Model(i.e. all numbers, constants, formulaes are in place) we have to connect the Model with the other classes so that the other classes know that their operations are based on this Model. One way of doing this is by creating the Model class by extending java.util.Observable. Because this class has all the necessary methods for reporting changes, observing the data, passing events, etc. which are required for MVC model. Java is designed based on MVC architecture, which is why java is one of the Language of choice for building MVC type applications.

View

This is what the user see. A View can be a gui class, a graphic class or anything that reflects the data and its operations. A nice approach to this is by creating a single class for each view type(e.g. gui,graphics,charts,etc). By doing so we can keep track of the Views more easily.

In designing the View class we have to makesure the View is updated parallel to the data. If we only update the data but we forgot to repaint the View, obviously this is a problem. We avoid this kind of problem by registering the View with the Model properly. Since the Model class is built by extending the Observable class we can easily do this by using the method addObserver. Besides registering the View with the Model, we must also use the setChanged, notifyObservers and update methods to detect the changes in the data and update the View and data(Model) as necessary. One positive point regarding View is no matter how much Views you have(graphs, animations,tables,guis,etc.) they can be assigned to the same Model(data). You can easily make new Views without worrying about the other classes. This really make MVC powerful.

Controller

Controller can be a single class or can be combined with the View class and the Model class. If we use the Observable class it is convinient to combine the Controller with the View. Controller is responsible to notify, detect, update the changes in the Model and the View. It controlls the program. All the setChanged, notifyObservers and update methods are the built in java Controllers, of course you can create your own Controllers but the idea is the same.

II. MVC SCIENCE APPLICATIONS

The authors of all the Applets below does not mentioned, how they design the Applets. In my view it is possible to design these Applets using MVC approach.

A. 1D Motion Java Applet (the first applet on the link page)

This applet is located here.
It is created by Fu-Kwun Hwang. Last time I accessed this site on 2.32 PM Saturday, October 11 2003.

Model

The Model class will consist of 1D Motion equation: x-x0=vot+1/2at*at. This equation must be put inside a public method that will return the displacement. The variables used can be float or doubles. There must be accessor methods to access each of the variables, a reset method to reset all the data, a start method that will start the time increament and a set methods to set the values of these variables, in this case x (displacement),v(velocity),a(acceleration),t(time). The data will change after the time change.

View

Actually there are three Views in the applet, the Acceleration graph, Velocity graph and Displacement graph. One way of doing this is by putting it all in one class but with different values (but same time values) for each views (e.g. the accelaration graph produced the graph by getting the acceleration data and time data from the Model). Another aspect that is also important is the scale. There must be a method in this class that will scale the data to the right size so that the graphs are viewed correctly. The graphs here can be drawn by using the fillRect function in java drawing function.

Controller

The controllers (the Mouse Listener,MouseMotion Listener, Button Listener and animation controllers) here can be put together with the Views where it will update the data and the graphs parallel to the time data updates.

B. Projectiles Motion Applet

This applet is located here.
It is created by Fu-Kwun Hwang. Last time I accessed this site on 3.05 PM Saturday, October 11 2003.

Model

The Model class will consist of 2 equations that will determine the position of the balls in a given time: x-x0=vocosdt (d=degree) for the x component and y-y0 = vosindt-1/2gt*t for the y component. There must be accessor methods to access each of the variables and a set methods to set the values of these variables, in this case y (y position),vy(y velocity),x (x position),vx(x velocity),g(gravitation for y component) for each balls and one t(time) for all the balls. The data will change after the time change. There must also be a simple collisions checker that checks the position of balls and stops the time if the ball collides and another method that will user allow single step the time after the collisions occured (a method that can control the time increament, which is also used for slowmotion).

View

Here there are one view in one class. The View are updated according to the time increment. One more important thing is the scale method that I have described for the first applet, same thing applied here. Here the cannon and the lines can be drawn using the fillRect function in java and the balls can be drawn by using drawOval method in java.

Controller

The controllers used here are the Mouse Listener, Button Listener for all the balls and animation controllers for each ball since their movements are not the same. These controllers must call all the necessary methods and makesure the data and the Views are synchronized. Each of the animation controllers must use different ball positions,velocities data but not the time data. Again the Controllers can be put together with the View class.

C. Spring Applet

This applet is located here.
Last time I accessed this site on 4.07 PM Saturday, October 11 2003.

Model

The formulae used is the simple spring equations: F=ma=kx. This equation must be put inside a public method. Other required methods are the set,get, and reset methods. Start and stop method that will stop or start the time increament. The variables used will be m (mass of the weight),k(spring constant),x(the change in length of the spring), t(time).

View

Cleary there are two views here, the graph and the animation. The animation will need the x value(after it is calculated by the spring equation(F=kx)) but not the t value, since it is only moving up and down depending upon the k and x. The graph view needed the x and the t values to plot the graph. And again the scale method is needed here. The spring animation can be drawn by using the fillRect function in java and a scale function (Graphics2D) in java can be use to scale the spring image in +y and -y directions to animate it. The graph can be drawn using the fillRect function in java.

Controller

Controllers(Button Listener and animation controllers) must update the views correctly. Here two animation controllers can be used, one for the graph and one for the animation.

D. Ball Collisions

This applet is located here.
It is created by Fu-Kwun Hwang. Last time I accessed this site on 4.08 PM Saturday, October 11 2003.

Model

The equation used will be the Momentum equations ( v1f = (((m1-m2)/(m1+m2))*v1i) + ((2 * m2)/(m1+m2))*v2i and v2f = (((m2-m1)/(m1+m2))*v2i) + ((2 * m1)/(m1+m2))*v1i ) that will determine the velocities of the balls after collision and the standard 1D motion equation to calculate the position of the balls. Since this is a 1D collision, we do not have to use the y component, instead we will only use one component. The other methods needed are the usual get,set,start,stop methods. The variables used will be x1(ball 1 position),x2(ball2 position),vx1(ball1 velocitiy),vx2(ball2 velocity),m1(ball1 mass),m2(ball2 mass),a1(ball1 acceleration),a2(ball2 acceleration),t(time),r1(ball1 radius),r2(ball2 radius). The last method needed is the collision detection method, where it checked whether the radius of the two balls have overlapped or not.

View

There is only one view here, the balls animation in one class. Scale method is always needed in this type of applications, sometimes it is impossible to draw based on actual values without any scaling. To draw the balls and the arrows and lines fillRect function and drawOval function in java can be used.

Controller

The controllers are the Button Listener,Mouse Listener and animation controllers. Because the velocities of the balls are different, they must have different controllers that control them.

E. Light Applet

This applet is located here.
It is created by Walter Fendt. Last time I accessed this site on 5.15 PM Saturday, October 11 2003.

Model

The equation used here is: n1sind=n2sind2 (d1=degree1, d2=degree2). The usual set,get,start,stop methods. The variables used will be n1(for surface1),n2(for surface2),d1(angle of incidence),d2(angle of refraction).Here the time increament menthod is not needed nor the time data since the animation is controlled by mouse not by time.

View

There is only on view here, the illustration of the light. Here the View is updated based upon the movement of the mouse not by time. To draw the line representing the light, Pythagorean Theorem must be used here to divide the angles into x and y component and put the result in drawLine function parameters in java.

Controller

The controllers are Button Listener, MouseMotion Listener, Mouse Listener, animation controllers. Two animation controllers are needed, one for the incoming light and one for the refracted light because their angles and constant(n1,n2) are not always the same.

III. CHAOS AND MVC

Brief Chaos History

The idea of chaos in differential equations was founded by Edward Lorenz in 1960s. He created a computer simulation that can ran simplified differential equations. From the simulation he noticed that a small change in the initial conditions of the equations can result in a really complicated final result. The patterns of these results are impossible to trace, they behaved randomly. The idea of small difference in the equations input can produce verry different outcome was called The Butterfly Effect which Lorenz explained in his paper in 1963 "Deterministic nonperiodic flow" in J. Atmos. Sci. that has become the icon and symbol of Chaos theory.This is what we call Chaos today.

Other researchers also did the chaos investigation. In around mid 1960s Steve Smale, produced evidence that there could be differential equations in which they can behave randomly like Lorenz have discovered. In 1970s Robert Shaw confirmed that there is a connection between information content of initial conditions and apparent randomness of behavior. The Chaos research continue on and on until today. More and more chaos visualizations are produced.

The Logistic map (The equation : Xn+1=r*xn(1-xn)=fr(xn))is one of the classic Chaos model. This map was introduced by the biologist Robert May in 1976. It was originally made as a very simple model for the population numbers of species in the presence of limiting factors such as Starvation or disease. There are countless of other Chaos equations out there, but the classic ones will always be a classic.

Chaos Graphs

Program Structure

My Chaos Program consist of 8 clasess:

Model Classes :

All Model classes have the set,get and other methods that are used to calculate the equations. The only difference between the Model classes are the equation used.

A. Logisticmodel.java

This class uses the base equation : Xn+1=r*xn(1-xn)=fr(xn). The graphs will be drawn with different r values.

B. Logisticmodel2.java

A slight variation with the first model : Xn+1=r*xn(1-sn*xn)=fr(xn). Here the value of Sn must be supplied by the user.

C. Sinemap

The equation used : Xn+1=B*sin(3.14*xn). The graphs will be drawn with different B values.

D. Tentmap

The equation used : Xn+1 = A min(Xn, 1 - Xn). The graphs will be drawn at different A values.

Views Clasess

A. Graph1.java

Detailed Time View. The user is allowed to change the r or B or A (depending upon the model used) increment rate. The default number of iterations is the best setup.

B. Graph2.java

Detailed Parameterview View. The user is allowed to change the r or B or A (depending upon the model used) increment rate and the number of iterations(loops).

C. Graph3.java

Detailed Phase View.The user is allowed to change the r or B or A (depending upon the model used) increment rate and the number of iterations(loops).

D. Gui1.java

This is the main class and default data View that calls the other Views.

MVC

The Controllers I combined them inside the View classes, 4 Controllers all. Each Controller is responsible to draw each of the type of the graphs. The last Controller is for the gui(default view) that excepts the user input.

I used 4 Models classes and not combine it to one so that I can easily modify and keep track of them. All the Models classes extends the Observable class which makes it easy for the classes to be connected with the Views and the Controllers. The way I did this is the same as I have described in the beginning of this report.

Inside the Graphs clasess I didn't specify anything under the update method because I like to make the Graphs stop after it is finished painting the whole Graph.

IV. REFERENCES


http://www.amath.washington.edu/courses/383-autumn-2001/messages/msg00002.html

http://students.bath.ac.uk/ns0ms/L1b.html

---------------------------------------------------------------------------------------------------------------------------------------------------

Last Modified: 10th of December 2003

Author: cmangund

---------------------------------------------------------------------------------------------------------------------------------------------------