File indexing completed on 2024-05-05 05:46:11

0001 /***************************************************************************
0002  *   Copyright (C) 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 "inductance.h"
0012 #include "elementset.h"
0013 
0014 Inductance::Inductance(double inductance, double delta)
0015     : Reactive(delta)
0016 {
0017     m_inductance = inductance;
0018     scaled_inductance = v_eq_old = 0.0;
0019     m_numCNodes = 2;
0020     m_numCBranches = 1;
0021     setMethod(Inductance::m_euler);
0022 }
0023 
0024 Inductance::~Inductance()
0025 {
0026 }
0027 
0028 void Inductance::setInductance(double i)
0029 {
0030     m_inductance = i;
0031 }
0032 
0033 void Inductance::add_initial_dc()
0034 {
0035     A_c(0, 0) = 1;
0036     A_b(0, 0) = 1;
0037     A_c(0, 1) = -1;
0038     A_b(1, 0) = -1;
0039 
0040     // The adding of r_eg and v_eq will be done for us by time_step.
0041     // So for now, just reset the constants used.
0042     scaled_inductance = v_eq_old = 0.0;
0043 }
0044 
0045 void Inductance::updateCurrents()
0046 {
0047     if (!b_status)
0048         return;
0049 
0050     m_cnodeI[0] = -p_cbranch[0]->i;
0051     m_cnodeI[1] = -m_cnodeI[0];
0052 }
0053 
0054 void Inductance::time_step()
0055 {
0056     if (!b_status)
0057         return;
0058 
0059     double i = p_cbranch[0]->i;
0060     double v_eq_new = 0.0, r_eq_new = 0.0;
0061 
0062     if (m_method == Inductance::m_euler) {
0063         r_eq_new = m_inductance / m_delta;
0064         v_eq_new = -i * r_eq_new;
0065     } else if (m_method == Inductance::m_trap) {
0066         // TODO Implement + test trapezoidal method
0067         r_eq_new = 2.0 * m_inductance / m_delta;
0068     }
0069 
0070     if (scaled_inductance != r_eq_new) {
0071         A_d(0, 0) -= r_eq_new - scaled_inductance;
0072     }
0073 
0074     if (v_eq_new != v_eq_old) {
0075         b_v(0) += v_eq_new - v_eq_old;
0076     }
0077 
0078     scaled_inductance = r_eq_new;
0079     v_eq_old = v_eq_new;
0080 }
0081 
0082 bool Inductance::updateStatus()
0083 {
0084     b_status = Reactive::updateStatus();
0085     if (m_method == Inductance::m_none)
0086         b_status = false;
0087     return b_status;
0088 }
0089 
0090 void Inductance::setMethod(Method m)
0091 {
0092     m_method = m;
0093     updateStatus();
0094 }