Warning, file /sdk/ktechlab/src/electronics/simulation/capacitance.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /***************************************************************************
0002  *   Copyright (C) 2003-2005 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 "capacitance.h"
0012 
0013 Capacitance::Capacitance(const double capacitance, const double delta)
0014     : Reactive(delta)
0015 {
0016     m_cap = capacitance;
0017     m_scaled_cap = i_eq_old = 0.;
0018     m_numCNodes = 2;
0019     setMethod(Capacitance::m_euler);
0020 }
0021 
0022 Capacitance::~Capacitance()
0023 {
0024 }
0025 
0026 void Capacitance::setCapacitance(const double c)
0027 {
0028     m_cap = c;
0029 }
0030 
0031 void Capacitance::add_initial_dc()
0032 {
0033     // We don't need to do anything here, as time_step() will do that for us,
0034     // apart from to make sure our old values are 0
0035     m_scaled_cap = i_eq_old = 0.;
0036 }
0037 
0038 void Capacitance::updateCurrents()
0039 {
0040     if (!b_status)
0041         return;
0042     const double r_i = (p_cnode[0]->v - p_cnode[1]->v) * m_scaled_cap;
0043     m_cnodeI[0] = -i_eq_old - r_i;
0044     m_cnodeI[1] = -m_cnodeI[0];
0045 }
0046 
0047 void Capacitance::time_step()
0048 {
0049     if (!b_status)
0050         return;
0051 
0052     double v = p_cnode[0]->v - p_cnode[1]->v;
0053     double i_eq_new = 0.0, scaled_cap_new = 0.0;
0054 
0055     if (m_method == Capacitance::m_euler) {
0056         scaled_cap_new = m_cap / m_delta;
0057         i_eq_new = -v * scaled_cap_new;
0058     } else if (m_method == Capacitance::m_trap) {
0059         // TODO Implement + test trapezoidal method
0060         scaled_cap_new = 2. * m_cap / m_delta;
0061     }
0062 
0063     if (m_scaled_cap != scaled_cap_new) {
0064         const double tmp = scaled_cap_new - m_scaled_cap;
0065         A_g(0, 0) += tmp;
0066         A_g(0, 1) -= tmp;
0067         A_g(1, 0) -= tmp;
0068         A_g(1, 1) += tmp;
0069     }
0070 
0071     if (i_eq_new != i_eq_old) {
0072         const double tmp = i_eq_new - i_eq_old;
0073         b_i(0) -= tmp;
0074         b_i(1) += tmp;
0075     }
0076 
0077     m_scaled_cap = scaled_cap_new;
0078     i_eq_old = i_eq_new;
0079 }
0080 
0081 bool Capacitance::updateStatus()
0082 {
0083     b_status = Reactive::updateStatus();
0084     if (m_method == Capacitance::m_none)
0085         b_status = false;
0086     return b_status;
0087 }
0088 
0089 void Capacitance::setMethod(Method m)
0090 {
0091     m_method = m;
0092     updateStatus();
0093 }