summaryrefslogtreecommitdiffstatshomepage
path: root/src/lib/netlist/analog/nlid_twoterm.cpp
blob: f59c366f2ee9d2b20af5a1ddb103329535bc8128 (plain) (blame)
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
// license:GPL-2.0+
// copyright-holders:Couriersud

#include "netlist/solver/nld_solver.h"

#include "netlist/nl_factory.h"
#include "nlid_twoterm.h"

namespace netlist
{
	namespace analog
	{

// ----------------------------------------------------------------------------------------
// nld_twoterm
// ----------------------------------------------------------------------------------------

void NETLIB_NAME(twoterm)::solve_now()
{
	// we only need to call the non-rail terminal
	if (m_P.has_net() && !m_P.net().isRailNet())
		m_P.solve_now();
	else if (m_N.has_net() && !m_N.net().isRailNet())
		m_N.solve_now();
}

void NETLIB_NAME(twoterm)::solve_later(netlist_time delay)
{
	// we only need to call the non-rail terminal
	if (m_P.has_net() && !m_P.net().isRailNet())
		m_P.schedule_solve_after(delay);
	else if (m_N.has_net() && !m_N.net().isRailNet())
		m_N.schedule_solve_after(delay);
}


NETLIB_UPDATE(twoterm)
{
	// only called if connected to a rail net ==> notify the solver to recalculate
	solve_now();
}

// ----------------------------------------------------------------------------------------
// nld_R_base
// ----------------------------------------------------------------------------------------

NETLIB_RESET(R_base)
{
	NETLIB_NAME(twoterm)::reset();
	set_R(plib::reciprocal(exec().gmin()));
}

// ----------------------------------------------------------------------------------------
// nld_POT
// ----------------------------------------------------------------------------------------

NETLIB_RESET(POT)
{
	nl_fptype v = m_Dial();
	if (m_DialIsLog())
		v = (plib::exp(v) - nlconst::one()) / (plib::exp(nlconst::one()) - nlconst::one());

	m_R1.set_R(std::max(m_R() * v, exec().gmin()));
	m_R2.set_R(std::max(m_R() * (nlconst::one() - v), exec().gmin()));
}

NETLIB_UPDATE_PARAM(POT)
{
	m_R1.solve_now();
	m_R2.solve_now();

	nl_fptype v = m_Dial();
	if (m_DialIsLog())
		v = (plib::exp(v) - nlconst::one()) / (plib::exp(nlconst::one()) - nlconst::one());
	if (m_Reverse())
		v = nlconst::one() - v;
	m_R1.set_R(std::max(m_R() * v, exec().gmin()));
	m_R2.set_R(std::max(m_R() * (nlconst::one() - v), exec().gmin()));

}

// ----------------------------------------------------------------------------------------
// nld_POT2
// ----------------------------------------------------------------------------------------

NETLIB_RESET(POT2)
{
	nl_fptype v = m_Dial();

	if (m_DialIsLog())
		v = (plib::exp(v) - nlconst::one()) / (plib::exp(nlconst::one()) - nlconst::one());
	if (m_Reverse())
		v = nlconst::one() - v;
	m_R1.set_R(std::max(m_R() * v, exec().gmin()));
}


NETLIB_UPDATE_PARAM(POT2)
{
	m_R1.solve_now();

	nl_fptype v = m_Dial();

	if (m_DialIsLog())
		v = (plib::exp(v) - nlconst::one()) / (plib::exp(nlconst::one()) - nlconst::one());
	if (m_Reverse())
		v = nlconst::one() - v;
	m_R1.set_R(std::max(m_R() * v, exec().gmin()));
}

// ----------------------------------------------------------------------------------------
// nld_L
// ----------------------------------------------------------------------------------------

NETLIB_RESET(L)
{
	m_gmin = exec().gmin();
	m_I = nlconst::zero();
	m_G = m_gmin;
	set_mat( m_G, -m_G, -m_I,
			-m_G,  m_G,  m_I);
	//set(1.0/NETLIST_GMIN, 0.0, -5.0 * NETLIST_GMIN);
}

NETLIB_UPDATE_PARAM(L)
{
}

NETLIB_TIMESTEP(L)
{
	// Gpar should support convergence
	m_I += m_I + m_G * deltaV();
	m_G = step / m_L() + m_gmin;
	set_mat( m_G, -m_G, -m_I,
			-m_G,  m_G,  m_I);
}

// ----------------------------------------------------------------------------------------
// nld_D
// ----------------------------------------------------------------------------------------

NETLIB_RESET(D)
{
	nl_fptype Is = m_model.m_IS;
	nl_fptype n = m_model.m_N;

	m_D.set_param(Is, n, exec().gmin(), nlconst::T0());
	set_G_V_I(m_D.G(), nlconst::zero(), m_D.Ieq());
}

NETLIB_UPDATE_PARAM(D)
{
	nl_fptype Is = m_model.m_IS;
	nl_fptype n = m_model.m_N;

	m_D.set_param(Is, n, exec().gmin(), nlconst::T0());
}

NETLIB_UPDATE_TERMINALS(D)
{
	m_D.update_diode(deltaV());
	const nl_fptype G = m_D.G();
	const nl_fptype I = m_D.Ieq();
	set_mat( G, -G, -I,
			-G,  G,  I);
	//set(m_D.G(), 0.0, m_D.Ieq());
}


	} //namespace analog

	namespace devices {
		NETLIB_DEVICE_IMPL_NS(analog, R,    "RES",   "R")
		NETLIB_DEVICE_IMPL_NS(analog, POT,  "POT",   "R")
		NETLIB_DEVICE_IMPL_NS(analog, POT2, "POT2",  "R")
		NETLIB_DEVICE_IMPL_NS(analog, C,    "CAP",   "C")
		NETLIB_DEVICE_IMPL_NS(analog, L,    "IND",   "L")
		NETLIB_DEVICE_IMPL_NS(analog, D,    "DIODE", "MODEL")
		NETLIB_DEVICE_IMPL_NS(analog, VS,   "VS",    "V")
		NETLIB_DEVICE_IMPL_NS(analog, CS,   "CS",    "I")
	} // namespace devices

} // namespace netlist