My Project  0.0.16
QUCS Mapping
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
vfile.cpp
Go to the documentation of this file.
1 /*
2  * vfile.cpp - file based voltage source class implementation
3  *
4  * Copyright (C) 2007 Gunther Kraut <gn.kraut@t-online.de>
5  * Copyright (C) 2007, 2008, 2009 Stefan Jahn <stefan@lkcc.org>
6  *
7  * This is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2, or (at your option)
10  * any later version.
11  *
12  * This software is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this package; see the file COPYING. If not, write to
19  * the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
20  * Boston, MA 02110-1301, USA.
21  *
22  * $Id: vfile.cpp 1825 2011-03-11 20:42:14Z ela $
23  *
24  */
25 
26 #if HAVE_CONFIG_H
27 # include <config.h>
28 #endif
29 
30 #include "component.h"
31 #include "dataset.h"
32 #include "poly.h"
33 #include "spline.h"
34 #include "interpolator.h"
35 #include "vfile.h"
36 
37 // Constructor creates vfile object in memory.
38 vfile::vfile () : circuit (2) {
39  type = CIR_VFILE;
40  setVSource (true);
41  setVoltageSources (1);
42  interpolType = dataType = 0;
43  data = NULL;
44  inter = NULL;
45 }
46 
47 // Destructor deletes vfile object from memory.
49  if (data) delete data;
50  if (inter) delete inter;
51 }
52 
53 void vfile::prepare (void) {
54 
55  // check type of interpolator
56  char * type = getPropertyString ("Interpolator");
57  if (!strcmp (type, "linear")) {
58  interpolType = INTERPOL_LINEAR;
59  } else if (!strcmp (type, "cubic")) {
60  interpolType = INTERPOL_CUBIC;
61  } else if (!strcmp (type, "hold")) {
62  interpolType = INTERPOL_HOLD;
63  }
64 
65  // check type of repetition
66  type = getPropertyString ("Repeat");
67  if (!strcmp (type, "no")) {
68  // rectangular data
69  dataType = REPEAT_NO;
70  } else if (!strcmp (type, "yes")) {
71  // polar data
72  dataType = REPEAT_YES;
73  }
74 
75  // load file with samples
76  char * file = getPropertyString ("File");
77  if (data == NULL) {
78  if (strlen (file) > 4 && !strcasecmp (&file[strlen (file) - 4], ".dat"))
79  data = dataset::load (file);
80  else
81  data = dataset::load_csv (file);
82  if (data != NULL) {
83  // check number of variables / dependencies defined by that file
84  if (data->countVariables () != 1 || data->countDependencies () != 1) {
85  logprint (LOG_ERROR, "ERROR: file `%s' must have time as an "
86  "independent and the voltage source samples as dependents\n",
87  file);
88  return;
89  }
90  vector * vs = data->getVariables(); // voltage
91  vector * ts = data->getDependencies(); // time
92  inter = new interpolator ();
93  inter->rvectors (vs, ts);
94  inter->prepare (interpolType, dataType);
95  }
96  }
97 }
98 
99 void vfile::initSP (void) {
100  allocMatrixS ();
101  setS (NODE_1, NODE_1, 0.0);
102  setS (NODE_1, NODE_2, 1.0);
103  setS (NODE_2, NODE_1, 1.0);
104  setS (NODE_2, NODE_2, 0.0);
105 }
106 
107 void vfile::initDC (void) {
108  allocMatrixMNA ();
110  prepare ();
111  setE (VSRC_1, 0);
112 }
113 
114 void vfile::initAC (void) {
115  initDC ();
116  setE (VSRC_1, 0);
117 }
118 
119 void vfile::initTR (void) {
120  initDC ();
121 }
122 
123 void vfile::calcTR (nr_double_t t) {
124  nr_double_t G = getPropertyDouble ("G");
125  nr_double_t T = getPropertyDouble ("T");
126  nr_double_t u = inter->rinterpolate (t - T);
127  setE (VSRC_1, G * u);
128 }
129 
130 // properties
131 PROP_REQ [] = {
132  { "File", PROP_STR, { PROP_NO_VAL, "vfile.dat" }, PROP_NO_RANGE },
133  PROP_NO_PROP };
134 PROP_OPT [] = {
135  { "Interpolator", PROP_STR, { PROP_NO_VAL, "linear" },
136  PROP_RNG_STR3 ("hold", "linear", "cubic") },
137  { "Repeat", PROP_STR, { PROP_NO_VAL, "no" }, PROP_RNG_YESNO },
138  { "G", PROP_REAL, { 1, PROP_NO_STR }, PROP_NO_RANGE },
139  { "T", PROP_REAL, { 0, PROP_NO_STR }, PROP_POS_RANGE },
140  PROP_NO_PROP };
141 struct define_t vfile::cirdef =