File: c_modify.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 (170 lines) | stat: -rw-r--r-- 5,573 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
/*$Id: c_modify.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.
 */
//testing=script,sparse 2006.07.17
#include "globals.h"
#include "e_elemnt.h"
#include "u_cardst.h"
#include "c_comand.h"

extern const int swp_type[];
extern const int swp_count[], swp_steps[];
extern const int swp_nest;
/*--------------------------------------------------------------------------*/
namespace {
/*--------------------------------------------------------------------------*/
enum WHATTODO {FAULT, MODIFY};
std::list<CARDSTASH> faultstack;
/*--------------------------------------------------------------------------*/
/* faultbranch: "fault" a single branch. (temporarily change a value)
 * save the existing info in "faultlist", then patch
 */
void faultbranch(CARD* brh, double value)
{
  if (!brh->is_device()) {
    untested();
    error(bWARNING, brh->long_label() + ": not a device, can't fault:\n");
  }else if (brh->subckt()) {
    untested();
    error(bWARNING, brh->long_label() + " has subckt, can't fault:\n");
  }else{
    faultstack.push_back(CARDSTASH(brh));
    ELEMENT* e = prechecked_cast<ELEMENT*>(brh);
    assert(e);
    e->set_value(value, 0);
    //BUG// messy way to do this, loses other attributes
  }
}
/*--------------------------------------------------------------------------*/
/* sweep_fix: fix the value for sweep command.
 * (find value by interpolation)
 * if not sweeping, return "start" (the first arg).
 */
double sweep_fix(CS& cmd, const CARD *brh)
{
  double start = cmd.ctof();
  double value = start;
  if (swp_steps[swp_nest] != 0   &&   cmd.is_float()) {
    untested();
    double last = cmd.ctof();
    double offset = static_cast<double>(swp_count[swp_nest]) 
      / static_cast<double>(swp_steps[swp_nest]);
    if (swp_type[swp_nest]=='L') {
      untested();
      if (start == 0.) {
	untested();
	throw Exception("log sweep can't pass zero");
	value = 0;
      }else{
	untested();
	value = start * pow( (last/start), offset );
      }
    }else{
      untested();
      value = start + (last-start) * offset;
    }
    IO::mstdout.setfloatwidth(7)
      << swp_count[swp_nest]+1 << "> sweep " << brh->long_label()
      << " =" << value << '\n';
  }
  return value;
}
/*--------------------------------------------------------------------------*/
void modify_fault(CS& cmd, WHATTODO command, CARD_LIST* scope)
{
  CKT_BASE::_sim->uninit();
  while (cmd.is_alpha()) {
    size_t mark = cmd.cursor();
    size_t cmax = cmd.cursor();
    CARD_LIST::fat_iterator ci(scope, scope->begin());
    for (;;) {
      cmd.reset(mark);
      ci = findbranch(cmd, ci);
      cmax = std::max(cmax, cmd.cursor());
      if (ci.is_end()) {
	break;
      }
      cmd.skip1b('=');
      CARD* brh = *ci;
      switch (command) {
      case MODIFY:{
	ELEMENT* e = prechecked_cast<ELEMENT*>(brh);
	assert(e);
	e->set_value(cmd.ctof(), 0);
	//BUG// messy way to do this, loses other attributes
      } break;
      case FAULT:{
	faultbranch(brh, sweep_fix(cmd,brh));
      }	break;
      }
      cmax = std::max(cmax, cmd.cursor());
      ++ci;
    }
    cmd.reset(cmax);
    if (mark == cmax) {
      untested();
      cmd.check(bWARNING, "what's this?");
      cmd.skiparg();
    }
  }
}   
/*--------------------------------------------------------------------------*/
class CMD_MODIFY : public CMD {
public:
  void do_it(CS& cmd, CARD_LIST* Scope)override {
    modify_fault(cmd, MODIFY, Scope);
  }
} p1;
DISPATCHER<CMD>::INSTALL d1(&command_dispatcher, "modify|alter", &p1);
/*--------------------------------------------------------------------------*/
class CMD_FAULT : public CMD {
public:
  void do_it(CS& cmd, CARD_LIST* Scope)override {
    modify_fault(cmd, FAULT, Scope);
  }
} p2;
DISPATCHER<CMD>::INSTALL d2(&command_dispatcher, "fault", &p2);
/*--------------------------------------------------------------------------*/
class CMD_RESTORE : public CMD {
public:
  void do_it(CS&, CARD_LIST* Scope)override {untested();
    command("unfault", Scope);
    command("unmark", Scope);
  }
} p3;
DISPATCHER<CMD>::INSTALL d3(&command_dispatcher, "restore", &p3);
/*--------------------------------------------------------------------------*/
class CMD_UNFAULT : public CMD {
public:
  void do_it(CS&, CARD_LIST*)override {
    while (!faultstack.empty()) {
      faultstack.back().restore();
      faultstack.pop_back();
    }
    _sim->uninit();
  }
} p4;
DISPATCHER<CMD>::INSTALL d4(&command_dispatcher, "unfault", &p4);
/*--------------------------------------------------------------------------*/
}
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
// vim:ts=8:sw=2:noet: