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)
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 * table as a .model card
25 //testing=script 2006.04.18
32 /*--------------------------------------------------------------------------*/
34 /*--------------------------------------------------------------------------*/
35 class EVAL_BM_TABLE
: public EVAL_BM_ACTION_BASE
{
37 explicit EVAL_BM_TABLE(const EVAL_BM_TABLE
& p
);
39 explicit EVAL_BM_TABLE(int c
=0);
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
{
53 PARAMETER
<int> _order
;
54 PARAMETER
<double> _below
;
55 PARAMETER
<double> _above
;
56 std::vector
<std::pair
<PARAMETER
<double>,PARAMETER
<double> > > _table
;
59 static int const _default_order
;
60 static double const _default_below
;
61 static double const _default_above
;
63 explicit MODEL_TABLE(const MODEL_TABLE
& p
);
65 explicit MODEL_TABLE();
67 private: // override virtual
68 std::string
dev_type()const {return "table";}
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
);
109 /*--------------------------------------------------------------------------*/
110 void EVAL_BM_TABLE::print_common_obsolete_callback(OMSTREAM
& o
, LANGUAGE
* lang
)const
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
);
122 const MODEL_TABLE
* m
= dynamic_cast<const MODEL_TABLE
*>(model());
124 throw Exception_Model_Type_Mismatch(d
->long_label(), modelname(), "table");
128 /*--------------------------------------------------------------------------*/
129 void EVAL_BM_TABLE::tr_eval(ELEMENT
* d
)const
132 tr_final_adjust(&(d
->_y
[0]), d
->f_is_value());
134 /*--------------------------------------------------------------------------*/
135 /*--------------------------------------------------------------------------*/
136 MODEL_TABLE::MODEL_TABLE()
138 _order(_default_order
),
139 _below(_default_below
),
140 _above(_default_above
),
145 /*--------------------------------------------------------------------------*/
146 MODEL_TABLE::MODEL_TABLE(const MODEL_TABLE
& p
)
155 /*--------------------------------------------------------------------------*/
156 MODEL_TABLE::~MODEL_TABLE()
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();
171 unsigned start_of_pair
= here
;
172 std::pair
<PARAMETER
<double>, PARAMETER
<double> > p
;
173 cmd
>> p
.first
; // key
174 if (cmd
.stuck(&here
)) {
177 cmd
>> p
.second
; // value
178 if (cmd
.stuck(&here
)) {
179 cmd
.reset(start_of_pair
);
186 if (got_opening_paren
&& !cmd
.skip1b(')')) {
188 cmd
.warn(bWARNING
, "need )");
189 }else if (!got_opening_paren
&& cmd
.skip1b(')')) {
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
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());
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
<< ' ';
210 /*--------------------------------------------------------------------------*/
211 void MODEL_TABLE::precalc_first()
213 MODEL_CARD::precalc_first();
215 const CARD_LIST
* par_scope
= 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
) {
230 error(bWARNING
, "%s: table is out of order: (%g, %g)\n",
231 long_label().c_str(), last
, double(p
->first
));
233 //std::pair<double,double> x(p->first, p->second);
234 //_num_table.push_back(x);
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
);
250 d
->_y
[0] = _spline
->at(d
->_y
[0].x
);
252 /*--------------------------------------------------------------------------*/
253 /*--------------------------------------------------------------------------*/
254 // vim:ts=8:sw=2:noet: