Просмотр исходного кода

Bugfix in GJS

Added the possibility to simplify models
rparedis 4 лет назад
Родитель
Сommit
8b7f2e8ce3

+ 23 - 0
src/CBD/converters/latexify.py

@@ -143,6 +143,17 @@ class CBD2Latex:
 			return "\\left\\{\\begin{array}{lcl}\n%s\\end{array}\\right." % latex
 		return latex
 
+	def eq(self):
+		"""
+		Obtains the current set of equations in a format that can be parsed by
+		the :mod:`CBD.converters.eq2CBD` converter.
+		This allows model simplifications and optimizations.
+		"""
+		res = ""
+		for k, v in self.equations.items():
+			res += "{} = {}\n".format(k, str(v))
+		return res
+
 	def create_ic(self):
 		"""
 		Creates the equations for the initial conditions of a system.
@@ -317,6 +328,16 @@ class Fnc:
 	def __repr__(self):
 		return "%s%s" % (self.name, self.args)
 
+	def __str__(self):
+		if self.name in ['+', '*', '^', '%', '<', '<=', '==', 'or', 'and']:
+			return " {} ".format(self.name).join(["({})".format(str(x)) for x in self.args])
+		elif self.name in ['-', '!']:
+			return "({}({}))".format(self.name, str(self.args[0]))
+		elif self.name == '~':
+			return "(1/({}))".format(str(self.args[0]))
+		else:
+			return "{}({})".format(self.name, ", ".join([str(x) for x in self.args]))
+
 	def __hash__(self):
 		return hash((self.name, tuple(self.args)))
 
@@ -718,3 +739,5 @@ if __name__ == '__main__':
 	# ltx.render()
 	ltx.simplify()
 	print(ltx.render())
+	print("----------------------------")
+	print(ltx.eq())

Разница между файлами не показана из-за своего большого размера
+ 1 - 1
src/CBD/preprocessing/rk-notes/rungekutta-visual.drawio


+ 2 - 3
src/CBD/simulator.py

@@ -175,7 +175,7 @@ class Simulator:
 		ret = self.__stop_requested
 		if self.__termination_condition is not None:
 			ret = self.__termination_condition(self.model, self.__sim_data[2])
-		return ret or self.__termination_time + 1e-10 <= self.getTime()
+		return ret or self.__termination_time <= self.getTime()
 
 	def stop(self):
 		"""
@@ -553,8 +553,7 @@ class Simulator:
 				# Detected a strongly connected component
 				self.__solver.checkValidity(self.model.getPath(), component)
 				solverInput = self.__solver.constructInput(component, curIteration)
-				self.__solver.solve(solverInput)
-				solutionVector = solverInput[1]
+				solutionVector = self.__solver.solve(solverInput)
 				for block in component:
 					if curIteration == 0 or self.__scheduler.mustCompute(block, self.getTime()):
 						blockIndex = component.index(block)

+ 8 - 4
src/CBD/solver.py

@@ -132,9 +132,9 @@ class GaussianJordanLinearSolver(Solver):
 		# Get low-level dependency
 		resolveBlock = lambda possibleDep, output_port: possibleDep if not isinstance(possibleDep, CBD) else possibleDep.getBlockByName(output_port)
 
-		# Get list of low-level dependencies from two inputs
+		# Get list of low-level dependencies from n inputs
 		def getBlockDependencies2(block):
-			return (resolveBlock(b, output_port) for (b, output_port) in [block.getBlockConnectedToInput("IN1"), block.getBlockConnectedToInput("IN2")])
+			return (resolveBlock(b, output_port) for (b, output_port) in [block.getBlockConnectedToInput(x) for x in block.getInputPortNames()])
 
 		for i, block in enumerate(strongComponent):
 			if block.getBlockType() == "AdderBlock":
@@ -143,11 +143,13 @@ class GaussianJordanLinearSolver(Solver):
 				M1[i, i] = -1
 
 				for compInStrong in [x for x in getBlockDependencies2(block) if x in strongComponent]:
-					M1[i, indexdict[compInStrong]] = 1
+					M1[i, indexdict[compInStrong]] += 1
 			elif block.getBlockType() == "ProductBlock":
 				# M2 can stay 0
 				M1[i, i] = -1
-				M1[i, indexdict[[x for x in getBlockDependencies2(block) if x in strongComponent][0]]] = reduce(lambda x, y: x * y, [x.getSignal()[curIteration].value for x in getBlockDependencies2(block) if x not in strongComponent])
+				fact = reduce(lambda x, y: x * y, [x.getSignal()[curIteration].value for x in getBlockDependencies2(block) if x not in strongComponent])
+				for compInStrong in [x for x in getBlockDependencies2(block) if x in strongComponent]:
+					M1[i, indexdict[compInStrong]] += fact
 			elif block.getBlockType() == "NegatorBlock":
 				# M2 can stay 0
 				M1[i, i] = -1
@@ -224,6 +226,8 @@ class GaussianJordanLinearSolver(Solver):
 				for k in range(n):
 					M1[k, indxr[l]], M1[k, indxc[l]] = M1[k, indxc[l]], M1[k, indxr[l]]
 
+		return solverInput[1]
+
 
 class Matrix:
 	"""Custom, efficient matrix class. This class is used for efficiency purposes.