summaryrefslogtreecommitdiffstats
path: root/src/lib/netlist/analog/nlid_twoterm.cpp
blob: 3d5d0721dfb29400c914e54f5ac0c6c1880b0078 (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
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
// license:BSD-3-Clause
// copyright-holders:Couriersud

#include "nlid_twoterm.h"

#include "nl_factory.h"

#include "solver/nld_solver.h"

namespace netlist::analog
{

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

	solver::matrix_solver_t *nld_two_terminal::solver() const noexcept
	{
		auto *solv(m_P.solver());
		if (solv != nullptr)
			return solv;
		return m_N.solver();
	}

	void nld_two_terminal::solve_now() const
	{
		auto *solv(solver());
		if (solv != nullptr)
			solv->solve_now();
	}

	NETLIB_HANDLER(two_terminal, terminal_handler)
	{
		// only called if connected to a rail net ==> notify the solver to
		// recalculate
		// printf("%s update\n", this->name().c_str());
		solve_now();
	}

	// -------------------------------------------------------------------------
	// 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)
	{
		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;

		nl_fptype r1(std::max(m_R() * v, exec().gmin()));
		nl_fptype r2(std::max(m_R() * (nlconst::one() - v), exec().gmin()));

		if (m_R1().solver() == m_R2().solver())
			m_R1().change_state(
				[this, &r1, &r2]()
				{
					m_R1().set_R(r1);
					m_R2().set_R(r2);
				});
		else
		{
			m_R1().change_state([this, &r1]() { m_R1().set_R(r1); });
			m_R2().change_state([this, &r2]() { m_R2().set_R(r2); });
		}
	}

	// -------------------------------------------------------------------------
	// 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)
	{
		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().change_state(
			[this, &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);
	}

	NETLIB_UPDATE_PARAM(L) {}

	NETLIB_TIMESTEP(L)
	{
		if (ts_type == time_step_type::FORWARD)
		{
			m_last_I = m_I;
			m_last_G = m_G;
			// Gpar should support convergence
			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);
		}
		else
		{
			m_I = m_last_I;
			m_G = m_last_G;
		}
	}

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

	NETLIB_RESET(D)
	{
		nl_fptype Is = m_modacc.m_IS;
		nl_fptype n = m_modacc.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_modacc.m_IS;
		nl_fptype n = m_modacc.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());
	}

	// -------------------------------------------------------------------------
	// nld_Z
	// -------------------------------------------------------------------------

	NETLIB_RESET(Z)
	{
		nl_fptype IsBV = m_modacc.m_IBV
						 / (plib::exp(m_modacc.m_BV
									  / nlconst::np_VT(m_modacc.m_NBV))
							- nlconst::one());

		m_D.set_param(m_modacc.m_IS, m_modacc.m_N, exec().gmin(),
					  nlconst::T0());
		m_R.set_param(IsBV, m_modacc.m_NBV, exec().gmin(), nlconst::T0());
		set_G_V_I(m_D.G(), nlconst::zero(), m_D.Ieq());
	}

	NETLIB_UPDATE_PARAM(Z)
	{
		nl_fptype IsBV = m_modacc.m_IBV
						 / (plib::exp(m_modacc.m_BV
									  / nlconst::np_VT(m_modacc.m_NBV))
							- nlconst::one());

		m_D.set_param(m_modacc.m_IS, m_modacc.m_N, exec().gmin(),
					  nlconst::T0());
		m_R.set_param(IsBV, m_modacc.m_NBV, exec().gmin(), nlconst::T0());
		set_G_V_I(m_D.G(), nlconst::zero(), m_D.Ieq());
	}

	NETLIB_UPDATE_TERMINALS(Z)
	{
		m_D.update_diode(deltaV());
		m_R.update_diode(-deltaV());
		const nl_fptype G(m_D.G() + m_R.G());
		const nl_fptype I(m_D.Ieq() - m_R.Ieq());
		set_mat(G, -G, -I, //
				-G, G, I);
	}

} // namespace netlist::analog

namespace netlist::devices
{
	// clang-format off
	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, Z,    "ZDIODE", "MODEL")
	NETLIB_DEVICE_IMPL_NS(analog, VS,   "VS",    "V")
	NETLIB_DEVICE_IMPL_NS(analog, CS,   "CS",    "I")
	// clang-format on
} // namespace netlist::devices