1 package net.sf.openrocket.optimization.rocketoptimization.parameters;
3 import java.util.Arrays;
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.SimulationCancelledException;
12 import net.sf.openrocket.simulation.exception.SimulationException;
13 import net.sf.openrocket.simulation.exception.SimulationLaunchException;
14 import net.sf.openrocket.simulation.listeners.SimulationListener;
15 import net.sf.openrocket.simulation.listeners.system.InterruptListener;
16 import net.sf.openrocket.startup.Application;
19 * An abstract optimization parameter that simulates a rocket flight and obtains
20 * a value from the simulation result.
22 * @author Sampo Niskanen <sampo.niskanen@iki.fi>
24 public abstract class SimulationBasedParameter implements OptimizableParameter {
26 private static final LogHelper log = Application.getLogger();
29 public double computeValue(Simulation simulation) throws OptimizationException, InterruptedException {
31 log.debug("Running simulation for " + getName());
33 SimulationListener[] listeners = getSimulationListeners();
34 listeners = Arrays.copyOf(listeners, listeners.length + 1);
35 listeners[listeners.length - 1] = new InterruptListener();
36 simulation.simulate(listeners);
38 double value = getResultValue(simulation.getSimulatedData());
39 log.debug("Parameter '" + getName() + " was " + value);
41 } catch (MotorIgnitionException e) {
42 // A problem with motor ignition will cause optimization to fail
43 throw new OptimizationException(e);
44 } catch (SimulationLaunchException e) {
45 // Other launch exceptions result in illegal value
47 } catch (SimulationCancelledException e) {
48 // Simulation cancellation stops the optimization
49 throw (InterruptedException) new InterruptedException("Optimization was interrupted").initCause(e);
50 } catch (SimulationException e) {
51 // Other exceptions fail
52 throw new OptimizationException(e);
58 * Return the optimization parameter from the simulation flight data.
60 * @param simulatedData the simulated data.
61 * @return the optimization parameter.
63 protected abstract double getResultValue(FlightData simulatedData);
66 * Return an array of simulation listeners to provide to the simulation.
67 * This may include a listener that stops the simulation after the necessary value
70 * This array should NOT contain InterruptListener, it will be added implicitly.
72 * @return an array of simulation listeners to include.
74 protected SimulationListener[] getSimulationListeners() {
75 return new SimulationListener[0];