File: bm_pwl.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 (219 lines) | stat: -rw-r--r-- 7,711 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
/*$Id: bm_pwl.cc 2016/03/23 al $ -*- 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.
 *------------------------------------------------------------------
 * HSPICE compatible PWL
 */
//testing=script 2005.10.06
#include "globals.h"
#include "u_lang.h"
#include "e_elemnt.h"
#include "m_interp.h"
#include "bm.h"
/*--------------------------------------------------------------------------*/
namespace {
/*--------------------------------------------------------------------------*/
const double _default_delta (NOT_INPUT);
const int    _default_smooth(0);
/*--------------------------------------------------------------------------*/
class EVAL_BM_PWL : public EVAL_BM_ACTION_BASE {
private:
  PARAMETER<double> _delta;
  PARAMETER<int>    _smooth;
  std::vector<std::pair<PARAMETER<double>,PARAMETER<double> > > _raw_table;
  std::vector<DPAIR> _num_table;
  explicit	EVAL_BM_PWL(const EVAL_BM_PWL& p);
public:
  explicit      EVAL_BM_PWL(int c=0);
		~EVAL_BM_PWL()		{}
private: // override virtual
  bool		operator==(const COMMON_COMPONENT&)const override;
  COMMON_COMPONENT* clone()const override	{return new EVAL_BM_PWL(*this);}
  void		print_common_obsolete_callback(OMSTREAM&, LANGUAGE*)const override;

  void		precalc_last(const CARD_LIST*)override;
  void		tr_eval(ELEMENT*)const override;
  TIME_PAIR	tr_review(COMPONENT*)const override;
  std::string	name()const override		{return "pwl";}
  bool		ac_too()const override		{return false;}
  bool		parse_numlist(CS&)override;
  bool		parse_params_obsolete_callback(CS&)override;
  void		skip_type_tail(CS& cmd)const override {cmd.umatch("(1)");}
};
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
EVAL_BM_PWL::EVAL_BM_PWL(int c)
  :EVAL_BM_ACTION_BASE(c),
   _delta(_default_delta),
   _smooth(_default_smooth),
   _raw_table(),
   _num_table()
{
}
/*--------------------------------------------------------------------------*/
EVAL_BM_PWL::EVAL_BM_PWL(const EVAL_BM_PWL& p)
  :EVAL_BM_ACTION_BASE(p),
   _delta(p._delta),
   _smooth(p._smooth),
   _raw_table(p._raw_table),
   _num_table(p._num_table)
{
}
/*--------------------------------------------------------------------------*/
bool EVAL_BM_PWL::operator==(const COMMON_COMPONENT& x)const
{
  const EVAL_BM_PWL* p = dynamic_cast<const EVAL_BM_PWL*>(&x);
  bool rv = p
    && _delta == p->_delta
    && _smooth == p->_smooth
    && _raw_table == p->_raw_table
    && _num_table == p->_num_table
    && EVAL_BM_ACTION_BASE::operator==(x);
  if (rv) {
    untested();
  }
  return rv;
}
/*--------------------------------------------------------------------------*/
void EVAL_BM_PWL::print_common_obsolete_callback(OMSTREAM& o, LANGUAGE* lang)const
{
  assert(lang);
  o << name() << '(';
  for (std::vector<std::pair<PARAMETER<double>,PARAMETER<double> > >::
	 const_iterator p = _raw_table.begin(); p != _raw_table.end(); ++p) {
    o << p->first << ',' << p->second << ' ';
  }
  o << ')';
  print_pair(o, lang, "delta", _delta, _delta.has_hard_value());
  print_pair(o, lang, "smooth",_smooth,_smooth.has_hard_value());
  EVAL_BM_ACTION_BASE::print_common_obsolete_callback(o, lang);
}
/*--------------------------------------------------------------------------*/
void EVAL_BM_PWL::precalc_last(const CARD_LIST* Scope)
{
  assert(Scope);
  EVAL_BM_ACTION_BASE::precalc_last(Scope);

  _delta.e_val(_default_delta, Scope);
  _smooth.e_val(_default_smooth, Scope);

  for (std::vector<std::pair<PARAMETER<double>,PARAMETER<double> > >::iterator
	 p = _raw_table.begin();  p != _raw_table.end();  ++p) {
    p->first.e_val(0, Scope);
    p->second.e_val(0, Scope);
  }

  double last = -BIGBIG;
  for (std::vector<std::pair<PARAMETER<double>,PARAMETER<double> > >::iterator
	 p = _raw_table.begin();  p != _raw_table.end();  ++p) {
    if (last > p->first) {
      throw Exception_Precalc("PWL is out of order: (" + to_string(last)
			      + ", " + to_string(p->first) + ")\n");
    }else{
      DPAIR x(p->first, p->second);
      _num_table.push_back(x);
    }
    last = p->first;
  }
}
/*--------------------------------------------------------------------------*/
void EVAL_BM_PWL::tr_eval(ELEMENT* d)const
{
  double ext = (d->is_source()) ? 0. : NOT_INPUT;
  d->_y[0] = interpolate(_num_table.begin(), _num_table.end(), 
		       ioffset(d->_y[0].x), ext, ext);
  tr_final_adjust(&(d->_y[0]), d->f_is_value());
}
/*--------------------------------------------------------------------------*/
TIME_PAIR EVAL_BM_PWL::tr_review(COMPONENT* d)const
{
  if (d->is_source()) {
    // index (x) is time
    ELEMENT* dd = prechecked_cast<ELEMENT*>(d);
    assert(dd);
    double x = dd->_y[0].x + d->_sim->_dtmin * .01;
    DPAIR here(x, BIGBIG);
    std::vector<DPAIR>::const_iterator begin = _num_table.begin();
    std::vector<DPAIR>::const_iterator end   = _num_table.end();
    std::vector<DPAIR>::const_iterator upper = upper_bound(begin, end, here);
    std::vector<DPAIR>::const_iterator lower = upper - 1;
    assert(x > lower->first);
    if(upper==end){
    }else if(x < upper->first){
      d->_time_by.min_event(upper->first);
    }else{ untested();
      // this is a no-op.
 //     d->_time_by.min_event(NEVER);
    }
  }else{itested();
    // index (x) is input
    // It's really needed here too, more work needed
  }

  return d->_time_by;
}
/*--------------------------------------------------------------------------*/
bool EVAL_BM_PWL::parse_numlist(CS& cmd)
{
  size_t start = cmd.cursor();
  size_t here = cmd.cursor();
  for (;;) {
    size_t start_of_pair = here;
    std::pair<PARAMETER<double>, PARAMETER<double> > p;
    //cmd >> key >> value;
    cmd >> p.first; // key
    if (cmd.stuck(&here)) {
      // no more, graceful finish
      break;
    }else{
      cmd >> p.second; // value
      if (cmd.stuck(&here)) {
	// ran out, but already have half of the pair
	// back up one, hoping somebody else knows what to do with it
	cmd.reset(start_of_pair);
	break;
      }else{
	_raw_table.push_back(p);
      }
    }
  }
  if (cmd.gotit(start)) {
  }else{
    untested();
  }
  return cmd.gotit(start);
}
/*--------------------------------------------------------------------------*/
bool EVAL_BM_PWL::parse_params_obsolete_callback(CS& cmd)
{
  return ONE_OF
    || Get(cmd, "delta",  &_delta)
    || Get(cmd, "smooth", &_smooth)
    || EVAL_BM_ACTION_BASE::parse_params_obsolete_callback(cmd)
    ;
}
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
EVAL_BM_PWL p1(CC_STATIC);
DISPATCHER<COMMON_COMPONENT>::INSTALL d1(&bm_dispatcher, "pwl", &p1);
}
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
// vim:ts=8:sw=2:noet: