upgraded idmclib version to 0.2.0
[iDMC.git] / src / java / org / tsho / dmc2 / managers / LyapunovManager.java
blob74f768d625ac203f464ca6ec3fb34666a51c86bf
1 /*
2 * iDMC the interactive Dynamical Model Calculator simulates and performs
3 * graphical and numerical analysis of systems of differential and
4 * difference equations.
6 * Copyright (C) 2004 Marji Lines and Alfredo Medio.
8 * Written by Daniele Pizzoni <auouo@tin.it>.
9 * Extended by Alexei Grigoriev <alexei_grigoriev@libero.it>.
13 * The software program was developed within a research project financed
14 * by the Italian Ministry of Universities, the Universities of Udine and
15 * Ca'Foscari of Venice, the Friuli-Venezia Giulia Region.
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 2 of the License, or any
20 * later version.
22 * This program is distributed in the hope that it will be useful, but
23 * WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25 * General Public License for more details.
27 package org.tsho.dmc2.managers;
29 import java.awt.event.ComponentAdapter;
30 import java.awt.event.ComponentEvent;
32 import org.jfree.chart.StandardLegend;
33 import org.jfree.data.Range;
34 import org.tsho.dmc2.core.CoreStatusEvent;
35 import org.tsho.dmc2.core.CoreStatusListener;
36 import org.tsho.dmc2.core.VariableDoubles;
37 import org.tsho.dmc2.core.chart.DmcPlotRenderer;
38 import org.tsho.dmc2.core.chart.LyapunovRenderer;
39 import org.tsho.dmc2.core.model.Model;
40 import org.tsho.dmc2.core.model.SimpleMap;
41 import org.tsho.dmc2.ui.InvalidData;
42 import org.tsho.dmc2.ui.lyapunov.LyapunovControlForm2;
43 import org.tsho.dmc2.core.model.ODE;
44 import org.tsho.dmc2.ui.lyapunov.*;
46 public class LyapunovManager extends AbstractManager
47 implements AbstractManager.Crosshair,
48 AbstractManager.GridLines,
49 AbstractManager.ConnectDots,
50 AbstractManager.BigDots,
51 AbstractManager.AxesVisibility {
53 private LyapunovComponent component;
55 private Model model;
56 //? seems that interferes with frame of the superclass // private ManagerListener frame;
57 private LyapunovControlForm2 form;
59 private LyapunovRenderer renderer;
61 private byte type;
63 private boolean gridlines;
64 private boolean crosshair;
65 private boolean connectWithDots;
66 private boolean bigDots;
68 public LyapunovManager(LyapunovComponent component) {
70 super((ManagerListener2) component);
71 this.component = component;
72 this.model = component.getModel();
73 this.form = (LyapunovControlForm2) component.getControlForm();
75 /* defaults */
76 setGridlines(true);
77 crosshair = false;
79 this.type = LyapunovControlForm2.TYPE_VS_TIME;
81 renderer = new LyapunovRenderer(plot, model, component);
82 plot.setPlotRenderer(renderer);
84 chart.setTitle(model.getName());
85 setCrosshair(crosshair);
87 chart.setLegend(new StandardLegend());
89 // stop everything on resizing
90 chartPanel.addComponentListener(new ComponentAdapter() {
91 public void componentResized(ComponentEvent e) {
92 LyapunovManager.this.stopRendering();
94 });
96 plot.addCoreStatusListener( new CoreStatusListener() {
97 public void sendCoreStatus(final CoreStatusEvent event) {
98 if (event.getType() == CoreStatusEvent.REDRAW) {
99 launchThread();
101 else if (event.getType() == CoreStatusEvent.REPAINT) {
102 chartPanel.repaint();
108 public boolean doRendering() {
109 plot.setDrawGridLines(gridlines);
111 plot.setNoData(false);
112 switch(type) {
113 case LyapunovControlForm2.TYPE_VS_TIME:
114 return renderVsTime();
116 case LyapunovControlForm2.TYPE_VS_PAR:
117 return renderVsParameter();
119 case LyapunovControlForm2.TYPE_PAR_SPACE:
120 return renderArea();
122 default:
123 throw new InternalError("invalid type");
127 public boolean renderVsTime() {
128 VariableDoubles parameters;
129 VariableDoubles initial;
130 Range timeRange, verRange;
131 double stepSize;
134 try {
135 parameters = form.getParameters();
136 //parameters = form.getParameterValues();
137 initial = form.getInitialValues();
138 timeRange = form.getTimeRange();
139 verRange = form.getVerticalRange();
141 stepSize=0;//needs to be initialized in any case..
142 if (model instanceof ODE)
143 stepSize =form.getStepSize();
145 catch (InvalidData e) {
146 frame.showInvalidDataDialog(e.getMessage());
147 return false;
150 if (!(model instanceof ODE)) {
151 renderer.initializeVsTime(parameters, initial, timeRange);
153 else{
154 renderer.initializeVsTime(parameters,initial,timeRange,stepSize);
156 renderer.setConnectWithLines(connectWithDots);
157 renderer.setBigDots(bigDots);
159 xAxis.setLabel("time");
160 yAxis.setLabel("exponent(s)");
161 plot.setDataRanges(timeRange, verRange);
163 launchThread();
164 return true;
167 public boolean renderVsParameter() {
168 VariableDoubles parameters;
169 VariableDoubles initial;
170 String label;
171 Range parRange, verRange;
172 int iterations=0;//initialization is needed since compiler overcautious
173 double timePeriod=0;
174 double stepSize=0;
176 try {
177 parameters = form.getParameters();
178 initial = form.getInitialValues();
179 parRange = form.getFirstParameterRange();
180 verRange = form.getVerticalRange();
181 if (model instanceof ODE){
182 timePeriod = form.getTimePeriod();
183 stepSize=form.getStepSize();
185 else{
186 iterations = form.getIterations();
189 catch (InvalidData e) {
190 frame.showInvalidDataDialog(e.getMessage());
191 return false;
194 label = form.getLabelOnH();
196 if (!(model instanceof ODE)){
197 renderer.initializeVsParameter(parameters, initial, label, parRange, iterations);
199 else{
200 renderer.initializeVsParameter(parameters,initial, label,parRange,timePeriod,stepSize);
202 renderer.setConnectWithLines(connectWithDots);
203 renderer.setBigDots(bigDots);
205 xAxis.setLabel(label);
206 yAxis.setLabel("exponent(s)");
207 plot.setDataRanges(parRange, verRange);
209 launchThread();
210 return true;
213 public boolean renderArea() {
214 VariableDoubles parameters = null;
215 VariableDoubles initial;
216 String firstLabel, secondLabel;
217 Range firstParRange, secondParRange;
218 double epsilon;
219 int iterations=0;
220 double timePeriod=0;
221 double stepSize=0;
223 try {
224 parameters = form.getParameters();
225 initial = form.getInitialValues();
226 firstParRange = form.getFirstParameterRange();
227 secondParRange = form.getSecondParameterRange();
228 epsilon = form.getEpsilon();
229 if (model instanceof ODE){
230 timePeriod = form.getTimePeriod();
231 stepSize=form.getStepSize();
233 else{
234 iterations = form.getIterations();
236 } catch (InvalidData e2) {
237 frame.showInvalidDataDialog(e2.getMessage());
238 return false;
241 firstLabel = form.getLabelOnH();
242 secondLabel = form.getLabelOnV();
244 if (model instanceof ODE){
245 renderer.initializeParArea(initial, parameters, firstLabel, secondLabel, epsilon, timePeriod,stepSize);
247 else{
248 renderer.initializeParArea(initial, parameters, firstLabel, secondLabel, epsilon, iterations);
250 plot.setDataRanges(firstParRange, secondParRange);
252 launchThread();
253 return true;
256 class RefreshThread extends Thread {
258 int state = DmcPlotRenderer.STATE_NONE;
259 int ratio;
261 RefreshThread() {
262 super("DMCDUE - LyapunovRefresh");
265 public void run() {
267 while (true) {
268 int newState = renderer.getState();
270 if (newState != state) {
272 state = newState;
274 switch (newState) {
275 case DmcPlotRenderer.STATE_NONE:
276 ratio = 0;
277 break;
279 case DmcPlotRenderer.STATE_RUNNING:
280 // frame.progressString("plotting...");
281 break;
283 case DmcPlotRenderer.STATE_FINISHED:
284 case DmcPlotRenderer.STATE_STOPPED:
285 // frame.progressString("ok. ");
286 return;
288 default:
289 ratio = 0;
293 if ((state & DmcPlotRenderer.STATE_RUNNING) != 0 ) {
294 chartPanel.repaint();
296 try {
297 sleep(250);
299 catch (InterruptedException e2) {}
304 private void launchThread() {
306 plot.getPlotRenderer().setState(DmcPlotRenderer.STATE_NONE);
308 Thread refreshJob = new RefreshThread();
309 refreshJob.start();
311 Thread plotJob = new PlotThread("DMCDUE - Lyapunov plotter", false);
312 plotJob.setPriority(Thread.currentThread().getPriority() - 1);
313 plotJob.start();
316 public void stopRendering() {
317 renderer.stop();
320 public void clear() {
321 plot.setNoData(true);
322 plot.zoom(0.0);
323 launchThread();
326 public void setCrosshair(boolean flag) {
327 crosshair = flag;
328 chartPanel.setHorizontalAxisTrace(flag);
329 chartPanel.setVerticalAxisTrace(flag);
332 public boolean isCrosshair() {
333 return crosshair;
336 public boolean isGridlines() {
337 return gridlines;
340 public void setGridlines(boolean b) {
341 gridlines = b;
344 public void setBigDots(boolean flag) {
345 bigDots = flag;
348 public boolean isBigDots() {
349 return bigDots;
352 public void setConnectDots(boolean flag) {
353 connectWithDots = flag;
356 public boolean isConnectDots() {
357 return connectWithDots;
360 public void setType(byte b) {
361 type = b;
364 public byte getType() {
365 return type;
368 public LyapunovControlForm2 getForm(){
369 return form;
372 public LyapunovRenderer getRenderer(){
373 return renderer;