Qucs-core  0.0.18
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
check_zvr.cpp
Go to the documentation of this file.
1 /*
2  * check_zvr.cpp - iterate a zvr file
3  *
4  * Copyright (C) 2006, 2007 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 <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <cmath>
33 #include <assert.h>
34 #include <float.h>
35 #include <ctype.h>
36 
37 #include "strlist.h"
38 #include "object.h"
39 #include "complex.h"
40 #include "vector.h"
41 #include "dataset.h"
42 #include "constants.h"
43 #include "check_zvr.h"
44 
45 using namespace qucs;
46 
47 // Global variables.
48 dataset * zvr_result = NULL;
49 struct zvr_data_t * zvr_root = NULL;
50 
51 // Creates a valid data vector description.
52 static char * zvr_vector_txt (struct zvr_vector_t * vec) {
53  int i, i1 = -1, i2 = -1, off = 0, len = strlen (vec->n1);
54  static char txt[64];
55  // strip off leading 're' or 'im'
56  if (strstr (vec->n1, "re") == vec->n1 ||
57  strstr (vec->n1, "im") == vec->n1 ||
58  strstr (vec->n1, "db") == vec->n1) {
59  off = 2;
60  }
61  // strip off leading 'mag' or 'ang'
62  else if (strstr (vec->n1, "mag") == vec->n1 ||
63  strstr (vec->n1, "ang") == vec->n1) {
64  off = 3;
65  }
66  for (i = off; i < len; i++) if (!isalpha (vec->n1[i])) break;
67  // get index 1
68  if (i < len) {
69  if (isdigit (vec->n1[i])) { i1 = vec->n1[i] - '0'; vec->n1[i] = '\0'; }
70  }
71  // get index 2
72  if (++i < len) {
73  if (isdigit (vec->n1[i])) { i2 = vec->n1[i] - '0'; vec->n1[i] = '\0'; }
74  }
75  // create vector description
76  if (i1 >= 0 && i2 >= 0) {
77  sprintf (txt, "%s[%d,%d]", &vec->n1[off], i1, i2);
78  }
79  else if (i1 >= 0) {
80  sprintf (txt, "%s[%d]", &vec->n1[off], i1);
81  }
82  else {
83  sprintf (txt, "%s", &vec->n1[off]);
84  }
85  return txt;
86 }
87 
88 // The function free's the memory used by the ZVR checker.
89 static void zvr_finalize (void) {
90  struct zvr_data_t * root, * next;
91  // go through each dataset
92  for (root = zvr_root; root; root = next) {
93  struct zvr_vector_t * vec = root->v;
94  struct zvr_header_t * hdr = root->h;
95  next = root->next;
96  // free each data line
97  if (root->d) {
98  struct zvr_line_t * rl, * nl;
99  for (rl = root->d; rl; rl = nl) {
100  nl = rl->next;
101  free (rl);
102  }
103  }
104  // free header
105  if (hdr) {
106  if (hdr->funit) free (hdr->funit);
107  if (hdr->d_UNT) free (hdr->d_UNT);
108  if (hdr->d_FMT) free (hdr->d_FMT);
109  if (hdr->d_TYP) free (hdr->d_TYP);
110  free (hdr);
111  }
112  // free data vector
113  if (vec) {
114  if (vec->nf) free (vec->nf);
115  if (vec->n1) free (vec->n1);
116  if (vec->n2) free (vec->n2);
117  free (vec);
118  }
119  }
120  zvr_root = NULL;
121 }
122 
123 // Create a dependency string list.
124 static strlist * zvr_create_dep (char * n) {
125  strlist * dep = new strlist ();
126  dep->add (n);
127  return dep;
128 }
129 
130 // Handles the dependency vectors.
131 static void zvr_check_dependencies (void) {
132  vector * dep1 = zvr_result->getDependencies ();
133  vector * depn = (vector *) dep1->getNext ();
134  bool equal;
135 
136  // check for differing dependency vectors
137  for (equal = true; depn != NULL; depn = (vector *) depn->getNext ()) {
138  if (depn->getSize () != dep1->getSize ()) {
139  equal = false; // differs in size
140  break;
141  } else {
142  for (int i = 0; i < depn->getSize (); i++) {
143  if (depn->get (i) != dep1->get (i)) {
144  equal = false; // differs in content
145  break;
146  }
147  }
148  }
149  }
150 
151  // all dependency vectors equal
152  if (equal) {
153  vector * ndep;
154  // delete unnecessary vectors
155  for (depn = (vector *) dep1->getNext (); depn != NULL; depn = ndep) {
156  ndep = (vector *) depn->getNext ();
157  zvr_result->delDependency (depn);
158  }
159  }
160  // at least one dependency vectors not equal
161  else {
162  vector * depn = zvr_result->getDependencies ();
163  vector * varn = zvr_result->getVariables ();
164  char txt[64]; int i = 1;
165  // change name of dependency vectors as well as the dependency
166  // reference in the appropriate variable vectors
167  while (depn != NULL && varn != NULL) {
168  sprintf (txt, "%s.%d", depn->getName (), i);
169  depn->setName (txt);
170  varn->setDependencies (zvr_create_dep (txt));
171  depn = (vector *) depn->getNext ();
172  varn = (vector *) varn->getNext ();
173  i++;
174  }
175  }
176 }
177 
178 // The function performs the data conversion if necessary.
179 static void zvr_conversion (struct zvr_data_t * root) {
180  for (; root != NULL; root = root->next) {
181  struct zvr_vector_t * vec = root->v;
182  struct zvr_header_t * hdr = root->h;
183  vector * var = vec->vd; int n;
184  // magnitude in [dB] and angle in [degree]
185  if (!strcmp (hdr->d_FMT, "COMPLEX") && !strcmp (hdr->d_UNT, "dB")) {
186  for (n = 0; n < var->getSize (); n++) {
187  nr_double_t r = real (var->get (n));
188  nr_double_t i = imag (var->get (n));
189  var->set (std::polar (std::pow (10.0, r / 20.0), rad (i)), n);
190  }
191  }
192  // magnitude in [dB]
193  else if (!strcmp (hdr->d_FMT, "MAGNITUDE") && !strcmp (hdr->d_UNT, "dB")) {
194  for (n = 0; n < var->getSize (); n++) {
195  nr_double_t r = real (var->get (n));
196  var->set (std::pow (10.0, r / 20.0), n);
197  }
198  }
199  // linear magnitude and angle in [degree]
200  else if (!strcmp (hdr->d_FMT, "MA")) {
201  for (n = 0; n < var->getSize (); n++) {
202  nr_double_t r = real (var->get (n));
203  nr_double_t i = imag (var->get (n));
204  var->set (std::polar (r, rad (i)), n);
205  }
206  }
207  // magnitude in [dB] and angle in [degree]
208  else if (!strcmp (hdr->d_FMT, "DB")) {
209  for (n = 0; n < var->getSize (); n++) {
210  nr_double_t r = real (var->get (n));
211  nr_double_t i = imag (var->get (n));
212  var->set (std::polar (std::pow (10.0, r / 20.0), rad (i)), n);
213  }
214  }
215  }
216 }
217 
218 /* This function is the overall ZVR data checker. It returns zero on
219  success, non-zero otherwise. */
220 int zvr_check (void) {
221  int errors = 0;
222  struct zvr_data_t * root;
223 
224  // create a dataset
225  zvr_result = new dataset ();
226 
227  // transfer ZVR tree data into the dataset
228  for (root = zvr_root; root; root = root->next) {
229  struct zvr_vector_t * vec = root->v;
230  vec->vi->setName (vec->nf);
231  vec->vd->setName (zvr_vector_txt (vec));
232  for (struct zvr_line_t * line = root->d; line; line = line->next) {
233  vec->vi->add (line->d);
234  vec->vd->add (nr_complex_t (line->r, line->i));
235  }
236  vec->vd->setDependencies (zvr_create_dep (vec->nf));
238  zvr_result->appendVariable (vec->vd);
239  }
240 
241  // handle dependency vectors
243 
244  // perform data conversions
246 
247  // free temporary memory
248  zvr_finalize ();
249  zvr_root = NULL;
250 
251  return errors ? -1 : 0;
252 }
253 
254 // Destroys data used by the ZVR checker.
255 void zvr_destroy (void) {
256  if (zvr_result != NULL) {
257  // delete associated dataset
258  delete zvr_result;
259  zvr_result = NULL;
260  }
261  if (zvr_root != NULL) {
262  zvr_finalize ();
263  zvr_root = NULL;
264  }
265 }
266 
267 // Initializes the ZVR checker.
268 void zvr_init (void) {
269  zvr_result = NULL;
270  zvr_root = NULL;
271 }
std::complex< nr_double_t > nr_complex_t
Definition: complex.h:31
matrix real(matrix a)
Real part matrix.
Definition: matrix.cpp:568
qucs::vector * getVariables(void)
Definition: dataset.h:63
static void zvr_finalize(void)
Definition: check_zvr.cpp:89
nr_complex_t pow(const nr_complex_t z, const nr_double_t d)
Compute power function with real exponent.
Definition: complex.cpp:238
struct zvr_line_t * d
Definition: check_zvr.h:84
char * nf
Definition: check_zvr.h:70
Global physical constants header file.
static char * zvr_vector_txt(struct zvr_vector_t *vec)
Definition: check_zvr.cpp:52
n
Definition: parse_citi.y:147
int zvr_check(void)
Definition: check_zvr.cpp:220
r
Definition: parse_mdl.y:515
struct zvr_line_t * next
Definition: check_zvr.h:78
i1
Definition: parse_citi.y:148
object * getNext(void)
Definition: object.h:59
char * d_UNT
Definition: check_zvr.h:63
void appendVariable(qucs::vector *)
Definition: dataset.cpp:186
void delDependency(qucs::vector *)
Definition: dataset.cpp:103
void setDependencies(strlist *)
Definition: vector.cpp:143
i
Definition: parse_mdl.y:516
matrix imag(matrix a)
Imaginary part matrix.
Definition: matrix.cpp:581
struct zvr_header_t * h
Definition: check_zvr.h:82
next
Definition: parse_spice.y:859
struct zvr_data_t * next
Definition: check_zvr.h:85
char * n2
Definition: check_zvr.h:69
void setName(const char *)
Definition: object.cpp:78
free($1)
dataset * zvr_result
Definition: check_zvr.cpp:48
void add(nr_complex_t)
Definition: vector.cpp:151
static strlist * zvr_create_dep(char *n)
Definition: check_zvr.cpp:124
qucs::vector * vi
Definition: check_zvr.h:71
void set(int, int, nr_complex_t)
Sets the matrix element at the given row and column.
Definition: matrix.cpp:205
struct zvr_vector_t * v
Definition: check_zvr.h:83
#define rad(x)
Convert degree to radian.
Definition: constants.h:118
void zvr_init(void)
Definition: check_zvr.cpp:268
i2
Definition: parse_citi.y:160
char * n1
Definition: check_zvr.h:68
struct zvr_data_t * zvr_root
Definition: check_zvr.cpp:49
void appendDependency(qucs::vector *)
Definition: dataset.cpp:128
char * d_FMT
Definition: check_zvr.h:64
void zvr_destroy(void)
Definition: check_zvr.cpp:255
var
Definition: parse_citi.y:145
char * funit
Definition: check_zvr.h:60
nr_complex_t polar(const nr_double_t mag, const nr_double_t ang)
Construct a complex number using polar notation.
Definition: complex.cpp:551
qucs::vector * vd
Definition: check_zvr.h:72
qucs::vector * getDependencies(void)
Definition: dataset.h:62
static void zvr_conversion(struct zvr_data_t *root)
Definition: check_zvr.cpp:179
static void zvr_check_dependencies(void)
Definition: check_zvr.cpp:131
char * d_TYP
Definition: check_zvr.h:62
line