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)
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
21 *------------------------------------------------------------------
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
34 /*--------------------------------------------------------------------------*/
36 /*--------------------------------------------------------------------------*/
37 class DEV_CPOLY_G
: public ELEMENT
{
43 const double** _inputs
;
45 explicit DEV_CPOLY_G(const DEV_CPOLY_G
& p
);
47 explicit 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();}
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();}
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();
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);
82 bool do_tr_con_chk_and_q();
84 /*--------------------------------------------------------------------------*/
86 class DEV_FPOLY_G
: public DEV_CPOLY_G
{
88 explicit DEV_FPOLY_G(const DEV_FPOLY_G
& p
)
89 :DEV_CPOLY_G(p
) {incomplete(); unreachable();}
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);}
99 /*--------------------------------------------------------------------------*/
100 /*--------------------------------------------------------------------------*/
101 DEV_CPOLY_G::DEV_CPOLY_G(const DEV_CPOLY_G
& p
)
105 _n_ports(p
._n_ports
),
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:
114 assert(!p
._old_values
);
115 assert(p
._n_ports
== 0);
118 /*--------------------------------------------------------------------------*/
119 DEV_CPOLY_G::DEV_CPOLY_G()
128 /*--------------------------------------------------------------------------*/
129 DEV_CPOLY_G::~DEV_CPOLY_G()
131 delete [] _old_values
;
132 if (net_nodes() > NODES_PER_BRANCH
) {
135 // it is part of a base class
138 /*--------------------------------------------------------------------------*/
139 bool DEV_CPOLY_G::do_tr_con_chk_and_q()
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
]));
151 /*--------------------------------------------------------------------------*/
152 bool DEV_CPOLY_G::do_tr()
155 _m0
= CPOLY1(0., _values
[0], _values
[1]);
156 return do_tr_con_chk_and_q();
158 /*--------------------------------------------------------------------------*/
160 bool DEV_FPOLY_G::do_tr()
163 double c0
= _values
[0];
164 if (_inputs
) {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
]);
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();
183 /*--------------------------------------------------------------------------*/
184 void DEV_CPOLY_G::tr_load()
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();
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
];
210 /*--------------------------------------------------------------------------*/
211 void DEV_CPOLY_G::ac_load()
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);
233 attach_common(Common
);
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()];
246 // use the default node list, already set
249 assert(_n_ports
== n_states
-1);
251 assert(net_nodes() == n_nodes
);
252 // assert could fail if changing the number of nodes after a run
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 /*--------------------------------------------------------------------------*/
266 DISPATCHER
<CARD
>::INSTALL
d4(&device_dispatcher
, "cpoly_g", &p4
);
268 /*--------------------------------------------------------------------------*/
269 /*--------------------------------------------------------------------------*/
270 // vim:ts=8:sw=2:noet: