My Project  0.0.16
QUCS Mapping
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
parasweep.cpp
Go to the documentation of this file.
1 /*
2  * parasweep.cpp - parameter sweep class implementation
3  *
4  * Copyright (C) 2004, 2005, 2006, 2007, 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: parasweep.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 <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 
33 #include "logging.h"
34 #include "complex.h"
35 #include "object.h"
36 #include "vector.h"
37 #include "dataset.h"
38 #include "net.h"
39 #include "netdefs.h"
40 #include "ptrlist.h"
41 #include "analysis.h"
42 #include "variable.h"
43 #include "environment.h"
44 #include "sweep.h"
45 #include "parasweep.h"
46 
47 // Constructor creates an unnamed instance of the parasweep class.
49  var = NULL;
50  swp = NULL;
51  eqn = NULL;
52  type = ANALYSIS_SWEEP;
53 }
54 
55 // Constructor creates a named instance of the parasweep class.
57  var = NULL;
58  swp = NULL;
59  eqn = NULL;
61 }
62 
63 // Destructor deletes the parasweep class object.
65  if (swp) delete swp;
66 }
67 
68 /* The copy constructor creates a new instance of the parasweep class
69  based on the given parasweep object. */
71  var = new variable (*p.var);
72  if (p.swp) swp = new sweep (*p.swp);
73 }
74 
75 // Short macro in order to obtain the correct constant value.
76 #define D(con) ((constant *) (con))->d
77 #define E(equ) ((eqn::node *) (equ))
78 
79 /* Initializes the parameter sweep. */
81  char * n;
82  constant * val;
83 
84  // get fixed simulation properties
85  n = getPropertyString ("Param");
86 
87  // create sweep if necessary
88  if (swp == NULL) {
89  swp = createSweep (n);
90  }
91 
92  // get parameter name and the appropriate variable from the current
93  // environment, possibly add the variable to the environment if it
94  // does not exist yet (which is somehow useless at all)
95  if ((var = env->getVariable (n)) == NULL) {
96  var = new variable (n);
97  val = new constant (TAG_DOUBLE);
98  var->setConstant (val);
99  env->addVariable (var);
100  }
101  else val = var->getConstant ();
102 
103  // put variable also into equation checker if necessary
104  if (!env->getChecker()->containsVariable (n)) {
105  eqn = env->getChecker()->addDouble ("#sweep", n, 0);
106  }
107 
108  // initialize first sweep value in environment and equation checker
109  nr_double_t v = swp->get (0);
110  env->setDoubleConstant (n, v);
111  env->setDouble (n, v);
112 
113  // also run initialize functionality for all children
114  for (int k = 0; actions && k < actions->length (); k++) {
115  analysis * a = actions->get (k);
116  a->initialize ();
117  a->setProgress (false);
118  }
119  return 0;
120 }
121 
122 /* Cleans the parameter sweep up. */
123 int parasweep::cleanup (void) {
124 
125  // remove additional equation from equation checker
126  if (eqn) {
127  env->getChecker()->dropEquation (E (eqn));
128  delete E (eqn);
129  eqn = NULL;
130  }
131 
132  // also run cleanup functionality for all children
133  for (int k = 0; actions && k < actions->length (); k++) {
134  analysis * a = actions->get (k);
135  a->cleanup ();
136  }
137  return 0;
138 }
139 
140 /* This is the parameter sweep solver. */
141 int parasweep::solve (void) {
142  int err = 0;
143  char * n;
144  runs++;
145 
146  // get fixed simulation properties
147  n = getPropertyString ("Param");
148 
149  // run the parameter sweep
150  swp->reset ();
151  for (int i = 0; i < swp->getSize (); i++) {
152  // obtain next sweep point
153  nr_double_t v = swp->next ();
154  // display progress bar if requested
155  if (progress) logprogressbar (i, swp->getSize (), 40);
156  // update environment and equation checker, then run solver
157  env->setDoubleConstant (n, v);
158  env->setDouble (n, v);
159  env->runSolver ();
160  // save results (swept parameter values)
161  if (runs == 1) saveResults ();
162 #if DEBUG
163  logprint (LOG_STATUS, "NOTIFY: %s: running netlist for %s = %g\n",
164  getName (), n, v);
165 #endif
166  for (int k = 0; k < actions->length (); k++) {
167  analysis * a = actions->get (k);
168  err |= a->solve ();
169  // assign variable dataset dependencies to last order analyses
171  for (ptrlistiterator<analysis> it (*last); *it; ++it) {
172  data->assignDependency ((*it)->getName (), var->getName ());
173  }
174  }
175  }
176  // clear progress bar
177  if (progress) logprogressclear (40);
178  return err;
179 }
180 
181 /* This function saves the results of a single solve() functionality
182  into the output dataset. */
184  vector * v;
185 
186  // add current frequency to the dependencies of the output dataset
187  if ((v = data->findDependency (var->getName ())) == NULL) {
188  v = new vector (var->getName ());
189  v->setOrigin (getName ());
190  data->addDependency (v);
191  }
192  v->add (D (var->getConstant ()));
193 }
194 
195 // properties
196 PROP_REQ [] = {
197  { "Type", PROP_STR, { PROP_NO_VAL, "lin" }, PROP_RNG_TYP },
198  { "Param", PROP_STR, { PROP_NO_VAL, "R1" }, PROP_NO_RANGE },
199  { "Sim", PROP_STR, { PROP_NO_VAL, "DC1" }, PROP_NO_RANGE },
200  PROP_NO_PROP };
201 PROP_OPT [] = {
202  { "Points", PROP_INT, { 5, PROP_NO_STR }, PROP_MIN_VAL (2) },
203  { "Stop", PROP_REAL, { 50, PROP_NO_STR }, PROP_NO_RANGE },
204  { "Start", PROP_REAL, { 5, PROP_NO_STR }, PROP_NO_RANGE },
205  { "Values", PROP_LIST, { 5, PROP_NO_STR }, PROP_NO_RANGE },
206  PROP_NO_PROP };
207 struct define_t parasweep::anadef =