1 /* atlc - arbitrary transmission line calculator, for the analysis of
2 transmission lines are directional couplers.
4 Copyright (C) 2002. Dr. David Kirkby, PhD (G8WRB).
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 package_version 2
9 of the License, or (at your option) any later package_version.
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.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
21 Dr. David Kirkby, e-mail drkirkby at ntlworld.com
25 /* The following calculates the odd and even mode impedances between two
26 zero thickness strips of width w, spaced a distance s between two
27 groundplanes of spacing h. */
30 #include "definitions.h"
36 double calculate_integer_values(struct transmission_line_properties *optimise,int nmax,int accuracy_level)
38 double grid_size, error, error_max=1e6;
39 int i, min_pixels, max_pixels, n, min_critical_pixels, max_critical_pixels;
40 int most_critical=0, step=1;
41 double min_pixel_size, max_pixel_size;
45 optimise->best[i]=-1; /* A silly value, indicating it is not acceptable */
47 /* Let the number of pixel in the image be between 2^n and 2^(n+2) */
49 min_pixels=(int) pow(2.0,(double)(accuracy_level-1));
50 max_pixels=(int) pow(2.0,(double)(accuracy_level+1));
52 /* Although the stucture W by H has an exact area of W*H, we will
53 allocate some pixels to this, which will be much smaller than
54 the W*H. Each pixel will have a variable area, which we don't
55 know yet, but can put upper and lower limits on it.
58 W=optimise->float_values[0]; /* The calculated W and H */
59 H=optimise->float_values[1];
60 min_pixel_size=sqrt(W*H/max_pixels);
61 max_pixel_size=sqrt(W*H/min_pixels);
63 /* find the most critical dimension */
66 if(optimise->importance[n]==MOST_IMPORTANT)
69 /* we will allocate a number of pixels to this most critical
72 min_critical_pixels = optimise->float_values[most_critical]/max_pixel_size;
73 max_critical_pixels = optimise->float_values[most_critical]/min_pixel_size;
76 /* Normally we would try every combination of i, but it may be necessary to keep it even or odd */
77 if(optimise->odd_or_even[most_critical] == ODD && min_critical_pixels%2==0)
78 min_critical_pixels--;
79 else if(optimise->odd_or_even[most_critical] == EVEN && min_critical_pixels%2==1)
80 min_critical_pixels--;
81 if(optimise->odd_or_even[most_critical] != DONT_CARE)
84 for(i=min_critical_pixels; i<max_critical_pixels;i+=step)
86 /* set the most critical dimension to i pixels, trying every i
87 between two set limits */
89 optimise->int_values[most_critical]=i;
90 grid_size=optimise->float_values[most_critical]/i;
92 /* Now set all the others to the optimal, assuming i would be used
93 for the most critical dimension */
97 optimise->int_values[n]=(int) (0.5+optimise->float_values[n]/grid_size);
98 if (optimise->importance[n] != NOT_IMPORTANT)
100 error+=fabs((double)optimise->int_values[n]*grid_size-optimise->float_values[n]);
103 /* Since the numbers generated for the integers seeme to differ on different
104 computers, I've suspected the problem might be that an error is very
105 similar at two different sets of integer values. Hence I will only
106 consider the error lower its lower by 1e-9. That should avoid silly
107 rounding problems. */
109 if(error<(error_max-TINY)) /* The 1e-9 avoids different results on different*/
110 { /* machines due to rounding errors */
113 optimise->best[n]=optimise->int_values[n];
116 return(error_max); /* return the error */