Imported Upstream version 4.6.0
[debian/atlc] / src / non_gui / find_optimal_dimensions_for_microstrip_coupler.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_STDLIB_H
27 #include <stdlib.h>
28 #endif  
29
30 #ifdef HAVE_STRING_H
31 #include <string.h>
32 #endif
33
34 #include "definitions.h"
35 #include "exit_codes.h"
36
37 extern char *my_optarg;
38 int main(int argc, char **argv)
39 {
40   double g, gmin=0.5, gmax=5, gstep=0.5;  
41   double s, smin=0.5, smax=5, sstep=0.5;
42   double w, wmin=0.5, wmax=5, wstep=0.5;
43   double Er1=1, Er2=1, h=1, t=1; /* keep the compiler from issuing warnings */   
44   double Zcomm, Zdiff, Zodd, Zeven;  
45   double Er_odd, Er_even, Zo;
46   double error_min=1e100, error; 
47   double ideal_Zodd=0, ideal_Zeven=0; 
48   char *outfile_name, *temporary_bmp_file, *temporary_txt_file;
49   int x, q, exit_code;
50   char *cmd, *null;
51   FILE *fp, *fp_best;
52   outfile_name = string(0,4000);
53   cmd = string(0,4000);
54   null = string(0,4000);
55   temporary_bmp_file = string(0,4000);
56   temporary_txt_file = string(0,4000);
57   while((q=get_options(argc,argv,"g:b:G:w:W:s:S:")) != -1)
58   switch (q) 
59   {
60     case 'C':
61       print_copyright( (char *) "1996-2002");
62       exit_with_msg_and_exit_code("",OKAY);
63     break;
64     case 'g':
65       gmin=atof(my_optarg);
66     break;
67     /*
68     case 'b':
69       bmp_size=atoi(my_optarg);
70     break;
71     */
72     case 'G':
73       gmax=atof(my_optarg);
74     break;
75     case 'h':
76       gstep=atof(my_optarg);
77     break;
78     case 's':
79       smin=atof(my_optarg);
80     break;
81     case 'S':
82       smax=atof(my_optarg);
83     break;
84     case 't':
85       sstep=atof(my_optarg);
86     break;
87     case 'w':
88       wmin=atof(my_optarg);
89     break;
90     case 'W':
91       wmax=atof(my_optarg);
92     break;
93     case 'x':
94       wstep=atof(my_optarg);
95     break;
96     case '?':
97       usage_find_optimal_dimensions_for_microstrip_coupler();
98     break;
99   }
100   if(argc-my_optind == 7)
101   {
102     h=atof(argv[my_optind]);
103     t=atof(argv[my_optind+1]);
104     Er1=atof(argv[my_optind+2]);
105     Er2=atof(argv[my_optind+3]);
106     ideal_Zodd=atof(argv[my_optind+4]);
107     ideal_Zeven=atof(argv[my_optind+5]);
108     check_parameters_for_find_optimal_dimensions_for_microstrip_coupler(h,t,Er1,Er2,ideal_Zodd,ideal_Zeven);
109     (void) strcpy(outfile_name, argv[my_optind+6]);
110   }
111   else
112    usage_find_optimal_dimensions_for_microstrip_coupler(); 
113   (void) strcpy(temporary_bmp_file,"1.bmp");
114   (void) strcpy(temporary_txt_file,"1.txt");
115   if( (exit_code=pclose(popen("rm 1.txt 1.bmp","w"))) != 0)
116   {
117     fprintf(stderr,"rm 1.txt 1.bmp did not returned an exit code of %d\n",exit_code);
118     exit(exit_code);
119   }
120   for(g=gmin; g <=gmax; g+=gstep)
121   {
122     for(w=wmin; w<=wmax; w+=wstep)
123     {
124       for(s=smin; s<=smax; s+=sstep)
125       {
126         sprintf(cmd,"create_bmp_for_microstrip_coupler %f %f %f %f %f %f %f %s\n",w, s, g, h, t, Er1, Er2,temporary_bmp_file);
127         if( (exit_code=pclose(popen(cmd,"w"))) != 0)
128         {
129           fprintf(stderr,"pclose(popen(%s,\"w\"))) returned with an exit code of %d\n",cmd, exit_code);
130           exit(exit_code);
131         }
132         sprintf(cmd,"atlc -S -s %s > %s\n", temporary_bmp_file, temporary_txt_file);
133         exit_code=pclose(popen(cmd,"w"));
134         if( (exit_code=pclose(popen(cmd,"w"))) != 0)
135         {
136           fprintf(stderr,"pclose(popen(%s,\"w\"))) returned with an exit code of %d\n",cmd,exit_code);
137           exit(exit_code);
138         }
139         if ((fp=fopen(temporary_txt_file,"r")) ==NULL)
140           exit_with_msg_and_exit_code("Error #1 cant't open file in find_optimal_dimensions_for_microstrip_coupler.c", CANT_OPEN_FILE_FOR_READING);
141         fscanf(fp,"%s %d %s %lf %s %lf %s %lf %s %lf %s %lf %s %lf %s %lf",null,&x,null,&Er_odd,null,&Er_even, null, &Zodd,null,&Zeven,null,&Zo, null, &Zdiff,null,&Zcomm);
142         if (fclose(fp) !=0)
143           exit_with_msg_and_exit_code("Error #2 Unable to close file in ind_optimal_dimensions_for_microstrip_coupler.c",CANT_CLOSE_FILE);
144         printf("x=%d Er_odd=%f Er_even=%f Zodd=%f Zeven=%f Zo=%f Zdiff=%f Zcomm=%f\n",x, Er_odd,Er_even,Zodd, Zeven,Zo, Zdiff, Zcomm);
145         error=fabs(Zodd-ideal_Zodd)/ideal_Zodd+fabs(Zeven-ideal_Zeven)/ideal_Zeven;
146         /* By forcing the error to be a be not just bettter, but better by at 
147         least TINY, it means the results will be the same on differerent computers,
148         with no difference due to rouunding errors */
149         if(error< error_min+TINY)
150         {
151           error_min=error;
152           printf("best so far = s=%f w=%f g=%f Zodd=%f Zeven=%f Zdiff=%f Zcomm=%f error=%f\n",s,w,g, Zodd, Zeven,Zdiff, Zcomm,error);
153           if ((fp_best=fopen(outfile_name,"a")) == NULL)
154             exit_with_msg_and_exit_code("Error #3 Can't open file output file update",CANT_OPEN_FILE_FOR_APPENDING);
155           fprintf(fp_best,"best so far = s=%f w=%f g=%f Zodd=%f Zeven=%f Zdiff=%f Zcomm=%f\n",s,w,g, Zdiff, Zcomm,Zodd, Zeven);
156           if (fclose(fp_best)  != 0)
157             exit_with_msg_and_exit_code("Error #4 Unable to close file in optimal_dimensions_for_microstrip_coupler.c",CANT_CLOSE_FILE);
158         } /* end of if */
159       } /* end of for s */
160     } /* end of for w */
161   } /* end of for g */
162   return(0);
163 }
164         
165