summaryrefslogtreecommitdiffstatshomepage
path: root/src/lib/netlist/analog/nlid_twoterm.h
blob: 8a9f8110aa69f53dbb9b67c3a7a6f2d78f558910 (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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
// license:GPL-2.0+
// copyright-holders:Couriersud

#ifndef NLID_TWOTERM_H_
#define NLID_TWOTERM_H_

///
/// \file nlid_twoterm.h
///
/// Devices with two terminals ...
///
///
///       (k)
///  +-----T-----+
///  |     |     |
///  |  +--+--+  |
///  |  |     |  |
///  |  R     |  |
///  |  R     |  |
///  |  R     I  |
///  |  |     I  |  Device n
///  |  V+    I  |
///  |  V     |  |
///  |  V-    |  |
///  |  |     |  |
///  |  +--+--+  |
///  |     |     |
///  +-----T-----+
///       (l)
///
///  This is a resistance in series to a voltage source and paralleled by a
///  current source. This is suitable to model voltage sources, current sources,
///  resistors, capacitors, inductances and diodes.
///
////

#include "netlist/nl_base.h"
#include "netlist/nl_setup.h"
#include "netlist/plib/pfunction.h"
#include "netlist/solver/nld_solver.h"
#include "nld_generic_models.h"

// -----------------------------------------------------------------------------
// Implementation
// -----------------------------------------------------------------------------

namespace netlist
{
namespace analog
{

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

	template <class C>
	inline core_device_t &bselect(bool b, C &d1, core_device_t &d2)
	{
		auto *h = dynamic_cast<core_device_t *>(&d1);
		return b ? *h : d2;
	}
	template<>
	inline core_device_t &bselect(bool b, netlist_state_t &d1, core_device_t &d2)
	{
		plib::unused_var(d1);
		if (b)
			throw nl_exception("bselect with netlist and b==true");
		return d2;
	}

	NETLIB_OBJECT(twoterm)
	{
		// FIXME locate use case of owned = true and eliminate them if possible
		NETLIB_CONSTRUCTOR_EX(twoterm, bool terminals_owned = false)
		, m_P(bselect(terminals_owned, owner, *this), (terminals_owned ? name + "." : "") + "1", &m_N)
		, m_N(bselect(terminals_owned, owner, *this), (terminals_owned ? name + "." : "") + "2", &m_P)
		{
		}

		//NETLIB_UPDATE_TERMINALSI() { }
		//NETLIB_RESETI() { }

	public:

		NETLIB_UPDATEI();

		solver::matrix_solver_t *solver() const noexcept;

		void solve_now() const;

		template <typename F>
		void change_state(F f, netlist_time delay = netlist_time::quantum())
		{
			auto *solv(solver());
			if (solv)
				solv->change_state(f, delay);
		}

		void set_G_V_I(nl_fptype G, nl_fptype V, nl_fptype I) const noexcept
		{
			//               GO, GT,        I
			m_P.set_go_gt_I( -G,  G, (  V) * G - I);
			m_N.set_go_gt_I( -G,  G, ( -V) * G + I);
		}

		nl_fptype deltaV() const noexcept
		{
			return m_P.net().Q_Analog() - m_N.net().Q_Analog();
		}

		nl_fptype V1P() const noexcept
		{
			return m_P.net().Q_Analog();
		}

		nl_fptype V2N() const noexcept
		{
			return m_N.net().Q_Analog();
		}

		void set_mat(nl_fptype a11, nl_fptype a12, nl_fptype rhs1,
					 nl_fptype a21, nl_fptype a22, nl_fptype rhs2) const noexcept
		{
			//               GO,  GT,     I
			m_P.set_go_gt_I(a12, a11, rhs1);
			m_N.set_go_gt_I(a21, a22, rhs2);
		}

		/// \brief Get a reference to the m_P terminal
		///
		/// This is typically called during initialization to connect
		/// terminals.
		///
		/// \returns Reference to m_P terminal.
		terminal_t &P() noexcept { return m_P; }

		/// \brief Get a reference to the m_N terminal
		///
		/// This is typically called during initialization to connect
		/// terminals.
		///
		/// \returns Reference to m_N terminal.
		terminal_t &N() noexcept { return m_N; }

	private:
		terminal_t m_P;
		terminal_t m_N;

	};


	// -----------------------------------------------------------------------------
	// nld_R
	// -----------------------------------------------------------------------------

	NETLIB_OBJECT_DERIVED(R_base, twoterm)
	{
		NETLIB_CONSTRUCTOR_DERIVED(R_base, twoterm)
		{
		}

		void set_R(nl_fptype R) const noexcept
		{
			const nl_fptype G = plib::reciprocal(R);
			set_mat( G, -G, nlconst::zero(),
					-G,  G, nlconst::zero());
		}

		NETLIB_RESETI();

	protected:
		//NETLIB_UPDATEI();

	};

	NETLIB_OBJECT_DERIVED(R, R_base)
	{
		NETLIB_CONSTRUCTOR_DERIVED(R, R_base)
		, m_R(*this, "R", nlconst::magic(1e9))
		{
		}


	protected:

		//NETLIB_UPDATEI() { }
		NETLIB_RESETI()
		{
			NETLIB_NAME(twoterm)::reset();
			set_R(std::max(m_R(), exec().gmin()));
		}

		NETLIB_UPDATE_PARAMI()
		{
			// FIXME: We only need to update the net first if this is a time stepping net
			change_state([this]()
			{
				set_R(std::max(m_R(), exec().gmin()));
			});
		}

	private:
		param_fp_t m_R;
		// protect set_R ... it's a recipe to desaster when used to bypass the parameter
		using NETLIB_NAME(R_base)::set_R;
	};

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

	NETLIB_OBJECT(POT)
	{
		NETLIB_CONSTRUCTOR(POT)
		, m_R1(*this, "_R1")
		, m_R2(*this, "_R2")
		, m_R(*this, "R", 10000)
		, m_Dial(*this, "DIAL", nlconst::half())
		, m_DialIsLog(*this, "DIALLOG", false)
		, m_Reverse(*this, "REVERSE", false)
		{
			register_subalias("1", m_R1.P());
			register_subalias("2", m_R1.N());
			register_subalias("3", m_R2.N());

			connect(m_R2.P(), m_R1.N());

		}

		//NETLIB_UPDATEI();
		NETLIB_RESETI();
		NETLIB_UPDATE_PARAMI();

	private:
		NETLIB_SUB(R_base) m_R1;
		NETLIB_SUB(R_base) m_R2;

		param_fp_t m_R;
		param_fp_t m_Dial;
		param_logic_t m_DialIsLog;
		param_logic_t m_Reverse;
	};

	NETLIB_OBJECT(POT2)
	{
		NETLIB_CONSTRUCTOR(POT2)
		, m_R1(*this, "_R1")
		, m_R(*this, "R", nlconst::magic(10000.0))
		, m_Dial(*this, "DIAL", nlconst::half())
		, m_DialIsLog(*this, "DIALLOG", false)
		, m_Reverse(*this, "REVERSE", false)
		{
			register_subalias("1", m_R1.P());
			register_subalias("2", m_R1.N());

		}

		//NETLIB_UPDATEI();
		NETLIB_RESETI();
		NETLIB_UPDATE_PARAMI();

	private:
		NETLIB_SUB(R_base) m_R1;

		param_fp_t m_R;
		param_fp_t m_Dial;
		param_logic_t m_DialIsLog;
		param_logic_t m_Reverse;
	};

	// -----------------------------------------------------------------------------
	// nld_C
	// -----------------------------------------------------------------------------
#if 1
	NETLIB_OBJECT_DERIVED(C, twoterm)
	{
	public:
		NETLIB_CONSTRUCTOR_DERIVED(C, twoterm)
		, m_C(*this, "C", nlconst::magic(1e-6))
		, m_cap(*this, "m_cap")
		{
		}

		NETLIB_IS_TIMESTEP(true)
		NETLIB_TIMESTEPI()
		{
			// G, Ieq
			const auto res(m_cap.timestep(m_C(), deltaV(), step));
			const nl_fptype G = res.first;
			const nl_fptype I = res.second;
			set_mat( G, -G, -I,
					-G,  G,  I);
		}

		NETLIB_RESETI()
		{
			m_cap.setparams(exec().gmin());
		}

		/// \brief Set capacitance
		///
		/// This call will set the capacitance. The typical use case are
		/// are components like BJTs which use this component to model
		/// internal capacitances. Typically called during initialization.
		///
		/// \param val Capacitance value
		///
		void set_cap_embedded(nl_fptype val)
		{
			m_C.set(val);
		}

	protected:
		//NETLIB_UPDATEI();
		//FIXME: should be able to change
		NETLIB_UPDATE_PARAMI() { }

	private:
		param_fp_t m_C;
		generic_capacitor_const m_cap;
	};

#else
	// Code preserved as a basis for a current/voltage controlled capacitor
	NETLIB_OBJECT_DERIVED(C, twoterm)
	{
	public:
		NETLIB_CONSTRUCTOR_DERIVED(C, twoterm)
		, m_C(*this, "C", nlconst::magic(1e-6))
		, m_cap(*this, "m_cap")
		{
		}

		NETLIB_IS_TIMESTEP(true)
		NETLIB_TIMESTEPI()
		{
			m_cap.timestep(m_C(), deltaV(), step);
			if (m_cap.type() == capacitor_e::CONSTANT_CAPACITY)
			{
				const nl_fptype I = m_cap.Ieq(m_C(), deltaV());
				const nl_fptype G = m_cap.G(m_C());
				set_mat( G, -G, -I,
						-G,  G,  I);
			}
		}

		NETLIB_IS_DYNAMIC(m_cap.type() == capacitor_e::VARIABLE_CAPACITY)
		NETLIB_UPDATE_TERMINALSI()
		{
			const nl_fptype I = m_cap.Ieq(m_C(), deltaV());
			const nl_fptype G = m_cap.G(m_C());
			set_mat( G, -G, -I,
					-G,  G,  I);
		}

		param_fp_t m_C;
		NETLIB_RESETI()
		{
			m_cap.setparams(exec().gmin());
		}

	protected:
		//NETLIB_UPDATEI();
		//FIXME: should be able to change
		NETLIB_UPDATE_PARAMI() { }

	private:
		//generic_capacitor<capacitor_e::VARIABLE_CAPACITY> m_cap;
		generic_capacitor<capacitor_e::CONSTANT_CAPACITY> m_cap;
	};
#endif
	// -----------------------------------------------------------------------------
	// nld_L
	// -----------------------------------------------------------------------------

	NETLIB_OBJECT_DERIVED(L, twoterm)
	{
	public:
		NETLIB_CONSTRUCTOR_DERIVED(L, twoterm)
		, m_L(*this, "L", nlconst::magic(1e-6))
		, m_gmin(nlconst::zero())
		, m_G(nlconst::zero())
		, m_I(nlconst::zero())
		{
			//register_term("1", m_P);
			//register_term("2", m_N);
		}

		NETLIB_IS_TIMESTEP(true)
		NETLIB_TIMESTEPI();
		NETLIB_RESETI();

	protected:
		//NETLIB_UPDATEI();
		NETLIB_UPDATE_PARAMI();

	private:
		param_fp_t m_L;

		nl_fptype m_gmin;
		nl_fptype m_G;
		nl_fptype m_I;
	};

	/// \brief Class representing the diode model paramers.
	///
	///  This is the model representation of the diode model. Typically, SPICE uses
	///  the following parameters. A "Y" in the first column indicates that the
	///  parameter is actually used in netlist.
	///
	///   |NL? |name  |parameter                        |units|default| example|area  |
	///   |:--:|:-----|:--------------------------------|:----|------:|-------:|:----:|
	///   | Y  |IS    |saturation current               |A    |1.0e-14| 1.0e-14|   *  |
	///   |    |RS    |ohmic resistance                 |Ohm  |      0|      10|   *  |
	///   | Y  |N     |emission coefficient             |-    |      1|       1|      |
	///   |    |TT    |transit-time                     |sec  |      0|   0.1ns|      |
	///   |    |CJO   |zero-bias junction capacitance   |F    |      0|     2pF|   *  |
	///   |    |VJ    |junction potential               |V    |      1|     0.6|      |
	///   |    |M     |grading coefficient              |-    |    0.5|     0.5|      |
	///   |    |EG    |band-gap energy                  |eV   |   1.11| 1.11 Si|      |
	///   |    |XTI   |saturation-current temp.exp      |-    |      3|3.0 pn. 2.0 Schottky| |
	///   |    |KF    |flicker noise coefficient        |-    |      0|        |      |
	///   |    |AF    |flicker noise exponent           |-    |      1|        |      |
	///   |    |FC    |coefficient for forward-bias depletion capacitance formula|-|0.5|| |
	///   |    |BV    |reverse breakdown voltage        |V    |infinite|     40|      |
	///   |    |IBV   |current at breakdown voltage     |V    |  0.001|        |      |
	///   |    |TNOM  |parameter measurement temperature|deg C|     27|      50|      |
	///
	class diode_model_t : public param_model_t
	{
	public:
		diode_model_t(device_t &device, const pstring &name, const pstring &val)
		: param_model_t(device, name, val)
		, m_IS(*this, "IS")
		, m_N(*this, "N")
		{}

		value_t m_IS;    //!< saturation current.
		value_t m_N;     //!< emission coefficient.
	};

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

	NETLIB_OBJECT_DERIVED(D, twoterm)
	{
	public:
		NETLIB_CONSTRUCTOR_DERIVED_EX(D, twoterm, const pstring &model = "D")
		, m_model(*this, "MODEL", model)
		, m_D(*this, "m_D")
		{
			register_subalias("A", P());
			register_subalias("K", N());
		}

		NETLIB_IS_DYNAMIC(true)
		NETLIB_UPDATE_TERMINALSI();
		NETLIB_RESETI();

	protected:
		//NETLIB_UPDATEI();
		NETLIB_UPDATE_PARAMI();

	private:
		diode_model_t m_model;
		generic_diode<diode_e::BIPOLAR> m_D;
	};

	// -----------------------------------------------------------------------------
	// nld_VS - Voltage source
	//
	// netlist voltage source must have inner resistance
	// -----------------------------------------------------------------------------

	NETLIB_OBJECT_DERIVED(VS, twoterm)
	{
	public:
		NETLIB_CONSTRUCTOR_DERIVED(VS, twoterm)
		, m_t(*this, "m_t", nlconst::zero())
		, m_R(*this, "R", nlconst::magic(0.1))
		, m_V(*this, "V", nlconst::zero())
		, m_func(*this,"FUNC", "")
		, m_compiled(this->name() + ".FUNCC", this, this->state().run_state_manager())
		, m_funcparam({nlconst::zero()})
		{
			register_subalias("P", P());
			register_subalias("N", N());
			if (m_func() != "")
				m_compiled.compile(m_func(), std::vector<pstring>({{pstring("T")}}));
		}

		NETLIB_IS_TIMESTEP(m_func() != "")

		NETLIB_TIMESTEPI()
		{
			m_t += step;
			m_funcparam[0] = m_t;
			this->set_G_V_I(plib::reciprocal(m_R()),
					m_compiled.evaluate(m_funcparam),
					nlconst::zero());
		}

	protected:

		NETLIB_RESETI()
		{
			NETLIB_NAME(twoterm)::reset();
			this->set_G_V_I(plib::reciprocal(m_R()), m_V(), nlconst::zero());
		}

	private:
		state_var<nl_fptype> m_t;
		param_fp_t m_R;
		param_fp_t m_V;
		param_str_t m_func;
		plib::pfunction<nl_fptype> m_compiled;
		std::vector<nl_fptype> m_funcparam;
	};

	// -----------------------------------------------------------------------------
	// nld_CS - Current source
	// -----------------------------------------------------------------------------

	NETLIB_OBJECT_DERIVED(CS, twoterm)
	{
	public:
		NETLIB_CONSTRUCTOR_DERIVED(CS, twoterm)
		, m_t(*this, "m_t", nlconst::zero())
		, m_I(*this, "I", nlconst::one())
		, m_func(*this,"FUNC", "")
		, m_compiled(this->name() + ".FUNCC", this, this->state().run_state_manager())
		, m_funcparam({nlconst::zero()})
		{
			register_subalias("P", P());
			register_subalias("N", N());
			if (m_func() != "")
				m_compiled.compile(m_func(), std::vector<pstring>({{pstring("T")}}));
		}

		NETLIB_IS_TIMESTEP(m_func() != "")
		NETLIB_TIMESTEPI()
		{
			m_t += step;
			m_funcparam[0] = m_t;
			const nl_fptype I = m_compiled.evaluate(m_funcparam);
			const auto zero(nlconst::zero());
			set_mat(zero, zero, -I,
					zero, zero,  I);
		}

	protected:

		NETLIB_RESETI()
		{
			NETLIB_NAME(twoterm)::reset();
			const auto zero(nlconst::zero());
			set_mat(zero, zero, -m_I(),
					zero, zero,  m_I());
		}

		NETLIB_UPDATE_PARAMI()
		{
			// FIXME: We only need to update the net first if this is a time stepping net
			//FIXME: works only for CS without function
			change_state([this]()
			{
				const auto zero(nlconst::zero());
				set_mat(zero, zero, -m_I(),
						zero, zero,  m_I());
			});
		}

	private:
		state_var<nl_fptype> m_t;
		param_fp_t m_I;
		param_str_t m_func;
		plib::pfunction<nl_fptype> m_compiled;
		std::vector<nl_fptype> m_funcparam;
	};

} // namespace analog
} // namespace netlist

#endif // NLD_TWOTERM_H_