Imported Upstream version 4.6.0
[debian/atlc] / src / non_gui / convert_create_bmp_for_circ_in_rect_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 #include "config.h"
25
26 #ifdef HAVE_STRING_H
27 #include <string.h>
28 #endif
29
30 #ifdef HAVE_STRINGS_H
31 #include <strings.h>
32 #endif
33
34 #ifdef HAVE_STDLIB_H
35 #include <stdlib.h>
36 #endif
37 #include "definitions.h"
38 #include "exit_codes.h"
39
40
41 /* names, colours and Ers are all arrays of 10. It would be better they were 
42 in a structure as they are all linked closely, but they are not and I
43 can't be bothered to change it */
44
45 extern double dd, WW, HH, xx, yy, Er1;
46 extern int d, W, H, x, y;
47 extern int lowest;
48 extern int highest;
49 extern int verbose;
50
51 void convert_create_bmp_for_circ_in_rect_dimensions_to_integers(int accuracy_level) 
52 {
53    int best_d=-1, best_W=-1, best_H=-1, best_x=-1, best_y=-1;
54    int min, max;
55    int i;
56    double max_gridsize, min_gridsize;
57    double error, error_min=VERY_LARGE, gridsize, best_grid_size=-1;
58
59    /* scale grid size,  according to a command line option */
60    lowest*=1<<accuracy_level;
61    highest*=1<<accuracy_level;
62
63    max_gridsize=sqrt(WW*HH/(double )lowest);  /* minimum dimension in m */
64    min_gridsize=sqrt(WW*HH/(double )highest);
65    if(W>=H)
66    {
67       min=WW/max_gridsize;
68       max=WW/min_gridsize;
69    }
70    else
71    {
72       min=HH/max_gridsize;
73       max=HH/min_gridsize;
74    }
75    for(i=min;i<=max;++i)
76    {
77       /* Try various combinations for  d, W, H, etc */
78       if(W>H)
79          gridsize=WW/i;
80       else
81          gridsize=HH/i;
82       d=(int)(dd/gridsize + 0.5);
83       W=(int)(WW/gridsize + 0.5);
84       H=(int)(HH/gridsize + 0.5);
85       x=(int)(xx/gridsize + 0.5);
86       y=(int)(yy/gridsize + 0.5);
87       error=0.0;
88       error+=pow((WW-W*gridsize)/WW,2.0); /* relative error in W */
89       error+=pow((HH-H*gridsize)/HH,2.0);
90       error+=pow((dd-d*gridsize)/dd,2.0);
91       if(x>0)
92          error+=pow((xx-x*gridsize)/xx,2.0);
93       if(y>0)
94           error+=pow((yy-y*gridsize)/yy,2.0);
95       if(error < (error_min-TINY))
96       {
97           error_min=error;
98           best_d=d;
99           best_W=W;
100           best_H=H;
101           best_x=x;
102           best_y=y;
103           best_grid_size=gridsize;
104       }
105    }
106    if(best_d==-1) /* it has not been initialised */
107       exit_with_msg_and_exit_code("d has not been initialised",VARIABLE_NOT_INITIALISED);
108    else
109      d=best_d;
110    if(best_W==-1) /* it has not been initialised */
111       exit_with_msg_and_exit_code("W has not been initialised",VARIABLE_NOT_INITIALISED);
112    else
113      W=best_W;
114    if(best_H==-1) /* it has not been initialised */
115       exit_with_msg_and_exit_code("H has not been initialised",VARIABLE_NOT_INITIALISED);
116    else
117      H=best_H;
118    if(best_x==-1) /* it has not been initialised */
119       exit_with_msg_and_exit_code("x has not been initialised",VARIABLE_NOT_INITIALISED);
120    else
121      x=best_x;
122    if(best_y==-1) /* it has not been initialised */
123       exit_with_msg_and_exit_code("y has not been initialised",VARIABLE_NOT_INITIALISED);
124    else
125      y=best_y;
126    if(verbose==TRUE)
127    {
128       fprintf(stderr,"error_min=%.16f\n",error_min);
129       fprintf(stderr,"User requested: dd=%f WW=%f HH=%f xx=%f yy=%f Er=%f\n\n",dd,WW,HH,xx,yy,Er1);
130       fprintf(stderr,"Internally the program is using the following grid:\n");
131       fprintf(stderr,"d=%d W=%d H=%d x=%d y=%d \n", d, W, H, x, y);
132       fprintf(stderr,"The  grid size is %f mm, inches or whatever\n\n", best_grid_size);
133       if(error_min > TINY)
134       {
135          fprintf(stderr,"This means we are simulating a transmission line with these dimensions:\n");
136          fprintf(stderr,"d=%f W=%f H=%f x=%f y=%f (mm, inches or whatever)\n",d*best_grid_size,W*best_grid_size,H*best_grid_size,x*best_grid_size,y*best_grid_size);
137          fprintf(stderr,"\nThese are slightly different to what you indicated on the command line,\n");
138          fprintf(stderr,"but they are the best approximation possible, given the grid size\n");
139       }
140    }
141    if( (d/2+x >= W) || (d/2+y >=H))
142    {
143       exit_with_msg_and_exit_code("The gap between the two conductors is too small. Either increase the bitmap size (-b option), or change the dimensions of one the conductors",GAP_BETWEEN_CONDUCTORS_TOO_SMALL);
144    }
145    check_error(dd,d,best_grid_size,"d");
146    check_error(WW,W,best_grid_size,"W");
147    check_error(HH,H,best_grid_size,"H");
148    check_error(xx,x,best_grid_size,"x");
149    check_error(yy,y,best_grid_size,"y");
150 }