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

Prepared for better C code gen

Randy Paredis преди 3 години
родител
ревизия
f3a3075a0f
променени са 5 файла, в които са добавени 86 реда и са изтрити 48 реда
  1. 38 30
      src/CBD/converters/CBD2C/__init__.py
  2. 0 14
      src/CBD/converters/CBD2C/example.c
  3. 17 4
      src/CBD/linearsolver.py
  4. 2 0
      src/CBD/simulator.py
  5. 29 0
      src/test/TEST.gv

+ 38 - 30
src/CBD/converters/CBD2C/__init__.py

@@ -142,6 +142,7 @@ class CBD2C:
 							break
 			else:  # ALGEBRAIC LOOP!
 				self.solver.checkValidity(self.model.getPath("."), c)
+				known = set()
 				other = []
 				for block in c:
 					path = block.getPath("_")
@@ -150,39 +151,46 @@ class CBD2C:
 						prev = block.getBlockConnectedToInput(inp).block
 						for e in eqs:
 							if e.startswith(p):
-								if prev in c:
-									other.append(e)
-								else:
+								if prev not in c:
 									c_order.insert(0, e)
-
+									known.add(p)
+				print(known)
+				mtx = []
+				for block in c:
+					pass
 				# TODO: use inputs over hardcoded numbers?
+				#       1. for each block, add the external connections (that are not in the loop)
+				#       2. for all blocks, list the interaction that is detected as a dict
+				#       3. for all connections, list the interaction as a dict
+				#       4. for all dicts, list the unique columns, w/o external conns
+				#       5. create a matrix from the dicts
 				# Get matrix representation
-				m1, m2 = self.solver.constructInput(c, 1 if curIt == "i" else curIt)
-				mat = []
-				for r in range(m1.rows):
-					row = []
-					for col in range(m1.cols):
-						row.append(float(m1[r, col]))
-					row.append(float(m2[r]))
-					mat.append(row)
-
-				deps = {}
-				for b in c:
-					D = [b.getBlockConnectedToInput(x) for x in b.getInputPortNames()]
-					deps[b] = [b.block.getPath('_') + '_' + b.output_port for b in D if b.block in c]
-
-				if len(mat) == 1:
-					nc = ([deps[c[mat[0].index(1)]]], mat[0][-1])
-				else:
-					dlist = []
-					for b in c:
-						for d in deps[b]:
-							if d not in dlist:
-								dlist.append(d)
-
-					nc = (dlist, str(mat).replace("[", "{").replace("]", "}"))
-
-				c_order.append(nc)
+				# m1, m2 = self.solver.constructInput(c, 1 if curIt == "i" else curIt)
+				# mat = []
+				# for r in range(m1.rows):
+				# 	row = []
+				# 	for col in range(m1.cols):
+				# 		row.append(float(m1[r, col]))
+				# 	row.append(float(m2[r]))
+				# 	mat.append(row)
+				#
+				# deps = {}
+				# for b in c:
+				# 	D = [b.getBlockConnectedToInput(x) for x in b.getInputPortNames()]
+				# 	deps[b] = [b.block.getPath('_') + '_' + b.output_port for b in D if b.block in c]
+				#
+				# if len(mat) == 1:
+				# 	nc = ([deps[c[mat[0].index(1)]]], mat[0][-1])
+				# else:
+				# 	dlist = []
+				# 	for b in c:
+				# 		for d in deps[b]:
+				# 			if d not in dlist:
+				# 				dlist.append(d)
+				#
+				# 	nc = (dlist, str(mat).replace("[", "{").replace("]", "}"))
+				#
+				# c_order.append(nc)
 				c_order += other
 			res_order = c_order + res_order
 		return res_order

+ 0 - 14
src/CBD/converters/CBD2C/example.c

@@ -30,13 +30,6 @@ void _eq_init(const double time, const double delta) {
     test_seven_OUT1[0] = 7;
     test_sum_IN1[0] = test_three_OUT1[0];
     test_mult_IN1[0] = test_seven_OUT1[0];
-
-    /* Algebraic Loop */
-    double _C4[2][3] = {{-1.0, 7.0, 0.0}, {1.0, -1.0, -3.0}};
-    agloop(&_C4, 2, &test_sum_OUT1[0], &test_mult_OUT1[0]);
-
-    test_mult_IN2[0] = test_sum_OUT1[0];
-    test_sum_IN2[0] = test_mult_OUT1[0];
     test_x_IN1[0] = test_sum_OUT1[0];
     test_x_OUT1[0] = test_x_IN1[0];
     test_x[0] = test_x_OUT1[0];
@@ -50,13 +43,6 @@ void _eq_iter(const unsigned int i, const double time, const double delta) {
     test_seven_OUT1[i] = 7;
     test_sum_IN1[i] = test_three_OUT1[i];
     test_mult_IN1[i] = test_seven_OUT1[i];
-
-    /* Algebraic Loop */
-    double _C4[2][3] = {{-1.0, 7.0, 0.0}, {1.0, -1.0, -3.0}};
-    agloop(&_C4, 2, &test_sum_OUT1[i], &test_mult_OUT1[i]);
-
-    test_mult_IN2[i] = test_sum_OUT1[i];
-    test_sum_IN2[i] = test_mult_OUT1[i];
     test_x_IN1[i] = test_sum_OUT1[i];
     test_x_OUT1[i] = test_x_IN1[i];
     test_x[i] = test_x_OUT1[i];

+ 17 - 4
src/CBD/linearsolver.py

@@ -118,6 +118,21 @@ class LinearSolver(Solver):
 			- M1: coefficient matrix, where each row represents an equation of the system
 			- M2: result matrix, where each element is the result for the corresponding equation in M1
 		"""
+		sep = '.'
+		known = {}
+		for block in strongComponent:
+			for inp, b in block.getLinksIn().items():
+				if b.block not in strongComponent:
+					val = b.block.getSignal(b.output_port)[curIteration].value
+					known[block.getPath(sep) + sep + inp] = val
+					known[b.block.getPath(sep)] = val
+		return self.get_matrix(strongComponent, sep, known)
+
+	@staticmethod
+	def get_matrix(strongComponent, sep='.', known=None):
+		if known is None:
+			known = {}
+
 		# Initialize matrices with zeros
 		size = len(strongComponent)
 		M1 = Matrix(size, size)
@@ -139,7 +154,7 @@ class LinearSolver(Solver):
 		for i, block in enumerate(strongComponent):
 			if block.getBlockType() == "AdderBlock":
 				for external in [x for x in getBlockDependencies2(block) if x not in strongComponent]:
-					M2[i] -= external.getSignal()[curIteration].value
+					M2[i] -= known[external.getPath(sep)]
 				M1[i, i] = -1
 
 				for compInStrong in [x for x in getBlockDependencies2(block) if x in strongComponent]:
@@ -149,7 +164,7 @@ class LinearSolver(Solver):
 				M1[i, i] = -1
 				fact = 1
 				for external in [x for x in getBlockDependencies2(block) if x not in strongComponent]:
-					fact *= external.getSignal()[curIteration].value
+					fact *= known[external.getPath(sep)]
 				for compInStrong in [x for x in getBlockDependencies2(block) if x in strongComponent]:
 					M1[i, indexdict[compInStrong]] += fact
 			elif block.getBlockType() == "NegatorBlock":
@@ -172,8 +187,6 @@ class LinearSolver(Solver):
 				M1[i, indexdict[dblock]] = - 1
 			elif block.getBlockType() == "DelayBlock":
 				# If a delay is in a strong component, this is the first iteration
-				# FIXME: turn this into a normal error?
-				assert curIteration == 0
 				# And so the dependency is the IC
 				# M2 can stay 0 because we have an equation of the type -x = -ic <=> -x + ic = 0
 				M1[i, i] = -1

+ 2 - 0
src/CBD/simulator.py

@@ -555,6 +555,8 @@ class Simulator:
 				self.__solver.checkValidity(self.model.getPath(), component)
 				solverInput = self.__solver.constructInput(component, curIteration)
 				solutionVector = self.__solver.solve(solverInput)
+				# print(solutionVector)
+				# print(component)
 				for block in component:
 					if curIteration == 0 or self.__scheduler.mustCompute(block, self.getTime()):
 						blockIndex = component.index(block)

+ 29 - 0
src/test/TEST.gv

@@ -0,0 +1,29 @@
+// CBD model of the block_under_test block
+// Created with CBD.converters.CBDDraw
+digraph model {
+ splines=ortho;
+ label=<<B>block_under_test (CBD)</B>>;
+ labelloc="t";
+ fontsize=20;
+ node_140043535377360 [label="CBD\n(constantCBD1)", shape=Msquare];
+ inter_140043535377360_outConstant1 [shape=point, width=0.01, height=0.01];
+ node_140043535377360 -> inter_140043535377360_outConstant1 [taillabel="outConstant1", arrowtail="invempty", arrowhead="none", dir=both];
+ node_140043535377600 [label="CBD\n(constantCBD2)", shape=Msquare];
+ inter_140043535377600_outConstant2 [shape=point, width=0.01, height=0.01];
+ node_140043535377600 -> inter_140043535377600_outConstant2 [taillabel="outConstant2", arrowtail="invempty", arrowhead="none", dir=both];
+ node_140043535377792 [label="CBD\n(adder1CBD)", shape=Msquare];
+ inter_140043535377360_outConstant1 -> node_140043535377792 [headlabel="in1Add1", arrowhead="normal", arrowtail="none", dir=both];
+ inter_140043535378224_outAdd2 -> node_140043535377792 [headlabel="in2Add1", arrowhead="normal", arrowtail="none", dir=both];
+ inter_140043535377792_outAdd1 [shape=point, width=0.01, height=0.01];
+ node_140043535377792 -> inter_140043535377792_outAdd1 [taillabel="outAdd1", arrowtail="invempty", arrowhead="none", dir=both];
+ node_140043535378224 [label="CBD\n(adder2CBD)", shape=Msquare];
+ inter_140043535377600_outConstant2 -> node_140043535378224 [headlabel="in1Add2", arrowhead="normal", arrowtail="none", dir=both];
+ inter_140043535378608_outNeg -> node_140043535378224 [headlabel="in2Add2", arrowhead="normal", arrowtail="none", dir=both];
+ inter_140043535378224_outAdd2 [shape=point, width=0.01, height=0.01];
+ node_140043535378224 -> inter_140043535378224_outAdd2 [taillabel="outAdd2", arrowtail="invempty", arrowhead="none", dir=both];
+ node_140043535378608 [label="CBD\n(negatorCBD)", shape=Msquare];
+ inter_140043535377792_outAdd1 -> node_140043535378608 [headlabel="inNeg", arrowhead="normal", arrowtail="none", dir=both];
+ inter_140043535378608_outNeg [shape=point, width=0.01, height=0.01];
+ node_140043535378608 -> inter_140043535378608_outNeg [taillabel="outNeg", arrowtail="invempty", arrowhead="none", dir=both];
+
+}