summaryrefslogtreecommitdiffstats
path: root/src/electronics/simulation/capacitance.cpp
blob: 9087c7f4bf414dd05218f90f37b24ddceacdc63f (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
/***************************************************************************
 *   Copyright (C) 2003-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 "capacitance.h"
#include "matrix.h"

Capacitance::Capacitance( const double capacitance, const double delta )
	: Reactive(delta)
{
	m_cap = capacitance;
	g_eq_old = i_eq_old = 0.;
	m_numCNodes = 2;
	setMethod( Capacitance::m_euler );
}

Capacitance::~Capacitance()
{
}

void Capacitance::setCapacitance( const double c )
{
	m_cap = c;
}

void Capacitance::add_initial_dc()
{
	// We don't need to do anything here, as time_step() will do that for us,
	// apart from to make sure our old values are 0
	g_eq_old = i_eq_old = 0.;
}

void Capacitance::updateCurrents()
{
	if (!b_status) return;
	const double r_i = (p_cnode[0]->v-p_cnode[1]->v)*g_eq_old;
	m_cnodeI[0] = -i_eq_old-r_i;
	m_cnodeI[1] = -m_cnodeI[0];
}


void Capacitance::add_map()
{
	if (!b_status) return;
	
	if ( !p_cnode[0]->isGround )
	{
		p_A->setUse( p_cnode[0]->n(), p_cnode[0]->n(), Map::et_unstable, false );
	}
	if ( !p_cnode[1]->isGround )
	{
		p_A->setUse( p_cnode[1]->n(), p_cnode[1]->n(), Map::et_unstable, false );
	}
	
	if ( !p_cnode[0]->isGround && !p_cnode[1]->isGround )
	{
		p_A->setUse( p_cnode[0]->n(), p_cnode[1]->n(), Map::et_unstable, false );
		p_A->setUse( p_cnode[1]->n(), p_cnode[0]->n(), Map::et_unstable, false );
	}
}


void Capacitance::time_step()
{
	if (!b_status) return;
	
	double v = p_cnode[0]->v-p_cnode[1]->v;
	double i_eq_new = 0.0, g_eq_new = 0.0;
	
	if ( m_method == Capacitance::m_euler )
	{
		g_eq_new = m_cap/m_delta;
		i_eq_new = -v*g_eq_new;
	}
	else if ( m_method == Capacitance::m_trap ) {
		// TODO Implement + test trapezoidal method
		g_eq_new = 2.*m_cap/m_delta;
	}
	
	if ( g_eq_old != g_eq_new )
	{
		A_g( 0, 0 ) += g_eq_new-g_eq_old;
		A_g( 1, 1 ) += g_eq_new-g_eq_old;
		A_g( 0, 1 ) -= g_eq_new-g_eq_old;
		A_g( 1, 0 ) -= g_eq_new-g_eq_old;
	}
	
	if ( i_eq_new != i_eq_old )
	{
		b_i( 0 ) -= i_eq_new-i_eq_old;
		b_i( 1 ) += i_eq_new-i_eq_old;
	}
	
	g_eq_old = g_eq_new;
	i_eq_old = i_eq_new;
}

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

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