Skip to content

Commit

Permalink
Add spice entry for time-controlled switch
Browse files Browse the repository at this point in the history
  • Loading branch information
Radvall committed Nov 9, 2023
1 parent df925bc commit a545447
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 5 deletions.
85 changes: 80 additions & 5 deletions qucs/components/switch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,34 +17,35 @@
#include "switch.h"
#include "node.h"
#include "schematic.h"
#include "misc.h"
#include "extsimkernels/spicecompat.h"

#include <QDebug>

Switch::Switch()
{
Description = QObject::tr("switch (time controlled)");
Simulator = spicecompat::simQucsator;

Props.append(new Property("init", "off", false,
QObject::tr("initial state")+" [on, off]"));
Props.append(new Property("time", "1 ms", false,
QObject::tr("time when state changes (semicolon separated list possible, even numbered lists are repeated)")));
Props.append(new Property("Ron", "0", false,
Props.append(new Property("Ron", "1e-9", false,
QObject::tr("resistance of \"on\" state in ohms")));
Props.append(new Property("Roff", "1e12", false,
QObject::tr("resistance of \"off\" state in ohms")));
Props.append(new Property("Temp", "26.85", false,
QObject::tr("simulation temperature in degree Celsius")));
QObject::tr("simulation temperature in degree Celsius (Qucsator only)")));
Props.append(new Property("MaxDuration", "1e-6", false,
QObject::tr("Max possible switch transition time (transition time 1/100 smallest value in 'time', or this number)")));
Props.append(new Property("Transition", "spline", false,
QObject::tr("Resistance transition shape")+" [abrupt, linear, spline]"));
QObject::tr("Resistance transition shape (Qucsator only)")+" [abrupt, linear, spline]"));

createSymbol();
tx = x1+4;
ty = y2+4;
Model = "Switch";
Name = "S";
SpiceModel = "S";
}

// -------------------------------------------------------
Expand Down Expand Up @@ -86,6 +87,80 @@ QString Switch::netlist()
return s + '\n';
}

QString Switch::spice_netlist(bool)
{
QString s = spicecompat::check_refdes(Name,SpiceModel);
QString port1 = spicecompat::normalize_node_name(Ports.at(0)->Connection->Name);
QString port2 = spicecompat::normalize_node_name(Ports.at(1)->Connection->Name);

s += QString(" %1 %2 control_net%3 0 switch_model%3\n").arg(port1).arg(port2).arg(Name);

QString init = spicecompat::normalize_value(getProperty("init")->Value);
QString times = spicecompat::normalize_value(getProperty("time")->Value);
QStringList timesList = times.split(";");
QString Ron = spicecompat::normalize_value(getProperty("Ron")->Value);
QString Roff = spicecompat::normalize_value(getProperty("Roff")->Value);
QString Max_duration = spicecompat::normalize_value(getProperty("MaxDuration")->Value);

double fac, timeValue, changingTime, maxDuration, firstTimeVal, time = 0.0;
QString unit;

misc::str2num(timesList[0].toLower(), maxDuration,unit,fac);
maxDuration *= fac / 100;
misc::str2num(timesList[0].toLower(),firstTimeVal,unit,fac);
firstTimeVal *= fac / 100;

if (firstTimeVal > maxDuration){
changingTime = maxDuration;
}
else {
changingTime = firstTimeVal;
}

QString oddValue, evenValue;
if (init == "{OFF}") {
oddValue = "0";
evenValue = "1";
} else {
oddValue = "1";
evenValue = "0";
}

s += QString("V%1 control_net%1 0 DC %2 PWL(0 %2").arg(Name).arg(oddValue);

for (int i = 0; i < timesList.size(); i++) {
QString timeStep = timesList[i].toLower();
misc::str2num(timeStep,timeValue,unit,fac);
timeValue *= fac;

if (i == 0) {
time += timeValue - changingTime;
s += QString(" %1 %2").arg(time).arg(oddValue);
time += changingTime;
s += QString(" %1 %2").arg(time).arg(evenValue);

} else {
if (i % 2 == 1) {
time += timeValue- changingTime;
s += QString(" %1 %2").arg(time).arg(evenValue);
time += changingTime;
s += QString(" %1 %2").arg(time).arg(oddValue);
}
else{
time += timeValue- changingTime;
s += QString(" %1 %2").arg(time).arg(oddValue);
time += changingTime;
s += QString(" %1 %2").arg(time).arg(evenValue);
}
}
}

s += ")\n";

s += QString(".model switch_model%1 sw vt =0.5 ron =%2 roff =%3\n").arg(Name).arg(Ron).arg(Roff);
return s;
}

// -------------------------------------------------------
void Switch::createSymbol()
{
Expand Down
1 change: 1 addition & 0 deletions qucs/components/switch.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class Switch : public MultiViewComponent {
static Element* info(QString&, char* &, bool getNewOne=false);

protected:
QString spice_netlist(bool isXyce = false);
QString netlist();
void createSymbol();
};
Expand Down

0 comments on commit a545447

Please sign in to comment.