Imported Upstream version 4.6.0
[debian/atlc] / src / non_gui / convert_create_bmp_for_circ_in_circ_dimensions_to_integers.c
1 /* atlc - arbitrary transmission line calculator, for the analysis of
2 transmission lines are directional couplers. 
3
4 Copyright (C) 2002. Dr. David Kirkby, PhD (G8WRB).
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 package_version 2
9 of the License, or (at your option) any later package_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 program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
19 USA.
20
21 Dr. David Kirkby, e-mail drkirkby at ntlworld.com 
22
23 */
24
25 #include "config.h"
26
27 #ifdef HAVE_STRING_H
28 #include <string.h>
29 #endif
30
31 #ifdef HAVE_STRINGS_H
32 #include <strings.h>
33 #endif
34
35 #ifdef HAVE_STDLIB_H
36 #include <stdlib.h>
37 #endif
38 #include "definitions.h"
39
40 /* names, colours and Ers are all arrays of 10. It would be better they were 
41 in a structure as they are all linked closely, but they are not and I
42 can't be bothered to change it */
43
44 extern double DD, dd, hh, Er1;
45 extern int D, d, h, W, H;
46 extern int lowest;
47 extern int highest;
48 extern int verbose;
49
50 void convert_create_bmp_for_circ_in_circ_dimensions_to_integers(int accuracy_level) 
51 {
52    int best_D=-1, best_d=-1, best_h=-1;
53    int min, max;
54    int i;
55    double max_gridsize, min_gridsize;
56    double error, error_min=VERY_LARGE, gridsize, best_grid_size=-1;
57
58    /* scale grid size,  according to a command line option */
59    lowest*=(int) 0.5+sqrt(pow(2.0,(double)accuracy_level));
60    highest*=(int) 0.5+sqrt(pow(2.0,(double)accuracy_level));
61
62    max_gridsize=DD/lowest;  /* minimum dimension in m */
63    min_gridsize=DD/highest;
64    min=DD/max_gridsize;
65    max=DD/min_gridsize;
66    for(i=min;i<=max;++i)
67    {
68       /* Try various combinations for  D,d,o */
69       gridsize=DD/i;
70       D=(int)(DD/gridsize + 0.5);
71       d=(int)(dd/gridsize + 0.5);
72       h=(int)(hh/gridsize + 0.5);
73       error=0.0;
74       error+=pow((DD-D*gridsize)/DD,2.0);
75       error+=pow((dd-d*gridsize)/dd,2.0);
76       if(h>0)
77           error+=pow((hh-h*gridsize)/hh,2.0);
78       if(error < (error_min-TINY))
79       {
80           error_min=error;
81           best_D=D;
82           best_d=d;
83           best_h=h;
84           best_grid_size=gridsize;
85       }
86    }
87    D=W=H=best_D;
88    d=best_d;
89    h=best_h;
90    if(verbose==TRUE)
91    {
92       fprintf(stderr,"error_min=%.16f\n",error_min);
93       fprintf(stderr,"User requested: DD=%f dd=%f hh=%f Er1=%f\n\n",DD,dd,hh,Er1);
94       fprintf(stderr,"Internally the program is using the following grid:\n");
95       fprintf(stderr,"D=%d d=%d h=%d \nThe  grid size is %f mm, inches or whatever\n\n", D,d,h,best_grid_size);
96       if(error_min > TINY)
97       {
98          fprintf(stderr,"This means we are simulating a transmission line with these dimensions:\n");
99          fprintf(stderr,"D=%f d=%f h=%f (mm, inches or whatever)\n",D*best_grid_size,d*best_grid_size,h*best_grid_size);
100          fprintf(stderr,"\nThese are slightly different to what you indicated on the command line,\n");
101          fprintf(stderr,"but they are the best approximation possible, given the grid size\n");
102       }
103    }
104    check_error(DD,D,best_grid_size,"D");
105    check_error(dd,d,best_grid_size,"d");
106    if(h > 0.0)
107       check_error(hh,h,best_grid_size,"h");
108 }
109