simplipfy.KirchhoffSolver package
Submodules
simplipfy.KirchhoffSolver.direction module
simplipfy.KirchhoffSolver.kirchhofSolver module
- class simplipfy.KirchhoffSolver.kirchhofSolver.KirchhoffSolver(circuitFileName: str, path: str, langSymbols: dict = {})
Bases:
object- checkJunctionByEq(cptWithSigns: list[tuple[str, int]], direction: int = 1) -> (<class 'int'>, <class 'str'>)
cptWithSigns: The component names with signs [[“R1”,1], [“R2”,1], [“R4”,-1], [“R5”,-1]] Check if the given components together with signs make a valid junction equation.
- checkJunctionRule(cptNames: list[str], direction: int = 1) tuple[int, tuple[str, str, str]]
- Parameters:
cptNames – The component names to check
direction – Can be used to invert the signs of the equation
Check if the given component names are at a junction and could potentially make a current equation.
- checkVoltageByEq(cptWithSigns: list[tuple[str, int]]) -> (<class 'int'>, <class 'str'>)
- checkVoltageLoopRule(cptNames: list[str]) tuple[int, str]
- Parameters:
cptNames – The component names to check
- Returns:
KirchhoffStates.value, eq
Check if the given component names are in a valid voltage loop and could potentially make a voltage equation.
- equations()
- Returns:
The equations found in the circuit
- equationsURI()
- Returns:
The equations found in the circuit
- foundAllCurrEquations() bool
- Returns:
True if all current equations are found, else False
To validate that all equations where found use self.foundAllEquations() The design of this object assumes that first the voltage equations are found and then the current equations. Therefore, this function is equivalent to self.foundAllEquations()
- foundAllEquations() bool
- Returns:
True if all equations are found, else False
- Raises:
AssertionError – if more equations are found than possible
- foundAllVoltEquations() bool
- Returns:
True if all voltage equations are found, else False
To validate that all equations where found use self.foundAllEquations()
- property foundEq: int
The number of equations found
- Type:
returns
- static makeDummy() KirchhoffSolver
Creates a fake KirchhoffSolver object for testing purposes.
- replaceURI(eq: str) str
- setCurrEq(eq: list[str | list[int]], cptNames: list[str]) KirchhoffStates
- Returns:
KirchhoffStates
Try to set/ save the current equation and coordinate Matrix
- static setEquation(value, cptNames, eqList, foundSets, eqMatrix) KirchhoffStates
value: tuple (equation, eqVector) :returns: KirchhoffStates
Try to set/ save the equation. If new independent equation save coordinate vector in EqMatrix
- setVoltEq(eq: tuple[str | list[int]], cptNames: list[str]) KirchhoffStates
- Returns:
KirchhoffStates
Try to set/ save the current equation and coordinate Matrix
- property voltEqURI
simplipfy.KirchhoffSolver.kirchhoffStates module
- class simplipfy.KirchhoffSolver.kirchhoffStates.KirchhoffStates(*values)
Bases:
EnumKirchhoffStates is an enumeration that defines the possible states of a Kirchhoff equation. Those are used for error handling/ error messages in the frontend.
- dependentEquation = 5
- duplicateEquation = 1
- isNewEquation = 0
- notAValidEquation = 2
- notAValidLoopOrder = 4
- toManyJunctions = 3
simplipfy.KirchhoffSolver.solver module
- simplipfy.KirchhoffSolver.solver.basicLoopsOfCircuit(cct: Circuit, eqNodeMap: None | dict[str, str]) list
- simplipfy.KirchhoffSolver.solver.checkCurrentEq(cct: Circuit, cptWithSigns: list[tuple[str, int]], eqNodeMap: dict) bool
- simplipfy.KirchhoffSolver.solver.checkVoltageEq(cct: Circuit, CptWithSigns: list[tuple[str, int]], eqNodeMap: dict) bool
- simplipfy.KirchhoffSolver.solver.createDispNames(cptNames: list[str], language: LangSymbols, ms: bool) dict[str, str]
- Parameters:
cptNames – list of component names
language – defines some language-specific symbols used in text, equations and labels
ms – multiple sources, true if there are multiple sources in the circuit, false otherwise
- Returns:
a dictionary with the cptNames as keys and the names that shall be displayed in the equations in the frontend
as values.
- simplipfy.KirchhoffSolver.solver.draw_graph(graph)
Draws a graph using matplotlib and networkx.
- simplipfy.KirchhoffSolver.solver.getEqNode(name: str, cct: Circuit, eqNodeMap: dict[str, str]) tuple[str, str]
- simplipfy.KirchhoffSolver.solver.isCurrentEquation(cct: Circuit, cptNames: list[str], eqNodeMap: dict) any | bool
- Parameters:
cct – circuit in witch cptNames are included
cptNames – list of component names
eqNodeMap – dictionary with node names as keys and master node names as values, see makeNodeMap()
- Returns:
the common node where all components, of cptNames, are connected to or False if there is no common node
Checks if the components in cptNames are connected to the same node/ potential. And if no other component is connected to the same node.
- simplipfy.KirchhoffSolver.solver.isImplicitCurrentEquation(cct: Circuit, cptNames: list[str]) any | bool
- Parameters:
cct – circuit in witch cptNames are included
cptNames – list of component names
- Returns:
the common node where all components, of cptNames, are connected to or False if there is no common node
Checks if the components in cptNames are connected to the same node/ potential. A current equation is considered implicit if there are only two components involved because it always results in I1 = I2.
- simplipfy.KirchhoffSolver.solver.isValidVoltageLoop(cct: Circuit, cptNames: list[str], loops: list, eqNodeMap: dict[str, str]) bool
- Parameters:
cct – circuit in witch cptNames are included
cptNames – list of component names
loops – list of loops in the circuit, extracted from the graph with networkx.simple_cycles()
eqNodeMap – dictionary with node names as keys and master node names as values, see makeNodeMap()
- Returns:
a list of nodes that make up the loop or an empty list if there is no loop
Check if the nodes of cptNames are part of a valid MESH in the graph. 1. meshes = minimal loops -> no node is passed more than once - check: node appears exactly twice 2. meshes/loops are connected components - check: are all nodes reachable from one starting node (with DFS)
- simplipfy.KirchhoffSolver.solver.loopsOfCircuit(cct: Circuit, eqNodeMap: None | dict[str, str]) tuple[list, int]
- Returns:
a list of list. Each list contains nodes that make up a loop. Wires are removed from the loops. The nodes are not adjusted and therefore not connected, because the wires are removed. See makeNodeMap() to find connected nodes/ components.
- simplipfy.KirchhoffSolver.solver.makeCurrentEquation(cct: Circuit, cptNames: list[str], commonNode, direction: Direction, language: LangSymbols, ms: bool) tuple[tuple[str, list], tuple[str, str, str]]
- Parameters:
cptNames – list of component names
commonNode – node where all components, of cptNames, are connected to (a key (master node) of the eqNodeMap dict)
direction – can be used to invert the signs of the equation
language – defines some language-specific symbols used in text, equations and labels
- Parm ms:
changes the source name to ges if only one source is in the circuit
- Returns:
a tuple [eq, decoy1, decoy2]
eq: the equation in latex format
decoy1: the equation in latex format with all signs negative, positive if direction is counterClockwise
decoy2: the equation in latex format with all signs positive, negative if direction is counterClockwise
Creates a current equation form the given cptNames and the common node. If the component is connected to the common node with its negative terminal the current is positive, if the component is connected to the common node with its positive terminal the current is negative.
- simplipfy.KirchhoffSolver.solver.makeLoopFromElmList(cptNames: list, cct: Circuit, eqNodeMap: dict[str, str]) list
- Parameters:
cptNames – list of component names
cct – circuit in witch cptNames are included
eqNodeMap – dictionary with node names as keys and master node names as values, see makeNodeMap()
- Returns:
a list of nodes that make up the loop or an empty list if there is no loop
Creates a loop from the given cptNames. The set of nodes in the loop is the same as the set nodes from all components. Asserts that there are no nodes from wires in the loop and the loop has the correct order of nodes.
- simplipfy.KirchhoffSolver.solver.makeNodeMap(cct: Circuit) dict[str, str]
- Returns:
a dictionary where key is node and value is master node.
Maps equipotential nodes to one master node.
Note
E.g.: Let there be a simple circuit with 2 components and 2 nodes: V1 0 1 dc {10}; down W 0 2; right R1 2 3; down W 3 1; left The equipotential nodes are: potential1 {0, 2}, potential2 {1, 3} therefore the node map is: {0: 0, 2: 0, 1: 1, 3: 1} remove the wires from the circuit and replace the nodes with the value of the nodeMap: V1 0 1 dc {10}; down R1 0 1; down
- simplipfy.KirchhoffSolver.solver.makePairsFromList(loop: list) list[tuple[str, str]]
- Parameters:
loop – list of nodes
- Returns:
a list of tuples. Each tuple contains two nodes that make up a pair. The last pair is (loop[-1], loop[0]).
- simplipfy.KirchhoffSolver.solver.makeVoltageEquations(cct: Circuit, cptNames: list[str], language: LangSymbols, eqNodeMap: dict[str, str], ms: bool) tuple[str, list[int]]
- Parameters:
cct – circuit in witch cptNames are included
cptNames – list of component names
language – defines some language-specific symbols used in text, equations and labels
eqNodeMap – dictionary with node names as keys and master node names as values, see makeNodeMap()
ms – multiple sources, true if there are multiple sources in the circuit, false otherwise
- Returns:
a string with the voltage equation in latex format
Creates a voltage equation from the given cptNames. The equation is in the form of 0 = … . The signs are defined by the direction of the loop. If the order of the nodes in the loop, is the same as pos to neg node from the component, the voltage is positive. If the order is reversed the voltage is negative. The loop is interpreted as pairs from left to right. A pair is (loop[n], loop[n+1]), with the last pair being (loop[-1] and loop[0]).
Saves equation in a list indexed by component names (to check for linear dependence)
V1 0 1 dc {10}; down R1 0 2; right R2 2 3; down R3 3 1; left loop = [0, 2, 3, 1] cptNames = [V1, R1, R2, R3] eqNodeMap = {0: 0, 1: 1, 2: 2, 3: 3} R1 from 0 to 2, loop pair is (0, 2), therefore VR1 is positive R2 from 2 to 3, loop pair is (2, 3), therefore V2 is positive R3 from 3 to 1, loop pair is (3, 1), therefore V3 is positive V1 from 0 to 1, loop pair is (1, 0), therefore V1 is negative eq: 0 = VR1 + V2 + V3 - V1 eqVect: [-1, 1, 1, 1]
Module contents
Module used to solve a circuit with kirchhoff’s laws. It can check if selected components make a valid voltage equation or a valid current equation. Module logic in kirchhofSolver.py Logic to figure out if equation is valid is in the circuitSolver.py, using graph theorie with networkx