My Project  0.0.16
QUCS Mapping
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
transline.cpp
Go to the documentation of this file.
1 /*
2  * transline.cpp - base for a transmission line implementation
3  *
4  * Copyright (C) 2005 Stefan Jahn <stefan@lkcc.org>
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this package; see the file COPYING. If not, write to
18  * the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  *
21  */
22 
23 #include "qucstrans.h"
24 #include "transline.h"
25 #include "units.h"
26 
27 // Unit conversion array for length.
28 static double conv_length[7][7] = {
29  { 1.0, 2.54e-3, 2.54e-2, 2.54e-5, 25.4, 1.e-3, 1./12000},
30  {1./2.54e-3, 1.0, 10.0, 1.e-2, 1.e4, 1./2.54, 1./30.48},
31  {1./2.54e-2, 1./10., 1.0, 1.e-3, 1.e3, 1./25.4, 1./304.8},
32  {1./2.54e-5, 1.e2, 1.e3, 1.0, 1.e6, 1./2.54e-2, 1./0.3048},
33  {1./25.4, 1.e-4, 1.e-3, 1.e-6, 1.0, 1./2.54e4, 1./3.048e5},
34  {1.e3, 2.54, 25.4, 2.54e-2, 2.54e4, 1.0, 1./12.},
35  {1.2e4, 30.48, 304.8, 0.3048, 3.048e5, 12.0, 1.0}
36 };
37 
38 // Unit conversion array for frequencies.
39 static double conv_freq[4][4] = {
40  { 1.0, 1.e9, 1.e6, 1.e3},
41  { 1.e-9, 1.0, 1.e-3, 1.e-6},
42  { 1.e-6, 1.e3, 1.0, 1.e-3},
43  { 1.e-3, 1.e6, 1.e3, 1.0}
44 };
45 
46 // Unit conversion array for resistances.
47 static double conv_res[2][2] = {
48  {1.0, 1.e-3},
49  {1.e3, 1.0}
50 };
51 
52 // Unit conversion array for angles.
53 static double conv_ang[2][2] = {
54  {1.0, M_PI/180.0},
55  {180.0/M_PI, 1.0}
56 };
57 
58 /* Constructor creates a transmission line instance. */
60  app = 0;
61  mur = 1.0;
62 }
63 
64 /* Destructor destroys a transmission line instance. */
66 }
67 
68 /* Sets the application instance. */
70  app = a;
71 }
72 
73 /* Sets a named property to the given value, access through the
74  application. */
75 void transline::setProperty (const char * prop, double value) {
76  app->setProperty (prop, value);
77 }
78 
79 /* Sets a named property to a given value. Depending on the source
80  and destination unit the value gets previously converted. */
81 void transline::setProperty (const char * prop, double value, int type,
82  int srcunit) {
83  int dstunit = translateUnit (getUnit (prop));
84  if (type == UNIT_LENGTH)
85  value *= conv_length[srcunit][dstunit];
86  else if (type == UNIT_RES)
87  value *= conv_res[srcunit][dstunit];
88  else if (type == UNIT_ANG)
89  value *= conv_ang[srcunit][dstunit];
90  else if (type == UNIT_FREQ)
91  value *= conv_freq[srcunit][dstunit];
92  setProperty (prop, value);
93 }
94 
95 /* Converts the given value/unit pair into a text representation and
96  puts this into the given result line. */
97 void transline::setResult (int line, double value, const char * unit) {
98  char text[256];
99  sprintf (text, "%g %s", value, unit);
100  app->setResult (line, text);
101 }
102 
103 /* Puts the text into the given result line. */
104 void transline::setResult (int line, const char * text) {
105  app->setResult (line, text);
106 }
107 
108 /* Returns a named property value. */
109 double transline::getProperty (const char * prop) {
110  return app->getProperty (prop);
111 }
112 
113 /* Returns a named property selection. */
114 bool transline::isSelected (const char * prop) {
115  return app->isSelected (prop);
116 }
117 
118 /* Returns a named property value. Depending on the source and
119  destination unit the actual value is converted. */
120 double transline::getProperty (const char * prop, int type, int dstunit) {
121  int srcunit = translateUnit (getUnit (prop));
122  double value = getProperty (prop);
123  if (type == UNIT_LENGTH)
124  return value * conv_length[srcunit][dstunit];
125  else if (type == UNIT_RES)
126  return value * conv_res[srcunit][dstunit];
127  else if (type == UNIT_ANG)
128  return value * conv_ang[srcunit][dstunit];
129  else if (type == UNIT_FREQ)
130  return value * conv_freq[srcunit][dstunit];
131  return value;
132 }
133 
134 /* The function converts the given value depending on the requested
135  unit and its source unit. */
136 double transline::convertProperty (const char * prop, double value, int type,
137  int srcunit) {
138  int dstunit = translateUnit (getUnit (prop));
139  if (type == UNIT_LENGTH)
140  value *= conv_length[srcunit][dstunit];
141  else if (type == UNIT_RES)
142  value *= conv_res[srcunit][dstunit];
143  else if (type == UNIT_ANG)
144  value *= conv_ang[srcunit][dstunit];
145  else if (type == UNIT_FREQ)
146  value *= conv_freq[srcunit][dstunit];
147  return value;
148 }
149 
150 /* Returns the unit of the given property. */
151 char * transline::getUnit (const char * prop) {
152  return app->getUnit (prop);
153 }
154 
155 /* The function translates the given textual unit into an
156  identifier. */
157 int transline::translateUnit (char * text) {
158  if (!strcmp (text, "mil")) return LENGTH_MIL;
159  else if (!strcmp (text, "cm")) return LENGTH_CM;
160  else if (!strcmp (text, "mm")) return LENGTH_MM;
161  else if (!strcmp (text, "m")) return LENGTH_M;
162  else if (!strcmp (text, "um")) return LENGTH_UM;
163  else if (!strcmp (text, "in")) return LENGTH_IN;
164  else if (!strcmp (text, "ft")) return LENGTH_FT;
165 
166  else if (!strcmp (text, "GHz")) return FREQ_GHZ;
167  else if (!strcmp (text, "Hz")) return FREQ_HZ;
168  else if (!strcmp (text, "kHz")) return FREQ_KHZ;
169  else if (!strcmp (text, "MHz")) return FREQ_MHZ;
170 
171  else if (!strcmp (text, "Ohm")) return RES_OHM;
172  else if (!strcmp (text, "kOhm")) return RES_KOHM;
173 
174  else if (!strcmp (text, "Deg")) return ANG_DEG;
175  else if (!strcmp (text, "Rad")) return ANG_RAD;
176  return -1;
177 }
178 
179 /*
180  * skin_depth - calculate skin depth
181  */
183 {
184  double depth;
185  depth = 1.0 / (sqrt(M_PI * f * mur * MU0 * sigma));
186  return depth;
187 }