# Algorithms for Simulation of Shunt Currents in a Vanadium Redox Flow Battery
## 4 Methods
### Algorithms
#### 4.4.2 Codification of Node Equations  
The node equations refer to the structure of the electrical circuit. The coefficients of these equations are always 1 or 0 and do not change as long as the circuit is not changed. Therefore, it is only necessary to run the coding process of these equations once, regardless of the simulation time imposed and the simulation steps to be performed. As explained above, the execution of the operations always starts in the stack list instance. This instance has 3 additional attributes: An instance of an object that serves as an equation pointer, which refers to the row of the matrix or vector in which to encode the equations; an instance of an object that serves as a stack pointer, referring to the previous stack processed, the current stack to be processed and the next stack to be processed; and an instance of an object that, being of the same type as the stack pointer, serves to reference the previous cell processed, the current cell to process, and the next cell to process. These two pointers do not refer to the objects itself. What they do is reference with the absolute position number the first variable/unknown that introduces the case of the object in the equation system. Algorithm 3 shows the method of how the stack list instance starts the process of encoding the node equations. The first thing that is performed in the method is the reset of the positions to which the pointers point, returning them to the starting positions. After that, it enters a loop in which, for each stack, the _buildNodeEquations_ method is invoked. Keep in mind that, as with the variables, the equations of each case are codified sequentially in the matrix and vectors, so that if a case introduces a total of 7 equations to the system (counting the equations of nodes and nets), these 7 will be coded in contiguous rows. Since, in this method, only the equations of the nodes of the cases are being codified, it is necessary to adjust the equation pointer to omit the rows relative to the equations of the nets after processing each case, which is why the _updtRowPntr_ method is invoked. Algorithm 4 has been implemented for the _buildNodeEquations_ method of the stacks. ```
Input:\(bMat\): equation system coefficient matrix. Result: Encode of the equations of stacks and cells nodes. Class the method belongs to: StackList. ```  
```
1:procedurebuildNodeEquations(\(bMat\))
2:\(self\_eqPntr.reset(1)\); /* Reset pointer to point to row 1 of matrix and vectors. */ self\(cPntr.reset(1)\);
3:\(/*\) Reset the object so that the index indicating the absolute position of the first variable of the next cell to be processed is equal to 1. */
4:\(tmpS\) = \(self\_fStack\); \(self\_sPntr.reset(1+tmpS.cellVarNumber)\); /* Reset the object so that the index indicating the absolute position of the first variable of the next stack to be processed is equal to the number of cell variables of the first stack +1. */
6:for\(i=1\):\(self\_stackNumber\) \(tmpS\):buildNodeEquations(\(bMat\), \(self\_cPntr\), \(self\_sPntr\), \(self\_eqPntr\));
7:\(/*\) Call to the generic method of Stack type objects that is implemented in the Stack superclass. */ self\(eqPntr.updtRowPntr(tmpS.netEquationsNumber)\); /* The pointer is updated each time a cell or stack equation is encoded. However, since the node and net equations of each cell/stack are encoded contiguously and this method only encodes node equations, it is necessary to update the pointer after processing each stack to omit the rows relating to the net equations of that stack. */ \(tmpS\) = \(tmpS\).getNext();
10:end
```  
**Algorithm 3** Algorithm for the codification of the node equations (I)  
The _updatePointerIndexes_ and _buildSelfNodeEquations_ methods have their implementation delegated to the subclasses of the Stack class. As mentioned earlier in the article, some equations of the first and last cells are modified in case there is more than one stack.