Merge branch 'testing-uf' into master-uf
[gnucap-felix.git] / apps / bm_poly.cc
blob643cd43d14cdadf7d6a8562e627e8d1900bdce2b
1 /* -*- 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 * HSPICE compatible POLY
24 //testing=script,complete 2005.10.06
25 #include "u_lang.h"
26 #include "e_elemnt.h"
27 #include "bm.h"
28 /*--------------------------------------------------------------------------*/
29 namespace {
30 /*--------------------------------------------------------------------------*/
31 const double _default_max(BIGBIG);
32 const double _default_min(-BIGBIG);
33 const bool _default_abs(false);
34 const bool _default_degree(0);
35 /*--------------------------------------------------------------------------*/
36 class EVAL_BM_POLY : public EVAL_BM_ACTION_BASE {
37 private:
38 PARAMETER<double> _min;
39 PARAMETER<double> _max;
40 PARAMETER<bool> _abs;
41 static map<string, PARA_BASE EVAL_BM_POLY::*> param_dict;
42 std::vector<PARAMETER<double> > _c;
43 PARAMETER<unsigned> _degree;
44 explicit EVAL_BM_POLY(const EVAL_BM_POLY& p);
45 public:
46 explicit EVAL_BM_POLY(int c=0);
47 ~EVAL_BM_POLY() {}
48 private: // override vitrual
49 bool operator==(const COMMON_COMPONENT&)const;
50 COMMON_COMPONENT* clone()const {return new EVAL_BM_POLY(*this);}
51 void print_common_obsolete_callback(OMSTREAM&, LANGUAGE*)const;
52 // not yet bool use_obsolete_callback_print()const {return false;}
54 void precalc_last(const CARD_LIST*);
55 void tr_eval(ELEMENT*)const;
56 std::string name()const {return "poly";}
57 bool ac_too()const {untested();return false;}
58 bool parse_numlist(CS&);
59 void set_param_by_name(string Name, string Value);
60 bool parse_params_obsolete_callback(CS&);
61 void skip_type_tail(CS& cmd)const {cmd.umatch("(1)");}
63 /*--------------------------------------------------------------------------*/
64 /*--------------------------------------------------------------------------*/
65 EVAL_BM_POLY::EVAL_BM_POLY(int c)
66 :EVAL_BM_ACTION_BASE(c),
67 _min(_default_min),
68 _max(_default_max),
69 _abs(_default_abs),
70 _degree(_default_degree)
73 /*--------------------------------------------------------------------------*/
74 EVAL_BM_POLY::EVAL_BM_POLY(const EVAL_BM_POLY& p)
75 :EVAL_BM_ACTION_BASE(p),
76 _min(p._min),
77 _max(p._max),
78 _abs(p._abs),
79 _c(p._c),
80 _degree(p._degree)
83 /*--------------------------------------------------------------------------*/
84 bool EVAL_BM_POLY::operator==(const COMMON_COMPONENT& x)const
86 const EVAL_BM_POLY* p = dynamic_cast<const EVAL_BM_POLY*>(&x);
87 bool rv = p
88 && _min == p->_min
89 && _max == p->_max
90 && _abs == p->_abs
91 && _c == p->_c
92 && _degree == p->_degree
93 && EVAL_BM_ACTION_BASE::operator==(x);
94 return rv;
96 /*--------------------------------------------------------------------------*/
97 void EVAL_BM_POLY::print_common_obsolete_callback(OMSTREAM& o, LANGUAGE* lang)const
99 assert(lang);
100 o << ' ' << name() << '(';
101 for (std::vector<PARAMETER<double> >::const_iterator
102 p = _c.begin(); p != _c.end(); ++p) {
103 o << *p << ' ';
105 o << ')';
106 print_pair(o, lang, "min", _min, _min.has_hard_value());
107 print_pair(o, lang, "max", _max, _max.has_hard_value());
108 print_pair(o, lang, "abs", _abs, _abs.has_hard_value());
109 EVAL_BM_ACTION_BASE::print_common_obsolete_callback(o, lang);
111 /*--------------------------------------------------------------------------*/
112 void EVAL_BM_POLY::precalc_last(const CARD_LIST* Scope)
114 assert(Scope);
115 EVAL_BM_ACTION_BASE::precalc_last(Scope);
117 for (std::vector<PARAMETER<double> >::const_iterator
118 p = _c.begin(); p != _c.end(); ++p) {
119 (*p).e_val(0, Scope);
121 _min.e_val(_default_min, Scope);
122 _max.e_val(_default_max, Scope);
123 _abs.e_val(_default_abs, Scope);
125 /*--------------------------------------------------------------------------*/
126 void EVAL_BM_POLY::tr_eval(ELEMENT* d)const
128 double x = ioffset(d->_y[0].x);
129 double f0 = 0.;
130 double f1 = 0.;
131 assert(_c.size());
132 for (size_t i=_c.size()-1; i>0; --i) {
133 f0 += _c[i];
134 f0 *= x;
135 f1 *= x;
136 f1 += _c[i]*int(i);
138 f0 += _c[0];
140 if (_abs && f0 < 0) {
141 f0 = -f0;
142 f1 = -f1;
145 if (f0 > _max) {
146 f0 = _max;
147 f1 = 0;
148 }else if (f0 < _min) {
149 f0 = _min;
150 f1 = 0;
153 d->_y[0] = FPOLY1(x, f0, f1);
154 tr_final_adjust(&(d->_y[0]), d->f_is_value());
156 /*--------------------------------------------------------------------------*/
157 bool EVAL_BM_POLY::parse_numlist(CS& cmd)
159 unsigned start = cmd.cursor();
160 unsigned here = cmd.cursor();
161 for (;;) {
162 unsigned old_here = here;
163 PARAMETER<double> val;
164 cmd >> val;
165 if (cmd.stuck(&here)) {
166 // no more, graceful finish
167 break;
168 }else{
169 if (cmd.match1('=')) {
170 // got one that doesn't belong, back up
171 cmd.reset(old_here);
172 break;
173 }else{
174 _c.push_back(val);
178 if (cmd.gotit(start)) {
179 }else{
180 untested();
182 return cmd.gotit(start);
184 /*--------------------------------------------------------------------------*/
185 map<string, PARA_BASE EVAL_BM_POLY::*> EVAL_BM_POLY::param_dict =
186 boost::assign::map_list_of
187 ("min", (PARA_BASE EVAL_BM_POLY::*) &EVAL_BM_POLY::_min)
188 ("max", (PARA_BASE EVAL_BM_POLY::*) &EVAL_BM_POLY::_max)
189 ("abs", (PARA_BASE EVAL_BM_POLY::*) &EVAL_BM_POLY::_abs);
190 /*--------------------------------------------------------------------------*/
191 void EVAL_BM_POLY::set_param_by_name(string Name, string Value)
193 PARA_BASE EVAL_BM_POLY::* x = (param_dict[Name]);
194 if(x) {
195 PARA_BASE* p = &(this->*x);
196 *p = Value;
197 } else {
198 EVAL_BM_ACTION_BASE::set_param_by_name(Name, Value);
201 /*--------------------------------------------------------------------------*/
202 bool EVAL_BM_POLY::parse_params_obsolete_callback(CS& cmd)
204 return ONE_OF
205 || Get(cmd, "min", &_min)
206 || Get(cmd, "max", &_max)
207 || Get(cmd, "abs", &_abs)
208 || EVAL_BM_ACTION_BASE::parse_params_obsolete_callback(cmd)
211 /*--------------------------------------------------------------------------*/
212 #if 0
213 void EVAL_BM_POLY::parse_type_tail(CS& cmd) {
214 untested();
215 if(cmd.umatch("(1)")){
216 _degree = 1;
217 }else{
218 untested();
221 #endif
222 /*--------------------------------------------------------------------------*/
223 /*--------------------------------------------------------------------------*/
224 EVAL_BM_POLY p1(CC_STATIC);
225 DISPATCHER<COMMON_COMPONENT>::INSTALL d1(&bm_dispatcher, "poly", &p1);
227 /*--------------------------------------------------------------------------*/
228 /*--------------------------------------------------------------------------*/
229 // vim:ts=8:sw=2:noet: