Qucs-core  0.0.19
circuit.h
Go to the documentation of this file.
00001 /*
00002  * circuit.h - circuit class definitions
00003  *
00004  * Copyright (C) 2003-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 
00031 #ifndef __CIRCUIT_H__
00032 #define __CIRCUIT_H__
00033 
00034 #include "characteristic.h"
00035 #include "operatingpoint.h"
00036 
00037 #define NODE_1 0
00038 #define NODE_2 1
00039 #define NODE_3 2
00040 #define NODE_4 3
00041 #define NODE_5 4
00042 #define NODE_6 5
00043 #define VSRC_1 0
00044 #define VSRC_2 1
00045 #define VSRC_3 2
00046 #define VSRC_4 3
00047 #define VSRC_5 4
00048 
00049 #define MODFLAG(val,bit) if (val) flag |= (bit); else flag &= ~(bit);
00050 #define RETFLAG(bit)     ((flag & (bit)) != 0)
00051 
00052 #define CREATOR(val) \
00053   val (); \
00054   static qucs::circuit * create (void) { return new val (); } \
00055   static struct define_t cirdef; \
00056   static struct define_t * definition (void) { return &cirdef; }
00057 
00058 #include <map>
00059 #include <string>
00060 
00061 #include "integrator.h"
00062 #include "valuelist.h"
00063 
00064 namespace qucs {
00065 
00066 enum circuit_flag {
00067   CIRCUIT_ENABLED     = 1,
00068   CIRCUIT_LINEAR      = 2,
00069   CIRCUIT_ORIGINAL    = 4,
00070   CIRCUIT_VSOURCE     = 8,
00071   CIRCUIT_ISOURCE     = 16,
00072   CIRCUIT_INTVSOURCE  = 32,
00073   CIRCUIT_VARSIZE     = 64,
00074   CIRCUIT_PROBE       = 128,
00075   CIRCUIT_HISTORY     = 256,
00076 };
00077 
00078 class node;
00079 class property;
00080 class substrate;
00081 class matrix;
00082 class net;
00083 class environment;
00084 class history;
00085 
00095 class circuit : public object, public integrator
00096 {
00097  public:
00098   circuit * getNext (void) const { return this->next; }
00099   void setNext (circuit * const o) { this->next = o; }
00100   circuit * getPrev (void) const { return prev; }
00101   void setPrev (circuit * const o) { this->prev = o; }
00102  private:
00103   circuit * next;
00104   circuit * prev;
00105   
00106  public:
00107   // constructor and destructor set
00108   circuit ();
00109   circuit (int);
00110   circuit (const circuit &);
00111   virtual ~circuit ();
00112 
00113   // functionality to be overloaded by real, derived circuit element
00114   // implementations
00123   virtual void initSP (void) { allocMatrixS (); }
00124   virtual void calcSP (nr_double_t) { }
00125   virtual void initDC (void) { allocMatrixMNA (); }
00126   virtual void calcDC (void) { }
00127   virtual void restartDC (void) { }
00128   virtual void initNoiseSP (void) { allocMatrixN (); }
00129   virtual void calcNoiseSP (nr_double_t) { }
00130   virtual void initNoiseAC (void) { allocMatrixN (vsources); }
00131   virtual void calcNoiseAC (nr_double_t) { }
00132   virtual void initAC (void) { allocMatrixMNA (); }
00133   virtual void calcAC (nr_double_t) { }
00134   virtual void initTR (void) { allocMatrixMNA (); }
00135   virtual void calcTR (nr_double_t) { }
00136   virtual void initHB (void) { allocMatrixMNA (); }
00137   virtual void calcHB (nr_double_t) { }
00138   virtual void initHB (int) { allocMatrixMNA (); }
00139   virtual void calcHB (int) { }
00140   virtual void calcOperatingPoints (void) { }
00141   virtual void saveOperatingPoints (void) { }
00142   virtual void calcCharacteristics (nr_double_t) { }
00143   virtual void saveCharacteristics (nr_double_t) { }
00144   virtual void saveCharacteristics (nr_complex_t) { }
00145 
00146   // basic circuit element functionality
00147   void   setNode (int, const std::string&, int intern = 0);
00148   node * getNode (int);
00149   void   setType (int t) { type = t; }
00150   int    getType (void) { return type; }
00156   int    getSize (void) { return size; }
00166   void   setSize (int);
00173   bool   isEnabled (void) { return RETFLAG (CIRCUIT_ENABLED); }
00180   void   setEnabled (bool e) { MODFLAG (e, CIRCUIT_ENABLED); }
00181   bool   isVariableSized (void) { return RETFLAG (CIRCUIT_VARSIZE); }
00182   void   setVariableSized (bool v) { MODFLAG (v, CIRCUIT_VARSIZE); }
00183   bool   isProbe (void) { return RETFLAG (CIRCUIT_PROBE); }
00184   void   setProbe (bool p) { MODFLAG (p, CIRCUIT_PROBE); }
00185   void   setNet (net * n) { subnet = n; }
00186   net *  getNet (void) { return subnet; }
00187 
00188   // subcircuitry
00189   std::string getSubcircuit (void) { return subcircuit; }
00190   void   setSubcircuit (const std::string &);
00191 
00192   // environment specific
00193   environment * getEnv (void) { return env; }
00194   void setEnv (environment * e) { env = e; }
00195 
00196   // nodal analyses helpers
00197   void setInternalVoltageSource (bool i) { MODFLAG (i, CIRCUIT_INTVSOURCE); }
00198   bool isInternalVoltageSource (void) { return RETFLAG (CIRCUIT_INTVSOURCE); }
00199   void setVoltageSource (int s) { vsource = s; }
00200   int  getVoltageSource (void) { return vsource; }
00201   int  getVoltageSources (void);
00202   void setVoltageSources (int);
00203   void voltageSource (int, int, int, nr_double_t value = 0.0);
00204   bool isVSource (void) { return RETFLAG (CIRCUIT_VSOURCE); }
00205   void setVSource (bool v) { MODFLAG (v, CIRCUIT_VSOURCE); }
00206   bool isISource (void) { return RETFLAG (CIRCUIT_ISOURCE); }
00207   void setISource (bool i) { MODFLAG (i, CIRCUIT_ISOURCE); }
00208   int  getNoiseSources (void);
00209   void setNoiseSources (int);
00210 
00211   // transient analyses helpers
00212   void transientCapacitance (int, int, int, nr_double_t, nr_double_t,
00213                              nr_double_t);
00214   void transientCapacitance (int, int, nr_double_t, nr_double_t, nr_double_t);
00215   void transientCapacitanceQ (int, int, int, nr_double_t);
00216   void transientCapacitanceQ (int, int, nr_double_t);
00217   void transientCapacitanceC (int, int, int, int, nr_double_t, nr_double_t);
00218   void transientCapacitanceC (int, int, nr_double_t, nr_double_t);
00219   void transientCapacitanceC2V (int, int, int, nr_double_t, nr_double_t);
00220   void transientCapacitanceC2Q (int, int, int, nr_double_t, nr_double_t);
00221   void setDelta (nr_double_t * d) { deltas = d; }
00222   nr_double_t * getDelta (void) { return deltas; }
00223 
00224   // history specific functionality
00225   bool hasHistory (void) { return RETFLAG (CIRCUIT_HISTORY); }
00226   void setHistory (bool h) { MODFLAG (h, CIRCUIT_HISTORY); }
00227   void initHistory (nr_double_t);
00228   void deleteHistory (void);
00229   void truncateHistory (nr_double_t);
00230   void appendHistory (int, nr_double_t);
00231   void applyHistory (history *);
00232   nr_double_t getV (int, nr_double_t);
00233   nr_double_t getV (int, int);
00234   nr_double_t getJ (int, nr_double_t);
00235   nr_double_t getHistoryAge (void);
00236   void setHistoryAge (nr_double_t);
00237   int getHistorySize (void);
00238   nr_double_t getHistoryTFromIndex (int);
00239 
00240   // s-parameter helpers
00241   int  getPort (void) { return pacport; }
00242   void setPort (int p) { pacport = p; }
00243   int  getInserted (void) { return inserted; }
00244   void setInserted (int i) { inserted = i; }
00245   bool isOriginal (void) { return RETFLAG (CIRCUIT_ORIGINAL); }
00246   void setOriginal (bool o) { MODFLAG (o, CIRCUIT_ORIGINAL); }
00247 
00248   // microstrip helpers
00249   substrate * getSubstrate (void);
00250   void setSubstrate (substrate *);
00251 
00252   // matrix entry modificators
00253   nr_complex_t getS (int, int);
00254   nr_complex_t getN (int, int);
00255   nr_complex_t getY (int, int);
00256   nr_complex_t getB (int, int);
00257   nr_complex_t getC (int, int);
00258   nr_complex_t getD (int, int);
00259   nr_complex_t getQV (int, int);
00260   nr_complex_t getGV (int);
00261   nr_complex_t getCV (int);
00262   nr_complex_t getE (int);
00263   nr_complex_t getI (int);
00264   nr_complex_t getJ (int);
00265   nr_complex_t getV (int);
00266   nr_complex_t getQ (int);
00267   nr_double_t getG (int, int);
00268   void setS (int, int, nr_complex_t);
00269   void setN (int, int, nr_complex_t);
00270   void setY (int, int, nr_complex_t);
00271   void setB (int, int, nr_complex_t);
00272   void setC (int, int, nr_complex_t);
00273   void setD (int, int, nr_complex_t);
00274   void setQV (int, int, nr_complex_t);
00275   void setGV (int, nr_complex_t);
00276   void setCV (int, nr_complex_t);
00277   void setE (int, nr_complex_t);
00278   void setI (int, nr_complex_t);
00279   void setJ (int, nr_complex_t);
00280   void setV (int, nr_complex_t);
00281   void setQ (int, nr_complex_t);
00282   void setG (int, int, nr_double_t);
00283   void clearB (void);
00284   void clearC (void);
00285   void clearD (void);
00286   void clearE (void);
00287   void clearI (void);
00288   void clearJ (void);
00289   void clearV (void);
00290   void clearY (void);
00291   void addY (int, int, nr_complex_t);
00292   void addY (int, int, nr_double_t);
00293   void addI (int, nr_complex_t);
00294   void addI (int, nr_double_t);
00295 
00296   // operating point functionality
00297   void        addOperatingPoint (const std::string &name, nr_double_t);
00298   nr_double_t getOperatingPoint (const std::string &name);
00299   void        setOperatingPoint (const std::string &name, nr_double_t);
00300   int         hasOperatingPoint (const std::string &name);
00301   valuelist<operatingpoint> & getOperatingPoints (void) { return oper; }
00302 
00303   // characteristics functionality
00304   void        addCharacteristic (const std::string &name, nr_double_t);
00305   nr_double_t getCharacteristic (const std::string &name);
00306   void        setCharacteristic (const std::string &name, nr_double_t);
00307   int         hasCharacteristic (const std::string &name);
00308   valuelist<characteristic> & getCharacteristics (void) { return charac; }
00309 
00310   // differentiate between linear and non-linear circuits
00311   void setNonLinear (bool l) { MODFLAG (!l, CIRCUIT_LINEAR); }
00312   bool isNonLinear (void) { return !RETFLAG (CIRCUIT_LINEAR); }
00313 
00314   // miscellaneous functionality
00315   void print (void);
00316   static std::string createInternal (const std::string &, const std::string &);
00317   void setInternalNode (int, const std::string &);
00318 
00319   // matrix operations
00320   void   allocMatrixS (void);
00321   void   allocMatrixN (int sources = 0);
00322   void   allocMatrixMNA (void);
00323   void   freeMatrixMNA (void);
00324   void   allocMatrixHB (void);
00325   void   freeMatrixHB (void);
00326   void   setMatrixS (matrix);
00327   matrix getMatrixS (void);
00328   void   setMatrixN (matrix);
00329   matrix getMatrixN (void);
00330   void   setMatrixY (matrix);
00331   matrix getMatrixY (void);
00332 
00333   static const nr_double_t z0;
00334 
00335  protected:
00336   int type;
00337   int pol;
00338 
00339  private:
00340   int size;
00341   int pacport;
00342   int vsource;
00343   int vsources;
00344   int nsources;
00345   int inserted;
00346   int flag;
00347   nr_complex_t * MatrixS;
00348   nr_complex_t * MatrixN;
00349   nr_complex_t * MatrixY;
00350   nr_complex_t * MatrixB;
00351   nr_complex_t * MatrixC;
00352   nr_complex_t * MatrixD;
00353   nr_complex_t * VectorE;
00354   nr_complex_t * VectorI;
00355   nr_complex_t * VectorV;
00356   nr_complex_t * VectorJ;
00357   nr_complex_t * VectorQ;
00358   nr_complex_t * MatrixQV;
00359   nr_complex_t * VectorGV;
00360   nr_complex_t * VectorCV;
00361   std::string subcircuit;
00362   node * nodes;
00363   substrate * subst;
00364   valuelist<operatingpoint> oper;
00365   valuelist<characteristic> charac;
00366   net * subnet;
00367   environment * env;
00368   nr_double_t * deltas;
00369   int nHistories;
00370   history * histories;
00371 };
00372 
00373 } // namespace qucs
00374 
00375 // typedef to make it easier to set up our factory
00376 typedef qucs::circuit *maker_t();
00377 // function typdefs to make it easier to set up our factories
00378 typedef qucs::circuit *creator_t();
00379 typedef struct define_t *defs_t();
00380 
00381 // our global factories defined in module.cpp
00382 extern "C" {
00383  extern std::map<std::string, creator_t *, std::less<std::string> > factorycreate;
00384  extern std::map<std::string, defs_t *, std::less<std::string> > factorydef;
00385 
00386 }
00387 
00388 #endif /* __CIRCUIT_H__ */