File: bmm_table.cc

package info (click to toggle)
gnucap 1%3A20230520-dev-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 9,836 kB
  • sloc: cpp: 29,956; sh: 352; makefile: 139
file content (254 lines) | stat: -rw-r--r-- 8,651 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/*$Id: bmm_table.cc,v 26.137 2010/04/10 02:37:05 al Exp $ -*- C++ -*-
 * Copyright (C) 2001 Albert Davis
 * Author: Albert Davis <aldavis@gnu.org>
 *
 * This file is part of "Gnucap", the Gnu Circuit Analysis Package
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 *------------------------------------------------------------------
 * behavioral modeling
 * table as a .model card
 */
//testing=script 2006.04.18
#include "globals.h"
#include "u_lang.h"
#include "e_elemnt.h"
#include "m_spline.h"
#include "e_model.h" 
#include "bm.h"
/*--------------------------------------------------------------------------*/
class SPLINE;
/*--------------------------------------------------------------------------*/
class EVAL_BM_TABLE : public EVAL_BM_ACTION_BASE {
protected:
  explicit EVAL_BM_TABLE(const EVAL_BM_TABLE& p);
public:
  explicit EVAL_BM_TABLE(int c=0);
  ~EVAL_BM_TABLE() {}
private: // override virtual
  bool		operator==(const COMMON_COMPONENT&)const override;
  COMMON_COMPONENT* clone()const override {return new EVAL_BM_TABLE(*this);}
  void		print_common_obsolete_callback(OMSTREAM&, LANGUAGE*)const override;
  void  	expand(const COMPONENT*) override;
  void		tr_eval(ELEMENT*)const override;
  std::string	name()const override	{untested();return modelname().c_str();}
  bool		ac_too()const override	{untested();return false;}
};
/*--------------------------------------------------------------------------*/
class MODEL_TABLE : public MODEL_CARD {
public:
  PARAMETER<int>    _order;
  PARAMETER<double> _below;
  PARAMETER<double> _above;
  std::vector<std::pair<PARAMETER<double>,PARAMETER<double> > > _table;
  SPLINE* _spline;
private:
  static int    const _default_order;
  static double const _default_below;
  static double const _default_above;
private:
  explicit MODEL_TABLE(const MODEL_TABLE& p);
public:
  explicit MODEL_TABLE();
  ~MODEL_TABLE();
private: // override virtual
  std::string dev_type()const override		{return "table";}
  void  precalc_first() override;
  COMMON_COMPONENT* new_common()const override	{return new EVAL_BM_TABLE;}
  CARD* clone()const override			{return new MODEL_TABLE(*this);}

  bool use_obsolete_callback_print()const override {return true;}
  bool use_obsolete_callback_parse()const override {return true;}
  void print_args_obsolete_callback(OMSTREAM&,LANGUAGE*)const override;
  bool parse_params_obsolete_callback(CS&) override;

  void tr_eval(COMPONENT*)const override;
};
/*--------------------------------------------------------------------------*/
int    const MODEL_TABLE::_default_order = 3;
double const MODEL_TABLE::_default_below = NOT_INPUT;
double const MODEL_TABLE::_default_above = NOT_INPUT;
/*--------------------------------------------------------------------------*/
static MODEL_TABLE p1;
static DISPATCHER<MODEL_CARD>::INSTALL
d1(&model_dispatcher, "table", &p1);
/*--------------------------------------------------------------------------*/
EVAL_BM_TABLE::EVAL_BM_TABLE(int c)
  :EVAL_BM_ACTION_BASE(c)
{
}
/*--------------------------------------------------------------------------*/
EVAL_BM_TABLE::EVAL_BM_TABLE(const EVAL_BM_TABLE& p)
  :EVAL_BM_ACTION_BASE(p)
{
}
/*--------------------------------------------------------------------------*/
bool EVAL_BM_TABLE::operator==(const COMMON_COMPONENT& x)const
{
  const EVAL_BM_TABLE* p = dynamic_cast<const EVAL_BM_TABLE*>(&x);
  bool rv = p && EVAL_BM_ACTION_BASE::operator==(x);
  if (rv) {
    untested();
  }else{
  }
  return rv;
}
/*--------------------------------------------------------------------------*/
void EVAL_BM_TABLE::print_common_obsolete_callback(OMSTREAM& o, LANGUAGE* lang)const
{
  assert(lang);
  o << modelname();
  EVAL_BM_ACTION_BASE::print_common_obsolete_callback(o, lang);
}
/*--------------------------------------------------------------------------*/
void EVAL_BM_TABLE::expand(const COMPONENT* d)
{
  EVAL_BM_ACTION_BASE::expand(d);
  attach_model(d);

  const MODEL_TABLE* m = dynamic_cast<const MODEL_TABLE*>(model());
  if (!m) {untested();
    throw Exception_Model_Type_Mismatch(d->long_label(), modelname(), "table");
  }else{
  }
}
/*--------------------------------------------------------------------------*/
void EVAL_BM_TABLE::tr_eval(ELEMENT* d)const
{
  model()->tr_eval(d);
  tr_final_adjust(&(d->_y[0]), d->f_is_value());
}
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
MODEL_TABLE::MODEL_TABLE()
  :MODEL_CARD(NULL),
   _order(_default_order),
   _below(_default_below),
   _above(_default_above),
   _table(),
   _spline(0)
{
}
/*--------------------------------------------------------------------------*/
MODEL_TABLE::MODEL_TABLE(const MODEL_TABLE& p)
  :MODEL_CARD(p),
   _order(p._order),
   _below(p._below),
   _above(p._above),
   _table(p._table),
   _spline(NULL)
{
}
/*--------------------------------------------------------------------------*/
MODEL_TABLE::~MODEL_TABLE()
{
  delete _spline;
}
/*--------------------------------------------------------------------------*/
bool MODEL_TABLE::parse_params_obsolete_callback(CS& cmd)
{
  size_t here1 = cmd.cursor();
  {
    Get(cmd, "order", &_order);
    Get(cmd, "below", &_below);
    Get(cmd, "above", &_above);
    bool got_opening_paren = cmd.skip1b('(');
    size_t here = cmd.cursor();
    for (;;) {
      size_t start_of_pair = here;
      std::pair<PARAMETER<double>, PARAMETER<double> > p;
      cmd >> p.first; // key
      if (cmd.stuck(&here)) {
	break;
      }else{
	cmd >> p.second; // value
	if (cmd.stuck(&here)) {
	  cmd.reset(start_of_pair);
	  break;
	}else{
	  _table.push_back(p);
	}
      }
    }
    if (got_opening_paren && !cmd.skip1b(')')) {
      untested();
      cmd.warn(bWARNING, "need )");
    }else if (!got_opening_paren && cmd.skip1b(')')) {
      untested();
      cmd.warn(bWARNING, here, "need (");
    }
  }
  return !(cmd.stuck(&here1));
}
/*--------------------------------------------------------------------------*/
void MODEL_TABLE::print_args_obsolete_callback(OMSTREAM& o, LANGUAGE* lang)const
{
  assert(lang);
  print_pair(o, lang, "order", _order);
  print_pair(o, lang, "below", _below, _below.has_hard_value());
  print_pair(o, lang, "above", _above, _above.has_hard_value());
  o << " (";
  for (std::vector<std::pair<PARAMETER<double>,PARAMETER<double> > >::
	 const_iterator p = _table.begin();  p != _table.end();  ++p) {
    o << p->first << ',' << p->second << ' ';
  }
  o << ')';
}
/*--------------------------------------------------------------------------*/
void MODEL_TABLE::precalc_first()
{
  MODEL_CARD::precalc_first();

  const CARD_LIST* par_scope = scope();
  assert(par_scope);

  _order.e_val(_default_order, par_scope);
  _below.e_val(_default_below, par_scope);
  _above.e_val(_default_above, par_scope);

  {
    double last = -BIGBIG;
    for (std::vector<std::pair<PARAMETER<double>,PARAMETER<double> > >::
	   iterator p = _table.begin();  p != _table.end();  ++p) {
      p->first.e_val(0, par_scope);
      p->second.e_val(0, par_scope);
      if (last > p->first) {
	untested();
	error(bWARNING, "%s: table is out of order: (%g, %g)\n",
	      long_label().c_str(), last, double(p->first));
      }else{
	//std::pair<double,double> x(p->first, p->second);
	//_num_table.push_back(x);
      }
      last = p->first;
    }
  }

  delete _spline;
  double below = _below.has_hard_value() ? _below : NOT_INPUT;
  double above = _above.has_hard_value() ? _above : NOT_INPUT;
  _spline = new SPLINE(_table, below, above, _order);
}
/*--------------------------------------------------------------------------*/
void MODEL_TABLE::tr_eval(COMPONENT* brh)const
{
  ELEMENT* d = prechecked_cast<ELEMENT*>(brh);
  assert(d);
  d->_y[0] = _spline->at(d->_y[0].x);
}
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
// vim:ts=8:sw=2:noet: