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.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;
20 * An abstract optimization parameter that simulates a rocket flight and obtains
21 * a value from the simulation result.
23 * @author Sampo Niskanen <sampo.niskanen@iki.fi>
25 public abstract class SimulationBasedParameter implements OptimizableParameter {
27 private static final LogHelper log = Application.getLogger();
30 public double computeValue(Simulation simulation) throws OptimizationException, InterruptedException {
32 log.debug("Running simulation for " + getName());
34 SimulationListener[] listeners = getSimulationListeners();
35 listeners = Arrays.copyOf(listeners, listeners.length + 1);
36 listeners[listeners.length - 1] = new InterruptListener();
37 simulation.simulate(listeners);
39 double value = getResultValue(simulation.getSimulatedData());
40 log.debug("Parameter '" + getName() + " was " + 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
48 } catch (SimulationCalculationException e) {
49 // Calculation errors result in illegal value
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);
62 * Return the optimization parameter from the simulation flight data.
64 * @param simulatedData the simulated data.
65 * @return the optimization parameter.
67 protected abstract double getResultValue(FlightData simulatedData);
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
74 * This array should NOT contain InterruptListener, it will be added implicitly.
76 * @return an array of simulation listeners to include.
78 protected SimulationListener[] getSimulationListeners() {
79 return new SimulationListener[0];