Qucs-GUI  0.0.19
/home/travis/build/Qucs/qucs/qucs/qucs/components/mutualx.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002                                  mutualx.cpp
00003                                 -----------
00004     begin                : Mon Nov 24 2014
00005     copyright            : (C) 2010 by Michael Margraf
00006     email                : michael.margraf@alumni.tu-berlin.de
00007     copyright            : (C) 2014 by Vadim Kuznetsov
00008     email                : ra3xdh@gmail.com
00009  ***************************************************************************/
00010 
00011 /***************************************************************************
00012  *                                                                         *
00013  *   This program is free software; you can redistribute it and/or modify  *
00014  *   it under the terms of the GNU General Public License as published by  *
00015  *   the Free Software Foundation; either version 2 of the License, or     *
00016  *   (at your option) any later version.                                   *
00017  *                                                                         *
00018  ***************************************************************************/
00019 
00020 
00021 #include "mutualx.h"
00022 #include "node.h"
00023 #include <cmath>
00024 
00025 MutualX::MutualX()
00026 {
00027   Description = QObject::tr("several mutual inductors");
00028 
00029   Model = "MUTX";
00030   Name  = "Tr";
00031 
00032   const int init_coils=2; // initial number of coils
00033   // must be the first property!
00034   Props.append(new Property("coils", QString::number(init_coils), false,
00035                 QObject::tr("number of mutual inductances")));
00036 
00037   for (int i=1;i<=init_coils; i++) {
00038       Props.append(new Property("L"+QString::number(i), "1 mH", false,
00039                                 QObject::tr("inductance of coil") + " " + QString::number(i)));
00040   }
00041 
00042   for(int i = 1; i < init_coils; i++)
00043     for(int j = i+1; j <= init_coils; j++) {
00044        QString nam = "k" + QString::number(i) + QString::number(j);
00045        QString desc = QObject::tr("coupling factor between coil %1 and coil %2").arg(i).arg(j);
00046        Props.append(new Property(nam,"0.9",false,desc));
00047     }
00048 
00049   createSymbol();
00050 }
00051 
00052 // --------------------------------------------------------
00053 Element* MutualX::info(QString& Name, char* &BitmapFile, bool getNewOne)
00054 {
00055   Name = QObject::tr("N Mutual Inductors");
00056   BitmapFile = (char *) "mutualx";
00057 
00058   if(getNewOne) {
00059       MutualX* p =  new MutualX();
00060       p->Props.at(0)->Value = "2";
00061       p->recreate(0);
00062       return p;
00063   }
00064   return 0;
00065 }
00066 
00067 Component* MutualX::newOne()
00068 {
00069     MutualX *p  = new MutualX();
00070     p->Props.at(0)->Value=Props.at(0)->Value;
00071     p->recreate(0);
00072     return p;
00073 }
00074 
00075 QString MutualX::netlist()
00076 {
00077     QString s = Model + ":" + Name;
00078 
00079     // output all node names
00080     foreach(Port *p1, Ports) {
00081       s += " "+p1->Connection->Name;   // node names
00082     }
00083 
00084     int coils = Props.at(0)->Value.toInt();
00085 
00086     QString L="";
00087     for (int i=0;i<coils;i++) {
00088         L += Props.at(i+1)->Value + ";";
00089     }
00090     L.chop(1);
00091 
00092     QString k="";
00093 
00094     QString **k_matrix = new QString*[coils];
00095     for (int i=0;i<coils;i++) {
00096         k_matrix[i]= new QString[coils];
00097     }
00098 
00099 
00100     for (int i=0, state=1;i<coils;i++) {
00101         for (int j=i;j<coils;j++,state++) {
00102             if (i==j) {
00103                 k_matrix[i][j] = "1.0"; // for self-inductance
00104                 state--;
00105             } else {
00106                 k_matrix[i][j] = Props.at(coils+state)->Value; // for mutual inductances
00107             }
00108 
00109         }
00110         for (int j=0;j<i;j++) {
00111             k_matrix[i][j]= k_matrix[j][i];
00112         }
00113     }
00114 
00115     for (int i=0;i<coils;i++) {
00116         for (int j=0;j<coils;j++) {
00117             k += k_matrix[i][j]+";";
00118         }
00119     }
00120     k.chop(1);
00121 
00122     for (int i=0;i<coils;i++) {
00123         delete [] k_matrix[i];
00124     }
00125     delete [] k_matrix;
00126 
00127 
00128     s += QString(" L=\"[%1]\" k=\"[%2]\"\n").arg(L,k);
00129 
00130     return s;
00131 }
00132 
00133 // --------------------------------------------------------
00134 void MutualX::createSymbol()
00135 {
00136   // adjust port number
00137   int Num = Props.first()->Value.toInt();
00138   if(Num < 2)
00139     Num = 2;
00140   else if(Num > 8)
00141     Num = 8;
00142   Props.first()->Value = QString::number(Num);
00143 
00144   int NumProps,oldNumProps;
00145   oldNumProps = Props.count();
00146   NumProps = Num + Num * (Num - 1) / 2 + 1;
00147   if (oldNumProps!=NumProps) { // Coils count was changed
00148     int oldCoils = rint(0.5*(sqrt(8*oldNumProps-7)-1.0)); // calculate old number of coils
00149     // we need to solve quadratic equation
00150     int dCoils = abs(oldCoils - Num);          // how many coils were added/removed?
00151 
00152     if (oldCoils>Num) { // reduce coils number
00153       for(int i = 0; i < dCoils; i++)
00154         Props.remove(Num+1); // remove excess coils
00155       // remove only the no longer valid coupling coefficients, leave the
00156       //   ones related to existing coils untouched
00157       for(int i = 1,state=1; i < oldCoils; i++)
00158         for(int j = i+1; j <= oldCoils; j++,state++) {
00159             if ((i>Num)||(j>Num)) {
00160                 Props.remove(Num + state);
00161                 state--;
00162             }
00163         }
00164 
00165     } else { // add new coils
00166       for(int i = 0; i < dCoils; i++) { // add new properties for coils
00167         Props.insert(oldCoils+1, new Property("L"+QString::number(Num-i), 
00168                 "1 mH", 
00169                 false, 
00170                 QObject::tr("inductance of coil") + " " + QString::number(Num-i)));
00171       }
00172 
00173       for(int i = 1,state=1; i < Num; i++)
00174         for(int j = i+1; j <= Num; j++,state++) {
00175             if ((i>oldCoils)||(j>oldCoils)) {
00176                 Props.insert(Num + state, new Property("k", "0.9", false, " "));
00177             }
00178         }
00179 
00180     }
00181   }
00182 
00183   // in any case rewrite properties Name and Description
00184   // (when loading a component, added properties have a default name)
00185   // adjust coils names
00186   Property * p1 = Props.at(1);
00187   for(int i = 1; i <= Num; i++) {
00188     p1->Name = "L"+QString::number(i);
00189     p1->Description = QObject::tr("inductance of coil") + " " + QString::number(i);
00190     p1 = Props.next();
00191   }
00192   // adjust coupling coeffs names
00193   for(int i = 1,state=1; i < Num; i++) 
00194     for(int j = i+1; j <= Num; j++,state++) {
00195       Props.at(Num+state)->Name = "k" + QString::number(i) + QString::number(j);
00196       Props.at(Num+state)->Description =
00197         QObject::tr("coupling factor between coil %1 and coil %2").arg(i).arg(j);
00198     }
00199 
00200   // draw symbol
00201   int x = -10 * (Num-1);
00202   Texts.append(new Text(x-9,-22,"1"));
00203 
00204   x1 = x-6;  y1 = -30;
00205   x2 = 10-x; y2 =  30;
00206 
00207   tx = x2+4;
00208   ty = y1+4;
00209 
00210   x -= 6;
00211   for(int i=0; i<Num; i++) {
00212     Arcs.append(new Arc(x,-18,12,12, 16*270,16*180, QPen(Qt::darkBlue,2)));
00213     Arcs.append(new Arc(x, -6,12,12, 16*270,16*180, QPen(Qt::darkBlue,2)));
00214     Arcs.append(new Arc(x,  6,12,12, 16*270,16*180, QPen(Qt::darkBlue,2)));
00215 
00216     x += 6;
00217     Lines.append(new Line(x,-18,x,-30,QPen(Qt::darkBlue,2)));
00218     Lines.append(new Line(x, 18,x, 30,QPen(Qt::darkBlue,2)));
00219 
00220     Ports.append(new Port(x,-30));
00221     Ports.append(new Port(x, 30));
00222     x += 14;
00223   }
00224 }
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines