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