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
34 #include "definitions.h"
35 #include "exit_codes.h"
37 extern char *my_optarg;
38 int main(int argc, char **argv)
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;
52 outfile_name = 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)
61 print_copyright( (char *) "1996-2002");
62 exit_with_msg_and_exit_code("",OKAY);
69 bmp_size=atoi(my_optarg);
76 gstep=atof(my_optarg);
85 sstep=atof(my_optarg);
94 wstep=atof(my_optarg);
97 usage_find_optimal_dimensions_for_microstrip_coupler();
100 if(argc-my_optind == 7)
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]);
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)
117 fprintf(stderr,"rm 1.txt 1.bmp did not returned an exit code of %d\n",exit_code);
120 for(g=gmin; g <=gmax; g+=gstep)
122 for(w=wmin; w<=wmax; w+=wstep)
124 for(s=smin; s<=smax; s+=sstep)
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)
129 fprintf(stderr,"pclose(popen(%s,\"w\"))) returned with an exit code of %d\n",cmd, exit_code);
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)
136 fprintf(stderr,"pclose(popen(%s,\"w\"))) returned with an exit code of %d\n",cmd,exit_code);
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);
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)
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);