Qucs-core  0.0.19
digisource.cpp
Go to the documentation of this file.
00001 /*
00002  * digisource.cpp - digital source class implementation
00003  *
00004  * Copyright (C) 2005, 2008 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 "digisource.h"
00031 
00032 using namespace qucs;
00033 
00034 digisource::digisource () : circuit (1) {
00035   type = CIR_DIGISOURCE;
00036   setVSource (true);
00037   setVoltageSources (1);
00038 }
00039 
00040 void digisource::initSP (void) {
00041   allocMatrixS ();
00042   setS (NODE_1, NODE_1, -1.0);
00043 }
00044 
00045 void digisource::initDC (void) {
00046   const char * const init = getPropertyString ("init");
00047   nr_double_t v = getPropertyDouble ("V");
00048   bool lo = !strcmp (init, "low");
00049   allocMatrixMNA ();
00050   setC (VSRC_1, NODE_1, 1.0);
00051   setB (NODE_1, VSRC_1, 1.0);
00052   setD (VSRC_1, VSRC_1, 0.0);
00053   setE (VSRC_1, lo ? 0 : v);
00054 }
00055 
00056 void digisource::initAC (void) {
00057   initDC ();
00058   setE (VSRC_1, 0);
00059 }
00060 
00061 void digisource::initTR (void) {
00062   qucs::vector * values = getPropertyVector ("times");
00063   T = real (sum (*values));
00064   initDC ();
00065 }
00066 
00067 void digisource::calcTR (nr_double_t t) {
00068   const char * const init = getPropertyString ("init");
00069   nr_double_t v = getPropertyDouble ("V");
00070   qucs::vector * values = getPropertyVector ("times");
00071   bool lo = !strcmp (init, "low");
00072   nr_double_t ti = 0;
00073 
00074   t = t - T * qucs::floor (t / T);
00075   for (int i = 0; i < values->getSize (); i++) {
00076     ti += real (values->get (i));
00077     if (t >= ti) lo = !lo; else break;
00078   }
00079 
00080   setE (VSRC_1, lo ? 0 : v);
00081 }
00082 
00083 // properties
00084 PROP_REQ [] = {
00085   { "init", PROP_STR, { PROP_NO_VAL, "low" }, PROP_RNG_STR2 ("low", "high") },
00086   { "times", PROP_LIST, { 1e-9, PROP_NO_STR }, PROP_POS_RANGE },
00087   PROP_NO_PROP };
00088 PROP_OPT [] = {
00089   { "V", PROP_REAL, { 1, PROP_NO_STR }, PROP_POS_RANGE }, PROP_NO_PROP };
00090 struct define_t digisource::cirdef =
00091   { "DigiSource",
00092     1, PROP_COMPONENT, PROP_NO_SUBSTRATE, PROP_LINEAR, PROP_DEF };