summaryrefslogtreecommitdiffstats
path: root/src/electronics/simulation/inductance.cpp
blob: 22c5f9edbee8eede06fb67e2f9471132829e60fd (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
/***************************************************************************
 *   Copyright (C) 2005 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 "elementset.h"
#include "inductance.h"
#include "matrix.h"

Inductance::Inductance( double inductance, double delta )
	: Reactive(delta)
{
	m_inductance = inductance;
	r_eq_old = v_eq_old = 0.0;
	m_numCNodes = 2;
	m_numCBranches = 1;
	setMethod( Inductance::m_euler );
}


Inductance::~Inductance()
{
}


void Inductance::setInductance( double i )
{
	m_inductance = i;
}


void Inductance::add_initial_dc()
{
	A_c( 0, 0 ) = 1;
	A_b( 0, 0 ) = 1;
	A_c( 0, 1 ) = -1;
	A_b( 1, 0 ) = -1;
	
	// The adding of r_eg and v_eq will be done for us by time_step.
	// So for now, just reset the constants used.
	r_eq_old = v_eq_old = 0.0;
}


void Inductance::updateCurrents()
{
	if (!b_status)
		return;
	m_cnodeI[0] = p_cbranch[0]->i;
	m_cnodeI[1] = -m_cnodeI[0];
}


void Inductance::add_map()
{
	if (!b_status)
		return;
	
	if ( !p_cnode[0]->isGround )
	{
		p_A->setUse_c( p_cbranch[0]->n(), p_cnode[0]->n(), Map::et_constant, true );
		p_A->setUse_b( p_cnode[0]->n(), p_cbranch[0]->n(), Map::et_constant, true );
	}
	
	if ( !p_cnode[1]->isGround )
	{
		p_A->setUse_c( p_cbranch[0]->n(), p_cnode[1]->n(), Map::et_constant, true );
		p_A->setUse_b( p_cnode[1]->n(), p_cbranch[0]->n(), Map::et_constant, true );
	}
	
	p_A->setUse_d( p_cbranch[0]->n(), p_cbranch[0]->n(), Map::et_unstable, false );
}


void Inductance::time_step()
{
	if (!b_status) return;
	
	double i = p_cbranch[0]->i;
	double v_eq_new = 0.0, r_eq_new = 0.0;
	
	if ( m_method == Inductance::m_euler )
	{
		r_eq_new = m_inductance/m_delta;
		v_eq_new = -i*r_eq_new;
	}
	else if ( m_method == Inductance::m_trap ) {
		// TODO Implement + test trapezoidal method
		r_eq_new = 2.0*m_inductance/m_delta;
	}
	
	if ( r_eq_old != r_eq_new )
	{
		A_d( 0, 0 ) -= r_eq_new - r_eq_old;
	}
	
	if ( v_eq_new != v_eq_old )
	{
		b_v( 0 ) += v_eq_new - v_eq_old;
	}
	
	r_eq_old = r_eq_new;
	v_eq_old = v_eq_new;
}


bool Inductance::updateStatus()
{
	b_status = Reactive::updateStatus();
	if ( m_method == Inductance::m_none )
		b_status = false;
	return b_status;
}


void Inductance::setMethod( Method m )
{
	m_method = m;
	updateStatus();
}