viminfo
[gnucap-felix.git] / apps / d_poly_g.cc
blobe634937fc9b06e02513a93b0e3bc969c20e76898
1 /*$Id: d_poly_g.cc,v 26.137 2010/04/10 02:37:05 al Exp $ -*- C++ -*-
2 * Copyright (C) 2001 Albert Davis
3 * Author: Albert Davis <aldavis@gnu.org>
5 * This file is part of "Gnucap", the Gnu Circuit Analysis Package
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3, or (at your option)
10 * any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 * 02110-1301, USA.
21 *------------------------------------------------------------------
22 * DEV_CPOLY_G
23 * number of nodes = 2*n_ports
24 * number of val, ov = n_ports+1
25 * val[0] is the constant part, val[1] is self admittance,
26 * val[2+] are transadmittances, up to n_ports
27 * node[0] and node[1] are the output.
28 * node[2] up are inputs.
29 * node[2*i] and node[2*i+1] correspond to val[i+1]
31 //testing=script 2006.07.17
32 #include "globals.h"
33 #include "e_elemnt.h"
34 /*--------------------------------------------------------------------------*/
35 namespace {
36 /*--------------------------------------------------------------------------*/
37 class DEV_CPOLY_G : public ELEMENT {
38 protected:
39 double* _values;
40 double* _old_values;
41 int _n_ports;
42 double _time;
43 const double** _inputs;
44 protected:
45 explicit DEV_CPOLY_G(const DEV_CPOLY_G& p);
46 public:
47 explicit DEV_CPOLY_G();
48 ~DEV_CPOLY_G();
49 protected: // override virtual
50 char id_letter()const {unreachable(); return '\0';}
51 std::string value_name()const {incomplete(); return "";}
52 std::string dev_type()const {unreachable(); return "cpoly_g";}
53 int max_nodes()const {return net_nodes();}
54 int min_nodes()const {return net_nodes();}
55 int matrix_nodes()const {return _n_ports*2;}
56 int net_nodes()const {return _n_ports*2;}
57 CARD* clone()const {return new DEV_CPOLY_G(*this);}
58 void tr_iwant_matrix() {tr_iwant_matrix_extended();}
59 bool do_tr();
60 void tr_load();
61 void tr_unload();
62 double tr_involts()const {unreachable(); return NOT_VALID;}
63 double tr_involts_limited()const {unreachable(); return NOT_VALID;}
64 double tr_amps()const;
65 void ac_iwant_matrix() {ac_iwant_matrix_extended();}
66 void ac_load();
67 COMPLEX ac_involts()const {itested(); return NOT_VALID;}
68 COMPLEX ac_amps()const {itested(); return NOT_VALID;}
70 std::string port_name(int)const {untested();
71 incomplete();
72 unreachable();
73 return "";
75 public:
76 void set_parameters(const std::string& Label, CARD* Parent,
77 COMMON_COMPONENT* Common, double Value,
78 int state_count, double state[],
79 int node_count, const node_t nodes[]);
80 // const double* inputs[]=0);
81 protected:
82 bool do_tr_con_chk_and_q();
84 /*--------------------------------------------------------------------------*/
85 #if 0
86 class DEV_FPOLY_G : public DEV_CPOLY_G {
87 private:
88 explicit DEV_FPOLY_G(const DEV_FPOLY_G& p)
89 :DEV_CPOLY_G(p) {incomplete(); unreachable();}
90 public:
91 explicit DEV_FPOLY_G() :DEV_CPOLY_G() {untested();}
92 private: // override virtual
93 char id_letter()const {unreachable(); return '\0';}
94 std::string dev_type()const {unreachable(); return "fpoly_g";}
95 CARD* clone()const {unreachable(); return new DEV_FPOLY_G(*this);}
96 bool do_tr();
98 #endif
99 /*--------------------------------------------------------------------------*/
100 /*--------------------------------------------------------------------------*/
101 DEV_CPOLY_G::DEV_CPOLY_G(const DEV_CPOLY_G& p)
102 :ELEMENT(p),
103 _values(NULL),
104 _old_values(NULL),
105 _n_ports(p._n_ports),
106 _time(NOT_VALID),
107 _inputs(NULL)
109 // not really a copy .. only valid to copy a default
110 // too lazy to do it right, and that's all that is being used
111 // to do it correctly requires a deep copy
112 // just filling in defaults is better than a shallow copy, hence this:
113 assert(!p._values);
114 assert(!p._old_values);
115 assert(p._n_ports == 0);
116 assert(!p._inputs);
118 /*--------------------------------------------------------------------------*/
119 DEV_CPOLY_G::DEV_CPOLY_G()
120 :ELEMENT(),
121 _values(NULL),
122 _old_values(NULL),
123 _n_ports(0),
124 _time(NOT_VALID),
125 _inputs(NULL)
128 /*--------------------------------------------------------------------------*/
129 DEV_CPOLY_G::~DEV_CPOLY_G()
131 delete [] _old_values;
132 if (net_nodes() > NODES_PER_BRANCH) {
133 delete [] _n;
134 }else{
135 // it is part of a base class
138 /*--------------------------------------------------------------------------*/
139 bool DEV_CPOLY_G::do_tr_con_chk_and_q()
141 q_load();
143 assert(_old_values);
144 set_converged(conchk(_time, _sim->_time0));
145 _time = _sim->_time0;
146 for (int i=0; converged() && i<=_n_ports; ++i) {
147 set_converged(conchk(_old_values[i], _values[i]));
149 return converged();
151 /*--------------------------------------------------------------------------*/
152 bool DEV_CPOLY_G::do_tr()
154 assert(_values);
155 _m0 = CPOLY1(0., _values[0], _values[1]);
156 return do_tr_con_chk_and_q();
158 /*--------------------------------------------------------------------------*/
159 #if 0
160 bool DEV_FPOLY_G::do_tr()
161 {untested();
162 assert(_values);
163 double c0 = _values[0];
164 if (_inputs) {untested();
165 untested();
166 for (int i=1; i<=_n_ports; ++i) {untested();
167 c0 -= *(_inputs[i]) * _values[i];
168 trace4("", i, *(_inputs[i]), _values[i], *(_inputs[i]) * _values[i]);
170 }else{untested();
171 for (int i=1; i<=_n_ports; ++i) {untested();
172 c0 -= volts_limited(_n[2*i-2],_n[2*i-1]) * _values[i];
173 trace4("", i, volts_limited(_n[2*i-2],_n[2*i-1]), _values[i],
174 volts_limited(_n[2*i-2],_n[2*i-1]) * _values[i]);
177 trace2("", _values[0], c0);
178 _m0 = CPOLY1(0., c0, _values[1]);
180 return do_tr_con_chk_and_q();
182 #endif
183 /*--------------------------------------------------------------------------*/
184 void DEV_CPOLY_G::tr_load()
186 tr_load_passive();
187 _old_values[0] = _values[0];
188 _old_values[1] = _values[1];
189 for (int i=2; i<=_n_ports; ++i) {
190 tr_load_extended(_n[OUT1], _n[OUT2], _n[2*i-2], _n[2*i-1], &(_values[i]), &(_old_values[i]));
193 /*--------------------------------------------------------------------------*/
194 void DEV_CPOLY_G::tr_unload()
196 std::fill_n(_values, _n_ports+1, 0.);
197 _m0.c0 = _m0.c1 = 0.;
198 _sim->mark_inc_mode_bad();
199 tr_load();
201 /*--------------------------------------------------------------------------*/
202 double DEV_CPOLY_G::tr_amps()const
204 double amps = _m0.c0;
205 for (int i=1; i<=_n_ports; ++i) {
206 amps += dn_diff(_n[2*i-2].v0(),_n[2*i-1].v0()) * _values[i];
208 return amps;
210 /*--------------------------------------------------------------------------*/
211 void DEV_CPOLY_G::ac_load()
213 _acg = _values[1];
214 ac_load_passive();
215 for (int i=2; i<=_n_ports; ++i) {
216 ac_load_extended(_n[OUT1], _n[OUT2], _n[2*i-2], _n[2*i-1], _values[i]);
219 /*--------------------------------------------------------------------------*/
220 /* set: set parameters, used in model building
222 void DEV_CPOLY_G::set_parameters(const std::string& Label, CARD *Owner,
223 COMMON_COMPONENT *Common, double Value,
224 int n_states, double states[],
225 int n_nodes, const node_t nodes[])
226 // const double* inputs[])
228 bool first_time = (net_nodes() == 0);
230 set_label(Label);
231 set_owner(Owner);
232 set_value(Value);
233 attach_common(Common);
235 if (first_time) {
236 _n_ports = n_nodes/2; // sets num_nodes() = _n_ports*2
237 assert(_n_ports == n_states-1);
239 assert(!_old_values);
240 _old_values = new double[n_states];
242 if (net_nodes() > NODES_PER_BRANCH) {
243 // allocate a bigger node list
244 _n = new node_t[net_nodes()];
245 }else{
246 // use the default node list, already set
248 }else{
249 assert(_n_ports == n_states-1);
250 assert(_old_values);
251 assert(net_nodes() == n_nodes);
252 // assert could fail if changing the number of nodes after a run
255 //_inputs = inputs;
256 _inputs = 0;
257 _values = states;
258 std::fill_n(_values, n_states, 0.);
259 std::fill_n(_old_values, n_states, 0.);
260 notstd::copy_n(nodes, net_nodes(), _n);
261 assert(net_nodes() == _n_ports * 2);
263 /*--------------------------------------------------------------------------*/
264 /*--------------------------------------------------------------------------*/
265 DEV_CPOLY_G p4;
266 DISPATCHER<CARD>::INSTALL d4(&device_dispatcher, "cpoly_g", &p4);
268 /*--------------------------------------------------------------------------*/
269 /*--------------------------------------------------------------------------*/
270 // vim:ts=8:sw=2:noet: