Qucs-core  0.0.18
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
qucs_interface.cpp
Go to the documentation of this file.
1 /*
2  * qucs_interface.cpp - m-code interface implementation
3  *
4  * Copyright (C) 2013 Richard Crozier <richard.crozier@yahoo.co.uk>
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 <assert.h>
32 #include <string.h>
33 #include <time.h>
34 
35 #include "logging.h"
36 #include "precision.h"
37 #include "component.h"
38 #include "components.h"
39 #include "net.h"
40 #include "input.h"
41 #include "dataset.h"
42 #include "equation.h"
43 #include "environment.h"
44 #include "exceptionstack.h"
45 #include "check_netlist.h"
46 #include "module.h"
47 #include "qucs_interface.h"
48 #include "analysis.h"
49 #include "e_trsolver.h"
50 
51 #if HAVE_UNISTD_H
52 #include <unistd.h>
53 #endif
54 
55 using namespace qucs;
56 
57 // constructor
58 qucsint::qucsint ()
59 {
60 
61  //int listing = 0;
62  ret = 0;
63  err = 0;
64 
65  loginit ();
66  precinit ();
67  ::srand (::time (NULL));
68 
69 }
70 
71 qucsint::qucsint (char* infile)
72 {
73  //int listing = 0;
74  ret = 0;
75  err = 0;
76 
77  loginit ();
78  precinit ();
79  ::srand (::time (NULL));
80 
81  prepare_netlist (infile);
82 }
83 
84 // destructor
85 qucsint::~qucsint ()
86 {
87  delete subnet;
88  delete in;
89 // delete out;
90  delete root;
91 
92  // delete modules
94 
96 }
97 
98 int qucsint::prepare_netlist (char * infile)
99 {
100 
101  // create static modules
103 
104  // create root environment
105  root = new qucs::environment ("root");
106 
107  // create netlist object and input
108  subnet = new net ("subnet");
109 
110  // test if the file actually exists
111  FILE * pFile;
112  pFile = fopen (infile,"r");
113  if (pFile!=NULL)
114  {
115  // close the file again
116  fclose (pFile);
117  }
118  else
119  {
120  return NETLIST_FILE_NOT_FOUND;
121  }
122 
123  in = infile ? new input (infile) : new input ();
124 
125  // pass root environment to netlist object and input
126  subnet->setEnv (root);
127  in->setEnv (root);
128 
129  // get input netlist
130  if (in->netlist (subnet) != 0)
131  {
132  if (netlist_check)
133  {
134  // replace with mex error message
135  logprint (LOG_STATUS, "checker notice, netlist check FAILED\n");
136  }
137  return NETLIST_FAILED_CHECK;
138  }
139 
140  // attach a ground to the netlist
141  gnd = new ground ();
142  gnd->setNode (0, "gnd");
143  gnd->setName ("GND");
144  subnet->insertCircuit (gnd);
145 
146  // apply some data to all analyses
147  for (int i = 0; i < subnet->getNActions(); i++)
148  {
149  subnet->setActionNet(i, subnet);
150  //a->setData (out);
151  }
152 
153  return NETLIST_OK;
154 }
155 
156 int qucsint::evaluate ()
157 {
158  // analyse the netlist
159  err = 0;
160  ret = 0;
161  out = subnet->runAnalysis (err);
162  ret |= err;
163 
164  return ret;
165 }
166 
167 int qucsint::output (char * outfile)
168 {
169  // evaluate output dataset
170  ret |= root->equationSolver (out);
171 
172  if (outfile != NULL)
173  {
174  out->setFile (outfile);
175  out->print ();
176  }
177 
178  return ret;
179 }
180 
181 /*/////////////////////////////////////////////////////////////////////////////
182 
183  trsolver_interface
184 
186 
187 trsolver_interface::trsolver_interface ()
188  : qucsint ()
189 {
190  etr = 0;
191  isInitialised = false;
192 }
193 
194 trsolver_interface::trsolver_interface (char * infile)
195  : qucsint (infile)
196 {
197  isInitialised = false;
198 
199  int result = prepare_netlist (infile);
200 
201  if (result == NETLIST_OK)
202  {
203  getETR ();
204  }
205 }
206 
207 //trsolver_interface::~trsolver_interface ()
208 //{
209 //
210 //}
211 
212 int trsolver_interface::getN (void)
213 {
214  if (etr) return etr->getN ();
215  else return -2;
216 }
217 
218 int trsolver_interface::getM (void)
219 {
220  if (etr) return etr->getM ();
221  else return -2;
222 }
223 
224 // gets a pointer to the trsolver_interface if it is present
225 void trsolver_interface::getETR()
226 {
227  // get a pointer to the external transient solver interface
228  // the pointer will be to the supclass of e_trsolver, analysis
229  // so we must perform a dynamic cast to convert it to an e_trsolver
230  etr = dynamic_cast<e_trsolver *>(subnet->findAnalysis (ANALYSIS_E_TRANSIENT));
231 
232  if (etr)
233  {
234  isInitialised = true;
235  }
236  else
237  {
238  isInitialised = false;
239  }
240 }
241 
242 int trsolver_interface::init (double start, double firstdelta, int mode)
243 {
244  if (etr) return etr->init ((nr_double_t)start, (nr_double_t)firstdelta, mode);
245  else return -2;
246 }
247 
248 
249 int trsolver_interface::stepsolve_sync (double synctime)
250 {
251  if (etr) return etr->stepsolve_sync ((nr_double_t)synctime);
252  else return -2;
253 }
254 
255 void trsolver_interface::acceptstep_sync (void)
256 {
257  if (etr) etr->acceptstep_sync ();
258 }
259 
260 int trsolver_interface::stepsolve_async (double steptime)
261 {
262  if (etr) return etr->stepsolve_async ((nr_double_t)steptime);
263  else return -2;
264 }
265 
266 void trsolver_interface::acceptstep_async (void)
267 {
268  if (etr) etr->acceptstep_async ();
269 }
270 
271 void trsolver_interface::rejectstep_async (void)
272 {
273  if (etr) etr->rejectstep_async ();
274 }
275 
276 void trsolver_interface::getsolution (double * sol)
277 {
278  if (etr) etr->getsolution (sol);
279 }
280 
281 int trsolver_interface::setECVSVoltage(char * ecvsname, double V)
282 {
283  if (etr) return etr->setECVSVoltage (ecvsname, (nr_double_t)V);
284  else return -2;
285 }
286 
287 int trsolver_interface::getJacRows ()
288 {
289  if (etr) return etr->getJacRows ();
290  else return -2;
291 }
292 
293 int trsolver_interface::getJacCols ()
294 {
295  if (etr) return etr->getJacCols ();
296  else return -2;
297 }
298 
299 int trsolver_interface::getJacData (int r, int c, double& data)
300 {
301  if (etr)
302  {
303  nr_double_t tempdata = (nr_double_t)data;
304  etr->getJacData (r, c, tempdata);
305  data = (double)tempdata;
306  return 0;
307  }
308  else
309  {
310  return -2;
311  }
312 }
313 
314 int trsolver_interface::getNodeV (char * label, double& nodeV)
315 {
316  if (etr)
317  {
318  nr_double_t tempnodeV = (nr_double_t)nodeV;
319  int flag = etr->getNodeV (label, tempnodeV);
320  nodeV = (double)tempnodeV;
321  return flag;
322  }
323  else
324  {
325  return -2;
326  }
327 }
328 
329 int trsolver_interface::getVProbeV (char * probename, double& probeV)
330 {
331  if (etr)
332  {
333  nr_double_t tempprobeV = (nr_double_t)probeV;
334  int flag = etr->getVProbeV (probename, tempprobeV);
335  probeV = (double)tempprobeV;
336  return flag;
337  }
338  else
339  {
340  return -2;
341  }
342 }
343 
344 
345 int trsolver_interface::getIProbeI (char * probename, double& probeI)
346 {
347  if (etr)
348  {
349  nr_double_t tempprobeI = (nr_double_t)probeI;
350  int flag = etr->getIProbeI (probename, tempprobeI);
351  probeI = (double)tempprobeI;
352  return flag;
353  }
354  else
355  {
356  return -2;
357  }
358 }
359 
360 //void trsolver_interface::debug (void)
361 //{
362 // if (etr) etr->debug ();
363 //}
364 //
365 
366 void trsolver_interface::printSolution (void)
367 {
368  if (etr) etr->printx ();
369 }
370 
371 void trsolver_interface::setMessageFcn(void (*newmessagefcn)(int level, const char * format, ...))
372 {
373  etr->messagefcn = newmessagefcn;
374 }
Houses the settings for netlist evaluation.
Definition: environment.h:51
static void registerModules(void)
Definition: module.cpp:156
i
Definition: parse_mdl.y:516
int netlist_check
Definition: input.cpp:51
The analysis class header file.
The externally controlled trsolver external class header file.
The generic external interface class header file.
void loginit(void)
Definition: logging.c:51
void precinit(void)
Definition: precision.c:36
void netlist_destroy_env(void)
The environment class definition.
static void unregisterModules(void)
Definition: module.cpp:323
#define LOG_STATUS
Definition: logging.h:29
void logprint(int level, const char *format,...)
Definition: logging.c:37
Definition: ground.h:28