trunk/src/emu/netlist/analog/nld_solver.c
| r30699 | r30700 | |
| 34 | 34 | delete m_inps[i]; |
| 35 | 35 | } |
| 36 | 36 | |
| 37 | | ATTR_COLD void netlist_matrix_solver_t::setup(netlist_analog_net_t::list_t &nets, NETLIB_NAME(solver) &aowner, net_entry *list) |
| 37 | ATTR_COLD void netlist_matrix_solver_t::setup(netlist_analog_net_t::list_t &nets, NETLIB_NAME(solver) &aowner) |
| 38 | 38 | { |
| 39 | 39 | m_owner = &aowner; |
| 40 | 40 | |
| 41 | 41 | NL_VERBOSE_OUT(("New solver setup\n")); |
| 42 | 42 | |
| 43 | | for (int k = 0; k < nets.count(); k++) |
| 44 | | list[k].m_net = nets[k]; |
| 43 | m_nets.resize(nets.count()); |
| 45 | 44 | |
| 46 | 45 | for (int k = 0; k < nets.count(); k++) |
| 47 | 46 | { |
| 48 | 47 | NL_VERBOSE_OUT(("setting up net\n")); |
| 49 | 48 | |
| 50 | | netlist_analog_net_t *net = list[k].m_net; |
| 49 | m_nets[k].m_net = nets[k]; |
| 50 | netlist_analog_net_t *net = nets[k]; |
| 51 | 51 | |
| 52 | 52 | net->m_solver = this; |
| 53 | 53 | |
| r30699 | r30700 | |
| 81 | 81 | pterm->m_new_analog_ptr = &pterm->m_otherterm->net().as_analog().m_new_Analog; |
| 82 | 82 | |
| 83 | 83 | if (pterm->m_otherterm->net().isRailNet()) |
| 84 | | list[k].m_rails.add(pterm); |
| 84 | m_nets[k].m_rails.add(pterm); |
| 85 | 85 | else |
| 86 | | list[k].m_terms.add(pterm); |
| 86 | m_nets[k].m_terms.add(pterm); |
| 87 | 87 | } |
| 88 | 88 | NL_VERBOSE_OUT(("Added terminal\n")); |
| 89 | 89 | break; |
| r30699 | r30700 | |
| 119 | 119 | } |
| 120 | 120 | } |
| 121 | 121 | |
| 122 | | ATTR_HOT double netlist_matrix_solver_t::compute_next_timestep(const double hn) |
| 122 | template <int m_N, int _storage_N> |
| 123 | ATTR_HOT double netlist_matrix_solver_direct_t<m_N, _storage_N>::compute_next_timestep(const double hn) |
| 123 | 124 | { |
| 124 | 125 | double new_solver_timestep = m_params.m_max_timestep; |
| 125 | 126 | |
| 126 | 127 | if (m_params.m_dynamic) |
| 127 | 128 | { |
| 129 | /* |
| 130 | * FIXME: this is a reduced LTE focusing on the nets which drive other nets |
| 131 | * The academically correct version using all nets is the one commented out |
| 132 | * This causes really bad performance due to rounding errors. |
| 133 | */ |
| 134 | #if 0 |
| 128 | 135 | for (netlist_analog_output_t * const *p = m_inps.first(); p != NULL; p = m_inps.next(p)) |
| 129 | 136 | { |
| 130 | 137 | netlist_analog_net_t *n = (*p)->m_proxied_net; |
| 131 | | double DD_n = (n->m_cur_Analog - n->m_last_Analog) / hn; |
| 138 | #else |
| 139 | for (int k = 0; k < N(); k++) |
| 140 | { |
| 141 | netlist_analog_net_t *n = m_nets[k].m_net; |
| 142 | #endif |
| 143 | double DD_n = (n->m_cur_Analog - n->m_last_Analog); |
| 132 | 144 | |
| 145 | if (fabs(DD_n) < 2.0 * m_params.m_accuracy) |
| 146 | DD_n = 0.0; |
| 147 | else |
| 148 | DD_n = DD_n / hn; |
| 149 | |
| 133 | 150 | double h_n_m_1 = n->m_h_n_m_1; |
| 134 | 151 | // limit last timestep in equation. |
| 135 | 152 | //if (h_n_m_1 > 3 * hn) |
| r30699 | r30700 | |
| 140 | 157 | |
| 141 | 158 | n->m_DD_n_m_1 = DD_n; |
| 142 | 159 | n->m_h_n_m_1 = hn; |
| 143 | | if (fabs(DD2) > 1e-200) // avoid div-by-zero |
| 160 | if (fabs(DD2) > 1e-50) // avoid div-by-zero |
| 144 | 161 | new_net_timestep = sqrt(m_params.m_lte / fabs(0.5*DD2)); |
| 145 | 162 | else |
| 163 | { |
| 146 | 164 | new_net_timestep = m_params.m_max_timestep; |
| 147 | 165 | |
| 166 | //if (hn > 0.0 && new_net_timestep > 100.0 * hn) |
| 167 | // new_net_timestep = 100.0 * hn; |
| 168 | } |
| 169 | //if (N()==2) |
| 170 | // printf("%s: k %d ts %e DD2 %e\n", name().cstr(), k, new_net_timestep, DD2); |
| 171 | |
| 148 | 172 | if (new_net_timestep < new_solver_timestep) |
| 149 | 173 | new_solver_timestep = new_net_timestep; |
| 150 | 174 | } |
| r30699 | r30700 | |
| 162 | 186 | for (netlist_analog_output_t * const *p = m_inps.first(); p != NULL; p = m_inps.next(p)) |
| 163 | 187 | if ((*p)->m_proxied_net->m_last_Analog != (*p)->m_proxied_net->m_cur_Analog) |
| 164 | 188 | (*p)->set_Q((*p)->m_proxied_net->m_cur_Analog); |
| 165 | | for (netlist_analog_output_t * const *p = m_inps.first(); p != NULL; p = m_inps.next(p)) |
| 189 | #if 1 |
| 190 | for (int k = 0; k < m_nets.count(); k++) |
| 166 | 191 | { |
| 167 | | if ((*p)->m_proxied_net->m_last_Analog != (*p)->m_proxied_net->m_cur_Analog) |
| 168 | | (*p)->m_proxied_net->m_last_Analog = (*p)->m_proxied_net->m_cur_Analog; |
| 192 | netlist_analog_net_t *p= m_nets[k].m_net; |
| 193 | p->m_last_Analog = p->m_cur_Analog; |
| 169 | 194 | } |
| 195 | #else |
| 196 | for (netlist_analog_output_t * const *p = m_inps.first(); p != NULL; p = m_inps.next(p)) |
| 197 | { |
| 198 | if ((*p)->m_proxied_net->m_last_Analog != (*p)->m_proxied_net->m_cur_Analog) |
| 199 | (*p)->m_proxied_net->m_last_Analog = (*p)->m_proxied_net->m_cur_Analog; |
| 200 | } |
| 201 | #endif |
| 170 | 202 | } |
| 171 | 203 | |
| 172 | 204 | |
| r30699 | r30700 | |
| 201 | 233 | { |
| 202 | 234 | const double new_timestep = solve(); |
| 203 | 235 | |
| 204 | | if (m_params.m_dynamic && new_timestep > 0) |
| 236 | if (m_params.m_dynamic && is_timestep() && new_timestep > 0) |
| 205 | 237 | m_Q_sync.net().reschedule_in_queue(netlist_time::from_double(new_timestep)); |
| 206 | 238 | } |
| 207 | 239 | |
| 208 | 240 | ATTR_COLD void netlist_matrix_solver_t::update_forced() |
| 209 | 241 | { |
| 210 | | const double new_timestep = solve(); |
| 242 | ATTR_UNUSED const double new_timestep = solve(); |
| 211 | 243 | |
| 212 | | if (!m_params.m_dynamic) |
| 213 | | return; |
| 214 | | |
| 215 | | if (new_timestep > 0) |
| 244 | if (m_params.m_dynamic && is_timestep()) |
| 216 | 245 | m_Q_sync.net().reschedule_in_queue(netlist_time::from_double(m_params.m_min_timestep)); |
| 217 | 246 | } |
| 218 | 247 | |
| r30699 | r30700 | |
| 305 | 334 | ATTR_COLD void netlist_matrix_solver_direct_t<m_N, _storage_N>::vsetup(netlist_analog_net_t::list_t &nets, NETLIB_NAME(solver) &owner) |
| 306 | 335 | { |
| 307 | 336 | m_dim = nets.count(); |
| 308 | | netlist_matrix_solver_t::setup(nets, owner, m_nets); |
| 337 | netlist_matrix_solver_t::setup(nets, owner); |
| 309 | 338 | |
| 310 | 339 | m_terms.clear(); |
| 311 | 340 | m_rail_start = 0; |
| r30699 | r30700 | |
| 477 | 506 | double cerr2 = 0; |
| 478 | 507 | for (int i = 0; i < this->N(); i++) |
| 479 | 508 | { |
| 480 | | double e = (V[i] - this->m_nets[i].m_net->m_cur_Analog); |
| 481 | | double e2 = (m_RHS[i] - this->m_last_RHS[i]); |
| 482 | | cerr += e * e; |
| 483 | | cerr2 += e2 * e2; |
| 509 | const double e = (V[i] - this->m_nets[i].m_net->m_cur_Analog); |
| 510 | const double e2 = (m_RHS[i] - this->m_last_RHS[i]); |
| 511 | cerr = (fabs(e) > cerr ? fabs(e) : cerr); |
| 512 | cerr2 = (fabs(e2) > cerr2 ? fabs(e2) : cerr2); |
| 484 | 513 | } |
| 485 | | return (cerr + cerr2*(100000.0 * 100000.0)) / this->N(); |
| 514 | // FIXME: Review |
| 515 | return cerr + cerr2*100000.0; |
| 486 | 516 | } |
| 487 | 517 | |
| 488 | 518 | template <int m_N, int _storage_N> |
| 489 | 519 | ATTR_HOT void netlist_matrix_solver_direct_t<m_N, _storage_N>::store( |
| 490 | | const double (* RESTRICT V), bool store_RHS) |
| 520 | const double (* RESTRICT V), const bool store_RHS) |
| 491 | 521 | { |
| 492 | 522 | for (int i = 0; i < this->N(); i++) |
| 493 | 523 | { |
| r30699 | r30700 | |
| 515 | 545 | |
| 516 | 546 | store(new_v, true); |
| 517 | 547 | |
| 518 | | if (err > this->m_params.m_accuracy * this->m_params.m_accuracy) |
| 548 | if (err > this->m_params.m_accuracy) |
| 519 | 549 | { |
| 520 | 550 | return 2; |
| 521 | 551 | } |
| r30699 | r30700 | |
| 548 | 578 | double new_val = m_RHS[0] / m_A[0][0]; |
| 549 | 579 | |
| 550 | 580 | double e = (new_val - net->m_cur_Analog); |
| 551 | | double cerr = e * e; |
| 581 | double cerr = fabs(e); |
| 552 | 582 | |
| 553 | 583 | net->m_cur_Analog = net->m_new_Analog = new_val; |
| 554 | 584 | |
| 555 | | if (is_dynamic() && (cerr > m_params.m_accuracy * m_params.m_accuracy)) |
| 585 | if (is_dynamic() && (cerr > m_params.m_accuracy)) |
| 556 | 586 | { |
| 557 | 587 | return 2; |
| 558 | 588 | } |
| r30699 | r30700 | |
| 578 | 608 | const double d = m_A[1][1]; |
| 579 | 609 | |
| 580 | 610 | double new_val[2]; |
| 581 | | new_val[1] = a / (a * d - b * c) * (m_RHS[1] - c / a * m_RHS[0]); |
| 611 | new_val[1] = (a * m_RHS[1] - c * m_RHS[0]) / (a * d - b * c); |
| 582 | 612 | new_val[0] = (m_RHS[0] - b * new_val[1]) / a; |
| 583 | 613 | |
| 584 | 614 | if (is_dynamic()) |
| 585 | 615 | { |
| 586 | 616 | double err = delta(new_val); |
| 587 | 617 | store(new_val, true); |
| 588 | | if (err > m_params.m_accuracy * m_params.m_accuracy) |
| 618 | if (err > m_params.m_accuracy ) |
| 589 | 619 | return 2; |
| 590 | 620 | else |
| 591 | 621 | return 1; |
| r30699 | r30700 | |
| 626 | 656 | |
| 627 | 657 | for (int k = 0; k < iN; k++) |
| 628 | 658 | { |
| 629 | | const int pk = k; |
| 630 | 659 | double Idrive = 0; |
| 631 | 660 | |
| 632 | 661 | // loop auto-vectorized |
| 633 | 662 | for (int i = 0; i < iN; i++) |
| 634 | | Idrive -= this->m_A[pk][i] * new_v[i]; |
| 663 | Idrive -= this->m_A[k][i] * new_v[i]; |
| 635 | 664 | |
| 636 | | const double new_val = (this->m_RHS[pk] + Idrive + this->m_A[pk][pk] * new_v[pk]) / this->m_A[pk][pk]; |
| 665 | const double new_val = (this->m_RHS[k] + Idrive + this->m_A[k][k] * new_v[k]) / this->m_A[k][k]; |
| 637 | 666 | |
| 638 | | const double e = (new_val - new_v[k]); |
| 639 | | cerr += e * e; |
| 640 | | |
| 667 | const double e = fabs(new_val - new_v[k]); |
| 668 | cerr = (e > cerr ? e : cerr); |
| 641 | 669 | new_v[k] = new_val; |
| 642 | 670 | } |
| 643 | | if (cerr > this->m_params.m_accuracy * this->m_params.m_accuracy) |
| 671 | |
| 672 | if (cerr > this->m_params.m_accuracy) |
| 644 | 673 | { |
| 645 | 674 | resched = true; |
| 646 | 675 | } |
| r30699 | r30700 | |
| 745 | 774 | const double new_val = net.m_new_Analog * one_m_w[k] + (Idrive + RHS[k]) * w[k]; |
| 746 | 775 | |
| 747 | 776 | const double e = (new_val - net.m_new_Analog); |
| 748 | | cerr += e * e; |
| 777 | cerr = (fabs(e) > cerr ? fabs(e) : cerr); |
| 749 | 778 | |
| 750 | 779 | net.m_new_Analog = new_val; |
| 751 | 780 | } |
| 752 | | if (cerr > this->m_params.m_accuracy * this->m_params.m_accuracy) |
| 781 | if (cerr > this->m_params.m_accuracy) |
| 753 | 782 | { |
| 754 | 783 | resched = true; |
| 755 | 784 | } |
| r30699 | r30700 | |
| 790 | 819 | |
| 791 | 820 | register_param("FREQ", m_freq, 48000.0); |
| 792 | 821 | |
| 793 | | register_param("ACCURACY", m_accuracy, 1e-7); |
| 822 | register_param("ACCURACY", m_accuracy, 1e-4); |
| 794 | 823 | register_param("GS_LOOPS", m_gs_loops, 5); // Gauss-Seidel loops |
| 795 | 824 | register_param("NR_LOOPS", m_nr_loops, 25); // Newton-Raphson loops |
| 796 | 825 | register_param("PARALLEL", m_parallel, 0); |
| 797 | 826 | register_param("GMIN", m_gmin, NETLIST_GMIN_DEFAULT); |
| 798 | 827 | register_param("DYNAMIC_TS", m_dynamic, 0); |
| 799 | | register_param("LTE", m_lte, 1e-3); // 1mV diff/timestep |
| 828 | register_param("LTE", m_lte, 1e-2); // 100mV diff/timestep |
| 800 | 829 | register_param("MIN_TIMESTEP", m_min_timestep, 2e-9); // double timestep resolution |
| 801 | 830 | |
| 802 | 831 | // internal staff |
| r30699 | r30700 | |
| 896 | 925 | |
| 897 | 926 | if (m_params.m_dynamic) |
| 898 | 927 | { |
| 899 | | m_params.m_max_timestep *= 100.0; |
| 928 | m_params.m_max_timestep *= 1000.0; |
| 900 | 929 | } |
| 901 | 930 | else |
| 902 | 931 | { |
| r30699 | r30700 | |
| 1009 | 1038 | } |
| 1010 | 1039 | } |
| 1011 | 1040 | } |
| 1012 | | |
| 1013 | 1041 | } |
| 1014 | 1042 | |
trunk/src/emu/netlist/analog/nld_solver.h
| r30699 | r30700 | |
| 71 | 71 | |
| 72 | 72 | class net_entry |
| 73 | 73 | { |
| 74 | | NETLIST_PREVENT_COPYING(net_entry) |
| 75 | | |
| 76 | 74 | public: |
| 77 | 75 | net_entry(netlist_analog_net_t *net) : m_net(net) {} |
| 78 | 76 | net_entry() : m_net(NULL) {} |
| 79 | 77 | |
| 78 | net_entry(const net_entry &rhs) |
| 79 | { |
| 80 | m_net = rhs.m_net; |
| 81 | m_terms = rhs.m_terms; |
| 82 | m_rails = rhs.m_rails; |
| 83 | } |
| 84 | |
| 85 | net_entry &operator=(const net_entry &rhs) |
| 86 | { |
| 87 | m_net = rhs.m_net; |
| 88 | m_terms = rhs.m_terms; |
| 89 | m_rails = rhs.m_rails; |
| 90 | return *this; |
| 91 | } |
| 92 | |
| 80 | 93 | netlist_analog_net_t * RESTRICT m_net; |
| 81 | 94 | netlist_terminal_t::list_t m_terms; |
| 82 | 95 | netlist_terminal_t::list_t m_rails; |
| 83 | 96 | }; |
| 84 | 97 | |
| 85 | | ATTR_COLD virtual void setup(netlist_analog_net_t::list_t &nets, |
| 86 | | NETLIB_NAME(solver) &owner, net_entry *list); |
| 98 | ATTR_COLD void setup(netlist_analog_net_t::list_t &nets, |
| 99 | NETLIB_NAME(solver) &owner); |
| 87 | 100 | |
| 88 | 101 | NETLIB_NAME(solver) *m_owner; |
| 89 | 102 | |
| r30699 | r30700 | |
| 92 | 105 | |
| 93 | 106 | int m_calculations; |
| 94 | 107 | |
| 108 | plinearlist_t<net_entry> m_nets; |
| 109 | plinearlist_t<netlist_analog_output_t *> m_inps; |
| 110 | |
| 95 | 111 | private: |
| 96 | 112 | |
| 97 | 113 | netlist_time m_last_step; |
| 98 | 114 | dev_list_t m_steps; |
| 99 | 115 | dev_list_t m_dynamic; |
| 100 | | plinearlist_t<netlist_analog_output_t *> m_inps; |
| 101 | 116 | |
| 102 | 117 | netlist_ttl_input_t m_fb_sync; |
| 103 | 118 | netlist_ttl_output_t m_Q_sync; |
| r30699 | r30700 | |
| 108 | 123 | * Don't schedule a new calculation time. The recalculation has to be |
| 109 | 124 | * triggered by the caller after the netlist element was changed. |
| 110 | 125 | */ |
| 111 | | ATTR_HOT double compute_next_timestep(const double hn); |
| 126 | ATTR_HOT virtual double compute_next_timestep(const double) = 0; |
| 112 | 127 | |
| 113 | 128 | ATTR_HOT void update_inputs(); |
| 114 | 129 | ATTR_HOT void update_dynamic(); |
| r30699 | r30700 | |
| 140 | 155 | ATTR_HOT inline void gauss_LE(double (* RESTRICT x)); |
| 141 | 156 | ATTR_HOT inline double delta( |
| 142 | 157 | const double (* RESTRICT V)); |
| 143 | | ATTR_HOT inline void store(const double (* RESTRICT V), bool store_RHS); |
| 158 | ATTR_HOT inline void store(const double (* RESTRICT V), const bool store_RHS); |
| 144 | 159 | |
| 145 | | net_entry m_nets[_storage_N]; |
| 160 | ATTR_HOT virtual double compute_next_timestep(const double); |
| 146 | 161 | |
| 147 | 162 | double m_A[_storage_N][_storage_N]; |
| 148 | 163 | double m_RHS[_storage_N]; |
| r30699 | r30700 | |
| 160 | 175 | : m_term(NULL), m_net_this(-1), m_net_other(-1) |
| 161 | 176 | {} |
| 162 | 177 | |
| 163 | | netlist_terminal_t *m_term; |
| 178 | netlist_terminal_t * RESTRICT m_term; |
| 164 | 179 | int m_net_this; |
| 165 | 180 | int m_net_other; |
| 166 | 181 | }; |