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