Optimizing the Solution


As described in the main text, each vertex introduces an equation that enforces Kirchhoff's first law. It would be acceptable for CVertex to implement IEqEquation for this purpose, but here I choose a more optimal solution. Notice that one equation and one unknown at each vertex can be eliminated by calculating one current. The dCurrent member of the last LegStruct is never used, nor is the last leg represented as an unknown. Instead, the current through this leg is calculated based on all other legs from the vertex, to enforce Kirchhoff's first law (see the snippet below).

The Newton-Raphson solver uses matrix operations to find a solution, each of which takes time proportional to the square of the size of the matrix. Eliminating one equation and one unknown for each vertex reduces the size of the matrices by the number of vertices in the network, thus greatly improving the performance of the Newton-Raphson solver.

double CVertex::GetCurrent( CLeg *pLeg )
{
   LegStructList::iterator itLegStruct = FindLegStruct( pLeg );

   ASSUMING ( itLegStruct != m_lLegs.end() )
   {
      if ( *itLegStruct == m_lLegs.back() )
      {
         // Enforce Kirchhoff's first law.
         double dCurrent = 0.0;
         for ( itLegStruct = m_lLegs.begin();
            (*itLegStruct)->pLeg != pLeg;
               itLegStruct++ )
         {
            dCurrent -= (*itLegStruct)->dCurrent;
         }
         return dCurrent;
      }
      else
      {
         // Get the current through the selected leg.
         return (*itLegStruct)->dCurrent;
      }
   }
   return 0.0;
}