Skip to content

Commit

Permalink
Merge branch 'ra3xdh-mutualx_fix' - pull request #214
Browse files Browse the repository at this point in the history
Fixes #213
  • Loading branch information
in3otd committed Apr 21, 2015
2 parents 43a668c + 4000346 commit 3d26eeb
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 34 deletions.
4 changes: 4 additions & 0 deletions qucs/qucs/components/component.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -819,6 +819,8 @@ bool Component::load(const QString& _s)
tmp = 8;
else if(Model == "VHDL")
tmp = 2;
else if(Model == "MUTX")
tmp = 5; // number of properties for the default MUTX (2 inductors)
else tmp = counts + 1; // "+1" because "counts" could be zero

for(; tmp<=(int)counts/2; tmp++)
Expand All @@ -830,6 +832,8 @@ bool Component::load(const QString& _s)
z++;
n = s.section('"',z,z); // property value
z++;
//qDebug() << "LOAD: " << p1->Description;

// not all properties have to be mentioned (backward compatible)
if(z > counts) {
if(p1->Description.isEmpty())
Expand Down
87 changes: 53 additions & 34 deletions qucs/qucs/components/mutualx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ MutualX::MutualX()
Model = "MUTX";
Name = "Tr";

const int init_coils=4; // initial number of coils
const int init_coils=2; // initial number of coils
// must be the first property!
Props.append(new Property("coils", QString::number(init_coils), false,
QObject::tr("number of mutual inductances")));
QObject::tr("number of mutual inductances")));

for (int i=1;i<=init_coils; i++) {
Props.append(new Property("L"+QString::number(i), "1 mH", false,
Expand All @@ -57,7 +57,7 @@ Element* MutualX::info(QString& Name, char* &BitmapFile, bool getNewOne)

if(getNewOne) {
MutualX* p = new MutualX();
p->Props.at(0)->Value = "4";
p->Props.at(0)->Value = "2";
p->recreate(0);
return p;
}
Expand Down Expand Up @@ -135,8 +135,8 @@ void MutualX::createSymbol()
{
// adjust port number
int Num = Props.first()->Value.toInt();
if(Num < 1)
Num = 1;
if(Num < 2)
Num = 2;
else if(Num > 8)
Num = 8;
Props.first()->Value = QString::number(Num);
Expand All @@ -145,39 +145,58 @@ void MutualX::createSymbol()
oldNumProps = Props.count();
NumProps = Num + Num * (Num - 1) / 2 + 1;
if (oldNumProps!=NumProps) { // Coils count was changed
int oldCoils = rint(0.5*(sqrt(8*oldNumProps-7)-1.0)); // calculate old number of coils
// we need to solve quadratic equation
int dCoils = abs(oldCoils - Num); // how many coils were added/removed?
int k_cnt = (Num*(Num-1))/2;
int old_k_cnt = (oldCoils*(oldCoils-1))/2;
int delta_cnt = abs(old_k_cnt-k_cnt);

if (oldCoils>Num) { // reduce coils number

for (int i=0;i<dCoils;i++) {
Props.remove(oldCoils);
}
for (int i=0;i<delta_cnt;i++) {
Props.removeLast();
}
} else { // add new coils
for(int i = 0; i < dCoils; i++) { // add new properties for coils
Props.insert(oldCoils+1, new Property("L"+QString::number(Num-i), "1 mH", false,
QObject::tr("inductance of coil") + " " + QString::number(Num-i)));
}
for (int i=0;i<delta_cnt;i++) { // and for coupling coeffs.
Props.append(new Property("k", "0.9", false, " "));
}
int oldCoils = rint(0.5*(sqrt(8*oldNumProps-7)-1.0)); // calculate old number of coils
// we need to solve quadratic equation
int dCoils = abs(oldCoils - Num); // how many coils were added/removed?

if (oldCoils>Num) { // reduce coils number
for(int i = 0; i < dCoils; i++)
Props.remove(Num+1); // remove excess coils
// remove only the no longer valid coupling coefficients, leave the
// ones related to existing coils untouched
for(int i = 1,state=1; i < oldCoils; i++)
for(int j = i+1; j <= oldCoils; j++,state++) {
if ((i>Num)||(j>Num)) {
Props.remove(Num + state);
state--;
}
}

} else { // add new coils
for(int i = 0; i < dCoils; i++) { // add new properties for coils
Props.insert(oldCoils+1, new Property("L"+QString::number(Num-i),
"1 mH",
false,
QObject::tr("inductance of coil") + " " + QString::number(Num-i)));
}

for(int i = 1,state=1; i < Num; i++) // Adjust coupling coeffs names
for(int i = 1,state=1; i < Num; i++)
for(int j = i+1; j <= Num; j++,state++) {
Props.at(Num+state)->Name = "k" + QString::number(i) + QString::number(j);
Props.at(Num+state)->Description =
QObject::tr("coupling factor between coil %1 and coil %2").arg(i).arg(j);
if ((i>oldCoils)||(j>oldCoils)) {
Props.insert(Num + state, new Property("k", "0.9", false, " "));
}
}

}
}

// in any case rewrite properties Name and Description
// (when loading a component, added properties have a default name)
// adjust coils names
Property * p1 = Props.at(1);
for(int i = 1; i <= Num; i++) {
p1->Name = "L"+QString::number(i);
p1->Description = QObject::tr("inductance of coil") + " " + QString::number(i);
p1 = Props.next();
}
// adjust coupling coeffs names
for(int i = 1,state=1; i < Num; i++)
for(int j = i+1; j <= Num; j++,state++) {
Props.at(Num+state)->Name = "k" + QString::number(i) + QString::number(j);
Props.at(Num+state)->Description =
QObject::tr("coupling factor between coil %1 and coil %2").arg(i).arg(j);
}

// draw symbol
int x = -10 * (Num-1);
Texts.append(new Text(x-9,-22,"1"));
Expand All @@ -194,12 +213,12 @@ void MutualX::createSymbol()
Arcs.append(new Arc(x, -6,12,12, 16*270,16*180, QPen(Qt::darkBlue,2)));
Arcs.append(new Arc(x, 6,12,12, 16*270,16*180, QPen(Qt::darkBlue,2)));

x += 6;
x += 6;
Lines.append(new Line(x,-18,x,-30,QPen(Qt::darkBlue,2)));
Lines.append(new Line(x, 18,x, 30,QPen(Qt::darkBlue,2)));

Ports.append(new Port(x,-30));
Ports.append(new Port(x, 30));
x += 14;
x += 14;
}
}

0 comments on commit 3d26eeb

Please sign in to comment.