# Algorithms for Simulation of Shunt Currents in a Vanadium Redox Flow Battery
## 4 Methods
### Algorithms
#### 4.4.2 Codification of Node Equations
In addition, the equations that are affected depend on how the stacks are connected to their cells (u-form or z-form, for example). To solve this peculiarity, we have assigned as an attribute (named as _stackConnector_) to the Stack class an object type to delegate the modification of these equations. As this work is oriented towards stacks connected in the form of z, we have ensured that these objects are created in the constructors. But in cases of allowing other types of connections, it would be an input parameter of the constructor or another function of the Stack-derived classes. The class relative to the case of having only one stack would have an object of this type, which would not perform any action. As with the Stack-derived classes, the _updatePointerIndexes_ and _buildNodeEquations_ of the Cell class have delegated implementations in the subclasses of Cell. This way, it is only necessary to differentiate cases in the construction of the data structures. The method _buildNodeEquations_ of the Cell classes has the form shown in Algorithm 5. ```
1:procedurebuildNodeEquations(\(bMat\), \(cPntr\), \(sPntr\), \(eqPntr\))
2:\(\quad\) self.updatePointerIndexes(\(sPntr\));
3:\(\quad\) /** Call to the Stack type object's own method to update the stacks variable pointer. Each stack of Stack implements this method. For objects relative to the first and middle stacks the implementation is the same, but for objects relative to the last stack it differs. **/
4:\(\quad\) \(tmpC\) = _self.Cell_;
5:\(\quad\) /** Call to the generic method of Cell type objects implemented in the Cell superclass. **/
6:\(\quad\) /** The same commented about the equation pointer in Algorithm 3 but applied to the context of cell equations. **/
7:\(\quad\) \(tmpC\) = _tmpC_.getNext()_;
8:end
9:\(\quad\) self.stackComector.connect(self, \(bMat\), \(sPntr\), \(eqPntr\));
10:\(\quad\) /** To complete the equations relating to the nodes that connect the stack to the first and last cell. It could have been implemented in the classes related to those objects. But doing so would mean losing the generalization of the implementation of the first and last cells in the case of there being only one stack. It was decided to create our own objects to carry out this implementation, to make it possible to have it generalized in case we wanted to implement other ways of connecting the stacks with the cells. **/
11:\(\quad\) self.buildSelfNodeEquations(\(bMat\), \(sPntr\), \(eqPntr\));
12:\(\quad\) /** Call to the Stack type object's own method to encode the equations relative to the stack. Each subclass of Stack implements this method. **/
13:end
```  
**Algorithm 4** Algorithm for the codification of the node equations (II)  
```
1:procedurebuildNodeEquations(\(bMat\), \(cPntr\), \(eqPntr\))
2:\(\quad\) self.updatePointerIndexes(\(cPntr\));
3:\(\quad\) self.buildEq1(\(bMat\), \(cPntr\), \(eqPntr\));
4:\(\quad\) eqPntr.updtRowPntr(1);
5:\(\quad\) self.buildEq2(\(bMat\), \(cPntr\), \(eqPntr\));
6:\(\quad\) eqPntr.updtRowPntr(1);
7:\(\quad\) //... Repeat to all node equations of the cell
8:end
```  
**Algorithm 5** Algorithm for the codification of the node equations (III). ```
1:procedurebuildNodeEquations(\(bMat\), \(cPntr\), \(eqPntr\))
2:\(\quad\)



Equation (2) is the first equation of the case relative to the first cell of the stacks. \[0=ICell_{i-1,n}-ICh_{i-1,n}^{C,1}-ICh_{i-1,n}^{C,1}-ICell_{i,1}-ICh_{i,1}^{A,1}- ICh_{i,1}^{A,2} \tag{2}\]

This equation is encoded in the matrix \(B\) as shown in Algorithm 6. The coding of the equations relative to the nodes have the same form. ```
1:Input:\(bMat\): equation system coefficient matrix, eqPntr: equation pointer, cPntr: cell pointer. Result: Encode the first equation of the first cell of a stack. Class the method belongs to: FirstCellOfStack.