title: C++ Design for Circuit Solver
body: <p>I have a C++ architectural design problem. I am trying to work on a circuit simulator where I have defined 3 classes. A node class, component class, and circuit class. Here is a simplified layout of those classes</p>
  
  <p><a href="https://gist.github.com/Nick0412/0b9a0ba8848cb5225d5bd663dad8fd77" rel="nofollow noreferrer">https://gist.github.com/Nick0412/0b9a0ba8848cb5225d5bd663dad8fd77</a></p>
  
  <p>Now my impression is that when I create a specific component, say resistor, it will inherit from the component class. However when I add an arbitrary component to the circuit, I want the circuit to do specific things depending on what kind of component is added. </p>
  
  <p>So I have two questions. First, would a visitor type pattern be the most appropriate way to solve this? Second, does anyone have any feedback on if this layout is reasonable?</p>
  
  <p>Edit:</p>
  
  <p>My description was not clear enough and that was my bad. Just for context I will link the repository here <a href="https://github.com/Nick0412/Circuit" rel="nofollow noreferrer">https://github.com/Nick0412/Circuit</a>. </p>
  
  <p>So long story short, I have been trying to learn about modified nodal analysis (MNA) from resources such as <a href="https://www.swarthmore.edu/NatSci/echeeve1/Ref/mna/MNA5.html" rel="nofollow noreferrer">https://www.swarthmore.edu/NatSci/echeeve1/Ref/mna/MNA5.html</a>. </p>
  
  <p>Based on this my procedural thought process is as follows.</p>
  
  <ul>
  <li>Set up linear set of equation of the form <strong>Ax=y</strong> where <strong>x</strong> is a vector of all voltages at each node in the circuit and all currents going through voltage sources, <strong>y</strong> is the vector of 0's representing a zero sum of current at each node as well as the voltages across the voltage sources and <strong>A</strong> is a matrix that has the conductances of each branch as well as 1s scattered in it to account for current through the voltage sources.</li>
  <li>We can solve for <strong>x</strong> by inverting the matrix <strong>A</strong> and left hand multiplying it to <strong>y</strong>. This can be done with a linear algebra library like <a href="http://eigen.tuxfamily.org/index.php?title=Main_Page" rel="nofollow noreferrer">Eigen</a>.</li>
  <li>The bulk of the work comes from setting up the matrix <strong>A</strong> and appropriately updating it each time stamp. We setup <strong>A</strong> by iterating over all the components and multiplying by the appropriate conductance of that component to its respectively connected nodes (MNA notes). The conductance for reactive components like capacitors and inductors will be complex (such as <strong>1/(sC)</strong> for caps. and <strong>sL</strong> for inductors) since we take into account frequency components.</li>
  <li>Solve for the vector <strong>x</strong> and update appropriately for the timestamp.</li>
  </ul>
  
  <p>So I think this mostly covers the basic idea of how the circuit solver to work. Now for the more specific details. So in order to set up the matrix <strong>A</strong> as described above, it would seem to make sense to me that I have a circuit class the contains a set of components without duplicates such as</p>
  
  <pre class="lang-c prettyprint-override"><code>// circuit.h
  
  class Circuit
  {
  private:
      std::unordered_set&lt;Component*&gt; m_components;
  // ... public methods
  };
  </code></pre>
  
  <p>Now a component is suppose to be a base class for which all actual components, (resistors, caps, inductors) inherit from. The component class should be very minimal and hold only data that all components share, such as a list of nodes to which it is attached.</p>
  
  <pre class="lang-c prettyprint-override"><code>// component.h
  
  class Component
  {
  private:
      std::unordered_set&lt;Node*&gt; m_nodes;
  // ... public methods
  };
  </code></pre>
  
  <p>Example of a component inheriting</p>
  
  <pre class="lang-c prettyprint-override"><code>// resistor.h
  
  class Resistor : public Component
  {
  private:
      double m_resistance;
  // ... public methods
  };
  </code></pre>
  
  <p>The node class I designed is rather really simple and I won't include it here but it can be found on the repo. So to add a component to the circuit, the user might do something like</p>
  
  <pre class="lang-c prettyprint-override"><code>// Circuit::addComponent(Component* c, Node* n1, Node* n2)
  
  Circuit c1;
  Node* n1 (new Node());
  Node* n2 (new Node());
  
  // 10 is suppose to be resistance in ohms that we init r with
  Resistor* r (new Resistor(10)); 
  
  c1.addComponent(r, n1, n2);
  
  // add some other components here ...
  
  // solve the circuit for DC (direct current) state lets say
  c1.solve();
  </code></pre>
  
  <p>So when we call the add component it adds the component to the circuit and adds the nodes to the component as well. But as you can see, since addComponent takes a generic component, it might be difficult to update the circuit since we may want to know the specific component being added. This is why I asked about the visitor pattern. How do I add a generic component appropriately to the circuit and update?</p>
  
  <p>Sorry for this long winded post, I hope it cleared up any misunderstanding and if not I can continue to add to it. Thank you.</p>