Merge branch 'testing-uf' into master-uf
[gnucap-felix.git] / lib / bm.cc
blob2617e1753022a562c161df8bcdf8bd0d3642d688
1 /*$Id: bm.cc 2016/03/23 al $ -*- 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 * behavioral modeling action base
24 //testing=script 2006.07.13
25 #include "u_lang.h"
26 #include "e_elemnt.h"
27 #include "bm.h"
28 /*--------------------------------------------------------------------------*/
29 using std::map;
30 /*--------------------------------------------------------------------------*/
31 const double _default_bandwidth (NOT_INPUT);
32 const double _default_delay (0.);
33 const double _default_phase (0.);
34 const double _default_ooffset (0.);
35 const double _default_ioffset (0.);
36 const double _default_scale (1.);
37 const double _default_tc1 (0.);
38 const double _default_tc2 (0.);
39 const double _default_ic (NOT_INPUT);
40 /*--------------------------------------------------------------------------*/
41 /*--------------------------------------------------------------------------*/
42 EVAL_BM_ACTION_BASE::EVAL_BM_ACTION_BASE(int c)
43 :EVAL_BM_BASE(c),
44 _bandwidth(_default_bandwidth),
45 _delay(_default_delay),
46 _phase(_default_phase),
47 _ooffset(_default_ooffset),
48 _ioffset(_default_ioffset),
49 _scale(_default_scale),
50 _tc1(_default_tc1),
51 _tc2(_default_tc2),
52 _ic(_default_ic)
55 /*--------------------------------------------------------------------------*/
56 EVAL_BM_ACTION_BASE::EVAL_BM_ACTION_BASE(const EVAL_BM_ACTION_BASE& p)
57 :EVAL_BM_BASE(p),
58 _bandwidth(p._bandwidth),
59 _delay(p._delay),
60 _phase(p._phase),
61 _ooffset(p._ooffset),
62 _ioffset(p._ioffset),
63 _scale(p._scale),
64 _tc1(p._tc1),
65 _tc2(p._tc2),
66 _ic(p._ic)
69 /*--------------------------------------------------------------------------*/
70 double EVAL_BM_ACTION_BASE::temp_adjust()const
72 double tempdiff = _temp_c - _tnom_c;
73 return (_scale * (1 + _tc1*tempdiff + _tc2*tempdiff*tempdiff));
75 /*--------------------------------------------------------------------------*/
76 void EVAL_BM_ACTION_BASE::tr_final_adjust(FPOLY1* y, bool f_is_value)const
78 if (f_is_value) {
79 y->f1 = y->f0; // used by vs as value(now)...
80 y->f0 = 0.;
81 }else{
82 // f0==offset
83 // f1==linear coefficient
84 y->f1 += _ooffset;
86 *y *= temp_adjust();
87 y->f0 += _ooffset;
89 /*--------------------------------------------------------------------------*/
90 void EVAL_BM_ACTION_BASE::tr_finish_tdv(ELEMENT* d, double val)const
92 d->_y[0] = FPOLY1(CPOLY1(ioffset(d->_y[0].x), 0., val));
93 tr_final_adjust(&(d->_y[0]), false);
95 /*--------------------------------------------------------------------------*/
96 template <class T>
97 void EVAL_BM_ACTION_BASE::ac_final_adjust(T* y)const
99 if (_bandwidth != NOT_INPUT && _bandwidth != 0.) {untested();
100 assert(y->imag() == 0);
101 double ratio = _sim->_freq / _bandwidth;
102 double coeff = double( y->real() / (1.+(ratio*ratio)));
103 *y = T(coeff, -coeff * ratio);
104 }else{
107 if (_phase != 0.) {itested();
108 *y *= std::polar(1., _phase*DTOR);
109 }else{
112 if (_delay != 0.) {untested();
113 double ratio = _sim->_freq * _delay;
114 if (ratio > 100000.) {untested();
115 error(bPICKY, "delay too long\n");
116 ratio = 0.;
117 }else{untested();
119 *y *= std::polar(1., -360.0 * DTOR * ratio);
120 }else{
123 /*--------------------------------------------------------------------------*/
124 template <class T>
125 void EVAL_BM_ACTION_BASE::ac_final_adjust_with_temp(T* y)const
127 *y *= temp_adjust();
128 ac_final_adjust(y);
130 /*--------------------------------------------------------------------------*/
131 void EVAL_BM_ACTION_BASE::ac_final_adjust_with_temp(COMPLEX* y)const
133 *y *= temp_adjust();
134 ac_final_adjust(y);
136 void EVAL_BM_ACTION_BASE::ac_final_adjust_with_temp(std::complex<long double>* y)const
138 *y *= temp_adjust();
139 ac_final_adjust(y);
141 /*--------------------------------------------------------------------------*/
142 inline bool EVAL_BM_BASE::operator==(const COMMON_COMPONENT&x)const
144 if (this==&x) { untested();
145 return true;
146 }else{
148 /* incomplete(); */
149 return 0;
150 /* breaks param.13.gc */
151 return COMMON_COMPONENT::operator==(x);
153 /*--------------------------------------------------------------------------*/
154 bool EVAL_BM_ACTION_BASE::operator==(const COMMON_COMPONENT& x)const
156 const EVAL_BM_ACTION_BASE* p = dynamic_cast<const EVAL_BM_ACTION_BASE*>(&x);
157 bool rv = p
158 && _bandwidth == p->_bandwidth
159 && _delay == p->_delay
160 && _phase == p->_phase
161 && _ooffset == p->_ooffset
162 && _ioffset == p->_ioffset
163 && _scale == p->_scale
164 && _tc1 == p->_tc1
165 && _tc2 == p->_tc2
166 && _ic == p->_ic
167 && EVAL_BM_BASE::operator==(x);
168 return rv;
170 /*--------------------------------------------------------------------------*/
171 void EVAL_BM_ACTION_BASE::print_common_obsolete_callback(OMSTREAM& o, LANGUAGE* lang)const
173 print_pair(o, lang, "bandwidth",_bandwidth,_bandwidth.has_hard_value());
174 print_pair(o, lang, "delay", _delay, _delay.has_hard_value());
175 print_pair(o, lang, "phase", _phase, _phase.has_hard_value());
176 print_pair(o, lang, "ioffset", _ioffset, _ioffset.has_hard_value());
177 print_pair(o, lang, "ooffset", _ooffset, _ooffset.has_hard_value());
178 print_pair(o, lang, "scale", _scale, _scale.has_hard_value());
179 print_pair(o, lang, "tc1", _tc1, _tc1.has_hard_value());
180 print_pair(o, lang, "tc2", _tc2, _tc2.has_hard_value());
181 print_pair(o, lang, "ic", _ic, _ic.has_hard_value());
182 COMMON_COMPONENT::print_common_obsolete_callback(o, lang);
184 /*--------------------------------------------------------------------------*/
185 void EVAL_BM_ACTION_BASE::precalc_last(const CARD_LIST* Scope)
187 assert(Scope);
188 COMMON_COMPONENT::precalc_last(Scope);
189 _bandwidth.e_val(_default_bandwidth, Scope);
190 _delay.e_val(_default_delay, Scope);
191 _phase.e_val(_default_phase, Scope);
192 _ooffset.e_val(_default_ooffset, Scope);
193 _ioffset.e_val(_default_ioffset, Scope);
194 _scale.e_val(_default_scale, Scope);
195 _tc1.e_val(_default_tc1, Scope);
196 _tc2.e_val(_default_tc2, Scope);
197 _ic.e_val(_default_ic, Scope);
199 trace1("EVAL_BM_ACTION_BASE::precalc_last ", _ic);
202 /*--------------------------------------------------------------------------*/
203 // void EVAL_BM_ACTION_BASE::precalc_last(const CARD_LIST* Scope)
204 // {
205 // assert(Scope);
206 // COMMON_COMPONENT::precalc_first(Scope);
207 // _ic.e_val(_default_ic, Scope);
208 // }
209 /*--------------------------------------------------------------------------*/
210 void EVAL_BM_ACTION_BASE::ac_eval(ELEMENT* d)const
212 tr_eval(d);
213 d->_ev = d->_y[0].f1;
214 ac_final_adjust(&(d->_ev));
216 /*--------------------------------------------------------------------------*/
217 map<string, PARA_BASE EVAL_BM_ACTION_BASE::*> EVAL_BM_ACTION_BASE::_param_dict =
218 boost::assign::map_list_of
219 ("bandwidth",(PARA_BASE EVAL_BM_ACTION_BASE::*) &EVAL_BM_ACTION_BASE::_bandwidth)
220 ("delay", (PARA_BASE EVAL_BM_ACTION_BASE::*) &EVAL_BM_ACTION_BASE::_delay)
221 ("phase", (PARA_BASE EVAL_BM_ACTION_BASE::*) &EVAL_BM_ACTION_BASE::_phase)
222 ("ioffset", (PARA_BASE EVAL_BM_ACTION_BASE::*) &EVAL_BM_ACTION_BASE::_ioffset)
223 ("ooffset", (PARA_BASE EVAL_BM_ACTION_BASE::*) &EVAL_BM_ACTION_BASE::_ooffset)
224 ("scale", (PARA_BASE EVAL_BM_ACTION_BASE::*) &EVAL_BM_ACTION_BASE::_scale)
225 ("tc1", (PARA_BASE EVAL_BM_ACTION_BASE::*) &EVAL_BM_ACTION_BASE::_tc1)
226 ("tc2", (PARA_BASE EVAL_BM_ACTION_BASE::*) &EVAL_BM_ACTION_BASE::_tc2)
227 ("ic", (PARA_BASE EVAL_BM_ACTION_BASE::*) &EVAL_BM_ACTION_BASE::_ic);
228 /*--------------------------------------------------------------------------*/
229 void EVAL_BM_ACTION_BASE::set_param_by_name(string Name, string Value)
231 PARA_BASE EVAL_BM_ACTION_BASE::* x = _param_dict[Name];
232 if(x) {
233 PARA_BASE* p = &(this->*x);
234 *p = Value;
235 }else{
236 COMMON_COMPONENT::set_param_by_name(Name, Value);
239 /*--------------------------------------------------------------------------*/
240 bool EVAL_BM_ACTION_BASE::parse_params_obsolete_callback(CS& cmd)
242 trace1("EVAL_BM_ACTION_BASE::parse_params_obsolete_callback", cmd.tail());
243 return ONE_OF
244 || Get(cmd, "bandwidth",&_bandwidth)
245 || Get(cmd, "delay", &_delay)
246 || Get(cmd, "phase", &_phase)
247 || Get(cmd, "ioffset", &_ioffset)
248 || Get(cmd, "ooffset", &_ooffset)
249 || Get(cmd, "scale", &_scale)
250 || Get(cmd, "tc1", &_tc1)
251 || Get(cmd, "tc2", &_tc2)
252 || Get(cmd, "ic", &_ic)
253 || COMMON_COMPONENT::parse_params_obsolete_callback(cmd);
256 /*--------------------------------------------------------------------------*/
257 bool EVAL_BM_ACTION_BASE::has_ext_args()const
259 return (_bandwidth.has_hard_value()
260 || _delay.has_hard_value()
261 || _phase.has_hard_value()
262 || _ooffset.has_hard_value()
263 || _ioffset.has_hard_value()
264 || _scale.has_hard_value()
265 || _tc1.has_hard_value()
266 || _tc2.has_hard_value()
267 || _ic.has_hard_value());
269 /*--------------------------------------------------------------------------*/
270 COMMON_COMPONENT* EVAL_BM_ACTION_BASE::parse_func_type(CS& cmd)
272 const COMMON_COMPONENT* p;
273 if (cmd.is_float() || cmd.match1('_') || cmd.skip1b('=')) {
274 p = bm_dispatcher["eval_bm_value"];
275 }else{
276 p = bm_dispatcher[cmd];
279 if (p) {
280 p->skip_type_tail(cmd);
281 COMMON_COMPONENT* q = p->clone();
282 q->parse_type_tail(cmd);
283 return q;
284 }else{
285 return NULL;
288 /*--------------------------------------------------------------------------*/
289 /*--------------------------------------------------------------------------*/
290 // vim:ts=8:sw=2:noet: