Преглед на файлове

rollback without need for rollback implemented. Also hard reset.

Claudio Gomes преди 6 години
родител
ревизия
59964670d8

+ 1 - 1
HintCOEngine/instances/elevator_load_scenario.hintco

@@ -1,6 +1,6 @@
 <?xml version="1.0" encoding="ASCII"?>
 <hintco:HintConfiguration xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:hintco="ua.ansymo.hintco">
-  <candidates identifier="Original" stopTime="3.0" stepSize="0.001" outputStepSize="0.01" maxInitIterations="30">
+  <candidates identifier="Original" stopTime="1.0" stepSize="1.0E-5" outputStepSize="0.01" maxInitIterations="30">
     <cosimunits xsi:type="hintco:CosimUnitInstance" identifier="load" declaration="//@csuDeclarations.0">
       <ports xsi:type="hintco:OutputPortInstance" identifier="T_load@expseu_" valueTo="//@candidates.0/@cosimunits.1/@ports.2"/>
     </cosimunits>

+ 5 - 2
HintCOEngine/src/ua/ansymo/hintco/AdaptiveCosimRunner.xtend

@@ -20,9 +20,12 @@ class AdaptiveCosimRunner implements ICosimRunner, Closeable {
 	
 	protected ICosimRunner internalRunner
 	
-	new (IOutputProcessor o, IFmuLoader l){
+	boolean hardReset
+	
+	new (IOutputProcessor o, IFmuLoader l, boolean hR){
 		outProcessor = o
 		fmuLoader = l
+		hardReset = hR
 	}
 	
 	override run(RootCandidateScenario rootScenario, Map<Scenario, List<PrecendenceNode>> scenarioNodesMap, String variantID) {
@@ -32,7 +35,7 @@ class AdaptiveCosimRunner implements ICosimRunner, Closeable {
 		
 		if (nodes.filter(InputPortInstance).filter[it.isInput].exists[it.adaptation instanceof RollbackInterpolationAdaptation]){
 			logger.debug("AdaptiveCosimRunner using string coupling.")
-			this.internalRunner = new StrongCouplingRunner(outProcessor, fmuLoader)
+			this.internalRunner = new StrongCouplingRunner(outProcessor, fmuLoader, hardReset)
 		} else {
 			logger.debug("AdaptiveCosimRunner using single runner.")
 			this.internalRunner = new SingleCosimRunner(outProcessor, fmuLoader)

+ 16 - 2
HintCOEngine/src/ua/ansymo/hintco/CosimRunUtils.xtend

@@ -1,9 +1,11 @@
 package ua.ansymo.hintco
 
+import java.io.File
 import java.util.HashMap
 import java.util.List
 import java.util.Map
 import java.util.Set
+import org.eclipse.core.runtime.Assert
 import org.intocps.fmi.Fmi2Status
 import org.intocps.fmi.FmuInvocationException
 import org.slf4j.Logger
@@ -75,9 +77,21 @@ class CosimRunUtils {
 		}
 	}
 	
-	def static resetUnits(List<UnitInstance> units, Map<UnitInstance, IFmuInstance> fmuInstanceMap) {
+	def static resetUnits(List<UnitInstance> units, Map<UnitInstance, IFmuInstance> fmuInstanceMap, IFmuLoader fmuLoader, Map<Scenario, List<PrecendenceNode>> scenarioNodesMap, boolean hardReset) {
 		for (unit : units) {
-			fmuInstanceMap.get(unit).reset()
+			if (hardReset){
+				// Terminate Unit
+				fmuInstanceMap.get(unit).free()
+				
+				// Create new instance
+				val fmuInstance = fmuLoader.instantiate(unit, scenarioNodesMap)
+				logger.debug("Unit {} instantiated", unit)
+				
+				// Replace the old unit in the map
+				fmuInstanceMap.put(unit, fmuInstance)
+			} else {
+				fmuInstanceMap.get(unit).reset()				
+			}
 			logger.debug("Unit {} free.", unit)
 		}
 	}

+ 2 - 1
HintCOEngine/src/ua/ansymo/hintco/HierarchicalInstance.xtend

@@ -136,7 +136,8 @@ class HierarchicalInstance extends AdaptedFMUInstance {
 	}
 	
 	override reset() {
-		resetUnits(units, fmuInstanceMap)
+		// This reset is never called in the case of hard reset coming from parent scenarios.
+		resetUnits(units, fmuInstanceMap, null, null, false)
 	}
 	
 }

+ 1 - 1
HintCOEngine/src/ua/ansymo/hintco/Main.xtend

@@ -88,7 +88,7 @@ class Main implements IApplication {
 			spaceGenerator.createCandidateSpace(hints, src)
 		}
 		
-		val runner = new AdaptiveCosimRunner(new OutputProcessor(outDir), new FmuLoader)
+		val runner = new AdaptiveCosimRunner(new OutputProcessor(outDir), new FmuLoader, false)
 		val generator = new CandidatesGenerator(new ConstraintChecker,new VariantValidator, new VariantProcessor(runner))
 		
 		generator.createVariantTree(src)

+ 9 - 5
HintCOEngine/src/ua/ansymo/hintco/StrongCouplingRunner.xtend

@@ -43,9 +43,12 @@ class StrongCouplingRunner implements ICosimRunner {
 	protected Logger logger = LoggerFactory.getLogger(typeof(StrongCouplingRunner))
 	protected IFmuLoader fmuLoader
 	
-	new (IOutputProcessor o, IFmuLoader l){
+	protected boolean hardReset
+	
+	new (IOutputProcessor o, IFmuLoader l, boolean hardReset){
 		outProcessor = o
 		fmuLoader = l
+		this.hardReset = hardReset
 	}
 	
 	override run(RootCandidateScenario rootScenario, Map<Scenario, List<PrecendenceNode>> scenarioNodesMap, String variantID) {
@@ -80,7 +83,7 @@ class StrongCouplingRunner implements ICosimRunner {
 			// run co-simulation up to step convergedStep and converge a new step.
 			convergeStep(stepToConverge, units, fmuInstanceMap, childInputPorts, 
 						childOutputPorts, rollbackOutputPorts, cosimSignals, rootScenario,
-						nodes)
+						nodes, scenarioNodesMap)
 			
 			logger.debug("String coupling iteration for step {} terminated.", stepToConverge)
 			stepToConverge=stepToConverge+1		
@@ -107,7 +110,8 @@ class StrongCouplingRunner implements ICosimRunner {
 						Set<OutputPortInstance> rollbackOutputPorts, 
 						HashMap<OutputPortInstance, Double>[] cosimSignals,
 						RootCandidateScenario rootScenario,
-						List<PrecendenceNode> nodes
+						List<PrecendenceNode> nodes,
+						Map<Scenario, List<PrecendenceNode>> scenarioNodesMap
 	) {
 		if (stepToConverge == 0){
 			// Initialize cosim and record first outputs.
@@ -127,7 +131,7 @@ class StrongCouplingRunner implements ICosimRunner {
 			logger.debug("Output line recorded for time {} and step {}.", 0.0, 0.0)
 			
 			exitInitMode(units, fmuInstanceMap)
-			resetUnits(units, fmuInstanceMap)
+			resetUnits(units, fmuInstanceMap, fmuLoader, scenarioNodesMap, hardReset)
 		} else {
 			val convergedStep = stepToConverge-1
 			
@@ -148,7 +152,7 @@ class StrongCouplingRunner implements ICosimRunner {
 												rootScenario,
 												nodes)
 				
-				resetUnits(units, fmuInstanceMap)
+				resetUnits(units, fmuInstanceMap, fmuLoader, scenarioNodesMap, hardReset)
 				
 				cosimCounter = cosimCounter + 1
 				

+ 14 - 12
HintCOEngine/test/ua/ansymo/hintco/test/WvFormCosimRunnerTest.xtend

@@ -2,6 +2,7 @@ package ua.ansymo.hintco.test
 
 import org.apache.commons.lang3.SystemUtils
 import org.junit.Assume
+import org.junit.Ignore
 import org.junit.Test
 import ua.ansymo.hintco.AdaptiveCosimRunner
 import ua.ansymo.hintco.CandidatesGenerator
@@ -10,6 +11,7 @@ import ua.ansymo.hintco.FmuLoader
 import ua.ansymo.hintco.ModelStorage
 import ua.ansymo.hintco.OutputProcessor
 import ua.ansymo.hintco.SingleCosimRunner
+import ua.ansymo.hintco.StrongCouplingRunner
 import ua.ansymo.hintco.VariantProcessor
 import ua.ansymo.hintco.VariantValidator
 
@@ -17,14 +19,14 @@ class WvFormCosimRunnerTest {
 	@Test
 	def void executeWVCosimMSD(){
 		val src = new ModelStorage().loadCandidates("instances/waveform_msd.hintco")
-		val runner = new AdaptiveCosimRunner(new OutputProcessor("results-gen/strongCoupMSDTest/Strong"), new FmuLoader)
+		val runner = new AdaptiveCosimRunner(new OutputProcessor("results-gen/strongCoupMSDTest/Strong"), new FmuLoader, false)
 		var generator = new CandidatesGenerator(new ConstraintChecker,new VariantValidator, new VariantProcessor(runner))
 		generator.createVariantTree(src)
 		generator.generateVariants(src, 1)
 		runner.close()
 		
 		val normal_src = new ModelStorage().loadCandidates("instances/waveform_msd_normal.hintco")
-		val normal_runner = new AdaptiveCosimRunner(new OutputProcessor("results-gen/strongCoupMSDTest/Normal"), new FmuLoader)
+		val normal_runner = new AdaptiveCosimRunner(new OutputProcessor("results-gen/strongCoupMSDTest/Normal"), new FmuLoader, false)
 		var normal_generator = new CandidatesGenerator(new ConstraintChecker,new VariantValidator, new VariantProcessor(normal_runner))
 		normal_generator.createVariantTree(normal_src)
 		normal_generator.generateVariants(normal_src, 1)
@@ -32,23 +34,23 @@ class WvFormCosimRunnerTest {
 	}
 	
 	@Test
-	//@Ignore // Only works if you have amesim license.
+	@Ignore // Only works if you have amesim license.
 	def void executeCosimulationElevatorLoad(){
 		Assume.assumeTrue(SystemUtils.IS_OS_WINDOWS) // Because it only works on windows.
 		
-//		val src = new ModelStorage().loadCandidates("instances/elevator_load_scenario.hintco")
-//		val runner = new SingleCosimRunner(new OutputProcessor("results-gen/elevator"), new FmuLoader)
-//		var generator = new CandidatesGenerator(new ConstraintChecker,new VariantValidator, new VariantProcessor(runner))
-//		generator.createVariantTree(src)
-//		generator.generateVariants(src, 1)
-//		runner.close()
+		val src = new ModelStorage().loadCandidates("instances/elevator_load_scenario.hintco")
+		val runner = new SingleCosimRunner(new OutputProcessor("results-gen/elevator"), new FmuLoader)
+		var generator = new CandidatesGenerator(new ConstraintChecker,new VariantValidator, new VariantProcessor(runner))
+		generator.createVariantTree(src)
+		generator.generateVariants(src, 1)
+		runner.close()
 		
 //		System.setProperty("org.slf4j.simpleLogger.defaultLogLevel", "debug")
-		System.setProperty("org.slf4j.simpleLogger.logFile", "elevator.log")
+//		System.setProperty("org.slf4j.simpleLogger.logFile", "elevator.log")
 		
 		val strong_src = new ModelStorage().loadCandidates("instances/elevator_load_scenario.hintco")
-		val strong_runner = new AdaptiveCosimRunner(new OutputProcessor("results-gen/elevatorStrong"), new FmuLoader)
-		var strong_generator = new CandidatesGenerator(new ConstraintChecker,new VariantValidator, new VariantProcessor(strong_runner))
+		val strong_runner = new StrongCouplingRunner(new OutputProcessor("results-gen/elevatorStrong"), new FmuLoader, true)
+		var strong_generator = new CandidatesGenerator(new ConstraintChecker, new VariantValidator, new VariantProcessor(strong_runner))
 		strong_generator.createVariantTree(strong_src)
 		strong_generator.generateVariants(strong_src, 1)
 		strong_runner.close()