Qucs-core  0.0.18
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
tswitch.cpp
Go to the documentation of this file.
1 /*
2  * tswitch.cpp - time controlled switch class implementation
3  *
4  * Copyright (C) 2006, 2008 Stefan Jahn <stefan@lkcc.org>
5  *
6  * This is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2, or (at your option)
9  * any later version.
10  *
11  * This software is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this package; see the file COPYING. If not, write to
18  * the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  *
21  * $Id$
22  *
23  */
24 
25 #if HAVE_CONFIG_H
26 # include <config.h>
27 #endif
28 
29 #include <iostream>
30 #include <cmath>
31 #include "component.h"
32 #include "tswitch.h"
33 
34 using namespace qucs;
35 
36 tswitch::tswitch () : circuit (2) {
37  type = CIR_TSWITCH;
38  setVoltageSources (1);
39 }
40 
41 nr_double_t tswitch::initState (void) {
42  char * init = getPropertyString ("init");
43  bool on = !strcmp (init, "on");
44  return on ? getPropertyDouble ("Ron") : getPropertyDouble ("Roff");
45 }
46 
47 void tswitch::initSP (void) {
48  nr_double_t r = initState ();
49  allocMatrixS ();
50  setS (NODE_1, NODE_1, r / (r + 2));
51  setS (NODE_2, NODE_2, r / (r + 2));
52  setS (NODE_1, NODE_2, 2 / (r + 2));
53  setS (NODE_2, NODE_1, 2 / (r + 2));
54 }
55 
56 void tswitch::calcNoiseSP (nr_double_t) {
57  nr_double_t T = getPropertyDouble ("Temp");
58  nr_double_t r = initState ();
59  nr_double_t f = kelvin (T) * 4.0 * r * z0 / sqr (2.0 * z0 + r) / T0;
60  setN (NODE_1, NODE_1, +f); setN (NODE_2, NODE_2, +f);
61  setN (NODE_1, NODE_2, -f); setN (NODE_2, NODE_1, -f);
62 }
63 
64 void tswitch::calcNoiseAC (nr_double_t) {
65  nr_double_t r = initState ();
66  if (r > 0.0 || r < 0.0) {
67  nr_double_t T = getPropertyDouble ("Temp");
68  nr_double_t f = kelvin (T) / T0 * 4.0 / r;
69  setN (NODE_1, NODE_1, +f); setN (NODE_2, NODE_2, +f);
70  setN (NODE_1, NODE_2, -f); setN (NODE_2, NODE_1, -f);
71  }
72 }
73 
74 void tswitch::initDC (void) {
75  nr_double_t r = initState ();
76  allocMatrixMNA ();
77  voltageSource (VSRC_1, NODE_1, NODE_2);
78  setD (VSRC_1, VSRC_1, -r);
79 }
80 
81 void tswitch::initAC (void) {
82  initDC ();
83 }
84 
85 void tswitch::initTR (void) {
86  qucs::vector * values = getPropertyVector ("time");
87  // Find the total time of the switching periods
88  T = real (sum (*values));
89  // if the user enters an even number of switchng times
90  // the pattern is repeated continuously
91  repeat = (values->getSize () % 2) == 0 ? true : false;
92  // make the time taken to go from fully on to fully off
93  // the smallest switching time / 100, or the smallest possible
94  // number, but no bogger than the max specified duration
95  nr_double_t maxduration = getPropertyDouble("MaxDuration");
96  duration = std::min ( std::max (10*NR_TINY, values->minimum() / 100),
97  maxduration );
98 
99  initDC ();
100 }
101 
102 void tswitch::calcTR (nr_double_t t) {
103  char * init = getPropertyString ("init");
104  nr_double_t ron = getPropertyDouble ("Ron");
105  nr_double_t roff = getPropertyDouble ("Roff");
106  nr_double_t r = 0;
107  nr_double_t rdiff = 0;
108  nr_double_t s_i = 0;
109  nr_double_t r_0 = 0;
110  qucs::vector * values = getPropertyVector ("time");
111  bool on = !strcmp (init, "on");
112  nr_double_t ti = 0;
113 
114  if (repeat) {
115  // if the user enters an even number of switchng times
116  // the pattern is repeated continuously. This is acieved by
117  // subtracting an integer number of total switching periods
118  // from the real time
119  t = t - T * qucs::floor (t / T);
120  }
121 
122  // Initialise the last switching time to be a full
123  // switching duration
124  nr_double_t ts = t - duration;
125 
126  // here we determine whether a switching event should occur
127  // by looping through the list of switching times and comparing
128  // to the current time
129  for (int i = 0; i < values->getSize (); i++) {
130  // add the current value from the list of switching times
131  // to a counter
132  ti += real (values->get (i));
133 
134  if (t >= ti)
135  {
136  // the current time is greater than or equal to the current
137  // sum of switching times so the switch state changes
138  on = !on;
139  // store the current switching time
140  ts = ti;
141  }
142  else {
143  // the current sum of switching times is in the future
144  // so exit the loop
145  break;
146  }
147  }
148  // calculate the time since the last switch occured
149  nr_double_t tdiff = std::max(NR_TINY, t - ts);
150 
151  // set the time difference to be no more than the max switch
152  // duration so when we interpolate below we only get the max
153  // or min function value if we are past a switching time
154  if (tdiff > duration) {
155  tdiff = duration;
156  }
157 
158  // Set the appropriate resistance. The resistance is interpolated
159  // along a cubic spline with zero derivative at the start and end
160  // points to ensure a smooth derivative
161  if (on)
162  {
163  r_0 = roff;
164 
165  rdiff = ron - roff;
166 
167  s_i = (rdiff) / (duration);
168  }
169  else
170  {
171  r_0 = ron;
172 
173  rdiff = roff - ron;
174 
175  s_i = (rdiff) / (duration);
176  }
177 
178  // perform the interpolation of the constrained cubic spline
179  r = r_0 + ((3. * s_i * qucs::pow (tdiff,2.0)) / (duration))
180  + ((-2. * s_i * qucs::pow (tdiff,3.0)) / qucs::pow (duration, 2.0));
181 
182  setD (VSRC_1, VSRC_1, -r);
183 }
184 
185 // properties
186 PROP_REQ [] = {
187  { "init", PROP_STR, { PROP_NO_VAL, "off" }, PROP_RNG_STR2 ("on", "off") },
188  { "time", PROP_LIST, { 1e-9, PROP_NO_STR }, PROP_POS_RANGE },
189  PROP_NO_PROP };
190 PROP_OPT [] = {
191  { "Ron", PROP_REAL, { 0, PROP_NO_STR }, PROP_POS_RANGE },
192  { "Roff", PROP_REAL, { 1e12, PROP_NO_STR }, PROP_POS_RANGE },
193  { "Temp", PROP_REAL, { 26.85, PROP_NO_STR }, PROP_MIN_VAL (K) },
194  { "MaxDuration", PROP_REAL, { 1e-6, PROP_NO_STR }, PROP_MIN_VAL (10*NR_TINY) },
195  PROP_NO_PROP };
196 struct define_t tswitch::cirdef =
#define PROP_POS_RANGE
Definition: netdefs.h:129
#define NODE_2
Definition: circuit.h:35
matrix real(matrix a)
Real part matrix.
Definition: matrix.cpp:568
#define T0
standard temperature
Definition: constants.h:61
#define kelvin(x)
Definition: constants.h:108
int getSize(void) const
Definition: vector.cpp:192
#define PROP_DEF
Definition: netdefs.h:189
#define PROP_RNG_STR2(s1, s2)
Definition: netdefs.h:145
PROP_OPT[]
Definition: acsolver.cpp:232
nr_complex_t pow(const nr_complex_t z, const nr_double_t d)
Compute power function with real exponent.
Definition: complex.cpp:238
#define PROP_REAL
Definition: netdefs.h:174
nr_complex_t sum(vector)
Definition: vector.cpp:251
void initDC(void)
Definition: tswitch.cpp:74
t
Definition: parse_vcd.y:290
#define PROP_NO_PROP
Definition: netdefs.h:122
#define K
Absolute 0 in centigrade.
Definition: constants.h:59
void initSP(void)
placehoder for S-Parameter initialisation function
Definition: tswitch.cpp:47
#define PROP_NO_STR
Definition: netdefs.h:125
#define PROP_LINEAR
Definition: netdefs.h:120
r
Definition: parse_mdl.y:515
nr_double_t initState(void)
Definition: tswitch.cpp:41
#define VSRC_1
Definition: circuit.h:40
i
Definition: parse_mdl.y:516
nr_complex_t sqr(const nr_complex_t z)
Square of complex number.
Definition: complex.cpp:673
#define PROP_COMPONENT
Definition: netdefs.h:116
nr_complex_t floor(const nr_complex_t z)
Complex floor.
Definition: complex.cpp:623
#define PROP_MIN_VAL(k)
Definition: netdefs.h:133
void calcNoiseAC(nr_double_t)
Definition: tswitch.cpp:64
void calcNoiseSP(nr_double_t)
Definition: tswitch.cpp:56
#define PROP_STR
Definition: netdefs.h:175
#define NODE_1
Definition: circuit.h:34
#define PROP_NO_VAL
Definition: netdefs.h:124
#define PROP_LIST
Definition: netdefs.h:176
void initAC(void)
Definition: tswitch.cpp:81
values
Definition: parse_spice.y:223
PROP_REQ[]
Definition: acsolver.cpp:229
nr_double_t minimum(void)
Definition: vector.cpp:222
void calcTR(nr_double_t)
Definition: tswitch.cpp:102
nr_complex_t get(int)
Definition: vector.cpp:179
#define PROP_NO_SUBSTRATE
Definition: netdefs.h:118
void initTR(void)
Definition: tswitch.cpp:85