create changelog entry
[debian/openrocket] / core / src / net / sf / openrocket / optimization / rocketoptimization / parameters / SimulationBasedParameter.java
1 package net.sf.openrocket.optimization.rocketoptimization.parameters;
2
3 import java.util.Arrays;
4
5 import net.sf.openrocket.document.Simulation;
6 import net.sf.openrocket.logging.LogHelper;
7 import net.sf.openrocket.optimization.general.OptimizationException;
8 import net.sf.openrocket.optimization.rocketoptimization.OptimizableParameter;
9 import net.sf.openrocket.simulation.FlightData;
10 import net.sf.openrocket.simulation.exception.MotorIgnitionException;
11 import net.sf.openrocket.simulation.exception.SimulationCalculationException;
12 import net.sf.openrocket.simulation.exception.SimulationCancelledException;
13 import net.sf.openrocket.simulation.exception.SimulationException;
14 import net.sf.openrocket.simulation.exception.SimulationLaunchException;
15 import net.sf.openrocket.simulation.listeners.SimulationListener;
16 import net.sf.openrocket.simulation.listeners.system.InterruptListener;
17 import net.sf.openrocket.startup.Application;
18
19 /**
20  * An abstract optimization parameter that simulates a rocket flight and obtains
21  * a value from the simulation result.
22  * 
23  * @author Sampo Niskanen <sampo.niskanen@iki.fi>
24  */
25 public abstract class SimulationBasedParameter implements OptimizableParameter {
26         
27         private static final LogHelper log = Application.getLogger();
28         
29         @Override
30         public double computeValue(Simulation simulation) throws OptimizationException, InterruptedException {
31                 try {
32                         log.debug("Running simulation for " + getName());
33                         
34                         SimulationListener[] listeners = getSimulationListeners();
35                         listeners = Arrays.copyOf(listeners, listeners.length + 1);
36                         listeners[listeners.length - 1] = new InterruptListener();
37                         simulation.simulate(listeners);
38                         
39                         double value = getResultValue(simulation.getSimulatedData());
40                         log.debug("Parameter '" + getName() + " was " + value);
41                         return value;
42                 } catch (MotorIgnitionException e) {
43                         // A problem with motor ignition will cause optimization to fail
44                         throw new OptimizationException(e);
45                 } catch (SimulationLaunchException e) {
46                         // Other launch exceptions result in illegal value
47                         return Double.NaN;
48                 } catch (SimulationCalculationException e) {
49                         // Calculation errors result in illegal value
50                         return Double.NaN;
51                 } catch (SimulationCancelledException e) {
52                         // Simulation cancellation stops the optimization
53                         throw (InterruptedException) new InterruptedException("Optimization was interrupted").initCause(e);
54                 } catch (SimulationException e) {
55                         // Other exceptions fail
56                         throw new OptimizationException(e);
57                 }
58         }
59         
60         
61         /**
62          * Return the optimization parameter from the simulation flight data.
63          * 
64          * @param simulatedData         the simulated data.
65          * @return                                      the optimization parameter.
66          */
67         protected abstract double getResultValue(FlightData simulatedData);
68         
69         /**
70          * Return an array of simulation listeners to provide to the simulation.
71          * This may include a listener that stops the simulation after the necessary value
72          * has been computed.
73          * <p>
74          * This array should NOT contain InterruptListener, it will be added implicitly.
75          * 
76          * @return      an array of simulation listeners to include.
77          */
78         protected SimulationListener[] getSimulationListeners() {
79                 return new SimulationListener[0];
80         }
81         
82 }