summaryrefslogtreecommitdiffstats
path: root/src/electronics/simulation/elementset.cpp
blob: 8a0adc95b443928a7f4f4349c9e828d404d17bc6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/***************************************************************************
 *   Copyright (C) 2003-2004 by David Saxton                               *
 *   david@bluehaze.org                                                    *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 ***************************************************************************/

#include "bjt.h"
#include "circuit.h"
#include "elementset.h"
#include "element.h"
#include "logic.h"
#include "matrix.h"
#include "nonlinear.h"
#include "vec.h"

#include <kdebug.h>

#include <cmath>
#include <iostream>
#include <assert.h>

ElementSet::ElementSet( Circuit * circuit, const int n, const int m )
{
	m_pCircuit = circuit;
	m_cn = n;
	m_cb = m;
	p_logicIn = 0l;
	p_A = new Matrix( m_cn, m_cb );
	p_b = new Vector(m_cn+m_cb);
	p_x = new Vector(m_cn+m_cb);
	p_x_prev = new Vector(m_cn+m_cb);
	m_cbranches = new CBranch*[m_cb];
	m_cnodes = new CNode*[m_cn];
	for ( uint i=0; i<m_cn; i++ )
	{
		m_cnodes[i] = new CNode();
		m_cnodes[i]->set_n(i);
	}
	for ( uint i=0; i<m_cb; i++ )
	{
		m_cbranches[i] = new CBranch();
		m_cbranches[i]->set_n(i);
	}
	m_ground = new CNode();
	m_ground->isGround = true;
	b_containsNonLinear = false;
}

ElementSet::~ElementSet()
{
	const ElementList::iterator end = m_elementList.end();
	for ( ElementList::iterator it = m_elementList.begin(); it != end; ++it )
	{
		// Note: By calling setElementSet(0l), we might have deleted it (the Element will commit
		// suicide when both the the ElementSet and Component to which it belongs have deleted
		// themselves). So be very careful it you plan to do anything with the (*it) pointer
		if (*it) (*it)->elementSetDeleted();
	}
	for ( uint i=0; i<m_cn; i++ )
	{
		delete m_cnodes[i];
	}
	for ( uint i=0; i<m_cb; i++ )
	{
		delete m_cbranches[i];
	}
	delete[] m_cbranches;
	delete[] m_cnodes;
	delete[] p_logicIn;
	delete m_ground;
	delete p_A;
	delete p_b;
	delete p_x;
	delete p_x_prev;
}


void ElementSet::setCacheInvalidated()
{
	m_pCircuit->setCacheInvalidated();
}


void ElementSet::addElement( Element *e )
{
	if ( !e || m_elementList.tqcontains(e) ) return;
	e->setElementSet(this);
	m_elementList.append(e);
	if ( e->isNonLinear() )
	{
		b_containsNonLinear = true;
		m_cnonLinearList.append( static_cast<NonLinear*>(e) );
	}
}


void ElementSet::createMatrixMap()
{
	p_A->createMap();
	
	
	// And do our logic as well...
	
	m_clogic = 0;
	ElementList::iterator end = m_elementList.end();
	for ( ElementList::iterator it = m_elementList.begin(); it != end; ++it )
	{
		if ( dynamic_cast<LogicIn*>(*it) )
			m_clogic++;
	}
	
	p_logicIn = new LogicIn*[m_clogic];
	int i=0;
	for ( ElementList::iterator it = m_elementList.begin(); it != end; ++it )
	{
		if ( LogicIn * in = dynamic_cast<LogicIn*>(*it) )
			p_logicIn[i++] = in;
	}
}


void ElementSet::doNonLinear( int maxIterations, double maxErrorV, double maxErrorI )
{
// 	p_x_prev->reset();
	
	// And now tell the cnodes and cbranches about their new voltages & currents
	updateInfo();
	
	const NonLinearList::iterator end = m_cnonLinearList.end();
	
	int k = 0;
	do
	{
		// Tell the nonlinear elements to update its J, A and b from the newly calculated x
		for ( NonLinearList::iterator it = m_cnonLinearList.begin(); it != end; ++it )
			(*it)->update_dc();
		
		*p_x = *p_b;
		p_A->performLU();
		p_A->fbSub(p_x);
		updateInfo();
		
		// Now, check for convergence
		bool converged = true;
		for ( unsigned i = 0; i < m_cn; ++i )
		{
			double diff = std::abs( (*p_x_prev)[i] - (*p_x)[i] );
			if ( diff > maxErrorI )
			{
				converged = false;
				break;
			}
		}
		if ( converged )
		{
			for ( unsigned i = m_cn; i < m_cn+m_cb; ++i )
			{
				double diff = std::abs( (*p_x_prev)[i] - (*p_x)[i] );
				if ( diff > maxErrorV )
				{
					converged = false;
					break;
				}
			}
		}
		
		*p_x_prev = *p_x;
		
		if ( converged )
			break;
	}
	while ( ++k < maxIterations );
}


bool ElementSet::doLinear( bool performLU )
{
	if ( b_containsNonLinear || (!p_b->isChanged() && ((performLU && !p_A->isChanged()) || !performLU)) )
		return false;
	
	if (performLU)
		p_A->performLU();
	
	*p_x = *p_b;
	p_A->fbSub(p_x);
	updateInfo();
	p_b->setUnchanged();
	
	return true;
}


void ElementSet::updateInfo()
{
	for ( uint i=0; i<m_cn; i++ )
	{
		const double v = (*p_x)[i];
		if (std::isfinite(v)) {
			m_cnodes[i]->v = v;
		} else {
			(*p_x)[i] = 0.;
			m_cnodes[i]->v = 0.;
		}
	}
	for ( uint i=0; i<m_cb; i++ )
	{
		// NOTE: I've used lowercase and uppercase "I" here, so be careful!
		const double I = (*p_x)[i+m_cn];
		if (std::isfinite(I)) {
			m_cbranches[i]->i = I;
		} else {
			(*p_x)[i+m_cn] = 0.;
			m_cbranches[i]->i = 0.;
		}
	}
	
	// Tell logic to check themselves
	for ( uint i=0; i<m_clogic; ++i )
	{
		p_logicIn[i]->check();
	}
}


void ElementSet::displayEquations()
{
	std::cout.setf(std::ios_base::fixed);
	std::cout.precision(5);
	std::cout.setf(std::ios_base::showpoint);
	std::cout << "A x = b :"<<std::endl;
	for ( uint i=0; i<m_cn+m_cb; i++ )
	{
		std::cout << "( ";
		for ( uint j=0; j<m_cn+m_cb; j++ )
		{
			const double value = p_A->g(i,j);
// 			if		( value > 0 ) cout <<"+";
// 			else if ( value == 0 ) cout <<" ";
			std::cout.width(10);
			std::cout << value<<" ";
		}
		std::cout << ") ( "<<(*p_x)[i]<<" ) = ( ";
		std::cout<<(*p_b)[i]<<" )"<<std::endl;
	}
	std::cout << "A_LU:"<<std::endl;
	p_A->displayLU();
}