summaryrefslogtreecommitdiffstatshomepage
path: root/src/emu/netlist/analog/nld_solver.c
blob: 56cead68c5c936ad989771e5b926f30d3f6f089b (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
/*
 * nld_solver.c
 *
 */

#include "nld_solver.h"
#include "nld_twoterm.h"

#if HAS_OPENMP
#include "omp.h"
#endif

// ----------------------------------------------------------------------------------------
// netlist_matrix_solver
// ----------------------------------------------------------------------------------------

#define SOLVER_VERBOSE_OUT(x) do {} while (0)
//#define SOLVER_VERBOSE_OUT(x) printf x

ATTR_COLD void netlist_matrix_solver_t::setup(netlist_net_t::list_t &nets, NETLIB_NAME(solver) &aowner)
{
    /* make sure we loop at least once */
    m_resched = true;

	m_owner = &aowner;
	for (netlist_net_t * const * pn = nets.first(); pn != NULL; pn = nets.next(pn))
	{
		NL_VERBOSE_OUT(("setting up net\n"));

	    m_nets.add(*pn);

	    (*pn)->m_solver = this;

		for (netlist_core_terminal_t *p = (*pn)->m_head; p != NULL; p = p->m_update_list_next)
		{
			switch (p->type())
			{
				case netlist_terminal_t::TERMINAL:
					switch (p->netdev().family())
					{
						case netlist_device_t::CAPACITOR:
							if (!m_steps.contains(&p->netdev()))
								m_steps.add(&p->netdev());
							break;
						case netlist_device_t::BJT_EB:
                        case netlist_device_t::DIODE:
						//case netlist_device_t::VCVS:
						//case netlist_device_t::BJT_SWITCH:
							if (!m_dynamic.contains(&p->netdev()))
								m_dynamic.add(&p->netdev());
							break;
						default:
							break;
					}
					(*pn)->m_terms.add(static_cast<netlist_terminal_t *>(p));
					NL_VERBOSE_OUT(("Added terminal\n"));
					break;
				case netlist_terminal_t::INPUT:
					if (!m_inps.contains(p))
						m_inps.add(p);
					NL_VERBOSE_OUT(("Added input\n"));
					break;
				default:
					owner().netlist().error("unhandled element found\n");
					break;
			}
		}
	}
}

ATTR_HOT inline void netlist_matrix_solver_t::step(const netlist_time delta)
{
	const double dd = delta.as_double();
	for (netlist_core_device_t * const *p = m_steps.first(); p != NULL; p = m_steps.next(p))
		(*p)->step_time(dd);
}

ATTR_HOT inline void netlist_matrix_solver_t::update_inputs()
{
	for (netlist_core_terminal_t * const *p = m_inps.first(); p != NULL; p = m_inps.next(p))
	{
	    if ((*p)->net().m_last.Analog != (*p)->net().m_cur.Analog)
	    {
	        (*p)->netdev().update_dev();
	    }
	}
    for (netlist_core_terminal_t * const *p = m_inps.first(); p != NULL; p = m_inps.next(p))
    {
        (*p)->net().m_last.Analog = (*p)->net().m_cur.Analog;
    }

}

ATTR_HOT inline int netlist_matrix_solver_t::solve_non_dynamic()
{
    bool resched = false;

    int  resched_cnt = 0;
    ATTR_UNUSED netlist_net_t *last_resched_net = NULL;

    /* over-relaxation not really works on these matrices */
    const double w = 1.0; //2.0 / (1.0 + sin(3.14159 / (m_nets.count()+1)));
    const double w1 = 1.0 - w;

    //printf("%f %d\n", w, m_nets.count());
    do {
        resched = false;

        for (netlist_net_t * const *pn = m_nets.first(); pn != NULL; pn = m_nets.next(pn))
        {
            netlist_net_t *net = *pn;
            const netlist_net_t::terminal_list_t &terms = net->m_terms;

            double gtot = 0;
            double gabs = 0;
            double iIdr = 0;
            double new_val;

            for (int i = 0; i < terms.count(); i++)
            {
                gtot += terms[i]->m_gt;
                gabs += fabs(terms[i]->m_go);
                iIdr += terms[i]->m_Idr + terms[i]->m_go * terms[i]->m_otherterm->net().Q_Analog();
            }
            gabs *= m_convergence_factor;
            if (gabs > gtot)
                new_val = (net->m_cur.Analog * gabs + iIdr) / (gtot + gabs);
            else
                new_val = w1 * net->m_cur.Analog + w * iIdr / gtot;

            if (fabs(new_val - net->m_cur.Analog) > m_accuracy)
            {
                resched = true;
                last_resched_net = net;
            }

            net->m_cur.Analog = net->m_new.Analog = new_val;

            NL_VERBOSE_OUT(("Info: %d\n", pn->object()->m_num_cons));
            //NL_VERBOSE_OUT(("New: %lld %f %f\n", netlist().time().as_raw(), netlist().time().as_double(), new_val));
        }
        resched_cnt++;
    } while (resched && (resched_cnt < m_resched_loops / 3 ));

    return resched_cnt;
}


ATTR_HOT inline bool netlist_matrix_solver_t::solve()
{
    int  resched_cnt = 0;
    ATTR_UNUSED netlist_net_t *last_resched_net = NULL;

    m_resched = false;

    if (is_dynamic())
    {
        int this_resched;
        do
        {
            /* update all non-linear devices  */
            for (netlist_core_device_t * const *p = m_dynamic.first(); p != NULL; p = m_dynamic.next(p))
                switch ((*p)->family())
                {
                    case netlist_device_t::DIODE:
                        static_cast<NETLIB_NAME(D) *>((*p))->update_terminals();
                        break;
                    default:
                        (*p)->update_terminals();
                        break;
                }
            this_resched = solve_non_dynamic();
            resched_cnt += this_resched;
        } while (this_resched > 1 && resched_cnt < m_resched_loops);
    }
    else
    {
        resched_cnt = solve_non_dynamic();
    }
    if (resched_cnt >= m_resched_loops)
        m_resched = true;

    //if (resched)
        //printf("Resched on net %s first term %s\n", last_resched_net->name().cstr(), last_resched_net->m_terms[0]->name().cstr());

	return m_resched;
}

// ----------------------------------------------------------------------------------------
// solver
// ----------------------------------------------------------------------------------------

typedef netlist_net_t::list_t  *net_groups_t;

ATTR_COLD static bool already_processed(net_groups_t groups, int &cur_group, netlist_net_t *net)
{
	if (net->isRailNet())
		return true;
	for (int i = 0; i <= cur_group; i++)
	{
		if (groups[i].contains(net))
			return true;
	}
	return false;
}

ATTR_COLD static void process_net(net_groups_t groups, int &cur_group, netlist_net_t *net)
{
	if (net->m_head == NULL)
		return;
    /* add the net */
	groups[cur_group].add(net);
	for (netlist_core_terminal_t *p = net->m_head; p != NULL; p = p->m_update_list_next)
	{
		if (p->isType(netlist_terminal_t::TERMINAL))
		{
			netlist_terminal_t *pt = static_cast<netlist_terminal_t *>(p);
			netlist_net_t *other_net = &pt->m_otherterm->net();
			if (!already_processed(groups, cur_group, other_net))
				process_net(groups, cur_group, other_net);
		}
	}
}


NETLIB_START(solver)
{
	register_output("Q_sync", m_Q_sync);
	register_output("Q_step", m_Q_step);
	//register_input("FB", m_feedback);

	register_param("SYNC_DELAY", m_sync_delay, NLTIME_FROM_NS(10).as_double());
	m_nt_sync_delay = m_sync_delay.Value();

	register_param("FREQ", m_freq, 48000.0);
	m_inc = netlist_time::from_hz(m_freq.Value());

    //register_param("ACCURACY", m_accuracy, 1e-3);
	register_param("ACCURACY", m_accuracy, 1e-6);
	register_param("CONVERG", m_convergence, 0.3);
    register_param("RESCHED_LOOPS", m_resched_loops, 35);

	// internal staff

	register_input("FB_sync", m_fb_sync, netlist_input_t::STATE_INP_ACTIVE);
	register_input("FB_step", m_fb_step, netlist_input_t::STATE_INP_ACTIVE);

	setup().connect(m_fb_sync, m_Q_sync);
	setup().connect(m_fb_step, m_Q_step);

	m_last_step = netlist_time::zero;

	save(NAME(m_last_step));

}

NETLIB_UPDATE_PARAM(solver)
{
	m_inc = netlist_time::from_hz(m_freq.Value());
}

NETLIB_NAME(solver)::~NETLIB_NAME(solver)()
{
	netlist_matrix_solver_t * const *e = m_mat_solvers.first();
	while (e != NULL)
	{
		netlist_matrix_solver_t * const *en = m_mat_solvers.next(e);
		delete *e;
		e = en;
	}

}

NETLIB_UPDATE(solver)
{
	netlist_time now = netlist().time();
	netlist_time delta = now - m_last_step;

	if (delta >= m_inc)
	{
		NL_VERBOSE_OUT(("Step!\n"));
		/* update all terminals for new time step */
		m_last_step = now;
		for (netlist_matrix_solver_t * const *e = m_mat_solvers.first(); e != NULL; e = m_mat_solvers.next(e))
		{
			(*e)->step(delta);
		}
	}
	bool global_resched = false;
	bool this_resched[100];

#if HAS_OPENMP && USE_OPENMP
    int t_cnt = m_mat_solvers.count();
    omp_set_num_threads(3);
    omp_set_dynamic(0);
    #pragma omp parallel
    {
        int i;
        #pragma omp for nowait
        for (i = 0; i <  t_cnt; i++)
        {
            this_resched[i] = m_mat_solvers[i]->solve();
        }
    }
#else
    for (int i = 0; i < m_mat_solvers.count(); i++)
    {
        this_resched[i] = m_mat_solvers[i]->solve();
    }
#endif

    for (int i = 0; i <  m_mat_solvers.count(); i++)
    {
        global_resched = global_resched || this_resched[i];
        if (!this_resched[i])
            m_mat_solvers[i]->update_inputs();
    }
	if (global_resched)
	{
		schedule();
	}
	else
	{
		/* step circuit */
		if (!m_Q_step.net().is_queued())
			m_Q_step.net().push_to_queue(m_inc);
	}

}

ATTR_COLD void NETLIB_NAME(solver)::post_start()
{
    netlist_net_t::list_t groups[100];
    int cur_group = -1;

    SOLVER_VERBOSE_OUT(("Scanning net groups ...\n"));
    // determine net groups
    for (netlist_net_t * const *pn = netlist().m_nets.first(); pn != NULL; pn = netlist().m_nets.next(pn))
    {
        if (!already_processed(groups, cur_group, *pn))
        {
            cur_group++;
            process_net(groups, cur_group, *pn);
        }
    }

    // setup the solvers
    SOLVER_VERBOSE_OUT(("Found %d net groups in %d nets\n", cur_group + 1, netlist().m_nets.count()));
    for (int i = 0; i <= cur_group; i++)
    {
        netlist_matrix_solver_t *ms = new netlist_matrix_solver_t();
        ms->m_accuracy = m_accuracy.Value();
        ms->m_convergence_factor = m_convergence.Value();
        ms->m_resched_loops = m_resched_loops.Value();
        ms->setup(groups[i], *this);
        m_mat_solvers.add(ms);
        SOLVER_VERBOSE_OUT(("%d ==> %d nets %s\n", i, groups[i].count(), groups[i].first()->object()->m_head->name().cstr()));
        SOLVER_VERBOSE_OUT(("       has %s elements\n", ms->is_dynamic() ? "dynamic" : "no dynamic"));
        SOLVER_VERBOSE_OUT(("       has %s elements\n", ms->is_timestep() ? "timestep" : "no timestep"));
    }

}