From: miettinen Date: Mon, 8 Apr 2013 08:57:26 +0000 (+0000) Subject: First iteration of sensitivity analysis which draws graphs and can be configured... X-Git-Tag: simantics-1.10~5 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=2c75ad63f01bc51655419c6cf20f51f2e75fa3e0;p=simantics%2Fsysdyn.git First iteration of sensitivity analysis which draws graphs and can be configured through the user interface. (refs #4177) git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@27259 ac1ea38d-2e2b-0410-8846-a27921b304fc --- diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynExperiment.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynExperiment.java index 093baadf..7c5c7f4d 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynExperiment.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynExperiment.java @@ -449,23 +449,18 @@ public class SysdynExperiment extends Experiment implements IDynamicExperiment, */ public synchronized void simulate(final IModelicaMonitor monitor, final IProgressMonitor progressMonitor) throws IOException { canceled = false; - progressMonitor.subTask("Write modelica classes"); // Write Modelica files - - String modelText = getModelicaCode(monitor, false, getOpenModelicaVersion()); if(modelText == null) return; - progressMonitor.worked(1); // Write initial files and add init-parameters progressMonitor.subTask("Write simulation files"); HashMap experimentParameters = getExperimentParameters(monitor); - // add loadFile script to load all related functions and function libraries String additionalScript = getAdditionalScripts(); @@ -473,7 +468,6 @@ public class SysdynExperiment extends Experiment implements IDynamicExperiment, SimulationLocation simulationLocation = createSimulationFiles(sysdynModel, modelText, experimentParameters, additionalScript, false); progressMonitor.worked(1); - // Build the model and store previous model structure and inits that affect the building // If there is no exe file OR the model structure has not changed, no need to build boolean structureChanged = hasStructureChanged(modelText); @@ -501,7 +495,6 @@ public class SysdynExperiment extends Experiment implements IDynamicExperiment, e.printStackTrace(); } } - progressMonitor.worked(1); if(simulationLocation != null && !canceled) { diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynSensitivityAnalysisExperiment.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynSensitivityAnalysisExperiment.java index 2a0d2c1d..abaa273e 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynSensitivityAnalysisExperiment.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynSensitivityAnalysisExperiment.java @@ -20,15 +20,19 @@ import java.util.HashMap; import java.util.concurrent.ScheduledExecutorService; import org.eclipse.core.runtime.IProgressMonitor; +import org.simantics.databoard.Bindings; +import org.simantics.db.ReadGraph; import org.simantics.db.Resource; +import org.simantics.db.common.request.ReadRequest; +import org.simantics.db.exception.DatabaseException; import org.simantics.modelica.IModelicaMonitor; -import org.simantics.modelica.ModelicaException; import org.simantics.modelica.ModelicaKeys; import org.simantics.modelica.ModelicaManager; import org.simantics.modelica.SimulationLocation; import org.simantics.modelica.data.CSVSimulationResult; import org.simantics.modelica.data.MatSimulationResult; import org.simantics.modelica.data.SimulationResult; +import org.simantics.sysdyn.SysdynResource; /** * Sensitivity analysis experiment @@ -42,113 +46,76 @@ public class SysdynSensitivityAnalysisExperiment extends SysdynExperiment { NORMAL } - private final Distribution propabilityDistribution = DEFAULT_PROPABILITY_DISTRIBUTION; - private final double min_value = DEFAULT_MIN_VALUE; - private final double max_value = DEFAULT_MAX_VALUE; - private final double num_values = DEFAULT_NUM_VALUES; + private Distribution propabilityDistribution = DEFAULT_PROPABILITY_DISTRIBUTION; + private double minValue = DEFAULT_MIN_VALUE; + private double maxValue = DEFAULT_MAX_VALUE; + private int numValues = DEFAULT_NUM_VALUES; + private String variedParameter = "Auxiliary1"; public static Distribution DEFAULT_PROPABILITY_DISTRIBUTION = Distribution.UNIFORM; public static double DEFAULT_MIN_VALUE = 1.0; public static double DEFAULT_MAX_VALUE = 3.0; public static int DEFAULT_NUM_VALUES = 3; - - private final Collection timeListeners = new ArrayList(); ScheduledExecutorService playbackExecutionService; - //SensitivityAnalysisConfiguration playbackConfiguration; private ArrayList results = null; public SysdynSensitivityAnalysisExperiment(Resource experiment, Resource model) { super(experiment, model); } - - /** - * Starts simulating a model. Call only from inside simulation job! Start simulation using simulate(true). - * @param monitor - * @param progressMonitor - * @throws IOException - */ @Override - public synchronized void simulate(final IModelicaMonitor monitor, final IProgressMonitor progressMonitor) throws IOException { - canceled = false; - - progressMonitor.subTask("Write modelica classes"); - - // Write Modelica files - - - String modelText = getModelicaCode(monitor, false, getOpenModelicaVersion()); - if(modelText == null) - return; - - progressMonitor.worked(1); - - // Write initial files and add init-parameters - progressMonitor.subTask("Write simulation files"); - HashMap experimentParameters = getExperimentParameters(monitor); - - - // add loadFile script to load all related functions and function libraries - String additionalScript = getAdditionalScripts(); - - // Create simulation files - SimulationLocation simulationLocation = createSimulationFiles(sysdynModel, modelText, experimentParameters, additionalScript, false); - progressMonitor.worked(1); - - - // Build the model and store previous model structure and inits that affect the building - // If there is no exe file OR the model structure has not changed, no need to build - boolean structureChanged = hasStructureChanged(modelText); - if (!simulationLocation.executableFile.isFile() || structureChanged) { - progressMonitor.subTask("Build model"); - buildModel(simulationLocation, modelText, monitor); - } + protected void runModelica(SimulationLocation simulationLocation, String modelText, IModelicaMonitor monitor, IProgressMonitor progressMonitor, HashMap experimentParameters, HashMap changes) throws IOException { + results = null; - // Add changed parameters in case that structure has not changed - HashMap changes = structureChanged ? null : new HashMap(); - if (omcVersion == null) { - omcVersion = ModelicaManager.getOMCVersion(simulationLocation.omcHome); + loadConfiguration(); + if (changes == null) { + changes = new HashMap(); } - - if(!structureChanged && previousParameters != null && omcVersion.startsWith("1.9")) { - try { - ModelicaManager.createFullMo(simulationLocation, monitor); - HashMap newParameters = ModelicaManager.getFullModelParameters(simulationLocation, monitor); - for(String key : previousParameters.keySet()) { - if(!previousParameters.get(key).equals(newParameters.get(key))) { - changes.put(key, newParameters.get(key)); - } - } - previousParameters = newParameters; - } catch (ModelicaException e) { - e.printStackTrace(); + if (propabilityDistribution == Distribution.UNIFORM) { + double intervalLength = (maxValue - minValue) / (numValues - 1); + for (int i = 0; i < numValues; ++i) { + double value = minValue + (intervalLength * i); + + // TODO: What if changes already include similarly named variable? + /* If the changes is altered, it affects also to the next simulation. So, + * All the parameters that are to be altered need to be defined explicitly + * before each new simulation. + */ + changes.put(variedParameter, Double.toString(value)); + + // Simulate the model for one parameter set + runModelica(simulationLocation, modelText, monitor, progressMonitor, experimentParameters, changes, i); } + } else if (propabilityDistribution == Distribution.NORMAL) { + // NOT IMPLEMENTED YET } + } - progressMonitor.worked(1); - - if(simulationLocation != null && !canceled) { - - results = null; - // Simulate the model - - /* If the changes is altered, it affects also to the next simulation. So, - * All the parameters that are to be altered need to be defined explicitly - * before each new simulation. - */ - - changes = new HashMap(); - changes.put("Auxiliary1", "444.0"); - runModelica(simulationLocation, modelText, monitor, progressMonitor, experimentParameters, changes, 0); - changes.put("Auxiliary1", "333.0"); - runModelica(simulationLocation, modelText, monitor, progressMonitor, experimentParameters, changes, 1); - changes.put("Auxiliary1", "222.0"); - runModelica(simulationLocation, modelText, monitor, progressMonitor, experimentParameters, changes, 2); + private void loadConfiguration(){ + try { + session.syncRequest(new ReadRequest() { + @Override + public void run(ReadGraph graph) throws DatabaseException { + + SysdynResource sr = SysdynResource.getInstance(graph); + maxValue = graph.getPossibleRelatedValue(experiment, sr.SensitivityAnalysisExperiment_maxValue, Bindings.DOUBLE); + minValue = graph.getPossibleRelatedValue(experiment, sr.SensitivityAnalysisExperiment_minValue, Bindings.DOUBLE); + numValues = graph.getPossibleRelatedValue(experiment, sr.SensitivityAnalysisExperiment_numValues, Bindings.INTEGER); + variedParameter = graph.getPossibleRelatedValue(experiment, sr.SensitivityAnalysisExperiment_variedParameter, Bindings.STRING); + String propabilityDistributionTemp = graph.getPossibleRelatedValue(experiment, sr.SensitivityAnalysisExperiment_propabilityDistribution, Bindings.STRING); + if (propabilityDistributionTemp.equalsIgnoreCase("uniform")) + propabilityDistribution = Distribution.UNIFORM; + else if (propabilityDistributionTemp.equalsIgnoreCase("normal")) + propabilityDistribution = Distribution.NORMAL; + else + propabilityDistribution = DEFAULT_PROPABILITY_DISTRIBUTION; + } + }); + } catch (DatabaseException e) { + // TODO Auto-generated catch block + e.printStackTrace(); } - if(canceled) - simulate(false); - process = null; } protected File renameResultFile(SimulationLocation simulationLocation, int resFileIndex) { @@ -324,114 +291,4 @@ public class SysdynSensitivityAnalysisExperiment extends SysdynExperiment { //saveResultJob.schedule(); } - //this.results.add(new MemoryResult(null, null)); - - - /** - * Interrupts a possible ongoing playback - * - * @param time - */ -// public void setTimeInterrupting(double time) { -// stopPlayback(); -// setTime(time); -// } -// -// /** -// * Sets a new time and continues playback from that point if -// * playback was running -// * @param time -// */ -// public void setTimeAndContinue(double time) { -// if(isPlaybackRunning()) { -// stopPlayback(); -// setTime(time); -// startPlayback(500); -// } else { -// setTime(time); -// } -// } -/* - private void setTime(double time) { - this.time = time; - resultsChanged(); - } - - public double getTime() { - return this.time; - } - - public double getStartTime() { - return this.startTime; - } - - public double getEndTime() { - return this.endTime; - } - - public void setPlaybackDuration(long duration) { - this.playbackDuration = duration; - if(isPlaybackRunning()) { - //Restart playback with different time settings - startPlayback(); - } - } - - public long getPlaybackDuration() { - return this.playbackDuration; - } - - @Override - public void init(ReadGraph g) { - super.init(g); - this.session = g.getSession(); - session.asyncRequest(new ReadRequest() { - - @Override - public void run(ReadGraph graph) throws DatabaseException { - changeState(ExperimentState.RUNNING); - final Resource configuration = graph.getPossibleObject(model, SimulationResource.getInstance(graph).HasConfiguration); - sysdynModel = SysdynModelManager.getInstance(session).getModel(graph, configuration); - toggleActivation(graph, true); - //getPlaybackConfiguration(graph); - startSimulationJob(); - } - }); - } - - - - private class SensitivityAnalysisConfiguration { - public double simulationDuration, simulationStepLength, intervals, endTime, startTime; - public long playbackDuration; - } - - @Override - protected void localStateChange() { - super.localStateChange(); - } - - // TIME LISTENERS - public void addTimeListener(Runnable timeListener) { - if(!this.timeListeners.contains(timeListener)) - this.timeListeners.add(timeListener); - } - - public Collection getTimeListeners() { - return this.timeListeners; - } - - public void removeTimeListener(Runnable timeListener) { - this.timeListeners.remove(timeListener); - } - - @Override - public void resultsChanged() { - for(Runnable listener : timeListeners) { - listener.run(); - } - super.resultsChanged(); - - } -*/ }