File indexing completed on 2024-04-28 05:43:15

0001 /***************************************************************************
0002  *   Copyright (C) 2003-2004 by David Saxton                               *
0003  *   david@bluehaze.org                                                    *
0004  *                                                                         *
0005  *   This program is free software; you can redistribute it and/or modify  *
0006  *   it under the terms of the GNU General Public License as published by  *
0007  *   the Free Software Foundation; either version 2 of the License, or     *
0008  *   (at your option) any later version.                                   *
0009  ***************************************************************************/
0010 
0011 #ifndef ELEMENTSET_H
0012 #define ELEMENTSET_H
0013 
0014 //#include <vector>
0015 
0016 #include <QList>
0017 
0018 class CBranch;
0019 class Circuit;
0020 class CNode;
0021 class Element;
0022 class ElementSet;
0023 class LogicIn;
0024 class Matrix;
0025 class NonLinear;
0026 class QuickVector; // not exactly sure how these types of declarations work.
0027 
0028 typedef QList<Element *> ElementList;
0029 typedef QList<NonLinear *> NonLinearList;
0030 
0031 /**
0032 Steps in simulation of a set of elements:
0033 (1) Create this class with given number of nodes "n" and voltage sources "m"
0034 (2) Add the various elements with addElement.
0035 (3) Call performDC()
0036 (4) Get the nodal voltages and voltage currents with x()
0037 (5) Repeat steps 3 and 4 if necessary for further transient analysis.
0038 
0039 This class shouldn't be confused with the Circuit class, but considered a helper class to Circuit.
0040 Circuit will handle the simulation of a set of components over time. This just finds the DC-operating
0041 point of the circuit for a given set of elements.
0042 
0043 @short Handles a set of circuit elements
0044 @author David Saxton
0045 */
0046 class ElementSet
0047 {
0048 public:
0049     /**
0050      * Create a new circuit, with "n" nodes and "m" voltage sources.
0051      * After creating the circuit, you must call setGround to specify
0052      * the ground nodes, before adding any elements.
0053      */
0054     ElementSet(Circuit *circuit, const int n, const int m);
0055     /**
0056      * Destructor. Note that only the matrix and supporting data is deleted.
0057      * i.e. Any elements added to the circuit will not be deleted.
0058      */
0059     ~ElementSet();
0060     Circuit *circuit() const
0061     {
0062         return m_pCircuit;
0063     }
0064     void addElement(Element *e);
0065     void setCacheInvalidated();
0066     /**
0067      * Returns the matrix in use. This is created once on the creation of the ElementSet
0068      * class, and deleted in the destructor, so the pointer returned will never change.
0069      */
0070     Matrix *matrix() const
0071     {
0072         return p_A;
0073     }
0074     /**
0075      * Returns the vector for b (i.e. the independent currents & voltages)
0076      */
0077     QuickVector *b() const
0078     {
0079         return p_b;
0080     }
0081     /**
0082      * Returns the vector for x (i.e. the currents & voltages at the branches and nodes)
0083      */
0084     QuickVector *x() const
0085     {
0086         return p_x;
0087     }
0088     /**
0089      * @return if we have any nonlinear elements (e.g. diodes, tranaistors).
0090      */
0091     bool containsNonLinear() const
0092     {
0093         return b_containsNonLinear;
0094     }
0095     /**
0096      * Solves for nonlinear elements, or just does linear if it doesn't contain
0097      * any nonlinear.
0098      */
0099     void doNonLinear(int maxIterations, double maxErrorV = 1e-9, double maxErrorI = 1e-12);
0100     /**
0101      * Solves for linear and logic elements.
0102      * @returns true if anything changed
0103      */
0104     bool doLinear(bool performLU);
0105     CBranch **cbranches() const
0106     {
0107         return m_cbranches;
0108     }
0109     CNode **cnodes() const
0110     {
0111         return m_cnodes;
0112     }
0113     CNode *ground() const
0114     {
0115         return m_ground;
0116     }
0117     /**
0118      * Returns the number of nodes in the circuit (excluding ground 'nodes')
0119      */
0120     int cnodeCount() const
0121     {
0122         return m_cn;
0123     }
0124     /**
0125      * Returns the number of voltage sources in the circuit
0126      */
0127     int cbranchCount() const
0128     {
0129         return m_cb;
0130     }
0131 
0132     void createMatrixMap();
0133     /**
0134      * Displays the matrix equations Ax=b and J(dx)=-r
0135      */
0136     void displayEquations();
0137     /**
0138      * Update the nodal voltages and branch currents from the x vector
0139      */
0140     void updateInfo();
0141 
0142 private:
0143     // calc engine stuff
0144     Matrix *p_A;
0145     QuickVector *p_x;
0146     QuickVector *p_b;
0147     // end calc engine stuff.
0148 
0149     ElementList m_elementList;
0150     NonLinearList m_cnonLinearList;
0151 
0152     uint m_cb;
0153     CBranch **m_cbranches; // Pointer to an array of cbranches
0154 
0155     uint m_cn;
0156     CNode **m_cnodes; // Pointer to an array of cnodes
0157     CNode *m_ground;
0158 
0159     uint m_clogic;
0160     LogicIn **p_logicIn;
0161     bool b_containsNonLinear;
0162     Circuit *m_pCircuit;
0163 };
0164 
0165 #endif