From: lempinen Date: Mon, 9 Aug 2010 06:22:23 +0000 (+0000) Subject: Tolerance -option for simulations X-Git-Tag: simantics-1.2.0~128 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=f8a6bcccb00253207df7de21d8dffd69d3471da3;p=simantics%2Fsysdyn.git Tolerance -option for simulations git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@17032 ac1ea38d-2e2b-0410-8846-a27921b304fc --- diff --git a/org.simantics.modelica/src/org/simantics/modelica/ModelicaManager.java b/org.simantics.modelica/src/org/simantics/modelica/ModelicaManager.java index 1eb99156..198dde1e 100644 --- a/org.simantics.modelica/src/org/simantics/modelica/ModelicaManager.java +++ b/org.simantics.modelica/src/org/simantics/modelica/ModelicaManager.java @@ -26,7 +26,7 @@ public class ModelicaManager { dir = "c:/OpenModelica1.5.0"; return new File(dir); } - + protected static File createTempDirectory() throws IOException { final File temp = File.createTempFile("temp", Long.toString(System.nanoTime())); if(!(temp.delete())) @@ -35,7 +35,7 @@ public class ModelicaManager { throw new IOException("Could not create temp directory: " + temp.getAbsolutePath()); return (temp); } - + protected static boolean recursiveDelete(File fileOrDir) { if(fileOrDir.isDirectory()) for(File innerFile: fileOrDir.listFiles()) @@ -43,7 +43,7 @@ public class ModelicaManager { return false; return fileOrDir.delete(); } - + protected static String readFile(File file) throws IOException { InputStream stream = new FileInputStream(file); byte[] buffer = new byte[(int)file.length()]; @@ -51,7 +51,7 @@ public class ModelicaManager { stream.close(); return new String(buffer); } - + protected static void printProcessOutput(Process process, IModelicaMonitor monitor) throws IOException { InputStream stream = process.getInputStream(); StringBuilder b = new StringBuilder(); @@ -62,19 +62,19 @@ public class ModelicaManager { if((char)c != '\n') b.append((char)c); else { - System.out.println("OMC output: " + b.toString()); + System.out.println("OMC output: " + b.toString()); monitor.message(b.toString()); b.delete(0, b.length()); } } } - + static class SimulationLocation { File simulationDir; File inputFile; File outputFile; File initFile; - + public SimulationLocation(File simulationDir, File inputFile, File outputFile, File initFile) { this.simulationDir = simulationDir; @@ -83,42 +83,45 @@ public class ModelicaManager { this.initFile = initFile; } } - - protected static SimulationLocation createInputFiles(String modelName, String modelText, Double startTime, Double stopTime) throws IOException { + + protected static SimulationLocation createInputFiles(String modelName, String modelText, Double startTime, Double stopTime, Double tolerance) throws IOException { File simulationDir = createTempDirectory(); modelName = modelName.replace(" ", ""); File modelFile = new File(simulationDir, modelName + ".mo"); File scriptFile = new File(simulationDir, modelName + ".mos"); - + { PrintStream s = new PrintStream(modelFile); s.print(modelText); s.close(); } - + { PrintStream s = new PrintStream(scriptFile); s.println("loadFile(\"" + modelName + ".mo\");"); - s.println("simulate("+modelName+ + s.print("simulate("+modelName+ ",startTime="+startTime+ - ",stopTime="+stopTime+ - ");"); + ",stopTime="+stopTime); + if(tolerance != null) { + s.print(",tolerance="+tolerance); + } + s.print(");\n"); s.close(); } - + return new SimulationLocation( simulationDir, new File(simulationDir, modelName + ".mos"), new File(simulationDir, modelName + "_res.plt"), new File(simulationDir, modelName + "_init.txt") - ); + ); } - + public static SimulationResult runModelica(String modelName, String modelText, - IModelicaMonitor monitor, Double startTime, Double stopTime) throws IOException { + IModelicaMonitor monitor, Double startTime, Double stopTime, Double tolerance) throws IOException { SimulationLocation simulationLocation = - createInputFiles(modelName, modelText, startTime, stopTime); - + createInputFiles(modelName, modelText, startTime, stopTime, tolerance); + try { File modelicaHome = getOpenModelicaHome(); File omc = new File(modelicaHome, "bin/omc.exe"); @@ -126,17 +129,17 @@ public class ModelicaManager { Process process = new ProcessBuilder( omc.getAbsolutePath(), simulationLocation.inputFile.getAbsolutePath() - ) - .directory(simulationLocation.simulationDir.getAbsoluteFile()) - .redirectErrorStream(true) - .start(); + ) + .directory(simulationLocation.simulationDir.getAbsoluteFile()) + .redirectErrorStream(true) + .start(); printProcessOutput(process, monitor); - + SimulationResult result = new SimulationResult(); result.read(simulationLocation.outputFile); result.readInits(simulationLocation.initFile); result.filter(); - + return result; } catch(IOException e) { e.printStackTrace(); diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/SysdynResource.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/SysdynResource.java index f2c110f9..1794aa36 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/SysdynResource.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/SysdynResource.java @@ -67,6 +67,7 @@ public class SysdynResource { public final Resource HasStopTime; public final Resource HasTail; public final Resource HasTailTerminal; + public final Resource HasTolerance; public final Resource HasUnit; public final Resource HasX; public final Resource HasY; @@ -155,6 +156,7 @@ public class SysdynResource { public static final String HasStopTime = "http://www.simantics.org/Sysdyn-1.0/HasStopTime"; public static final String HasTail = "http://www.simantics.org/Sysdyn-1.0/HasTail"; public static final String HasTailTerminal = "http://www.simantics.org/Sysdyn-1.0/HasTailTerminal"; + public static final String HasTolerance = "http://www.simantics.org/Sysdyn-1.0/HasTolerance"; public static final String HasUnit = "http://www.simantics.org/Sysdyn-1.0/HasUnit"; public static final String HasX = "http://www.simantics.org/Sysdyn-1.0/HasX"; public static final String HasY = "http://www.simantics.org/Sysdyn-1.0/HasY"; @@ -253,6 +255,7 @@ public class SysdynResource { HasStopTime = getResourceOrNull(graph, URIs.HasStopTime); HasTail = getResourceOrNull(graph, URIs.HasTail); HasTailTerminal = getResourceOrNull(graph, URIs.HasTailTerminal); + HasTolerance = getResourceOrNull(graph, URIs.HasTolerance); HasUnit = getResourceOrNull(graph, URIs.HasUnit); HasX = getResourceOrNull(graph, URIs.HasX); HasY = getResourceOrNull(graph, URIs.HasY); diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynModel.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynModel.java index dfb8d396..c8accfab 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynModel.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynModel.java @@ -137,7 +137,8 @@ public class SysdynModel implements IMappingListener, IModel { writer.toString(), monitor, configuration.startTime, - configuration.stopTime); + configuration.stopTime, + configuration.tolerance); sysdynResult.setResult(result); } catch(Exception e) { e.printStackTrace(); diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Configuration.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Configuration.java index 77e632bf..87e6fbe0 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Configuration.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Configuration.java @@ -30,6 +30,9 @@ public class Configuration { @RelatedValue("http://www.simantics.org/Sysdyn-1.0/HasStopTime") public Double stopTime = 10.0; + @RelatedValue("http://www.simantics.org/Sysdyn-1.0/HasTolerance") + public Double tolerance; + @RelatedValue("http://www.simantics.org/Layer0-1.0/HasName") private String name; diff --git a/sysdyn_ontologies/sysdyn.graph b/sysdyn_ontologies/sysdyn.graph index 0838d09f..011c3ff2 100644 --- a/sysdyn_ontologies/sysdyn.graph +++ b/sysdyn_ontologies/sysdyn.graph @@ -82,6 +82,7 @@ SysdynModel 0 then 0 else WorkAllocation" : L0.String + HasEquation "if ProjectIsReady < 1 then WorkAllocation else 0" : L0.String ErrorsGenerated @ "ErrorsGenerated" : Valve tag(MOD.Mapped) HasExpression _ : NormalExpression @@ -696,6 +729,12 @@ WorkModuleConfiguration @ "WorkModuleConfiguration" : Configuration tag(MOD.Mapped) HasExpression _ : NormalExpression HasEquation "Errors/ErrorsFoundTime" : L0.String + ProjectReadyness @ "ProjectReadyness" : Valve + tag(MOD.Mapped) + HasExpression _ : NormalExpression + HasEquation "(xidz(first=WorkDone, second=ProjectWorkAmount, x=0.0) - ProjectIsReady) / 0.08" : L0.String + + ## Stocks WorkToDo @ "WorkToDo" : Stock @@ -706,10 +745,15 @@ WorkModuleConfiguration @ "WorkModuleConfiguration" : Configuration tag(MOD.Mapped) HasExpression _ : StockExpression HasInitialEquation "0" : L0.String + tag(IsOutput) Errors @ "Errors" : Stock tag(MOD.Mapped) HasExpression _ : StockExpression - HasInitialEquation "0" : L0.String + HasInitialEquation "0" : L0.String + ProjectIsReady @ "ProjectIsReady" : Stock + tag(MOD.Mapped) + HasExpression _ : StockExpression + HasInitialEquation "0" : L0.String ## Auxiliaries TimeToDeadline @ "TimeToDeadline" : Auxiliary @@ -720,23 +764,15 @@ WorkModuleConfiguration @ "WorkModuleConfiguration" : Configuration tag(MOD.Mapped) tag(IsOutput) HasExpression _ : NormalExpression - HasEquation "if ProjectIsReady > 0 then 0 else if TimeToDeadline < 0.0 or TimeToDeadline > 0.0 then WorkToDo / TimeToDeadline else MaximumWorkingSpeed" : L0.String + HasEquation "if ProjectIsReady < 1 then if TimeToDeadline > 0.0 then min(MaximumWorkingSpeed, WorkToDo / TimeToDeadline) else MaximumWorkingSpeed else 0" : L0.String WorkAllocation @ "WorkAllocation" : Auxiliary tag(MOD.Mapped) HasExpression _ : NormalExpression HasEquation "xidz(first = RequiredWorkingSpeed, second = RequiredWorkingSpeed + RequiredWorkingSpeedOtherProjectsInput, x = 0.0) * PossibleWorkingSpeedInput" : L0.String - ProjectIsReady @ "ProjectIsReady" : Auxiliary - tag(MOD.Mapped) - HasExpression _ : NormalExpression - HasEquation "if ProjectReadyness >= 1 then 1 else 0" : L0.String - ProjectReadyness @ "ProjectReadyness" : Auxiliary - tag(MOD.Mapped) - HasExpression _ : NormalExpression - HasEquation "WorkDone/ProjectWorkAmount" : L0.String ErrorsFoundTime @ "ErrorsFoundTime" : Auxiliary tag(MOD.Mapped) HasExpression _ : WithLookupExpression - HasEquation "ProjectReadyness" : L0.String + HasEquation "xidz(first=WorkDone, second=ProjectWorkAmount, x=0.0)" : L0.String HasLookup "{{0,5},{0.5,3},{1,0.5},{2,0.5}}" : L0.String ## Inputs @@ -745,9 +781,13 @@ WorkModuleConfiguration @ "WorkModuleConfiguration" : Configuration PossibleWorkingSpeedInput @ "PossibleWorkingSpeedInput" : Input tag(MOD.Mapped) RequiredWorkingSpeedOtherProjectsInput @ "RequiredWorkingSpeedOtherProjectsInput" : Input - tag(MOD.Mapped) + tag(MOD.Mapped) + ProjectWorkAmount @ "ProjectWorkAmount" : Input + tag(MOD.Mapped) + HasDefaultInputValue "1000.0" : L0.Double c31 : Cloud + c33 : Cloud d31 : Dependency conf_dep(TimeToDeadline, WorkCompletionTimeInput) @@ -764,9 +804,7 @@ WorkModuleConfiguration @ "WorkModuleConfiguration" : Configuration d37 : Dependency conf_dep(WorkAllocation, PossibleWorkingSpeedInput) d38 : Dependency - conf_dep(WorkAllocation, RequiredWorkingSpeed) - d39 : Dependency - conf_dep(ProjectIsReady, ProjectReadyness) + conf_dep(WorkAllocation, RequiredWorkingSpeed) d40 : Dependency conf_dep(ProjectReadyness, WorkDone) d41 : Dependency @@ -784,7 +822,11 @@ WorkModuleConfiguration @ "WorkModuleConfiguration" : Configuration d47 : Dependency conf_dep(ErrorsFoundRate, ErrorsFoundTime) d48 : Dependency - conf_dep(ErrorsFoundTime, ProjectReadyness) + conf_dep(ErrorsFoundTime, ProjectWorkAmount) + d49 : Dependency + conf_dep(ErrorsFoundTime, WorkDone) + + f31 : Flow conf_dep(WorkingSpeed, WorkToDo) f32 : Flow @@ -799,6 +841,10 @@ WorkModuleConfiguration @ "WorkModuleConfiguration" : Configuration conf_dep(ErrorsFoundRate, WorkDone) f37 : Flow conf_dep(WorkToDo, ErrorsFoundRate) + f40 : Flow + conf_dep(ProjectIsReady, ProjectReadyness) + f41 : Flow + conf_dep(ProjectReadyness, c33) WorkModuleConfigurationDiagram : ConfigurationDiagram