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

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 #include "resistance.h"
0012 #include "elementset.h"
0013 
0014 #include <ktechlab_debug.h>
0015 
0016 Resistance::Resistance(const double resistance)
0017     : Element::Element()
0018 {
0019     m_g = resistance < 1e-9 ? 1e9 : 1. / resistance;
0020     m_numCNodes = 2;
0021     //  qCDebug(KTL_LOG);
0022 }
0023 
0024 Resistance::~Resistance()
0025 {
0026     //  qCDebug(KTL_LOG);
0027 }
0028 
0029 void Resistance::setConductance(const double g)
0030 {
0031     if (g == m_g)
0032         return;
0033 
0034     if (p_eSet)
0035         p_eSet->setCacheInvalidated();
0036 
0037     // Remove old resistance
0038     m_g = -m_g;
0039     add_initial_dc();
0040 
0041     m_g = g;
0042     add_initial_dc();
0043 }
0044 
0045 void Resistance::setResistance(const double r)
0046 {
0047     setConductance(r < 1e-9 ? 1e9 : 1. / r);
0048 }
0049 
0050 void Resistance::add_initial_dc()
0051 {
0052     if (!b_status)
0053         return;
0054 
0055     A_g(0, 0) += m_g;
0056     A_g(1, 1) += m_g;
0057     A_g(0, 1) -= m_g;
0058     A_g(1, 0) -= m_g;
0059 }
0060 
0061 void Resistance::updateCurrents()
0062 {
0063     if (!b_status)
0064         return;
0065     const double v = p_cnode[0]->v - p_cnode[1]->v;
0066     m_cnodeI[1] = v * m_g;
0067     m_cnodeI[0] = -m_cnodeI[1];
0068 }