capacitance.cpp

00001 /***************************************************************************
00002  *   Copyright (C) 2003-2005 by David Saxton                               *
00003  *   david@bluehaze.org                                                    *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  ***************************************************************************/
00010 
00011 #include "capacitance.h"
00012 
00013 Capacitance::Capacitance( const double capacitance, const double delta )
00014         : Reactive(delta)
00015 {
00016         m_cap = capacitance;
00017         g_eq_old = i_eq_old = 0.;
00018         m_numCNodes = 2;
00019         setMethod( Capacitance::m_euler );
00020 }
00021 
00022 Capacitance::~Capacitance()
00023 {
00024 }
00025 
00026 void Capacitance::setCapacitance( const double c )
00027 {
00028         m_cap = c;
00029 }
00030 
00031 void Capacitance::add_initial_dc()
00032 {
00033         // We don't need to do anything here, as time_step() will do that for us,
00034         // apart from to make sure our old values are 0
00035         g_eq_old = i_eq_old = 0.;
00036 }
00037 
00038 void Capacitance::updateCurrents()
00039 {
00040         if (!b_status) return;
00041         const double r_i = (p_cnode[0]->v-p_cnode[1]->v)*g_eq_old;
00042         m_cnodeI[0] = -i_eq_old-r_i;
00043         m_cnodeI[1] = -m_cnodeI[0];
00044 }
00045 
00046 
00047 void Capacitance::add_map()
00048 {
00049         if (!b_status) return;
00050         
00051         if ( !p_cnode[0]->isGround )
00052         {
00053                 p_A->setUse( p_cnode[0]->n(), p_cnode[0]->n(), Map::et_unstable, false );
00054         }
00055         if ( !p_cnode[1]->isGround )
00056         {
00057                 p_A->setUse( p_cnode[1]->n(), p_cnode[1]->n(), Map::et_unstable, false );
00058         }
00059         
00060         if ( !p_cnode[0]->isGround && !p_cnode[1]->isGround )
00061         {
00062                 p_A->setUse( p_cnode[0]->n(), p_cnode[1]->n(), Map::et_unstable, false );
00063                 p_A->setUse( p_cnode[1]->n(), p_cnode[0]->n(), Map::et_unstable, false );
00064         }
00065 }
00066 
00067 
00068 void Capacitance::time_step()
00069 {
00070         if (!b_status) return;
00071         
00072         double v = p_cnode[0]->v-p_cnode[1]->v;
00073         double i_eq_new = 0.0, g_eq_new = 0.0;
00074         
00075         if ( m_method == Capacitance::m_euler )
00076         {
00077                 g_eq_new = m_cap/m_delta;
00078                 i_eq_new = -v*g_eq_new;
00079         } else if ( m_method == Capacitance::m_trap ) {
00080                 // TODO Implement + test trapezoidal method
00081                 g_eq_new = 2.*m_cap/m_delta;
00082         }
00083         
00084         if ( g_eq_old != g_eq_new )
00085         {
00086                 A_g( 0, 0 ) += g_eq_new-g_eq_old;
00087                 A_g( 1, 1 ) += g_eq_new-g_eq_old;
00088                 A_g( 0, 1 ) -= g_eq_new-g_eq_old;
00089                 A_g( 1, 0 ) -= g_eq_new-g_eq_old;
00090         }
00091         
00092         if ( i_eq_new != i_eq_old )
00093         {
00094                 b_i( 0 ) -= i_eq_new-i_eq_old;
00095                 b_i( 1 ) += i_eq_new-i_eq_old;
00096         }
00097         
00098         g_eq_old = g_eq_new;
00099         i_eq_old = i_eq_new;
00100 }
00101 
00102 bool Capacitance::updateStatus()
00103 {
00104         b_status = Reactive::updateStatus();
00105         if ( m_method == Capacitance::m_none ) b_status = false;
00106         return b_status;
00107 }
00108 
00109 void Capacitance::setMethod( Method m )
00110 {
00111         m_method = m;
00112         updateStatus();
00113 }
00114 
00115 

Generated on Tue May 8 17:05:28 2007 for KTechLab by  doxygen 1.5.1