File indexing completed on 2024-04-28 05:43:15
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 "element.h" 0012 #include "elementset.h" 0013 0014 #include <cassert> 0015 0016 #include <ktechlab_debug.h> 0017 0018 // BEGIN class Element 0019 Element::Element() 0020 { 0021 b_status = false; 0022 p_eSet = nullptr; 0023 b_componentDeleted = false; 0024 0025 for (int i = 0; i < MAX_CNODES; i++) 0026 p_cnode[i] = nullptr; 0027 0028 resetCurrents(); 0029 0030 for (int i = 0; i < MAX_CBRANCHES; i++) 0031 p_cbranch[i] = nullptr; 0032 0033 m_numCBranches = 0; 0034 m_numCNodes = 0; 0035 } 0036 0037 Element::~Element() 0038 { 0039 } 0040 0041 void Element::resetCurrents() 0042 { 0043 for (int i = 0; i < 8; i++) 0044 m_cnodeI[i] = 0.0; 0045 } 0046 0047 void Element::setElementSet(ElementSet *c) 0048 { 0049 assert(!b_componentDeleted); 0050 assert(!p_eSet); 0051 if (!c) 0052 return elementSetDeleted(); 0053 p_eSet = c; 0054 updateStatus(); 0055 } 0056 0057 void Element::componentDeleted() 0058 { 0059 b_componentDeleted = true; 0060 b_status = false; 0061 0062 p_eSet = nullptr; 0063 setCNodes(); 0064 setCBranches(); 0065 } 0066 0067 void Element::elementSetDeleted() 0068 { 0069 if (b_componentDeleted) 0070 return delete this; 0071 0072 b_status = false; 0073 // qCDebug(KTL_LOG) << "Element::elementSetDeleted(): Setting b_status to false, this="<<this; 0074 0075 p_eSet = nullptr; 0076 setCNodes(); 0077 setCBranches(); 0078 } 0079 0080 void Element::setCNodes(const int n0, const int n1, const int n2, const int n3) 0081 { 0082 if (!p_eSet) { 0083 // cerr << "Element::setCNodes: can't set nodes without circuit!"<<endl; 0084 for (int i = 0; i < MAX_CNODES; i++) 0085 p_cnode[i] = nullptr; 0086 return; 0087 } 0088 0089 p_cnode[0] = (n0 > -1) ? p_eSet->cnodes()[n0] : (n0 == -1 ? p_eSet->ground() : nullptr); 0090 p_cnode[1] = (n1 > -1) ? p_eSet->cnodes()[n1] : (n1 == -1 ? p_eSet->ground() : nullptr); 0091 p_cnode[2] = (n2 > -1) ? p_eSet->cnodes()[n2] : (n2 == -1 ? p_eSet->ground() : nullptr); 0092 p_cnode[3] = (n3 > -1) ? p_eSet->cnodes()[n3] : (n3 == -1 ? p_eSet->ground() : nullptr); 0093 updateStatus(); 0094 } 0095 0096 void Element::setCBranches(const int b0, const int b1, const int b2, const int b3) 0097 { 0098 if (!p_eSet) { 0099 // cerr << "Element::setCBranches: can't set branches without circuit!"<<endl; 0100 for (int i = 0; i < 4; i++) 0101 p_cbranch[i] = nullptr; 0102 return; 0103 } 0104 p_cbranch[0] = (b0 > -1) ? p_eSet->cbranches()[b0] : nullptr; 0105 p_cbranch[1] = (b1 > -1) ? p_eSet->cbranches()[b1] : nullptr; 0106 p_cbranch[2] = (b2 > -1) ? p_eSet->cbranches()[b2] : nullptr; 0107 p_cbranch[3] = (b3 > -1) ? p_eSet->cbranches()[b3] : nullptr; 0108 updateStatus(); 0109 } 0110 0111 bool Element::updateStatus() 0112 { 0113 // First, set status to false if all nodes in use are ground 0114 b_status = false; 0115 for (int i = 0; i < m_numCNodes; i++) { 0116 b_status |= p_cnode[i] ? !p_cnode[i]->isGround : false; 0117 } 0118 0119 // Set status to false if any of the nodes are not set 0120 for (int i = 0; i < m_numCNodes; i++) { 0121 if (!p_cnode[i]) 0122 b_status = false; 0123 } 0124 0125 // Finally, set status to false if not all the required branches are set 0126 for (int i = 0; i < m_numCBranches; i++) { 0127 if (!p_cbranch[i]) 0128 b_status = false; 0129 } 0130 0131 // Finally, check for various pointers 0132 if (!p_eSet) 0133 b_status = false; 0134 0135 if (!b_status) { 0136 resetCurrents(); 0137 } 0138 0139 // And return the status :-) 0140 // qCDebug(KTL_LOG) << "Element::updateStatus(): Setting b_status to "<<(b_status?"true":"false")<<" this="<<this; 0141 return b_status; 0142 } 0143 0144 double Element::cbranchCurrent(const int branch) 0145 { 0146 if (!b_status || branch < 0 || branch >= m_numCBranches) 0147 return 0.; 0148 return (*p_cbranch)[branch].i; 0149 } 0150 0151 double Element::cnodeVoltage(const int node) 0152 { 0153 if (!b_status || node < 0 || node >= m_numCNodes) 0154 return 0.; 0155 return (*p_cnode)[node].v; 0156 } 0157 0158 // END class Element