KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > Lorenz


1 import java.applet.Applet JavaDoc;
2 import java.awt.BorderLayout JavaDoc;
3 import java.awt.Button JavaDoc;
4 import java.awt.Panel JavaDoc;
5 import java.awt.event.ActionEvent JavaDoc;
6 import java.awt.event.ActionListener JavaDoc;
7 import java.util.Properties JavaDoc;
8
9 import jcckit.Graphics2DPlotCanvas;
10 import jcckit.GraphicsPlotCanvas;
11 import jcckit.data.DataCurve;
12 import jcckit.data.DataPlot;
13 import jcckit.data.DataPoint;
14 import jcckit.util.ConfigParameters;
15 import jcckit.util.PropertiesBasedConfigData;
16
17
18 /**
19  *
20  *
21  * @author Franz-Josef Elmer
22  */

23 public class Lorenz extends Applet JavaDoc {
24   private double _b = 8.0 / 3;
25   private double _sigma = 10;
26   private double _r = 28;
27   private double _dt = 0.015;
28   private double[] _x = new double[3];
29   private double[] _xDot1 = new double[3];
30   private double[] _xDot2 = new double[3];
31   private double[][] _walk = new double[50][2];
32   private int _walkIndex;
33   private DataPlot _dataPlot;
34   private Thread JavaDoc _animationThread;
35   
36   public void init() {
37     GraphicsPlotCanvas plotCanvas
38         = createPlotCanvas("true".equals(getParameter("graphics2D")));
39     
40     _dataPlot = new DataPlot();
41     _dataPlot.addElement(new DataCurve("")); // dummy
42
reset();
43     plotCanvas.connect(_dataPlot);
44     
45     setLayout(new BorderLayout JavaDoc());
46     add(plotCanvas.getGraphicsCanvas(), BorderLayout.CENTER);
47     add(createControlPanel(), BorderLayout.SOUTH);
48   }
49
50   private GraphicsPlotCanvas createPlotCanvas(boolean graphics2D) {
51     Properties JavaDoc props = new Properties JavaDoc();
52     ConfigParameters config
53         = new ConfigParameters(new PropertiesBasedConfigData(props));
54     props.put("foreground", "0xffffff");
55     props.put("background", "0");
56     props.put("plot/legendVisible", "false");
57     props.put("plot/coordinateSystem/xAxis/minimum", "-20");
58     props.put("plot/coordinateSystem/xAxis/maximum", "20");
59     props.put("plot/coordinateSystem/xAxis/ticLabelFormat", "%d");
60     props.put("plot/coordinateSystem/yAxis/axisLabel", "z");
61     props.put("plot/coordinateSystem/yAxis/minimum", "0");
62     props.put("plot/coordinateSystem/yAxis/maximum", "50");
63     props.put("plot/coordinateSystem/yAxis/ticLabelFormat", "%d");
64     props.put("plot/curveFactory/definitions", "curve");
65     props.put("plot/curveFactory/curve/initialHintForNextPoint/className",
66               "jcckit.plot.ShapeAttributesHint");
67     props.put("plot/curveFactory/curve/initialHintForNextPoint/"
68               + "initialAttributes/fillColor", "0x50a");
69     props.put("plot/curveFactory/curve/initialHintForNextPoint/"
70               + "fillColorHSBIncrement", "0.0 0.0 0.018");
71     props.put("plot/curveFactory/curve/withLine", "false");
72     props.put("plot/curveFactory/curve/symbolFactory/className",
73               "jcckit.plot.CircleSymbolFactory");
74     props.put("plot/curveFactory/curve/symbolFactory/size", "0.015");
75     
76     return graphics2D ? new Graphics2DPlotCanvas(config)
77                       : new GraphicsPlotCanvas(config);
78   }
79   
80   private Panel JavaDoc createControlPanel() {
81     Panel JavaDoc controlPanel = new Panel JavaDoc();
82     Button JavaDoc startButton = new Button JavaDoc("start");
83     startButton.addActionListener(new ActionListener JavaDoc() {
84                 public void actionPerformed(ActionEvent JavaDoc e) {
85                   _animationThread = new Thread JavaDoc() {
86                           public void run() {
87                             while (_animationThread != null) {
88                               try {
89                                 Thread.sleep(50);
90                               } catch (InterruptedException JavaDoc e) {}
91                               next();
92                             }
93                           }
94                         };
95                   _animationThread.start();
96                 }
97               });
98     controlPanel.add(startButton);
99     Button JavaDoc stopButton = new Button JavaDoc("stop");
100     stopButton.addActionListener(new ActionListener JavaDoc() {
101                 public void actionPerformed(ActionEvent JavaDoc e) {
102                   _animationThread = null;
103                 }
104               });
105     controlPanel.add(stopButton);
106     Button JavaDoc resetButton = new Button JavaDoc("reset");
107     resetButton.addActionListener(new ActionListener JavaDoc() {
108                 public void actionPerformed(ActionEvent JavaDoc e) {
109                   reset();
110                 }
111               });
112     controlPanel.add(resetButton);
113     
114     return controlPanel;
115   }
116   
117   private void reset() {
118     _x[0] = 10 * (Math.random() - 0.5);
119     _x[1] = 10 * (Math.random() - 0.5);
120     _x[2] = 10;
121     for (int i = 0; i < _walk.length; i++) {
122       _walk[i][0] = _x[0];
123       _walk[i][1] = _x[2];
124     }
125     updateCurve();
126   }
127   
128   private void next() {
129     integrate();
130     _walk[_walkIndex][0] = _x[0];
131     _walk[_walkIndex][1] = _x[2];
132     _walkIndex = (_walkIndex + 1) % _walk.length;
133     updateCurve();
134   }
135
136   /**
137    * Integrates differential equations by using a second-order Runge-Kutta
138    * method.
139    */

140   private void integrate() {
141     calculateDerivatives(_x, _xDot1);
142     for (int i = 0; i < _x.length; i++) {
143       _x[i] += _dt * _xDot1[i];
144     }
145     calculateDerivatives(_x, _xDot2);
146     for (int i = 0; i < _x.length; i++) {
147       _x[i] += 0.5 * _dt * (_xDot2[i] - _xDot1[i]);
148     }
149   }
150   
151   private void calculateDerivatives(double[] x, double[] xDot) {
152     xDot[0] = _sigma * (x[1] - x[0]);
153     xDot[1] = _r * x[0] - x[1] - x[0] * x[2];
154     xDot[2] = x[0] * x[1] - _b * x[2];
155   }
156   
157   private void updateCurve() {
158     DataCurve curve = new DataCurve("trajectory");
159     for (int i = 0; i < _walk.length; i++) {
160       int index = (_walkIndex + i) % _walk.length;
161       curve.addElement(new DataPoint(_walk[index][0], _walk[index][1]));
162     }
163     _dataPlot.replaceElementAt(0, curve);
164   }
165 }
166
Free Books   Free Magazines  
Popular Tags