DESTDIR in uninstall target
[gnucap-felix.git] / apps / bmm_table.cc
blobab51dd9253758db2a76c99abef1471ca775ee453
1 /*$Id: bmm_table.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 * behavioral modeling
23 * table as a .model card
25 //testing=script 2006.04.18
26 #include "globals.h"
27 #include "u_lang.h"
28 #include "e_elemnt.h"
29 #include "m_spline.h"
30 #include "e_model.h"
31 #include "bm.h"
32 /*--------------------------------------------------------------------------*/
33 class SPLINE;
34 /*--------------------------------------------------------------------------*/
35 class EVAL_BM_TABLE : public EVAL_BM_ACTION_BASE {
36 protected:
37 explicit EVAL_BM_TABLE(const EVAL_BM_TABLE& p);
38 public:
39 explicit EVAL_BM_TABLE(int c=0);
40 ~EVAL_BM_TABLE() {}
41 private: // override virtual
42 bool operator==(const COMMON_COMPONENT&)const;
43 COMMON_COMPONENT* clone()const {return new EVAL_BM_TABLE(*this);}
44 void print_common_obsolete_callback(OMSTREAM&, LANGUAGE*)const;
45 void expand(const COMPONENT*);
46 void tr_eval(ELEMENT*)const;
47 std::string name()const {untested();return modelname().c_str();}
48 bool ac_too()const {untested();return false;}
50 /*--------------------------------------------------------------------------*/
51 class MODEL_TABLE : public MODEL_CARD {
52 public:
53 PARAMETER<int> _order;
54 PARAMETER<double> _below;
55 PARAMETER<double> _above;
56 std::vector<std::pair<PARAMETER<double>,PARAMETER<double> > > _table;
57 SPLINE* _spline;
58 private:
59 static int const _default_order;
60 static double const _default_below;
61 static double const _default_above;
62 private:
63 explicit MODEL_TABLE(const MODEL_TABLE& p);
64 public:
65 explicit MODEL_TABLE();
66 ~MODEL_TABLE();
67 private: // override virtual
68 std::string dev_type()const {return "table";}
69 void precalc_first();
70 COMMON_COMPONENT* new_common()const {return new EVAL_BM_TABLE;}
71 CARD* clone()const {return new MODEL_TABLE(*this);}
73 bool use_obsolete_callback_print()const {return true;}
74 bool use_obsolete_callback_parse()const {return true;}
75 void print_args_obsolete_callback(OMSTREAM&,LANGUAGE*)const;
76 bool parse_params_obsolete_callback(CS&);
78 void tr_eval(COMPONENT*)const;
80 /*--------------------------------------------------------------------------*/
81 int const MODEL_TABLE::_default_order = 3;
82 double const MODEL_TABLE::_default_below = NOT_INPUT;
83 double const MODEL_TABLE::_default_above = NOT_INPUT;
84 /*--------------------------------------------------------------------------*/
85 static MODEL_TABLE p1;
86 static DISPATCHER<MODEL_CARD>::INSTALL
87 d1(&model_dispatcher, "table", &p1);
88 /*--------------------------------------------------------------------------*/
89 EVAL_BM_TABLE::EVAL_BM_TABLE(int c)
90 :EVAL_BM_ACTION_BASE(c)
93 /*--------------------------------------------------------------------------*/
94 EVAL_BM_TABLE::EVAL_BM_TABLE(const EVAL_BM_TABLE& p)
95 :EVAL_BM_ACTION_BASE(p)
98 /*--------------------------------------------------------------------------*/
99 bool EVAL_BM_TABLE::operator==(const COMMON_COMPONENT& x)const
101 const EVAL_BM_TABLE* p = dynamic_cast<const EVAL_BM_TABLE*>(&x);
102 bool rv = p && EVAL_BM_ACTION_BASE::operator==(x);
103 if (rv) {
104 untested();
105 }else{
107 return rv;
109 /*--------------------------------------------------------------------------*/
110 void EVAL_BM_TABLE::print_common_obsolete_callback(OMSTREAM& o, LANGUAGE* lang)const
112 assert(lang);
113 o << modelname();
114 EVAL_BM_ACTION_BASE::print_common_obsolete_callback(o, lang);
116 /*--------------------------------------------------------------------------*/
117 void EVAL_BM_TABLE::expand(const COMPONENT* d)
119 EVAL_BM_ACTION_BASE::expand(d);
120 attach_model(d);
122 const MODEL_TABLE* m = dynamic_cast<const MODEL_TABLE*>(model());
123 if (!m) {untested();
124 throw Exception_Model_Type_Mismatch(d->long_label(), modelname(), "table");
125 }else{
128 /*--------------------------------------------------------------------------*/
129 void EVAL_BM_TABLE::tr_eval(ELEMENT* d)const
131 model()->tr_eval(d);
132 tr_final_adjust(&(d->_y[0]), d->f_is_value());
134 /*--------------------------------------------------------------------------*/
135 /*--------------------------------------------------------------------------*/
136 MODEL_TABLE::MODEL_TABLE()
137 :MODEL_CARD(NULL),
138 _order(_default_order),
139 _below(_default_below),
140 _above(_default_above),
141 _table(),
142 _spline(0)
145 /*--------------------------------------------------------------------------*/
146 MODEL_TABLE::MODEL_TABLE(const MODEL_TABLE& p)
147 :MODEL_CARD(p),
148 _order(p._order),
149 _below(p._below),
150 _above(p._above),
151 _table(p._table),
152 _spline(p._spline)
155 /*--------------------------------------------------------------------------*/
156 MODEL_TABLE::~MODEL_TABLE()
158 delete _spline;
160 /*--------------------------------------------------------------------------*/
161 bool MODEL_TABLE::parse_params_obsolete_callback(CS& cmd)
163 unsigned here1 = cmd.cursor();
165 Get(cmd, "order", &_order);
166 Get(cmd, "below", &_below);
167 Get(cmd, "above", &_above);
168 bool got_opening_paren = cmd.skip1b('(');
169 unsigned here = cmd.cursor();
170 for (;;) {
171 unsigned start_of_pair = here;
172 std::pair<PARAMETER<double>, PARAMETER<double> > p;
173 cmd >> p.first; // key
174 if (cmd.stuck(&here)) {
175 break;
176 }else{
177 cmd >> p.second; // value
178 if (cmd.stuck(&here)) {
179 cmd.reset(start_of_pair);
180 break;
181 }else{
182 _table.push_back(p);
186 if (got_opening_paren && !cmd.skip1b(')')) {
187 untested();
188 cmd.warn(bWARNING, "need )");
189 }else if (!got_opening_paren && cmd.skip1b(')')) {
190 untested();
191 cmd.warn(bWARNING, here, "need (");
194 return !(cmd.stuck(&here1));
196 /*--------------------------------------------------------------------------*/
197 void MODEL_TABLE::print_args_obsolete_callback(OMSTREAM& o, LANGUAGE* lang)const
199 assert(lang);
200 print_pair(o, lang, "order", _order);
201 print_pair(o, lang, "below", _below, _below.has_hard_value());
202 print_pair(o, lang, "above", _above, _above.has_hard_value());
203 o << " (";
204 for (std::vector<std::pair<PARAMETER<double>,PARAMETER<double> > >::
205 const_iterator p = _table.begin(); p != _table.end(); ++p) {
206 o << p->first << ',' << p->second << ' ';
208 o << ')';
210 /*--------------------------------------------------------------------------*/
211 void MODEL_TABLE::precalc_first()
213 MODEL_CARD::precalc_first();
215 const CARD_LIST* par_scope = scope();
216 assert(par_scope);
218 _order.e_val(_default_order, par_scope);
219 _below.e_val(_default_below, par_scope);
220 _above.e_val(_default_above, par_scope);
223 double last = -BIGBIG;
224 for (std::vector<std::pair<PARAMETER<double>,PARAMETER<double> > >::
225 iterator p = _table.begin(); p != _table.end(); ++p) {
226 p->first.e_val(0, par_scope);
227 p->second.e_val(0, par_scope);
228 if (last > p->first) {
229 untested();
230 error(bWARNING, "%s: table is out of order: (%g, %g)\n",
231 long_label().c_str(), last, double(p->first));
232 }else{
233 //std::pair<double,double> x(p->first, p->second);
234 //_num_table.push_back(x);
236 last = p->first;
240 delete _spline;
241 double below = _below.has_hard_value() ? _below : NOT_INPUT;
242 double above = _above.has_hard_value() ? _above : NOT_INPUT;
243 _spline = new SPLINE(_table, below, above, _order);
245 /*--------------------------------------------------------------------------*/
246 void MODEL_TABLE::tr_eval(COMPONENT* brh)const
248 ELEMENT* d = prechecked_cast<ELEMENT*>(brh);
249 assert(d);
250 d->_y[0] = _spline->at(d->_y[0].x);
252 /*--------------------------------------------------------------------------*/
253 /*--------------------------------------------------------------------------*/
254 // vim:ts=8:sw=2:noet: