View Javadoc

1   /*
2    * @(#) DifferentialEquation.java Oct 25, 2003
3    * 
4    * Copyright (c) 2003 Delft University of Technology Jaffalaan 5, 2628 BX Delft,
5    * the Netherlands All rights reserved.
6    * 
7    * This software is proprietary information of Delft University of Technology
8    * The code is published under the General Public License
9    */
10  package nl.tudelft.simulation.dsol.formalisms.dess;
11  
12  import java.rmi.RemoteException;
13  
14  import nl.tudelft.simulation.dsol.simulators.DESSSimulatorInterface;
15  import nl.tudelft.simulation.dsol.simulators.SimulatorInterface;
16  import nl.tudelft.simulation.event.EventInterface;
17  import nl.tudelft.simulation.event.EventListenerInterface;
18  import nl.tudelft.simulation.event.EventType;
19  import nl.tudelft.simulation.jstats.ode.integrators.NumericalIntegrator;
20  import nl.tudelft.simulation.logger.Logger;
21  
22  /***
23   * The Differential equation provides a reference implementation of the
24   * differential equation.
25   * <p>
26   * (c) copyright 2003 <a href="http://www.simulation.tudelft.nl">Delft
27   * University of Technology </a>, the Netherlands. <br>
28   * See for project information <a href="http://www.simulation.tudelft.nl">
29   * www.simulation.tudelft.nl </a> <br>
30   * License of use: <a href="http://www.gnu.org/copyleft/gpl.html">General Public
31   * License (GPL) </a>, no warranty <br>
32   * 
33   * @author <a href="http://www.simulation.tudelft.nl/people/jacobs.html">Peter
34   *         Jacobs </a>
35   * @version 1.4 2004-03-26
36   * @since 1.0
37   */
38  public abstract class DifferentialEquation extends
39  		nl.tudelft.simulation.jstats.ode.DifferentialEquation implements
40  		DifferentialEquationInterface, EventListenerInterface
41  {
42  	//we initialze the array (30 size set seems enough..)
43  	static
44  	{
45  		for (int i = 0; i < VALUE_CHANGED_EVENT.length; i++)
46  		{
47  			VALUE_CHANGED_EVENT[i] = new EventType("VALUE_CHANGED_EVENT[" + i
48  					+ "]");
49  		}
50  	}
51  
52  	/*** simulator */
53  	protected DESSSimulatorInterface simulator = null;
54  
55  	/*** the previousX */
56  	protected double previousX = 0.0;
57  
58  	/*** the previousY */
59  	protected double[] previousY = null;
60  
61  	/***
62  	 * constructs a new stateful DifferentialEquation with Euleras numerical
63  	 * integration method.
64  	 * 
65  	 * @param simulator the simulator
66  	 * @throws RemoteException on network exception
67  	 */
68  	public DifferentialEquation(final DESSSimulatorInterface simulator)
69  			throws RemoteException
70  	{
71  		this(simulator, simulator.getTimeStep(),
72  				NumericalIntegrator.DEFAULT_INTEGRATOR);
73  	}
74  
75  	/***
76  	 * constructs a new stateful DifferentialEquation with Euleras numerical
77  	 * integration method.
78  	 * 
79  	 * @param simulator the simulator
80  	 * @param timeStep the timeStep for ODE estimation
81  	 */
82  	public DifferentialEquation(final DESSSimulatorInterface simulator,
83  			final double timeStep)
84  	{
85  		this(simulator, timeStep, NumericalIntegrator.DEFAULT_INTEGRATOR);
86  	}
87  
88  	/***
89  	 * constructs a new DifferentialEquation
90  	 * 
91  	 * @param simulator the simulator.
92  	 * @param timeStep the timeStep for ODE estimation.
93  	 * @param numericalMethod the numerical method to be used.
94  	 */
95  	public DifferentialEquation(final DESSSimulatorInterface simulator,
96  			final double timeStep, final short numericalMethod)
97  	{
98  		super(timeStep, numericalMethod);
99  		this.simulator = simulator;
100 		try
101 		{
102 			simulator.addListener(this, SimulatorInterface.TIME_CHANGED_EVENT,
103 					false);
104 		} catch (RemoteException exception)
105 		{
106 			Logger.warning(this, "DifferentialEquation", exception);
107 		}
108 	}
109 
110 	/***
111 	 * constructs a new DifferentialEquation
112 	 * 
113 	 * @param simulator the simulator.
114 	 * @param timeStep the timeStep for ODE estimation.
115 	 * @param numericalIntegrator the actual integrator to be used.
116 	 */
117 	public DifferentialEquation(final DESSSimulatorInterface simulator,
118 			double timeStep, final NumericalIntegrator numericalIntegrator)
119 	{
120 		super(timeStep, numericalIntegrator);
121 		this.simulator = simulator;
122 		try
123 		{
124 			simulator.addListener(this, SimulatorInterface.TIME_CHANGED_EVENT,
125 					false);
126 		} catch (RemoteException exception)
127 		{
128 			Logger.warning(this, "DifferentialEquation", exception);
129 		}
130 	}
131 
132 	/***
133 	 * @see EventListenerInterface#notify(EventInterface)
134 	 */
135 	public synchronized void notify(final EventInterface event)
136 			throws RemoteException
137 	{
138 		if (event.getSource() instanceof DESSSimulatorInterface
139 				&& event.getType()
140 						.equals(SimulatorInterface.TIME_CHANGED_EVENT))
141 		{
142 			if (this.simulator.getSimulatorTime() < super.x0
143 					|| Double.isNaN(super.x0))
144 			{
145 				return;
146 			}
147 			// do not put super here!
148 			this.previousY = integrateY(this.simulator.getSimulatorTime(),
149 					this.previousX, this.previousY);
150 			for (int i = 0; i < super.y0.length; i++)
151 			{
152 				this.fireEvent(
153 						DifferentialEquationInterface.VALUE_CHANGED_EVENT[i],
154 						this.previousY[i], this.simulator.getSimulatorTime());
155 			}
156 			this.previousX = this.simulator.getSimulatorTime();
157 		}
158 	}
159 
160 	/***
161 	 * @see nl.tudelft.simulation.jstats.ode.DifferentialEquationInterface#initialize(double,
162 	 *      double[])
163 	 */
164 	public void initialize(double x, double[] y)
165 	{
166 		super.initialize(x, y);
167 		this.previousX = x;
168 		this.previousY = y;
169 	}
170 }