Qucs-core  0.0.19
nasolver.h
Go to the documentation of this file.
00001 /*
00002  * nasolver.h - nodal analysis solver class definitions
00003  *
00004  * Copyright (C) 2004, 2005, 2006, 2007, 2008 Stefan Jahn <stefan@lkcc.org>
00005  *
00006  * This is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2, or (at your option)
00009  * any later version.
00010  *
00011  * This software is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this package; see the file COPYING.  If not, write to
00018  * the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
00019  * Boston, MA 02110-1301, USA.
00020  *
00021  * $Id$
00022  *
00023  */
00024 
00025 #ifndef __NASOLVER_H__
00026 #define __NASOLVER_H__
00027 
00028 #include "qucs_typedefs.h"
00029 #include "tvector.h"
00030 #include "tmatrix.h"
00031 #include "eqnsys.h"
00032 #include "nasolution.h"
00033 #include "analysis.h"
00034 
00035 // Convergence helper definitions.
00036 #define CONV_None            0
00037 #define CONV_Attenuation     1
00038 #define CONV_LineSearch      2
00039 #define CONV_SteepestDescent 3
00040 #define CONV_GMinStepping    4
00041 #define CONV_SourceStepping  5
00042 
00043 namespace qucs {
00044 
00045 class analysis;
00046 class circuit;
00047 class nodelist;
00048 class vector;
00049 
00050 template <class nr_type_t>
00051 class nasolver : public analysis
00052 {
00053 public:
00054     nasolver ();
00055     nasolver (const std::string&);
00056     nasolver (nasolver &);
00057     ~nasolver ();
00058     int  solve_once (void);
00059     int  solve_nonlinear (void);
00060     int  solve_nonlinear_continuation_gMin (void);
00061     int  solve_nonlinear_continuation_Source (void);
00062     int  solve_linear (void);
00063     void solve_pre (void);
00064     void solve_post (void);
00065     void setDescription (const std::string &n) { desc = n; }
00066     std::string getDescription (void) const { return desc; }
00067     void saveResults (const std::string &, const std::string &, int, qucs::vector * f = NULL);
00068     typedef void (* calculate_func_t) (nasolver<nr_type_t> *);
00069     void setCalculation (calculate_func_t f) { calculate_func = f; }
00070     void calculate (void)
00071     {
00072         if (calculate_func) (*calculate_func) (this);
00073     }
00074     const char * getHelperDescription (void);
00075 
00076     //interface convenience functions
00078     int getN ();
00080     int getM ();
00081 
00082 protected:
00083     void restartNR (void);
00084     void savePreviousIteration (void);
00085     void restorePreviousIteration (void);
00086     int  countNodes (void);
00087     int  getNodeNr (const std::string &);
00088     int  findAssignedNode (circuit *, int);
00089     int  countVoltageSources (void);
00090     void saveSolution (void);
00091     circuit * findVoltageSource (int);
00092     void applyNodeset (bool nokeep = true);
00093     void createNoiseMatrix (void);
00094     void runMNA (void);
00095     void createMatrix (void);
00096     void storeSolution (void);
00097     void recallSolution (void);
00098     int  checkConvergence (void);
00099 
00100 private:
00101     void assignVoltageSources (void);
00102     void createGMatrix (void);
00103     void createBMatrix (void);
00104     void createCMatrix (void);
00105     void createDMatrix (void);
00106     void createIVector (void);
00107     void createEVector (void);
00108     void createZVector (void);
00109     void applyAttenuation (void);
00110     void lineSearch (void);
00111     void steepestDescent (void);
00112     std::string createV (int, const std::string&, int);
00113     std::string createI (int, const std::string&, int);
00114     std::string createOP (const std::string&, const std::string &);
00115     void saveNodeVoltages (void);
00116     void saveBranchCurrents (void);
00117     nr_type_t MatValX (nr_complex_t, nr_complex_t *);
00118     nr_type_t MatValX (nr_complex_t, nr_double_t *);
00119 
00120 protected:
00121     tvector<nr_type_t> * z;
00122     tvector<nr_type_t> * x;
00123     tvector<nr_type_t> * xprev;
00124     tvector<nr_type_t> * zprev;
00125     tmatrix<nr_type_t> * A;
00126     tmatrix<nr_type_t> * C;
00127     int iterations;
00128     int convHelper;
00129     int fixpoint;
00130     int eqnAlgo;
00131     int updateMatrix;
00132     nr_double_t gMin, srcFactor;
00133     std::string desc;
00134     nodelist * nlist;
00135 
00136 private:
00137     eqnsys<nr_type_t> * eqns;
00138     nr_double_t reltol;
00139     nr_double_t abstol;
00140     nr_double_t vntol;
00141     nasolution<nr_type_t> solution;
00142 
00143 private:
00144 
00145     calculate_func_t calculate_func;
00146 };
00147 
00148 } // namespace qucs
00149 
00150 #include "nasolver.cpp"
00151 
00152 #endif /* __NASOLVER_H__ */