Qucs-core  0.0.19
vfile.cpp
Go to the documentation of this file.
00001 /*
00002  * vfile.cpp - file based voltage source class implementation
00003  *
00004  * Copyright (C) 2007 Gunther Kraut <gn.kraut@t-online.de>
00005  * Copyright (C) 2007, 2008, 2009 Stefan Jahn <stefan@lkcc.org>
00006  *
00007  * This is free software; you can redistribute it and/or modify
00008  * it under the terms of the GNU General Public License as published by
00009  * the Free Software Foundation; either version 2, or (at your option)
00010  * any later version.
00011  *
00012  * This software is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this package; see the file COPYING.  If not, write to
00019  * the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
00020  * Boston, MA 02110-1301, USA.
00021  *
00022  * $Id$
00023  *
00024  */
00025 
00026 #if HAVE_CONFIG_H
00027 # include <config.h>
00028 #endif
00029 
00030 #include "component.h"
00031 #include "dataset.h"
00032 #include "poly.h"
00033 #include "spline.h"
00034 #include "interpolator.h"
00035 #include "vfile.h"
00036 
00037 using namespace qucs;
00038 
00039 // Constructor creates vfile object in memory.
00040 vfile::vfile () : circuit (2) {
00041   type = CIR_VFILE;
00042   setVSource (true);
00043   setVoltageSources (1);
00044   interpolType = dataType = 0;
00045   data = NULL;
00046   inter = NULL;
00047 }
00048 
00049 // Destructor deletes vfile object from memory.
00050 vfile::~vfile () {
00051   delete data;
00052   delete inter;
00053 }
00054 
00055 void vfile::prepare (void) {
00056 
00057   // check type of interpolator
00058   const char * itype = getPropertyString ("Interpolator");
00059   if (!strcmp (itype, "linear")) {
00060     interpolType = INTERPOL_LINEAR;
00061   } else if (!strcmp (itype, "cubic")) {
00062     interpolType = INTERPOL_CUBIC;
00063   } else if (!strcmp (itype, "hold")) {
00064     interpolType = INTERPOL_HOLD;
00065   }
00066 
00067   // check type of repetition
00068   const char * rtype = getPropertyString ("Repeat");
00069   if (!strcmp (rtype, "no")) {
00070     // rectangular data
00071     dataType = REPEAT_NO;
00072   } else if (!strcmp (rtype, "yes")) {
00073     // polar data
00074     dataType = REPEAT_YES;
00075   }
00076 
00077   // load file with samples
00078   const char * file = getPropertyString ("File");
00079   if (data == NULL) {
00080     if (strlen (file) > 4 && !strcasecmp (&file[strlen (file) - 4], ".dat"))
00081       data = dataset::load (file);
00082     else
00083       data = dataset::load_csv (file);
00084     if (data != NULL) {
00085       // check number of variables / dependencies defined by that file
00086       if (data->countVariables () != 1 || data->countDependencies () != 1) {
00087         logprint (LOG_ERROR, "ERROR: file `%s' must have time as an "
00088                   "independent and the voltage source samples as dependents\n",
00089                   file);
00090         return;
00091       }
00092       qucs::vector * vs = data->getVariables();    // voltage
00093       qucs::vector * ts = data->getDependencies(); // time
00094       inter = new interpolator ();
00095       inter->rvectors (vs, ts);
00096       inter->prepare (interpolType, dataType);
00097     }
00098   }
00099 }
00100 
00101 void vfile::initSP (void) {
00102   allocMatrixS ();
00103   setS (NODE_1, NODE_1, 0.0);
00104   setS (NODE_1, NODE_2, 1.0);
00105   setS (NODE_2, NODE_1, 1.0);
00106   setS (NODE_2, NODE_2, 0.0);
00107 }
00108 
00109 void vfile::initDC (void) {
00110   allocMatrixMNA ();
00111   voltageSource (VSRC_1, NODE_1, NODE_2);
00112   prepare ();
00113   setE (VSRC_1, 0);
00114 }
00115 
00116 void vfile::initAC (void) {
00117   initDC ();
00118   setE (VSRC_1, 0);
00119 }
00120 
00121 void vfile::initTR (void) {
00122   initDC ();
00123 }
00124 
00125 void vfile::calcTR (nr_double_t t) {
00126   nr_double_t G = getPropertyDouble ("G");
00127   nr_double_t T = getPropertyDouble ("T");
00128   nr_double_t u = inter->rinterpolate (t - T);
00129   setE (VSRC_1, G * u);
00130 }
00131 
00132 // properties
00133 PROP_REQ [] = {
00134   { "File", PROP_STR, { PROP_NO_VAL, "vfile.dat" }, PROP_NO_RANGE },
00135   PROP_NO_PROP };
00136 PROP_OPT [] = {
00137   { "Interpolator", PROP_STR, { PROP_NO_VAL, "linear" },
00138     PROP_RNG_STR3 ("hold", "linear", "cubic") },
00139   { "Repeat", PROP_STR, { PROP_NO_VAL, "no" }, PROP_RNG_YESNO },
00140   { "G", PROP_REAL, { 1, PROP_NO_STR }, PROP_NO_RANGE },
00141   { "T", PROP_REAL, { 0, PROP_NO_STR }, PROP_POS_RANGE },
00142   PROP_NO_PROP };
00143 struct define_t vfile::cirdef =
00144   { "Vfile", 2, PROP_COMPONENT, PROP_NO_SUBSTRATE, PROP_LINEAR, PROP_DEF };