Browse Source

Added more efficient simulation algorithm

Yentl Van Tendeloo 7 years ago
parent
commit
ff12061a94
1 changed files with 14 additions and 10 deletions
  1. 14 10
      models/cbd_simulate.alc

+ 14 - 10
models/cbd_simulate.alc

@@ -21,6 +21,15 @@ Boolean function main(model : Element):
 	schedule_init = create_schedule(model)
 	schedule_run = read_root()
 
+	Element nodes
+	Element inputs
+	String node
+	nodes = allInstances(model, "FullRuntime/Block")
+	inputs = dict_create()
+	while (set_len(nodes) > 0):
+		node = set_pop(nodes)
+		dict_add(inputs, node, allAssociationOrigins(model, node, "FullRuntime/Link"))
+
 	while (bool_not(has_input())):
 		if (read_attribute(model, time, "start_time") == read_attribute(model, time, "current_time")):
 			schedule = schedule_init
@@ -28,7 +37,7 @@ Boolean function main(model : Element):
 			if (element_eq(schedule_run, read_root())):
 				schedule_run = create_schedule(model)
 			schedule = schedule_run
-		current_time = step_simulation(model, schedule, current_time)
+		current_time = step_simulation(model, schedule, current_time, inputs)
 
 	instantiate_attribute(model, time, "current_time", current_time)
 	output("CLOSE")
@@ -277,7 +286,7 @@ Integer function list_index_of(lst : Element, elem : Element):
 			i = i + 1
 	return -1!
 
-Float function step_simulation(model : Element, schedule : Element, time : Float):
+Float function step_simulation(model : Element, schedule : Element, time : Float, inputs : Element):
 	Float signal
 	Element incoming
 	String selected
@@ -306,29 +315,26 @@ Float function step_simulation(model : Element, schedule : Element, time : Float
 
 			// Execute "block"
 			blocktype = read_type(model, block)
+			incoming = set_copy(inputs[block])
 			if (blocktype == "FullRuntime/ConstantBlock"):
 				signal = read_attribute(model, block, "value")
 			elif (blocktype == "FullRuntime/AdditionBlock"):
 				signal = 0.0
-				incoming = allAssociationOrigins(model, block, "FullRuntime/Link")
 				while (set_len(incoming) > 0):
 					selected = set_pop(incoming)
 					signal = signal + cast_float(read_attribute(model, selected, "signal"))
 			elif (blocktype == "FullRuntime/MultiplyBlock"):
 				signal = 1.0
-				incoming = allAssociationOrigins(model, block, "FullRuntime/Link")
 				while (set_len(incoming) > 0):
 					selected = set_pop(incoming)
 					signal = signal * cast_float(read_attribute(model, selected, "signal"))
 			elif (blocktype == "FullRuntime/NegatorBlock"):
 				signal = 0.0
-				incoming = allAssociationOrigins(model, block, "FullRuntime/Link")
 				while (set_len(incoming) > 0):
 					selected = set_pop(incoming)
 					signal = float_neg(cast_float(read_attribute(model, selected, "signal")))
 			elif (blocktype == "FullRuntime/InverseBlock"):
 				signal = 0.0
-				incoming = allAssociationOrigins(model, block, "FullRuntime/Link")
 				while (set_len(incoming) > 0):
 					selected = set_pop(incoming)
 					signal = float_division(1.0, cast_float(read_attribute(model, selected, "signal")))
@@ -362,13 +368,11 @@ Float function step_simulation(model : Element, schedule : Element, time : Float
 						selected = set_pop(incoming)
 						signal = cast_float(read_attribute(model, selected, "signal"))
 				else:
-					incoming = allAssociationOrigins(model, block, "FullRuntime/Link")
 					while (set_len(incoming) > 0):
 						selected = set_pop(incoming)
 						signal = (cast_float(read_attribute(model, selected, "signal")) - cast_float(read_attribute(model, block, "last_in"))) / delta_t
 				set_add(memory_blocks, block)
 			elif (blocktype == "FullRuntime/ProbeBlock"):
-				incoming = allAssociationOrigins(model, block, "FullRuntime/Link")
 				while (set_len(incoming) > 0):
 					signal = cast_float(read_attribute(model, set_pop(incoming), "signal"))
 					output(cast_string(time) + " " + cast_string(read_attribute(model, block, "name")) + " " + cast_string(signal))
@@ -378,9 +382,9 @@ Float function step_simulation(model : Element, schedule : Element, time : Float
 	while (set_len(memory_blocks) > 0):
 		block = set_pop(memory_blocks)
 		// Update memory
-		incoming = allIncomingAssociationInstances(model, block, "FullRuntime/Link")
+		incoming = set_copy(inputs[block])
 		while (set_len(incoming) > 0):
-			selected = readAssociationSource(model, set_pop(incoming))
+			selected = set_pop(incoming)
 			instantiate_attribute(model, block, "last_in", cast_float(read_attribute(model, selected, "signal")))
 
 	// Increase simulation time