My Project  0.0.16
QUCS Mapping
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros 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: tswitch.cpp 1825 2011-03-11 20:42:14Z ela $
22  *
23  */
24 
25 #if HAVE_CONFIG_H
26 # include <config.h>
27 #endif
28 
29 #include "component.h"
30 #include "tswitch.h"
31 
32 tswitch::tswitch () : circuit (2) {
33  type = CIR_TSWITCH;
35 }
36 
37 nr_double_t tswitch::initState (void) {
38  char * init = getPropertyString ("init");
39  bool on = !strcmp (init, "on");
40  return on ? getPropertyDouble ("Ron") : getPropertyDouble ("Roff");
41 }
42 
43 void tswitch::initSP (void) {
44  nr_double_t r = initState ();
45  allocMatrixS ();
46  setS (NODE_1, NODE_1, r / (r + 2));
47  setS (NODE_2, NODE_2, r / (r + 2));
48  setS (NODE_1, NODE_2, 2 / (r + 2));
49  setS (NODE_2, NODE_1, 2 / (r + 2));
50 }
51 
52 void tswitch::calcNoiseSP (nr_double_t) {
53  nr_double_t T = getPropertyDouble ("Temp");
54  nr_double_t r = initState ();
55  nr_double_t f = kelvin (T) * 4.0 * r * z0 / sqr (2.0 * z0 + r) / T0;
56  setN (NODE_1, NODE_1, +f); setN (NODE_2, NODE_2, +f);
57  setN (NODE_1, NODE_2, -f); setN (NODE_2, NODE_1, -f);
58 }
59 
60 void tswitch::calcNoiseAC (nr_double_t) {
61  nr_double_t r = initState ();
62  if (r > 0.0 || r < 0.0) {
63  nr_double_t T = getPropertyDouble ("Temp");
64  nr_double_t f = kelvin (T) / T0 * 4.0 / r;
65  setN (NODE_1, NODE_1, +f); setN (NODE_2, NODE_2, +f);
66  setN (NODE_1, NODE_2, -f); setN (NODE_2, NODE_1, -f);
67  }
68 }
69 
70 void tswitch::initDC (void) {
71  nr_double_t r = initState ();
72  allocMatrixMNA ();
74  setD (VSRC_1, VSRC_1, -r);
75 }
76 
77 void tswitch::initAC (void) {
78  initDC ();
79 }
80 
81 void tswitch::initTR (void) {
82  vector * values = getPropertyVector ("time");
83  T = real (sum (*values));
84  repeat = (values->getSize () % 2) == 0 ? true : false;
85  initDC ();
86 }
87 
88 void tswitch::calcTR (nr_double_t t) {
89  char * init = getPropertyString ("init");
90  nr_double_t ron = getPropertyDouble ("Ron");
91  nr_double_t roff = getPropertyDouble ("Roff");
92  vector * values = getPropertyVector ("time");
93  bool on = !strcmp (init, "on");
94  nr_double_t ti = 0;
95 
96  if (repeat) t = t - T * floor (t / T);
97  for (int i = 0; i < values->getSize (); i++) {
98  ti += real (values->get (i));
99  if (t >= ti) on = !on; else break;
100  }
101 
102  setD (VSRC_1, VSRC_1, on ? -ron : -roff);
103 }
104 
105 // properties
106 PROP_REQ [] = {
107  { "init", PROP_STR, { PROP_NO_VAL, "off" }, PROP_RNG_STR2 ("on", "off") },
108  { "time", PROP_LIST, { 1e-9, PROP_NO_STR }, PROP_POS_RANGE },
109  PROP_NO_PROP };
110 PROP_OPT [] = {
111  { "Ron", PROP_REAL, { 0, PROP_NO_STR }, PROP_POS_RANGE },
112  { "Roff", PROP_REAL, { 1e12, PROP_NO_STR }, PROP_POS_RANGE },
113  { "Temp", PROP_REAL, { 26.85, PROP_NO_STR }, PROP_MIN_VAL (K) },
114  PROP_NO_PROP };
115 struct define_t tswitch::cirdef =